SQLite Archiver

Check-in [72b6147da3]
Login

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

Overview
Comment:Update the built-in SQLite to the latest 3.15.0 alpha version.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 72b6147da3c69ad4c43517bcb6c11cb054ddb3d0
User & Date: drh 2016-09-19 12:10:56
Context
2016-09-19
12:39
Update to the latest 3.15.0 prerelease code. Improved SEE support, including a new SEE makefile. Other makefile improvements. check-in: 72ca977d53 user: drh tags: trunk
12:10
Update the built-in SQLite to the latest 3.15.0 alpha version. check-in: 72b6147da3 user: drh tags: trunk
2016-08-22
17:23
Add the -d option to delete content from the SQLAR. Accept GLOB patterns as filenames for -l, -e, and -d. check-in: b45aa9345e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to sqlar.c.

    22     22   #include <unistd.h>
    23     23   #include <dirent.h>
    24     24   #include <string.h>
    25     25   #include <assert.h>
    26     26   #include <ctype.h>
    27     27   
    28     28   /* Maximum length of a pass-phrase */
    29         -#define MX_PASSPHRASE  40
           29  +#define MX_PASSPHRASE  120
    30     30   
    31     31   /*
    32     32   ** Show a help message and quit.
    33     33   */
    34     34   static void showHelp(const char *argv0){
    35     35     fprintf(stderr, "Usage: %s [options] archive [files...]\n", argv0);
    36     36     fprintf(stderr,
................................................................................
   269    269       sqlite3_create_function(db, "name_on_list", 1, SQLITE_UTF8,
   270    270                              (char*)x, name_on_list, 0, 0);
   271    271     }else{
   272    272       sqlite3_create_function(db, "name_on_list", 1, SQLITE_UTF8,
   273    273                               0, alwaysTrue, 0, 0);
   274    274     }
   275    275     if( seeFlag ){
   276         -    char *zSql;
   277    276       char zPassPhrase[MX_PASSPHRASE+1];
   278    277   #ifndef SQLITE_HAS_CODEC
   279    278       printf("WARNING:  The passphrase is a no-op because this build of\n"
   280    279              "sqlar is compiled without encryption capabilities.\n");
   281    280   #endif
   282    281       memset(zPassPhrase, 0, sizeof(zPassPhrase));
   283    282       prompt_for_passphrase("passphrase: ", seeFlag>1, zPassPhrase);
   284         -    zSql = sqlite3_mprintf("PRAGMA key(%Q)", zPassPhrase);
   285         -    sqlite3_exec(db, zSql, 0, 0, 0);
   286         -    sqlite3_free(zSql);
          283  +#ifdef SQLITE_HAS_CODEC
          284  +    sqlite3_key_v2(db, "main", zPassPhrase, -1);
          285  +#endif
   287    286     }
   288    287     sqlite3_exec(db, "BEGIN", 0, 0, 0);
   289    288     sqlite3_exec(db, zSchema, 0, 0, 0);
   290    289   }
   291    290   
   292    291   /*
   293    292   ** Prepare the pStmt statement.

Changes to sqlarfs.c.

   229    229        "Options:\n"
   230    230        "   -e      Prompt for passphrase.  -ee to scramble the prompt\n"
   231    231     );
   232    232     exit(1);
   233    233   }
   234    234   
   235    235   /* Maximum length of a pass-phrase */
   236         -#define MX_PASSPHRASE  40
          236  +#define MX_PASSPHRASE  120
   237    237   
   238    238   /*
   239    239   ** Scramble substitution matrix:
   240    240   */
   241    241   static char aSubst[256];
   242    242   
   243    243   /*
................................................................................
   361    361     if( zMountPoint==0 ) showHelp(argv[0]);
   362    362     rc = sqlite3_open(zArchive, &g.db);
   363    363     if( rc!=SQLITE_OK ){
   364    364       fprintf(stderr, "Cannot open sqlar file [%s]\n", argv[1]);
   365    365       exit(1);
   366    366     }
   367    367     if( seeFlag ){
   368         -    char *zSql;
   369    368       char zPassPhrase[MX_PASSPHRASE+1];
   370    369   #ifndef SQLITE_HAS_CODEC
   371    370       printf("WARNING:  The passphrase is a no-op because this build of\n"
   372    371              "sqlar is compiled without encryption capabilities.\n");
   373    372   #endif
   374    373       memset(zPassPhrase, 0, sizeof(zPassPhrase));
   375    374       prompt_for_passphrase("passphrase: ", seeFlag>1, zPassPhrase);
   376         -    zSql = sqlite3_mprintf("PRAGMA key(%Q)", zPassPhrase);
   377         -    sqlite3_exec(g.db, zSql, 0, 0, 0);
   378         -    sqlite3_free(zSql);
          375  +#ifdef SQLITE_HAS_CODEC
          376  +    sqlite3_key_v2(db, "main", zPassPhrase, -1);
          377  +#endif
   379    378     }
   380    379     rc = sqlite3_exec(g.db, "SELECT 1 FROM sqlar LIMIT 1", 0, 0, 0);
   381    380     if( rc!=SQLITE_OK ){
   382    381       fprintf(stderr, "File [%s] is not an SQLite archive\n", argv[1]);
   383    382       exit(1);
   384    383     }
   385    384     g.uid = getuid();

Changes to sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.13.0.  By combining all the individual C code files into this 
            3  +** version 3.15.0.  By combining all the individual C code files into this 
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
    33     33   **    May you find forgiveness for yourself and forgive others.
    34     34   **    May you share freely, never taking more than you give.
    35     35   **
    36     36   *************************************************************************
    37     37   ** Internal interface definitions for SQLite.
    38     38   **
    39     39   */
    40         -#ifndef _SQLITEINT_H_
    41         -#define _SQLITEINT_H_
           40  +#ifndef SQLITEINT_H
           41  +#define SQLITEINT_H
    42     42   
    43     43   /* Special Comments:
    44     44   **
    45     45   ** Some comments have special meaning to the tools that measure test
    46     46   ** coverage:
    47     47   **
    48     48   **    NO_TEST                     - The branches on this line are not
................................................................................
    63     63   **                                  if it did occur.  
    64     64   **
    65     65   ** In all cases, the special comment must be enclosed in the usual
    66     66   ** slash-asterisk...asterisk-slash comment marks, with no spaces between the 
    67     67   ** asterisks and the comment text.
    68     68   */
    69     69   
           70  +/*
           71  +** Make sure the Tcl calling convention macro is defined.  This macro is
           72  +** only used by test code and Tcl integration code.
           73  +*/
           74  +#ifndef SQLITE_TCLAPI
           75  +#  define SQLITE_TCLAPI
           76  +#endif
           77  +
    70     78   /*
    71     79   ** Make sure that rand_s() is available on Windows systems with MSVC 2005
    72     80   ** or higher.
    73     81   */
    74     82   #if defined(_MSC_VER) && _MSC_VER>=1400
    75     83   #  define _CRT_RAND_S
    76     84   #endif
................................................................................
    93    101   **    May you find forgiveness for yourself and forgive others.
    94    102   **    May you share freely, never taking more than you give.
    95    103   **
    96    104   ******************************************************************************
    97    105   **
    98    106   ** This file contains code that is specific to MSVC.
    99    107   */
   100         -#ifndef _MSVC_H_
   101         -#define _MSVC_H_
          108  +#ifndef SQLITE_MSVC_H
          109  +#define SQLITE_MSVC_H
   102    110   
   103    111   #if defined(_MSC_VER)
   104    112   #pragma warning(disable : 4054)
   105    113   #pragma warning(disable : 4055)
   106    114   #pragma warning(disable : 4100)
   107    115   #pragma warning(disable : 4127)
   108    116   #pragma warning(disable : 4130)
................................................................................
   114    122   #pragma warning(disable : 4244)
   115    123   #pragma warning(disable : 4305)
   116    124   #pragma warning(disable : 4306)
   117    125   #pragma warning(disable : 4702)
   118    126   #pragma warning(disable : 4706)
   119    127   #endif /* defined(_MSC_VER) */
   120    128   
   121         -#endif /* _MSVC_H_ */
          129  +#endif /* SQLITE_MSVC_H */
   122    130   
   123    131   /************** End of msvc.h ************************************************/
   124    132   /************** Continuing where we left off in sqliteInt.h ******************/
   125    133   
   126    134   /*
   127    135   ** Special setup for VxWorks
   128    136   */
................................................................................
   278    286   ** on how SQLite interfaces are supposed to operate.
   279    287   **
   280    288   ** The name of this file under configuration management is "sqlite.h.in".
   281    289   ** The makefile makes some minor changes to this file (such as inserting
   282    290   ** the version number) and changes its name to "sqlite3.h" as
   283    291   ** part of the build process.
   284    292   */
   285         -#ifndef _SQLITE3_H_
   286         -#define _SQLITE3_H_
          293  +#ifndef SQLITE3_H
          294  +#define SQLITE3_H
   287    295   #include <stdarg.h>     /* Needed for the definition of va_list */
   288    296   
   289    297   /*
   290    298   ** Make sure we can call this stuff from C++.
   291    299   */
   292    300   #if 0
   293    301   extern "C" {
................................................................................
   302    310   #endif
   303    311   #ifndef SQLITE_API
   304    312   # define SQLITE_API
   305    313   #endif
   306    314   #ifndef SQLITE_CDECL
   307    315   # define SQLITE_CDECL
   308    316   #endif
          317  +#ifndef SQLITE_APICALL
          318  +# define SQLITE_APICALL
          319  +#endif
   309    320   #ifndef SQLITE_STDCALL
   310         -# define SQLITE_STDCALL
          321  +# define SQLITE_STDCALL SQLITE_APICALL
          322  +#endif
          323  +#ifndef SQLITE_CALLBACK
          324  +# define SQLITE_CALLBACK
          325  +#endif
          326  +#ifndef SQLITE_SYSAPI
          327  +# define SQLITE_SYSAPI
   311    328   #endif
   312    329   
   313    330   /*
   314    331   ** These no-op macros are used in front of interfaces to mark those
   315    332   ** interfaces as either deprecated or experimental.  New applications
   316    333   ** should not use deprecated interfaces - they are supported for backwards
   317    334   ** compatibility only.  Application writers should be aware that
................................................................................
   347    364   ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
   348    365   ** numbers used in [SQLITE_VERSION].)^
   349    366   ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
   350    367   ** be larger than the release from which it is derived.  Either Y will
   351    368   ** be held constant and Z will be incremented or else Y will be incremented
   352    369   ** and Z will be reset to zero.
   353    370   **
   354         -** Since version 3.6.18, SQLite source code has been stored in the
          371  +** Since [version 3.6.18] ([dateof:3.6.18]), 
          372  +** SQLite source code has been stored in the
   355    373   ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   356    374   ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   357    375   ** a string which identifies a particular check-in of SQLite
   358    376   ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   359    377   ** string contains the date and time of the check-in (UTC) and an SHA1
   360    378   ** hash of the entire source tree.
   361    379   **
   362    380   ** See also: [sqlite3_libversion()],
   363    381   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   364    382   ** [sqlite_version()] and [sqlite_source_id()].
   365    383   */
   366         -#define SQLITE_VERSION        "3.13.0"
   367         -#define SQLITE_VERSION_NUMBER 3013000
   368         -#define SQLITE_SOURCE_ID      "2016-05-18 10:57:30 fc49f556e48970561d7ab6a2f24fdd7d9eb81ff2"
          384  +#define SQLITE_VERSION        "3.15.0"
          385  +#define SQLITE_VERSION_NUMBER 3015000
          386  +#define SQLITE_SOURCE_ID      "2016-09-19 11:00:42 2401ea5acfeee8042489d1db38036ff86e8a6916"
   369    387   
   370    388   /*
   371    389   ** CAPI3REF: Run-Time Library Version Numbers
   372    390   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   373    391   **
   374    392   ** These interfaces provide the same information as the [SQLITE_VERSION],
   375    393   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   691    709   ** CAPI3REF: Extended Result Codes
   692    710   ** KEYWORDS: {extended result code definitions}
   693    711   **
   694    712   ** In its default configuration, SQLite API routines return one of 30 integer
   695    713   ** [result codes].  However, experience has shown that many of
   696    714   ** these result codes are too coarse-grained.  They do not provide as
   697    715   ** much information about problems as programmers might like.  In an effort to
   698         -** address this, newer versions of SQLite (version 3.3.8 and later) include
          716  +** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
          717  +** and later) include
   699    718   ** support for additional result codes that provide more detailed information
   700    719   ** about errors. These [extended result codes] are enabled or disabled
   701    720   ** on a per database connection basis using the
   702    721   ** [sqlite3_extended_result_codes()] API.  Or, the extended code for
   703    722   ** the most recent error can be obtained using
   704    723   ** [sqlite3_extended_errcode()].
   705    724   */
................................................................................
   754    773   #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
   755    774   #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
   756    775   #define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
   757    776   #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
   758    777   #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
   759    778   #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
   760    779   #define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
          780  +#define SQLITE_OK_LOAD_PERMANENTLY     (SQLITE_OK | (1<<8))
   761    781   
   762    782   /*
   763    783   ** CAPI3REF: Flags For File Open Operations
   764    784   **
   765    785   ** These bit values are intended for use in the
   766    786   ** 3rd parameter to the [sqlite3_open_v2()] interface and
   767    787   ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
................................................................................
  1283   1303   ** at the internal representation of an [sqlite3_mutex].  It only
  1284   1304   ** deals with pointers to the [sqlite3_mutex] object.
  1285   1305   **
  1286   1306   ** Mutexes are created using [sqlite3_mutex_alloc()].
  1287   1307   */
  1288   1308   typedef struct sqlite3_mutex sqlite3_mutex;
  1289   1309   
         1310  +/*
         1311  +** CAPI3REF: Loadable Extension Thunk
         1312  +**
         1313  +** A pointer to the opaque sqlite3_api_routines structure is passed as
         1314  +** the third parameter to entry points of [loadable extensions].  This
         1315  +** structure must be typedefed in order to work around compiler warnings
         1316  +** on some platforms.
         1317  +*/
         1318  +typedef struct sqlite3_api_routines sqlite3_api_routines;
         1319  +
  1290   1320   /*
  1291   1321   ** CAPI3REF: OS Interface Object
  1292   1322   **
  1293   1323   ** An instance of the sqlite3_vfs object defines the interface between
  1294   1324   ** the SQLite core and the underlying operating system.  The "vfs"
  1295   1325   ** in the name of the object stands for "virtual file system".  See
  1296   1326   ** the [VFS | VFS documentation] for further information.
................................................................................
  2187   2217   ** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
  2188   2218   ** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
  2189   2219   ** interface independently of the [load_extension()] SQL function.
  2190   2220   ** The [sqlite3_enable_load_extension()] API enables or disables both the
  2191   2221   ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
  2192   2222   ** There should be two additional arguments.
  2193   2223   ** When the first argument to this interface is 1, then only the C-API is
  2194         -** enabled and the SQL function remains disabled.  If the first argment to
         2224  +** enabled and the SQL function remains disabled.  If the first argument to
  2195   2225   ** this interface is 0, then both the C-API and the SQL function are disabled.
  2196   2226   ** If the first argument is -1, then no changes are made to state of either the
  2197   2227   ** C-API or the SQL function.
  2198   2228   ** The second parameter is a pointer to an integer into which
  2199   2229   ** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
  2200   2230   ** is disabled or enabled following this call.  The second parameter may
  2201   2231   ** be a NULL pointer, in which case the new setting is not reported back.
  2202   2232   ** </dd>
  2203   2233   **
         2234  +** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
         2235  +** <dd> ^This option is used to change the name of the "main" database
         2236  +** schema.  ^The sole argument is a pointer to a constant UTF8 string
         2237  +** which will become the new schema name in place of "main".  ^SQLite
         2238  +** does not make a copy of the new main schema name string, so the application
         2239  +** must ensure that the argument passed into this DBCONFIG option is unchanged
         2240  +** until after the database connection closes.
         2241  +** </dd>
         2242  +**
  2204   2243   ** </dl>
  2205   2244   */
         2245  +#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
  2206   2246   #define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
  2207   2247   #define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
  2208   2248   #define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
  2209   2249   #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
  2210   2250   #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
  2211   2251   
  2212   2252   
................................................................................
  3001   3041   #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  3002   3042   #define SQLITE_COPY                  0   /* No longer used */
  3003   3043   #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  3004   3044   
  3005   3045   /*
  3006   3046   ** CAPI3REF: Tracing And Profiling Functions
  3007   3047   ** METHOD: sqlite3
         3048  +**
         3049  +** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
         3050  +** instead of the routines described here.
  3008   3051   **
  3009   3052   ** These routines register callback functions that can be used for
  3010   3053   ** tracing and profiling the execution of SQL statements.
  3011   3054   **
  3012   3055   ** ^The callback function registered by sqlite3_trace() is invoked at
  3013   3056   ** various times when an SQL statement is being run by [sqlite3_step()].
  3014   3057   ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
................................................................................
  3027   3070   ** time is in units of nanoseconds, however the current implementation
  3028   3071   ** is only capable of millisecond resolution so the six least significant
  3029   3072   ** digits in the time are meaningless.  Future versions of SQLite
  3030   3073   ** might provide greater resolution on the profiler callback.  The
  3031   3074   ** sqlite3_profile() function is considered experimental and is
  3032   3075   ** subject to change in future versions of SQLite.
  3033   3076   */
  3034         -SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  3035         -SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
         3077  +SQLITE_API SQLITE_DEPRECATED void *SQLITE_STDCALL sqlite3_trace(sqlite3*,
         3078  +   void(*xTrace)(void*,const char*), void*);
         3079  +SQLITE_API SQLITE_DEPRECATED void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
  3036   3080      void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
         3081  +
         3082  +/*
         3083  +** CAPI3REF: SQL Trace Event Codes
         3084  +** KEYWORDS: SQLITE_TRACE
         3085  +**
         3086  +** These constants identify classes of events that can be monitored
         3087  +** using the [sqlite3_trace_v2()] tracing logic.  The third argument
         3088  +** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of
         3089  +** the following constants.  ^The first argument to the trace callback
         3090  +** is one of the following constants.
         3091  +**
         3092  +** New tracing constants may be added in future releases.
         3093  +**
         3094  +** ^A trace callback has four arguments: xCallback(T,C,P,X).
         3095  +** ^The T argument is one of the integer type codes above.
         3096  +** ^The C argument is a copy of the context pointer passed in as the
         3097  +** fourth argument to [sqlite3_trace_v2()].
         3098  +** The P and X arguments are pointers whose meanings depend on T.
         3099  +**
         3100  +** <dl>
         3101  +** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
         3102  +** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
         3103  +** first begins running and possibly at other times during the
         3104  +** execution of the prepared statement, such as at the start of each
         3105  +** trigger subprogram. ^The P argument is a pointer to the
         3106  +** [prepared statement]. ^The X argument is a pointer to a string which
         3107  +** is the unexpanded SQL text of the prepared statement or an SQL comment 
         3108  +** that indicates the invocation of a trigger.  ^The callback can compute
         3109  +** the same text that would have been returned by the legacy [sqlite3_trace()]
         3110  +** interface by using the X argument when X begins with "--" and invoking
         3111  +** [sqlite3_expanded_sql(P)] otherwise.
         3112  +**
         3113  +** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
         3114  +** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
         3115  +** information as is provided by the [sqlite3_profile()] callback.
         3116  +** ^The P argument is a pointer to the [prepared statement] and the
         3117  +** X argument points to a 64-bit integer which is the estimated of
         3118  +** the number of nanosecond that the prepared statement took to run.
         3119  +** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
         3120  +**
         3121  +** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
         3122  +** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
         3123  +** statement generates a single row of result.  
         3124  +** ^The P argument is a pointer to the [prepared statement] and the
         3125  +** X argument is unused.
         3126  +**
         3127  +** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
         3128  +** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
         3129  +** connection closes.
         3130  +** ^The P argument is a pointer to the [database connection] object
         3131  +** and the X argument is unused.
         3132  +** </dl>
         3133  +*/
         3134  +#define SQLITE_TRACE_STMT       0x01
         3135  +#define SQLITE_TRACE_PROFILE    0x02
         3136  +#define SQLITE_TRACE_ROW        0x04
         3137  +#define SQLITE_TRACE_CLOSE      0x08
         3138  +
         3139  +/*
         3140  +** CAPI3REF: SQL Trace Hook
         3141  +** METHOD: sqlite3
         3142  +**
         3143  +** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
         3144  +** function X against [database connection] D, using property mask M
         3145  +** and context pointer P.  ^If the X callback is
         3146  +** NULL or if the M mask is zero, then tracing is disabled.  The
         3147  +** M argument should be the bitwise OR-ed combination of
         3148  +** zero or more [SQLITE_TRACE] constants.
         3149  +**
         3150  +** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides 
         3151  +** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
         3152  +**
         3153  +** ^The X callback is invoked whenever any of the events identified by 
         3154  +** mask M occur.  ^The integer return value from the callback is currently
         3155  +** ignored, though this may change in future releases.  Callback
         3156  +** implementations should return zero to ensure future compatibility.
         3157  +**
         3158  +** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
         3159  +** ^The T argument is one of the [SQLITE_TRACE]
         3160  +** constants to indicate why the callback was invoked.
         3161  +** ^The C argument is a copy of the context pointer.
         3162  +** The P and X arguments are pointers whose meanings depend on T.
         3163  +**
         3164  +** The sqlite3_trace_v2() interface is intended to replace the legacy
         3165  +** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
         3166  +** are deprecated.
         3167  +*/
         3168  +SQLITE_API int SQLITE_STDCALL sqlite3_trace_v2(
         3169  +  sqlite3*,
         3170  +  unsigned uMask,
         3171  +  int(*xCallback)(unsigned,void*,void*,void*),
         3172  +  void *pCtx
         3173  +);
  3037   3174   
  3038   3175   /*
  3039   3176   ** CAPI3REF: Query Progress Callbacks
  3040   3177   ** METHOD: sqlite3
  3041   3178   **
  3042   3179   ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  3043   3180   ** function X to be invoked periodically during long running calls to
................................................................................
  3649   3786     const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3650   3787   );
  3651   3788   
  3652   3789   /*
  3653   3790   ** CAPI3REF: Retrieving Statement SQL
  3654   3791   ** METHOD: sqlite3_stmt
  3655   3792   **
  3656         -** ^This interface can be used to retrieve a saved copy of the original
  3657         -** SQL text used to create a [prepared statement] if that statement was
  3658         -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
         3793  +** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
         3794  +** SQL text used to create [prepared statement] P if P was
         3795  +** created by either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
         3796  +** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
         3797  +** string containing the SQL text of prepared statement P with
         3798  +** [bound parameters] expanded.
         3799  +**
         3800  +** ^(For example, if a prepared statement is created using the SQL
         3801  +** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
         3802  +** and parameter :xyz is unbound, then sqlite3_sql() will return
         3803  +** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
         3804  +** will return "SELECT 2345,NULL".)^
         3805  +**
         3806  +** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
         3807  +** is available to hold the result, or if the result would exceed the
         3808  +** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
         3809  +**
         3810  +** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
         3811  +** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
         3812  +** option causes sqlite3_expanded_sql() to always return NULL.
         3813  +**
         3814  +** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
         3815  +** automatically freed when the prepared statement is finalized.
         3816  +** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
         3817  +** is obtained from [sqlite3_malloc()] and must be free by the application
         3818  +** by passing it to [sqlite3_free()].
  3659   3819   */
  3660   3820   SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
         3821  +SQLITE_API char *SQLITE_STDCALL sqlite3_expanded_sql(sqlite3_stmt *pStmt);
  3661   3822   
  3662   3823   /*
  3663   3824   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3664   3825   ** METHOD: sqlite3_stmt
  3665   3826   **
  3666   3827   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3667   3828   ** and only if the [prepared statement] X makes no direct changes to
................................................................................
  4148   4309   ** more threads at the same moment in time.
  4149   4310   **
  4150   4311   ** For all versions of SQLite up to and including 3.6.23.1, a call to
  4151   4312   ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  4152   4313   ** other than [SQLITE_ROW] before any subsequent invocation of
  4153   4314   ** sqlite3_step().  Failure to reset the prepared statement using 
  4154   4315   ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  4155         -** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
         4316  +** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
         4317  +** sqlite3_step() began
  4156   4318   ** calling [sqlite3_reset()] automatically in this circumstance rather
  4157   4319   ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  4158   4320   ** break because any application that ever receives an SQLITE_MISUSE error
  4159   4321   ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  4160   4322   ** can be used to restore the legacy behavior.
  4161   4323   **
  4162   4324   ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
................................................................................
  4811   4973   ** calls to sqlite3_get_auxdata(C,N) return P from the most recent
  4812   4974   ** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
  4813   4975   ** NULL if the metadata has been discarded.
  4814   4976   ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
  4815   4977   ** SQLite will invoke the destructor function X with parameter P exactly
  4816   4978   ** once, when the metadata is discarded.
  4817   4979   ** SQLite is free to discard the metadata at any time, including: <ul>
  4818         -** <li> when the corresponding function parameter changes, or
  4819         -** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
  4820         -**      SQL statement, or
  4821         -** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
  4822         -** <li> during the original sqlite3_set_auxdata() call when a memory 
  4823         -**      allocation error occurs. </ul>)^
         4980  +** <li> ^(when the corresponding function parameter changes)^, or
         4981  +** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
         4982  +**      SQL statement)^, or
         4983  +** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
         4984  +**       parameter)^, or
         4985  +** <li> ^(during the original sqlite3_set_auxdata() call when a memory 
         4986  +**      allocation error occurs.)^ </ul>
  4824   4987   **
  4825   4988   ** Note the last bullet in particular.  The destructor X in 
  4826   4989   ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
  4827   4990   ** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
  4828   4991   ** should be called near the end of the function implementation and the
  4829   4992   ** function implementation should not make any use of P after
  4830   4993   ** sqlite3_set_auxdata() has been called.
................................................................................
  5510   5673   **
  5511   5674   ** ^(This routine enables or disables the sharing of the database cache
  5512   5675   ** and schema data structures between [database connection | connections]
  5513   5676   ** to the same database. Sharing is enabled if the argument is true
  5514   5677   ** and disabled if the argument is false.)^
  5515   5678   **
  5516   5679   ** ^Cache sharing is enabled and disabled for an entire process.
  5517         -** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
         5680  +** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). 
         5681  +** In prior versions of SQLite,
  5518   5682   ** sharing was enabled or disabled for each thread separately.
  5519   5683   **
  5520   5684   ** ^(The cache sharing mode set by this interface effects all subsequent
  5521   5685   ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  5522   5686   ** Existing database connections continue use the sharing mode
  5523   5687   ** that was in effect at the time they were opened.)^
  5524   5688   **
................................................................................
  5604   5768   ** <li> An alternative page cache implementation is specified using
  5605   5769   **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  5606   5770   ** <li> The page cache allocates from its own memory pool supplied
  5607   5771   **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  5608   5772   **      from the heap.
  5609   5773   ** </ul>)^
  5610   5774   **
  5611         -** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
         5775  +** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]), 
         5776  +** the soft heap limit is enforced
  5612   5777   ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  5613   5778   ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  5614   5779   ** the soft heap limit is enforced on every memory allocation.  Without
  5615   5780   ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  5616   5781   ** when memory is allocated by the page cache.  Testing suggests that because
  5617   5782   ** the page cache is the predominate memory user in SQLite, most
  5618   5783   ** applications will achieve adequate soft heap limit enforcement without
................................................................................
  5643   5808   ** information about column C of table T in database D
  5644   5809   ** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
  5645   5810   ** interface returns SQLITE_OK and fills in the non-NULL pointers in
  5646   5811   ** the final five arguments with appropriate values if the specified
  5647   5812   ** column exists.  ^The sqlite3_table_column_metadata() interface returns
  5648   5813   ** SQLITE_ERROR and if the specified column does not exist.
  5649   5814   ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
  5650         -** NULL pointer, then this routine simply checks for the existance of the
         5815  +** NULL pointer, then this routine simply checks for the existence of the
  5651   5816   ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
  5652   5817   ** does not.
  5653   5818   **
  5654   5819   ** ^The column is identified by the second, third and fourth parameters to
  5655   5820   ** this function. ^(The second parameter is either the name of the database
  5656   5821   ** (i.e. "main", "temp", or an attached database) containing the specified
  5657   5822   ** table or NULL.)^ ^If it is NULL, then all attached databases are searched
................................................................................
  5777   5942   ** ^Extension loading is off by default.
  5778   5943   ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  5779   5944   ** to turn extension loading on and call it with onoff==0 to turn
  5780   5945   ** it back off again.
  5781   5946   **
  5782   5947   ** ^This interface enables or disables both the C-API
  5783   5948   ** [sqlite3_load_extension()] and the SQL function [load_extension()].
  5784         -** Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
  5785         -** to enable or disable only the C-API.
         5949  +** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
         5950  +** to enable or disable only the C-API.)^
  5786   5951   **
  5787   5952   ** <b>Security warning:</b> It is recommended that extension loading
  5788   5953   ** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
  5789   5954   ** rather than this interface, so the [load_extension()] SQL function
  5790   5955   ** remains disabled. This will prevent SQL injections from giving attackers
  5791   5956   ** access to extension loading capabilities.
  5792   5957   */
................................................................................
  5798   5963   ** ^This interface causes the xEntryPoint() function to be invoked for
  5799   5964   ** each new [database connection] that is created.  The idea here is that
  5800   5965   ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
  5801   5966   ** that is to be automatically loaded into all new database connections.
  5802   5967   **
  5803   5968   ** ^(Even though the function prototype shows that xEntryPoint() takes
  5804   5969   ** no arguments and returns void, SQLite invokes xEntryPoint() with three
  5805         -** arguments and expects and integer result as if the signature of the
         5970  +** arguments and expects an integer result as if the signature of the
  5806   5971   ** entry point where as follows:
  5807   5972   **
  5808   5973   ** <blockquote><pre>
  5809   5974   ** &nbsp;  int xEntryPoint(
  5810   5975   ** &nbsp;    sqlite3 *db,
  5811   5976   ** &nbsp;    const char **pzErrMsg,
  5812   5977   ** &nbsp;    const struct sqlite3_api_routines *pThunk
................................................................................
  5998   6163   ** any database changes. In other words, if the xUpdate() returns
  5999   6164   ** SQLITE_CONSTRAINT, the database contents must be exactly as they were
  6000   6165   ** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
  6001   6166   ** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
  6002   6167   ** the xUpdate method are automatically rolled back by SQLite.
  6003   6168   **
  6004   6169   ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
  6005         -** structure for SQLite version 3.8.2. If a virtual table extension is
         6170  +** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). 
         6171  +** If a virtual table extension is
  6006   6172   ** used with an SQLite version earlier than 3.8.2, the results of attempting 
  6007   6173   ** to read or write the estimatedRows field are undefined (but are likely 
  6008   6174   ** to included crashing the application). The estimatedRows field should
  6009   6175   ** therefore only be used if [sqlite3_libversion_number()] returns a
  6010   6176   ** value greater than or equal to 3008002. Similarly, the idxFlags field
  6011         -** was added for version 3.9.0. It may therefore only be used if
         6177  +** was added for [version 3.9.0] ([dateof:3.9.0]). 
         6178  +** It may therefore only be used if
  6012   6179   ** sqlite3_libversion_number() returns a value greater than or equal to
  6013   6180   ** 3009000.
  6014   6181   */
  6015   6182   struct sqlite3_index_info {
  6016   6183     /* Inputs */
  6017   6184     int nConstraint;           /* Number of entries in aConstraint */
  6018   6185     struct sqlite3_index_constraint {
................................................................................
  6702   6869   */
  6703   6870   #define SQLITE_MUTEX_FAST             0
  6704   6871   #define SQLITE_MUTEX_RECURSIVE        1
  6705   6872   #define SQLITE_MUTEX_STATIC_MASTER    2
  6706   6873   #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6707   6874   #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6708   6875   #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6709         -#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
         6876  +#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
  6710   6877   #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6711   6878   #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6712   6879   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6713   6880   #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
  6714   6881   #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
  6715   6882   #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6716   6883   #define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
................................................................................
  6806   6973   #define SQLITE_TESTCTRL_ALWAYS                  13
  6807   6974   #define SQLITE_TESTCTRL_RESERVE                 14
  6808   6975   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6809   6976   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6810   6977   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6811   6978   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6812   6979   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
         6980  +#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
  6813   6981   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6814   6982   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6815   6983   #define SQLITE_TESTCTRL_BYTEORDER               22
  6816   6984   #define SQLITE_TESTCTRL_ISINIT                  23
  6817   6985   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  6818   6986   #define SQLITE_TESTCTRL_IMPOSTER                25
  6819   6987   #define SQLITE_TESTCTRL_LAST                    25
................................................................................
  7012   7180   ** the current value is always zero.)^
  7013   7181   **
  7014   7182   ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  7015   7183   ** <dd>This parameter returns the approximate number of bytes of heap
  7016   7184   ** memory used by all pager caches associated with the database connection.)^
  7017   7185   ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  7018   7186   **
         7187  +** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] 
         7188  +** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
         7189  +** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
         7190  +** pager cache is shared between two or more connections the bytes of heap
         7191  +** memory used by that pager cache is divided evenly between the attached
         7192  +** connections.)^  In other words, if none of the pager caches associated
         7193  +** with the database connection are shared, this request returns the same
         7194  +** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
         7195  +** shared, the value returned by this call will be smaller than that returned
         7196  +** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
         7197  +** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
         7198  +**
  7019   7199   ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  7020   7200   ** <dd>This parameter returns the approximate number of bytes of heap
  7021   7201   ** memory used to store the schema for all databases associated
  7022   7202   ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
  7023   7203   ** ^The full amount of memory used by the schemas is reported, even if the
  7024   7204   ** schema memory is shared with other database connections due to
  7025   7205   ** [shared cache mode] being enabled.
................................................................................
  7069   7249   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  7070   7250   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  7071   7251   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  7072   7252   #define SQLITE_DBSTATUS_CACHE_HIT            7
  7073   7253   #define SQLITE_DBSTATUS_CACHE_MISS           8
  7074   7254   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  7075   7255   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  7076         -#define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
         7256  +#define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
         7257  +#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
  7077   7258   
  7078   7259   
  7079   7260   /*
  7080   7261   ** CAPI3REF: Prepared Statement Status
  7081   7262   ** METHOD: sqlite3_stmt
  7082   7263   **
  7083   7264   ** ^(Each prepared statement maintains various
................................................................................
  8225   8406   ** ^The preupdate hook only fires for changes to [rowid tables]; the preupdate
  8226   8407   ** hook is not invoked for changes to [virtual tables] or [WITHOUT ROWID]
  8227   8408   ** tables.
  8228   8409   **
  8229   8410   ** ^The second parameter to the preupdate callback is a pointer to
  8230   8411   ** the [database connection] that registered the preupdate hook.
  8231   8412   ** ^The third parameter to the preupdate callback is one of the constants
  8232         -** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to indentify the
         8413  +** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
  8233   8414   ** kind of update operation that is about to occur.
  8234   8415   ** ^(The fourth parameter to the preupdate callback is the name of the
  8235   8416   ** database within the database connection that is being modified.  This
  8236   8417   ** will be "main" for the main database or "temp" for TEMP tables or 
  8237   8418   ** the name given after the AS keyword in the [ATTACH] statement for attached
  8238   8419   ** databases.)^
  8239   8420   ** ^The fifth parameter to the preupdate callback is the name of the
................................................................................
  8452   8633   #ifdef SQLITE_OMIT_FLOATING_POINT
  8453   8634   # undef double
  8454   8635   #endif
  8455   8636   
  8456   8637   #if 0
  8457   8638   }  /* End of the 'extern "C"' block */
  8458   8639   #endif
  8459         -#endif /* _SQLITE3_H_ */
         8640  +#endif /* SQLITE3_H */
  8460   8641   
  8461   8642   /******** Begin file sqlite3rtree.h *********/
  8462   8643   /*
  8463   8644   ** 2010 August 30
  8464   8645   **
  8465   8646   ** The author disclaims copyright to this source code.  In place of
  8466   8647   ** a legal notice, here is a blessing:
................................................................................
  9302   9483     void *pB,                       /* Pointer to buffer containing changeset B */
  9303   9484     int *pnOut,                     /* OUT: Number of bytes in output changeset */
  9304   9485     void **ppOut                    /* OUT: Buffer containing output changeset */
  9305   9486   );
  9306   9487   
  9307   9488   
  9308   9489   /*
  9309         -** Changegroup handle.
         9490  +** CAPI3REF: Changegroup Handle
  9310   9491   */
  9311   9492   typedef struct sqlite3_changegroup sqlite3_changegroup;
  9312   9493   
  9313   9494   /*
  9314         -** CAPI3REF: Combine two or more changesets into a single changeset.
         9495  +** CAPI3REF: Create A New Changegroup Object
  9315   9496   **
  9316   9497   ** An sqlite3_changegroup object is used to combine two or more changesets
  9317   9498   ** (or patchsets) into a single changeset (or patchset). A single changegroup
  9318   9499   ** object may combine changesets or patchsets, but not both. The output is
  9319   9500   ** always in the same format as the input.
  9320   9501   **
  9321   9502   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
  9344   9525   ** As well as the regular sqlite3changegroup_add() and 
  9345   9526   ** sqlite3changegroup_output() functions, also available are the streaming
  9346   9527   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
  9347   9528   */
  9348   9529   int sqlite3changegroup_new(sqlite3_changegroup **pp);
  9349   9530   
  9350   9531   /*
         9532  +** CAPI3REF: Add A Changeset To A Changegroup
         9533  +**
  9351   9534   ** Add all changes within the changeset (or patchset) in buffer pData (size
  9352   9535   ** nData bytes) to the changegroup. 
  9353   9536   **
  9354   9537   ** If the buffer contains a patchset, then all prior calls to this function
  9355   9538   ** on the same changegroup object must also have specified patchsets. Or, if
  9356   9539   ** the buffer contains a changeset, so must have the earlier calls to this
  9357   9540   ** function. Otherwise, SQLITE_ERROR is returned and no changes are added
................................................................................
  9419   9602   ** final contents of the changegroup is undefined.
  9420   9603   **
  9421   9604   ** If no error occurs, SQLITE_OK is returned.
  9422   9605   */
  9423   9606   int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
  9424   9607   
  9425   9608   /*
         9609  +** CAPI3REF: Obtain A Composite Changeset From A Changegroup
         9610  +**
  9426   9611   ** Obtain a buffer containing a changeset (or patchset) representing the
  9427   9612   ** current contents of the changegroup. If the inputs to the changegroup
  9428   9613   ** were themselves changesets, the output is a changeset. Or, if the
  9429   9614   ** inputs were patchsets, the output is also a patchset.
  9430   9615   **
  9431   9616   ** As with the output of the sqlite3session_changeset() and
  9432   9617   ** sqlite3session_patchset() functions, all changes related to a single
................................................................................
  9447   9632   int sqlite3changegroup_output(
  9448   9633     sqlite3_changegroup*,
  9449   9634     int *pnData,                    /* OUT: Size of output buffer in bytes */
  9450   9635     void **ppData                   /* OUT: Pointer to output buffer */
  9451   9636   );
  9452   9637   
  9453   9638   /*
  9454         -** Delete a changegroup object.
         9639  +** CAPI3REF: Delete A Changegroup Object
  9455   9640   */
  9456   9641   void sqlite3changegroup_delete(sqlite3_changegroup*);
  9457   9642   
  9458   9643   /*
  9459   9644   ** CAPI3REF: Apply A Changeset To A Database
  9460   9645   **
  9461   9646   ** Apply a changeset to a database. This function attempts to update the
................................................................................
 10172  10357   ** Applications may also register custom tokenizer types. A tokenizer 
 10173  10358   ** is registered by providing fts5 with a populated instance of the 
 10174  10359   ** following structure. All structure methods must be defined, setting
 10175  10360   ** any member of the fts5_tokenizer struct to NULL leads to undefined
 10176  10361   ** behaviour. The structure methods are expected to function as follows:
 10177  10362   **
 10178  10363   ** xCreate:
 10179         -**   This function is used to allocate and inititalize a tokenizer instance.
        10364  +**   This function is used to allocate and initialize a tokenizer instance.
 10180  10365   **   A tokenizer instance is required to actually tokenize text.
 10181  10366   **
 10182  10367   **   The first argument passed to this function is a copy of the (void*)
 10183  10368   **   pointer provided by the application when the fts5_tokenizer object
 10184  10369   **   was registered with FTS5 (the third argument to xCreateTokenizer()). 
 10185  10370   **   The second and third arguments are an array of nul-terminated strings
 10186  10371   **   containing the tokenizer arguments, if any, specified following the
................................................................................
 10431  10616   *************************************************************************/
 10432  10617   
 10433  10618   #if 0
 10434  10619   }  /* end of the 'extern "C"' block */
 10435  10620   #endif
 10436  10621   
 10437  10622   #endif /* _FTS5_H */
 10438         -
 10439  10623   
 10440  10624   /******** End of fts5.h *********/
 10441  10625   
 10442  10626   /************** End of sqlite3.h *********************************************/
 10443  10627   /************** Continuing where we left off in sqliteInt.h ******************/
 10444  10628   
 10445  10629   /*
................................................................................
 10730  10914   
 10731  10915   /*
 10732  10916   ** Make sure that the compiler intrinsics we desire are enabled when
 10733  10917   ** compiling with an appropriate version of MSVC unless prevented by
 10734  10918   ** the SQLITE_DISABLE_INTRINSIC define.
 10735  10919   */
 10736  10920   #if !defined(SQLITE_DISABLE_INTRINSIC)
 10737         -#  if defined(_MSC_VER) && _MSC_VER>=1300
        10921  +#  if defined(_MSC_VER) && _MSC_VER>=1400
 10738  10922   #    if !defined(_WIN32_WCE)
 10739  10923   #      include <intrin.h>
 10740  10924   #      pragma intrinsic(_byteswap_ushort)
 10741  10925   #      pragma intrinsic(_byteswap_ulong)
 10742  10926   #      pragma intrinsic(_ReadWriteBarrier)
 10743  10927   #    else
 10744  10928   #      include <cmnintrin.h>
................................................................................
 11007  11191   **    May you find forgiveness for yourself and forgive others.
 11008  11192   **    May you share freely, never taking more than you give.
 11009  11193   **
 11010  11194   *************************************************************************
 11011  11195   ** This is the header file for the generic hash-table implementation
 11012  11196   ** used in SQLite.
 11013  11197   */
 11014         -#ifndef _SQLITE_HASH_H_
 11015         -#define _SQLITE_HASH_H_
        11198  +#ifndef SQLITE_HASH_H
        11199  +#define SQLITE_HASH_H
 11016  11200   
 11017  11201   /* Forward declarations of structures. */
 11018  11202   typedef struct Hash Hash;
 11019  11203   typedef struct HashElem HashElem;
 11020  11204   
 11021  11205   /* A complete hash table is an instance of the following structure.
 11022  11206   ** The internals of this structure are intended to be opaque -- client
................................................................................
 11088  11272   /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
 11089  11273   
 11090  11274   /*
 11091  11275   ** Number of entries in a hash table
 11092  11276   */
 11093  11277   /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
 11094  11278   
 11095         -#endif /* _SQLITE_HASH_H_ */
        11279  +#endif /* SQLITE_HASH_H */
 11096  11280   
 11097  11281   /************** End of hash.h ************************************************/
 11098  11282   /************** Continuing where we left off in sqliteInt.h ******************/
 11099  11283   /************** Include parse.h in the middle of sqliteInt.h *****************/
 11100  11284   /************** Begin file parse.h *******************************************/
 11101  11285   #define TK_SEMI                             1
 11102  11286   #define TK_EXPLAIN                          2
................................................................................
 11251  11435   #define TK_FUNCTION                       151
 11252  11436   #define TK_COLUMN                         152
 11253  11437   #define TK_AGG_FUNCTION                   153
 11254  11438   #define TK_AGG_COLUMN                     154
 11255  11439   #define TK_UMINUS                         155
 11256  11440   #define TK_UPLUS                          156
 11257  11441   #define TK_REGISTER                       157
 11258         -#define TK_ASTERISK                       158
 11259         -#define TK_SPAN                           159
 11260         -#define TK_SPACE                          160
 11261         -#define TK_ILLEGAL                        161
        11442  +#define TK_VECTOR                         158
        11443  +#define TK_SELECT_COLUMN                  159
        11444  +#define TK_ASTERISK                       160
        11445  +#define TK_SPAN                           161
        11446  +#define TK_SPACE                          162
        11447  +#define TK_ILLEGAL                        163
 11262  11448   
 11263  11449   /* The token codes above must all fit in 8 bits */
 11264  11450   #define TKFLG_MASK           0xff  
 11265  11451   
 11266  11452   /* Flags that can be added to a token code when it is not
 11267  11453   ** being stored in a u8: */
 11268  11454   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 11836  12022   **    May you share freely, never taking more than you give.
 11837  12023   **
 11838  12024   *************************************************************************
 11839  12025   ** This header file defines the interface that the sqlite B-Tree file
 11840  12026   ** subsystem.  See comments in the source code for a detailed description
 11841  12027   ** of what each interface routine does.
 11842  12028   */
 11843         -#ifndef _BTREE_H_
 11844         -#define _BTREE_H_
        12029  +#ifndef SQLITE_BTREE_H
        12030  +#define SQLITE_BTREE_H
 11845  12031   
 11846  12032   /* TODO: This definition is just included so other modules compile. It
 11847  12033   ** needs to be revisited.
 11848  12034   */
 11849  12035   #define SQLITE_N_BTREE_META 16
 11850  12036   
 11851  12037   /*
................................................................................
 11862  12048   
 11863  12049   /*
 11864  12050   ** Forward declarations of structure
 11865  12051   */
 11866  12052   typedef struct Btree Btree;
 11867  12053   typedef struct BtCursor BtCursor;
 11868  12054   typedef struct BtShared BtShared;
        12055  +typedef struct BtreePayload BtreePayload;
 11869  12056   
 11870  12057   
 11871  12058   SQLITE_PRIVATE int sqlite3BtreeOpen(
 11872  12059     sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
 11873  12060     const char *zFilename,   /* Name of database file to open */
 11874  12061     sqlite3 *db,             /* Associated database connection */
 11875  12062     Btree **ppBtree,         /* Return open Btree* here */
................................................................................
 11912  12099   SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
 11913  12100   SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
 11914  12101   SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
 11915  12102   SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
 11916  12103   SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
 11917  12104   SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
 11918  12105   SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
        12106  +#ifndef SQLITE_OMIT_SHARED_CACHE
 11919  12107   SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
        12108  +#endif
 11920  12109   SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
 11921  12110   
 11922  12111   SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
 11923  12112   SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
 11924  12113   SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
 11925  12114   
 11926  12115   SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
................................................................................
 12073  12262   SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
 12074  12263   SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
 12075  12264   
 12076  12265   /* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
 12077  12266   #define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
 12078  12267   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
 12079  12268   
 12080         -SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
 12081         -                                  const void *pData, int nData,
 12082         -                                  int nZero, int bias, int seekResult);
        12269  +/* An instance of the BtreePayload object describes the content of a single
        12270  +** entry in either an index or table btree.
        12271  +**
        12272  +** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
        12273  +** an arbitrary key and no data.  These btrees have pKey,nKey set to their
        12274  +** key and pData,nData,nZero set to zero.
        12275  +**
        12276  +** Table btrees (used for rowid tables) contain an integer rowid used as
        12277  +** the key and passed in the nKey field.  The pKey field is zero.  
        12278  +** pData,nData hold the content of the new entry.  nZero extra zero bytes
        12279  +** are appended to the end of the content when constructing the entry.
        12280  +**
        12281  +** This object is used to pass information into sqlite3BtreeInsert().  The
        12282  +** same information used to be passed as five separate parameters.  But placing
        12283  +** the information into this object helps to keep the interface more 
        12284  +** organized and understandable, and it also helps the resulting code to
        12285  +** run a little faster by using fewer registers for parameter passing.
        12286  +*/
        12287  +struct BtreePayload {
        12288  +  const void *pKey;       /* Key content for indexes.  NULL for tables */
        12289  +  sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
        12290  +  const void *pData;      /* Data for tables.  NULL for indexes */
        12291  +  int nData;              /* Size of pData.  0 if none. */
        12292  +  int nZero;              /* Extra zero data appended after pData,nData */
        12293  +};
        12294  +
        12295  +SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
        12296  +                       int bias, int seekResult);
 12083  12297   SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
 12084  12298   SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
 12085  12299   SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
 12086  12300   SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
 12087  12301   SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
 12088         -SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
        12302  +SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
 12089  12303   SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
 12090         -SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
 12091         -SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
 12092         -SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
        12304  +SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
        12305  +SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
 12093  12306   SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
 12094  12307   
 12095  12308   SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
 12096  12309   SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
 12097  12310   
        12311  +#ifndef SQLITE_OMIT_INCRBLOB
 12098  12312   SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
 12099  12313   SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
        12314  +#endif
 12100  12315   SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
 12101  12316   SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
 12102  12317   SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
 12103  12318   SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
 12104  12319   SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
 12105  12320   
 12106  12321   #ifndef NDEBUG
................................................................................
 12126  12341   ** Enter and Leave procedures no-ops.
 12127  12342   */
 12128  12343   #ifndef SQLITE_OMIT_SHARED_CACHE
 12129  12344   SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
 12130  12345   SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
 12131  12346   SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
 12132  12347   SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
        12348  +SQLITE_PRIVATE   int sqlite3BtreeConnectionCount(Btree*);
 12133  12349   #else
 12134  12350   # define sqlite3BtreeEnter(X) 
 12135  12351   # define sqlite3BtreeEnterAll(X)
 12136  12352   # define sqlite3BtreeSharable(X) 0
 12137  12353   # define sqlite3BtreeEnterCursor(X)
        12354  +# define sqlite3BtreeConnectionCount(X) 1
 12138  12355   #endif
 12139  12356   
 12140  12357   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
 12141  12358   SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
 12142  12359   SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
 12143  12360   SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
 12144  12361   #ifndef NDEBUG
................................................................................
 12155  12372   
 12156  12373   # define sqlite3BtreeHoldsMutex(X) 1
 12157  12374   # define sqlite3BtreeHoldsAllMutexes(X) 1
 12158  12375   # define sqlite3SchemaMutexHeld(X,Y,Z) 1
 12159  12376   #endif
 12160  12377   
 12161  12378   
 12162         -#endif /* _BTREE_H_ */
        12379  +#endif /* SQLITE_BTREE_H */
 12163  12380   
 12164  12381   /************** End of btree.h ***********************************************/
 12165  12382   /************** Continuing where we left off in sqliteInt.h ******************/
 12166  12383   /************** Include vdbe.h in the middle of sqliteInt.h ******************/
 12167  12384   /************** Begin file vdbe.h ********************************************/
 12168  12385   /*
 12169  12386   ** 2001 September 15
................................................................................
 12178  12395   *************************************************************************
 12179  12396   ** Header file for the Virtual DataBase Engine (VDBE)
 12180  12397   **
 12181  12398   ** This header defines the interface to the virtual database engine
 12182  12399   ** or VDBE.  The VDBE implements an abstract machine that runs a
 12183  12400   ** simple program to access and modify the underlying database.
 12184  12401   */
 12185         -#ifndef _SQLITE_VDBE_H_
 12186         -#define _SQLITE_VDBE_H_
        12402  +#ifndef SQLITE_VDBE_H
        12403  +#define SQLITE_VDBE_H
 12187  12404   /* #include <stdio.h> */
 12188  12405   
 12189  12406   /*
 12190  12407   ** A single VDBE is an opaque structure named "Vdbe".  Only routines
 12191  12408   ** in the source file sqliteVdbe.c are allowed to see the insides
 12192  12409   ** of this structure.
 12193  12410   */
................................................................................
 12251  12468   ** A sub-routine used to implement a trigger program.
 12252  12469   */
 12253  12470   struct SubProgram {
 12254  12471     VdbeOp *aOp;                  /* Array of opcodes for sub-program */
 12255  12472     int nOp;                      /* Elements in aOp[] */
 12256  12473     int nMem;                     /* Number of memory cells required */
 12257  12474     int nCsr;                     /* Number of cursors required */
 12258         -  int nOnce;                    /* Number of OP_Once instructions */
 12259  12475     void *token;                  /* id that may be used to recursive triggers */
 12260  12476     SubProgram *pNext;            /* Next sub-program already visited */
 12261  12477   };
 12262  12478   
 12263  12479   /*
 12264  12480   ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
 12265  12481   ** it takes up less space.
................................................................................
 12364  12580   #define OP_SeekGE         25 /* synopsis: key=r[P3@P4]                     */
 12365  12581   #define OP_SeekGT         26 /* synopsis: key=r[P3@P4]                     */
 12366  12582   #define OP_Or             27 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
 12367  12583   #define OP_And            28 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
 12368  12584   #define OP_NoConflict     29 /* synopsis: key=r[P3@P4]                     */
 12369  12585   #define OP_NotFound       30 /* synopsis: key=r[P3@P4]                     */
 12370  12586   #define OP_Found          31 /* synopsis: key=r[P3@P4]                     */
 12371         -#define OP_NotExists      32 /* synopsis: intkey=r[P3]                     */
 12372         -#define OP_Last           33
        12587  +#define OP_SeekRowid      32 /* synopsis: intkey=r[P3]                     */
        12588  +#define OP_NotExists      33 /* synopsis: intkey=r[P3]                     */
 12373  12589   #define OP_IsNull         34 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
 12374  12590   #define OP_NotNull        35 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
 12375         -#define OP_Ne             36 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
 12376         -#define OP_Eq             37 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
 12377         -#define OP_Gt             38 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
 12378         -#define OP_Le             39 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
 12379         -#define OP_Lt             40 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
 12380         -#define OP_Ge             41 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
 12381         -#define OP_SorterSort     42
        12591  +#define OP_Ne             36 /* same as TK_NE, synopsis: IF r[P3]!=r[P1]   */
        12592  +#define OP_Eq             37 /* same as TK_EQ, synopsis: IF r[P3]==r[P1]   */
        12593  +#define OP_Gt             38 /* same as TK_GT, synopsis: IF r[P3]>r[P1]    */
        12594  +#define OP_Le             39 /* same as TK_LE, synopsis: IF r[P3]<=r[P1]   */
        12595  +#define OP_Lt             40 /* same as TK_LT, synopsis: IF r[P3]<r[P1]    */
        12596  +#define OP_Ge             41 /* same as TK_GE, synopsis: IF r[P3]>=r[P1]   */
        12597  +#define OP_ElseNotEq      42 /* same as TK_ESCAPE                          */
 12382  12598   #define OP_BitAnd         43 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
 12383  12599   #define OP_BitOr          44 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
 12384  12600   #define OP_ShiftLeft      45 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
 12385  12601   #define OP_ShiftRight     46 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
 12386  12602   #define OP_Add            47 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
 12387  12603   #define OP_Subtract       48 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 12388  12604   #define OP_Multiply       49 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 12389  12605   #define OP_Divide         50 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 12390  12606   #define OP_Remainder      51 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 12391  12607   #define OP_Concat         52 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 12392         -#define OP_Sort           53
        12608  +#define OP_Last           53
 12393  12609   #define OP_BitNot         54 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 12394         -#define OP_Rewind         55
 12395         -#define OP_IdxLE          56 /* synopsis: key=r[P3@P4]                     */
 12396         -#define OP_IdxGT          57 /* synopsis: key=r[P3@P4]                     */
 12397         -#define OP_IdxLT          58 /* synopsis: key=r[P3@P4]                     */
 12398         -#define OP_IdxGE          59 /* synopsis: key=r[P3@P4]                     */
 12399         -#define OP_RowSetRead     60 /* synopsis: r[P3]=rowset(P1)                 */
 12400         -#define OP_RowSetTest     61 /* synopsis: if r[P3] in rowset(P1) goto P2   */
 12401         -#define OP_Program        62
 12402         -#define OP_FkIfZero       63 /* synopsis: if fkctr[P1]==0 goto P2          */
 12403         -#define OP_IfPos          64 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
 12404         -#define OP_IfNotZero      65 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
 12405         -#define OP_DecrJumpZero   66 /* synopsis: if (--r[P1])==0 goto P2          */
 12406         -#define OP_IncrVacuum     67
 12407         -#define OP_VNext          68
 12408         -#define OP_Init           69 /* synopsis: Start at P2                      */
 12409         -#define OP_Return         70
 12410         -#define OP_EndCoroutine   71
 12411         -#define OP_HaltIfNull     72 /* synopsis: if r[P3]=null halt               */
 12412         -#define OP_Halt           73
 12413         -#define OP_Integer        74 /* synopsis: r[P2]=P1                         */
 12414         -#define OP_Int64          75 /* synopsis: r[P2]=P4                         */
 12415         -#define OP_String         76 /* synopsis: r[P2]='P4' (len=P1)              */
 12416         -#define OP_Null           77 /* synopsis: r[P2..P3]=NULL                   */
 12417         -#define OP_SoftNull       78 /* synopsis: r[P1]=NULL                       */
 12418         -#define OP_Blob           79 /* synopsis: r[P2]=P4 (len=P1)                */
 12419         -#define OP_Variable       80 /* synopsis: r[P2]=parameter(P1,P4)           */
 12420         -#define OP_Move           81 /* synopsis: r[P2@P3]=r[P1@P3]                */
 12421         -#define OP_Copy           82 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
 12422         -#define OP_SCopy          83 /* synopsis: r[P2]=r[P1]                      */
 12423         -#define OP_IntCopy        84 /* synopsis: r[P2]=r[P1]                      */
 12424         -#define OP_ResultRow      85 /* synopsis: output=r[P1@P2]                  */
 12425         -#define OP_CollSeq        86
 12426         -#define OP_Function0      87 /* synopsis: r[P3]=func(r[P2@P5])             */
 12427         -#define OP_Function       88 /* synopsis: r[P3]=func(r[P2@P5])             */
 12428         -#define OP_AddImm         89 /* synopsis: r[P1]=r[P1]+P2                   */
 12429         -#define OP_RealAffinity   90
 12430         -#define OP_Cast           91 /* synopsis: affinity(r[P1])                  */
 12431         -#define OP_Permutation    92
 12432         -#define OP_Compare        93 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 12433         -#define OP_Column         94 /* synopsis: r[P3]=PX                         */
 12434         -#define OP_Affinity       95 /* synopsis: affinity(r[P1@P2])               */
 12435         -#define OP_MakeRecord     96 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        12610  +#define OP_SorterSort     55
        12611  +#define OP_Sort           56
        12612  +#define OP_Rewind         57
        12613  +#define OP_IdxLE          58 /* synopsis: key=r[P3@P4]                     */
        12614  +#define OP_IdxGT          59 /* synopsis: key=r[P3@P4]                     */
        12615  +#define OP_IdxLT          60 /* synopsis: key=r[P3@P4]                     */
        12616  +#define OP_IdxGE          61 /* synopsis: key=r[P3@P4]                     */
        12617  +#define OP_RowSetRead     62 /* synopsis: r[P3]=rowset(P1)                 */
        12618  +#define OP_RowSetTest     63 /* synopsis: if r[P3] in rowset(P1) goto P2   */
        12619  +#define OP_Program        64
        12620  +#define OP_FkIfZero       65 /* synopsis: if fkctr[P1]==0 goto P2          */
        12621  +#define OP_IfPos          66 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
        12622  +#define OP_IfNotZero      67 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
        12623  +#define OP_DecrJumpZero   68 /* synopsis: if (--r[P1])==0 goto P2          */
        12624  +#define OP_IncrVacuum     69
        12625  +#define OP_VNext          70
        12626  +#define OP_Init           71 /* synopsis: Start at P2                      */
        12627  +#define OP_Return         72
        12628  +#define OP_EndCoroutine   73
        12629  +#define OP_HaltIfNull     74 /* synopsis: if r[P3]=null halt               */
        12630  +#define OP_Halt           75
        12631  +#define OP_Integer        76 /* synopsis: r[P2]=P1                         */
        12632  +#define OP_Int64          77 /* synopsis: r[P2]=P4                         */
        12633  +#define OP_String         78 /* synopsis: r[P2]='P4' (len=P1)              */
        12634  +#define OP_Null           79 /* synopsis: r[P2..P3]=NULL                   */
        12635  +#define OP_SoftNull       80 /* synopsis: r[P1]=NULL                       */
        12636  +#define OP_Blob           81 /* synopsis: r[P2]=P4 (len=P1)                */
        12637  +#define OP_Variable       82 /* synopsis: r[P2]=parameter(P1,P4)           */
        12638  +#define OP_Move           83 /* synopsis: r[P2@P3]=r[P1@P3]                */
        12639  +#define OP_Copy           84 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
        12640  +#define OP_SCopy          85 /* synopsis: r[P2]=r[P1]                      */
        12641  +#define OP_IntCopy        86 /* synopsis: r[P2]=r[P1]                      */
        12642  +#define OP_ResultRow      87 /* synopsis: output=r[P1@P2]                  */
        12643  +#define OP_CollSeq        88
        12644  +#define OP_Function0      89 /* synopsis: r[P3]=func(r[P2@P5])             */
        12645  +#define OP_Function       90 /* synopsis: r[P3]=func(r[P2@P5])             */
        12646  +#define OP_AddImm         91 /* synopsis: r[P1]=r[P1]+P2                   */
        12647  +#define OP_RealAffinity   92
        12648  +#define OP_Cast           93 /* synopsis: affinity(r[P1])                  */
        12649  +#define OP_Permutation    94
        12650  +#define OP_Compare        95 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
        12651  +#define OP_Column         96 /* synopsis: r[P3]=PX                         */
 12436  12652   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 12437         -#define OP_Count          98 /* synopsis: r[P2]=count()                    */
 12438         -#define OP_ReadCookie     99
 12439         -#define OP_SetCookie     100
 12440         -#define OP_ReopenIdx     101 /* synopsis: root=P2 iDb=P3                   */
 12441         -#define OP_OpenRead      102 /* synopsis: root=P2 iDb=P3                   */
 12442         -#define OP_OpenWrite     103 /* synopsis: root=P2 iDb=P3                   */
 12443         -#define OP_OpenAutoindex 104 /* synopsis: nColumn=P2                       */
 12444         -#define OP_OpenEphemeral 105 /* synopsis: nColumn=P2                       */
 12445         -#define OP_SorterOpen    106
 12446         -#define OP_SequenceTest  107 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 12447         -#define OP_OpenPseudo    108 /* synopsis: P3 columns in r[P2]              */
 12448         -#define OP_Close         109
 12449         -#define OP_ColumnsUsed   110
 12450         -#define OP_Sequence      111 /* synopsis: r[P2]=cursor[P1].ctr++           */
 12451         -#define OP_NewRowid      112 /* synopsis: r[P2]=rowid                      */
 12452         -#define OP_Insert        113 /* synopsis: intkey=r[P3] data=r[P2]          */
 12453         -#define OP_InsertInt     114 /* synopsis: intkey=P3 data=r[P2]             */
 12454         -#define OP_Delete        115
 12455         -#define OP_ResetCount    116
 12456         -#define OP_SorterCompare 117 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 12457         -#define OP_SorterData    118 /* synopsis: r[P2]=data                       */
 12458         -#define OP_RowKey        119 /* synopsis: r[P2]=key                        */
 12459         -#define OP_RowData       120 /* synopsis: r[P2]=data                       */
 12460         -#define OP_Rowid         121 /* synopsis: r[P2]=rowid                      */
 12461         -#define OP_NullRow       122
 12462         -#define OP_SorterInsert  123
 12463         -#define OP_IdxInsert     124 /* synopsis: key=r[P2]                        */
 12464         -#define OP_IdxDelete     125 /* synopsis: key=r[P2@P3]                     */
 12465         -#define OP_Seek          126 /* synopsis: Move P3 to P1.rowid              */
 12466         -#define OP_IdxRowid      127 /* synopsis: r[P2]=rowid                      */
 12467         -#define OP_Destroy       128
 12468         -#define OP_Clear         129
 12469         -#define OP_ResetSorter   130
 12470         -#define OP_CreateIndex   131 /* synopsis: r[P2]=root iDb=P1                */
 12471         -#define OP_CreateTable   132 /* synopsis: r[P2]=root iDb=P1                */
        12653  +#define OP_Affinity       98 /* synopsis: affinity(r[P1@P2])               */
        12654  +#define OP_MakeRecord     99 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        12655  +#define OP_Count         100 /* synopsis: r[P2]=count()                    */
        12656  +#define OP_ReadCookie    101
        12657  +#define OP_SetCookie     102
        12658  +#define OP_ReopenIdx     103 /* synopsis: root=P2 iDb=P3                   */
        12659  +#define OP_OpenRead      104 /* synopsis: root=P2 iDb=P3                   */
        12660  +#define OP_OpenWrite     105 /* synopsis: root=P2 iDb=P3                   */
        12661  +#define OP_OpenAutoindex 106 /* synopsis: nColumn=P2                       */
        12662  +#define OP_OpenEphemeral 107 /* synopsis: nColumn=P2                       */
        12663  +#define OP_SorterOpen    108
        12664  +#define OP_SequenceTest  109 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        12665  +#define OP_OpenPseudo    110 /* synopsis: P3 columns in r[P2]              */
        12666  +#define OP_Close         111
        12667  +#define OP_ColumnsUsed   112
        12668  +#define OP_Sequence      113 /* synopsis: r[P2]=cursor[P1].ctr++           */
        12669  +#define OP_NewRowid      114 /* synopsis: r[P2]=rowid                      */
        12670  +#define OP_Insert        115 /* synopsis: intkey=r[P3] data=r[P2]          */
        12671  +#define OP_InsertInt     116 /* synopsis: intkey=P3 data=r[P2]             */
        12672  +#define OP_Delete        117
        12673  +#define OP_ResetCount    118
        12674  +#define OP_SorterCompare 119 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        12675  +#define OP_SorterData    120 /* synopsis: r[P2]=data                       */
        12676  +#define OP_RowKey        121 /* synopsis: r[P2]=key                        */
        12677  +#define OP_RowData       122 /* synopsis: r[P2]=data                       */
        12678  +#define OP_Rowid         123 /* synopsis: r[P2]=rowid                      */
        12679  +#define OP_NullRow       124
        12680  +#define OP_SorterInsert  125
        12681  +#define OP_IdxInsert     126 /* synopsis: key=r[P2]                        */
        12682  +#define OP_IdxDelete     127 /* synopsis: key=r[P2@P3]                     */
        12683  +#define OP_Seek          128 /* synopsis: Move P3 to P1.rowid              */
        12684  +#define OP_IdxRowid      129 /* synopsis: r[P2]=rowid                      */
        12685  +#define OP_Destroy       130
        12686  +#define OP_Clear         131
        12687  +#define OP_ResetSorter   132
 12472  12688   #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 12473         -#define OP_ParseSchema   134
 12474         -#define OP_LoadAnalysis  135
 12475         -#define OP_DropTable     136
 12476         -#define OP_DropIndex     137
 12477         -#define OP_DropTrigger   138
 12478         -#define OP_IntegrityCk   139
 12479         -#define OP_RowSetAdd     140 /* synopsis: rowset(P1)=r[P2]                 */
 12480         -#define OP_Param         141
 12481         -#define OP_FkCounter     142 /* synopsis: fkctr[P1]+=P2                    */
 12482         -#define OP_MemMax        143 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 12483         -#define OP_OffsetLimit   144 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 12484         -#define OP_AggStep0      145 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12485         -#define OP_AggStep       146 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 12486         -#define OP_AggFinal      147 /* synopsis: accum=r[P1] N=P2                 */
 12487         -#define OP_Expire        148
 12488         -#define OP_TableLock     149 /* synopsis: iDb=P1 root=P2 write=P3          */
 12489         -#define OP_VBegin        150
 12490         -#define OP_VCreate       151
 12491         -#define OP_VDestroy      152
 12492         -#define OP_VOpen         153
 12493         -#define OP_VColumn       154 /* synopsis: r[P3]=vcolumn(P2)                */
 12494         -#define OP_VRename       155
 12495         -#define OP_Pagecount     156
 12496         -#define OP_MaxPgcnt      157
 12497         -#define OP_CursorHint    158
 12498         -#define OP_Noop          159
 12499         -#define OP_Explain       160
        12689  +#define OP_CreateIndex   134 /* synopsis: r[P2]=root iDb=P1                */
        12690  +#define OP_CreateTable   135 /* synopsis: r[P2]=root iDb=P1                */
        12691  +#define OP_ParseSchema   136
        12692  +#define OP_LoadAnalysis  137
        12693  +#define OP_DropTable     138
        12694  +#define OP_DropIndex     139
        12695  +#define OP_DropTrigger   140
        12696  +#define OP_IntegrityCk   141
        12697  +#define OP_RowSetAdd     142 /* synopsis: rowset(P1)=r[P2]                 */
        12698  +#define OP_Param         143
        12699  +#define OP_FkCounter     144 /* synopsis: fkctr[P1]+=P2                    */
        12700  +#define OP_MemMax        145 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        12701  +#define OP_OffsetLimit   146 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        12702  +#define OP_AggStep0      147 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12703  +#define OP_AggStep       148 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        12704  +#define OP_AggFinal      149 /* synopsis: accum=r[P1] N=P2                 */
        12705  +#define OP_Expire        150
        12706  +#define OP_TableLock     151 /* synopsis: iDb=P1 root=P2 write=P3          */
        12707  +#define OP_VBegin        152
        12708  +#define OP_VCreate       153
        12709  +#define OP_VDestroy      154
        12710  +#define OP_VOpen         155
        12711  +#define OP_VColumn       156 /* synopsis: r[P3]=vcolumn(P2)                */
        12712  +#define OP_VRename       157
        12713  +#define OP_Pagecount     158
        12714  +#define OP_MaxPgcnt      159
        12715  +#define OP_CursorHint    160
        12716  +#define OP_Noop          161
        12717  +#define OP_Explain       162
 12500  12718   
 12501  12719   /* Properties such as "out2" or "jump" that are specified in
 12502  12720   ** comments following the "case" for each opcode in the vdbe.c
 12503  12721   ** are encoded into bitvectors as follows:
 12504  12722   */
 12505  12723   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 12506  12724   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 12509  12727   #define OPFLG_OUT2        0x10  /* out2:  P2 is an output */
 12510  12728   #define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
 12511  12729   #define OPFLG_INITIALIZER {\
 12512  12730   /*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
 12513  12731   /*   8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01,\
 12514  12732   /*  16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x03, 0x03, 0x09,\
 12515  12733   /*  24 */ 0x09, 0x09, 0x09, 0x26, 0x26, 0x09, 0x09, 0x09,\
 12516         -/*  32 */ 0x09, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
        12734  +/*  32 */ 0x09, 0x09, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 12517  12735   /*  40 */ 0x0b, 0x0b, 0x01, 0x26, 0x26, 0x26, 0x26, 0x26,\
 12518  12736   /*  48 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x01, 0x12, 0x01,\
 12519         -/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b, 0x01, 0x01,\
 12520         -/*  64 */ 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x02, 0x02,\
 12521         -/*  72 */ 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10,\
 12522         -/*  80 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
 12523         -/*  88 */ 0x00, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
 12524         -/*  96 */ 0x00, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
 12525         -/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
 12526         -/* 112 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12527         -/* 120 */ 0x00, 0x10, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
 12528         -/* 128 */ 0x10, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 12529         -/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
 12530         -/* 144 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 12531         -/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 12532         -/* 160 */ 0x00,}
        12737  +/*  56 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23, 0x0b,\
        12738  +/*  64 */ 0x01, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01,\
        12739  +/*  72 */ 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10,\
        12740  +/*  80 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00,\
        12741  +/*  88 */ 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
        12742  +/*  96 */ 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        12743  +/* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        12744  +/* 112 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        12745  +/* 120 */ 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x04, 0x00,\
        12746  +/* 128 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x10,\
        12747  +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10,\
        12748  +/* 144 */ 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00,\
        12749  +/* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
        12750  +/* 160 */ 0x00, 0x00, 0x00,}
 12533  12751   
 12534  12752   /* The sqlite3P2Values() routine is able to run faster if it knows
 12535  12753   ** the value of the largest JUMP opcode.  The smaller the maximum
 12536  12754   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 12537  12755   ** generated this include file strives to group all JUMP opcodes
 12538  12756   ** together near the beginning of the list.
 12539  12757   */
 12540         -#define SQLITE_MX_JUMP_OPCODE  69  /* Maximum JUMP opcode */
        12758  +#define SQLITE_MX_JUMP_OPCODE  71  /* Maximum JUMP opcode */
 12541  12759   
 12542  12760   /************** End of opcodes.h *********************************************/
 12543  12761   /************** Continuing where we left off in vdbe.h ***********************/
 12544  12762   
 12545  12763   /*
 12546  12764   ** Prototypes for the VDBE interface.  See comments on the implementation
 12547  12765   ** for a description of what each of these routines does.
................................................................................
 12680  12898   
 12681  12899   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 12682  12900   SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
 12683  12901   #else
 12684  12902   # define sqlite3VdbeScanStatus(a,b,c,d,e)
 12685  12903   #endif
 12686  12904   
 12687         -#endif
        12905  +#endif /* SQLITE_VDBE_H */
 12688  12906   
 12689  12907   /************** End of vdbe.h ************************************************/
 12690  12908   /************** Continuing where we left off in sqliteInt.h ******************/
 12691  12909   /************** Include pager.h in the middle of sqliteInt.h *****************/
 12692  12910   /************** Begin file pager.h *******************************************/
 12693  12911   /*
 12694  12912   ** 2001 September 15
................................................................................
 12702  12920   **
 12703  12921   *************************************************************************
 12704  12922   ** This header file defines the interface that the sqlite page cache
 12705  12923   ** subsystem.  The page cache subsystem reads and writes a file a page
 12706  12924   ** at a time and provides a journal for rollback.
 12707  12925   */
 12708  12926   
 12709         -#ifndef _PAGER_H_
 12710         -#define _PAGER_H_
        12927  +#ifndef SQLITE_PAGER_H
        12928  +#define SQLITE_PAGER_H
 12711  12929   
 12712  12930   /*
 12713  12931   ** Default maximum size for persistent journal files. A negative 
 12714  12932   ** value means no limit. This value may be overridden using the 
 12715  12933   ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
 12716  12934   */
 12717  12935   #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
................................................................................
 12918  13136     void disable_simulated_io_errors(void);
 12919  13137     void enable_simulated_io_errors(void);
 12920  13138   #else
 12921  13139   # define disable_simulated_io_errors()
 12922  13140   # define enable_simulated_io_errors()
 12923  13141   #endif
 12924  13142   
 12925         -#endif /* _PAGER_H_ */
        13143  +#endif /* SQLITE_PAGER_H */
 12926  13144   
 12927  13145   /************** End of pager.h ***********************************************/
 12928  13146   /************** Continuing where we left off in sqliteInt.h ******************/
 12929  13147   /************** Include pcache.h in the middle of sqliteInt.h ****************/
 12930  13148   /************** Begin file pcache.h ******************************************/
 12931  13149   /*
 12932  13150   ** 2008 August 05
................................................................................
 13156  13374   **    May you share freely, never taking more than you give.
 13157  13375   **
 13158  13376   ******************************************************************************
 13159  13377   **
 13160  13378   ** This file contains pre-processor directives related to operating system
 13161  13379   ** detection and/or setup.
 13162  13380   */
 13163         -#ifndef _OS_SETUP_H_
 13164         -#define _OS_SETUP_H_
        13381  +#ifndef SQLITE_OS_SETUP_H
        13382  +#define SQLITE_OS_SETUP_H
 13165  13383   
 13166  13384   /*
 13167  13385   ** Figure out if we are dealing with Unix, Windows, or some other operating
 13168  13386   ** system.
 13169  13387   **
 13170  13388   ** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
 13171  13389   ** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
................................................................................
 13197  13415   #  endif
 13198  13416   #else
 13199  13417   #  ifndef SQLITE_OS_WIN
 13200  13418   #    define SQLITE_OS_WIN 0
 13201  13419   #  endif
 13202  13420   #endif
 13203  13421   
 13204         -#endif /* _OS_SETUP_H_ */
        13422  +#endif /* SQLITE_OS_SETUP_H */
 13205  13423   
 13206  13424   /************** End of os_setup.h ********************************************/
 13207  13425   /************** Continuing where we left off in os.h *************************/
 13208  13426   
 13209  13427   /* If the SET_FULLSYNC macro is not defined above, then make it
 13210  13428   ** a no-op
 13211  13429   */
................................................................................
 13502  13720   ** Each database file to be accessed by the system is an instance
 13503  13721   ** of the following structure.  There are normally two of these structures
 13504  13722   ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
 13505  13723   ** aDb[1] is the database file used to hold temporary tables.  Additional
 13506  13724   ** databases may be attached.
 13507  13725   */
 13508  13726   struct Db {
 13509         -  char *zName;         /* Name of this database */
        13727  +  char *zDbSName;      /* Name of this database. (schema name, not filename) */
 13510  13728     Btree *pBt;          /* The B*Tree structure for this database file */
 13511  13729     u8 safety_level;     /* How aggressive at syncing data to disk */
 13512  13730     u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
 13513  13731     Schema *pSchema;     /* Pointer to database schema (possibly shared) */
 13514  13732   };
 13515  13733   
 13516  13734   /*
................................................................................
 13654  13872     typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
 13655  13873                                  const char*, const char*);
 13656  13874   #else
 13657  13875     typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
 13658  13876                                  const char*);
 13659  13877   #endif
 13660  13878   
        13879  +#ifndef SQLITE_OMIT_DEPRECATED
        13880  +/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
        13881  +** in the style of sqlite3_trace()
        13882  +*/
        13883  +#define SQLITE_TRACE_LEGACY  0x80
        13884  +#else
        13885  +#define SQLITE_TRACE_LEGACY  0
        13886  +#endif /* SQLITE_OMIT_DEPRECATED */
        13887  +
 13661  13888   
 13662  13889   /*
 13663  13890   ** Each database connection is an instance of the following structure.
 13664  13891   */
 13665  13892   struct sqlite3 {
 13666  13893     sqlite3_vfs *pVfs;            /* OS Interface */
 13667  13894     struct Vdbe *pVdbe;           /* List of active virtual machines */
................................................................................
 13683  13910     u8 mallocFailed;              /* True if we have seen a malloc failure */
 13684  13911     u8 bBenignMalloc;             /* Do not require OOMs if true */
 13685  13912     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 13686  13913     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 13687  13914     u8 suppressErr;               /* Do not issue error messages if true */
 13688  13915     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 13689  13916     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
        13917  +  u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
 13690  13918     int nextPagesize;             /* Pagesize after VACUUM if >0 */
 13691  13919     u32 magic;                    /* Magic number for detect library misuse */
 13692  13920     int nChange;                  /* Value returned by sqlite3_changes() */
 13693  13921     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 13694  13922     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 13695  13923     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 13696  13924     struct sqlite3InitInfo {      /* Information used during initialization */
................................................................................
 13703  13931     int nVdbeActive;              /* Number of VDBEs currently running */
 13704  13932     int nVdbeRead;                /* Number of active VDBEs that read or write */
 13705  13933     int nVdbeWrite;               /* Number of active VDBEs that read and write */
 13706  13934     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 13707  13935     int nVDestroy;                /* Number of active OP_VDestroy operations */
 13708  13936     int nExtension;               /* Number of loaded extensions */
 13709  13937     void **aExtension;            /* Array of shared library handles */
 13710         -  void (*xTrace)(void*,const char*);        /* Trace function */
        13938  +  int (*xTrace)(u32,void*,void*,void*);     /* Trace function */
 13711  13939     void *pTraceArg;                          /* Argument to the trace function */
 13712  13940     void (*xProfile)(void*,const char*,u64);  /* Profiling function */
 13713  13941     void *pProfileArg;                        /* Argument to profile function */
 13714  13942     void *pCommitArg;                 /* Argument to xCommitCallback() */
 13715  13943     int (*xCommitCallback)(void*);    /* Invoked at every commit. */
 13716  13944     void *pRollbackArg;               /* Argument to xRollbackCallback() */
 13717  13945     void (*xRollbackCallback)(void*); /* Invoked at every commit. */
................................................................................
 14128  14356   ** changing the affinity.
 14129  14357   **
 14130  14358   ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
 14131  14359   ** It causes an assert() to fire if either operand to a comparison
 14132  14360   ** operator is NULL.  It is added to certain comparison operators to
 14133  14361   ** prove that the operands are always NOT NULL.
 14134  14362   */
        14363  +#define SQLITE_KEEPNULL     0x08  /* Used by vector == or <> */
 14135  14364   #define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
 14136  14365   #define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
 14137  14366   #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
 14138  14367   #define SQLITE_NOTNULL      0x90  /* Assert that operands are never NULL */
 14139  14368   
 14140  14369   /*
 14141  14370   ** An object of this type is created for each virtual table present in
................................................................................
 14692  14921   
 14693  14922   #if SQLITE_MAX_EXPR_DEPTH>0
 14694  14923     int nHeight;           /* Height of the tree headed by this node */
 14695  14924   #endif
 14696  14925     int iTable;            /* TK_COLUMN: cursor number of table holding column
 14697  14926                            ** TK_REGISTER: register number
 14698  14927                            ** TK_TRIGGER: 1 -> new, 0 -> old
 14699         -                         ** EP_Unlikely:  134217728 times likelihood */
        14928  +                         ** EP_Unlikely:  134217728 times likelihood
        14929  +                         ** TK_SELECT: 1st register of result vector */
 14700  14930     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 14701         -                         ** TK_VARIABLE: variable number (always >= 1). */
        14931  +                         ** TK_VARIABLE: variable number (always >= 1).
        14932  +                         ** TK_SELECT_COLUMN: column of the result vector */
 14702  14933     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 14703  14934     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 14704  14935     u8 op2;                /* TK_REGISTER: original value of Expr.op
 14705  14936                            ** TK_COLUMN: the value of p5 for OP_Column
 14706  14937                            ** TK_AGG_FUNCTION: nesting depth */
 14707  14938     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 14708  14939     Table *pTab;           /* Table for TK_COLUMN expressions. */
................................................................................
 14898  15129       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 14899  15130       Table *pTab;      /* An SQL table corresponding to zName */
 14900  15131       Select *pSelect;  /* A SELECT statement used in place of a table name */
 14901  15132       int addrFillSub;  /* Address of subroutine to manifest a subquery */
 14902  15133       int regReturn;    /* Register holding return address of addrFillSub */
 14903  15134       int regResult;    /* Registers holding results of a co-routine */
 14904  15135       struct {
 14905         -      u8 jointype;      /* Type of join between this able and the previous */
        15136  +      u8 jointype;      /* Type of join between this table and the previous */
 14906  15137         unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 14907  15138         unsigned isIndexedBy :1;   /* True if there is an INDEXED BY clause */
 14908  15139         unsigned isTabFunc :1;     /* True if table-valued-function syntax */
 14909  15140         unsigned isCorrelated :1;  /* True if sub-query is correlated */
 14910  15141         unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 14911  15142         unsigned isRecursive :1;   /* True for recursive reference in WITH */
 14912  15143       } fg;
................................................................................
 14944  15175   ** Value constraints (enforced via assert()):
 14945  15176   **     WHERE_USE_LIMIT  == SF_FixedLimit
 14946  15177   */
 14947  15178   #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 14948  15179   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 14949  15180   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 14950  15181   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 14951         -#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 14952         -#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 14953         -#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 14954         -#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 14955         -#define WHERE_NO_AUTOINDEX     0x0080 /* Disallow automatic indexes */
 14956         -#define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
 14957         -#define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
 14958         -#define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
 14959         -#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
 14960         -#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
 14961         -#define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */
 14962         -#define WHERE_USE_LIMIT        0x4000 /* There is a constant LIMIT clause */
 14963         -#define WHERE_SEEK_TABLE       0x8000 /* Do not defer seeks on main table */
        15182  +#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
        15183  +#define WHERE_DUPLICATES_OK    0x0010 /* Ok to return a row more than once */
        15184  +#define WHERE_OR_SUBCLAUSE     0x0020 /* Processing a sub-WHERE as part of
        15185  +                                      ** the OR optimization  */
        15186  +#define WHERE_GROUPBY          0x0040 /* pOrderBy is really a GROUP BY */
        15187  +#define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
        15188  +#define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
        15189  +#define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
        15190  +#define WHERE_SEEK_TABLE       0x0400 /* Do not defer seeks on main table */
        15191  +#define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
        15192  +                        /*     0x1000    not currently used */
        15193  +                        /*     0x2000    not currently used */
        15194  +#define WHERE_USE_LIMIT        0x4000 /* Use the LIMIT in cost estimates */
        15195  +                        /*     0x8000    not currently used */
 14964  15196   
 14965  15197   /* Allowed return values from sqlite3WhereIsDistinct()
 14966  15198   */
 14967  15199   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 14968  15200   #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 14969  15201   #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 14970  15202   #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
................................................................................
 15174  15406   
 15175  15407   /*
 15176  15408   ** An instance of this object describes where to put of the results of
 15177  15409   ** a SELECT statement.
 15178  15410   */
 15179  15411   struct SelectDest {
 15180  15412     u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
 15181         -  char affSdst;        /* Affinity used when eDest==SRT_Set */
        15413  +  char *zAffSdst;      /* Affinity used when eDest==SRT_Set */
 15182  15414     int iSDParm;         /* A parameter used by the eDest disposal method */
 15183  15415     int iSdst;           /* Base register where results are written */
 15184  15416     int nSdst;           /* Number of registers allocated */
 15185  15417     ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
 15186  15418   };
 15187  15419   
 15188  15420   /*
................................................................................
 15287  15519     int aTempReg[8];     /* Holding area for temporary registers */
 15288  15520     int nRangeReg;       /* Size of the temporary register block */
 15289  15521     int iRangeReg;       /* First register in temporary register block */
 15290  15522     int nErr;            /* Number of errors seen */
 15291  15523     int nTab;            /* Number of previously allocated VDBE cursors */
 15292  15524     int nMem;            /* Number of memory cells used so far */
 15293  15525     int nSet;            /* Number of sets used so far */
 15294         -  int nOnce;           /* Number of OP_Once instructions so far */
 15295  15526     int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
 15296  15527     int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
 15297  15528     int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
 15298  15529     int ckBase;          /* Base register of data during check constraints */
 15299  15530     int iSelfTab;        /* Table of an index whose exprs are being coded */
 15300  15531     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 15301  15532     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
................................................................................
 15621  15852     void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 15622  15853     void *pVdbeBranchArg;                                     /* 1st argument */
 15623  15854   #endif
 15624  15855   #ifndef SQLITE_OMIT_BUILTIN_TEST
 15625  15856     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
 15626  15857   #endif
 15627  15858     int bLocaltimeFault;              /* True to fail localtime() calls */
        15859  +  int iOnceResetThreshold;          /* When to reset OP_Once counters */
 15628  15860   };
 15629  15861   
 15630  15862   /*
 15631  15863   ** This macro is used inside of assert() statements to indicate that
 15632  15864   ** the assert is only valid on a well-formed database.  Instead of:
 15633  15865   **
 15634  15866   **     assert( X );
................................................................................
 15659  15891       NameContext *pNC;                          /* Naming context */
 15660  15892       int n;                                     /* A counter */
 15661  15893       int iCur;                                  /* A cursor number */
 15662  15894       SrcList *pSrcList;                         /* FROM clause */
 15663  15895       struct SrcCount *pSrcCount;                /* Counting column references */
 15664  15896       struct CCurHint *pCCurHint;                /* Used by codeCursorHint() */
 15665  15897       int *aiCol;                                /* array of column indexes */
        15898  +    struct IdxCover *pIdxCover;                /* Check for index coverage */
 15666  15899     } u;
 15667  15900   };
 15668  15901   
 15669  15902   /* Forward declarations */
 15670  15903   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 15671  15904   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 15672  15905   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 15842  16075   # define sqlite3StackFree(D,P)
 15843  16076   #else
 15844  16077   # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
 15845  16078   # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
 15846  16079   # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
 15847  16080   #endif
 15848  16081   
 15849         -#ifdef SQLITE_ENABLE_MEMSYS3
 15850         -SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 15851         -#endif
        16082  +/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together.  If they
        16083  +** are, disable MEMSYS3
        16084  +*/
 15852  16085   #ifdef SQLITE_ENABLE_MEMSYS5
 15853  16086   SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
        16087  +#undef SQLITE_ENABLE_MEMSYS3
        16088  +#endif
        16089  +#ifdef SQLITE_ENABLE_MEMSYS3
        16090  +SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 15854  16091   #endif
 15855  16092   
 15856  16093   
 15857  16094   #ifndef SQLITE_MUTEX_OMIT
 15858  16095   SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
 15859  16096   SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
 15860  16097   SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
................................................................................
 15901  16138   #endif
 15902  16139   #if defined(SQLITE_TEST)
 15903  16140   SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 15904  16141   #endif
 15905  16142   
 15906  16143   #if defined(SQLITE_DEBUG)
 15907  16144   SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
        16145  +SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
 15908  16146   SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
 15909  16147   SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
 15910  16148   SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
 15911  16149   #endif
 15912  16150   
 15913  16151   
 15914  16152   SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
................................................................................
 15932  16170   SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 15933  16171   SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
 15934  16172   SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 15935  16173   SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 15936  16174   SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
 15937  16175   SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 15938  16176   SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
        16177  +SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
 15939  16178   SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
 15940  16179   SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 15941  16180   SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
 15942  16181   SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 15943  16182   SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
 15944  16183   SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 15945  16184   SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
................................................................................
 16029  16268   SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
 16030  16269   SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
 16031  16270   SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 16032  16271   SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 16033  16272   SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 16034  16273   SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 16035  16274   SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
 16036         -SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 16037         -                          Expr*, int, int);
        16275  +SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
        16276  +                          Expr*, int, int, u8);
 16038  16277   SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 16039  16278   SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 16040  16279   SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 16041  16280                            Expr*,ExprList*,u32,Expr*,Expr*);
 16042  16281   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 16043  16282   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 16044  16283   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
................................................................................
 16049  16288   SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 16050  16289   SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 16051  16290   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 16052  16291   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 16053  16292   SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
 16054  16293   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 16055  16294   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
        16295  +SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo*);
 16056  16296   SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
 16057  16297   SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
 16058  16298   SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
 16059  16299   SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
 16060  16300   #define ONEPASS_OFF      0        /* Use of ONEPASS not allowed */
 16061  16301   #define ONEPASS_SINGLE   1        /* ONEPASS valid for a single row update */
 16062  16302   #define ONEPASS_MULTI    2        /* ONEPASS is valid for multiple rows */
................................................................................
 16082  16322   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
 16083  16323   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
 16084  16324   #define SQLITE_ECEL_REF      0x04  /* Use ExprList.u.x.iOrderByCol */
 16085  16325   SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 16086  16326   SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 16087  16327   SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
 16088  16328   SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 16089         -SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
 16090         -SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
        16329  +#define LOCATE_VIEW    0x01
        16330  +#define LOCATE_NOERR   0x02
        16331  +SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
        16332  +SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
 16091  16333   SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 16092  16334   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 16093  16335   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 16094         -SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 16095         -SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
        16336  +SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
        16337  +SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 16096  16338   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 16097  16339   SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
 16098  16340   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 16099  16341   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
 16100  16342   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 16101  16343   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
        16344  +SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 16102  16345   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 16103  16346   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 16104  16347   #ifndef SQLITE_OMIT_BUILTIN_TEST
 16105  16348   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 16106  16349   SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 16107  16350   #endif
 16108  16351   SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
................................................................................
 16263  16506   #define putVarint    sqlite3PutVarint
 16264  16507   
 16265  16508   
 16266  16509   SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
 16267  16510   SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
 16268  16511   SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 16269  16512   SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
        16513  +SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
 16270  16514   SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 16271  16515   SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 16272  16516   SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
 16273  16517   SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
 16274  16518   SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
 16275  16519   SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
 16276  16520   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
................................................................................
 16382  16626   SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 16383  16627   SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 16384  16628   SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 16385  16629   SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 16386  16630   
 16387  16631   SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 16388  16632   SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
        16633  +
        16634  +#ifndef SQLITE_OMIT_SUBQUERY
        16635  +SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
        16636  +#else
        16637  +# define sqlite3ExprCheckIN(x,y) SQLITE_OK
        16638  +#endif
 16389  16639   
 16390  16640   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 16391  16641   SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
 16392         -SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
        16642  +SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
        16643  +    Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
 16393  16644   SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
 16394  16645   SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
 16395  16646   SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
        16647  +SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
 16396  16648   #endif
 16397  16649   
 16398  16650   /*
 16399  16651   ** The interface to the LEMON-generated parser
 16400  16652   */
 16401  16653   SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
 16402  16654   SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
................................................................................
 16541  16793   #define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
 16542  16794   /*
 16543  16795   ** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
 16544  16796   */
 16545  16797   #define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
 16546  16798   #define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
 16547  16799   #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
 16548         -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
        16800  +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*);
 16549  16801   
 16550  16802   SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 16551  16803   SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
 16552  16804   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 16553  16805   SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 16554  16806   #endif
 16555  16807   
................................................................................
 16646  16898   SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
 16647  16899   #endif
 16648  16900   
 16649  16901   #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
 16650  16902   SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
 16651  16903   #endif
 16652  16904   
 16653         -#endif /* _SQLITEINT_H_ */
        16905  +SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
        16906  +SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
        16907  +SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
        16908  +SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
        16909  +
        16910  +#endif /* SQLITEINT_H */
 16654  16911   
 16655  16912   /************** End of sqliteInt.h *******************************************/
 16656  16913   /************** Begin file global.c ******************************************/
 16657  16914   /*
 16658  16915   ** 2008 June 13
 16659  16916   **
 16660  16917   ** The author disclaims copyright to this source code.  In place of
................................................................................
 16877  17134   #ifdef SQLITE_VDBE_COVERAGE
 16878  17135      0,                         /* xVdbeBranch */
 16879  17136      0,                         /* pVbeBranchArg */
 16880  17137   #endif
 16881  17138   #ifndef SQLITE_OMIT_BUILTIN_TEST
 16882  17139      0,                         /* xTestCallback */
 16883  17140   #endif
 16884         -   0                          /* bLocaltimeFault */
        17141  +   0,                         /* bLocaltimeFault */
        17142  +   0x7ffffffe                 /* iOnceResetThreshold */
 16885  17143   };
 16886  17144   
 16887  17145   /*
 16888  17146   ** Hash table for global functions - functions common to all
 16889  17147   ** database connections.  After initialization, this table is
 16890  17148   ** read-only.
 16891  17149   */
................................................................................
 16981  17239   #endif
 16982  17240   #if SQLITE_CASE_SENSITIVE_LIKE
 16983  17241     "CASE_SENSITIVE_LIKE",
 16984  17242   #endif
 16985  17243   #if SQLITE_CHECK_PAGES
 16986  17244     "CHECK_PAGES",
 16987  17245   #endif
        17246  +#if defined(__clang__) && defined(__clang_major__)
        17247  +  "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
        17248  +                    CTIMEOPT_VAL(__clang_minor__) "."
        17249  +                    CTIMEOPT_VAL(__clang_patchlevel__),
        17250  +#elif defined(_MSC_VER)
        17251  +  "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
        17252  +#elif defined(__GNUC__) && defined(__VERSION__)
        17253  +  "COMPILER=gcc-" __VERSION__,
        17254  +#endif
 16988  17255   #if SQLITE_COVERAGE_TEST
 16989  17256     "COVERAGE_TEST",
 16990  17257   #endif
 16991  17258   #if SQLITE_DEBUG
 16992  17259     "DEBUG",
 16993  17260   #endif
 16994  17261   #if SQLITE_DEFAULT_LOCKING_MODE
................................................................................
 17000  17267   #if SQLITE_DISABLE_DIRSYNC
 17001  17268     "DISABLE_DIRSYNC",
 17002  17269   #endif
 17003  17270   #if SQLITE_DISABLE_LFS
 17004  17271     "DISABLE_LFS",
 17005  17272   #endif
 17006  17273   #if SQLITE_ENABLE_8_3_NAMES
 17007         -  "ENABLE_8_3_NAMES",
        17274  +  "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
 17008  17275   #endif
 17009  17276   #if SQLITE_ENABLE_API_ARMOR
 17010  17277     "ENABLE_API_ARMOR",
 17011  17278   #endif
 17012  17279   #if SQLITE_ENABLE_ATOMIC_WRITE
 17013  17280     "ENABLE_ATOMIC_WRITE",
 17014  17281   #endif
................................................................................
 17414  17681   *************************************************************************
 17415  17682   ** This is the header file for information that is private to the
 17416  17683   ** VDBE.  This information used to all be at the top of the single
 17417  17684   ** source code file "vdbe.c".  When that file became too big (over
 17418  17685   ** 6000 lines long) it was split up into several smaller files and
 17419  17686   ** this header information was factored out.
 17420  17687   */
 17421         -#ifndef _VDBEINT_H_
 17422         -#define _VDBEINT_H_
        17688  +#ifndef SQLITE_VDBEINT_H
        17689  +#define SQLITE_VDBEINT_H
 17423  17690   
 17424  17691   /*
 17425  17692   ** The maximum number of times that a statement will try to reparse
 17426  17693   ** itself before giving up and returning SQLITE_SCHEMA.
 17427  17694   */
 17428  17695   #ifndef SQLITE_MAX_SCHEMA_RETRY
 17429  17696   # define SQLITE_MAX_SCHEMA_RETRY 50
................................................................................
 17556  17823   typedef struct VdbeFrame VdbeFrame;
 17557  17824   struct VdbeFrame {
 17558  17825     Vdbe *v;                /* VM this frame belongs to */
 17559  17826     VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 17560  17827     Op *aOp;                /* Program instructions for parent frame */
 17561  17828     i64 *anExec;            /* Event counters from parent frame */
 17562  17829     Mem *aMem;              /* Array of memory cells for parent frame */
 17563         -  u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
 17564  17830     VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
 17565  17831     void *token;            /* Copy of SubProgram.token */
 17566  17832     i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 17567  17833     AuxData *pAuxData;      /* Linked list of auxdata allocations */
 17568  17834     int nCursor;            /* Number of entries in apCsr */
 17569  17835     int pc;                 /* Program Counter in parent (calling) frame */
 17570  17836     int nOp;                /* Size of aOp array */
 17571  17837     int nMem;               /* Number of entries in aMem */
 17572         -  int nOnceFlag;          /* Number of entries in aOnceFlag */
 17573  17838     int nChildMem;          /* Number of memory cells for child frame */
 17574  17839     int nChildCsr;          /* Number of cursors for child frame */
 17575  17840     int nChange;            /* Statement changes (Vdbe.nChange)     */
 17576  17841     int nDbChange;          /* Value of db->nChange */
 17577  17842   };
 17578  17843   
 17579  17844   #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
................................................................................
 17809  18074     char *zSql;             /* Text of the SQL statement that generated this */
 17810  18075     void *pFree;            /* Free this when deleting the vdbe */
 17811  18076     VdbeFrame *pFrame;      /* Parent frame */
 17812  18077     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 17813  18078     int nFrame;             /* Number of frames in pFrame list */
 17814  18079     u32 expmask;            /* Binding to these vars invalidates VM */
 17815  18080     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 17816         -  int nOnceFlag;          /* Size of array aOnceFlag[] */
 17817         -  u8 *aOnceFlag;          /* Flags for OP_Once */
 17818  18081     AuxData *pAuxData;      /* Linked list of auxdata allocations */
 17819  18082   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 17820  18083     i64 *anExec;            /* Number of times each op has been executed */
 17821  18084     int nScan;              /* Entries in aScan[] */
 17822  18085     ScanStatus *aScan;      /* Scan definitions for sqlite3_stmt_scanstatus() */
 17823  18086   #endif
 17824  18087   };
................................................................................
 17957  18220   SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
 17958  18221     #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
 17959  18222   #else
 17960  18223     #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
 17961  18224     #define ExpandBlob(P) SQLITE_OK
 17962  18225   #endif
 17963  18226   
 17964         -#endif /* !defined(_VDBEINT_H_) */
        18227  +#endif /* !defined(SQLITE_VDBEINT_H) */
 17965  18228   
 17966  18229   /************** End of vdbeInt.h *********************************************/
 17967  18230   /************** Continuing where we left off in status.c *********************/
 17968  18231   
 17969  18232   /*
 17970  18233   ** Variables in which to record status information.
 17971  18234   */
................................................................................
 18104  18367       wsdStat.mxValue[op] = wsdStat.nowValue[op];
 18105  18368     }
 18106  18369     sqlite3_mutex_leave(pMutex);
 18107  18370     (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
 18108  18371     return SQLITE_OK;
 18109  18372   }
 18110  18373   SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
 18111         -  sqlite3_int64 iCur, iHwtr;
        18374  +  sqlite3_int64 iCur = 0, iHwtr = 0;
 18112  18375     int rc;
 18113  18376   #ifdef SQLITE_ENABLE_API_ARMOR
 18114  18377     if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
 18115  18378   #endif
 18116  18379     rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
 18117  18380     if( rc==0 ){
 18118  18381       *pCurrent = (int)iCur;
................................................................................
 18165  18428       }
 18166  18429   
 18167  18430       /* 
 18168  18431       ** Return an approximation for the amount of memory currently used
 18169  18432       ** by all pagers associated with the given database connection.  The
 18170  18433       ** highwater mark is meaningless and is returned as zero.
 18171  18434       */
        18435  +    case SQLITE_DBSTATUS_CACHE_USED_SHARED:
 18172  18436       case SQLITE_DBSTATUS_CACHE_USED: {
 18173  18437         int totalUsed = 0;
 18174  18438         int i;
 18175  18439         sqlite3BtreeEnterAll(db);
 18176  18440         for(i=0; i<db->nDb; i++){
 18177  18441           Btree *pBt = db->aDb[i].pBt;
 18178  18442           if( pBt ){
 18179  18443             Pager *pPager = sqlite3BtreePager(pBt);
 18180         -          totalUsed += sqlite3PagerMemUsed(pPager);
        18444  +          int nByte = sqlite3PagerMemUsed(pPager);
        18445  +          if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
        18446  +            nByte = nByte / sqlite3BtreeConnectionCount(pBt);
        18447  +          }
        18448  +          totalUsed += nByte;
 18181  18449           }
 18182  18450         }
 18183  18451         sqlite3BtreeLeaveAll(db);
 18184  18452         *pCurrent = totalUsed;
 18185  18453         *pHighwater = 0;
 18186  18454         break;
 18187  18455       }
................................................................................
 19407  19675   static void currentTimeFunc(
 19408  19676     sqlite3_context *context,
 19409  19677     int argc,
 19410  19678     sqlite3_value **argv
 19411  19679   ){
 19412  19680     time_t t;
 19413  19681     char *zFormat = (char *)sqlite3_user_data(context);
 19414         -  sqlite3 *db;
 19415  19682     sqlite3_int64 iT;
 19416  19683     struct tm *pTm;
 19417  19684     struct tm sNow;
 19418  19685     char zBuf[20];
 19419  19686   
 19420  19687     UNUSED_PARAMETER(argc);
 19421  19688     UNUSED_PARAMETER(argv);
................................................................................
 19476  19743   **    May you share freely, never taking more than you give.
 19477  19744   **
 19478  19745   ******************************************************************************
 19479  19746   **
 19480  19747   ** This file contains OS interface code that is common to all
 19481  19748   ** architectures.
 19482  19749   */
 19483         -#define _SQLITE_OS_C_ 1
 19484  19750   /* #include "sqliteInt.h" */
 19485         -#undef _SQLITE_OS_C_
 19486  19751   
 19487  19752   /*
 19488  19753   ** If we compile with the SQLITE_TEST macro set, then the following block
 19489  19754   ** of code will give us the ability to simulate a disk I/O error.  This
 19490  19755   ** is used for testing the I/O recovery logic.
 19491  19756   */
 19492  19757   #if defined(SQLITE_TEST)
................................................................................
 22991  23256   **    May you share freely, never taking more than you give.
 22992  23257   **
 22993  23258   ******************************************************************************
 22994  23259   **
 22995  23260   ** This file contains inline asm code for retrieving "high-performance"
 22996  23261   ** counters for x86 class CPUs.
 22997  23262   */
 22998         -#ifndef _HWTIME_H_
 22999         -#define _HWTIME_H_
        23263  +#ifndef SQLITE_HWTIME_H
        23264  +#define SQLITE_HWTIME_H
 23000  23265   
 23001  23266   /*
 23002  23267   ** The following routine only works on pentium-class (or newer) processors.
 23003  23268   ** It uses the RDTSC opcode to read the cycle count value out of the
 23004  23269   ** processor and returns that value.  This can be used for high-res
 23005  23270   ** profiling.
 23006  23271   */
................................................................................
 23060  23325     ** of the debugging and testing utilities, but it should at
 23061  23326     ** least compile and run.
 23062  23327     */
 23063  23328   SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 23064  23329   
 23065  23330   #endif
 23066  23331   
 23067         -#endif /* !defined(_HWTIME_H_) */
        23332  +#endif /* !defined(SQLITE_HWTIME_H) */
 23068  23333   
 23069  23334   /************** End of hwtime.h **********************************************/
 23070  23335   /************** Continuing where we left off in os_common.h ******************/
 23071  23336   
 23072  23337   static sqlite_uint64 g_start;
 23073  23338   static sqlite_uint64 g_elapsed;
 23074  23339   #define TIMER_START       g_start=sqlite3Hwtime()
................................................................................
 23150  23415   **    May you find forgiveness for yourself and forgive others.
 23151  23416   **    May you share freely, never taking more than you give.
 23152  23417   **
 23153  23418   ******************************************************************************
 23154  23419   **
 23155  23420   ** This file contains code that is specific to Windows.
 23156  23421   */
 23157         -#ifndef _OS_WIN_H_
 23158         -#define _OS_WIN_H_
        23422  +#ifndef SQLITE_OS_WIN_H
        23423  +#define SQLITE_OS_WIN_H
 23159  23424   
 23160  23425   /*
 23161  23426   ** Include the primary Windows SDK header file.
 23162  23427   */
 23163  23428   #include "windows.h"
 23164  23429   
 23165  23430   #ifdef __CYGWIN__
................................................................................
 23223  23488   #if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
 23224  23489       SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
 23225  23490   # define SQLITE_OS_WIN_THREADS 1
 23226  23491   #else
 23227  23492   # define SQLITE_OS_WIN_THREADS 0
 23228  23493   #endif
 23229  23494   
 23230         -#endif /* _OS_WIN_H_ */
        23495  +#endif /* SQLITE_OS_WIN_H */
 23231  23496   
 23232  23497   /************** End of os_win.h **********************************************/
 23233  23498   /************** Continuing where we left off in mutex_w32.c ******************/
 23234  23499   #endif
 23235  23500   
 23236  23501   /*
 23237  23502   ** The code in this file is only used if we are compiling multithreaded
................................................................................
 25649  25914       }
 25650  25915       sqlite3TreeViewPop(pView);
 25651  25916     }
 25652  25917   }
 25653  25918   
 25654  25919   
 25655  25920   /*
 25656         -** Generate a human-readable description of a the Select object.
        25921  +** Generate a human-readable description of a Select object.
 25657  25922   */
 25658  25923   SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
 25659  25924     int n = 0;
 25660  25925     int cnt = 0;
 25661  25926     pView = sqlite3TreeViewPush(pView, moreToFollow);
 25662  25927     if( p->pWith ){
 25663  25928       sqlite3TreeViewWith(pView, p->pWith, 1);
................................................................................
 25974  26239           case OE_Fail:       zType = "fail";      break;
 25975  26240           case OE_Ignore:     zType = "ignore";    break;
 25976  26241         }
 25977  26242         sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
 25978  26243         break;
 25979  26244       }
 25980  26245   #endif
        26246  +    case TK_MATCH: {
        26247  +      sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
        26248  +                          pExpr->iTable, pExpr->iColumn, zFlgs);
        26249  +      sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
        26250  +      break;
        26251  +    }
        26252  +    case TK_VECTOR: {
        26253  +      sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
        26254  +      break;
        26255  +    }
        26256  +    case TK_SELECT_COLUMN: {
        26257  +      sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
        26258  +      sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
        26259  +      break;
        26260  +    }
 25981  26261       default: {
 25982  26262         sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
 25983  26263         break;
 25984  26264       }
 25985  26265     }
 25986  26266     if( zBinOp ){
 25987  26267       sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
................................................................................
 25989  26269       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
 25990  26270     }else if( zUniOp ){
 25991  26271       sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
 25992  26272       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 25993  26273     }
 25994  26274     sqlite3TreeViewPop(pView);
 25995  26275   }
        26276  +
 25996  26277   
 25997  26278   /*
 25998  26279   ** Generate a human-readable explanation of an expression list.
 25999  26280   */
 26000         -SQLITE_PRIVATE void sqlite3TreeViewExprList(
        26281  +SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
 26001  26282     TreeView *pView,
 26002  26283     const ExprList *pList,
 26003         -  u8 moreToFollow,
 26004  26284     const char *zLabel
 26005  26285   ){
 26006         -  int i;
 26007         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
 26008  26286     if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
 26009  26287     if( pList==0 ){
 26010  26288       sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
 26011  26289     }else{
        26290  +    int i;
 26012  26291       sqlite3TreeViewLine(pView, "%s", zLabel);
 26013  26292       for(i=0; i<pList->nExpr; i++){
 26014  26293         int j = pList->a[i].u.x.iOrderByCol;
 26015  26294         if( j ){
 26016  26295           sqlite3TreeViewPush(pView, 0);
 26017  26296           sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
 26018  26297         }
 26019  26298         sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
 26020  26299         if( j ) sqlite3TreeViewPop(pView);
 26021  26300       }
 26022  26301     }
        26302  +}
        26303  +SQLITE_PRIVATE void sqlite3TreeViewExprList(
        26304  +  TreeView *pView,
        26305  +  const ExprList *pList,
        26306  +  u8 moreToFollow,
        26307  +  const char *zLabel
        26308  +){
        26309  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
        26310  +  sqlite3TreeViewBareExprList(pView, pList, zLabel);
 26023  26311     sqlite3TreeViewPop(pView);
 26024  26312   }
 26025  26313   
 26026  26314   #endif /* SQLITE_DEBUG */
 26027  26315   
 26028  26316   /************** End of treeview.c ********************************************/
 26029  26317   /************** Begin file random.c ******************************************/
................................................................................
 28757  29045       /*  25 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
 28758  29046       /*  26 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
 28759  29047       /*  27 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 28760  29048       /*  28 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 28761  29049       /*  29 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
 28762  29050       /*  30 */ "NotFound"         OpHelp("key=r[P3@P4]"),
 28763  29051       /*  31 */ "Found"            OpHelp("key=r[P3@P4]"),
 28764         -    /*  32 */ "NotExists"        OpHelp("intkey=r[P3]"),
 28765         -    /*  33 */ "Last"             OpHelp(""),
        29052  +    /*  32 */ "SeekRowid"        OpHelp("intkey=r[P3]"),
        29053  +    /*  33 */ "NotExists"        OpHelp("intkey=r[P3]"),
 28766  29054       /*  34 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
 28767  29055       /*  35 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 28768         -    /*  36 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
 28769         -    /*  37 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
 28770         -    /*  38 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
 28771         -    /*  39 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
 28772         -    /*  40 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
 28773         -    /*  41 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
 28774         -    /*  42 */ "SorterSort"       OpHelp(""),
        29056  +    /*  36 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
        29057  +    /*  37 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
        29058  +    /*  38 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
        29059  +    /*  39 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
        29060  +    /*  40 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
        29061  +    /*  41 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
        29062  +    /*  42 */ "ElseNotEq"        OpHelp(""),
 28775  29063       /*  43 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 28776  29064       /*  44 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 28777  29065       /*  45 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 28778  29066       /*  46 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 28779  29067       /*  47 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 28780  29068       /*  48 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 28781  29069       /*  49 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 28782  29070       /*  50 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 28783  29071       /*  51 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 28784  29072       /*  52 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 28785         -    /*  53 */ "Sort"             OpHelp(""),
        29073  +    /*  53 */ "Last"             OpHelp(""),
 28786  29074       /*  54 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 28787         -    /*  55 */ "Rewind"           OpHelp(""),
 28788         -    /*  56 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 28789         -    /*  57 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 28790         -    /*  58 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 28791         -    /*  59 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 28792         -    /*  60 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 28793         -    /*  61 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 28794         -    /*  62 */ "Program"          OpHelp(""),
 28795         -    /*  63 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 28796         -    /*  64 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
 28797         -    /*  65 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
 28798         -    /*  66 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
 28799         -    /*  67 */ "IncrVacuum"       OpHelp(""),
 28800         -    /*  68 */ "VNext"            OpHelp(""),
 28801         -    /*  69 */ "Init"             OpHelp("Start at P2"),
 28802         -    /*  70 */ "Return"           OpHelp(""),
 28803         -    /*  71 */ "EndCoroutine"     OpHelp(""),
 28804         -    /*  72 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
 28805         -    /*  73 */ "Halt"             OpHelp(""),
 28806         -    /*  74 */ "Integer"          OpHelp("r[P2]=P1"),
 28807         -    /*  75 */ "Int64"            OpHelp("r[P2]=P4"),
 28808         -    /*  76 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 28809         -    /*  77 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 28810         -    /*  78 */ "SoftNull"         OpHelp("r[P1]=NULL"),
 28811         -    /*  79 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 28812         -    /*  80 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 28813         -    /*  81 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 28814         -    /*  82 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 28815         -    /*  83 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 28816         -    /*  84 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
 28817         -    /*  85 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 28818         -    /*  86 */ "CollSeq"          OpHelp(""),
 28819         -    /*  87 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 28820         -    /*  88 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 28821         -    /*  89 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 28822         -    /*  90 */ "RealAffinity"     OpHelp(""),
 28823         -    /*  91 */ "Cast"             OpHelp("affinity(r[P1])"),
 28824         -    /*  92 */ "Permutation"      OpHelp(""),
 28825         -    /*  93 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 28826         -    /*  94 */ "Column"           OpHelp("r[P3]=PX"),
 28827         -    /*  95 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 28828         -    /*  96 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        29075  +    /*  55 */ "SorterSort"       OpHelp(""),
        29076  +    /*  56 */ "Sort"             OpHelp(""),
        29077  +    /*  57 */ "Rewind"           OpHelp(""),
        29078  +    /*  58 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
        29079  +    /*  59 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
        29080  +    /*  60 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
        29081  +    /*  61 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
        29082  +    /*  62 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
        29083  +    /*  63 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
        29084  +    /*  64 */ "Program"          OpHelp(""),
        29085  +    /*  65 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
        29086  +    /*  66 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
        29087  +    /*  67 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
        29088  +    /*  68 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
        29089  +    /*  69 */ "IncrVacuum"       OpHelp(""),
        29090  +    /*  70 */ "VNext"            OpHelp(""),
        29091  +    /*  71 */ "Init"             OpHelp("Start at P2"),
        29092  +    /*  72 */ "Return"           OpHelp(""),
        29093  +    /*  73 */ "EndCoroutine"     OpHelp(""),
        29094  +    /*  74 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
        29095  +    /*  75 */ "Halt"             OpHelp(""),
        29096  +    /*  76 */ "Integer"          OpHelp("r[P2]=P1"),
        29097  +    /*  77 */ "Int64"            OpHelp("r[P2]=P4"),
        29098  +    /*  78 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
        29099  +    /*  79 */ "Null"             OpHelp("r[P2..P3]=NULL"),
        29100  +    /*  80 */ "SoftNull"         OpHelp("r[P1]=NULL"),
        29101  +    /*  81 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
        29102  +    /*  82 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
        29103  +    /*  83 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
        29104  +    /*  84 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
        29105  +    /*  85 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
        29106  +    /*  86 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
        29107  +    /*  87 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
        29108  +    /*  88 */ "CollSeq"          OpHelp(""),
        29109  +    /*  89 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        29110  +    /*  90 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        29111  +    /*  91 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
        29112  +    /*  92 */ "RealAffinity"     OpHelp(""),
        29113  +    /*  93 */ "Cast"             OpHelp("affinity(r[P1])"),
        29114  +    /*  94 */ "Permutation"      OpHelp(""),
        29115  +    /*  95 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
        29116  +    /*  96 */ "Column"           OpHelp("r[P3]=PX"),
 28829  29117       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 28830         -    /*  98 */ "Count"            OpHelp("r[P2]=count()"),
 28831         -    /*  99 */ "ReadCookie"       OpHelp(""),
 28832         -    /* 100 */ "SetCookie"        OpHelp(""),
 28833         -    /* 101 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 28834         -    /* 102 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 28835         -    /* 103 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 28836         -    /* 104 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 28837         -    /* 105 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 28838         -    /* 106 */ "SorterOpen"       OpHelp(""),
 28839         -    /* 107 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 28840         -    /* 108 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 28841         -    /* 109 */ "Close"            OpHelp(""),
 28842         -    /* 110 */ "ColumnsUsed"      OpHelp(""),
 28843         -    /* 111 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 28844         -    /* 112 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 28845         -    /* 113 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 28846         -    /* 114 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 28847         -    /* 115 */ "Delete"           OpHelp(""),
 28848         -    /* 116 */ "ResetCount"       OpHelp(""),
 28849         -    /* 117 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 28850         -    /* 118 */ "SorterData"       OpHelp("r[P2]=data"),
 28851         -    /* 119 */ "RowKey"           OpHelp("r[P2]=key"),
 28852         -    /* 120 */ "RowData"          OpHelp("r[P2]=data"),
 28853         -    /* 121 */ "Rowid"            OpHelp("r[P2]=rowid"),
 28854         -    /* 122 */ "NullRow"          OpHelp(""),
 28855         -    /* 123 */ "SorterInsert"     OpHelp(""),
 28856         -    /* 124 */ "IdxInsert"        OpHelp("key=r[P2]"),
 28857         -    /* 125 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 28858         -    /* 126 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
 28859         -    /* 127 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 28860         -    /* 128 */ "Destroy"          OpHelp(""),
 28861         -    /* 129 */ "Clear"            OpHelp(""),
 28862         -    /* 130 */ "ResetSorter"      OpHelp(""),
 28863         -    /* 131 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 28864         -    /* 132 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        29118  +    /*  98 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        29119  +    /*  99 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        29120  +    /* 100 */ "Count"            OpHelp("r[P2]=count()"),
        29121  +    /* 101 */ "ReadCookie"       OpHelp(""),
        29122  +    /* 102 */ "SetCookie"        OpHelp(""),
        29123  +    /* 103 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        29124  +    /* 104 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        29125  +    /* 105 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        29126  +    /* 106 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        29127  +    /* 107 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        29128  +    /* 108 */ "SorterOpen"       OpHelp(""),
        29129  +    /* 109 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        29130  +    /* 110 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        29131  +    /* 111 */ "Close"            OpHelp(""),
        29132  +    /* 112 */ "ColumnsUsed"      OpHelp(""),
        29133  +    /* 113 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        29134  +    /* 114 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        29135  +    /* 115 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        29136  +    /* 116 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        29137  +    /* 117 */ "Delete"           OpHelp(""),
        29138  +    /* 118 */ "ResetCount"       OpHelp(""),
        29139  +    /* 119 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        29140  +    /* 120 */ "SorterData"       OpHelp("r[P2]=data"),
        29141  +    /* 121 */ "RowKey"           OpHelp("r[P2]=key"),
        29142  +    /* 122 */ "RowData"          OpHelp("r[P2]=data"),
        29143  +    /* 123 */ "Rowid"            OpHelp("r[P2]=rowid"),
        29144  +    /* 124 */ "NullRow"          OpHelp(""),
        29145  +    /* 125 */ "SorterInsert"     OpHelp(""),
        29146  +    /* 126 */ "IdxInsert"        OpHelp("key=r[P2]"),
        29147  +    /* 127 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        29148  +    /* 128 */ "Seek"             OpHelp("Move P3 to P1.rowid"),
        29149  +    /* 129 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        29150  +    /* 130 */ "Destroy"          OpHelp(""),
        29151  +    /* 131 */ "Clear"            OpHelp(""),
        29152  +    /* 132 */ "ResetSorter"      OpHelp(""),
 28865  29153       /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 28866         -    /* 134 */ "ParseSchema"      OpHelp(""),
 28867         -    /* 135 */ "LoadAnalysis"     OpHelp(""),
 28868         -    /* 136 */ "DropTable"        OpHelp(""),
 28869         -    /* 137 */ "DropIndex"        OpHelp(""),
 28870         -    /* 138 */ "DropTrigger"      OpHelp(""),
 28871         -    /* 139 */ "IntegrityCk"      OpHelp(""),
 28872         -    /* 140 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 28873         -    /* 141 */ "Param"            OpHelp(""),
 28874         -    /* 142 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 28875         -    /* 143 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 28876         -    /* 144 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 28877         -    /* 145 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 28878         -    /* 146 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 28879         -    /* 147 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 28880         -    /* 148 */ "Expire"           OpHelp(""),
 28881         -    /* 149 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 28882         -    /* 150 */ "VBegin"           OpHelp(""),
 28883         -    /* 151 */ "VCreate"          OpHelp(""),
 28884         -    /* 152 */ "VDestroy"         OpHelp(""),
 28885         -    /* 153 */ "VOpen"            OpHelp(""),
 28886         -    /* 154 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 28887         -    /* 155 */ "VRename"          OpHelp(""),
 28888         -    /* 156 */ "Pagecount"        OpHelp(""),
 28889         -    /* 157 */ "MaxPgcnt"         OpHelp(""),
 28890         -    /* 158 */ "CursorHint"       OpHelp(""),
 28891         -    /* 159 */ "Noop"             OpHelp(""),
 28892         -    /* 160 */ "Explain"          OpHelp(""),
        29154  +    /* 134 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
        29155  +    /* 135 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
        29156  +    /* 136 */ "ParseSchema"      OpHelp(""),
        29157  +    /* 137 */ "LoadAnalysis"     OpHelp(""),
        29158  +    /* 138 */ "DropTable"        OpHelp(""),
        29159  +    /* 139 */ "DropIndex"        OpHelp(""),
        29160  +    /* 140 */ "DropTrigger"      OpHelp(""),
        29161  +    /* 141 */ "IntegrityCk"      OpHelp(""),
        29162  +    /* 142 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        29163  +    /* 143 */ "Param"            OpHelp(""),
        29164  +    /* 144 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        29165  +    /* 145 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        29166  +    /* 146 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        29167  +    /* 147 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        29168  +    /* 148 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        29169  +    /* 149 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        29170  +    /* 150 */ "Expire"           OpHelp(""),
        29171  +    /* 151 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        29172  +    /* 152 */ "VBegin"           OpHelp(""),
        29173  +    /* 153 */ "VCreate"          OpHelp(""),
        29174  +    /* 154 */ "VDestroy"         OpHelp(""),
        29175  +    /* 155 */ "VOpen"            OpHelp(""),
        29176  +    /* 156 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        29177  +    /* 157 */ "VRename"          OpHelp(""),
        29178  +    /* 158 */ "Pagecount"        OpHelp(""),
        29179  +    /* 159 */ "MaxPgcnt"         OpHelp(""),
        29180  +    /* 160 */ "CursorHint"       OpHelp(""),
        29181  +    /* 161 */ "Noop"             OpHelp(""),
        29182  +    /* 162 */ "Explain"          OpHelp(""),
 28893  29183     };
 28894  29184     return azName[i];
 28895  29185   }
 28896  29186   #endif
 28897  29187   
 28898  29188   /************** End of opcodes.c *********************************************/
 28899  29189   /************** Begin file os_unix.c *****************************************/
................................................................................
 29235  29525   **    May you share freely, never taking more than you give.
 29236  29526   **
 29237  29527   ******************************************************************************
 29238  29528   **
 29239  29529   ** This file contains inline asm code for retrieving "high-performance"
 29240  29530   ** counters for x86 class CPUs.
 29241  29531   */
 29242         -#ifndef _HWTIME_H_
 29243         -#define _HWTIME_H_
        29532  +#ifndef SQLITE_HWTIME_H
        29533  +#define SQLITE_HWTIME_H
 29244  29534   
 29245  29535   /*
 29246  29536   ** The following routine only works on pentium-class (or newer) processors.
 29247  29537   ** It uses the RDTSC opcode to read the cycle count value out of the
 29248  29538   ** processor and returns that value.  This can be used for high-res
 29249  29539   ** profiling.
 29250  29540   */
................................................................................
 29304  29594     ** of the debugging and testing utilities, but it should at
 29305  29595     ** least compile and run.
 29306  29596     */
 29307  29597   SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 29308  29598   
 29309  29599   #endif
 29310  29600   
 29311         -#endif /* !defined(_HWTIME_H_) */
        29601  +#endif /* !defined(SQLITE_HWTIME_H) */
 29312  29602   
 29313  29603   /************** End of hwtime.h **********************************************/
 29314  29604   /************** Continuing where we left off in os_common.h ******************/
 29315  29605   
 29316  29606   static sqlite_uint64 g_start;
 29317  29607   static sqlite_uint64 g_elapsed;
 29318  29608   #define TIMER_START       g_start=sqlite3Hwtime()
................................................................................
 34621  34911         }
 34622  34912       }
 34623  34913       unixLeaveMutex();
 34624  34914     }
 34625  34915   #endif    /* if !OS_VXWORKS */
 34626  34916     return pUnused;
 34627  34917   }
        34918  +
        34919  +/*
        34920  +** Find the mode, uid and gid of file zFile. 
        34921  +*/
        34922  +static int getFileMode(
        34923  +  const char *zFile,              /* File name */
        34924  +  mode_t *pMode,                  /* OUT: Permissions of zFile */
        34925  +  uid_t *pUid,                    /* OUT: uid of zFile. */
        34926  +  gid_t *pGid                     /* OUT: gid of zFile. */
        34927  +){
        34928  +  struct stat sStat;              /* Output of stat() on database file */
        34929  +  int rc = SQLITE_OK;
        34930  +  if( 0==osStat(zFile, &sStat) ){
        34931  +    *pMode = sStat.st_mode & 0777;
        34932  +    *pUid = sStat.st_uid;
        34933  +    *pGid = sStat.st_gid;
        34934  +  }else{
        34935  +    rc = SQLITE_IOERR_FSTAT;
        34936  +  }
        34937  +  return rc;
        34938  +}
 34628  34939   
 34629  34940   /*
 34630  34941   ** This function is called by unixOpen() to determine the unix permissions
 34631  34942   ** to create new files with. If no error occurs, then SQLITE_OK is returned
 34632  34943   ** and a value suitable for passing as the third argument to open(2) is
 34633  34944   ** written to *pMode. If an IO error occurs, an SQLite error code is 
 34634  34945   ** returned and the value of *pMode is not modified.
................................................................................
 34657  34968     int rc = SQLITE_OK;             /* Return Code */
 34658  34969     *pMode = 0;
 34659  34970     *pUid = 0;
 34660  34971     *pGid = 0;
 34661  34972     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 34662  34973       char zDb[MAX_PATHNAME+1];     /* Database file path */
 34663  34974       int nDb;                      /* Number of valid bytes in zDb */
 34664         -    struct stat sStat;            /* Output of stat() on database file */
 34665  34975   
 34666  34976       /* zPath is a path to a WAL or journal file. The following block derives
 34667  34977       ** the path to the associated database file from zPath. This block handles
 34668  34978       ** the following naming conventions:
 34669  34979       **
 34670  34980       **   "<path to db>-journal"
 34671  34981       **   "<path to db>-wal"
................................................................................
 34688  34998         if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
 34689  34999   #endif
 34690  35000         nDb--;
 34691  35001       }
 34692  35002       memcpy(zDb, zPath, nDb);
 34693  35003       zDb[nDb] = '\0';
 34694  35004   
 34695         -    if( 0==osStat(zDb, &sStat) ){
 34696         -      *pMode = sStat.st_mode & 0777;
 34697         -      *pUid = sStat.st_uid;
 34698         -      *pGid = sStat.st_gid;
 34699         -    }else{
 34700         -      rc = SQLITE_IOERR_FSTAT;
 34701         -    }
        35005  +    rc = getFileMode(zDb, pMode, pUid, pGid);
 34702  35006     }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 34703  35007       *pMode = 0600;
        35008  +  }else if( flags & SQLITE_OPEN_URI ){
        35009  +    /* If this is a main database file and the file was opened using a URI
        35010  +    ** filename, check for the "modeof" parameter. If present, interpret
        35011  +    ** its value as a filename and try to copy the mode, uid and gid from
        35012  +    ** that file.  */
        35013  +    const char *z = sqlite3_uri_parameter(zPath, "modeof");
        35014  +    if( z ){
        35015  +      rc = getFileMode(z, pMode, pUid, pGid);
        35016  +    }
 34704  35017     }
 34705  35018     return rc;
 34706  35019   }
 34707  35020   
 34708  35021   /*
 34709  35022   ** Open the file zPath.
 34710  35023   ** 
................................................................................
 36764  37077   **    May you share freely, never taking more than you give.
 36765  37078   **
 36766  37079   ******************************************************************************
 36767  37080   **
 36768  37081   ** This file contains inline asm code for retrieving "high-performance"
 36769  37082   ** counters for x86 class CPUs.
 36770  37083   */
 36771         -#ifndef _HWTIME_H_
 36772         -#define _HWTIME_H_
        37084  +#ifndef SQLITE_HWTIME_H
        37085  +#define SQLITE_HWTIME_H
 36773  37086   
 36774  37087   /*
 36775  37088   ** The following routine only works on pentium-class (or newer) processors.
 36776  37089   ** It uses the RDTSC opcode to read the cycle count value out of the
 36777  37090   ** processor and returns that value.  This can be used for high-res
 36778  37091   ** profiling.
 36779  37092   */
................................................................................
 36833  37146     ** of the debugging and testing utilities, but it should at
 36834  37147     ** least compile and run.
 36835  37148     */
 36836  37149   SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 36837  37150   
 36838  37151   #endif
 36839  37152   
 36840         -#endif /* !defined(_HWTIME_H_) */
        37153  +#endif /* !defined(SQLITE_HWTIME_H) */
 36841  37154   
 36842  37155   /************** End of hwtime.h **********************************************/
 36843  37156   /************** Continuing where we left off in os_common.h ******************/
 36844  37157   
 36845  37158   static sqlite_uint64 g_start;
 36846  37159   static sqlite_uint64 g_elapsed;
 36847  37160   #define TIMER_START       g_start=sqlite3Hwtime()
................................................................................
 37173  37486     HANDLE hMap;                  /* Handle for accessing memory mapping */
 37174  37487     void *pMapRegion;             /* Area memory mapped */
 37175  37488     sqlite3_int64 mmapSize;       /* Usable size of mapped region */
 37176  37489     sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
 37177  37490     sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
 37178  37491   #endif
 37179  37492   };
        37493  +
        37494  +/*
        37495  +** The winVfsAppData structure is used for the pAppData member for all of the
        37496  +** Win32 VFS variants.
        37497  +*/
        37498  +typedef struct winVfsAppData winVfsAppData;
        37499  +struct winVfsAppData {
        37500  +  const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
        37501  +  void *pAppData;                    /* The extra pAppData, if any. */
        37502  +  BOOL bNoLock;                      /* Non-zero if locking is disabled. */
        37503  +};
 37180  37504   
 37181  37505   /*
 37182  37506   ** Allowed values for winFile.ctrlFlags
 37183  37507   */
 37184  37508   #define WINFILE_RDONLY          0x02   /* Connection is read only */
 37185  37509   #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
 37186  37510   #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
................................................................................
 39495  39819   
 39496  39820     do{
 39497  39821       rc = osCloseHandle(pFile->h);
 39498  39822       /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
 39499  39823     }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
 39500  39824   #if SQLITE_OS_WINCE
 39501  39825   #define WINCE_DELETION_ATTEMPTS 3
        39826  +  {
        39827  +    winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
        39828  +    if( pAppData==NULL || !pAppData->bNoLock ){
 39502  39829     winceDestroyLock(pFile);
        39830  +    }
        39831  +  }
 39503  39832     if( pFile->zDeleteOnClose ){
 39504  39833       int cnt = 0;
 39505  39834       while(
 39506  39835              osDeleteFileW(pFile->zDeleteOnClose)==0
 39507  39836           && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
 39508  39837           && cnt++ < WINCE_DELETION_ATTEMPTS
 39509  39838       ){
................................................................................
 40227  40556     }
 40228  40557     pFile->locktype = (u8)locktype;
 40229  40558     OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
 40230  40559              pFile->h, pFile->locktype, sqlite3ErrName(rc)));
 40231  40560     return rc;
 40232  40561   }
 40233  40562   
        40563  +/******************************************************************************
        40564  +****************************** No-op Locking **********************************
        40565  +**
        40566  +** Of the various locking implementations available, this is by far the
        40567  +** simplest:  locking is ignored.  No attempt is made to lock the database
        40568  +** file for reading or writing.
        40569  +**
        40570  +** This locking mode is appropriate for use on read-only databases
        40571  +** (ex: databases that are burned into CD-ROM, for example.)  It can
        40572  +** also be used if the application employs some external mechanism to
        40573  +** prevent simultaneous access of the same database by two or more
        40574  +** database connections.  But there is a serious risk of database
        40575  +** corruption if this locking mode is used in situations where multiple
        40576  +** database connections are accessing the same database file at the same
        40577  +** time and one or more of those connections are writing.
        40578  +*/
        40579  +
        40580  +static int winNolockLock(sqlite3_file *id, int locktype){
        40581  +  UNUSED_PARAMETER(id);
        40582  +  UNUSED_PARAMETER(locktype);
        40583  +  return SQLITE_OK;
        40584  +}
        40585  +
        40586  +static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
        40587  +  UNUSED_PARAMETER(id);
        40588  +  UNUSED_PARAMETER(pResOut);
        40589  +  return SQLITE_OK;
        40590  +}
        40591  +
        40592  +static int winNolockUnlock(sqlite3_file *id, int locktype){
        40593  +  UNUSED_PARAMETER(id);
        40594  +  UNUSED_PARAMETER(locktype);
        40595  +  return SQLITE_OK;
        40596  +}
        40597  +
        40598  +/******************* End of the no-op lock implementation *********************
        40599  +******************************************************************************/
        40600  +
 40234  40601   /*
 40235  40602   ** If *pArg is initially negative then this is a query.  Set *pArg to
 40236  40603   ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 40237  40604   **
 40238  40605   ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 40239  40606   */
 40240  40607   static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
................................................................................
 40505  40872   */
 40506  40873   #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
 40507  40874   #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 40508  40875   
 40509  40876   /*
 40510  40877   ** Apply advisory locks for all n bytes beginning at ofst.
 40511  40878   */
 40512         -#define _SHM_UNLCK  1
 40513         -#define _SHM_RDLCK  2
 40514         -#define _SHM_WRLCK  3
        40879  +#define WINSHM_UNLCK  1
        40880  +#define WINSHM_RDLCK  2
        40881  +#define WINSHM_WRLCK  3
 40515  40882   static int winShmSystemLock(
 40516  40883     winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
 40517         -  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
        40884  +  int lockType,         /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
 40518  40885     int ofst,             /* Offset to first byte to be locked/unlocked */
 40519  40886     int nByte             /* Number of bytes to lock or unlock */
 40520  40887   ){
 40521  40888     int rc = 0;           /* Result code form Lock/UnlockFileEx() */
 40522  40889   
 40523  40890     /* Access to the winShmNode object is serialized by the caller */
 40524  40891     assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
 40525  40892   
 40526  40893     OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
 40527  40894              pFile->hFile.h, lockType, ofst, nByte));
 40528  40895   
 40529  40896     /* Release/Acquire the system-level lock */
 40530         -  if( lockType==_SHM_UNLCK ){
        40897  +  if( lockType==WINSHM_UNLCK ){
 40531  40898       rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
 40532  40899     }else{
 40533  40900       /* Initialize the locking parameters */
 40534  40901       DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
 40535         -    if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
        40902  +    if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
 40536  40903       rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
 40537  40904     }
 40538  40905   
 40539  40906     if( rc!= 0 ){
 40540  40907       rc = SQLITE_OK;
 40541  40908     }else{
 40542  40909       pFile->lastErrno =  osGetLastError();
 40543  40910       rc = SQLITE_BUSY;
 40544  40911     }
 40545  40912   
 40546  40913     OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
 40547         -           pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
        40914  +           pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
 40548  40915              "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
 40549  40916   
 40550  40917     return rc;
 40551  40918   }
 40552  40919   
 40553  40920   /* Forward references to VFS methods */
 40554  40921   static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
................................................................................
 40668  41035       if( SQLITE_OK!=rc ){
 40669  41036         goto shm_open_err;
 40670  41037       }
 40671  41038   
 40672  41039       /* Check to see if another process is holding the dead-man switch.
 40673  41040       ** If not, truncate the file to zero length.
 40674  41041       */
 40675         -    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
        41042  +    if( winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
 40676  41043         rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
 40677  41044         if( rc!=SQLITE_OK ){
 40678  41045           rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
 40679  41046                            "winOpenShm", pDbFd->zPath);
 40680  41047         }
 40681  41048       }
 40682  41049       if( rc==SQLITE_OK ){
 40683         -      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 40684         -      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
        41050  +      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
        41051  +      rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
 40685  41052       }
 40686  41053       if( rc ) goto shm_open_err;
 40687  41054     }
 40688  41055   
 40689  41056     /* Make the new connection a child of the winShmNode */
 40690  41057     p->pShmNode = pShmNode;
 40691  41058   #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
................................................................................
 40706  41073     p->pNext = pShmNode->pFirst;
 40707  41074     pShmNode->pFirst = p;
 40708  41075     sqlite3_mutex_leave(pShmNode->mutex);
 40709  41076     return SQLITE_OK;
 40710  41077   
 40711  41078     /* Jump here on any error */
 40712  41079   shm_open_err:
 40713         -  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
        41080  +  winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
 40714  41081     winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
 40715  41082     sqlite3_free(p);
 40716  41083     sqlite3_free(pNew);
 40717  41084     winShmLeaveMutex();
 40718  41085     return rc;
 40719  41086   }
 40720  41087   
................................................................................
 40795  41162         if( pX==p ) continue;
 40796  41163         assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 40797  41164         allMask |= pX->sharedMask;
 40798  41165       }
 40799  41166   
 40800  41167       /* Unlock the system-level locks */
 40801  41168       if( (mask & allMask)==0 ){
 40802         -      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
        41169  +      rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
 40803  41170       }else{
 40804  41171         rc = SQLITE_OK;
 40805  41172       }
 40806  41173   
 40807  41174       /* Undo the local locks */
 40808  41175       if( rc==SQLITE_OK ){
 40809  41176         p->exclMask &= ~mask;
................................................................................
 40823  41190         }
 40824  41191         allShared |= pX->sharedMask;
 40825  41192       }
 40826  41193   
 40827  41194       /* Get shared locks at the system level, if necessary */
 40828  41195       if( rc==SQLITE_OK ){
 40829  41196         if( (allShared & mask)==0 ){
 40830         -        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
        41197  +        rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
 40831  41198         }else{
 40832  41199           rc = SQLITE_OK;
 40833  41200         }
 40834  41201       }
 40835  41202   
 40836  41203       /* Get the local shared locks */
 40837  41204       if( rc==SQLITE_OK ){
................................................................................
 40848  41215         }
 40849  41216       }
 40850  41217   
 40851  41218       /* Get the exclusive locks at the system level.  Then if successful
 40852  41219       ** also mark the local connection as being locked.
 40853  41220       */
 40854  41221       if( rc==SQLITE_OK ){
 40855         -      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
        41222  +      rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
 40856  41223         if( rc==SQLITE_OK ){
 40857  41224           assert( (p->sharedMask & mask)==0 );
 40858  41225           p->exclMask |= mask;
 40859  41226         }
 40860  41227       }
 40861  41228     }
 40862  41229     sqlite3_mutex_leave(pShmNode->mutex);
................................................................................
 41290  41657     winShmMap,                      /* xShmMap */
 41291  41658     winShmLock,                     /* xShmLock */
 41292  41659     winShmBarrier,                  /* xShmBarrier */
 41293  41660     winShmUnmap,                    /* xShmUnmap */
 41294  41661     winFetch,                       /* xFetch */
 41295  41662     winUnfetch                      /* xUnfetch */
 41296  41663   };
        41664  +
        41665  +/*
        41666  +** This vector defines all the methods that can operate on an
        41667  +** sqlite3_file for win32 without performing any locking.
        41668  +*/
        41669  +static const sqlite3_io_methods winIoNolockMethod = {
        41670  +  3,                              /* iVersion */
        41671  +  winClose,                       /* xClose */
        41672  +  winRead,                        /* xRead */
        41673  +  winWrite,                       /* xWrite */
        41674  +  winTruncate,                    /* xTruncate */
        41675  +  winSync,                        /* xSync */
        41676  +  winFileSize,                    /* xFileSize */
        41677  +  winNolockLock,                  /* xLock */
        41678  +  winNolockUnlock,                /* xUnlock */
        41679  +  winNolockCheckReservedLock,     /* xCheckReservedLock */
        41680  +  winFileControl,                 /* xFileControl */
        41681  +  winSectorSize,                  /* xSectorSize */
        41682  +  winDeviceCharacteristics,       /* xDeviceCharacteristics */
        41683  +  winShmMap,                      /* xShmMap */
        41684  +  winShmLock,                     /* xShmLock */
        41685  +  winShmBarrier,                  /* xShmBarrier */
        41686  +  winShmUnmap,                    /* xShmUnmap */
        41687  +  winFetch,                       /* xFetch */
        41688  +  winUnfetch                      /* xUnfetch */
        41689  +};
        41690  +
        41691  +static winVfsAppData winAppData = {
        41692  +  &winIoMethod,       /* pMethod */
        41693  +  0,                  /* pAppData */
        41694  +  0                   /* bNoLock */
        41695  +};
        41696  +
        41697  +static winVfsAppData winNolockAppData = {
        41698  +  &winIoNolockMethod, /* pMethod */
        41699  +  0,                  /* pAppData */
        41700  +  1                   /* bNoLock */
        41701  +};
 41297  41702   
 41298  41703   /****************************************************************************
 41299  41704   **************************** sqlite3_vfs methods ****************************
 41300  41705   **
 41301  41706   ** This division contains the implementation of methods on the
 41302  41707   ** sqlite3_vfs object.
 41303  41708   */
................................................................................
 41623  42028     return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
 41624  42029   }
 41625  42030   
 41626  42031   /*
 41627  42032   ** Open a file.
 41628  42033   */
 41629  42034   static int winOpen(
 41630         -  sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
        42035  +  sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
 41631  42036     const char *zName,        /* Name of the file (UTF-8) */
 41632  42037     sqlite3_file *id,         /* Write the SQLite file handle here */
 41633  42038     int flags,                /* Open mode flags */
 41634  42039     int *pOutFlags            /* Status return flags */
 41635  42040   ){
 41636  42041     HANDLE h;
 41637  42042     DWORD lastErrno = 0;
................................................................................
 41638  42043     DWORD dwDesiredAccess;
 41639  42044     DWORD dwShareMode;
 41640  42045     DWORD dwCreationDisposition;
 41641  42046     DWORD dwFlagsAndAttributes = 0;
 41642  42047   #if SQLITE_OS_WINCE
 41643  42048     int isTemp = 0;
 41644  42049   #endif
        42050  +  winVfsAppData *pAppData;
 41645  42051     winFile *pFile = (winFile*)id;
 41646  42052     void *zConverted;              /* Filename in OS encoding */
 41647  42053     const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
 41648  42054     int cnt = 0;
 41649  42055   
 41650  42056     /* If argument zPath is a NULL pointer, this function is required to open
 41651  42057     ** a temporary file. Use this buffer to store the file name in.
................................................................................
 41859  42265       }
 41860  42266     }
 41861  42267   
 41862  42268     OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
 41863  42269              "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
 41864  42270              *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
 41865  42271   
        42272  +  pAppData = (winVfsAppData*)pVfs->pAppData;
        42273  +
 41866  42274   #if SQLITE_OS_WINCE
        42275  +  {
 41867  42276     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
        42277  +         && ((pAppData==NULL) || !pAppData->bNoLock)
 41868  42278          && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
 41869  42279     ){
 41870  42280       osCloseHandle(h);
 41871  42281       sqlite3_free(zConverted);
 41872  42282       sqlite3_free(zTmpname);
 41873  42283       OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
 41874  42284       return rc;
 41875  42285     }
        42286  +  }
 41876  42287     if( isTemp ){
 41877  42288       pFile->zDeleteOnClose = zConverted;
 41878  42289     }else
 41879  42290   #endif
 41880  42291     {
 41881  42292       sqlite3_free(zConverted);
 41882  42293     }
 41883  42294   
 41884  42295     sqlite3_free(zTmpname);
 41885         -  pFile->pMethod = &winIoMethod;
        42296  +  pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
 41886  42297     pFile->pVfs = pVfs;
 41887  42298     pFile->h = h;
 41888  42299     if( isReadonly ){
 41889  42300       pFile->ctrlFlags |= WINFILE_RDONLY;
 41890  42301     }
 41891  42302     if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 41892  42303       pFile->ctrlFlags |= WINFILE_PSOW;
................................................................................
 42153  42564   */
 42154  42565   static int winFullPathname(
 42155  42566     sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 42156  42567     const char *zRelative,        /* Possibly relative input path */
 42157  42568     int nFull,                    /* Size of output buffer in bytes */
 42158  42569     char *zFull                   /* Output buffer */
 42159  42570   ){
        42571  +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
        42572  +  DWORD nByte;
        42573  +  void *zConverted;
        42574  +  char *zOut;
        42575  +#endif
        42576  +
        42577  +  /* If this path name begins with "/X:", where "X" is any alphabetic
        42578  +  ** character, discard the initial "/" from the pathname.
        42579  +  */
        42580  +  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
        42581  +    zRelative++;
        42582  +  }
 42160  42583   
 42161  42584   #if defined(__CYGWIN__)
 42162  42585     SimulateIOError( return SQLITE_ERROR );
 42163  42586     UNUSED_PARAMETER(nFull);
 42164  42587     assert( nFull>=pVfs->mxPathname );
 42165  42588     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 42166  42589       /*
................................................................................
 42231  42654     }else{
 42232  42655       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
 42233  42656     }
 42234  42657     return SQLITE_OK;
 42235  42658   #endif
 42236  42659   
 42237  42660   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
 42238         -  DWORD nByte;
 42239         -  void *zConverted;
 42240         -  char *zOut;
 42241         -
 42242         -  /* If this path name begins with "/X:", where "X" is any alphabetic
 42243         -  ** character, discard the initial "/" from the pathname.
 42244         -  */
 42245         -  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
 42246         -    zRelative++;
 42247         -  }
 42248         -
 42249  42661     /* It's odd to simulate an io-error here, but really this is just
 42250  42662     ** using the io-error infrastructure to test that SQLite handles this
 42251  42663     ** function failing. This function could fail if, for example, the
 42252  42664     ** current working directory has been unlinked.
 42253  42665     */
 42254  42666     SimulateIOError( return SQLITE_ERROR );
 42255  42667     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
................................................................................
 42605  43017   SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
 42606  43018     static sqlite3_vfs winVfs = {
 42607  43019       3,                   /* iVersion */
 42608  43020       sizeof(winFile),     /* szOsFile */
 42609  43021       SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
 42610  43022       0,                   /* pNext */
 42611  43023       "win32",             /* zName */
 42612         -    0,                   /* pAppData */
        43024  +    &winAppData,           /* pAppData */
 42613  43025       winOpen,             /* xOpen */
 42614  43026       winDelete,           /* xDelete */
 42615  43027       winAccess,           /* xAccess */
 42616  43028       winFullPathname,     /* xFullPathname */
 42617  43029       winDlOpen,           /* xDlOpen */
 42618  43030       winDlError,          /* xDlError */
 42619  43031       winDlSym,            /* xDlSym */
................................................................................
 42630  43042   #if defined(SQLITE_WIN32_HAS_WIDE)
 42631  43043     static sqlite3_vfs winLongPathVfs = {
 42632  43044       3,                   /* iVersion */
 42633  43045       sizeof(winFile),     /* szOsFile */
 42634  43046       SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
 42635  43047       0,                   /* pNext */
 42636  43048       "win32-longpath",    /* zName */
 42637         -    0,                   /* pAppData */
        43049  +    &winAppData,           /* pAppData */
        43050  +    winOpen,               /* xOpen */
        43051  +    winDelete,             /* xDelete */
        43052  +    winAccess,             /* xAccess */
        43053  +    winFullPathname,       /* xFullPathname */
        43054  +    winDlOpen,             /* xDlOpen */
        43055  +    winDlError,            /* xDlError */
        43056  +    winDlSym,              /* xDlSym */
        43057  +    winDlClose,            /* xDlClose */
        43058  +    winRandomness,         /* xRandomness */
        43059  +    winSleep,              /* xSleep */
        43060  +    winCurrentTime,        /* xCurrentTime */
        43061  +    winGetLastError,       /* xGetLastError */
        43062  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
        43063  +    winSetSystemCall,      /* xSetSystemCall */
        43064  +    winGetSystemCall,      /* xGetSystemCall */
        43065  +    winNextSystemCall,     /* xNextSystemCall */
        43066  +  };
        43067  +#endif
        43068  +  static sqlite3_vfs winNolockVfs = {
        43069  +    3,                     /* iVersion */
        43070  +    sizeof(winFile),       /* szOsFile */
        43071  +    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
        43072  +    0,                     /* pNext */
        43073  +    "win32-none",          /* zName */
        43074  +    &winNolockAppData,     /* pAppData */
        43075  +    winOpen,               /* xOpen */
        43076  +    winDelete,             /* xDelete */
        43077  +    winAccess,             /* xAccess */
        43078  +    winFullPathname,       /* xFullPathname */
        43079  +    winDlOpen,             /* xDlOpen */
        43080  +    winDlError,            /* xDlError */
        43081  +    winDlSym,              /* xDlSym */
        43082  +    winDlClose,            /* xDlClose */
        43083  +    winRandomness,         /* xRandomness */
        43084  +    winSleep,              /* xSleep */
        43085  +    winCurrentTime,        /* xCurrentTime */
        43086  +    winGetLastError,       /* xGetLastError */
        43087  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
        43088  +    winSetSystemCall,      /* xSetSystemCall */
        43089  +    winGetSystemCall,      /* xGetSystemCall */
        43090  +    winNextSystemCall,     /* xNextSystemCall */
        43091  +  };
        43092  +#if defined(SQLITE_WIN32_HAS_WIDE)
        43093  +  static sqlite3_vfs winLongPathNolockVfs = {
        43094  +    3,                     /* iVersion */
        43095  +    sizeof(winFile),       /* szOsFile */
        43096  +    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
        43097  +    0,                     /* pNext */
        43098  +    "win32-longpath-none", /* zName */
        43099  +    &winNolockAppData,     /* pAppData */
 42638  43100       winOpen,             /* xOpen */
 42639  43101       winDelete,           /* xDelete */
 42640  43102       winAccess,           /* xAccess */
 42641  43103       winFullPathname,     /* xFullPathname */
 42642  43104       winDlOpen,           /* xDlOpen */
 42643  43105       winDlError,          /* xDlError */
 42644  43106       winDlSym,            /* xDlSym */
................................................................................
 42669  43131     assert( winSysInfo.dwPageSize>0 );
 42670  43132   
 42671  43133     sqlite3_vfs_register(&winVfs, 1);
 42672  43134   
 42673  43135   #if defined(SQLITE_WIN32_HAS_WIDE)
 42674  43136     sqlite3_vfs_register(&winLongPathVfs, 0);
 42675  43137   #endif
        43138  +
        43139  +  sqlite3_vfs_register(&winNolockVfs, 0);
        43140  +
        43141  +#if defined(SQLITE_WIN32_HAS_WIDE)
        43142  +  sqlite3_vfs_register(&winLongPathNolockVfs, 0);
        43143  +#endif
 42676  43144   
 42677  43145     return SQLITE_OK;
 42678  43146   }
 42679  43147   
 42680  43148   SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
 42681  43149   #if SQLITE_OS_WINRT
 42682  43150     if( sleepObj!=NULL ){
................................................................................
 43790  44258   */
 43791  44259   SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
 43792  44260     sqlite3PcacheTruncate(pCache, 0);
 43793  44261   }
 43794  44262   
 43795  44263   /*
 43796  44264   ** Merge two lists of pages connected by pDirty and in pgno order.
 43797         -** Do not both fixing the pDirtyPrev pointers.
        44265  +** Do not bother fixing the pDirtyPrev pointers.
 43798  44266   */
 43799  44267   static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
 43800  44268     PgHdr result, *pTail;
 43801  44269     pTail = &result;
 43802         -  while( pA && pB ){
        44270  +  assert( pA!=0 && pB!=0 );
        44271  +  for(;;){
 43803  44272       if( pA->pgno<pB->pgno ){
 43804  44273         pTail->pDirty = pA;
 43805  44274         pTail = pA;
 43806  44275         pA = pA->pDirty;
        44276  +      if( pA==0 ){
        44277  +        pTail->pDirty = pB;
        44278  +        break;
        44279  +      }
 43807  44280       }else{
 43808  44281         pTail->pDirty = pB;
 43809  44282         pTail = pB;
 43810  44283         pB = pB->pDirty;
 43811         -    }
 43812         -  }
 43813         -  if( pA ){
        44284  +      if( pB==0 ){
 43814  44285       pTail->pDirty = pA;
 43815         -  }else if( pB ){
 43816         -    pTail->pDirty = pB;
 43817         -  }else{
 43818         -    pTail->pDirty = 0;
        44286  +        break;
        44287  +      }
        44288  +    }
 43819  44289     }
 43820  44290     return result.pDirty;
 43821  44291   }
 43822  44292   
 43823  44293   /*
 43824  44294   ** Sort the list of pages in accending order by pgno.  Pages are
 43825  44295   ** connected by pDirty pointers.  The pDirtyPrev pointers are
................................................................................
 43853  44323         ** the input list.  But that is impossible.
 43854  44324         */
 43855  44325         a[i] = pcacheMergeDirtyList(a[i], p);
 43856  44326       }
 43857  44327     }
 43858  44328     p = a[0];
 43859  44329     for(i=1; i<N_SORT_BUCKET; i++){
 43860         -    p = pcacheMergeDirtyList(p, a[i]);
        44330  +    if( a[i]==0 ) continue;
        44331  +    p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
 43861  44332     }
 43862  44333     return p;
 43863  44334   }
 43864  44335   
 43865  44336   /*
 43866  44337   ** Return a list of all dirty pages in the cache, sorted by page number.
 43867  44338   */
................................................................................
 44608  45079   **
 44609  45080   ** The PCache mutex must be held when this function is called.
 44610  45081   */
 44611  45082   static void pcache1TruncateUnsafe(
 44612  45083     PCache1 *pCache,             /* The cache to truncate */
 44613  45084     unsigned int iLimit          /* Drop pages with this pgno or larger */
 44614  45085   ){
 44615         -  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
 44616         -  unsigned int h;
        45086  +  TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
        45087  +  unsigned int h, iStop;
 44617  45088     assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 44618         -  for(h=0; h<pCache->nHash; h++){
 44619         -    PgHdr1 **pp = &pCache->apHash[h]; 
        45089  +  assert( pCache->iMaxKey >= iLimit );
        45090  +  assert( pCache->nHash > 0 );
        45091  +  if( pCache->iMaxKey - iLimit < pCache->nHash ){
        45092  +    /* If we are just shaving the last few pages off the end of the
        45093  +    ** cache, then there is no point in scanning the entire hash table.
        45094  +    ** Only scan those hash slots that might contain pages that need to
        45095  +    ** be removed. */
        45096  +    h = iLimit % pCache->nHash;
        45097  +    iStop = pCache->iMaxKey % pCache->nHash;
        45098  +    TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
        45099  +  }else{
        45100  +    /* This is the general case where many pages are being removed.
        45101  +    ** It is necessary to scan the entire hash table */
        45102  +    h = pCache->nHash/2;
        45103  +    iStop = h - 1;
        45104  +  }
        45105  +  for(;;){
        45106  +    PgHdr1 **pp;
 44620  45107       PgHdr1 *pPage;
        45108  +    assert( h<pCache->nHash );
        45109  +    pp = &pCache->apHash[h]; 
 44621  45110       while( (pPage = *pp)!=0 ){
 44622  45111         if( pPage->iKey>=iLimit ){
 44623  45112           pCache->nPage--;
 44624  45113           *pp = pPage->pNext;
 44625  45114           if( !pPage->isPinned ) pcache1PinPage(pPage);
 44626  45115           pcache1FreePage(pPage);
 44627  45116         }else{
 44628  45117           pp = &pPage->pNext;
 44629         -        TESTONLY( nPage++; )
        45118  +        TESTONLY( if( nPage>=0 ) nPage++; )
 44630  45119         }
 44631  45120       }
        45121  +    if( h==iStop ) break;
        45122  +    h = (h+1) % pCache->nHash;
 44632  45123     }
 44633         -  assert( pCache->nPage==nPage );
        45124  +  assert( nPage<0 || pCache->nPage==(unsigned)nPage );
 44634  45125   }
 44635  45126   
 44636  45127   /******************************************************************************/
 44637  45128   /******** sqlite3_pcache Methods **********************************************/
 44638  45129   
 44639  45130   /*
 44640  45131   ** Implementation of the sqlite3_pcache.xInit method.
................................................................................
 45103  45594   ** Destroy a cache allocated using pcache1Create().
 45104  45595   */
 45105  45596   static void pcache1Destroy(sqlite3_pcache *p){
 45106  45597     PCache1 *pCache = (PCache1 *)p;
 45107  45598     PGroup *pGroup = pCache->pGroup;
 45108  45599     assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
 45109  45600     pcache1EnterMutex(pGroup);
 45110         -  pcache1TruncateUnsafe(pCache, 0);
        45601  +  if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
 45111  45602     assert( pGroup->nMaxPage >= pCache->nMax );
 45112  45603     pGroup->nMaxPage -= pCache->nMax;
 45113  45604     assert( pGroup->nMinPage >= pCache->nMin );
 45114  45605     pGroup->nMinPage -= pCache->nMin;
 45115  45606     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 45116  45607     pcache1EnforceMaxPage(pCache);
 45117  45608     pcache1LeaveMutex(pGroup);
................................................................................
 45458  45949     struct RowSetEntry *pA,    /* First sorted list to be merged */
 45459  45950     struct RowSetEntry *pB     /* Second sorted list to be merged */
 45460  45951   ){
 45461  45952     struct RowSetEntry head;
 45462  45953     struct RowSetEntry *pTail;
 45463  45954   
 45464  45955     pTail = &head;
 45465         -  while( pA && pB ){
        45956  +  assert( pA!=0 && pB!=0 );
        45957  +  for(;;){
 45466  45958       assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 45467  45959       assert( pB->pRight==0 || pB->v<=pB->pRight->v );
 45468         -    if( pA->v<pB->v ){
 45469         -      pTail->pRight = pA;
        45960  +    if( pA->v<=pB->v ){
        45961  +      if( pA->v<pB->v ) pTail = pTail->pRight = pA;
 45470  45962         pA = pA->pRight;
 45471         -      pTail = pTail->pRight;
 45472         -    }else if( pB->v<pA->v ){
        45963  +      if( pA==0 ){
 45473  45964         pTail->pRight = pB;
        45965  +        break;
        45966  +      }
        45967  +    }else{
        45968  +      pTail = pTail->pRight = pB;
 45474  45969         pB = pB->pRight;
 45475         -      pTail = pTail->pRight;
 45476         -    }else{
 45477         -      pA = pA->pRight;
 45478         -    }
 45479         -  }
 45480         -  if( pA ){
 45481         -    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
        45970  +      if( pB==0 ){
 45482  45971       pTail->pRight = pA;
 45483         -  }else{
 45484         -    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
 45485         -    pTail->pRight = pB;
        45972  +        break;
        45973  +      }
        45974  +    }
 45486  45975     }
 45487  45976     return head.pRight;
 45488  45977   }
 45489  45978   
 45490  45979   /*
 45491  45980   ** Sort all elements on the list of RowSetEntry objects into order of
 45492  45981   ** increasing v.
................................................................................
 45502  45991       for(i=0; aBucket[i]; i++){
 45503  45992         pIn = rowSetEntryMerge(aBucket[i], pIn);
 45504  45993         aBucket[i] = 0;
 45505  45994       }
 45506  45995       aBucket[i] = pIn;
 45507  45996       pIn = pNext;
 45508  45997     }
 45509         -  pIn = 0;
 45510         -  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
 45511         -    pIn = rowSetEntryMerge(pIn, aBucket[i]);
        45998  +  pIn = aBucket[0];
        45999  +  for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
        46000  +    if( aBucket[i]==0 ) continue;
        46001  +    pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
 45512  46002     }
 45513  46003     return pIn;
 45514  46004   }
 45515  46005   
 45516  46006   
 45517  46007   /*
 45518  46008   ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
................................................................................
 45756  46246   **
 45757  46247   *************************************************************************
 45758  46248   ** This header file defines the interface to the write-ahead logging 
 45759  46249   ** system. Refer to the comments below and the header comment attached to 
 45760  46250   ** the implementation of each function in log.c for further details.
 45761  46251   */
 45762  46252   
 45763         -#ifndef _WAL_H_
 45764         -#define _WAL_H_
        46253  +#ifndef SQLITE_WAL_H
        46254  +#define SQLITE_WAL_H
 45765  46255   
 45766  46256   /* #include "sqliteInt.h" */
 45767  46257   
 45768  46258   /* Additional values that can be added to the sync_flags argument of
 45769  46259   ** sqlite3WalFrames():
 45770  46260   */
 45771  46261   #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
................................................................................
 45885  46375   SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
 45886  46376   #endif
 45887  46377   
 45888  46378   /* Return the sqlite3_file object for the WAL file */
 45889  46379   SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
 45890  46380   
 45891  46381   #endif /* ifndef SQLITE_OMIT_WAL */
 45892         -#endif /* _WAL_H_ */
        46382  +#endif /* SQLITE_WAL_H */
 45893  46383   
 45894  46384   /************** End of wal.h *************************************************/
 45895  46385   /************** Continuing where we left off in pager.c **********************/
 45896  46386   
 45897  46387   
 45898  46388   /******************* NOTES ON THE DESIGN OF THE PAGER ************************
 45899  46389   **
................................................................................
 52524  53014   ** then savepoint iSavepoint is also destroyed.
 52525  53015   **
 52526  53016   ** This function may return SQLITE_NOMEM if a memory allocation fails,
 52527  53017   ** or an IO error code if an IO error occurs while rolling back a 
 52528  53018   ** savepoint. If no errors occur, SQLITE_OK is returned.
 52529  53019   */ 
 52530  53020   SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
 52531         -  int rc = pPager->errCode;       /* Return code */
        53021  +  int rc = pPager->errCode;
        53022  +  
        53023  +#ifdef SQLITE_ENABLE_ZIPVFS
        53024  +  if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
        53025  +#endif
 52532  53026   
 52533  53027     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 52534  53028     assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
 52535  53029   
 52536  53030     if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
 52537  53031       int ii;            /* Iterator variable */
 52538  53032       int nNew;          /* Number of remaining savepoints after this op. */
................................................................................
 52565  53059       ** the database file, so the playback operation can be skipped.
 52566  53060       */
 52567  53061       else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
 52568  53062         PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
 52569  53063         rc = pagerPlaybackSavepoint(pPager, pSavepoint);
 52570  53064         assert(rc!=SQLITE_DONE);
 52571  53065       }
        53066  +    
        53067  +#ifdef SQLITE_ENABLE_ZIPVFS
        53068  +    /* If the cache has been modified but the savepoint cannot be rolled 
        53069  +    ** back journal_mode=off, put the pager in the error state. This way,
        53070  +    ** if the VFS used by this pager includes ZipVFS, the entire transaction
        53071  +    ** can be rolled back at the ZipVFS level.  */
        53072  +    else if( 
        53073  +        pPager->journalMode==PAGER_JOURNALMODE_OFF 
        53074  +     && pPager->eState>=PAGER_WRITER_CACHEMOD
        53075  +    ){
        53076  +      pPager->errCode = SQLITE_ABORT;
        53077  +      pPager->eState = PAGER_ERROR;
        53078  +    }
        53079  +#endif
 52572  53080     }
 52573  53081   
 52574  53082     return rc;
 52575  53083   }
 52576  53084   
 52577  53085   /*
 52578  53086   ** Return the full pathname of the database file.
................................................................................
 53034  53542   ** Unless this is an in-memory or temporary database, clear the pager cache.
 53035  53543   */
 53036  53544   SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
 53037  53545     assert( MEMDB==0 || pPager->tempFile );
 53038  53546     if( pPager->tempFile==0 ) pager_reset(pPager);
 53039  53547   }
 53040  53548   #endif
        53549  +
 53041  53550   
 53042  53551   #ifndef SQLITE_OMIT_WAL
 53043  53552   /*
 53044  53553   ** This function is called when the user invokes "PRAGMA wal_checkpoint",
 53045  53554   ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
 53046  53555   ** or wal_blocking_checkpoint() API functions.
 53047  53556   **
................................................................................
 53259  53768   ** is empty, return 0.
 53260  53769   */
 53261  53770   SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
 53262  53771     assert( pPager->eState>=PAGER_READER );
 53263  53772     return sqlite3WalFramesize(pPager->pWal);
 53264  53773   }
 53265  53774   #endif
 53266         -
 53267  53775   
 53268  53776   #endif /* SQLITE_OMIT_DISKIO */
 53269  53777   
 53270  53778   /************** End of pager.c ***********************************************/
 53271  53779   /************** Begin file wal.c *********************************************/
 53272  53780   /*
 53273  53781   ** 2010 February 1
................................................................................
 56376  56884     ** needed and only the sync is done.  If padding is needed, then the
 56377  56885     ** final frame is repeated (with its commit mark) until the next sector
 56378  56886     ** boundary is crossed.  Only the part of the WAL prior to the last
 56379  56887     ** sector boundary is synced; the part of the last frame that extends
 56380  56888     ** past the sector boundary is written after the sync.
 56381  56889     */
 56382  56890     if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
        56891  +    int bSync = 1;
 56383  56892       if( pWal->padToSectorBoundary ){
 56384  56893         int sectorSize = sqlite3SectorSize(pWal->pWalFd);
 56385  56894         w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
        56895  +      bSync = (w.iSyncPoint==iOffset);
        56896  +      testcase( bSync );
 56386  56897         while( iOffset<w.iSyncPoint ){
 56387  56898           rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
 56388  56899           if( rc ) return rc;
 56389  56900           iOffset += szFrame;
 56390  56901           nExtra++;
 56391  56902         }
 56392         -    }else{
        56903  +    }
        56904  +    if( bSync ){
        56905  +      assert( rc==SQLITE_OK );
 56393  56906         rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
 56394  56907       }
 56395  56908     }
 56396  56909   
 56397  56910     /* If this frame set completes the first transaction in the WAL and
 56398  56911     ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
 56399  56912     ** journal size limit, if possible.
................................................................................
 58172  58685   **
 58173  58686   ** Verify that the cursor holds the mutex on its BtShared
 58174  58687   */
 58175  58688   #ifdef SQLITE_DEBUG
 58176  58689   static int cursorHoldsMutex(BtCursor *p){
 58177  58690     return sqlite3_mutex_held(p->pBt->mutex);
 58178  58691   }
        58692  +
        58693  +/* Verify that the cursor and the BtShared agree about what is the current
        58694  +** database connetion. This is important in shared-cache mode. If the database 
        58695  +** connection pointers get out-of-sync, it is possible for routines like
        58696  +** btreeInitPage() to reference an stale connection pointer that references a
        58697  +** a connection that has already closed.  This routine is used inside assert()
        58698  +** statements only and for the purpose of double-checking that the btree code
        58699  +** does keep the database connection pointers up-to-date.
        58700  +*/
 58179  58701   static int cursorOwnsBtShared(BtCursor *p){
 58180  58702     assert( cursorHoldsMutex(p) );
 58181  58703     return (p->pBtree->db==p->pBt->db);
 58182  58704   }
 58183  58705   #endif
 58184  58706   
 58185  58707   /*
................................................................................
 58331  58853   ** If the cursor is open on an intkey table, then the integer key
 58332  58854   ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
 58333  58855   ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is 
 58334  58856   ** set to point to a malloced buffer pCur->nKey bytes in size containing 
 58335  58857   ** the key.
 58336  58858   */
 58337  58859   static int saveCursorKey(BtCursor *pCur){
 58338         -  int rc;
        58860  +  int rc = SQLITE_OK;
 58339  58861     assert( CURSOR_VALID==pCur->eState );
 58340  58862     assert( 0==pCur->pKey );
 58341  58863     assert( cursorHoldsMutex(pCur) );
 58342  58864   
 58343         -  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
 58344         -  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
 58345         -
 58346         -  /* If this is an intKey table, then the above call to BtreeKeySize()
 58347         -  ** stores the integer key in pCur->nKey. In this case this value is
 58348         -  ** all that is required. Otherwise, if pCur is not open on an intKey
 58349         -  ** table, then malloc space for and store the pCur->nKey bytes of key 
 58350         -  ** data.  */
 58351         -  if( 0==pCur->curIntKey ){
 58352         -    void *pKey = sqlite3Malloc( pCur->nKey );
        58865  +  if( pCur->curIntKey ){
        58866  +    /* Only the rowid is required for a table btree */
        58867  +    pCur->nKey = sqlite3BtreeIntegerKey(pCur);
        58868  +  }else{
        58869  +    /* For an index btree, save the complete key content */
        58870  +    void *pKey;
        58871  +    pCur->nKey = sqlite3BtreePayloadSize(pCur);
        58872  +    pKey = sqlite3Malloc( pCur->nKey );
 58353  58873       if( pKey ){
 58354  58874         rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
 58355  58875         if( rc==SQLITE_OK ){
 58356  58876           pCur->pKey = pKey;
 58357  58877         }else{
 58358  58878           sqlite3_free(pKey);
 58359  58879         }
................................................................................
 59320  59840     ** spot on the list where iStart should be inserted.
 59321  59841     */
 59322  59842     hdr = pPage->hdrOffset;
 59323  59843     iPtr = hdr + 1;
 59324  59844     if( data[iPtr+1]==0 && data[iPtr]==0 ){
 59325  59845       iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
 59326  59846     }else{
 59327         -    while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
 59328         -      if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
        59847  +    while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
        59848  +      if( iFreeBlk<iPtr+4 ){
        59849  +        if( iFreeBlk==0 ) break;
        59850  +        return SQLITE_CORRUPT_BKPT;
        59851  +      }
 59329  59852         iPtr = iFreeBlk;
 59330  59853       }
 59331  59854       if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
 59332  59855       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 59333  59856     
 59334  59857       /* At this point:
 59335  59858       **    iFreeBlk:   First freeblock after iStart, or zero if none
................................................................................
 60052  60575       if( rc ) goto btree_open_out;
 60053  60576       pBt->usableSize = pBt->pageSize - nReserve;
 60054  60577       assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
 60055  60578      
 60056  60579   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 60057  60580       /* Add the new BtShared object to the linked list sharable BtShareds.
 60058  60581       */
        60582  +    pBt->nRef = 1;
 60059  60583       if( p->sharable ){
 60060  60584         MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 60061         -      pBt->nRef = 1;
 60062  60585         MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
 60063  60586         if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
 60064  60587           pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
 60065  60588           if( pBt->mutex==0 ){
 60066  60589             rc = SQLITE_NOMEM_BKPT;
 60067  60590             goto btree_open_out;
 60068  60591           }
................................................................................
 60125  60648         sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
 60126  60649       }
 60127  60650     }
 60128  60651     if( mutexOpen ){
 60129  60652       assert( sqlite3_mutex_held(mutexOpen) );
 60130  60653       sqlite3_mutex_leave(mutexOpen);
 60131  60654     }
        60655  +  assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
 60132  60656     return rc;
 60133  60657   }
 60134  60658   
 60135  60659   /*
 60136  60660   ** Decrement the BtShared.nRef counter.  When it reaches zero,
 60137  60661   ** remove the BtShared structure from the sharing list.  Return
 60138  60662   ** true if the BtShared.nRef counter reaches zero and return
................................................................................
 61984  62508   */
 61985  62509   SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
 61986  62510     return pCur && pCur->eState==CURSOR_VALID;
 61987  62511   }
 61988  62512   #endif /* NDEBUG */
 61989  62513   
 61990  62514   /*
 61991         -** Set *pSize to the size of the buffer needed to hold the value of
 61992         -** the key for the current entry.  If the cursor is not pointing
 61993         -** to a valid entry, *pSize is set to 0. 
 61994         -**
 61995         -** For a table with the INTKEY flag set, this routine returns the key
 61996         -** itself, not the number of bytes in the key.
 61997         -**
 61998         -** The caller must position the cursor prior to invoking this routine.
 61999         -** 
 62000         -** This routine cannot fail.  It always returns SQLITE_OK.  
        62515  +** Return the value of the integer key or "rowid" for a table btree.
        62516  +** This routine is only valid for a cursor that is pointing into a
        62517  +** ordinary table btree.  If the cursor points to an index btree or
        62518  +** is invalid, the result of this routine is undefined.
 62001  62519   */
 62002         -SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
        62520  +SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
 62003  62521     assert( cursorHoldsMutex(pCur) );
 62004  62522     assert( pCur->eState==CURSOR_VALID );
        62523  +  assert( pCur->curIntKey );
 62005  62524     getCellInfo(pCur);
 62006         -  *pSize = pCur->info.nKey;
 62007         -  return SQLITE_OK;
        62525  +  return pCur->info.nKey;
 62008  62526   }
 62009  62527   
 62010  62528   /*
 62011         -** Set *pSize to the number of bytes of data in the entry the
 62012         -** cursor currently points to.
        62529  +** Return the number of bytes of payload for the entry that pCur is
        62530  +** currently pointing to.  For table btrees, this will be the amount
        62531  +** of data.  For index btrees, this will be the size of the key.
 62013  62532   **
 62014  62533   ** The caller must guarantee that the cursor is pointing to a non-NULL
 62015  62534   ** valid entry.  In other words, the calling procedure must guarantee
 62016  62535   ** that the cursor has Cursor.eState==CURSOR_VALID.
 62017         -**
 62018         -** Failure is not possible.  This function always returns SQLITE_OK.
 62019         -** It might just as well be a procedure (returning void) but we continue
 62020         -** to return an integer result code for historical reasons.
 62021  62536   */
 62022         -SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
 62023         -  assert( cursorOwnsBtShared(pCur) );
        62537  +SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
        62538  +  assert( cursorHoldsMutex(pCur) );
 62024  62539     assert( pCur->eState==CURSOR_VALID );
 62025         -  assert( pCur->iPage>=0 );
 62026         -  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 62027         -  assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
 62028  62540     getCellInfo(pCur);
 62029         -  *pSize = pCur->info.nPayload;
 62030         -  return SQLITE_OK;
        62541  +  return pCur->info.nPayload;
 62031  62542   }
 62032  62543   
 62033  62544   /*
 62034  62545   ** Given the page number of an overflow page in the database (parameter
 62035  62546   ** ovfl), this function finds the page number of the next page in the 
 62036  62547   ** linked list of overflow pages. If possible, it uses the auto-vacuum
 62037  62548   ** pointer-map data instead of reading the content of page ovfl to do so. 
................................................................................
 62465  62976   ** including calls from other threads against the same cache.
 62466  62977   ** Hence, a mutex on the BtShared should be held prior to calling
 62467  62978   ** this routine.
 62468  62979   **
 62469  62980   ** These routines is used to get quick access to key and data
 62470  62981   ** in the common case where no overflow pages are used.
 62471  62982   */
 62472         -SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
 62473         -  return fetchPayload(pCur, pAmt);
 62474         -}
 62475         -SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
        62983  +SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
 62476  62984     return fetchPayload(pCur, pAmt);
 62477  62985   }
 62478  62986   
 62479  62987   
 62480  62988   /*
 62481  62989   ** Move the cursor down to a new child page.  The newPgno argument is the
 62482  62990   ** page number of the child page to move to.
................................................................................
 62801  63309     int rc;
 62802  63310     RecordCompare xRecordCompare;
 62803  63311   
 62804  63312     assert( cursorOwnsBtShared(pCur) );
 62805  63313     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 62806  63314     assert( pRes );
 62807  63315     assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
        63316  +  assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
 62808  63317   
 62809  63318     /* If the cursor is already positioned at the point we are trying
 62810  63319     ** to move to, then just return without doing any work */
 62811         -  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
 62812         -   && pCur->curIntKey 
        63320  +  if( pIdxKey==0
        63321  +   && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
 62813  63322     ){
 62814  63323       if( pCur->info.nKey==intKey ){
 62815  63324         *pRes = 0;
 62816  63325         return SQLITE_OK;
 62817  63326       }
 62818  63327       if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
 62819  63328         *pRes = -1;
................................................................................
 63794  64303   ** area.  pCell might point to some temporary storage.  The cell will
 63795  64304   ** be constructed in this temporary area then copied into pPage->aData
 63796  64305   ** later.
 63797  64306   */
 63798  64307   static int fillInCell(
 63799  64308     MemPage *pPage,                /* The page that contains the cell */
 63800  64309     unsigned char *pCell,          /* Complete text of the cell */
 63801         -  const void *pKey, i64 nKey,    /* The key */
 63802         -  const void *pData,int nData,   /* The data */
 63803         -  int nZero,                     /* Extra zero bytes to append to pData */
        64310  +  const BtreePayload *pX,        /* Payload with which to construct the cell */
 63804  64311     int *pnSize                    /* Write cell size here */
 63805  64312   ){
 63806  64313     int nPayload;
 63807  64314     const u8 *pSrc;
 63808  64315     int nSrc, n, rc;
 63809  64316     int spaceLeft;
 63810  64317     MemPage *pOvfl = 0;
................................................................................
 63820  64327     /* pPage is not necessarily writeable since pCell might be auxiliary
 63821  64328     ** buffer space that is separate from the pPage buffer area */
 63822  64329     assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
 63823  64330               || sqlite3PagerIswriteable(pPage->pDbPage) );
 63824  64331   
 63825  64332     /* Fill in the header. */
 63826  64333     nHeader = pPage->childPtrSize;
 63827         -  nPayload = nData + nZero;
 63828         -  if( pPage->intKeyLeaf ){
 63829         -    nHeader += putVarint32(&pCell[nHeader], nPayload);
 63830         -  }else{
 63831         -    assert( nData==0 );
 63832         -    assert( nZero==0 );
 63833         -  }
 63834         -  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
 63835         -  
 63836         -  /* Fill in the payload size */
 63837  64334     if( pPage->intKey ){
 63838         -    pSrc = pData;
 63839         -    nSrc = nData;
 63840         -    nData = 0;
        64335  +    nPayload = pX->nData + pX->nZero;
        64336  +    pSrc = pX->pData;
        64337  +    nSrc = pX->nData;
        64338  +    assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
        64339  +    nHeader += putVarint32(&pCell[nHeader], nPayload);
        64340  +    nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
 63841  64341     }else{ 
 63842         -    assert( nKey<=0x7fffffff && pKey!=0 );
 63843         -    nPayload = (int)nKey;
 63844         -    pSrc = pKey;
 63845         -    nSrc = (int)nKey;
        64342  +    assert( pX->nData==0 );
        64343  +    assert( pX->nZero==0 );
        64344  +    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
        64345  +    nSrc = nPayload = (int)pX->nKey;
        64346  +    pSrc = pX->pKey;
        64347  +    nHeader += putVarint32(&pCell[nHeader], nPayload);
 63846  64348     }
        64349  +  
        64350  +  /* Fill in the payload */
 63847  64351     if( nPayload<=pPage->maxLocal ){
 63848  64352       n = nHeader + nPayload;
 63849  64353       testcase( n==3 );
 63850  64354       testcase( n==4 );
 63851  64355       if( n<4 ) n = 4;
 63852  64356       *pnSize = n;
 63853  64357       spaceLeft = nPayload;
................................................................................
 63877  64381     ** were computed correctly.
 63878  64382     */
 63879  64383   #if SQLITE_DEBUG
 63880  64384     {
 63881  64385       CellInfo info;
 63882  64386       pPage->xParseCell(pPage, pCell, &info);
 63883  64387       assert( nHeader==(int)(info.pPayload - pCell) );
 63884         -    assert( info.nKey==nKey );
        64388  +    assert( info.nKey==pX->nKey );
 63885  64389       assert( *pnSize == info.nSize );
 63886  64390       assert( spaceLeft == info.nLocal );
 63887  64391     }
 63888  64392   #endif
 63889  64393   
 63890  64394     /* Write the payload into the local Cell and any extra into overflow pages */
 63891  64395     while( nPayload>0 ){
................................................................................
 63962  64466         memset(pPayload, 0, n);
 63963  64467       }
 63964  64468       nPayload -= n;
 63965  64469       pPayload += n;
 63966  64470       pSrc += n;
 63967  64471       nSrc -= n;
 63968  64472       spaceLeft -= n;
 63969         -    if( nSrc==0 ){
 63970         -      nSrc = nData;
 63971         -      pSrc = pData;
 63972         -    }
 63973  64473     }
 63974  64474     releasePage(pToRelease);
 63975  64475     return SQLITE_OK;
 63976  64476   }
 63977  64477   
 63978  64478   /*
 63979  64479   ** Remove the i-th cell from pPage.  This routine effects pPage only.
................................................................................
 64032  64532   ** If the cell content will fit on the page, then put it there.  If it
 64033  64533   ** will not fit, then make a copy of the cell content into pTemp if
 64034  64534   ** pTemp is not null.  Regardless of pTemp, allocate a new entry
 64035  64535   ** in pPage->apOvfl[] and make it point to the cell content (either
 64036  64536   ** in pTemp or the original pCell) and also record its index. 
 64037  64537   ** Allocating a new entry in pPage->aCell[] implies that 
 64038  64538   ** pPage->nOverflow is incremented.
        64539  +**
        64540  +** *pRC must be SQLITE_OK when this routine is called.
 64039  64541   */
 64040  64542   static void insertCell(
 64041  64543     MemPage *pPage,   /* Page into which we are copying */
 64042  64544     int i,            /* New cell becomes the i-th cell of the page */
 64043  64545     u8 *pCell,        /* Content of the new cell */
 64044  64546     int sz,           /* Bytes of content in pCell */
 64045  64547     u8 *pTemp,        /* Temp storage space for pCell, if needed */
................................................................................
 64047  64549     int *pRC          /* Read and write return code from here */
 64048  64550   ){
 64049  64551     int idx = 0;      /* Where to write new cell content in data[] */
 64050  64552     int j;            /* Loop counter */
 64051  64553     u8 *data;         /* The content of the whole page */
 64052  64554     u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
 64053  64555   
 64054         -  if( *pRC ) return;
 64055         -
        64556  +  assert( *pRC==SQLITE_OK );
 64056  64557     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
 64057  64558     assert( MX_CELL(pPage->pBt)<=10921 );
 64058  64559     assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
 64059  64560     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
 64060  64561     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
 64061  64562     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 64062  64563     /* The cell should normally be sized correctly.  However, when moving a
................................................................................
 64122  64623       }
 64123  64624   #endif
 64124  64625     }
 64125  64626   }
 64126  64627   
 64127  64628   /*
 64128  64629   ** A CellArray object contains a cache of pointers and sizes for a
 64129         -** consecutive sequence of cells that might be held multiple pages.
        64630  +** consecutive sequence of cells that might be held on multiple pages.
 64130  64631   */
 64131  64632   typedef struct CellArray CellArray;
 64132  64633   struct CellArray {
 64133  64634     int nCell;              /* Number of cells in apCell[] */
 64134  64635     MemPage *pRef;          /* Reference page */
 64135  64636     u8 **apCell;            /* All cells begin balanced */
 64136  64637     u16 *szCell;            /* Local size of all cells in apCell[] */
................................................................................
 64554  65055       pCell = findCell(pPage, pPage->nCell-1);
 64555  65056       pStop = &pCell[9];
 64556  65057       while( (*(pCell++)&0x80) && pCell<pStop );
 64557  65058       pStop = &pCell[9];
 64558  65059       while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
 64559  65060   
 64560  65061       /* Insert the new divider cell into pParent. */
        65062  +    if( rc==SQLITE_OK ){
 64561  65063       insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
 64562  65064                  0, pPage->pgno, &rc);
        65065  +    }
 64563  65066   
 64564  65067       /* Set the right-child pointer of pParent to point to the new page. */
 64565  65068       put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
 64566  65069     
 64567  65070       /* Release the reference to the new page. */
 64568  65071       releasePage(pNew);
 64569  65072     }
................................................................................
 65075  65578       d = r + 1 - leafData;
 65076  65579       (void)cachedCellSize(&b, d);
 65077  65580       do{
 65078  65581         assert( d<nMaxCells );
 65079  65582         assert( r<nMaxCells );
 65080  65583         (void)cachedCellSize(&b, r);
 65081  65584         if( szRight!=0
 65082         -       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
        65585  +       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
 65083  65586           break;
 65084  65587         }
 65085  65588         szRight += b.szCell[d] + 2;
 65086  65589         szLeft -= b.szCell[r] + 2;
 65087  65590         cntNew[i-1] = r;
 65088  65591         r--;
 65089  65592         d--;
................................................................................
 65647  66150       sqlite3PageFree(pFree);
 65648  66151     }
 65649  66152     return rc;
 65650  66153   }
 65651  66154   
 65652  66155   
 65653  66156   /*
 65654         -** Insert a new record into the BTree.  The key is given by (pKey,nKey)
 65655         -** and the data is given by (pData,nData).  The cursor is used only to
 65656         -** define what table the record should be inserted into.  The cursor
 65657         -** is left pointing at a random location.
        66157  +** Insert a new record into the BTree.  The content of the new record
        66158  +** is described by the pX object.  The pCur cursor is used only to
        66159  +** define what table the record should be inserted into, and is left
        66160  +** pointing at a random location.
 65658  66161   **
 65659         -** For an INTKEY table, only the nKey value of the key is used.  pKey is
 65660         -** ignored.  For a ZERODATA table, the pData and nData are both ignored.
        66162  +** For a table btree (used for rowid tables), only the pX.nKey value of
        66163  +** the key is used. The pX.pKey value must be NULL.  The pX.nKey is the
        66164  +** rowid or INTEGER PRIMARY KEY of the row.  The pX.nData,pData,nZero fields
        66165  +** hold the content of the row.
        66166  +**
        66167  +** For an index btree (used for indexes and WITHOUT ROWID tables), the
        66168  +** key is an arbitrary byte sequence stored in pX.pKey,nKey.  The 
        66169  +** pX.pData,nData,nZero fields must be zero.
 65661  66170   **
 65662  66171   ** If the seekResult parameter is non-zero, then a successful call to
 65663  66172   ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
 65664  66173   ** been performed. seekResult is the search result returned (a negative
 65665  66174   ** number if pCur points at an entry that is smaller than (pKey, nKey), or
 65666  66175   ** a positive value if pCur points at an entry that is larger than 
 65667  66176   ** (pKey, nKey)). 
................................................................................
 65670  66179   ** cursor pCur is pointing at the existing copy of a row that is to be
 65671  66180   ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
 65672  66181   ** point to any entry or to no entry at all and so this function has to seek
 65673  66182   ** the cursor before the new key can be inserted.
 65674  66183   */
 65675  66184   SQLITE_PRIVATE int sqlite3BtreeInsert(
 65676  66185     BtCursor *pCur,                /* Insert data into the table of this cursor */
 65677         -  const void *pKey, i64 nKey,    /* The key of the new record */
 65678         -  const void *pData, int nData,  /* The data of the new record */
 65679         -  int nZero,                     /* Number of extra 0 bytes to append to data */
        66186  +  const BtreePayload *pX,        /* Content of the row to be inserted */
 65680  66187     int appendBias,                /* True if this is likely an append */
 65681  66188     int seekResult                 /* Result of prior MovetoUnpacked() call */
 65682  66189   ){
 65683  66190     int rc;
 65684  66191     int loc = seekResult;          /* -1: before desired location  +1: after */
 65685  66192     int szNew = 0;
 65686  66193     int idx;
................................................................................
 65702  66209     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 65703  66210   
 65704  66211     /* Assert that the caller has been consistent. If this cursor was opened
 65705  66212     ** expecting an index b-tree, then the caller should be inserting blob
 65706  66213     ** keys with no associated data. If the cursor was opened expecting an
 65707  66214     ** intkey table, the caller should be inserting integer keys with a
 65708  66215     ** blob of associated data.  */
 65709         -  assert( (pKey==0)==(pCur->pKeyInfo==0) );
        66216  +  assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
 65710  66217   
 65711  66218     /* Save the positions of any other cursors open on this table.
 65712  66219     **
 65713  66220     ** In some cases, the call to btreeMoveto() below is a no-op. For
 65714  66221     ** example, when inserting data into a table with auto-generated integer
 65715  66222     ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
 65716  66223     ** integer key to use. It then calls this function to actually insert the 
................................................................................
 65721  66228     */
 65722  66229     if( pCur->curFlags & BTCF_Multiple ){
 65723  66230       rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 65724  66231       if( rc ) return rc;
 65725  66232     }
 65726  66233   
 65727  66234     if( pCur->pKeyInfo==0 ){
 65728         -    assert( pKey==0 );
        66235  +    assert( pX->pKey==0 );
 65729  66236       /* If this is an insert into a table b-tree, invalidate any incrblob 
 65730  66237       ** cursors open on the row being replaced */
 65731         -    invalidateIncrblobCursors(p, nKey, 0);
        66238  +    invalidateIncrblobCursors(p, pX->nKey, 0);
 65732  66239   
 65733  66240       /* If the cursor is currently on the last row and we are appending a
 65734  66241       ** new row onto the end, set the "loc" to avoid an unnecessary
 65735  66242       ** btreeMoveto() call */
 65736         -    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
 65737         -      && pCur->info.nKey==nKey-1 ){
        66243  +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
        66244  +      && pCur->info.nKey==pX->nKey-1 ){
 65738  66245          loc = -1;
 65739  66246       }else if( loc==0 ){
 65740         -      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
        66247  +      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
 65741  66248         if( rc ) return rc;
 65742  66249       }
 65743  66250     }else if( loc==0 ){
 65744         -    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
        66251  +    rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
 65745  66252       if( rc ) return rc;
 65746  66253     }
 65747  66254     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 65748  66255   
 65749  66256     pPage = pCur->apPage[pCur->iPage];
 65750         -  assert( pPage->intKey || nKey>=0 );
        66257  +  assert( pPage->intKey || pX->nKey>=0 );
 65751  66258     assert( pPage->leaf || !pPage->intKey );
 65752  66259   
 65753  66260     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 65754         -          pCur->pgnoRoot, nKey, nData, pPage->pgno,
        66261  +          pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
 65755  66262             loc==0 ? "overwrite" : "new entry"));
 65756  66263     assert( pPage->isInit );
 65757  66264     newCell = pBt->pTmpSpace;
 65758  66265     assert( newCell!=0 );
 65759         -  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
        66266  +  rc = fillInCell(pPage, newCell, pX, &szNew);
 65760  66267     if( rc ) goto end_insert;
 65761  66268     assert( szNew==pPage->xCellSize(pPage, newCell) );
 65762  66269     assert( szNew <= MX_CELL_SIZE(pBt) );
 65763  66270     idx = pCur->aiIdx[pCur->iPage];
 65764  66271     if( loc==0 ){
 65765  66272       u16 szOld;
 65766  66273       assert( idx<pPage->nCell );
................................................................................
 65778  66285     }else if( loc<0 && pPage->nCell>0 ){
 65779  66286       assert( pPage->leaf );
 65780  66287       idx = ++pCur->aiIdx[pCur->iPage];
 65781  66288     }else{
 65782  66289       assert( pPage->leaf );
 65783  66290     }
 65784  66291     insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
        66292  +  assert( pPage->nOverflow==0 || rc==SQLITE_OK );
 65785  66293     assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
 65786  66294   
 65787  66295     /* If no error has occurred and pPage has an overflow cell, call balance() 
 65788  66296     ** to redistribute the cells within the tree. Since balance() may move
 65789  66297     ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
 65790  66298     ** variables.
 65791  66299     **
................................................................................
 65801  66309     ** is advantageous to leave the cursor pointing to the last entry in
 65802  66310     ** the b-tree if possible. If the cursor is left pointing to the last
 65803  66311     ** entry in the table, and the next row inserted has an integer key
 65804  66312     ** larger than the largest existing key, it is possible to insert the
 65805  66313     ** row without seeking the cursor. This can be a big performance boost.
 65806  66314     */
 65807  66315     pCur->info.nSize = 0;
 65808         -  if( rc==SQLITE_OK && pPage->nOverflow ){
        66316  +  if( pPage->nOverflow ){
        66317  +    assert( rc==SQLITE_OK );
 65809  66318       pCur->curFlags &= ~(BTCF_ValidNKey);
 65810  66319       rc = balance(pCur);
 65811  66320   
 65812  66321       /* Must make sure nOverflow is reset to zero even if the balance()
 65813  66322       ** fails. Internal data structure corruption will result otherwise. 
 65814  66323       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 65815  66324       ** from trying to save the current position of the cursor.  */
................................................................................
 65937  66446       pCell = findCell(pLeaf, pLeaf->nCell-1);
 65938  66447       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
 65939  66448       nCell = pLeaf->xCellSize(pLeaf, pCell);
 65940  66449       assert( MX_CELL_SIZE(pBt) >= nCell );
 65941  66450       pTmp = pBt->pTmpSpace;
 65942  66451       assert( pTmp!=0 );
 65943  66452       rc = sqlite3PagerWrite(pLeaf->pDbPage);
        66453  +    if( rc==SQLITE_OK ){
 65944  66454       insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
        66455  +    }
 65945  66456       dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
 65946  66457       if( rc ) return rc;
 65947  66458     }
 65948  66459   
 65949  66460     /* Balance the tree. If the entry deleted was located on a leaf page,
 65950  66461     ** then the cursor still points to that page. In this case the first
 65951  66462     ** call to balance() repairs the tree, and the if(...) condition is
................................................................................
 67426  67937   #if !defined(SQLITE_OMIT_SHARED_CACHE)
 67427  67938   /*
 67428  67939   ** Return true if the Btree passed as the only argument is sharable.
 67429  67940   */
 67430  67941   SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
 67431  67942     return p->sharable;
 67432  67943   }
        67944  +
        67945  +/*
        67946  +** Return the number of connections to the BtShared object accessed by
        67947  +** the Btree handle passed as the only argument. For private caches 
        67948  +** this is always 1. For shared caches it may be 1 or greater.
        67949  +*/
        67950  +SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
        67951  +  testcase( p->sharable );
        67952  +  return p->pBt->nRef;
        67953  +}
 67433  67954   #endif
 67434  67955   
 67435  67956   /************** End of btree.c ***********************************************/
 67436  67957   /************** Begin file backup.c ******************************************/
 67437  67958   /*
 67438  67959   ** 2009 January 28
 67439  67960   **
................................................................................
 67628  68149       p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 67629  68150       p->pDestDb = pDestDb;
 67630  68151       p->pSrcDb = pSrcDb;
 67631  68152       p->iNext = 1;
 67632  68153       p->isAttached = 0;
 67633  68154   
 67634  68155       if( 0==p->pSrc || 0==p->pDest 
 67635         -     || setDestPgsz(p)==SQLITE_NOMEM 
 67636  68156        || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK 
 67637  68157        ){
 67638  68158         /* One (or both) of the named databases did not exist or an OOM
 67639  68159         ** error was hit. Or there is a transaction open on the destination
 67640  68160         ** database. The error has already been written into the pDestDb 
 67641  68161         ** handle. All that is left to do here is free the sqlite3_backup 
 67642  68162         ** structure.  */
................................................................................
 67816  68336       */
 67817  68337       if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
 67818  68338         rc = SQLITE_BUSY;
 67819  68339       }else{
 67820  68340         rc = SQLITE_OK;
 67821  68341       }
 67822  68342   
 67823         -    /* Lock the destination database, if it is not locked already. */
 67824         -    if( SQLITE_OK==rc && p->bDestLocked==0
 67825         -     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
 67826         -    ){
 67827         -      p->bDestLocked = 1;
 67828         -      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
 67829         -    }
 67830         -
 67831  68343       /* If there is no open read-transaction on the source database, open
 67832  68344       ** one now. If a transaction is opened here, then it will be closed
 67833  68345       ** before this function exits.
 67834  68346       */
 67835  68347       if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
 67836  68348         rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
 67837  68349         bCloseTrans = 1;
 67838  68350       }
        68351  +
        68352  +    /* If the destination database has not yet been locked (i.e. if this
        68353  +    ** is the first call to backup_step() for the current backup operation),
        68354  +    ** try to set its page size to the same as the source database. This
        68355  +    ** is especially important on ZipVFS systems, as in that case it is
        68356  +    ** not possible to create a database file that uses one page size by
        68357  +    ** writing to it with another.  */
        68358  +    if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
        68359  +      rc = SQLITE_NOMEM;
        68360  +    }
        68361  +
        68362  +    /* Lock the destination database, if it is not locked already. */
        68363  +    if( SQLITE_OK==rc && p->bDestLocked==0
        68364  +     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
        68365  +    ){
        68366  +      p->bDestLocked = 1;
        68367  +      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
        68368  +    }
 67839  68369   
 67840  68370       /* Do not allow backup if the destination database is in WAL mode
 67841  68371       ** and the page sizes are different between source and destination */
 67842  68372       pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
 67843  68373       pgszDest = sqlite3BtreeGetPageSize(p->pDest);
 67844  68374       destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
 67845  68375       if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
................................................................................
 68209  68739   #endif
 68210  68740   
 68211  68741     /* 0x7FFFFFFF is the hard limit for the number of pages in a database
 68212  68742     ** file. By passing this as the number of pages to copy to
 68213  68743     ** sqlite3_backup_step(), we can guarantee that the copy finishes 
 68214  68744     ** within a single call (unless an error occurs). The assert() statement
 68215  68745     ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
 68216         -  ** or an error code.
 68217         -  */
        68746  +  ** or an error code.  */
 68218  68747     sqlite3_backup_step(&b, 0x7FFFFFFF);
 68219  68748     assert( b.rc!=SQLITE_OK );
        68749  +
 68220  68750     rc = sqlite3_backup_finish(&b);
 68221  68751     if( rc==SQLITE_OK ){
 68222  68752       pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
 68223  68753     }else{
 68224  68754       sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
 68225  68755     }
 68226  68756   
................................................................................
 68424  68954   **
 68425  68955   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 68426  68956   */
 68427  68957   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 68428  68958     int f;
 68429  68959     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 68430  68960     assert( (pMem->flags&MEM_RowSet)==0 );
 68431         -  ExpandBlob(pMem);
        68961  +  (void)ExpandBlob(pMem);
 68432  68962     f = pMem->flags;
 68433  68963     if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
 68434  68964       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 68435  68965         return SQLITE_NOMEM_BKPT;
 68436  68966       }
 68437  68967       pMem->z[pMem->n] = 0;
 68438  68968       pMem->z[pMem->n+1] = 0;
................................................................................
 68807  69337       }else{
 68808  69338         pMem->u.r = sqlite3VdbeRealValue(pMem);
 68809  69339         MemSetTypeFlag(pMem, MEM_Real);
 68810  69340         sqlite3VdbeIntegerAffinity(pMem);
 68811  69341       }
 68812  69342     }
 68813  69343     assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 68814         -  pMem->flags &= ~(MEM_Str|MEM_Blob);
        69344  +  pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
 68815  69345     return SQLITE_OK;
 68816  69346   }
 68817  69347   
 68818  69348   /*
 68819  69349   ** Cast the datatype of the value in pMem according to the affinity
 68820  69350   ** "aff".  Casting is different from applying affinity in that a cast
 68821  69351   ** is forced.  In other words, the value is converted into the desired
................................................................................
 69223  69753   
 69224  69754     assert( sqlite3BtreeCursorIsValid(pCur) );
 69225  69755     assert( !VdbeMemDynamic(pMem) );
 69226  69756   
 69227  69757     /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
 69228  69758     ** that both the BtShared and database handle mutexes are held. */
 69229  69759     assert( (pMem->flags & MEM_RowSet)==0 );
 69230         -  if( key ){
 69231         -    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
 69232         -  }else{
 69233         -    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
 69234         -  }
        69760  +  zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
 69235  69761     assert( zData!=0 );
 69236  69762   
 69237  69763     if( offset+amt<=available ){
 69238  69764       pMem->z = &zData[offset];
 69239  69765       pMem->flags = MEM_Blob|MEM_Ephem;
 69240  69766       pMem->n = (int)amt;
 69241  69767     }else{
................................................................................
 69254  69780     assert( pVal!=0 );
 69255  69781     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 69256  69782     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 69257  69783     assert( (pVal->flags & MEM_RowSet)==0 );
 69258  69784     assert( (pVal->flags & (MEM_Null))==0 );
 69259  69785     if( pVal->flags & (MEM_Blob|MEM_Str) ){
 69260  69786       pVal->flags |= MEM_Str;
 69261         -    if( pVal->flags & MEM_Zero ){
 69262         -      sqlite3VdbeMemExpandBlob(pVal);
 69263         -    }
 69264  69787       if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
 69265  69788         sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 69266  69789       }
 69267  69790       if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 69268  69791         assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 69269  69792         if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
 69270  69793           return 0;
................................................................................
 69756  70279   }
 69757  70280   
 69758  70281   /*
 69759  70282   ** This function is used to allocate and populate UnpackedRecord 
 69760  70283   ** structures intended to be compared against sample index keys stored 
 69761  70284   ** in the sqlite_stat4 table.
 69762  70285   **
 69763         -** A single call to this function attempts to populates field iVal (leftmost 
 69764         -** is 0 etc.) of the unpacked record with a value extracted from expression
 69765         -** pExpr. Extraction of values is possible if:
        70286  +** A single call to this function populates zero or more fields of the
        70287  +** record starting with field iVal (fields are numbered from left to
        70288  +** right starting with 0). A single field is populated if:
 69766  70289   **
 69767  70290   **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
 69768  70291   **
 69769  70292   **  * The expression is a bound variable, and this is a reprepare, or
 69770  70293   **
 69771  70294   **  * The sqlite3ValueFromExpr() function is able to extract a value 
 69772  70295   **    from the expression (i.e. the expression is a literal value).
 69773  70296   **
 69774         -** If a value can be extracted, the affinity passed as the 5th argument
 69775         -** is applied to it before it is copied into the UnpackedRecord. Output
 69776         -** parameter *pbOk is set to true if a value is extracted, or false 
 69777         -** otherwise.
        70297  +** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
        70298  +** vector components that match either of the two latter criteria listed
        70299  +** above.
        70300  +**
        70301  +** Before any value is appended to the record, the affinity of the 
        70302  +** corresponding column within index pIdx is applied to it. Before
        70303  +** this function returns, output parameter *pnExtract is set to the
        70304  +** number of values appended to the record.
 69778  70305   **
 69779  70306   ** When this function is called, *ppRec must either point to an object
 69780  70307   ** allocated by an earlier call to this function, or must be NULL. If it
 69781  70308   ** is NULL and a value can be successfully extracted, a new UnpackedRecord
 69782  70309   ** is allocated (and *ppRec set to point to it) before returning.
 69783  70310   **
 69784  70311   ** Unless an error is encountered, SQLITE_OK is returned. It is not an
................................................................................
 69786  70313   ** occur, an SQLite error code is returned.
 69787  70314   */
 69788  70315   SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
 69789  70316     Parse *pParse,                  /* Parse context */
 69790  70317     Index *pIdx,                    /* Index being probed */
 69791  70318     UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
 69792  70319     Expr *pExpr,                    /* The expression to extract a value from */
 69793         -  u8 affinity,                    /* Affinity to use */
        70320  +  int nElem,                      /* Maximum number of values to append */
 69794  70321     int iVal,                       /* Array element to populate */
 69795         -  int *pbOk                       /* OUT: True if value was extracted */
        70322  +  int *pnExtract                  /* OUT: Values appended to the record */
 69796  70323   ){
 69797         -  int rc;
 69798         -  sqlite3_value *pVal = 0;
        70324  +  int rc = SQLITE_OK;
        70325  +  int nExtract = 0;
        70326  +
        70327  +  if( pExpr==0 || pExpr->op!=TK_SELECT ){
        70328  +    int i;
 69799  70329     struct ValueNewStat4Ctx alloc;
 69800  70330   
 69801  70331     alloc.pParse = pParse;
 69802  70332     alloc.pIdx = pIdx;
 69803  70333     alloc.ppRec = ppRec;
 69804         -  alloc.iVal = iVal;
 69805  70334   
 69806         -  rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
 69807         -  assert( pVal==0 || pVal->db==pParse->db );
 69808         -  *pbOk = (pVal!=0);
        70335  +    for(i=0; i<nElem; i++){
        70336  +      sqlite3_value *pVal = 0;
        70337  +      Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
        70338  +      u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
        70339  +      alloc.iVal = iVal+i;
        70340  +      rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
        70341  +      if( !pVal ) break;
        70342  +      nExtract++;
        70343  +    }
        70344  +  }
        70345  +
        70346  +  *pnExtract = nExtract;
 69809  70347     return rc;
 69810  70348   }
 69811  70349   
 69812  70350   /*
 69813  70351   ** Attempt to extract a value from expression pExpr using the methods
 69814  70352   ** as described for sqlite3Stat4ProbeSetValue() above. 
 69815  70353   **
................................................................................
 70007  70545     if( !isPrepareV2 ) return;
 70008  70546   #endif
 70009  70547     assert( p->zSql==0 );
 70010  70548     p->zSql = sqlite3DbStrNDup(p->db, z, n);
 70011  70549     p->isPrepareV2 = (u8)isPrepareV2;
 70012  70550   }
 70013  70551   
 70014         -/*
 70015         -** Return the SQL associated with a prepared statement
 70016         -*/
 70017         -SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
 70018         -  Vdbe *p = (Vdbe *)pStmt;
 70019         -  return p ? p->zSql : 0;
 70020         -}
 70021         -
 70022  70552   /*
 70023  70553   ** Swap all content between two VDBE structures.
 70024  70554   */
 70025  70555   SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 70026  70556     Vdbe tmp, *pTmp;
 70027  70557     char *zTmp;
 70028  70558     assert( pA->db==pB->db );
................................................................................
 70734  71264   
 70735  71265   
 70736  71266   /*
 70737  71267   ** If the input FuncDef structure is ephemeral, then free it.  If
 70738  71268   ** the FuncDef is not ephermal, then do nothing.
 70739  71269   */
 70740  71270   static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
 70741         -  if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
        71271  +  if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
 70742  71272       sqlite3DbFree(db, pDef);
 70743  71273     }
 70744  71274   }
 70745  71275   
 70746  71276   static void vdbeFreeOpArray(sqlite3 *, Op *, int);
 70747  71277   
 70748  71278   /*
 70749  71279   ** Delete a P4 value if necessary.
 70750  71280   */
        71281  +static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
        71282  +  if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
        71283  +  sqlite3DbFree(db, p);
        71284  +}
        71285  +static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
        71286  +  freeEphemeralFunction(db, p->pFunc);
        71287  +  sqlite3DbFree(db, p);
        71288  +}
 70751  71289   static void freeP4(sqlite3 *db, int p4type, void *p4){
 70752  71290     assert( db );
 70753  71291     switch( p4type ){
 70754  71292       case P4_FUNCCTX: {
 70755         -      freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
 70756         -      /* Fall through into the next case */
        71293  +      freeP4FuncCtx(db, (sqlite3_context*)p4);
        71294  +      break;
 70757  71295       }
 70758  71296       case P4_REAL:
 70759  71297       case P4_INT64:
 70760  71298       case P4_DYNAMIC:
 70761  71299       case P4_INTARRAY: {
 70762  71300         sqlite3DbFree(db, p4);
 70763  71301         break;
................................................................................
 70780  71318         freeEphemeralFunction(db, (FuncDef*)p4);
 70781  71319         break;
 70782  71320       }
 70783  71321       case P4_MEM: {
 70784  71322         if( db->pnBytesFreed==0 ){
 70785  71323           sqlite3ValueFree((sqlite3_value*)p4);
 70786  71324         }else{
 70787         -        Mem *p = (Mem*)p4;
 70788         -        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
 70789         -        sqlite3DbFree(db, p);
        71325  +        freeP4Mem(db, (Mem*)p4);
 70790  71326         }
 70791  71327         break;
 70792  71328       }
 70793  71329       case P4_VTAB : {
 70794  71330         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
 70795  71331         break;
 70796  71332       }
................................................................................
 71039  71575     char *zTemp,       /* Write result here */
 71040  71576     int nTemp          /* Space available in zTemp[] */
 71041  71577   ){
 71042  71578     const char *zOpName;
 71043  71579     const char *zSynopsis;
 71044  71580     int nOpName;
 71045  71581     int ii, jj;
        71582  +  char zAlt[50];
 71046  71583     zOpName = sqlite3OpcodeName(pOp->opcode);
 71047  71584     nOpName = sqlite3Strlen30(zOpName);
 71048  71585     if( zOpName[nOpName+1] ){
 71049  71586       int seenCom = 0;
 71050  71587       char c;
 71051  71588       zSynopsis = zOpName += nOpName + 1;
        71589  +    if( strncmp(zSynopsis,"IF ",3)==0 ){
        71590  +      if( pOp->p5 & SQLITE_STOREP2 ){
        71591  +        sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
        71592  +      }else{
        71593  +        sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
        71594  +      }
        71595  +      zSynopsis = zAlt;
        71596  +    }
 71052  71597       for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
 71053  71598         if( c=='P' ){
 71054  71599           c = zSynopsis[++ii];
 71055  71600           if( c=='4' ){
 71056  71601             sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
 71057  71602           }else if( c=='X' ){
 71058  71603             sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
................................................................................
 71805  72350     Parse *pParse                  /* Parsing context */
 71806  72351   ){
 71807  72352     sqlite3 *db;                   /* The database connection */
 71808  72353     int nVar;                      /* Number of parameters */
 71809  72354     int nMem;                      /* Number of VM memory registers */
 71810  72355     int nCursor;                   /* Number of cursors required */
 71811  72356     int nArg;                      /* Number of arguments in subprograms */
 71812         -  int nOnce;                     /* Number of OP_Once instructions */
 71813  72357     int n;                         /* Loop counter */
 71814  72358     struct ReusableSpace x;        /* Reusable bulk memory */
 71815  72359   
 71816  72360     assert( p!=0 );
 71817  72361     assert( p->nOp>0 );
 71818  72362     assert( pParse!=0 );
 71819  72363     assert( p->magic==VDBE_MAGIC_INIT );
................................................................................
 71820  72364     assert( pParse==p->pParse );
 71821  72365     db = p->db;
 71822  72366     assert( db->mallocFailed==0 );
 71823  72367     nVar = pParse->nVar;
 71824  72368     nMem = pParse->nMem;
 71825  72369     nCursor = pParse->nTab;
 71826  72370     nArg = pParse->nMaxArg;
 71827         -  nOnce = pParse->nOnce;
 71828         -  if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
 71829  72371     
 71830  72372     /* Each cursor uses a memory cell.  The first cursor (cursor 0) can
 71831  72373     ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
 71832  72374     ** space at the end of aMem[] for cursors 1 and greater.
 71833  72375     ** See also: allocateCursor().
 71834  72376     */
 71835  72377     nMem += nCursor;
................................................................................
 71868  72410     */
 71869  72411     do {
 71870  72412       x.nNeeded = 0;
 71871  72413       p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
 71872  72414       p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
 71873  72415       p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
 71874  72416       p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
 71875         -    p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce);
 71876  72417   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 71877  72418       p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
 71878  72419   #endif
 71879  72420       if( x.nNeeded==0 ) break;
 71880  72421       x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded);
 71881  72422       x.nFree = x.nNeeded;
 71882  72423     }while( !db->mallocFailed );
 71883  72424   
 71884  72425     p->nCursor = nCursor;
 71885         -  p->nOnceFlag = nOnce;
 71886  72426     if( p->aVar ){
 71887  72427       p->nVar = (ynVar)nVar;
 71888  72428       for(n=0; n<nVar; n++){
 71889  72429         p->aVar[n].flags = MEM_Null;
 71890  72430         p->aVar[n].db = db;
 71891  72431       }
 71892  72432     }
................................................................................
 71966  72506   */
 71967  72507   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 71968  72508     Vdbe *v = pFrame->v;
 71969  72509     closeCursorsInFrame(v);
 71970  72510   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 71971  72511     v->anExec = pFrame->anExec;
 71972  72512   #endif
 71973         -  v->aOnceFlag = pFrame->aOnceFlag;
 71974         -  v->nOnceFlag = pFrame->nOnceFlag;
 71975  72513     v->aOp = pFrame->aOp;
 71976  72514     v->nOp = pFrame->nOp;
 71977  72515     v->aMem = pFrame->aMem;
 71978  72516     v->nMem = pFrame->nMem;
 71979  72517     v->apCsr = pFrame->apCsr;
 71980  72518     v->nCursor = pFrame->nCursor;
 71981  72519     v->db->lastRowid = pFrame->lastRowid;
................................................................................
 72508  73046     ** state.  We need to rollback the statement transaction, if there is
 72509  73047     ** one, or the complete transaction if there is no statement transaction.
 72510  73048     */
 72511  73049   
 72512  73050     if( db->mallocFailed ){
 72513  73051       p->rc = SQLITE_NOMEM_BKPT;
 72514  73052     }
 72515         -  if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
 72516  73053     closeAllCursors(p);
 72517  73054     if( p->magic!=VDBE_MAGIC_RUN ){
 72518  73055       return SQLITE_OK;
 72519  73056     }
 72520  73057     checkActiveVdbeCnt(db);
 72521  73058   
 72522  73059     /* No commit or rollback needed if the program never started or if the
................................................................................
 73651  74188       rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 73652  74189       if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
 73653  74190       sqlite3VdbeMemRelease(&c1);
 73654  74191       sqlite3VdbeMemRelease(&c2);
 73655  74192       return rc;
 73656  74193     }
 73657  74194   }
        74195  +
        74196  +/*
        74197  +** The input pBlob is guaranteed to be a Blob that is not marked
        74198  +** with MEM_Zero.  Return true if it could be a zero-blob.
        74199  +*/
        74200  +static int isZeroBlob(const Mem *pBlob){
        74201  +  int i;
        74202  +  for(i=0; i<pBlob->n && pBlob->z[i]==0; i++){}
        74203  +  return i==pBlob->n;
        74204  +}
 73658  74205   
 73659  74206   /*
 73660  74207   ** Compare two blobs.  Return negative, zero, or positive if the first
 73661  74208   ** is less than, equal to, or greater than the second, respectively.
 73662  74209   ** If one blob is a prefix of the other, then the shorter is the lessor.
 73663  74210   */
 73664  74211   static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
 73665         -  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
        74212  +  int c;
        74213  +  int n1 = pB1->n;
        74214  +  int n2 = pB2->n;
        74215  +
        74216  +  /* It is possible to have a Blob value that has some non-zero content
        74217  +  ** followed by zero content.  But that only comes up for Blobs formed
        74218  +  ** by the OP_MakeRecord opcode, and such Blobs never get passed into
        74219  +  ** sqlite3MemCompare(). */
        74220  +  assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
        74221  +  assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
        74222  +
        74223  +  if( (pB1->flags|pB2->flags) & MEM_Zero ){
        74224  +    if( pB1->flags & pB2->flags & MEM_Zero ){
        74225  +      return pB1->u.nZero - pB2->u.nZero;
        74226  +    }else if( pB1->flags & MEM_Zero ){
        74227  +      if( !isZeroBlob(pB2) ) return -1;
        74228  +      return pB1->u.nZero - n2;
        74229  +    }else{
        74230  +      if( !isZeroBlob(pB1) ) return +1;
        74231  +      return n1 - pB2->u.nZero;
        74232  +    }
        74233  +  }
        74234  +  c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
 73666  74235     if( c ) return c;
 73667         -  return pB1->n - pB2->n;
        74236  +  return n1 - n2;
 73668  74237   }
 73669  74238   
 73670  74239   /*
 73671  74240   ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
 73672  74241   ** number.  Return negative, zero, or positive if the first (i64) is less than,
 73673  74242   ** equal to, or greater than the second (double).
 73674  74243   */
................................................................................
 74047  74616     UnpackedRecord *pPKey2        /* Right key */
 74048  74617   ){
 74049  74618     const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
 74050  74619     int serial_type = ((const u8*)pKey1)[1];
 74051  74620     int res;
 74052  74621     u32 y;
 74053  74622     u64 x;
 74054         -  i64 v = pPKey2->aMem[0].u.i;
        74623  +  i64 v;
 74055  74624     i64 lhs;
 74056  74625   
 74057  74626     vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
 74058  74627     assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
 74059  74628     switch( serial_type ){
 74060  74629       case 1: { /* 1-byte signed integer */
 74061  74630         lhs = ONE_BYTE_INT(aKey);
................................................................................
 74106  74675       case 0: case 7:
 74107  74676         return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
 74108  74677   
 74109  74678       default:
 74110  74679         return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
 74111  74680     }
 74112  74681   
        74682  +  v = pPKey2->aMem[0].u.i;
 74113  74683     if( v>lhs ){
 74114  74684       res = pPKey2->r1;
 74115  74685     }else if( v<lhs ){
 74116  74686       res = pPKey2->r2;
 74117  74687     }else if( pPKey2->nField>1 ){
 74118  74688       /* The first fields of the two keys are equal. Compare the trailing 
 74119  74689       ** fields.  */
................................................................................
 74252  74822   
 74253  74823     /* Get the size of the index entry.  Only indices entries of less
 74254  74824     ** than 2GiB are support - anything large must be database corruption.
 74255  74825     ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
 74256  74826     ** this code can safely assume that nCellKey is 32-bits  
 74257  74827     */
 74258  74828     assert( sqlite3BtreeCursorIsValid(pCur) );
 74259         -  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 74260         -  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
        74829  +  nCellKey = sqlite3BtreePayloadSize(pCur);
 74261  74830     assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 74262  74831   
 74263  74832     /* Read in the complete content of the index entry */
 74264  74833     sqlite3VdbeMemInit(&m, db, 0);
 74265  74834     rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
 74266  74835     if( rc ){
 74267  74836       return rc;
................................................................................
 74330  74899     int rc;
 74331  74900     BtCursor *pCur;
 74332  74901     Mem m;
 74333  74902   
 74334  74903     assert( pC->eCurType==CURTYPE_BTREE );
 74335  74904     pCur = pC->uc.pCursor;
 74336  74905     assert( sqlite3BtreeCursorIsValid(pCur) );
 74337         -  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 74338         -  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
        74906  +  nCellKey = sqlite3BtreePayloadSize(pCur);
 74339  74907     /* nCellKey will always be between 0 and 0xffffffff because of the way
 74340  74908     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 74341  74909     if( nCellKey<=0 || nCellKey>0x7fffffff ){
 74342  74910       *res = 0;
 74343  74911       return SQLITE_CORRUPT_BKPT;
 74344  74912     }
 74345  74913     sqlite3VdbeMemInit(&m, db, 0);
................................................................................
 74593  75161   #ifndef SQLITE_OMIT_TRACE
 74594  75162   /*
 74595  75163   ** Invoke the profile callback.  This routine is only called if we already
 74596  75164   ** know that the profile callback is defined and needs to be invoked.
 74597  75165   */
 74598  75166   static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
 74599  75167     sqlite3_int64 iNow;
        75168  +  sqlite3_int64 iElapse;
 74600  75169     assert( p->startTime>0 );
 74601         -  assert( db->xProfile!=0 );
        75170  +  assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
 74602  75171     assert( db->init.busy==0 );
 74603  75172     assert( p->zSql!=0 );
 74604  75173     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 74605         -  db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
        75174  +  iElapse = (iNow - p->startTime)*1000000;
        75175  +  if( db->xProfile ){
        75176  +    db->xProfile(db->pProfileArg, p->zSql, iElapse);
        75177  +  }
        75178  +  if( db->mTrace & SQLITE_TRACE_PROFILE ){
        75179  +    db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
        75180  +  }
 74606  75181     p->startTime = 0;
 74607  75182   }
 74608  75183   /*
 74609  75184   ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
 74610  75185   ** is needed, and it invokes the callback if it is needed.
 74611  75186   */
 74612  75187   # define checkProfileCallback(DB,P) \
................................................................................
 75025  75600       Btree *pBt = db->aDb[i].pBt;
 75026  75601       if( pBt ){
 75027  75602         int nEntry;
 75028  75603         sqlite3BtreeEnter(pBt);
 75029  75604         nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 75030  75605         sqlite3BtreeLeave(pBt);
 75031  75606         if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
 75032         -        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
        75607  +        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
 75033  75608         }
 75034  75609       }
 75035  75610     }
 75036  75611   #endif
 75037  75612     return rc;
 75038  75613   }
 75039  75614   
................................................................................
 75102  75677       }
 75103  75678   
 75104  75679       assert( db->nVdbeWrite>0 || db->autoCommit==0 
 75105  75680           || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
 75106  75681       );
 75107  75682   
 75108  75683   #ifndef SQLITE_OMIT_TRACE
 75109         -    if( db->xProfile && !db->init.busy && p->zSql ){
        75684  +    if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
        75685  +        && !db->init.busy && p->zSql ){
 75110  75686         sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 75111  75687       }else{
 75112  75688         assert( p->startTime==0 );
 75113  75689       }
 75114  75690   #endif
 75115  75691   
 75116  75692       db->nVdbeActive++;
................................................................................
 76136  76712       return 0;
 76137  76713     }
 76138  76714   #endif
 76139  76715     v = pVdbe->aCounter[op];
 76140  76716     if( resetFlag ) pVdbe->aCounter[op] = 0;
 76141  76717     return (int)v;
 76142  76718   }
        76719  +
        76720  +/*
        76721  +** Return the SQL associated with a prepared statement
        76722  +*/
        76723  +SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
        76724  +  Vdbe *p = (Vdbe *)pStmt;
        76725  +  return p ? p->zSql : 0;
        76726  +}
        76727  +
        76728  +/*
        76729  +** Return the SQL associated with a prepared statement with
        76730  +** bound parameters expanded.  Space to hold the returned string is
        76731  +** obtained from sqlite3_malloc().  The caller is responsible for
        76732  +** freeing the returned string by passing it to sqlite3_free().
        76733  +**
        76734  +** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
        76735  +** expanded bound parameters.
        76736  +*/
        76737  +SQLITE_API char *SQLITE_STDCALL sqlite3_expanded_sql(sqlite3_stmt *pStmt){
        76738  +#ifdef SQLITE_OMIT_TRACE
        76739  +  return 0;
        76740  +#else
        76741  +  char *z = 0;
        76742  +  const char *zSql = sqlite3_sql(pStmt);
        76743  +  if( zSql ){
        76744  +    Vdbe *p = (Vdbe *)pStmt;
        76745  +    sqlite3_mutex_enter(p->db->mutex);
        76746  +    z = sqlite3VdbeExpandSql(p, zSql);
        76747  +    sqlite3_mutex_leave(p->db->mutex);
        76748  +  }
        76749  +  return z;
        76750  +#endif
        76751  +}
 76143  76752   
 76144  76753   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 76145  76754   /*
 76146  76755   ** Allocate and populate an UnpackedRecord structure based on the serialized
 76147  76756   ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
 76148  76757   ** if successful, or a NULL pointer if an OOM error is encountered.
 76149  76758   */
................................................................................
 76183  76792     }
 76184  76793   
 76185  76794     /* If the old.* record has not yet been loaded into memory, do so now. */
 76186  76795     if( p->pUnpacked==0 ){
 76187  76796       u32 nRec;
 76188  76797       u8 *aRec;
 76189  76798   
 76190         -    rc = sqlite3BtreeDataSize(p->pCsr->uc.pCursor, &nRec);
 76191         -    if( rc!=SQLITE_OK ) goto preupdate_old_out;
        76799  +    nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
 76192  76800       aRec = sqlite3DbMallocRaw(db, nRec);
 76193  76801       if( !aRec ) goto preupdate_old_out;
 76194  76802       rc = sqlite3BtreeData(p->pCsr->uc.pCursor, 0, nRec, aRec);
 76195  76803       if( rc==SQLITE_OK ){
 76196  76804         p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
 76197  76805         if( !p->pUnpacked ) rc = SQLITE_NOMEM;
 76198  76806       }
................................................................................
 76472  77080     int idx = 0;             /* Index of a host parameter */
 76473  77081     int nextIndex = 1;       /* Index of next ? host parameter */
 76474  77082     int n;                   /* Length of a token prefix */
 76475  77083     int nToken;              /* Length of the parameter token */
 76476  77084     int i;                   /* Loop counter */
 76477  77085     Mem *pVar;               /* Value of a host parameter */
 76478  77086     StrAccum out;            /* Accumulate the output here */
        77087  +#ifndef SQLITE_OMIT_UTF16
        77088  +  Mem utf8;                /* Used to convert UTF16 parameters into UTF8 for display */
        77089  +#endif
 76479  77090     char zBase[100];         /* Initial working space */
 76480  77091   
 76481  77092     db = p->db;
 76482         -  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), 
        77093  +  sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), 
 76483  77094                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 76484  77095     if( db->nVdbeExec>1 ){
 76485  77096       while( *zRawSql ){
 76486  77097         const char *zStart = zRawSql;
 76487  77098         while( *(zRawSql++)!='\n' && *zRawSql );
 76488  77099         sqlite3StrAccumAppend(&out, "-- ", 3);
 76489  77100         assert( (zRawSql - zStart) > 0 );
................................................................................
 76526  77137           sqlite3XPrintf(&out, "%lld", pVar->u.i);
 76527  77138         }else if( pVar->flags & MEM_Real ){
 76528  77139           sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
 76529  77140         }else if( pVar->flags & MEM_Str ){
 76530  77141           int nOut;  /* Number of bytes of the string text to include in output */
 76531  77142   #ifndef SQLITE_OMIT_UTF16
 76532  77143           u8 enc = ENC(db);
 76533         -        Mem utf8;
 76534  77144           if( enc!=SQLITE_UTF8 ){
 76535  77145             memset(&utf8, 0, sizeof(utf8));
 76536  77146             utf8.db = db;
 76537  77147             sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 76538         -          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
        77148  +          if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
        77149  +            out.accError = STRACCUM_NOMEM;
        77150  +            out.nAlloc = 0;
        77151  +          }
 76539  77152             pVar = &utf8;
 76540  77153           }
 76541  77154   #endif
 76542  77155           nOut = pVar->n;
 76543  77156   #ifdef SQLITE_TRACE_SIZE_LIMIT
 76544  77157           if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
 76545  77158             nOut = SQLITE_TRACE_SIZE_LIMIT;
................................................................................
 76573  77186           if( nOut<pVar->n ){
 76574  77187             sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
 76575  77188           }
 76576  77189   #endif
 76577  77190         }
 76578  77191       }
 76579  77192     }
        77193  +  if( out.accError ) sqlite3StrAccumReset(&out);
 76580  77194     return sqlite3StrAccumFinish(&out);
 76581  77195   }
 76582  77196   
 76583  77197   #endif /* #ifndef SQLITE_OMIT_TRACE */
 76584  77198   
 76585  77199   /************** End of vdbetrace.c *******************************************/
 76586  77200   /************** Begin file vdbe.c ********************************************/
................................................................................
 77105  77719   **    May you share freely, never taking more than you give.
 77106  77720   **
 77107  77721   ******************************************************************************
 77108  77722   **
 77109  77723   ** This file contains inline asm code for retrieving "high-performance"
 77110  77724   ** counters for x86 class CPUs.
 77111  77725   */
 77112         -#ifndef _HWTIME_H_
 77113         -#define _HWTIME_H_
        77726  +#ifndef SQLITE_HWTIME_H
        77727  +#define SQLITE_HWTIME_H
 77114  77728   
 77115  77729   /*
 77116  77730   ** The following routine only works on pentium-class (or newer) processors.
 77117  77731   ** It uses the RDTSC opcode to read the cycle count value out of the
 77118  77732   ** processor and returns that value.  This can be used for high-res
 77119  77733   ** profiling.
 77120  77734   */
................................................................................
 77174  77788     ** of the debugging and testing utilities, but it should at
 77175  77789     ** least compile and run.
 77176  77790     */
 77177  77791   SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 77178  77792   
 77179  77793   #endif
 77180  77794   
 77181         -#endif /* !defined(_HWTIME_H_) */
        77795  +#endif /* !defined(SQLITE_HWTIME_H) */
 77182  77796   
 77183  77797   /************** End of hwtime.h **********************************************/
 77184  77798   /************** Continuing where we left off in vdbe.c ***********************/
 77185  77799   
 77186  77800   #endif
 77187  77801   
 77188  77802   #ifndef NDEBUG
................................................................................
 77244  77858   #ifdef SQLITE_DEBUG
 77245  77859     int nExtraDelete = 0;      /* Verifies FORDELETE and AUXDELETE flags */
 77246  77860   #endif
 77247  77861     int rc = SQLITE_OK;        /* Value to return */
 77248  77862     sqlite3 *db = p->db;       /* The database */
 77249  77863     u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 77250  77864     u8 encoding = ENC(db);     /* The database encoding */
 77251         -  int iCompare = 0;          /* Result of last OP_Compare operation */
        77865  +  int iCompare = 0;          /* Result of last comparison */
 77252  77866     unsigned nVmStep = 0;      /* Number of virtual machine steps */
 77253  77867   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 77254  77868     unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
 77255  77869   #endif
 77256  77870     Mem *aMem = p->aMem;       /* Copy of p->aMem */
 77257  77871     Mem *pIn1 = 0;             /* 1st input operand */
 77258  77872     Mem *pIn2 = 0;             /* 2nd input operand */
................................................................................
 78053  78667       Deephemeralize(&pMem[i]);
 78054  78668       assert( (pMem[i].flags & MEM_Ephem)==0
 78055  78669               || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
 78056  78670       sqlite3VdbeMemNulTerminate(&pMem[i]);
 78057  78671       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
 78058  78672     }
 78059  78673     if( db->mallocFailed ) goto no_mem;
        78674  +
        78675  +  if( db->mTrace & SQLITE_TRACE_ROW ){
        78676  +    db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
        78677  +  }
 78060  78678   
 78061  78679     /* Return SQLITE_ROW
 78062  78680     */
 78063  78681     p->pc = (int)(pOp - aOp) + 1;
 78064  78682     rc = SQLITE_ROW;
 78065  78683     goto vdbe_return;
 78066  78684   }
................................................................................
 78547  79165     sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
 78548  79166     UPDATE_MAX_BLOBSIZE(pIn1);
 78549  79167     if( rc ) goto abort_due_to_error;
 78550  79168     break;
 78551  79169   }
 78552  79170   #endif /* SQLITE_OMIT_CAST */
 78553  79171   
        79172  +/* Opcode: Eq P1 P2 P3 P4 P5
        79173  +** Synopsis: IF r[P3]==r[P1]
        79174  +**
        79175  +** Compare the values in register P1 and P3.  If reg(P3)==reg(P1) then
        79176  +** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5, then
        79177  +** store the result of comparison in register P2.
        79178  +**
        79179  +** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
        79180  +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
        79181  +** to coerce both inputs according to this affinity before the
        79182  +** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
        79183  +** affinity is used. Note that the affinity conversions are stored
        79184  +** back into the input registers P1 and P3.  So this opcode can cause
        79185  +** persistent changes to registers P1 and P3.
        79186  +**
        79187  +** Once any conversions have taken place, and neither value is NULL, 
        79188  +** the values are compared. If both values are blobs then memcmp() is
        79189  +** used to determine the results of the comparison.  If both values
        79190  +** are text, then the appropriate collating function specified in
        79191  +** P4 is used to do the comparison.  If P4 is not specified then
        79192  +** memcmp() is used to compare text string.  If both values are
        79193  +** numeric, then a numeric comparison is used. If the two values
        79194  +** are of different types, then numbers are considered less than
        79195  +** strings and strings are considered less than blobs.
        79196  +**
        79197  +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
        79198  +** true or false and is never NULL.  If both operands are NULL then the result
        79199  +** of comparison is true.  If either operand is NULL then the result is false.
        79200  +** If neither operand is NULL the result is the same as it would be if
        79201  +** the SQLITE_NULLEQ flag were omitted from P5.
        79202  +**
        79203  +** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
        79204  +** content of r[P2] is only changed if the new value is NULL or 0 (false).
        79205  +** In other words, a prior r[P2] value will not be overwritten by 1 (true).
        79206  +*/
        79207  +/* Opcode: Ne P1 P2 P3 P4 P5
        79208  +** Synopsis: IF r[P3]!=r[P1]
        79209  +**
        79210  +** This works just like the Eq opcode except that the jump is taken if
        79211  +** the operands in registers P1 and P3 are not equal.  See the Eq opcode for
        79212  +** additional information.
        79213  +**
        79214  +** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
        79215  +** content of r[P2] is only changed if the new value is NULL or 1 (true).
        79216  +** In other words, a prior r[P2] value will not be overwritten by 0 (false).
        79217  +*/
 78554  79218   /* Opcode: Lt P1 P2 P3 P4 P5
 78555         -** Synopsis: if r[P1]<r[P3] goto P2
        79219  +** Synopsis: IF r[P3]<r[P1]
 78556  79220   **
 78557  79221   ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
 78558         -** jump to address P2.  
        79222  +** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5 store
        79223  +** the result of comparison (0 or 1 or NULL) into register P2.
 78559  79224   **
 78560  79225   ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
 78561         -** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
        79226  +** reg(P3) is NULL then the take the jump.  If the SQLITE_JUMPIFNULL 
 78562  79227   ** bit is clear then fall through if either operand is NULL.
 78563  79228   **
 78564  79229   ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
 78565  79230   ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
 78566  79231   ** to coerce both inputs according to this affinity before the
 78567  79232   ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
 78568  79233   ** affinity is used. Note that the affinity conversions are stored
................................................................................
 78574  79239   ** used to determine the results of the comparison.  If both values
 78575  79240   ** are text, then the appropriate collating function specified in
 78576  79241   ** P4 is  used to do the comparison.  If P4 is not specified then
 78577  79242   ** memcmp() is used to compare text string.  If both values are
 78578  79243   ** numeric, then a numeric comparison is used. If the two values
 78579  79244   ** are of different types, then numbers are considered less than
 78580  79245   ** strings and strings are considered less than blobs.
 78581         -**
 78582         -** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
 78583         -** store a boolean result (either 0, or 1, or NULL) in register P2.
 78584         -**
 78585         -** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
 78586         -** equal to one another, provided that they do not have their MEM_Cleared
 78587         -** bit set.
 78588         -*/
 78589         -/* Opcode: Ne P1 P2 P3 P4 P5
 78590         -** Synopsis: if r[P1]!=r[P3] goto P2
 78591         -**
 78592         -** This works just like the Lt opcode except that the jump is taken if
 78593         -** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 78594         -** additional information.
 78595         -**
 78596         -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 78597         -** true or false and is never NULL.  If both operands are NULL then the result
 78598         -** of comparison is false.  If either operand is NULL then the result is true.
 78599         -** If neither operand is NULL the result is the same as it would be if
 78600         -** the SQLITE_NULLEQ flag were omitted from P5.
 78601         -*/
 78602         -/* Opcode: Eq P1 P2 P3 P4 P5
 78603         -** Synopsis: if r[P1]==r[P3] goto P2
 78604         -**
 78605         -** This works just like the Lt opcode except that the jump is taken if
 78606         -** the operands in registers P1 and P3 are equal.
 78607         -** See the Lt opcode for additional information.
 78608         -**
 78609         -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 78610         -** true or false and is never NULL.  If both operands are NULL then the result
 78611         -** of comparison is true.  If either operand is NULL then the result is false.
 78612         -** If neither operand is NULL the result is the same as it would be if
 78613         -** the SQLITE_NULLEQ flag were omitted from P5.
 78614  79246   */
 78615  79247   /* Opcode: Le P1 P2 P3 P4 P5
 78616         -** Synopsis: if r[P1]<=r[P3] goto P2
        79248  +** Synopsis: IF r[P3]<=r[P1]
 78617  79249   **
 78618  79250   ** This works just like the Lt opcode except that the jump is taken if
 78619  79251   ** the content of register P3 is less than or equal to the content of
 78620  79252   ** register P1.  See the Lt opcode for additional information.
 78621  79253   */
 78622  79254   /* Opcode: Gt P1 P2 P3 P4 P5
 78623         -** Synopsis: if r[P1]>r[P3] goto P2
        79255  +** Synopsis: IF r[P3]>r[P1]
 78624  79256   **
 78625  79257   ** This works just like the Lt opcode except that the jump is taken if
 78626  79258   ** the content of register P3 is greater than the content of
 78627  79259   ** register P1.  See the Lt opcode for additional information.
 78628  79260   */
 78629  79261   /* Opcode: Ge P1 P2 P3 P4 P5
 78630         -** Synopsis: if r[P1]>=r[P3] goto P2
        79262  +** Synopsis: IF r[P3]>=r[P1]
 78631  79263   **
 78632  79264   ** This works just like the Lt opcode except that the jump is taken if
 78633  79265   ** the content of register P3 is greater than or equal to the content of
 78634  79266   ** register P1.  See the Lt opcode for additional information.
 78635  79267   */
 78636  79268   case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 78637  79269   case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 78638  79270   case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 78639  79271   case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 78640  79272   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 78641  79273   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 78642         -  int res;            /* Result of the comparison of pIn1 against pIn3 */
        79274  +  int res, res2;      /* Result of the comparison of pIn1 against pIn3 */
 78643  79275     char affinity;      /* Affinity to use for comparison */
 78644  79276     u16 flags1;         /* Copy of initial value of pIn1->flags */
 78645  79277     u16 flags3;         /* Copy of initial value of pIn3->flags */
 78646  79278   
 78647  79279     pIn1 = &aMem[pOp->p1];
 78648  79280     pIn3 = &aMem[pOp->p3];
 78649  79281     flags1 = pIn1->flags;
................................................................................
 78658  79290         assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 78659  79291         assert( (flags1 & MEM_Cleared)==0 );
 78660  79292         assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
 78661  79293         if( (flags1&MEM_Null)!=0
 78662  79294          && (flags3&MEM_Null)!=0
 78663  79295          && (flags3&MEM_Cleared)==0
 78664  79296         ){
 78665         -        res = 0;  /* Results are equal */
        79297  +        res = 0;  /* Operands are equal */
 78666  79298         }else{
 78667         -        res = 1;  /* Results are not equal */
        79299  +        res = 1;  /* Operands are not equal */
 78668  79300         }
 78669  79301       }else{
 78670  79302         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 78671  79303         ** then the result is always NULL.
 78672  79304         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 78673  79305         */
 78674  79306         if( pOp->p5 & SQLITE_STOREP2 ){
 78675  79307           pOut = &aMem[pOp->p2];
        79308  +        iCompare = 1;    /* Operands are not equal */
 78676  79309           memAboutToChange(p, pOut);
 78677  79310           MemSetTypeFlag(pOut, MEM_Null);
 78678  79311           REGISTER_TRACE(pOp->p2, pOut);
 78679  79312         }else{
 78680  79313           VdbeBranchTaken(2,3);
 78681  79314           if( pOp->p5 & SQLITE_JUMPIFNULL ){
 78682  79315             goto jump_to_p2;
................................................................................
 78687  79320     }else{
 78688  79321       /* Neither operand is NULL.  Do a comparison. */
 78689  79322       affinity = pOp->p5 & SQLITE_AFF_MASK;
 78690  79323       if( affinity>=SQLITE_AFF_NUMERIC ){
 78691  79324         if( (flags1 | flags3)&MEM_Str ){
 78692  79325           if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
 78693  79326             applyNumericAffinity(pIn1,0);
        79327  +          testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
        79328  +          flags3 = pIn3->flags;
 78694  79329           }
 78695  79330           if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
 78696  79331             applyNumericAffinity(pIn3,0);
 78697  79332           }
        79333  +      }
        79334  +      /* Handle the common case of integer comparison here, as an
        79335  +      ** optimization, to avoid a call to sqlite3MemCompare() */
        79336  +      if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
        79337  +        if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
        79338  +        if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
        79339  +        res = 0;
        79340  +        goto compare_op;
 78698  79341         }
 78699  79342       }else if( affinity==SQLITE_AFF_TEXT ){
 78700  79343         if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
 78701  79344           testcase( pIn1->flags & MEM_Int );
 78702  79345           testcase( pIn1->flags & MEM_Real );
 78703  79346           sqlite3VdbeMemStringify(pIn1, encoding, 1);
 78704  79347           testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
 78705  79348           flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
        79349  +        assert( pIn1!=pIn3 );
 78706  79350         }
 78707  79351         if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
 78708  79352           testcase( pIn3->flags & MEM_Int );
 78709  79353           testcase( pIn3->flags & MEM_Real );
 78710  79354           sqlite3VdbeMemStringify(pIn3, encoding, 1);
 78711  79355           testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
 78712  79356           flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
 78713  79357         }
 78714  79358       }
 78715  79359       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 78716         -    if( flags1 & MEM_Zero ){
 78717         -      sqlite3VdbeMemExpandBlob(pIn1);
 78718         -      flags1 &= ~MEM_Zero;
 78719         -    }
 78720         -    if( flags3 & MEM_Zero ){
 78721         -      sqlite3VdbeMemExpandBlob(pIn3);
 78722         -      flags3 &= ~MEM_Zero;
 78723         -    }
 78724  79360       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 78725  79361     }
        79362  +compare_op:
 78726  79363     switch( pOp->opcode ){
 78727         -    case OP_Eq:    res = res==0;     break;
 78728         -    case OP_Ne:    res = res!=0;     break;
 78729         -    case OP_Lt:    res = res<0;      break;
 78730         -    case OP_Le:    res = res<=0;     break;
 78731         -    case OP_Gt:    res = res>0;      break;
 78732         -    default:       res = res>=0;     break;
        79364  +    case OP_Eq:    res2 = res==0;     break;
        79365  +    case OP_Ne:    res2 = res;        break;
        79366  +    case OP_Lt:    res2 = res<0;      break;
        79367  +    case OP_Le:    res2 = res<=0;     break;
        79368  +    case OP_Gt:    res2 = res>0;      break;
        79369  +    default:       res2 = res>=0;     break;
 78733  79370     }
 78734  79371   
 78735  79372     /* Undo any changes made by applyAffinity() to the input registers. */
 78736  79373     assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
 78737  79374     pIn1->flags = flags1;
 78738  79375     assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
 78739  79376     pIn3->flags = flags3;
 78740  79377   
 78741  79378     if( pOp->p5 & SQLITE_STOREP2 ){
 78742  79379       pOut = &aMem[pOp->p2];
        79380  +    iCompare = res;
        79381  +    res2 = res2!=0;  /* For this path res2 must be exactly 0 or 1 */
        79382  +    if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
        79383  +      /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
        79384  +      ** and prevents OP_Ne from overwriting NULL with 0.  This flag
        79385  +      ** is only used in contexts where either:
        79386  +      **   (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
        79387  +      **   (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
        79388  +      ** Therefore it is not necessary to check the content of r[P2] for
        79389  +      ** NULL. */
        79390  +      assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
        79391  +      assert( res2==0 || res2==1 );
        79392  +      testcase( res2==0 && pOp->opcode==OP_Eq );
        79393  +      testcase( res2==1 && pOp->opcode==OP_Eq );
        79394  +      testcase( res2==0 && pOp->opcode==OP_Ne );
        79395  +      testcase( res2==1 && pOp->opcode==OP_Ne );
        79396  +      if( (pOp->opcode==OP_Eq)==res2 ) break;
        79397  +    }
 78743  79398       memAboutToChange(p, pOut);
 78744  79399       MemSetTypeFlag(pOut, MEM_Int);
 78745         -    pOut->u.i = res;
        79400  +    pOut->u.i = res2;
 78746  79401       REGISTER_TRACE(pOp->p2, pOut);
 78747  79402     }else{
 78748  79403       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 78749         -    if( res ){
        79404  +    if( res2 ){
 78750  79405         goto jump_to_p2;
 78751  79406       }
 78752  79407     }
 78753  79408     break;
 78754  79409   }
        79410  +
        79411  +/* Opcode: ElseNotEq * P2 * * *
        79412  +**
        79413  +** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
        79414  +** If result of an OP_Eq comparison on the same two operands
        79415  +** would have be NULL or false (0), then then jump to P2. 
        79416  +** If the result of an OP_Eq comparison on the two previous operands
        79417  +** would have been true (1), then fall through.
        79418  +*/
        79419  +case OP_ElseNotEq: {       /* same as TK_ESCAPE, jump */
        79420  +  assert( pOp>aOp );
        79421  +  assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
        79422  +  assert( pOp[-1].p5 & SQLITE_STOREP2 );
        79423  +  VdbeBranchTaken(iCompare!=0, 2);
        79424  +  if( iCompare!=0 ) goto jump_to_p2;
        79425  +  break;
        79426  +}
        79427  +
 78755  79428   
 78756  79429   /* Opcode: Permutation * * * P4 *
 78757  79430   **
 78758  79431   ** Set the permutation used by the OP_Compare operator to be the array
 78759  79432   ** of integers in P4.
 78760  79433   **
 78761  79434   ** The permutation is only valid until the next OP_Compare that has
................................................................................
 78944  79617       pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
 78945  79618     }
 78946  79619     break;
 78947  79620   }
 78948  79621   
 78949  79622   /* Opcode: Once P1 P2 * * *
 78950  79623   **
 78951         -** Check the "once" flag number P1. If it is set, jump to instruction P2. 
 78952         -** Otherwise, set the flag and fall through to the next instruction.
 78953         -** In other words, this opcode causes all following opcodes up through P2
 78954         -** (but not including P2) to run just once and to be skipped on subsequent
 78955         -** times through the loop.
 78956         -**
 78957         -** All "once" flags are initially cleared whenever a prepared statement
 78958         -** first begins to run.
        79624  +** If the P1 value is equal to the P1 value on the OP_Init opcode at
        79625  +** instruction 0, then jump to P2.  If the two P1 values differ, then
        79626  +** set the P1 value on this opcode to equal the P1 value on the OP_Init
        79627  +** and fall through.
 78959  79628   */
 78960  79629   case OP_Once: {             /* jump */
 78961         -  assert( pOp->p1<p->nOnceFlag );
 78962         -  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
 78963         -  if( p->aOnceFlag[pOp->p1] ){
        79630  +  assert( p->aOp[0].opcode==OP_Init );
        79631  +  VdbeBranchTaken(p->aOp[0].p1==pOp->p1, 2);
        79632  +  if( p->aOp[0].p1==pOp->p1 ){
 78964  79633       goto jump_to_p2;
 78965  79634     }else{
 78966         -    p->aOnceFlag[pOp->p1] = 1;
        79635  +    pOp->p1 = p->aOp[0].p1;
 78967  79636     }
 78968  79637     break;
 78969  79638   }
 78970  79639   
 78971  79640   /* Opcode: If P1 P2 P3 * *
 78972  79641   **
 78973  79642   ** Jump to P2 if the value in register P1 is true.  The value
................................................................................
 79051  79720   **
 79052  79721   ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
 79053  79722   ** the result is guaranteed to only be used as the argument of a length()
 79054  79723   ** or typeof() function, respectively.  The loading of large blobs can be
 79055  79724   ** skipped for length() and all content loading can be skipped for typeof().
 79056  79725   */
 79057  79726   case OP_Column: {
 79058         -  i64 payloadSize64; /* Number of bytes in the record */
 79059  79727     int p2;            /* column number to retrieve */
 79060  79728     VdbeCursor *pC;    /* The VDBE cursor */
 79061  79729     BtCursor *pCrsr;   /* The BTree cursor */
 79062  79730     u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 79063  79731     int len;           /* The length of the serialized data for the column */
 79064  79732     int i;             /* Loop counter */
 79065  79733     Mem *pDest;        /* Where to write the extracted value */
................................................................................
 79074  79742     Mem *pReg;         /* PseudoTable input register */
 79075  79743   
 79076  79744     pC = p->apCsr[pOp->p1];
 79077  79745     p2 = pOp->p2;
 79078  79746   
 79079  79747     /* If the cursor cache is stale, bring it up-to-date */
 79080  79748     rc = sqlite3VdbeCursorMoveto(&pC, &p2);
        79749  +  if( rc ) goto abort_due_to_error;
 79081  79750   
 79082  79751     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
 79083  79752     pDest = &aMem[pOp->p3];
 79084  79753     memAboutToChange(p, pDest);
 79085  79754     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 79086  79755     assert( pC!=0 );
 79087  79756     assert( p2<pC->nField );
 79088  79757     aOffset = pC->aOffset;
 79089  79758     assert( pC->eCurType!=CURTYPE_VTAB );
 79090  79759     assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
 79091  79760     assert( pC->eCurType!=CURTYPE_SORTER );
 79092  79761     pCrsr = pC->uc.pCursor;
 79093  79762   
 79094         -  if( rc ) goto abort_due_to_error;
 79095         -  if( pC->cacheStatus!=p->cacheCtr ){
        79763  +  if( pC->cacheStatus!=p->cacheCtr ){                /*OPTIMIZATION-IF-FALSE*/
 79096  79764       if( pC->nullRow ){
 79097  79765         if( pC->eCurType==CURTYPE_PSEUDO ){
 79098  79766           assert( pC->uc.pseudoTableReg>0 );
 79099  79767           pReg = &aMem[pC->uc.pseudoTableReg];
 79100  79768           assert( pReg->flags & MEM_Blob );
 79101  79769           assert( memIsValid(pReg) );
 79102  79770           pC->payloadSize = pC->szRow = avail = pReg->n;
................................................................................
 79104  79772         }else{
 79105  79773           sqlite3VdbeMemSetNull(pDest);
 79106  79774           goto op_column_out;
 79107  79775         }
 79108  79776       }else{
 79109  79777         assert( pC->eCurType==CURTYPE_BTREE );
 79110  79778         assert( pCrsr );
 79111         -      if( pC->isTable==0 ){
 79112  79779           assert( sqlite3BtreeCursorIsValid(pCrsr) );
 79113         -        VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
 79114         -        assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
 79115         -        /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
 79116         -        ** payload size, so it is impossible for payloadSize64 to be
 79117         -        ** larger than 32 bits. */
 79118         -        assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
 79119         -        pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
 79120         -        pC->payloadSize = (u32)payloadSize64;
 79121         -      }else{
 79122         -        assert( sqlite3BtreeCursorIsValid(pCrsr) );
 79123         -        VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
 79124         -        assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
 79125         -        pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
 79126         -      }
        79780  +      pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
        79781  +      pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
 79127  79782         assert( avail<=65536 );  /* Maximum page size is 64KiB */
 79128  79783         if( pC->payloadSize <= (u32)avail ){
 79129  79784           pC->szRow = pC->payloadSize;
 79130  79785         }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 79131  79786           goto too_big;
 79132  79787         }else{
 79133  79788           pC->szRow = avail;
................................................................................
 79135  79790       }
 79136  79791       pC->cacheStatus = p->cacheCtr;
 79137  79792       pC->iHdrOffset = getVarint32(pC->aRow, offset);
 79138  79793       pC->nHdrParsed = 0;
 79139  79794       aOffset[0] = offset;
 79140  79795   
 79141  79796   
 79142         -    if( avail<offset ){
        79797  +    if( avail<offset ){      /*OPTIMIZATION-IF-FALSE*/
 79143  79798         /* pC->aRow does not have to hold the entire row, but it does at least
 79144  79799         ** need to cover the header of the record.  If pC->aRow does not contain
 79145  79800         ** the complete header, then set it to zero, forcing the header to be
 79146  79801         ** dynamically allocated. */
 79147  79802         pC->aRow = 0;
 79148  79803         pC->szRow = 0;
 79149  79804   
................................................................................
 79156  79811         ** 3-byte type for each of the maximum of 32768 columns plus three
 79157  79812         ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
 79158  79813         */
 79159  79814         if( offset > 98307 || offset > pC->payloadSize ){
 79160  79815           rc = SQLITE_CORRUPT_BKPT;
 79161  79816           goto abort_due_to_error;
 79162  79817         }
 79163         -    }
 79164         -
        79818  +    }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
 79165  79819       /* The following goto is an optimization.  It can be omitted and
 79166  79820       ** everything will still work.  But OP_Column is measurably faster
 79167  79821       ** by skipping the subsequent conditional, which is always true.
 79168  79822       */
        79823  +      zData = pC->aRow;
 79169  79824       assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
 79170  79825       goto op_column_read_header;
 79171  79826     }
        79827  +  }
 79172  79828   
 79173  79829     /* Make sure at least the first p2+1 entries of the header have been
 79174  79830     ** parsed and valid information is in aOffset[] and pC->aType[].
 79175  79831     */
 79176  79832     if( pC->nHdrParsed<=p2 ){
 79177  79833       /* If there is more header available for parsing in the record, try
 79178  79834       ** to extract additional fields up through the p2+1-th field 
 79179  79835       */
 79180         -    op_column_read_header:
 79181  79836       if( pC->iHdrOffset<aOffset[0] ){
 79182  79837         /* Make sure zData points to enough of the record to cover the header. */
 79183  79838         if( pC->aRow==0 ){
 79184  79839           memset(&sMem, 0, sizeof(sMem));
 79185  79840           rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem);
 79186  79841           if( rc!=SQLITE_OK ) goto abort_due_to_error;
 79187  79842           zData = (u8*)sMem.z;
 79188  79843         }else{
 79189  79844           zData = pC->aRow;
 79190  79845         }
 79191  79846     
 79192  79847         /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
        79848  +    op_column_read_header:
 79193  79849         i = pC->nHdrParsed;
 79194  79850         offset64 = aOffset[i];
 79195  79851         zHdr = zData + pC->iHdrOffset;
 79196  79852         zEndHdr = zData + aOffset[0];
 79197         -      assert( i<=p2 && zHdr<zEndHdr );
 79198  79853         do{
 79199  79854           if( (t = zHdr[0])<0x80 ){
 79200  79855             zHdr++;
 79201  79856             offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
 79202  79857           }else{
 79203  79858             zHdr += sqlite3GetVarint32(zHdr, &t);
 79204  79859             offset64 += sqlite3VdbeSerialTypeLen(t);
 79205  79860           }
 79206  79861           pC->aType[i++] = t;
 79207  79862           aOffset[i] = (u32)(offset64 & 0xffffffff);
 79208  79863         }while( i<=p2 && zHdr<zEndHdr );
 79209         -      pC->nHdrParsed = i;
 79210         -      pC->iHdrOffset = (u32)(zHdr - zData);
 79211  79864     
 79212  79865         /* The record is corrupt if any of the following are true:
 79213  79866         ** (1) the bytes of the header extend past the declared header size
 79214  79867         ** (2) the entire header was used but not all data was used
 79215  79868         ** (3) the end of the data extends beyond the end of the record.
 79216  79869         */
 79217  79870         if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
 79218  79871          || (offset64 > pC->payloadSize)
 79219  79872         ){
 79220  79873           if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
 79221  79874           rc = SQLITE_CORRUPT_BKPT;
 79222  79875           goto abort_due_to_error;
 79223  79876         }
        79877  +
        79878  +      pC->nHdrParsed = i;
        79879  +      pC->iHdrOffset = (u32)(zHdr - zData);
 79224  79880         if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
 79225         -
 79226  79881       }else{
 79227  79882         t = 0;
 79228  79883       }
 79229  79884   
 79230  79885       /* If after trying to extract new entries from the header, nHdrParsed is
 79231  79886       ** still not up to p2, that means that the record has fewer than p2
 79232  79887       ** columns.  So the result will be either the default value or a NULL.
................................................................................
 79246  79901     /* Extract the content for the p2+1-th column.  Control can only
 79247  79902     ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
 79248  79903     ** all valid.
 79249  79904     */
 79250  79905     assert( p2<pC->nHdrParsed );
 79251  79906     assert( rc==SQLITE_OK );
 79252  79907     assert( sqlite3VdbeCheckMemInvariants(pDest) );
 79253         -  if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
        79908  +  if( VdbeMemDynamic(pDest) ){
        79909  +    sqlite3VdbeMemSetNull(pDest);
        79910  +  }
 79254  79911     assert( t==pC->aType[p2] );
 79255         -  pDest->enc = encoding;
 79256  79912     if( pC->szRow>=aOffset[p2+1] ){
 79257  79913       /* This is the common case where the desired content fits on the original
 79258  79914       ** page - where the content is not on an overflow page */
 79259  79915       zData = pC->aRow + aOffset[p2];
 79260  79916       if( t<12 ){
 79261  79917         sqlite3VdbeSerialGet(zData, t, pDest);
 79262  79918       }else{
 79263  79919         /* If the column value is a string, we need a persistent value, not
 79264  79920         ** a MEM_Ephem value.  This branch is a fast short-cut that is equivalent
 79265  79921         ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
 79266  79922         */
 79267  79923         static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
 79268  79924         pDest->n = len = (t-12)/2;
        79925  +      pDest->enc = encoding;
 79269  79926         if( pDest->szMalloc < len+2 ){
 79270  79927           pDest->flags = MEM_Null;
 79271  79928           if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
 79272  79929         }else{
 79273  79930           pDest->z = pDest->zMalloc;
 79274  79931         }
 79275  79932         memcpy(pDest->z, zData, len);
 79276  79933         pDest->z[len] = 0;
 79277  79934         pDest->z[len+1] = 0;
 79278  79935         pDest->flags = aFlag[t&1];
 79279  79936       }
 79280  79937     }else{
        79938  +    pDest->enc = encoding;
 79281  79939       /* This branch happens only when content is on overflow pages */
 79282  79940       if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
 79283  79941             && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
 79284  79942        || (len = sqlite3VdbeSerialTypeLen(t))==0
 79285  79943       ){
 79286  79944         /* Content is irrelevant for
 79287  79945         **    1. the typeof() function,
................................................................................
 80499  81157       assert( oc!=OP_SeekGE || r.default_rc==+1 );
 80500  81158       assert( oc!=OP_SeekLT || r.default_rc==+1 );
 80501  81159   
 80502  81160       r.aMem = &aMem[pOp->p3];
 80503  81161   #ifdef SQLITE_DEBUG
 80504  81162       { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 80505  81163   #endif
 80506         -    ExpandBlob(r.aMem);
        81164  +    (void)ExpandBlob(r.aMem);
 80507  81165       r.eqSeen = 0;
 80508  81166       rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
 80509  81167       if( rc!=SQLITE_OK ){
 80510  81168         goto abort_due_to_error;
 80511  81169       }
 80512  81170       if( eqOnly && r.eqSeen==0 ){
 80513  81171         assert( res!=0 );
................................................................................
 80548  81206     }else if( eqOnly ){
 80549  81207       assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
 80550  81208       pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
 80551  81209     }
 80552  81210     break;
 80553  81211   }
 80554  81212     
 80555         -
 80556  81213   /* Opcode: Found P1 P2 P3 P4 *
 80557  81214   ** Synopsis: key=r[P3@P4]
 80558  81215   **
 80559  81216   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 80560  81217   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 80561  81218   ** record.
 80562  81219   **
................................................................................
 80643  81300     pFree = 0;
 80644  81301     if( pOp->p4.i>0 ){
 80645  81302       r.pKeyInfo = pC->pKeyInfo;
 80646  81303       r.nField = (u16)pOp->p4.i;
 80647  81304       r.aMem = pIn3;
 80648  81305       for(ii=0; ii<r.nField; ii++){
 80649  81306         assert( memIsValid(&r.aMem[ii]) );
 80650         -      ExpandBlob(&r.aMem[ii]);
        81307  +      (void)ExpandBlob(&r.aMem[ii]);
 80651  81308   #ifdef SQLITE_DEBUG
 80652  81309         if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
 80653  81310   #endif
 80654  81311       }
 80655  81312       pIdxKey = &r;
 80656  81313     }else{
 80657  81314       pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 80658  81315           pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
 80659  81316       );
 80660  81317       if( pIdxKey==0 ) goto no_mem;
 80661  81318       assert( pIn3->flags & MEM_Blob );
 80662         -    ExpandBlob(pIn3);
        81319  +    (void)ExpandBlob(pIn3);
 80663  81320       sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 80664  81321     }
 80665  81322     pIdxKey->default_rc = 0;
 80666  81323     takeJump = 0;
 80667  81324     if( pOp->opcode==OP_NoConflict ){
 80668  81325       /* For the OP_NoConflict opcode, take the jump if any of the
 80669  81326       ** input fields are NULL, since any key with a NULL will not
................................................................................
 80691  81348     }else{
 80692  81349       VdbeBranchTaken(takeJump||alreadyExists==0,2);
 80693  81350       if( takeJump || !alreadyExists ) goto jump_to_p2;
 80694  81351     }
 80695  81352     break;
 80696  81353   }
 80697  81354   
        81355  +/* Opcode: SeekRowid P1 P2 P3 * *
        81356  +** Synopsis: intkey=r[P3]
        81357  +**
        81358  +** P1 is the index of a cursor open on an SQL table btree (with integer
        81359  +** keys).  If register P3 does not contain an integer or if P1 does not
        81360  +** contain a record with rowid P3 then jump immediately to P2.  
        81361  +** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
        81362  +** a record with rowid P3 then 
        81363  +** leave the cursor pointing at that record and fall through to the next
        81364  +** instruction.
        81365  +**
        81366  +** The OP_NotExists opcode performs the same operation, but with OP_NotExists
        81367  +** the P3 register must be guaranteed to contain an integer value.  With this
        81368  +** opcode, register P3 might not contain an integer.
        81369  +**
        81370  +** The OP_NotFound opcode performs the same operation on index btrees
        81371  +** (with arbitrary multi-value keys).
        81372  +**
        81373  +** This opcode leaves the cursor in a state where it cannot be advanced
        81374  +** in either direction.  In other words, the Next and Prev opcodes will
        81375  +** not work following this opcode.
        81376  +**
        81377  +** See also: Found, NotFound, NoConflict, SeekRowid
        81378  +*/
 80698  81379   /* Opcode: NotExists P1 P2 P3 * *
 80699  81380   ** Synopsis: intkey=r[P3]
 80700  81381   **
 80701  81382   ** P1 is the index of a cursor open on an SQL table btree (with integer
 80702  81383   ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
 80703  81384   ** rowid P3 then jump immediately to P2.  Or, if P2 is 0, raise an
 80704  81385   ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 
 80705  81386   ** leave the cursor pointing at that record and fall through to the next
 80706  81387   ** instruction.
 80707  81388   **
        81389  +** The OP_SeekRowid opcode performs the same operation but also allows the
        81390  +** P3 register to contain a non-integer value, in which case the jump is
        81391  +** always taken.  This opcode requires that P3 always contain an integer.
        81392  +**
 80708  81393   ** The OP_NotFound opcode performs the same operation on index btrees
 80709  81394   ** (with arbitrary multi-value keys).
 80710  81395   **
 80711  81396   ** This opcode leaves the cursor in a state where it cannot be advanced
 80712  81397   ** in either direction.  In other words, the Next and Prev opcodes will
 80713  81398   ** not work following this opcode.
 80714  81399   **
 80715         -** See also: Found, NotFound, NoConflict
        81400  +** See also: Found, NotFound, NoConflict, SeekRowid
 80716  81401   */
 80717         -case OP_NotExists: {        /* jump, in3 */
        81402  +case OP_SeekRowid: {        /* jump, in3 */
 80718  81403     VdbeCursor *pC;
 80719  81404     BtCursor *pCrsr;
 80720  81405     int res;
 80721  81406     u64 iKey;
 80722  81407   
 80723  81408     pIn3 = &aMem[pOp->p3];
        81409  +  if( (pIn3->flags & MEM_Int)==0 ){
        81410  +    applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
        81411  +    if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
        81412  +  }
        81413  +  /* Fall through into OP_NotExists */
        81414  +case OP_NotExists:          /* jump, in3 */
        81415  +  pIn3 = &aMem[pOp->p3];
 80724  81416     assert( pIn3->flags & MEM_Int );
 80725  81417     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 80726  81418     pC = p->apCsr[pOp->p1];
 80727  81419     assert( pC!=0 );
 80728  81420   #ifdef SQLITE_DEBUG
 80729  81421     pC->seekOp = 0;
 80730  81422   #endif
................................................................................
 80834  81526         if( rc!=SQLITE_OK ){
 80835  81527           goto abort_due_to_error;
 80836  81528         }
 80837  81529         if( res ){
 80838  81530           v = 1;   /* IMP: R-61914-48074 */
 80839  81531         }else{
 80840  81532           assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
 80841         -        rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
 80842         -        assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
        81533  +        v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
 80843  81534           if( v>=MAX_ROWID ){
 80844  81535             pC->useRandomRowid = 1;
 80845  81536           }else{
 80846  81537             v++;   /* IMP: R-29538-34987 */
 80847  81538           }
 80848  81539         }
 80849  81540       }
................................................................................
 80918  81609   **
 80919  81610   ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
 80920  81611   ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
 80921  81612   ** then rowid is stored for subsequent return by the
 80922  81613   ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
 80923  81614   **
 80924  81615   ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
 80925         -** the last seek operation (OP_NotExists) was a success, then this
        81616  +** the last seek operation (OP_NotExists or OP_SeekRowid) was a success,
        81617  +** then this
 80926  81618   ** operation will not attempt to find the appropriate row before doing
 80927  81619   ** the insert but will instead overwrite the row that the cursor is
 80928         -** currently pointing to.  Presumably, the prior OP_NotExists opcode
        81620  +** currently pointing to.  Presumably, the prior OP_NotExists or
        81621  +** OP_SeekRowid opcode
 80929  81622   ** has already positioned the cursor correctly.  This is an optimization
 80930  81623   ** that boosts performance by avoiding redundant seeks.
 80931  81624   **
 80932  81625   ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
 80933  81626   ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
 80934  81627   ** is part of an INSERT operation.  The difference is only important to
 80935  81628   ** the update hook.
................................................................................
 80953  81646   ** This works exactly like OP_Insert except that the key is the
 80954  81647   ** integer value P3, not the value of the integer stored in register P3.
 80955  81648   */
 80956  81649   case OP_Insert: 
 80957  81650   case OP_InsertInt: {
 80958  81651     Mem *pData;       /* MEM cell holding data for the record to be inserted */
 80959  81652     Mem *pKey;        /* MEM cell holding key  for the record */
 80960         -  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 80961  81653     VdbeCursor *pC;   /* Cursor to table into which insert is written */
 80962         -  int nZero;        /* Number of zero-bytes to append */
 80963  81654     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 80964  81655     const char *zDb;  /* database name - used by the update hook */
 80965  81656     Table *pTab;      /* Table structure - used by update and pre-update hooks */
 80966  81657     int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
        81658  +  BtreePayload x;   /* Payload to be inserted */
 80967  81659   
 80968  81660     op = 0;
 80969  81661     pData = &aMem[pOp->p2];
 80970  81662     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 80971  81663     assert( memIsValid(pData) );
 80972  81664     pC = p->apCsr[pOp->p1];
 80973  81665     assert( pC!=0 );
................................................................................
 80978  81670     REGISTER_TRACE(pOp->p2, pData);
 80979  81671   
 80980  81672     if( pOp->opcode==OP_Insert ){
 80981  81673       pKey = &aMem[pOp->p3];
 80982  81674       assert( pKey->flags & MEM_Int );
 80983  81675       assert( memIsValid(pKey) );
 80984  81676       REGISTER_TRACE(pOp->p3, pKey);
 80985         -    iKey = pKey->u.i;
        81677  +    x.nKey = pKey->u.i;
 80986  81678     }else{
 80987  81679       assert( pOp->opcode==OP_InsertInt );
 80988         -    iKey = pOp->p3;
        81680  +    x.nKey = pOp->p3;
 80989  81681     }
 80990  81682   
 80991  81683     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 80992  81684       assert( pC->isTable );
 80993  81685       assert( pC->iDb>=0 );
 80994         -    zDb = db->aDb[pC->iDb].zName;
        81686  +    zDb = db->aDb[pC->iDb].zDbSName;
 80995  81687       pTab = pOp->p4.pTab;
 80996  81688       assert( HasRowid(pTab) );
 80997  81689       op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 80998  81690     }else{
 80999  81691       pTab = 0; /* Not needed.  Silence a comiler warning. */
 81000  81692       zDb = 0;  /* Not needed.  Silence a compiler warning. */
 81001  81693     }
................................................................................
 81002  81694   
 81003  81695   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
 81004  81696     /* Invoke the pre-update hook, if any */
 81005  81697     if( db->xPreUpdateCallback 
 81006  81698      && pOp->p4type==P4_TABLE
 81007  81699      && !(pOp->p5 & OPFLAG_ISUPDATE)
 81008  81700     ){
 81009         -    sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, iKey, pOp->p2);
        81701  +    sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
 81010  81702     }
 81011  81703   #endif
 81012  81704   
 81013  81705     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 81014         -  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
        81706  +  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = x.nKey;
 81015  81707     if( pData->flags & MEM_Null ){
 81016         -    pData->z = 0;
 81017         -    pData->n = 0;
        81708  +    x.pData = 0;
        81709  +    x.nData = 0;
 81018  81710     }else{
 81019  81711       assert( pData->flags & (MEM_Blob|MEM_Str) );
        81712  +    x.pData = pData->z;
        81713  +    x.nData = pData->n;
 81020  81714     }
 81021  81715     seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
 81022  81716     if( pData->flags & MEM_Zero ){
 81023         -    nZero = pData->u.nZero;
        81717  +    x.nZero = pData->u.nZero;
 81024  81718     }else{
 81025         -    nZero = 0;
        81719  +    x.nZero = 0;
 81026  81720     }
 81027         -  rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey,
 81028         -                          pData->z, pData->n, nZero,
        81721  +  x.pKey = 0;
        81722  +  rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
 81029  81723                             (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
 81030  81724     );
 81031  81725     pC->deferredMoveto = 0;
 81032  81726     pC->cacheStatus = CACHE_STALE;
 81033  81727   
 81034  81728     /* Invoke the update-hook if required. */
 81035  81729     if( rc ) goto abort_due_to_error;
 81036  81730     if( db->xUpdateCallback && op ){
 81037         -    db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, iKey);
        81731  +    db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
 81038  81732     }
 81039  81733     break;
 81040  81734   }
 81041  81735   
 81042  81736   /* Opcode: Delete P1 P2 P3 P4 P5
 81043  81737   **
 81044  81738   ** Delete the record at which the P1 cursor is currently pointing.
................................................................................
 81089  81783     assert( pC->deferredMoveto==0 );
 81090  81784   
 81091  81785   #ifdef SQLITE_DEBUG
 81092  81786     if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
 81093  81787       /* If p5 is zero, the seek operation that positioned the cursor prior to
 81094  81788       ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
 81095  81789       ** the row that is being deleted */
 81096         -    i64 iKey = 0;
 81097         -    sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
        81790  +    i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
 81098  81791       assert( pC->movetoTarget==iKey );
 81099  81792     }
 81100  81793   #endif
 81101  81794   
 81102  81795     /* If the update-hook or pre-update-hook will be invoked, set zDb to
 81103  81796     ** the name of the db to pass as to it. Also set local pTab to a copy
 81104  81797     ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
 81105  81798     ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 
 81106  81799     ** VdbeCursor.movetoTarget to the current rowid.  */
 81107  81800     if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
 81108  81801       assert( pC->iDb>=0 );
 81109  81802       assert( pOp->p4.pTab!=0 );
 81110         -    zDb = db->aDb[pC->iDb].zName;
        81803  +    zDb = db->aDb[pC->iDb].zDbSName;
 81111  81804       pTab = pOp->p4.pTab;
 81112  81805       if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
 81113         -      sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget);
        81806  +      pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
 81114  81807       }
 81115  81808     }else{
 81116  81809       zDb = 0;   /* Not needed.  Silence a compiler warning. */
 81117  81810       pTab = 0;  /* Not needed.  Silence a compiler warning. */
 81118  81811     }
 81119  81812   
 81120  81813   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
................................................................................
 81260  81953   ** of a real table, not a pseudo-table.
 81261  81954   */
 81262  81955   case OP_RowKey:
 81263  81956   case OP_RowData: {
 81264  81957     VdbeCursor *pC;
 81265  81958     BtCursor *pCrsr;
 81266  81959     u32 n;
 81267         -  i64 n64;
 81268  81960   
 81269  81961     pOut = &aMem[pOp->p2];
 81270  81962     memAboutToChange(p, pOut);
 81271  81963   
 81272  81964     /* Note that RowKey and RowData are really exactly the same instruction */
 81273  81965     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 81274  81966     pC = p->apCsr[pOp->p1];
................................................................................
 81278  81970     assert( pC->isTable || pOp->opcode!=OP_RowData );
 81279  81971     assert( pC->isTable==0 || pOp->opcode==OP_RowData );
 81280  81972     assert( pC->nullRow==0 );
 81281  81973     assert( pC->uc.pCursor!=0 );
 81282  81974     pCrsr = pC->uc.pCursor;
 81283  81975   
 81284  81976     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 81285         -  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 81286         -  ** the cursor.  If this where not the case, on of the following assert()s
        81977  +  ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
        81978  +  ** that might invalidate the cursor.
        81979  +  ** If this where not the case, on of the following assert()s
 81287  81980     ** would fail.  Should this ever change (because of changes in the code
 81288  81981     ** generator) then the fix would be to insert a call to
 81289  81982     ** sqlite3VdbeCursorMoveto().
 81290  81983     */
 81291  81984     assert( pC->deferredMoveto==0 );
 81292  81985     assert( sqlite3BtreeCursorIsValid(pCrsr) );
 81293  81986   #if 0  /* Not required due to the previous to assert() statements */
 81294  81987     rc = sqlite3VdbeCursorMoveto(pC);
 81295  81988     if( rc!=SQLITE_OK ) goto abort_due_to_error;
 81296  81989   #endif
 81297  81990   
 81298         -  if( pC->isTable==0 ){
 81299         -    assert( !pC->isTable );
 81300         -    VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
 81301         -    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 81302         -    if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 81303         -      goto too_big;
 81304         -    }
 81305         -    n = (u32)n64;
 81306         -  }else{
 81307         -    VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
 81308         -    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
        81991  +  n = sqlite3BtreePayloadSize(pCrsr);
 81309  81992       if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 81310  81993         goto too_big;
 81311  81994       }
 81312         -  }
 81313  81995     testcase( n==0 );
 81314  81996     if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
 81315  81997       goto no_mem;
 81316  81998     }
 81317  81999     pOut->n = n;
 81318  82000     MemSetTypeFlag(pOut, MEM_Blob);
 81319  82001     if( pC->isTable==0 ){
................................................................................
 81369  82051       assert( pC->uc.pCursor!=0 );
 81370  82052       rc = sqlite3VdbeCursorRestore(pC);
 81371  82053       if( rc ) goto abort_due_to_error;
 81372  82054       if( pC->nullRow ){
 81373  82055         pOut->flags = MEM_Null;
 81374  82056         break;
 81375  82057       }
 81376         -    rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
 81377         -    assert( rc==SQLITE_OK );  /* Always so because of CursorRestore() above */
        82058  +    v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
 81378  82059     }
 81379  82060     pOut->u.i = v;
 81380  82061     break;
 81381  82062   }
 81382  82063   
 81383  82064   /* Opcode: NullRow P1 * * * *
 81384  82065   **
................................................................................
 81645  82326   **
 81646  82327   ** This instruction only works for indices.  The equivalent instruction
 81647  82328   ** for tables is OP_Insert.
 81648  82329   */
 81649  82330   case OP_SorterInsert:       /* in2 */
 81650  82331   case OP_IdxInsert: {        /* in2 */
 81651  82332     VdbeCursor *pC;
 81652         -  int nKey;
 81653         -  const char *zKey;
        82333  +  BtreePayload x;
 81654  82334   
 81655  82335     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 81656  82336     pC = p->apCsr[pOp->p1];
 81657  82337     assert( pC!=0 );
 81658  82338     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
 81659  82339     pIn2 = &aMem[pOp->p2];
 81660  82340     assert( pIn2->flags & MEM_Blob );
................................................................................
 81662  82342     assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
 81663  82343     assert( pC->isTable==0 );
 81664  82344     rc = ExpandBlob(pIn2);
 81665  82345     if( rc ) goto abort_due_to_error;
 81666  82346     if( pOp->opcode==OP_SorterInsert ){
 81667  82347       rc = sqlite3VdbeSorterWrite(pC, pIn2);
 81668  82348     }else{
 81669         -    nKey = pIn2->n;
 81670         -    zKey = pIn2->z;
 81671         -    rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, 
        82349  +    x.nKey = pIn2->n;
        82350  +    x.pKey = pIn2->z;
        82351  +    x.nData = 0;
        82352  +    x.nZero = 0;
        82353  +    x.pData = 0;
        82354  +    rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, pOp->p3, 
 81672  82355           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
 81673  82356           );
 81674  82357       assert( pC->deferredMoveto==0 );
 81675  82358       pC->cacheStatus = CACHE_STALE;
 81676  82359     }
 81677  82360     if( rc) goto abort_due_to_error;
 81678  82361     break;
................................................................................
 82083  82766     /* Used to be a conditional */ {
 82084  82767       zMaster = SCHEMA_TABLE(iDb);
 82085  82768       initData.db = db;
 82086  82769       initData.iDb = pOp->p1;
 82087  82770       initData.pzErrMsg = &p->zErrMsg;
 82088  82771       zSql = sqlite3MPrintf(db,
 82089  82772          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 82090         -       db->aDb[iDb].zName, zMaster, pOp->p4.z);
        82773  +       db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
 82091  82774       if( zSql==0 ){
 82092  82775         rc = SQLITE_NOMEM_BKPT;
 82093  82776       }else{
 82094  82777         assert( db->init.busy==0 );
 82095  82778         db->init.busy = 1;
 82096  82779         initData.rc = SQLITE_OK;
 82097  82780         assert( !db->mallocFailed );
................................................................................
 82388  83071       ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
 82389  83072       */
 82390  83073       nMem = pProgram->nMem + pProgram->nCsr;
 82391  83074       assert( nMem>0 );
 82392  83075       if( pProgram->nCsr==0 ) nMem++;
 82393  83076       nByte = ROUND8(sizeof(VdbeFrame))
 82394  83077                 + nMem * sizeof(Mem)
 82395         -              + pProgram->nCsr * sizeof(VdbeCursor *)
 82396         -              + pProgram->nOnce * sizeof(u8);
        83078  +              + pProgram->nCsr * sizeof(VdbeCursor *);
 82397  83079       pFrame = sqlite3DbMallocZero(db, nByte);
 82398  83080       if( !pFrame ){
 82399  83081         goto no_mem;
 82400  83082       }
 82401  83083       sqlite3VdbeMemRelease(pRt);
 82402  83084       pRt->flags = MEM_Frame;
 82403  83085       pRt->u.pFrame = pFrame;
................................................................................
 82409  83091       pFrame->aMem = p->aMem;
 82410  83092       pFrame->nMem = p->nMem;
 82411  83093       pFrame->apCsr = p->apCsr;
 82412  83094       pFrame->nCursor = p->nCursor;
 82413  83095       pFrame->aOp = p->aOp;
 82414  83096       pFrame->nOp = p->nOp;
 82415  83097       pFrame->token = pProgram->token;
 82416         -    pFrame->aOnceFlag = p->aOnceFlag;
 82417         -    pFrame->nOnceFlag = p->nOnceFlag;
 82418  83098   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 82419  83099       pFrame->anExec = p->anExec;
 82420  83100   #endif
 82421  83101   
 82422  83102       pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
 82423  83103       for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
 82424  83104         pMem->flags = MEM_Undefined;
................................................................................
 82444  83124     p->pFrame = pFrame;
 82445  83125     p->aMem = aMem = VdbeFrameMem(pFrame);
 82446  83126     p->nMem = pFrame->nChildMem;
 82447  83127     p->nCursor = (u16)pFrame->nChildCsr;
 82448  83128     p->apCsr = (VdbeCursor **)&aMem[p->nMem];
 82449  83129     p->aOp = aOp = pProgram->aOp;
 82450  83130     p->nOp = pProgram->nOp;
 82451         -  p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 82452         -  p->nOnceFlag = pProgram->nOnce;
 82453  83131   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
 82454  83132     p->anExec = 0;
 82455  83133   #endif
 82456  83134     pOp = &aOp[-1];
 82457         -  memset(p->aOnceFlag, 0, p->nOnceFlag);
 82458  83135   
 82459  83136     break;
 82460  83137   }
 82461  83138   
 82462  83139   /* Opcode: Param P1 P2 * * *
 82463  83140   **
 82464  83141   ** This opcode is only ever present in sub-programs called via the 
................................................................................
 82912  83589     sqlite3VdbeChangeEncoding(pOut, encoding);
 82913  83590     if( rc ) goto abort_due_to_error;
 82914  83591     break;
 82915  83592   };
 82916  83593   #endif /* SQLITE_OMIT_PRAGMA */
 82917  83594   
 82918  83595   #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 82919         -/* Opcode: Vacuum * * * * *
        83596  +/* Opcode: Vacuum P1 * * * *
 82920  83597   **
 82921         -** Vacuum the entire database.  This opcode will cause other virtual
 82922         -** machines to be created and run.  It may not be called from within
 82923         -** a transaction.
        83598  +** Vacuum the entire database P1.  P1 is 0 for "main", and 2 or more
        83599  +** for an attached database.  The "temp" database may not be vacuumed.
 82924  83600   */
 82925  83601   case OP_Vacuum: {
 82926  83602     assert( p->readOnly==0 );
 82927         -  rc = sqlite3RunVacuum(&p->zErrMsg, db);
        83603  +  rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
 82928  83604     if( rc ) goto abort_due_to_error;
 82929  83605     break;
 82930  83606   }
 82931  83607   #endif
 82932  83608   
 82933  83609   #if !defined(SQLITE_OMIT_AUTOVACUUM)
 82934  83610   /* Opcode: IncrVacuum P1 P2 * * *
................................................................................
 83418  84094     }
 83419  84095     pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 83420  84096     break;
 83421  84097   }
 83422  84098   #endif
 83423  84099   
 83424  84100   
 83425         -/* Opcode: Init * P2 * P4 *
        84101  +/* Opcode: Init P1 P2 * P4 *
 83426  84102   ** Synopsis:  Start at P2
 83427  84103   **
 83428  84104   ** Programs contain a single instance of this opcode as the very first
 83429  84105   ** opcode.
 83430  84106   **
 83431  84107   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 83432  84108   ** the UTF-8 string contained in P4 is emitted on the trace callback.
 83433  84109   ** Or if P4 is blank, use the string returned by sqlite3_sql().
 83434  84110   **
 83435  84111   ** If P2 is not zero, jump to instruction P2.
        84112  +**
        84113  +** Increment the value of P1 so that OP_Once opcodes will jump the
        84114  +** first time they are evaluated for this run.
 83436  84115   */
 83437  84116   case OP_Init: {          /* jump */
 83438  84117     char *zTrace;
 83439         -  char *z;
        84118  +  int i;
        84119  +
        84120  +  /* If the P4 argument is not NULL, then it must be an SQL comment string.
        84121  +  ** The "--" string is broken up to prevent false-positives with srcck1.c.
        84122  +  **
        84123  +  ** This assert() provides evidence for:
        84124  +  ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
        84125  +  ** would have been returned by the legacy sqlite3_trace() interface by
        84126  +  ** using the X argument when X begins with "--" and invoking
        84127  +  ** sqlite3_expanded_sql(P) otherwise.
        84128  +  */
        84129  +  assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
        84130  +  assert( pOp==p->aOp );  /* Always instruction 0 */
 83440  84131   
 83441  84132   #ifndef SQLITE_OMIT_TRACE
 83442         -  if( db->xTrace
        84133  +  if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
 83443  84134      && !p->doingRerun
 83444  84135      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 83445  84136     ){
 83446         -    z = sqlite3VdbeExpandSql(p, zTrace);
 83447         -    db->xTrace(db->pTraceArg, z);
 83448         -    sqlite3DbFree(db, z);
        84137  +#ifndef SQLITE_OMIT_DEPRECATED
        84138  +    if( db->mTrace & SQLITE_TRACE_LEGACY ){
        84139  +      void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
        84140  +      char *z = sqlite3VdbeExpandSql(p, zTrace);
        84141  +      x(db->pTraceArg, z);
        84142  +      sqlite3_free(z);
        84143  +    }else
        84144  +#endif
        84145  +    {
        84146  +      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
        84147  +    }
 83449  84148     }
 83450  84149   #ifdef SQLITE_USE_FCNTL_TRACE
 83451  84150     zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 83452  84151     if( zTrace ){
 83453  84152       int i;
 83454  84153       for(i=0; i<db->nDb; i++){
 83455  84154         if( DbMaskTest(p->btreeMask, i)==0 ) continue;
 83456         -      sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
        84155  +      sqlite3_file_control(db, db->aDb[i].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
 83457  84156       }
 83458  84157     }
 83459  84158   #endif /* SQLITE_USE_FCNTL_TRACE */
 83460  84159   #ifdef SQLITE_DEBUG
 83461  84160     if( (db->flags & SQLITE_SqlTrace)!=0
 83462  84161      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 83463  84162     ){
 83464  84163       sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 83465  84164     }
 83466  84165   #endif /* SQLITE_DEBUG */
 83467  84166   #endif /* SQLITE_OMIT_TRACE */
 83468         -  if( pOp->p2 ) goto jump_to_p2;
 83469         -  break;
        84167  +  assert( pOp->p2>0 );
        84168  +  if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
        84169  +    for(i=1; i<p->nOp; i++){
        84170  +      if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
        84171  +    }
        84172  +    pOp->p1 = 0;
        84173  +  }
        84174  +  pOp->p1++;
        84175  +  goto jump_to_p2;
 83470  84176   }
 83471  84177   
 83472  84178   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 83473  84179   /* Opcode: CursorHint P1 * * P4 *
 83474  84180   **
 83475  84181   ** Provide a hint to cursor P1 that it only needs to return rows that
 83476  84182   ** satisfy the Expr in P4.  TK_REGISTER terms in the P4 expression refer
................................................................................
 83785  84491           pParse->zErrMsg = 0;
 83786  84492         }
 83787  84493         rc = SQLITE_ERROR;
 83788  84494         sqlite3BtreeLeaveAll(db);
 83789  84495         goto blob_open_out;
 83790  84496       }
 83791  84497       pBlob->pTab = pTab;
 83792         -    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zName;
        84498  +    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
 83793  84499   
 83794  84500       /* Now search pTab for the exact column. */
 83795  84501       for(iCol=0; iCol<pTab->nCol; iCol++) {
 83796  84502         if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
 83797  84503           break;
 83798  84504         }
 83799  84505       }
................................................................................
 84024  84730         ** SQLITE_UPDATE where the PK columns do not change is handled in the 
 84025  84731         ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
 84026  84732         ** slightly more efficient). Since you cannot write to a PK column
 84027  84733         ** using the incremental-blob API, this works. For the sessions module
 84028  84734         ** anyhow.
 84029  84735         */
 84030  84736         sqlite3_int64 iKey;
 84031         -      sqlite3BtreeKeySize(p->pCsr, &iKey);
        84737  +      iKey = sqlite3BtreeIntegerKey(p->pCsr);
 84032  84738         sqlite3VdbePreUpdateHook(
 84033  84739             v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
 84034  84740         );
 84035  84741       }
 84036  84742   #endif
 84037  84743   
 84038  84744       rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
................................................................................
 85459  86165     }
 85460  86166     return SQLITE_OK;
 85461  86167   }
 85462  86168   
 85463  86169   
 85464  86170   /*
 85465  86171   ** Merge the two sorted lists p1 and p2 into a single list.
 85466         -** Set *ppOut to the head of the new list.
 85467  86172   */
 85468         -static void vdbeSorterMerge(
        86173  +static SorterRecord *vdbeSorterMerge(
 85469  86174     SortSubtask *pTask,             /* Calling thread context */
 85470  86175     SorterRecord *p1,               /* First list to merge */
 85471         -  SorterRecord *p2,               /* Second list to merge */
 85472         -  SorterRecord **ppOut            /* OUT: Head of merged list */
        86176  +  SorterRecord *p2                /* Second list to merge */
 85473  86177   ){
 85474  86178     SorterRecord *pFinal = 0;
 85475  86179     SorterRecord **pp = &pFinal;
 85476  86180     int bCached = 0;
 85477  86181   
 85478         -  while( p1 && p2 ){
        86182  +  assert( p1!=0 && p2!=0 );
        86183  +  for(;;){
 85479  86184       int res;
 85480  86185       res = pTask->xCompare(
 85481  86186           pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
 85482  86187       );
 85483  86188   
 85484  86189       if( res<=0 ){
 85485  86190         *pp = p1;
 85486  86191         pp = &p1->u.pNext;
 85487  86192         p1 = p1->u.pNext;
        86193  +      if( p1==0 ){
        86194  +        *pp = p2;
        86195  +        break;
        86196  +      }
 85488  86197       }else{
 85489  86198         *pp = p2;
 85490  86199         pp = &p2->u.pNext;
 85491  86200         p2 = p2->u.pNext;
 85492  86201         bCached = 0;
        86202  +      if( p2==0 ){
        86203  +        *pp = p1;
        86204  +        break;
 85493  86205       }
 85494  86206     }
 85495         -  *pp = p1 ? p1 : p2;
 85496         -  *ppOut = pFinal;
        86207  +  }
        86208  +  return pFinal;
 85497  86209   }
 85498  86210   
 85499  86211   /*
 85500  86212   ** Return the SorterCompare function to compare values collected by the
 85501  86213   ** sorter object passed as the only argument.
 85502  86214   */
 85503  86215   static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
................................................................................
 85542  86254         }
 85543  86255       }else{
 85544  86256         pNext = p->u.pNext;
 85545  86257       }
 85546  86258   
 85547  86259       p->u.pNext = 0;
 85548  86260       for(i=0; aSlot[i]; i++){
 85549         -      vdbeSorterMerge(pTask, p, aSlot[i], &p);
        86261  +      p = vdbeSorterMerge(pTask, p, aSlot[i]);
 85550  86262         aSlot[i] = 0;
 85551  86263       }
 85552  86264       aSlot[i] = p;
 85553  86265       p = pNext;
 85554  86266     }
 85555  86267   
 85556  86268     p = 0;
 85557  86269     for(i=0; i<64; i++){
 85558         -    vdbeSorterMerge(pTask, p, aSlot[i], &p);
        86270  +    if( aSlot[i]==0 ) continue;
        86271  +    p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
 85559  86272     }
 85560  86273     pList->pList = p;
 85561  86274   
 85562  86275     sqlite3_free(aSlot);
 85563  86276     assert( pTask->pUnpacked->errCode==SQLITE_OK 
 85564  86277          || pTask->pUnpacked->errCode==SQLITE_NOMEM 
 85565  86278     );
................................................................................
 87330  88043   ** and WRC_Continue to continue.
 87331  88044   */
 87332  88045   static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
 87333  88046     int rc;
 87334  88047     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 87335  88048     testcase( ExprHasProperty(pExpr, EP_Reduced) );
 87336  88049     rc = pWalker->xExprCallback(pWalker, pExpr);
 87337         -  if( rc==WRC_Continue
 87338         -              && !ExprHasProperty(pExpr,EP_TokenOnly) ){
 87339         -    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 87340         -    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
        88050  +  if( rc || ExprHasProperty(pExpr,EP_TokenOnly) ) return rc & WRC_Abort;
        88051  +  if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
        88052  +  if( pExpr->pRight && walkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 87341  88053       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 87342  88054         if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 87343         -    }else{
        88055  +  }else if( pExpr->x.pList ){
 87344  88056         if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 87345  88057       }
 87346         -  }
 87347         -  return rc & WRC_Abort;
        88058  +  return WRC_Continue;
 87348  88059   }
 87349  88060   SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 87350  88061     return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
 87351  88062   }
 87352  88063   
 87353  88064   /*
 87354  88065   ** Call sqlite3WalkExpr() for every expression in list p or until
................................................................................
 87674  88385         /* Silently ignore database qualifiers inside CHECK constraints and
 87675  88386         ** partial indices.  Do not raise errors because that might break
 87676  88387         ** legacy and because it does not hurt anything to just ignore the
 87677  88388         ** database name. */
 87678  88389         zDb = 0;
 87679  88390       }else{
 87680  88391         for(i=0; i<db->nDb; i++){
 87681         -        assert( db->aDb[i].zName );
 87682         -        if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
        88392  +        assert( db->aDb[i].zDbSName );
        88393  +        if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
 87683  88394             pSchema = db->aDb[i].pSchema;
 87684  88395             break;
 87685  88396           }
 87686  88397         }
 87687  88398       }
 87688  88399     }
 87689  88400   
................................................................................
 88099  88810       */
 88100  88811       case TK_FUNCTION: {
 88101  88812         ExprList *pList = pExpr->x.pList;    /* The argument list */
 88102  88813         int n = pList ? pList->nExpr : 0;    /* Number of arguments */
 88103  88814         int no_such_func = 0;       /* True if no such function exists */
 88104  88815         int wrong_num_args = 0;     /* True if wrong number of arguments */
 88105  88816         int is_agg = 0;             /* True if is an aggregate function */
 88106         -      int auth;                   /* Authorization to use the function */
 88107  88817         int nId;                    /* Number of characters in function name */
 88108  88818         const char *zId;            /* The function name. */
 88109  88819         FuncDef *pDef;              /* Information about the function */
 88110  88820         u8 enc = ENC(pParse->db);   /* The database encoding */
 88111  88821   
 88112  88822         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 88113  88823         notValid(pParse, pNC, "functions", NC_PartIdx);
................................................................................
 88143  88853               ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
 88144  88854               ** to likelihood(X,0.9375). */
 88145  88855               /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
 88146  88856               pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
 88147  88857             }             
 88148  88858           }
 88149  88859   #ifndef SQLITE_OMIT_AUTHORIZATION
 88150         -        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
        88860  +        {
        88861  +          int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
 88151  88862           if( auth!=SQLITE_OK ){
 88152  88863             if( auth==SQLITE_DENY ){
 88153  88864               sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
 88154  88865                                       pDef->zName);
 88155  88866               pNC->nErr++;
 88156  88867             }
 88157  88868             pExpr->op = TK_NULL;
 88158  88869             return WRC_Prune;
 88159  88870           }
        88871  +        }
 88160  88872   #endif
 88161  88873           if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
 88162  88874             /* For the purposes of the EP_ConstFunc flag, date and time
 88163  88875             ** functions and other functions that change slowly are considered
 88164  88876             ** constant because they are constant for the duration of one query */
 88165  88877             ExprSetProperty(pExpr,EP_ConstFunc);
 88166  88878           }
................................................................................
 88171  88883             notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr);
 88172  88884           }
 88173  88885         }
 88174  88886         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 88175  88887           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 88176  88888           pNC->nErr++;
 88177  88889           is_agg = 0;
 88178         -      }else if( no_such_func && pParse->db->init.busy==0 ){
        88890  +      }else if( no_such_func && pParse->db->init.busy==0
        88891  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
        88892  +                && pParse->explain==0
        88893  +#endif
        88894  +      ){
 88179  88895           sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
 88180  88896           pNC->nErr++;
 88181  88897         }else if( wrong_num_args ){
 88182  88898           sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 88183  88899                nId, zId);
 88184  88900           pNC->nErr++;
 88185  88901         }
................................................................................
 88224  88940           }
 88225  88941         }
 88226  88942         break;
 88227  88943       }
 88228  88944       case TK_VARIABLE: {
 88229  88945         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
 88230  88946         break;
        88947  +    }
        88948  +    case TK_EQ:
        88949  +    case TK_NE:
        88950  +    case TK_LT:
        88951  +    case TK_LE:
        88952  +    case TK_GT:
        88953  +    case TK_GE:
        88954  +    case TK_IS:
        88955  +    case TK_ISNOT: {
        88956  +      int nLeft, nRight;
        88957  +      if( pParse->db->mallocFailed ) break;
        88958  +      assert( pExpr->pRight!=0 );
        88959  +      assert( pExpr->pLeft!=0 );
        88960  +      nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
        88961  +      nRight = sqlite3ExprVectorSize(pExpr->pRight);
        88962  +      if( nLeft!=nRight ){
        88963  +        testcase( pExpr->op==TK_EQ );
        88964  +        testcase( pExpr->op==TK_NE );
        88965  +        testcase( pExpr->op==TK_LT );
        88966  +        testcase( pExpr->op==TK_LE );
        88967  +        testcase( pExpr->op==TK_GT );
        88968  +        testcase( pExpr->op==TK_GE );
        88969  +        testcase( pExpr->op==TK_IS );
        88970  +        testcase( pExpr->op==TK_ISNOT );
        88971  +        sqlite3ErrorMsg(pParse, "row value misused");
        88972  +      }
        88973  +      break; 
 88231  88974       }
 88232  88975     }
 88233  88976     return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
 88234  88977   }
 88235  88978   
 88236  88979   /*
 88237  88980   ** pEList is a list of expressions which are really the result set of the
................................................................................
 88966  89709   **    May you share freely, never taking more than you give.
 88967  89710   **
 88968  89711   *************************************************************************
 88969  89712   ** This file contains routines used for analyzing expressions and
 88970  89713   ** for generating VDBE code that evaluates expressions in SQLite.
 88971  89714   */
 88972  89715   /* #include "sqliteInt.h" */
        89716  +
        89717  +/* Forward declarations */
        89718  +static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
        89719  +static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
        89720  +
        89721  +/*
        89722  +** Return the affinity character for a single column of a table.
        89723  +*/
        89724  +SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
        89725  +  assert( iCol<pTab->nCol );
        89726  +  return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
        89727  +}
 88973  89728   
 88974  89729   /*
 88975  89730   ** Return the 'affinity' of the expression pExpr if any.
 88976  89731   **
 88977  89732   ** If pExpr is a column, a reference to a column via an 'AS' alias,
 88978  89733   ** or a sub-select with a column as the return value, then the 
 88979  89734   ** affinity of that column is returned. Otherwise, 0x00 is returned,
................................................................................
 88992  89747     pExpr = sqlite3ExprSkipCollate(pExpr);
 88993  89748     if( pExpr->flags & EP_Generic ) return 0;
 88994  89749     op = pExpr->op;
 88995  89750     if( op==TK_SELECT ){
 88996  89751       assert( pExpr->flags&EP_xIsSelect );
 88997  89752       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 88998  89753     }
        89754  +  if( op==TK_REGISTER ) op = pExpr->op2;
 88999  89755   #ifndef SQLITE_OMIT_CAST
 89000  89756     if( op==TK_CAST ){
 89001  89757       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 89002  89758       return sqlite3AffinityType(pExpr->u.zToken, 0);
 89003  89759     }
 89004  89760   #endif
 89005         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
 89006         -   && pExpr->pTab!=0
 89007         -  ){
 89008         -    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
 89009         -    ** a TK_COLUMN but was previously evaluated and cached in a register */
 89010         -    int j = pExpr->iColumn;
 89011         -    if( j<0 ) return SQLITE_AFF_INTEGER;
 89012         -    assert( pExpr->pTab && j<pExpr->pTab->nCol );
 89013         -    return pExpr->pTab->aCol[j].affinity;
        89761  +  if( op==TK_AGG_COLUMN || op==TK_COLUMN ){
        89762  +    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
        89763  +  }
        89764  +  if( op==TK_SELECT_COLUMN ){
        89765  +    assert( pExpr->pLeft->flags&EP_xIsSelect );
        89766  +    return sqlite3ExprAffinity(
        89767  +        pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
        89768  +    );
 89014  89769     }
 89015  89770     return pExpr->affinity;
 89016  89771   }
 89017  89772   
 89018  89773   /*
 89019  89774   ** Set the collating sequence for expression pExpr to be the collating
 89020  89775   ** sequence named by pToken.   Return a pointer to a new Expr node that
................................................................................
 89172  89927             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 89173  89928     assert( pExpr->pLeft );
 89174  89929     aff = sqlite3ExprAffinity(pExpr->pLeft);
 89175  89930     if( pExpr->pRight ){
 89176  89931       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 89177  89932     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 89178  89933       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 89179         -  }else if( !aff ){
        89934  +  }else if( NEVER(aff==0) ){
 89180  89935       aff = SQLITE_AFF_BLOB;
 89181  89936     }
 89182  89937     return aff;
 89183  89938   }
 89184  89939   
 89185  89940   /*
 89186  89941   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
................................................................................
 89261  90016     p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
 89262  90017     p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
 89263  90018     addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
 89264  90019                              (void*)p4, P4_COLLSEQ);
 89265  90020     sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
 89266  90021     return addr;
 89267  90022   }
        90023  +
        90024  +/*
        90025  +** Return true if expression pExpr is a vector, or false otherwise.
        90026  +**
        90027  +** A vector is defined as any expression that results in two or more
        90028  +** columns of result.  Every TK_VECTOR node is an vector because the
        90029  +** parser will not generate a TK_VECTOR with fewer than two entries.
        90030  +** But a TK_SELECT might be either a vector or a scalar. It is only
        90031  +** considered a vector if it has two or more result columns.
        90032  +*/
        90033  +SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
        90034  +  return sqlite3ExprVectorSize(pExpr)>1;
        90035  +}
        90036  +
        90037  +/*
        90038  +** If the expression passed as the only argument is of type TK_VECTOR 
        90039  +** return the number of expressions in the vector. Or, if the expression
        90040  +** is a sub-select, return the number of columns in the sub-select. For
        90041  +** any other type of expression, return 1.
        90042  +*/
        90043  +SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
        90044  +  u8 op = pExpr->op;
        90045  +  if( op==TK_REGISTER ) op = pExpr->op2;
        90046  +  if( op==TK_VECTOR ){
        90047  +    return pExpr->x.pList->nExpr;
        90048  +  }else if( op==TK_SELECT ){
        90049  +    return pExpr->x.pSelect->pEList->nExpr;
        90050  +  }else{
        90051  +    return 1;
        90052  +  }
        90053  +}
        90054  +
        90055  +#ifndef SQLITE_OMIT_SUBQUERY
        90056  +/*
        90057  +** Return a pointer to a subexpression of pVector that is the i-th
        90058  +** column of the vector (numbered starting with 0).  The caller must
        90059  +** ensure that i is within range.
        90060  +**
        90061  +** If pVector is really a scalar (and "scalar" here includes subqueries
        90062  +** that return a single column!) then return pVector unmodified.
        90063  +**
        90064  +** pVector retains ownership of the returned subexpression.
        90065  +**
        90066  +** If the vector is a (SELECT ...) then the expression returned is
        90067  +** just the expression for the i-th term of the result set, and may
        90068  +** not be ready for evaluation because the table cursor has not yet
        90069  +** been positioned.
        90070  +*/
        90071  +SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
        90072  +  assert( i<sqlite3ExprVectorSize(pVector) );
        90073  +  if( sqlite3ExprIsVector(pVector) ){
        90074  +    assert( pVector->op2==0 || pVector->op==TK_REGISTER );
        90075  +    if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
        90076  +      return pVector->x.pSelect->pEList->a[i].pExpr;
        90077  +    }else{
        90078  +      return pVector->x.pList->a[i].pExpr;
        90079  +    }
        90080  +  }
        90081  +  return pVector;
        90082  +}
        90083  +#endif /* !defined(SQLITE_OMIT_SUBQUERY) */
        90084  +
        90085  +#ifndef SQLITE_OMIT_SUBQUERY
        90086  +/*
        90087  +** Compute and return a new Expr object which when passed to
        90088  +** sqlite3ExprCode() will generate all necessary code to compute
        90089  +** the iField-th column of the vector expression pVector.
        90090  +**
        90091  +** It is ok for pVector to be a scalar (as long as iField==0).  
        90092  +** In that case, this routine works like sqlite3ExprDup().
        90093  +**
        90094  +** The caller owns the returned Expr object and is responsible for
        90095  +** ensuring that the returned value eventually gets freed.
        90096  +**
        90097  +** The caller retains ownership of pVector.  If pVector is a TK_SELECT,
        90098  +** then the returned object will reference pVector and so pVector must remain
        90099  +** valid for the life of the returned object.  If pVector is a TK_VECTOR
        90100  +** or a scalar expression, then it can be deleted as soon as this routine
        90101  +** returns.
        90102  +**
        90103  +** A trick to cause a TK_SELECT pVector to be deleted together with
        90104  +** the returned Expr object is to attach the pVector to the pRight field
        90105  +** of the returned TK_SELECT_COLUMN Expr object.
        90106  +*/
        90107  +SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
        90108  +  Parse *pParse,       /* Parsing context */
        90109  +  Expr *pVector,       /* The vector.  List of expressions or a sub-SELECT */
        90110  +  int iField           /* Which column of the vector to return */
        90111  +){
        90112  +  Expr *pRet;
        90113  +  if( pVector->op==TK_SELECT ){
        90114  +    assert( pVector->flags & EP_xIsSelect );
        90115  +    /* The TK_SELECT_COLUMN Expr node:
        90116  +    **
        90117  +    ** pLeft:           pVector containing TK_SELECT
        90118  +    ** pRight:          not used.  But recursively deleted.
        90119  +    ** iColumn:         Index of a column in pVector
        90120  +    ** pLeft->iTable:   First in an array of register holding result, or 0
        90121  +    **                  if the result is not yet computed.
        90122  +    **
        90123  +    ** sqlite3ExprDelete() specifically skips the recursive delete of
        90124  +    ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
        90125  +    ** can be attached to pRight to cause this node to take ownership of
        90126  +    ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
        90127  +    ** with the same pLeft pointer to the pVector, but only one of them
        90128  +    ** will own the pVector.
        90129  +    */
        90130  +    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0, 0);
        90131  +    if( pRet ){
        90132  +      pRet->iColumn = iField;
        90133  +      pRet->pLeft = pVector;
        90134  +    }
        90135  +    assert( pRet==0 || pRet->iTable==0 );
        90136  +  }else{
        90137  +    if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
        90138  +    pRet = sqlite3ExprDup(pParse->db, pVector, 0);
        90139  +  }
        90140  +  return pRet;
        90141  +}
        90142  +#endif /* !define(SQLITE_OMIT_SUBQUERY) */
        90143  +
        90144  +/*
        90145  +** If expression pExpr is of type TK_SELECT, generate code to evaluate
        90146  +** it. Return the register in which the result is stored (or, if the 
        90147  +** sub-select returns more than one column, the first in an array
        90148  +** of registers in which the result is stored).
        90149  +**
        90150  +** If pExpr is not a TK_SELECT expression, return 0.
        90151  +*/
        90152  +static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
        90153  +  int reg = 0;
        90154  +#ifndef SQLITE_OMIT_SUBQUERY
        90155  +  if( pExpr->op==TK_SELECT ){
        90156  +    reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        90157  +  }
        90158  +#endif
        90159  +  return reg;
        90160  +}
        90161  +
        90162  +/*
        90163  +** Argument pVector points to a vector expression - either a TK_VECTOR
        90164  +** or TK_SELECT that returns more than one column. This function returns
        90165  +** the register number of a register that contains the value of
        90166  +** element iField of the vector.
        90167  +**
        90168  +** If pVector is a TK_SELECT expression, then code for it must have 
        90169  +** already been generated using the exprCodeSubselect() routine. In this
        90170  +** case parameter regSelect should be the first in an array of registers
        90171  +** containing the results of the sub-select. 
        90172  +**
        90173  +** If pVector is of type TK_VECTOR, then code for the requested field
        90174  +** is generated. In this case (*pRegFree) may be set to the number of
        90175  +** a temporary register to be freed by the caller before returning.
        90176  +**
        90177  +** Before returning, output parameter (*ppExpr) is set to point to the
        90178  +** Expr object corresponding to element iElem of the vector.
        90179  +*/
        90180  +static int exprVectorRegister(
        90181  +  Parse *pParse,                  /* Parse context */
        90182  +  Expr *pVector,                  /* Vector to extract element from */
        90183  +  int iField,                     /* Field to extract from pVector */
        90184  +  int regSelect,                  /* First in array of registers */
        90185  +  Expr **ppExpr,                  /* OUT: Expression element */
        90186  +  int *pRegFree                   /* OUT: Temp register to free */
        90187  +){
        90188  +  u8 op = pVector->op;
        90189  +  assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
        90190  +  if( op==TK_REGISTER ){
        90191  +    *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
        90192  +    return pVector->iTable+iField;
        90193  +  }
        90194  +  if( op==TK_SELECT ){
        90195  +    *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
        90196  +     return regSelect+iField;
        90197  +  }
        90198  +  *ppExpr = pVector->x.pList->a[iField].pExpr;
        90199  +  return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
        90200  +}
        90201  +
        90202  +/*
        90203  +** Expression pExpr is a comparison between two vector values. Compute
        90204  +** the result of the comparison (1, 0, or NULL) and write that
        90205  +** result into register dest.
        90206  +**
        90207  +** The caller must satisfy the following preconditions:
        90208  +**
        90209  +**    if pExpr->op==TK_IS:      op==TK_EQ and p5==SQLITE_NULLEQ
        90210  +**    if pExpr->op==TK_ISNOT:   op==TK_NE and p5==SQLITE_NULLEQ
        90211  +**    otherwise:                op==pExpr->op and p5==0
        90212  +*/
        90213  +static void codeVectorCompare(
        90214  +  Parse *pParse,        /* Code generator context */
        90215  +  Expr *pExpr,          /* The comparison operation */
        90216  +  int dest,             /* Write results into this register */
        90217  +  u8 op,                /* Comparison operator */
        90218  +  u8 p5                 /* SQLITE_NULLEQ or zero */
        90219  +){
        90220  +  Vdbe *v = pParse->pVdbe;
        90221  +  Expr *pLeft = pExpr->pLeft;
        90222  +  Expr *pRight = pExpr->pRight;
        90223  +  int nLeft = sqlite3ExprVectorSize(pLeft);
        90224  +  int i;
        90225  +  int regLeft = 0;
        90226  +  int regRight = 0;
        90227  +  u8 opx = op;
        90228  +  int addrDone = sqlite3VdbeMakeLabel(v);
        90229  +
        90230  +  assert( nLeft==sqlite3ExprVectorSize(pRight) );
        90231  +  assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 
        90232  +       || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 
        90233  +       || pExpr->op==TK_LT || pExpr->op==TK_GT 
        90234  +       || pExpr->op==TK_LE || pExpr->op==TK_GE 
        90235  +  );
        90236  +  assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
        90237  +            || (pExpr->op==TK_ISNOT && op==TK_NE) );
        90238  +  assert( p5==0 || pExpr->op!=op );
        90239  +  assert( p5==SQLITE_NULLEQ || pExpr->op==op );
        90240  +
        90241  +  p5 |= SQLITE_STOREP2;
        90242  +  if( opx==TK_LE ) opx = TK_LT;
        90243  +  if( opx==TK_GE ) opx = TK_GT;
        90244  +
        90245  +  regLeft = exprCodeSubselect(pParse, pLeft);
        90246  +  regRight = exprCodeSubselect(pParse, pRight);
        90247  +
        90248  +  for(i=0; 1 /*Loop exits by "break"*/; i++){
        90249  +    int regFree1 = 0, regFree2 = 0;
        90250  +    Expr *pL, *pR; 
        90251  +    int r1, r2;
        90252  +    assert( i>=0 && i<nLeft );
        90253  +    if( i>0 ) sqlite3ExprCachePush(pParse);
        90254  +    r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
        90255  +    r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
        90256  +    codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
        90257  +    testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
        90258  +    testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
        90259  +    testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
        90260  +    testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
        90261  +    testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
        90262  +    testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
        90263  +    sqlite3ReleaseTempReg(pParse, regFree1);
        90264  +    sqlite3ReleaseTempReg(pParse, regFree2);
        90265  +    if( i>0 ) sqlite3ExprCachePop(pParse);
        90266  +    if( i==nLeft-1 ){
        90267  +      break;
        90268  +    }
        90269  +    if( opx==TK_EQ ){
        90270  +      sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
        90271  +      p5 |= SQLITE_KEEPNULL;
        90272  +    }else if( opx==TK_NE ){
        90273  +      sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
        90274  +      p5 |= SQLITE_KEEPNULL;
        90275  +    }else{
        90276  +      assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
        90277  +      sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
        90278  +      VdbeCoverageIf(v, op==TK_LT);
        90279  +      VdbeCoverageIf(v, op==TK_GT);
        90280  +      VdbeCoverageIf(v, op==TK_LE);
        90281  +      VdbeCoverageIf(v, op==TK_GE);
        90282  +      if( i==nLeft-2 ) opx = op;
        90283  +    }
        90284  +  }
        90285  +  sqlite3VdbeResolveLabel(v, addrDone);
        90286  +}
 89268  90287   
 89269  90288   #if SQLITE_MAX_EXPR_DEPTH>0
 89270  90289   /*
 89271  90290   ** Check that argument nHeight is less than or equal to the maximum
 89272  90291   ** expression depth allowed. If it is not, leave an error message in
 89273  90292   ** pParse.
 89274  90293   */
................................................................................
 89696  90715   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
 89697  90716     assert( p!=0 );
 89698  90717     /* Sanity check: Assert that the IntValue is non-negative if it exists */
 89699  90718     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 89700  90719     if( !ExprHasProperty(p, EP_TokenOnly) ){
 89701  90720       /* The Expr.x union is never used at the same time as Expr.pRight */
 89702  90721       assert( p->x.pList==0 || p->pRight==0 );
 89703         -    sqlite3ExprDelete(db, p->pLeft);
        90722  +    if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
 89704  90723       sqlite3ExprDelete(db, p->pRight);
 89705  90724       if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 89706  90725       if( ExprHasProperty(p, EP_xIsSelect) ){
 89707  90726         sqlite3SelectDelete(db, p->x.pSelect);
 89708  90727       }else{
 89709  90728         sqlite3ExprListDelete(db, p->x.pList);
 89710  90729       }
................................................................................
 89904  90923                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
 89905  90924         }
 89906  90925         if( pzBuffer ){
 89907  90926           *pzBuffer = zAlloc;
 89908  90927         }
 89909  90928       }else{
 89910  90929         if( !ExprHasProperty(p, EP_TokenOnly) ){
        90930  +        if( pNew->op==TK_SELECT_COLUMN ){
        90931  +          pNew->pLeft = p->pLeft;
        90932  +        }else{
 89911  90933           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
        90934  +        }
 89912  90935           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 89913  90936         }
 89914  90937       }
 89915  90938     }
 89916  90939     return pNew;
 89917  90940   }
 89918  90941   
................................................................................
 90144  91167   
 90145  91168   no_mem:     
 90146  91169     /* Avoid leaking memory if malloc has failed. */
 90147  91170     sqlite3ExprDelete(db, pExpr);
 90148  91171     sqlite3ExprListDelete(db, pList);
 90149  91172     return 0;
 90150  91173   }
        91174  +
        91175  +/*
        91176  +** pColumns and pExpr form a vector assignment which is part of the SET
        91177  +** clause of an UPDATE statement.  Like this:
        91178  +**
        91179  +**        (a,b,c) = (expr1,expr2,expr3)
        91180  +** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
        91181  +**
        91182  +** For each term of the vector assignment, append new entries to the
        91183  +** expression list pList.  In the case of a subquery on the LHS, append
        91184  +** TK_SELECT_COLUMN expressions.
        91185  +*/
        91186  +SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
        91187  +  Parse *pParse,         /* Parsing context */
        91188  +  ExprList *pList,       /* List to which to append. Might be NULL */
        91189  +  IdList *pColumns,      /* List of names of LHS of the assignment */
        91190  +  Expr *pExpr            /* Vector expression to be appended. Might be NULL */
        91191  +){
        91192  +  sqlite3 *db = pParse->db;
        91193  +  int n;
        91194  +  int i;
        91195  +  int iFirst = pList ? pList->nExpr : 0;
        91196  +  /* pColumns can only be NULL due to an OOM but an OOM will cause an
        91197  +  ** exit prior to this routine being invoked */
        91198  +  if( NEVER(pColumns==0) ) goto vector_append_error;
        91199  +  if( pExpr==0 ) goto vector_append_error;
        91200  +  n = sqlite3ExprVectorSize(pExpr);
        91201  +  if( pColumns->nId!=n ){
        91202  +    sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
        91203  +                    pColumns->nId, n);
        91204  +    goto vector_append_error;
        91205  +  }
        91206  +  for(i=0; i<n; i++){
        91207  +    Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
        91208  +    pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
        91209  +    if( pList ){
        91210  +      assert( pList->nExpr==iFirst+i+1 );
        91211  +      pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
        91212  +      pColumns->a[i].zName = 0;
        91213  +    }
        91214  +  }
        91215  +  if( pExpr->op==TK_SELECT ){
        91216  +    if( pList && pList->a[iFirst].pExpr ){
        91217  +      assert( pList->a[iFirst].pExpr->op==TK_SELECT_COLUMN );
        91218  +      pList->a[iFirst].pExpr->pRight = pExpr;
        91219  +      pExpr = 0;
        91220  +    }
        91221  +  }
        91222  +
        91223  +vector_append_error:
        91224  +  sqlite3ExprDelete(db, pExpr);
        91225  +  sqlite3IdListDelete(db, pColumns);
        91226  +  return pList;
        91227  +}
 90151  91228   
 90152  91229   /*
 90153  91230   ** Set the sort order for the last element on the given ExprList.
 90154  91231   */
 90155  91232   SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
 90156  91233     if( p==0 ) return;
 90157  91234     assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
................................................................................
 90552  91629   ** table, then return NULL.
 90553  91630   */
 90554  91631   #ifndef SQLITE_OMIT_SUBQUERY
 90555  91632   static Select *isCandidateForInOpt(Expr *pX){
 90556  91633     Select *p;
 90557  91634     SrcList *pSrc;
 90558  91635     ExprList *pEList;
 90559         -  Expr *pRes;
 90560  91636     Table *pTab;
        91637  +  int i;
 90561  91638     if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0;  /* Not a subquery */
 90562  91639     if( ExprHasProperty(pX, EP_VarSelect)  ) return 0;  /* Correlated subq */
 90563  91640     p = pX->x.pSelect;
 90564  91641     if( p->pPrior ) return 0;              /* Not a compound SELECT */
 90565  91642     if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 90566  91643       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 90567  91644       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
................................................................................
 90576  91653     if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 90577  91654     if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 90578  91655     pTab = pSrc->a[0].pTab;
 90579  91656     assert( pTab!=0 );
 90580  91657     assert( pTab->pSelect==0 );            /* FROM clause is not a view */
 90581  91658     if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
 90582  91659     pEList = p->pEList;
 90583         -  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
 90584         -  pRes = pEList->a[0].pExpr;
 90585         -  if( pRes->op!=TK_COLUMN ) return 0;    /* Result is a column */
        91660  +  assert( pEList!=0 );
        91661  +  /* All SELECT results must be columns. */
        91662  +  for(i=0; i<pEList->nExpr; i++){
        91663  +    Expr *pRes = pEList->a[i].pExpr;
        91664  +    if( pRes->op!=TK_COLUMN ) return 0;
 90586  91665     assert( pRes->iTable==pSrc->a[0].iCursor );  /* Not a correlated subquery */
        91666  +  }
 90587  91667     return p;
 90588  91668   }
 90589  91669   #endif /* SQLITE_OMIT_SUBQUERY */
 90590  91670   
 90591  91671   /*
 90592  91672   ** Code an OP_Once instruction and allocate space for its flag. Return the 
 90593  91673   ** address of the new instruction.
 90594  91674   */
 90595  91675   SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
 90596  91676     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 90597         -  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
        91677  +  return sqlite3VdbeAddOp0(v, OP_Once);
 90598  91678   }
 90599  91679   
        91680  +#ifndef SQLITE_OMIT_SUBQUERY
 90600  91681   /*
 90601  91682   ** Generate code that checks the left-most column of index table iCur to see if
 90602  91683   ** it contains any NULL entries.  Cause the register at regHasNull to be set
 90603  91684   ** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
 90604  91685   ** to be set to NULL if iCur contains one or more NULL values.
 90605  91686   */
 90606  91687   static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
................................................................................
 90608  91689     sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
 90609  91690     addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
 90610  91691     sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
 90611  91692     sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
 90612  91693     VdbeComment((v, "first_entry_in(%d)", iCur));
 90613  91694     sqlite3VdbeJumpHere(v, addr1);
 90614  91695   }
        91696  +#endif
 90615  91697   
 90616  91698   
 90617  91699   #ifndef SQLITE_OMIT_SUBQUERY
 90618  91700   /*
 90619  91701   ** The argument is an IN operator with a list (not a subquery) on the 
 90620  91702   ** right-hand side.  Return TRUE if that list is constant.
 90621  91703   */
................................................................................
 90652  91734   **                         populated epheremal table.
 90653  91735   **   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
 90654  91736   **                         implemented as a sequence of comparisons.
 90655  91737   **
 90656  91738   ** An existing b-tree might be used if the RHS expression pX is a simple
 90657  91739   ** subquery such as:
 90658  91740   **
 90659         -**     SELECT <column> FROM <table>
        91741  +**     SELECT <column1>, <column2>... FROM <table>
 90660  91742   **
 90661  91743   ** If the RHS of the IN operator is a list or a more complex subquery, then
 90662  91744   ** an ephemeral table might need to be generated from the RHS and then
 90663  91745   ** pX->iTable made to point to the ephemeral table instead of an
 90664  91746   ** existing table.
 90665  91747   **
 90666  91748   ** The inFlags parameter must contain exactly one of the bits
................................................................................
 90668  91750   ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
 90669  91751   ** fast membership test.  When the IN_INDEX_LOOP bit is set, the
 90670  91752   ** IN index will be used to loop over all values of the RHS of the
 90671  91753   ** IN operator.
 90672  91754   **
 90673  91755   ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
 90674  91756   ** through the set members) then the b-tree must not contain duplicates.
 90675         -** An epheremal table must be used unless the selected <column> is guaranteed
 90676         -** to be unique - either because it is an INTEGER PRIMARY KEY or it
 90677         -** has a UNIQUE constraint or UNIQUE index.
        91757  +** An epheremal table must be used unless the selected columns are guaranteed
        91758  +** to be unique - either because it is an INTEGER PRIMARY KEY or due to
        91759  +** a UNIQUE constraint or index.
 90678  91760   **
 90679  91761   ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
 90680  91762   ** for fast set membership tests) then an epheremal table must 
 90681         -** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
 90682         -** be found with <column> as its left-most column.
        91763  +** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 
        91764  +** index can be found with the specified <columns> as its left-most.
 90683  91765   **
 90684  91766   ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
 90685  91767   ** if the RHS of the IN operator is a list (not a subquery) then this
 90686  91768   ** routine might decide that creating an ephemeral b-tree for membership
 90687  91769   ** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
 90688  91770   ** calling routine should implement the IN operator using a sequence
 90689  91771   ** of Eq or Ne comparison operations.
................................................................................
 90696  91778   ** to *prRhsHasNull. If there is no chance that the (...) contains a
 90697  91779   ** NULL value, then *prRhsHasNull is left unchanged.
 90698  91780   **
 90699  91781   ** If a register is allocated and its location stored in *prRhsHasNull, then
 90700  91782   ** the value in that register will be NULL if the b-tree contains one or more
 90701  91783   ** NULL values, and it will be some non-NULL value if the b-tree contains no
 90702  91784   ** NULL values.
        91785  +**
        91786  +** If the aiMap parameter is not NULL, it must point to an array containing
        91787  +** one element for each column returned by the SELECT statement on the RHS
        91788  +** of the IN(...) operator. The i'th entry of the array is populated with the
        91789  +** offset of the index column that matches the i'th column returned by the
        91790  +** SELECT. For example, if the expression and selected index are:
        91791  +**
        91792  +**   (?,?,?) IN (SELECT a, b, c FROM t1)
        91793  +**   CREATE INDEX i1 ON t1(b, c, a);
        91794  +**
        91795  +** then aiMap[] is populated with {2, 0, 1}.
 90703  91796   */
 90704  91797   #ifndef SQLITE_OMIT_SUBQUERY
 90705         -SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
        91798  +SQLITE_PRIVATE int sqlite3FindInIndex(
        91799  +  Parse *pParse,             /* Parsing context */
        91800  +  Expr *pX,                  /* The right-hand side (RHS) of the IN operator */
        91801  +  u32 inFlags,               /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
        91802  +  int *prRhsHasNull,         /* Register holding NULL status.  See notes */
        91803  +  int *aiMap                 /* Mapping from Index fields to RHS fields */
        91804  +){
 90706  91805     Select *p;                            /* SELECT to the right of IN operator */
 90707  91806     int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
 90708  91807     int iTab = pParse->nTab++;            /* Cursor of the RHS table */
 90709  91808     int mustBeUnique;                     /* True if RHS must be unique */
 90710  91809     Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
 90711  91810   
 90712  91811     assert( pX->op==TK_IN );
 90713  91812     mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
        91813  +
        91814  +  /* If the RHS of this IN(...) operator is a SELECT, and if it matters 
        91815  +  ** whether or not the SELECT result contains NULL values, check whether
        91816  +  ** or not NULL is actually possible (it may not be, for example, due 
        91817  +  ** to NOT NULL constraints in the schema). If no NULL values are possible,
        91818  +  ** set prRhsHasNull to 0 before continuing.  */
        91819  +  if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
        91820  +    int i;
        91821  +    ExprList *pEList = pX->x.pSelect->pEList;
        91822  +    for(i=0; i<pEList->nExpr; i++){
        91823  +      if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
        91824  +    }
        91825  +    if( i==pEList->nExpr ){
        91826  +      prRhsHasNull = 0;
        91827  +    }
        91828  +  }
 90714  91829   
 90715  91830     /* Check to see if an existing table or index can be used to
 90716  91831     ** satisfy the query.  This is preferable to generating a new 
 90717         -  ** ephemeral table.
 90718         -  */
        91832  +  ** ephemeral table.  */
 90719  91833     if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
 90720  91834       sqlite3 *db = pParse->db;              /* Database connection */
 90721  91835       Table *pTab;                           /* Table <table>. */
 90722         -    Expr *pExpr;                           /* Expression <column> */
 90723         -    i16 iCol;                              /* Index of column <column> */
 90724  91836       i16 iDb;                               /* Database idx for pTab */
        91837  +    ExprList *pEList = p->pEList;
        91838  +    int nExpr = pEList->nExpr;
 90725  91839   
 90726  91840       assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 90727  91841       assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 90728  91842       assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 90729  91843       pTab = p->pSrc->a[0].pTab;
 90730         -    pExpr = p->pEList->a[0].pExpr;
 90731         -    iCol = (i16)pExpr->iColumn;
 90732  91844      
 90733  91845       /* Code an OP_Transaction and OP_TableLock for <table>. */
 90734  91846       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 90735  91847       sqlite3CodeVerifySchema(pParse, iDb);
 90736  91848       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 90737  91849   
 90738         -    /* This function is only called from two places. In both cases the vdbe
 90739         -    ** has already been allocated. So assume sqlite3GetVdbe() is always
 90740         -    ** successful here.
 90741         -    */
 90742         -    assert(v);
 90743         -    if( iCol<0 ){
        91850  +    assert(v);  /* sqlite3GetVdbe() has always been previously called */
        91851  +    if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
        91852  +      /* The "x IN (SELECT rowid FROM table)" case */
 90744  91853         int iAddr = sqlite3CodeOnce(pParse);
 90745  91854         VdbeCoverage(v);
 90746  91855   
 90747  91856         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 90748  91857         eType = IN_INDEX_ROWID;
 90749  91858   
 90750  91859         sqlite3VdbeJumpHere(v, iAddr);
 90751  91860       }else{
 90752  91861         Index *pIdx;                         /* Iterator variable */
 90753         -
 90754         -      /* The collation sequence used by the comparison. If an index is to
 90755         -      ** be used in place of a temp-table, it must be ordered according
 90756         -      ** to this collation sequence.  */
 90757         -      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
 90758         -
 90759         -      /* Check that the affinity that will be used to perform the 
 90760         -      ** comparison is the same as the affinity of the column. If
 90761         -      ** it is not, it is not possible to use any index.
 90762         -      */
 90763         -      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 90764         -
 90765         -      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 90766         -        if( (pIdx->aiColumn[0]==iCol)
 90767         -         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 90768         -         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
 90769         -        ){
        91862  +      int affinity_ok = 1;
        91863  +      int i;
        91864  +
        91865  +      /* Check that the affinity that will be used to perform each 
        91866  +      ** comparison is the same as the affinity of each column in table
        91867  +      ** on the RHS of the IN operator.  If it not, it is not possible to
        91868  +      ** use any index of the RHS table.  */
        91869  +      for(i=0; i<nExpr && affinity_ok; i++){
        91870  +        Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
        91871  +        int iCol = pEList->a[i].pExpr->iColumn;
        91872  +        char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
        91873  +        char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
        91874  +        testcase( cmpaff==SQLITE_AFF_BLOB );
        91875  +        testcase( cmpaff==SQLITE_AFF_TEXT );
        91876  +        switch( cmpaff ){
        91877  +          case SQLITE_AFF_BLOB:
        91878  +            break;
        91879  +          case SQLITE_AFF_TEXT:
        91880  +            /* sqlite3CompareAffinity() only returns TEXT if one side or the
        91881  +            ** other has no affinity and the other side is TEXT.  Hence,
        91882  +            ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
        91883  +            ** and for the term on the LHS of the IN to have no affinity. */
        91884  +            assert( idxaff==SQLITE_AFF_TEXT );
        91885  +            break;
        91886  +          default:
        91887  +            affinity_ok = sqlite3IsNumericAffinity(idxaff);
        91888  +        }
        91889  +      }
        91890  +
        91891  +      if( affinity_ok ){
        91892  +        /* Search for an existing index that will work for this IN operator */
        91893  +        for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
        91894  +          Bitmask colUsed;      /* Columns of the index used */
        91895  +          Bitmask mCol;         /* Mask for the current column */
        91896  +          if( pIdx->nColumn<nExpr ) continue;
        91897  +          /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
        91898  +          ** BITMASK(nExpr) without overflowing */
        91899  +          testcase( pIdx->nColumn==BMS-2 );
        91900  +          testcase( pIdx->nColumn==BMS-1 );
        91901  +          if( pIdx->nColumn>=BMS-1 ) continue;
        91902  +          if( mustBeUnique ){
        91903  +            if( pIdx->nKeyCol>nExpr
        91904  +             ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
        91905  +            ){
        91906  +              continue;  /* This index is not unique over the IN RHS columns */
        91907  +            }
        91908  +          }
        91909  +  
        91910  +          colUsed = 0;   /* Columns of index used so far */
        91911  +          for(i=0; i<nExpr; i++){
        91912  +            Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
        91913  +            Expr *pRhs = pEList->a[i].pExpr;
        91914  +            CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
        91915  +            int j;
        91916  +  
        91917  +            assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
        91918  +            for(j=0; j<nExpr; j++){
        91919  +              if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
        91920  +              assert( pIdx->azColl[j] );
        91921  +              if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
        91922  +                continue;
        91923  +              }
        91924  +              break;
        91925  +            }
        91926  +            if( j==nExpr ) break;
        91927  +            mCol = MASKBIT(j);
        91928  +            if( mCol & colUsed ) break; /* Each column used only once */
        91929  +            colUsed |= mCol;
        91930  +            if( aiMap ) aiMap[i] = j;
        91931  +          }
        91932  +  
        91933  +          assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
        91934  +          if( colUsed==(MASKBIT(nExpr)-1) ){
        91935  +            /* If we reach this point, that means the index pIdx is usable */
 90770  91936             int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
        91937  +#ifndef SQLITE_OMIT_EXPLAIN
        91938  +            sqlite3VdbeAddOp4(v, OP_Explain, 0, 0, 0,
        91939  +              sqlite3MPrintf(db, "USING INDEX %s FOR IN-OPERATOR",pIdx->zName),
        91940  +              P4_DYNAMIC);
        91941  +#endif
 90771  91942             sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 90772  91943             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 90773  91944             VdbeComment((v, "%s", pIdx->zName));
 90774  91945             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 90775  91946             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 90776  91947   
 90777         -          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
        91948  +            if( prRhsHasNull ){
        91949  +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
        91950  +              i64 mask = (1<<nExpr)-1;
        91951  +              sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 
        91952  +                  iTab, 0, 0, (u8*)&mask, P4_INT64);
        91953  +#endif
 90778  91954               *prRhsHasNull = ++pParse->nMem;
        91955  +              if( nExpr==1 ){
 90779  91956               sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
 90780  91957             }
        91958  +            }
 90781  91959             sqlite3VdbeJumpHere(v, iAddr);
 90782  91960           }
 90783         -      }
 90784         -    }
 90785         -  }
        91961  +        } /* End loop over indexes */
        91962  +      } /* End if( affinity_ok ) */
        91963  +    } /* End if not an rowid index */
        91964  +  } /* End attempt to optimize using an index */
 90786  91965   
 90787  91966     /* If no preexisting index is available for the IN clause
 90788  91967     ** and IN_INDEX_NOOP is an allowed reply
 90789  91968     ** and the RHS of the IN operator is a list, not a subquery
 90790         -  ** and the RHS is not contant or has two or fewer terms,
        91969  +  ** and the RHS is not constant or has two or fewer terms,
 90791  91970     ** then it is not worth creating an ephemeral table to evaluate
 90792  91971     ** the IN operator so return IN_INDEX_NOOP.
 90793  91972     */
 90794  91973     if( eType==0
 90795  91974      && (inFlags & IN_INDEX_NOOP_OK)
 90796  91975      && !ExprHasProperty(pX, EP_xIsSelect)
 90797  91976      && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
 90798  91977     ){
 90799  91978       eType = IN_INDEX_NOOP;
 90800  91979     }
 90801  91980        
 90802         -
 90803  91981     if( eType==0 ){
 90804  91982       /* Could not find an existing table or index to use as the RHS b-tree.
 90805  91983       ** We will have to generate an ephemeral table to do the job.
 90806  91984       */
 90807  91985       u32 savedNQueryLoop = pParse->nQueryLoop;
 90808  91986       int rMayHaveNull = 0;
 90809  91987       eType = IN_INDEX_EPH;
................................................................................
 90816  91994         *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
 90817  91995       }
 90818  91996       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 90819  91997       pParse->nQueryLoop = savedNQueryLoop;
 90820  91998     }else{
 90821  91999       pX->iTable = iTab;
 90822  92000     }
        92001  +
        92002  +  if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
        92003  +    int i, n;
        92004  +    n = sqlite3ExprVectorSize(pX->pLeft);
        92005  +    for(i=0; i<n; i++) aiMap[i] = i;
        92006  +  }
 90823  92007     return eType;
 90824  92008   }
 90825  92009   #endif
        92010  +
        92011  +#ifndef SQLITE_OMIT_SUBQUERY
        92012  +/*
        92013  +** Argument pExpr is an (?, ?...) IN(...) expression. This 
        92014  +** function allocates and returns a nul-terminated string containing 
        92015  +** the affinities to be used for each column of the comparison.
        92016  +**
        92017  +** It is the responsibility of the caller to ensure that the returned
        92018  +** string is eventually freed using sqlite3DbFree().
        92019  +*/
        92020  +static char *exprINAffinity(Parse *pParse, Expr *pExpr){
        92021  +  Expr *pLeft = pExpr->pLeft;
        92022  +  int nVal = sqlite3ExprVectorSize(pLeft);
        92023  +  Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
        92024  +  char *zRet;
        92025  +
        92026  +  assert( pExpr->op==TK_IN );
        92027  +  zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
        92028  +  if( zRet ){
        92029  +    int i;
        92030  +    for(i=0; i<nVal; i++){
        92031  +      Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
        92032  +      char a = sqlite3ExprAffinity(pA);
        92033  +      if( pSelect ){
        92034  +        zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
        92035  +      }else{
        92036  +        zRet[i] = a;
        92037  +      }
        92038  +    }
        92039  +    zRet[nVal] = '\0';
        92040  +  }
        92041  +  return zRet;
        92042  +}
        92043  +#endif
        92044  +
        92045  +#ifndef SQLITE_OMIT_SUBQUERY
        92046  +/*
        92047  +** Load the Parse object passed as the first argument with an error 
        92048  +** message of the form:
        92049  +**
        92050  +**   "sub-select returns N columns - expected M"
        92051  +*/   
        92052  +SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
        92053  +  const char *zFmt = "sub-select returns %d columns - expected %d";
        92054  +  sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
        92055  +}
        92056  +#endif
 90826  92057   
 90827  92058   /*
 90828  92059   ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
 90829  92060   ** or IN operators.  Examples:
 90830  92061   **
 90831  92062   **     (SELECT a FROM b)          -- subquery
 90832  92063   **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
................................................................................
 90845  92076   ** If rMayHaveNull is non-zero, that means that the operation is an IN
 90846  92077   ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
 90847  92078   ** All this routine does is initialize the register given by rMayHaveNull
 90848  92079   ** to NULL.  Calling routines will take care of changing this register
 90849  92080   ** value to non-NULL if the RHS is NULL-free.
 90850  92081   **
 90851  92082   ** For a SELECT or EXISTS operator, return the register that holds the
 90852         -** result.  For IN operators or if an error occurs, the return value is 0.
        92083  +** result.  For a multi-column SELECT, the result is stored in a contiguous
        92084  +** array of registers and the return value is the register of the left-most
        92085  +** result column.  Return 0 for IN operators or if an error occurs.
 90853  92086   */
 90854  92087   #ifndef SQLITE_OMIT_SUBQUERY
 90855  92088   SQLITE_PRIVATE int sqlite3CodeSubselect(
 90856  92089     Parse *pParse,          /* Parsing context */
 90857  92090     Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 90858  92091     int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
 90859  92092     int isRowid             /* If true, LHS of IN operator is a rowid */
................................................................................
 90860  92093   ){
 90861  92094     int jmpIfDynamic = -1;                      /* One-time test address */
 90862  92095     int rReg = 0;                           /* Register storing resulting */
 90863  92096     Vdbe *v = sqlite3GetVdbe(pParse);
 90864  92097     if( NEVER(v==0) ) return 0;
 90865  92098     sqlite3ExprCachePush(pParse);
 90866  92099   
 90867         -  /* This code must be run in its entirety every time it is encountered
 90868         -  ** if any of the following is true:
        92100  +  /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
        92101  +  ** is encountered if any of the following is true:
 90869  92102     **
 90870  92103     **    *  The right-hand side is a correlated subquery
 90871  92104     **    *  The right-hand side is an expression list containing variables
 90872  92105     **    *  We are inside a trigger
 90873  92106     **
 90874  92107     ** If all of the above are false, then we can run this code just once
 90875  92108     ** save the results, and reuse the same result on subsequent invocations.
................................................................................
 90887  92120       );
 90888  92121       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 90889  92122     }
 90890  92123   #endif
 90891  92124   
 90892  92125     switch( pExpr->op ){
 90893  92126       case TK_IN: {
 90894         -      char affinity;              /* Affinity of the LHS of the IN */
 90895  92127         int addr;                   /* Address of OP_OpenEphemeral instruction */
 90896  92128         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 90897  92129         KeyInfo *pKeyInfo = 0;      /* Key information */
        92130  +      int nVal;                   /* Size of vector pLeft */
 90898  92131   
 90899         -      affinity = sqlite3ExprAffinity(pLeft);
        92132  +      nVal = sqlite3ExprVectorSize(pLeft);
        92133  +      assert( !isRowid || nVal==1 );
 90900  92134   
 90901  92135         /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
 90902  92136         ** expression it is handled the same way.  An ephemeral table is 
 90903         -      ** filled with single-field index keys representing the results
 90904         -      ** from the SELECT or the <exprlist>.
        92137  +      ** filled with index keys representing the results from the 
        92138  +      ** SELECT or the <exprlist>.
 90905  92139         **
 90906  92140         ** If the 'x' expression is a column value, or the SELECT...
 90907  92141         ** statement returns a column value, then the affinity of that
 90908  92142         ** column is used to build the index keys. If both 'x' and the
 90909  92143         ** SELECT... statement are columns, then numeric affinity is used
 90910  92144         ** if either column has NUMERIC or INTEGER affinity. If neither
 90911  92145         ** 'x' nor the SELECT... statement are columns, then numeric affinity
 90912  92146         ** is used.
 90913  92147         */
 90914  92148         pExpr->iTable = pParse->nTab++;
 90915         -      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 90916         -      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
        92149  +      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 
        92150  +          pExpr->iTable, (isRowid?0:nVal));
        92151  +      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
 90917  92152   
 90918  92153         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 90919  92154           /* Case 1:     expr IN (SELECT ...)
 90920  92155           **
 90921  92156           ** Generate code to write the results of the select into the temporary
 90922  92157           ** table allocated and opened above.
 90923  92158           */
 90924  92159           Select *pSelect = pExpr->x.pSelect;
 90925         -        SelectDest dest;
 90926         -        ExprList *pEList;
        92160  +        ExprList *pEList = pSelect->pEList;
 90927  92161   
 90928  92162           assert( !isRowid );
        92163  +        /* If the LHS and RHS of the IN operator do not match, that
        92164  +        ** error will have been caught long before we reach this point. */
        92165  +        if( ALWAYS(pEList->nExpr==nVal) ){
        92166  +          SelectDest dest;
        92167  +          int i;
 90929  92168           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 90930         -        dest.affSdst = (u8)affinity;
        92169  +          dest.zAffSdst = exprINAffinity(pParse, pExpr);
 90931  92170           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 90932  92171           pSelect->iLimit = 0;
 90933  92172           testcase( pSelect->selFlags & SF_Distinct );
 90934  92173           testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
 90935  92174           if( sqlite3Select(pParse, pSelect, &dest) ){
        92175  +            sqlite3DbFree(pParse->db, dest.zAffSdst);
 90936  92176             sqlite3KeyInfoUnref(pKeyInfo);
 90937  92177             return 0;
 90938  92178           }
 90939         -        pEList = pSelect->pEList;
        92179  +          sqlite3DbFree(pParse->db, dest.zAffSdst);
 90940  92180           assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
 90941  92181           assert( pEList!=0 );
 90942  92182           assert( pEList->nExpr>0 );
 90943  92183           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 90944         -        pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
 90945         -                                                         pEList->a[0].pExpr);
        92184  +          for(i=0; i<nVal; i++){
        92185  +            Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
        92186  +            pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
        92187  +                pParse, p, pEList->a[i].pExpr
        92188  +            );
        92189  +          }
        92190  +        }
 90946  92191         }else if( ALWAYS(pExpr->x.pList!=0) ){
 90947  92192           /* Case 2:     expr IN (exprlist)
 90948  92193           **
 90949  92194           ** For each expression, build an index key from the evaluation and
 90950  92195           ** store it in the temporary table. If <expr> is a column, then use
 90951  92196           ** that columns affinity when building index keys. If <expr> is not
 90952  92197           ** a column, use numeric affinity.
 90953  92198           */
        92199  +        char affinity;            /* Affinity of the LHS of the IN */
 90954  92200           int i;
 90955  92201           ExprList *pList = pExpr->x.pList;
 90956  92202           struct ExprList_item *pItem;
 90957  92203           int r1, r2, r3;
 90958  92204   
        92205  +        affinity = sqlite3ExprAffinity(pLeft);
 90959  92206           if( !affinity ){
 90960  92207             affinity = SQLITE_AFF_BLOB;
 90961  92208           }
 90962  92209           if( pKeyInfo ){
 90963  92210             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 90964  92211             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 90965  92212           }
................................................................................
 91007  92254         }
 91008  92255         break;
 91009  92256       }
 91010  92257   
 91011  92258       case TK_EXISTS:
 91012  92259       case TK_SELECT:
 91013  92260       default: {
 91014         -      /* If this has to be a scalar SELECT.  Generate code to put the
 91015         -      ** value of this select in a memory cell and record the number
 91016         -      ** of the memory cell in iColumn.  If this is an EXISTS, write
 91017         -      ** an integer 0 (not exists) or 1 (exists) into a memory cell
 91018         -      ** and record that memory cell in iColumn.
        92261  +      /* Case 3:    (SELECT ... FROM ...)
        92262  +      **     or:    EXISTS(SELECT ... FROM ...)
        92263  +      **
        92264  +      ** For a SELECT, generate code to put the values for all columns of
        92265  +      ** the first row into an array of registers and return the index of
        92266  +      ** the first register.
        92267  +      **
        92268  +      ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
        92269  +      ** into a register and return that register number.
        92270  +      **
        92271  +      ** In both cases, the query is augmented with "LIMIT 1".  Any 
        92272  +      ** preexisting limit is discarded in place of the new LIMIT 1.
 91019  92273         */
 91020  92274         Select *pSel;                         /* SELECT statement to encode */
 91021         -      SelectDest dest;                      /* How to deal with SELECt result */
        92275  +      SelectDest dest;                      /* How to deal with SELECT result */
        92276  +      int nReg;                             /* Registers to allocate */
 91022  92277   
 91023  92278         testcase( pExpr->op==TK_EXISTS );
 91024  92279         testcase( pExpr->op==TK_SELECT );
 91025  92280         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 91026         -
 91027  92281         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
        92282  +
 91028  92283         pSel = pExpr->x.pSelect;
 91029         -      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
        92284  +      nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
        92285  +      sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
        92286  +      pParse->nMem += nReg;
 91030  92287         if( pExpr->op==TK_SELECT ){
 91031  92288           dest.eDest = SRT_Mem;
 91032  92289           dest.iSdst = dest.iSDParm;
 91033         -        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
        92290  +        dest.nSdst = nReg;
        92291  +        sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
 91034  92292           VdbeComment((v, "Init subquery result"));
 91035  92293         }else{
 91036  92294           dest.eDest = SRT_Exists;
 91037  92295           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 91038  92296           VdbeComment((v, "Init EXISTS result"));
 91039  92297         }
 91040  92298         sqlite3ExprDelete(pParse->db, pSel->pLimit);
................................................................................
 91059  92317       sqlite3VdbeJumpHere(v, jmpIfDynamic);
 91060  92318     }
 91061  92319     sqlite3ExprCachePop(pParse);
 91062  92320   
 91063  92321     return rReg;
 91064  92322   }
 91065  92323   #endif /* SQLITE_OMIT_SUBQUERY */
        92324  +
        92325  +#ifndef SQLITE_OMIT_SUBQUERY
        92326  +/*
        92327  +** Expr pIn is an IN(...) expression. This function checks that the 
        92328  +** sub-select on the RHS of the IN() operator has the same number of 
        92329  +** columns as the vector on the LHS. Or, if the RHS of the IN() is not 
        92330  +** a sub-query, that the LHS is a vector of size 1.
        92331  +*/
        92332  +SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
        92333  +  int nVector = sqlite3ExprVectorSize(pIn->pLeft);
        92334  +  if( (pIn->flags & EP_xIsSelect) ){
        92335  +    if( nVector!=pIn->x.pSelect->pEList->nExpr ){
        92336  +      sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
        92337  +      return 1;
        92338  +    }
        92339  +  }else if( nVector!=1 ){
        92340  +    if( (pIn->pLeft->flags & EP_xIsSelect) ){
        92341  +      sqlite3SubselectError(pParse, nVector, 1);
        92342  +    }else{
        92343  +      sqlite3ErrorMsg(pParse, "row value misused");
        92344  +    }
        92345  +    return 1;
        92346  +  }
        92347  +  return 0;
        92348  +}
        92349  +#endif
 91066  92350   
 91067  92351   #ifndef SQLITE_OMIT_SUBQUERY
 91068  92352   /*
 91069  92353   ** Generate code for an IN expression.
 91070  92354   **
 91071  92355   **      x IN (SELECT ...)
 91072  92356   **      x IN (value, value, ...)
 91073  92357   **
 91074         -** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
 91075         -** is an array of zero or more values.  The expression is true if the LHS is
 91076         -** contained within the RHS.  The value of the expression is unknown (NULL)
 91077         -** if the LHS is NULL or if the LHS is not contained within the RHS and the
 91078         -** RHS contains one or more NULL values.
        92358  +** The left-hand side (LHS) is a scalar or vector expression.  The 
        92359  +** right-hand side (RHS) is an array of zero or more scalar values, or a
        92360  +** subquery.  If the RHS is a subquery, the number of result columns must
        92361  +** match the number of columns in the vector on the LHS.  If the RHS is
        92362  +** a list of values, the LHS must be a scalar. 
        92363  +**
        92364  +** The IN operator is true if the LHS value is contained within the RHS.
        92365  +** The result is false if the LHS is definitely not in the RHS.  The 
        92366  +** result is NULL if the presence of the LHS in the RHS cannot be 
        92367  +** determined due to NULLs.
 91079  92368   **
 91080  92369   ** This routine generates code that jumps to destIfFalse if the LHS is not 
 91081  92370   ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
 91082  92371   ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
 91083  92372   ** within the RHS then fall through.
        92373  +**
        92374  +** See the separate in-operator.md documentation file in the canonical
        92375  +** SQLite source tree for additional information.
 91084  92376   */
 91085  92377   static void sqlite3ExprCodeIN(
 91086  92378     Parse *pParse,        /* Parsing and code generating context */
 91087  92379     Expr *pExpr,          /* The IN expression */
 91088  92380     int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
 91089  92381     int destIfNull        /* Jump here if the results are unknown due to NULLs */
 91090  92382   ){
 91091  92383     int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
 91092         -  char affinity;        /* Comparison affinity to use */
 91093  92384     int eType;            /* Type of the RHS */
 91094         -  int r1;               /* Temporary use register */
        92385  +  int rLhs;             /* Register(s) holding the LHS values */
        92386  +  int rLhsOrig;         /* LHS values prior to reordering by aiMap[] */
 91095  92387     Vdbe *v;              /* Statement under construction */
        92388  +  int *aiMap = 0;       /* Map from vector field to index column */
        92389  +  char *zAff = 0;       /* Affinity string for comparisons */
        92390  +  int nVector;          /* Size of vectors for this IN operator */
        92391  +  int iDummy;           /* Dummy parameter to exprCodeVector() */
        92392  +  Expr *pLeft;          /* The LHS of the IN operator */
        92393  +  int i;                /* loop counter */
        92394  +  int destStep2;        /* Where to jump when NULLs seen in step 2 */
        92395  +  int destStep6 = 0;    /* Start of code for Step 6 */
        92396  +  int addrTruthOp;      /* Address of opcode that determines the IN is true */
        92397  +  int destNotNull;      /* Jump here if a comparison is not true in step 6 */
        92398  +  int addrTop;          /* Top of the step-6 loop */ 
 91096  92399   
 91097         -  /* Compute the RHS.   After this step, the table with cursor
 91098         -  ** pExpr->iTable will contains the values that make up the RHS.
 91099         -  */
        92400  +  pLeft = pExpr->pLeft;
        92401  +  if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
        92402  +  zAff = exprINAffinity(pParse, pExpr);
        92403  +  nVector = sqlite3ExprVectorSize(pExpr->pLeft);
        92404  +  aiMap = (int*)sqlite3DbMallocZero(
        92405  +      pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
        92406  +  );
        92407  +  if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
        92408  +
        92409  +  /* Attempt to compute the RHS. After this step, if anything other than
        92410  +  ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable 
        92411  +  ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
        92412  +  ** the RHS has not yet been coded.  */
 91100  92413     v = pParse->pVdbe;
 91101  92414     assert( v!=0 );       /* OOM detected prior to this routine */
 91102  92415     VdbeNoopComment((v, "begin IN expr"));
 91103  92416     eType = sqlite3FindInIndex(pParse, pExpr,
 91104  92417                                IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
 91105         -                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
        92418  +                             destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
 91106  92419   
 91107         -  /* Figure out the affinity to use to create a key from the results
 91108         -  ** of the expression. affinityStr stores a static string suitable for
 91109         -  ** P4 of OP_MakeRecord.
 91110         -  */
 91111         -  affinity = comparisonAffinity(pExpr);
        92420  +  assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
        92421  +       || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 
        92422  +  );
        92423  +#ifdef SQLITE_DEBUG
        92424  +  /* Confirm that aiMap[] contains nVector integer values between 0 and
        92425  +  ** nVector-1. */
        92426  +  for(i=0; i<nVector; i++){
        92427  +    int j, cnt;
        92428  +    for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
        92429  +    assert( cnt==1 );
        92430  +  }
        92431  +#endif
 91112  92432   
 91113         -  /* Code the LHS, the <expr> from "<expr> IN (...)".
        92433  +  /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 
        92434  +  ** vector, then it is stored in an array of nVector registers starting 
        92435  +  ** at r1.
        92436  +  **
        92437  +  ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
        92438  +  ** so that the fields are in the same order as an existing index.   The
        92439  +  ** aiMap[] array contains a mapping from the original LHS field order to
        92440  +  ** the field order that matches the RHS index.
 91114  92441     */
 91115  92442     sqlite3ExprCachePush(pParse);
 91116         -  r1 = sqlite3GetTempReg(pParse);
 91117         -  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
        92443  +  rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
        92444  +  for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
        92445  +  if( i==nVector ){
        92446  +    /* LHS fields are not reordered */
        92447  +    rLhs = rLhsOrig;
        92448  +  }else{
        92449  +    /* Need to reorder the LHS fields according to aiMap */
        92450  +    rLhs = sqlite3GetTempRange(pParse, nVector);
        92451  +    for(i=0; i<nVector; i++){
        92452  +      sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
        92453  +    }
        92454  +  }
 91118  92455   
 91119  92456     /* If sqlite3FindInIndex() did not find or create an index that is
 91120  92457     ** suitable for evaluating the IN operator, then evaluate using a
 91121  92458     ** sequence of comparisons.
        92459  +  **
        92460  +  ** This is step (1) in the in-operator.md optimized algorithm.
 91122  92461     */
 91123  92462     if( eType==IN_INDEX_NOOP ){
 91124  92463       ExprList *pList = pExpr->x.pList;
 91125  92464       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 91126  92465       int labelOk = sqlite3VdbeMakeLabel(v);
 91127  92466       int r2, regToFree;
 91128  92467       int regCkNull = 0;
 91129  92468       int ii;
 91130  92469       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 91131  92470       if( destIfNull!=destIfFalse ){
 91132  92471         regCkNull = sqlite3GetTempReg(pParse);
 91133         -      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
        92472  +      sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
 91134  92473       }
 91135  92474       for(ii=0; ii<pList->nExpr; ii++){
 91136  92475         r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
 91137  92476         if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
 91138  92477           sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
 91139  92478         }
 91140  92479         if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
 91141         -        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
        92480  +        sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
 91142  92481                             (void*)pColl, P4_COLLSEQ);
 91143  92482           VdbeCoverageIf(v, ii<pList->nExpr-1);
 91144  92483           VdbeCoverageIf(v, ii==pList->nExpr-1);
 91145         -        sqlite3VdbeChangeP5(v, affinity);
        92484  +        sqlite3VdbeChangeP5(v, zAff[0]);
 91146  92485         }else{
 91147  92486           assert( destIfNull==destIfFalse );
 91148         -        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
        92487  +        sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
 91149  92488                             (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
 91150         -        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
        92489  +        sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
 91151  92490         }
 91152  92491         sqlite3ReleaseTempReg(pParse, regToFree);
 91153  92492       }
 91154  92493       if( regCkNull ){
 91155  92494         sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
 91156  92495         sqlite3VdbeGoto(v, destIfFalse);
 91157  92496       }
 91158  92497       sqlite3VdbeResolveLabel(v, labelOk);
 91159  92498       sqlite3ReleaseTempReg(pParse, regCkNull);
 91160         -  }else{
        92499  +    goto sqlite3ExprCodeIN_finished;
        92500  +  }
 91161  92501     
 91162         -    /* If the LHS is NULL, then the result is either false or NULL depending
 91163         -    ** on whether the RHS is empty or not, respectively.
        92502  +  /* Step 2: Check to see if the LHS contains any NULL columns.  If the
        92503  +  ** LHS does contain NULLs then the result must be either FALSE or NULL.
        92504  +  ** We will then skip the binary search of the RHS.
 91164  92505       */
 91165         -    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
 91166  92506         if( destIfNull==destIfFalse ){
 91167         -        /* Shortcut for the common case where the false and NULL outcomes are
 91168         -        ** the same. */
 91169         -        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
        92507  +    destStep2 = destIfFalse;
 91170  92508         }else{
 91171         -        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
 91172         -        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
        92509  +    destStep2 = destStep6 = sqlite3VdbeMakeLabel(v);
        92510  +  }
        92511  +  for(i=0; i<nVector; i++){
        92512  +    Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
        92513  +    if( sqlite3ExprCanBeNull(p) ){
        92514  +      sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
 91173  92515           VdbeCoverage(v);
 91174         -        sqlite3VdbeGoto(v, destIfNull);
 91175         -        sqlite3VdbeJumpHere(v, addr1);
 91176  92516         }
 91177  92517       }
 91178  92518     
        92519  +  /* Step 3.  The LHS is now known to be non-NULL.  Do the binary search
        92520  +  ** of the RHS using the LHS as a probe.  If found, the result is
        92521  +  ** true.
        92522  +  */
 91179  92523       if( eType==IN_INDEX_ROWID ){
 91180         -      /* In this case, the RHS is the ROWID of table b-tree
 91181         -      */
 91182         -      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
 91183         -      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
 91184         -      VdbeCoverage(v);
 91185         -    }else{
 91186         -      /* In this case, the RHS is an index b-tree.
 91187         -      */
 91188         -      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 91189         -  
 91190         -      /* If the set membership test fails, then the result of the 
 91191         -      ** "x IN (...)" expression must be either 0 or NULL. If the set
 91192         -      ** contains no NULL values, then the result is 0. If the set 
 91193         -      ** contains one or more NULL values, then the result of the
 91194         -      ** expression is also NULL.
 91195         -      */
 91196         -      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
 91197         -      if( rRhsHasNull==0 ){
 91198         -        /* This branch runs if it is known at compile time that the RHS
 91199         -        ** cannot contain NULL values. This happens as the result
 91200         -        ** of a "NOT NULL" constraint in the database schema.
 91201         -        **
 91202         -        ** Also run this branch if NULL is equivalent to FALSE
 91203         -        ** for this particular IN operator.
 91204         -        */
 91205         -        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 91206         -        VdbeCoverage(v);
 91207         -      }else{
 91208         -        /* In this branch, the RHS of the IN might contain a NULL and
 91209         -        ** the presence of a NULL on the RHS makes a difference in the
 91210         -        ** outcome.
 91211         -        */
 91212         -        int addr1;
 91213         -  
 91214         -        /* First check to see if the LHS is contained in the RHS.  If so,
 91215         -        ** then the answer is TRUE the presence of NULLs in the RHS does
 91216         -        ** not matter.  If the LHS is not contained in the RHS, then the
 91217         -        ** answer is NULL if the RHS contains NULLs and the answer is
 91218         -        ** FALSE if the RHS is NULL-free.
 91219         -        */
 91220         -        addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
 91221         -        VdbeCoverage(v);
 91222         -        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
 91223         -        VdbeCoverage(v);
 91224         -        sqlite3VdbeGoto(v, destIfFalse);
 91225         -        sqlite3VdbeJumpHere(v, addr1);
 91226         -      }
 91227         -    }
 91228         -  }
 91229         -  sqlite3ReleaseTempReg(pParse, r1);
        92524  +    /* In this case, the RHS is the ROWID of table b-tree and so we also
        92525  +    ** know that the RHS is non-NULL.  Hence, we combine steps 3 and 4
        92526  +    ** into a single opcode. */
        92527  +    sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
        92528  +    VdbeCoverage(v);
        92529  +    addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto);  /* Return True */
        92530  +  }else{
        92531  +    sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
        92532  +    if( destIfFalse==destIfNull ){
        92533  +      /* Combine Step 3 and Step 5 into a single opcode */
        92534  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
        92535  +                           rLhs, nVector); VdbeCoverage(v);
        92536  +      goto sqlite3ExprCodeIN_finished;
        92537  +    }
        92538  +    /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
        92539  +    addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
        92540  +                                      rLhs, nVector); VdbeCoverage(v);
        92541  +  }
        92542  +
        92543  +  /* Step 4.  If the RHS is known to be non-NULL and we did not find
        92544  +  ** an match on the search above, then the result must be FALSE.
        92545  +  */
        92546  +  if( rRhsHasNull && nVector==1 ){
        92547  +    sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
        92548  +    VdbeCoverage(v);
        92549  +  }
        92550  +
        92551  +  /* Step 5.  If we do not care about the difference between NULL and
        92552  +  ** FALSE, then just return false. 
        92553  +  */
        92554  +  if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
        92555  +
        92556  +  /* Step 6: Loop through rows of the RHS.  Compare each row to the LHS.
        92557  +  ** If any comparison is NULL, then the result is NULL.  If all
        92558  +  ** comparisons are FALSE then the final result is FALSE.
        92559  +  **
        92560  +  ** For a scalar LHS, it is sufficient to check just the first row
        92561  +  ** of the RHS.
        92562  +  */
        92563  +  if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
        92564  +  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
        92565  +  VdbeCoverage(v);
        92566  +  if( nVector>1 ){
        92567  +    destNotNull = sqlite3VdbeMakeLabel(v);
        92568  +  }else{
        92569  +    /* For nVector==1, combine steps 6 and 7 by immediately returning
        92570  +    ** FALSE if the first comparison is not NULL */
        92571  +    destNotNull = destIfFalse;
        92572  +  }
        92573  +  for(i=0; i<nVector; i++){
        92574  +    Expr *p;
        92575  +    CollSeq *pColl;
        92576  +    int r3 = sqlite3GetTempReg(pParse);
        92577  +    p = sqlite3VectorFieldSubexpr(pLeft, i);
        92578  +    pColl = sqlite3ExprCollSeq(pParse, p);
        92579  +    sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
        92580  +    sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
        92581  +                      (void*)pColl, P4_COLLSEQ);
        92582  +    VdbeCoverage(v);
        92583  +    sqlite3ReleaseTempReg(pParse, r3);
        92584  +  }
        92585  +  sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
        92586  +  if( nVector>1 ){
        92587  +    sqlite3VdbeResolveLabel(v, destNotNull);
        92588  +    sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
        92589  +    VdbeCoverage(v);
        92590  +
        92591  +    /* Step 7:  If we reach this point, we know that the result must
        92592  +    ** be false. */
        92593  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
        92594  +  }
        92595  +
        92596  +  /* Jumps here in order to return true. */
        92597  +  sqlite3VdbeJumpHere(v, addrTruthOp);
        92598  +
        92599  +sqlite3ExprCodeIN_finished:
        92600  +  if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
 91230  92601     sqlite3ExprCachePop(pParse);
 91231  92602     VdbeComment((v, "end IN expr"));
        92603  +sqlite3ExprCodeIN_oom_error:
        92604  +  sqlite3DbFree(pParse->db, aiMap);
        92605  +  sqlite3DbFree(pParse->db, zAff);
 91232  92606   }
 91233  92607   #endif /* SQLITE_OMIT_SUBQUERY */
 91234  92608   
 91235  92609   #ifndef SQLITE_OMIT_FLOATING_POINT
 91236  92610   /*
 91237  92611   ** Generate an instruction that will put the floating point
 91238  92612   ** value described by z[0..n-1] into register iMem.
................................................................................
 91489  92863     int regOut      /* Extract the value into this register */
 91490  92864   ){
 91491  92865     if( iCol<0 || iCol==pTab->iPKey ){
 91492  92866       sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
 91493  92867     }else{
 91494  92868       int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
 91495  92869       int x = iCol;
 91496         -    if( !HasRowid(pTab) ){
        92870  +    if( !HasRowid(pTab) && !IsVirtual(pTab) ){
 91497  92871         x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
 91498  92872       }
 91499  92873       sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
 91500  92874     }
 91501  92875     if( iCol>=0 ){
 91502  92876       sqlite3ColumnDefault(v, pTab, iCol, regOut);
 91503  92877     }
................................................................................
 91609  92983     }
 91610  92984     return 0;
 91611  92985   }
 91612  92986   #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
 91613  92987   
 91614  92988   
 91615  92989   /*
 91616         -** Convert an expression node to a TK_REGISTER
        92990  +** Convert a scalar expression node to a TK_REGISTER referencing
        92991  +** register iReg.  The caller must ensure that iReg already contains
        92992  +** the correct value for the expression.
 91617  92993   */
 91618  92994   static void exprToRegister(Expr *p, int iReg){
 91619  92995     p->op2 = p->op;
 91620  92996     p->op = TK_REGISTER;
 91621  92997     p->iTable = iReg;
 91622  92998     ExprClearProperty(p, EP_Skip);
 91623  92999   }
        93000  +
        93001  +/*
        93002  +** Evaluate an expression (either a vector or a scalar expression) and store
        93003  +** the result in continguous temporary registers.  Return the index of
        93004  +** the first register used to store the result.
        93005  +**
        93006  +** If the returned result register is a temporary scalar, then also write
        93007  +** that register number into *piFreeable.  If the returned result register
        93008  +** is not a temporary or if the expression is a vector set *piFreeable
        93009  +** to 0.
        93010  +*/
        93011  +static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
        93012  +  int iResult;
        93013  +  int nResult = sqlite3ExprVectorSize(p);
        93014  +  if( nResult==1 ){
        93015  +    iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
        93016  +  }else{
        93017  +    *piFreeable = 0;
        93018  +    if( p->op==TK_SELECT ){
        93019  +      iResult = sqlite3CodeSubselect(pParse, p, 0, 0);
        93020  +    }else{
        93021  +      int i;
        93022  +      iResult = pParse->nMem+1;
        93023  +      pParse->nMem += nResult;
        93024  +      for(i=0; i<nResult; i++){
        93025  +        sqlite3ExprCode(pParse, p->x.pList->a[i].pExpr, i+iResult);
        93026  +      }
        93027  +    }
        93028  +  }
        93029  +  return iResult;
        93030  +}
        93031  +
 91624  93032   
 91625  93033   /*
 91626  93034   ** Generate code into the current Vdbe to evaluate the given
 91627  93035   ** expression.  Attempt to store the results in register "target".
 91628  93036   ** Return the register where results are stored.
 91629  93037   **
 91630  93038   ** With this routine, there is no guarantee that results will
................................................................................
 91635  93043   */
 91636  93044   SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
 91637  93045     Vdbe *v = pParse->pVdbe;  /* The VM under construction */
 91638  93046     int op;                   /* The opcode being coded */
 91639  93047     int inReg = target;       /* Results stored in register inReg */
 91640  93048     int regFree1 = 0;         /* If non-zero free this temporary register */
 91641  93049     int regFree2 = 0;         /* If non-zero free this temporary register */
 91642         -  int r1, r2, r3, r4;       /* Various register numbers */
 91643         -  sqlite3 *db = pParse->db; /* The database connection */
        93050  +  int r1, r2;               /* Various register numbers */
 91644  93051     Expr tempX;               /* Temporary expression node */
        93052  +  int p5 = 0;
 91645  93053   
 91646  93054     assert( target>0 && target<=pParse->nMem );
 91647  93055     if( v==0 ){
 91648  93056       assert( pParse->db->mallocFailed );
 91649  93057       return 0;
 91650  93058     }
 91651  93059   
................................................................................
 91656  93064     }
 91657  93065     switch( op ){
 91658  93066       case TK_AGG_COLUMN: {
 91659  93067         AggInfo *pAggInfo = pExpr->pAggInfo;
 91660  93068         struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 91661  93069         if( !pAggInfo->directMode ){
 91662  93070           assert( pCol->iMem>0 );
 91663         -        inReg = pCol->iMem;
 91664         -        break;
        93071  +        return pCol->iMem;
 91665  93072         }else if( pAggInfo->useSortingIdx ){
 91666  93073           sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 91667  93074                                 pCol->iSorterColumn, target);
 91668         -        break;
        93075  +        return target;
 91669  93076         }
 91670  93077         /* Otherwise, fall thru into the TK_COLUMN case */
 91671  93078       }
 91672  93079       case TK_COLUMN: {
 91673  93080         int iTab = pExpr->iTable;
 91674  93081         if( iTab<0 ){
 91675  93082           if( pParse->ckBase>0 ){
 91676  93083             /* Generating CHECK constraints or inserting into partial index */
 91677         -          inReg = pExpr->iColumn + pParse->ckBase;
 91678         -          break;
        93084  +          return pExpr->iColumn + pParse->ckBase;
 91679  93085           }else{
 91680  93086             /* Coding an expression that is part of an index where column names
 91681  93087             ** in the index refer to the table to which the index belongs */
 91682  93088             iTab = pParse->iSelfTab;
 91683  93089           }
 91684  93090         }
 91685         -      inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
        93091  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 91686  93092                                  pExpr->iColumn, iTab, target,
 91687  93093                                  pExpr->op2);
 91688         -      break;
 91689  93094       }
 91690  93095       case TK_INTEGER: {
 91691  93096         codeInteger(pParse, pExpr, 0, target);
 91692         -      break;
        93097  +      return target;
 91693  93098       }
 91694  93099   #ifndef SQLITE_OMIT_FLOATING_POINT
 91695  93100       case TK_FLOAT: {
 91696  93101         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91697  93102         codeReal(v, pExpr->u.zToken, 0, target);
 91698         -      break;
        93103  +      return target;
 91699  93104       }
 91700  93105   #endif
 91701  93106       case TK_STRING: {
 91702  93107         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91703  93108         sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
 91704         -      break;
        93109  +      return target;
 91705  93110       }
 91706  93111       case TK_NULL: {
 91707  93112         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 91708         -      break;
        93113  +      return target;
 91709  93114       }
 91710  93115   #ifndef SQLITE_OMIT_BLOB_LITERAL
 91711  93116       case TK_BLOB: {
 91712  93117         int n;
 91713  93118         const char *z;
 91714  93119         char *zBlob;
 91715  93120         assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
 91716  93121         assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 91717  93122         assert( pExpr->u.zToken[1]=='\'' );
 91718  93123         z = &pExpr->u.zToken[2];
 91719  93124         n = sqlite3Strlen30(z) - 1;
 91720  93125         assert( z[n]=='\'' );
 91721  93126         zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
 91722  93127         sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
 91723         -      break;
        93128  +      return target;
 91724  93129       }
 91725  93130   #endif
 91726  93131       case TK_VARIABLE: {
 91727  93132         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91728  93133         assert( pExpr->u.zToken!=0 );
 91729  93134         assert( pExpr->u.zToken[0]!=0 );
 91730  93135         sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 91731  93136         if( pExpr->u.zToken[1]!=0 ){
 91732  93137           assert( pExpr->u.zToken[0]=='?' 
 91733  93138                || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
 91734  93139           sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
 91735  93140         }
 91736         -      break;
        93141  +      return target;
 91737  93142       }
 91738  93143       case TK_REGISTER: {
 91739         -      inReg = pExpr->iTable;
 91740         -      break;
        93144  +      return pExpr->iTable;
 91741  93145       }
 91742  93146   #ifndef SQLITE_OMIT_CAST
 91743  93147       case TK_CAST: {
 91744  93148         /* Expressions of the form:   CAST(pLeft AS token) */
 91745  93149         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 91746  93150         if( inReg!=target ){
 91747  93151           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
 91748  93152           inReg = target;
 91749  93153         }
 91750  93154         sqlite3VdbeAddOp2(v, OP_Cast, target,
 91751  93155                           sqlite3AffinityType(pExpr->u.zToken, 0));
 91752  93156         testcase( usedAsColumnCache(pParse, inReg, inReg) );
 91753  93157         sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
 91754         -      break;
        93158  +      return inReg;
 91755  93159       }
 91756  93160   #endif /* SQLITE_OMIT_CAST */
        93161  +    case TK_IS:
        93162  +    case TK_ISNOT:
        93163  +      op = (op==TK_IS) ? TK_EQ : TK_NE;
        93164  +      p5 = SQLITE_NULLEQ;
        93165  +      /* fall-through */
 91757  93166       case TK_LT:
 91758  93167       case TK_LE:
 91759  93168       case TK_GT:
 91760  93169       case TK_GE:
 91761  93170       case TK_NE:
 91762  93171       case TK_EQ: {
 91763         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
        93172  +      Expr *pLeft = pExpr->pLeft;
        93173  +      if( sqlite3ExprIsVector(pLeft) ){
        93174  +        codeVectorCompare(pParse, pExpr, target, op, p5);
        93175  +      }else{
        93176  +        r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 91764  93177         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 91765         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 91766         -                  r1, r2, inReg, SQLITE_STOREP2);
        93178  +        codeCompare(pParse, pLeft, pExpr->pRight, op,
        93179  +            r1, r2, inReg, SQLITE_STOREP2 | p5);
 91767  93180         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 91768  93181         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
 91769  93182         assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
 91770  93183         assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
 91771  93184         assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
 91772  93185         assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 91773  93186         testcase( regFree1==0 );
 91774  93187         testcase( regFree2==0 );
 91775         -      break;
 91776  93188       }
 91777         -    case TK_IS:
 91778         -    case TK_ISNOT: {
 91779         -      testcase( op==TK_IS );
 91780         -      testcase( op==TK_ISNOT );
 91781         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 91782         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 91783         -      op = (op==TK_IS) ? TK_EQ : TK_NE;
 91784         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 91785         -                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
 91786         -      VdbeCoverageIf(v, op==TK_EQ);
 91787         -      VdbeCoverageIf(v, op==TK_NE);
 91788         -      testcase( regFree1==0 );
 91789         -      testcase( regFree2==0 );
 91790  93189         break;
 91791  93190       }
 91792  93191       case TK_AND:
 91793  93192       case TK_OR:
 91794  93193       case TK_PLUS:
 91795  93194       case TK_STAR:
 91796  93195       case TK_MINUS:
................................................................................
 91820  93219         break;
 91821  93220       }
 91822  93221       case TK_UMINUS: {
 91823  93222         Expr *pLeft = pExpr->pLeft;
 91824  93223         assert( pLeft );
 91825  93224         if( pLeft->op==TK_INTEGER ){
 91826  93225           codeInteger(pParse, pLeft, 1, target);
        93226  +        return target;
 91827  93227   #ifndef SQLITE_OMIT_FLOATING_POINT
 91828  93228         }else if( pLeft->op==TK_FLOAT ){
 91829  93229           assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91830  93230           codeReal(v, pLeft->u.zToken, 1, target);
        93231  +        return target;
 91831  93232   #endif
 91832  93233         }else{
 91833  93234           tempX.op = TK_INTEGER;
 91834  93235           tempX.flags = EP_IntValue|EP_TokenOnly;
 91835  93236           tempX.u.iValue = 0;
 91836  93237           r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
 91837  93238           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
 91838  93239           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
 91839  93240           testcase( regFree2==0 );
 91840  93241         }
 91841         -      inReg = target;
 91842  93242         break;
 91843  93243       }
 91844  93244       case TK_BITNOT:
 91845  93245       case TK_NOT: {
 91846  93246         assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 91847  93247         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 91848  93248         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 91849  93249         testcase( regFree1==0 );
 91850         -      inReg = target;
 91851  93250         sqlite3VdbeAddOp2(v, op, r1, inReg);
 91852  93251         break;
 91853  93252       }
 91854  93253       case TK_ISNULL:
 91855  93254       case TK_NOTNULL: {
 91856  93255         int addr;
 91857  93256         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
................................................................................
 91868  93267       }
 91869  93268       case TK_AGG_FUNCTION: {
 91870  93269         AggInfo *pInfo = pExpr->pAggInfo;
 91871  93270         if( pInfo==0 ){
 91872  93271           assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91873  93272           sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
 91874  93273         }else{
 91875         -        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
        93274  +        return pInfo->aFunc[pExpr->iAgg].iMem;
 91876  93275         }
 91877  93276         break;
 91878  93277       }
 91879  93278       case TK_FUNCTION: {
 91880  93279         ExprList *pFarg;       /* List of function arguments */
 91881  93280         int nFarg;             /* Number of function arguments */
 91882  93281         FuncDef *pDef;         /* The function definition object */
 91883  93282         const char *zId;       /* The function name */
 91884  93283         u32 constMask = 0;     /* Mask of function arguments that are constant */
 91885  93284         int i;                 /* Loop counter */
        93285  +      sqlite3 *db = pParse->db;  /* The database connection */
 91886  93286         u8 enc = ENC(db);      /* The text encoding used by this database */
 91887  93287         CollSeq *pColl = 0;    /* A collating sequence */
 91888  93288   
 91889  93289         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 91890  93290         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 91891  93291           pFarg = 0;
 91892  93292         }else{
 91893  93293           pFarg = pExpr->x.pList;
 91894  93294         }
 91895  93295         nFarg = pFarg ? pFarg->nExpr : 0;
 91896  93296         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 91897  93297         zId = pExpr->u.zToken;
 91898  93298         pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
        93299  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
        93300  +      if( pDef==0 && pParse->explain ){
        93301  +        pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
        93302  +      }
        93303  +#endif
 91899  93304         if( pDef==0 || pDef->xFinalize!=0 ){
 91900  93305           sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
 91901  93306           break;
 91902  93307         }
 91903  93308   
 91904  93309         /* Attempt a direct implementation of the built-in COALESCE() and
 91905  93310         ** IFNULL() functions.  This avoids unnecessary evaluation of
................................................................................
 91922  93327         }
 91923  93328   
 91924  93329         /* The UNLIKELY() function is a no-op.  The result is the value
 91925  93330         ** of the first argument.
 91926  93331         */
 91927  93332         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 91928  93333           assert( nFarg>=1 );
 91929         -        inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 91930         -        break;
        93334  +        return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
 91931  93335         }
 91932  93336   
 91933  93337         for(i=0; i<nFarg; i++){
 91934  93338           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 91935  93339             testcase( i==31 );
 91936  93340             constMask |= MASKBIT32(i);
 91937  93341           }
................................................................................
 91998  93402         }
 91999  93403         sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
 92000  93404                           (char*)pDef, P4_FUNCDEF);
 92001  93405         sqlite3VdbeChangeP5(v, (u8)nFarg);
 92002  93406         if( nFarg && constMask==0 ){
 92003  93407           sqlite3ReleaseTempRange(pParse, r1, nFarg);
 92004  93408         }
 92005         -      break;
        93409  +      return target;
 92006  93410       }
 92007  93411   #ifndef SQLITE_OMIT_SUBQUERY
 92008  93412       case TK_EXISTS:
 92009  93413       case TK_SELECT: {
        93414  +      int nCol;
 92010  93415         testcase( op==TK_EXISTS );
 92011  93416         testcase( op==TK_SELECT );
 92012         -      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        93417  +      if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
        93418  +        sqlite3SubselectError(pParse, nCol, 1);
        93419  +      }else{
        93420  +        return sqlite3CodeSubselect(pParse, pExpr, 0, 0);
        93421  +      }
 92013  93422         break;
 92014  93423       }
        93424  +    case TK_SELECT_COLUMN: {
        93425  +      if( pExpr->pLeft->iTable==0 ){
        93426  +        pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
        93427  +      }
        93428  +      return pExpr->pLeft->iTable + pExpr->iColumn;
        93429  +    }
 92015  93430       case TK_IN: {
 92016  93431         int destIfFalse = sqlite3VdbeMakeLabel(v);
 92017  93432         int destIfNull = sqlite3VdbeMakeLabel(v);
 92018  93433         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 92019  93434         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 92020  93435         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 92021  93436         sqlite3VdbeResolveLabel(v, destIfFalse);
 92022  93437         sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
 92023  93438         sqlite3VdbeResolveLabel(v, destIfNull);
 92024         -      break;
        93439  +      return target;
 92025  93440       }
 92026  93441   #endif /* SQLITE_OMIT_SUBQUERY */
 92027  93442   
 92028  93443   
 92029  93444       /*
 92030  93445       **    x BETWEEN y AND z
 92031  93446       **
................................................................................
 92034  93449       **    x>=y AND x<=z
 92035  93450       **
 92036  93451       ** X is stored in pExpr->pLeft.
 92037  93452       ** Y is stored in pExpr->pList->a[0].pExpr.
 92038  93453       ** Z is stored in pExpr->pList->a[1].pExpr.
 92039  93454       */
 92040  93455       case TK_BETWEEN: {
 92041         -      Expr *pLeft = pExpr->pLeft;
 92042         -      struct ExprList_item *pLItem = pExpr->x.pList->a;
 92043         -      Expr *pRight = pLItem->pExpr;
 92044         -
 92045         -      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 92046         -      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 92047         -      testcase( regFree1==0 );
 92048         -      testcase( regFree2==0 );
 92049         -      r3 = sqlite3GetTempReg(pParse);
 92050         -      r4 = sqlite3GetTempReg(pParse);
 92051         -      codeCompare(pParse, pLeft, pRight, OP_Ge,
 92052         -                  r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
 92053         -      pLItem++;
 92054         -      pRight = pLItem->pExpr;
 92055         -      sqlite3ReleaseTempReg(pParse, regFree2);
 92056         -      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 92057         -      testcase( regFree2==0 );
 92058         -      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 92059         -      VdbeCoverage(v);
 92060         -      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 92061         -      sqlite3ReleaseTempReg(pParse, r3);
 92062         -      sqlite3ReleaseTempReg(pParse, r4);
 92063         -      break;
        93456  +      exprCodeBetween(pParse, pExpr, target, 0, 0);
        93457  +      return target;
 92064  93458       }
 92065  93459       case TK_SPAN:
 92066  93460       case TK_COLLATE: 
 92067  93461       case TK_UPLUS: {
 92068         -      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 92069         -      break;
        93462  +      return sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 92070  93463       }
 92071  93464   
 92072  93465       case TK_TRIGGER: {
 92073  93466         /* If the opcode is TK_TRIGGER, then the expression is a reference
 92074  93467         ** to a column in the new.* or old.* pseudo-tables available to
 92075  93468         ** trigger programs. In this case Expr.iTable is set to 1 for the
 92076  93469         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
................................................................................
 92121  93514         ){
 92122  93515           sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
 92123  93516         }
 92124  93517   #endif
 92125  93518         break;
 92126  93519       }
 92127  93520   
        93521  +    case TK_VECTOR: {
        93522  +      sqlite3ErrorMsg(pParse, "row value misused");
        93523  +      break;
        93524  +    }
 92128  93525   
 92129  93526       /*
 92130  93527       ** Form A:
 92131  93528       **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 92132  93529       **
 92133  93530       ** Form B:
 92134  93531       **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
................................................................................
 92164  93561         pEList = pExpr->x.pList;
 92165  93562         aListelem = pEList->a;
 92166  93563         nExpr = pEList->nExpr;
 92167  93564         endLabel = sqlite3VdbeMakeLabel(v);
 92168  93565         if( (pX = pExpr->pLeft)!=0 ){
 92169  93566           tempX = *pX;
 92170  93567           testcase( pX->op==TK_COLUMN );
 92171         -        exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
        93568  +        exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
 92172  93569           testcase( regFree1==0 );
        93570  +        memset(&opCompare, 0, sizeof(opCompare));
 92173  93571           opCompare.op = TK_EQ;
 92174  93572           opCompare.pLeft = &tempX;
 92175  93573           pTest = &opCompare;
 92176  93574           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
 92177  93575           ** The value in regFree1 might get SCopy-ed into the file result.
 92178  93576           ** So make sure that the regFree1 register is not reused for other
 92179  93577           ** purposes and possibly overwritten.  */
................................................................................
 92199  93597         if( (nExpr&1)!=0 ){
 92200  93598           sqlite3ExprCachePush(pParse);
 92201  93599           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
 92202  93600           sqlite3ExprCachePop(pParse);
 92203  93601         }else{
 92204  93602           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 92205  93603         }
 92206         -      assert( db->mallocFailed || pParse->nErr>0 
        93604  +      assert( pParse->db->mallocFailed || pParse->nErr>0 
 92207  93605              || pParse->iCacheLevel==iCacheLevel );
 92208  93606         sqlite3VdbeResolveLabel(v, endLabel);
 92209  93607         break;
 92210  93608       }
 92211  93609   #ifndef SQLITE_OMIT_TRIGGER
 92212  93610       case TK_RAISE: {
 92213  93611         assert( pExpr->affinity==OE_Rollback 
................................................................................
 92444  93842   **
 92445  93843   ** The above is equivalent to 
 92446  93844   **
 92447  93845   **    x>=y AND x<=z
 92448  93846   **
 92449  93847   ** Code it as such, taking care to do the common subexpression
 92450  93848   ** elimination of x.
        93849  +**
        93850  +** The xJumpIf parameter determines details:
        93851  +**
        93852  +**    NULL:                   Store the boolean result in reg[dest]
        93853  +**    sqlite3ExprIfTrue:      Jump to dest if true
        93854  +**    sqlite3ExprIfFalse:     Jump to dest if false
        93855  +**
        93856  +** The jumpIfNull parameter is ignored if xJumpIf is NULL.
 92451  93857   */
 92452  93858   static void exprCodeBetween(
 92453  93859     Parse *pParse,    /* Parsing and code generating context */
 92454  93860     Expr *pExpr,      /* The BETWEEN expression */
 92455         -  int dest,         /* Jump here if the jump is taken */
 92456         -  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
        93861  +  int dest,         /* Jump destination or storage location */
        93862  +  void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
 92457  93863     int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
 92458  93864   ){
 92459  93865     Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
 92460  93866     Expr compLeft;    /* The  x>=y  term */
 92461  93867     Expr compRight;   /* The  x<=z  term */
 92462  93868     Expr exprX;       /* The  x  subexpression */
 92463  93869     int regFree1 = 0; /* Temporary use register */
 92464  93870   
        93871  +
        93872  +  memset(&compLeft, 0, sizeof(Expr));
        93873  +  memset(&compRight, 0, sizeof(Expr));
        93874  +  memset(&exprAnd, 0, sizeof(Expr));
        93875  +
 92465  93876     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 92466  93877     exprX = *pExpr->pLeft;
 92467  93878     exprAnd.op = TK_AND;
 92468  93879     exprAnd.pLeft = &compLeft;
 92469  93880     exprAnd.pRight = &compRight;
 92470  93881     compLeft.op = TK_GE;
 92471  93882     compLeft.pLeft = &exprX;
 92472  93883     compLeft.pRight = pExpr->x.pList->a[0].pExpr;
 92473  93884     compRight.op = TK_LE;
 92474  93885     compRight.pLeft = &exprX;
 92475  93886     compRight.pRight = pExpr->x.pList->a[1].pExpr;
 92476         -  exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
 92477         -  if( jumpIfTrue ){
 92478         -    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
        93887  +  exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
        93888  +  if( xJump ){
        93889  +    xJump(pParse, &exprAnd, dest, jumpIfNull);
 92479  93890     }else{
 92480         -    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
        93891  +    exprX.flags |= EP_FromJoin;
        93892  +    sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
 92481  93893     }
 92482  93894     sqlite3ReleaseTempReg(pParse, regFree1);
 92483  93895   
 92484  93896     /* Ensure adequate test coverage */
 92485         -  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
 92486         -  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
 92487         -  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
 92488         -  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
 92489         -  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
 92490         -  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
 92491         -  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
 92492         -  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
        93897  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
        93898  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
        93899  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
        93900  +  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
        93901  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
        93902  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
        93903  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
        93904  +  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
        93905  +  testcase( xJump==0 );
 92493  93906   }
 92494  93907   
 92495  93908   /*
 92496  93909   ** Generate code for a boolean expression such that a jump is made
 92497  93910   ** to the label "dest" if the expression is true but execution
 92498  93911   ** continues straight thru if the expression is false.
 92499  93912   **
................................................................................
 92550  93963         /* Fall thru */
 92551  93964       case TK_LT:
 92552  93965       case TK_LE:
 92553  93966       case TK_GT:
 92554  93967       case TK_GE:
 92555  93968       case TK_NE:
 92556  93969       case TK_EQ: {
        93970  +      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
 92557  93971         testcase( jumpIfNull==0 );
 92558  93972         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 92559  93973         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 92560  93974         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 92561  93975                     r1, r2, dest, jumpIfNull);
 92562  93976         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 92563  93977         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
................................................................................
 92582  93996         VdbeCoverageIf(v, op==TK_ISNULL);
 92583  93997         VdbeCoverageIf(v, op==TK_NOTNULL);
 92584  93998         testcase( regFree1==0 );
 92585  93999         break;
 92586  94000       }
 92587  94001       case TK_BETWEEN: {
 92588  94002         testcase( jumpIfNull==0 );
 92589         -      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
        94003  +      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
 92590  94004         break;
 92591  94005       }
 92592  94006   #ifndef SQLITE_OMIT_SUBQUERY
 92593  94007       case TK_IN: {
 92594  94008         int destIfFalse = sqlite3VdbeMakeLabel(v);
 92595  94009         int destIfNull = jumpIfNull ? dest : destIfFalse;
 92596  94010         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 92597  94011         sqlite3VdbeGoto(v, dest);
 92598  94012         sqlite3VdbeResolveLabel(v, destIfFalse);
 92599  94013         break;
 92600  94014       }
 92601  94015   #endif
 92602  94016       default: {
        94017  +    default_expr:
 92603  94018         if( exprAlwaysTrue(pExpr) ){
 92604  94019           sqlite3VdbeGoto(v, dest);
 92605  94020         }else if( exprAlwaysFalse(pExpr) ){
 92606  94021           /* No-op */
 92607  94022         }else{
 92608  94023           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 92609  94024           sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
................................................................................
 92702  94117         /* Fall thru */
 92703  94118       case TK_LT:
 92704  94119       case TK_LE:
 92705  94120       case TK_GT:
 92706  94121       case TK_GE:
 92707  94122       case TK_NE:
 92708  94123       case TK_EQ: {
        94124  +      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
 92709  94125         testcase( jumpIfNull==0 );
 92710  94126         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 92711  94127         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 92712  94128         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 92713  94129                     r1, r2, dest, jumpIfNull);
 92714  94130         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 92715  94131         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
................................................................................
 92732  94148         testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
 92733  94149         testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
 92734  94150         testcase( regFree1==0 );
 92735  94151         break;
 92736  94152       }
 92737  94153       case TK_BETWEEN: {
 92738  94154         testcase( jumpIfNull==0 );
 92739         -      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
        94155  +      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
 92740  94156         break;
 92741  94157       }
 92742  94158   #ifndef SQLITE_OMIT_SUBQUERY
 92743  94159       case TK_IN: {
 92744  94160         if( jumpIfNull ){
 92745  94161           sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 92746  94162         }else{
................................................................................
 92748  94164           sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 92749  94165           sqlite3VdbeResolveLabel(v, destIfNull);
 92750  94166         }
 92751  94167         break;
 92752  94168       }
 92753  94169   #endif
 92754  94170       default: {
        94171  +    default_expr: 
 92755  94172         if( exprAlwaysFalse(pExpr) ){
 92756  94173           sqlite3VdbeGoto(v, dest);
 92757  94174         }else if( exprAlwaysTrue(pExpr) ){
 92758  94175           /* no-op */
 92759  94176         }else{
 92760  94177           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 92761  94178           sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
................................................................................
 92913  94330      && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
 92914  94331      && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
 92915  94332     ){
 92916  94333       return 1;
 92917  94334     }
 92918  94335     return 0;
 92919  94336   }
        94337  +
        94338  +/*
        94339  +** An instance of the following structure is used by the tree walker
        94340  +** to determine if an expression can be evaluated by reference to the
        94341  +** index only, without having to do a search for the corresponding
        94342  +** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
        94343  +** is the cursor for the table.
        94344  +*/
        94345  +struct IdxCover {
        94346  +  Index *pIdx;     /* The index to be tested for coverage */
        94347  +  int iCur;        /* Cursor number for the table corresponding to the index */
        94348  +};
        94349  +
        94350  +/*
        94351  +** Check to see if there are references to columns in table 
        94352  +** pWalker->u.pIdxCover->iCur can be satisfied using the index
        94353  +** pWalker->u.pIdxCover->pIdx.
        94354  +*/
        94355  +static int exprIdxCover(Walker *pWalker, Expr *pExpr){
        94356  +  if( pExpr->op==TK_COLUMN
        94357  +   && pExpr->iTable==pWalker->u.pIdxCover->iCur
        94358  +   && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
        94359  +  ){
        94360  +    pWalker->eCode = 1;
        94361  +    return WRC_Abort;
        94362  +  }
        94363  +  return WRC_Continue;
        94364  +}
        94365  +
        94366  +/*
        94367  +** Determine if an index pIdx on table with cursor iCur contains will
        94368  +** the expression pExpr.  Return true if the index does cover the
        94369  +** expression and false if the pExpr expression references table columns
        94370  +** that are not found in the index pIdx.
        94371  +**
        94372  +** An index covering an expression means that the expression can be
        94373  +** evaluated using only the index and without having to lookup the
        94374  +** corresponding table entry.
        94375  +*/
        94376  +SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
        94377  +  Expr *pExpr,        /* The index to be tested */
        94378  +  int iCur,           /* The cursor number for the corresponding table */
        94379  +  Index *pIdx         /* The index that might be used for coverage */
        94380  +){
        94381  +  Walker w;
        94382  +  struct IdxCover xcov;
        94383  +  memset(&w, 0, sizeof(w));
        94384  +  xcov.iCur = iCur;
        94385  +  xcov.pIdx = pIdx;
        94386  +  w.xExprCallback = exprIdxCover;
        94387  +  w.u.pIdxCover = &xcov;
        94388  +  sqlite3WalkExpr(&w, pExpr);
        94389  +  return !w.eCode;
        94390  +}
        94391  +
 92920  94392   
 92921  94393   /*
 92922  94394   ** An instance of the following structure is used by the tree walker
 92923  94395   ** to count references to table columns in the arguments of an 
 92924  94396   ** aggregate function, in order to implement the
 92925  94397   ** sqlite3FunctionThisSrc() routine.
 92926  94398   */
................................................................................
 93208  94680         }
 93209  94681       }
 93210  94682       pParse->aTempReg[pParse->nTempReg++] = iReg;
 93211  94683     }
 93212  94684   }
 93213  94685   
 93214  94686   /*
 93215         -** Allocate or deallocate a block of nReg consecutive registers
        94687  +** Allocate or deallocate a block of nReg consecutive registers.
 93216  94688   */
 93217  94689   SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
 93218  94690     int i, n;
        94691  +  if( nReg==1 ) return sqlite3GetTempReg(pParse);
 93219  94692     i = pParse->iRangeReg;
 93220  94693     n = pParse->nRangeReg;
 93221  94694     if( nReg<=n ){
 93222  94695       assert( !usedAsColumnCache(pParse, i, i+n-1) );
 93223  94696       pParse->iRangeReg += nReg;
 93224  94697       pParse->nRangeReg -= nReg;
 93225  94698     }else{
 93226  94699       i = pParse->nMem+1;
 93227  94700       pParse->nMem += nReg;
 93228  94701     }
 93229  94702     return i;
 93230  94703   }
 93231  94704   SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
        94705  +  if( nReg==1 ){
        94706  +    sqlite3ReleaseTempReg(pParse, iReg);
        94707  +    return;
        94708  +  }
 93232  94709     sqlite3ExprCacheRemove(pParse, iReg, nReg);
 93233  94710     if( nReg>pParse->nRangeReg ){
 93234  94711       pParse->nRangeReg = nReg;
 93235  94712       pParse->iRangeReg = iReg;
 93236  94713     }
 93237  94714   }
 93238  94715   
................................................................................
 93680  95157     if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 93681  95158     assert( pSrc->nSrc==1 );
 93682  95159     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 93683  95160   
 93684  95161     pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 93685  95162     if( !pTab ) goto exit_rename_table;
 93686  95163     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 93687         -  zDb = db->aDb[iDb].zName;
        95164  +  zDb = db->aDb[iDb].zDbSName;
 93688  95165     db->flags |= SQLITE_PreferBuiltin;
 93689  95166   
 93690  95167     /* Get a NULL terminated version of the new table name. */
 93691  95168     zName = sqlite3NameFromToken(db, pName);
 93692  95169     if( !zName ) goto exit_rename_table;
 93693  95170   
 93694  95171     /* Check that a table or index named 'zName' does not already exist
................................................................................
 93868  95345     const char *zDb;          /* Database name */
 93869  95346     const char *zTab;         /* Table name */
 93870  95347     char *zCol;               /* Null-terminated column definition */
 93871  95348     Column *pCol;             /* The new column */
 93872  95349     Expr *pDflt;              /* Default value for the new column */
 93873  95350     sqlite3 *db;              /* The database connection; */
 93874  95351     Vdbe *v = pParse->pVdbe;  /* The prepared statement under construction */
        95352  +  int r1;                   /* Temporary registers */
 93875  95353   
 93876  95354     db = pParse->db;
 93877  95355     if( pParse->nErr || db->mallocFailed ) return;
 93878  95356     assert( v!=0 );
 93879  95357     pNew = pParse->pNewTable;
 93880  95358     assert( pNew );
 93881  95359   
 93882  95360     assert( sqlite3BtreeHoldsAllMutexes(db) );
 93883  95361     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
 93884         -  zDb = db->aDb[iDb].zName;
        95362  +  zDb = db->aDb[iDb].zDbSName;
 93885  95363     zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
 93886  95364     pCol = &pNew->aCol[pNew->nCol-1];
 93887  95365     pDflt = pCol->pDflt;
 93888  95366     pTab = sqlite3FindTable(db, zTab, zDb);
 93889  95367     assert( pTab );
 93890  95368   
 93891  95369   #ifndef SQLITE_OMIT_AUTHORIZATION
................................................................................
 93962  95440         zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
 93963  95441         zTab
 93964  95442       );
 93965  95443       sqlite3DbFree(db, zCol);
 93966  95444       db->flags = savedDbFlags;
 93967  95445     }
 93968  95446   
 93969         -  /* If the default value of the new column is NULL, then the file
 93970         -  ** format to 2. If the default value of the new column is not NULL,
 93971         -  ** the file format be 3.  Back when this feature was first added
 93972         -  ** in 2006, we went to the trouble to upgrade the file format to the
 93973         -  ** minimum support values.  But 10-years on, we can assume that all
 93974         -  ** extent versions of SQLite support file-format 4, so we always and
 93975         -  ** unconditionally upgrade to 4.
        95447  +  /* Make sure the schema version is at least 3.  But do not upgrade
        95448  +  ** from less than 3 to 4, as that will corrupt any preexisting DESC
        95449  +  ** index.
 93976  95450     */
 93977         -  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 
 93978         -                    SQLITE_MAX_FILE_FORMAT);
        95451  +  r1 = sqlite3GetTempReg(pParse);
        95452  +  sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
        95453  +  sqlite3VdbeUsesBtree(v, iDb);
        95454  +  sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
        95455  +  sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
        95456  +  VdbeCoverage(v);
        95457  +  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
        95458  +  sqlite3ReleaseTempReg(pParse, r1);
 93979  95459   
 93980  95460     /* Reload the schema of the modified table. */
 93981  95461     reloadTableSchema(pParse, pTab, pTab->zName);
 93982  95462   }
 93983  95463   
 93984  95464   /*
 93985  95465   ** This function is called by the parser after the table-name in
................................................................................
 94285  95765   
 94286  95766     /* Create new statistic tables if they do not exist, or clear them
 94287  95767     ** if they do already exist.
 94288  95768     */
 94289  95769     for(i=0; i<ArraySize(aTable); i++){
 94290  95770       const char *zTab = aTable[i].zName;
 94291  95771       Table *pStat;
 94292         -    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
        95772  +    if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
 94293  95773         if( aTable[i].zCols ){
 94294  95774           /* The sqlite_statN table does not exist. Create it. Note that a 
 94295  95775           ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 94296  95776           ** of the new table in register pParse->regRoot. This is important 
 94297  95777           ** because the OpenWrite opcode below will be needing it. */
 94298  95778           sqlite3NestedParse(pParse,
 94299         -            "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
        95779  +            "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
 94300  95780           );
 94301  95781           aRoot[i] = pParse->regRoot;
 94302  95782           aCreateTbl[i] = OPFLAG_P2ISREG;
 94303  95783         }
 94304  95784       }else{
 94305  95785         /* The table already exists. If zWhere is not NULL, delete all entries 
 94306  95786         ** associated with the table zWhere. If zWhere is NULL, delete the
................................................................................
 94307  95787         ** entire contents of the table. */
 94308  95788         aRoot[i] = pStat->tnum;
 94309  95789         aCreateTbl[i] = 0;
 94310  95790         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 94311  95791         if( zWhere ){
 94312  95792           sqlite3NestedParse(pParse,
 94313  95793              "DELETE FROM %Q.%s WHERE %s=%Q",
 94314         -           pDb->zName, zTab, zWhereType, zWhere
        95794  +           pDb->zDbSName, zTab, zWhereType, zWhere
 94315  95795           );
 94316  95796         }else{
 94317  95797           /* The sqlite_stat[134] table already exists.  Delete all rows. */
 94318  95798           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 94319  95799         }
 94320  95800       }
 94321  95801     }
................................................................................
 95069  96549     }
 95070  96550     assert( sqlite3BtreeHoldsAllMutexes(db) );
 95071  96551     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 95072  96552     assert( iDb>=0 );
 95073  96553     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 95074  96554   #ifndef SQLITE_OMIT_AUTHORIZATION
 95075  96555     if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 95076         -      db->aDb[iDb].zName ) ){
        96556  +      db->aDb[iDb].zDbSName ) ){
 95077  96557       return;
 95078  96558     }
 95079  96559   #endif
 95080  96560   
 95081  96561     /* Establish a read-lock on the table at the shared-cache level. 
 95082  96562     ** Open a read-only cursor on the table. Also allocate a cursor number
 95083  96563     ** to use for scanning indexes (iIdxCur). No index cursor is opened at
................................................................................
 95459  96939           sqlite3DbFree(db, z);
 95460  96940         }
 95461  96941       }
 95462  96942     }else{
 95463  96943       /* Form 3: Analyze the fully qualified table name */
 95464  96944       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 95465  96945       if( iDb>=0 ){
 95466         -      zDb = db->aDb[iDb].zName;
        96946  +      zDb = db->aDb[iDb].zDbSName;
 95467  96947         z = sqlite3NameFromToken(db, pTableName);
 95468  96948         if( z ){
 95469  96949           if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 95470  96950             analyzeTable(pParse, pIdx->pTable, pIdx);
 95471  96951           }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 95472  96952             analyzeTable(pParse, pTab, 0);
 95473  96953           }
................................................................................
 95919  97399       sqlite3DeleteIndexSamples(db, pIdx);
 95920  97400       pIdx->aSample = 0;
 95921  97401   #endif
 95922  97402     }
 95923  97403   
 95924  97404     /* Load new statistics out of the sqlite_stat1 table */
 95925  97405     sInfo.db = db;
 95926         -  sInfo.zDatabase = db->aDb[iDb].zName;
        97406  +  sInfo.zDatabase = db->aDb[iDb].zDbSName;
 95927  97407     if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
 95928  97408       zSql = sqlite3MPrintf(db, 
 95929  97409           "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 95930  97410       if( zSql==0 ){
 95931  97411         rc = SQLITE_NOMEM_BKPT;
 95932  97412       }else{
 95933  97413         rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
................................................................................
 96062  97542       goto attach_error;
 96063  97543     }
 96064  97544     if( !db->autoCommit ){
 96065  97545       zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 96066  97546       goto attach_error;
 96067  97547     }
 96068  97548     for(i=0; i<db->nDb; i++){
 96069         -    char *z = db->aDb[i].zName;
        97549  +    char *z = db->aDb[i].zDbSName;
 96070  97550       assert( z && zName );
 96071  97551       if( sqlite3StrICmp(z, zName)==0 ){
 96072  97552         zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 96073  97553         goto attach_error;
 96074  97554       }
 96075  97555     }
 96076  97556   
................................................................................
 96127  97607   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 96128  97608       sqlite3BtreeSetPagerFlags(aNew->pBt,
 96129  97609                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
 96130  97610   #endif
 96131  97611       sqlite3BtreeLeave(aNew->pBt);
 96132  97612     }
 96133  97613     aNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
 96134         -  aNew->zName = sqlite3DbStrDup(db, zName);
 96135         -  if( rc==SQLITE_OK && aNew->zName==0 ){
        97614  +  aNew->zDbSName = sqlite3DbStrDup(db, zName);
        97615  +  if( rc==SQLITE_OK && aNew->zDbSName==0 ){
 96136  97616       rc = SQLITE_NOMEM_BKPT;
 96137  97617     }
 96138  97618   
 96139  97619   
 96140  97620   #ifdef SQLITE_HAS_CODEC
 96141  97621     if( rc==SQLITE_OK ){
 96142  97622       extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
................................................................................
 96157  97637           zKey = (char *)sqlite3_value_blob(argv[2]);
 96158  97638           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 96159  97639           break;
 96160  97640   
 96161  97641         case SQLITE_NULL:
 96162  97642           /* No key specified.  Use the key from the main database */
 96163  97643           sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 96164         -        if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
        97644  +        if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
 96165  97645             rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 96166  97646           }
 96167  97647           break;
 96168  97648       }
 96169  97649     }
 96170  97650   #endif
 96171  97651   
................................................................................
 96240  97720   
 96241  97721     UNUSED_PARAMETER(NotUsed);
 96242  97722   
 96243  97723     if( zName==0 ) zName = "";
 96244  97724     for(i=0; i<db->nDb; i++){
 96245  97725       pDb = &db->aDb[i];
 96246  97726       if( pDb->pBt==0 ) continue;
 96247         -    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
        97727  +    if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
 96248  97728     }
 96249  97729   
 96250  97730     if( i>=db->nDb ){
 96251  97731       sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 96252  97732       goto detach_error;
 96253  97733     }
 96254  97734     if( i<2 ){
................................................................................
 96398  97878     const Token *pName  /* Name of the view, trigger, or index */
 96399  97879   ){
 96400  97880     sqlite3 *db;
 96401  97881   
 96402  97882     db = pParse->db;
 96403  97883     assert( db->nDb>iDb );
 96404  97884     pFix->pParse = pParse;
 96405         -  pFix->zDb = db->aDb[iDb].zName;
        97885  +  pFix->zDb = db->aDb[iDb].zDbSName;
 96406  97886     pFix->pSchema = db->aDb[iDb].pSchema;
 96407  97887     pFix->zType = zType;
 96408  97888     pFix->pName = pName;
 96409  97889     pFix->bVarOnly = (iDb==1);
 96410  97890   }
 96411  97891   
 96412  97892   /*
................................................................................
 96657  98137   SQLITE_PRIVATE int sqlite3AuthReadCol(
 96658  98138     Parse *pParse,                  /* The parser context */
 96659  98139     const char *zTab,               /* Table name */
 96660  98140     const char *zCol,               /* Column name */
 96661  98141     int iDb                         /* Index of containing database. */
 96662  98142   ){
 96663  98143     sqlite3 *db = pParse->db;       /* Database handle */
 96664         -  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
        98144  +  char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
 96665  98145     int rc;                         /* Auth callback return code */
 96666  98146   
        98147  +  if( db->init.busy ) return SQLITE_OK;
 96667  98148     rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
 96668  98149   #ifdef SQLITE_USER_AUTHENTICATION
 96669  98150                    ,db->auth.zAuthUser
 96670  98151   #endif
 96671  98152                   );
 96672  98153     if( rc==SQLITE_DENY ){
 96673  98154       if( db->nDb>2 || iDb!=0 ){
................................................................................
 96965  98446       while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
 96966  98447       sqlite3VdbeAddOp0(v, OP_Halt);
 96967  98448   
 96968  98449   #if SQLITE_USER_AUTHENTICATION
 96969  98450       if( pParse->nTableLock>0 && db->init.busy==0 ){
 96970  98451         sqlite3UserAuthInit(db);
 96971  98452         if( db->auth.authLevel<UAUTH_User ){
 96972         -        pParse->rc = SQLITE_AUTH_USER;
 96973  98453           sqlite3ErrorMsg(pParse, "user not authenticated");
        98454  +        pParse->rc = SQLITE_AUTH_USER;
 96974  98455           return;
 96975  98456         }
 96976  98457       }
 96977  98458   #endif
 96978  98459   
 96979  98460       /* The cookie mask contains one bit for each database file open.
 96980  98461       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
................................................................................
 97130  98611     ** exists */
 97131  98612     if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
 97132  98613       return 0;
 97133  98614     }
 97134  98615   #endif
 97135  98616     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 97136  98617       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 97137         -    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
        98618  +    if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
 97138  98619       assert( sqlite3SchemaMutexHeld(db, j, 0) );
 97139  98620       p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
 97140  98621       if( p ) break;
 97141  98622     }
        98623  +  }
 97142  98624     return p;
 97143  98625   }
 97144  98626   
 97145  98627   /*
 97146  98628   ** Locate the in-memory structure that describes a particular database
 97147  98629   ** table given the name of that table and (optionally) the name of the
 97148  98630   ** database containing the table.  Return NULL if not found.  Also leave an
................................................................................
 97150  98632   **
 97151  98633   ** The difference between this routine and sqlite3FindTable() is that this
 97152  98634   ** routine leaves an error message in pParse->zErrMsg where
 97153  98635   ** sqlite3FindTable() does not.
 97154  98636   */
 97155  98637   SQLITE_PRIVATE Table *sqlite3LocateTable(
 97156  98638     Parse *pParse,         /* context in which to report errors */
 97157         -  int isView,            /* True if looking for a VIEW rather than a TABLE */
        98639  +  u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
 97158  98640     const char *zName,     /* Name of the table we are looking for */
 97159  98641     const char *zDbase     /* Name of the database.  Might be NULL */
 97160  98642   ){
 97161  98643     Table *p;
 97162  98644   
 97163  98645     /* Read the database schema. If an error occurs, leave an error message
 97164  98646     ** and code in pParse and return NULL. */
 97165  98647     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 97166  98648       return 0;
 97167  98649     }
 97168  98650   
 97169  98651     p = sqlite3FindTable(pParse->db, zName, zDbase);
 97170  98652     if( p==0 ){
 97171         -    const char *zMsg = isView ? "no such view" : "no such table";
        98653  +    const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
 97172  98654   #ifndef SQLITE_OMIT_VIRTUALTABLE
 97173  98655       if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
 97174  98656         /* If zName is the not the name of a table in the schema created using
 97175  98657         ** CREATE, then check to see if it is the name of an virtual table that
 97176  98658         ** can be an eponymous virtual table. */
 97177  98659         Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
 97178  98660         if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
 97179  98661           return pMod->pEpoTab;
 97180  98662         }
 97181  98663       }
 97182  98664   #endif
        98665  +    if( (flags & LOCATE_NOERR)==0 ){
 97183  98666       if( zDbase ){
 97184  98667         sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
 97185  98668       }else{
 97186  98669         sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
 97187  98670       }
 97188  98671       pParse->checkSchema = 1;
 97189  98672     }
        98673  +  }
 97190  98674   
 97191  98675     return p;
 97192  98676   }
 97193  98677   
 97194  98678   /*
 97195  98679   ** Locate the table identified by *p.
 97196  98680   **
................................................................................
 97198  98682   ** sqlite3LocateTable() and this function is that this function restricts
 97199  98683   ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
 97200  98684   ** non-NULL if it is part of a view or trigger program definition. See
 97201  98685   ** sqlite3FixSrcList() for details.
 97202  98686   */
 97203  98687   SQLITE_PRIVATE Table *sqlite3LocateTableItem(
 97204  98688     Parse *pParse, 
 97205         -  int isView, 
        98689  +  u32 flags,
 97206  98690     struct SrcList_item *p
 97207  98691   ){
 97208  98692     const char *zDb;
 97209  98693     assert( p->pSchema==0 || p->zDatabase==0 );
 97210  98694     if( p->pSchema ){
 97211  98695       int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 97212         -    zDb = pParse->db->aDb[iDb].zName;
        98696  +    zDb = pParse->db->aDb[iDb].zDbSName;
 97213  98697     }else{
 97214  98698       zDb = p->zDatabase;
 97215  98699     }
 97216         -  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
        98700  +  return sqlite3LocateTable(pParse, flags, p->zName, zDb);
 97217  98701   }
 97218  98702   
 97219  98703   /*
 97220  98704   ** Locate the in-memory structure that describes 
 97221  98705   ** a particular index given the name of that index
 97222  98706   ** and the name of the database that contains the index.
 97223  98707   ** Return NULL if not found.
................................................................................
 97233  98717     int i;
 97234  98718     /* All mutexes are required for schema access.  Make sure we hold them. */
 97235  98719     assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 97236  98720     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 97237  98721       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 97238  98722       Schema *pSchema = db->aDb[j].pSchema;
 97239  98723       assert( pSchema );
 97240         -    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
        98724  +    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
 97241  98725       assert( sqlite3SchemaMutexHeld(db, j, 0) );
 97242  98726       p = sqlite3HashFind(&pSchema->idxHash, zName);
 97243  98727       if( p ) break;
 97244  98728     }
 97245  98729     return p;
 97246  98730   }
 97247  98731   
................................................................................
 97302  98786   ** are never candidates for being collapsed.
 97303  98787   */
 97304  98788   SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
 97305  98789     int i, j;
 97306  98790     for(i=j=2; i<db->nDb; i++){
 97307  98791       struct Db *pDb = &db->aDb[i];
 97308  98792       if( pDb->pBt==0 ){
 97309         -      sqlite3DbFree(db, pDb->zName);
 97310         -      pDb->zName = 0;
        98793  +      sqlite3DbFree(db, pDb->zDbSName);
        98794  +      pDb->zDbSName = 0;
 97311  98795         continue;
 97312  98796       }
 97313  98797       if( j<i ){
 97314  98798         db->aDb[j] = db->aDb[i];
 97315  98799       }
 97316  98800       j++;
 97317  98801     }
................................................................................
 97417  98901     ** lookaside, this number should not change. */
 97418  98902     TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
 97419  98903                            db->lookaside.nOut : 0 );
 97420  98904   
 97421  98905     /* Delete all indices associated with this table. */
 97422  98906     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
 97423  98907       pNext = pIndex->pNext;
 97424         -    assert( pIndex->pSchema==pTable->pSchema );
 97425         -    if( !db || db->pnBytesFreed==0 ){
        98908  +    assert( pIndex->pSchema==pTable->pSchema
        98909  +         || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
        98910  +    if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
 97426  98911         char *zName = pIndex->zName; 
 97427  98912         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 97428  98913            &pIndex->pSchema->idxHash, zName, 0
 97429  98914         );
 97430  98915         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 97431  98916         assert( pOld==pIndex || pOld==0 );
 97432  98917       }
................................................................................
 97522  99007   ** -1 if the named db cannot be found.
 97523  99008   */
 97524  99009   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
 97525  99010     int i = -1;         /* Database number */
 97526  99011     if( zName ){
 97527  99012       Db *pDb;
 97528  99013       for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
 97529         -      if( 0==sqlite3StrICmp(pDb->zName, zName) ) break;
        99014  +      if( 0==sqlite3StrICmp(pDb->zDbSName, zName) ) break;
 97530  99015       }
 97531  99016     }
 97532  99017     return i;
 97533  99018   }
 97534  99019   
 97535  99020   /*
 97536  99021   ** The token *pName contains the name of a database (either "main" or
................................................................................
 97581  99066       *pUnqual = pName2;
 97582  99067       iDb = sqlite3FindDb(db, pName1);
 97583  99068       if( iDb<0 ){
 97584  99069         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 97585  99070         return -1;
 97586  99071       }
 97587  99072     }else{
 97588         -    assert( db->init.iDb==0 || db->init.busy );
        99073  +    assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0);
 97589  99074       iDb = db->init.iDb;
 97590  99075       *pUnqual = pName1;
 97591  99076     }
 97592  99077     return iDb;
 97593  99078   }
 97594  99079   
 97595  99080   /*
................................................................................
 97692  99177     {
 97693  99178       static const u8 aCode[] = {
 97694  99179          SQLITE_CREATE_TABLE,
 97695  99180          SQLITE_CREATE_TEMP_TABLE,
 97696  99181          SQLITE_CREATE_VIEW,
 97697  99182          SQLITE_CREATE_TEMP_VIEW
 97698  99183       };
 97699         -    char *zDb = db->aDb[iDb].zName;
        99184  +    char *zDb = db->aDb[iDb].zDbSName;
 97700  99185       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 97701  99186         goto begin_table_error;
 97702  99187       }
 97703  99188       if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
 97704  99189                                          zName, 0, zDb) ){
 97705  99190         goto begin_table_error;
 97706  99191       }
................................................................................
 97711  99196     ** index or table name in the same database.  Issue an error message if
 97712  99197     ** it does. The exception is if the statement being parsed was passed
 97713  99198     ** to an sqlite3_declare_vtab() call. In that case only the column names
 97714  99199     ** and types will be used, so there is no need to test for namespace
 97715  99200     ** collisions.
 97716  99201     */
 97717  99202     if( !IN_DECLARE_VTAB ){
 97718         -    char *zDb = db->aDb[iDb].zName;
        99203  +    char *zDb = db->aDb[iDb].zDbSName;
 97719  99204       if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 97720  99205         goto begin_table_error;
 97721  99206       }
 97722  99207       pTable = sqlite3FindTable(db, zName, zDb);
 97723  99208       if( pTable ){
 97724  99209         if( !noErr ){
 97725  99210           sqlite3ErrorMsg(pParse, "table %T already exists", pName);
................................................................................
 98100  99585     int autoInc,      /* True if the AUTOINCREMENT keyword is present */
 98101  99586     int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
 98102  99587   ){
 98103  99588     Table *pTab = pParse->pNewTable;
 98104  99589     Column *pCol = 0;
 98105  99590     int iCol = -1, i;
 98106  99591     int nTerm;
 98107         -  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
        99592  +  if( pTab==0 ) goto primary_key_exit;
 98108  99593     if( pTab->tabFlags & TF_HasPrimaryKey ){
 98109  99594       sqlite3ErrorMsg(pParse, 
 98110  99595         "table \"%s\" has more than one primary key", pTab->zName);
 98111  99596       goto primary_key_exit;
 98112  99597     }
 98113  99598     pTab->tabFlags |= TF_HasPrimaryKey;
 98114  99599     if( pList==0 ){
................................................................................
 98146  99631       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
 98147  99632     }else if( autoInc ){
 98148  99633   #ifndef SQLITE_OMIT_AUTOINCREMENT
 98149  99634       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
 98150  99635          "INTEGER PRIMARY KEY");
 98151  99636   #endif
 98152  99637     }else{
 98153         -    Index *p;
 98154         -    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
 98155         -                           0, sortOrder, 0);
 98156         -    if( p ){
 98157         -      p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
 98158         -    }
        99638  +    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
        99639  +                           0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
 98159  99640       pList = 0;
 98160  99641     }
 98161  99642   
 98162  99643   primary_key_exit:
 98163  99644     sqlite3ExprListDelete(pParse->db, pList);
 98164  99645     return;
 98165  99646   }
................................................................................
 98468  99949   /*
 98469  99950   ** This routine runs at the end of parsing a CREATE TABLE statement that
 98470  99951   ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
 98471  99952   ** internal schema data structures and the generated VDBE code so that they
 98472  99953   ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
 98473  99954   ** Changes include:
 98474  99955   **
 98475         -**     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
        99956  +**     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
        99957  +**     (2)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
 98476  99958   **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
 98477  99959   **          data storage is a covering index btree.
 98478         -**     (2)  Bypass the creation of the sqlite_master table entry
        99960  +**     (3)  Bypass the creation of the sqlite_master table entry
 98479  99961   **          for the PRIMARY KEY as the primary key index is now
 98480  99962   **          identified by the sqlite_master table entry of the table itself.
 98481         -**     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
        99963  +**     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
 98482  99964   **          schema to the rootpage from the main table.
 98483         -**     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
 98484  99965   **     (5)  Add all table columns to the PRIMARY KEY Index object
 98485  99966   **          so that the PRIMARY KEY is a covering index.  The surplus
 98486  99967   **          columns are part of KeyInfo.nXField and are not used for
 98487  99968   **          sorting or lookup or uniqueness checks.
 98488  99969   **     (6)  Replace the rowid tail on all automatically generated UNIQUE
 98489  99970   **          indices with the PRIMARY KEY columns.
        99971  +**
        99972  +** For virtual tables, only (1) is performed.
 98490  99973   */
 98491  99974   static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
 98492  99975     Index *pIdx;
 98493  99976     Index *pPk;
 98494  99977     int nPk;
 98495  99978     int i, j;
 98496  99979     sqlite3 *db = pParse->db;
 98497  99980     Vdbe *v = pParse->pVdbe;
        99981  +
        99982  +  /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
        99983  +  */
        99984  +  if( !db->init.imposterTable ){
        99985  +    for(i=0; i<pTab->nCol; i++){
        99986  +      if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
        99987  +        pTab->aCol[i].notNull = OE_Abort;
        99988  +      }
        99989  +    }
        99990  +  }
        99991  +
        99992  +  /* The remaining transformations only apply to b-tree tables, not to
        99993  +  ** virtual tables */
        99994  +  if( IN_DECLARE_VTAB ) return;
 98498  99995   
 98499  99996     /* Convert the OP_CreateTable opcode that would normally create the
 98500  99997     ** root-page for the table into an OP_CreateIndex opcode.  The index
 98501  99998     ** created will become the PRIMARY KEY index.
 98502  99999     */
 98503 100000     if( pParse->addrCrTab ){
 98504 100001       assert( v );
................................................................................
 98513 100010       Token ipkToken;
 98514 100011       sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
 98515 100012       pList = sqlite3ExprListAppend(pParse, 0, 
 98516 100013                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
 98517 100014       if( pList==0 ) return;
 98518 100015       pList->a[0].sortOrder = pParse->iPkSortOrder;
 98519 100016       assert( pParse->pNewTable==pTab );
 98520         -    pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
 98521         -    if( pPk==0 ) return;
 98522         -    pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
       100017  +    sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
       100018  +                       SQLITE_IDXTYPE_PRIMARYKEY);
       100019  +    if( db->mallocFailed ) return;
       100020  +    pPk = sqlite3PrimaryKeyIndex(pTab);
 98523 100021       pTab->iPKey = -1;
 98524 100022     }else{
 98525 100023       pPk = sqlite3PrimaryKeyIndex(pTab);
 98526 100024   
 98527 100025       /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
 98528 100026       ** table entry. This is only required if currently generating VDBE
 98529 100027       ** code for a CREATE TABLE (not when parsing one as part of reading
................................................................................
 98543 100041           pPk->nColumn--;
 98544 100042         }else{
 98545 100043           pPk->aiColumn[j++] = pPk->aiColumn[i];
 98546 100044         }
 98547 100045       }
 98548 100046       pPk->nKeyCol = j;
 98549 100047     }
 98550         -  pPk->isCovering = 1;
 98551 100048     assert( pPk!=0 );
       100049  +  pPk->isCovering = 1;
       100050  +  if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
 98552 100051     nPk = pPk->nKeyCol;
 98553 100052   
 98554         -  /* Make sure every column of the PRIMARY KEY is NOT NULL.  (Except,
 98555         -  ** do not enforce this for imposter tables.) */
 98556         -  if( !db->init.imposterTable ){
 98557         -    for(i=0; i<nPk; i++){
 98558         -      pTab->aCol[pPk->aiColumn[i]].notNull = OE_Abort;
 98559         -    }
 98560         -    pPk->uniqNotNull = 1;
 98561         -  }
 98562         -
 98563 100053     /* The root page of the PRIMARY KEY is the table root page */
 98564 100054     pPk->tnum = pTab->tnum;
 98565 100055   
 98566 100056     /* Update the in-memory representation of all UNIQUE indices by converting
 98567 100057     ** the final rowid column into one or more columns of the PRIMARY KEY.
 98568 100058     */
 98569 100059     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
................................................................................
 98799 100289       ** SQLITE_MASTER table.  We just need to update that slot with all
 98800 100290       ** the information we've collected.
 98801 100291       */
 98802 100292       sqlite3NestedParse(pParse,
 98803 100293         "UPDATE %Q.%s "
 98804 100294            "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
 98805 100295          "WHERE rowid=#%d",
 98806         -      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       100296  +      db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb),
 98807 100297         zType,
 98808 100298         p->zName,
 98809 100299         p->zName,
 98810 100300         pParse->regRoot,
 98811 100301         zStmt,
 98812 100302         pParse->regRowid
 98813 100303       );
................................................................................
 98814 100304       sqlite3DbFree(db, zStmt);
 98815 100305       sqlite3ChangeCookie(pParse, iDb);
 98816 100306   
 98817 100307   #ifndef SQLITE_OMIT_AUTOINCREMENT
 98818 100308       /* Check to see if we need to create an sqlite_sequence table for
 98819 100309       ** keeping track of autoincrement keys.
 98820 100310       */
 98821         -    if( p->tabFlags & TF_Autoincrement ){
       100311  +    if( (p->tabFlags & TF_Autoincrement)!=0 ){
 98822 100312         Db *pDb = &db->aDb[iDb];
 98823 100313         assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98824 100314         if( pDb->pSchema->pSeqTab==0 ){
 98825 100315           sqlite3NestedParse(pParse,
 98826 100316             "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 98827         -          pDb->zName
       100317  +          pDb->zDbSName
 98828 100318           );
 98829 100319         }
 98830 100320       }
 98831 100321   #endif
 98832 100322   
 98833 100323       /* Reparse everything to update our internal data structures */
 98834 100324       sqlite3VdbeAddParseSchemaOp(v, iDb,
................................................................................
 98944 100434   */
 98945 100435   SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 98946 100436     Table *pSelTab;   /* A fake table from which we get the result set */
 98947 100437     Select *pSel;     /* Copy of the SELECT that implements the view */
 98948 100438     int nErr = 0;     /* Number of errors encountered */
 98949 100439     int n;            /* Temporarily holds the number of cursors assigned */
 98950 100440     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
       100441  +#ifndef SQLITE_OMIT_AUTHORIZATION
 98951 100442     sqlite3_xauth xAuth;       /* Saved xAuth pointer */
       100443  +#endif
 98952 100444   
 98953 100445     assert( pTable );
 98954 100446   
 98955 100447   #ifndef SQLITE_OMIT_VIRTUALTABLE
 98956 100448     if( sqlite3VtabCallConnect(pParse, pTable) ){
 98957 100449       return SQLITE_ERROR;
 98958 100450     }
................................................................................
 99134 100626     **
 99135 100627     ** The "#NNN" in the SQL is a special constant that means whatever value
 99136 100628     ** is in register NNN.  See grammar rules associated with the TK_REGISTER
 99137 100629     ** token for additional information.
 99138 100630     */
 99139 100631     sqlite3NestedParse(pParse, 
 99140 100632        "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
 99141         -     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
       100633  +     pParse->db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), iTable, r1, r1);
 99142 100634   #endif
 99143 100635     sqlite3ReleaseTempReg(pParse, r1);
 99144 100636   }
 99145 100637   
 99146 100638   /*
 99147 100639   ** Write VDBE code to erase table pTab and all associated indices on disk.
 99148 100640   ** Code to update the sqlite_master tables and internal schema definitions
................................................................................
 99210 100702   static void sqlite3ClearStatTables(
 99211 100703     Parse *pParse,         /* The parsing context */
 99212 100704     int iDb,               /* The database number */
 99213 100705     const char *zType,     /* "idx" or "tbl" */
 99214 100706     const char *zName      /* Name of index or table */
 99215 100707   ){
 99216 100708     int i;
 99217         -  const char *zDbName = pParse->db->aDb[iDb].zName;
       100709  +  const char *zDbName = pParse->db->aDb[iDb].zDbSName;
 99218 100710     for(i=1; i<=4; i++){
 99219 100711       char zTab[24];
 99220 100712       sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
 99221 100713       if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 99222 100714         sqlite3NestedParse(pParse,
 99223 100715           "DELETE FROM %Q.%s WHERE %s=%Q",
 99224 100716           zDbName, zTab, zType, zName
................................................................................
 99263 100755     ** the table being dropped. This is done before the table is dropped
 99264 100756     ** at the btree level, in case the sqlite_sequence table needs to
 99265 100757     ** move as a result of the drop (can happen in auto-vacuum mode).
 99266 100758     */
 99267 100759     if( pTab->tabFlags & TF_Autoincrement ){
 99268 100760       sqlite3NestedParse(pParse,
 99269 100761         "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
 99270         -      pDb->zName, pTab->zName
       100762  +      pDb->zDbSName, pTab->zName
 99271 100763       );
 99272 100764     }
 99273 100765   #endif
 99274 100766   
 99275 100767     /* Drop all SQLITE_MASTER table and index entries that refer to the
 99276 100768     ** table. The program name loops through the master table and deletes
 99277 100769     ** every row that refers to a table of the same name as the one being
 99278 100770     ** dropped. Triggers are handled separately because a trigger can be
 99279 100771     ** created in the temp database that refers to a table in another
 99280 100772     ** database.
 99281 100773     */
 99282 100774     sqlite3NestedParse(pParse, 
 99283 100775         "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 99284         -      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
       100776  +      pDb->zDbSName, SCHEMA_TABLE(iDb), pTab->zName);
 99285 100777     if( !isView && !IsVirtual(pTab) ){
 99286 100778       destroyTable(pParse, pTab);
 99287 100779     }
 99288 100780   
 99289 100781     /* Remove the table entry from SQLite's internal schema and modify
 99290 100782     ** the schema cookie.
 99291 100783     */
................................................................................
 99310 100802     if( db->mallocFailed ){
 99311 100803       goto exit_drop_table;
 99312 100804     }
 99313 100805     assert( pParse->nErr==0 );
 99314 100806     assert( pName->nSrc==1 );
 99315 100807     if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
 99316 100808     if( noErr ) db->suppressErr++;
       100809  +  assert( isView==0 || isView==LOCATE_VIEW );
 99317 100810     pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 99318 100811     if( noErr ) db->suppressErr--;
 99319 100812   
 99320 100813     if( pTab==0 ){
 99321 100814       if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 99322 100815       goto exit_drop_table;
 99323 100816     }
................................................................................
 99330 100823     if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
 99331 100824       goto exit_drop_table;
 99332 100825     }
 99333 100826   #ifndef SQLITE_OMIT_AUTHORIZATION
 99334 100827     {
 99335 100828       int code;
 99336 100829       const char *zTab = SCHEMA_TABLE(iDb);
 99337         -    const char *zDb = db->aDb[iDb].zName;
       100830  +    const char *zDb = db->aDb[iDb].zDbSName;
 99338 100831       const char *zArg2 = 0;
 99339 100832       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
 99340 100833         goto exit_drop_table;
 99341 100834       }
 99342 100835       if( isView ){
 99343 100836         if( !OMIT_TEMPDB && iDb==1 ){
 99344 100837           code = SQLITE_DROP_TEMP_VIEW;
................................................................................
 99571 101064     KeyInfo *pKey;                 /* KeyInfo for index */
 99572 101065     int regRecord;                 /* Register holding assembled index record */
 99573 101066     sqlite3 *db = pParse->db;      /* The database connection */
 99574 101067     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 99575 101068   
 99576 101069   #ifndef SQLITE_OMIT_AUTHORIZATION
 99577 101070     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 99578         -      db->aDb[iDb].zName ) ){
       101071  +      db->aDb[iDb].zDbSName ) ){
 99579 101072       return;
 99580 101073     }
 99581 101074   #endif
 99582 101075   
 99583 101076     /* Require a write-lock on the table to perform this operation */
 99584 101077     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
 99585 101078   
................................................................................
 99680 101173   ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
 99681 101174   ** as the table to be indexed.  pParse->pNewTable is a table that is
 99682 101175   ** currently being constructed by a CREATE TABLE statement.
 99683 101176   **
 99684 101177   ** pList is a list of columns to be indexed.  pList will be NULL if this
 99685 101178   ** is a primary key or unique-constraint on the most recent column added
 99686 101179   ** to the table currently under construction.  
 99687         -**
 99688         -** If the index is created successfully, return a pointer to the new Index
 99689         -** structure. This is used by sqlite3AddPrimaryKey() to mark the index
 99690         -** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
 99691 101180   */
 99692         -SQLITE_PRIVATE Index *sqlite3CreateIndex(
       101181  +SQLITE_PRIVATE void sqlite3CreateIndex(
 99693 101182     Parse *pParse,     /* All information about this parse */
 99694 101183     Token *pName1,     /* First part of index name. May be NULL */
 99695 101184     Token *pName2,     /* Second part of index name. May be NULL */
 99696 101185     SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 99697 101186     ExprList *pList,   /* A list of columns to be indexed */
 99698 101187     int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 99699 101188     Token *pStart,     /* The CREATE token that begins this statement */
 99700 101189     Expr *pPIWhere,    /* WHERE clause for partial indices */
 99701 101190     int sortOrder,     /* Sort order of primary key when pList==NULL */
 99702         -  int ifNotExist     /* Omit error if index already exists */
       101191  +  int ifNotExist,    /* Omit error if index already exists */
       101192  +  u8 idxType         /* The index type */
 99703 101193   ){
 99704         -  Index *pRet = 0;     /* Pointer to return */
 99705 101194     Table *pTab = 0;     /* Table to be indexed */
 99706 101195     Index *pIndex = 0;   /* The index to be created */
 99707 101196     char *zName = 0;     /* Name of the index */
 99708 101197     int nName;           /* Number of characters in zName */
 99709 101198     int i, j;
 99710 101199     DbFixer sFix;        /* For assigning database names to pTable */
 99711 101200     int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
................................................................................
 99715 101204     Token *pName = 0;    /* Unqualified name of the index to create */
 99716 101205     struct ExprList_item *pListItem; /* For looping over pList */
 99717 101206     int nExtra = 0;                  /* Space allocated for zExtra[] */
 99718 101207     int nExtraCol;                   /* Number of extra columns needed */
 99719 101208     char *zExtra = 0;                /* Extra space after the Index object */
 99720 101209     Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
 99721 101210   
 99722         -  if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
       101211  +  if( db->mallocFailed || pParse->nErr>0 ){
       101212  +    goto exit_create_index;
       101213  +  }
       101214  +  if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
 99723 101215       goto exit_create_index;
 99724 101216     }
 99725 101217     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 99726 101218       goto exit_create_index;
 99727 101219     }
 99728 101220   
 99729 101221     /*
................................................................................
 99824 101316       }
 99825 101317       if( !db->init.busy ){
 99826 101318         if( sqlite3FindTable(db, zName, 0)!=0 ){
 99827 101319           sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 99828 101320           goto exit_create_index;
 99829 101321         }
 99830 101322       }
 99831         -    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
       101323  +    if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
 99832 101324         if( !ifNotExist ){
 99833 101325           sqlite3ErrorMsg(pParse, "index %s already exists", zName);
 99834 101326         }else{
 99835 101327           assert( !db->init.busy );
 99836 101328           sqlite3CodeVerifySchema(pParse, iDb);
 99837 101329         }
 99838 101330         goto exit_create_index;
................................................................................
 99841 101333       int n;
 99842 101334       Index *pLoop;
 99843 101335       for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
 99844 101336       zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
 99845 101337       if( zName==0 ){
 99846 101338         goto exit_create_index;
 99847 101339       }
       101340  +
       101341  +    /* Automatic index names generated from within sqlite3_declare_vtab()
       101342  +    ** must have names that are distinct from normal automatic index names.
       101343  +    ** The following statement converts "sqlite3_autoindex..." into
       101344  +    ** "sqlite3_butoindex..." in order to make the names distinct.
       101345  +    ** The "vtab_err.test" test demonstrates the need of this statement. */
       101346  +    if( IN_DECLARE_VTAB ) zName[7]++;
 99848 101347     }
 99849 101348   
 99850 101349     /* Check for authorization to create an index.
 99851 101350     */
 99852 101351   #ifndef SQLITE_OMIT_AUTHORIZATION
 99853 101352     {
 99854         -    const char *zDb = pDb->zName;
       101353  +    const char *zDb = pDb->zDbSName;
 99855 101354       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
 99856 101355         goto exit_create_index;
 99857 101356       }
 99858 101357       i = SQLITE_CREATE_INDEX;
 99859 101358       if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
 99860 101359       if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
 99861 101360         goto exit_create_index;
................................................................................
 99904 101403     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
 99905 101404     pIndex->zName = zExtra;
 99906 101405     zExtra += nName + 1;
 99907 101406     memcpy(pIndex->zName, zName, nName+1);
 99908 101407     pIndex->pTable = pTab;
 99909 101408     pIndex->onError = (u8)onError;
 99910 101409     pIndex->uniqNotNull = onError!=OE_None;
 99911         -  pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
       101410  +  pIndex->idxType = idxType;
 99912 101411     pIndex->pSchema = db->aDb[iDb].pSchema;
 99913 101412     pIndex->nKeyCol = pList->nExpr;
 99914 101413     if( pPIWhere ){
 99915 101414       sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
 99916 101415       pIndex->pPartIdxWhere = pPIWhere;
 99917 101416       pPIWhere = 0;
 99918 101417     }
................................................................................
100084 101583               sqlite3ErrorMsg(pParse, 
100085 101584                   "conflicting ON CONFLICT clauses specified", 0);
100086 101585             }
100087 101586             if( pIdx->onError==OE_Default ){
100088 101587               pIdx->onError = pIndex->onError;
100089 101588             }
100090 101589           }
100091         -        pRet = pIdx;
       101590  +        if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
100092 101591           goto exit_create_index;
100093 101592         }
100094 101593       }
100095 101594     }
100096 101595   
100097 101596     /* Link the new Index structure to its table and to the other
100098 101597     ** in-memory database structures. 
100099 101598     */
100100 101599     assert( pParse->nErr==0 );
100101 101600     if( db->init.busy ){
100102 101601       Index *p;
       101602  +    assert( !IN_DECLARE_VTAB );
100103 101603       assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
100104 101604       p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
100105 101605                             pIndex->zName, pIndex);
100106 101606       if( p ){
100107 101607         assert( p==pIndex );  /* Malloc must have failed */
100108 101608         sqlite3OomFault(db);
100109 101609         goto exit_create_index;
................................................................................
100161 101661         zStmt = 0;
100162 101662       }
100163 101663   
100164 101664       /* Add an entry in sqlite_master for this index
100165 101665       */
100166 101666       sqlite3NestedParse(pParse, 
100167 101667           "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
100168         -        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
       101668  +        db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb),
100169 101669           pIndex->zName,
100170 101670           pTab->zName,
100171 101671           iMem,
100172 101672           zStmt
100173 101673       );
100174 101674       sqlite3DbFree(db, zStmt);
100175 101675   
................................................................................
100177 101677       ** to invalidate all pre-compiled statements.
100178 101678       */
100179 101679       if( pTblName ){
100180 101680         sqlite3RefillIndex(pParse, pIndex, iMem);
100181 101681         sqlite3ChangeCookie(pParse, iDb);
100182 101682         sqlite3VdbeAddParseSchemaOp(v, iDb,
100183 101683            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
100184         -      sqlite3VdbeAddOp1(v, OP_Expire, 0);
       101684  +      sqlite3VdbeAddOp0(v, OP_Expire);
100185 101685       }
100186 101686   
100187 101687       sqlite3VdbeJumpHere(v, pIndex->tnum);
100188 101688     }
100189 101689   
100190 101690     /* When adding an index to the list of indices for a table, make
100191 101691     ** sure all indices labeled OE_Replace come after all those labeled
................................................................................
100202 101702         Index *pOther = pTab->pIndex;
100203 101703         while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
100204 101704           pOther = pOther->pNext;
100205 101705         }
100206 101706         pIndex->pNext = pOther->pNext;
100207 101707         pOther->pNext = pIndex;
100208 101708       }
100209         -    pRet = pIndex;
100210 101709       pIndex = 0;
100211 101710     }
100212 101711   
100213 101712     /* Clean up before exiting */
100214 101713   exit_create_index:
100215 101714     if( pIndex ) freeIndex(db, pIndex);
100216 101715     sqlite3ExprDelete(db, pPIWhere);
100217 101716     sqlite3ExprListDelete(db, pList);
100218 101717     sqlite3SrcListDelete(db, pTblName);
100219 101718     sqlite3DbFree(db, zName);
100220         -  return pRet;
100221 101719   }
100222 101720   
100223 101721   /*
100224 101722   ** Fill the Index.aiRowEst[] array with default information - information
100225 101723   ** to be used when we have not run the ANALYZE command.
100226 101724   **
100227 101725   ** aiRowEst[0] is supposed to contain the number of elements in the index.
................................................................................
100242 101740     /*                10,  9,  8,  7,  6 */
100243 101741     LogEst aVal[] = { 33, 32, 30, 28, 26 };
100244 101742     LogEst *a = pIdx->aiRowLogEst;
100245 101743     int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
100246 101744     int i;
100247 101745   
100248 101746     /* Set the first entry (number of rows in the index) to the estimated 
100249         -  ** number of rows in the table. Or 10, if the estimated number of rows 
100250         -  ** in the table is less than that.  */
       101747  +  ** number of rows in the table, or half the number of rows in the table
       101748  +  ** for a partial index.   But do not let the estimate drop below 10. */
100251 101749     a[0] = pIdx->pTable->nRowLogEst;
       101750  +  if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
100252 101751     if( a[0]<33 ) a[0] = 33;        assert( 33==sqlite3LogEst(10) );
100253 101752   
100254 101753     /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
100255 101754     ** 6 and each subsequent value (if any) is 5.  */
100256 101755     memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
100257 101756     for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
100258 101757       a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
................................................................................
100296 101795       goto exit_drop_index;
100297 101796     }
100298 101797     iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
100299 101798   #ifndef SQLITE_OMIT_AUTHORIZATION
100300 101799     {
100301 101800       int code = SQLITE_DROP_INDEX;
100302 101801       Table *pTab = pIndex->pTable;
100303         -    const char *zDb = db->aDb[iDb].zName;
       101802  +    const char *zDb = db->aDb[iDb].zDbSName;
100304 101803       const char *zTab = SCHEMA_TABLE(iDb);
100305 101804       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
100306 101805         goto exit_drop_index;
100307 101806       }
100308 101807       if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
100309 101808       if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
100310 101809         goto exit_drop_index;
................................................................................
100314 101813   
100315 101814     /* Generate code to remove the index and from the master table */
100316 101815     v = sqlite3GetVdbe(pParse);
100317 101816     if( v ){
100318 101817       sqlite3BeginWriteOperation(pParse, 1, iDb);
100319 101818       sqlite3NestedParse(pParse,
100320 101819          "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
100321         -       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
       101820  +       db->aDb[iDb].zDbSName, SCHEMA_TABLE(iDb), pIndex->zName
100322 101821       );
100323 101822       sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
100324 101823       sqlite3ChangeCookie(pParse, iDb);
100325 101824       destroyRootPage(pParse, pIndex->tnum, iDb);
100326 101825       sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
100327 101826     }
100328 101827   
................................................................................
100859 102358   ** attached database. Otherwise, invoke it for the database named zDb only.
100860 102359   */
100861 102360   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
100862 102361     sqlite3 *db = pParse->db;
100863 102362     int i;
100864 102363     for(i=0; i<db->nDb; i++){
100865 102364       Db *pDb = &db->aDb[i];
100866         -    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
       102365  +    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
100867 102366         sqlite3CodeVerifySchema(pParse, i);
100868 102367       }
100869 102368     }
100870 102369   }
100871 102370   
100872 102371   /*
100873 102372   ** Generate VDBE code that prepares for doing an operation that
................................................................................
101106 102605       }
101107 102606       sqlite3DbFree(db, zColl);
101108 102607     }
101109 102608     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
101110 102609     if( iDb<0 ) return;
101111 102610     z = sqlite3NameFromToken(db, pObjName);
101112 102611     if( z==0 ) return;
101113         -  zDb = db->aDb[iDb].zName;
       102612  +  zDb = db->aDb[iDb].zDbSName;
101114 102613     pTab = sqlite3FindTable(db, z, zDb);
101115 102614     if( pTab ){
101116 102615       reindexTable(pParse, pTab, 0);
101117 102616       sqlite3DbFree(db, z);
101118 102617       return;
101119 102618     }
101120 102619     pIndex = sqlite3FindIndex(db, z, zDb);
................................................................................
101127 102626     sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
101128 102627   }
101129 102628   #endif
101130 102629   
101131 102630   /*
101132 102631   ** Return a KeyInfo structure that is appropriate for the given Index.
101133 102632   **
101134         -** The KeyInfo structure for an index is cached in the Index object.
101135         -** So there might be multiple references to the returned pointer.  The
101136         -** caller should not try to modify the KeyInfo object.
101137         -**
101138 102633   ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
101139 102634   ** when it has finished using it.
101140 102635   */
101141 102636   SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
101142 102637     int i;
101143 102638     int nCol = pIdx->nColumn;
101144 102639     int nKey = pIdx->nKeyCol;
................................................................................
101824 103319     sqlite3 *db = pParse->db;
101825 103320     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
101826 103321     pWhere = sqlite3ExprDup(db, pWhere, 0);
101827 103322     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
101828 103323     if( pFrom ){
101829 103324       assert( pFrom->nSrc==1 );
101830 103325       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
101831         -    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
       103326  +    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
101832 103327       assert( pFrom->a[0].pOn==0 );
101833 103328       assert( pFrom->a[0].pUsing==0 );
101834 103329     }
101835 103330     pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 
101836 103331                             SF_IncludeHidden, 0, 0);
101837 103332     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
101838 103333     sqlite3Select(pParse, pSel, &dest);
................................................................................
101934 103429   SQLITE_PRIVATE void sqlite3DeleteFrom(
101935 103430     Parse *pParse,         /* The parser context */
101936 103431     SrcList *pTabList,     /* The table from which we should delete things */
101937 103432     Expr *pWhere           /* The WHERE clause.  May be null */
101938 103433   ){
101939 103434     Vdbe *v;               /* The virtual database engine */
101940 103435     Table *pTab;           /* The table from which records will be deleted */
101941         -  const char *zDb;       /* Name of database holding pTab */
101942 103436     int i;                 /* Loop counter */
101943 103437     WhereInfo *pWInfo;     /* Information about the WHERE clause */
101944 103438     Index *pIdx;           /* For looping over indices of the table */
101945 103439     int iTabCur;           /* Cursor number for the table */
101946 103440     int iDataCur = 0;      /* VDBE cursor for the canonical data source */
101947 103441     int iIdxCur = 0;       /* Cursor number of the first index */
101948 103442     int nIdx;              /* Number of indices */
................................................................................
102011 103505     }
102012 103506   
102013 103507     if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
102014 103508       goto delete_from_cleanup;
102015 103509     }
102016 103510     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102017 103511     assert( iDb<db->nDb );
102018         -  zDb = db->aDb[iDb].zName;
102019         -  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
       103512  +  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, 
       103513  +                            db->aDb[iDb].zDbSName);
102020 103514     assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
102021 103515     if( rcauth==SQLITE_DENY ){
102022 103516       goto delete_from_cleanup;
102023 103517     }
102024 103518     assert(!isView || pTrigger);
102025 103519   
102026 103520     /* Assign cursor numbers to the table and all its indices.
................................................................................
103362 104856             return 0;
103363 104857           }
103364 104858           continue;
103365 104859         }
103366 104860       }
103367 104861       c2 = Utf8Read(zString);
103368 104862       if( c==c2 ) continue;
103369         -    if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
       104863  +    if( noCase  && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
103370 104864         continue;
103371 104865       }
103372 104866       if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
103373 104867       return 0;
103374 104868     }
103375 104869     return *zString==0;
103376 104870   }
................................................................................
103937 105431       if( zCharSet ){
103938 105432         sqlite3_free(azChar);
103939 105433       }
103940 105434     }
103941 105435     sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
103942 105436   }
103943 105437   
       105438  +
       105439  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
       105440  +/*
       105441  +** The "unknown" function is automatically substituted in place of
       105442  +** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
       105443  +** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
       105444  +** When the "sqlite3" command-line shell is built using this functionality,
       105445  +** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
       105446  +** involving application-defined functions to be examined in a generic
       105447  +** sqlite3 shell.
       105448  +*/
       105449  +static void unknownFunc(
       105450  +  sqlite3_context *context,
       105451  +  int argc,
       105452  +  sqlite3_value **argv
       105453  +){
       105454  +  /* no-op */
       105455  +}
       105456  +#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
       105457  +
103944 105458   
103945 105459   /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
103946 105460   ** is only available if the SQLITE_SOUNDEX compile-time option is used
103947 105461   ** when SQLite is built.
103948 105462   */
103949 105463   #ifdef SQLITE_SOUNDEX
103950 105464   /*
................................................................................
104415 105929     #ifdef SQLITE_CASE_SENSITIVE_LIKE
104416 105930       LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
104417 105931       LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
104418 105932     #else
104419 105933       LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
104420 105934       LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
104421 105935     #endif
       105936  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
       105937  +    FUNCTION(unknown,           -1, 0, 0, unknownFunc      ),
       105938  +#endif
104422 105939       FUNCTION(coalesce,           1, 0, 0, 0                ),
104423 105940       FUNCTION(coalesce,           0, 0, 0, 0                ),
104424 105941       FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
104425 105942     };
104426 105943   #ifndef SQLITE_OMIT_ALTERTABLE
104427 105944     sqlite3AlterFunctions();
104428 105945   #endif
................................................................................
105320 106837     /* Exactly one of regOld and regNew should be non-zero. */
105321 106838     assert( (regOld==0)!=(regNew==0) );
105322 106839   
105323 106840     /* If foreign-keys are disabled, this function is a no-op. */
105324 106841     if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
105325 106842   
105326 106843     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105327         -  zDb = db->aDb[iDb].zName;
       106844  +  zDb = db->aDb[iDb].zDbSName;
105328 106845   
105329 106846     /* Loop through all the foreign key constraints for which pTab is the
105330 106847     ** child table (the table that the foreign key definition is part of).  */
105331 106848     for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
105332 106849       Table *pTo;                   /* Parent table of foreign key pFKey */
105333 106850       Index *pIdx = 0;              /* Index on key columns in pTo */
105334 106851       int *aiFree = 0;
................................................................................
105821 107338   ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
105822 107339   ** hash table.
105823 107340   */
105824 107341   SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
105825 107342     FKey *pFKey;                    /* Iterator variable */
105826 107343     FKey *pNext;                    /* Copy of pFKey->pNextFrom */
105827 107344   
105828         -  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
       107345  +  assert( db==0 || IsVirtual(pTab)
       107346  +         || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
105829 107347     for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
105830 107348   
105831 107349       /* Remove the FK from the fkeyHash hash table. */
105832 107350       if( !db || db->pnBytesFreed==0 ){
105833 107351         if( pFKey->pPrevTo ){
105834 107352           pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
105835 107353         }else{
................................................................................
106059 107577     return 0;
106060 107578   }
106061 107579   
106062 107580   #ifndef SQLITE_OMIT_AUTOINCREMENT
106063 107581   /*
106064 107582   ** Locate or create an AutoincInfo structure associated with table pTab
106065 107583   ** which is in database iDb.  Return the register number for the register
106066         -** that holds the maximum rowid.
       107584  +** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
       107585  +** table.  (Also return zero when doing a VACUUM since we do not want to
       107586  +** update the AUTOINCREMENT counters during a VACUUM.)
106067 107587   **
106068 107588   ** There is at most one AutoincInfo structure per table even if the
106069 107589   ** same table is autoincremented multiple times due to inserts within
106070 107590   ** triggers.  A new AutoincInfo structure is created if this is the
106071 107591   ** first use of table pTab.  On 2nd and subsequent uses, the original
106072 107592   ** AutoincInfo structure is used.
106073 107593   **
................................................................................
106082 107602   */
106083 107603   static int autoIncBegin(
106084 107604     Parse *pParse,      /* Parsing context */
106085 107605     int iDb,            /* Index of the database holding pTab */
106086 107606     Table *pTab         /* The table we are writing to */
106087 107607   ){
106088 107608     int memId = 0;      /* Register holding maximum rowid */
106089         -  if( pTab->tabFlags & TF_Autoincrement ){
       107609  +  if( (pTab->tabFlags & TF_Autoincrement)!=0
       107610  +   && (pParse->db->flags & SQLITE_Vacuum)==0
       107611  +  ){
106090 107612       Parse *pToplevel = sqlite3ParseToplevel(pParse);
106091 107613       AutoincInfo *pInfo;
106092 107614   
106093 107615       pInfo = pToplevel->pAinc;
106094 107616       while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
106095 107617       if( pInfo==0 ){
106096 107618         pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
................................................................................
106340 107862     Select *pSelect,      /* A SELECT statement to use as the data source */
106341 107863     IdList *pColumn,      /* Column names corresponding to IDLIST. */
106342 107864     int onError           /* How to handle constraint errors */
106343 107865   ){
106344 107866     sqlite3 *db;          /* The main database structure */
106345 107867     Table *pTab;          /* The table to insert into.  aka TABLE */
106346 107868     char *zTab;           /* Name of the table into which we are inserting */
106347         -  const char *zDb;      /* Name of the database holding this table */
106348 107869     int i, j, idx;        /* Loop counters */
106349 107870     Vdbe *v;              /* Generate code into this virtual machine */
106350 107871     Index *pIdx;          /* For looping over indices of the table */
106351 107872     int nColumn;          /* Number of columns in the data */
106352 107873     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
106353 107874     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
106354 107875     int iIdxCur = 0;      /* First index cursor */
................................................................................
106355 107876     int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
106356 107877     int endOfLoop;        /* Label for the end of the insertion loop */
106357 107878     int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
106358 107879     int addrInsTop = 0;   /* Jump to label "D" */
106359 107880     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
106360 107881     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
106361 107882     int iDb;              /* Index of database holding TABLE */
106362         -  Db *pDb;              /* The database containing table being inserted into */
106363 107883     u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
106364 107884     u8 appendFlag = 0;    /* True if the insert is likely to be an append */
106365 107885     u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
106366 107886     u8 bIdListInOrder;    /* True if IDLIST is in table order */
106367 107887     ExprList *pList = 0;  /* List of VALUES() to be inserted  */
106368 107888   
106369 107889     /* Register allocations */
................................................................................
106405 107925     if( NEVER(zTab==0) ) goto insert_cleanup;
106406 107926     pTab = sqlite3SrcListLookup(pParse, pTabList);
106407 107927     if( pTab==0 ){
106408 107928       goto insert_cleanup;
106409 107929     }
106410 107930     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106411 107931     assert( iDb<db->nDb );
106412         -  pDb = &db->aDb[iDb];
106413         -  zDb = pDb->zName;
106414         -  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
       107932  +  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
       107933  +                       db->aDb[iDb].zDbSName) ){
106415 107934       goto insert_cleanup;
106416 107935     }
106417 107936     withoutRowid = !HasRowid(pTab);
106418 107937   
106419 107938     /* Figure out if we have any triggers and if the table being
106420 107939     ** inserted into is a view
106421 107940     */
................................................................................
107636 109155     }else{
107637 109156       sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
107638 109157     }
107639 109158     if( piIdxCur ) *piIdxCur = iBase;
107640 109159     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
107641 109160       int iIdxCur = iBase++;
107642 109161       assert( pIdx->pSchema==pTab->pSchema );
       109162  +    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
       109163  +      if( piDataCur ) *piDataCur = iIdxCur;
       109164  +      p5 = 0;
       109165  +    }
107643 109166       if( aToOpen==0 || aToOpen[i+1] ){
107644 109167         sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
107645 109168         sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
107646         -      VdbeComment((v, "%s", pIdx->zName));
107647         -    }
107648         -    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
107649         -      if( piDataCur ) *piDataCur = iIdxCur;
107650         -    }else{
107651 109169         sqlite3VdbeChangeP5(v, p5);
       109170  +      VdbeComment((v, "%s", pIdx->zName));
107652 109171       }
107653 109172     }
107654 109173     if( iBase>pParse->nTab ) pParse->nTab = iBase;
107655 109174     return i;
107656 109175   }
107657 109176   
107658 109177   
................................................................................
108231 109750   *************************************************************************
108232 109751   ** This header file defines the SQLite interface for use by
108233 109752   ** shared libraries that want to be imported as extensions into
108234 109753   ** an SQLite instance.  Shared libraries that intend to be loaded
108235 109754   ** as extensions by SQLite should #include this file instead of 
108236 109755   ** sqlite3.h.
108237 109756   */
108238         -#ifndef _SQLITE3EXT_H_
108239         -#define _SQLITE3EXT_H_
       109757  +#ifndef SQLITE3EXT_H
       109758  +#define SQLITE3EXT_H
108240 109759   /* #include "sqlite3.h" */
108241 109760   
108242         -typedef struct sqlite3_api_routines sqlite3_api_routines;
108243         -
108244 109761   /*
108245 109762   ** The following structure holds pointers to all of the SQLite API
108246 109763   ** routines.
108247 109764   **
108248 109765   ** WARNING:  In order to maintain backwards compatibility, add new
108249 109766   ** interfaces to the end of this structure only.  If you insert new
108250 109767   ** interfaces in the middle of this structure, then older different
................................................................................
108497 110014     void (*result_subtype)(sqlite3_context*,unsigned int);
108498 110015     /* Version 3.10.0 and later */
108499 110016     int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
108500 110017     int (*strlike)(const char*,const char*,unsigned int);
108501 110018     int (*db_cacheflush)(sqlite3*);
108502 110019     /* Version 3.12.0 and later */
108503 110020     int (*system_errno)(sqlite3*);
       110021  +  /* Version 3.14.0 and later */
       110022  +  int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
       110023  +  char *(*expanded_sql)(sqlite3_stmt*);
108504 110024   };
       110025  +
       110026  +/*
       110027  +** This is the function signature used for all extension entry points.  It
       110028  +** is also defined in the file "loadext.c".
       110029  +*/
       110030  +typedef int (*sqlite3_loadext_entry)(
       110031  +  sqlite3 *db,                       /* Handle to the database. */
       110032  +  char **pzErrMsg,                   /* Used to set error string on failure. */
       110033  +  const sqlite3_api_routines *pThunk /* Extension API function pointers. */
       110034  +);
108505 110035   
108506 110036   /*
108507 110037   ** The following macros redefine the API routines so that they are
108508 110038   ** redirected through the global sqlite3_api structure.
108509 110039   **
108510 110040   ** This header file is also used by the loadext.c source file
108511 110041   ** (part of the main SQLite library - not an extension) so that
................................................................................
108742 110272   #define sqlite3_result_subtype         sqlite3_api->result_subtype
108743 110273   /* Version 3.10.0 and later */
108744 110274   #define sqlite3_status64               sqlite3_api->status64
108745 110275   #define sqlite3_strlike                sqlite3_api->strlike
108746 110276   #define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
108747 110277   /* Version 3.12.0 and later */
108748 110278   #define sqlite3_system_errno           sqlite3_api->system_errno
       110279  +/* Version 3.14.0 and later */
       110280  +#define sqlite3_trace_v2               sqlite3_api->trace_v2
       110281  +#define sqlite3_expanded_sql           sqlite3_api->expanded_sql
108749 110282   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
108750 110283   
108751 110284   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
108752 110285     /* This case when the file really is being compiled as a loadable 
108753 110286     ** extension */
108754 110287   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
108755 110288   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
................................................................................
108759 110292     /* This case when the file is being statically linked into the 
108760 110293     ** application */
108761 110294   # define SQLITE_EXTENSION_INIT1     /*no-op*/
108762 110295   # define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
108763 110296   # define SQLITE_EXTENSION_INIT3     /*no-op*/
108764 110297   #endif
108765 110298   
108766         -#endif /* _SQLITE3EXT_H_ */
       110299  +#endif /* SQLITE3EXT_H */
108767 110300   
108768 110301   /************** End of sqlite3ext.h ******************************************/
108769 110302   /************** Continuing where we left off in loadext.c ********************/
108770 110303   /* #include "sqliteInt.h" */
108771 110304   /* #include <string.h> */
108772 110305   
108773 110306   #ifndef SQLITE_OMIT_LOAD_EXTENSION
108774         -
108775 110307   /*
108776 110308   ** Some API routines are omitted when various features are
108777 110309   ** excluded from a build of SQLite.  Substitute a NULL pointer
108778 110310   ** for any missing APIs.
108779 110311   */
108780 110312   #ifndef SQLITE_ENABLE_COLUMN_METADATA
108781 110313   # define sqlite3_column_database_name   0
................................................................................
108837 110369   # define sqlite3_vtab_on_conflict 0
108838 110370   #endif
108839 110371   
108840 110372   #ifdef SQLITE_OMIT_SHARED_CACHE
108841 110373   # define sqlite3_enable_shared_cache 0
108842 110374   #endif
108843 110375   
108844         -#ifdef SQLITE_OMIT_TRACE
       110376  +#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
108845 110377   # define sqlite3_profile       0
108846 110378   # define sqlite3_trace         0
108847 110379   #endif
108848 110380   
108849 110381   #ifdef SQLITE_OMIT_GET_TABLE
108850 110382   # define sqlite3_free_table    0
108851 110383   # define sqlite3_get_table     0
................................................................................
108856 110388   #define sqlite3_blob_bytes     0
108857 110389   #define sqlite3_blob_close     0
108858 110390   #define sqlite3_blob_open      0
108859 110391   #define sqlite3_blob_read      0
108860 110392   #define sqlite3_blob_write     0
108861 110393   #define sqlite3_blob_reopen    0
108862 110394   #endif
       110395  +
       110396  +#if defined(SQLITE_OMIT_TRACE)
       110397  +# define sqlite3_trace_v2      0
       110398  +#endif
108863 110399   
108864 110400   /*
108865 110401   ** The following structure contains pointers to all SQLite API routines.
108866 110402   ** A pointer to this structure is passed into extensions when they are
108867 110403   ** loaded so that the extension can make calls back into the SQLite
108868 110404   ** library.
108869 110405   **
................................................................................
109162 110698     sqlite3_value_subtype,
109163 110699     sqlite3_result_subtype,
109164 110700     /* Version 3.10.0 and later */
109165 110701     sqlite3_status64,
109166 110702     sqlite3_strlike,
109167 110703     sqlite3_db_cacheflush,
109168 110704     /* Version 3.12.0 and later */
109169         -  sqlite3_system_errno
       110705  +  sqlite3_system_errno,
       110706  +  /* Version 3.14.0 and later */
       110707  +  sqlite3_trace_v2,
       110708  +  sqlite3_expanded_sql
109170 110709   };
109171 110710   
109172 110711   /*
109173 110712   ** Attempt to load an SQLite extension library contained in the file
109174 110713   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
109175 110714   ** default entry point name (sqlite3_extension_init) is used.  Use
109176 110715   ** of the default name is recommended.
................................................................................
109185 110724     sqlite3 *db,          /* Load the extension into this database connection */
109186 110725     const char *zFile,    /* Name of the shared library containing extension */
109187 110726     const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
109188 110727     char **pzErrMsg       /* Put error message here if not 0 */
109189 110728   ){
109190 110729     sqlite3_vfs *pVfs = db->pVfs;
109191 110730     void *handle;
109192         -  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       110731  +  sqlite3_loadext_entry xInit;
109193 110732     char *zErrmsg = 0;
109194 110733     const char *zEntry;
109195 110734     char *zAltEntry = 0;
109196 110735     void **aHandle;
109197 110736     u64 nMsg = 300 + sqlite3Strlen30(zFile);
109198 110737     int ii;
       110738  +  int rc;
109199 110739   
109200 110740     /* Shared library endings to try if zFile cannot be loaded as written */
109201 110741     static const char *azEndings[] = {
109202 110742   #if SQLITE_OS_WIN
109203 110743        "dll"   
109204 110744   #elif defined(__APPLE__)
109205 110745        "dylib"
................................................................................
109243 110783           sqlite3_snprintf(nMsg, zErrmsg, 
109244 110784               "unable to open shared library [%s]", zFile);
109245 110785           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
109246 110786         }
109247 110787       }
109248 110788       return SQLITE_ERROR;
109249 110789     }
109250         -  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
109251         -                   sqlite3OsDlSym(pVfs, handle, zEntry);
       110790  +  xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
109252 110791   
109253 110792     /* If no entry point was specified and the default legacy
109254 110793     ** entry point name "sqlite3_extension_init" was not found, then
109255 110794     ** construct an entry point name "sqlite3_X_init" where the X is
109256 110795     ** replaced by the lowercase value of every ASCII alphabetic 
109257 110796     ** character in the filename after the last "/" upto the first ".",
109258 110797     ** and eliding the first three characters if they are "lib".  
................................................................................
109276 110815       for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
109277 110816         if( sqlite3Isalpha(c) ){
109278 110817           zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
109279 110818         }
109280 110819       }
109281 110820       memcpy(zAltEntry+iEntry, "_init", 6);
109282 110821       zEntry = zAltEntry;
109283         -    xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
109284         -                     sqlite3OsDlSym(pVfs, handle, zEntry);
       110822  +    xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
109285 110823     }
109286 110824     if( xInit==0 ){
109287 110825       if( pzErrMsg ){
109288 110826         nMsg += sqlite3Strlen30(zEntry);
109289 110827         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
109290 110828         if( zErrmsg ){
109291 110829           sqlite3_snprintf(nMsg, zErrmsg,
................................................................................
109294 110832         }
109295 110833       }
109296 110834       sqlite3OsDlClose(pVfs, handle);
109297 110835       sqlite3_free(zAltEntry);
109298 110836       return SQLITE_ERROR;
109299 110837     }
109300 110838     sqlite3_free(zAltEntry);
109301         -  if( xInit(db, &zErrmsg, &sqlite3Apis) ){
       110839  +  rc = xInit(db, &zErrmsg, &sqlite3Apis);
       110840  +  if( rc ){
       110841  +    if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
109302 110842       if( pzErrMsg ){
109303 110843         *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
109304 110844       }
109305 110845       sqlite3_free(zErrmsg);
109306 110846       sqlite3OsDlClose(pVfs, handle);
109307 110847       return SQLITE_ERROR;
109308 110848     }
................................................................................
109359 110899     }else{
109360 110900       db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
109361 110901     }
109362 110902     sqlite3_mutex_leave(db->mutex);
109363 110903     return SQLITE_OK;
109364 110904   }
109365 110905   
109366         -#endif /* SQLITE_OMIT_LOAD_EXTENSION */
109367         -
109368         -/*
109369         -** The auto-extension code added regardless of whether or not extension
109370         -** loading is supported.  We need a dummy sqlite3Apis pointer for that
109371         -** code if regular extension loading is not available.  This is that
109372         -** dummy pointer.
109373         -*/
109374         -#ifdef SQLITE_OMIT_LOAD_EXTENSION
109375         -static const sqlite3_api_routines sqlite3Apis = { 0 };
109376         -#endif
109377         -
       110906  +#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
109378 110907   
109379 110908   /*
109380 110909   ** The following object holds the list of automatically loaded
109381 110910   ** extensions.
109382 110911   **
109383 110912   ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
109384 110913   ** mutex must be held while accessing this list.
................................................................................
109405 110934   #endif
109406 110935   
109407 110936   
109408 110937   /*
109409 110938   ** Register a statically linked extension that is automatically
109410 110939   ** loaded by every new database connection.
109411 110940   */
109412         -SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
       110941  +SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(
       110942  +  void (*xInit)(void)
       110943  +){
109413 110944     int rc = SQLITE_OK;
109414 110945   #ifndef SQLITE_OMIT_AUTOINIT
109415 110946     rc = sqlite3_initialize();
109416 110947     if( rc ){
109417 110948       return rc;
109418 110949     }else
109419 110950   #endif
................................................................................
109450 110981   ** set of routines that is invoked for each new database connection, if it
109451 110982   ** is currently on the list.  If xInit is not on the list, then this
109452 110983   ** routine is a no-op.
109453 110984   **
109454 110985   ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
109455 110986   ** was not on the list.
109456 110987   */
109457         -SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xInit)(void)){
       110988  +SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(
       110989  +  void (*xInit)(void)
       110990  +){
109458 110991   #if SQLITE_THREADSAFE
109459 110992     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
109460 110993   #endif
109461 110994     int i;
109462 110995     int n = 0;
109463 110996     wsdAutoextInit;
109464 110997     sqlite3_mutex_enter(mutex);
................................................................................
109499 111032   **
109500 111033   ** If anything goes wrong, set an error in the database connection.
109501 111034   */
109502 111035   SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
109503 111036     u32 i;
109504 111037     int go = 1;
109505 111038     int rc;
109506         -  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       111039  +  sqlite3_loadext_entry xInit;
109507 111040   
109508 111041     wsdAutoextInit;
109509 111042     if( wsdAutoext.nExt==0 ){
109510 111043       /* Common case: early out without every having to acquire a mutex */
109511 111044       return;
109512 111045     }
109513 111046     for(i=0; go; i++){
109514 111047       char *zErrmsg;
109515 111048   #if SQLITE_THREADSAFE
109516 111049       sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STAT