db/sqlite3/src/sqlite3.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /******************************************************************************
     2 ** This file is an amalgamation of many separate C source files from SQLite
     3 ** version 3.8.4.2.  By combining all the individual C code files into this 
     4 ** single large file, the entire code can be compiled as a single translation
     5 ** unit.  This allows many compilers to do optimizations that would not be
     6 ** possible if the files were compiled separately.  Performance improvements
     7 ** of 5% or more are commonly seen when SQLite is compiled as a single
     8 ** translation unit.
     9 **
    10 ** This file is all you need to compile SQLite.  To use SQLite in other
    11 ** programs, you need this file and the "sqlite3.h" header file that defines
    12 ** the programming interface to the SQLite library.  (If you do not have 
    13 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
    14 ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
    15 ** of the embedded sqlite3.h header file.) Additional code files may be needed
    16 ** if you want a wrapper to interface SQLite with your choice of programming
    17 ** language. The code for the "sqlite3" command-line shell is also in a
    18 ** separate file. This file contains only code for the core SQLite library.
    19 */
    20 #define SQLITE_CORE 1
    21 #define SQLITE_AMALGAMATION 1
    22 #ifndef SQLITE_PRIVATE
    23 # define SQLITE_PRIVATE static
    24 #endif
    25 #ifndef SQLITE_API
    26 # define SQLITE_API
    27 #endif
    28 /************** Begin file sqliteInt.h ***************************************/
    29 /*
    30 ** 2001 September 15
    31 **
    32 ** The author disclaims copyright to this source code.  In place of
    33 ** a legal notice, here is a blessing:
    34 **
    35 **    May you do good and not evil.
    36 **    May you find forgiveness for yourself and forgive others.
    37 **    May you share freely, never taking more than you give.
    38 **
    39 *************************************************************************
    40 ** Internal interface definitions for SQLite.
    41 **
    42 */
    43 #ifndef _SQLITEINT_H_
    44 #define _SQLITEINT_H_
    46 /*
    47 ** These #defines should enable >2GB file support on POSIX if the
    48 ** underlying operating system supports it.  If the OS lacks
    49 ** large file support, or if the OS is windows, these should be no-ops.
    50 **
    51 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
    52 ** system #includes.  Hence, this block of code must be the very first
    53 ** code in all source files.
    54 **
    55 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    56 ** on the compiler command line.  This is necessary if you are compiling
    57 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
    58 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
    59 ** without this option, LFS is enable.  But LFS does not exist in the kernel
    60 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
    61 ** portability you should omit LFS.
    62 **
    63 ** The previous paragraph was written in 2005.  (This paragraph is written
    64 ** on 2008-11-28.) These days, all Linux kernels support large files, so
    65 ** you should probably leave LFS enabled.  But some embedded platforms might
    66 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
    67 **
    68 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
    69 */
    70 #ifndef SQLITE_DISABLE_LFS
    71 # define _LARGE_FILE       1
    72 # ifndef _FILE_OFFSET_BITS
    73 #   define _FILE_OFFSET_BITS 64
    74 # endif
    75 # define _LARGEFILE_SOURCE 1
    76 #endif
    78 /*
    79 ** For MinGW, check to see if we can include the header file containing its
    80 ** version information, among other things.  Normally, this internal MinGW
    81 ** header file would [only] be included automatically by other MinGW header
    82 ** files; however, the contained version information is now required by this
    83 ** header file to work around binary compatibility issues (see below) and
    84 ** this is the only known way to reliably obtain it.  This entire #if block
    85 ** would be completely unnecessary if there was any other way of detecting
    86 ** MinGW via their preprocessor (e.g. if they customized their GCC to define
    87 ** some MinGW-specific macros).  When compiling for MinGW, either the
    88 ** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
    89 ** defined; otherwise, detection of conditions specific to MinGW will be
    90 ** disabled.
    91 */
    92 #if defined(_HAVE_MINGW_H)
    93 # include "mingw.h"
    94 #elif defined(_HAVE__MINGW_H)
    95 # include "_mingw.h"
    96 #endif
    98 /*
    99 ** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
   100 ** define is required to maintain binary compatibility with the MSVC runtime
   101 ** library in use (e.g. for Windows XP).
   102 */
   103 #if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
   104     defined(_WIN32) && !defined(_WIN64) && \
   105     defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
   106     defined(__MSVCRT__)
   107 # define _USE_32BIT_TIME_T
   108 #endif
   110 /* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
   111 ** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
   112 ** MinGW.
   113 */
   114 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
   115 /************** Begin file sqlite3.h *****************************************/
   116 /*
   117 ** 2001 September 15
   118 **
   119 ** The author disclaims copyright to this source code.  In place of
   120 ** a legal notice, here is a blessing:
   121 **
   122 **    May you do good and not evil.
   123 **    May you find forgiveness for yourself and forgive others.
   124 **    May you share freely, never taking more than you give.
   125 **
   126 *************************************************************************
   127 ** This header file defines the interface that the SQLite library
   128 ** presents to client programs.  If a C-function, structure, datatype,
   129 ** or constant definition does not appear in this file, then it is
   130 ** not a published API of SQLite, is subject to change without
   131 ** notice, and should not be referenced by programs that use SQLite.
   132 **
   133 ** Some of the definitions that are in this file are marked as
   134 ** "experimental".  Experimental interfaces are normally new
   135 ** features recently added to SQLite.  We do not anticipate changes
   136 ** to experimental interfaces but reserve the right to make minor changes
   137 ** if experience from use "in the wild" suggest such changes are prudent.
   138 **
   139 ** The official C-language API documentation for SQLite is derived
   140 ** from comments in this file.  This file is the authoritative source
   141 ** on how SQLite interfaces are suppose to operate.
   142 **
   143 ** The name of this file under configuration management is "sqlite.h.in".
   144 ** The makefile makes some minor changes to this file (such as inserting
   145 ** the version number) and changes its name to "sqlite3.h" as
   146 ** part of the build process.
   147 */
   148 #ifndef _SQLITE3_H_
   149 #define _SQLITE3_H_
   150 #include <stdarg.h>     /* Needed for the definition of va_list */
   152 /*
   153 ** Make sure we can call this stuff from C++.
   154 */
   155 #if 0
   156 extern "C" {
   157 #endif
   160 /*
   161 ** Add the ability to override 'extern'
   162 */
   163 #ifndef SQLITE_EXTERN
   164 # define SQLITE_EXTERN extern
   165 #endif
   167 #ifndef SQLITE_API
   168 # define SQLITE_API
   169 #endif
   172 /*
   173 ** These no-op macros are used in front of interfaces to mark those
   174 ** interfaces as either deprecated or experimental.  New applications
   175 ** should not use deprecated interfaces - they are support for backwards
   176 ** compatibility only.  Application writers should be aware that
   177 ** experimental interfaces are subject to change in point releases.
   178 **
   179 ** These macros used to resolve to various kinds of compiler magic that
   180 ** would generate warning messages when they were used.  But that
   181 ** compiler magic ended up generating such a flurry of bug reports
   182 ** that we have taken it all out and gone back to using simple
   183 ** noop macros.
   184 */
   185 #define SQLITE_DEPRECATED
   186 #define SQLITE_EXPERIMENTAL
   188 /*
   189 ** Ensure these symbols were not defined by some previous header file.
   190 */
   191 #ifdef SQLITE_VERSION
   192 # undef SQLITE_VERSION
   193 #endif
   194 #ifdef SQLITE_VERSION_NUMBER
   195 # undef SQLITE_VERSION_NUMBER
   196 #endif
   198 /*
   199 ** CAPI3REF: Compile-Time Library Version Numbers
   200 **
   201 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
   202 ** evaluates to a string literal that is the SQLite version in the
   203 ** format "X.Y.Z" where X is the major version number (always 3 for
   204 ** SQLite3) and Y is the minor version number and Z is the release number.)^
   205 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
   206 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
   207 ** numbers used in [SQLITE_VERSION].)^
   208 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
   209 ** be larger than the release from which it is derived.  Either Y will
   210 ** be held constant and Z will be incremented or else Y will be incremented
   211 ** and Z will be reset to zero.
   212 **
   213 ** Since version 3.6.18, SQLite source code has been stored in the
   214 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
   215 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
   216 ** a string which identifies a particular check-in of SQLite
   217 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
   218 ** string contains the date and time of the check-in (UTC) and an SHA1
   219 ** hash of the entire source tree.
   220 **
   221 ** See also: [sqlite3_libversion()],
   222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   223 ** [sqlite_version()] and [sqlite_source_id()].
   224 */
   225 #define SQLITE_VERSION        "3.8.4.2"
   226 #define SQLITE_VERSION_NUMBER 3008004
   227 #define SQLITE_SOURCE_ID      "2014-03-26 18:51:19 02ea166372bdb2ef9d8dfbb05e78a97609673a8e"
   229 /*
   230 ** CAPI3REF: Run-Time Library Version Numbers
   231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   232 **
   233 ** These interfaces provide the same information as the [SQLITE_VERSION],
   234 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
   235 ** but are associated with the library instead of the header file.  ^(Cautious
   236 ** programmers might include assert() statements in their application to
   237 ** verify that values returned by these interfaces match the macros in
   238 ** the header, and thus insure that the application is
   239 ** compiled with matching library and header files.
   240 **
   241 ** <blockquote><pre>
   242 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
   243 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
   244 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
   245 ** </pre></blockquote>)^
   246 **
   247 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
   248 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
   249 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
   250 ** function is provided for use in DLLs since DLL users usually do not have
   251 ** direct access to string constants within the DLL.  ^The
   252 ** sqlite3_libversion_number() function returns an integer equal to
   253 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
   254 ** a pointer to a string constant whose value is the same as the 
   255 ** [SQLITE_SOURCE_ID] C preprocessor macro.
   256 **
   257 ** See also: [sqlite_version()] and [sqlite_source_id()].
   258 */
   259 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
   260 SQLITE_API const char *sqlite3_libversion(void);
   261 SQLITE_API const char *sqlite3_sourceid(void);
   262 SQLITE_API int sqlite3_libversion_number(void);
   264 /*
   265 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
   266 **
   267 ** ^The sqlite3_compileoption_used() function returns 0 or 1 
   268 ** indicating whether the specified option was defined at 
   269 ** compile time.  ^The SQLITE_ prefix may be omitted from the 
   270 ** option name passed to sqlite3_compileoption_used().  
   271 **
   272 ** ^The sqlite3_compileoption_get() function allows iterating
   273 ** over the list of options that were defined at compile time by
   274 ** returning the N-th compile time option string.  ^If N is out of range,
   275 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
   276 ** prefix is omitted from any strings returned by 
   277 ** sqlite3_compileoption_get().
   278 **
   279 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
   280 ** and sqlite3_compileoption_get() may be omitted by specifying the 
   281 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
   282 **
   283 ** See also: SQL functions [sqlite_compileoption_used()] and
   284 ** [sqlite_compileoption_get()] and the [compile_options pragma].
   285 */
   286 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   287 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
   288 SQLITE_API const char *sqlite3_compileoption_get(int N);
   289 #endif
   291 /*
   292 ** CAPI3REF: Test To See If The Library Is Threadsafe
   293 **
   294 ** ^The sqlite3_threadsafe() function returns zero if and only if
   295 ** SQLite was compiled with mutexing code omitted due to the
   296 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
   297 **
   298 ** SQLite can be compiled with or without mutexes.  When
   299 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
   300 ** are enabled and SQLite is threadsafe.  When the
   301 ** [SQLITE_THREADSAFE] macro is 0, 
   302 ** the mutexes are omitted.  Without the mutexes, it is not safe
   303 ** to use SQLite concurrently from more than one thread.
   304 **
   305 ** Enabling mutexes incurs a measurable performance penalty.
   306 ** So if speed is of utmost importance, it makes sense to disable
   307 ** the mutexes.  But for maximum safety, mutexes should be enabled.
   308 ** ^The default behavior is for mutexes to be enabled.
   309 **
   310 ** This interface can be used by an application to make sure that the
   311 ** version of SQLite that it is linking against was compiled with
   312 ** the desired setting of the [SQLITE_THREADSAFE] macro.
   313 **
   314 ** This interface only reports on the compile-time mutex setting
   315 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
   316 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
   317 ** can be fully or partially disabled using a call to [sqlite3_config()]
   318 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
   319 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
   320 ** sqlite3_threadsafe() function shows only the compile-time setting of
   321 ** thread safety, not any run-time changes to that setting made by
   322 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
   323 ** is unchanged by calls to sqlite3_config().)^
   324 **
   325 ** See the [threading mode] documentation for additional information.
   326 */
   327 SQLITE_API int sqlite3_threadsafe(void);
   329 /*
   330 ** CAPI3REF: Database Connection Handle
   331 ** KEYWORDS: {database connection} {database connections}
   332 **
   333 ** Each open SQLite database is represented by a pointer to an instance of
   334 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
   335 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
   336 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
   337 ** and [sqlite3_close_v2()] are its destructors.  There are many other
   338 ** interfaces (such as
   339 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
   340 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
   341 ** sqlite3 object.
   342 */
   343 typedef struct sqlite3 sqlite3;
   345 /*
   346 ** CAPI3REF: 64-Bit Integer Types
   347 ** KEYWORDS: sqlite_int64 sqlite_uint64
   348 **
   349 ** Because there is no cross-platform way to specify 64-bit integer types
   350 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
   351 **
   352 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
   353 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
   354 ** compatibility only.
   355 **
   356 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
   357 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
   358 ** sqlite3_uint64 and sqlite_uint64 types can store integer values 
   359 ** between 0 and +18446744073709551615 inclusive.
   360 */
   361 #ifdef SQLITE_INT64_TYPE
   362   typedef SQLITE_INT64_TYPE sqlite_int64;
   363   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
   364 #elif defined(_MSC_VER) || defined(__BORLANDC__)
   365   typedef __int64 sqlite_int64;
   366   typedef unsigned __int64 sqlite_uint64;
   367 #else
   368   typedef long long int sqlite_int64;
   369   typedef unsigned long long int sqlite_uint64;
   370 #endif
   371 typedef sqlite_int64 sqlite3_int64;
   372 typedef sqlite_uint64 sqlite3_uint64;
   374 /*
   375 ** If compiling for a processor that lacks floating point support,
   376 ** substitute integer for floating-point.
   377 */
   378 #ifdef SQLITE_OMIT_FLOATING_POINT
   379 # define double sqlite3_int64
   380 #endif
   382 /*
   383 ** CAPI3REF: Closing A Database Connection
   384 **
   385 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
   386 ** for the [sqlite3] object.
   387 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
   388 ** the [sqlite3] object is successfully destroyed and all associated
   389 ** resources are deallocated.
   390 **
   391 ** ^If the database connection is associated with unfinalized prepared
   392 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
   393 ** will leave the database connection open and return [SQLITE_BUSY].
   394 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
   395 ** and unfinished sqlite3_backups, then the database connection becomes
   396 ** an unusable "zombie" which will automatically be deallocated when the
   397 ** last prepared statement is finalized or the last sqlite3_backup is
   398 ** finished.  The sqlite3_close_v2() interface is intended for use with
   399 ** host languages that are garbage collected, and where the order in which
   400 ** destructors are called is arbitrary.
   401 **
   402 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
   403 ** [sqlite3_blob_close | close] all [BLOB handles], and 
   404 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
   405 ** with the [sqlite3] object prior to attempting to close the object.  ^If
   406 ** sqlite3_close_v2() is called on a [database connection] that still has
   407 ** outstanding [prepared statements], [BLOB handles], and/or
   408 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
   409 ** of resources is deferred until all [prepared statements], [BLOB handles],
   410 ** and [sqlite3_backup] objects are also destroyed.
   411 **
   412 ** ^If an [sqlite3] object is destroyed while a transaction is open,
   413 ** the transaction is automatically rolled back.
   414 **
   415 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
   416 ** must be either a NULL
   417 ** pointer or an [sqlite3] object pointer obtained
   418 ** from [sqlite3_open()], [sqlite3_open16()], or
   419 ** [sqlite3_open_v2()], and not previously closed.
   420 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
   421 ** argument is a harmless no-op.
   422 */
   423 SQLITE_API int sqlite3_close(sqlite3*);
   424 SQLITE_API int sqlite3_close_v2(sqlite3*);
   426 /*
   427 ** The type for a callback function.
   428 ** This is legacy and deprecated.  It is included for historical
   429 ** compatibility and is not documented.
   430 */
   431 typedef int (*sqlite3_callback)(void*,int,char**, char**);
   433 /*
   434 ** CAPI3REF: One-Step Query Execution Interface
   435 **
   436 ** The sqlite3_exec() interface is a convenience wrapper around
   437 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
   438 ** that allows an application to run multiple statements of SQL
   439 ** without having to use a lot of C code. 
   440 **
   441 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
   442 ** semicolon-separate SQL statements passed into its 2nd argument,
   443 ** in the context of the [database connection] passed in as its 1st
   444 ** argument.  ^If the callback function of the 3rd argument to
   445 ** sqlite3_exec() is not NULL, then it is invoked for each result row
   446 ** coming out of the evaluated SQL statements.  ^The 4th argument to
   447 ** sqlite3_exec() is relayed through to the 1st argument of each
   448 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
   449 ** is NULL, then no callback is ever invoked and result rows are
   450 ** ignored.
   451 **
   452 ** ^If an error occurs while evaluating the SQL statements passed into
   453 ** sqlite3_exec(), then execution of the current statement stops and
   454 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
   455 ** is not NULL then any error message is written into memory obtained
   456 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
   457 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
   458 ** on error message strings returned through the 5th parameter of
   459 ** of sqlite3_exec() after the error message string is no longer needed.
   460 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
   461 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
   462 ** NULL before returning.
   463 **
   464 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
   465 ** routine returns SQLITE_ABORT without invoking the callback again and
   466 ** without running any subsequent SQL statements.
   467 **
   468 ** ^The 2nd argument to the sqlite3_exec() callback function is the
   469 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
   470 ** callback is an array of pointers to strings obtained as if from
   471 ** [sqlite3_column_text()], one for each column.  ^If an element of a
   472 ** result row is NULL then the corresponding string pointer for the
   473 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
   474 ** sqlite3_exec() callback is an array of pointers to strings where each
   475 ** entry represents the name of corresponding result column as obtained
   476 ** from [sqlite3_column_name()].
   477 **
   478 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
   479 ** to an empty string, or a pointer that contains only whitespace and/or 
   480 ** SQL comments, then no SQL statements are evaluated and the database
   481 ** is not changed.
   482 **
   483 ** Restrictions:
   484 **
   485 ** <ul>
   486 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
   487 **      is a valid and open [database connection].
   488 ** <li> The application must not close the [database connection] specified by
   489 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
   490 ** <li> The application must not modify the SQL statement text passed into
   491 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
   492 ** </ul>
   493 */
   494 SQLITE_API int sqlite3_exec(
   495   sqlite3*,                                  /* An open database */
   496   const char *sql,                           /* SQL to be evaluated */
   497   int (*callback)(void*,int,char**,char**),  /* Callback function */
   498   void *,                                    /* 1st argument to callback */
   499   char **errmsg                              /* Error msg written here */
   500 );
   502 /*
   503 ** CAPI3REF: Result Codes
   504 ** KEYWORDS: SQLITE_OK {error code} {error codes}
   505 ** KEYWORDS: {result code} {result codes}
   506 **
   507 ** Many SQLite functions return an integer result code from the set shown
   508 ** here in order to indicate success or failure.
   509 **
   510 ** New error codes may be added in future versions of SQLite.
   511 **
   512 ** See also: [SQLITE_IOERR_READ | extended result codes],
   513 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
   514 */
   515 #define SQLITE_OK           0   /* Successful result */
   516 /* beginning-of-error-codes */
   517 #define SQLITE_ERROR        1   /* SQL error or missing database */
   518 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
   519 #define SQLITE_PERM         3   /* Access permission denied */
   520 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
   521 #define SQLITE_BUSY         5   /* The database file is locked */
   522 #define SQLITE_LOCKED       6   /* A table in the database is locked */
   523 #define SQLITE_NOMEM        7   /* A malloc() failed */
   524 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
   525 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
   526 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
   527 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
   528 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
   529 #define SQLITE_FULL        13   /* Insertion failed because database is full */
   530 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
   531 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
   532 #define SQLITE_EMPTY       16   /* Database is empty */
   533 #define SQLITE_SCHEMA      17   /* The database schema changed */
   534 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
   535 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
   536 #define SQLITE_MISMATCH    20   /* Data type mismatch */
   537 #define SQLITE_MISUSE      21   /* Library used incorrectly */
   538 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
   539 #define SQLITE_AUTH        23   /* Authorization denied */
   540 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
   541 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
   542 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
   543 #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
   544 #define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
   545 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
   546 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
   547 /* end-of-error-codes */
   549 /*
   550 ** CAPI3REF: Extended Result Codes
   551 ** KEYWORDS: {extended error code} {extended error codes}
   552 ** KEYWORDS: {extended result code} {extended result codes}
   553 **
   554 ** In its default configuration, SQLite API routines return one of 26 integer
   555 ** [SQLITE_OK | result codes].  However, experience has shown that many of
   556 ** these result codes are too coarse-grained.  They do not provide as
   557 ** much information about problems as programmers might like.  In an effort to
   558 ** address this, newer versions of SQLite (version 3.3.8 and later) include
   559 ** support for additional result codes that provide more detailed information
   560 ** about errors. The extended result codes are enabled or disabled
   561 ** on a per database connection basis using the
   562 ** [sqlite3_extended_result_codes()] API.
   563 **
   564 ** Some of the available extended result codes are listed here.
   565 ** One may expect the number of extended result codes will increase
   566 ** over time.  Software that uses extended result codes should expect
   567 ** to see new result codes in future releases of SQLite.
   568 **
   569 ** The SQLITE_OK result code will never be extended.  It will always
   570 ** be exactly zero.
   571 */
   572 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
   573 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
   574 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
   575 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
   576 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
   577 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
   578 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
   579 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
   580 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
   581 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
   582 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
   583 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
   584 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
   585 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
   586 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
   587 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
   588 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
   589 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
   590 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
   591 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   592 #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   593 #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   594 #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
   595 #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
   596 #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
   597 #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
   598 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   599 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   600 #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
   601 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   602 #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   603 #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   604 #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
   605 #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   606 #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   607 #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   608 #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
   609 #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
   610 #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   611 #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
   612 #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
   613 #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
   614 #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
   615 #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
   616 #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
   617 #define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
   618 #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
   619 #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
   620 #define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
   621 #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
   622 #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
   623 #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
   625 /*
   626 ** CAPI3REF: Flags For File Open Operations
   627 **
   628 ** These bit values are intended for use in the
   629 ** 3rd parameter to the [sqlite3_open_v2()] interface and
   630 ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
   631 */
   632 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
   633 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
   634 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
   635 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
   636 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
   637 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
   638 #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
   639 #define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
   640 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
   641 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
   642 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
   643 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
   644 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
   645 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
   646 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
   647 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
   648 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
   649 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
   650 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
   651 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
   653 /* Reserved:                         0x00F00000 */
   655 /*
   656 ** CAPI3REF: Device Characteristics
   657 **
   658 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
   659 ** object returns an integer which is a vector of these
   660 ** bit values expressing I/O characteristics of the mass storage
   661 ** device that holds the file that the [sqlite3_io_methods]
   662 ** refers to.
   663 **
   664 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   665 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   666 ** mean that writes of blocks that are nnn bytes in size and
   667 ** are aligned to an address which is an integer multiple of
   668 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
   669 ** that when data is appended to a file, the data is appended
   670 ** first then the size of the file is extended, never the other
   671 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   672 ** information is written to disk in the same order as calls
   673 ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
   674 ** after reboot following a crash or power loss, the only bytes in a
   675 ** file that were written at the application level might have changed
   676 ** and that adjacent bytes, even bytes within the same sector are
   677 ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   678 ** flag indicate that a file cannot be deleted when open.
   679 */
   680 #define SQLITE_IOCAP_ATOMIC                 0x00000001
   681 #define SQLITE_IOCAP_ATOMIC512              0x00000002
   682 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
   683 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
   684 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
   685 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
   686 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
   687 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
   688 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
   689 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
   690 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
   691 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
   692 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
   694 /*
   695 ** CAPI3REF: File Locking Levels
   696 **
   697 ** SQLite uses one of these integer values as the second
   698 ** argument to calls it makes to the xLock() and xUnlock() methods
   699 ** of an [sqlite3_io_methods] object.
   700 */
   701 #define SQLITE_LOCK_NONE          0
   702 #define SQLITE_LOCK_SHARED        1
   703 #define SQLITE_LOCK_RESERVED      2
   704 #define SQLITE_LOCK_PENDING       3
   705 #define SQLITE_LOCK_EXCLUSIVE     4
   707 /*
   708 ** CAPI3REF: Synchronization Type Flags
   709 **
   710 ** When SQLite invokes the xSync() method of an
   711 ** [sqlite3_io_methods] object it uses a combination of
   712 ** these integer values as the second argument.
   713 **
   714 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
   715 ** sync operation only needs to flush data to mass storage.  Inode
   716 ** information need not be flushed. If the lower four bits of the flag
   717 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
   718 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
   719 ** to use Mac OS X style fullsync instead of fsync().
   720 **
   721 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
   722 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
   723 ** settings.  The [synchronous pragma] determines when calls to the
   724 ** xSync VFS method occur and applies uniformly across all platforms.
   725 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
   726 ** energetic or rigorous or forceful the sync operations are and
   727 ** only make a difference on Mac OSX for the default SQLite code.
   728 ** (Third-party VFS implementations might also make the distinction
   729 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
   730 ** operating systems natively supported by SQLite, only Mac OSX
   731 ** cares about the difference.)
   732 */
   733 #define SQLITE_SYNC_NORMAL        0x00002
   734 #define SQLITE_SYNC_FULL          0x00003
   735 #define SQLITE_SYNC_DATAONLY      0x00010
   737 /*
   738 ** CAPI3REF: OS Interface Open File Handle
   739 **
   740 ** An [sqlite3_file] object represents an open file in the 
   741 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
   742 ** implementations will
   743 ** want to subclass this object by appending additional fields
   744 ** for their own use.  The pMethods entry is a pointer to an
   745 ** [sqlite3_io_methods] object that defines methods for performing
   746 ** I/O operations on the open file.
   747 */
   748 typedef struct sqlite3_file sqlite3_file;
   749 struct sqlite3_file {
   750   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
   751 };
   753 /*
   754 ** CAPI3REF: OS Interface File Virtual Methods Object
   755 **
   756 ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
   757 ** [sqlite3_file] object (or, more commonly, a subclass of the
   758 ** [sqlite3_file] object) with a pointer to an instance of this object.
   759 ** This object defines the methods used to perform various operations
   760 ** against the open file represented by the [sqlite3_file] object.
   761 **
   762 ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element 
   763 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
   764 ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
   765 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
   766 ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
   767 ** to NULL.
   768 **
   769 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
   770 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
   771 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
   772 ** flag may be ORed in to indicate that only the data of the file
   773 ** and not its inode needs to be synced.
   774 **
   775 ** The integer values to xLock() and xUnlock() are one of
   776 ** <ul>
   777 ** <li> [SQLITE_LOCK_NONE],
   778 ** <li> [SQLITE_LOCK_SHARED],
   779 ** <li> [SQLITE_LOCK_RESERVED],
   780 ** <li> [SQLITE_LOCK_PENDING], or
   781 ** <li> [SQLITE_LOCK_EXCLUSIVE].
   782 ** </ul>
   783 ** xLock() increases the lock. xUnlock() decreases the lock.
   784 ** The xCheckReservedLock() method checks whether any database connection,
   785 ** either in this process or in some other process, is holding a RESERVED,
   786 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
   787 ** if such a lock exists and false otherwise.
   788 **
   789 ** The xFileControl() method is a generic interface that allows custom
   790 ** VFS implementations to directly control an open file using the
   791 ** [sqlite3_file_control()] interface.  The second "op" argument is an
   792 ** integer opcode.  The third argument is a generic pointer intended to
   793 ** point to a structure that may contain arguments or space in which to
   794 ** write return values.  Potential uses for xFileControl() might be
   795 ** functions to enable blocking locks with timeouts, to change the
   796 ** locking strategy (for example to use dot-file locks), to inquire
   797 ** about the status of a lock, or to break stale locks.  The SQLite
   798 ** core reserves all opcodes less than 100 for its own use.
   799 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
   800 ** Applications that define a custom xFileControl method should use opcodes
   801 ** greater than 100 to avoid conflicts.  VFS implementations should
   802 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
   803 ** recognize.
   804 **
   805 ** The xSectorSize() method returns the sector size of the
   806 ** device that underlies the file.  The sector size is the
   807 ** minimum write that can be performed without disturbing
   808 ** other bytes in the file.  The xDeviceCharacteristics()
   809 ** method returns a bit vector describing behaviors of the
   810 ** underlying device:
   811 **
   812 ** <ul>
   813 ** <li> [SQLITE_IOCAP_ATOMIC]
   814 ** <li> [SQLITE_IOCAP_ATOMIC512]
   815 ** <li> [SQLITE_IOCAP_ATOMIC1K]
   816 ** <li> [SQLITE_IOCAP_ATOMIC2K]
   817 ** <li> [SQLITE_IOCAP_ATOMIC4K]
   818 ** <li> [SQLITE_IOCAP_ATOMIC8K]
   819 ** <li> [SQLITE_IOCAP_ATOMIC16K]
   820 ** <li> [SQLITE_IOCAP_ATOMIC32K]
   821 ** <li> [SQLITE_IOCAP_ATOMIC64K]
   822 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
   823 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
   824 ** </ul>
   825 **
   826 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
   827 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
   828 ** mean that writes of blocks that are nnn bytes in size and
   829 ** are aligned to an address which is an integer multiple of
   830 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
   831 ** that when data is appended to a file, the data is appended
   832 ** first then the size of the file is extended, never the other
   833 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
   834 ** information is written to disk in the same order as calls
   835 ** to xWrite().
   836 **
   837 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
   838 ** in the unread portions of the buffer with zeros.  A VFS that
   839 ** fails to zero-fill short reads might seem to work.  However,
   840 ** failure to zero-fill short reads will eventually lead to
   841 ** database corruption.
   842 */
   843 typedef struct sqlite3_io_methods sqlite3_io_methods;
   844 struct sqlite3_io_methods {
   845   int iVersion;
   846   int (*xClose)(sqlite3_file*);
   847   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
   848   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
   849   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
   850   int (*xSync)(sqlite3_file*, int flags);
   851   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
   852   int (*xLock)(sqlite3_file*, int);
   853   int (*xUnlock)(sqlite3_file*, int);
   854   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
   855   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
   856   int (*xSectorSize)(sqlite3_file*);
   857   int (*xDeviceCharacteristics)(sqlite3_file*);
   858   /* Methods above are valid for version 1 */
   859   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
   860   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
   861   void (*xShmBarrier)(sqlite3_file*);
   862   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
   863   /* Methods above are valid for version 2 */
   864   int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
   865   int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
   866   /* Methods above are valid for version 3 */
   867   /* Additional methods may be added in future releases */
   868 };
   870 /*
   871 ** CAPI3REF: Standard File Control Opcodes
   872 **
   873 ** These integer constants are opcodes for the xFileControl method
   874 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
   875 ** interface.
   876 **
   877 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
   878 ** opcode causes the xFileControl method to write the current state of
   879 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
   880 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
   881 ** into an integer that the pArg argument points to. This capability
   882 ** is used during testing and only needs to be supported when SQLITE_TEST
   883 ** is defined.
   884 ** <ul>
   885 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
   886 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
   887 ** layer a hint of how large the database file will grow to be during the
   888 ** current transaction.  This hint is not guaranteed to be accurate but it
   889 ** is often close.  The underlying VFS might choose to preallocate database
   890 ** file space based on this hint in order to help writes to the database
   891 ** file run faster.
   892 **
   893 ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
   894 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
   895 ** extends and truncates the database file in chunks of a size specified
   896 ** by the user. The fourth argument to [sqlite3_file_control()] should 
   897 ** point to an integer (type int) containing the new chunk-size to use
   898 ** for the nominated database. Allocating database file space in large
   899 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
   900 ** improve performance on some systems.
   901 **
   902 ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
   903 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
   904 ** to the [sqlite3_file] object associated with a particular database
   905 ** connection.  See the [sqlite3_file_control()] documentation for
   906 ** additional information.
   907 **
   908 ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
   909 ** No longer in use.
   910 **
   911 ** <li>[[SQLITE_FCNTL_SYNC]]
   912 ** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
   913 ** sent to the VFS immediately before the xSync method is invoked on a
   914 ** database file descriptor. Or, if the xSync method is not invoked 
   915 ** because the user has configured SQLite with 
   916 ** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place 
   917 ** of the xSync method. In most cases, the pointer argument passed with
   918 ** this file-control is NULL. However, if the database file is being synced
   919 ** as part of a multi-database commit, the argument points to a nul-terminated
   920 ** string containing the transactions master-journal file name. VFSes that 
   921 ** do not need this signal should silently ignore this opcode. Applications 
   922 ** should not call [sqlite3_file_control()] with this opcode as doing so may 
   923 ** disrupt the operation of the specialized VFSes that do require it.  
   924 **
   925 ** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
   926 ** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
   927 ** and sent to the VFS after a transaction has been committed immediately
   928 ** but before the database is unlocked. VFSes that do not need this signal
   929 ** should silently ignore this opcode. Applications should not call
   930 ** [sqlite3_file_control()] with this opcode as doing so may disrupt the 
   931 ** operation of the specialized VFSes that do require it.  
   932 **
   933 ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
   934 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
   935 ** retry counts and intervals for certain disk I/O operations for the
   936 ** windows [VFS] in order to provide robustness in the presence of
   937 ** anti-virus programs.  By default, the windows VFS will retry file read,
   938 ** file write, and file delete operations up to 10 times, with a delay
   939 ** of 25 milliseconds before the first retry and with the delay increasing
   940 ** by an additional 25 milliseconds with each subsequent retry.  This
   941 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
   942 ** to be adjusted.  The values are changed for all database connections
   943 ** within the same process.  The argument is a pointer to an array of two
   944 ** integers where the first integer i the new retry count and the second
   945 ** integer is the delay.  If either integer is negative, then the setting
   946 ** is not changed but instead the prior value of that setting is written
   947 ** into the array entry, allowing the current retry settings to be
   948 ** interrogated.  The zDbName parameter is ignored.
   949 **
   950 ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
   951 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
   952 ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
   953 ** write ahead log and shared memory files used for transaction control
   954 ** are automatically deleted when the latest connection to the database
   955 ** closes.  Setting persistent WAL mode causes those files to persist after
   956 ** close.  Persisting the files is useful when other processes that do not
   957 ** have write permission on the directory containing the database file want
   958 ** to read the database file, as the WAL and shared memory files must exist
   959 ** in order for the database to be readable.  The fourth parameter to
   960 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
   961 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
   962 ** WAL mode.  If the integer is -1, then it is overwritten with the current
   963 ** WAL persistence setting.
   964 **
   965 ** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
   966 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
   967 ** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
   968 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
   969 ** xDeviceCharacteristics methods. The fourth parameter to
   970 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
   971 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
   972 ** mode.  If the integer is -1, then it is overwritten with the current
   973 ** zero-damage mode setting.
   974 **
   975 ** <li>[[SQLITE_FCNTL_OVERWRITE]]
   976 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
   977 ** a write transaction to indicate that, unless it is rolled back for some
   978 ** reason, the entire database file will be overwritten by the current 
   979 ** transaction. This is used by VACUUM operations.
   980 **
   981 ** <li>[[SQLITE_FCNTL_VFSNAME]]
   982 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
   983 ** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
   984 ** final bottom-level VFS are written into memory obtained from 
   985 ** [sqlite3_malloc()] and the result is stored in the char* variable
   986 ** that the fourth parameter of [sqlite3_file_control()] points to.
   987 ** The caller is responsible for freeing the memory when done.  As with
   988 ** all file-control actions, there is no guarantee that this will actually
   989 ** do anything.  Callers should initialize the char* variable to a NULL
   990 ** pointer in case this file-control is not implemented.  This file-control
   991 ** is intended for diagnostic use only.
   992 **
   993 ** <li>[[SQLITE_FCNTL_PRAGMA]]
   994 ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
   995 ** file control is sent to the open [sqlite3_file] object corresponding
   996 ** to the database file to which the pragma statement refers. ^The argument
   997 ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
   998 ** pointers to strings (char**) in which the second element of the array
   999 ** is the name of the pragma and the third element is the argument to the
  1000 ** pragma or NULL if the pragma has no argument.  ^The handler for an
  1001 ** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
  1002 ** of the char** argument point to a string obtained from [sqlite3_mprintf()]
  1003 ** or the equivalent and that string will become the result of the pragma or
  1004 ** the error message if the pragma fails. ^If the
  1005 ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal 
  1006 ** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
  1007 ** file control returns [SQLITE_OK], then the parser assumes that the
  1008 ** VFS has handled the PRAGMA itself and the parser generates a no-op
  1009 ** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
  1010 ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
  1011 ** that the VFS encountered an error while handling the [PRAGMA] and the
  1012 ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
  1013 ** file control occurs at the beginning of pragma statement analysis and so
  1014 ** it is able to override built-in [PRAGMA] statements.
  1015 **
  1016 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
  1017 ** ^The [SQLITE_FCNTL_BUSYHANDLER]
  1018 ** file-control may be invoked by SQLite on the database file handle
  1019 ** shortly after it is opened in order to provide a custom VFS with access
  1020 ** to the connections busy-handler callback. The argument is of type (void **)
  1021 ** - an array of two (void *) values. The first (void *) actually points
  1022 ** to a function of type (int (*)(void *)). In order to invoke the connections
  1023 ** busy-handler, this function should be invoked with the second (void *) in
  1024 ** the array as the only argument. If it returns non-zero, then the operation
  1025 ** should be retried. If it returns zero, the custom VFS should abandon the
  1026 ** current operation.
  1027 **
  1028 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
  1029 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
  1030 ** to have SQLite generate a
  1031 ** temporary filename using the same algorithm that is followed to generate
  1032 ** temporary filenames for TEMP tables and other internal uses.  The
  1033 ** argument should be a char** which will be filled with the filename
  1034 ** written into memory obtained from [sqlite3_malloc()].  The caller should
  1035 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
  1036 **
  1037 ** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
  1038 ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
  1039 ** maximum number of bytes that will be used for memory-mapped I/O.
  1040 ** The argument is a pointer to a value of type sqlite3_int64 that
  1041 ** is an advisory maximum number of bytes in the file to memory map.  The
  1042 ** pointer is overwritten with the old value.  The limit is not changed if
  1043 ** the value originally pointed to is negative, and so the current limit 
  1044 ** can be queried by passing in a pointer to a negative number.  This
  1045 ** file-control is used internally to implement [PRAGMA mmap_size].
  1046 **
  1047 ** <li>[[SQLITE_FCNTL_TRACE]]
  1048 ** The [SQLITE_FCNTL_TRACE] file control provides advisory information
  1049 ** to the VFS about what the higher layers of the SQLite stack are doing.
  1050 ** This file control is used by some VFS activity tracing [shims].
  1051 ** The argument is a zero-terminated string.  Higher layers in the
  1052 ** SQLite stack may generate instances of this file control if
  1053 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
  1054 **
  1055 ** <li>[[SQLITE_FCNTL_HAS_MOVED]]
  1056 ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
  1057 ** pointer to an integer and it writes a boolean into that integer depending
  1058 ** on whether or not the file has been renamed, moved, or deleted since it
  1059 ** was first opened.
  1060 **
  1061 ** </ul>
  1062 */
  1063 #define SQLITE_FCNTL_LOCKSTATE               1
  1064 #define SQLITE_GET_LOCKPROXYFILE             2
  1065 #define SQLITE_SET_LOCKPROXYFILE             3
  1066 #define SQLITE_LAST_ERRNO                    4
  1067 #define SQLITE_FCNTL_SIZE_HINT               5
  1068 #define SQLITE_FCNTL_CHUNK_SIZE              6
  1069 #define SQLITE_FCNTL_FILE_POINTER            7
  1070 #define SQLITE_FCNTL_SYNC_OMITTED            8
  1071 #define SQLITE_FCNTL_WIN32_AV_RETRY          9
  1072 #define SQLITE_FCNTL_PERSIST_WAL            10
  1073 #define SQLITE_FCNTL_OVERWRITE              11
  1074 #define SQLITE_FCNTL_VFSNAME                12
  1075 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
  1076 #define SQLITE_FCNTL_PRAGMA                 14
  1077 #define SQLITE_FCNTL_BUSYHANDLER            15
  1078 #define SQLITE_FCNTL_TEMPFILENAME           16
  1079 #define SQLITE_FCNTL_MMAP_SIZE              18
  1080 #define SQLITE_FCNTL_TRACE                  19
  1081 #define SQLITE_FCNTL_HAS_MOVED              20
  1082 #define SQLITE_FCNTL_SYNC                   21
  1083 #define SQLITE_FCNTL_COMMIT_PHASETWO        22
  1085 /*
  1086 ** CAPI3REF: Mutex Handle
  1087 **
  1088 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1089 ** abstract type for a mutex object.  The SQLite core never looks
  1090 ** at the internal representation of an [sqlite3_mutex].  It only
  1091 ** deals with pointers to the [sqlite3_mutex] object.
  1092 **
  1093 ** Mutexes are created using [sqlite3_mutex_alloc()].
  1094 */
  1095 typedef struct sqlite3_mutex sqlite3_mutex;
  1097 /*
  1098 ** CAPI3REF: OS Interface Object
  1099 **
  1100 ** An instance of the sqlite3_vfs object defines the interface between
  1101 ** the SQLite core and the underlying operating system.  The "vfs"
  1102 ** in the name of the object stands for "virtual file system".  See
  1103 ** the [VFS | VFS documentation] for further information.
  1104 **
  1105 ** The value of the iVersion field is initially 1 but may be larger in
  1106 ** future versions of SQLite.  Additional fields may be appended to this
  1107 ** object when the iVersion value is increased.  Note that the structure
  1108 ** of the sqlite3_vfs object changes in the transaction between
  1109 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  1110 ** modified.
  1111 **
  1112 ** The szOsFile field is the size of the subclassed [sqlite3_file]
  1113 ** structure used by this VFS.  mxPathname is the maximum length of
  1114 ** a pathname in this VFS.
  1115 **
  1116 ** Registered sqlite3_vfs objects are kept on a linked list formed by
  1117 ** the pNext pointer.  The [sqlite3_vfs_register()]
  1118 ** and [sqlite3_vfs_unregister()] interfaces manage this list
  1119 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
  1120 ** searches the list.  Neither the application code nor the VFS
  1121 ** implementation should use the pNext pointer.
  1122 **
  1123 ** The pNext field is the only field in the sqlite3_vfs
  1124 ** structure that SQLite will ever modify.  SQLite will only access
  1125 ** or modify this field while holding a particular static mutex.
  1126 ** The application should never modify anything within the sqlite3_vfs
  1127 ** object once the object has been registered.
  1128 **
  1129 ** The zName field holds the name of the VFS module.  The name must
  1130 ** be unique across all VFS modules.
  1131 **
  1132 ** [[sqlite3_vfs.xOpen]]
  1133 ** ^SQLite guarantees that the zFilename parameter to xOpen
  1134 ** is either a NULL pointer or string obtained
  1135 ** from xFullPathname() with an optional suffix added.
  1136 ** ^If a suffix is added to the zFilename parameter, it will
  1137 ** consist of a single "-" character followed by no more than
  1138 ** 11 alphanumeric and/or "-" characters.
  1139 ** ^SQLite further guarantees that
  1140 ** the string will be valid and unchanged until xClose() is
  1141 ** called. Because of the previous sentence,
  1142 ** the [sqlite3_file] can safely store a pointer to the
  1143 ** filename if it needs to remember the filename for some reason.
  1144 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
  1145 ** must invent its own temporary name for the file.  ^Whenever the 
  1146 ** xFilename parameter is NULL it will also be the case that the
  1147 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
  1148 **
  1149 ** The flags argument to xOpen() includes all bits set in
  1150 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
  1151 ** or [sqlite3_open16()] is used, then flags includes at least
  1152 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
  1153 ** If xOpen() opens a file read-only then it sets *pOutFlags to
  1154 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
  1155 **
  1156 ** ^(SQLite will also add one of the following flags to the xOpen()
  1157 ** call, depending on the object being opened:
  1158 **
  1159 ** <ul>
  1160 ** <li>  [SQLITE_OPEN_MAIN_DB]
  1161 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
  1162 ** <li>  [SQLITE_OPEN_TEMP_DB]
  1163 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
  1164 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
  1165 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
  1166 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
  1167 ** <li>  [SQLITE_OPEN_WAL]
  1168 ** </ul>)^
  1169 **
  1170 ** The file I/O implementation can use the object type flags to
  1171 ** change the way it deals with files.  For example, an application
  1172 ** that does not care about crash recovery or rollback might make
  1173 ** the open of a journal file a no-op.  Writes to this journal would
  1174 ** also be no-ops, and any attempt to read the journal would return
  1175 ** SQLITE_IOERR.  Or the implementation might recognize that a database
  1176 ** file will be doing page-aligned sector reads and writes in a random
  1177 ** order and set up its I/O subsystem accordingly.
  1178 **
  1179 ** SQLite might also add one of the following flags to the xOpen method:
  1180 **
  1181 ** <ul>
  1182 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
  1183 ** <li> [SQLITE_OPEN_EXCLUSIVE]
  1184 ** </ul>
  1185 **
  1186 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
  1187 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
  1188 ** will be set for TEMP databases and their journals, transient
  1189 ** databases, and subjournals.
  1190 **
  1191 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
  1192 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
  1193 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
  1194 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
  1195 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
  1196 ** be created, and that it is an error if it already exists.
  1197 ** It is <i>not</i> used to indicate the file should be opened 
  1198 ** for exclusive access.
  1199 **
  1200 ** ^At least szOsFile bytes of memory are allocated by SQLite
  1201 ** to hold the  [sqlite3_file] structure passed as the third
  1202 ** argument to xOpen.  The xOpen method does not have to
  1203 ** allocate the structure; it should just fill it in.  Note that
  1204 ** the xOpen method must set the sqlite3_file.pMethods to either
  1205 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
  1206 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
  1207 ** element will be valid after xOpen returns regardless of the success
  1208 ** or failure of the xOpen call.
  1209 **
  1210 ** [[sqlite3_vfs.xAccess]]
  1211 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
  1212 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
  1213 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
  1214 ** to test whether a file is at least readable.   The file can be a
  1215 ** directory.
  1216 **
  1217 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
  1218 ** output buffer xFullPathname.  The exact size of the output buffer
  1219 ** is also passed as a parameter to both  methods. If the output buffer
  1220 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
  1221 ** handled as a fatal error by SQLite, vfs implementations should endeavor
  1222 ** to prevent this by setting mxPathname to a sufficiently large value.
  1223 **
  1224 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
  1225 ** interfaces are not strictly a part of the filesystem, but they are
  1226 ** included in the VFS structure for completeness.
  1227 ** The xRandomness() function attempts to return nBytes bytes
  1228 ** of good-quality randomness into zOut.  The return value is
  1229 ** the actual number of bytes of randomness obtained.
  1230 ** The xSleep() method causes the calling thread to sleep for at
  1231 ** least the number of microseconds given.  ^The xCurrentTime()
  1232 ** method returns a Julian Day Number for the current date and time as
  1233 ** a floating point value.
  1234 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
  1235 ** Day Number multiplied by 86400000 (the number of milliseconds in 
  1236 ** a 24-hour day).  
  1237 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
  1238 ** date and time if that method is available (if iVersion is 2 or 
  1239 ** greater and the function pointer is not NULL) and will fall back
  1240 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
  1241 **
  1242 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
  1243 ** are not used by the SQLite core.  These optional interfaces are provided
  1244 ** by some VFSes to facilitate testing of the VFS code. By overriding 
  1245 ** system calls with functions under its control, a test program can
  1246 ** simulate faults and error conditions that would otherwise be difficult
  1247 ** or impossible to induce.  The set of system calls that can be overridden
  1248 ** varies from one VFS to another, and from one version of the same VFS to the
  1249 ** next.  Applications that use these interfaces must be prepared for any
  1250 ** or all of these interfaces to be NULL or for their behavior to change
  1251 ** from one release to the next.  Applications must not attempt to access
  1252 ** any of these methods if the iVersion of the VFS is less than 3.
  1253 */
  1254 typedef struct sqlite3_vfs sqlite3_vfs;
  1255 typedef void (*sqlite3_syscall_ptr)(void);
  1256 struct sqlite3_vfs {
  1257   int iVersion;            /* Structure version number (currently 3) */
  1258   int szOsFile;            /* Size of subclassed sqlite3_file */
  1259   int mxPathname;          /* Maximum file pathname length */
  1260   sqlite3_vfs *pNext;      /* Next registered VFS */
  1261   const char *zName;       /* Name of this virtual file system */
  1262   void *pAppData;          /* Pointer to application-specific data */
  1263   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1264                int flags, int *pOutFlags);
  1265   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
  1266   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
  1267   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
  1268   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
  1269   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
  1270   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
  1271   void (*xDlClose)(sqlite3_vfs*, void*);
  1272   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
  1273   int (*xSleep)(sqlite3_vfs*, int microseconds);
  1274   int (*xCurrentTime)(sqlite3_vfs*, double*);
  1275   int (*xGetLastError)(sqlite3_vfs*, int, char *);
  1276   /*
  1277   ** The methods above are in version 1 of the sqlite_vfs object
  1278   ** definition.  Those that follow are added in version 2 or later
  1279   */
  1280   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
  1281   /*
  1282   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
  1283   ** Those below are for version 3 and greater.
  1284   */
  1285   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
  1286   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
  1287   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
  1288   /*
  1289   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
  1290   ** New fields may be appended in figure versions.  The iVersion
  1291   ** value will increment whenever this happens. 
  1292   */
  1293 };
  1295 /*
  1296 ** CAPI3REF: Flags for the xAccess VFS method
  1297 **
  1298 ** These integer constants can be used as the third parameter to
  1299 ** the xAccess method of an [sqlite3_vfs] object.  They determine
  1300 ** what kind of permissions the xAccess method is looking for.
  1301 ** With SQLITE_ACCESS_EXISTS, the xAccess method
  1302 ** simply checks whether the file exists.
  1303 ** With SQLITE_ACCESS_READWRITE, the xAccess method
  1304 ** checks whether the named directory is both readable and writable
  1305 ** (in other words, if files can be added, removed, and renamed within
  1306 ** the directory).
  1307 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
  1308 ** [temp_store_directory pragma], though this could change in a future
  1309 ** release of SQLite.
  1310 ** With SQLITE_ACCESS_READ, the xAccess method
  1311 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
  1312 ** currently unused, though it might be used in a future release of
  1313 ** SQLite.
  1314 */
  1315 #define SQLITE_ACCESS_EXISTS    0
  1316 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
  1317 #define SQLITE_ACCESS_READ      2   /* Unused */
  1319 /*
  1320 ** CAPI3REF: Flags for the xShmLock VFS method
  1321 **
  1322 ** These integer constants define the various locking operations
  1323 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
  1324 ** following are the only legal combinations of flags to the
  1325 ** xShmLock method:
  1326 **
  1327 ** <ul>
  1328 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
  1329 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
  1330 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
  1331 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
  1332 ** </ul>
  1333 **
  1334 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
  1335 ** was given no the corresponding lock.  
  1336 **
  1337 ** The xShmLock method can transition between unlocked and SHARED or
  1338 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
  1339 ** and EXCLUSIVE.
  1340 */
  1341 #define SQLITE_SHM_UNLOCK       1
  1342 #define SQLITE_SHM_LOCK         2
  1343 #define SQLITE_SHM_SHARED       4
  1344 #define SQLITE_SHM_EXCLUSIVE    8
  1346 /*
  1347 ** CAPI3REF: Maximum xShmLock index
  1348 **
  1349 ** The xShmLock method on [sqlite3_io_methods] may use values
  1350 ** between 0 and this upper bound as its "offset" argument.
  1351 ** The SQLite core will never attempt to acquire or release a
  1352 ** lock outside of this range
  1353 */
  1354 #define SQLITE_SHM_NLOCK        8
  1357 /*
  1358 ** CAPI3REF: Initialize The SQLite Library
  1359 **
  1360 ** ^The sqlite3_initialize() routine initializes the
  1361 ** SQLite library.  ^The sqlite3_shutdown() routine
  1362 ** deallocates any resources that were allocated by sqlite3_initialize().
  1363 ** These routines are designed to aid in process initialization and
  1364 ** shutdown on embedded systems.  Workstation applications using
  1365 ** SQLite normally do not need to invoke either of these routines.
  1366 **
  1367 ** A call to sqlite3_initialize() is an "effective" call if it is
  1368 ** the first time sqlite3_initialize() is invoked during the lifetime of
  1369 ** the process, or if it is the first time sqlite3_initialize() is invoked
  1370 ** following a call to sqlite3_shutdown().  ^(Only an effective call
  1371 ** of sqlite3_initialize() does any initialization.  All other calls
  1372 ** are harmless no-ops.)^
  1373 **
  1374 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
  1375 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
  1376 ** an effective call to sqlite3_shutdown() does any deinitialization.
  1377 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
  1378 **
  1379 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
  1380 ** is not.  The sqlite3_shutdown() interface must only be called from a
  1381 ** single thread.  All open [database connections] must be closed and all
  1382 ** other SQLite resources must be deallocated prior to invoking
  1383 ** sqlite3_shutdown().
  1384 **
  1385 ** Among other things, ^sqlite3_initialize() will invoke
  1386 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
  1387 ** will invoke sqlite3_os_end().
  1388 **
  1389 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
  1390 ** ^If for some reason, sqlite3_initialize() is unable to initialize
  1391 ** the library (perhaps it is unable to allocate a needed resource such
  1392 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
  1393 **
  1394 ** ^The sqlite3_initialize() routine is called internally by many other
  1395 ** SQLite interfaces so that an application usually does not need to
  1396 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
  1397 ** calls sqlite3_initialize() so the SQLite library will be automatically
  1398 ** initialized when [sqlite3_open()] is called if it has not be initialized
  1399 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
  1400 ** compile-time option, then the automatic calls to sqlite3_initialize()
  1401 ** are omitted and the application must call sqlite3_initialize() directly
  1402 ** prior to using any other SQLite interface.  For maximum portability,
  1403 ** it is recommended that applications always invoke sqlite3_initialize()
  1404 ** directly prior to using any other SQLite interface.  Future releases
  1405 ** of SQLite may require this.  In other words, the behavior exhibited
  1406 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
  1407 ** default behavior in some future release of SQLite.
  1408 **
  1409 ** The sqlite3_os_init() routine does operating-system specific
  1410 ** initialization of the SQLite library.  The sqlite3_os_end()
  1411 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
  1412 ** performed by these routines include allocation or deallocation
  1413 ** of static resources, initialization of global variables,
  1414 ** setting up a default [sqlite3_vfs] module, or setting up
  1415 ** a default configuration using [sqlite3_config()].
  1416 **
  1417 ** The application should never invoke either sqlite3_os_init()
  1418 ** or sqlite3_os_end() directly.  The application should only invoke
  1419 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
  1420 ** interface is called automatically by sqlite3_initialize() and
  1421 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
  1422 ** implementations for sqlite3_os_init() and sqlite3_os_end()
  1423 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
  1424 ** When [custom builds | built for other platforms]
  1425 ** (using the [SQLITE_OS_OTHER=1] compile-time
  1426 ** option) the application must supply a suitable implementation for
  1427 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
  1428 ** implementation of sqlite3_os_init() or sqlite3_os_end()
  1429 ** must return [SQLITE_OK] on success and some other [error code] upon
  1430 ** failure.
  1431 */
  1432 SQLITE_API int sqlite3_initialize(void);
  1433 SQLITE_API int sqlite3_shutdown(void);
  1434 SQLITE_API int sqlite3_os_init(void);
  1435 SQLITE_API int sqlite3_os_end(void);
  1437 /*
  1438 ** CAPI3REF: Configuring The SQLite Library
  1439 **
  1440 ** The sqlite3_config() interface is used to make global configuration
  1441 ** changes to SQLite in order to tune SQLite to the specific needs of
  1442 ** the application.  The default configuration is recommended for most
  1443 ** applications and so this routine is usually not necessary.  It is
  1444 ** provided to support rare applications with unusual needs.
  1445 **
  1446 ** The sqlite3_config() interface is not threadsafe.  The application
  1447 ** must insure that no other SQLite interfaces are invoked by other
  1448 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
  1449 ** may only be invoked prior to library initialization using
  1450 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
  1451 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
  1452 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
  1453 ** Note, however, that ^sqlite3_config() can be called as part of the
  1454 ** implementation of an application-defined [sqlite3_os_init()].
  1455 **
  1456 ** The first argument to sqlite3_config() is an integer
  1457 ** [configuration option] that determines
  1458 ** what property of SQLite is to be configured.  Subsequent arguments
  1459 ** vary depending on the [configuration option]
  1460 ** in the first argument.
  1461 **
  1462 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
  1463 ** ^If the option is unknown or SQLite is unable to set the option
  1464 ** then this routine returns a non-zero [error code].
  1465 */
  1466 SQLITE_API int sqlite3_config(int, ...);
  1468 /*
  1469 ** CAPI3REF: Configure database connections
  1470 **
  1471 ** The sqlite3_db_config() interface is used to make configuration
  1472 ** changes to a [database connection].  The interface is similar to
  1473 ** [sqlite3_config()] except that the changes apply to a single
  1474 ** [database connection] (specified in the first argument).
  1475 **
  1476 ** The second argument to sqlite3_db_config(D,V,...)  is the
  1477 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
  1478 ** that indicates what aspect of the [database connection] is being configured.
  1479 ** Subsequent arguments vary depending on the configuration verb.
  1480 **
  1481 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1482 ** the call is considered successful.
  1483 */
  1484 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
  1486 /*
  1487 ** CAPI3REF: Memory Allocation Routines
  1488 **
  1489 ** An instance of this object defines the interface between SQLite
  1490 ** and low-level memory allocation routines.
  1491 **
  1492 ** This object is used in only one place in the SQLite interface.
  1493 ** A pointer to an instance of this object is the argument to
  1494 ** [sqlite3_config()] when the configuration option is
  1495 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
  1496 ** By creating an instance of this object
  1497 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
  1498 ** during configuration, an application can specify an alternative
  1499 ** memory allocation subsystem for SQLite to use for all of its
  1500 ** dynamic memory needs.
  1501 **
  1502 ** Note that SQLite comes with several [built-in memory allocators]
  1503 ** that are perfectly adequate for the overwhelming majority of applications
  1504 ** and that this object is only useful to a tiny minority of applications
  1505 ** with specialized memory allocation requirements.  This object is
  1506 ** also used during testing of SQLite in order to specify an alternative
  1507 ** memory allocator that simulates memory out-of-memory conditions in
  1508 ** order to verify that SQLite recovers gracefully from such
  1509 ** conditions.
  1510 **
  1511 ** The xMalloc, xRealloc, and xFree methods must work like the
  1512 ** malloc(), realloc() and free() functions from the standard C library.
  1513 ** ^SQLite guarantees that the second argument to
  1514 ** xRealloc is always a value returned by a prior call to xRoundup.
  1515 **
  1516 ** xSize should return the allocated size of a memory allocation
  1517 ** previously obtained from xMalloc or xRealloc.  The allocated size
  1518 ** is always at least as big as the requested size but may be larger.
  1519 **
  1520 ** The xRoundup method returns what would be the allocated size of
  1521 ** a memory allocation given a particular requested size.  Most memory
  1522 ** allocators round up memory allocations at least to the next multiple
  1523 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
  1524 ** Every memory allocation request coming in through [sqlite3_malloc()]
  1525 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
  1526 ** that causes the corresponding memory allocation to fail.
  1527 **
  1528 ** The xInit method initializes the memory allocator.  For example,
  1529 ** it might allocate any require mutexes or initialize internal data
  1530 ** structures.  The xShutdown method is invoked (indirectly) by
  1531 ** [sqlite3_shutdown()] and should deallocate any resources acquired
  1532 ** by xInit.  The pAppData pointer is used as the only parameter to
  1533 ** xInit and xShutdown.
  1534 **
  1535 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
  1536 ** the xInit method, so the xInit method need not be threadsafe.  The
  1537 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1538 ** not need to be threadsafe either.  For all other methods, SQLite
  1539 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
  1540 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
  1541 ** it is by default) and so the methods are automatically serialized.
  1542 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
  1543 ** methods must be threadsafe or else make their own arrangements for
  1544 ** serialization.
  1545 **
  1546 ** SQLite will never invoke xInit() more than once without an intervening
  1547 ** call to xShutdown().
  1548 */
  1549 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
  1550 struct sqlite3_mem_methods {
  1551   void *(*xMalloc)(int);         /* Memory allocation function */
  1552   void (*xFree)(void*);          /* Free a prior allocation */
  1553   void *(*xRealloc)(void*,int);  /* Resize an allocation */
  1554   int (*xSize)(void*);           /* Return the size of an allocation */
  1555   int (*xRoundup)(int);          /* Round up request size to allocation size */
  1556   int (*xInit)(void*);           /* Initialize the memory allocator */
  1557   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
  1558   void *pAppData;                /* Argument to xInit() and xShutdown() */
  1559 };
  1561 /*
  1562 ** CAPI3REF: Configuration Options
  1563 ** KEYWORDS: {configuration option}
  1564 **
  1565 ** These constants are the available integer configuration options that
  1566 ** can be passed as the first argument to the [sqlite3_config()] interface.
  1567 **
  1568 ** New configuration options may be added in future releases of SQLite.
  1569 ** Existing configuration options might be discontinued.  Applications
  1570 ** should check the return code from [sqlite3_config()] to make sure that
  1571 ** the call worked.  The [sqlite3_config()] interface will return a
  1572 ** non-zero [error code] if a discontinued or unsupported configuration option
  1573 ** is invoked.
  1574 **
  1575 ** <dl>
  1576 ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
  1577 ** <dd>There are no arguments to this option.  ^This option sets the
  1578 ** [threading mode] to Single-thread.  In other words, it disables
  1579 ** all mutexing and puts SQLite into a mode where it can only be used
  1580 ** by a single thread.   ^If SQLite is compiled with
  1581 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1582 ** it is not possible to change the [threading mode] from its default
  1583 ** value of Single-thread and so [sqlite3_config()] will return 
  1584 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
  1585 ** configuration option.</dd>
  1586 **
  1587 ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
  1588 ** <dd>There are no arguments to this option.  ^This option sets the
  1589 ** [threading mode] to Multi-thread.  In other words, it disables
  1590 ** mutexing on [database connection] and [prepared statement] objects.
  1591 ** The application is responsible for serializing access to
  1592 ** [database connections] and [prepared statements].  But other mutexes
  1593 ** are enabled so that SQLite will be safe to use in a multi-threaded
  1594 ** environment as long as no two threads attempt to use the same
  1595 ** [database connection] at the same time.  ^If SQLite is compiled with
  1596 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1597 ** it is not possible to set the Multi-thread [threading mode] and
  1598 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1599 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
  1600 **
  1601 ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
  1602 ** <dd>There are no arguments to this option.  ^This option sets the
  1603 ** [threading mode] to Serialized. In other words, this option enables
  1604 ** all mutexes including the recursive
  1605 ** mutexes on [database connection] and [prepared statement] objects.
  1606 ** In this mode (which is the default when SQLite is compiled with
  1607 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
  1608 ** to [database connections] and [prepared statements] so that the
  1609 ** application is free to use the same [database connection] or the
  1610 ** same [prepared statement] in different threads at the same time.
  1611 ** ^If SQLite is compiled with
  1612 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1613 ** it is not possible to set the Serialized [threading mode] and
  1614 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1615 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
  1616 **
  1617 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
  1618 ** <dd> ^(This option takes a single argument which is a pointer to an
  1619 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
  1620 ** alternative low-level memory allocation routines to be used in place of
  1621 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
  1622 ** its own private copy of the content of the [sqlite3_mem_methods] structure
  1623 ** before the [sqlite3_config()] call returns.</dd>
  1624 **
  1625 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
  1626 ** <dd> ^(This option takes a single argument which is a pointer to an
  1627 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
  1628 ** structure is filled with the currently defined memory allocation routines.)^
  1629 ** This option can be used to overload the default memory allocation
  1630 ** routines with a wrapper that simulations memory allocation failure or
  1631 ** tracks memory usage, for example. </dd>
  1632 **
  1633 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1634 ** <dd> ^This option takes single argument of type int, interpreted as a 
  1635 ** boolean, which enables or disables the collection of memory allocation 
  1636 ** statistics. ^(When memory allocation statistics are disabled, the 
  1637 ** following SQLite interfaces become non-operational:
  1638 **   <ul>
  1639 **   <li> [sqlite3_memory_used()]
  1640 **   <li> [sqlite3_memory_highwater()]
  1641 **   <li> [sqlite3_soft_heap_limit64()]
  1642 **   <li> [sqlite3_status()]
  1643 **   </ul>)^
  1644 ** ^Memory allocation statistics are enabled by default unless SQLite is
  1645 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1646 ** allocation statistics are disabled by default.
  1647 ** </dd>
  1648 **
  1649 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
  1650 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1651 ** scratch memory.  There are three arguments:  A pointer an 8-byte
  1652 ** aligned memory buffer from which the scratch allocations will be
  1653 ** drawn, the size of each scratch allocation (sz),
  1654 ** and the maximum number of scratch allocations (N).  The sz
  1655 ** argument must be a multiple of 16.
  1656 ** The first argument must be a pointer to an 8-byte aligned buffer
  1657 ** of at least sz*N bytes of memory.
  1658 ** ^SQLite will use no more than two scratch buffers per thread.  So
  1659 ** N should be set to twice the expected maximum number of threads.
  1660 ** ^SQLite will never require a scratch buffer that is more than 6
  1661 ** times the database page size. ^If SQLite needs needs additional
  1662 ** scratch memory beyond what is provided by this configuration option, then 
  1663 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
  1664 **
  1665 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1666 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1667 ** the database page cache with the default page cache implementation.  
  1668 ** This configuration should not be used if an application-define page
  1669 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
  1670 ** There are three arguments to this option: A pointer to 8-byte aligned
  1671 ** memory, the size of each page buffer (sz), and the number of pages (N).
  1672 ** The sz argument should be the size of the largest database page
  1673 ** (a power of two between 512 and 32768) plus a little extra for each
  1674 ** page header.  ^The page header size is 20 to 40 bytes depending on
  1675 ** the host architecture.  ^It is harmless, apart from the wasted memory,
  1676 ** to make sz a little too large.  The first
  1677 ** argument should point to an allocation of at least sz*N bytes of memory.
  1678 ** ^SQLite will use the memory provided by the first argument to satisfy its
  1679 ** memory needs for the first N pages that it adds to cache.  ^If additional
  1680 ** page cache memory is needed beyond what is provided by this option, then
  1681 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
  1682 ** The pointer in the first argument must
  1683 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
  1684 ** will be undefined.</dd>
  1685 **
  1686 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  1687 ** <dd> ^This option specifies a static memory buffer that SQLite will use
  1688 ** for all of its dynamic memory allocation needs beyond those provided
  1689 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  1690 ** There are three arguments: An 8-byte aligned pointer to the memory,
  1691 ** the number of bytes in the memory buffer, and the minimum allocation size.
  1692 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1693 ** to using its default memory allocator (the system malloc() implementation),
  1694 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1695 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1696 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1697 ** allocator is engaged to handle all of SQLites memory allocation needs.
  1698 ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1699 ** boundary or subsequent behavior of SQLite will be undefined.
  1700 ** The minimum allocation size is capped at 2**12. Reasonable values
  1701 ** for the minimum allocation size are 2**5 through 2**8.</dd>
  1702 **
  1703 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1704 ** <dd> ^(This option takes a single argument which is a pointer to an
  1705 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1706 ** alternative low-level mutex routines to be used in place
  1707 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1708 ** content of the [sqlite3_mutex_methods] structure before the call to
  1709 ** [sqlite3_config()] returns. ^If SQLite is compiled with
  1710 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1711 ** the entire mutexing subsystem is omitted from the build and hence calls to
  1712 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  1713 ** return [SQLITE_ERROR].</dd>
  1714 **
  1715 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
  1716 ** <dd> ^(This option takes a single argument which is a pointer to an
  1717 ** instance of the [sqlite3_mutex_methods] structure.  The
  1718 ** [sqlite3_mutex_methods]
  1719 ** structure is filled with the currently defined mutex routines.)^
  1720 ** This option can be used to overload the default mutex allocation
  1721 ** routines with a wrapper used to track mutex usage for performance
  1722 ** profiling or testing, for example.   ^If SQLite is compiled with
  1723 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1724 ** the entire mutexing subsystem is omitted from the build and hence calls to
  1725 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
  1726 ** return [SQLITE_ERROR].</dd>
  1727 **
  1728 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
  1729 ** <dd> ^(This option takes two arguments that determine the default
  1730 ** memory allocation for the lookaside memory allocator on each
  1731 ** [database connection].  The first argument is the
  1732 ** size of each lookaside buffer slot and the second is the number of
  1733 ** slots allocated to each database connection.)^  ^(This option sets the
  1734 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
  1735 ** verb to [sqlite3_db_config()] can be used to change the lookaside
  1736 ** configuration on individual connections.)^ </dd>
  1737 **
  1738 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
  1739 ** <dd> ^(This option takes a single argument which is a pointer to
  1740 ** an [sqlite3_pcache_methods2] object.  This object specifies the interface
  1741 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
  1742 ** object and uses it for page cache memory allocations.</dd>
  1743 **
  1744 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
  1745 ** <dd> ^(This option takes a single argument which is a pointer to an
  1746 ** [sqlite3_pcache_methods2] object.  SQLite copies of the current
  1747 ** page cache implementation into that object.)^ </dd>
  1748 **
  1749 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
  1750 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
  1751 ** global [error log].
  1752 ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
  1753 ** function with a call signature of void(*)(void*,int,const char*), 
  1754 ** and a pointer to void. ^If the function pointer is not NULL, it is
  1755 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
  1756 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
  1757 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
  1758 ** passed through as the first parameter to the application-defined logger
  1759 ** function whenever that function is invoked.  ^The second parameter to
  1760 ** the logger function is a copy of the first parameter to the corresponding
  1761 ** [sqlite3_log()] call and is intended to be a [result code] or an
  1762 ** [extended result code].  ^The third parameter passed to the logger is
  1763 ** log message after formatting via [sqlite3_snprintf()].
  1764 ** The SQLite logging interface is not reentrant; the logger function
  1765 ** supplied by the application must not invoke any SQLite interface.
  1766 ** In a multi-threaded application, the application-defined logger
  1767 ** function must be threadsafe. </dd>
  1768 **
  1769 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
  1770 ** <dd>^(This option takes a single argument of type int. If non-zero, then
  1771 ** URI handling is globally enabled. If the parameter is zero, then URI handling
  1772 ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
  1773 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
  1774 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1775 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1776 ** connection is opened. ^If it is globally disabled, filenames are
  1777 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1778 ** database connection is opened. ^(By default, URI handling is globally
  1779 ** disabled. The default value may be changed by compiling with the
  1780 ** [SQLITE_USE_URI] symbol defined.)^
  1781 **
  1782 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
  1783 ** <dd>^This option takes a single integer argument which is interpreted as
  1784 ** a boolean in order to enable or disable the use of covering indices for
  1785 ** full table scans in the query optimizer.  ^The default setting is determined
  1786 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
  1787 ** if that compile-time option is omitted.
  1788 ** The ability to disable the use of covering indices for full table scans
  1789 ** is because some incorrectly coded legacy applications might malfunction
  1790 ** when the optimization is enabled.  Providing the ability to
  1791 ** disable the optimization allows the older, buggy application code to work
  1792 ** without change even with newer versions of SQLite.
  1793 **
  1794 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1795 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  1796 ** <dd> These options are obsolete and should not be used by new code.
  1797 ** They are retained for backwards compatibility but are now no-ops.
  1798 ** </dd>
  1799 **
  1800 ** [[SQLITE_CONFIG_SQLLOG]]
  1801 ** <dt>SQLITE_CONFIG_SQLLOG
  1802 ** <dd>This option is only available if sqlite is compiled with the
  1803 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
  1804 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
  1805 ** The second should be of type (void*). The callback is invoked by the library
  1806 ** in three separate circumstances, identified by the value passed as the
  1807 ** fourth parameter. If the fourth parameter is 0, then the database connection
  1808 ** passed as the second argument has just been opened. The third argument
  1809 ** points to a buffer containing the name of the main database file. If the
  1810 ** fourth parameter is 1, then the SQL statement that the third parameter
  1811 ** points to has just been executed. Or, if the fourth parameter is 2, then
  1812 ** the connection being passed as the second parameter is being closed. The
  1813 ** third parameter is passed NULL In this case.  An example of using this
  1814 ** configuration option can be seen in the "test_sqllog.c" source file in
  1815 ** the canonical SQLite source tree.</dd>
  1816 **
  1817 ** [[SQLITE_CONFIG_MMAP_SIZE]]
  1818 ** <dt>SQLITE_CONFIG_MMAP_SIZE
  1819 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
  1820 ** that are the default mmap size limit (the default setting for
  1821 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
  1822 ** ^The default setting can be overridden by each database connection using
  1823 ** either the [PRAGMA mmap_size] command, or by using the
  1824 ** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
  1825 ** cannot be changed at run-time.  Nor may the maximum allowed mmap size
  1826 ** exceed the compile-time maximum mmap size set by the
  1827 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
  1828 ** ^If either argument to this option is negative, then that argument is
  1829 ** changed to its compile-time default.
  1830 **
  1831 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
  1832 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
  1833 ** <dd>^This option is only available if SQLite is compiled for Windows
  1834 ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
  1835 ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
  1836 ** that specifies the maximum size of the created heap.
  1837 ** </dl>
  1838 */
  1839 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1840 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1841 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1842 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1843 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
  1844 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
  1845 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
  1846 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
  1847 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
  1848 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
  1849 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
  1850 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
  1851 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1852 #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1853 #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1854 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1855 #define SQLITE_CONFIG_URI          17  /* int */
  1856 #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1857 #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  1858 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  1859 #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  1860 #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
  1861 #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
  1863 /*
  1864 ** CAPI3REF: Database Connection Configuration Options
  1865 **
  1866 ** These constants are the available integer configuration options that
  1867 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1868 **
  1869 ** New configuration options may be added in future releases of SQLite.
  1870 ** Existing configuration options might be discontinued.  Applications
  1871 ** should check the return code from [sqlite3_db_config()] to make sure that
  1872 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
  1873 ** non-zero [error code] if a discontinued or unsupported configuration option
  1874 ** is invoked.
  1875 **
  1876 ** <dl>
  1877 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  1878 ** <dd> ^This option takes three additional arguments that determine the 
  1879 ** [lookaside memory allocator] configuration for the [database connection].
  1880 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  1881 ** pointer to a memory buffer to use for lookaside memory.
  1882 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  1883 ** may be NULL in which case SQLite will allocate the
  1884 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  1885 ** size of each lookaside buffer slot.  ^The third argument is the number of
  1886 ** slots.  The size of the buffer in the first argument must be greater than
  1887 ** or equal to the product of the second and third arguments.  The buffer
  1888 ** must be aligned to an 8-byte boundary.  ^If the second argument to
  1889 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
  1890 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
  1891 ** configuration for a database connection can only be changed when that
  1892 ** connection is not currently using lookaside memory, or in other words
  1893 ** when the "current value" returned by
  1894 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  1895 ** Any attempt to change the lookaside memory configuration when lookaside
  1896 ** memory is in use leaves the configuration unchanged and returns 
  1897 ** [SQLITE_BUSY].)^</dd>
  1898 **
  1899 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  1900 ** <dd> ^This option is used to enable or disable the enforcement of
  1901 ** [foreign key constraints].  There should be two additional arguments.
  1902 ** The first argument is an integer which is 0 to disable FK enforcement,
  1903 ** positive to enable FK enforcement or negative to leave FK enforcement
  1904 ** unchanged.  The second parameter is a pointer to an integer into which
  1905 ** is written 0 or 1 to indicate whether FK enforcement is off or on
  1906 ** following this call.  The second parameter may be a NULL pointer, in
  1907 ** which case the FK enforcement setting is not reported back. </dd>
  1908 **
  1909 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  1910 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  1911 ** There should be two additional arguments.
  1912 ** The first argument is an integer which is 0 to disable triggers,
  1913 ** positive to enable triggers or negative to leave the setting unchanged.
  1914 ** The second parameter is a pointer to an integer into which
  1915 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  1916 ** following this call.  The second parameter may be a NULL pointer, in
  1917 ** which case the trigger setting is not reported back. </dd>
  1918 **
  1919 ** </dl>
  1920 */
  1921 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
  1922 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
  1923 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  1926 /*
  1927 ** CAPI3REF: Enable Or Disable Extended Result Codes
  1928 **
  1929 ** ^The sqlite3_extended_result_codes() routine enables or disables the
  1930 ** [extended result codes] feature of SQLite. ^The extended result
  1931 ** codes are disabled by default for historical compatibility.
  1932 */
  1933 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1935 /*
  1936 ** CAPI3REF: Last Insert Rowid
  1937 **
  1938 ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
  1939 ** has a unique 64-bit signed
  1940 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1941 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1942 ** names are not also used by explicitly declared columns. ^If
  1943 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  1944 ** is another alias for the rowid.
  1945 **
  1946 ** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
  1947 ** most recent successful [INSERT] into a rowid table or [virtual table]
  1948 ** on database connection D.
  1949 ** ^Inserts into [WITHOUT ROWID] tables are not recorded.
  1950 ** ^If no successful [INSERT]s into rowid tables
  1951 ** have ever occurred on the database connection D, 
  1952 ** then sqlite3_last_insert_rowid(D) returns zero.
  1953 **
  1954 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  1955 ** method, then this routine will return the [rowid] of the inserted
  1956 ** row as long as the trigger or virtual table method is running.
  1957 ** But once the trigger or virtual table method ends, the value returned 
  1958 ** by this routine reverts to what it was before the trigger or virtual
  1959 ** table method began.)^
  1960 **
  1961 ** ^An [INSERT] that fails due to a constraint violation is not a
  1962 ** successful [INSERT] and does not change the value returned by this
  1963 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  1964 ** and INSERT OR ABORT make no changes to the return value of this
  1965 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
  1966 ** encounters a constraint violation, it does not fail.  The
  1967 ** INSERT continues to completion after deleting rows that caused
  1968 ** the constraint problem so INSERT OR REPLACE will always change
  1969 ** the return value of this interface.)^
  1970 **
  1971 ** ^For the purposes of this routine, an [INSERT] is considered to
  1972 ** be successful even if it is subsequently rolled back.
  1973 **
  1974 ** This function is accessible to SQL statements via the
  1975 ** [last_insert_rowid() SQL function].
  1976 **
  1977 ** If a separate thread performs a new [INSERT] on the same
  1978 ** database connection while the [sqlite3_last_insert_rowid()]
  1979 ** function is running and thus changes the last insert [rowid],
  1980 ** then the value returned by [sqlite3_last_insert_rowid()] is
  1981 ** unpredictable and might not equal either the old or the new
  1982 ** last insert [rowid].
  1983 */
  1984 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  1986 /*
  1987 ** CAPI3REF: Count The Number Of Rows Modified
  1988 **
  1989 ** ^This function returns the number of database rows that were changed
  1990 ** or inserted or deleted by the most recently completed SQL statement
  1991 ** on the [database connection] specified by the first parameter.
  1992 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
  1993 ** or [DELETE] statement are counted.  Auxiliary changes caused by
  1994 ** triggers or [foreign key actions] are not counted.)^ Use the
  1995 ** [sqlite3_total_changes()] function to find the total number of changes
  1996 ** including changes caused by triggers and foreign key actions.
  1997 **
  1998 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
  1999 ** are not counted.  Only real table changes are counted.
  2000 **
  2001 ** ^(A "row change" is a change to a single row of a single table
  2002 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
  2003 ** are changed as side effects of [REPLACE] constraint resolution,
  2004 ** rollback, ABORT processing, [DROP TABLE], or by any other
  2005 ** mechanisms do not count as direct row changes.)^
  2006 **
  2007 ** A "trigger context" is a scope of execution that begins and
  2008 ** ends with the script of a [CREATE TRIGGER | trigger]. 
  2009 ** Most SQL statements are
  2010 ** evaluated outside of any trigger.  This is the "top level"
  2011 ** trigger context.  If a trigger fires from the top level, a
  2012 ** new trigger context is entered for the duration of that one
  2013 ** trigger.  Subtriggers create subcontexts for their duration.
  2014 **
  2015 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
  2016 ** not create a new trigger context.
  2017 **
  2018 ** ^This function returns the number of direct row changes in the
  2019 ** most recent INSERT, UPDATE, or DELETE statement within the same
  2020 ** trigger context.
  2021 **
  2022 ** ^Thus, when called from the top level, this function returns the
  2023 ** number of changes in the most recent INSERT, UPDATE, or DELETE
  2024 ** that also occurred at the top level.  ^(Within the body of a trigger,
  2025 ** the sqlite3_changes() interface can be called to find the number of
  2026 ** changes in the most recently completed INSERT, UPDATE, or DELETE
  2027 ** statement within the body of the same trigger.
  2028 ** However, the number returned does not include changes
  2029 ** caused by subtriggers since those have their own context.)^
  2030 **
  2031 ** See also the [sqlite3_total_changes()] interface, the
  2032 ** [count_changes pragma], and the [changes() SQL function].
  2033 **
  2034 ** If a separate thread makes changes on the same database connection
  2035 ** while [sqlite3_changes()] is running then the value returned
  2036 ** is unpredictable and not meaningful.
  2037 */
  2038 SQLITE_API int sqlite3_changes(sqlite3*);
  2040 /*
  2041 ** CAPI3REF: Total Number Of Rows Modified
  2042 **
  2043 ** ^This function returns the number of row changes caused by [INSERT],
  2044 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  2045 ** ^(The count returned by sqlite3_total_changes() includes all changes
  2046 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
  2047 ** [foreign key actions]. However,
  2048 ** the count does not include changes used to implement [REPLACE] constraints,
  2049 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
  2050 ** count does not include rows of views that fire an [INSTEAD OF trigger],
  2051 ** though if the INSTEAD OF trigger makes changes of its own, those changes 
  2052 ** are counted.)^
  2053 ** ^The sqlite3_total_changes() function counts the changes as soon as
  2054 ** the statement that makes them is completed (when the statement handle
  2055 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
  2056 **
  2057 ** See also the [sqlite3_changes()] interface, the
  2058 ** [count_changes pragma], and the [total_changes() SQL function].
  2059 **
  2060 ** If a separate thread makes changes on the same database connection
  2061 ** while [sqlite3_total_changes()] is running then the value
  2062 ** returned is unpredictable and not meaningful.
  2063 */
  2064 SQLITE_API int sqlite3_total_changes(sqlite3*);
  2066 /*
  2067 ** CAPI3REF: Interrupt A Long-Running Query
  2068 **
  2069 ** ^This function causes any pending database operation to abort and
  2070 ** return at its earliest opportunity. This routine is typically
  2071 ** called in response to a user action such as pressing "Cancel"
  2072 ** or Ctrl-C where the user wants a long query operation to halt
  2073 ** immediately.
  2074 **
  2075 ** ^It is safe to call this routine from a thread different from the
  2076 ** thread that is currently running the database operation.  But it
  2077 ** is not safe to call this routine with a [database connection] that
  2078 ** is closed or might close before sqlite3_interrupt() returns.
  2079 **
  2080 ** ^If an SQL operation is very nearly finished at the time when
  2081 ** sqlite3_interrupt() is called, then it might not have an opportunity
  2082 ** to be interrupted and might continue to completion.
  2083 **
  2084 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
  2085 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
  2086 ** that is inside an explicit transaction, then the entire transaction
  2087 ** will be rolled back automatically.
  2088 **
  2089 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
  2090 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
  2091 ** that are started after the sqlite3_interrupt() call and before the 
  2092 ** running statements reaches zero are interrupted as if they had been
  2093 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
  2094 ** that are started after the running statement count reaches zero are
  2095 ** not effected by the sqlite3_interrupt().
  2096 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  2097 ** SQL statements is a no-op and has no effect on SQL statements
  2098 ** that are started after the sqlite3_interrupt() call returns.
  2099 **
  2100 ** If the database connection closes while [sqlite3_interrupt()]
  2101 ** is running then bad things will likely happen.
  2102 */
  2103 SQLITE_API void sqlite3_interrupt(sqlite3*);
  2105 /*
  2106 ** CAPI3REF: Determine If An SQL Statement Is Complete
  2107 **
  2108 ** These routines are useful during command-line input to determine if the
  2109 ** currently entered text seems to form a complete SQL statement or
  2110 ** if additional input is needed before sending the text into
  2111 ** SQLite for parsing.  ^These routines return 1 if the input string
  2112 ** appears to be a complete SQL statement.  ^A statement is judged to be
  2113 ** complete if it ends with a semicolon token and is not a prefix of a
  2114 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
  2115 ** string literals or quoted identifier names or comments are not
  2116 ** independent tokens (they are part of the token in which they are
  2117 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
  2118 ** and comments that follow the final semicolon are ignored.
  2119 **
  2120 ** ^These routines return 0 if the statement is incomplete.  ^If a
  2121 ** memory allocation fails, then SQLITE_NOMEM is returned.
  2122 **
  2123 ** ^These routines do not parse the SQL statements thus
  2124 ** will not detect syntactically incorrect SQL.
  2125 **
  2126 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 
  2127 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
  2128 ** automatically by sqlite3_complete16().  If that initialization fails,
  2129 ** then the return value from sqlite3_complete16() will be non-zero
  2130 ** regardless of whether or not the input SQL is complete.)^
  2131 **
  2132 ** The input to [sqlite3_complete()] must be a zero-terminated
  2133 ** UTF-8 string.
  2134 **
  2135 ** The input to [sqlite3_complete16()] must be a zero-terminated
  2136 ** UTF-16 string in native byte order.
  2137 */
  2138 SQLITE_API int sqlite3_complete(const char *sql);
  2139 SQLITE_API int sqlite3_complete16(const void *sql);
  2141 /*
  2142 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2143 **
  2144 ** ^This routine sets a callback function that might be invoked whenever
  2145 ** an attempt is made to open a database table that another thread
  2146 ** or process has locked.
  2147 **
  2148 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  2149 ** is returned immediately upon encountering the lock.  ^If the busy callback
  2150 ** is not NULL, then the callback might be invoked with two arguments.
  2151 **
  2152 ** ^The first argument to the busy handler is a copy of the void* pointer which
  2153 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
  2154 ** the busy handler callback is the number of times that the busy handler has
  2155 ** been invoked for this locking event.  ^If the
  2156 ** busy callback returns 0, then no additional attempts are made to
  2157 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
  2158 ** ^If the callback returns non-zero, then another attempt
  2159 ** is made to open the database for reading and the cycle repeats.
  2160 **
  2161 ** The presence of a busy handler does not guarantee that it will be invoked
  2162 ** when there is lock contention. ^If SQLite determines that invoking the busy
  2163 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  2164 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
  2165 ** Consider a scenario where one process is holding a read lock that
  2166 ** it is trying to promote to a reserved lock and
  2167 ** a second process is holding a reserved lock that it is trying
  2168 ** to promote to an exclusive lock.  The first process cannot proceed
  2169 ** because it is blocked by the second and the second process cannot
  2170 ** proceed because it is blocked by the first.  If both processes
  2171 ** invoke the busy handlers, neither will make any progress.  Therefore,
  2172 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  2173 ** will induce the first process to release its read lock and allow
  2174 ** the second process to proceed.
  2175 **
  2176 ** ^The default busy callback is NULL.
  2177 **
  2178 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  2179 ** when SQLite is in the middle of a large transaction where all the
  2180 ** changes will not fit into the in-memory cache.  SQLite will
  2181 ** already hold a RESERVED lock on the database file, but it needs
  2182 ** to promote this lock to EXCLUSIVE so that it can spill cache
  2183 ** pages into the database file without harm to concurrent
  2184 ** readers.  ^If it is unable to promote the lock, then the in-memory
  2185 ** cache will be left in an inconsistent state and so the error
  2186 ** code is promoted from the relatively benign [SQLITE_BUSY] to
  2187 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
  2188 ** forces an automatic rollback of the changes.  See the
  2189 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  2190 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  2191 ** this is important.
  2192 **
  2193 ** ^(There can only be a single busy handler defined for each
  2194 ** [database connection].  Setting a new busy handler clears any
  2195 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
  2196 ** will also set or clear the busy handler.
  2197 **
  2198 ** The busy callback should not take any actions which modify the
  2199 ** database connection that invoked the busy handler.  Any such actions
  2200 ** result in undefined behavior.
  2201 ** 
  2202 ** A busy handler must not close the database connection
  2203 ** or [prepared statement] that invoked the busy handler.
  2204 */
  2205 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2207 /*
  2208 ** CAPI3REF: Set A Busy Timeout
  2209 **
  2210 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2211 ** for a specified amount of time when a table is locked.  ^The handler
  2212 ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2213 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
  2214 ** the handler returns 0 which causes [sqlite3_step()] to return
  2215 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
  2216 **
  2217 ** ^Calling this routine with an argument less than or equal to zero
  2218 ** turns off all busy handlers.
  2219 **
  2220 ** ^(There can only be a single busy handler for a particular
  2221 ** [database connection] any any given moment.  If another busy handler
  2222 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
  2223 ** this routine, that other busy handler is cleared.)^
  2224 */
  2225 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
  2227 /*
  2228 ** CAPI3REF: Convenience Routines For Running Queries
  2229 **
  2230 ** This is a legacy interface that is preserved for backwards compatibility.
  2231 ** Use of this interface is not recommended.
  2232 **
  2233 ** Definition: A <b>result table</b> is memory data structure created by the
  2234 ** [sqlite3_get_table()] interface.  A result table records the
  2235 ** complete query results from one or more queries.
  2236 **
  2237 ** The table conceptually has a number of rows and columns.  But
  2238 ** these numbers are not part of the result table itself.  These
  2239 ** numbers are obtained separately.  Let N be the number of rows
  2240 ** and M be the number of columns.
  2241 **
  2242 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
  2243 ** There are (N+1)*M elements in the array.  The first M pointers point
  2244 ** to zero-terminated strings that  contain the names of the columns.
  2245 ** The remaining entries all point to query results.  NULL values result
  2246 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
  2247 ** string representation as returned by [sqlite3_column_text()].
  2248 **
  2249 ** A result table might consist of one or more memory allocations.
  2250 ** It is not safe to pass a result table directly to [sqlite3_free()].
  2251 ** A result table should be deallocated using [sqlite3_free_table()].
  2252 **
  2253 ** ^(As an example of the result table format, suppose a query result
  2254 ** is as follows:
  2255 **
  2256 ** <blockquote><pre>
  2257 **        Name        | Age
  2258 **        -----------------------
  2259 **        Alice       | 43
  2260 **        Bob         | 28
  2261 **        Cindy       | 21
  2262 ** </pre></blockquote>
  2263 **
  2264 ** There are two column (M==2) and three rows (N==3).  Thus the
  2265 ** result table has 8 entries.  Suppose the result table is stored
  2266 ** in an array names azResult.  Then azResult holds this content:
  2267 **
  2268 ** <blockquote><pre>
  2269 **        azResult&#91;0] = "Name";
  2270 **        azResult&#91;1] = "Age";
  2271 **        azResult&#91;2] = "Alice";
  2272 **        azResult&#91;3] = "43";
  2273 **        azResult&#91;4] = "Bob";
  2274 **        azResult&#91;5] = "28";
  2275 **        azResult&#91;6] = "Cindy";
  2276 **        azResult&#91;7] = "21";
  2277 ** </pre></blockquote>)^
  2278 **
  2279 ** ^The sqlite3_get_table() function evaluates one or more
  2280 ** semicolon-separated SQL statements in the zero-terminated UTF-8
  2281 ** string of its 2nd parameter and returns a result table to the
  2282 ** pointer given in its 3rd parameter.
  2283 **
  2284 ** After the application has finished with the result from sqlite3_get_table(),
  2285 ** it must pass the result table pointer to sqlite3_free_table() in order to
  2286 ** release the memory that was malloced.  Because of the way the
  2287 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
  2288 ** function must not try to call [sqlite3_free()] directly.  Only
  2289 ** [sqlite3_free_table()] is able to release the memory properly and safely.
  2290 **
  2291 ** The sqlite3_get_table() interface is implemented as a wrapper around
  2292 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
  2293 ** to any internal data structures of SQLite.  It uses only the public
  2294 ** interface defined here.  As a consequence, errors that occur in the
  2295 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
  2296 ** reflected in subsequent calls to [sqlite3_errcode()] or
  2297 ** [sqlite3_errmsg()].
  2298 */
  2299 SQLITE_API int sqlite3_get_table(
  2300   sqlite3 *db,          /* An open database */
  2301   const char *zSql,     /* SQL to be evaluated */
  2302   char ***pazResult,    /* Results of the query */
  2303   int *pnRow,           /* Number of result rows written here */
  2304   int *pnColumn,        /* Number of result columns written here */
  2305   char **pzErrmsg       /* Error msg written here */
  2306 );
  2307 SQLITE_API void sqlite3_free_table(char **result);
  2309 /*
  2310 ** CAPI3REF: Formatted String Printing Functions
  2311 **
  2312 ** These routines are work-alikes of the "printf()" family of functions
  2313 ** from the standard C library.
  2314 **
  2315 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2316 ** results into memory obtained from [sqlite3_malloc()].
  2317 ** The strings returned by these two routines should be
  2318 ** released by [sqlite3_free()].  ^Both routines return a
  2319 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  2320 ** memory to hold the resulting string.
  2321 **
  2322 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2323 ** the standard C library.  The result is written into the
  2324 ** buffer supplied as the second parameter whose size is given by
  2325 ** the first parameter. Note that the order of the
  2326 ** first two parameters is reversed from snprintf().)^  This is an
  2327 ** historical accident that cannot be fixed without breaking
  2328 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
  2329 ** returns a pointer to its buffer instead of the number of
  2330 ** characters actually written into the buffer.)^  We admit that
  2331 ** the number of characters written would be a more useful return
  2332 ** value but we cannot change the implementation of sqlite3_snprintf()
  2333 ** now without breaking compatibility.
  2334 **
  2335 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  2336 ** guarantees that the buffer is always zero-terminated.  ^The first
  2337 ** parameter "n" is the total size of the buffer, including space for
  2338 ** the zero terminator.  So the longest string that can be completely
  2339 ** written will be n-1 characters.
  2340 **
  2341 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2342 **
  2343 ** These routines all implement some additional formatting
  2344 ** options that are useful for constructing SQL statements.
  2345 ** All of the usual printf() formatting options apply.  In addition, there
  2346 ** is are "%q", "%Q", and "%z" options.
  2347 **
  2348 ** ^(The %q option works like %s in that it substitutes a nul-terminated
  2349 ** string from the argument list.  But %q also doubles every '\'' character.
  2350 ** %q is designed for use inside a string literal.)^  By doubling each '\''
  2351 ** character it escapes that character and allows it to be inserted into
  2352 ** the string.
  2353 **
  2354 ** For example, assume the string variable zText contains text as follows:
  2355 **
  2356 ** <blockquote><pre>
  2357 **  char *zText = "It's a happy day!";
  2358 ** </pre></blockquote>
  2359 **
  2360 ** One can use this text in an SQL statement as follows:
  2361 **
  2362 ** <blockquote><pre>
  2363 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2364 **  sqlite3_exec(db, zSQL, 0, 0, 0);
  2365 **  sqlite3_free(zSQL);
  2366 ** </pre></blockquote>
  2367 **
  2368 ** Because the %q format string is used, the '\'' character in zText
  2369 ** is escaped and the SQL generated is as follows:
  2370 **
  2371 ** <blockquote><pre>
  2372 **  INSERT INTO table1 VALUES('It''s a happy day!')
  2373 ** </pre></blockquote>
  2374 **
  2375 ** This is correct.  Had we used %s instead of %q, the generated SQL
  2376 ** would have looked like this:
  2377 **
  2378 ** <blockquote><pre>
  2379 **  INSERT INTO table1 VALUES('It's a happy day!');
  2380 ** </pre></blockquote>
  2381 **
  2382 ** This second example is an SQL syntax error.  As a general rule you should
  2383 ** always use %q instead of %s when inserting text into a string literal.
  2384 **
  2385 ** ^(The %Q option works like %q except it also adds single quotes around
  2386 ** the outside of the total string.  Additionally, if the parameter in the
  2387 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2388 ** single quotes).)^  So, for example, one could say:
  2389 **
  2390 ** <blockquote><pre>
  2391 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2392 **  sqlite3_exec(db, zSQL, 0, 0, 0);
  2393 **  sqlite3_free(zSQL);
  2394 ** </pre></blockquote>
  2395 **
  2396 ** The code above will render a correct SQL statement in the zSQL
  2397 ** variable even if the zText variable is a NULL pointer.
  2398 **
  2399 ** ^(The "%z" formatting option works like "%s" but with the
  2400 ** addition that after the string has been read and copied into
  2401 ** the result, [sqlite3_free()] is called on the input string.)^
  2402 */
  2403 SQLITE_API char *sqlite3_mprintf(const char*,...);
  2404 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2405 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2406 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2408 /*
  2409 ** CAPI3REF: Memory Allocation Subsystem
  2410 **
  2411 ** The SQLite core uses these three routines for all of its own
  2412 ** internal memory allocation needs. "Core" in the previous sentence
  2413 ** does not include operating-system specific VFS implementation.  The
  2414 ** Windows VFS uses native malloc() and free() for some operations.
  2415 **
  2416 ** ^The sqlite3_malloc() routine returns a pointer to a block
  2417 ** of memory at least N bytes in length, where N is the parameter.
  2418 ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2419 ** memory, it returns a NULL pointer.  ^If the parameter N to
  2420 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2421 ** a NULL pointer.
  2422 **
  2423 ** ^Calling sqlite3_free() with a pointer previously returned
  2424 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2425 ** that it might be reused.  ^The sqlite3_free() routine is
  2426 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
  2427 ** to sqlite3_free() is harmless.  After being freed, memory
  2428 ** should neither be read nor written.  Even reading previously freed
  2429 ** memory might result in a segmentation fault or other severe error.
  2430 ** Memory corruption, a segmentation fault, or other severe error
  2431 ** might result if sqlite3_free() is called with a non-NULL pointer that
  2432 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2433 **
  2434 ** ^(The sqlite3_realloc() interface attempts to resize a
  2435 ** prior memory allocation to be at least N bytes, where N is the
  2436 ** second parameter.  The memory allocation to be resized is the first
  2437 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
  2438 ** is a NULL pointer then its behavior is identical to calling
  2439 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2440 ** ^If the second parameter to sqlite3_realloc() is zero or
  2441 ** negative then the behavior is exactly the same as calling
  2442 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2443 ** ^sqlite3_realloc() returns a pointer to a memory allocation
  2444 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
  2445 ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2446 ** of the prior allocation are copied into the beginning of buffer returned
  2447 ** by sqlite3_realloc() and the prior allocation is freed.
  2448 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2449 ** is not freed.
  2450 **
  2451 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  2452 ** is always aligned to at least an 8 byte boundary, or to a
  2453 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  2454 ** option is used.
  2455 **
  2456 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2457 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2458 ** implementation of these routines to be omitted.  That capability
  2459 ** is no longer provided.  Only built-in memory allocators can be used.
  2460 **
  2461 ** Prior to SQLite version 3.7.10, the Windows OS interface layer called
  2462 ** the system malloc() and free() directly when converting
  2463 ** filenames between the UTF-8 encoding used by SQLite
  2464 ** and whatever filename encoding is used by the particular Windows
  2465 ** installation.  Memory allocation errors were detected, but
  2466 ** they were reported back as [SQLITE_CANTOPEN] or
  2467 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
  2468 **
  2469 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
  2470 ** must be either NULL or else pointers obtained from a prior
  2471 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
  2472 ** not yet been released.
  2473 **
  2474 ** The application must not read or write any part of
  2475 ** a block of memory after it has been released using
  2476 ** [sqlite3_free()] or [sqlite3_realloc()].
  2477 */
  2478 SQLITE_API void *sqlite3_malloc(int);
  2479 SQLITE_API void *sqlite3_realloc(void*, int);
  2480 SQLITE_API void sqlite3_free(void*);
  2482 /*
  2483 ** CAPI3REF: Memory Allocator Statistics
  2484 **
  2485 ** SQLite provides these two interfaces for reporting on the status
  2486 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2487 ** routines, which form the built-in memory allocation subsystem.
  2488 **
  2489 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
  2490 ** of memory currently outstanding (malloced but not freed).
  2491 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
  2492 ** value of [sqlite3_memory_used()] since the high-water mark
  2493 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
  2494 ** [sqlite3_memory_highwater()] include any overhead
  2495 ** added by SQLite in its implementation of [sqlite3_malloc()],
  2496 ** but not overhead added by the any underlying system library
  2497 ** routines that [sqlite3_malloc()] may call.
  2498 **
  2499 ** ^The memory high-water mark is reset to the current value of
  2500 ** [sqlite3_memory_used()] if and only if the parameter to
  2501 ** [sqlite3_memory_highwater()] is true.  ^The value returned
  2502 ** by [sqlite3_memory_highwater(1)] is the high-water mark
  2503 ** prior to the reset.
  2504 */
  2505 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
  2506 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
  2508 /*
  2509 ** CAPI3REF: Pseudo-Random Number Generator
  2510 **
  2511 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  2512 ** select random [ROWID | ROWIDs] when inserting new records into a table that
  2513 ** already uses the largest possible [ROWID].  The PRNG is also used for
  2514 ** the build-in random() and randomblob() SQL functions.  This interface allows
  2515 ** applications to access the same PRNG for other purposes.
  2516 **
  2517 ** ^A call to this routine stores N bytes of randomness into buffer P.
  2518 ** ^If N is less than one, then P can be a NULL pointer.
  2519 **
  2520 ** ^If this routine has not been previously called or if the previous
  2521 ** call had N less than one, then the PRNG is seeded using randomness
  2522 ** obtained from the xRandomness method of the default [sqlite3_vfs] object.
  2523 ** ^If the previous call to this routine had an N of 1 or more then
  2524 ** the pseudo-randomness is generated
  2525 ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2526 ** method.
  2527 */
  2528 SQLITE_API void sqlite3_randomness(int N, void *P);
  2530 /*
  2531 ** CAPI3REF: Compile-Time Authorization Callbacks
  2532 **
  2533 ** ^This routine registers an authorizer callback with a particular
  2534 ** [database connection], supplied in the first argument.
  2535 ** ^The authorizer callback is invoked as SQL statements are being compiled
  2536 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2537 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2538 ** points during the compilation process, as logic is being created
  2539 ** to perform various actions, the authorizer callback is invoked to
  2540 ** see if those actions are allowed.  ^The authorizer callback should
  2541 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
  2542 ** specific action but allow the SQL statement to continue to be
  2543 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
  2544 ** rejected with an error.  ^If the authorizer callback returns
  2545 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
  2546 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
  2547 ** the authorizer will fail with an error message.
  2548 **
  2549 ** When the callback returns [SQLITE_OK], that means the operation
  2550 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
  2551 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
  2552 ** authorizer will fail with an error message explaining that
  2553 ** access is denied. 
  2554 **
  2555 ** ^The first parameter to the authorizer callback is a copy of the third
  2556 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
  2557 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
  2558 ** the particular action to be authorized. ^The third through sixth parameters
  2559 ** to the callback are zero-terminated strings that contain additional
  2560 ** details about the action to be authorized.
  2561 **
  2562 ** ^If the action code is [SQLITE_READ]
  2563 ** and the callback returns [SQLITE_IGNORE] then the
  2564 ** [prepared statement] statement is constructed to substitute
  2565 ** a NULL value in place of the table column that would have
  2566 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
  2567 ** return can be used to deny an untrusted user access to individual
  2568 ** columns of a table.
  2569 ** ^If the action code is [SQLITE_DELETE] and the callback returns
  2570 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
  2571 ** [truncate optimization] is disabled and all rows are deleted individually.
  2572 **
  2573 ** An authorizer is used when [sqlite3_prepare | preparing]
  2574 ** SQL statements from an untrusted source, to ensure that the SQL statements
  2575 ** do not try to access data they are not allowed to see, or that they do not
  2576 ** try to execute malicious statements that damage the database.  For
  2577 ** example, an application may allow a user to enter arbitrary
  2578 ** SQL queries for evaluation by a database.  But the application does
  2579 ** not want the user to be able to make arbitrary changes to the
  2580 ** database.  An authorizer could then be put in place while the
  2581 ** user-entered SQL is being [sqlite3_prepare | prepared] that
  2582 ** disallows everything except [SELECT] statements.
  2583 **
  2584 ** Applications that need to process SQL from untrusted sources
  2585 ** might also consider lowering resource limits using [sqlite3_limit()]
  2586 ** and limiting database size using the [max_page_count] [PRAGMA]
  2587 ** in addition to using an authorizer.
  2588 **
  2589 ** ^(Only a single authorizer can be in place on a database connection
  2590 ** at a time.  Each call to sqlite3_set_authorizer overrides the
  2591 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
  2592 ** The authorizer is disabled by default.
  2593 **
  2594 ** The authorizer callback must not do anything that will modify
  2595 ** the database connection that invoked the authorizer callback.
  2596 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2597 ** database connections for the meaning of "modify" in this paragraph.
  2598 **
  2599 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
  2600 ** statement might be re-prepared during [sqlite3_step()] due to a 
  2601 ** schema change.  Hence, the application should ensure that the
  2602 ** correct authorizer callback remains in place during the [sqlite3_step()].
  2603 **
  2604 ** ^Note that the authorizer callback is invoked only during
  2605 ** [sqlite3_prepare()] or its variants.  Authorization is not
  2606 ** performed during statement evaluation in [sqlite3_step()], unless
  2607 ** as stated in the previous paragraph, sqlite3_step() invokes
  2608 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
  2609 */
  2610 SQLITE_API int sqlite3_set_authorizer(
  2611   sqlite3*,
  2612   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  2613   void *pUserData
  2614 );
  2616 /*
  2617 ** CAPI3REF: Authorizer Return Codes
  2618 **
  2619 ** The [sqlite3_set_authorizer | authorizer callback function] must
  2620 ** return either [SQLITE_OK] or one of these two constants in order
  2621 ** to signal SQLite whether or not the action is permitted.  See the
  2622 ** [sqlite3_set_authorizer | authorizer documentation] for additional
  2623 ** information.
  2624 **
  2625 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
  2626 ** from the [sqlite3_vtab_on_conflict()] interface.
  2627 */
  2628 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
  2629 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
  2631 /*
  2632 ** CAPI3REF: Authorizer Action Codes
  2633 **
  2634 ** The [sqlite3_set_authorizer()] interface registers a callback function
  2635 ** that is invoked to authorize certain SQL statement actions.  The
  2636 ** second parameter to the callback is an integer code that specifies
  2637 ** what action is being authorized.  These are the integer action codes that
  2638 ** the authorizer callback may be passed.
  2639 **
  2640 ** These action code values signify what kind of operation is to be
  2641 ** authorized.  The 3rd and 4th parameters to the authorization
  2642 ** callback function will be parameters or NULL depending on which of these
  2643 ** codes is used as the second parameter.  ^(The 5th parameter to the
  2644 ** authorizer callback is the name of the database ("main", "temp",
  2645 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
  2646 ** is the name of the inner-most trigger or view that is responsible for
  2647 ** the access attempt or NULL if this access attempt is directly from
  2648 ** top-level SQL code.
  2649 */
  2650 /******************************************* 3rd ************ 4th ***********/
  2651 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
  2652 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
  2653 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
  2654 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
  2655 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
  2656 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
  2657 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
  2658 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
  2659 #define SQLITE_DELETE                9   /* Table Name      NULL            */
  2660 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
  2661 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
  2662 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
  2663 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
  2664 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
  2665 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
  2666 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
  2667 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
  2668 #define SQLITE_INSERT               18   /* Table Name      NULL            */
  2669 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
  2670 #define SQLITE_READ                 20   /* Table Name      Column Name     */
  2671 #define SQLITE_SELECT               21   /* NULL            NULL            */
  2672 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
  2673 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
  2674 #define SQLITE_ATTACH               24   /* Filename        NULL            */
  2675 #define SQLITE_DETACH               25   /* Database Name   NULL            */
  2676 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
  2677 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
  2678 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
  2679 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
  2680 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
  2681 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
  2682 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
  2683 #define SQLITE_COPY                  0   /* No longer used */
  2684 #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
  2686 /*
  2687 ** CAPI3REF: Tracing And Profiling Functions
  2688 **
  2689 ** These routines register callback functions that can be used for
  2690 ** tracing and profiling the execution of SQL statements.
  2691 **
  2692 ** ^The callback function registered by sqlite3_trace() is invoked at
  2693 ** various times when an SQL statement is being run by [sqlite3_step()].
  2694 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
  2695 ** SQL statement text as the statement first begins executing.
  2696 ** ^(Additional sqlite3_trace() callbacks might occur
  2697 ** as each triggered subprogram is entered.  The callbacks for triggers
  2698 ** contain a UTF-8 SQL comment that identifies the trigger.)^
  2699 **
  2700 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
  2701 ** the length of [bound parameter] expansion in the output of sqlite3_trace().
  2702 **
  2703 ** ^The callback function registered by sqlite3_profile() is invoked
  2704 ** as each SQL statement finishes.  ^The profile callback contains
  2705 ** the original statement text and an estimate of wall-clock time
  2706 ** of how long that statement took to run.  ^The profile callback
  2707 ** time is in units of nanoseconds, however the current implementation
  2708 ** is only capable of millisecond resolution so the six least significant
  2709 ** digits in the time are meaningless.  Future versions of SQLite
  2710 ** might provide greater resolution on the profiler callback.  The
  2711 ** sqlite3_profile() function is considered experimental and is
  2712 ** subject to change in future versions of SQLite.
  2713 */
  2714 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2715 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  2716    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2718 /*
  2719 ** CAPI3REF: Query Progress Callbacks
  2720 **
  2721 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2722 ** function X to be invoked periodically during long running calls to
  2723 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2724 ** database connection D.  An example use for this
  2725 ** interface is to keep a GUI updated during a large query.
  2726 **
  2727 ** ^The parameter P is passed through as the only parameter to the 
  2728 ** callback function X.  ^The parameter N is the approximate number of 
  2729 ** [virtual machine instructions] that are evaluated between successive
  2730 ** invocations of the callback X.  ^If N is less than one then the progress
  2731 ** handler is disabled.
  2732 **
  2733 ** ^Only a single progress handler may be defined at one time per
  2734 ** [database connection]; setting a new progress handler cancels the
  2735 ** old one.  ^Setting parameter X to NULL disables the progress handler.
  2736 ** ^The progress handler is also disabled by setting N to a value less
  2737 ** than 1.
  2738 **
  2739 ** ^If the progress callback returns non-zero, the operation is
  2740 ** interrupted.  This feature can be used to implement a
  2741 ** "Cancel" button on a GUI progress dialog box.
  2742 **
  2743 ** The progress handler callback must not do anything that will modify
  2744 ** the database connection that invoked the progress handler.
  2745 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2746 ** database connections for the meaning of "modify" in this paragraph.
  2747 **
  2748 */
  2749 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2751 /*
  2752 ** CAPI3REF: Opening A New Database Connection
  2753 **
  2754 ** ^These routines open an SQLite database file as specified by the 
  2755 ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2756 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2757 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2758 ** returned in *ppDb, even if an error occurs.  The only exception is that
  2759 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
  2760 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  2761 ** object.)^ ^(If the database is opened (and/or created) successfully, then
  2762 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
  2763 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  2764 ** an English language description of the error following a failure of any
  2765 ** of the sqlite3_open() routines.
  2766 **
  2767 ** ^The default encoding for the database will be UTF-8 if
  2768 ** sqlite3_open() or sqlite3_open_v2() is called and
  2769 ** UTF-16 in the native byte order if sqlite3_open16() is used.
  2770 **
  2771 ** Whether or not an error occurs when it is opened, resources
  2772 ** associated with the [database connection] handle should be released by
  2773 ** passing it to [sqlite3_close()] when it is no longer required.
  2774 **
  2775 ** The sqlite3_open_v2() interface works like sqlite3_open()
  2776 ** except that it accepts two additional parameters for additional control
  2777 ** over the new database connection.  ^(The flags parameter to
  2778 ** sqlite3_open_v2() can take one of
  2779 ** the following three values, optionally combined with the 
  2780 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
  2781 ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
  2782 **
  2783 ** <dl>
  2784 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
  2785 ** <dd>The database is opened in read-only mode.  If the database does not
  2786 ** already exist, an error is returned.</dd>)^
  2787 **
  2788 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  2789 ** <dd>The database is opened for reading and writing if possible, or reading
  2790 ** only if the file is write protected by the operating system.  In either
  2791 ** case the database must already exist, otherwise an error is returned.</dd>)^
  2792 **
  2793 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  2794 ** <dd>The database is opened for reading and writing, and is created if
  2795 ** it does not already exist. This is the behavior that is always used for
  2796 ** sqlite3_open() and sqlite3_open16().</dd>)^
  2797 ** </dl>
  2798 **
  2799 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
  2800 ** combinations shown above optionally combined with other
  2801 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
  2802 ** then the behavior is undefined.
  2803 **
  2804 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
  2805 ** opens in the multi-thread [threading mode] as long as the single-thread
  2806 ** mode has not been set at compile-time or start-time.  ^If the
  2807 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
  2808 ** in the serialized [threading mode] unless single-thread was
  2809 ** previously selected at compile-time or start-time.
  2810 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
  2811 ** eligible to use [shared cache mode], regardless of whether or not shared
  2812 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
  2813 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
  2814 ** participate in [shared cache mode] even if it is enabled.
  2815 **
  2816 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
  2817 ** [sqlite3_vfs] object that defines the operating system interface that
  2818 ** the new database connection should use.  ^If the fourth parameter is
  2819 ** a NULL pointer then the default [sqlite3_vfs] object is used.
  2820 **
  2821 ** ^If the filename is ":memory:", then a private, temporary in-memory database
  2822 ** is created for the connection.  ^This in-memory database will vanish when
  2823 ** the database connection is closed.  Future versions of SQLite might
  2824 ** make use of additional special filenames that begin with the ":" character.
  2825 ** It is recommended that when a database filename actually does begin with
  2826 ** a ":" character you should prefix the filename with a pathname such as
  2827 ** "./" to avoid ambiguity.
  2828 **
  2829 ** ^If the filename is an empty string, then a private, temporary
  2830 ** on-disk database will be created.  ^This private database will be
  2831 ** automatically deleted as soon as the database connection is closed.
  2832 **
  2833 ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
  2834 **
  2835 ** ^If [URI filename] interpretation is enabled, and the filename argument
  2836 ** begins with "file:", then the filename is interpreted as a URI. ^URI
  2837 ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
  2838 ** set in the fourth argument to sqlite3_open_v2(), or if it has
  2839 ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
  2840 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
  2841 ** As of SQLite version 3.7.7, URI filename interpretation is turned off
  2842 ** by default, but future releases of SQLite might enable URI filename
  2843 ** interpretation by default.  See "[URI filenames]" for additional
  2844 ** information.
  2845 **
  2846 ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
  2847 ** authority, then it must be either an empty string or the string 
  2848 ** "localhost". ^If the authority is not an empty string or "localhost", an 
  2849 ** error is returned to the caller. ^The fragment component of a URI, if 
  2850 ** present, is ignored.
  2851 **
  2852 ** ^SQLite uses the path component of the URI as the name of the disk file
  2853 ** which contains the database. ^If the path begins with a '/' character, 
  2854 ** then it is interpreted as an absolute path. ^If the path does not begin 
  2855 ** with a '/' (meaning that the authority section is omitted from the URI)
  2856 ** then the path is interpreted as a relative path. 
  2857 ** ^On windows, the first component of an absolute path 
  2858 ** is a drive specification (e.g. "C:").
  2859 **
  2860 ** [[core URI query parameters]]
  2861 ** The query component of a URI may contain parameters that are interpreted
  2862 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
  2863 ** SQLite interprets the following three query parameters:
  2864 **
  2865 ** <ul>
  2866 **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
  2867 **     a VFS object that provides the operating system interface that should
  2868 **     be used to access the database file on disk. ^If this option is set to
  2869 **     an empty string the default VFS object is used. ^Specifying an unknown
  2870 **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
  2871 **     present, then the VFS specified by the option takes precedence over
  2872 **     the value passed as the fourth parameter to sqlite3_open_v2().
  2873 **
  2874 **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
  2875 **     "rwc", or "memory". Attempting to set it to any other value is
  2876 **     an error)^. 
  2877 **     ^If "ro" is specified, then the database is opened for read-only 
  2878 **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
  2879 **     third argument to sqlite3_open_v2(). ^If the mode option is set to 
  2880 **     "rw", then the database is opened for read-write (but not create) 
  2881 **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
  2882 **     been set. ^Value "rwc" is equivalent to setting both 
  2883 **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
  2884 **     set to "memory" then a pure [in-memory database] that never reads
  2885 **     or writes from disk is used. ^It is an error to specify a value for
  2886 **     the mode parameter that is less restrictive than that specified by
  2887 **     the flags passed in the third parameter to sqlite3_open_v2().
  2888 **
  2889 **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
  2890 **     "private". ^Setting it to "shared" is equivalent to setting the
  2891 **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  2892 **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
  2893 **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  2894 **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  2895 **     a URI filename, its value overrides any behavior requested by setting
  2896 **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
  2897 ** </ul>
  2898 **
  2899 ** ^Specifying an unknown parameter in the query component of a URI is not an
  2900 ** error.  Future versions of SQLite might understand additional query
  2901 ** parameters.  See "[query parameters with special meaning to SQLite]" for
  2902 ** additional information.
  2903 **
  2904 ** [[URI filename examples]] <h3>URI filename examples</h3>
  2905 **
  2906 ** <table border="1" align=center cellpadding=5>
  2907 ** <tr><th> URI filenames <th> Results
  2908 ** <tr><td> file:data.db <td> 
  2909 **          Open the file "data.db" in the current directory.
  2910 ** <tr><td> file:/home/fred/data.db<br>
  2911 **          file:///home/fred/data.db <br> 
  2912 **          file://localhost/home/fred/data.db <br> <td> 
  2913 **          Open the database file "/home/fred/data.db".
  2914 ** <tr><td> file://darkstar/home/fred/data.db <td> 
  2915 **          An error. "darkstar" is not a recognized authority.
  2916 ** <tr><td style="white-space:nowrap"> 
  2917 **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
  2918 **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
  2919 **          C:. Note that the %20 escaping in this example is not strictly 
  2920 **          necessary - space characters can be used literally
  2921 **          in URI filenames.
  2922 ** <tr><td> file:data.db?mode=ro&cache=private <td> 
  2923 **          Open file "data.db" in the current directory for read-only access.
  2924 **          Regardless of whether or not shared-cache mode is enabled by
  2925 **          default, use a private cache.
  2926 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
  2927 **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
  2928 ** <tr><td> file:data.db?mode=readonly <td> 
  2929 **          An error. "readonly" is not a valid option for the "mode" parameter.
  2930 ** </table>
  2931 **
  2932 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
  2933 ** query components of a URI. A hexadecimal escape sequence consists of a
  2934 ** percent sign - "%" - followed by exactly two hexadecimal digits 
  2935 ** specifying an octet value. ^Before the path or query components of a
  2936 ** URI filename are interpreted, they are encoded using UTF-8 and all 
  2937 ** hexadecimal escape sequences replaced by a single byte containing the
  2938 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
  2939 ** the results are undefined.
  2940 **
  2941 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
  2942 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
  2943 ** codepage is currently defined.  Filenames containing international
  2944 ** characters must be converted to UTF-8 prior to passing them into
  2945 ** sqlite3_open() or sqlite3_open_v2().
  2946 **
  2947 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  2948 ** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
  2949 ** features that require the use of temporary files may fail.
  2950 **
  2951 ** See also: [sqlite3_temp_directory]
  2952 */
  2953 SQLITE_API int sqlite3_open(
  2954   const char *filename,   /* Database filename (UTF-8) */
  2955   sqlite3 **ppDb          /* OUT: SQLite db handle */
  2956 );
  2957 SQLITE_API int sqlite3_open16(
  2958   const void *filename,   /* Database filename (UTF-16) */
  2959   sqlite3 **ppDb          /* OUT: SQLite db handle */
  2960 );
  2961 SQLITE_API int sqlite3_open_v2(
  2962   const char *filename,   /* Database filename (UTF-8) */
  2963   sqlite3 **ppDb,         /* OUT: SQLite db handle */
  2964   int flags,              /* Flags */
  2965   const char *zVfs        /* Name of VFS module to use */
  2966 );
  2968 /*
  2969 ** CAPI3REF: Obtain Values For URI Parameters
  2970 **
  2971 ** These are utility routines, useful to VFS implementations, that check
  2972 ** to see if a database file was a URI that contained a specific query 
  2973 ** parameter, and if so obtains the value of that query parameter.
  2974 **
  2975 ** If F is the database filename pointer passed into the xOpen() method of 
  2976 ** a VFS implementation when the flags parameter to xOpen() has one or 
  2977 ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
  2978 ** P is the name of the query parameter, then
  2979 ** sqlite3_uri_parameter(F,P) returns the value of the P
  2980 ** parameter if it exists or a NULL pointer if P does not appear as a 
  2981 ** query parameter on F.  If P is a query parameter of F
  2982 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
  2983 ** a pointer to an empty string.
  2984 **
  2985 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
  2986 ** parameter and returns true (1) or false (0) according to the value
  2987 ** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
  2988 ** value of query parameter P is one of "yes", "true", or "on" in any
  2989 ** case or if the value begins with a non-zero number.  The 
  2990 ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
  2991 ** query parameter P is one of "no", "false", or "off" in any case or
  2992 ** if the value begins with a numeric zero.  If P is not a query
  2993 ** parameter on F or if the value of P is does not match any of the
  2994 ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
  2995 **
  2996 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
  2997 ** 64-bit signed integer and returns that integer, or D if P does not
  2998 ** exist.  If the value of P is something other than an integer, then
  2999 ** zero is returned.
  3000 ** 
  3001 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
  3002 ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
  3003 ** is not a database file pathname pointer that SQLite passed into the xOpen
  3004 ** VFS method, then the behavior of this routine is undefined and probably
  3005 ** undesirable.
  3006 */
  3007 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3008 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3009 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3012 /*
  3013 ** CAPI3REF: Error Codes And Messages
  3014 **
  3015 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
  3016 ** [extended result code] for the most recent failed sqlite3_* API call
  3017 ** associated with a [database connection]. If a prior API call failed
  3018 ** but the most recent API call succeeded, the return value from
  3019 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
  3020 ** interface is the same except that it always returns the 
  3021 ** [extended result code] even when extended result codes are
  3022 ** disabled.
  3023 **
  3024 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  3025 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  3026 ** ^(Memory to hold the error message string is managed internally.
  3027 ** The application does not need to worry about freeing the result.
  3028 ** However, the error string might be overwritten or deallocated by
  3029 ** subsequent calls to other SQLite interface functions.)^
  3030 **
  3031 ** ^The sqlite3_errstr() interface returns the English-language text
  3032 ** that describes the [result code], as UTF-8.
  3033 ** ^(Memory to hold the error message string is managed internally
  3034 ** and must not be freed by the application)^.
  3035 **
  3036 ** When the serialized [threading mode] is in use, it might be the
  3037 ** case that a second error occurs on a separate thread in between
  3038 ** the time of the first error and the call to these interfaces.
  3039 ** When that happens, the second error will be reported since these
  3040 ** interfaces always report the most recent result.  To avoid
  3041 ** this, each thread can obtain exclusive use of the [database connection] D
  3042 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
  3043 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
  3044 ** all calls to the interfaces listed here are completed.
  3045 **
  3046 ** If an interface fails with SQLITE_MISUSE, that means the interface
  3047 ** was invoked incorrectly by the application.  In that case, the
  3048 ** error code and message may or may not be set.
  3049 */
  3050 SQLITE_API int sqlite3_errcode(sqlite3 *db);
  3051 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
  3052 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
  3053 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
  3054 SQLITE_API const char *sqlite3_errstr(int);
  3056 /*
  3057 ** CAPI3REF: SQL Statement Object
  3058 ** KEYWORDS: {prepared statement} {prepared statements}
  3059 **
  3060 ** An instance of this object represents a single SQL statement.
  3061 ** This object is variously known as a "prepared statement" or a
  3062 ** "compiled SQL statement" or simply as a "statement".
  3063 **
  3064 ** The life of a statement object goes something like this:
  3065 **
  3066 ** <ol>
  3067 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3068 **      function.
  3069 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
  3070 **      interfaces.
  3071 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3072 ** <li> Reset the statement using [sqlite3_reset()] then go back
  3073 **      to step 2.  Do this zero or more times.
  3074 ** <li> Destroy the object using [sqlite3_finalize()].
  3075 ** </ol>
  3076 **
  3077 ** Refer to documentation on individual methods above for additional
  3078 ** information.
  3079 */
  3080 typedef struct sqlite3_stmt sqlite3_stmt;
  3082 /*
  3083 ** CAPI3REF: Run-time Limits
  3084 **
  3085 ** ^(This interface allows the size of various constructs to be limited
  3086 ** on a connection by connection basis.  The first parameter is the
  3087 ** [database connection] whose limit is to be set or queried.  The
  3088 ** second parameter is one of the [limit categories] that define a
  3089 ** class of constructs to be size limited.  The third parameter is the
  3090 ** new limit for that construct.)^
  3091 **
  3092 ** ^If the new limit is a negative number, the limit is unchanged.
  3093 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 
  3094 ** [limits | hard upper bound]
  3095 ** set at compile-time by a C preprocessor macro called
  3096 ** [limits | SQLITE_MAX_<i>NAME</i>].
  3097 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
  3098 ** ^Attempts to increase a limit above its hard upper bound are
  3099 ** silently truncated to the hard upper bound.
  3100 **
  3101 ** ^Regardless of whether or not the limit was changed, the 
  3102 ** [sqlite3_limit()] interface returns the prior value of the limit.
  3103 ** ^Hence, to find the current value of a limit without changing it,
  3104 ** simply invoke this interface with the third parameter set to -1.
  3105 **
  3106 ** Run-time limits are intended for use in applications that manage
  3107 ** both their own internal database and also databases that are controlled
  3108 ** by untrusted external sources.  An example application might be a
  3109 ** web browser that has its own databases for storing history and
  3110 ** separate databases controlled by JavaScript applications downloaded
  3111 ** off the Internet.  The internal databases can be given the
  3112 ** large, default limits.  Databases managed by external sources can
  3113 ** be given much smaller limits designed to prevent a denial of service
  3114 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
  3115 ** interface to further control untrusted SQL.  The size of the database
  3116 ** created by an untrusted script can be contained using the
  3117 ** [max_page_count] [PRAGMA].
  3118 **
  3119 ** New run-time limit categories may be added in future releases.
  3120 */
  3121 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
  3123 /*
  3124 ** CAPI3REF: Run-Time Limit Categories
  3125 ** KEYWORDS: {limit category} {*limit categories}
  3126 **
  3127 ** These constants define various performance limits
  3128 ** that can be lowered at run-time using [sqlite3_limit()].
  3129 ** The synopsis of the meanings of the various limits is shown below.
  3130 ** Additional information is available at [limits | Limits in SQLite].
  3131 **
  3132 ** <dl>
  3133 ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
  3134 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
  3135 **
  3136 ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
  3137 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
  3138 **
  3139 ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
  3140 ** <dd>The maximum number of columns in a table definition or in the
  3141 ** result set of a [SELECT] or the maximum number of columns in an index
  3142 ** or in an ORDER BY or GROUP BY clause.</dd>)^
  3143 **
  3144 ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
  3145 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3146 **
  3147 ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3148 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3149 **
  3150 ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3151 ** <dd>The maximum number of instructions in a virtual machine program
  3152 ** used to implement an SQL statement.  This limit is not currently
  3153 ** enforced, though that might be added in some future release of
  3154 ** SQLite.</dd>)^
  3155 **
  3156 ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3157 ** <dd>The maximum number of arguments on a function.</dd>)^
  3158 **
  3159 ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3160 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3161 **
  3162 ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
  3163 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
  3164 ** <dd>The maximum length of the pattern argument to the [LIKE] or
  3165 ** [GLOB] operators.</dd>)^
  3166 **
  3167 ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
  3168 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
  3169 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
  3170 **
  3171 ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
  3172 ** <dd>The maximum depth of recursion for triggers.</dd>)^
  3173 ** </dl>
  3174 */
  3175 #define SQLITE_LIMIT_LENGTH                    0
  3176 #define SQLITE_LIMIT_SQL_LENGTH                1
  3177 #define SQLITE_LIMIT_COLUMN                    2
  3178 #define SQLITE_LIMIT_EXPR_DEPTH                3
  3179 #define SQLITE_LIMIT_COMPOUND_SELECT           4
  3180 #define SQLITE_LIMIT_VDBE_OP                   5
  3181 #define SQLITE_LIMIT_FUNCTION_ARG              6
  3182 #define SQLITE_LIMIT_ATTACHED                  7
  3183 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
  3184 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
  3185 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
  3187 /*
  3188 ** CAPI3REF: Compiling An SQL Statement
  3189 ** KEYWORDS: {SQL statement compiler}
  3190 **
  3191 ** To execute an SQL query, it must first be compiled into a byte-code
  3192 ** program using one of these routines.
  3193 **
  3194 ** The first argument, "db", is a [database connection] obtained from a
  3195 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3196 ** [sqlite3_open16()].  The database connection must not have been closed.
  3197 **
  3198 ** The second argument, "zSql", is the statement to be compiled, encoded
  3199 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
  3200 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
  3201 ** use UTF-16.
  3202 **
  3203 ** ^If the nByte argument is less than zero, then zSql is read up to the
  3204 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  3205 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
  3206 ** zSql string ends at either the first '\000' or '\u0000' character or
  3207 ** the nByte-th byte, whichever comes first. If the caller knows
  3208 ** that the supplied string is nul-terminated, then there is a small
  3209 ** performance advantage to be gained by passing an nByte parameter that
  3210 ** is equal to the number of bytes in the input string <i>including</i>
  3211 ** the nul-terminator bytes as this saves SQLite from having to
  3212 ** make a copy of the input string.
  3213 **
  3214 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  3215 ** past the end of the first SQL statement in zSql.  These routines only
  3216 ** compile the first statement in zSql, so *pzTail is left pointing to
  3217 ** what remains uncompiled.
  3218 **
  3219 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
  3220 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
  3221 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
  3222 ** string or a comment) then *ppStmt is set to NULL.
  3223 ** The calling procedure is responsible for deleting the compiled
  3224 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
  3225 ** ppStmt may not be NULL.
  3226 **
  3227 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
  3228 ** otherwise an [error code] is returned.
  3229 **
  3230 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
  3231 ** recommended for all new programs. The two older interfaces are retained
  3232 ** for backwards compatibility, but their use is discouraged.
  3233 ** ^In the "v2" interfaces, the prepared statement
  3234 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
  3235 ** original SQL text. This causes the [sqlite3_step()] interface to
  3236 ** behave differently in three ways:
  3237 **
  3238 ** <ol>
  3239 ** <li>
  3240 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
  3241 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
  3242 ** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
  3243 ** retries will occur before sqlite3_step() gives up and returns an error.
  3244 ** </li>
  3245 **
  3246 ** <li>
  3247 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
  3248 ** [error codes] or [extended error codes].  ^The legacy behavior was that
  3249 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
  3250 ** and the application would have to make a second call to [sqlite3_reset()]
  3251 ** in order to find the underlying cause of the problem. With the "v2" prepare
  3252 ** interfaces, the underlying reason for the error is returned immediately.
  3253 ** </li>
  3254 **
  3255 ** <li>
  3256 ** ^If the specific value bound to [parameter | host parameter] in the 
  3257 ** WHERE clause might influence the choice of query plan for a statement,
  3258 ** then the statement will be automatically recompiled, as if there had been 
  3259 ** a schema change, on the first  [sqlite3_step()] call following any change
  3260 ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3261 ** ^The specific value of WHERE-clause [parameter] might influence the 
  3262 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3263 ** or [GLOB] operator or if the parameter is compared to an indexed column
  3264 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3265 ** </li>
  3266 ** </ol>
  3267 */
  3268 SQLITE_API int sqlite3_prepare(
  3269   sqlite3 *db,            /* Database handle */
  3270   const char *zSql,       /* SQL statement, UTF-8 encoded */
  3271   int nByte,              /* Maximum length of zSql in bytes. */
  3272   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3273   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  3274 );
  3275 SQLITE_API int sqlite3_prepare_v2(
  3276   sqlite3 *db,            /* Database handle */
  3277   const char *zSql,       /* SQL statement, UTF-8 encoded */
  3278   int nByte,              /* Maximum length of zSql in bytes. */
  3279   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3280   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  3281 );
  3282 SQLITE_API int sqlite3_prepare16(
  3283   sqlite3 *db,            /* Database handle */
  3284   const void *zSql,       /* SQL statement, UTF-16 encoded */
  3285   int nByte,              /* Maximum length of zSql in bytes. */
  3286   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3287   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3288 );
  3289 SQLITE_API int sqlite3_prepare16_v2(
  3290   sqlite3 *db,            /* Database handle */
  3291   const void *zSql,       /* SQL statement, UTF-16 encoded */
  3292   int nByte,              /* Maximum length of zSql in bytes. */
  3293   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  3294   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
  3295 );
  3297 /*
  3298 ** CAPI3REF: Retrieving Statement SQL
  3299 **
  3300 ** ^This interface can be used to retrieve a saved copy of the original
  3301 ** SQL text used to create a [prepared statement] if that statement was
  3302 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3303 */
  3304 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3306 /*
  3307 ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3308 **
  3309 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3310 ** and only if the [prepared statement] X makes no direct changes to
  3311 ** the content of the database file.
  3312 **
  3313 ** Note that [application-defined SQL functions] or
  3314 ** [virtual tables] might change the database indirectly as a side effect.  
  3315 ** ^(For example, if an application defines a function "eval()" that 
  3316 ** calls [sqlite3_exec()], then the following SQL statement would
  3317 ** change the database file through side-effects:
  3318 **
  3319 ** <blockquote><pre>
  3320 **    SELECT eval('DELETE FROM t1') FROM t2;
  3321 ** </pre></blockquote>
  3322 **
  3323 ** But because the [SELECT] statement does not change the database file
  3324 ** directly, sqlite3_stmt_readonly() would still return true.)^
  3325 **
  3326 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
  3327 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
  3328 ** since the statements themselves do not actually modify the database but
  3329 ** rather they control the timing of when other statements modify the 
  3330 ** database.  ^The [ATTACH] and [DETACH] statements also cause
  3331 ** sqlite3_stmt_readonly() to return true since, while those statements
  3332 ** change the configuration of a database connection, they do not make 
  3333 ** changes to the content of the database files on disk.
  3334 */
  3335 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3337 /*
  3338 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  3339 **
  3340 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3341 ** [prepared statement] S has been stepped at least once using 
  3342 ** [sqlite3_step(S)] but has not run to completion and/or has not 
  3343 ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
  3344 ** interface returns false if S is a NULL pointer.  If S is not a 
  3345 ** NULL pointer and is not a pointer to a valid [prepared statement]
  3346 ** object, then the behavior is undefined and probably undesirable.
  3347 **
  3348 ** This interface can be used in combination [sqlite3_next_stmt()]
  3349 ** to locate all prepared statements associated with a database 
  3350 ** connection that are in need of being reset.  This can be used,
  3351 ** for example, in diagnostic routines to search for prepared 
  3352 ** statements that are holding a transaction open.
  3353 */
  3354 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
  3356 /*
  3357 ** CAPI3REF: Dynamically Typed Value Object
  3358 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  3359 **
  3360 ** SQLite uses the sqlite3_value object to represent all values
  3361 ** that can be stored in a database table. SQLite uses dynamic typing
  3362 ** for the values it stores.  ^Values stored in sqlite3_value objects
  3363 ** can be integers, floating point values, strings, BLOBs, or NULL.
  3364 **
  3365 ** An sqlite3_value object may be either "protected" or "unprotected".
  3366 ** Some interfaces require a protected sqlite3_value.  Other interfaces
  3367 ** will accept either a protected or an unprotected sqlite3_value.
  3368 ** Every interface that accepts sqlite3_value arguments specifies
  3369 ** whether or not it requires a protected sqlite3_value.
  3370 **
  3371 ** The terms "protected" and "unprotected" refer to whether or not
  3372 ** a mutex is held.  An internal mutex is held for a protected
  3373 ** sqlite3_value object but no mutex is held for an unprotected
  3374 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
  3375 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  3376 ** or if SQLite is run in one of reduced mutex modes 
  3377 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  3378 ** then there is no distinction between protected and unprotected
  3379 ** sqlite3_value objects and they can be used interchangeably.  However,
  3380 ** for maximum code portability it is recommended that applications
  3381 ** still make the distinction between protected and unprotected
  3382 ** sqlite3_value objects even when not strictly required.
  3383 **
  3384 ** ^The sqlite3_value objects that are passed as parameters into the
  3385 ** implementation of [application-defined SQL functions] are protected.
  3386 ** ^The sqlite3_value object returned by
  3387 ** [sqlite3_column_value()] is unprotected.
  3388 ** Unprotected sqlite3_value objects may only be used with
  3389 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
  3390 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  3391 ** interfaces require protected sqlite3_value objects.
  3392 */
  3393 typedef struct Mem sqlite3_value;
  3395 /*
  3396 ** CAPI3REF: SQL Function Context Object
  3397 **
  3398 ** The context in which an SQL function executes is stored in an
  3399 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
  3400 ** is always first parameter to [application-defined SQL functions].
  3401 ** The application-defined SQL function implementation will pass this
  3402 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
  3403 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
  3404 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
  3405 ** and/or [sqlite3_set_auxdata()].
  3406 */
  3407 typedef struct sqlite3_context sqlite3_context;
  3409 /*
  3410 ** CAPI3REF: Binding Values To Prepared Statements
  3411 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3412 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
  3413 **
  3414 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3415 ** literals may be replaced by a [parameter] that matches one of following
  3416 ** templates:
  3417 **
  3418 ** <ul>
  3419 ** <li>  ?
  3420 ** <li>  ?NNN
  3421 ** <li>  :VVV
  3422 ** <li>  @VVV
  3423 ** <li>  $VVV
  3424 ** </ul>
  3425 **
  3426 ** In the templates above, NNN represents an integer literal,
  3427 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
  3428 ** parameters (also called "host parameter names" or "SQL parameters")
  3429 ** can be set using the sqlite3_bind_*() routines defined here.
  3430 **
  3431 ** ^The first argument to the sqlite3_bind_*() routines is always
  3432 ** a pointer to the [sqlite3_stmt] object returned from
  3433 ** [sqlite3_prepare_v2()] or its variants.
  3434 **
  3435 ** ^The second argument is the index of the SQL parameter to be set.
  3436 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
  3437 ** SQL parameter is used more than once, second and subsequent
  3438 ** occurrences have the same index as the first occurrence.
  3439 ** ^The index for named parameters can be looked up using the
  3440 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
  3441 ** for "?NNN" parameters is the value of NNN.
  3442 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
  3443 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  3444 **
  3445 ** ^The third argument is the value to bind to the parameter.
  3446 ** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  3447 ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
  3448 ** is ignored and the end result is the same as sqlite3_bind_null().
  3449 **
  3450 ** ^(In those routines that have a fourth argument, its value is the
  3451 ** number of bytes in the parameter.  To be clear: the value is the
  3452 ** number of <u>bytes</u> in the value, not the number of characters.)^
  3453 ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  3454 ** is negative, then the length of the string is
  3455 ** the number of bytes up to the first zero terminator.
  3456 ** If the fourth parameter to sqlite3_bind_blob() is negative, then
  3457 ** the behavior is undefined.
  3458 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3459 ** or sqlite3_bind_text16() then that parameter must be the byte offset
  3460 ** where the NUL terminator would occur assuming the string were NUL
  3461 ** terminated.  If any NUL characters occur at byte offsets less than 
  3462 ** the value of the fourth parameter then the resulting string value will
  3463 ** contain embedded NULs.  The result of expressions involving strings
  3464 ** with embedded NULs is undefined.
  3465 **
  3466 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3467 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  3468 ** string after SQLite has finished with it.  ^The destructor is called
  3469 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3470 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3471 ** ^If the fifth argument is
  3472 ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3473 ** information is in static, unmanaged space and does not need to be freed.
  3474 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3475 ** SQLite makes its own private copy of the data immediately, before
  3476 ** the sqlite3_bind_*() routine returns.
  3477 **
  3478 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3479 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3480 ** (just an integer to hold its size) while it is being processed.
  3481 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3482 ** content is later written using
  3483 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
  3484 ** ^A negative value for the zeroblob results in a zero-length BLOB.
  3485 **
  3486 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  3487 ** for the [prepared statement] or with a prepared statement for which
  3488 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  3489 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
  3490 ** routine is passed a [prepared statement] that has been finalized, the
  3491 ** result is undefined and probably harmful.
  3492 **
  3493 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3494 ** ^Unbound parameters are interpreted as NULL.
  3495 **
  3496 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3497 ** [error code] if anything goes wrong.
  3498 ** ^[SQLITE_RANGE] is returned if the parameter
  3499 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
  3500 **
  3501 ** See also: [sqlite3_bind_parameter_count()],
  3502 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3503 */
  3504 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  3505 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3506 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3507 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3508 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
  3509 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
  3510 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  3511 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3512 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3514 /*
  3515 ** CAPI3REF: Number Of SQL Parameters
  3516 **
  3517 ** ^This routine can be used to find the number of [SQL parameters]
  3518 ** in a [prepared statement].  SQL parameters are tokens of the
  3519 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3520 ** placeholders for values that are [sqlite3_bind_blob | bound]
  3521 ** to the parameters at a later time.
  3522 **
  3523 ** ^(This routine actually returns the index of the largest (rightmost)
  3524 ** parameter. For all forms except ?NNN, this will correspond to the
  3525 ** number of unique parameters.  If parameters of the ?NNN form are used,
  3526 ** there may be gaps in the list.)^
  3527 **
  3528 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3529 ** [sqlite3_bind_parameter_name()], and
  3530 ** [sqlite3_bind_parameter_index()].
  3531 */
  3532 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
  3534 /*
  3535 ** CAPI3REF: Name Of A Host Parameter
  3536 **
  3537 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3538 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3539 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3540 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3541 ** respectively.
  3542 ** In other words, the initial ":" or "$" or "@" or "?"
  3543 ** is included as part of the name.)^
  3544 ** ^Parameters of the form "?" without a following integer have no name
  3545 ** and are referred to as "nameless" or "anonymous parameters".
  3546 **
  3547 ** ^The first host parameter has an index of 1, not 0.
  3548 **
  3549 ** ^If the value N is out of range or if the N-th parameter is
  3550 ** nameless, then NULL is returned.  ^The returned string is
  3551 ** always in UTF-8 encoding even if the named parameter was
  3552 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
  3553 ** [sqlite3_prepare16_v2()].
  3554 **
  3555 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3556 ** [sqlite3_bind_parameter_count()], and
  3557 ** [sqlite3_bind_parameter_index()].
  3558 */
  3559 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3561 /*
  3562 ** CAPI3REF: Index Of A Parameter With A Given Name
  3563 **
  3564 ** ^Return the index of an SQL parameter given its name.  ^The
  3565 ** index value returned is suitable for use as the second
  3566 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
  3567 ** is returned if no matching parameter is found.  ^The parameter
  3568 ** name must be given in UTF-8 even if the original statement
  3569 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
  3570 **
  3571 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3572 ** [sqlite3_bind_parameter_count()], and
  3573 ** [sqlite3_bind_parameter_index()].
  3574 */
  3575 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3577 /*
  3578 ** CAPI3REF: Reset All Bindings On A Prepared Statement
  3579 **
  3580 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3581 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3582 ** ^Use this routine to reset all host parameters to NULL.
  3583 */
  3584 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  3586 /*
  3587 ** CAPI3REF: Number Of Columns In A Result Set
  3588 **
  3589 ** ^Return the number of columns in the result set returned by the
  3590 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3591 ** statement that does not return data (for example an [UPDATE]).
  3592 **
  3593 ** See also: [sqlite3_data_count()]
  3594 */
  3595 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  3597 /*
  3598 ** CAPI3REF: Column Names In A Result Set
  3599 **
  3600 ** ^These routines return the name assigned to a particular column
  3601 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
  3602 ** interface returns a pointer to a zero-terminated UTF-8 string
  3603 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3604 ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3605 ** that implements the [SELECT] statement. ^The second parameter is the
  3606 ** column number.  ^The leftmost column is number 0.
  3607 **
  3608 ** ^The returned string pointer is valid until either the [prepared statement]
  3609 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
  3610 ** reprepared by the first call to [sqlite3_step()] for a particular run
  3611 ** or until the next call to
  3612 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  3613 **
  3614 ** ^If sqlite3_malloc() fails during the processing of either routine
  3615 ** (for example during a conversion from UTF-8 to UTF-16) then a
  3616 ** NULL pointer is returned.
  3617 **
  3618 ** ^The name of a result column is the value of the "AS" clause for
  3619 ** that column, if there is an AS clause.  If there is no AS clause
  3620 ** then the name of the column is unspecified and may change from
  3621 ** one release of SQLite to the next.
  3622 */
  3623 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
  3624 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  3626 /*
  3627 ** CAPI3REF: Source Of Data In A Query Result
  3628 **
  3629 ** ^These routines provide a means to determine the database, table, and
  3630 ** table column that is the origin of a particular result column in
  3631 ** [SELECT] statement.
  3632 ** ^The name of the database or table or column can be returned as
  3633 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3634 ** the database name, the _table_ routines return the table name, and
  3635 ** the origin_ routines return the column name.
  3636 ** ^The returned string is valid until the [prepared statement] is destroyed
  3637 ** using [sqlite3_finalize()] or until the statement is automatically
  3638 ** reprepared by the first call to [sqlite3_step()] for a particular run
  3639 ** or until the same information is requested
  3640 ** again in a different encoding.
  3641 **
  3642 ** ^The names returned are the original un-aliased names of the
  3643 ** database, table, and column.
  3644 **
  3645 ** ^The first argument to these interfaces is a [prepared statement].
  3646 ** ^These functions return information about the Nth result column returned by
  3647 ** the statement, where N is the second function argument.
  3648 ** ^The left-most column is column 0 for these routines.
  3649 **
  3650 ** ^If the Nth column returned by the statement is an expression or
  3651 ** subquery and is not a column value, then all of these functions return
  3652 ** NULL.  ^These routine might also return NULL if a memory allocation error
  3653 ** occurs.  ^Otherwise, they return the name of the attached database, table,
  3654 ** or column that query result column was extracted from.
  3655 **
  3656 ** ^As with all other SQLite APIs, those whose names end with "16" return
  3657 ** UTF-16 encoded strings and the other functions return UTF-8.
  3658 **
  3659 ** ^These APIs are only available if the library was compiled with the
  3660 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
  3661 **
  3662 ** If two or more threads call one or more of these routines against the same
  3663 ** prepared statement and column at the same time then the results are
  3664 ** undefined.
  3665 **
  3666 ** If two or more threads call one or more
  3667 ** [sqlite3_column_database_name | column metadata interfaces]
  3668 ** for the same [prepared statement] and result column
  3669 ** at the same time then the results are undefined.
  3670 */
  3671 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
  3672 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
  3673 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  3674 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  3675 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  3676 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3678 /*
  3679 ** CAPI3REF: Declared Datatype Of A Query Result
  3680 **
  3681 ** ^(The first parameter is a [prepared statement].
  3682 ** If this statement is a [SELECT] statement and the Nth column of the
  3683 ** returned result set of that [SELECT] is a table column (not an
  3684 ** expression or subquery) then the declared type of the table
  3685 ** column is returned.)^  ^If the Nth column of the result set is an
  3686 ** expression or subquery, then a NULL pointer is returned.
  3687 ** ^The returned string is always UTF-8 encoded.
  3688 **
  3689 ** ^(For example, given the database schema:
  3690 **
  3691 ** CREATE TABLE t1(c1 VARIANT);
  3692 **
  3693 ** and the following statement to be compiled:
  3694 **
  3695 ** SELECT c1 + 1, c1 FROM t1;
  3696 **
  3697 ** this routine would return the string "VARIANT" for the second result
  3698 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
  3699 **
  3700 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
  3701 ** is declared to contain a particular type does not mean that the
  3702 ** data stored in that column is of the declared type.  SQLite is
  3703 ** strongly typed, but the typing is dynamic not static.  ^Type
  3704 ** is associated with individual values, not with the containers
  3705 ** used to hold those values.
  3706 */
  3707 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  3708 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  3710 /*
  3711 ** CAPI3REF: Evaluate An SQL Statement
  3712 **
  3713 ** After a [prepared statement] has been prepared using either
  3714 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3715 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3716 ** must be called one or more times to evaluate the statement.
  3717 **
  3718 ** The details of the behavior of the sqlite3_step() interface depend
  3719 ** on whether the statement was prepared using the newer "v2" interface
  3720 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
  3721 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
  3722 ** new "v2" interface is recommended for new applications but the legacy
  3723 ** interface will continue to be supported.
  3724 **
  3725 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
  3726 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
  3727 ** ^With the "v2" interface, any of the other [result codes] or
  3728 ** [extended result codes] might be returned as well.
  3729 **
  3730 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
  3731 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
  3732 ** or occurs outside of an explicit transaction, then you can retry the
  3733 ** statement.  If the statement is not a [COMMIT] and occurs within an
  3734 ** explicit transaction then you should rollback the transaction before
  3735 ** continuing.
  3736 **
  3737 ** ^[SQLITE_DONE] means that the statement has finished executing
  3738 ** successfully.  sqlite3_step() should not be called again on this virtual
  3739 ** machine without first calling [sqlite3_reset()] to reset the virtual
  3740 ** machine back to its initial state.
  3741 **
  3742 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
  3743 ** is returned each time a new row of data is ready for processing by the
  3744 ** caller. The values may be accessed using the [column access functions].
  3745 ** sqlite3_step() is called again to retrieve the next row of data.
  3746 **
  3747 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
  3748 ** violation) has occurred.  sqlite3_step() should not be called again on
  3749 ** the VM. More information may be found by calling [sqlite3_errmsg()].
  3750 ** ^With the legacy interface, a more specific error code (for example,
  3751 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
  3752 ** can be obtained by calling [sqlite3_reset()] on the
  3753 ** [prepared statement].  ^In the "v2" interface,
  3754 ** the more specific error code is returned directly by sqlite3_step().
  3755 **
  3756 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  3757 ** Perhaps it was called on a [prepared statement] that has
  3758 ** already been [sqlite3_finalize | finalized] or on one that had
  3759 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
  3760 ** be the case that the same database connection is being used by two or
  3761 ** more threads at the same moment in time.
  3762 **
  3763 ** For all versions of SQLite up to and including 3.6.23.1, a call to
  3764 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  3765 ** other than [SQLITE_ROW] before any subsequent invocation of
  3766 ** sqlite3_step().  Failure to reset the prepared statement using 
  3767 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  3768 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
  3769 ** calling [sqlite3_reset()] automatically in this circumstance rather
  3770 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
  3771 ** break because any application that ever receives an SQLITE_MISUSE error
  3772 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
  3773 ** can be used to restore the legacy behavior.
  3774 **
  3775 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  3776 ** API always returns a generic error code, [SQLITE_ERROR], following any
  3777 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
  3778 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  3779 ** specific [error codes] that better describes the error.
  3780 ** We admit that this is a goofy design.  The problem has been fixed
  3781 ** with the "v2" interface.  If you prepare all of your SQL statements
  3782 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
  3783 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
  3784 ** then the more specific [error codes] are returned directly
  3785 ** by sqlite3_step().  The use of the "v2" interface is recommended.
  3786 */
  3787 SQLITE_API int sqlite3_step(sqlite3_stmt*);
  3789 /*
  3790 ** CAPI3REF: Number of columns in a result set
  3791 **
  3792 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3793 ** current row of the result set of [prepared statement] P.
  3794 ** ^If prepared statement P does not have results ready to return
  3795 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3796 ** interfaces) then sqlite3_data_count(P) returns 0.
  3797 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
  3798 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
  3799 ** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
  3800 ** will return non-zero if previous call to [sqlite3_step](P) returned
  3801 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
  3802 ** where it always returns zero since each step of that multi-step
  3803 ** pragma returns 0 columns of data.
  3804 **
  3805 ** See also: [sqlite3_column_count()]
  3806 */
  3807 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  3809 /*
  3810 ** CAPI3REF: Fundamental Datatypes
  3811 ** KEYWORDS: SQLITE_TEXT
  3812 **
  3813 ** ^(Every value in SQLite has one of five fundamental datatypes:
  3814 **
  3815 ** <ul>
  3816 ** <li> 64-bit signed integer
  3817 ** <li> 64-bit IEEE floating point number
  3818 ** <li> string
  3819 ** <li> BLOB
  3820 ** <li> NULL
  3821 ** </ul>)^
  3822 **
  3823 ** These constants are codes for each of those types.
  3824 **
  3825 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
  3826 ** for a completely different meaning.  Software that links against both
  3827 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
  3828 ** SQLITE_TEXT.
  3829 */
  3830 #define SQLITE_INTEGER  1
  3831 #define SQLITE_FLOAT    2
  3832 #define SQLITE_BLOB     4
  3833 #define SQLITE_NULL     5
  3834 #ifdef SQLITE_TEXT
  3835 # undef SQLITE_TEXT
  3836 #else
  3837 # define SQLITE_TEXT     3
  3838 #endif
  3839 #define SQLITE3_TEXT     3
  3841 /*
  3842 ** CAPI3REF: Result Values From A Query
  3843 ** KEYWORDS: {column access functions}
  3844 **
  3845 ** These routines form the "result set" interface.
  3846 **
  3847 ** ^These routines return information about a single column of the current
  3848 ** result row of a query.  ^In every case the first argument is a pointer
  3849 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3850 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  3851 ** and the second argument is the index of the column for which information
  3852 ** should be returned. ^The leftmost column of the result set has the index 0.
  3853 ** ^The number of columns in the result can be determined using
  3854 ** [sqlite3_column_count()].
  3855 **
  3856 ** If the SQL statement does not currently point to a valid row, or if the
  3857 ** column index is out of range, the result is undefined.
  3858 ** These routines may only be called when the most recent call to
  3859 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
  3860 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
  3861 ** If any of these routines are called after [sqlite3_reset()] or
  3862 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
  3863 ** something other than [SQLITE_ROW], the results are undefined.
  3864 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
  3865 ** are called from a different thread while any of these routines
  3866 ** are pending, then the results are undefined.
  3867 **
  3868 ** ^The sqlite3_column_type() routine returns the
  3869 ** [SQLITE_INTEGER | datatype code] for the initial data type
  3870 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
  3871 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
  3872 ** returned by sqlite3_column_type() is only meaningful if no type
  3873 ** conversions have occurred as described below.  After a type conversion,
  3874 ** the value returned by sqlite3_column_type() is undefined.  Future
  3875 ** versions of SQLite may change the behavior of sqlite3_column_type()
  3876 ** following a type conversion.
  3877 **
  3878 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
  3879 ** routine returns the number of bytes in that BLOB or string.
  3880 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
  3881 ** the string to UTF-8 and then returns the number of bytes.
  3882 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
  3883 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
  3884 ** the number of bytes in that string.
  3885 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
  3886 **
  3887 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
  3888 ** routine returns the number of bytes in that BLOB or string.
  3889 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
  3890 ** the string to UTF-16 and then returns the number of bytes.
  3891 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
  3892 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
  3893 ** the number of bytes in that string.
  3894 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
  3895 **
  3896 ** ^The values returned by [sqlite3_column_bytes()] and 
  3897 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
  3898 ** of the string.  ^For clarity: the values returned by
  3899 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
  3900 ** bytes in the string, not the number of characters.
  3901 **
  3902 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  3903 ** even empty strings, are always zero-terminated.  ^The return
  3904 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
  3905 **
  3906 ** ^The object returned by [sqlite3_column_value()] is an
  3907 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
  3908 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
  3909 ** If the [unprotected sqlite3_value] object returned by
  3910 ** [sqlite3_column_value()] is used in any other way, including calls
  3911 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  3912 ** or [sqlite3_value_bytes()], then the behavior is undefined.
  3913 **
  3914 ** These routines attempt to convert the value where appropriate.  ^For
  3915 ** example, if the internal representation is FLOAT and a text result
  3916 ** is requested, [sqlite3_snprintf()] is used internally to perform the
  3917 ** conversion automatically.  ^(The following table details the conversions
  3918 ** that are applied:
  3919 **
  3920 ** <blockquote>
  3921 ** <table border="1">
  3922 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
  3923 **
  3924 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
  3925 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
  3926 ** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
  3927 ** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
  3928 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
  3929 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
  3930 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
  3931 ** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
  3932 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
  3933 ** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
  3934 ** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
  3935 ** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
  3936 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
  3937 ** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
  3938 ** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
  3939 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
  3940 ** </table>
  3941 ** </blockquote>)^
  3942 **
  3943 ** The table above makes reference to standard C library functions atoi()
  3944 ** and atof().  SQLite does not really use these functions.  It has its
  3945 ** own equivalent internal routines.  The atoi() and atof() names are
  3946 ** used in the table for brevity and because they are familiar to most
  3947 ** C programmers.
  3948 **
  3949 ** Note that when type conversions occur, pointers returned by prior
  3950 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  3951 ** sqlite3_column_text16() may be invalidated.
  3952 ** Type conversions and pointer invalidations might occur
  3953 ** in the following cases:
  3954 **
  3955 ** <ul>
  3956 ** <li> The initial content is a BLOB and sqlite3_column_text() or
  3957 **      sqlite3_column_text16() is called.  A zero-terminator might
  3958 **      need to be added to the string.</li>
  3959 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
  3960 **      sqlite3_column_text16() is called.  The content must be converted
  3961 **      to UTF-16.</li>
  3962 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
  3963 **      sqlite3_column_text() is called.  The content must be converted
  3964 **      to UTF-8.</li>
  3965 ** </ul>
  3966 **
  3967 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  3968 ** not invalidate a prior pointer, though of course the content of the buffer
  3969 ** that the prior pointer references will have been modified.  Other kinds
  3970 ** of conversion are done in place when it is possible, but sometimes they
  3971 ** are not possible and in those cases prior pointers are invalidated.
  3972 **
  3973 ** The safest and easiest to remember policy is to invoke these routines
  3974 ** in one of the following ways:
  3975 **
  3976 ** <ul>
  3977 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  3978 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  3979 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  3980 ** </ul>
  3981 **
  3982 ** In other words, you should call sqlite3_column_text(),
  3983 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
  3984 ** into the desired format, then invoke sqlite3_column_bytes() or
  3985 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
  3986 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  3987 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  3988 ** with calls to sqlite3_column_bytes().
  3989 **
  3990 ** ^The pointers returned are valid until a type conversion occurs as
  3991 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  3992 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
  3993 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
  3994 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  3995 ** [sqlite3_free()].
  3996 **
  3997 ** ^(If a memory allocation error occurs during the evaluation of any
  3998 ** of these routines, a default value is returned.  The default value
  3999 ** is either the integer 0, the floating point number 0.0, or a NULL
  4000 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
  4001 ** [SQLITE_NOMEM].)^
  4002 */
  4003 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  4004 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  4005 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
  4006 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
  4007 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
  4008 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  4009 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  4010 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4011 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
  4012 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  4014 /*
  4015 ** CAPI3REF: Destroy A Prepared Statement Object
  4016 **
  4017 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4018 ** ^If the most recent evaluation of the statement encountered no errors
  4019 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4020 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
  4021 ** sqlite3_finalize(S) returns the appropriate [error code] or
  4022 ** [extended error code].
  4023 **
  4024 ** ^The sqlite3_finalize(S) routine can be called at any point during
  4025 ** the life cycle of [prepared statement] S:
  4026 ** before statement S is ever evaluated, after
  4027 ** one or more calls to [sqlite3_reset()], or after any call
  4028 ** to [sqlite3_step()] regardless of whether or not the statement has
  4029 ** completed execution.
  4030 **
  4031 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
  4032 **
  4033 ** The application must finalize every [prepared statement] in order to avoid
  4034 ** resource leaks.  It is a grievous error for the application to try to use
  4035 ** a prepared statement after it has been finalized.  Any use of a prepared
  4036 ** statement after it has been finalized can result in undefined and
  4037 ** undesirable behavior such as segfaults and heap corruption.
  4038 */
  4039 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
  4041 /*
  4042 ** CAPI3REF: Reset A Prepared Statement Object
  4043 **
  4044 ** The sqlite3_reset() function is called to reset a [prepared statement]
  4045 ** object back to its initial state, ready to be re-executed.
  4046 ** ^Any SQL statement variables that had values bound to them using
  4047 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4048 ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4049 **
  4050 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
  4051 ** back to the beginning of its program.
  4052 **
  4053 ** ^If the most recent call to [sqlite3_step(S)] for the
  4054 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
  4055 ** or if [sqlite3_step(S)] has never before been called on S,
  4056 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
  4057 **
  4058 ** ^If the most recent call to [sqlite3_step(S)] for the
  4059 ** [prepared statement] S indicated an error, then
  4060 ** [sqlite3_reset(S)] returns an appropriate [error code].
  4061 **
  4062 ** ^The [sqlite3_reset(S)] interface does not change the values
  4063 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
  4064 */
  4065 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
  4067 /*
  4068 ** CAPI3REF: Create Or Redefine SQL Functions
  4069 ** KEYWORDS: {function creation routines}
  4070 ** KEYWORDS: {application-defined SQL function}
  4071 ** KEYWORDS: {application-defined SQL functions}
  4072 **
  4073 ** ^These functions (collectively known as "function creation routines")
  4074 ** are used to add SQL functions or aggregates or to redefine the behavior
  4075 ** of existing SQL functions or aggregates.  The only differences between
  4076 ** these routines are the text encoding expected for
  4077 ** the second parameter (the name of the function being created)
  4078 ** and the presence or absence of a destructor callback for
  4079 ** the application data pointer.
  4080 **
  4081 ** ^The first parameter is the [database connection] to which the SQL
  4082 ** function is to be added.  ^If an application uses more than one database
  4083 ** connection then application-defined SQL functions must be added
  4084 ** to each database connection separately.
  4085 **
  4086 ** ^The second parameter is the name of the SQL function to be created or
  4087 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
  4088 ** representation, exclusive of the zero-terminator.  ^Note that the name
  4089 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.  
  4090 ** ^Any attempt to create a function with a longer name
  4091 ** will result in [SQLITE_MISUSE] being returned.
  4092 **
  4093 ** ^The third parameter (nArg)
  4094 ** is the number of arguments that the SQL function or
  4095 ** aggregate takes. ^If this parameter is -1, then the SQL function or
  4096 ** aggregate may take any number of arguments between 0 and the limit
  4097 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
  4098 ** parameter is less than -1 or greater than 127 then the behavior is
  4099 ** undefined.
  4100 **
  4101 ** ^The fourth parameter, eTextRep, specifies what
  4102 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  4103 ** its parameters.  The application should set this parameter to
  4104 ** [SQLITE_UTF16LE] if the function implementation invokes 
  4105 ** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
  4106 ** implementation invokes [sqlite3_value_text16be()] on an input, or
  4107 ** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
  4108 ** otherwise.  ^The same SQL function may be registered multiple times using
  4109 ** different preferred text encodings, with different implementations for
  4110 ** each encoding.
  4111 ** ^When multiple implementations of the same function are available, SQLite
  4112 ** will pick the one that involves the least amount of data conversion.
  4113 **
  4114 ** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
  4115 ** to signal that the function will always return the same result given
  4116 ** the same inputs within a single SQL statement.  Most SQL functions are
  4117 ** deterministic.  The built-in [random()] SQL function is an example of a
  4118 ** function that is not deterministic.  The SQLite query planner is able to
  4119 ** perform additional optimizations on deterministic functions, so use
  4120 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
  4121 **
  4122 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  4123 ** function can gain access to this pointer using [sqlite3_user_data()].)^
  4124 **
  4125 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  4126 ** pointers to C-language functions that implement the SQL function or
  4127 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  4128 ** callback only; NULL pointers must be passed as the xStep and xFinal
  4129 ** parameters. ^An aggregate SQL function requires an implementation of xStep
  4130 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  4131 ** SQL function or aggregate, pass NULL pointers for all three function
  4132 ** callbacks.
  4133 **
  4134 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  4135 ** then it is destructor for the application data pointer. 
  4136 ** The destructor is invoked when the function is deleted, either by being
  4137 ** overloaded or when the database connection closes.)^
  4138 ** ^The destructor is also invoked if the call to
  4139 ** sqlite3_create_function_v2() fails.
  4140 ** ^When the destructor callback of the tenth parameter is invoked, it
  4141 ** is passed a single argument which is a copy of the application data 
  4142 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
  4143 **
  4144 ** ^It is permitted to register multiple implementations of the same
  4145 ** functions with the same name but with either differing numbers of
  4146 ** arguments or differing preferred text encodings.  ^SQLite will use
  4147 ** the implementation that most closely matches the way in which the
  4148 ** SQL function is used.  ^A function implementation with a non-negative
  4149 ** nArg parameter is a better match than a function implementation with
  4150 ** a negative nArg.  ^A function where the preferred text encoding
  4151 ** matches the database encoding is a better
  4152 ** match than a function where the encoding is different.  
  4153 ** ^A function where the encoding difference is between UTF16le and UTF16be
  4154 ** is a closer match than a function where the encoding difference is
  4155 ** between UTF8 and UTF16.
  4156 **
  4157 ** ^Built-in functions may be overloaded by new application-defined functions.
  4158 **
  4159 ** ^An application-defined function is permitted to call other
  4160 ** SQLite interfaces.  However, such calls must not
  4161 ** close the database connection nor finalize or reset the prepared
  4162 ** statement in which the function is running.
  4163 */
  4164 SQLITE_API int sqlite3_create_function(
  4165   sqlite3 *db,
  4166   const char *zFunctionName,
  4167   int nArg,
  4168   int eTextRep,
  4169   void *pApp,
  4170   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4171   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4172   void (*xFinal)(sqlite3_context*)
  4173 );
  4174 SQLITE_API int sqlite3_create_function16(
  4175   sqlite3 *db,
  4176   const void *zFunctionName,
  4177   int nArg,
  4178   int eTextRep,
  4179   void *pApp,
  4180   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4181   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4182   void (*xFinal)(sqlite3_context*)
  4183 );
  4184 SQLITE_API int sqlite3_create_function_v2(
  4185   sqlite3 *db,
  4186   const char *zFunctionName,
  4187   int nArg,
  4188   int eTextRep,
  4189   void *pApp,
  4190   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4191   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4192   void (*xFinal)(sqlite3_context*),
  4193   void(*xDestroy)(void*)
  4194 );
  4196 /*
  4197 ** CAPI3REF: Text Encodings
  4198 **
  4199 ** These constant define integer codes that represent the various
  4200 ** text encodings supported by SQLite.
  4201 */
  4202 #define SQLITE_UTF8           1
  4203 #define SQLITE_UTF16LE        2
  4204 #define SQLITE_UTF16BE        3
  4205 #define SQLITE_UTF16          4    /* Use native byte order */
  4206 #define SQLITE_ANY            5    /* Deprecated */
  4207 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
  4209 /*
  4210 ** CAPI3REF: Function Flags
  4211 **
  4212 ** These constants may be ORed together with the 
  4213 ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
  4214 ** to [sqlite3_create_function()], [sqlite3_create_function16()], or
  4215 ** [sqlite3_create_function_v2()].
  4216 */
  4217 #define SQLITE_DETERMINISTIC    0x800
  4219 /*
  4220 ** CAPI3REF: Deprecated Functions
  4221 ** DEPRECATED
  4222 **
  4223 ** These functions are [deprecated].  In order to maintain
  4224 ** backwards compatibility with older code, these functions continue 
  4225 ** to be supported.  However, new applications should avoid
  4226 ** the use of these functions.  To help encourage people to avoid
  4227 ** using these functions, we are not going to tell you what they do.
  4228 */
  4229 #ifndef SQLITE_OMIT_DEPRECATED
  4230 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
  4231 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
  4232 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
  4233 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
  4234 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  4235 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
  4236                       void*,sqlite3_int64);
  4237 #endif
  4239 /*
  4240 ** CAPI3REF: Obtaining SQL Function Parameter Values
  4241 **
  4242 ** The C-language implementation of SQL functions and aggregates uses
  4243 ** this set of interface routines to access the parameter values on
  4244 ** the function or aggregate.
  4245 **
  4246 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4247 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  4248 ** define callbacks that implement the SQL functions and aggregates.
  4249 ** The 3rd parameter to these callbacks is an array of pointers to
  4250 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
  4251 ** each parameter to the SQL function.  These routines are used to
  4252 ** extract values from the [sqlite3_value] objects.
  4253 **
  4254 ** These routines work only with [protected sqlite3_value] objects.
  4255 ** Any attempt to use these routines on an [unprotected sqlite3_value]
  4256 ** object results in undefined behavior.
  4257 **
  4258 ** ^These routines work just like the corresponding [column access functions]
  4259 ** except that  these routines take a single [protected sqlite3_value] object
  4260 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  4261 **
  4262 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  4263 ** in the native byte-order of the host machine.  ^The
  4264 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  4265 ** extract UTF-16 strings as big-endian and little-endian respectively.
  4266 **
  4267 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
  4268 ** numeric affinity to the value.  This means that an attempt is
  4269 ** made to convert the value to an integer or floating point.  If
  4270 ** such a conversion is possible without loss of information (in other
  4271 ** words, if the value is a string that looks like a number)
  4272 ** then the conversion is performed.  Otherwise no conversion occurs.
  4273 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
  4274 **
  4275 ** Please pay particular attention to the fact that the pointer returned
  4276 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  4277 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  4278 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  4279 ** or [sqlite3_value_text16()].
  4280 **
  4281 ** These routines must be called from the same thread as
  4282 ** the SQL function that supplied the [sqlite3_value*] parameters.
  4283 */
  4284 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
  4285 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  4286 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  4287 SQLITE_API double sqlite3_value_double(sqlite3_value*);
  4288 SQLITE_API int sqlite3_value_int(sqlite3_value*);
  4289 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
  4290 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
  4291 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  4292 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  4293 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  4294 SQLITE_API int sqlite3_value_type(sqlite3_value*);
  4295 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  4297 /*
  4298 ** CAPI3REF: Obtain Aggregate Function Context
  4299 **
  4300 ** Implementations of aggregate SQL functions use this
  4301 ** routine to allocate memory for storing their state.
  4302 **
  4303 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
  4304 ** for a particular aggregate function, SQLite
  4305 ** allocates N of memory, zeroes out that memory, and returns a pointer
  4306 ** to the new memory. ^On second and subsequent calls to
  4307 ** sqlite3_aggregate_context() for the same aggregate function instance,
  4308 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
  4309 ** called once for each invocation of the xStep callback and then one
  4310 ** last time when the xFinal callback is invoked.  ^(When no rows match
  4311 ** an aggregate query, the xStep() callback of the aggregate function
  4312 ** implementation is never called and xFinal() is called exactly once.
  4313 ** In those cases, sqlite3_aggregate_context() might be called for the
  4314 ** first time from within xFinal().)^
  4315 **
  4316 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer 
  4317 ** when first called if N is less than or equal to zero or if a memory
  4318 ** allocate error occurs.
  4319 **
  4320 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
  4321 ** determined by the N parameter on first successful call.  Changing the
  4322 ** value of N in subsequent call to sqlite3_aggregate_context() within
  4323 ** the same aggregate function instance will not resize the memory
  4324 ** allocation.)^  Within the xFinal callback, it is customary to set
  4325 ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no 
  4326 ** pointless memory allocations occur.
  4327 **
  4328 ** ^SQLite automatically frees the memory allocated by 
  4329 ** sqlite3_aggregate_context() when the aggregate query concludes.
  4330 **
  4331 ** The first parameter must be a copy of the
  4332 ** [sqlite3_context | SQL function context] that is the first parameter
  4333 ** to the xStep or xFinal callback routine that implements the aggregate
  4334 ** function.
  4335 **
  4336 ** This routine must be called from the same thread in which
  4337 ** the aggregate SQL function is running.
  4338 */
  4339 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4341 /*
  4342 ** CAPI3REF: User Data For Functions
  4343 **
  4344 ** ^The sqlite3_user_data() interface returns a copy of
  4345 ** the pointer that was the pUserData parameter (the 5th parameter)
  4346 ** of the [sqlite3_create_function()]
  4347 ** and [sqlite3_create_function16()] routines that originally
  4348 ** registered the application defined function.
  4349 **
  4350 ** This routine must be called from the same thread in which
  4351 ** the application-defined function is running.
  4352 */
  4353 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
  4355 /*
  4356 ** CAPI3REF: Database Connection For Functions
  4357 **
  4358 ** ^The sqlite3_context_db_handle() interface returns a copy of
  4359 ** the pointer to the [database connection] (the 1st parameter)
  4360 ** of the [sqlite3_create_function()]
  4361 ** and [sqlite3_create_function16()] routines that originally
  4362 ** registered the application defined function.
  4363 */
  4364 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  4366 /*
  4367 ** CAPI3REF: Function Auxiliary Data
  4368 **
  4369 ** These functions may be used by (non-aggregate) SQL functions to
  4370 ** associate metadata with argument values. If the same value is passed to
  4371 ** multiple invocations of the same SQL function during query execution, under
  4372 ** some circumstances the associated metadata may be preserved.  An example
  4373 ** of where this might be useful is in a regular-expression matching
  4374 ** function. The compiled version of the regular expression can be stored as
  4375 ** metadata associated with the pattern string.  
  4376 ** Then as long as the pattern string remains the same,
  4377 ** the compiled regular expression can be reused on multiple
  4378 ** invocations of the same function.
  4379 **
  4380 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  4381 ** associated by the sqlite3_set_auxdata() function with the Nth argument
  4382 ** value to the application-defined function. ^If there is no metadata
  4383 ** associated with the function argument, this sqlite3_get_auxdata() interface
  4384 ** returns a NULL pointer.
  4385 **
  4386 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
  4387 ** argument of the application-defined function.  ^Subsequent
  4388 ** calls to sqlite3_get_auxdata(C,N) return P from the most recent
  4389 ** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
  4390 ** NULL if the metadata has been discarded.
  4391 ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
  4392 ** SQLite will invoke the destructor function X with parameter P exactly
  4393 ** once, when the metadata is discarded.
  4394 ** SQLite is free to discard the metadata at any time, including: <ul>
  4395 ** <li> when the corresponding function parameter changes, or
  4396 ** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
  4397 **      SQL statement, or
  4398 ** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
  4399 ** <li> during the original sqlite3_set_auxdata() call when a memory 
  4400 **      allocation error occurs. </ul>)^
  4401 **
  4402 ** Note the last bullet in particular.  The destructor X in 
  4403 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
  4404 ** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
  4405 ** should be called near the end of the function implementation and the
  4406 ** function implementation should not make any use of P after
  4407 ** sqlite3_set_auxdata() has been called.
  4408 **
  4409 ** ^(In practice, metadata is preserved between function calls for
  4410 ** function parameters that are compile-time constants, including literal
  4411 ** values and [parameters] and expressions composed from the same.)^
  4412 **
  4413 ** These routines must be called from the same thread in which
  4414 ** the SQL function is running.
  4415 */
  4416 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
  4417 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  4420 /*
  4421 ** CAPI3REF: Constants Defining Special Destructor Behavior
  4422 **
  4423 ** These are special values for the destructor that is passed in as the
  4424 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
  4425 ** argument is SQLITE_STATIC, it means that the content pointer is constant
  4426 ** and will never change.  It does not need to be destroyed.  ^The
  4427 ** SQLITE_TRANSIENT value means that the content will likely change in
  4428 ** the near future and that SQLite should make its own private copy of
  4429 ** the content before returning.
  4430 **
  4431 ** The typedef is necessary to work around problems in certain
  4432 ** C++ compilers.
  4433 */
  4434 typedef void (*sqlite3_destructor_type)(void*);
  4435 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
  4436 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
  4438 /*
  4439 ** CAPI3REF: Setting The Result Of An SQL Function
  4440 **
  4441 ** These routines are used by the xFunc or xFinal callbacks that
  4442 ** implement SQL functions and aggregates.  See
  4443 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4444 ** for additional information.
  4445 **
  4446 ** These functions work very much like the [parameter binding] family of
  4447 ** functions used to bind values to host parameters in prepared statements.
  4448 ** Refer to the [SQL parameter] documentation for additional information.
  4449 **
  4450 ** ^The sqlite3_result_blob() interface sets the result from
  4451 ** an application-defined function to be the BLOB whose content is pointed
  4452 ** to by the second parameter and which is N bytes long where N is the
  4453 ** third parameter.
  4454 **
  4455 ** ^The sqlite3_result_zeroblob() interfaces set the result of
  4456 ** the application-defined function to be a BLOB containing all zero
  4457 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
  4458 **
  4459 ** ^The sqlite3_result_double() interface sets the result from
  4460 ** an application-defined function to be a floating point value specified
  4461 ** by its 2nd argument.
  4462 **
  4463 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
  4464 ** cause the implemented SQL function to throw an exception.
  4465 ** ^SQLite uses the string pointed to by the
  4466 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
  4467 ** as the text of an error message.  ^SQLite interprets the error
  4468 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
  4469 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
  4470 ** byte order.  ^If the third parameter to sqlite3_result_error()
  4471 ** or sqlite3_result_error16() is negative then SQLite takes as the error
  4472 ** message all text up through the first zero character.
  4473 ** ^If the third parameter to sqlite3_result_error() or
  4474 ** sqlite3_result_error16() is non-negative then SQLite takes that many
  4475 ** bytes (not characters) from the 2nd parameter as the error message.
  4476 ** ^The sqlite3_result_error() and sqlite3_result_error16()
  4477 ** routines make a private copy of the error message text before
  4478 ** they return.  Hence, the calling function can deallocate or
  4479 ** modify the text after they return without harm.
  4480 ** ^The sqlite3_result_error_code() function changes the error code
  4481 ** returned by SQLite as a result of an error in a function.  ^By default,
  4482 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
  4483 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4484 **
  4485 ** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
  4486 ** error indicating that a string or BLOB is too long to represent.
  4487 **
  4488 ** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
  4489 ** error indicating that a memory allocation failed.
  4490 **
  4491 ** ^The sqlite3_result_int() interface sets the return value
  4492 ** of the application-defined function to be the 32-bit signed integer
  4493 ** value given in the 2nd argument.
  4494 ** ^The sqlite3_result_int64() interface sets the return value
  4495 ** of the application-defined function to be the 64-bit signed integer
  4496 ** value given in the 2nd argument.
  4497 **
  4498 ** ^The sqlite3_result_null() interface sets the return value
  4499 ** of the application-defined function to be NULL.
  4500 **
  4501 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4502 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  4503 ** set the return value of the application-defined function to be
  4504 ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4505 ** UTF-16 little endian, or UTF-16 big endian, respectively.
  4506 ** ^SQLite takes the text result from the application from
  4507 ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4508 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4509 ** is negative, then SQLite takes result text from the 2nd parameter
  4510 ** through the first zero character.
  4511 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4512 ** is non-negative, then as many bytes (not characters) of the text
  4513 ** pointed to by the 2nd parameter are taken as the application-defined
  4514 ** function result.  If the 3rd parameter is non-negative, then it
  4515 ** must be the byte offset into the string where the NUL terminator would
  4516 ** appear if the string where NUL terminated.  If any NUL characters occur
  4517 ** in the string at a byte offset that is less than the value of the 3rd
  4518 ** parameter, then the resulting string will contain embedded NULs and the
  4519 ** result of expressions operating on strings with embedded NULs is undefined.
  4520 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4521 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  4522 ** function as the destructor on the text or BLOB result when it has
  4523 ** finished using that result.
  4524 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  4525 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  4526 ** assumes that the text or BLOB result is in constant space and does not
  4527 ** copy the content of the parameter nor call a destructor on the content
  4528 ** when it has finished using that result.
  4529 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4530 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
  4531 ** then SQLite makes a copy of the result into space obtained from
  4532 ** from [sqlite3_malloc()] before it returns.
  4533 **
  4534 ** ^The sqlite3_result_value() interface sets the result of
  4535 ** the application-defined function to be a copy the
  4536 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
  4537 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  4538 ** so that the [sqlite3_value] specified in the parameter may change or
  4539 ** be deallocated after sqlite3_result_value() returns without harm.
  4540 ** ^A [protected sqlite3_value] object may always be used where an
  4541 ** [unprotected sqlite3_value] object is required, so either
  4542 ** kind of [sqlite3_value] object can be used with this interface.
  4543 **
  4544 ** If these routines are called from within the different thread
  4545 ** than the one containing the application-defined function that received
  4546 ** the [sqlite3_context] pointer, the results are undefined.
  4547 */
  4548 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  4549 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
  4550 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
  4551 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4552 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
  4553 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
  4554 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
  4555 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
  4556 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  4557 SQLITE_API void sqlite3_result_null(sqlite3_context*);
  4558 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  4559 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  4560 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4561 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4562 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4563 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4565 /*
  4566 ** CAPI3REF: Define New Collating Sequences
  4567 **
  4568 ** ^These functions add, remove, or modify a [collation] associated
  4569 ** with the [database connection] specified as the first argument.
  4570 **
  4571 ** ^The name of the collation is a UTF-8 string
  4572 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4573 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
  4574 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
  4575 ** considered to be the same name.
  4576 **
  4577 ** ^(The third argument (eTextRep) must be one of the constants:
  4578 ** <ul>
  4579 ** <li> [SQLITE_UTF8],
  4580 ** <li> [SQLITE_UTF16LE],
  4581 ** <li> [SQLITE_UTF16BE],
  4582 ** <li> [SQLITE_UTF16], or
  4583 ** <li> [SQLITE_UTF16_ALIGNED].
  4584 ** </ul>)^
  4585 ** ^The eTextRep argument determines the encoding of strings passed
  4586 ** to the collating function callback, xCallback.
  4587 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
  4588 ** force strings to be UTF16 with native byte order.
  4589 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
  4590 ** on an even byte address.
  4591 **
  4592 ** ^The fourth argument, pArg, is an application data pointer that is passed
  4593 ** through as the first argument to the collating function callback.
  4594 **
  4595 ** ^The fifth argument, xCallback, is a pointer to the collating function.
  4596 ** ^Multiple collating functions can be registered using the same name but
  4597 ** with different eTextRep parameters and SQLite will use whichever
  4598 ** function requires the least amount of data transformation.
  4599 ** ^If the xCallback argument is NULL then the collating function is
  4600 ** deleted.  ^When all collating functions having the same name are deleted,
  4601 ** that collation is no longer usable.
  4602 **
  4603 ** ^The collating function callback is invoked with a copy of the pArg 
  4604 ** application data pointer and with two strings in the encoding specified
  4605 ** by the eTextRep argument.  The collating function must return an
  4606 ** integer that is negative, zero, or positive
  4607 ** if the first string is less than, equal to, or greater than the second,
  4608 ** respectively.  A collating function must always return the same answer
  4609 ** given the same inputs.  If two or more collating functions are registered
  4610 ** to the same collation name (using different eTextRep values) then all
  4611 ** must give an equivalent answer when invoked with equivalent strings.
  4612 ** The collating function must obey the following properties for all
  4613 ** strings A, B, and C:
  4614 **
  4615 ** <ol>
  4616 ** <li> If A==B then B==A.
  4617 ** <li> If A==B and B==C then A==C.
  4618 ** <li> If A&lt;B THEN B&gt;A.
  4619 ** <li> If A&lt;B and B&lt;C then A&lt;C.
  4620 ** </ol>
  4621 **
  4622 ** If a collating function fails any of the above constraints and that
  4623 ** collating function is  registered and used, then the behavior of SQLite
  4624 ** is undefined.
  4625 **
  4626 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
  4627 ** with the addition that the xDestroy callback is invoked on pArg when
  4628 ** the collating function is deleted.
  4629 ** ^Collating functions are deleted when they are overridden by later
  4630 ** calls to the collation creation functions or when the
  4631 ** [database connection] is closed using [sqlite3_close()].
  4632 **
  4633 ** ^The xDestroy callback is <u>not</u> called if the 
  4634 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
  4635 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 
  4636 ** check the return code and dispose of the application data pointer
  4637 ** themselves rather than expecting SQLite to deal with it for them.
  4638 ** This is different from every other SQLite interface.  The inconsistency 
  4639 ** is unfortunate but cannot be changed without breaking backwards 
  4640 ** compatibility.
  4641 **
  4642 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
  4643 */
  4644 SQLITE_API int sqlite3_create_collation(
  4645   sqlite3*, 
  4646   const char *zName, 
  4647   int eTextRep, 
  4648   void *pArg,
  4649   int(*xCompare)(void*,int,const void*,int,const void*)
  4650 );
  4651 SQLITE_API int sqlite3_create_collation_v2(
  4652   sqlite3*, 
  4653   const char *zName, 
  4654   int eTextRep, 
  4655   void *pArg,
  4656   int(*xCompare)(void*,int,const void*,int,const void*),
  4657   void(*xDestroy)(void*)
  4658 );
  4659 SQLITE_API int sqlite3_create_collation16(
  4660   sqlite3*, 
  4661   const void *zName,
  4662   int eTextRep, 
  4663   void *pArg,
  4664   int(*xCompare)(void*,int,const void*,int,const void*)
  4665 );
  4667 /*
  4668 ** CAPI3REF: Collation Needed Callbacks
  4669 **
  4670 ** ^To avoid having to register all collation sequences before a database
  4671 ** can be used, a single callback function may be registered with the
  4672 ** [database connection] to be invoked whenever an undefined collation
  4673 ** sequence is required.
  4674 **
  4675 ** ^If the function is registered using the sqlite3_collation_needed() API,
  4676 ** then it is passed the names of undefined collation sequences as strings
  4677 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
  4678 ** the names are passed as UTF-16 in machine native byte order.
  4679 ** ^A call to either function replaces the existing collation-needed callback.
  4680 **
  4681 ** ^(When the callback is invoked, the first argument passed is a copy
  4682 ** of the second argument to sqlite3_collation_needed() or
  4683 ** sqlite3_collation_needed16().  The second argument is the database
  4684 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
  4685 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
  4686 ** sequence function required.  The fourth parameter is the name of the
  4687 ** required collation sequence.)^
  4688 **
  4689 ** The callback function should register the desired collation using
  4690 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
  4691 ** [sqlite3_create_collation_v2()].
  4692 */
  4693 SQLITE_API int sqlite3_collation_needed(
  4694   sqlite3*, 
  4695   void*, 
  4696   void(*)(void*,sqlite3*,int eTextRep,const char*)
  4697 );
  4698 SQLITE_API int sqlite3_collation_needed16(
  4699   sqlite3*, 
  4700   void*,
  4701   void(*)(void*,sqlite3*,int eTextRep,const void*)
  4702 );
  4704 #ifdef SQLITE_HAS_CODEC
  4705 /*
  4706 ** Specify the key for an encrypted database.  This routine should be
  4707 ** called right after sqlite3_open().
  4708 **
  4709 ** The code to implement this API is not available in the public release
  4710 ** of SQLite.
  4711 */
  4712 SQLITE_API int sqlite3_key(
  4713   sqlite3 *db,                   /* Database to be rekeyed */
  4714   const void *pKey, int nKey     /* The key */
  4715 );
  4716 SQLITE_API int sqlite3_key_v2(
  4717   sqlite3 *db,                   /* Database to be rekeyed */
  4718   const char *zDbName,           /* Name of the database */
  4719   const void *pKey, int nKey     /* The key */
  4720 );
  4722 /*
  4723 ** Change the key on an open database.  If the current database is not
  4724 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
  4725 ** database is decrypted.
  4726 **
  4727 ** The code to implement this API is not available in the public release
  4728 ** of SQLite.
  4729 */
  4730 SQLITE_API int sqlite3_rekey(
  4731   sqlite3 *db,                   /* Database to be rekeyed */
  4732   const void *pKey, int nKey     /* The new key */
  4733 );
  4734 SQLITE_API int sqlite3_rekey_v2(
  4735   sqlite3 *db,                   /* Database to be rekeyed */
  4736   const char *zDbName,           /* Name of the database */
  4737   const void *pKey, int nKey     /* The new key */
  4738 );
  4740 /*
  4741 ** Specify the activation key for a SEE database.  Unless 
  4742 ** activated, none of the SEE routines will work.
  4743 */
  4744 SQLITE_API void sqlite3_activate_see(
  4745   const char *zPassPhrase        /* Activation phrase */
  4746 );
  4747 #endif
  4749 #ifdef SQLITE_ENABLE_CEROD
  4750 /*
  4751 ** Specify the activation key for a CEROD database.  Unless 
  4752 ** activated, none of the CEROD routines will work.
  4753 */
  4754 SQLITE_API void sqlite3_activate_cerod(
  4755   const char *zPassPhrase        /* Activation phrase */
  4756 );
  4757 #endif
  4759 /*
  4760 ** CAPI3REF: Suspend Execution For A Short Time
  4761 **
  4762 ** The sqlite3_sleep() function causes the current thread to suspend execution
  4763 ** for at least a number of milliseconds specified in its parameter.
  4764 **
  4765 ** If the operating system does not support sleep requests with
  4766 ** millisecond time resolution, then the time will be rounded up to
  4767 ** the nearest second. The number of milliseconds of sleep actually
  4768 ** requested from the operating system is returned.
  4769 **
  4770 ** ^SQLite implements this interface by calling the xSleep()
  4771 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
  4772 ** of the default VFS is not implemented correctly, or not implemented at
  4773 ** all, then the behavior of sqlite3_sleep() may deviate from the description
  4774 ** in the previous paragraphs.
  4775 */
  4776 SQLITE_API int sqlite3_sleep(int);
  4778 /*
  4779 ** CAPI3REF: Name Of The Folder Holding Temporary Files
  4780 **
  4781 ** ^(If this global variable is made to point to a string which is
  4782 ** the name of a folder (a.k.a. directory), then all temporary files
  4783 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  4784 ** will be placed in that directory.)^  ^If this variable
  4785 ** is a NULL pointer, then SQLite performs a search for an appropriate
  4786 ** temporary file directory.
  4787 **
  4788 ** It is not safe to read or modify this variable in more than one
  4789 ** thread at a time.  It is not safe to read or modify this variable
  4790 ** if a [database connection] is being used at the same time in a separate
  4791 ** thread.
  4792 ** It is intended that this variable be set once
  4793 ** as part of process initialization and before any SQLite interface
  4794 ** routines have been called and that this variable remain unchanged
  4795 ** thereafter.
  4796 **
  4797 ** ^The [temp_store_directory pragma] may modify this variable and cause
  4798 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  4799 ** the [temp_store_directory pragma] always assumes that any string
  4800 ** that this variable points to is held in memory obtained from 
  4801 ** [sqlite3_malloc] and the pragma may attempt to free that memory
  4802 ** using [sqlite3_free].
  4803 ** Hence, if this variable is modified directly, either it should be
  4804 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  4805 ** or else the use of the [temp_store_directory pragma] should be avoided.
  4806 **
  4807 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
  4808 ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
  4809 ** features that require the use of temporary files may fail.  Here is an
  4810 ** example of how to do this using C++ with the Windows Runtime:
  4811 **
  4812 ** <blockquote><pre>
  4813 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
  4814 ** &nbsp;     TemporaryFolder->Path->Data();
  4815 ** char zPathBuf&#91;MAX_PATH + 1&#93;;
  4816 ** memset(zPathBuf, 0, sizeof(zPathBuf));
  4817 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
  4818 ** &nbsp;     NULL, NULL);
  4819 ** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
  4820 ** </pre></blockquote>
  4821 */
  4822 SQLITE_API char *sqlite3_temp_directory;
  4824 /*
  4825 ** CAPI3REF: Name Of The Folder Holding Database Files
  4826 **
  4827 ** ^(If this global variable is made to point to a string which is
  4828 ** the name of a folder (a.k.a. directory), then all database files
  4829 ** specified with a relative pathname and created or accessed by
  4830 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
  4831 ** to be relative to that directory.)^ ^If this variable is a NULL
  4832 ** pointer, then SQLite assumes that all database files specified
  4833 ** with a relative pathname are relative to the current directory
  4834 ** for the process.  Only the windows VFS makes use of this global
  4835 ** variable; it is ignored by the unix VFS.
  4836 **
  4837 ** Changing the value of this variable while a database connection is
  4838 ** open can result in a corrupt database.
  4839 **
  4840 ** It is not safe to read or modify this variable in more than one
  4841 ** thread at a time.  It is not safe to read or modify this variable
  4842 ** if a [database connection] is being used at the same time in a separate
  4843 ** thread.
  4844 ** It is intended that this variable be set once
  4845 ** as part of process initialization and before any SQLite interface
  4846 ** routines have been called and that this variable remain unchanged
  4847 ** thereafter.
  4848 **
  4849 ** ^The [data_store_directory pragma] may modify this variable and cause
  4850 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
  4851 ** the [data_store_directory pragma] always assumes that any string
  4852 ** that this variable points to is held in memory obtained from 
  4853 ** [sqlite3_malloc] and the pragma may attempt to free that memory
  4854 ** using [sqlite3_free].
  4855 ** Hence, if this variable is modified directly, either it should be
  4856 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  4857 ** or else the use of the [data_store_directory pragma] should be avoided.
  4858 */
  4859 SQLITE_API char *sqlite3_data_directory;
  4861 /*
  4862 ** CAPI3REF: Test For Auto-Commit Mode
  4863 ** KEYWORDS: {autocommit mode}
  4864 **
  4865 ** ^The sqlite3_get_autocommit() interface returns non-zero or
  4866 ** zero if the given database connection is or is not in autocommit mode,
  4867 ** respectively.  ^Autocommit mode is on by default.
  4868 ** ^Autocommit mode is disabled by a [BEGIN] statement.
  4869 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  4870 **
  4871 ** If certain kinds of errors occur on a statement within a multi-statement
  4872 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
  4873 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
  4874 ** transaction might be rolled back automatically.  The only way to
  4875 ** find out whether SQLite automatically rolled back the transaction after
  4876 ** an error is to use this function.
  4877 **
  4878 ** If another thread changes the autocommit status of the database
  4879 ** connection while this routine is running, then the return value
  4880 ** is undefined.
  4881 */
  4882 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
  4884 /*
  4885 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
  4886 **
  4887 ** ^The sqlite3_db_handle interface returns the [database connection] handle
  4888 ** to which a [prepared statement] belongs.  ^The [database connection]
  4889 ** returned by sqlite3_db_handle is the same [database connection]
  4890 ** that was the first argument
  4891 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4892 ** create the statement in the first place.
  4893 */
  4894 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  4896 /*
  4897 ** CAPI3REF: Return The Filename For A Database Connection
  4898 **
  4899 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  4900 ** associated with database N of connection D.  ^The main database file
  4901 ** has the name "main".  If there is no attached database N on the database
  4902 ** connection D, or if database N is a temporary or in-memory database, then
  4903 ** a NULL pointer is returned.
  4904 **
  4905 ** ^The filename returned by this function is the output of the
  4906 ** xFullPathname method of the [VFS].  ^In other words, the filename
  4907 ** will be an absolute pathname, even if the filename used
  4908 ** to open the database originally was a URI or relative pathname.
  4909 */
  4910 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  4912 /*
  4913 ** CAPI3REF: Determine if a database is read-only
  4914 **
  4915 ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
  4916 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
  4917 ** the name of a database on connection D.
  4918 */
  4919 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
  4921 /*
  4922 ** CAPI3REF: Find the next prepared statement
  4923 **
  4924 ** ^This interface returns a pointer to the next [prepared statement] after
  4925 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
  4926 ** then this interface returns a pointer to the first prepared statement
  4927 ** associated with the database connection pDb.  ^If no prepared statement
  4928 ** satisfies the conditions of this routine, it returns NULL.
  4929 **
  4930 ** The [database connection] pointer D in a call to
  4931 ** [sqlite3_next_stmt(D,S)] must refer to an open database
  4932 ** connection and in particular must not be a NULL pointer.
  4933 */
  4934 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  4936 /*
  4937 ** CAPI3REF: Commit And Rollback Notification Callbacks
  4938 **
  4939 ** ^The sqlite3_commit_hook() interface registers a callback
  4940 ** function to be invoked whenever a transaction is [COMMIT | committed].
  4941 ** ^Any callback set by a previous call to sqlite3_commit_hook()
  4942 ** for the same database connection is overridden.
  4943 ** ^The sqlite3_rollback_hook() interface registers a callback
  4944 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
  4945 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
  4946 ** for the same database connection is overridden.
  4947 ** ^The pArg argument is passed through to the callback.
  4948 ** ^If the callback on a commit hook function returns non-zero,
  4949 ** then the commit is converted into a rollback.
  4950 **
  4951 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
  4952 ** return the P argument from the previous call of the same function
  4953 ** on the same [database connection] D, or NULL for
  4954 ** the first call for each function on D.
  4955 **
  4956 ** The commit and rollback hook callbacks are not reentrant.
  4957 ** The callback implementation must not do anything that will modify
  4958 ** the database connection that invoked the callback.  Any actions
  4959 ** to modify the database connection must be deferred until after the
  4960 ** completion of the [sqlite3_step()] call that triggered the commit
  4961 ** or rollback hook in the first place.
  4962 ** Note that running any other SQL statements, including SELECT statements,
  4963 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
  4964 ** the database connections for the meaning of "modify" in this paragraph.
  4965 **
  4966 ** ^Registering a NULL function disables the callback.
  4967 **
  4968 ** ^When the commit hook callback routine returns zero, the [COMMIT]
  4969 ** operation is allowed to continue normally.  ^If the commit hook
  4970 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
  4971 ** ^The rollback hook is invoked on a rollback that results from a commit
  4972 ** hook returning non-zero, just as it would be with any other rollback.
  4973 **
  4974 ** ^For the purposes of this API, a transaction is said to have been
  4975 ** rolled back if an explicit "ROLLBACK" statement is executed, or
  4976 ** an error or constraint causes an implicit rollback to occur.
  4977 ** ^The rollback callback is not invoked if a transaction is
  4978 ** automatically rolled back because the database connection is closed.
  4979 **
  4980 ** See also the [sqlite3_update_hook()] interface.
  4981 */
  4982 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  4983 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  4985 /*
  4986 ** CAPI3REF: Data Change Notification Callbacks
  4987 **
  4988 ** ^The sqlite3_update_hook() interface registers a callback function
  4989 ** with the [database connection] identified by the first argument
  4990 ** to be invoked whenever a row is updated, inserted or deleted in
  4991 ** a rowid table.
  4992 ** ^Any callback set by a previous call to this function
  4993 ** for the same database connection is overridden.
  4994 **
  4995 ** ^The second argument is a pointer to the function to invoke when a
  4996 ** row is updated, inserted or deleted in a rowid table.
  4997 ** ^The first argument to the callback is a copy of the third argument
  4998 ** to sqlite3_update_hook().
  4999 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  5000 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
  5001 ** to be invoked.
  5002 ** ^The third and fourth arguments to the callback contain pointers to the
  5003 ** database and table name containing the affected row.
  5004 ** ^The final callback parameter is the [rowid] of the row.
  5005 ** ^In the case of an update, this is the [rowid] after the update takes place.
  5006 **
  5007 ** ^(The update hook is not invoked when internal system tables are
  5008 ** modified (i.e. sqlite_master and sqlite_sequence).)^
  5009 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
  5010 **
  5011 ** ^In the current implementation, the update hook
  5012 ** is not invoked when duplication rows are deleted because of an
  5013 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
  5014 ** invoked when rows are deleted using the [truncate optimization].
  5015 ** The exceptions defined in this paragraph might change in a future
  5016 ** release of SQLite.
  5017 **
  5018 ** The update hook implementation must not do anything that will modify
  5019 ** the database connection that invoked the update hook.  Any actions
  5020 ** to modify the database connection must be deferred until after the
  5021 ** completion of the [sqlite3_step()] call that triggered the update hook.
  5022 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  5023 ** database connections for the meaning of "modify" in this paragraph.
  5024 **
  5025 ** ^The sqlite3_update_hook(D,C,P) function
  5026 ** returns the P argument from the previous call
  5027 ** on the same [database connection] D, or NULL for
  5028 ** the first call on D.
  5029 **
  5030 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
  5031 ** interfaces.
  5032 */
  5033 SQLITE_API void *sqlite3_update_hook(
  5034   sqlite3*, 
  5035   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
  5036   void*
  5037 );
  5039 /*
  5040 ** CAPI3REF: Enable Or Disable Shared Pager Cache
  5041 **
  5042 ** ^(This routine enables or disables the sharing of the database cache
  5043 ** and schema data structures between [database connection | connections]
  5044 ** to the same database. Sharing is enabled if the argument is true
  5045 ** and disabled if the argument is false.)^
  5046 **
  5047 ** ^Cache sharing is enabled and disabled for an entire process.
  5048 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
  5049 ** sharing was enabled or disabled for each thread separately.
  5050 **
  5051 ** ^(The cache sharing mode set by this interface effects all subsequent
  5052 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  5053 ** Existing database connections continue use the sharing mode
  5054 ** that was in effect at the time they were opened.)^
  5055 **
  5056 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  5057 ** successfully.  An [error code] is returned otherwise.)^
  5058 **
  5059 ** ^Shared cache is disabled by default. But this might change in
  5060 ** future releases of SQLite.  Applications that care about shared
  5061 ** cache setting should set it explicitly.
  5062 **
  5063 ** This interface is threadsafe on processors where writing a
  5064 ** 32-bit integer is atomic.
  5065 **
  5066 ** See Also:  [SQLite Shared-Cache Mode]
  5067 */
  5068 SQLITE_API int sqlite3_enable_shared_cache(int);
  5070 /*
  5071 ** CAPI3REF: Attempt To Free Heap Memory
  5072 **
  5073 ** ^The sqlite3_release_memory() interface attempts to free N bytes
  5074 ** of heap memory by deallocating non-essential memory allocations
  5075 ** held by the database library.   Memory used to cache database
  5076 ** pages to improve performance is an example of non-essential memory.
  5077 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
  5078 ** which might be more or less than the amount requested.
  5079 ** ^The sqlite3_release_memory() routine is a no-op returning zero
  5080 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5081 **
  5082 ** See also: [sqlite3_db_release_memory()]
  5083 */
  5084 SQLITE_API int sqlite3_release_memory(int);
  5086 /*
  5087 ** CAPI3REF: Free Memory Used By A Database Connection
  5088 **
  5089 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5090 ** memory as possible from database connection D. Unlike the
  5091 ** [sqlite3_release_memory()] interface, this interface is in effect even
  5092 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5093 ** omitted.
  5094 **
  5095 ** See also: [sqlite3_release_memory()]
  5096 */
  5097 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
  5099 /*
  5100 ** CAPI3REF: Impose A Limit On Heap Size
  5101 **
  5102 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
  5103 ** soft limit on the amount of heap memory that may be allocated by SQLite.
  5104 ** ^SQLite strives to keep heap memory utilization below the soft heap
  5105 ** limit by reducing the number of pages held in the page cache
  5106 ** as heap memory usages approaches the limit.
  5107 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
  5108 ** below the limit, it will exceed the limit rather than generate
  5109 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit 
  5110 ** is advisory only.
  5111 **
  5112 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
  5113 ** the soft heap limit prior to the call, or negative in the case of an
  5114 ** error.  ^If the argument N is negative
  5115 ** then no change is made to the soft heap limit.  Hence, the current
  5116 ** size of the soft heap limit can be determined by invoking
  5117 ** sqlite3_soft_heap_limit64() with a negative argument.
  5118 **
  5119 ** ^If the argument N is zero then the soft heap limit is disabled.
  5120 **
  5121 ** ^(The soft heap limit is not enforced in the current implementation
  5122 ** if one or more of following conditions are true:
  5123 **
  5124 ** <ul>
  5125 ** <li> The soft heap limit is set to zero.
  5126 ** <li> Memory accounting is disabled using a combination of the
  5127 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
  5128 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
  5129 ** <li> An alternative page cache implementation is specified using
  5130 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  5131 ** <li> The page cache allocates from its own memory pool supplied
  5132 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  5133 **      from the heap.
  5134 ** </ul>)^
  5135 **
  5136 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
  5137 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  5138 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  5139 ** the soft heap limit is enforced on every memory allocation.  Without
  5140 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  5141 ** when memory is allocated by the page cache.  Testing suggests that because
  5142 ** the page cache is the predominate memory user in SQLite, most
  5143 ** applications will achieve adequate soft heap limit enforcement without
  5144 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5145 **
  5146 ** The circumstances under which SQLite will enforce the soft heap limit may
  5147 ** changes in future releases of SQLite.
  5148 */
  5149 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
  5151 /*
  5152 ** CAPI3REF: Deprecated Soft Heap Limit Interface
  5153 ** DEPRECATED
  5154 **
  5155 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
  5156 ** interface.  This routine is provided for historical compatibility
  5157 ** only.  All new applications should use the
  5158 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5159 */
  5160 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
  5163 /*
  5164 ** CAPI3REF: Extract Metadata About A Column Of A Table
  5165 **
  5166 ** ^This routine returns metadata about a specific column of a specific
  5167 ** database table accessible using the [database connection] handle
  5168 ** passed as the first function argument.
  5169 **
  5170 ** ^The column is identified by the second, third and fourth parameters to
  5171 ** this function. ^The second parameter is either the name of the database
  5172 ** (i.e. "main", "temp", or an attached database) containing the specified
  5173 ** table or NULL. ^If it is NULL, then all attached databases are searched
  5174 ** for the table using the same algorithm used by the database engine to
  5175 ** resolve unqualified table references.
  5176 **
  5177 ** ^The third and fourth parameters to this function are the table and column
  5178 ** name of the desired column, respectively. Neither of these parameters
  5179 ** may be NULL.
  5180 **
  5181 ** ^Metadata is returned by writing to the memory locations passed as the 5th
  5182 ** and subsequent parameters to this function. ^Any of these arguments may be
  5183 ** NULL, in which case the corresponding element of metadata is omitted.
  5184 **
  5185 ** ^(<blockquote>
  5186 ** <table border="1">
  5187 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
  5188 **
  5189 ** <tr><td> 5th <td> const char* <td> Data type
  5190 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
  5191 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
  5192 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
  5193 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
  5194 ** </table>
  5195 ** </blockquote>)^
  5196 **
  5197 ** ^The memory pointed to by the character pointers returned for the
  5198 ** declaration type and collation sequence is valid only until the next
  5199 ** call to any SQLite API function.
  5200 **
  5201 ** ^If the specified table is actually a view, an [error code] is returned.
  5202 **
  5203 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
  5204 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
  5205 ** parameters are set for the explicitly declared column. ^(If there is no
  5206 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
  5207 ** parameters are set as follows:
  5208 **
  5209 ** <pre>
  5210 **     data type: "INTEGER"
  5211 **     collation sequence: "BINARY"
  5212 **     not null: 0
  5213 **     primary key: 1
  5214 **     auto increment: 0
  5215 ** </pre>)^
  5216 **
  5217 ** ^(This function may load one or more schemas from database files. If an
  5218 ** error occurs during this process, or if the requested table or column
  5219 ** cannot be found, an [error code] is returned and an error message left
  5220 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
  5221 **
  5222 ** ^This API is only available if the library was compiled with the
  5223 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
  5224 */
  5225 SQLITE_API int sqlite3_table_column_metadata(
  5226   sqlite3 *db,                /* Connection handle */
  5227   const char *zDbName,        /* Database name or NULL */
  5228   const char *zTableName,     /* Table name */
  5229   const char *zColumnName,    /* Column name */
  5230   char const **pzDataType,    /* OUTPUT: Declared data type */
  5231   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  5232   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  5233   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  5234   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  5235 );
  5237 /*
  5238 ** CAPI3REF: Load An Extension
  5239 **
  5240 ** ^This interface loads an SQLite extension library from the named file.
  5241 **
  5242 ** ^The sqlite3_load_extension() interface attempts to load an
  5243 ** [SQLite extension] library contained in the file zFile.  If
  5244 ** the file cannot be loaded directly, attempts are made to load
  5245 ** with various operating-system specific extensions added.
  5246 ** So for example, if "samplelib" cannot be loaded, then names like
  5247 ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
  5248 ** be tried also.
  5249 **
  5250 ** ^The entry point is zProc.
  5251 ** ^(zProc may be 0, in which case SQLite will try to come up with an
  5252 ** entry point name on its own.  It first tries "sqlite3_extension_init".
  5253 ** If that does not work, it constructs a name "sqlite3_X_init" where the
  5254 ** X is consists of the lower-case equivalent of all ASCII alphabetic
  5255 ** characters in the filename from the last "/" to the first following
  5256 ** "." and omitting any initial "lib".)^
  5257 ** ^The sqlite3_load_extension() interface returns
  5258 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
  5259 ** ^If an error occurs and pzErrMsg is not 0, then the
  5260 ** [sqlite3_load_extension()] interface shall attempt to
  5261 ** fill *pzErrMsg with error message text stored in memory
  5262 ** obtained from [sqlite3_malloc()]. The calling function
  5263 ** should free this memory by calling [sqlite3_free()].
  5264 **
  5265 ** ^Extension loading must be enabled using
  5266 ** [sqlite3_enable_load_extension()] prior to calling this API,
  5267 ** otherwise an error will be returned.
  5268 **
  5269 ** See also the [load_extension() SQL function].
  5270 */
  5271 SQLITE_API int sqlite3_load_extension(
  5272   sqlite3 *db,          /* Load the extension into this database connection */
  5273   const char *zFile,    /* Name of the shared library containing extension */
  5274   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
  5275   char **pzErrMsg       /* Put error message here if not 0 */
  5276 );
  5278 /*
  5279 ** CAPI3REF: Enable Or Disable Extension Loading
  5280 **
  5281 ** ^So as not to open security holes in older applications that are
  5282 ** unprepared to deal with [extension loading], and as a means of disabling
  5283 ** [extension loading] while evaluating user-entered SQL, the following API
  5284 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5285 **
  5286 ** ^Extension loading is off by default.
  5287 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  5288 ** to turn extension loading on and call it with onoff==0 to turn
  5289 ** it back off again.
  5290 */
  5291 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
  5293 /*
  5294 ** CAPI3REF: Automatically Load Statically Linked Extensions
  5295 **
  5296 ** ^This interface causes the xEntryPoint() function to be invoked for
  5297 ** each new [database connection] that is created.  The idea here is that
  5298 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
  5299 ** that is to be automatically loaded into all new database connections.
  5300 **
  5301 ** ^(Even though the function prototype shows that xEntryPoint() takes
  5302 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
  5303 ** arguments and expects and integer result as if the signature of the
  5304 ** entry point where as follows:
  5305 **
  5306 ** <blockquote><pre>
  5307 ** &nbsp;  int xEntryPoint(
  5308 ** &nbsp;    sqlite3 *db,
  5309 ** &nbsp;    const char **pzErrMsg,
  5310 ** &nbsp;    const struct sqlite3_api_routines *pThunk
  5311 ** &nbsp;  );
  5312 ** </pre></blockquote>)^
  5313 **
  5314 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
  5315 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
  5316 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
  5317 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
  5318 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
  5319 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
  5320 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
  5321 **
  5322 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
  5323 ** on the list of automatic extensions is a harmless no-op. ^No entry point
  5324 ** will be called more than once for each database connection that is opened.
  5325 **
  5326 ** See also: [sqlite3_reset_auto_extension()]
  5327 ** and [sqlite3_cancel_auto_extension()]
  5328 */
  5329 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
  5331 /*
  5332 ** CAPI3REF: Cancel Automatic Extension Loading
  5333 **
  5334 ** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
  5335 ** initialization routine X that was registered using a prior call to
  5336 ** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
  5337 ** routine returns 1 if initialization routine X was successfully 
  5338 ** unregistered and it returns 0 if X was not on the list of initialization
  5339 ** routines.
  5340 */
  5341 SQLITE_API int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
  5343 /*
  5344 ** CAPI3REF: Reset Automatic Extension Loading
  5345 **
  5346 ** ^This interface disables all automatic extensions previously
  5347 ** registered using [sqlite3_auto_extension()].
  5348 */
  5349 SQLITE_API void sqlite3_reset_auto_extension(void);
  5351 /*
  5352 ** The interface to the virtual-table mechanism is currently considered
  5353 ** to be experimental.  The interface might change in incompatible ways.
  5354 ** If this is a problem for you, do not use the interface at this time.
  5355 **
  5356 ** When the virtual-table mechanism stabilizes, we will declare the
  5357 ** interface fixed, support it indefinitely, and remove this comment.
  5358 */
  5360 /*
  5361 ** Structures used by the virtual table interface
  5362 */
  5363 typedef struct sqlite3_vtab sqlite3_vtab;
  5364 typedef struct sqlite3_index_info sqlite3_index_info;
  5365 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  5366 typedef struct sqlite3_module sqlite3_module;
  5368 /*
  5369 ** CAPI3REF: Virtual Table Object
  5370 ** KEYWORDS: sqlite3_module {virtual table module}
  5371 **
  5372 ** This structure, sometimes called a "virtual table module", 
  5373 ** defines the implementation of a [virtual tables].  
  5374 ** This structure consists mostly of methods for the module.
  5375 **
  5376 ** ^A virtual table module is created by filling in a persistent
  5377 ** instance of this structure and passing a pointer to that instance
  5378 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  5379 ** ^The registration remains valid until it is replaced by a different
  5380 ** module or until the [database connection] closes.  The content
  5381 ** of this structure must not change while it is registered with
  5382 ** any database connection.
  5383 */
  5384 struct sqlite3_module {
  5385   int iVersion;
  5386   int (*xCreate)(sqlite3*, void *pAux,
  5387                int argc, const char *const*argv,
  5388                sqlite3_vtab **ppVTab, char**);
  5389   int (*xConnect)(sqlite3*, void *pAux,
  5390                int argc, const char *const*argv,
  5391                sqlite3_vtab **ppVTab, char**);
  5392   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
  5393   int (*xDisconnect)(sqlite3_vtab *pVTab);
  5394   int (*xDestroy)(sqlite3_vtab *pVTab);
  5395   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
  5396   int (*xClose)(sqlite3_vtab_cursor*);
  5397   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
  5398                 int argc, sqlite3_value **argv);
  5399   int (*xNext)(sqlite3_vtab_cursor*);
  5400   int (*xEof)(sqlite3_vtab_cursor*);
  5401   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
  5402   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
  5403   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
  5404   int (*xBegin)(sqlite3_vtab *pVTab);
  5405   int (*xSync)(sqlite3_vtab *pVTab);
  5406   int (*xCommit)(sqlite3_vtab *pVTab);
  5407   int (*xRollback)(sqlite3_vtab *pVTab);
  5408   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
  5409                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
  5410                        void **ppArg);
  5411   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  5412   /* The methods above are in version 1 of the sqlite_module object. Those 
  5413   ** below are for version 2 and greater. */
  5414   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  5415   int (*xRelease)(sqlite3_vtab *pVTab, int);
  5416   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
  5417 };
  5419 /*
  5420 ** CAPI3REF: Virtual Table Indexing Information
  5421 ** KEYWORDS: sqlite3_index_info
  5422 **
  5423 ** The sqlite3_index_info structure and its substructures is used as part
  5424 ** of the [virtual table] interface to
  5425 ** pass information into and receive the reply from the [xBestIndex]
  5426 ** method of a [virtual table module].  The fields under **Inputs** are the
  5427 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
  5428 ** results into the **Outputs** fields.
  5429 **
  5430 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
  5431 **
  5432 ** <blockquote>column OP expr</blockquote>
  5433 **
  5434 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
  5435 ** stored in aConstraint[].op using one of the
  5436 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
  5437 ** ^(The index of the column is stored in
  5438 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
  5439 ** expr on the right-hand side can be evaluated (and thus the constraint
  5440 ** is usable) and false if it cannot.)^
  5441 **
  5442 ** ^The optimizer automatically inverts terms of the form "expr OP column"
  5443 ** and makes other simplifications to the WHERE clause in an attempt to
  5444 ** get as many WHERE clause terms into the form shown above as possible.
  5445 ** ^The aConstraint[] array only reports WHERE clause terms that are
  5446 ** relevant to the particular virtual table being queried.
  5447 **
  5448 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
  5449 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
  5450 **
  5451 ** The [xBestIndex] method must fill aConstraintUsage[] with information
  5452 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
  5453 ** the right-hand side of the corresponding aConstraint[] is evaluated
  5454 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
  5455 ** is true, then the constraint is assumed to be fully handled by the
  5456 ** virtual table and is not checked again by SQLite.)^
  5457 **
  5458 ** ^The idxNum and idxPtr values are recorded and passed into the
  5459 ** [xFilter] method.
  5460 ** ^[sqlite3_free()] is used to free idxPtr if and only if
  5461 ** needToFreeIdxPtr is true.
  5462 **
  5463 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  5464 ** the correct order to satisfy the ORDER BY clause so that no separate
  5465 ** sorting step is required.
  5466 **
  5467 ** ^The estimatedCost value is an estimate of the cost of a particular
  5468 ** strategy. A cost of N indicates that the cost of the strategy is similar
  5469 ** to a linear scan of an SQLite table with N rows. A cost of log(N) 
  5470 ** indicates that the expense of the operation is similar to that of a
  5471 ** binary search on a unique indexed field of an SQLite table with N rows.
  5472 **
  5473 ** ^The estimatedRows value is an estimate of the number of rows that
  5474 ** will be returned by the strategy.
  5475 **
  5476 ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
  5477 ** structure for SQLite version 3.8.2. If a virtual table extension is
  5478 ** used with an SQLite version earlier than 3.8.2, the results of attempting 
  5479 ** to read or write the estimatedRows field are undefined (but are likely 
  5480 ** to included crashing the application). The estimatedRows field should
  5481 ** therefore only be used if [sqlite3_libversion_number()] returns a
  5482 ** value greater than or equal to 3008002.
  5483 */
  5484 struct sqlite3_index_info {
  5485   /* Inputs */
  5486   int nConstraint;           /* Number of entries in aConstraint */
  5487   struct sqlite3_index_constraint {
  5488      int iColumn;              /* Column on left-hand side of constraint */
  5489      unsigned char op;         /* Constraint operator */
  5490      unsigned char usable;     /* True if this constraint is usable */
  5491      int iTermOffset;          /* Used internally - xBestIndex should ignore */
  5492   } *aConstraint;            /* Table of WHERE clause constraints */
  5493   int nOrderBy;              /* Number of terms in the ORDER BY clause */
  5494   struct sqlite3_index_orderby {
  5495      int iColumn;              /* Column number */
  5496      unsigned char desc;       /* True for DESC.  False for ASC. */
  5497   } *aOrderBy;               /* The ORDER BY clause */
  5498   /* Outputs */
  5499   struct sqlite3_index_constraint_usage {
  5500     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
  5501     unsigned char omit;      /* Do not code a test for this constraint */
  5502   } *aConstraintUsage;
  5503   int idxNum;                /* Number used to identify the index */
  5504   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
  5505   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
  5506   int orderByConsumed;       /* True if output is already ordered */
  5507   double estimatedCost;           /* Estimated cost of using this index */
  5508   /* Fields below are only available in SQLite 3.8.2 and later */
  5509   sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
  5510 };
  5512 /*
  5513 ** CAPI3REF: Virtual Table Constraint Operator Codes
  5514 **
  5515 ** These macros defined the allowed values for the
  5516 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
  5517 ** an operator that is part of a constraint term in the wHERE clause of
  5518 ** a query that uses a [virtual table].
  5519 */
  5520 #define SQLITE_INDEX_CONSTRAINT_EQ    2
  5521 #define SQLITE_INDEX_CONSTRAINT_GT    4
  5522 #define SQLITE_INDEX_CONSTRAINT_LE    8
  5523 #define SQLITE_INDEX_CONSTRAINT_LT    16
  5524 #define SQLITE_INDEX_CONSTRAINT_GE    32
  5525 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5527 /*
  5528 ** CAPI3REF: Register A Virtual Table Implementation
  5529 **
  5530 ** ^These routines are used to register a new [virtual table module] name.
  5531 ** ^Module names must be registered before
  5532 ** creating a new [virtual table] using the module and before using a
  5533 ** preexisting [virtual table] for the module.
  5534 **
  5535 ** ^The module name is registered on the [database connection] specified
  5536 ** by the first parameter.  ^The name of the module is given by the 
  5537 ** second parameter.  ^The third parameter is a pointer to
  5538 ** the implementation of the [virtual table module].   ^The fourth
  5539 ** parameter is an arbitrary client data pointer that is passed through
  5540 ** into the [xCreate] and [xConnect] methods of the virtual table module
  5541 ** when a new virtual table is be being created or reinitialized.
  5542 **
  5543 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
  5544 ** is a pointer to a destructor for the pClientData.  ^SQLite will
  5545 ** invoke the destructor function (if it is not NULL) when SQLite
  5546 ** no longer needs the pClientData pointer.  ^The destructor will also
  5547 ** be invoked if the call to sqlite3_create_module_v2() fails.
  5548 ** ^The sqlite3_create_module()
  5549 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
  5550 ** destructor.
  5551 */
  5552 SQLITE_API int sqlite3_create_module(
  5553   sqlite3 *db,               /* SQLite connection to register module with */
  5554   const char *zName,         /* Name of the module */
  5555   const sqlite3_module *p,   /* Methods for the module */
  5556   void *pClientData          /* Client data for xCreate/xConnect */
  5557 );
  5558 SQLITE_API int sqlite3_create_module_v2(
  5559   sqlite3 *db,               /* SQLite connection to register module with */
  5560   const char *zName,         /* Name of the module */
  5561   const sqlite3_module *p,   /* Methods for the module */
  5562   void *pClientData,         /* Client data for xCreate/xConnect */
  5563   void(*xDestroy)(void*)     /* Module destructor function */
  5564 );
  5566 /*
  5567 ** CAPI3REF: Virtual Table Instance Object
  5568 ** KEYWORDS: sqlite3_vtab
  5569 **
  5570 ** Every [virtual table module] implementation uses a subclass
  5571 ** of this object to describe a particular instance
  5572 ** of the [virtual table].  Each subclass will
  5573 ** be tailored to the specific needs of the module implementation.
  5574 ** The purpose of this superclass is to define certain fields that are
  5575 ** common to all module implementations.
  5576 **
  5577 ** ^Virtual tables methods can set an error message by assigning a
  5578 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
  5579 ** take care that any prior string is freed by a call to [sqlite3_free()]
  5580 ** prior to assigning a new string to zErrMsg.  ^After the error message
  5581 ** is delivered up to the client application, the string will be automatically
  5582 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  5583 */
  5584 struct sqlite3_vtab {
  5585   const sqlite3_module *pModule;  /* The module for this virtual table */
  5586   int nRef;                       /* NO LONGER USED */
  5587   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
  5588   /* Virtual table implementations will typically add additional fields */
  5589 };
  5591 /*
  5592 ** CAPI3REF: Virtual Table Cursor Object
  5593 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
  5594 **
  5595 ** Every [virtual table module] implementation uses a subclass of the
  5596 ** following structure to describe cursors that point into the
  5597 ** [virtual table] and are used
  5598 ** to loop through the virtual table.  Cursors are created using the
  5599 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
  5600 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
  5601 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
  5602 ** of the module.  Each module implementation will define
  5603 ** the content of a cursor structure to suit its own needs.
  5604 **
  5605 ** This superclass exists in order to define fields of the cursor that
  5606 ** are common to all implementations.
  5607 */
  5608 struct sqlite3_vtab_cursor {
  5609   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
  5610   /* Virtual table implementations will typically add additional fields */
  5611 };
  5613 /*
  5614 ** CAPI3REF: Declare The Schema Of A Virtual Table
  5615 **
  5616 ** ^The [xCreate] and [xConnect] methods of a
  5617 ** [virtual table module] call this interface
  5618 ** to declare the format (the names and datatypes of the columns) of
  5619 ** the virtual tables they implement.
  5620 */
  5621 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5623 /*
  5624 ** CAPI3REF: Overload A Function For A Virtual Table
  5625 **
  5626 ** ^(Virtual tables can provide alternative implementations of functions
  5627 ** using the [xFindFunction] method of the [virtual table module].  
  5628 ** But global versions of those functions
  5629 ** must exist in order to be overloaded.)^
  5630 **
  5631 ** ^(This API makes sure a global version of a function with a particular
  5632 ** name and number of parameters exists.  If no such function exists
  5633 ** before this API is called, a new function is created.)^  ^The implementation
  5634 ** of the new function always causes an exception to be thrown.  So
  5635 ** the new function is not good for anything by itself.  Its only
  5636 ** purpose is to be a placeholder function that can be overloaded
  5637 ** by a [virtual table].
  5638 */
  5639 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
  5641 /*
  5642 ** The interface to the virtual-table mechanism defined above (back up
  5643 ** to a comment remarkably similar to this one) is currently considered
  5644 ** to be experimental.  The interface might change in incompatible ways.
  5645 ** If this is a problem for you, do not use the interface at this time.
  5646 **
  5647 ** When the virtual-table mechanism stabilizes, we will declare the
  5648 ** interface fixed, support it indefinitely, and remove this comment.
  5649 */
  5651 /*
  5652 ** CAPI3REF: A Handle To An Open BLOB
  5653 ** KEYWORDS: {BLOB handle} {BLOB handles}
  5654 **
  5655 ** An instance of this object represents an open BLOB on which
  5656 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
  5657 ** ^Objects of this type are created by [sqlite3_blob_open()]
  5658 ** and destroyed by [sqlite3_blob_close()].
  5659 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
  5660 ** can be used to read or write small subsections of the BLOB.
  5661 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5662 */
  5663 typedef struct sqlite3_blob sqlite3_blob;
  5665 /*
  5666 ** CAPI3REF: Open A BLOB For Incremental I/O
  5667 **
  5668 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5669 ** in row iRow, column zColumn, table zTable in database zDb;
  5670 ** in other words, the same BLOB that would be selected by:
  5671 **
  5672 ** <pre>
  5673 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  5674 ** </pre>)^
  5675 **
  5676 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
  5677 ** and write access. ^If it is zero, the BLOB is opened for read access.
  5678 ** ^It is not possible to open a column that is part of an index or primary 
  5679 ** key for writing. ^If [foreign key constraints] are enabled, it is 
  5680 ** not possible to open a column that is part of a [child key] for writing.
  5681 **
  5682 ** ^Note that the database name is not the filename that contains
  5683 ** the database but rather the symbolic name of the database that
  5684 ** appears after the AS keyword when the database is connected using [ATTACH].
  5685 ** ^For the main database file, the database name is "main".
  5686 ** ^For TEMP tables, the database name is "temp".
  5687 **
  5688 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
  5689 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
  5690 ** to be a null pointer.)^
  5691 ** ^This function sets the [database connection] error code and message
  5692 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
  5693 ** functions. ^Note that the *ppBlob variable is always initialized in a
  5694 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
  5695 ** regardless of the success or failure of this routine.
  5696 **
  5697 ** ^(If the row that a BLOB handle points to is modified by an
  5698 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  5699 ** then the BLOB handle is marked as "expired".
  5700 ** This is true if any column of the row is changed, even a column
  5701 ** other than the one the BLOB handle is open on.)^
  5702 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  5703 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
  5704 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
  5705 ** rolled back by the expiration of the BLOB.  Such changes will eventually
  5706 ** commit if the transaction continues to completion.)^
  5707 **
  5708 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  5709 ** the opened blob.  ^The size of a blob may not be changed by this
  5710 ** interface.  Use the [UPDATE] SQL command to change the size of a
  5711 ** blob.
  5712 **
  5713 ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
  5714 ** table.  Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
  5715 **
  5716 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  5717 ** and the built-in [zeroblob] SQL function can be used, if desired,
  5718 ** to create an empty, zero-filled blob in which to read or write using
  5719 ** this interface.
  5720 **
  5721 ** To avoid a resource leak, every open [BLOB handle] should eventually
  5722 ** be released by a call to [sqlite3_blob_close()].
  5723 */
  5724 SQLITE_API int sqlite3_blob_open(
  5725   sqlite3*,
  5726   const char *zDb,
  5727   const char *zTable,
  5728   const char *zColumn,
  5729   sqlite3_int64 iRow,
  5730   int flags,
  5731   sqlite3_blob **ppBlob
  5732 );
  5734 /*
  5735 ** CAPI3REF: Move a BLOB Handle to a New Row
  5736 **
  5737 ** ^This function is used to move an existing blob handle so that it points
  5738 ** to a different row of the same database table. ^The new row is identified
  5739 ** by the rowid value passed as the second argument. Only the row can be
  5740 ** changed. ^The database, table and column on which the blob handle is open
  5741 ** remain the same. Moving an existing blob handle to a new row can be
  5742 ** faster than closing the existing handle and opening a new one.
  5743 **
  5744 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  5745 ** it must exist and there must be either a blob or text value stored in
  5746 ** the nominated column.)^ ^If the new row is not present in the table, or if
  5747 ** it does not contain a blob or text value, or if another error occurs, an
  5748 ** SQLite error code is returned and the blob handle is considered aborted.
  5749 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
  5750 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
  5751 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
  5752 ** always returns zero.
  5753 **
  5754 ** ^This function sets the database handle error code and message.
  5755 */
  5756 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  5758 /*
  5759 ** CAPI3REF: Close A BLOB Handle
  5760 **
  5761 ** ^Closes an open [BLOB handle].
  5762 **
  5763 ** ^Closing a BLOB shall cause the current transaction to commit
  5764 ** if there are no other BLOBs, no pending prepared statements, and the
  5765 ** database connection is in [autocommit mode].
  5766 ** ^If any writes were made to the BLOB, they might be held in cache
  5767 ** until the close operation if they will fit.
  5768 **
  5769 ** ^(Closing the BLOB often forces the changes
  5770 ** out to disk and so if any I/O errors occur, they will likely occur
  5771 ** at the time when the BLOB is closed.  Any errors that occur during
  5772 ** closing are reported as a non-zero return value.)^
  5773 **
  5774 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
  5775 ** an error code, the BLOB is still closed.)^
  5776 **
  5777 ** ^Calling this routine with a null pointer (such as would be returned
  5778 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
  5779 */
  5780 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
  5782 /*
  5783 ** CAPI3REF: Return The Size Of An Open BLOB
  5784 **
  5785 ** ^Returns the size in bytes of the BLOB accessible via the 
  5786 ** successfully opened [BLOB handle] in its only argument.  ^The
  5787 ** incremental blob I/O routines can only read or overwriting existing
  5788 ** blob content; they cannot change the size of a blob.
  5789 **
  5790 ** This routine only works on a [BLOB handle] which has been created
  5791 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5792 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  5793 ** to this routine results in undefined and probably undesirable behavior.
  5794 */
  5795 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
  5797 /*
  5798 ** CAPI3REF: Read Data From A BLOB Incrementally
  5799 **
  5800 ** ^(This function is used to read data from an open [BLOB handle] into a
  5801 ** caller-supplied buffer. N bytes of data are copied into buffer Z
  5802 ** from the open BLOB, starting at offset iOffset.)^
  5803 **
  5804 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5805 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
  5806 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
  5807 ** ^The size of the blob (and hence the maximum value of N+iOffset)
  5808 ** can be determined using the [sqlite3_blob_bytes()] interface.
  5809 **
  5810 ** ^An attempt to read from an expired [BLOB handle] fails with an
  5811 ** error code of [SQLITE_ABORT].
  5812 **
  5813 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
  5814 ** Otherwise, an [error code] or an [extended error code] is returned.)^
  5815 **
  5816 ** This routine only works on a [BLOB handle] which has been created
  5817 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5818 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  5819 ** to this routine results in undefined and probably undesirable behavior.
  5820 **
  5821 ** See also: [sqlite3_blob_write()].
  5822 */
  5823 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  5825 /*
  5826 ** CAPI3REF: Write Data Into A BLOB Incrementally
  5827 **
  5828 ** ^This function is used to write data into an open [BLOB handle] from a
  5829 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
  5830 ** into the open BLOB, starting at offset iOffset.
  5831 **
  5832 ** ^If the [BLOB handle] passed as the first argument was not opened for
  5833 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
  5834 ** this function returns [SQLITE_READONLY].
  5835 **
  5836 ** ^This function may only modify the contents of the BLOB; it is
  5837 ** not possible to increase the size of a BLOB using this API.
  5838 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5839 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
  5840 ** less than zero [SQLITE_ERROR] is returned and no data is written.
  5841 ** The size of the BLOB (and hence the maximum value of N+iOffset)
  5842 ** can be determined using the [sqlite3_blob_bytes()] interface.
  5843 **
  5844 ** ^An attempt to write to an expired [BLOB handle] fails with an
  5845 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
  5846 ** before the [BLOB handle] expired are not rolled back by the
  5847 ** expiration of the handle, though of course those changes might
  5848 ** have been overwritten by the statement that expired the BLOB handle
  5849 ** or by other independent statements.
  5850 **
  5851 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  5852 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
  5853 **
  5854 ** This routine only works on a [BLOB handle] which has been created
  5855 ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5856 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
  5857 ** to this routine results in undefined and probably undesirable behavior.
  5858 **
  5859 ** See also: [sqlite3_blob_read()].
  5860 */
  5861 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
  5863 /*
  5864 ** CAPI3REF: Virtual File System Objects
  5865 **
  5866 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
  5867 ** that SQLite uses to interact
  5868 ** with the underlying operating system.  Most SQLite builds come with a
  5869 ** single default VFS that is appropriate for the host computer.
  5870 ** New VFSes can be registered and existing VFSes can be unregistered.
  5871 ** The following interfaces are provided.
  5872 **
  5873 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
  5874 ** ^Names are case sensitive.
  5875 ** ^Names are zero-terminated UTF-8 strings.
  5876 ** ^If there is no match, a NULL pointer is returned.
  5877 ** ^If zVfsName is NULL then the default VFS is returned.
  5878 **
  5879 ** ^New VFSes are registered with sqlite3_vfs_register().
  5880 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
  5881 ** ^The same VFS can be registered multiple times without injury.
  5882 ** ^To make an existing VFS into the default VFS, register it again
  5883 ** with the makeDflt flag set.  If two different VFSes with the
  5884 ** same name are registered, the behavior is undefined.  If a
  5885 ** VFS is registered with a name that is NULL or an empty string,
  5886 ** then the behavior is undefined.
  5887 **
  5888 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
  5889 ** ^(If the default VFS is unregistered, another VFS is chosen as
  5890 ** the default.  The choice for the new VFS is arbitrary.)^
  5891 */
  5892 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
  5893 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
  5894 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
  5896 /*
  5897 ** CAPI3REF: Mutexes
  5898 **
  5899 ** The SQLite core uses these routines for thread
  5900 ** synchronization. Though they are intended for internal
  5901 ** use by SQLite, code that links against SQLite is
  5902 ** permitted to use any of these routines.
  5903 **
  5904 ** The SQLite source code contains multiple implementations
  5905 ** of these mutex routines.  An appropriate implementation
  5906 ** is selected automatically at compile-time.  ^(The following
  5907 ** implementations are available in the SQLite core:
  5908 **
  5909 ** <ul>
  5910 ** <li>   SQLITE_MUTEX_PTHREADS
  5911 ** <li>   SQLITE_MUTEX_W32
  5912 ** <li>   SQLITE_MUTEX_NOOP
  5913 ** </ul>)^
  5914 **
  5915 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  5916 ** that does no real locking and is appropriate for use in
  5917 ** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
  5918 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
  5919 ** and Windows.
  5920 **
  5921 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  5922 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  5923 ** implementation is included with the library. In this case the
  5924 ** application must supply a custom mutex implementation using the
  5925 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  5926 ** before calling sqlite3_initialize() or any other public sqlite3_
  5927 ** function that calls sqlite3_initialize().)^
  5928 **
  5929 ** ^The sqlite3_mutex_alloc() routine allocates a new
  5930 ** mutex and returns a pointer to it. ^If it returns NULL
  5931 ** that means that a mutex could not be allocated.  ^SQLite
  5932 ** will unwind its stack and return an error.  ^(The argument
  5933 ** to sqlite3_mutex_alloc() is one of these integer constants:
  5934 **
  5935 ** <ul>
  5936 ** <li>  SQLITE_MUTEX_FAST
  5937 ** <li>  SQLITE_MUTEX_RECURSIVE
  5938 ** <li>  SQLITE_MUTEX_STATIC_MASTER
  5939 ** <li>  SQLITE_MUTEX_STATIC_MEM
  5940 ** <li>  SQLITE_MUTEX_STATIC_MEM2
  5941 ** <li>  SQLITE_MUTEX_STATIC_PRNG
  5942 ** <li>  SQLITE_MUTEX_STATIC_LRU
  5943 ** <li>  SQLITE_MUTEX_STATIC_LRU2
  5944 ** </ul>)^
  5945 **
  5946 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  5947 ** cause sqlite3_mutex_alloc() to create
  5948 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  5949 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  5950 ** The mutex implementation does not need to make a distinction
  5951 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  5952 ** not want to.  ^SQLite will only request a recursive mutex in
  5953 ** cases where it really needs one.  ^If a faster non-recursive mutex
  5954 ** implementation is available on the host platform, the mutex subsystem
  5955 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  5956 **
  5957 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
  5958 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
  5959 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
  5960 ** used by the current version of SQLite.  Future versions of SQLite
  5961 ** may add additional static mutexes.  Static mutexes are for internal
  5962 ** use by SQLite only.  Applications that use SQLite mutexes should
  5963 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  5964 ** SQLITE_MUTEX_RECURSIVE.
  5965 **
  5966 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  5967 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  5968 ** returns a different mutex on every call.  ^But for the static
  5969 ** mutex types, the same mutex is returned on every call that has
  5970 ** the same type number.
  5971 **
  5972 ** ^The sqlite3_mutex_free() routine deallocates a previously
  5973 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
  5974 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
  5975 ** use when they are deallocated.  Attempting to deallocate a static
  5976 ** mutex results in undefined behavior.  ^SQLite never deallocates
  5977 ** a static mutex.
  5978 **
  5979 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  5980 ** to enter a mutex.  ^If another thread is already within the mutex,
  5981 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  5982 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
  5983 ** upon successful entry.  ^(Mutexes created using
  5984 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
  5985 ** In such cases the,
  5986 ** mutex must be exited an equal number of times before another thread
  5987 ** can enter.)^  ^(If the same thread tries to enter any other
  5988 ** kind of mutex more than once, the behavior is undefined.
  5989 ** SQLite will never exhibit
  5990 ** such behavior in its own use of mutexes.)^
  5991 **
  5992 ** ^(Some systems (for example, Windows 95) do not support the operation
  5993 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
  5994 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
  5995 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
  5996 **
  5997 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
  5998 ** previously entered by the same thread.   ^(The behavior
  5999 ** is undefined if the mutex is not currently entered by the
  6000 ** calling thread or is not currently allocated.  SQLite will
  6001 ** never do either.)^
  6002 **
  6003 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
  6004 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
  6005 ** behave as no-ops.
  6006 **
  6007 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
  6008 */
  6009 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
  6010 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
  6011 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
  6012 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
  6013 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
  6015 /*
  6016 ** CAPI3REF: Mutex Methods Object
  6017 **
  6018 ** An instance of this structure defines the low-level routines
  6019 ** used to allocate and use mutexes.
  6020 **
  6021 ** Usually, the default mutex implementations provided by SQLite are
  6022 ** sufficient, however the user has the option of substituting a custom
  6023 ** implementation for specialized deployments or systems for which SQLite
  6024 ** does not provide a suitable implementation. In this case, the user
  6025 ** creates and populates an instance of this structure to pass
  6026 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
  6027 ** Additionally, an instance of this structure can be used as an
  6028 ** output variable when querying the system for the current mutex
  6029 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
  6030 **
  6031 ** ^The xMutexInit method defined by this structure is invoked as
  6032 ** part of system initialization by the sqlite3_initialize() function.
  6033 ** ^The xMutexInit routine is called by SQLite exactly once for each
  6034 ** effective call to [sqlite3_initialize()].
  6035 **
  6036 ** ^The xMutexEnd method defined by this structure is invoked as
  6037 ** part of system shutdown by the sqlite3_shutdown() function. The
  6038 ** implementation of this method is expected to release all outstanding
  6039 ** resources obtained by the mutex methods implementation, especially
  6040 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
  6041 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  6042 **
  6043 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  6044 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  6045 ** xMutexNotheld) implement the following interfaces (respectively):
  6046 **
  6047 ** <ul>
  6048 **   <li>  [sqlite3_mutex_alloc()] </li>
  6049 **   <li>  [sqlite3_mutex_free()] </li>
  6050 **   <li>  [sqlite3_mutex_enter()] </li>
  6051 **   <li>  [sqlite3_mutex_try()] </li>
  6052 **   <li>  [sqlite3_mutex_leave()] </li>
  6053 **   <li>  [sqlite3_mutex_held()] </li>
  6054 **   <li>  [sqlite3_mutex_notheld()] </li>
  6055 ** </ul>)^
  6056 **
  6057 ** The only difference is that the public sqlite3_XXX functions enumerated
  6058 ** above silently ignore any invocations that pass a NULL pointer instead
  6059 ** of a valid mutex handle. The implementations of the methods defined
  6060 ** by this structure are not required to handle this case, the results
  6061 ** of passing a NULL pointer instead of a valid mutex handle are undefined
  6062 ** (i.e. it is acceptable to provide an implementation that segfaults if
  6063 ** it is passed a NULL pointer).
  6064 **
  6065 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
  6066 ** invoke xMutexInit() multiple times within the same process and without
  6067 ** intervening calls to xMutexEnd().  Second and subsequent calls to
  6068 ** xMutexInit() must be no-ops.
  6069 **
  6070 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
  6071 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
  6072 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
  6073 ** memory allocation for a fast or recursive mutex.
  6074 **
  6075 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  6076 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
  6077 ** If xMutexInit fails in any way, it is expected to clean up after itself
  6078 ** prior to returning.
  6079 */
  6080 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
  6081 struct sqlite3_mutex_methods {
  6082   int (*xMutexInit)(void);
  6083   int (*xMutexEnd)(void);
  6084   sqlite3_mutex *(*xMutexAlloc)(int);
  6085   void (*xMutexFree)(sqlite3_mutex *);
  6086   void (*xMutexEnter)(sqlite3_mutex *);
  6087   int (*xMutexTry)(sqlite3_mutex *);
  6088   void (*xMutexLeave)(sqlite3_mutex *);
  6089   int (*xMutexHeld)(sqlite3_mutex *);
  6090   int (*xMutexNotheld)(sqlite3_mutex *);
  6091 };
  6093 /*
  6094 ** CAPI3REF: Mutex Verification Routines
  6095 **
  6096 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
  6097 ** are intended for use inside assert() statements.  ^The SQLite core
  6098 ** never uses these routines except inside an assert() and applications
  6099 ** are advised to follow the lead of the core.  ^The SQLite core only
  6100 ** provides implementations for these routines when it is compiled
  6101 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
  6102 ** are only required to provide these routines if SQLITE_DEBUG is
  6103 ** defined and if NDEBUG is not defined.
  6104 **
  6105 ** ^These routines should return true if the mutex in their argument
  6106 ** is held or not held, respectively, by the calling thread.
  6107 **
  6108 ** ^The implementation is not required to provide versions of these
  6109 ** routines that actually work. If the implementation does not provide working
  6110 ** versions of these routines, it should at least provide stubs that always
  6111 ** return true so that one does not get spurious assertion failures.
  6112 **
  6113 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
  6114 ** the routine should return 1.   This seems counter-intuitive since
  6115 ** clearly the mutex cannot be held if it does not exist.  But
  6116 ** the reason the mutex does not exist is because the build is not
  6117 ** using mutexes.  And we do not want the assert() containing the
  6118 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
  6119 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
  6120 ** interface should also return 1 when given a NULL pointer.
  6121 */
  6122 #ifndef NDEBUG
  6123 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
  6124 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
  6125 #endif
  6127 /*
  6128 ** CAPI3REF: Mutex Types
  6129 **
  6130 ** The [sqlite3_mutex_alloc()] interface takes a single argument
  6131 ** which is one of these integer constants.
  6132 **
  6133 ** The set of static mutexes may change from one SQLite release to the
  6134 ** next.  Applications that override the built-in mutex logic must be
  6135 ** prepared to accommodate additional static mutexes.
  6136 */
  6137 #define SQLITE_MUTEX_FAST             0
  6138 #define SQLITE_MUTEX_RECURSIVE        1
  6139 #define SQLITE_MUTEX_STATIC_MASTER    2
  6140 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6141 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6142 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6143 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  6144 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6145 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6146 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
  6148 /*
  6149 ** CAPI3REF: Retrieve the mutex for a database connection
  6150 **
  6151 ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6152 ** serializes access to the [database connection] given in the argument
  6153 ** when the [threading mode] is Serialized.
  6154 ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6155 ** routine returns a NULL pointer.
  6156 */
  6157 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  6159 /*
  6160 ** CAPI3REF: Low-Level Control Of Database Files
  6161 **
  6162 ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6163 ** xFileControl method for the [sqlite3_io_methods] object associated
  6164 ** with a particular database identified by the second argument. ^The
  6165 ** name of the database is "main" for the main database or "temp" for the
  6166 ** TEMP database, or the name that appears after the AS keyword for
  6167 ** databases that are added using the [ATTACH] SQL command.
  6168 ** ^A NULL pointer can be used in place of "main" to refer to the
  6169 ** main database file.
  6170 ** ^The third and fourth parameters to this routine
  6171 ** are passed directly through to the second and third parameters of
  6172 ** the xFileControl method.  ^The return value of the xFileControl
  6173 ** method becomes the return value of this routine.
  6174 **
  6175 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
  6176 ** a pointer to the underlying [sqlite3_file] object to be written into
  6177 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
  6178 ** case is a short-circuit path which does not actually invoke the
  6179 ** underlying sqlite3_io_methods.xFileControl method.
  6180 **
  6181 ** ^If the second parameter (zDbName) does not match the name of any
  6182 ** open database file, then SQLITE_ERROR is returned.  ^This error
  6183 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
  6184 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
  6185 ** also return SQLITE_ERROR.  There is no way to distinguish between
  6186 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  6187 ** xFileControl method.
  6188 **
  6189 ** See also: [SQLITE_FCNTL_LOCKSTATE]
  6190 */
  6191 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  6193 /*
  6194 ** CAPI3REF: Testing Interface
  6195 **
  6196 ** ^The sqlite3_test_control() interface is used to read out internal
  6197 ** state of SQLite and to inject faults into SQLite for testing
  6198 ** purposes.  ^The first parameter is an operation code that determines
  6199 ** the number, meaning, and operation of all subsequent parameters.
  6200 **
  6201 ** This interface is not for use by applications.  It exists solely
  6202 ** for verifying the correct operation of the SQLite library.  Depending
  6203 ** on how the SQLite library is compiled, this interface might not exist.
  6204 **
  6205 ** The details of the operation codes, their meanings, the parameters
  6206 ** they take, and what they do are all subject to change without notice.
  6207 ** Unlike most of the SQLite API, this function is not guaranteed to
  6208 ** operate consistently from one release to the next.
  6209 */
  6210 SQLITE_API int sqlite3_test_control(int op, ...);
  6212 /*
  6213 ** CAPI3REF: Testing Interface Operation Codes
  6214 **
  6215 ** These constants are the valid operation code parameters used
  6216 ** as the first argument to [sqlite3_test_control()].
  6217 **
  6218 ** These parameters and their meanings are subject to change
  6219 ** without notice.  These values are for testing purposes only.
  6220 ** Applications should not use any of these parameters or the
  6221 ** [sqlite3_test_control()] interface.
  6222 */
  6223 #define SQLITE_TESTCTRL_FIRST                    5
  6224 #define SQLITE_TESTCTRL_PRNG_SAVE                5
  6225 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
  6226 #define SQLITE_TESTCTRL_PRNG_RESET               7
  6227 #define SQLITE_TESTCTRL_BITVEC_TEST              8
  6228 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  6229 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  6230 #define SQLITE_TESTCTRL_PENDING_BYTE            11
  6231 #define SQLITE_TESTCTRL_ASSERT                  12
  6232 #define SQLITE_TESTCTRL_ALWAYS                  13
  6233 #define SQLITE_TESTCTRL_RESERVE                 14
  6234 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6235 #define SQLITE_TESTCTRL_ISKEYWORD               16
  6236 #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6237 #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6238 #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6239 #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6240 #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  6241 #define SQLITE_TESTCTRL_LAST                    21
  6243 /*
  6244 ** CAPI3REF: SQLite Runtime Status
  6245 **
  6246 ** ^This interface is used to retrieve runtime status information
  6247 ** about the performance of SQLite, and optionally to reset various
  6248 ** highwater marks.  ^The first argument is an integer code for
  6249 ** the specific parameter to measure.  ^(Recognized integer codes
  6250 ** are of the form [status parameters | SQLITE_STATUS_...].)^
  6251 ** ^The current value of the parameter is returned into *pCurrent.
  6252 ** ^The highest recorded value is returned in *pHighwater.  ^If the
  6253 ** resetFlag is true, then the highest record value is reset after
  6254 ** *pHighwater is written.  ^(Some parameters do not record the highest
  6255 ** value.  For those parameters
  6256 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
  6257 ** ^(Other parameters record only the highwater mark and not the current
  6258 ** value.  For these latter parameters nothing is written into *pCurrent.)^
  6259 **
  6260 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
  6261 ** non-zero [error code] on failure.
  6262 **
  6263 ** This routine is threadsafe but is not atomic.  This routine can be
  6264 ** called while other threads are running the same or different SQLite
  6265 ** interfaces.  However the values returned in *pCurrent and
  6266 ** *pHighwater reflect the status of SQLite at different points in time
  6267 ** and it is possible that another thread might change the parameter
  6268 ** in between the times when *pCurrent and *pHighwater are written.
  6269 **
  6270 ** See also: [sqlite3_db_status()]
  6271 */
  6272 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
  6275 /*
  6276 ** CAPI3REF: Status Parameters
  6277 ** KEYWORDS: {status parameters}
  6278 **
  6279 ** These integer constants designate various run-time status parameters
  6280 ** that can be returned by [sqlite3_status()].
  6281 **
  6282 ** <dl>
  6283 ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  6284 ** <dd>This parameter is the current amount of memory checked out
  6285 ** using [sqlite3_malloc()], either directly or indirectly.  The
  6286 ** figure includes calls made to [sqlite3_malloc()] by the application
  6287 ** and internal memory usage by the SQLite library.  Scratch memory
  6288 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
  6289 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  6290 ** this parameter.  The amount returned is the sum of the allocation
  6291 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  6292 **
  6293 ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  6294 ** <dd>This parameter records the largest memory allocation request
  6295 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  6296 ** internal equivalents).  Only the value returned in the
  6297 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6298 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6299 **
  6300 ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
  6301 ** <dd>This parameter records the number of separate memory allocations
  6302 ** currently checked out.</dd>)^
  6303 **
  6304 ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  6305 ** <dd>This parameter returns the number of pages used out of the
  6306 ** [pagecache memory allocator] that was configured using 
  6307 ** [SQLITE_CONFIG_PAGECACHE].  The
  6308 ** value returned is in pages, not in bytes.</dd>)^
  6309 **
  6310 ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] 
  6311 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
  6312 ** <dd>This parameter returns the number of bytes of page cache
  6313 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
  6314 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
  6315 ** returned value includes allocations that overflowed because they
  6316 ** where too large (they were larger than the "sz" parameter to
  6317 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
  6318 ** no space was left in the page cache.</dd>)^
  6319 **
  6320 ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  6321 ** <dd>This parameter records the largest memory allocation request
  6322 ** handed to [pagecache memory allocator].  Only the value returned in the
  6323 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6324 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6325 **
  6326 ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  6327 ** <dd>This parameter returns the number of allocations used out of the
  6328 ** [scratch memory allocator] configured using
  6329 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
  6330 ** in bytes.  Since a single thread may only have one scratch allocation
  6331 ** outstanding at time, this parameter also reports the number of threads
  6332 ** using scratch memory at the same time.</dd>)^
  6333 **
  6334 ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  6335 ** <dd>This parameter returns the number of bytes of scratch memory
  6336 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
  6337 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
  6338 ** returned include overflows because the requested allocation was too
  6339 ** larger (that is, because the requested allocation was larger than the
  6340 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  6341 ** slots were available.
  6342 ** </dd>)^
  6343 **
  6344 ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  6345 ** <dd>This parameter records the largest memory allocation request
  6346 ** handed to [scratch memory allocator].  Only the value returned in the
  6347 ** *pHighwater parameter to [sqlite3_status()] is of interest.  
  6348 ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6349 **
  6350 ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  6351 ** <dd>This parameter records the deepest parser stack.  It is only
  6352 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  6353 ** </dl>
  6354 **
  6355 ** New status parameters may be added from time to time.
  6356 */
  6357 #define SQLITE_STATUS_MEMORY_USED          0
  6358 #define SQLITE_STATUS_PAGECACHE_USED       1
  6359 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
  6360 #define SQLITE_STATUS_SCRATCH_USED         3
  6361 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
  6362 #define SQLITE_STATUS_MALLOC_SIZE          5
  6363 #define SQLITE_STATUS_PARSER_STACK         6
  6364 #define SQLITE_STATUS_PAGECACHE_SIZE       7
  6365 #define SQLITE_STATUS_SCRATCH_SIZE         8
  6366 #define SQLITE_STATUS_MALLOC_COUNT         9
  6368 /*
  6369 ** CAPI3REF: Database Connection Status
  6370 **
  6371 ** ^This interface is used to retrieve runtime status information 
  6372 ** about a single [database connection].  ^The first argument is the
  6373 ** database connection object to be interrogated.  ^The second argument
  6374 ** is an integer constant, taken from the set of
  6375 ** [SQLITE_DBSTATUS options], that
  6376 ** determines the parameter to interrogate.  The set of 
  6377 ** [SQLITE_DBSTATUS options] is likely
  6378 ** to grow in future releases of SQLite.
  6379 **
  6380 ** ^The current value of the requested parameter is written into *pCur
  6381 ** and the highest instantaneous value is written into *pHiwtr.  ^If
  6382 ** the resetFlg is true, then the highest instantaneous value is
  6383 ** reset back down to the current value.
  6384 **
  6385 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
  6386 ** non-zero [error code] on failure.
  6387 **
  6388 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
  6389 */
  6390 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  6392 /*
  6393 ** CAPI3REF: Status Parameters for database connections
  6394 ** KEYWORDS: {SQLITE_DBSTATUS options}
  6395 **
  6396 ** These constants are the available integer "verbs" that can be passed as
  6397 ** the second argument to the [sqlite3_db_status()] interface.
  6398 **
  6399 ** New verbs may be added in future releases of SQLite. Existing verbs
  6400 ** might be discontinued. Applications should check the return code from
  6401 ** [sqlite3_db_status()] to make sure that the call worked.
  6402 ** The [sqlite3_db_status()] interface will return a non-zero error code
  6403 ** if a discontinued or unsupported verb is invoked.
  6404 **
  6405 ** <dl>
  6406 ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  6407 ** <dd>This parameter returns the number of lookaside memory slots currently
  6408 ** checked out.</dd>)^
  6409 **
  6410 ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
  6411 ** <dd>This parameter returns the number malloc attempts that were 
  6412 ** satisfied using lookaside memory. Only the high-water value is meaningful;
  6413 ** the current value is always zero.)^
  6414 **
  6415 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
  6416 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
  6417 ** <dd>This parameter returns the number malloc attempts that might have
  6418 ** been satisfied using lookaside memory but failed due to the amount of
  6419 ** memory requested being larger than the lookaside slot size.
  6420 ** Only the high-water value is meaningful;
  6421 ** the current value is always zero.)^
  6422 **
  6423 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
  6424 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
  6425 ** <dd>This parameter returns the number malloc attempts that might have
  6426 ** been satisfied using lookaside memory but failed due to all lookaside
  6427 ** memory already being in use.
  6428 ** Only the high-water value is meaningful;
  6429 ** the current value is always zero.)^
  6430 **
  6431 ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  6432 ** <dd>This parameter returns the approximate number of of bytes of heap
  6433 ** memory used by all pager caches associated with the database connection.)^
  6434 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  6435 **
  6436 ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  6437 ** <dd>This parameter returns the approximate number of of bytes of heap
  6438 ** memory used to store the schema for all databases associated
  6439 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
  6440 ** ^The full amount of memory used by the schemas is reported, even if the
  6441 ** schema memory is shared with other database connections due to
  6442 ** [shared cache mode] being enabled.
  6443 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
  6444 **
  6445 ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  6446 ** <dd>This parameter returns the approximate number of of bytes of heap
  6447 ** and lookaside memory used by all prepared statements associated with
  6448 ** the database connection.)^
  6449 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  6450 ** </dd>
  6451 **
  6452 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
  6453 ** <dd>This parameter returns the number of pager cache hits that have
  6454 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
  6455 ** is always 0.
  6456 ** </dd>
  6457 **
  6458 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  6459 ** <dd>This parameter returns the number of pager cache misses that have
  6460 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
  6461 ** is always 0.
  6462 ** </dd>
  6463 **
  6464 ** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
  6465 ** <dd>This parameter returns the number of dirty cache entries that have
  6466 ** been written to disk. Specifically, the number of pages written to the
  6467 ** wal file in wal mode databases, or the number of pages written to the
  6468 ** database file in rollback mode databases. Any pages written as part of
  6469 ** transaction rollback or database recovery operations are not included.
  6470 ** If an IO or other error occurs while writing a page to disk, the effect
  6471 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  6472 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  6473 ** </dd>
  6474 **
  6475 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  6476 ** <dd>This parameter returns zero for the current value if and only if
  6477 ** all foreign key constraints (deferred or immediate) have been
  6478 ** resolved.)^  ^The highwater mark is always 0.
  6479 ** </dd>
  6480 ** </dl>
  6481 */
  6482 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6483 #define SQLITE_DBSTATUS_CACHE_USED           1
  6484 #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6485 #define SQLITE_DBSTATUS_STMT_USED            3
  6486 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  6487 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  6488 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  6489 #define SQLITE_DBSTATUS_CACHE_HIT            7
  6490 #define SQLITE_DBSTATUS_CACHE_MISS           8
  6491 #define SQLITE_DBSTATUS_CACHE_WRITE          9
  6492 #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  6493 #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
  6496 /*
  6497 ** CAPI3REF: Prepared Statement Status
  6498 **
  6499 ** ^(Each prepared statement maintains various
  6500 ** [SQLITE_STMTSTATUS counters] that measure the number
  6501 ** of times it has performed specific operations.)^  These counters can
  6502 ** be used to monitor the performance characteristics of the prepared
  6503 ** statements.  For example, if the number of table steps greatly exceeds
  6504 ** the number of table searches or result rows, that would tend to indicate
  6505 ** that the prepared statement is using a full table scan rather than
  6506 ** an index.  
  6507 **
  6508 ** ^(This interface is used to retrieve and reset counter values from
  6509 ** a [prepared statement].  The first argument is the prepared statement
  6510 ** object to be interrogated.  The second argument
  6511 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
  6512 ** to be interrogated.)^
  6513 ** ^The current value of the requested counter is returned.
  6514 ** ^If the resetFlg is true, then the counter is reset to zero after this
  6515 ** interface call returns.
  6516 **
  6517 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
  6518 */
  6519 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
  6521 /*
  6522 ** CAPI3REF: Status Parameters for prepared statements
  6523 ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
  6524 **
  6525 ** These preprocessor macros define integer codes that name counter
  6526 ** values associated with the [sqlite3_stmt_status()] interface.
  6527 ** The meanings of the various counters are as follows:
  6528 **
  6529 ** <dl>
  6530 ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
  6531 ** <dd>^This is the number of times that SQLite has stepped forward in
  6532 ** a table as part of a full table scan.  Large numbers for this counter
  6533 ** may indicate opportunities for performance improvement through 
  6534 ** careful use of indices.</dd>
  6535 **
  6536 ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
  6537 ** <dd>^This is the number of sort operations that have occurred.
  6538 ** A non-zero value in this counter may indicate an opportunity to
  6539 ** improvement performance through careful use of indices.</dd>
  6540 **
  6541 ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  6542 ** <dd>^This is the number of rows inserted into transient indices that
  6543 ** were created automatically in order to help joins run faster.
  6544 ** A non-zero value in this counter may indicate an opportunity to
  6545 ** improvement performance by adding permanent indices that do not
  6546 ** need to be reinitialized each time the statement is run.</dd>
  6547 **
  6548 ** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
  6549 ** <dd>^This is the number of virtual machine operations executed
  6550 ** by the prepared statement if that number is less than or equal
  6551 ** to 2147483647.  The number of virtual machine operations can be 
  6552 ** used as a proxy for the total work done by the prepared statement.
  6553 ** If the number of virtual machine operations exceeds 2147483647
  6554 ** then the value returned by this statement status code is undefined.
  6555 ** </dd>
  6556 ** </dl>
  6557 */
  6558 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  6559 #define SQLITE_STMTSTATUS_SORT              2
  6560 #define SQLITE_STMTSTATUS_AUTOINDEX         3
  6561 #define SQLITE_STMTSTATUS_VM_STEP           4
  6563 /*
  6564 ** CAPI3REF: Custom Page Cache Object
  6565 **
  6566 ** The sqlite3_pcache type is opaque.  It is implemented by
  6567 ** the pluggable module.  The SQLite core has no knowledge of
  6568 ** its size or internal structure and never deals with the
  6569 ** sqlite3_pcache object except by holding and passing pointers
  6570 ** to the object.
  6571 **
  6572 ** See [sqlite3_pcache_methods2] for additional information.
  6573 */
  6574 typedef struct sqlite3_pcache sqlite3_pcache;
  6576 /*
  6577 ** CAPI3REF: Custom Page Cache Object
  6578 **
  6579 ** The sqlite3_pcache_page object represents a single page in the
  6580 ** page cache.  The page cache will allocate instances of this
  6581 ** object.  Various methods of the page cache use pointers to instances
  6582 ** of this object as parameters or as their return value.
  6583 **
  6584 ** See [sqlite3_pcache_methods2] for additional information.
  6585 */
  6586 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
  6587 struct sqlite3_pcache_page {
  6588   void *pBuf;        /* The content of the page */
  6589   void *pExtra;      /* Extra information associated with the page */
  6590 };
  6592 /*
  6593 ** CAPI3REF: Application Defined Page Cache.
  6594 ** KEYWORDS: {page cache}
  6595 **
  6596 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
  6597 ** register an alternative page cache implementation by passing in an 
  6598 ** instance of the sqlite3_pcache_methods2 structure.)^
  6599 ** In many applications, most of the heap memory allocated by 
  6600 ** SQLite is used for the page cache.
  6601 ** By implementing a 
  6602 ** custom page cache using this API, an application can better control
  6603 ** the amount of memory consumed by SQLite, the way in which 
  6604 ** that memory is allocated and released, and the policies used to 
  6605 ** determine exactly which parts of a database file are cached and for 
  6606 ** how long.
  6607 **
  6608 ** The alternative page cache mechanism is an
  6609 ** extreme measure that is only needed by the most demanding applications.
  6610 ** The built-in page cache is recommended for most uses.
  6611 **
  6612 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
  6613 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
  6614 ** the application may discard the parameter after the call to
  6615 ** [sqlite3_config()] returns.)^
  6616 **
  6617 ** [[the xInit() page cache method]]
  6618 ** ^(The xInit() method is called once for each effective 
  6619 ** call to [sqlite3_initialize()])^
  6620 ** (usually only once during the lifetime of the process). ^(The xInit()
  6621 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
  6622 ** The intent of the xInit() method is to set up global data structures 
  6623 ** required by the custom page cache implementation. 
  6624 ** ^(If the xInit() method is NULL, then the 
  6625 ** built-in default page cache is used instead of the application defined
  6626 ** page cache.)^
  6627 **
  6628 ** [[the xShutdown() page cache method]]
  6629 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
  6630 ** It can be used to clean up 
  6631 ** any outstanding resources before process shutdown, if required.
  6632 ** ^The xShutdown() method may be NULL.
  6633 **
  6634 ** ^SQLite automatically serializes calls to the xInit method,
  6635 ** so the xInit method need not be threadsafe.  ^The
  6636 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  6637 ** not need to be threadsafe either.  All other methods must be threadsafe
  6638 ** in multithreaded applications.
  6639 **
  6640 ** ^SQLite will never invoke xInit() more than once without an intervening
  6641 ** call to xShutdown().
  6642 **
  6643 ** [[the xCreate() page cache methods]]
  6644 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
  6645 ** SQLite will typically create one cache instance for each open database file,
  6646 ** though this is not guaranteed. ^The
  6647 ** first parameter, szPage, is the size in bytes of the pages that must
  6648 ** be allocated by the cache.  ^szPage will always a power of two.  ^The
  6649 ** second parameter szExtra is a number of bytes of extra storage 
  6650 ** associated with each page cache entry.  ^The szExtra parameter will
  6651 ** a number less than 250.  SQLite will use the
  6652 ** extra szExtra bytes on each page to store metadata about the underlying
  6653 ** database page on disk.  The value passed into szExtra depends
  6654 ** on the SQLite version, the target platform, and how SQLite was compiled.
  6655 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
  6656 ** created will be used to cache database pages of a file stored on disk, or
  6657 ** false if it is used for an in-memory database. The cache implementation
  6658 ** does not have to do anything special based with the value of bPurgeable;
  6659 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
  6660 ** never invoke xUnpin() except to deliberately delete a page.
  6661 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
  6662 ** false will always have the "discard" flag set to true.  
  6663 ** ^Hence, a cache created with bPurgeable false will
  6664 ** never contain any unpinned pages.
  6665 **
  6666 ** [[the xCachesize() page cache method]]
  6667 ** ^(The xCachesize() method may be called at any time by SQLite to set the
  6668 ** suggested maximum cache-size (number of pages stored by) the cache
  6669 ** instance passed as the first argument. This is the value configured using
  6670 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
  6671 ** parameter, the implementation is not required to do anything with this
  6672 ** value; it is advisory only.
  6673 **
  6674 ** [[the xPagecount() page cache methods]]
  6675 ** The xPagecount() method must return the number of pages currently
  6676 ** stored in the cache, both pinned and unpinned.
  6677 ** 
  6678 ** [[the xFetch() page cache methods]]
  6679 ** The xFetch() method locates a page in the cache and returns a pointer to 
  6680 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
  6681 ** The pBuf element of the returned sqlite3_pcache_page object will be a
  6682 ** pointer to a buffer of szPage bytes used to store the content of a 
  6683 ** single database page.  The pExtra element of sqlite3_pcache_page will be
  6684 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
  6685 ** for each entry in the page cache.
  6686 **
  6687 ** The page to be fetched is determined by the key. ^The minimum key value
  6688 ** is 1.  After it has been retrieved using xFetch, the page is considered
  6689 ** to be "pinned".
  6690 **
  6691 ** If the requested page is already in the page cache, then the page cache
  6692 ** implementation must return a pointer to the page buffer with its content
  6693 ** intact.  If the requested page is not already in the cache, then the
  6694 ** cache implementation should use the value of the createFlag
  6695 ** parameter to help it determined what action to take:
  6696 **
  6697 ** <table border=1 width=85% align=center>
  6698 ** <tr><th> createFlag <th> Behavior when page is not already in cache
  6699 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
  6700 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  6701 **                 Otherwise return NULL.
  6702 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
  6703 **                 NULL if allocating a new page is effectively impossible.
  6704 ** </table>
  6705 **
  6706 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
  6707 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
  6708 ** failed.)^  In between the to xFetch() calls, SQLite may
  6709 ** attempt to unpin one or more cache pages by spilling the content of
  6710 ** pinned pages to disk and synching the operating system disk cache.
  6711 **
  6712 ** [[the xUnpin() page cache method]]
  6713 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
  6714 ** as its second argument.  If the third parameter, discard, is non-zero,
  6715 ** then the page must be evicted from the cache.
  6716 ** ^If the discard parameter is
  6717 ** zero, then the page may be discarded or retained at the discretion of
  6718 ** page cache implementation. ^The page cache implementation
  6719 ** may choose to evict unpinned pages at any time.
  6720 **
  6721 ** The cache must not perform any reference counting. A single 
  6722 ** call to xUnpin() unpins the page regardless of the number of prior calls 
  6723 ** to xFetch().
  6724 **
  6725 ** [[the xRekey() page cache methods]]
  6726 ** The xRekey() method is used to change the key value associated with the
  6727 ** page passed as the second argument. If the cache
  6728 ** previously contains an entry associated with newKey, it must be
  6729 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
  6730 ** to be pinned.
  6731 **
  6732 ** When SQLite calls the xTruncate() method, the cache must discard all
  6733 ** existing cache entries with page numbers (keys) greater than or equal
  6734 ** to the value of the iLimit parameter passed to xTruncate(). If any
  6735 ** of these pages are pinned, they are implicitly unpinned, meaning that
  6736 ** they can be safely discarded.
  6737 **
  6738 ** [[the xDestroy() page cache method]]
  6739 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
  6740 ** All resources associated with the specified cache should be freed. ^After
  6741 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  6742 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
  6743 ** functions.
  6744 **
  6745 ** [[the xShrink() page cache method]]
  6746 ** ^SQLite invokes the xShrink() method when it wants the page cache to
  6747 ** free up as much of heap memory as possible.  The page cache implementation
  6748 ** is not obligated to free any memory, but well-behaved implementations should
  6749 ** do their best.
  6750 */
  6751 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
  6752 struct sqlite3_pcache_methods2 {
  6753   int iVersion;
  6754   void *pArg;
  6755   int (*xInit)(void*);
  6756   void (*xShutdown)(void*);
  6757   sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
  6758   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  6759   int (*xPagecount)(sqlite3_pcache*);
  6760   sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  6761   void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
  6762   void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, 
  6763       unsigned oldKey, unsigned newKey);
  6764   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  6765   void (*xDestroy)(sqlite3_pcache*);
  6766   void (*xShrink)(sqlite3_pcache*);
  6767 };
  6769 /*
  6770 ** This is the obsolete pcache_methods object that has now been replaced
  6771 ** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
  6772 ** retained in the header file for backwards compatibility only.
  6773 */
  6774 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
  6775 struct sqlite3_pcache_methods {
  6776   void *pArg;
  6777   int (*xInit)(void*);
  6778   void (*xShutdown)(void*);
  6779   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
  6780   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  6781   int (*xPagecount)(sqlite3_pcache*);
  6782   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  6783   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
  6784   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
  6785   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  6786   void (*xDestroy)(sqlite3_pcache*);
  6787 };
  6790 /*
  6791 ** CAPI3REF: Online Backup Object
  6792 **
  6793 ** The sqlite3_backup object records state information about an ongoing
  6794 ** online backup operation.  ^The sqlite3_backup object is created by
  6795 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
  6796 ** [sqlite3_backup_finish()].
  6797 **
  6798 ** See Also: [Using the SQLite Online Backup API]
  6799 */
  6800 typedef struct sqlite3_backup sqlite3_backup;
  6802 /*
  6803 ** CAPI3REF: Online Backup API.
  6804 **
  6805 ** The backup API copies the content of one database into another.
  6806 ** It is useful either for creating backups of databases or
  6807 ** for copying in-memory databases to or from persistent files. 
  6808 **
  6809 ** See Also: [Using the SQLite Online Backup API]
  6810 **
  6811 ** ^SQLite holds a write transaction open on the destination database file
  6812 ** for the duration of the backup operation.
  6813 ** ^The source database is read-locked only while it is being read;
  6814 ** it is not locked continuously for the entire backup operation.
  6815 ** ^Thus, the backup may be performed on a live source database without
  6816 ** preventing other database connections from
  6817 ** reading or writing to the source database while the backup is underway.
  6818 ** 
  6819 ** ^(To perform a backup operation: 
  6820 **   <ol>
  6821 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
  6822 **         backup, 
  6823 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
  6824 **         the data between the two databases, and finally
  6825 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
  6826 **         associated with the backup operation. 
  6827 **   </ol>)^
  6828 ** There should be exactly one call to sqlite3_backup_finish() for each
  6829 ** successful call to sqlite3_backup_init().
  6830 **
  6831 ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
  6832 **
  6833 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 
  6834 ** [database connection] associated with the destination database 
  6835 ** and the database name, respectively.
  6836 ** ^The database name is "main" for the main database, "temp" for the
  6837 ** temporary database, or the name specified after the AS keyword in
  6838 ** an [ATTACH] statement for an attached database.
  6839 ** ^The S and M arguments passed to 
  6840 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  6841 ** and database name of the source database, respectively.
  6842 ** ^The source and destination [database connections] (parameters S and D)
  6843 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
  6844 ** an error.
  6845 **
  6846 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  6847 ** returned and an error code and error message are stored in the
  6848 ** destination [database connection] D.
  6849 ** ^The error code and message for the failed call to sqlite3_backup_init()
  6850 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  6851 ** [sqlite3_errmsg16()] functions.
  6852 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
  6853 ** [sqlite3_backup] object.
  6854 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
  6855 ** sqlite3_backup_finish() functions to perform the specified backup 
  6856 ** operation.
  6857 **
  6858 ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
  6859 **
  6860 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
  6861 ** the source and destination databases specified by [sqlite3_backup] object B.
  6862 ** ^If N is negative, all remaining source pages are copied. 
  6863 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  6864 ** are still more pages to be copied, then the function returns [SQLITE_OK].
  6865 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  6866 ** from source to destination, then it returns [SQLITE_DONE].
  6867 ** ^If an error occurs while running sqlite3_backup_step(B,N),
  6868 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
  6869 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  6870 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  6871 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  6872 **
  6873 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
  6874 ** <ol>
  6875 ** <li> the destination database was opened read-only, or
  6876 ** <li> the destination database is using write-ahead-log journaling
  6877 ** and the destination and source page sizes differ, or
  6878 ** <li> the destination database is an in-memory database and the
  6879 ** destination and source page sizes differ.
  6880 ** </ol>)^
  6881 **
  6882 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  6883 ** the [sqlite3_busy_handler | busy-handler function]
  6884 ** is invoked (if one is specified). ^If the 
  6885 ** busy-handler returns non-zero before the lock is available, then 
  6886 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
  6887 ** sqlite3_backup_step() can be retried later. ^If the source
  6888 ** [database connection]
  6889 ** is being used to write to the source database when sqlite3_backup_step()
  6890 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
  6891 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
  6892 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
  6893 ** [SQLITE_READONLY] is returned, then 
  6894 ** there is no point in retrying the call to sqlite3_backup_step(). These 
  6895 ** errors are considered fatal.)^  The application must accept 
  6896 ** that the backup operation has failed and pass the backup operation handle 
  6897 ** to the sqlite3_backup_finish() to release associated resources.
  6898 **
  6899 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
  6900 ** on the destination file. ^The exclusive lock is not released until either 
  6901 ** sqlite3_backup_finish() is called or the backup operation is complete 
  6902 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
  6903 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
  6904 ** lasts for the duration of the sqlite3_backup_step() call.
  6905 ** ^Because the source database is not locked between calls to
  6906 ** sqlite3_backup_step(), the source database may be modified mid-way
  6907 ** through the backup process.  ^If the source database is modified by an
  6908 ** external process or via a database connection other than the one being
  6909 ** used by the backup operation, then the backup will be automatically
  6910 ** restarted by the next call to sqlite3_backup_step(). ^If the source 
  6911 ** database is modified by the using the same database connection as is used
  6912 ** by the backup operation, then the backup database is automatically
  6913 ** updated at the same time.
  6914 **
  6915 ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
  6916 **
  6917 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
  6918 ** application wishes to abandon the backup operation, the application
  6919 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
  6920 ** ^The sqlite3_backup_finish() interfaces releases all
  6921 ** resources associated with the [sqlite3_backup] object. 
  6922 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
  6923 ** active write-transaction on the destination database is rolled back.
  6924 ** The [sqlite3_backup] object is invalid
  6925 ** and may not be used following a call to sqlite3_backup_finish().
  6926 **
  6927 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
  6928 ** sqlite3_backup_step() errors occurred, regardless or whether or not
  6929 ** sqlite3_backup_step() completed.
  6930 ** ^If an out-of-memory condition or IO error occurred during any prior
  6931 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
  6932 ** sqlite3_backup_finish() returns the corresponding [error code].
  6933 **
  6934 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
  6935 ** is not a permanent error and does not affect the return value of
  6936 ** sqlite3_backup_finish().
  6937 **
  6938 ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
  6939 ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
  6940 **
  6941 ** ^Each call to sqlite3_backup_step() sets two values inside
  6942 ** the [sqlite3_backup] object: the number of pages still to be backed
  6943 ** up and the total number of pages in the source database file.
  6944 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
  6945 ** retrieve these two values, respectively.
  6946 **
  6947 ** ^The values returned by these functions are only updated by
  6948 ** sqlite3_backup_step(). ^If the source database is modified during a backup
  6949 ** operation, then the values are not updated to account for any extra
  6950 ** pages that need to be updated or the size of the source database file
  6951 ** changing.
  6952 **
  6953 ** <b>Concurrent Usage of Database Handles</b>
  6954 **
  6955 ** ^The source [database connection] may be used by the application for other
  6956 ** purposes while a backup operation is underway or being initialized.
  6957 ** ^If SQLite is compiled and configured to support threadsafe database
  6958 ** connections, then the source database connection may be used concurrently
  6959 ** from within other threads.
  6960 **
  6961 ** However, the application must guarantee that the destination 
  6962 ** [database connection] is not passed to any other API (by any thread) after 
  6963 ** sqlite3_backup_init() is called and before the corresponding call to
  6964 ** sqlite3_backup_finish().  SQLite does not currently check to see
  6965 ** if the application incorrectly accesses the destination [database connection]
  6966 ** and so no error code is reported, but the operations may malfunction
  6967 ** nevertheless.  Use of the destination database connection while a
  6968 ** backup is in progress might also also cause a mutex deadlock.
  6969 **
  6970 ** If running in [shared cache mode], the application must
  6971 ** guarantee that the shared cache used by the destination database
  6972 ** is not accessed while the backup is running. In practice this means
  6973 ** that the application must guarantee that the disk file being 
  6974 ** backed up to is not accessed by any connection within the process,
  6975 ** not just the specific connection that was passed to sqlite3_backup_init().
  6976 **
  6977 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
  6978 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
  6979 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
  6980 ** APIs are not strictly speaking threadsafe. If they are invoked at the
  6981 ** same time as another thread is invoking sqlite3_backup_step() it is
  6982 ** possible that they return invalid values.
  6983 */
  6984 SQLITE_API sqlite3_backup *sqlite3_backup_init(
  6985   sqlite3 *pDest,                        /* Destination database handle */
  6986   const char *zDestName,                 /* Destination database name */
  6987   sqlite3 *pSource,                      /* Source database handle */
  6988   const char *zSourceName                /* Source database name */
  6989 );
  6990 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  6991 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
  6992 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
  6993 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
  6995 /*
  6996 ** CAPI3REF: Unlock Notification
  6997 **
  6998 ** ^When running in shared-cache mode, a database operation may fail with
  6999 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  7000 ** individual tables within the shared-cache cannot be obtained. See
  7001 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
  7002 ** ^This API may be used to register a callback that SQLite will invoke 
  7003 ** when the connection currently holding the required lock relinquishes it.
  7004 ** ^This API is only available if the library was compiled with the
  7005 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
  7006 **
  7007 ** See Also: [Using the SQLite Unlock Notification Feature].
  7008 **
  7009 ** ^Shared-cache locks are released when a database connection concludes
  7010 ** its current transaction, either by committing it or rolling it back. 
  7011 **
  7012 ** ^When a connection (known as the blocked connection) fails to obtain a
  7013 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
  7014 ** identity of the database connection (the blocking connection) that
  7015 ** has locked the required resource is stored internally. ^After an 
  7016 ** application receives an SQLITE_LOCKED error, it may call the
  7017 ** sqlite3_unlock_notify() method with the blocked connection handle as 
  7018 ** the first argument to register for a callback that will be invoked
  7019 ** when the blocking connections current transaction is concluded. ^The
  7020 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
  7021 ** call that concludes the blocking connections transaction.
  7022 **
  7023 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
  7024 ** there is a chance that the blocking connection will have already
  7025 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
  7026 ** If this happens, then the specified callback is invoked immediately,
  7027 ** from within the call to sqlite3_unlock_notify().)^
  7028 **
  7029 ** ^If the blocked connection is attempting to obtain a write-lock on a
  7030 ** shared-cache table, and more than one other connection currently holds
  7031 ** a read-lock on the same table, then SQLite arbitrarily selects one of 
  7032 ** the other connections to use as the blocking connection.
  7033 **
  7034 ** ^(There may be at most one unlock-notify callback registered by a 
  7035 ** blocked connection. If sqlite3_unlock_notify() is called when the
  7036 ** blocked connection already has a registered unlock-notify callback,
  7037 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
  7038 ** called with a NULL pointer as its second argument, then any existing
  7039 ** unlock-notify callback is canceled. ^The blocked connections 
  7040 ** unlock-notify callback may also be canceled by closing the blocked
  7041 ** connection using [sqlite3_close()].
  7042 **
  7043 ** The unlock-notify callback is not reentrant. If an application invokes
  7044 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
  7045 ** crash or deadlock may be the result.
  7046 **
  7047 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
  7048 ** returns SQLITE_OK.
  7049 **
  7050 ** <b>Callback Invocation Details</b>
  7051 **
  7052 ** When an unlock-notify callback is registered, the application provides a 
  7053 ** single void* pointer that is passed to the callback when it is invoked.
  7054 ** However, the signature of the callback function allows SQLite to pass
  7055 ** it an array of void* context pointers. The first argument passed to
  7056 ** an unlock-notify callback is a pointer to an array of void* pointers,
  7057 ** and the second is the number of entries in the array.
  7058 **
  7059 ** When a blocking connections transaction is concluded, there may be
  7060 ** more than one blocked connection that has registered for an unlock-notify
  7061 ** callback. ^If two or more such blocked connections have specified the
  7062 ** same callback function, then instead of invoking the callback function
  7063 ** multiple times, it is invoked once with the set of void* context pointers
  7064 ** specified by the blocked connections bundled together into an array.
  7065 ** This gives the application an opportunity to prioritize any actions 
  7066 ** related to the set of unblocked database connections.
  7067 **
  7068 ** <b>Deadlock Detection</b>
  7069 **
  7070 ** Assuming that after registering for an unlock-notify callback a 
  7071 ** database waits for the callback to be issued before taking any further
  7072 ** action (a reasonable assumption), then using this API may cause the
  7073 ** application to deadlock. For example, if connection X is waiting for
  7074 ** connection Y's transaction to be concluded, and similarly connection
  7075 ** Y is waiting on connection X's transaction, then neither connection
  7076 ** will proceed and the system may remain deadlocked indefinitely.
  7077 **
  7078 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
  7079 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
  7080 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
  7081 ** unlock-notify callback is registered. The system is said to be in
  7082 ** a deadlocked state if connection A has registered for an unlock-notify
  7083 ** callback on the conclusion of connection B's transaction, and connection
  7084 ** B has itself registered for an unlock-notify callback when connection
  7085 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
  7086 ** the system is also considered to be deadlocked if connection B has
  7087 ** registered for an unlock-notify callback on the conclusion of connection
  7088 ** C's transaction, where connection C is waiting on connection A. ^Any
  7089 ** number of levels of indirection are allowed.
  7090 **
  7091 ** <b>The "DROP TABLE" Exception</b>
  7092 **
  7093 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
  7094 ** always appropriate to call sqlite3_unlock_notify(). There is however,
  7095 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
  7096 ** SQLite checks if there are any currently executing SELECT statements
  7097 ** that belong to the same connection. If there are, SQLITE_LOCKED is
  7098 ** returned. In this case there is no "blocking connection", so invoking
  7099 ** sqlite3_unlock_notify() results in the unlock-notify callback being
  7100 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
  7101 ** or "DROP INDEX" query, an infinite loop might be the result.
  7102 **
  7103 ** One way around this problem is to check the extended error code returned
  7104 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
  7105 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
  7106 ** the special "DROP TABLE/INDEX" case, the extended error code is just 
  7107 ** SQLITE_LOCKED.)^
  7108 */
  7109 SQLITE_API int sqlite3_unlock_notify(
  7110   sqlite3 *pBlocked,                          /* Waiting connection */
  7111   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
  7112   void *pNotifyArg                            /* Argument to pass to xNotify */
  7113 );
  7116 /*
  7117 ** CAPI3REF: String Comparison
  7118 **
  7119 ** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
  7120 ** and extensions to compare the contents of two buffers containing UTF-8
  7121 ** strings in a case-independent fashion, using the same definition of "case
  7122 ** independence" that SQLite uses internally when comparing identifiers.
  7123 */
  7124 SQLITE_API int sqlite3_stricmp(const char *, const char *);
  7125 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
  7127 /*
  7128 ** CAPI3REF: String Globbing
  7130 ** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
  7131 ** the glob pattern P, and it returns non-zero if string X does not match
  7132 ** the glob pattern P.  ^The definition of glob pattern matching used in
  7133 ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
  7134 ** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
  7135 ** sensitive.
  7136 **
  7137 ** Note that this routine returns zero on a match and non-zero if the strings
  7138 ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
  7139 */
  7140 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
  7142 /*
  7143 ** CAPI3REF: Error Logging Interface
  7144 **
  7145 ** ^The [sqlite3_log()] interface writes a message into the [error log]
  7146 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
  7147 ** ^If logging is enabled, the zFormat string and subsequent arguments are
  7148 ** used with [sqlite3_snprintf()] to generate the final output string.
  7149 **
  7150 ** The sqlite3_log() interface is intended for use by extensions such as
  7151 ** virtual tables, collating functions, and SQL functions.  While there is
  7152 ** nothing to prevent an application from calling sqlite3_log(), doing so
  7153 ** is considered bad form.
  7154 **
  7155 ** The zFormat string must not be NULL.
  7156 **
  7157 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
  7158 ** will not use dynamically allocated memory.  The log message is stored in
  7159 ** a fixed-length buffer on the stack.  If the log message is longer than
  7160 ** a few hundred characters, it will be truncated to the length of the
  7161 ** buffer.
  7162 */
  7163 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
  7165 /*
  7166 ** CAPI3REF: Write-Ahead Log Commit Hook
  7167 **
  7168 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  7169 ** will be invoked each time a database connection commits data to a
  7170 ** [write-ahead log] (i.e. whenever a transaction is committed in
  7171 ** [journal_mode | journal_mode=WAL mode]). 
  7172 **
  7173 ** ^The callback is invoked by SQLite after the commit has taken place and 
  7174 ** the associated write-lock on the database released, so the implementation 
  7175 ** may read, write or [checkpoint] the database as required.
  7176 **
  7177 ** ^The first parameter passed to the callback function when it is invoked
  7178 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
  7179 ** registering the callback. ^The second is a copy of the database handle.
  7180 ** ^The third parameter is the name of the database that was written to -
  7181 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
  7182 ** is the number of pages currently in the write-ahead log file,
  7183 ** including those that were just committed.
  7184 **
  7185 ** The callback function should normally return [SQLITE_OK].  ^If an error
  7186 ** code is returned, that error will propagate back up through the
  7187 ** SQLite code base to cause the statement that provoked the callback
  7188 ** to report an error, though the commit will have still occurred. If the
  7189 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
  7190 ** that does not correspond to any valid SQLite error code, the results
  7191 ** are undefined.
  7192 **
  7193 ** A single database handle may have at most a single write-ahead log callback 
  7194 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
  7195 ** previously registered write-ahead log callback. ^Note that the
  7196 ** [sqlite3_wal_autocheckpoint()] interface and the
  7197 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
  7198 ** those overwrite any prior [sqlite3_wal_hook()] settings.
  7199 */
  7200 SQLITE_API void *sqlite3_wal_hook(
  7201   sqlite3*, 
  7202   int(*)(void *,sqlite3*,const char*,int),
  7203   void*
  7204 );
  7206 /*
  7207 ** CAPI3REF: Configure an auto-checkpoint
  7208 **
  7209 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  7210 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7211 ** to automatically [checkpoint]
  7212 ** after committing a transaction if there are N or
  7213 ** more frames in the [write-ahead log] file.  ^Passing zero or 
  7214 ** a negative value as the nFrame parameter disables automatic
  7215 ** checkpoints entirely.
  7216 **
  7217 ** ^The callback registered by this function replaces any existing callback
  7218 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
  7219 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  7220 ** configured by this function.
  7221 **
  7222 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  7223 ** from SQL.
  7224 **
  7225 ** ^Every new [database connection] defaults to having the auto-checkpoint
  7226 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
  7227 ** pages.  The use of this interface
  7228 ** is only necessary if the default setting is found to be suboptimal
  7229 ** for a particular application.
  7230 */
  7231 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7233 /*
  7234 ** CAPI3REF: Checkpoint a database
  7235 **
  7236 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  7237 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
  7238 ** empty string, then a checkpoint is run on all databases of
  7239 ** connection D.  ^If the database connection D is not in
  7240 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  7241 **
  7242 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
  7243 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  7244 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
  7245 ** run whenever the WAL reaches a certain size threshold.
  7246 **
  7247 ** See also: [sqlite3_wal_checkpoint_v2()]
  7248 */
  7249 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7251 /*
  7252 ** CAPI3REF: Checkpoint a database
  7253 **
  7254 ** Run a checkpoint operation on WAL database zDb attached to database 
  7255 ** handle db. The specific operation is determined by the value of the 
  7256 ** eMode parameter:
  7257 **
  7258 ** <dl>
  7259 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  7260 **   Checkpoint as many frames as possible without waiting for any database 
  7261 **   readers or writers to finish. Sync the db file if all frames in the log
  7262 **   are checkpointed. This mode is the same as calling 
  7263 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
  7264 **
  7265 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
  7266 **   This mode blocks (calls the busy-handler callback) until there is no
  7267 **   database writer and all readers are reading from the most recent database
  7268 **   snapshot. It then checkpoints all frames in the log file and syncs the
  7269 **   database file. This call blocks database writers while it is running,
  7270 **   but not database readers.
  7271 **
  7272 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  7273 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
  7274 **   checkpointing the log file it blocks (calls the busy-handler callback)
  7275 **   until all readers are reading from the database file only. This ensures 
  7276 **   that the next client to write to the database file restarts the log file 
  7277 **   from the beginning. This call blocks database writers while it is running,
  7278 **   but not database readers.
  7279 ** </dl>
  7280 **
  7281 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
  7282 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
  7283 ** the total number of checkpointed frames (including any that were already
  7284 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
  7285 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
  7286 ** If no values are available because of an error, they are both set to -1
  7287 ** before returning to communicate this to the caller.
  7288 **
  7289 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
  7290 ** any other process is running a checkpoint operation at the same time, the 
  7291 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
  7292 ** busy-handler configured, it will not be invoked in this case.
  7293 **
  7294 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
  7295 ** "writer" lock on the database file. If the writer lock cannot be obtained
  7296 ** immediately, and a busy-handler is configured, it is invoked and the writer
  7297 ** lock retried until either the busy-handler returns 0 or the lock is
  7298 ** successfully obtained. The busy-handler is also invoked while waiting for
  7299 ** database readers as described above. If the busy-handler returns 0 before
  7300 ** the writer lock is obtained or while waiting for database readers, the
  7301 ** checkpoint operation proceeds from that point in the same way as 
  7302 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
  7303 ** without blocking any further. SQLITE_BUSY is returned in this case.
  7304 **
  7305 ** If parameter zDb is NULL or points to a zero length string, then the
  7306 ** specified operation is attempted on all WAL databases. In this case the
  7307 ** values written to output parameters *pnLog and *pnCkpt are undefined. If 
  7308 ** an SQLITE_BUSY error is encountered when processing one or more of the 
  7309 ** attached WAL databases, the operation is still attempted on any remaining 
  7310 ** attached databases and SQLITE_BUSY is returned to the caller. If any other 
  7311 ** error occurs while processing an attached database, processing is abandoned 
  7312 ** and the error code returned to the caller immediately. If no error 
  7313 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
  7314 ** databases, SQLITE_OK is returned.
  7315 **
  7316 ** If database zDb is the name of an attached database that is not in WAL
  7317 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
  7318 ** zDb is not NULL (or a zero length string) and is not the name of any
  7319 ** attached database, SQLITE_ERROR is returned to the caller.
  7320 */
  7321 SQLITE_API int sqlite3_wal_checkpoint_v2(
  7322   sqlite3 *db,                    /* Database handle */
  7323   const char *zDb,                /* Name of attached database (or NULL) */
  7324   int eMode,                      /* SQLITE_CHECKPOINT_* value */
  7325   int *pnLog,                     /* OUT: Size of WAL log in frames */
  7326   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
  7327 );
  7329 /*
  7330 ** CAPI3REF: Checkpoint operation parameters
  7331 **
  7332 ** These constants can be used as the 3rd parameter to
  7333 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
  7334 ** documentation for additional information about the meaning and use of
  7335 ** each of these values.
  7336 */
  7337 #define SQLITE_CHECKPOINT_PASSIVE 0
  7338 #define SQLITE_CHECKPOINT_FULL    1
  7339 #define SQLITE_CHECKPOINT_RESTART 2
  7341 /*
  7342 ** CAPI3REF: Virtual Table Interface Configuration
  7343 **
  7344 ** This function may be called by either the [xConnect] or [xCreate] method
  7345 ** of a [virtual table] implementation to configure
  7346 ** various facets of the virtual table interface.
  7347 **
  7348 ** If this interface is invoked outside the context of an xConnect or
  7349 ** xCreate virtual table method then the behavior is undefined.
  7350 **
  7351 ** At present, there is only one option that may be configured using
  7352 ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
  7353 ** may be added in the future.
  7354 */
  7355 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
  7357 /*
  7358 ** CAPI3REF: Virtual Table Configuration Options
  7359 **
  7360 ** These macros define the various options to the
  7361 ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  7362 ** can use to customize and optimize their behavior.
  7363 **
  7364 ** <dl>
  7365 ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  7366 ** <dd>Calls of the form
  7367 ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  7368 ** where X is an integer.  If X is zero, then the [virtual table] whose
  7369 ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  7370 ** support constraints.  In this configuration (which is the default) if
  7371 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
  7372 ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
  7373 ** specified as part of the users SQL statement, regardless of the actual
  7374 ** ON CONFLICT mode specified.
  7375 **
  7376 ** If X is non-zero, then the virtual table implementation guarantees
  7377 ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
  7378 ** any modifications to internal or persistent data structures have been made.
  7379 ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite 
  7380 ** is able to roll back a statement or database transaction, and abandon
  7381 ** or continue processing the current SQL statement as appropriate. 
  7382 ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
  7383 ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
  7384 ** had been ABORT.
  7385 **
  7386 ** Virtual table implementations that are required to handle OR REPLACE
  7387 ** must do so within the [xUpdate] method. If a call to the 
  7388 ** [sqlite3_vtab_on_conflict()] function indicates that the current ON 
  7389 ** CONFLICT policy is REPLACE, the virtual table implementation should 
  7390 ** silently replace the appropriate rows within the xUpdate callback and
  7391 ** return SQLITE_OK. Or, if this is not possible, it may return
  7392 ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT 
  7393 ** constraint handling.
  7394 ** </dl>
  7395 */
  7396 #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
  7398 /*
  7399 ** CAPI3REF: Determine The Virtual Table Conflict Policy
  7400 **
  7401 ** This function may only be called from within a call to the [xUpdate] method
  7402 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
  7403 ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
  7404 ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
  7405 ** of the SQL statement that triggered the call to the [xUpdate] method of the
  7406 ** [virtual table].
  7407 */
  7408 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
  7410 /*
  7411 ** CAPI3REF: Conflict resolution modes
  7412 **
  7413 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  7414 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
  7415 ** is for the SQL statement being evaluated.
  7416 **
  7417 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
  7418 ** return value from the [sqlite3_set_authorizer()] callback and that
  7419 ** [SQLITE_ABORT] is also a [result code].
  7420 */
  7421 #define SQLITE_ROLLBACK 1
  7422 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
  7423 #define SQLITE_FAIL     3
  7424 /* #define SQLITE_ABORT 4  // Also an error code */
  7425 #define SQLITE_REPLACE  5
  7429 /*
  7430 ** Undo the hack that converts floating point types to integer for
  7431 ** builds on processors without floating point support.
  7432 */
  7433 #ifdef SQLITE_OMIT_FLOATING_POINT
  7434 # undef double
  7435 #endif
  7437 #if 0
  7438 }  /* End of the 'extern "C"' block */
  7439 #endif
  7440 #endif /* _SQLITE3_H_ */
  7442 /*
  7443 ** 2010 August 30
  7444 **
  7445 ** The author disclaims copyright to this source code.  In place of
  7446 ** a legal notice, here is a blessing:
  7447 **
  7448 **    May you do good and not evil.
  7449 **    May you find forgiveness for yourself and forgive others.
  7450 **    May you share freely, never taking more than you give.
  7451 **
  7452 *************************************************************************
  7453 */
  7455 #ifndef _SQLITE3RTREE_H_
  7456 #define _SQLITE3RTREE_H_
  7459 #if 0
  7460 extern "C" {
  7461 #endif
  7463 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
  7465 /*
  7466 ** Register a geometry callback named zGeom that can be used as part of an
  7467 ** R-Tree geometry query as follows:
  7468 **
  7469 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  7470 */
  7471 SQLITE_API int sqlite3_rtree_geometry_callback(
  7472   sqlite3 *db,
  7473   const char *zGeom,
  7474 #ifdef SQLITE_RTREE_INT_ONLY
  7475   int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
  7476 #else
  7477   int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
  7478 #endif
  7479   void *pContext
  7480 );
  7483 /*
  7484 ** A pointer to a structure of the following type is passed as the first
  7485 ** argument to callbacks registered using rtree_geometry_callback().
  7486 */
  7487 struct sqlite3_rtree_geometry {
  7488   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
  7489   int nParam;                     /* Size of array aParam[] */
  7490   double *aParam;                 /* Parameters passed to SQL geom function */
  7491   void *pUser;                    /* Callback implementation user data */
  7492   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
  7493 };
  7496 #if 0
  7497 }  /* end of the 'extern "C"' block */
  7498 #endif
  7500 #endif  /* ifndef _SQLITE3RTREE_H_ */
  7503 /************** End of sqlite3.h *********************************************/
  7504 /************** Continuing where we left off in sqliteInt.h ******************/
  7506 /*
  7507 ** Include the configuration header output by 'configure' if we're using the
  7508 ** autoconf-based build
  7509 */
  7510 #ifdef _HAVE_SQLITE_CONFIG_H
  7511 #include "config.h"
  7512 #endif
  7514 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
  7515 /************** Begin file sqliteLimit.h *************************************/
  7516 /*
  7517 ** 2007 May 7
  7518 **
  7519 ** The author disclaims copyright to this source code.  In place of
  7520 ** a legal notice, here is a blessing:
  7521 **
  7522 **    May you do good and not evil.
  7523 **    May you find forgiveness for yourself and forgive others.
  7524 **    May you share freely, never taking more than you give.
  7525 **
  7526 *************************************************************************
  7527 ** 
  7528 ** This file defines various limits of what SQLite can process.
  7529 */
  7531 /*
  7532 ** The maximum length of a TEXT or BLOB in bytes.   This also
  7533 ** limits the size of a row in a table or index.
  7534 **
  7535 ** The hard limit is the ability of a 32-bit signed integer
  7536 ** to count the size: 2^31-1 or 2147483647.
  7537 */
  7538 #ifndef SQLITE_MAX_LENGTH
  7539 # define SQLITE_MAX_LENGTH 1000000000
  7540 #endif
  7542 /*
  7543 ** This is the maximum number of
  7544 **
  7545 **    * Columns in a table
  7546 **    * Columns in an index
  7547 **    * Columns in a view
  7548 **    * Terms in the SET clause of an UPDATE statement
  7549 **    * Terms in the result set of a SELECT statement
  7550 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
  7551 **    * Terms in the VALUES clause of an INSERT statement
  7552 **
  7553 ** The hard upper limit here is 32676.  Most database people will
  7554 ** tell you that in a well-normalized database, you usually should
  7555 ** not have more than a dozen or so columns in any table.  And if
  7556 ** that is the case, there is no point in having more than a few
  7557 ** dozen values in any of the other situations described above.
  7558 */
  7559 #ifndef SQLITE_MAX_COLUMN
  7560 # define SQLITE_MAX_COLUMN 2000
  7561 #endif
  7563 /*
  7564 ** The maximum length of a single SQL statement in bytes.
  7565 **
  7566 ** It used to be the case that setting this value to zero would
  7567 ** turn the limit off.  That is no longer true.  It is not possible
  7568 ** to turn this limit off.
  7569 */
  7570 #ifndef SQLITE_MAX_SQL_LENGTH
  7571 # define SQLITE_MAX_SQL_LENGTH 1000000000
  7572 #endif
  7574 /*
  7575 ** The maximum depth of an expression tree. This is limited to 
  7576 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
  7577 ** want to place more severe limits on the complexity of an 
  7578 ** expression.
  7579 **
  7580 ** A value of 0 used to mean that the limit was not enforced.
  7581 ** But that is no longer true.  The limit is now strictly enforced
  7582 ** at all times.
  7583 */
  7584 #ifndef SQLITE_MAX_EXPR_DEPTH
  7585 # define SQLITE_MAX_EXPR_DEPTH 1000
  7586 #endif
  7588 /*
  7589 ** The maximum number of terms in a compound SELECT statement.
  7590 ** The code generator for compound SELECT statements does one
  7591 ** level of recursion for each term.  A stack overflow can result
  7592 ** if the number of terms is too large.  In practice, most SQL
  7593 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
  7594 ** any limit on the number of terms in a compount SELECT.
  7595 */
  7596 #ifndef SQLITE_MAX_COMPOUND_SELECT
  7597 # define SQLITE_MAX_COMPOUND_SELECT 500
  7598 #endif
  7600 /*
  7601 ** The maximum number of opcodes in a VDBE program.
  7602 ** Not currently enforced.
  7603 */
  7604 #ifndef SQLITE_MAX_VDBE_OP
  7605 # define SQLITE_MAX_VDBE_OP 25000
  7606 #endif
  7608 /*
  7609 ** The maximum number of arguments to an SQL function.
  7610 */
  7611 #ifndef SQLITE_MAX_FUNCTION_ARG
  7612 # define SQLITE_MAX_FUNCTION_ARG 127
  7613 #endif
  7615 /*
  7616 ** The maximum number of in-memory pages to use for the main database
  7617 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
  7618 */
  7619 #ifndef SQLITE_DEFAULT_CACHE_SIZE
  7620 # define SQLITE_DEFAULT_CACHE_SIZE  2000
  7621 #endif
  7622 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
  7623 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
  7624 #endif
  7626 /*
  7627 ** The default number of frames to accumulate in the log file before
  7628 ** checkpointing the database in WAL mode.
  7629 */
  7630 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
  7631 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
  7632 #endif
  7634 /*
  7635 ** The maximum number of attached databases.  This must be between 0
  7636 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
  7637 ** is used internally to track attached databases.
  7638 */
  7639 #ifndef SQLITE_MAX_ATTACHED
  7640 # define SQLITE_MAX_ATTACHED 10
  7641 #endif
  7644 /*
  7645 ** The maximum value of a ?nnn wildcard that the parser will accept.
  7646 */
  7647 #ifndef SQLITE_MAX_VARIABLE_NUMBER
  7648 # define SQLITE_MAX_VARIABLE_NUMBER 999
  7649 #endif
  7651 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
  7652 ** imposed by the use of 16-bit offsets within each page.
  7653 **
  7654 ** Earlier versions of SQLite allowed the user to change this value at
  7655 ** compile time. This is no longer permitted, on the grounds that it creates
  7656 ** a library that is technically incompatible with an SQLite library 
  7657 ** compiled with a different limit. If a process operating on a database 
  7658 ** with a page-size of 65536 bytes crashes, then an instance of SQLite 
  7659 ** compiled with the default page-size limit will not be able to rollback 
  7660 ** the aborted transaction. This could lead to database corruption.
  7661 */
  7662 #ifdef SQLITE_MAX_PAGE_SIZE
  7663 # undef SQLITE_MAX_PAGE_SIZE
  7664 #endif
  7665 #define SQLITE_MAX_PAGE_SIZE 65536
  7668 /*
  7669 ** The default size of a database page.
  7670 */
  7671 #ifndef SQLITE_DEFAULT_PAGE_SIZE
  7672 # define SQLITE_DEFAULT_PAGE_SIZE 1024
  7673 #endif
  7674 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  7675 # undef SQLITE_DEFAULT_PAGE_SIZE
  7676 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  7677 #endif
  7679 /*
  7680 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
  7681 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
  7682 ** device characteristics (sector-size and atomic write() support),
  7683 ** SQLite may choose a larger value. This constant is the maximum value
  7684 ** SQLite will choose on its own.
  7685 */
  7686 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
  7687 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
  7688 #endif
  7689 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  7690 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
  7691 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  7692 #endif
  7695 /*
  7696 ** Maximum number of pages in one database file.
  7697 **
  7698 ** This is really just the default value for the max_page_count pragma.
  7699 ** This value can be lowered (or raised) at run-time using that the
  7700 ** max_page_count macro.
  7701 */
  7702 #ifndef SQLITE_MAX_PAGE_COUNT
  7703 # define SQLITE_MAX_PAGE_COUNT 1073741823
  7704 #endif
  7706 /*
  7707 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  7708 ** operator.
  7709 */
  7710 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
  7711 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
  7712 #endif
  7714 /*
  7715 ** Maximum depth of recursion for triggers.
  7716 **
  7717 ** A value of 1 means that a trigger program will not be able to itself
  7718 ** fire any triggers. A value of 0 means that no trigger programs at all 
  7719 ** may be executed.
  7720 */
  7721 #ifndef SQLITE_MAX_TRIGGER_DEPTH
  7722 # define SQLITE_MAX_TRIGGER_DEPTH 1000
  7723 #endif
  7725 /************** End of sqliteLimit.h *****************************************/
  7726 /************** Continuing where we left off in sqliteInt.h ******************/
  7728 /* Disable nuisance warnings on Borland compilers */
  7729 #if defined(__BORLANDC__)
  7730 #pragma warn -rch /* unreachable code */
  7731 #pragma warn -ccc /* Condition is always true or false */
  7732 #pragma warn -aus /* Assigned value is never used */
  7733 #pragma warn -csu /* Comparing signed and unsigned */
  7734 #pragma warn -spa /* Suspicious pointer arithmetic */
  7735 #endif
  7737 /* Needed for various definitions... */
  7738 #ifndef _GNU_SOURCE
  7739 # define _GNU_SOURCE
  7740 #endif
  7742 #if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
  7743 # define _BSD_SOURCE
  7744 #endif
  7746 /*
  7747 ** Include standard header files as necessary
  7748 */
  7749 #ifdef HAVE_STDINT_H
  7750 #include <stdint.h>
  7751 #endif
  7752 #ifdef HAVE_INTTYPES_H
  7753 #include <inttypes.h>
  7754 #endif
  7756 /*
  7757 ** The following macros are used to cast pointers to integers and
  7758 ** integers to pointers.  The way you do this varies from one compiler
  7759 ** to the next, so we have developed the following set of #if statements
  7760 ** to generate appropriate macros for a wide range of compilers.
  7761 **
  7762 ** The correct "ANSI" way to do this is to use the intptr_t type. 
  7763 ** Unfortunately, that typedef is not available on all compilers, or
  7764 ** if it is available, it requires an #include of specific headers
  7765 ** that vary from one machine to the next.
  7766 **
  7767 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
  7768 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
  7769 ** So we have to define the macros in different ways depending on the
  7770 ** compiler.
  7771 */
  7772 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
  7773 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
  7774 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
  7775 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
  7776 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
  7777 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
  7778 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
  7779 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
  7780 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
  7781 #else                          /* Generates a warning - but it always works */
  7782 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
  7783 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
  7784 #endif
  7786 /*
  7787 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
  7788 ** 0 means mutexes are permanently disable and the library is never
  7789 ** threadsafe.  1 means the library is serialized which is the highest
  7790 ** level of threadsafety.  2 means the library is multithreaded - multiple
  7791 ** threads can use SQLite as long as no two threads try to use the same
  7792 ** database connection at the same time.
  7793 **
  7794 ** Older versions of SQLite used an optional THREADSAFE macro.
  7795 ** We support that for legacy.
  7796 */
  7797 #if !defined(SQLITE_THREADSAFE)
  7798 # if defined(THREADSAFE)
  7799 #   define SQLITE_THREADSAFE THREADSAFE
  7800 # else
  7801 #   define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
  7802 # endif
  7803 #endif
  7805 /*
  7806 ** Powersafe overwrite is on by default.  But can be turned off using
  7807 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
  7808 */
  7809 #ifndef SQLITE_POWERSAFE_OVERWRITE
  7810 # define SQLITE_POWERSAFE_OVERWRITE 1
  7811 #endif
  7813 /*
  7814 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
  7815 ** It determines whether or not the features related to 
  7816 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
  7817 ** be overridden at runtime using the sqlite3_config() API.
  7818 */
  7819 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
  7820 # define SQLITE_DEFAULT_MEMSTATUS 1
  7821 #endif
  7823 /*
  7824 ** Exactly one of the following macros must be defined in order to
  7825 ** specify which memory allocation subsystem to use.
  7826 **
  7827 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
  7828 **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
  7829 **     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
  7830 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
  7831 **
  7832 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
  7833 ** assert() macro is enabled, each call into the Win32 native heap subsystem
  7834 ** will cause HeapValidate to be called.  If heap validation should fail, an
  7835 ** assertion will be triggered.
  7836 **
  7837 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
  7838 ** the default.
  7839 */
  7840 #if defined(SQLITE_SYSTEM_MALLOC) \
  7841   + defined(SQLITE_WIN32_MALLOC) \
  7842   + defined(SQLITE_ZERO_MALLOC) \
  7843   + defined(SQLITE_MEMDEBUG)>1
  7844 # error "Two or more of the following compile-time configuration options\
  7845  are defined but at most one is allowed:\
  7846  SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
  7847  SQLITE_ZERO_MALLOC"
  7848 #endif
  7849 #if defined(SQLITE_SYSTEM_MALLOC) \
  7850   + defined(SQLITE_WIN32_MALLOC) \
  7851   + defined(SQLITE_ZERO_MALLOC) \
  7852   + defined(SQLITE_MEMDEBUG)==0
  7853 # define SQLITE_SYSTEM_MALLOC 1
  7854 #endif
  7856 /*
  7857 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
  7858 ** sizes of memory allocations below this value where possible.
  7859 */
  7860 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
  7861 # define SQLITE_MALLOC_SOFT_LIMIT 1024
  7862 #endif
  7864 /*
  7865 ** We need to define _XOPEN_SOURCE as follows in order to enable
  7866 ** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
  7867 ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
  7868 ** it.
  7869 */
  7870 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
  7871 #  define _XOPEN_SOURCE 600
  7872 #endif
  7874 /*
  7875 ** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
  7876 ** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
  7877 ** make it true by defining or undefining NDEBUG.
  7878 **
  7879 ** Setting NDEBUG makes the code smaller and faster by disabling the
  7880 ** assert() statements in the code.  So we want the default action
  7881 ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
  7882 ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
  7883 ** feature.
  7884 */
  7885 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  7886 # define NDEBUG 1
  7887 #endif
  7888 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
  7889 # undef NDEBUG
  7890 #endif
  7892 /*
  7893 ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
  7894 */
  7895 #if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
  7896 # define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
  7897 #endif
  7899 /*
  7900 ** The testcase() macro is used to aid in coverage testing.  When 
  7901 ** doing coverage testing, the condition inside the argument to
  7902 ** testcase() must be evaluated both true and false in order to
  7903 ** get full branch coverage.  The testcase() macro is inserted
  7904 ** to help ensure adequate test coverage in places where simple
  7905 ** condition/decision coverage is inadequate.  For example, testcase()
  7906 ** can be used to make sure boundary values are tested.  For
  7907 ** bitmask tests, testcase() can be used to make sure each bit
  7908 ** is significant and used at least once.  On switch statements
  7909 ** where multiple cases go to the same block of code, testcase()
  7910 ** can insure that all cases are evaluated.
  7911 **
  7912 */
  7913 #ifdef SQLITE_COVERAGE_TEST
  7914 SQLITE_PRIVATE   void sqlite3Coverage(int);
  7915 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
  7916 #else
  7917 # define testcase(X)
  7918 #endif
  7920 /*
  7921 ** The TESTONLY macro is used to enclose variable declarations or
  7922 ** other bits of code that are needed to support the arguments
  7923 ** within testcase() and assert() macros.
  7924 */
  7925 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
  7926 # define TESTONLY(X)  X
  7927 #else
  7928 # define TESTONLY(X)
  7929 #endif
  7931 /*
  7932 ** Sometimes we need a small amount of code such as a variable initialization
  7933 ** to setup for a later assert() statement.  We do not want this code to
  7934 ** appear when assert() is disabled.  The following macro is therefore
  7935 ** used to contain that setup code.  The "VVA" acronym stands for
  7936 ** "Verification, Validation, and Accreditation".  In other words, the
  7937 ** code within VVA_ONLY() will only run during verification processes.
  7938 */
  7939 #ifndef NDEBUG
  7940 # define VVA_ONLY(X)  X
  7941 #else
  7942 # define VVA_ONLY(X)
  7943 #endif
  7945 /*
  7946 ** The ALWAYS and NEVER macros surround boolean expressions which 
  7947 ** are intended to always be true or false, respectively.  Such
  7948 ** expressions could be omitted from the code completely.  But they
  7949 ** are included in a few cases in order to enhance the resilience
  7950 ** of SQLite to unexpected behavior - to make the code "self-healing"
  7951 ** or "ductile" rather than being "brittle" and crashing at the first
  7952 ** hint of unplanned behavior.
  7953 **
  7954 ** In other words, ALWAYS and NEVER are added for defensive code.
  7955 **
  7956 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
  7957 ** be true and false so that the unreachable code they specify will
  7958 ** not be counted as untested code.
  7959 */
  7960 #if defined(SQLITE_COVERAGE_TEST)
  7961 # define ALWAYS(X)      (1)
  7962 # define NEVER(X)       (0)
  7963 #elif !defined(NDEBUG)
  7964 # define ALWAYS(X)      ((X)?1:(assert(0),0))
  7965 # define NEVER(X)       ((X)?(assert(0),1):0)
  7966 #else
  7967 # define ALWAYS(X)      (X)
  7968 # define NEVER(X)       (X)
  7969 #endif
  7971 /*
  7972 ** Return true (non-zero) if the input is a integer that is too large
  7973 ** to fit in 32-bits.  This macro is used inside of various testcase()
  7974 ** macros to verify that we have tested SQLite for large-file support.
  7975 */
  7976 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
  7978 /*
  7979 ** The macro unlikely() is a hint that surrounds a boolean
  7980 ** expression that is usually false.  Macro likely() surrounds
  7981 ** a boolean expression that is usually true.  These hints could,
  7982 ** in theory, be used by the compiler to generate better code, but
  7983 ** currently they are just comments for human readers.
  7984 */
  7985 #define likely(X)    (X)
  7986 #define unlikely(X)  (X)
  7988 /************** Include hash.h in the middle of sqliteInt.h ******************/
  7989 /************** Begin file hash.h ********************************************/
  7990 /*
  7991 ** 2001 September 22
  7992 **
  7993 ** The author disclaims copyright to this source code.  In place of
  7994 ** a legal notice, here is a blessing:
  7995 **
  7996 **    May you do good and not evil.
  7997 **    May you find forgiveness for yourself and forgive others.
  7998 **    May you share freely, never taking more than you give.
  7999 **
  8000 *************************************************************************
  8001 ** This is the header file for the generic hash-table implementation
  8002 ** used in SQLite.
  8003 */
  8004 #ifndef _SQLITE_HASH_H_
  8005 #define _SQLITE_HASH_H_
  8007 /* Forward declarations of structures. */
  8008 typedef struct Hash Hash;
  8009 typedef struct HashElem HashElem;
  8011 /* A complete hash table is an instance of the following structure.
  8012 ** The internals of this structure are intended to be opaque -- client
  8013 ** code should not attempt to access or modify the fields of this structure
  8014 ** directly.  Change this structure only by using the routines below.
  8015 ** However, some of the "procedures" and "functions" for modifying and
  8016 ** accessing this structure are really macros, so we can't really make
  8017 ** this structure opaque.
  8018 **
  8019 ** All elements of the hash table are on a single doubly-linked list.
  8020 ** Hash.first points to the head of this list.
  8021 **
  8022 ** There are Hash.htsize buckets.  Each bucket points to a spot in
  8023 ** the global doubly-linked list.  The contents of the bucket are the
  8024 ** element pointed to plus the next _ht.count-1 elements in the list.
  8025 **
  8026 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
  8027 ** by a linear search of the global list.  For small tables, the 
  8028 ** Hash.ht table is never allocated because if there are few elements
  8029 ** in the table, it is faster to do a linear search than to manage
  8030 ** the hash table.
  8031 */
  8032 struct Hash {
  8033   unsigned int htsize;      /* Number of buckets in the hash table */
  8034   unsigned int count;       /* Number of entries in this table */
  8035   HashElem *first;          /* The first element of the array */
  8036   struct _ht {              /* the hash table */
  8037     int count;                 /* Number of entries with this hash */
  8038     HashElem *chain;           /* Pointer to first entry with this hash */
  8039   } *ht;
  8040 };
  8042 /* Each element in the hash table is an instance of the following 
  8043 ** structure.  All elements are stored on a single doubly-linked list.
  8044 **
  8045 ** Again, this structure is intended to be opaque, but it can't really
  8046 ** be opaque because it is used by macros.
  8047 */
  8048 struct HashElem {
  8049   HashElem *next, *prev;       /* Next and previous elements in the table */
  8050   void *data;                  /* Data associated with this element */
  8051   const char *pKey; int nKey;  /* Key associated with this element */
  8052 };
  8054 /*
  8055 ** Access routines.  To delete, insert a NULL pointer.
  8056 */
  8057 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
  8058 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  8059 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  8060 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
  8062 /*
  8063 ** Macros for looping over all elements of a hash table.  The idiom is
  8064 ** like this:
  8065 **
  8066 **   Hash h;
  8067 **   HashElem *p;
  8068 **   ...
  8069 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  8070 **     SomeStructure *pData = sqliteHashData(p);
  8071 **     // do something with pData
  8072 **   }
  8073 */
  8074 #define sqliteHashFirst(H)  ((H)->first)
  8075 #define sqliteHashNext(E)   ((E)->next)
  8076 #define sqliteHashData(E)   ((E)->data)
  8077 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
  8078 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
  8080 /*
  8081 ** Number of entries in a hash table
  8082 */
  8083 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
  8085 #endif /* _SQLITE_HASH_H_ */
  8087 /************** End of hash.h ************************************************/
  8088 /************** Continuing where we left off in sqliteInt.h ******************/
  8089 /************** Include parse.h in the middle of sqliteInt.h *****************/
  8090 /************** Begin file parse.h *******************************************/
  8091 #define TK_SEMI                             1
  8092 #define TK_EXPLAIN                          2
  8093 #define TK_QUERY                            3
  8094 #define TK_PLAN                             4
  8095 #define TK_BEGIN                            5
  8096 #define TK_TRANSACTION                      6
  8097 #define TK_DEFERRED                         7
  8098 #define TK_IMMEDIATE                        8
  8099 #define TK_EXCLUSIVE                        9
  8100 #define TK_COMMIT                          10
  8101 #define TK_END                             11
  8102 #define TK_ROLLBACK                        12
  8103 #define TK_SAVEPOINT                       13
  8104 #define TK_RELEASE                         14
  8105 #define TK_TO                              15
  8106 #define TK_TABLE                           16
  8107 #define TK_CREATE                          17
  8108 #define TK_IF                              18
  8109 #define TK_NOT                             19
  8110 #define TK_EXISTS                          20
  8111 #define TK_TEMP                            21
  8112 #define TK_LP                              22
  8113 #define TK_RP                              23
  8114 #define TK_AS                              24
  8115 #define TK_WITHOUT                         25
  8116 #define TK_COMMA                           26
  8117 #define TK_ID                              27
  8118 #define TK_INDEXED                         28
  8119 #define TK_ABORT                           29
  8120 #define TK_ACTION                          30
  8121 #define TK_AFTER                           31
  8122 #define TK_ANALYZE                         32
  8123 #define TK_ASC                             33
  8124 #define TK_ATTACH                          34
  8125 #define TK_BEFORE                          35
  8126 #define TK_BY                              36
  8127 #define TK_CASCADE                         37
  8128 #define TK_CAST                            38
  8129 #define TK_COLUMNKW                        39
  8130 #define TK_CONFLICT                        40
  8131 #define TK_DATABASE                        41
  8132 #define TK_DESC                            42
  8133 #define TK_DETACH                          43
  8134 #define TK_EACH                            44
  8135 #define TK_FAIL                            45
  8136 #define TK_FOR                             46
  8137 #define TK_IGNORE                          47
  8138 #define TK_INITIALLY                       48
  8139 #define TK_INSTEAD                         49
  8140 #define TK_LIKE_KW                         50
  8141 #define TK_MATCH                           51
  8142 #define TK_NO                              52
  8143 #define TK_KEY                             53
  8144 #define TK_OF                              54
  8145 #define TK_OFFSET                          55
  8146 #define TK_PRAGMA                          56
  8147 #define TK_RAISE                           57
  8148 #define TK_RECURSIVE                       58
  8149 #define TK_REPLACE                         59
  8150 #define TK_RESTRICT                        60
  8151 #define TK_ROW                             61
  8152 #define TK_TRIGGER                         62
  8153 #define TK_VACUUM                          63
  8154 #define TK_VIEW                            64
  8155 #define TK_VIRTUAL                         65
  8156 #define TK_WITH                            66
  8157 #define TK_REINDEX                         67
  8158 #define TK_RENAME                          68
  8159 #define TK_CTIME_KW                        69
  8160 #define TK_ANY                             70
  8161 #define TK_OR                              71
  8162 #define TK_AND                             72
  8163 #define TK_IS                              73
  8164 #define TK_BETWEEN                         74
  8165 #define TK_IN                              75
  8166 #define TK_ISNULL                          76
  8167 #define TK_NOTNULL                         77
  8168 #define TK_NE                              78
  8169 #define TK_EQ                              79
  8170 #define TK_GT                              80
  8171 #define TK_LE                              81
  8172 #define TK_LT                              82
  8173 #define TK_GE                              83
  8174 #define TK_ESCAPE                          84
  8175 #define TK_BITAND                          85
  8176 #define TK_BITOR                           86
  8177 #define TK_LSHIFT                          87
  8178 #define TK_RSHIFT                          88
  8179 #define TK_PLUS                            89
  8180 #define TK_MINUS                           90
  8181 #define TK_STAR                            91
  8182 #define TK_SLASH                           92
  8183 #define TK_REM                             93
  8184 #define TK_CONCAT                          94
  8185 #define TK_COLLATE                         95
  8186 #define TK_BITNOT                          96
  8187 #define TK_STRING                          97
  8188 #define TK_JOIN_KW                         98
  8189 #define TK_CONSTRAINT                      99
  8190 #define TK_DEFAULT                        100
  8191 #define TK_NULL                           101
  8192 #define TK_PRIMARY                        102
  8193 #define TK_UNIQUE                         103
  8194 #define TK_CHECK                          104
  8195 #define TK_REFERENCES                     105
  8196 #define TK_AUTOINCR                       106
  8197 #define TK_ON                             107
  8198 #define TK_INSERT                         108
  8199 #define TK_DELETE                         109
  8200 #define TK_UPDATE                         110
  8201 #define TK_SET                            111
  8202 #define TK_DEFERRABLE                     112
  8203 #define TK_FOREIGN                        113
  8204 #define TK_DROP                           114
  8205 #define TK_UNION                          115
  8206 #define TK_ALL                            116
  8207 #define TK_EXCEPT                         117
  8208 #define TK_INTERSECT                      118
  8209 #define TK_SELECT                         119
  8210 #define TK_VALUES                         120
  8211 #define TK_DISTINCT                       121
  8212 #define TK_DOT                            122
  8213 #define TK_FROM                           123
  8214 #define TK_JOIN                           124
  8215 #define TK_USING                          125
  8216 #define TK_ORDER                          126
  8217 #define TK_GROUP                          127
  8218 #define TK_HAVING                         128
  8219 #define TK_LIMIT                          129
  8220 #define TK_WHERE                          130
  8221 #define TK_INTO                           131
  8222 #define TK_INTEGER                        132
  8223 #define TK_FLOAT                          133
  8224 #define TK_BLOB                           134
  8225 #define TK_VARIABLE                       135
  8226 #define TK_CASE                           136
  8227 #define TK_WHEN                           137
  8228 #define TK_THEN                           138
  8229 #define TK_ELSE                           139
  8230 #define TK_INDEX                          140
  8231 #define TK_ALTER                          141
  8232 #define TK_ADD                            142
  8233 #define TK_TO_TEXT                        143
  8234 #define TK_TO_BLOB                        144
  8235 #define TK_TO_NUMERIC                     145
  8236 #define TK_TO_INT                         146
  8237 #define TK_TO_REAL                        147
  8238 #define TK_ISNOT                          148
  8239 #define TK_END_OF_FILE                    149
  8240 #define TK_ILLEGAL                        150
  8241 #define TK_SPACE                          151
  8242 #define TK_UNCLOSED_STRING                152
  8243 #define TK_FUNCTION                       153
  8244 #define TK_COLUMN                         154
  8245 #define TK_AGG_FUNCTION                   155
  8246 #define TK_AGG_COLUMN                     156
  8247 #define TK_UMINUS                         157
  8248 #define TK_UPLUS                          158
  8249 #define TK_REGISTER                       159
  8251 /************** End of parse.h ***********************************************/
  8252 /************** Continuing where we left off in sqliteInt.h ******************/
  8253 #include <stdio.h>
  8254 #include <stdlib.h>
  8255 #include <string.h>
  8256 #include <assert.h>
  8257 #include <stddef.h>
  8259 /*
  8260 ** If compiling for a processor that lacks floating point support,
  8261 ** substitute integer for floating-point
  8262 */
  8263 #ifdef SQLITE_OMIT_FLOATING_POINT
  8264 # define double sqlite_int64
  8265 # define float sqlite_int64
  8266 # define LONGDOUBLE_TYPE sqlite_int64
  8267 # ifndef SQLITE_BIG_DBL
  8268 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  8269 # endif
  8270 # define SQLITE_OMIT_DATETIME_FUNCS 1
  8271 # define SQLITE_OMIT_TRACE 1
  8272 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  8273 # undef SQLITE_HAVE_ISNAN
  8274 #endif
  8275 #ifndef SQLITE_BIG_DBL
  8276 # define SQLITE_BIG_DBL (1e99)
  8277 #endif
  8279 /*
  8280 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  8281 ** afterward. Having this macro allows us to cause the C compiler 
  8282 ** to omit code used by TEMP tables without messy #ifndef statements.
  8283 */
  8284 #ifdef SQLITE_OMIT_TEMPDB
  8285 #define OMIT_TEMPDB 1
  8286 #else
  8287 #define OMIT_TEMPDB 0
  8288 #endif
  8290 /*
  8291 ** The "file format" number is an integer that is incremented whenever
  8292 ** the VDBE-level file format changes.  The following macros define the
  8293 ** the default file format for new databases and the maximum file format
  8294 ** that the library can read.
  8295 */
  8296 #define SQLITE_MAX_FILE_FORMAT 4
  8297 #ifndef SQLITE_DEFAULT_FILE_FORMAT
  8298 # define SQLITE_DEFAULT_FILE_FORMAT 4
  8299 #endif
  8301 /*
  8302 ** Determine whether triggers are recursive by default.  This can be
  8303 ** changed at run-time using a pragma.
  8304 */
  8305 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  8306 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  8307 #endif
  8309 /*
  8310 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  8311 ** on the command-line
  8312 */
  8313 #ifndef SQLITE_TEMP_STORE
  8314 # define SQLITE_TEMP_STORE 1
  8315 # define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
  8316 #endif
  8318 /*
  8319 ** GCC does not define the offsetof() macro so we'll have to do it
  8320 ** ourselves.
  8321 */
  8322 #ifndef offsetof
  8323 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  8324 #endif
  8326 /*
  8327 ** Macros to compute minimum and maximum of two numbers.
  8328 */
  8329 #define MIN(A,B) ((A)<(B)?(A):(B))
  8330 #define MAX(A,B) ((A)>(B)?(A):(B))
  8332 /*
  8333 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
  8334 ** not, there are still machines out there that use EBCDIC.)
  8335 */
  8336 #if 'A' == '\301'
  8337 # define SQLITE_EBCDIC 1
  8338 #else
  8339 # define SQLITE_ASCII 1
  8340 #endif
  8342 /*
  8343 ** Integers of known sizes.  These typedefs might change for architectures
  8344 ** where the sizes very.  Preprocessor macros are available so that the
  8345 ** types can be conveniently redefined at compile-type.  Like this:
  8346 **
  8347 **         cc '-DUINTPTR_TYPE=long long int' ...
  8348 */
  8349 #ifndef UINT32_TYPE
  8350 # ifdef HAVE_UINT32_T
  8351 #  define UINT32_TYPE uint32_t
  8352 # else
  8353 #  define UINT32_TYPE unsigned int
  8354 # endif
  8355 #endif
  8356 #ifndef UINT16_TYPE
  8357 # ifdef HAVE_UINT16_T
  8358 #  define UINT16_TYPE uint16_t
  8359 # else
  8360 #  define UINT16_TYPE unsigned short int
  8361 # endif
  8362 #endif
  8363 #ifndef INT16_TYPE
  8364 # ifdef HAVE_INT16_T
  8365 #  define INT16_TYPE int16_t
  8366 # else
  8367 #  define INT16_TYPE short int
  8368 # endif
  8369 #endif
  8370 #ifndef UINT8_TYPE
  8371 # ifdef HAVE_UINT8_T
  8372 #  define UINT8_TYPE uint8_t
  8373 # else
  8374 #  define UINT8_TYPE unsigned char
  8375 # endif
  8376 #endif
  8377 #ifndef INT8_TYPE
  8378 # ifdef HAVE_INT8_T
  8379 #  define INT8_TYPE int8_t
  8380 # else
  8381 #  define INT8_TYPE signed char
  8382 # endif
  8383 #endif
  8384 #ifndef LONGDOUBLE_TYPE
  8385 # define LONGDOUBLE_TYPE long double
  8386 #endif
  8387 typedef sqlite_int64 i64;          /* 8-byte signed integer */
  8388 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
  8389 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
  8390 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
  8391 typedef INT16_TYPE i16;            /* 2-byte signed integer */
  8392 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
  8393 typedef INT8_TYPE i8;              /* 1-byte signed integer */
  8395 /*
  8396 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  8397 ** that can be stored in a u32 without loss of data.  The value
  8398 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
  8399 ** have to specify the value in the less intuitive manner shown:
  8400 */
  8401 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
  8403 /*
  8404 ** The datatype used to store estimates of the number of rows in a
  8405 ** table or index.  This is an unsigned integer type.  For 99.9% of
  8406 ** the world, a 32-bit integer is sufficient.  But a 64-bit integer
  8407 ** can be used at compile-time if desired.
  8408 */
  8409 #ifdef SQLITE_64BIT_STATS
  8410  typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
  8411 #else
  8412  typedef u32 tRowcnt;    /* 32-bit is the default */
  8413 #endif
  8415 /*
  8416 ** Estimated quantities used for query planning are stored as 16-bit
  8417 ** logarithms.  For quantity X, the value stored is 10*log2(X).  This
  8418 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
  8419 ** But the allowed values are "grainy".  Not every value is representable.
  8420 ** For example, quantities 16 and 17 are both represented by a LogEst
  8421 ** of 40.  However, since LogEst quantatites are suppose to be estimates,
  8422 ** not exact values, this imprecision is not a problem.
  8423 **
  8424 ** "LogEst" is short for "Logarithimic Estimate".
  8425 **
  8426 ** Examples:
  8427 **      1 -> 0              20 -> 43          10000 -> 132
  8428 **      2 -> 10             25 -> 46          25000 -> 146
  8429 **      3 -> 16            100 -> 66        1000000 -> 199
  8430 **      4 -> 20           1000 -> 99        1048576 -> 200
  8431 **     10 -> 33           1024 -> 100    4294967296 -> 320
  8432 **
  8433 ** The LogEst can be negative to indicate fractional values. 
  8434 ** Examples:
  8435 **
  8436 **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
  8437 */
  8438 typedef INT16_TYPE LogEst;
  8440 /*
  8441 ** Macros to determine whether the machine is big or little endian,
  8442 ** evaluated at runtime.
  8443 */
  8444 #ifdef SQLITE_AMALGAMATION
  8445 SQLITE_PRIVATE const int sqlite3one = 1;
  8446 #else
  8447 SQLITE_PRIVATE const int sqlite3one;
  8448 #endif
  8449 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  8450                              || defined(__x86_64) || defined(__x86_64__)
  8451 # define SQLITE_BIGENDIAN    0
  8452 # define SQLITE_LITTLEENDIAN 1
  8453 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
  8454 #else
  8455 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
  8456 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  8457 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  8458 #endif
  8460 /*
  8461 ** Constants for the largest and smallest possible 64-bit signed integers.
  8462 ** These macros are designed to work correctly on both 32-bit and 64-bit
  8463 ** compilers.
  8464 */
  8465 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
  8466 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  8468 /* 
  8469 ** Round up a number to the next larger multiple of 8.  This is used
  8470 ** to force 8-byte alignment on 64-bit architectures.
  8471 */
  8472 #define ROUND8(x)     (((x)+7)&~7)
  8474 /*
  8475 ** Round down to the nearest multiple of 8
  8476 */
  8477 #define ROUNDDOWN8(x) ((x)&~7)
  8479 /*
  8480 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
  8481 ** macro is used only within assert() to verify that the code gets
  8482 ** all alignment restrictions correct.
  8483 **
  8484 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  8485 ** underlying malloc() implemention might return us 4-byte aligned
  8486 ** pointers.  In that case, only verify 4-byte alignment.
  8487 */
  8488 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  8489 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
  8490 #else
  8491 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
  8492 #endif
  8494 /*
  8495 ** Disable MMAP on platforms where it is known to not work
  8496 */
  8497 #if defined(__OpenBSD__) || defined(__QNXNTO__)
  8498 # undef SQLITE_MAX_MMAP_SIZE
  8499 # define SQLITE_MAX_MMAP_SIZE 0
  8500 #endif
  8502 /*
  8503 ** Default maximum size of memory used by memory-mapped I/O in the VFS
  8504 */
  8505 #ifdef __APPLE__
  8506 # include <TargetConditionals.h>
  8507 # if TARGET_OS_IPHONE
  8508 #   undef SQLITE_MAX_MMAP_SIZE
  8509 #   define SQLITE_MAX_MMAP_SIZE 0
  8510 # endif
  8511 #endif
  8512 #ifndef SQLITE_MAX_MMAP_SIZE
  8513 # if defined(__linux__) \
  8514   || defined(_WIN32) \
  8515   || (defined(__APPLE__) && defined(__MACH__)) \
  8516   || defined(__sun)
  8517 #   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
  8518 # else
  8519 #   define SQLITE_MAX_MMAP_SIZE 0
  8520 # endif
  8521 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
  8522 #endif
  8524 /*
  8525 ** The default MMAP_SIZE is zero on all platforms.  Or, even if a larger
  8526 ** default MMAP_SIZE is specified at compile-time, make sure that it does
  8527 ** not exceed the maximum mmap size.
  8528 */
  8529 #ifndef SQLITE_DEFAULT_MMAP_SIZE
  8530 # define SQLITE_DEFAULT_MMAP_SIZE 0
  8531 # define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
  8532 #endif
  8533 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
  8534 # undef SQLITE_DEFAULT_MMAP_SIZE
  8535 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
  8536 #endif
  8538 /*
  8539 ** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
  8540 ** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
  8541 ** define SQLITE_ENABLE_STAT3_OR_STAT4
  8542 */
  8543 #ifdef SQLITE_ENABLE_STAT4
  8544 # undef SQLITE_ENABLE_STAT3
  8545 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
  8546 #elif SQLITE_ENABLE_STAT3
  8547 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
  8548 #elif SQLITE_ENABLE_STAT3_OR_STAT4
  8549 # undef SQLITE_ENABLE_STAT3_OR_STAT4
  8550 #endif
  8552 /*
  8553 ** An instance of the following structure is used to store the busy-handler
  8554 ** callback for a given sqlite handle. 
  8555 **
  8556 ** The sqlite.busyHandler member of the sqlite struct contains the busy
  8557 ** callback for the database handle. Each pager opened via the sqlite
  8558 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  8559 ** callback is currently invoked only from within pager.c.
  8560 */
  8561 typedef struct BusyHandler BusyHandler;
  8562 struct BusyHandler {
  8563   int (*xFunc)(void *,int);  /* The busy callback */
  8564   void *pArg;                /* First arg to busy callback */
  8565   int nBusy;                 /* Incremented with each busy call */
  8566 };
  8568 /*
  8569 ** Name of the master database table.  The master database table
  8570 ** is a special table that holds the names and attributes of all
  8571 ** user tables and indices.
  8572 */
  8573 #define MASTER_NAME       "sqlite_master"
  8574 #define TEMP_MASTER_NAME  "sqlite_temp_master"
  8576 /*
  8577 ** The root-page of the master database table.
  8578 */
  8579 #define MASTER_ROOT       1
  8581 /*
  8582 ** The name of the schema table.
  8583 */
  8584 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  8586 /*
  8587 ** A convenience macro that returns the number of elements in
  8588 ** an array.
  8589 */
  8590 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
  8592 /*
  8593 ** Determine if the argument is a power of two
  8594 */
  8595 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
  8597 /*
  8598 ** The following value as a destructor means to use sqlite3DbFree().
  8599 ** The sqlite3DbFree() routine requires two parameters instead of the 
  8600 ** one parameter that destructors normally want.  So we have to introduce 
  8601 ** this magic value that the code knows to handle differently.  Any 
  8602 ** pointer will work here as long as it is distinct from SQLITE_STATIC
  8603 ** and SQLITE_TRANSIENT.
  8604 */
  8605 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
  8607 /*
  8608 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  8609 ** not support Writable Static Data (WSD) such as global and static variables.
  8610 ** All variables must either be on the stack or dynamically allocated from
  8611 ** the heap.  When WSD is unsupported, the variable declarations scattered
  8612 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
  8613 ** macro is used for this purpose.  And instead of referencing the variable
  8614 ** directly, we use its constant as a key to lookup the run-time allocated
  8615 ** buffer that holds real variable.  The constant is also the initializer
  8616 ** for the run-time allocated buffer.
  8617 **
  8618 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  8619 ** macros become no-ops and have zero performance impact.
  8620 */
  8621 #ifdef SQLITE_OMIT_WSD
  8622   #define SQLITE_WSD const
  8623   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  8624   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  8625 SQLITE_API   int sqlite3_wsd_init(int N, int J);
  8626 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
  8627 #else
  8628   #define SQLITE_WSD 
  8629   #define GLOBAL(t,v) v
  8630   #define sqlite3GlobalConfig sqlite3Config
  8631 #endif
  8633 /*
  8634 ** The following macros are used to suppress compiler warnings and to
  8635 ** make it clear to human readers when a function parameter is deliberately 
  8636 ** left unused within the body of a function. This usually happens when
  8637 ** a function is called via a function pointer. For example the 
  8638 ** implementation of an SQL aggregate step callback may not use the
  8639 ** parameter indicating the number of arguments passed to the aggregate,
  8640 ** if it knows that this is enforced elsewhere.
  8641 **
  8642 ** When a function parameter is not used at all within the body of a function,
  8643 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  8644 ** However, these macros may also be used to suppress warnings related to
  8645 ** parameters that may or may not be used depending on compilation options.
  8646 ** For example those parameters only used in assert() statements. In these
  8647 ** cases the parameters are named as per the usual conventions.
  8648 */
  8649 #define UNUSED_PARAMETER(x) (void)(x)
  8650 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  8652 /*
  8653 ** Forward references to structures
  8654 */
  8655 typedef struct AggInfo AggInfo;
  8656 typedef struct AuthContext AuthContext;
  8657 typedef struct AutoincInfo AutoincInfo;
  8658 typedef struct Bitvec Bitvec;
  8659 typedef struct CollSeq CollSeq;
  8660 typedef struct Column Column;
  8661 typedef struct Db Db;
  8662 typedef struct Schema Schema;
  8663 typedef struct Expr Expr;
  8664 typedef struct ExprList ExprList;
  8665 typedef struct ExprSpan ExprSpan;
  8666 typedef struct FKey FKey;
  8667 typedef struct FuncDestructor FuncDestructor;
  8668 typedef struct FuncDef FuncDef;
  8669 typedef struct FuncDefHash FuncDefHash;
  8670 typedef struct IdList IdList;
  8671 typedef struct Index Index;
  8672 typedef struct IndexSample IndexSample;
  8673 typedef struct KeyClass KeyClass;
  8674 typedef struct KeyInfo KeyInfo;
  8675 typedef struct Lookaside Lookaside;
  8676 typedef struct LookasideSlot LookasideSlot;
  8677 typedef struct Module Module;
  8678 typedef struct NameContext NameContext;
  8679 typedef struct Parse Parse;
  8680 typedef struct PrintfArguments PrintfArguments;
  8681 typedef struct RowSet RowSet;
  8682 typedef struct Savepoint Savepoint;
  8683 typedef struct Select Select;
  8684 typedef struct SelectDest SelectDest;
  8685 typedef struct SrcList SrcList;
  8686 typedef struct StrAccum StrAccum;
  8687 typedef struct Table Table;
  8688 typedef struct TableLock TableLock;
  8689 typedef struct Token Token;
  8690 typedef struct Trigger Trigger;
  8691 typedef struct TriggerPrg TriggerPrg;
  8692 typedef struct TriggerStep TriggerStep;
  8693 typedef struct UnpackedRecord UnpackedRecord;
  8694 typedef struct VTable VTable;
  8695 typedef struct VtabCtx VtabCtx;
  8696 typedef struct Walker Walker;
  8697 typedef struct WhereInfo WhereInfo;
  8698 typedef struct With With;
  8700 /*
  8701 ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
  8702 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  8703 ** pointer types (i.e. FuncDef) defined above.
  8704 */
  8705 /************** Include btree.h in the middle of sqliteInt.h *****************/
  8706 /************** Begin file btree.h *******************************************/
  8707 /*
  8708 ** 2001 September 15
  8709 **
  8710 ** The author disclaims copyright to this source code.  In place of
  8711 ** a legal notice, here is a blessing:
  8712 **
  8713 **    May you do good and not evil.
  8714 **    May you find forgiveness for yourself and forgive others.
  8715 **    May you share freely, never taking more than you give.
  8716 **
  8717 *************************************************************************
  8718 ** This header file defines the interface that the sqlite B-Tree file
  8719 ** subsystem.  See comments in the source code for a detailed description
  8720 ** of what each interface routine does.
  8721 */
  8722 #ifndef _BTREE_H_
  8723 #define _BTREE_H_
  8725 /* TODO: This definition is just included so other modules compile. It
  8726 ** needs to be revisited.
  8727 */
  8728 #define SQLITE_N_BTREE_META 10
  8730 /*
  8731 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  8732 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  8733 */
  8734 #ifndef SQLITE_DEFAULT_AUTOVACUUM
  8735   #define SQLITE_DEFAULT_AUTOVACUUM 0
  8736 #endif
  8738 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
  8739 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
  8740 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
  8742 /*
  8743 ** Forward declarations of structure
  8744 */
  8745 typedef struct Btree Btree;
  8746 typedef struct BtCursor BtCursor;
  8747 typedef struct BtShared BtShared;
  8750 SQLITE_PRIVATE int sqlite3BtreeOpen(
  8751   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
  8752   const char *zFilename,   /* Name of database file to open */
  8753   sqlite3 *db,             /* Associated database connection */
  8754   Btree **ppBtree,         /* Return open Btree* here */
  8755   int flags,               /* Flags */
  8756   int vfsFlags             /* Flags passed through to VFS open */
  8757 );
  8759 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  8760 ** following values.
  8761 **
  8762 ** NOTE:  These values must match the corresponding PAGER_ values in
  8763 ** pager.h.
  8764 */
  8765 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
  8766 #define BTREE_MEMORY        2  /* This is an in-memory DB */
  8767 #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
  8768 #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
  8770 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  8771 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  8772 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  8773 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  8774 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  8775 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  8776 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  8777 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  8778 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  8779 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
  8780 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  8781 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
  8782 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
  8783 #endif
  8784 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  8785 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  8786 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  8787 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  8788 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  8789 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  8790 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
  8791 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  8792 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  8793 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  8794 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  8795 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
  8796 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  8797 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
  8798 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  8799 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
  8801 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
  8802 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
  8803 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
  8805 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
  8807 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  8808 ** of the flags shown below.
  8809 **
  8810 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
  8811 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
  8812 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
  8813 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
  8814 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
  8815 ** indices.)
  8816 */
  8817 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
  8818 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
  8820 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  8821 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  8822 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  8824 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  8825 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  8827 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
  8829 /*
  8830 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  8831 ** should be one of the following values. The integer values are assigned 
  8832 ** to constants so that the offset of the corresponding field in an
  8833 ** SQLite database header may be found using the following formula:
  8834 **
  8835 **   offset = 36 + (idx * 4)
  8836 **
  8837 ** For example, the free-page-count field is located at byte offset 36 of
  8838 ** the database file header. The incr-vacuum-flag field is located at
  8839 ** byte offset 64 (== 36+4*7).
  8840 */
  8841 #define BTREE_FREE_PAGE_COUNT     0
  8842 #define BTREE_SCHEMA_VERSION      1
  8843 #define BTREE_FILE_FORMAT         2
  8844 #define BTREE_DEFAULT_CACHE_SIZE  3
  8845 #define BTREE_LARGEST_ROOT_PAGE   4
  8846 #define BTREE_TEXT_ENCODING       5
  8847 #define BTREE_USER_VERSION        6
  8848 #define BTREE_INCR_VACUUM         7
  8849 #define BTREE_APPLICATION_ID      8
  8851 /*
  8852 ** Values that may be OR'd together to form the second argument of an
  8853 ** sqlite3BtreeCursorHints() call.
  8854 */
  8855 #define BTREE_BULKLOAD 0x00000001
  8857 SQLITE_PRIVATE int sqlite3BtreeCursor(
  8858   Btree*,                              /* BTree containing table to open */
  8859   int iTable,                          /* Index of root page */
  8860   int wrFlag,                          /* 1 for writing.  0 for read-only */
  8861   struct KeyInfo*,                     /* First argument to compare function */
  8862   BtCursor *pCursor                    /* Space to write cursor structure */
  8863 );
  8864 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
  8865 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
  8867 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
  8868 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  8869   BtCursor*,
  8870   UnpackedRecord *pUnKey,
  8871   i64 intKey,
  8872   int bias,
  8873   int *pRes
  8874 );
  8875 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  8876 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
  8877 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  8878                                   const void *pData, int nData,
  8879                                   int nZero, int bias, int seekResult);
  8880 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
  8881 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
  8882 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
  8883 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
  8884 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  8885 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  8886 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  8887 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
  8888 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
  8889 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  8890 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  8892 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8893 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8895 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8896 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  8897 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  8898 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  8899 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  8901 #ifndef NDEBUG
  8902 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  8903 #endif
  8905 #ifndef SQLITE_OMIT_BTREECOUNT
  8906 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
  8907 #endif
  8909 #ifdef SQLITE_TEST
  8910 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  8911 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  8912 #endif
  8914 #ifndef SQLITE_OMIT_WAL
  8915 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  8916 #endif
  8918 /*
  8919 ** If we are not using shared cache, then there is no need to
  8920 ** use mutexes to access the BtShared structures.  So make the
  8921 ** Enter and Leave procedures no-ops.
  8922 */
  8923 #ifndef SQLITE_OMIT_SHARED_CACHE
  8924 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
  8925 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
  8926 #else
  8927 # define sqlite3BtreeEnter(X) 
  8928 # define sqlite3BtreeEnterAll(X)
  8929 #endif
  8931 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  8932 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
  8933 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
  8934 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
  8935 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
  8936 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
  8937 #ifndef NDEBUG
  8938   /* These routines are used inside assert() statements only. */
  8939 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
  8940 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  8941 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  8942 #endif
  8943 #else
  8945 # define sqlite3BtreeSharable(X) 0
  8946 # define sqlite3BtreeLeave(X)
  8947 # define sqlite3BtreeEnterCursor(X)
  8948 # define sqlite3BtreeLeaveCursor(X)
  8949 # define sqlite3BtreeLeaveAll(X)
  8951 # define sqlite3BtreeHoldsMutex(X) 1
  8952 # define sqlite3BtreeHoldsAllMutexes(X) 1
  8953 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
  8954 #endif
  8957 #endif /* _BTREE_H_ */
  8959 /************** End of btree.h ***********************************************/
  8960 /************** Continuing where we left off in sqliteInt.h ******************/
  8961 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
  8962 /************** Begin file vdbe.h ********************************************/
  8963 /*
  8964 ** 2001 September 15
  8965 **
  8966 ** The author disclaims copyright to this source code.  In place of
  8967 ** a legal notice, here is a blessing:
  8968 **
  8969 **    May you do good and not evil.
  8970 **    May you find forgiveness for yourself and forgive others.
  8971 **    May you share freely, never taking more than you give.
  8972 **
  8973 *************************************************************************
  8974 ** Header file for the Virtual DataBase Engine (VDBE)
  8975 **
  8976 ** This header defines the interface to the virtual database engine
  8977 ** or VDBE.  The VDBE implements an abstract machine that runs a
  8978 ** simple program to access and modify the underlying database.
  8979 */
  8980 #ifndef _SQLITE_VDBE_H_
  8981 #define _SQLITE_VDBE_H_
  8982 /* #include <stdio.h> */
  8984 /*
  8985 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
  8986 ** in the source file sqliteVdbe.c are allowed to see the insides
  8987 ** of this structure.
  8988 */
  8989 typedef struct Vdbe Vdbe;
  8991 /*
  8992 ** The names of the following types declared in vdbeInt.h are required
  8993 ** for the VdbeOp definition.
  8994 */
  8995 typedef struct Mem Mem;
  8996 typedef struct SubProgram SubProgram;
  8998 /*
  8999 ** A single instruction of the virtual machine has an opcode
  9000 ** and as many as three operands.  The instruction is recorded
  9001 ** as an instance of the following structure:
  9002 */
  9003 struct VdbeOp {
  9004   u8 opcode;          /* What operation to perform */
  9005   signed char p4type; /* One of the P4_xxx constants for p4 */
  9006   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
  9007   u8 p5;              /* Fifth parameter is an unsigned character */
  9008   int p1;             /* First operand */
  9009   int p2;             /* Second parameter (often the jump destination) */
  9010   int p3;             /* The third parameter */
  9011   union {             /* fourth parameter */
  9012     int i;                 /* Integer value if p4type==P4_INT32 */
  9013     void *p;               /* Generic pointer */
  9014     char *z;               /* Pointer to data for string (char array) types */
  9015     i64 *pI64;             /* Used when p4type is P4_INT64 */
  9016     double *pReal;         /* Used when p4type is P4_REAL */
  9017     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
  9018     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
  9019     Mem *pMem;             /* Used when p4type is P4_MEM */
  9020     VTable *pVtab;         /* Used when p4type is P4_VTAB */
  9021     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
  9022     int *ai;               /* Used when p4type is P4_INTARRAY */
  9023     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
  9024     int (*xAdvance)(BtCursor *, int *);
  9025   } p4;
  9026 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  9027   char *zComment;          /* Comment to improve readability */
  9028 #endif
  9029 #ifdef VDBE_PROFILE
  9030   u32 cnt;                 /* Number of times this instruction was executed */
  9031   u64 cycles;              /* Total time spent executing this instruction */
  9032 #endif
  9033 #ifdef SQLITE_VDBE_COVERAGE
  9034   int iSrcLine;            /* Source-code line that generated this opcode */
  9035 #endif
  9036 };
  9037 typedef struct VdbeOp VdbeOp;
  9040 /*
  9041 ** A sub-routine used to implement a trigger program.
  9042 */
  9043 struct SubProgram {
  9044   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
  9045   int nOp;                      /* Elements in aOp[] */
  9046   int nMem;                     /* Number of memory cells required */
  9047   int nCsr;                     /* Number of cursors required */
  9048   int nOnce;                    /* Number of OP_Once instructions */
  9049   void *token;                  /* id that may be used to recursive triggers */
  9050   SubProgram *pNext;            /* Next sub-program already visited */
  9051 };
  9053 /*
  9054 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  9055 ** it takes up less space.
  9056 */
  9057 struct VdbeOpList {
  9058   u8 opcode;          /* What operation to perform */
  9059   signed char p1;     /* First operand */
  9060   signed char p2;     /* Second parameter (often the jump destination) */
  9061   signed char p3;     /* Third parameter */
  9062 };
  9063 typedef struct VdbeOpList VdbeOpList;
  9065 /*
  9066 ** Allowed values of VdbeOp.p4type
  9067 */
  9068 #define P4_NOTUSED    0   /* The P4 parameter is not used */
  9069 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
  9070 #define P4_STATIC   (-2)  /* Pointer to a static string */
  9071 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
  9072 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
  9073 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
  9074 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
  9075 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
  9076 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  9077 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  9078 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
  9079 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
  9080 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
  9081 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  9082 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
  9083 #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  9085 /* Error message codes for OP_Halt */
  9086 #define P5_ConstraintNotNull 1
  9087 #define P5_ConstraintUnique  2
  9088 #define P5_ConstraintCheck   3
  9089 #define P5_ConstraintFK      4
  9091 /*
  9092 ** The Vdbe.aColName array contains 5n Mem structures, where n is the 
  9093 ** number of columns of data returned by the statement.
  9094 */
  9095 #define COLNAME_NAME     0
  9096 #define COLNAME_DECLTYPE 1
  9097 #define COLNAME_DATABASE 2
  9098 #define COLNAME_TABLE    3
  9099 #define COLNAME_COLUMN   4
  9100 #ifdef SQLITE_ENABLE_COLUMN_METADATA
  9101 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
  9102 #else
  9103 # ifdef SQLITE_OMIT_DECLTYPE
  9104 #   define COLNAME_N      1      /* Store only the name */
  9105 # else
  9106 #   define COLNAME_N      2      /* Store the name and decltype */
  9107 # endif
  9108 #endif
  9110 /*
  9111 ** The following macro converts a relative address in the p2 field
  9112 ** of a VdbeOp structure into a negative number so that 
  9113 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
  9114 ** the macro again restores the address.
  9115 */
  9116 #define ADDR(X)  (-1-(X))
  9118 /*
  9119 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  9120 ** header file that defines a number for each opcode used by the VDBE.
  9121 */
  9122 /************** Include opcodes.h in the middle of vdbe.h ********************/
  9123 /************** Begin file opcodes.h *****************************************/
  9124 /* Automatically generated.  Do not edit */
  9125 /* See the mkopcodeh.awk script for details */
  9126 #define OP_Function        1 /* synopsis: r[P3]=func(r[P2@P5])             */
  9127 #define OP_Savepoint       2
  9128 #define OP_AutoCommit      3
  9129 #define OP_Transaction     4
  9130 #define OP_SorterNext      5
  9131 #define OP_PrevIfOpen      6
  9132 #define OP_NextIfOpen      7
  9133 #define OP_Prev            8
  9134 #define OP_Next            9
  9135 #define OP_AggStep        10 /* synopsis: accum=r[P3] step(r[P2@P5])       */
  9136 #define OP_Checkpoint     11
  9137 #define OP_JournalMode    12
  9138 #define OP_Vacuum         13
  9139 #define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
  9140 #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
  9141 #define OP_Goto           16
  9142 #define OP_Gosub          17
  9143 #define OP_Return         18
  9144 #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
  9145 #define OP_InitCoroutine  20
  9146 #define OP_EndCoroutine   21
  9147 #define OP_Yield          22
  9148 #define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
  9149 #define OP_Halt           24
  9150 #define OP_Integer        25 /* synopsis: r[P2]=P1                         */
  9151 #define OP_Int64          26 /* synopsis: r[P2]=P4                         */
  9152 #define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
  9153 #define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
  9154 #define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
  9155 #define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
  9156 #define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
  9157 #define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
  9158 #define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
  9159 #define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
  9160 #define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
  9161 #define OP_CollSeq        36
  9162 #define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
  9163 #define OP_MustBeInt      38
  9164 #define OP_RealAffinity   39
  9165 #define OP_Permutation    40
  9166 #define OP_Compare        41
  9167 #define OP_Jump           42
  9168 #define OP_Once           43
  9169 #define OP_If             44
  9170 #define OP_IfNot          45
  9171 #define OP_Column         46 /* synopsis: r[P3]=PX                         */
  9172 #define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
  9173 #define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
  9174 #define OP_Count          49 /* synopsis: r[P2]=count()                    */
  9175 #define OP_ReadCookie     50
  9176 #define OP_SetCookie      51
  9177 #define OP_OpenRead       52 /* synopsis: root=P2 iDb=P3                   */
  9178 #define OP_OpenWrite      53 /* synopsis: root=P2 iDb=P3                   */
  9179 #define OP_OpenAutoindex  54 /* synopsis: nColumn=P2                       */
  9180 #define OP_OpenEphemeral  55 /* synopsis: nColumn=P2                       */
  9181 #define OP_SorterOpen     56
  9182 #define OP_OpenPseudo     57 /* synopsis: P3 columns in r[P2]              */
  9183 #define OP_Close          58
  9184 #define OP_SeekLT         59
  9185 #define OP_SeekLE         60
  9186 #define OP_SeekGE         61
  9187 #define OP_SeekGT         62
  9188 #define OP_Seek           63 /* synopsis: intkey=r[P2]                     */
  9189 #define OP_NoConflict     64 /* synopsis: key=r[P3@P4]                     */
  9190 #define OP_NotFound       65 /* synopsis: key=r[P3@P4]                     */
  9191 #define OP_Found          66 /* synopsis: key=r[P3@P4]                     */
  9192 #define OP_NotExists      67 /* synopsis: intkey=r[P3]                     */
  9193 #define OP_Sequence       68 /* synopsis: r[P2]=rowid                      */
  9194 #define OP_NewRowid       69 /* synopsis: r[P2]=rowid                      */
  9195 #define OP_Insert         70 /* synopsis: intkey=r[P3] data=r[P2]          */
  9196 #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
  9197 #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
  9198 #define OP_InsertInt      73 /* synopsis: intkey=P3 data=r[P2]             */
  9199 #define OP_Delete         74
  9200 #define OP_ResetCount     75
  9201 #define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
  9202 #define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
  9203 #define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
  9204 #define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
  9205 #define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
  9206 #define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
  9207 #define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
  9208 #define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
  9209 #define OP_SorterCompare  84 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
  9210 #define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
  9211 #define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
  9212 #define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
  9213 #define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
  9214 #define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
  9215 #define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
  9216 #define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
  9217 #define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
  9218 #define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
  9219 #define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
  9220 #define OP_SorterData     95 /* synopsis: r[P2]=data                       */
  9221 #define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
  9222 #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
  9223 #define OP_RowKey         98 /* synopsis: r[P2]=key                        */
  9224 #define OP_RowData        99 /* synopsis: r[P2]=data                       */
  9225 #define OP_Rowid         100 /* synopsis: r[P2]=rowid                      */
  9226 #define OP_NullRow       101
  9227 #define OP_Last          102
  9228 #define OP_SorterSort    103
  9229 #define OP_Sort          104
  9230 #define OP_Rewind        105
  9231 #define OP_SorterInsert  106
  9232 #define OP_IdxInsert     107 /* synopsis: key=r[P2]                        */
  9233 #define OP_IdxDelete     108 /* synopsis: key=r[P2@P3]                     */
  9234 #define OP_IdxRowid      109 /* synopsis: r[P2]=rowid                      */
  9235 #define OP_IdxLE         110 /* synopsis: key=r[P3@P4]                     */
  9236 #define OP_IdxGT         111 /* synopsis: key=r[P3@P4]                     */
  9237 #define OP_IdxLT         112 /* synopsis: key=r[P3@P4]                     */
  9238 #define OP_IdxGE         113 /* synopsis: key=r[P3@P4]                     */
  9239 #define OP_Destroy       114
  9240 #define OP_Clear         115
  9241 #define OP_CreateIndex   116 /* synopsis: r[P2]=root iDb=P1                */
  9242 #define OP_CreateTable   117 /* synopsis: r[P2]=root iDb=P1                */
  9243 #define OP_ParseSchema   118
  9244 #define OP_LoadAnalysis  119
  9245 #define OP_DropTable     120
  9246 #define OP_DropIndex     121
  9247 #define OP_DropTrigger   122
  9248 #define OP_IntegrityCk   123
  9249 #define OP_RowSetAdd     124 /* synopsis: rowset(P1)=r[P2]                 */
  9250 #define OP_RowSetRead    125 /* synopsis: r[P3]=rowset(P1)                 */
  9251 #define OP_RowSetTest    126 /* synopsis: if r[P3] in rowset(P1) goto P2   */
  9252 #define OP_Program       127
  9253 #define OP_Param         128
  9254 #define OP_FkCounter     129 /* synopsis: fkctr[P1]+=P2                    */
  9255 #define OP_FkIfZero      130 /* synopsis: if fkctr[P1]==0 goto P2          */
  9256 #define OP_MemMax        131 /* synopsis: r[P1]=max(r[P1],r[P2])           */
  9257 #define OP_IfPos         132 /* synopsis: if r[P1]>0 goto P2               */
  9258 #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
  9259 #define OP_IfNeg         134 /* synopsis: if r[P1]<0 goto P2               */
  9260 #define OP_IfZero        135 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
  9261 #define OP_AggFinal      136 /* synopsis: accum=r[P1] N=P2                 */
  9262 #define OP_IncrVacuum    137
  9263 #define OP_Expire        138
  9264 #define OP_TableLock     139 /* synopsis: iDb=P1 root=P2 write=P3          */
  9265 #define OP_VBegin        140
  9266 #define OP_VCreate       141
  9267 #define OP_VDestroy      142
  9268 #define OP_ToText        143 /* same as TK_TO_TEXT                         */
  9269 #define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
  9270 #define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
  9271 #define OP_ToInt         146 /* same as TK_TO_INT                          */
  9272 #define OP_ToReal        147 /* same as TK_TO_REAL                         */
  9273 #define OP_VOpen         148
  9274 #define OP_VColumn       149 /* synopsis: r[P3]=vcolumn(P2)                */
  9275 #define OP_VNext         150
  9276 #define OP_VRename       151
  9277 #define OP_Pagecount     152
  9278 #define OP_MaxPgcnt      153
  9279 #define OP_Init          154 /* synopsis: Start at P2                      */
  9280 #define OP_Noop          155
  9281 #define OP_Explain       156
  9284 /* Properties such as "out2" or "jump" that are specified in
  9285 ** comments following the "case" for each opcode in the vdbe.c
  9286 ** are encoded into bitvectors as follows:
  9287 */
  9288 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
  9289 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
  9290 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
  9291 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
  9292 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
  9293 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
  9294 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
  9295 #define OPFLG_INITIALIZER {\
  9296 /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
  9297 /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
  9298 /*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
  9299 /*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
  9300 /*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
  9301 /*  40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
  9302 /*  48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
  9303 /*  56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
  9304 /*  64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
  9305 /*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
  9306 /*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
  9307 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
  9308 /*  96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
  9309 /* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
  9310 /* 112 */ 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00,\
  9311 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01,\
  9312 /* 128 */ 0x02, 0x00, 0x01, 0x08, 0x05, 0x02, 0x05, 0x05,\
  9313 /* 136 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,\
  9314 /* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x01, 0x00,\
  9315 /* 152 */ 0x02, 0x02, 0x01, 0x00, 0x00,}
  9317 /************** End of opcodes.h *********************************************/
  9318 /************** Continuing where we left off in vdbe.h ***********************/
  9320 /*
  9321 ** Prototypes for the VDBE interface.  See comments on the implementation
  9322 ** for a description of what each of these routines does.
  9323 */
  9324 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
  9325 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  9326 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  9327 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  9328 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  9329 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  9330 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  9331 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
  9332 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  9333 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  9334 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  9335 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  9336 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  9337 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  9338 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  9339 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
  9340 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  9341 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
  9342 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  9343 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  9344 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  9345 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  9346 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  9347 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  9348 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  9349 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
  9350 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
  9351 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
  9352 #ifdef SQLITE_DEBUG
  9353 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  9354 #endif
  9355 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
  9356 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
  9357 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
  9358 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
  9359 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  9360 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
  9361 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
  9362 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  9363 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  9364 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  9365 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
  9366 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  9367 #ifndef SQLITE_OMIT_TRACE
  9368 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  9369 #endif
  9371 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  9372 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
  9373 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  9375 typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int);
  9376 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
  9378 #ifndef SQLITE_OMIT_TRIGGER
  9379 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  9380 #endif
  9382 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
  9383 ** each VDBE opcode.
  9384 **
  9385 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
  9386 ** comments in VDBE programs that show key decision points in the code
  9387 ** generator.
  9388 */
  9389 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  9390 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
  9391 # define VdbeComment(X)  sqlite3VdbeComment X
  9392 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  9393 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
  9394 # ifdef SQLITE_ENABLE_MODULE_COMMENTS
  9395 #   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
  9396 # else
  9397 #   define VdbeModuleComment(X)
  9398 # endif
  9399 #else
  9400 # define VdbeComment(X)
  9401 # define VdbeNoopComment(X)
  9402 # define VdbeModuleComment(X)
  9403 #endif
  9405 /*
  9406 ** The VdbeCoverage macros are used to set a coverage testing point
  9407 ** for VDBE branch instructions.  The coverage testing points are line
  9408 ** numbers in the sqlite3.c source file.  VDBE branch coverage testing
  9409 ** only works with an amalagmation build.  That's ok since a VDBE branch
  9410 ** coverage build designed for testing the test suite only.  No application
  9411 ** should ever ship with VDBE branch coverage measuring turned on.
  9412 **
  9413 **    VdbeCoverage(v)                  // Mark the previously coded instruction
  9414 **                                     // as a branch
  9415 **
  9416 **    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
  9417 **
  9418 **    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
  9419 **
  9420 **    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
  9421 **
  9422 ** Every VDBE branch operation must be tagged with one of the macros above.
  9423 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
  9424 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
  9425 ** routine in vdbe.c, alerting the developer to the missed tag.
  9426 */
  9427 #ifdef SQLITE_VDBE_COVERAGE
  9428 SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
  9429 # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
  9430 # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
  9431 # define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
  9432 # define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
  9433 # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
  9434 #else
  9435 # define VdbeCoverage(v)
  9436 # define VdbeCoverageIf(v,x)
  9437 # define VdbeCoverageAlwaysTaken(v)
  9438 # define VdbeCoverageNeverTaken(v)
  9439 # define VDBE_OFFSET_LINENO(x) 0
  9440 #endif
  9442 #endif
  9444 /************** End of vdbe.h ************************************************/
  9445 /************** Continuing where we left off in sqliteInt.h ******************/
  9446 /************** Include pager.h in the middle of sqliteInt.h *****************/
  9447 /************** Begin file pager.h *******************************************/
  9448 /*
  9449 ** 2001 September 15
  9450 **
  9451 ** The author disclaims copyright to this source code.  In place of
  9452 ** a legal notice, here is a blessing:
  9453 **
  9454 **    May you do good and not evil.
  9455 **    May you find forgiveness for yourself and forgive others.
  9456 **    May you share freely, never taking more than you give.
  9457 **
  9458 *************************************************************************
  9459 ** This header file defines the interface that the sqlite page cache
  9460 ** subsystem.  The page cache subsystem reads and writes a file a page
  9461 ** at a time and provides a journal for rollback.
  9462 */
  9464 #ifndef _PAGER_H_
  9465 #define _PAGER_H_
  9467 /*
  9468 ** Default maximum size for persistent journal files. A negative 
  9469 ** value means no limit. This value may be overridden using the 
  9470 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
  9471 */
  9472 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  9473   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
  9474 #endif
  9476 /*
  9477 ** The type used to represent a page number.  The first page in a file
  9478 ** is called page 1.  0 is used to represent "not a page".
  9479 */
  9480 typedef u32 Pgno;
  9482 /*
  9483 ** Each open file is managed by a separate instance of the "Pager" structure.
  9484 */
  9485 typedef struct Pager Pager;
  9487 /*
  9488 ** Handle type for pages.
  9489 */
  9490 typedef struct PgHdr DbPage;
  9492 /*
  9493 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
  9494 ** reserved for working around a windows/posix incompatibility). It is
  9495 ** used in the journal to signify that the remainder of the journal file 
  9496 ** is devoted to storing a master journal name - there are no more pages to
  9497 ** roll back. See comments for function writeMasterJournal() in pager.c 
  9498 ** for details.
  9499 */
  9500 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
  9502 /*
  9503 ** Allowed values for the flags parameter to sqlite3PagerOpen().
  9504 **
  9505 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
  9506 */
  9507 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
  9508 #define PAGER_MEMORY        0x0002    /* In-memory database */
  9510 /*
  9511 ** Valid values for the second argument to sqlite3PagerLockingMode().
  9512 */
  9513 #define PAGER_LOCKINGMODE_QUERY      -1
  9514 #define PAGER_LOCKINGMODE_NORMAL      0
  9515 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
  9517 /*
  9518 ** Numeric constants that encode the journalmode.  
  9519 */
  9520 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
  9521 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
  9522 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
  9523 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
  9524 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
  9525 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
  9526 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
  9528 /*
  9529 ** Flags that make up the mask passed to sqlite3PagerAcquire().
  9530 */
  9531 #define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
  9532 #define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
  9534 /*
  9535 ** Flags for sqlite3PagerSetFlags()
  9536 */
  9537 #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
  9538 #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
  9539 #define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
  9540 #define PAGER_SYNCHRONOUS_MASK      0x03  /* Mask for three values above */
  9541 #define PAGER_FULLFSYNC             0x04  /* PRAGMA fullfsync=ON */
  9542 #define PAGER_CKPT_FULLFSYNC        0x08  /* PRAGMA checkpoint_fullfsync=ON */
  9543 #define PAGER_CACHESPILL            0x10  /* PRAGMA cache_spill=ON */
  9544 #define PAGER_FLAGS_MASK            0x1c  /* All above except SYNCHRONOUS */
  9546 /*
  9547 ** The remainder of this file contains the declarations of the functions
  9548 ** that make up the Pager sub-system API. See source code comments for 
  9549 ** a detailed description of each routine.
  9550 */
  9552 /* Open and close a Pager connection. */ 
  9553 SQLITE_PRIVATE int sqlite3PagerOpen(
  9554   sqlite3_vfs*,
  9555   Pager **ppPager,
  9556   const char*,
  9557   int,
  9558   int,
  9559   int,
  9560   void(*)(DbPage*)
  9561 );
  9562 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
  9563 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
  9565 /* Functions used to configure a Pager object. */
  9566 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
  9567 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
  9568 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
  9569 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
  9570 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
  9571 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
  9572 SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
  9573 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
  9574 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
  9575 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
  9576 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
  9577 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
  9578 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
  9580 /* Functions used to obtain and release page references. */ 
  9581 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
  9582 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
  9583 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  9584 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  9585 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  9586 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
  9588 /* Operations on page references. */
  9589 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  9590 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
  9591 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
  9592 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
  9593 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
  9594 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
  9596 /* Functions used to manage pager transactions and savepoints. */
  9597 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
  9598 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  9599 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  9600 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  9601 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
  9602 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  9603 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  9604 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  9605 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  9606 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  9608 #ifndef SQLITE_OMIT_WAL
  9609 SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
  9610 SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
  9611 SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
  9612 SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
  9613 SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
  9614 #endif
  9616 #ifdef SQLITE_ENABLE_ZIPVFS
  9617 SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
  9618 #endif
  9620 /* Functions used to query pager state and configuration. */
  9621 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
  9622 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
  9623 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
  9624 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
  9625 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  9626 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  9627 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  9628 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  9629 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  9630 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  9631 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  9632 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
  9633 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
  9635 /* Functions used to truncate the database file. */
  9636 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  9638 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  9639 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  9640 #endif
  9642 /* Functions to support testing and debugging. */
  9643 #if !defined(NDEBUG) || defined(SQLITE_TEST)
  9644 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
  9645 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
  9646 #endif
  9647 #ifdef SQLITE_TEST
  9648 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
  9649 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
  9650   void disable_simulated_io_errors(void);
  9651   void enable_simulated_io_errors(void);
  9652 #else
  9653 # define disable_simulated_io_errors()
  9654 # define enable_simulated_io_errors()
  9655 #endif
  9657 #endif /* _PAGER_H_ */
  9659 /************** End of pager.h ***********************************************/
  9660 /************** Continuing where we left off in sqliteInt.h ******************/
  9661 /************** Include pcache.h in the middle of sqliteInt.h ****************/
  9662 /************** Begin file pcache.h ******************************************/
  9663 /*
  9664 ** 2008 August 05
  9665 **
  9666 ** The author disclaims copyright to this source code.  In place of
  9667 ** a legal notice, here is a blessing:
  9668 **
  9669 **    May you do good and not evil.
  9670 **    May you find forgiveness for yourself and forgive others.
  9671 **    May you share freely, never taking more than you give.
  9672 **
  9673 *************************************************************************
  9674 ** This header file defines the interface that the sqlite page cache
  9675 ** subsystem. 
  9676 */
  9678 #ifndef _PCACHE_H_
  9680 typedef struct PgHdr PgHdr;
  9681 typedef struct PCache PCache;
  9683 /*
  9684 ** Every page in the cache is controlled by an instance of the following
  9685 ** structure.
  9686 */
  9687 struct PgHdr {
  9688   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
  9689   void *pData;                   /* Page data */
  9690   void *pExtra;                  /* Extra content */
  9691   PgHdr *pDirty;                 /* Transient list of dirty pages */
  9692   Pager *pPager;                 /* The pager this page is part of */
  9693   Pgno pgno;                     /* Page number for this page */
  9694 #ifdef SQLITE_CHECK_PAGES
  9695   u32 pageHash;                  /* Hash of page content */
  9696 #endif
  9697   u16 flags;                     /* PGHDR flags defined below */
  9699   /**********************************************************************
  9700   ** Elements above are public.  All that follows is private to pcache.c
  9701   ** and should not be accessed by other modules.
  9702   */
  9703   i16 nRef;                      /* Number of users of this page */
  9704   PCache *pCache;                /* Cache that owns this page */
  9706   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
  9707   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
  9708 };
  9710 /* Bit values for PgHdr.flags */
  9711 #define PGHDR_DIRTY             0x002  /* Page has changed */
  9712 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
  9713                                        ** writing this page to the database */
  9714 #define PGHDR_NEED_READ         0x008  /* Content is unread */
  9715 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
  9716 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
  9718 #define PGHDR_MMAP              0x040  /* This is an mmap page object */
  9720 /* Initialize and shutdown the page cache subsystem */
  9721 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  9722 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
  9724 /* Page cache buffer management:
  9725 ** These routines implement SQLITE_CONFIG_PAGECACHE.
  9726 */
  9727 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
  9729 /* Create a new pager cache.
  9730 ** Under memory stress, invoke xStress to try to make pages clean.
  9731 ** Only clean and unpinned pages can be reclaimed.
  9732 */
  9733 SQLITE_PRIVATE void sqlite3PcacheOpen(
  9734   int szPage,                    /* Size of every page */
  9735   int szExtra,                   /* Extra space associated with each page */
  9736   int bPurgeable,                /* True if pages are on backing store */
  9737   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  9738   void *pStress,                 /* Argument to xStress */
  9739   PCache *pToInit                /* Preallocated space for the PCache */
  9740 );
  9742 /* Modify the page-size after the cache has been created. */
  9743 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
  9745 /* Return the size in bytes of a PCache object.  Used to preallocate
  9746 ** storage space.
  9747 */
  9748 SQLITE_PRIVATE int sqlite3PcacheSize(void);
  9750 /* One release per successful fetch.  Page is pinned until released.
  9751 ** Reference counted. 
  9752 */
  9753 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  9754 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
  9756 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
  9757 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
  9758 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
  9759 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
  9761 /* Change a page number.  Used by incr-vacuum. */
  9762 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  9764 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
  9765 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
  9767 /* Get a list of all dirty pages in the cache, sorted by page number */
  9768 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
  9770 /* Reset and close the cache object */
  9771 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
  9773 /* Clear flags from pages of the page cache */
  9774 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
  9776 /* Discard the contents of the cache */
  9777 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
  9779 /* Return the total number of outstanding page references */
  9780 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
  9782 /* Increment the reference count of an existing page */
  9783 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
  9785 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
  9787 /* Return the total number of pages stored in the cache */
  9788 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
  9790 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  9791 /* Iterate through all dirty pages currently stored in the cache. This
  9792 ** interface is only available if SQLITE_CHECK_PAGES is defined when the 
  9793 ** library is built.
  9794 */
  9795 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  9796 #endif
  9798 /* Set and get the suggested cache-size for the specified pager-cache.
  9799 **
  9800 ** If no global maximum is configured, then the system attempts to limit
  9801 ** the total number of pages cached by purgeable pager-caches to the sum
  9802 ** of the suggested cache-sizes.
  9803 */
  9804 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
  9805 #ifdef SQLITE_TEST
  9806 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
  9807 #endif
  9809 /* Free up as much memory as possible from the page cache */
  9810 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
  9812 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  9813 /* Try to return memory used by the pcache module to the main memory heap */
  9814 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
  9815 #endif
  9817 #ifdef SQLITE_TEST
  9818 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
  9819 #endif
  9821 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
  9823 #endif /* _PCACHE_H_ */
  9825 /************** End of pcache.h **********************************************/
  9826 /************** Continuing where we left off in sqliteInt.h ******************/
  9828 /************** Include os.h in the middle of sqliteInt.h ********************/
  9829 /************** Begin file os.h **********************************************/
  9830 /*
  9831 ** 2001 September 16
  9832 **
  9833 ** The author disclaims copyright to this source code.  In place of
  9834 ** a legal notice, here is a blessing:
  9835 **
  9836 **    May you do good and not evil.
  9837 **    May you find forgiveness for yourself and forgive others.
  9838 **    May you share freely, never taking more than you give.
  9839 **
  9840 ******************************************************************************
  9841 **
  9842 ** This header file (together with is companion C source-code file
  9843 ** "os.c") attempt to abstract the underlying operating system so that
  9844 ** the SQLite library will work on both POSIX and windows systems.
  9845 **
  9846 ** This header file is #include-ed by sqliteInt.h and thus ends up
  9847 ** being included by every source file.
  9848 */
  9849 #ifndef _SQLITE_OS_H_
  9850 #define _SQLITE_OS_H_
  9852 /*
  9853 ** Figure out if we are dealing with Unix, Windows, or some other
  9854 ** operating system.  After the following block of preprocess macros,
  9855 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  9856 ** will defined to either 1 or 0.  One of the four will be 1.  The other 
  9857 ** three will be 0.
  9858 */
  9859 #if defined(SQLITE_OS_OTHER)
  9860 # if SQLITE_OS_OTHER==1
  9861 #   undef SQLITE_OS_UNIX
  9862 #   define SQLITE_OS_UNIX 0
  9863 #   undef SQLITE_OS_WIN
  9864 #   define SQLITE_OS_WIN 0
  9865 # else
  9866 #   undef SQLITE_OS_OTHER
  9867 # endif
  9868 #endif
  9869 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  9870 # define SQLITE_OS_OTHER 0
  9871 # ifndef SQLITE_OS_WIN
  9872 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  9873 #     define SQLITE_OS_WIN 1
  9874 #     define SQLITE_OS_UNIX 0
  9875 #   else
  9876 #     define SQLITE_OS_WIN 0
  9877 #     define SQLITE_OS_UNIX 1
  9878 #  endif
  9879 # else
  9880 #  define SQLITE_OS_UNIX 0
  9881 # endif
  9882 #else
  9883 # ifndef SQLITE_OS_WIN
  9884 #  define SQLITE_OS_WIN 0
  9885 # endif
  9886 #endif
  9888 #if SQLITE_OS_WIN
  9889 # include <windows.h>
  9890 #endif
  9892 /*
  9893 ** Determine if we are dealing with Windows NT.
  9894 **
  9895 ** We ought to be able to determine if we are compiling for win98 or winNT
  9896 ** using the _WIN32_WINNT macro as follows:
  9897 **
  9898 ** #if defined(_WIN32_WINNT)
  9899 ** # define SQLITE_OS_WINNT 1
  9900 ** #else
  9901 ** # define SQLITE_OS_WINNT 0
  9902 ** #endif
  9903 **
  9904 ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  9905 ** so the above test does not work.  We'll just assume that everything is
  9906 ** winNT unless the programmer explicitly says otherwise by setting
  9907 ** SQLITE_OS_WINNT to 0.
  9908 */
  9909 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  9910 # define SQLITE_OS_WINNT 1
  9911 #endif
  9913 /*
  9914 ** Determine if we are dealing with WindowsCE - which has a much
  9915 ** reduced API.
  9916 */
  9917 #if defined(_WIN32_WCE)
  9918 # define SQLITE_OS_WINCE 1
  9919 #else
  9920 # define SQLITE_OS_WINCE 0
  9921 #endif
  9923 /*
  9924 ** Determine if we are dealing with WinRT, which provides only a subset of
  9925 ** the full Win32 API.
  9926 */
  9927 #if !defined(SQLITE_OS_WINRT)
  9928 # define SQLITE_OS_WINRT 0
  9929 #endif
  9931 /* If the SET_FULLSYNC macro is not defined above, then make it
  9932 ** a no-op
  9933 */
  9934 #ifndef SET_FULLSYNC
  9935 # define SET_FULLSYNC(x,y)
  9936 #endif
  9938 /*
  9939 ** The default size of a disk sector
  9940 */
  9941 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
  9942 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
  9943 #endif
  9945 /*
  9946 ** Temporary files are named starting with this prefix followed by 16 random
  9947 ** alphanumeric characters, and no file extension. They are stored in the
  9948 ** OS's standard temporary file directory, and are deleted prior to exit.
  9949 ** If sqlite is being embedded in another program, you may wish to change the
  9950 ** prefix to reflect your program's name, so that if your program exits
  9951 ** prematurely, old temporary files can be easily identified. This can be done
  9952 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  9953 **
  9954 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
  9955 ** Mcafee started using SQLite in their anti-virus product and it
  9956 ** started putting files with the "sqlite" name in the c:/temp folder.
  9957 ** This annoyed many windows users.  Those users would then do a 
  9958 ** Google search for "sqlite", find the telephone numbers of the
  9959 ** developers and call to wake them up at night and complain.
  9960 ** For this reason, the default name prefix is changed to be "sqlite" 
  9961 ** spelled backwards.  So the temp files are still identified, but
  9962 ** anybody smart enough to figure out the code is also likely smart
  9963 ** enough to know that calling the developer will not help get rid
  9964 ** of the file.
  9965 */
  9966 #ifndef SQLITE_TEMP_FILE_PREFIX
  9967 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  9968 #endif
  9970 /*
  9971 ** The following values may be passed as the second argument to
  9972 ** sqlite3OsLock(). The various locks exhibit the following semantics:
  9973 **
  9974 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
  9975 ** RESERVED:  A single process may hold a RESERVED lock on a file at
  9976 **            any time. Other processes may hold and obtain new SHARED locks.
  9977 ** PENDING:   A single process may hold a PENDING lock on a file at
  9978 **            any one time. Existing SHARED locks may persist, but no new
  9979 **            SHARED locks may be obtained by other processes.
  9980 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  9981 **
  9982 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  9983 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  9984 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  9985 ** sqlite3OsLock().
  9986 */
  9987 #define NO_LOCK         0
  9988 #define SHARED_LOCK     1
  9989 #define RESERVED_LOCK   2
  9990 #define PENDING_LOCK    3
  9991 #define EXCLUSIVE_LOCK  4
  9993 /*
  9994 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
  9995 **
  9996 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  9997 ** those functions are not available.  So we use only LockFile() and
  9998 ** UnlockFile().
  9999 **
 10000 ** LockFile() prevents not just writing but also reading by other processes.
 10001 ** A SHARED_LOCK is obtained by locking a single randomly-chosen 
 10002 ** byte out of a specific range of bytes. The lock byte is obtained at 
 10003 ** random so two separate readers can probably access the file at the 
 10004 ** same time, unless they are unlucky and choose the same lock byte.
 10005 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
 10006 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
 10007 ** a single byte of the file that is designated as the reserved lock byte.
 10008 ** A PENDING_LOCK is obtained by locking a designated byte different from
 10009 ** the RESERVED_LOCK byte.
 10010 **
 10011 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
 10012 ** which means we can use reader/writer locks.  When reader/writer locks
 10013 ** are used, the lock is placed on the same range of bytes that is used
 10014 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
 10015 ** will support two or more Win95 readers or two or more WinNT readers.
 10016 ** But a single Win95 reader will lock out all WinNT readers and a single
 10017 ** WinNT reader will lock out all other Win95 readers.
 10018 **
 10019 ** The following #defines specify the range of bytes used for locking.
 10020 ** SHARED_SIZE is the number of bytes available in the pool from which
 10021 ** a random byte is selected for a shared lock.  The pool of bytes for
 10022 ** shared locks begins at SHARED_FIRST. 
 10023 **
 10024 ** The same locking strategy and
 10025 ** byte ranges are used for Unix.  This leaves open the possiblity of having
 10026 ** clients on win95, winNT, and unix all talking to the same shared file
 10027 ** and all locking correctly.  To do so would require that samba (or whatever
 10028 ** tool is being used for file sharing) implements locks correctly between
 10029 ** windows and unix.  I'm guessing that isn't likely to happen, but by
 10030 ** using the same locking range we are at least open to the possibility.
 10031 **
 10032 ** Locking in windows is manditory.  For this reason, we cannot store
 10033 ** actual data in the bytes used for locking.  The pager never allocates
 10034 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
 10035 ** that all locks will fit on a single page even at the minimum page size.
 10036 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
 10037 ** is set high so that we don't have to allocate an unused page except
 10038 ** for very large databases.  But one should test the page skipping logic 
 10039 ** by setting PENDING_BYTE low and running the entire regression suite.
 10040 **
 10041 ** Changing the value of PENDING_BYTE results in a subtly incompatible
 10042 ** file format.  Depending on how it is changed, you might not notice
 10043 ** the incompatibility right away, even running a full regression test.
 10044 ** The default location of PENDING_BYTE is the first byte past the
 10045 ** 1GB boundary.
 10046 **
 10047 */
 10048 #ifdef SQLITE_OMIT_WSD
 10049 # define PENDING_BYTE     (0x40000000)
 10050 #else
 10051 # define PENDING_BYTE      sqlite3PendingByte
 10052 #endif
 10053 #define RESERVED_BYTE     (PENDING_BYTE+1)
 10054 #define SHARED_FIRST      (PENDING_BYTE+2)
 10055 #define SHARED_SIZE       510
 10057 /*
 10058 ** Wrapper around OS specific sqlite3_os_init() function.
 10059 */
 10060 SQLITE_PRIVATE int sqlite3OsInit(void);
 10062 /* 
 10063 ** Functions for accessing sqlite3_file methods 
 10064 */
 10065 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
 10066 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
 10067 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
 10068 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
 10069 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
 10070 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
 10071 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
 10072 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
 10073 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
 10074 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
 10075 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
 10076 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
 10077 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
 10078 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
 10079 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
 10080 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
 10081 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
 10082 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
 10083 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
 10084 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
 10087 /* 
 10088 ** Functions for accessing sqlite3_vfs methods 
 10089 */
 10090 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
 10091 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
 10092 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
 10093 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
 10094 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 10095 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
 10096 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
 10097 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
 10098 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
 10099 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
 10100 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
 10101 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
 10102 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
 10104 /*
 10105 ** Convenience functions for opening and closing files using 
 10106 ** sqlite3_malloc() to obtain space for the file-handle structure.
 10107 */
 10108 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
 10109 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
 10111 #endif /* _SQLITE_OS_H_ */
 10113 /************** End of os.h **************************************************/
 10114 /************** Continuing where we left off in sqliteInt.h ******************/
 10115 /************** Include mutex.h in the middle of sqliteInt.h *****************/
 10116 /************** Begin file mutex.h *******************************************/
 10117 /*
 10118 ** 2007 August 28
 10119 **
 10120 ** The author disclaims copyright to this source code.  In place of
 10121 ** a legal notice, here is a blessing:
 10122 **
 10123 **    May you do good and not evil.
 10124 **    May you find forgiveness for yourself and forgive others.
 10125 **    May you share freely, never taking more than you give.
 10126 **
 10127 *************************************************************************
 10128 **
 10129 ** This file contains the common header for all mutex implementations.
 10130 ** The sqliteInt.h header #includes this file so that it is available
 10131 ** to all source files.  We break it out in an effort to keep the code
 10132 ** better organized.
 10133 **
 10134 ** NOTE:  source files should *not* #include this header file directly.
 10135 ** Source files should #include the sqliteInt.h file and let that file
 10136 ** include this one indirectly.
 10137 */
 10140 /*
 10141 ** Figure out what version of the code to use.  The choices are
 10142 **
 10143 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
 10144 **                             mutexes implemention cannot be overridden
 10145 **                             at start-time.
 10146 **
 10147 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
 10148 **                             mutual exclusion is provided.  But this
 10149 **                             implementation can be overridden at
 10150 **                             start-time.
 10151 **
 10152 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
 10153 **
 10154 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
 10155 */
 10156 #if !SQLITE_THREADSAFE
 10157 # define SQLITE_MUTEX_OMIT
 10158 #endif
 10159 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
 10160 #  if SQLITE_OS_UNIX
 10161 #    define SQLITE_MUTEX_PTHREADS
 10162 #  elif SQLITE_OS_WIN
 10163 #    define SQLITE_MUTEX_W32
 10164 #  else
 10165 #    define SQLITE_MUTEX_NOOP
 10166 #  endif
 10167 #endif
 10169 #ifdef SQLITE_MUTEX_OMIT
 10170 /*
 10171 ** If this is a no-op implementation, implement everything as macros.
 10172 */
 10173 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
 10174 #define sqlite3_mutex_free(X)
 10175 #define sqlite3_mutex_enter(X)    
 10176 #define sqlite3_mutex_try(X)      SQLITE_OK
 10177 #define sqlite3_mutex_leave(X)    
 10178 #define sqlite3_mutex_held(X)     ((void)(X),1)
 10179 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
 10180 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
 10181 #define sqlite3MutexInit()        SQLITE_OK
 10182 #define sqlite3MutexEnd()
 10183 #define MUTEX_LOGIC(X)
 10184 #else
 10185 #define MUTEX_LOGIC(X)            X
 10186 #endif /* defined(SQLITE_MUTEX_OMIT) */
 10188 /************** End of mutex.h ***********************************************/
 10189 /************** Continuing where we left off in sqliteInt.h ******************/
 10192 /*
 10193 ** Each database file to be accessed by the system is an instance
 10194 ** of the following structure.  There are normally two of these structures
 10195 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
 10196 ** aDb[1] is the database file used to hold temporary tables.  Additional
 10197 ** databases may be attached.
 10198 */
 10199 struct Db {
 10200   char *zName;         /* Name of this database */
 10201   Btree *pBt;          /* The B*Tree structure for this database file */
 10202   u8 safety_level;     /* How aggressive at syncing data to disk */
 10203   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
 10204 };
 10206 /*
 10207 ** An instance of the following structure stores a database schema.
 10208 **
 10209 ** Most Schema objects are associated with a Btree.  The exception is
 10210 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
 10211 ** In shared cache mode, a single Schema object can be shared by multiple
 10212 ** Btrees that refer to the same underlying BtShared object.
 10213 ** 
 10214 ** Schema objects are automatically deallocated when the last Btree that
 10215 ** references them is destroyed.   The TEMP Schema is manually freed by
 10216 ** sqlite3_close().
 10218 ** A thread must be holding a mutex on the corresponding Btree in order
 10219 ** to access Schema content.  This implies that the thread must also be
 10220 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
 10221 ** For a TEMP Schema, only the connection mutex is required.
 10222 */
 10223 struct Schema {
 10224   int schema_cookie;   /* Database schema version number for this file */
 10225   int iGeneration;     /* Generation counter.  Incremented with each change */
 10226   Hash tblHash;        /* All tables indexed by name */
 10227   Hash idxHash;        /* All (named) indices indexed by name */
 10228   Hash trigHash;       /* All triggers indexed by name */
 10229   Hash fkeyHash;       /* All foreign keys by referenced table name */
 10230   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
 10231   u8 file_format;      /* Schema format version for this file */
 10232   u8 enc;              /* Text encoding used by this database */
 10233   u16 flags;           /* Flags associated with this schema */
 10234   int cache_size;      /* Number of pages to use in the cache */
 10235 };
 10237 /*
 10238 ** These macros can be used to test, set, or clear bits in the 
 10239 ** Db.pSchema->flags field.
 10240 */
 10241 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
 10242 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
 10243 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
 10244 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
 10246 /*
 10247 ** Allowed values for the DB.pSchema->flags field.
 10248 **
 10249 ** The DB_SchemaLoaded flag is set after the database schema has been
 10250 ** read into internal hash tables.
 10251 **
 10252 ** DB_UnresetViews means that one or more views have column names that
 10253 ** have been filled out.  If the schema changes, these column names might
 10254 ** changes and so the view will need to be reset.
 10255 */
 10256 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
 10257 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
 10258 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
 10260 /*
 10261 ** The number of different kinds of things that can be limited
 10262 ** using the sqlite3_limit() interface.
 10263 */
 10264 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
 10266 /*
 10267 ** Lookaside malloc is a set of fixed-size buffers that can be used
 10268 ** to satisfy small transient memory allocation requests for objects
 10269 ** associated with a particular database connection.  The use of
 10270 ** lookaside malloc provides a significant performance enhancement
 10271 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
 10272 ** SQL statements.
 10273 **
 10274 ** The Lookaside structure holds configuration information about the
 10275 ** lookaside malloc subsystem.  Each available memory allocation in
 10276 ** the lookaside subsystem is stored on a linked list of LookasideSlot
 10277 ** objects.
 10278 **
 10279 ** Lookaside allocations are only allowed for objects that are associated
 10280 ** with a particular database connection.  Hence, schema information cannot
 10281 ** be stored in lookaside because in shared cache mode the schema information
 10282 ** is shared by multiple database connections.  Therefore, while parsing
 10283 ** schema information, the Lookaside.bEnabled flag is cleared so that
 10284 ** lookaside allocations are not used to construct the schema objects.
 10285 */
 10286 struct Lookaside {
 10287   u16 sz;                 /* Size of each buffer in bytes */
 10288   u8 bEnabled;            /* False to disable new lookaside allocations */
 10289   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
 10290   int nOut;               /* Number of buffers currently checked out */
 10291   int mxOut;              /* Highwater mark for nOut */
 10292   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
 10293   LookasideSlot *pFree;   /* List of available buffers */
 10294   void *pStart;           /* First byte of available memory space */
 10295   void *pEnd;             /* First byte past end of available space */
 10296 };
 10297 struct LookasideSlot {
 10298   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
 10299 };
 10301 /*
 10302 ** A hash table for function definitions.
 10303 **
 10304 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
 10305 ** Collisions are on the FuncDef.pHash chain.
 10306 */
 10307 struct FuncDefHash {
 10308   FuncDef *a[23];       /* Hash table for functions */
 10309 };
 10311 /*
 10312 ** Each database connection is an instance of the following structure.
 10313 */
 10314 struct sqlite3 {
 10315   sqlite3_vfs *pVfs;            /* OS Interface */
 10316   struct Vdbe *pVdbe;           /* List of active virtual machines */
 10317   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
 10318   sqlite3_mutex *mutex;         /* Connection mutex */
 10319   Db *aDb;                      /* All backends */
 10320   int nDb;                      /* Number of backends currently in use */
 10321   int flags;                    /* Miscellaneous flags. See below */
 10322   i64 lastRowid;                /* ROWID of most recent insert (see above) */
 10323   i64 szMmap;                   /* Default mmap_size setting */
 10324   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
 10325   int errCode;                  /* Most recent error code (SQLITE_*) */
 10326   int errMask;                  /* & result codes with this before returning */
 10327   u16 dbOptFlags;               /* Flags to enable/disable optimizations */
 10328   u8 autoCommit;                /* The auto-commit flag. */
 10329   u8 temp_store;                /* 1: file 2: memory 0: default */
 10330   u8 mallocFailed;              /* True if we have seen a malloc failure */
 10331   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
 10332   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
 10333   u8 suppressErr;               /* Do not issue error messages if true */
 10334   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
 10335   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
 10336   int nextPagesize;             /* Pagesize after VACUUM if >0 */
 10337   u32 magic;                    /* Magic number for detect library misuse */
 10338   int nChange;                  /* Value returned by sqlite3_changes() */
 10339   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 10340   int aLimit[SQLITE_N_LIMIT];   /* Limits */
 10341   struct sqlite3InitInfo {      /* Information used during initialization */
 10342     int newTnum;                /* Rootpage of table being initialized */
 10343     u8 iDb;                     /* Which db file is being initialized */
 10344     u8 busy;                    /* TRUE if currently initializing */
 10345     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 10346   } init;
 10347   int nVdbeActive;              /* Number of VDBEs currently running */
 10348   int nVdbeRead;                /* Number of active VDBEs that read or write */
 10349   int nVdbeWrite;               /* Number of active VDBEs that read and write */
 10350   int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 10351   int nExtension;               /* Number of loaded extensions */
 10352   void **aExtension;            /* Array of shared library handles */
 10353   void (*xTrace)(void*,const char*);        /* Trace function */
 10354   void *pTraceArg;                          /* Argument to the trace function */
 10355   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
 10356   void *pProfileArg;                        /* Argument to profile function */
 10357   void *pCommitArg;                 /* Argument to xCommitCallback() */   
 10358   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
 10359   void *pRollbackArg;               /* Argument to xRollbackCallback() */   
 10360   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
 10361   void *pUpdateArg;
 10362   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
 10363 #ifndef SQLITE_OMIT_WAL
 10364   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
 10365   void *pWalArg;
 10366 #endif
 10367   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
 10368   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
 10369   void *pCollNeededArg;
 10370   sqlite3_value *pErr;          /* Most recent error message */
 10371   union {
 10372     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
 10373     double notUsed1;            /* Spacer */
 10374   } u1;
 10375   Lookaside lookaside;          /* Lookaside malloc configuration */
 10376 #ifndef SQLITE_OMIT_AUTHORIZATION
 10377   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 10378                                 /* Access authorization function */
 10379   void *pAuthArg;               /* 1st argument to the access auth function */
 10380 #endif
 10381 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 10382   int (*xProgress)(void *);     /* The progress callback */
 10383   void *pProgressArg;           /* Argument to the progress callback */
 10384   unsigned nProgressOps;        /* Number of opcodes for progress callback */
 10385 #endif
 10386 #ifndef SQLITE_OMIT_VIRTUALTABLE
 10387   int nVTrans;                  /* Allocated size of aVTrans */
 10388   Hash aModule;                 /* populated by sqlite3_create_module() */
 10389   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
 10390   VTable **aVTrans;             /* Virtual tables with open transactions */
 10391   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
 10392 #endif
 10393   FuncDefHash aFunc;            /* Hash table of connection functions */
 10394   Hash aCollSeq;                /* All collating sequences */
 10395   BusyHandler busyHandler;      /* Busy callback */
 10396   Db aDbStatic[2];              /* Static space for the 2 default backends */
 10397   Savepoint *pSavepoint;        /* List of active savepoints */
 10398   int busyTimeout;              /* Busy handler timeout, in msec */
 10399   int nSavepoint;               /* Number of non-transaction savepoints */
 10400   int nStatement;               /* Number of nested statement-transactions  */
 10401   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
 10402   i64 nDeferredImmCons;         /* Net deferred immediate constraints */
 10403   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
 10405 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 10406   /* The following variables are all protected by the STATIC_MASTER 
 10407   ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
 10408   **
 10409   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
 10410   ** unlock so that it can proceed.
 10411   **
 10412   ** When X.pBlockingConnection==Y, that means that something that X tried
 10413   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
 10414   ** held by Y.
 10415   */
 10416   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
 10417   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
 10418   void *pUnlockArg;                     /* Argument to xUnlockNotify */
 10419   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
 10420   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
 10421 #endif
 10422 };
 10424 /*
 10425 ** A macro to discover the encoding of a database.
 10426 */
 10427 #define ENC(db) ((db)->aDb[0].pSchema->enc)
 10429 /*
 10430 ** Possible values for the sqlite3.flags.
 10431 */
 10432 #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
 10433 #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
 10434 #define SQLITE_FullFSync      0x00000004  /* Use full fsync on the backend */
 10435 #define SQLITE_CkptFullFSync  0x00000008  /* Use full fsync for checkpoint */
 10436 #define SQLITE_CacheSpill     0x00000010  /* OK to spill pager cache */
 10437 #define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
 10438 #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
 10439 #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
 10440                                           /*   DELETE, or UPDATE and return */
 10441                                           /*   the count using a callback. */
 10442 #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
 10443                                           /*   result set is empty */
 10444 #define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
 10445 #define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
 10446 #define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
 10447 #define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
 10448 #define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
 10449 #define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
 10450 #define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
 10451 #define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
 10452 #define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
 10453 #define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
 10454 #define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
 10455 #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
 10456 #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
 10457 #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
 10458 #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
 10459 #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
 10460 #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
 10461 #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
 10464 /*
 10465 ** Bits of the sqlite3.dbOptFlags field that are used by the
 10466 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
 10467 ** selectively disable various optimizations.
 10468 */
 10469 #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
 10470 #define SQLITE_ColumnCache    0x0002   /* Column cache */
 10471 #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
 10472 #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
 10473 /*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
 10474 #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
 10475 #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
 10476 #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
 10477 #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
 10478 #define SQLITE_Transitive     0x0200   /* Transitive constraints */
 10479 #define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
 10480 #define SQLITE_Stat3          0x0800   /* Use the SQLITE_STAT3 table */
 10481 #define SQLITE_AdjustOutEst   0x1000   /* Adjust output estimates using WHERE */
 10482 #define SQLITE_AllOpts        0xffff   /* All optimizations */
 10484 /*
 10485 ** Macros for testing whether or not optimizations are enabled or disabled.
 10486 */
 10487 #ifndef SQLITE_OMIT_BUILTIN_TEST
 10488 #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 10489 #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
 10490 #else
 10491 #define OptimizationDisabled(db, mask)  0
 10492 #define OptimizationEnabled(db, mask)   1
 10493 #endif
 10495 /*
 10496 ** Return true if it OK to factor constant expressions into the initialization
 10497 ** code. The argument is a Parse object for the code generator.
 10498 */
 10499 #define ConstFactorOk(P) ((P)->okConstFactor)
 10501 /*
 10502 ** Possible values for the sqlite.magic field.
 10503 ** The numbers are obtained at random and have no special meaning, other
 10504 ** than being distinct from one another.
 10505 */
 10506 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
 10507 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
 10508 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
 10509 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
 10510 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
 10511 #define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
 10513 /*
 10514 ** Each SQL function is defined by an instance of the following
 10515 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
 10516 ** hash table.  When multiple functions have the same name, the hash table
 10517 ** points to a linked list of these structures.
 10518 */
 10519 struct FuncDef {
 10520   i16 nArg;            /* Number of arguments.  -1 means unlimited */
 10521   u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
 10522   void *pUserData;     /* User data parameter */
 10523   FuncDef *pNext;      /* Next function with same name */
 10524   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
 10525   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
 10526   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
 10527   char *zName;         /* SQL name of the function. */
 10528   FuncDef *pHash;      /* Next with a different name but the same hash */
 10529   FuncDestructor *pDestructor;   /* Reference counted destructor function */
 10530 };
 10532 /*
 10533 ** This structure encapsulates a user-function destructor callback (as
 10534 ** configured using create_function_v2()) and a reference counter. When
 10535 ** create_function_v2() is called to create a function with a destructor,
 10536 ** a single object of this type is allocated. FuncDestructor.nRef is set to 
 10537 ** the number of FuncDef objects created (either 1 or 3, depending on whether
 10538 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
 10539 ** member of each of the new FuncDef objects is set to point to the allocated
 10540 ** FuncDestructor.
 10541 **
 10542 ** Thereafter, when one of the FuncDef objects is deleted, the reference
 10543 ** count on this object is decremented. When it reaches 0, the destructor
 10544 ** is invoked and the FuncDestructor structure freed.
 10545 */
 10546 struct FuncDestructor {
 10547   int nRef;
 10548   void (*xDestroy)(void *);
 10549   void *pUserData;
 10550 };
 10552 /*
 10553 ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
 10554 ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
 10555 ** are assert() statements in the code to verify this.
 10556 */
 10557 #define SQLITE_FUNC_ENCMASK  0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
 10558 #define SQLITE_FUNC_LIKE     0x004 /* Candidate for the LIKE optimization */
 10559 #define SQLITE_FUNC_CASE     0x008 /* Case-sensitive LIKE-type function */
 10560 #define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
 10561 #define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
 10562 #define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
 10563 #define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
 10564 #define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
 10565 #define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
 10566 #define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
 10567 #define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
 10569 /*
 10570 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
 10571 ** used to create the initializers for the FuncDef structures.
 10572 **
 10573 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
 10574 **     Used to create a scalar function definition of a function zName 
 10575 **     implemented by C function xFunc that accepts nArg arguments. The
 10576 **     value passed as iArg is cast to a (void*) and made available
 10577 **     as the user-data (sqlite3_user_data()) for the function. If 
 10578 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
 10579 **
 10580 **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
 10581 **     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
 10582 **
 10583 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
 10584 **     Used to create an aggregate function definition implemented by
 10585 **     the C functions xStep and xFinal. The first four parameters
 10586 **     are interpreted in the same way as the first 4 parameters to
 10587 **     FUNCTION().
 10588 **
 10589 **   LIKEFUNC(zName, nArg, pArg, flags)
 10590 **     Used to create a scalar function definition of a function zName 
 10591 **     that accepts nArg arguments and is implemented by a call to C 
 10592 **     function likeFunc. Argument pArg is cast to a (void *) and made
 10593 **     available as the function user-data (sqlite3_user_data()). The
 10594 **     FuncDef.flags variable is set to the value passed as the flags
 10595 **     parameter.
 10596 */
 10597 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
 10598   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 10599    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 10600 #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
 10601   {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 10602    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 10603 #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
 10604   {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
 10605    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
 10606 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
 10607   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
 10608    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
 10609 #define LIKEFUNC(zName, nArg, arg, flags) \
 10610   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
 10611    (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
 10612 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
 10613   {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
 10614    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
 10616 /*
 10617 ** All current savepoints are stored in a linked list starting at
 10618 ** sqlite3.pSavepoint. The first element in the list is the most recently
 10619 ** opened savepoint. Savepoints are added to the list by the vdbe
 10620 ** OP_Savepoint instruction.
 10621 */
 10622 struct Savepoint {
 10623   char *zName;                        /* Savepoint name (nul-terminated) */
 10624   i64 nDeferredCons;                  /* Number of deferred fk violations */
 10625   i64 nDeferredImmCons;               /* Number of deferred imm fk. */
 10626   Savepoint *pNext;                   /* Parent savepoint (if any) */
 10627 };
 10629 /*
 10630 ** The following are used as the second parameter to sqlite3Savepoint(),
 10631 ** and as the P1 argument to the OP_Savepoint instruction.
 10632 */
 10633 #define SAVEPOINT_BEGIN      0
 10634 #define SAVEPOINT_RELEASE    1
 10635 #define SAVEPOINT_ROLLBACK   2
 10638 /*
 10639 ** Each SQLite module (virtual table definition) is defined by an
 10640 ** instance of the following structure, stored in the sqlite3.aModule
 10641 ** hash table.
 10642 */
 10643 struct Module {
 10644   const sqlite3_module *pModule;       /* Callback pointers */
 10645   const char *zName;                   /* Name passed to create_module() */
 10646   void *pAux;                          /* pAux passed to create_module() */
 10647   void (*xDestroy)(void *);            /* Module destructor function */
 10648 };
 10650 /*
 10651 ** information about each column of an SQL table is held in an instance
 10652 ** of this structure.
 10653 */
 10654 struct Column {
 10655   char *zName;     /* Name of this column */
 10656   Expr *pDflt;     /* Default value of this column */
 10657   char *zDflt;     /* Original text of the default value */
 10658   char *zType;     /* Data type for this column */
 10659   char *zColl;     /* Collating sequence.  If NULL, use the default */
 10660   u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
 10661   char affinity;   /* One of the SQLITE_AFF_... values */
 10662   u8 szEst;        /* Estimated size of this column.  INT==1 */
 10663   u8 colFlags;     /* Boolean properties.  See COLFLAG_ defines below */
 10664 };
 10666 /* Allowed values for Column.colFlags:
 10667 */
 10668 #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 10669 #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 10671 /*
 10672 ** A "Collating Sequence" is defined by an instance of the following
 10673 ** structure. Conceptually, a collating sequence consists of a name and
 10674 ** a comparison routine that defines the order of that sequence.
 10675 **
 10676 ** If CollSeq.xCmp is NULL, it means that the
 10677 ** collating sequence is undefined.  Indices built on an undefined
 10678 ** collating sequence may not be read or written.
 10679 */
 10680 struct CollSeq {
 10681   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
 10682   u8 enc;               /* Text encoding handled by xCmp() */
 10683   void *pUser;          /* First argument to xCmp() */
 10684   int (*xCmp)(void*,int, const void*, int, const void*);
 10685   void (*xDel)(void*);  /* Destructor for pUser */
 10686 };
 10688 /*
 10689 ** A sort order can be either ASC or DESC.
 10690 */
 10691 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
 10692 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
 10694 /*
 10695 ** Column affinity types.
 10696 **
 10697 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
 10698 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
 10699 ** the speed a little by numbering the values consecutively.  
 10700 **
 10701 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
 10702 ** when multiple affinity types are concatenated into a string and
 10703 ** used as the P4 operand, they will be more readable.
 10704 **
 10705 ** Note also that the numeric types are grouped together so that testing
 10706 ** for a numeric type is a single comparison.
 10707 */
 10708 #define SQLITE_AFF_TEXT     'a'
 10709 #define SQLITE_AFF_NONE     'b'
 10710 #define SQLITE_AFF_NUMERIC  'c'
 10711 #define SQLITE_AFF_INTEGER  'd'
 10712 #define SQLITE_AFF_REAL     'e'
 10714 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
 10716 /*
 10717 ** The SQLITE_AFF_MASK values masks off the significant bits of an
 10718 ** affinity value. 
 10719 */
 10720 #define SQLITE_AFF_MASK     0x67
 10722 /*
 10723 ** Additional bit values that can be ORed with an affinity without
 10724 ** changing the affinity.
 10725 **
 10726 ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
 10727 ** It causes an assert() to fire if either operand to a comparison
 10728 ** operator is NULL.  It is added to certain comparison operators to
 10729 ** prove that the operands are always NOT NULL.
 10730 */
 10731 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
 10732 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
 10733 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
 10734 #define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
 10736 /*
 10737 ** An object of this type is created for each virtual table present in
 10738 ** the database schema. 
 10739 **
 10740 ** If the database schema is shared, then there is one instance of this
 10741 ** structure for each database connection (sqlite3*) that uses the shared
 10742 ** schema. This is because each database connection requires its own unique
 10743 ** instance of the sqlite3_vtab* handle used to access the virtual table 
 10744 ** implementation. sqlite3_vtab* handles can not be shared between 
 10745 ** database connections, even when the rest of the in-memory database 
 10746 ** schema is shared, as the implementation often stores the database
 10747 ** connection handle passed to it via the xConnect() or xCreate() method
 10748 ** during initialization internally. This database connection handle may
 10749 ** then be used by the virtual table implementation to access real tables 
 10750 ** within the database. So that they appear as part of the callers 
 10751 ** transaction, these accesses need to be made via the same database 
 10752 ** connection as that used to execute SQL operations on the virtual table.
 10753 **
 10754 ** All VTable objects that correspond to a single table in a shared
 10755 ** database schema are initially stored in a linked-list pointed to by
 10756 ** the Table.pVTable member variable of the corresponding Table object.
 10757 ** When an sqlite3_prepare() operation is required to access the virtual
 10758 ** table, it searches the list for the VTable that corresponds to the
 10759 ** database connection doing the preparing so as to use the correct
 10760 ** sqlite3_vtab* handle in the compiled query.
 10761 **
 10762 ** When an in-memory Table object is deleted (for example when the
 10763 ** schema is being reloaded for some reason), the VTable objects are not 
 10764 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
 10765 ** immediately. Instead, they are moved from the Table.pVTable list to
 10766 ** another linked list headed by the sqlite3.pDisconnect member of the
 10767 ** corresponding sqlite3 structure. They are then deleted/xDisconnected 
 10768 ** next time a statement is prepared using said sqlite3*. This is done
 10769 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
 10770 ** Refer to comments above function sqlite3VtabUnlockList() for an
 10771 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
 10772 ** list without holding the corresponding sqlite3.mutex mutex.
 10773 **
 10774 ** The memory for objects of this type is always allocated by 
 10775 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
 10776 ** the first argument.
 10777 */
 10778 struct VTable {
 10779   sqlite3 *db;              /* Database connection associated with this table */
 10780   Module *pMod;             /* Pointer to module implementation */
 10781   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
 10782   int nRef;                 /* Number of pointers to this structure */
 10783   u8 bConstraint;           /* True if constraints are supported */
 10784   int iSavepoint;           /* Depth of the SAVEPOINT stack */
 10785   VTable *pNext;            /* Next in linked list (see above) */
 10786 };
 10788 /*
 10789 ** Each SQL table is represented in memory by an instance of the
 10790 ** following structure.
 10791 **
 10792 ** Table.zName is the name of the table.  The case of the original
 10793 ** CREATE TABLE statement is stored, but case is not significant for
 10794 ** comparisons.
 10795 **
 10796 ** Table.nCol is the number of columns in this table.  Table.aCol is a
 10797 ** pointer to an array of Column structures, one for each column.
 10798 **
 10799 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
 10800 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
 10801 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
 10802 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
 10803 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
 10804 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
 10805 ** the table has any PRIMARY KEY, INTEGER or otherwise.
 10806 **
 10807 ** Table.tnum is the page number for the root BTree page of the table in the
 10808 ** database file.  If Table.iDb is the index of the database table backend
 10809 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
 10810 ** holds temporary tables and indices.  If TF_Ephemeral is set
 10811 ** then the table is stored in a file that is automatically deleted
 10812 ** when the VDBE cursor to the table is closed.  In this case Table.tnum 
 10813 ** refers VDBE cursor number that holds the table open, not to the root
 10814 ** page number.  Transient tables are used to hold the results of a
 10815 ** sub-query that appears instead of a real table name in the FROM clause 
 10816 ** of a SELECT statement.
 10817 */
 10818 struct Table {
 10819   char *zName;         /* Name of the table or view */
 10820   Column *aCol;        /* Information about each column */
 10821   Index *pIndex;       /* List of SQL indexes on this table. */
 10822   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
 10823   FKey *pFKey;         /* Linked list of all foreign keys in this table */
 10824   char *zColAff;       /* String defining the affinity of each column */
 10825 #ifndef SQLITE_OMIT_CHECK
 10826   ExprList *pCheck;    /* All CHECK constraints */
 10827 #endif
 10828   tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
 10829   int tnum;            /* Root BTree node for this table (see note above) */
 10830   i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
 10831   i16 nCol;            /* Number of columns in this table */
 10832   u16 nRef;            /* Number of pointers to this Table */
 10833   LogEst szTabRow;     /* Estimated size of each table row in bytes */
 10834   u8 tabFlags;         /* Mask of TF_* values */
 10835   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
 10836 #ifndef SQLITE_OMIT_ALTERTABLE
 10837   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
 10838 #endif
 10839 #ifndef SQLITE_OMIT_VIRTUALTABLE
 10840   int nModuleArg;      /* Number of arguments to the module */
 10841   char **azModuleArg;  /* Text of all module args. [0] is module name */
 10842   VTable *pVTable;     /* List of VTable objects. */
 10843 #endif
 10844   Trigger *pTrigger;   /* List of triggers stored in pSchema */
 10845   Schema *pSchema;     /* Schema that contains this table */
 10846   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
 10847 };
 10849 /*
 10850 ** Allowed values for Table.tabFlags.
 10851 */
 10852 #define TF_Readonly        0x01    /* Read-only system table */
 10853 #define TF_Ephemeral       0x02    /* An ephemeral table */
 10854 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
 10855 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
 10856 #define TF_Virtual         0x10    /* Is a virtual table */
 10857 #define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
 10860 /*
 10861 ** Test to see whether or not a table is a virtual table.  This is
 10862 ** done as a macro so that it will be optimized out when virtual
 10863 ** table support is omitted from the build.
 10864 */
 10865 #ifndef SQLITE_OMIT_VIRTUALTABLE
 10866 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
 10867 #  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
 10868 #else
 10869 #  define IsVirtual(X)      0
 10870 #  define IsHiddenColumn(X) 0
 10871 #endif
 10873 /* Does the table have a rowid */
 10874 #define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
 10876 /*
 10877 ** Each foreign key constraint is an instance of the following structure.
 10878 **
 10879 ** A foreign key is associated with two tables.  The "from" table is
 10880 ** the table that contains the REFERENCES clause that creates the foreign
 10881 ** key.  The "to" table is the table that is named in the REFERENCES clause.
 10882 ** Consider this example:
 10883 **
 10884 **     CREATE TABLE ex1(
 10885 **       a INTEGER PRIMARY KEY,
 10886 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
 10887 **     );
 10888 **
 10889 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
 10890 ** Equivalent names:
 10891 **
 10892 **     from-table == child-table
 10893 **       to-table == parent-table
 10894 **
 10895 ** Each REFERENCES clause generates an instance of the following structure
 10896 ** which is attached to the from-table.  The to-table need not exist when
 10897 ** the from-table is created.  The existence of the to-table is not checked.
 10898 **
 10899 ** The list of all parents for child Table X is held at X.pFKey.
 10900 **
 10901 ** A list of all children for a table named Z (which might not even exist)
 10902 ** is held in Schema.fkeyHash with a hash key of Z.
 10903 */
 10904 struct FKey {
 10905   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
 10906   FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
 10907   char *zTo;        /* Name of table that the key points to (aka: Parent) */
 10908   FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
 10909   FKey *pPrevTo;    /* Previous with the same zTo */
 10910   int nCol;         /* Number of columns in this key */
 10911   /* EV: R-30323-21917 */
 10912   u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
 10913   u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
 10914   Trigger *apTrigger[2];/* Triggers for aAction[] actions */
 10915   struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
 10916     int iFrom;            /* Index of column in pFrom */
 10917     char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
 10918   } aCol[1];            /* One entry for each of nCol columns */
 10919 };
 10921 /*
 10922 ** SQLite supports many different ways to resolve a constraint
 10923 ** error.  ROLLBACK processing means that a constraint violation
 10924 ** causes the operation in process to fail and for the current transaction
 10925 ** to be rolled back.  ABORT processing means the operation in process
 10926 ** fails and any prior changes from that one operation are backed out,
 10927 ** but the transaction is not rolled back.  FAIL processing means that
 10928 ** the operation in progress stops and returns an error code.  But prior
 10929 ** changes due to the same operation are not backed out and no rollback
 10930 ** occurs.  IGNORE means that the particular row that caused the constraint
 10931 ** error is not inserted or updated.  Processing continues and no error
 10932 ** is returned.  REPLACE means that preexisting database rows that caused
 10933 ** a UNIQUE constraint violation are removed so that the new insert or
 10934 ** update can proceed.  Processing continues and no error is reported.
 10935 **
 10936 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
 10937 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
 10938 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
 10939 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
 10940 ** referenced table row is propagated into the row that holds the
 10941 ** foreign key.
 10942 ** 
 10943 ** The following symbolic values are used to record which type
 10944 ** of action to take.
 10945 */
 10946 #define OE_None     0   /* There is no constraint to check */
 10947 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
 10948 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
 10949 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
 10950 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
 10951 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
 10953 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
 10954 #define OE_SetNull  7   /* Set the foreign key value to NULL */
 10955 #define OE_SetDflt  8   /* Set the foreign key value to its default */
 10956 #define OE_Cascade  9   /* Cascade the changes */
 10958 #define OE_Default  10  /* Do whatever the default action is */
 10961 /*
 10962 ** An instance of the following structure is passed as the first
 10963 ** argument to sqlite3VdbeKeyCompare and is used to control the 
 10964 ** comparison of the two index keys.
 10965 **
 10966 ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
 10967 ** are nField slots for the columns of an index then one extra slot
 10968 ** for the rowid at the end.
 10969 */
 10970 struct KeyInfo {
 10971   u32 nRef;           /* Number of references to this KeyInfo object */
 10972   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
 10973   u16 nField;         /* Number of key columns in the index */
 10974   u16 nXField;        /* Number of columns beyond the key columns */
 10975   sqlite3 *db;        /* The database connection */
 10976   u8 *aSortOrder;     /* Sort order for each column. */
 10977   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
 10978 };
 10980 /*
 10981 ** An instance of the following structure holds information about a
 10982 ** single index record that has already been parsed out into individual
 10983 ** values.
 10984 **
 10985 ** A record is an object that contains one or more fields of data.
 10986 ** Records are used to store the content of a table row and to store
 10987 ** the key of an index.  A blob encoding of a record is created by
 10988 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
 10989 ** OP_Column opcode.
 10990 **
 10991 ** This structure holds a record that has already been disassembled
 10992 ** into its constituent fields.
 10993 **
 10994 ** The r1 and r2 member variables are only used by the optimized comparison
 10995 ** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
 10996 */
 10997 struct UnpackedRecord {
 10998   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
 10999   u16 nField;         /* Number of entries in apMem[] */
 11000   i8 default_rc;      /* Comparison result if keys are equal */
 11001   Mem *aMem;          /* Values */
 11002   int r1;             /* Value to return if (lhs > rhs) */
 11003   int r2;             /* Value to return if (rhs < lhs) */
 11004 };
 11007 /*
 11008 ** Each SQL index is represented in memory by an
 11009 ** instance of the following structure.
 11010 **
 11011 ** The columns of the table that are to be indexed are described
 11012 ** by the aiColumn[] field of this structure.  For example, suppose
 11013 ** we have the following table and index:
 11014 **
 11015 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
 11016 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
 11017 **
 11018 ** In the Table structure describing Ex1, nCol==3 because there are
 11019 ** three columns in the table.  In the Index structure describing
 11020 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
 11021 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
 11022 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
 11023 ** The second column to be indexed (c1) has an index of 0 in
 11024 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
 11025 **
 11026 ** The Index.onError field determines whether or not the indexed columns
 11027 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
 11028 ** it means this is not a unique index.  Otherwise it is a unique index
 11029 ** and the value of Index.onError indicate the which conflict resolution 
 11030 ** algorithm to employ whenever an attempt is made to insert a non-unique
 11031 ** element.
 11032 */
 11033 struct Index {
 11034   char *zName;             /* Name of this index */
 11035   i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
 11036   tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
 11037   Table *pTable;           /* The SQL table being indexed */
 11038   char *zColAff;           /* String defining the affinity of each column */
 11039   Index *pNext;            /* The next index associated with the same table */
 11040   Schema *pSchema;         /* Schema containing this index */
 11041   u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
 11042   char **azColl;           /* Array of collation sequence names for index */
 11043   Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
 11044   KeyInfo *pKeyInfo;       /* A KeyInfo object suitable for this index */
 11045   int tnum;                /* DB Page containing root of this index */
 11046   LogEst szIdxRow;         /* Estimated average row size in bytes */
 11047   u16 nKeyCol;             /* Number of columns forming the key */
 11048   u16 nColumn;             /* Number of columns stored in the index */
 11049   u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 11050   unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
 11051   unsigned bUnordered:1;   /* Use this index for == or IN queries only */
 11052   unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
 11053   unsigned isResized:1;    /* True if resizeIndexObject() has been called */
 11054   unsigned isCovering:1;   /* True if this is a covering index */
 11055 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 11056   int nSample;             /* Number of elements in aSample[] */
 11057   int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
 11058   tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
 11059   IndexSample *aSample;    /* Samples of the left-most key */
 11060 #endif
 11061 };
 11063 /*
 11064 ** Each sample stored in the sqlite_stat3 table is represented in memory 
 11065 ** using a structure of this type.  See documentation at the top of the
 11066 ** analyze.c source file for additional information.
 11067 */
 11068 struct IndexSample {
 11069   void *p;          /* Pointer to sampled record */
 11070   int n;            /* Size of record in bytes */
 11071   tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
 11072   tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
 11073   tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
 11074 };
 11076 /*
 11077 ** Each token coming out of the lexer is an instance of
 11078 ** this structure.  Tokens are also used as part of an expression.
 11079 **
 11080 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
 11081 ** may contain random values.  Do not make any assumptions about Token.dyn
 11082 ** and Token.n when Token.z==0.
 11083 */
 11084 struct Token {
 11085   const char *z;     /* Text of the token.  Not NULL-terminated! */
 11086   unsigned int n;    /* Number of characters in this token */
 11087 };
 11089 /*
 11090 ** An instance of this structure contains information needed to generate
 11091 ** code for a SELECT that contains aggregate functions.
 11092 **
 11093 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
 11094 ** pointer to this structure.  The Expr.iColumn field is the index in
 11095 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
 11096 ** code for that node.
 11097 **
 11098 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
 11099 ** original Select structure that describes the SELECT statement.  These
 11100 ** fields do not need to be freed when deallocating the AggInfo structure.
 11101 */
 11102 struct AggInfo {
 11103   u8 directMode;          /* Direct rendering mode means take data directly
 11104                           ** from source tables rather than from accumulators */
 11105   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
 11106                           ** than the source table */
 11107   int sortingIdx;         /* Cursor number of the sorting index */
 11108   int sortingIdxPTab;     /* Cursor number of pseudo-table */
 11109   int nSortingColumn;     /* Number of columns in the sorting index */
 11110   int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
 11111   ExprList *pGroupBy;     /* The group by clause */
 11112   struct AggInfo_col {    /* For each column used in source tables */
 11113     Table *pTab;             /* Source table */
 11114     int iTable;              /* Cursor number of the source table */
 11115     int iColumn;             /* Column number within the source table */
 11116     int iSorterColumn;       /* Column number in the sorting index */
 11117     int iMem;                /* Memory location that acts as accumulator */
 11118     Expr *pExpr;             /* The original expression */
 11119   } *aCol;
 11120   int nColumn;            /* Number of used entries in aCol[] */
 11121   int nAccumulator;       /* Number of columns that show through to the output.
 11122                           ** Additional columns are used only as parameters to
 11123                           ** aggregate functions */
 11124   struct AggInfo_func {   /* For each aggregate function */
 11125     Expr *pExpr;             /* Expression encoding the function */
 11126     FuncDef *pFunc;          /* The aggregate function implementation */
 11127     int iMem;                /* Memory location that acts as accumulator */
 11128     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
 11129   } *aFunc;
 11130   int nFunc;              /* Number of entries in aFunc[] */
 11131 };
 11133 /*
 11134 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
 11135 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
 11136 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
 11137 ** it uses less memory in the Expr object, which is a big memory user
 11138 ** in systems with lots of prepared statements.  And few applications
 11139 ** need more than about 10 or 20 variables.  But some extreme users want
 11140 ** to have prepared statements with over 32767 variables, and for them
 11141 ** the option is available (at compile-time).
 11142 */
 11143 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
 11144 typedef i16 ynVar;
 11145 #else
 11146 typedef int ynVar;
 11147 #endif
 11149 /*
 11150 ** Each node of an expression in the parse tree is an instance
 11151 ** of this structure.
 11152 **
 11153 ** Expr.op is the opcode. The integer parser token codes are reused
 11154 ** as opcodes here. For example, the parser defines TK_GE to be an integer
 11155 ** code representing the ">=" operator. This same integer code is reused
 11156 ** to represent the greater-than-or-equal-to operator in the expression
 11157 ** tree.
 11158 **
 11159 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
 11160 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
 11161 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
 11162 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
 11163 ** then Expr.token contains the name of the function.
 11164 **
 11165 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
 11166 ** binary operator. Either or both may be NULL.
 11167 **
 11168 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
 11169 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
 11170 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
 11171 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
 11172 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
 11173 ** valid.
 11174 **
 11175 ** An expression of the form ID or ID.ID refers to a column in a table.
 11176 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
 11177 ** the integer cursor number of a VDBE cursor pointing to that table and
 11178 ** Expr.iColumn is the column number for the specific column.  If the
 11179 ** expression is used as a result in an aggregate SELECT, then the
 11180 ** value is also stored in the Expr.iAgg column in the aggregate so that
 11181 ** it can be accessed after all aggregates are computed.
 11182 **
 11183 ** If the expression is an unbound variable marker (a question mark 
 11184 ** character '?' in the original SQL) then the Expr.iTable holds the index 
 11185 ** number for that variable.
 11186 **
 11187 ** If the expression is a subquery then Expr.iColumn holds an integer
 11188 ** register number containing the result of the subquery.  If the
 11189 ** subquery gives a constant result, then iTable is -1.  If the subquery
 11190 ** gives a different answer at different times during statement processing
 11191 ** then iTable is the address of a subroutine that computes the subquery.
 11192 **
 11193 ** If the Expr is of type OP_Column, and the table it is selecting from
 11194 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
 11195 ** corresponding table definition.
 11196 **
 11197 ** ALLOCATION NOTES:
 11198 **
 11199 ** Expr objects can use a lot of memory space in database schema.  To
 11200 ** help reduce memory requirements, sometimes an Expr object will be
 11201 ** truncated.  And to reduce the number of memory allocations, sometimes
 11202 ** two or more Expr objects will be stored in a single memory allocation,
 11203 ** together with Expr.zToken strings.
 11204 **
 11205 ** If the EP_Reduced and EP_TokenOnly flags are set when
 11206 ** an Expr object is truncated.  When EP_Reduced is set, then all
 11207 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
 11208 ** are contained within the same memory allocation.  Note, however, that
 11209 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
 11210 ** allocated, regardless of whether or not EP_Reduced is set.
 11211 */
 11212 struct Expr {
 11213   u8 op;                 /* Operation performed by this node */
 11214   char affinity;         /* The affinity of the column or 0 if not a column */
 11215   u32 flags;             /* Various flags.  EP_* See below */
 11216   union {
 11217     char *zToken;          /* Token value. Zero terminated and dequoted */
 11218     int iValue;            /* Non-negative integer value if EP_IntValue */
 11219   } u;
 11221   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
 11222   ** space is allocated for the fields below this point. An attempt to
 11223   ** access them will result in a segfault or malfunction. 
 11224   *********************************************************************/
 11226   Expr *pLeft;           /* Left subnode */
 11227   Expr *pRight;          /* Right subnode */
 11228   union {
 11229     ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
 11230     Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
 11231   } x;
 11233   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
 11234   ** space is allocated for the fields below this point. An attempt to
 11235   ** access them will result in a segfault or malfunction.
 11236   *********************************************************************/
 11238 #if SQLITE_MAX_EXPR_DEPTH>0
 11239   int nHeight;           /* Height of the tree headed by this node */
 11240 #endif
 11241   int iTable;            /* TK_COLUMN: cursor number of table holding column
 11242                          ** TK_REGISTER: register number
 11243                          ** TK_TRIGGER: 1 -> new, 0 -> old
 11244                          ** EP_Unlikely:  1000 times likelihood */
 11245   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
 11246                          ** TK_VARIABLE: variable number (always >= 1). */
 11247   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
 11248   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
 11249   u8 op2;                /* TK_REGISTER: original value of Expr.op
 11250                          ** TK_COLUMN: the value of p5 for OP_Column
 11251                          ** TK_AGG_FUNCTION: nesting depth */
 11252   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
 11253   Table *pTab;           /* Table for TK_COLUMN expressions. */
 11254 };
 11256 /*
 11257 ** The following are the meanings of bits in the Expr.flags field.
 11258 */
 11259 #define EP_FromJoin  0x000001 /* Originated in ON or USING clause of a join */
 11260 #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
 11261 #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
 11262 #define EP_Error     0x000008 /* Expression contains one or more errors */
 11263 #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
 11264 #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
 11265 #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
 11266 #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
 11267 #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
 11268       /* unused      0x000200 */
 11269 #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
 11270 #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
 11271 #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
 11272 #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
 11273 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
 11274 #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
 11275 #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
 11276 #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
 11277 #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
 11278 #define EP_Constant  0x080000 /* Node is a constant */
 11280 /*
 11281 ** These macros can be used to test, set, or clear bits in the 
 11282 ** Expr.flags field.
 11283 */
 11284 #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
 11285 #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
 11286 #define ExprSetProperty(E,P)     (E)->flags|=(P)
 11287 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
 11289 /* The ExprSetVVAProperty() macro is used for Verification, Validation,
 11290 ** and Accreditation only.  It works like ExprSetProperty() during VVA
 11291 ** processes but is a no-op for delivery.
 11292 */
 11293 #ifdef SQLITE_DEBUG
 11294 # define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
 11295 #else
 11296 # define ExprSetVVAProperty(E,P)
 11297 #endif
 11299 /*
 11300 ** Macros to determine the number of bytes required by a normal Expr 
 11301 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
 11302 ** and an Expr struct with the EP_TokenOnly flag set.
 11303 */
 11304 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
 11305 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
 11306 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
 11308 /*
 11309 ** Flags passed to the sqlite3ExprDup() function. See the header comment 
 11310 ** above sqlite3ExprDup() for details.
 11311 */
 11312 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
 11314 /*
 11315 ** A list of expressions.  Each expression may optionally have a
 11316 ** name.  An expr/name combination can be used in several ways, such
 11317 ** as the list of "expr AS ID" fields following a "SELECT" or in the
 11318 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
 11319 ** also be used as the argument to a function, in which case the a.zName
 11320 ** field is not used.
 11321 **
 11322 ** By default the Expr.zSpan field holds a human-readable description of
 11323 ** the expression that is used in the generation of error messages and
 11324 ** column labels.  In this case, Expr.zSpan is typically the text of a
 11325 ** column expression as it exists in a SELECT statement.  However, if
 11326 ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
 11327 ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
 11328 ** form is used for name resolution with nested FROM clauses.
 11329 */
 11330 struct ExprList {
 11331   int nExpr;             /* Number of expressions on the list */
 11332   int iECursor;          /* VDBE Cursor associated with this ExprList */
 11333   struct ExprList_item { /* For each expression in the list */
 11334     Expr *pExpr;            /* The list of expressions */
 11335     char *zName;            /* Token associated with this expression */
 11336     char *zSpan;            /* Original text of the expression */
 11337     u8 sortOrder;           /* 1 for DESC or 0 for ASC */
 11338     unsigned done :1;       /* A flag to indicate when processing is finished */
 11339     unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
 11340     unsigned reusable :1;   /* Constant expression is reusable */
 11341     union {
 11342       struct {
 11343         u16 iOrderByCol;      /* For ORDER BY, column number in result set */
 11344         u16 iAlias;           /* Index into Parse.aAlias[] for zName */
 11345       } x;
 11346       int iConstExprReg;      /* Register in which Expr value is cached */
 11347     } u;
 11348   } *a;                  /* Alloc a power of two greater or equal to nExpr */
 11349 };
 11351 /*
 11352 ** An instance of this structure is used by the parser to record both
 11353 ** the parse tree for an expression and the span of input text for an
 11354 ** expression.
 11355 */
 11356 struct ExprSpan {
 11357   Expr *pExpr;          /* The expression parse tree */
 11358   const char *zStart;   /* First character of input text */
 11359   const char *zEnd;     /* One character past the end of input text */
 11360 };
 11362 /*
 11363 ** An instance of this structure can hold a simple list of identifiers,
 11364 ** such as the list "a,b,c" in the following statements:
 11365 **
 11366 **      INSERT INTO t(a,b,c) VALUES ...;
 11367 **      CREATE INDEX idx ON t(a,b,c);
 11368 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
 11369 **
 11370 ** The IdList.a.idx field is used when the IdList represents the list of
 11371 ** column names after a table name in an INSERT statement.  In the statement
 11372 **
 11373 **     INSERT INTO t(a,b,c) ...
 11374 **
 11375 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
 11376 */
 11377 struct IdList {
 11378   struct IdList_item {
 11379     char *zName;      /* Name of the identifier */
 11380     int idx;          /* Index in some Table.aCol[] of a column named zName */
 11381   } *a;
 11382   int nId;         /* Number of identifiers on the list */
 11383 };
 11385 /*
 11386 ** The bitmask datatype defined below is used for various optimizations.
 11387 **
 11388 ** Changing this from a 64-bit to a 32-bit type limits the number of
 11389 ** tables in a join to 32 instead of 64.  But it also reduces the size
 11390 ** of the library by 738 bytes on ix86.
 11391 */
 11392 typedef u64 Bitmask;
 11394 /*
 11395 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
 11396 */
 11397 #define BMS  ((int)(sizeof(Bitmask)*8))
 11399 /*
 11400 ** A bit in a Bitmask
 11401 */
 11402 #define MASKBIT(n)   (((Bitmask)1)<<(n))
 11403 #define MASKBIT32(n) (((unsigned int)1)<<(n))
 11405 /*
 11406 ** The following structure describes the FROM clause of a SELECT statement.
 11407 ** Each table or subquery in the FROM clause is a separate element of
 11408 ** the SrcList.a[] array.
 11409 **
 11410 ** With the addition of multiple database support, the following structure
 11411 ** can also be used to describe a particular table such as the table that
 11412 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
 11413 ** such a table must be a simple name: ID.  But in SQLite, the table can
 11414 ** now be identified by a database name, a dot, then the table name: ID.ID.
 11415 **
 11416 ** The jointype starts out showing the join type between the current table
 11417 ** and the next table on the list.  The parser builds the list this way.
 11418 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
 11419 ** jointype expresses the join between the table and the previous table.
 11420 **
 11421 ** In the colUsed field, the high-order bit (bit 63) is set if the table
 11422 ** contains more than 63 columns and the 64-th or later column is used.
 11423 */
 11424 struct SrcList {
 11425   int nSrc;        /* Number of tables or subqueries in the FROM clause */
 11426   u32 nAlloc;      /* Number of entries allocated in a[] below */
 11427   struct SrcList_item {
 11428     Schema *pSchema;  /* Schema to which this item is fixed */
 11429     char *zDatabase;  /* Name of database holding this table */
 11430     char *zName;      /* Name of the table */
 11431     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
 11432     Table *pTab;      /* An SQL table corresponding to zName */
 11433     Select *pSelect;  /* A SELECT statement used in place of a table name */
 11434     int addrFillSub;  /* Address of subroutine to manifest a subquery */
 11435     int regReturn;    /* Register holding return address of addrFillSub */
 11436     int regResult;    /* Registers holding results of a co-routine */
 11437     u8 jointype;      /* Type of join between this able and the previous */
 11438     unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
 11439     unsigned isCorrelated :1;  /* True if sub-query is correlated */
 11440     unsigned viaCoroutine :1;  /* Implemented as a co-routine */
 11441     unsigned isRecursive :1;   /* True for recursive reference in WITH */
 11442 #ifndef SQLITE_OMIT_EXPLAIN
 11443     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
 11444 #endif
 11445     int iCursor;      /* The VDBE cursor number used to access this table */
 11446     Expr *pOn;        /* The ON clause of a join */
 11447     IdList *pUsing;   /* The USING clause of a join */
 11448     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
 11449     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
 11450     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
 11451   } a[1];             /* One entry for each identifier on the list */
 11452 };
 11454 /*
 11455 ** Permitted values of the SrcList.a.jointype field
 11456 */
 11457 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
 11458 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
 11459 #define JT_NATURAL   0x0004    /* True for a "natural" join */
 11460 #define JT_LEFT      0x0008    /* Left outer join */
 11461 #define JT_RIGHT     0x0010    /* Right outer join */
 11462 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
 11463 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
 11466 /*
 11467 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
 11468 ** and the WhereInfo.wctrlFlags member.
 11469 */
 11470 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
 11471 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
 11472 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
 11473 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
 11474 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
 11475 #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
 11476 #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
 11477 #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
 11478 #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
 11479 #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
 11480 #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
 11481 #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
 11483 /* Allowed return values from sqlite3WhereIsDistinct()
 11484 */
 11485 #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
 11486 #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
 11487 #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
 11488 #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
 11490 /*
 11491 ** A NameContext defines a context in which to resolve table and column
 11492 ** names.  The context consists of a list of tables (the pSrcList) field and
 11493 ** a list of named expression (pEList).  The named expression list may
 11494 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
 11495 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
 11496 ** pEList corresponds to the result set of a SELECT and is NULL for
 11497 ** other statements.
 11498 **
 11499 ** NameContexts can be nested.  When resolving names, the inner-most 
 11500 ** context is searched first.  If no match is found, the next outer
 11501 ** context is checked.  If there is still no match, the next context
 11502 ** is checked.  This process continues until either a match is found
 11503 ** or all contexts are check.  When a match is found, the nRef member of
 11504 ** the context containing the match is incremented. 
 11505 **
 11506 ** Each subquery gets a new NameContext.  The pNext field points to the
 11507 ** NameContext in the parent query.  Thus the process of scanning the
 11508 ** NameContext list corresponds to searching through successively outer
 11509 ** subqueries looking for a match.
 11510 */
 11511 struct NameContext {
 11512   Parse *pParse;       /* The parser */
 11513   SrcList *pSrcList;   /* One or more tables used to resolve names */
 11514   ExprList *pEList;    /* Optional list of result-set columns */
 11515   AggInfo *pAggInfo;   /* Information about aggregates at this level */
 11516   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
 11517   int nRef;            /* Number of names resolved by this context */
 11518   int nErr;            /* Number of errors encountered while resolving names */
 11519   u8 ncFlags;          /* Zero or more NC_* flags defined below */
 11520 };
 11522 /*
 11523 ** Allowed values for the NameContext, ncFlags field.
 11524 */
 11525 #define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
 11526 #define NC_HasAgg    0x02    /* One or more aggregate functions seen */
 11527 #define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
 11528 #define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
 11529 #define NC_PartIdx   0x10    /* True if resolving a partial index WHERE */
 11531 /*
 11532 ** An instance of the following structure contains all information
 11533 ** needed to generate code for a single SELECT statement.
 11534 **
 11535 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
 11536 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
 11537 ** limit and nOffset to the value of the offset (or 0 if there is not
 11538 ** offset).  But later on, nLimit and nOffset become the memory locations
 11539 ** in the VDBE that record the limit and offset counters.
 11540 **
 11541 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
 11542 ** These addresses must be stored so that we can go back and fill in
 11543 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
 11544 ** the number of columns in P2 can be computed at the same time
 11545 ** as the OP_OpenEphm instruction is coded because not
 11546 ** enough information about the compound query is known at that point.
 11547 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
 11548 ** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
 11549 ** sequences for the ORDER BY clause.
 11550 */
 11551 struct Select {
 11552   ExprList *pEList;      /* The fields of the result */
 11553   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 11554   u16 selFlags;          /* Various SF_* values */
 11555   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
 11556   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
 11557   u64 nSelectRow;        /* Estimated number of result rows */
 11558   SrcList *pSrc;         /* The FROM clause */
 11559   Expr *pWhere;          /* The WHERE clause */
 11560   ExprList *pGroupBy;    /* The GROUP BY clause */
 11561   Expr *pHaving;         /* The HAVING clause */
 11562   ExprList *pOrderBy;    /* The ORDER BY clause */
 11563   Select *pPrior;        /* Prior select in a compound select statement */
 11564   Select *pNext;         /* Next select to the left in a compound */
 11565   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 11566   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 11567   With *pWith;           /* WITH clause attached to this select. Or NULL. */
 11568 };
 11570 /*
 11571 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
 11572 ** "Select Flag".
 11573 */
 11574 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
 11575 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
 11576 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
 11577 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
 11578 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
 11579 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
 11580 #define SF_UseSorter       0x0040  /* Sort using a sorter */
 11581 #define SF_Values          0x0080  /* Synthesized from VALUES clause */
 11582 #define SF_Materialize     0x0100  /* NOT USED */
 11583 #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
 11584 #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
 11585 #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
 11586 #define SF_Compound        0x1000  /* Part of a compound query */
 11589 /*
 11590 ** The results of a SELECT can be distributed in several ways, as defined
 11591 ** by one of the following macros.  The "SRT" prefix means "SELECT Result
 11592 ** Type".
 11593 **
 11594 **     SRT_Union       Store results as a key in a temporary index 
 11595 **                     identified by pDest->iSDParm.
 11596 **
 11597 **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
 11598 **
 11599 **     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
 11600 **                     set is not empty.
 11601 **
 11602 **     SRT_Discard     Throw the results away.  This is used by SELECT
 11603 **                     statements within triggers whose only purpose is
 11604 **                     the side-effects of functions.
 11605 **
 11606 ** All of the above are free to ignore their ORDER BY clause. Those that
 11607 ** follow must honor the ORDER BY clause.
 11608 **
 11609 **     SRT_Output      Generate a row of output (using the OP_ResultRow
 11610 **                     opcode) for each row in the result set.
 11611 **
 11612 **     SRT_Mem         Only valid if the result is a single column.
 11613 **                     Store the first column of the first result row
 11614 **                     in register pDest->iSDParm then abandon the rest
 11615 **                     of the query.  This destination implies "LIMIT 1".
 11616 **
 11617 **     SRT_Set         The result must be a single column.  Store each
 11618 **                     row of result as the key in table pDest->iSDParm. 
 11619 **                     Apply the affinity pDest->affSdst before storing
 11620 **                     results.  Used to implement "IN (SELECT ...)".
 11621 **
 11622 **     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
 11623 **                     the result there. The cursor is left open after
 11624 **                     returning.  This is like SRT_Table except that
 11625 **                     this destination uses OP_OpenEphemeral to create
 11626 **                     the table first.
 11627 **
 11628 **     SRT_Coroutine   Generate a co-routine that returns a new row of
 11629 **                     results each time it is invoked.  The entry point
 11630 **                     of the co-routine is stored in register pDest->iSDParm
 11631 **                     and the result row is stored in pDest->nDest registers
 11632 **                     starting with pDest->iSdst.
 11633 **
 11634 **     SRT_Table       Store results in temporary table pDest->iSDParm.
 11635 **                     This is like SRT_EphemTab except that the table
 11636 **                     is assumed to already be open.
 11637 **
 11638 **     SRT_DistTable   Store results in a temporary table pDest->iSDParm.
 11639 **                     But also use temporary table pDest->iSDParm+1 as
 11640 **                     a record of all prior results and ignore any duplicate
 11641 **                     rows.  Name means:  "Distinct Table".
 11642 **
 11643 **     SRT_Queue       Store results in priority queue pDest->iSDParm (really
 11644 **                     an index).  Append a sequence number so that all entries
 11645 **                     are distinct.
 11646 **
 11647 **     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
 11648 **                     the same record has never been stored before.  The
 11649 **                     index at pDest->iSDParm+1 hold all prior stores.
 11650 */
 11651 #define SRT_Union        1  /* Store result as keys in an index */
 11652 #define SRT_Except       2  /* Remove result from a UNION index */
 11653 #define SRT_Exists       3  /* Store 1 if the result is not empty */
 11654 #define SRT_Discard      4  /* Do not save the results anywhere */
 11656 /* The ORDER BY clause is ignored for all of the above */
 11657 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
 11659 #define SRT_Output       5  /* Output each row of result */
 11660 #define SRT_Mem          6  /* Store result in a memory cell */
 11661 #define SRT_Set          7  /* Store results as keys in an index */
 11662 #define SRT_EphemTab     8  /* Create transient tab and store like SRT_Table */
 11663 #define SRT_Coroutine    9  /* Generate a single row of result */
 11664 #define SRT_Table       10  /* Store result as data with an automatic rowid */
 11665 #define SRT_DistTable   11  /* Like SRT_Table, but unique results only */
 11666 #define SRT_Queue       12  /* Store result in an queue */
 11667 #define SRT_DistQueue   13  /* Like SRT_Queue, but unique results only */
 11669 /*
 11670 ** An instance of this object describes where to put of the results of
 11671 ** a SELECT statement.
 11672 */
 11673 struct SelectDest {
 11674   u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
 11675   char affSdst;        /* Affinity used when eDest==SRT_Set */
 11676   int iSDParm;         /* A parameter used by the eDest disposal method */
 11677   int iSdst;           /* Base register where results are written */
 11678   int nSdst;           /* Number of registers allocated */
 11679   ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
 11680 };
 11682 /*
 11683 ** During code generation of statements that do inserts into AUTOINCREMENT 
 11684 ** tables, the following information is attached to the Table.u.autoInc.p
 11685 ** pointer of each autoincrement table to record some side information that
 11686 ** the code generator needs.  We have to keep per-table autoincrement
 11687 ** information in case inserts are down within triggers.  Triggers do not
 11688 ** normally coordinate their activities, but we do need to coordinate the
 11689 ** loading and saving of autoincrement information.
 11690 */
 11691 struct AutoincInfo {
 11692   AutoincInfo *pNext;   /* Next info block in a list of them all */
 11693   Table *pTab;          /* Table this info block refers to */
 11694   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
 11695   int regCtr;           /* Memory register holding the rowid counter */
 11696 };
 11698 /*
 11699 ** Size of the column cache
 11700 */
 11701 #ifndef SQLITE_N_COLCACHE
 11702 # define SQLITE_N_COLCACHE 10
 11703 #endif
 11705 /*
 11706 ** At least one instance of the following structure is created for each 
 11707 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
 11708 ** statement. All such objects are stored in the linked list headed at
 11709 ** Parse.pTriggerPrg and deleted once statement compilation has been
 11710 ** completed.
 11711 **
 11712 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
 11713 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
 11714 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
 11715 ** The Parse.pTriggerPrg list never contains two entries with the same
 11716 ** values for both pTrigger and orconf.
 11717 **
 11718 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
 11719 ** accessed (or set to 0 for triggers fired as a result of INSERT 
 11720 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
 11721 ** a mask of new.* columns used by the program.
 11722 */
 11723 struct TriggerPrg {
 11724   Trigger *pTrigger;      /* Trigger this program was coded from */
 11725   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
 11726   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
 11727   int orconf;             /* Default ON CONFLICT policy */
 11728   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
 11729 };
 11731 /*
 11732 ** The yDbMask datatype for the bitmask of all attached databases.
 11733 */
 11734 #if SQLITE_MAX_ATTACHED>30
 11735   typedef sqlite3_uint64 yDbMask;
 11736 #else
 11737   typedef unsigned int yDbMask;
 11738 #endif
 11740 /*
 11741 ** An SQL parser context.  A copy of this structure is passed through
 11742 ** the parser and down into all the parser action routine in order to
 11743 ** carry around information that is global to the entire parse.
 11744 **
 11745 ** The structure is divided into two parts.  When the parser and code
 11746 ** generate call themselves recursively, the first part of the structure
 11747 ** is constant but the second part is reset at the beginning and end of
 11748 ** each recursion.
 11749 **
 11750 ** The nTableLock and aTableLock variables are only used if the shared-cache 
 11751 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
 11752 ** used to store the set of table-locks required by the statement being
 11753 ** compiled. Function sqlite3TableLock() is used to add entries to the
 11754 ** list.
 11755 */
 11756 struct Parse {
 11757   sqlite3 *db;         /* The main database structure */
 11758   char *zErrMsg;       /* An error message */
 11759   Vdbe *pVdbe;         /* An engine for executing database bytecode */
 11760   int rc;              /* Return code from execution */
 11761   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
 11762   u8 checkSchema;      /* Causes schema cookie check after an error */
 11763   u8 nested;           /* Number of nested calls to the parser/code generator */
 11764   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
 11765   u8 nColCache;        /* Number of entries in aColCache[] */
 11766   u8 iColCache;        /* Next entry in aColCache[] to replace */
 11767   u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
 11768   u8 mayAbort;         /* True if statement may throw an ABORT exception */
 11769   u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
 11770   u8 okConstFactor;    /* OK to factor out constants */
 11771   int aTempReg[8];     /* Holding area for temporary registers */
 11772   int nRangeReg;       /* Size of the temporary register block */
 11773   int iRangeReg;       /* First register in temporary register block */
 11774   int nErr;            /* Number of errors seen */
 11775   int nTab;            /* Number of previously allocated VDBE cursors */
 11776   int nMem;            /* Number of memory cells used so far */
 11777   int nSet;            /* Number of sets used so far */
 11778   int nOnce;           /* Number of OP_Once instructions so far */
 11779   int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
 11780   int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
 11781   int ckBase;          /* Base register of data during check constraints */
 11782   int iPartIdxTab;     /* Table corresponding to a partial index */
 11783   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
 11784   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
 11785   int nLabel;          /* Number of labels used */
 11786   int *aLabel;         /* Space to hold the labels */
 11787   struct yColCache {
 11788     int iTable;           /* Table cursor number */
 11789     i16 iColumn;          /* Table column number */
 11790     u8 tempReg;           /* iReg is a temp register that needs to be freed */
 11791     int iLevel;           /* Nesting level */
 11792     int iReg;             /* Reg with value of this column. 0 means none. */
 11793     int lru;              /* Least recently used entry has the smallest value */
 11794   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 11795   ExprList *pConstExpr;/* Constant expressions */
 11796   Token constraintName;/* Name of the constraint currently being parsed */
 11797   yDbMask writeMask;   /* Start a write transaction on these databases */
 11798   yDbMask cookieMask;  /* Bitmask of schema verified databases */
 11799   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 11800   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 11801   int regRoot;         /* Register holding root page number for new objects */
 11802   int nMaxArg;         /* Max args passed to user function by sub-program */
 11803 #ifndef SQLITE_OMIT_SHARED_CACHE
 11804   int nTableLock;        /* Number of locks in aTableLock */
 11805   TableLock *aTableLock; /* Required table locks for shared-cache mode */
 11806 #endif
 11807   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 11809   /* Information used while coding trigger programs. */
 11810   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
 11811   Table *pTriggerTab;  /* Table triggers are being coded for */
 11812   int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
 11813   int addrSkipPK;      /* Address of instruction to skip PRIMARY KEY index */
 11814   u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
 11815   u32 oldmask;         /* Mask of old.* columns referenced */
 11816   u32 newmask;         /* Mask of new.* columns referenced */
 11817   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
 11818   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
 11819   u8 disableTriggers;  /* True to disable triggers */
 11821   /************************************************************************
 11822   ** Above is constant between recursions.  Below is reset before and after
 11823   ** each recursion.  The boundary between these two regions is determined
 11824   ** using offsetof(Parse,nVar) so the nVar field must be the first field
 11825   ** in the recursive region.
 11826   ************************************************************************/
 11828   int nVar;                 /* Number of '?' variables seen in the SQL so far */
 11829   int nzVar;                /* Number of available slots in azVar[] */
 11830   u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
 11831   u8 bFreeWith;             /* True if pWith should be freed with parser */
 11832   u8 explain;               /* True if the EXPLAIN flag is found on the query */
 11833 #ifndef SQLITE_OMIT_VIRTUALTABLE
 11834   u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
 11835   int nVtabLock;            /* Number of virtual tables to lock */
 11836 #endif
 11837   int nAlias;               /* Number of aliased result set columns */
 11838   int nHeight;              /* Expression tree height of current sub-select */
 11839 #ifndef SQLITE_OMIT_EXPLAIN
 11840   int iSelectId;            /* ID of current select for EXPLAIN output */
 11841   int iNextSelectId;        /* Next available select ID for EXPLAIN output */
 11842 #endif
 11843   char **azVar;             /* Pointers to names of parameters */
 11844   Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
 11845   const char *zTail;        /* All SQL text past the last semicolon parsed */
 11846   Table *pNewTable;         /* A table being constructed by CREATE TABLE */
 11847   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
 11848   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
 11849   Token sNameToken;         /* Token with unqualified schema object name */
 11850   Token sLastToken;         /* The last token parsed */
 11851 #ifndef SQLITE_OMIT_VIRTUALTABLE
 11852   Token sArg;               /* Complete text of a module argument */
 11853   Table **apVtabLock;       /* Pointer to virtual tables needing locking */
 11854 #endif
 11855   Table *pZombieTab;        /* List of Table objects to delete after code gen */
 11856   TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
 11857   With *pWith;              /* Current WITH clause, or NULL */
 11858 };
 11860 /*
 11861 ** Return true if currently inside an sqlite3_declare_vtab() call.
 11862 */
 11863 #ifdef SQLITE_OMIT_VIRTUALTABLE
 11864   #define IN_DECLARE_VTAB 0
 11865 #else
 11866   #define IN_DECLARE_VTAB (pParse->declareVtab)
 11867 #endif
 11869 /*
 11870 ** An instance of the following structure can be declared on a stack and used
 11871 ** to save the Parse.zAuthContext value so that it can be restored later.
 11872 */
 11873 struct AuthContext {
 11874   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
 11875   Parse *pParse;              /* The Parse structure */
 11876 };
 11878 /*
 11879 ** Bitfield flags for P5 value in various opcodes.
 11880 */
 11881 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
 11882 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
 11883 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
 11884 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
 11885 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
 11886 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
 11887 #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
 11888 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
 11889 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
 11890 #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
 11891 #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
 11893 /*
 11894  * Each trigger present in the database schema is stored as an instance of
 11895  * struct Trigger. 
 11897  * Pointers to instances of struct Trigger are stored in two ways.
 11898  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
 11899  *    database). This allows Trigger structures to be retrieved by name.
 11900  * 2. All triggers associated with a single table form a linked list, using the
 11901  *    pNext member of struct Trigger. A pointer to the first element of the
 11902  *    linked list is stored as the "pTrigger" member of the associated
 11903  *    struct Table.
 11905  * The "step_list" member points to the first element of a linked list
 11906  * containing the SQL statements specified as the trigger program.
 11907  */
 11908 struct Trigger {
 11909   char *zName;            /* The name of the trigger                        */
 11910   char *table;            /* The table or view to which the trigger applies */
 11911   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
 11912   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 11913   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
 11914   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
 11915                              the <column-list> is stored here */
 11916   Schema *pSchema;        /* Schema containing the trigger */
 11917   Schema *pTabSchema;     /* Schema containing the table */
 11918   TriggerStep *step_list; /* Link list of trigger program steps             */
 11919   Trigger *pNext;         /* Next trigger associated with the table */
 11920 };
 11922 /*
 11923 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
 11924 ** determine which. 
 11925 **
 11926 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
 11927 ** In that cases, the constants below can be ORed together.
 11928 */
 11929 #define TRIGGER_BEFORE  1
 11930 #define TRIGGER_AFTER   2
 11932 /*
 11933  * An instance of struct TriggerStep is used to store a single SQL statement
 11934  * that is a part of a trigger-program. 
 11936  * Instances of struct TriggerStep are stored in a singly linked list (linked
 11937  * using the "pNext" member) referenced by the "step_list" member of the 
 11938  * associated struct Trigger instance. The first element of the linked list is
 11939  * the first step of the trigger-program.
 11941  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
 11942  * "SELECT" statement. The meanings of the other members is determined by the 
 11943  * value of "op" as follows:
 11945  * (op == TK_INSERT)
 11946  * orconf    -> stores the ON CONFLICT algorithm
 11947  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
 11948  *              this stores a pointer to the SELECT statement. Otherwise NULL.
 11949  * target    -> A token holding the quoted name of the table to insert into.
 11950  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
 11951  *              this stores values to be inserted. Otherwise NULL.
 11952  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
 11953  *              statement, then this stores the column-names to be
 11954  *              inserted into.
 11956  * (op == TK_DELETE)
 11957  * target    -> A token holding the quoted name of the table to delete from.
 11958  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
 11959  *              Otherwise NULL.
 11961  * (op == TK_UPDATE)
 11962  * target    -> A token holding the quoted name of the table to update rows of.
 11963  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
 11964  *              Otherwise NULL.
 11965  * pExprList -> A list of the columns to update and the expressions to update
 11966  *              them to. See sqlite3Update() documentation of "pChanges"
 11967  *              argument.
 11969  */
 11970 struct TriggerStep {
 11971   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
 11972   u8 orconf;           /* OE_Rollback etc. */
 11973   Trigger *pTrig;      /* The trigger that this step is a part of */
 11974   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
 11975   Token target;        /* Target table for DELETE, UPDATE, INSERT */
 11976   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
 11977   ExprList *pExprList; /* SET clause for UPDATE. */
 11978   IdList *pIdList;     /* Column names for INSERT */
 11979   TriggerStep *pNext;  /* Next in the link-list */
 11980   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 11981 };
 11983 /*
 11984 ** The following structure contains information used by the sqliteFix...
 11985 ** routines as they walk the parse tree to make database references
 11986 ** explicit.  
 11987 */
 11988 typedef struct DbFixer DbFixer;
 11989 struct DbFixer {
 11990   Parse *pParse;      /* The parsing context.  Error messages written here */
 11991   Schema *pSchema;    /* Fix items to this schema */
 11992   int bVarOnly;       /* Check for variable references only */
 11993   const char *zDb;    /* Make sure all objects are contained in this database */
 11994   const char *zType;  /* Type of the container - used for error messages */
 11995   const Token *pName; /* Name of the container - used for error messages */
 11996 };
 11998 /*
 11999 ** An objected used to accumulate the text of a string where we
 12000 ** do not necessarily know how big the string will be in the end.
 12001 */
 12002 struct StrAccum {
 12003   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
 12004   char *zBase;         /* A base allocation.  Not from malloc. */
 12005   char *zText;         /* The string collected so far */
 12006   int  nChar;          /* Length of the string so far */
 12007   int  nAlloc;         /* Amount of space allocated in zText */
 12008   int  mxAlloc;        /* Maximum allowed string length */
 12009   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
 12010   u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
 12011 };
 12012 #define STRACCUM_NOMEM   1
 12013 #define STRACCUM_TOOBIG  2
 12015 /*
 12016 ** A pointer to this structure is used to communicate information
 12017 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
 12018 */
 12019 typedef struct {
 12020   sqlite3 *db;        /* The database being initialized */
 12021   char **pzErrMsg;    /* Error message stored here */
 12022   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
 12023   int rc;             /* Result code stored here */
 12024 } InitData;
 12026 /*
 12027 ** Structure containing global configuration data for the SQLite library.
 12028 **
 12029 ** This structure also contains some state information.
 12030 */
 12031 struct Sqlite3Config {
 12032   int bMemstat;                     /* True to enable memory status */
 12033   int bCoreMutex;                   /* True to enable core mutexing */
 12034   int bFullMutex;                   /* True to enable full mutexing */
 12035   int bOpenUri;                     /* True to interpret filenames as URIs */
 12036   int bUseCis;                      /* Use covering indices for full-scans */
 12037   int mxStrlen;                     /* Maximum string length */
 12038   int neverCorrupt;                 /* Database is always well-formed */
 12039   int szLookaside;                  /* Default lookaside buffer size */
 12040   int nLookaside;                   /* Default lookaside buffer count */
 12041   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
 12042   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
 12043   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
 12044   void *pHeap;                      /* Heap storage space */
 12045   int nHeap;                        /* Size of pHeap[] */
 12046   int mnReq, mxReq;                 /* Min and max heap requests sizes */
 12047   sqlite3_int64 szMmap;             /* mmap() space per open file */
 12048   sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
 12049   void *pScratch;                   /* Scratch memory */
 12050   int szScratch;                    /* Size of each scratch buffer */
 12051   int nScratch;                     /* Number of scratch buffers */
 12052   void *pPage;                      /* Page cache memory */
 12053   int szPage;                       /* Size of each page in pPage[] */
 12054   int nPage;                        /* Number of pages in pPage[] */
 12055   int mxParserStack;                /* maximum depth of the parser stack */
 12056   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
 12057   /* The above might be initialized to non-zero.  The following need to always
 12058   ** initially be zero, however. */
 12059   int isInit;                       /* True after initialization has finished */
 12060   int inProgress;                   /* True while initialization in progress */
 12061   int isMutexInit;                  /* True after mutexes are initialized */
 12062   int isMallocInit;                 /* True after malloc is initialized */
 12063   int isPCacheInit;                 /* True after malloc is initialized */
 12064   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
 12065   int nRefInitMutex;                /* Number of users of pInitMutex */
 12066   void (*xLog)(void*,int,const char*); /* Function for logging */
 12067   void *pLogArg;                       /* First argument to xLog() */
 12068   int bLocaltimeFault;              /* True to fail localtime() calls */
 12069 #ifdef SQLITE_ENABLE_SQLLOG
 12070   void(*xSqllog)(void*,sqlite3*,const char*, int);
 12071   void *pSqllogArg;
 12072 #endif
 12073 #ifdef SQLITE_VDBE_COVERAGE
 12074   /* The following callback (if not NULL) is invoked on every VDBE branch
 12075   ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
 12076   */
 12077   void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
 12078   void *pVdbeBranchArg;                                     /* 1st argument */
 12079 #endif
 12080 };
 12082 /*
 12083 ** This macro is used inside of assert() statements to indicate that
 12084 ** the assert is only valid on a well-formed database.  Instead of:
 12085 **
 12086 **     assert( X );
 12087 **
 12088 ** One writes:
 12089 **
 12090 **     assert( X || CORRUPT_DB );
 12091 **
 12092 ** CORRUPT_DB is true during normal operation.  CORRUPT_DB does not indicate
 12093 ** that the database is definitely corrupt, only that it might be corrupt.
 12094 ** For most test cases, CORRUPT_DB is set to false using a special
 12095 ** sqlite3_test_control().  This enables assert() statements to prove
 12096 ** things that are always true for well-formed databases.
 12097 */
 12098 #define CORRUPT_DB  (sqlite3Config.neverCorrupt==0)
 12100 /*
 12101 ** Context pointer passed down through the tree-walk.
 12102 */
 12103 struct Walker {
 12104   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
 12105   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
 12106   void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
 12107   Parse *pParse;                            /* Parser context.  */
 12108   int walkerDepth;                          /* Number of subqueries */
 12109   union {                                   /* Extra data for callback */
 12110     NameContext *pNC;                          /* Naming context */
 12111     int i;                                     /* Integer value */
 12112     SrcList *pSrcList;                         /* FROM clause */
 12113     struct SrcCount *pSrcCount;                /* Counting column references */
 12114   } u;
 12115 };
 12117 /* Forward declarations */
 12118 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 12119 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 12120 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
 12121 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
 12122 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
 12124 /*
 12125 ** Return code from the parse-tree walking primitives and their
 12126 ** callbacks.
 12127 */
 12128 #define WRC_Continue    0   /* Continue down into children */
 12129 #define WRC_Prune       1   /* Omit children but continue walking siblings */
 12130 #define WRC_Abort       2   /* Abandon the tree walk */
 12132 /*
 12133 ** An instance of this structure represents a set of one or more CTEs
 12134 ** (common table expressions) created by a single WITH clause.
 12135 */
 12136 struct With {
 12137   int nCte;                       /* Number of CTEs in the WITH clause */
 12138   With *pOuter;                   /* Containing WITH clause, or NULL */
 12139   struct Cte {                    /* For each CTE in the WITH clause.... */
 12140     char *zName;                    /* Name of this CTE */
 12141     ExprList *pCols;                /* List of explicit column names, or NULL */
 12142     Select *pSelect;                /* The definition of this CTE */
 12143     const char *zErr;               /* Error message for circular references */
 12144   } a[1];
 12145 };
 12147 /*
 12148 ** Assuming zIn points to the first byte of a UTF-8 character,
 12149 ** advance zIn to point to the first byte of the next UTF-8 character.
 12150 */
 12151 #define SQLITE_SKIP_UTF8(zIn) {                        \
 12152   if( (*(zIn++))>=0xc0 ){                              \
 12153     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
 12154   }                                                    \
 12157 /*
 12158 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
 12159 ** the same name but without the _BKPT suffix.  These macros invoke
 12160 ** routines that report the line-number on which the error originated
 12161 ** using sqlite3_log().  The routines also provide a convenient place
 12162 ** to set a debugger breakpoint.
 12163 */
 12164 SQLITE_PRIVATE int sqlite3CorruptError(int);
 12165 SQLITE_PRIVATE int sqlite3MisuseError(int);
 12166 SQLITE_PRIVATE int sqlite3CantopenError(int);
 12167 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
 12168 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
 12169 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
 12172 /*
 12173 ** FTS4 is really an extension for FTS3.  It is enabled using the
 12174 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
 12175 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
 12176 */
 12177 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
 12178 # define SQLITE_ENABLE_FTS3
 12179 #endif
 12181 /*
 12182 ** The ctype.h header is needed for non-ASCII systems.  It is also
 12183 ** needed by FTS3 when FTS3 is included in the amalgamation.
 12184 */
 12185 #if !defined(SQLITE_ASCII) || \
 12186     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
 12187 # include <ctype.h>
 12188 #endif
 12190 /*
 12191 ** The following macros mimic the standard library functions toupper(),
 12192 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
 12193 ** sqlite versions only work for ASCII characters, regardless of locale.
 12194 */
 12195 #ifdef SQLITE_ASCII
 12196 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
 12197 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
 12198 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
 12199 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
 12200 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
 12201 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
 12202 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
 12203 #else
 12204 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
 12205 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
 12206 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
 12207 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
 12208 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
 12209 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
 12210 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
 12211 #endif
 12213 /*
 12214 ** Internal function prototypes
 12215 */
 12216 #define sqlite3StrICmp sqlite3_stricmp
 12217 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
 12218 #define sqlite3StrNICmp sqlite3_strnicmp
 12220 SQLITE_PRIVATE int sqlite3MallocInit(void);
 12221 SQLITE_PRIVATE void sqlite3MallocEnd(void);
 12222 SQLITE_PRIVATE void *sqlite3Malloc(int);
 12223 SQLITE_PRIVATE void *sqlite3MallocZero(int);
 12224 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
 12225 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
 12226 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
 12227 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
 12228 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
 12229 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
 12230 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
 12231 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
 12232 SQLITE_PRIVATE int sqlite3MallocSize(void*);
 12233 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
 12234 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
 12235 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
 12236 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
 12237 SQLITE_PRIVATE void sqlite3PageFree(void*);
 12238 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 12239 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
 12240 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
 12242 /*
 12243 ** On systems with ample stack space and that support alloca(), make
 12244 ** use of alloca() to obtain space for large automatic objects.  By default,
 12245 ** obtain space from malloc().
 12246 **
 12247 ** The alloca() routine never returns NULL.  This will cause code paths
 12248 ** that deal with sqlite3StackAlloc() failures to be unreachable.
 12249 */
 12250 #ifdef SQLITE_USE_ALLOCA
 12251 # define sqlite3StackAllocRaw(D,N)   alloca(N)
 12252 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
 12253 # define sqlite3StackFree(D,P)       
 12254 #else
 12255 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
 12256 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
 12257 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
 12258 #endif
 12260 #ifdef SQLITE_ENABLE_MEMSYS3
 12261 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
 12262 #endif
 12263 #ifdef SQLITE_ENABLE_MEMSYS5
 12264 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
 12265 #endif
 12268 #ifndef SQLITE_MUTEX_OMIT
 12269 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
 12270 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
 12271 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
 12272 SQLITE_PRIVATE   int sqlite3MutexInit(void);
 12273 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
 12274 #endif
 12276 SQLITE_PRIVATE int sqlite3StatusValue(int);
 12277 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
 12278 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
 12280 #ifndef SQLITE_OMIT_FLOATING_POINT
 12281 SQLITE_PRIVATE   int sqlite3IsNaN(double);
 12282 #else
 12283 # define sqlite3IsNaN(X)  0
 12284 #endif
 12286 /*
 12287 ** An instance of the following structure holds information about SQL
 12288 ** functions arguments that are the parameters to the printf() function.
 12289 */
 12290 struct PrintfArguments {
 12291   int nArg;                /* Total number of arguments */
 12292   int nUsed;               /* Number of arguments used so far */
 12293   sqlite3_value **apArg;   /* The argument values */
 12294 };
 12296 #define SQLITE_PRINTF_INTERNAL 0x01
 12297 #define SQLITE_PRINTF_SQLFUNC  0x02
 12298 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
 12299 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
 12300 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
 12301 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 12302 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
 12303 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 12304 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 12305 #endif
 12306 #if defined(SQLITE_TEST)
 12307 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
 12308 #endif
 12310 /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
 12311 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 12312 SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
 12313 SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
 12314 SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
 12315 SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
 12316 SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
 12317 SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
 12318 SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
 12319 SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
 12320 SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
 12321 SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
 12322 #else
 12323 # define sqlite3ExplainBegin(X)
 12324 # define sqlite3ExplainSelect(A,B)
 12325 # define sqlite3ExplainExpr(A,B)
 12326 # define sqlite3ExplainExprList(A,B)
 12327 # define sqlite3ExplainFinish(X)
 12328 # define sqlite3VdbeExplanation(X) 0
 12329 #endif
 12332 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
 12333 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
 12334 SQLITE_PRIVATE int sqlite3Dequote(char*);
 12335 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 12336 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 12337 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
 12338 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
 12339 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
 12340 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
 12341 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
 12342 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
 12343 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
 12344 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
 12345 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
 12346 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 12347 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 12348 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 12349 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
 12350 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
 12351 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
 12352 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
 12353 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
 12354 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
 12355 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 12356 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
 12357 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
 12358 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
 12359 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
 12360 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
 12361 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
 12362 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
 12363 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
 12364 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
 12365 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
 12366 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
 12367 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
 12368 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
 12369 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
 12370 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
 12371 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
 12372 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
 12373 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
 12374 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
 12375 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
 12376 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
 12377                     sqlite3_vfs**,char**,char **);
 12378 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
 12379 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
 12381 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
 12382 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
 12383 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
 12384 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
 12385 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
 12386 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
 12387 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
 12389 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
 12390 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
 12391 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
 12392 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
 12393 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
 12395 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
 12397 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 12398 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
 12399 #else
 12400 # define sqlite3ViewGetColumnNames(A,B) 0
 12401 #endif
 12403 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 12404 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
 12405 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
 12406 #ifndef SQLITE_OMIT_AUTOINCREMENT
 12407 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
 12408 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
 12409 #else
 12410 # define sqlite3AutoincrementBegin(X)
 12411 # define sqlite3AutoincrementEnd(X)
 12412 #endif
 12413 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
 12414 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
 12415 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 12416 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 12417 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
 12418 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
 12419 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
 12420                                       Token*, Select*, Expr*, IdList*);
 12421 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
 12422 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
 12423 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 12424 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 12425 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
 12426 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 12427 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
 12428 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 12429                           Expr*, int, int);
 12430 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 12431 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 12432 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 12433                          Expr*,ExprList*,u16,Expr*,Expr*);
 12434 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 12435 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 12436 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 12437 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 12438 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 12439 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
 12440 #endif
 12441 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
 12442 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
 12443 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 12444 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 12445 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
 12446 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 12447 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 12448 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
 12449 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
 12450 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
 12451 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
 12452 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
 12453 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 12454 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
 12455 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
 12456 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
 12457 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
 12458 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
 12459 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
 12460 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
 12461 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
 12462 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
 12463 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
 12464 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
 12465 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
 12466 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
 12467 #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
 12468 #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
 12469 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
 12470 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
 12471 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
 12472 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
 12473 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
 12474 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
 12475 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 12476 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 12477 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 12478 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
 12479 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 12480 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
 12481 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 12482 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
 12483 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 12484 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 12485 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 12486 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 12487 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 12488 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 12489 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
 12490 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 12491 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 12492 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 12493 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 12494 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 12495 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 12496 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 12497 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 12498 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 12499 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 12500 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
 12501 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 12502 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 12503 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 12504 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
 12505 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
 12506 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
 12507 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
 12508 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
 12509                                      u8,u8,int,int*);
 12510 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
 12511 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
 12512 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
 12513 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
 12514 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
 12515 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
 12516 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
 12517 SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
 12518 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
 12519 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 12520 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 12521 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
 12522 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
 12523 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
 12524 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
 12525 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
 12526 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 12527 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
 12528 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 12529 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 12530 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 12532 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 12533 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
 12534 #endif
 12536 #ifndef SQLITE_OMIT_TRIGGER
 12537 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
 12538                            Expr*,int, int);
 12539 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 12540 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
 12541 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
 12542 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
 12543 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
 12544 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
 12545                             int, int, int);
 12546 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
 12547   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 12548 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
 12549 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
 12550 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
 12551                                         Select*,u8);
 12552 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
 12553 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 12554 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
 12555 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 12556 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
 12557 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
 12558 #else
 12559 # define sqlite3TriggersExist(B,C,D,E,F) 0
 12560 # define sqlite3DeleteTrigger(A,B)
 12561 # define sqlite3DropTriggerPtr(A,B)
 12562 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
 12563 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
 12564 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
 12565 # define sqlite3TriggerList(X, Y) 0
 12566 # define sqlite3ParseToplevel(p) p
 12567 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
 12568 #endif
 12570 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
 12571 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
 12572 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
 12573 #ifndef SQLITE_OMIT_AUTHORIZATION
 12574 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
 12575 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
 12576 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
 12577 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
 12578 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
 12579 #else
 12580 # define sqlite3AuthRead(a,b,c,d)
 12581 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
 12582 # define sqlite3AuthContextPush(a,b,c)
 12583 # define sqlite3AuthContextPop(a)  ((void)(a))
 12584 #endif
 12585 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
 12586 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
 12587 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
 12588 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
 12589 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
 12590 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
 12591 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
 12592 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
 12593 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
 12594 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
 12595 SQLITE_PRIVATE int sqlite3Atoi(const char*);
 12596 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
 12597 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
 12598 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
 12599 SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
 12600 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
 12601 #ifndef SQLITE_OMIT_VIRTUALTABLE
 12602 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
 12603 #endif
 12604 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
 12606 /*
 12607 ** Routines to read and write variable-length integers.  These used to
 12608 ** be defined locally, but now we use the varint routines in the util.c
 12609 ** file.  Code should use the MACRO forms below, as the Varint32 versions
 12610 ** are coded to assume the single byte case is already handled (which 
 12611 ** the MACRO form does).
 12612 */
 12613 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
 12614 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
 12615 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
 12616 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
 12617 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
 12619 /*
 12620 ** The header of a record consists of a sequence variable-length integers.
 12621 ** These integers are almost always small and are encoded as a single byte.
 12622 ** The following macros take advantage this fact to provide a fast encode
 12623 ** and decode of the integers in a record header.  It is faster for the common
 12624 ** case where the integer is a single byte.  It is a little slower when the
 12625 ** integer is two or more bytes.  But overall it is faster.
 12626 **
 12627 ** The following expressions are equivalent:
 12628 **
 12629 **     x = sqlite3GetVarint32( A, &B );
 12630 **     x = sqlite3PutVarint32( A, B );
 12631 **
 12632 **     x = getVarint32( A, B );
 12633 **     x = putVarint32( A, B );
 12634 **
 12635 */
 12636 #define getVarint32(A,B)  \
 12637   (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
 12638 #define putVarint32(A,B)  \
 12639   (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
 12640   sqlite3PutVarint32((A),(B)))
 12641 #define getVarint    sqlite3GetVarint
 12642 #define putVarint    sqlite3PutVarint
 12645 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
 12646 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
 12647 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
 12648 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
 12649 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 12650 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
 12651 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
 12652 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 12653 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 12654 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 12656 #if defined(SQLITE_TEST) 
 12657 SQLITE_PRIVATE const char *sqlite3ErrName(int);
 12658 #endif
 12660 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 12661 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 12662 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 12663 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 12664 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 12665 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
 12666 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
 12667 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
 12668 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 12669 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 12670 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
 12671 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
 12672 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
 12673 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
 12674 SQLITE_PRIVATE int sqlite3AbsInt32(int);
 12675 #ifdef SQLITE_ENABLE_8_3_NAMES
 12676 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
 12677 #else
 12678 # define sqlite3FileSuffix3(X,Y)
 12679 #endif
 12680 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
 12682 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 12683 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
 12684 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
 12685                         void(*)(void*));
 12686 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
 12687 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
 12688 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
 12689 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 12690 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
 12691 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
 12692 #ifndef SQLITE_AMALGAMATION
 12693 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
 12694 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
 12695 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
 12696 SQLITE_PRIVATE const Token sqlite3IntTokens[];
 12697 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 12698 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 12699 #ifndef SQLITE_OMIT_WSD
 12700 SQLITE_PRIVATE int sqlite3PendingByte;
 12701 #endif
 12702 #endif
 12703 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 12704 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 12705 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 12706 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 12707 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 12708 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 12709 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 12710 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
 12711 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
 12712 SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
 12713 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
 12714 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
 12715 SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
 12716 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 12717 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 12718 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 12719 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 12720 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 12721 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
 12722 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 12723 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
 12724 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 12725 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 12726 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 12727 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 12728 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 12729 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 12730 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
 12731 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
 12732 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
 12733 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
 12734 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
 12735 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
 12736 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
 12737 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
 12738 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
 12739 #ifdef SQLITE_DEBUG
 12740 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
 12741 #endif
 12742 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
 12743   void (*)(sqlite3_context*,int,sqlite3_value **),
 12744   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 12745   FuncDestructor *pDestructor
 12746 );
 12747 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
 12748 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
 12750 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
 12751 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 12752 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
 12753 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
 12754 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
 12755 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
 12756 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
 12757 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 12759 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
 12760 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 12762 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 12763 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
 12764 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
 12765 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
 12766 #endif
 12768 /*
 12769 ** The interface to the LEMON-generated parser
 12770 */
 12771 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
 12772 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
 12773 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
 12774 #ifdef YYTRACKMAXSTACKDEPTH
 12775 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
 12776 #endif
 12778 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
 12779 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 12780 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
 12781 #else
 12782 # define sqlite3CloseExtensions(X)
 12783 #endif
 12785 #ifndef SQLITE_OMIT_SHARED_CACHE
 12786 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
 12787 #else
 12788   #define sqlite3TableLock(v,w,x,y,z)
 12789 #endif
 12791 #ifdef SQLITE_TEST
 12792 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
 12793 #endif
 12795 #ifdef SQLITE_OMIT_VIRTUALTABLE
 12796 #  define sqlite3VtabClear(Y)
 12797 #  define sqlite3VtabSync(X,Y) SQLITE_OK
 12798 #  define sqlite3VtabRollback(X)
 12799 #  define sqlite3VtabCommit(X)
 12800 #  define sqlite3VtabInSync(db) 0
 12801 #  define sqlite3VtabLock(X) 
 12802 #  define sqlite3VtabUnlock(X)
 12803 #  define sqlite3VtabUnlockList(X)
 12804 #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
 12805 #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
 12806 #else
 12807 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
 12808 SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
 12809 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, Vdbe*);
 12810 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
 12811 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
 12812 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
 12813 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
 12814 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
 12815 SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
 12816 SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
 12817 SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
 12818 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
 12819 #endif
 12820 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
 12821 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
 12822 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
 12823 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
 12824 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
 12825 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 12826 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 12827 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 12828 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
 12829 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 12830 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 12831 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
 12832 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
 12833 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 12834 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
 12835 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 12836 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 12837 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 12838 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 12839 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 12840 #ifndef SQLITE_OMIT_WAL
 12841 SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
 12842 SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
 12843 #endif
 12844 #ifndef SQLITE_OMIT_CTE
 12845 SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
 12846 SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
 12847 SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
 12848 #else
 12849 #define sqlite3WithPush(x,y,z)
 12850 #define sqlite3WithDelete(x,y)
 12851 #endif
 12853 /* Declarations for functions in fkey.c. All of these are replaced by
 12854 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 12855 ** key functionality is available. If OMIT_TRIGGER is defined but
 12856 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 12857 ** this case foreign keys are parsed, but no other functionality is 
 12858 ** provided (enforcement of FK constraints requires the triggers sub-system).
 12859 */
 12860 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 12861 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
 12862 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
 12863 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
 12864 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
 12865 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
 12866 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
 12867 #else
 12868   #define sqlite3FkActions(a,b,c,d,e,f)
 12869   #define sqlite3FkCheck(a,b,c,d,e,f)
 12870   #define sqlite3FkDropTable(a,b,c)
 12871   #define sqlite3FkOldmask(a,b)         0
 12872   #define sqlite3FkRequired(a,b,c,d)    0
 12873 #endif
 12874 #ifndef SQLITE_OMIT_FOREIGN_KEY
 12875 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
 12876 SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
 12877 #else
 12878   #define sqlite3FkDelete(a,b)
 12879   #define sqlite3FkLocateIndex(a,b,c,d,e)
 12880 #endif
 12883 /*
 12884 ** Available fault injectors.  Should be numbered beginning with 0.
 12885 */
 12886 #define SQLITE_FAULTINJECTOR_MALLOC     0
 12887 #define SQLITE_FAULTINJECTOR_COUNT      1
 12889 /*
 12890 ** The interface to the code in fault.c used for identifying "benign"
 12891 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
 12892 ** is not defined.
 12893 */
 12894 #ifndef SQLITE_OMIT_BUILTIN_TEST
 12895 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
 12896 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
 12897 #else
 12898   #define sqlite3BeginBenignMalloc()
 12899   #define sqlite3EndBenignMalloc()
 12900 #endif
 12902 #define IN_INDEX_ROWID           1
 12903 #define IN_INDEX_EPH             2
 12904 #define IN_INDEX_INDEX_ASC       3
 12905 #define IN_INDEX_INDEX_DESC      4
 12906 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
 12908 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 12909 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
 12910 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
 12911 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
 12912 SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
 12913 #else
 12914   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
 12915   #define sqlite3JournalExists(p) 1
 12916 #endif
 12918 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
 12919 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
 12920 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
 12922 #if SQLITE_MAX_EXPR_DEPTH>0
 12923 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
 12924 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
 12925 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
 12926 #else
 12927   #define sqlite3ExprSetHeight(x,y)
 12928   #define sqlite3SelectExprHeight(x) 0
 12929   #define sqlite3ExprCheckHeight(x,y)
 12930 #endif
 12932 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
 12933 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
 12935 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 12936 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
 12937 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
 12938 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
 12939 #else
 12940   #define sqlite3ConnectionBlocked(x,y)
 12941   #define sqlite3ConnectionUnlocked(x)
 12942   #define sqlite3ConnectionClosed(x)
 12943 #endif
 12945 #ifdef SQLITE_DEBUG
 12946 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
 12947 #endif
 12949 /*
 12950 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
 12951 ** sqlite3IoTrace is a pointer to a printf-like routine used to
 12952 ** print I/O tracing messages. 
 12953 */
 12954 #ifdef SQLITE_ENABLE_IOTRACE
 12955 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
 12956 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
 12957 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
 12958 #else
 12959 # define IOTRACE(A)
 12960 # define sqlite3VdbeIOTraceSql(X)
 12961 #endif
 12963 /*
 12964 ** These routines are available for the mem2.c debugging memory allocator
 12965 ** only.  They are used to verify that different "types" of memory
 12966 ** allocations are properly tracked by the system.
 12967 **
 12968 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
 12969 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
 12970 ** a single bit set.
 12971 **
 12972 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
 12973 ** argument match the type set by the previous sqlite3MemdebugSetType().
 12974 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
 12975 **
 12976 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
 12977 ** argument match the type set by the previous sqlite3MemdebugSetType().
 12978 **
 12979 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
 12980 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
 12981 ** it might have been allocated by lookaside, except the allocation was
 12982 ** too large or lookaside was already full.  It is important to verify
 12983 ** that allocations that might have been satisfied by lookaside are not
 12984 ** passed back to non-lookaside free() routines.  Asserts such as the
 12985 ** example above are placed on the non-lookaside free() routines to verify
 12986 ** this constraint. 
 12987 **
 12988 ** All of this is no-op for a production build.  It only comes into
 12989 ** play when the SQLITE_MEMDEBUG compile-time option is used.
 12990 */
 12991 #ifdef SQLITE_MEMDEBUG
 12992 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
 12993 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
 12994 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
 12995 #else
 12996 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
 12997 # define sqlite3MemdebugHasType(X,Y)  1
 12998 # define sqlite3MemdebugNoType(X,Y)   1
 12999 #endif
 13000 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
 13001 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
 13002 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
 13003 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
 13004 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
 13006 #endif /* _SQLITEINT_H_ */
 13008 /************** End of sqliteInt.h *******************************************/
 13009 /************** Begin file global.c ******************************************/
 13010 /*
 13011 ** 2008 June 13
 13012 **
 13013 ** The author disclaims copyright to this source code.  In place of
 13014 ** a legal notice, here is a blessing:
 13015 **
 13016 **    May you do good and not evil.
 13017 **    May you find forgiveness for yourself and forgive others.
 13018 **    May you share freely, never taking more than you give.
 13019 **
 13020 *************************************************************************
 13021 **
 13022 ** This file contains definitions of global variables and contants.
 13023 */
 13025 /* An array to map all upper-case characters into their corresponding
 13026 ** lower-case character. 
 13027 **
 13028 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
 13029 ** handle case conversions for the UTF character set since the tables
 13030 ** involved are nearly as big or bigger than SQLite itself.
 13031 */
 13032 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
 13033 #ifdef SQLITE_ASCII
 13034       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
 13035      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
 13036      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
 13037      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
 13038     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
 13039     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
 13040     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
 13041     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 13042     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
 13043     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
 13044     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
 13045     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
 13046     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
 13047     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
 13048     252,253,254,255
 13049 #endif
 13050 #ifdef SQLITE_EBCDIC
 13051       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
 13052      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
 13053      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
 13054      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
 13055      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
 13056      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
 13057      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
 13058     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
 13059     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
 13060     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
 13061     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
 13062     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
 13063     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
 13064     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
 13065     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
 13066     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
 13067 #endif
 13068 };
 13070 /*
 13071 ** The following 256 byte lookup table is used to support SQLites built-in
 13072 ** equivalents to the following standard library functions:
 13073 **
 13074 **   isspace()                        0x01
 13075 **   isalpha()                        0x02
 13076 **   isdigit()                        0x04
 13077 **   isalnum()                        0x06
 13078 **   isxdigit()                       0x08
 13079 **   toupper()                        0x20
 13080 **   SQLite identifier character      0x40
 13081 **
 13082 ** Bit 0x20 is set if the mapped character requires translation to upper
 13083 ** case. i.e. if the character is a lower-case ASCII character.
 13084 ** If x is a lower-case ASCII character, then its upper-case equivalent
 13085 ** is (x - 0x20). Therefore toupper() can be implemented as:
 13086 **
 13087 **   (x & ~(map[x]&0x20))
 13088 **
 13089 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
 13090 ** array. tolower() is used more often than toupper() by SQLite.
 13091 **
 13092 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
 13093 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
 13094 ** non-ASCII UTF character. Hence the test for whether or not a character is
 13095 ** part of an identifier is 0x46.
 13096 **
 13097 ** SQLite's versions are identical to the standard versions assuming a
 13098 ** locale of "C". They are implemented as macros in sqliteInt.h.
 13099 */
 13100 #ifdef SQLITE_ASCII
 13101 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
 13102   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
 13103   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
 13104   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
 13105   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
 13106   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
 13107   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
 13108   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
 13109   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
 13111   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
 13112   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
 13113   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
 13114   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
 13115   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
 13116   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
 13117   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
 13118   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
 13120   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
 13121   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
 13122   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
 13123   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
 13124   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
 13125   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
 13126   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
 13127   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
 13129   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
 13130   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
 13131   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
 13132   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
 13133   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
 13134   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
 13135   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
 13136   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
 13137 };
 13138 #endif
 13140 #ifndef SQLITE_USE_URI
 13141 # define  SQLITE_USE_URI 0
 13142 #endif
 13144 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
 13145 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
 13146 #endif
 13148 /*
 13149 ** The following singleton contains the global configuration for
 13150 ** the SQLite library.
 13151 */
 13152 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
 13153    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
 13154    1,                         /* bCoreMutex */
 13155    SQLITE_THREADSAFE==1,      /* bFullMutex */
 13156    SQLITE_USE_URI,            /* bOpenUri */
 13157    SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
 13158    0x7ffffffe,                /* mxStrlen */
 13159    0,                         /* neverCorrupt */
 13160    128,                       /* szLookaside */
 13161    500,                       /* nLookaside */
 13162    {0,0,0,0,0,0,0,0},         /* m */
 13163    {0,0,0,0,0,0,0,0,0},       /* mutex */
 13164    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
 13165    (void*)0,                  /* pHeap */
 13166    0,                         /* nHeap */
 13167    0, 0,                      /* mnHeap, mxHeap */
 13168    SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
 13169    SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
 13170    (void*)0,                  /* pScratch */
 13171    0,                         /* szScratch */
 13172    0,                         /* nScratch */
 13173    (void*)0,                  /* pPage */
 13174    0,                         /* szPage */
 13175    0,                         /* nPage */
 13176    0,                         /* mxParserStack */
 13177    0,                         /* sharedCacheEnabled */
 13178    /* All the rest should always be initialized to zero */
 13179    0,                         /* isInit */
 13180    0,                         /* inProgress */
 13181    0,                         /* isMutexInit */
 13182    0,                         /* isMallocInit */
 13183    0,                         /* isPCacheInit */
 13184    0,                         /* pInitMutex */
 13185    0,                         /* nRefInitMutex */
 13186    0,                         /* xLog */
 13187    0,                         /* pLogArg */
 13188    0,                         /* bLocaltimeFault */
 13189 #ifdef SQLITE_ENABLE_SQLLOG
 13190    0,                         /* xSqllog */
 13191    0                          /* pSqllogArg */
 13192 #endif
 13193 };
 13195 /*
 13196 ** Hash table for global functions - functions common to all
 13197 ** database connections.  After initialization, this table is
 13198 ** read-only.
 13199 */
 13200 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 13202 /*
 13203 ** Constant tokens for values 0 and 1.
 13204 */
 13205 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 13206    { "0", 1 },
 13207    { "1", 1 }
 13208 };
 13211 /*
 13212 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 13213 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 13214 ** the database page that contains the pending byte.  It never attempts
 13215 ** to read or write that page.  The pending byte page is set assign
 13216 ** for use by the VFS layers as space for managing file locks.
 13217 **
 13218 ** During testing, it is often desirable to move the pending byte to
 13219 ** a different position in the file.  This allows code that has to
 13220 ** deal with the pending byte to run on files that are much smaller
 13221 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
 13222 ** move the pending byte.
 13223 **
 13224 ** IMPORTANT:  Changing the pending byte to any value other than
 13225 ** 0x40000000 results in an incompatible database file format!
 13226 ** Changing the pending byte during operating results in undefined
 13227 ** and dileterious behavior.
 13228 */
 13229 #ifndef SQLITE_OMIT_WSD
 13230 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
 13231 #endif
 13233 /*
 13234 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
 13235 ** created by mkopcodeh.awk during compilation.  Data is obtained
 13236 ** from the comments following the "case OP_xxxx:" statements in
 13237 ** the vdbe.c file.  
 13238 */
 13239 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
 13241 /************** End of global.c **********************************************/
 13242 /************** Begin file ctime.c *******************************************/
 13243 /*
 13244 ** 2010 February 23
 13245 **
 13246 ** The author disclaims copyright to this source code.  In place of
 13247 ** a legal notice, here is a blessing:
 13248 **
 13249 **    May you do good and not evil.
 13250 **    May you find forgiveness for yourself and forgive others.
 13251 **    May you share freely, never taking more than you give.
 13252 **
 13253 *************************************************************************
 13254 **
 13255 ** This file implements routines used to report what compile-time options
 13256 ** SQLite was built with.
 13257 */
 13259 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 13262 /*
 13263 ** An array of names of all compile-time options.  This array should 
 13264 ** be sorted A-Z.
 13265 **
 13266 ** This array looks large, but in a typical installation actually uses
 13267 ** only a handful of compile-time options, so most times this array is usually
 13268 ** rather short and uses little memory space.
 13269 */
 13270 static const char * const azCompileOpt[] = {
 13272 /* These macros are provided to "stringify" the value of the define
 13273 ** for those options in which the value is meaningful. */
 13274 #define CTIMEOPT_VAL_(opt) #opt
 13275 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
 13277 #ifdef SQLITE_32BIT_ROWID
 13278   "32BIT_ROWID",
 13279 #endif
 13280 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
 13281   "4_BYTE_ALIGNED_MALLOC",
 13282 #endif
 13283 #ifdef SQLITE_CASE_SENSITIVE_LIKE
 13284   "CASE_SENSITIVE_LIKE",
 13285 #endif
 13286 #ifdef SQLITE_CHECK_PAGES
 13287   "CHECK_PAGES",
 13288 #endif
 13289 #ifdef SQLITE_COVERAGE_TEST
 13290   "COVERAGE_TEST",
 13291 #endif
 13292 #ifdef SQLITE_DEBUG
 13293   "DEBUG",
 13294 #endif
 13295 #ifdef SQLITE_DEFAULT_LOCKING_MODE
 13296   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 13297 #endif
 13298 #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
 13299   "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
 13300 #endif
 13301 #ifdef SQLITE_DISABLE_DIRSYNC
 13302   "DISABLE_DIRSYNC",
 13303 #endif
 13304 #ifdef SQLITE_DISABLE_LFS
 13305   "DISABLE_LFS",
 13306 #endif
 13307 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 13308   "ENABLE_ATOMIC_WRITE",
 13309 #endif
 13310 #ifdef SQLITE_ENABLE_CEROD
 13311   "ENABLE_CEROD",
 13312 #endif
 13313 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 13314   "ENABLE_COLUMN_METADATA",
 13315 #endif
 13316 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 13317   "ENABLE_EXPENSIVE_ASSERT",
 13318 #endif
 13319 #ifdef SQLITE_ENABLE_FTS1
 13320   "ENABLE_FTS1",
 13321 #endif
 13322 #ifdef SQLITE_ENABLE_FTS2
 13323   "ENABLE_FTS2",
 13324 #endif
 13325 #ifdef SQLITE_ENABLE_FTS3
 13326   "ENABLE_FTS3",
 13327 #endif
 13328 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
 13329   "ENABLE_FTS3_PARENTHESIS",
 13330 #endif
 13331 #ifdef SQLITE_ENABLE_FTS4
 13332   "ENABLE_FTS4",
 13333 #endif
 13334 #ifdef SQLITE_ENABLE_ICU
 13335   "ENABLE_ICU",
 13336 #endif
 13337 #ifdef SQLITE_ENABLE_IOTRACE
 13338   "ENABLE_IOTRACE",
 13339 #endif
 13340 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
 13341   "ENABLE_LOAD_EXTENSION",
 13342 #endif
 13343 #ifdef SQLITE_ENABLE_LOCKING_STYLE
 13344   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
 13345 #endif
 13346 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 13347   "ENABLE_MEMORY_MANAGEMENT",
 13348 #endif
 13349 #ifdef SQLITE_ENABLE_MEMSYS3
 13350   "ENABLE_MEMSYS3",
 13351 #endif
 13352 #ifdef SQLITE_ENABLE_MEMSYS5
 13353   "ENABLE_MEMSYS5",
 13354 #endif
 13355 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
 13356   "ENABLE_OVERSIZE_CELL_CHECK",
 13357 #endif
 13358 #ifdef SQLITE_ENABLE_RTREE
 13359   "ENABLE_RTREE",
 13360 #endif
 13361 #if defined(SQLITE_ENABLE_STAT4)
 13362   "ENABLE_STAT4",
 13363 #elif defined(SQLITE_ENABLE_STAT3)
 13364   "ENABLE_STAT3",
 13365 #endif
 13366 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 13367   "ENABLE_UNLOCK_NOTIFY",
 13368 #endif
 13369 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
 13370   "ENABLE_UPDATE_DELETE_LIMIT",
 13371 #endif
 13372 #ifdef SQLITE_HAS_CODEC
 13373   "HAS_CODEC",
 13374 #endif
 13375 #ifdef SQLITE_HAVE_ISNAN
 13376   "HAVE_ISNAN",
 13377 #endif
 13378 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 13379   "HOMEGROWN_RECURSIVE_MUTEX",
 13380 #endif
 13381 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 13382   "IGNORE_AFP_LOCK_ERRORS",
 13383 #endif
 13384 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 13385   "IGNORE_FLOCK_LOCK_ERRORS",
 13386 #endif
 13387 #ifdef SQLITE_INT64_TYPE
 13388   "INT64_TYPE",
 13389 #endif
 13390 #ifdef SQLITE_LOCK_TRACE
 13391   "LOCK_TRACE",
 13392 #endif
 13393 #if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
 13394   "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
 13395 #endif
 13396 #ifdef SQLITE_MAX_SCHEMA_RETRY
 13397   "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
 13398 #endif
 13399 #ifdef SQLITE_MEMDEBUG
 13400   "MEMDEBUG",
 13401 #endif
 13402 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 13403   "MIXED_ENDIAN_64BIT_FLOAT",
 13404 #endif
 13405 #ifdef SQLITE_NO_SYNC
 13406   "NO_SYNC",
 13407 #endif
 13408 #ifdef SQLITE_OMIT_ALTERTABLE
 13409   "OMIT_ALTERTABLE",
 13410 #endif
 13411 #ifdef SQLITE_OMIT_ANALYZE
 13412   "OMIT_ANALYZE",
 13413 #endif
 13414 #ifdef SQLITE_OMIT_ATTACH
 13415   "OMIT_ATTACH",
 13416 #endif
 13417 #ifdef SQLITE_OMIT_AUTHORIZATION
 13418   "OMIT_AUTHORIZATION",
 13419 #endif
 13420 #ifdef SQLITE_OMIT_AUTOINCREMENT
 13421   "OMIT_AUTOINCREMENT",
 13422 #endif
 13423 #ifdef SQLITE_OMIT_AUTOINIT
 13424   "OMIT_AUTOINIT",
 13425 #endif
 13426 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
 13427   "OMIT_AUTOMATIC_INDEX",
 13428 #endif
 13429 #ifdef SQLITE_OMIT_AUTORESET
 13430   "OMIT_AUTORESET",
 13431 #endif
 13432 #ifdef SQLITE_OMIT_AUTOVACUUM
 13433   "OMIT_AUTOVACUUM",
 13434 #endif
 13435 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
 13436   "OMIT_BETWEEN_OPTIMIZATION",
 13437 #endif
 13438 #ifdef SQLITE_OMIT_BLOB_LITERAL
 13439   "OMIT_BLOB_LITERAL",
 13440 #endif
 13441 #ifdef SQLITE_OMIT_BTREECOUNT
 13442   "OMIT_BTREECOUNT",
 13443 #endif
 13444 #ifdef SQLITE_OMIT_BUILTIN_TEST
 13445   "OMIT_BUILTIN_TEST",
 13446 #endif
 13447 #ifdef SQLITE_OMIT_CAST
 13448   "OMIT_CAST",
 13449 #endif
 13450 #ifdef SQLITE_OMIT_CHECK
 13451   "OMIT_CHECK",
 13452 #endif
 13453 #ifdef SQLITE_OMIT_COMPLETE
 13454   "OMIT_COMPLETE",
 13455 #endif
 13456 #ifdef SQLITE_OMIT_COMPOUND_SELECT
 13457   "OMIT_COMPOUND_SELECT",
 13458 #endif
 13459 #ifdef SQLITE_OMIT_CTE
 13460   "OMIT_CTE",
 13461 #endif
 13462 #ifdef SQLITE_OMIT_DATETIME_FUNCS
 13463   "OMIT_DATETIME_FUNCS",
 13464 #endif
 13465 #ifdef SQLITE_OMIT_DECLTYPE
 13466   "OMIT_DECLTYPE",
 13467 #endif
 13468 #ifdef SQLITE_OMIT_DEPRECATED
 13469   "OMIT_DEPRECATED",
 13470 #endif
 13471 #ifdef SQLITE_OMIT_DISKIO
 13472   "OMIT_DISKIO",
 13473 #endif
 13474 #ifdef SQLITE_OMIT_EXPLAIN
 13475   "OMIT_EXPLAIN",
 13476 #endif
 13477 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
 13478   "OMIT_FLAG_PRAGMAS",
 13479 #endif
 13480 #ifdef SQLITE_OMIT_FLOATING_POINT
 13481   "OMIT_FLOATING_POINT",
 13482 #endif
 13483 #ifdef SQLITE_OMIT_FOREIGN_KEY
 13484   "OMIT_FOREIGN_KEY",
 13485 #endif
 13486 #ifdef SQLITE_OMIT_GET_TABLE
 13487   "OMIT_GET_TABLE",
 13488 #endif
 13489 #ifdef SQLITE_OMIT_INCRBLOB
 13490   "OMIT_INCRBLOB",
 13491 #endif
 13492 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
 13493   "OMIT_INTEGRITY_CHECK",
 13494 #endif
 13495 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
 13496   "OMIT_LIKE_OPTIMIZATION",
 13497 #endif
 13498 #ifdef SQLITE_OMIT_LOAD_EXTENSION
 13499   "OMIT_LOAD_EXTENSION",
 13500 #endif
 13501 #ifdef SQLITE_OMIT_LOCALTIME
 13502   "OMIT_LOCALTIME",
 13503 #endif
 13504 #ifdef SQLITE_OMIT_LOOKASIDE
 13505   "OMIT_LOOKASIDE",
 13506 #endif
 13507 #ifdef SQLITE_OMIT_MEMORYDB
 13508   "OMIT_MEMORYDB",
 13509 #endif
 13510 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
 13511   "OMIT_OR_OPTIMIZATION",
 13512 #endif
 13513 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
 13514   "OMIT_PAGER_PRAGMAS",
 13515 #endif
 13516 #ifdef SQLITE_OMIT_PRAGMA
 13517   "OMIT_PRAGMA",
 13518 #endif
 13519 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 13520   "OMIT_PROGRESS_CALLBACK",
 13521 #endif
 13522 #ifdef SQLITE_OMIT_QUICKBALANCE
 13523   "OMIT_QUICKBALANCE",
 13524 #endif
 13525 #ifdef SQLITE_OMIT_REINDEX
 13526   "OMIT_REINDEX",
 13527 #endif
 13528 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
 13529   "OMIT_SCHEMA_PRAGMAS",
 13530 #endif
 13531 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 13532   "OMIT_SCHEMA_VERSION_PRAGMAS",
 13533 #endif
 13534 #ifdef SQLITE_OMIT_SHARED_CACHE
 13535   "OMIT_SHARED_CACHE",
 13536 #endif
 13537 #ifdef SQLITE_OMIT_SUBQUERY
 13538   "OMIT_SUBQUERY",
 13539 #endif
 13540 #ifdef SQLITE_OMIT_TCL_VARIABLE
 13541   "OMIT_TCL_VARIABLE",
 13542 #endif
 13543 #ifdef SQLITE_OMIT_TEMPDB
 13544   "OMIT_TEMPDB",
 13545 #endif
 13546 #ifdef SQLITE_OMIT_TRACE
 13547   "OMIT_TRACE",
 13548 #endif
 13549 #ifdef SQLITE_OMIT_TRIGGER
 13550   "OMIT_TRIGGER",
 13551 #endif
 13552 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 13553   "OMIT_TRUNCATE_OPTIMIZATION",
 13554 #endif
 13555 #ifdef SQLITE_OMIT_UTF16
 13556   "OMIT_UTF16",
 13557 #endif
 13558 #ifdef SQLITE_OMIT_VACUUM
 13559   "OMIT_VACUUM",
 13560 #endif
 13561 #ifdef SQLITE_OMIT_VIEW
 13562   "OMIT_VIEW",
 13563 #endif
 13564 #ifdef SQLITE_OMIT_VIRTUALTABLE
 13565   "OMIT_VIRTUALTABLE",
 13566 #endif
 13567 #ifdef SQLITE_OMIT_WAL
 13568   "OMIT_WAL",
 13569 #endif
 13570 #ifdef SQLITE_OMIT_WSD
 13571   "OMIT_WSD",
 13572 #endif
 13573 #ifdef SQLITE_OMIT_XFER_OPT
 13574   "OMIT_XFER_OPT",
 13575 #endif
 13576 #ifdef SQLITE_PERFORMANCE_TRACE
 13577   "PERFORMANCE_TRACE",
 13578 #endif
 13579 #ifdef SQLITE_PROXY_DEBUG
 13580   "PROXY_DEBUG",
 13581 #endif
 13582 #ifdef SQLITE_RTREE_INT_ONLY
 13583   "RTREE_INT_ONLY",
 13584 #endif
 13585 #ifdef SQLITE_SECURE_DELETE
 13586   "SECURE_DELETE",
 13587 #endif
 13588 #ifdef SQLITE_SMALL_STACK
 13589   "SMALL_STACK",
 13590 #endif
 13591 #ifdef SQLITE_SOUNDEX
 13592   "SOUNDEX",
 13593 #endif
 13594 #ifdef SQLITE_SYSTEM_MALLOC
 13595   "SYSTEM_MALLOC",
 13596 #endif
 13597 #ifdef SQLITE_TCL
 13598   "TCL",
 13599 #endif
 13600 #if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
 13601   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
 13602 #endif
 13603 #ifdef SQLITE_TEST
 13604   "TEST",
 13605 #endif
 13606 #if defined(SQLITE_THREADSAFE)
 13607   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
 13608 #endif
 13609 #ifdef SQLITE_USE_ALLOCA
 13610   "USE_ALLOCA",
 13611 #endif
 13612 #ifdef SQLITE_WIN32_MALLOC
 13613   "WIN32_MALLOC",
 13614 #endif
 13615 #ifdef SQLITE_ZERO_MALLOC
 13616   "ZERO_MALLOC"
 13617 #endif
 13618 };
 13620 /*
 13621 ** Given the name of a compile-time option, return true if that option
 13622 ** was used and false if not.
 13623 **
 13624 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
 13625 ** is not required for a match.
 13626 */
 13627 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
 13628   int i, n;
 13629   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
 13630   n = sqlite3Strlen30(zOptName);
 13632   /* Since ArraySize(azCompileOpt) is normally in single digits, a
 13633   ** linear search is adequate.  No need for a binary search. */
 13634   for(i=0; i<ArraySize(azCompileOpt); i++){
 13635     if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
 13636      && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
 13637     ){
 13638       return 1;
 13641   return 0;
 13644 /*
 13645 ** Return the N-th compile-time option string.  If N is out of range,
 13646 ** return a NULL pointer.
 13647 */
 13648 SQLITE_API const char *sqlite3_compileoption_get(int N){
 13649   if( N>=0 && N<ArraySize(azCompileOpt) ){
 13650     return azCompileOpt[N];
 13652   return 0;
 13655 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 13657 /************** End of ctime.c ***********************************************/
 13658 /************** Begin file status.c ******************************************/
 13659 /*
 13660 ** 2008 June 18
 13661 **
 13662 ** The author disclaims copyright to this source code.  In place of
 13663 ** a legal notice, here is a blessing:
 13664 **
 13665 **    May you do good and not evil.
 13666 **    May you find forgiveness for yourself and forgive others.
 13667 **    May you share freely, never taking more than you give.
 13668 **
 13669 *************************************************************************
 13670 **
 13671 ** This module implements the sqlite3_status() interface and related
 13672 ** functionality.
 13673 */
 13674 /************** Include vdbeInt.h in the middle of status.c ******************/
 13675 /************** Begin file vdbeInt.h *****************************************/
 13676 /*
 13677 ** 2003 September 6
 13678 **
 13679 ** The author disclaims copyright to this source code.  In place of
 13680 ** a legal notice, here is a blessing:
 13681 **
 13682 **    May you do good and not evil.
 13683 **    May you find forgiveness for yourself and forgive others.
 13684 **    May you share freely, never taking more than you give.
 13685 **
 13686 *************************************************************************
 13687 ** This is the header file for information that is private to the
 13688 ** VDBE.  This information used to all be at the top of the single
 13689 ** source code file "vdbe.c".  When that file became too big (over
 13690 ** 6000 lines long) it was split up into several smaller files and
 13691 ** this header information was factored out.
 13692 */
 13693 #ifndef _VDBEINT_H_
 13694 #define _VDBEINT_H_
 13696 /*
 13697 ** The maximum number of times that a statement will try to reparse
 13698 ** itself before giving up and returning SQLITE_SCHEMA.
 13699 */
 13700 #ifndef SQLITE_MAX_SCHEMA_RETRY
 13701 # define SQLITE_MAX_SCHEMA_RETRY 50
 13702 #endif
 13704 /*
 13705 ** SQL is translated into a sequence of instructions to be
 13706 ** executed by a virtual machine.  Each instruction is an instance
 13707 ** of the following structure.
 13708 */
 13709 typedef struct VdbeOp Op;
 13711 /*
 13712 ** Boolean values
 13713 */
 13714 typedef unsigned Bool;
 13716 /* Opaque type used by code in vdbesort.c */
 13717 typedef struct VdbeSorter VdbeSorter;
 13719 /* Opaque type used by the explainer */
 13720 typedef struct Explain Explain;
 13722 /* Elements of the linked list at Vdbe.pAuxData */
 13723 typedef struct AuxData AuxData;
 13725 /*
 13726 ** A cursor is a pointer into a single BTree within a database file.
 13727 ** The cursor can seek to a BTree entry with a particular key, or
 13728 ** loop over all entries of the Btree.  You can also insert new BTree
 13729 ** entries or retrieve the key or data from the entry that the cursor
 13730 ** is currently pointing to.
 13731 **
 13732 ** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
 13733 ** A pseudo-table is a single-row table implemented by registers.
 13734 ** 
 13735 ** Every cursor that the virtual machine has open is represented by an
 13736 ** instance of the following structure.
 13737 */
 13738 struct VdbeCursor {
 13739   BtCursor *pCursor;    /* The cursor structure of the backend */
 13740   Btree *pBt;           /* Separate file holding temporary table */
 13741   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
 13742   int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
 13743   int pseudoTableReg;   /* Register holding pseudotable content. */
 13744   i16 nField;           /* Number of fields in the header */
 13745   u16 nHdrParsed;       /* Number of header fields parsed so far */
 13746   i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
 13747   u8 nullRow;           /* True if pointing to a row with no data */
 13748   u8 rowidIsValid;      /* True if lastRowid is valid */
 13749   u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
 13750   Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
 13751   Bool isTable:1;       /* True if a table requiring integer keys */
 13752   Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
 13753   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
 13754   i64 seqCount;         /* Sequence counter */
 13755   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
 13756   i64 lastRowid;        /* Rowid being deleted by OP_Delete */
 13757   VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
 13759   /* Cached information about the header for the data record that the
 13760   ** cursor is currently pointing to.  Only valid if cacheStatus matches
 13761   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
 13762   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
 13763   ** the cache is out of date.
 13764   **
 13765   ** aRow might point to (ephemeral) data for the current row, or it might
 13766   ** be NULL.
 13767   */
 13768   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
 13769   u32 payloadSize;      /* Total number of bytes in the record */
 13770   u32 szRow;            /* Byte available in aRow */
 13771   u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
 13772   const u8 *aRow;       /* Data for the current row, if all on one page */
 13773   u32 aType[1];         /* Type values for all entries in the record */
 13774   /* 2*nField extra array elements allocated for aType[], beyond the one
 13775   ** static element declared in the structure.  nField total array slots for
 13776   ** aType[] and nField+1 array slots for aOffset[] */
 13777 };
 13778 typedef struct VdbeCursor VdbeCursor;
 13780 /*
 13781 ** When a sub-program is executed (OP_Program), a structure of this type
 13782 ** is allocated to store the current value of the program counter, as
 13783 ** well as the current memory cell array and various other frame specific
 13784 ** values stored in the Vdbe struct. When the sub-program is finished, 
 13785 ** these values are copied back to the Vdbe from the VdbeFrame structure,
 13786 ** restoring the state of the VM to as it was before the sub-program
 13787 ** began executing.
 13788 **
 13789 ** The memory for a VdbeFrame object is allocated and managed by a memory
 13790 ** cell in the parent (calling) frame. When the memory cell is deleted or
 13791 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
 13792 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
 13793 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
 13794 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
 13795 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
 13796 ** child frame are released.
 13797 **
 13798 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
 13799 ** set to NULL if the currently executing frame is the main program.
 13800 */
 13801 typedef struct VdbeFrame VdbeFrame;
 13802 struct VdbeFrame {
 13803   Vdbe *v;                /* VM this frame belongs to */
 13804   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
 13805   Op *aOp;                /* Program instructions for parent frame */
 13806   Mem *aMem;              /* Array of memory cells for parent frame */
 13807   u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
 13808   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
 13809   void *token;            /* Copy of SubProgram.token */
 13810   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
 13811   int nCursor;            /* Number of entries in apCsr */
 13812   int pc;                 /* Program Counter in parent (calling) frame */
 13813   int nOp;                /* Size of aOp array */
 13814   int nMem;               /* Number of entries in aMem */
 13815   int nOnceFlag;          /* Number of entries in aOnceFlag */
 13816   int nChildMem;          /* Number of memory cells for child frame */
 13817   int nChildCsr;          /* Number of cursors for child frame */
 13818   int nChange;            /* Statement changes (Vdbe.nChanges)     */
 13819 };
 13821 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
 13823 /*
 13824 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
 13825 */
 13826 #define CACHE_STALE 0
 13828 /*
 13829 ** Internally, the vdbe manipulates nearly all SQL values as Mem
 13830 ** structures. Each Mem struct may cache multiple representations (string,
 13831 ** integer etc.) of the same value.
 13832 */
 13833 struct Mem {
 13834   sqlite3 *db;        /* The associated database connection */
 13835   char *z;            /* String or BLOB value */
 13836   double r;           /* Real value */
 13837   union {
 13838     i64 i;              /* Integer value used when MEM_Int is set in flags */
 13839     int nZero;          /* Used when bit MEM_Zero is set in flags */
 13840     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
 13841     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
 13842     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
 13843   } u;
 13844   int n;              /* Number of characters in string value, excluding '\0' */
 13845   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
 13846   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
 13847 #ifdef SQLITE_DEBUG
 13848   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
 13849   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
 13850 #endif
 13851   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
 13852   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
 13853 };
 13855 /* One or more of the following flags are set to indicate the validOK
 13856 ** representations of the value stored in the Mem struct.
 13857 **
 13858 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 13859 ** No other flags may be set in this case.
 13860 **
 13861 ** If the MEM_Str flag is set then Mem.z points at a string representation.
 13862 ** Usually this is encoded in the same unicode encoding as the main
 13863 ** database (see below for exceptions). If the MEM_Term flag is also
 13864 ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 13865 ** flags may coexist with the MEM_Str flag.
 13866 */
 13867 #define MEM_Null      0x0001   /* Value is NULL */
 13868 #define MEM_Str       0x0002   /* Value is a string */
 13869 #define MEM_Int       0x0004   /* Value is an integer */
 13870 #define MEM_Real      0x0008   /* Value is a real number */
 13871 #define MEM_Blob      0x0010   /* Value is a BLOB */
 13872 #define MEM_AffMask   0x001f   /* Mask of affinity bits */
 13873 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
 13874 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
 13875 #define MEM_Undefined 0x0080   /* Value is undefined */
 13876 #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
 13877 #define MEM_TypeMask  0x01ff   /* Mask of type bits */
 13880 /* Whenever Mem contains a valid string or blob representation, one of
 13881 ** the following flags must be set to determine the memory management
 13882 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
 13883 ** string is \000 or \u0000 terminated
 13884 */
 13885 #define MEM_Term      0x0200   /* String rep is nul terminated */
 13886 #define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
 13887 #define MEM_Static    0x0800   /* Mem.z points to a static string */
 13888 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
 13889 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
 13890 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
 13891 #ifdef SQLITE_OMIT_INCRBLOB
 13892   #undef MEM_Zero
 13893   #define MEM_Zero 0x0000
 13894 #endif
 13896 /*
 13897 ** Clear any existing type flags from a Mem and replace them with f
 13898 */
 13899 #define MemSetTypeFlag(p, f) \
 13900    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
 13902 /*
 13903 ** Return true if a memory cell is not marked as invalid.  This macro
 13904 ** is for use inside assert() statements only.
 13905 */
 13906 #ifdef SQLITE_DEBUG
 13907 #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
 13908 #endif
 13910 /*
 13911 ** Each auxilliary data pointer stored by a user defined function 
 13912 ** implementation calling sqlite3_set_auxdata() is stored in an instance
 13913 ** of this structure. All such structures associated with a single VM
 13914 ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
 13915 ** when the VM is halted (if not before).
 13916 */
 13917 struct AuxData {
 13918   int iOp;                        /* Instruction number of OP_Function opcode */
 13919   int iArg;                       /* Index of function argument. */
 13920   void *pAux;                     /* Aux data pointer */
 13921   void (*xDelete)(void *);        /* Destructor for the aux data */
 13922   AuxData *pNext;                 /* Next element in list */
 13923 };
 13925 /*
 13926 ** The "context" argument for a installable function.  A pointer to an
 13927 ** instance of this structure is the first argument to the routines used
 13928 ** implement the SQL functions.
 13929 **
 13930 ** There is a typedef for this structure in sqlite.h.  So all routines,
 13931 ** even the public interface to SQLite, can use a pointer to this structure.
 13932 ** But this file is the only place where the internal details of this
 13933 ** structure are known.
 13934 **
 13935 ** This structure is defined inside of vdbeInt.h because it uses substructures
 13936 ** (Mem) which are only defined there.
 13937 */
 13938 struct sqlite3_context {
 13939   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
 13940   Mem s;                /* The return value is stored here */
 13941   Mem *pMem;            /* Memory cell used to store aggregate context */
 13942   CollSeq *pColl;       /* Collating sequence */
 13943   Vdbe *pVdbe;          /* The VM that owns this context */
 13944   int iOp;              /* Instruction number of OP_Function */
 13945   int isError;          /* Error code returned by the function. */
 13946   u8 skipFlag;          /* Skip skip accumulator loading if true */
 13947   u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
 13948 };
 13950 /*
 13951 ** An Explain object accumulates indented output which is helpful
 13952 ** in describing recursive data structures.
 13953 */
 13954 struct Explain {
 13955   Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
 13956   StrAccum str;      /* The string being accumulated */
 13957   int nIndent;       /* Number of elements in aIndent */
 13958   u16 aIndent[100];  /* Levels of indentation */
 13959   char zBase[100];   /* Initial space */
 13960 };
 13962 /* A bitfield type for use inside of structures.  Always follow with :N where
 13963 ** N is the number of bits.
 13964 */
 13965 typedef unsigned bft;  /* Bit Field Type */
 13967 /*
 13968 ** An instance of the virtual machine.  This structure contains the complete
 13969 ** state of the virtual machine.
 13970 **
 13971 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
 13972 ** is really a pointer to an instance of this structure.
 13973 **
 13974 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
 13975 ** any virtual table method invocations made by the vdbe program. It is
 13976 ** set to 2 for xDestroy method calls and 1 for all other methods. This
 13977 ** variable is used for two purposes: to allow xDestroy methods to execute
 13978 ** "DROP TABLE" statements and to prevent some nasty side effects of
 13979 ** malloc failure when SQLite is invoked recursively by a virtual table 
 13980 ** method function.
 13981 */
 13982 struct Vdbe {
 13983   sqlite3 *db;            /* The database connection that owns this statement */
 13984   Op *aOp;                /* Space to hold the virtual machine's program */
 13985   Mem *aMem;              /* The memory locations */
 13986   Mem **apArg;            /* Arguments to currently executing user function */
 13987   Mem *aColName;          /* Column names to return */
 13988   Mem *pResultSet;        /* Pointer to an array of results */
 13989   Parse *pParse;          /* Parsing context used to create this Vdbe */
 13990   int nMem;               /* Number of memory locations currently allocated */
 13991   int nOp;                /* Number of instructions in the program */
 13992   int nCursor;            /* Number of slots in apCsr[] */
 13993   u32 magic;              /* Magic number for sanity checking */
 13994   char *zErrMsg;          /* Error message written here */
 13995   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
 13996   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
 13997   Mem *aVar;              /* Values for the OP_Variable opcode. */
 13998   char **azVar;           /* Name of variables */
 13999   ynVar nVar;             /* Number of entries in aVar[] */
 14000   ynVar nzVar;            /* Number of entries in azVar[] */
 14001   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
 14002   int pc;                 /* The program counter */
 14003   int rc;                 /* Value to return */
 14004   u16 nResColumn;         /* Number of columns in one row of the result set */
 14005   u8 errorAction;         /* Recovery action to do in case of an error */
 14006   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 14007   bft explain:2;          /* True if EXPLAIN present on SQL command */
 14008   bft inVtabMethod:2;     /* See comments above */
 14009   bft changeCntOn:1;      /* True to update the change-counter */
 14010   bft expired:1;          /* True if the VM needs to be recompiled */
 14011   bft runOnlyOnce:1;      /* Automatically expire on reset */
 14012   bft usesStmtJournal:1;  /* True if uses a statement journal */
 14013   bft readOnly:1;         /* True for statements that do not write */
 14014   bft bIsReader:1;        /* True for statements that read */
 14015   bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
 14016   bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
 14017   int nChange;            /* Number of db changes made since last reset */
 14018   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
 14019   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 14020   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 14021   u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
 14022 #ifndef SQLITE_OMIT_TRACE
 14023   i64 startTime;          /* Time when query started - used for profiling */
 14024 #endif
 14025   i64 iCurrentTime;       /* Value of julianday('now') for this statement */
 14026   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
 14027   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 14028   i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
 14029   char *zSql;             /* Text of the SQL statement that generated this */
 14030   void *pFree;            /* Free this when deleting the vdbe */
 14031 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
 14032   Explain *pExplain;      /* The explainer */
 14033   char *zExplain;         /* Explanation of data structures */
 14034 #endif
 14035   VdbeFrame *pFrame;      /* Parent frame */
 14036   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
 14037   int nFrame;             /* Number of frames in pFrame list */
 14038   u32 expmask;            /* Binding to these vars invalidates VM */
 14039   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
 14040   int nOnceFlag;          /* Size of array aOnceFlag[] */
 14041   u8 *aOnceFlag;          /* Flags for OP_Once */
 14042   AuxData *pAuxData;      /* Linked list of auxdata allocations */
 14043 };
 14045 /*
 14046 ** The following are allowed values for Vdbe.magic
 14047 */
 14048 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
 14049 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
 14050 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
 14051 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
 14053 /*
 14054 ** Function prototypes
 14055 */
 14056 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
 14057 void sqliteVdbePopStack(Vdbe*,int);
 14058 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
 14059 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 14060 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
 14061 #endif
 14062 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
 14063 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
 14064 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
 14065 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 14066 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
 14068 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
 14069 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
 14070 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
 14071 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 14072 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
 14073 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
 14074 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
 14075 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
 14076 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
 14077 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
 14078 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
 14079 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
 14080 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
 14081 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
 14082 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
 14083 #ifdef SQLITE_OMIT_FLOATING_POINT
 14084 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
 14085 #else
 14086 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
 14087 #endif
 14088 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
 14089 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 14090 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 14091 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 14092 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
 14093 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 14094 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 14095 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
 14096 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 14097 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 14098 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 14099 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
 14100 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 14101 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
 14102 #define VdbeMemDynamic(X)  \
 14103   (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
 14104 #define VdbeMemRelease(X)  \
 14105   if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
 14106 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 14107 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 14108 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 14109 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 14110 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 14111 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 14112 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
 14114 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
 14115 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
 14116 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
 14117 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
 14118 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
 14119 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
 14120 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
 14122 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 14123 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
 14124 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
 14125 #else
 14126 # define sqlite3VdbeEnter(X)
 14127 # define sqlite3VdbeLeave(X)
 14128 #endif
 14130 #ifdef SQLITE_DEBUG
 14131 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
 14132 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
 14133 #endif
 14135 #ifndef SQLITE_OMIT_FOREIGN_KEY
 14136 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
 14137 #else
 14138 # define sqlite3VdbeCheckFk(p,i) 0
 14139 #endif
 14141 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
 14142 #ifdef SQLITE_DEBUG
 14143 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
 14144 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
 14145 #endif
 14146 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
 14148 #ifndef SQLITE_OMIT_INCRBLOB
 14149 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
 14150   #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
 14151 #else
 14152   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
 14153   #define ExpandBlob(P) SQLITE_OK
 14154 #endif
 14156 #endif /* !defined(_VDBEINT_H_) */
 14158 /************** End of vdbeInt.h *********************************************/
 14159 /************** Continuing where we left off in status.c *********************/
 14161 /*
 14162 ** Variables in which to record status information.
 14163 */
 14164 typedef struct sqlite3StatType sqlite3StatType;
 14165 static SQLITE_WSD struct sqlite3StatType {
 14166   int nowValue[10];         /* Current value */
 14167   int mxValue[10];          /* Maximum value */
 14168 } sqlite3Stat = { {0,}, {0,} };
 14171 /* The "wsdStat" macro will resolve to the status information
 14172 ** state vector.  If writable static data is unsupported on the target,
 14173 ** we have to locate the state vector at run-time.  In the more common
 14174 ** case where writable static data is supported, wsdStat can refer directly
 14175 ** to the "sqlite3Stat" state vector declared above.
 14176 */
 14177 #ifdef SQLITE_OMIT_WSD
 14178 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
 14179 # define wsdStat x[0]
 14180 #else
 14181 # define wsdStatInit
 14182 # define wsdStat sqlite3Stat
 14183 #endif
 14185 /*
 14186 ** Return the current value of a status parameter.
 14187 */
 14188 SQLITE_PRIVATE int sqlite3StatusValue(int op){
 14189   wsdStatInit;
 14190   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 14191   return wsdStat.nowValue[op];
 14194 /*
 14195 ** Add N to the value of a status record.  It is assumed that the
 14196 ** caller holds appropriate locks.
 14197 */
 14198 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
 14199   wsdStatInit;
 14200   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 14201   wsdStat.nowValue[op] += N;
 14202   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 14203     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 14207 /*
 14208 ** Set the value of a status to X.
 14209 */
 14210 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
 14211   wsdStatInit;
 14212   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
 14213   wsdStat.nowValue[op] = X;
 14214   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
 14215     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 14219 /*
 14220 ** Query status information.
 14221 **
 14222 ** This implementation assumes that reading or writing an aligned
 14223 ** 32-bit integer is an atomic operation.  If that assumption is not true,
 14224 ** then this routine is not threadsafe.
 14225 */
 14226 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
 14227   wsdStatInit;
 14228   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
 14229     return SQLITE_MISUSE_BKPT;
 14231   *pCurrent = wsdStat.nowValue[op];
 14232   *pHighwater = wsdStat.mxValue[op];
 14233   if( resetFlag ){
 14234     wsdStat.mxValue[op] = wsdStat.nowValue[op];
 14236   return SQLITE_OK;
 14239 /*
 14240 ** Query status information for a single database connection
 14241 */
 14242 SQLITE_API int sqlite3_db_status(
 14243   sqlite3 *db,          /* The database connection whose status is desired */
 14244   int op,               /* Status verb */
 14245   int *pCurrent,        /* Write current value here */
 14246   int *pHighwater,      /* Write high-water mark here */
 14247   int resetFlag         /* Reset high-water mark if true */
 14248 ){
 14249   int rc = SQLITE_OK;   /* Return code */
 14250   sqlite3_mutex_enter(db->mutex);
 14251   switch( op ){
 14252     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
 14253       *pCurrent = db->lookaside.nOut;
 14254       *pHighwater = db->lookaside.mxOut;
 14255       if( resetFlag ){
 14256         db->lookaside.mxOut = db->lookaside.nOut;
 14258       break;
 14261     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
 14262     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
 14263     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
 14264       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
 14265       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
 14266       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
 14267       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
 14268       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
 14269       *pCurrent = 0;
 14270       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
 14271       if( resetFlag ){
 14272         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
 14274       break;
 14277     /* 
 14278     ** Return an approximation for the amount of memory currently used
 14279     ** by all pagers associated with the given database connection.  The
 14280     ** highwater mark is meaningless and is returned as zero.
 14281     */
 14282     case SQLITE_DBSTATUS_CACHE_USED: {
 14283       int totalUsed = 0;
 14284       int i;
 14285       sqlite3BtreeEnterAll(db);
 14286       for(i=0; i<db->nDb; i++){
 14287         Btree *pBt = db->aDb[i].pBt;
 14288         if( pBt ){
 14289           Pager *pPager = sqlite3BtreePager(pBt);
 14290           totalUsed += sqlite3PagerMemUsed(pPager);
 14293       sqlite3BtreeLeaveAll(db);
 14294       *pCurrent = totalUsed;
 14295       *pHighwater = 0;
 14296       break;
 14299     /*
 14300     ** *pCurrent gets an accurate estimate of the amount of memory used
 14301     ** to store the schema for all databases (main, temp, and any ATTACHed
 14302     ** databases.  *pHighwater is set to zero.
 14303     */
 14304     case SQLITE_DBSTATUS_SCHEMA_USED: {
 14305       int i;                      /* Used to iterate through schemas */
 14306       int nByte = 0;              /* Used to accumulate return value */
 14308       sqlite3BtreeEnterAll(db);
 14309       db->pnBytesFreed = &nByte;
 14310       for(i=0; i<db->nDb; i++){
 14311         Schema *pSchema = db->aDb[i].pSchema;
 14312         if( ALWAYS(pSchema!=0) ){
 14313           HashElem *p;
 14315           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
 14316               pSchema->tblHash.count 
 14317             + pSchema->trigHash.count
 14318             + pSchema->idxHash.count
 14319             + pSchema->fkeyHash.count
 14320           );
 14321           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
 14322           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
 14323           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
 14324           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
 14326           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
 14327             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
 14329           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 14330             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
 14334       db->pnBytesFreed = 0;
 14335       sqlite3BtreeLeaveAll(db);
 14337       *pHighwater = 0;
 14338       *pCurrent = nByte;
 14339       break;
 14342     /*
 14343     ** *pCurrent gets an accurate estimate of the amount of memory used
 14344     ** to store all prepared statements.
 14345     ** *pHighwater is set to zero.
 14346     */
 14347     case SQLITE_DBSTATUS_STMT_USED: {
 14348       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
 14349       int nByte = 0;              /* Used to accumulate return value */
 14351       db->pnBytesFreed = &nByte;
 14352       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
 14353         sqlite3VdbeClearObject(db, pVdbe);
 14354         sqlite3DbFree(db, pVdbe);
 14356       db->pnBytesFreed = 0;
 14358       *pHighwater = 0;
 14359       *pCurrent = nByte;
 14361       break;
 14364     /*
 14365     ** Set *pCurrent to the total cache hits or misses encountered by all
 14366     ** pagers the database handle is connected to. *pHighwater is always set 
 14367     ** to zero.
 14368     */
 14369     case SQLITE_DBSTATUS_CACHE_HIT:
 14370     case SQLITE_DBSTATUS_CACHE_MISS:
 14371     case SQLITE_DBSTATUS_CACHE_WRITE:{
 14372       int i;
 14373       int nRet = 0;
 14374       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 14375       assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
 14377       for(i=0; i<db->nDb; i++){
 14378         if( db->aDb[i].pBt ){
 14379           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
 14380           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
 14383       *pHighwater = 0;
 14384       *pCurrent = nRet;
 14385       break;
 14388     /* Set *pCurrent to non-zero if there are unresolved deferred foreign
 14389     ** key constraints.  Set *pCurrent to zero if all foreign key constraints
 14390     ** have been satisfied.  The *pHighwater is always set to zero.
 14391     */
 14392     case SQLITE_DBSTATUS_DEFERRED_FKS: {
 14393       *pHighwater = 0;
 14394       *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
 14395       break;
 14398     default: {
 14399       rc = SQLITE_ERROR;
 14402   sqlite3_mutex_leave(db->mutex);
 14403   return rc;
 14406 /************** End of status.c **********************************************/
 14407 /************** Begin file date.c ********************************************/
 14408 /*
 14409 ** 2003 October 31
 14410 **
 14411 ** The author disclaims copyright to this source code.  In place of
 14412 ** a legal notice, here is a blessing:
 14413 **
 14414 **    May you do good and not evil.
 14415 **    May you find forgiveness for yourself and forgive others.
 14416 **    May you share freely, never taking more than you give.
 14417 **
 14418 *************************************************************************
 14419 ** This file contains the C functions that implement date and time
 14420 ** functions for SQLite.  
 14421 **
 14422 ** There is only one exported symbol in this file - the function
 14423 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
 14424 ** All other code has file scope.
 14425 **
 14426 ** SQLite processes all times and dates as Julian Day numbers.  The
 14427 ** dates and times are stored as the number of days since noon
 14428 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
 14429 ** calendar system. 
 14430 **
 14431 ** 1970-01-01 00:00:00 is JD 2440587.5
 14432 ** 2000-01-01 00:00:00 is JD 2451544.5
 14433 **
 14434 ** This implemention requires years to be expressed as a 4-digit number
 14435 ** which means that only dates between 0000-01-01 and 9999-12-31 can
 14436 ** be represented, even though julian day numbers allow a much wider
 14437 ** range of dates.
 14438 **
 14439 ** The Gregorian calendar system is used for all dates and times,
 14440 ** even those that predate the Gregorian calendar.  Historians usually
 14441 ** use the Julian calendar for dates prior to 1582-10-15 and for some
 14442 ** dates afterwards, depending on locale.  Beware of this difference.
 14443 **
 14444 ** The conversion algorithms are implemented based on descriptions
 14445 ** in the following text:
 14446 **
 14447 **      Jean Meeus
 14448 **      Astronomical Algorithms, 2nd Edition, 1998
 14449 **      ISBM 0-943396-61-1
 14450 **      Willmann-Bell, Inc
 14451 **      Richmond, Virginia (USA)
 14452 */
 14453 /* #include <stdlib.h> */
 14454 /* #include <assert.h> */
 14455 #include <time.h>
 14457 #ifndef SQLITE_OMIT_DATETIME_FUNCS
 14460 /*
 14461 ** A structure for holding a single date and time.
 14462 */
 14463 typedef struct DateTime DateTime;
 14464 struct DateTime {
 14465   sqlite3_int64 iJD; /* The julian day number times 86400000 */
 14466   int Y, M, D;       /* Year, month, and day */
 14467   int h, m;          /* Hour and minutes */
 14468   int tz;            /* Timezone offset in minutes */
 14469   double s;          /* Seconds */
 14470   char validYMD;     /* True (1) if Y,M,D are valid */
 14471   char validHMS;     /* True (1) if h,m,s are valid */
 14472   char validJD;      /* True (1) if iJD is valid */
 14473   char validTZ;      /* True (1) if tz is valid */
 14474 };
 14477 /*
 14478 ** Convert zDate into one or more integers.  Additional arguments
 14479 ** come in groups of 5 as follows:
 14480 **
 14481 **       N       number of digits in the integer
 14482 **       min     minimum allowed value of the integer
 14483 **       max     maximum allowed value of the integer
 14484 **       nextC   first character after the integer
 14485 **       pVal    where to write the integers value.
 14486 **
 14487 ** Conversions continue until one with nextC==0 is encountered.
 14488 ** The function returns the number of successful conversions.
 14489 */
 14490 static int getDigits(const char *zDate, ...){
 14491   va_list ap;
 14492   int val;
 14493   int N;
 14494   int min;
 14495   int max;
 14496   int nextC;
 14497   int *pVal;
 14498   int cnt = 0;
 14499   va_start(ap, zDate);
 14500   do{
 14501     N = va_arg(ap, int);
 14502     min = va_arg(ap, int);
 14503     max = va_arg(ap, int);
 14504     nextC = va_arg(ap, int);
 14505     pVal = va_arg(ap, int*);
 14506     val = 0;
 14507     while( N-- ){
 14508       if( !sqlite3Isdigit(*zDate) ){
 14509         goto end_getDigits;
 14511       val = val*10 + *zDate - '0';
 14512       zDate++;
 14514     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
 14515       goto end_getDigits;
 14517     *pVal = val;
 14518     zDate++;
 14519     cnt++;
 14520   }while( nextC );
 14521 end_getDigits:
 14522   va_end(ap);
 14523   return cnt;
 14526 /*
 14527 ** Parse a timezone extension on the end of a date-time.
 14528 ** The extension is of the form:
 14529 **
 14530 **        (+/-)HH:MM
 14531 **
 14532 ** Or the "zulu" notation:
 14533 **
 14534 **        Z
 14535 **
 14536 ** If the parse is successful, write the number of minutes
 14537 ** of change in p->tz and return 0.  If a parser error occurs,
 14538 ** return non-zero.
 14539 **
 14540 ** A missing specifier is not considered an error.
 14541 */
 14542 static int parseTimezone(const char *zDate, DateTime *p){
 14543   int sgn = 0;
 14544   int nHr, nMn;
 14545   int c;
 14546   while( sqlite3Isspace(*zDate) ){ zDate++; }
 14547   p->tz = 0;
 14548   c = *zDate;
 14549   if( c=='-' ){
 14550     sgn = -1;
 14551   }else if( c=='+' ){
 14552     sgn = +1;
 14553   }else if( c=='Z' || c=='z' ){
 14554     zDate++;
 14555     goto zulu_time;
 14556   }else{
 14557     return c!=0;
 14559   zDate++;
 14560   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
 14561     return 1;
 14563   zDate += 5;
 14564   p->tz = sgn*(nMn + nHr*60);
 14565 zulu_time:
 14566   while( sqlite3Isspace(*zDate) ){ zDate++; }
 14567   return *zDate!=0;
 14570 /*
 14571 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
 14572 ** The HH, MM, and SS must each be exactly 2 digits.  The
 14573 ** fractional seconds FFFF can be one or more digits.
 14574 **
 14575 ** Return 1 if there is a parsing error and 0 on success.
 14576 */
 14577 static int parseHhMmSs(const char *zDate, DateTime *p){
 14578   int h, m, s;
 14579   double ms = 0.0;
 14580   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
 14581     return 1;
 14583   zDate += 5;
 14584   if( *zDate==':' ){
 14585     zDate++;
 14586     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
 14587       return 1;
 14589     zDate += 2;
 14590     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
 14591       double rScale = 1.0;
 14592       zDate++;
 14593       while( sqlite3Isdigit(*zDate) ){
 14594         ms = ms*10.0 + *zDate - '0';
 14595         rScale *= 10.0;
 14596         zDate++;
 14598       ms /= rScale;
 14600   }else{
 14601     s = 0;
 14603   p->validJD = 0;
 14604   p->validHMS = 1;
 14605   p->h = h;
 14606   p->m = m;
 14607   p->s = s + ms;
 14608   if( parseTimezone(zDate, p) ) return 1;
 14609   p->validTZ = (p->tz!=0)?1:0;
 14610   return 0;
 14613 /*
 14614 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
 14615 ** that the YYYY-MM-DD is according to the Gregorian calendar.
 14616 **
 14617 ** Reference:  Meeus page 61
 14618 */
 14619 static void computeJD(DateTime *p){
 14620   int Y, M, D, A, B, X1, X2;
 14622   if( p->validJD ) return;
 14623   if( p->validYMD ){
 14624     Y = p->Y;
 14625     M = p->M;
 14626     D = p->D;
 14627   }else{
 14628     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
 14629     M = 1;
 14630     D = 1;
 14632   if( M<=2 ){
 14633     Y--;
 14634     M += 12;
 14636   A = Y/100;
 14637   B = 2 - A + (A/4);
 14638   X1 = 36525*(Y+4716)/100;
 14639   X2 = 306001*(M+1)/10000;
 14640   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
 14641   p->validJD = 1;
 14642   if( p->validHMS ){
 14643     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
 14644     if( p->validTZ ){
 14645       p->iJD -= p->tz*60000;
 14646       p->validYMD = 0;
 14647       p->validHMS = 0;
 14648       p->validTZ = 0;
 14653 /*
 14654 ** Parse dates of the form
 14655 **
 14656 **     YYYY-MM-DD HH:MM:SS.FFF
 14657 **     YYYY-MM-DD HH:MM:SS
 14658 **     YYYY-MM-DD HH:MM
 14659 **     YYYY-MM-DD
 14660 **
 14661 ** Write the result into the DateTime structure and return 0
 14662 ** on success and 1 if the input string is not a well-formed
 14663 ** date.
 14664 */
 14665 static int parseYyyyMmDd(const char *zDate, DateTime *p){
 14666   int Y, M, D, neg;
 14668   if( zDate[0]=='-' ){
 14669     zDate++;
 14670     neg = 1;
 14671   }else{
 14672     neg = 0;
 14674   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
 14675     return 1;
 14677   zDate += 10;
 14678   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
 14679   if( parseHhMmSs(zDate, p)==0 ){
 14680     /* We got the time */
 14681   }else if( *zDate==0 ){
 14682     p->validHMS = 0;
 14683   }else{
 14684     return 1;
 14686   p->validJD = 0;
 14687   p->validYMD = 1;
 14688   p->Y = neg ? -Y : Y;
 14689   p->M = M;
 14690   p->D = D;
 14691   if( p->validTZ ){
 14692     computeJD(p);
 14694   return 0;
 14697 /*
 14698 ** Set the time to the current time reported by the VFS.
 14699 **
 14700 ** Return the number of errors.
 14701 */
 14702 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
 14703   p->iJD = sqlite3StmtCurrentTime(context);
 14704   if( p->iJD>0 ){
 14705     p->validJD = 1;
 14706     return 0;
 14707   }else{
 14708     return 1;
 14712 /*
 14713 ** Attempt to parse the given string into a Julian Day Number.  Return
 14714 ** the number of errors.
 14715 **
 14716 ** The following are acceptable forms for the input string:
 14717 **
 14718 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
 14719 **      DDDD.DD 
 14720 **      now
 14721 **
 14722 ** In the first form, the +/-HH:MM is always optional.  The fractional
 14723 ** seconds extension (the ".FFF") is optional.  The seconds portion
 14724 ** (":SS.FFF") is option.  The year and date can be omitted as long
 14725 ** as there is a time string.  The time string can be omitted as long
 14726 ** as there is a year and date.
 14727 */
 14728 static int parseDateOrTime(
 14729   sqlite3_context *context, 
 14730   const char *zDate, 
 14731   DateTime *p
 14732 ){
 14733   double r;
 14734   if( parseYyyyMmDd(zDate,p)==0 ){
 14735     return 0;
 14736   }else if( parseHhMmSs(zDate, p)==0 ){
 14737     return 0;
 14738   }else if( sqlite3StrICmp(zDate,"now")==0){
 14739     return setDateTimeToCurrent(context, p);
 14740   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
 14741     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
 14742     p->validJD = 1;
 14743     return 0;
 14745   return 1;
 14748 /*
 14749 ** Compute the Year, Month, and Day from the julian day number.
 14750 */
 14751 static void computeYMD(DateTime *p){
 14752   int Z, A, B, C, D, E, X1;
 14753   if( p->validYMD ) return;
 14754   if( !p->validJD ){
 14755     p->Y = 2000;
 14756     p->M = 1;
 14757     p->D = 1;
 14758   }else{
 14759     Z = (int)((p->iJD + 43200000)/86400000);
 14760     A = (int)((Z - 1867216.25)/36524.25);
 14761     A = Z + 1 + A - (A/4);
 14762     B = A + 1524;
 14763     C = (int)((B - 122.1)/365.25);
 14764     D = (36525*C)/100;
 14765     E = (int)((B-D)/30.6001);
 14766     X1 = (int)(30.6001*E);
 14767     p->D = B - D - X1;
 14768     p->M = E<14 ? E-1 : E-13;
 14769     p->Y = p->M>2 ? C - 4716 : C - 4715;
 14771   p->validYMD = 1;
 14774 /*
 14775 ** Compute the Hour, Minute, and Seconds from the julian day number.
 14776 */
 14777 static void computeHMS(DateTime *p){
 14778   int s;
 14779   if( p->validHMS ) return;
 14780   computeJD(p);
 14781   s = (int)((p->iJD + 43200000) % 86400000);
 14782   p->s = s/1000.0;
 14783   s = (int)p->s;
 14784   p->s -= s;
 14785   p->h = s/3600;
 14786   s -= p->h*3600;
 14787   p->m = s/60;
 14788   p->s += s - p->m*60;
 14789   p->validHMS = 1;
 14792 /*
 14793 ** Compute both YMD and HMS
 14794 */
 14795 static void computeYMD_HMS(DateTime *p){
 14796   computeYMD(p);
 14797   computeHMS(p);
 14800 /*
 14801 ** Clear the YMD and HMS and the TZ
 14802 */
 14803 static void clearYMD_HMS_TZ(DateTime *p){
 14804   p->validYMD = 0;
 14805   p->validHMS = 0;
 14806   p->validTZ = 0;
 14809 /*
 14810 ** On recent Windows platforms, the localtime_s() function is available
 14811 ** as part of the "Secure CRT". It is essentially equivalent to 
 14812 ** localtime_r() available under most POSIX platforms, except that the 
 14813 ** order of the parameters is reversed.
 14814 **
 14815 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
 14816 **
 14817 ** If the user has not indicated to use localtime_r() or localtime_s()
 14818 ** already, check for an MSVC build environment that provides 
 14819 ** localtime_s().
 14820 */
 14821 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
 14822      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
 14823 #define HAVE_LOCALTIME_S 1
 14824 #endif
 14826 #ifndef SQLITE_OMIT_LOCALTIME
 14827 /*
 14828 ** The following routine implements the rough equivalent of localtime_r()
 14829 ** using whatever operating-system specific localtime facility that
 14830 ** is available.  This routine returns 0 on success and
 14831 ** non-zero on any kind of error.
 14832 **
 14833 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
 14834 ** routine will always fail.
 14835 **
 14836 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
 14837 ** library function localtime_r() is used to assist in the calculation of
 14838 ** local time.
 14839 */
 14840 static int osLocaltime(time_t *t, struct tm *pTm){
 14841   int rc;
 14842 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
 14843       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
 14844   struct tm *pX;
 14845 #if SQLITE_THREADSAFE>0
 14846   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 14847 #endif
 14848   sqlite3_mutex_enter(mutex);
 14849   pX = localtime(t);
 14850 #ifndef SQLITE_OMIT_BUILTIN_TEST
 14851   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
 14852 #endif
 14853   if( pX ) *pTm = *pX;
 14854   sqlite3_mutex_leave(mutex);
 14855   rc = pX==0;
 14856 #else
 14857 #ifndef SQLITE_OMIT_BUILTIN_TEST
 14858   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
 14859 #endif
 14860 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
 14861   rc = localtime_r(t, pTm)==0;
 14862 #else
 14863   rc = localtime_s(pTm, t);
 14864 #endif /* HAVE_LOCALTIME_R */
 14865 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
 14866   return rc;
 14868 #endif /* SQLITE_OMIT_LOCALTIME */
 14871 #ifndef SQLITE_OMIT_LOCALTIME
 14872 /*
 14873 ** Compute the difference (in milliseconds) between localtime and UTC
 14874 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
 14875 ** return this value and set *pRc to SQLITE_OK. 
 14876 **
 14877 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
 14878 ** is undefined in this case.
 14879 */
 14880 static sqlite3_int64 localtimeOffset(
 14881   DateTime *p,                    /* Date at which to calculate offset */
 14882   sqlite3_context *pCtx,          /* Write error here if one occurs */
 14883   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
 14884 ){
 14885   DateTime x, y;
 14886   time_t t;
 14887   struct tm sLocal;
 14889   /* Initialize the contents of sLocal to avoid a compiler warning. */
 14890   memset(&sLocal, 0, sizeof(sLocal));
 14892   x = *p;
 14893   computeYMD_HMS(&x);
 14894   if( x.Y<1971 || x.Y>=2038 ){
 14895     /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
 14896     ** works for years between 1970 and 2037. For dates outside this range,
 14897     ** SQLite attempts to map the year into an equivalent year within this
 14898     ** range, do the calculation, then map the year back.
 14899     */
 14900     x.Y = 2000;
 14901     x.M = 1;
 14902     x.D = 1;
 14903     x.h = 0;
 14904     x.m = 0;
 14905     x.s = 0.0;
 14906   } else {
 14907     int s = (int)(x.s + 0.5);
 14908     x.s = s;
 14910   x.tz = 0;
 14911   x.validJD = 0;
 14912   computeJD(&x);
 14913   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
 14914   if( osLocaltime(&t, &sLocal) ){
 14915     sqlite3_result_error(pCtx, "local time unavailable", -1);
 14916     *pRc = SQLITE_ERROR;
 14917     return 0;
 14919   y.Y = sLocal.tm_year + 1900;
 14920   y.M = sLocal.tm_mon + 1;
 14921   y.D = sLocal.tm_mday;
 14922   y.h = sLocal.tm_hour;
 14923   y.m = sLocal.tm_min;
 14924   y.s = sLocal.tm_sec;
 14925   y.validYMD = 1;
 14926   y.validHMS = 1;
 14927   y.validJD = 0;
 14928   y.validTZ = 0;
 14929   computeJD(&y);
 14930   *pRc = SQLITE_OK;
 14931   return y.iJD - x.iJD;
 14933 #endif /* SQLITE_OMIT_LOCALTIME */
 14935 /*
 14936 ** Process a modifier to a date-time stamp.  The modifiers are
 14937 ** as follows:
 14938 **
 14939 **     NNN days
 14940 **     NNN hours
 14941 **     NNN minutes
 14942 **     NNN.NNNN seconds
 14943 **     NNN months
 14944 **     NNN years
 14945 **     start of month
 14946 **     start of year
 14947 **     start of week
 14948 **     start of day
 14949 **     weekday N
 14950 **     unixepoch
 14951 **     localtime
 14952 **     utc
 14953 **
 14954 ** Return 0 on success and 1 if there is any kind of error. If the error
 14955 ** is in a system call (i.e. localtime()), then an error message is written
 14956 ** to context pCtx. If the error is an unrecognized modifier, no error is
 14957 ** written to pCtx.
 14958 */
 14959 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
 14960   int rc = 1;
 14961   int n;
 14962   double r;
 14963   char *z, zBuf[30];
 14964   z = zBuf;
 14965   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
 14966     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
 14968   z[n] = 0;
 14969   switch( z[0] ){
 14970 #ifndef SQLITE_OMIT_LOCALTIME
 14971     case 'l': {
 14972       /*    localtime
 14973       **
 14974       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
 14975       ** show local time.
 14976       */
 14977       if( strcmp(z, "localtime")==0 ){
 14978         computeJD(p);
 14979         p->iJD += localtimeOffset(p, pCtx, &rc);
 14980         clearYMD_HMS_TZ(p);
 14982       break;
 14984 #endif
 14985     case 'u': {
 14986       /*
 14987       **    unixepoch
 14988       **
 14989       ** Treat the current value of p->iJD as the number of
 14990       ** seconds since 1970.  Convert to a real julian day number.
 14991       */
 14992       if( strcmp(z, "unixepoch")==0 && p->validJD ){
 14993         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
 14994         clearYMD_HMS_TZ(p);
 14995         rc = 0;
 14997 #ifndef SQLITE_OMIT_LOCALTIME
 14998       else if( strcmp(z, "utc")==0 ){
 14999         sqlite3_int64 c1;
 15000         computeJD(p);
 15001         c1 = localtimeOffset(p, pCtx, &rc);
 15002         if( rc==SQLITE_OK ){
 15003           p->iJD -= c1;
 15004           clearYMD_HMS_TZ(p);
 15005           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
 15008 #endif
 15009       break;
 15011     case 'w': {
 15012       /*
 15013       **    weekday N
 15014       **
 15015       ** Move the date to the same time on the next occurrence of
 15016       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
 15017       ** date is already on the appropriate weekday, this is a no-op.
 15018       */
 15019       if( strncmp(z, "weekday ", 8)==0
 15020                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
 15021                && (n=(int)r)==r && n>=0 && r<7 ){
 15022         sqlite3_int64 Z;
 15023         computeYMD_HMS(p);
 15024         p->validTZ = 0;
 15025         p->validJD = 0;
 15026         computeJD(p);
 15027         Z = ((p->iJD + 129600000)/86400000) % 7;
 15028         if( Z>n ) Z -= 7;
 15029         p->iJD += (n - Z)*86400000;
 15030         clearYMD_HMS_TZ(p);
 15031         rc = 0;
 15033       break;
 15035     case 's': {
 15036       /*
 15037       **    start of TTTTT
 15038       **
 15039       ** Move the date backwards to the beginning of the current day,
 15040       ** or month or year.
 15041       */
 15042       if( strncmp(z, "start of ", 9)!=0 ) break;
 15043       z += 9;
 15044       computeYMD(p);
 15045       p->validHMS = 1;
 15046       p->h = p->m = 0;
 15047       p->s = 0.0;
 15048       p->validTZ = 0;
 15049       p->validJD = 0;
 15050       if( strcmp(z,"month")==0 ){
 15051         p->D = 1;
 15052         rc = 0;
 15053       }else if( strcmp(z,"year")==0 ){
 15054         computeYMD(p);
 15055         p->M = 1;
 15056         p->D = 1;
 15057         rc = 0;
 15058       }else if( strcmp(z,"day")==0 ){
 15059         rc = 0;
 15061       break;
 15063     case '+':
 15064     case '-':
 15065     case '0':
 15066     case '1':
 15067     case '2':
 15068     case '3':
 15069     case '4':
 15070     case '5':
 15071     case '6':
 15072     case '7':
 15073     case '8':
 15074     case '9': {
 15075       double rRounder;
 15076       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
 15077       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
 15078         rc = 1;
 15079         break;
 15081       if( z[n]==':' ){
 15082         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
 15083         ** specified number of hours, minutes, seconds, and fractional seconds
 15084         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
 15085         ** omitted.
 15086         */
 15087         const char *z2 = z;
 15088         DateTime tx;
 15089         sqlite3_int64 day;
 15090         if( !sqlite3Isdigit(*z2) ) z2++;
 15091         memset(&tx, 0, sizeof(tx));
 15092         if( parseHhMmSs(z2, &tx) ) break;
 15093         computeJD(&tx);
 15094         tx.iJD -= 43200000;
 15095         day = tx.iJD/86400000;
 15096         tx.iJD -= day*86400000;
 15097         if( z[0]=='-' ) tx.iJD = -tx.iJD;
 15098         computeJD(p);
 15099         clearYMD_HMS_TZ(p);
 15100         p->iJD += tx.iJD;
 15101         rc = 0;
 15102         break;
 15104       z += n;
 15105       while( sqlite3Isspace(*z) ) z++;
 15106       n = sqlite3Strlen30(z);
 15107       if( n>10 || n<3 ) break;
 15108       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
 15109       computeJD(p);
 15110       rc = 0;
 15111       rRounder = r<0 ? -0.5 : +0.5;
 15112       if( n==3 && strcmp(z,"day")==0 ){
 15113         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
 15114       }else if( n==4 && strcmp(z,"hour")==0 ){
 15115         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
 15116       }else if( n==6 && strcmp(z,"minute")==0 ){
 15117         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
 15118       }else if( n==6 && strcmp(z,"second")==0 ){
 15119         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
 15120       }else if( n==5 && strcmp(z,"month")==0 ){
 15121         int x, y;
 15122         computeYMD_HMS(p);
 15123         p->M += (int)r;
 15124         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
 15125         p->Y += x;
 15126         p->M -= x*12;
 15127         p->validJD = 0;
 15128         computeJD(p);
 15129         y = (int)r;
 15130         if( y!=r ){
 15131           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
 15133       }else if( n==4 && strcmp(z,"year")==0 ){
 15134         int y = (int)r;
 15135         computeYMD_HMS(p);
 15136         p->Y += y;
 15137         p->validJD = 0;
 15138         computeJD(p);
 15139         if( y!=r ){
 15140           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
 15142       }else{
 15143         rc = 1;
 15145       clearYMD_HMS_TZ(p);
 15146       break;
 15148     default: {
 15149       break;
 15152   return rc;
 15155 /*
 15156 ** Process time function arguments.  argv[0] is a date-time stamp.
 15157 ** argv[1] and following are modifiers.  Parse them all and write
 15158 ** the resulting time into the DateTime structure p.  Return 0
 15159 ** on success and 1 if there are any errors.
 15160 **
 15161 ** If there are zero parameters (if even argv[0] is undefined)
 15162 ** then assume a default value of "now" for argv[0].
 15163 */
 15164 static int isDate(
 15165   sqlite3_context *context, 
 15166   int argc, 
 15167   sqlite3_value **argv, 
 15168   DateTime *p
 15169 ){
 15170   int i;
 15171   const unsigned char *z;
 15172   int eType;
 15173   memset(p, 0, sizeof(*p));
 15174   if( argc==0 ){
 15175     return setDateTimeToCurrent(context, p);
 15177   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
 15178                    || eType==SQLITE_INTEGER ){
 15179     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
 15180     p->validJD = 1;
 15181   }else{
 15182     z = sqlite3_value_text(argv[0]);
 15183     if( !z || parseDateOrTime(context, (char*)z, p) ){
 15184       return 1;
 15187   for(i=1; i<argc; i++){
 15188     z = sqlite3_value_text(argv[i]);
 15189     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
 15191   return 0;
 15195 /*
 15196 ** The following routines implement the various date and time functions
 15197 ** of SQLite.
 15198 */
 15200 /*
 15201 **    julianday( TIMESTRING, MOD, MOD, ...)
 15202 **
 15203 ** Return the julian day number of the date specified in the arguments
 15204 */
 15205 static void juliandayFunc(
 15206   sqlite3_context *context,
 15207   int argc,
 15208   sqlite3_value **argv
 15209 ){
 15210   DateTime x;
 15211   if( isDate(context, argc, argv, &x)==0 ){
 15212     computeJD(&x);
 15213     sqlite3_result_double(context, x.iJD/86400000.0);
 15217 /*
 15218 **    datetime( TIMESTRING, MOD, MOD, ...)
 15219 **
 15220 ** Return YYYY-MM-DD HH:MM:SS
 15221 */
 15222 static void datetimeFunc(
 15223   sqlite3_context *context,
 15224   int argc,
 15225   sqlite3_value **argv
 15226 ){
 15227   DateTime x;
 15228   if( isDate(context, argc, argv, &x)==0 ){
 15229     char zBuf[100];
 15230     computeYMD_HMS(&x);
 15231     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
 15232                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
 15233     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 15237 /*
 15238 **    time( TIMESTRING, MOD, MOD, ...)
 15239 **
 15240 ** Return HH:MM:SS
 15241 */
 15242 static void timeFunc(
 15243   sqlite3_context *context,
 15244   int argc,
 15245   sqlite3_value **argv
 15246 ){
 15247   DateTime x;
 15248   if( isDate(context, argc, argv, &x)==0 ){
 15249     char zBuf[100];
 15250     computeHMS(&x);
 15251     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
 15252     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 15256 /*
 15257 **    date( TIMESTRING, MOD, MOD, ...)
 15258 **
 15259 ** Return YYYY-MM-DD
 15260 */
 15261 static void dateFunc(
 15262   sqlite3_context *context,
 15263   int argc,
 15264   sqlite3_value **argv
 15265 ){
 15266   DateTime x;
 15267   if( isDate(context, argc, argv, &x)==0 ){
 15268     char zBuf[100];
 15269     computeYMD(&x);
 15270     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
 15271     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 15275 /*
 15276 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
 15277 **
 15278 ** Return a string described by FORMAT.  Conversions as follows:
 15279 **
 15280 **   %d  day of month
 15281 **   %f  ** fractional seconds  SS.SSS
 15282 **   %H  hour 00-24
 15283 **   %j  day of year 000-366
 15284 **   %J  ** Julian day number
 15285 **   %m  month 01-12
 15286 **   %M  minute 00-59
 15287 **   %s  seconds since 1970-01-01
 15288 **   %S  seconds 00-59
 15289 **   %w  day of week 0-6  sunday==0
 15290 **   %W  week of year 00-53
 15291 **   %Y  year 0000-9999
 15292 **   %%  %
 15293 */
 15294 static void strftimeFunc(
 15295   sqlite3_context *context,
 15296   int argc,
 15297   sqlite3_value **argv
 15298 ){
 15299   DateTime x;
 15300   u64 n;
 15301   size_t i,j;
 15302   char *z;
 15303   sqlite3 *db;
 15304   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
 15305   char zBuf[100];
 15306   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
 15307   db = sqlite3_context_db_handle(context);
 15308   for(i=0, n=1; zFmt[i]; i++, n++){
 15309     if( zFmt[i]=='%' ){
 15310       switch( zFmt[i+1] ){
 15311         case 'd':
 15312         case 'H':
 15313         case 'm':
 15314         case 'M':
 15315         case 'S':
 15316         case 'W':
 15317           n++;
 15318           /* fall thru */
 15319         case 'w':
 15320         case '%':
 15321           break;
 15322         case 'f':
 15323           n += 8;
 15324           break;
 15325         case 'j':
 15326           n += 3;
 15327           break;
 15328         case 'Y':
 15329           n += 8;
 15330           break;
 15331         case 's':
 15332         case 'J':
 15333           n += 50;
 15334           break;
 15335         default:
 15336           return;  /* ERROR.  return a NULL */
 15338       i++;
 15341   testcase( n==sizeof(zBuf)-1 );
 15342   testcase( n==sizeof(zBuf) );
 15343   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 15344   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
 15345   if( n<sizeof(zBuf) ){
 15346     z = zBuf;
 15347   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 15348     sqlite3_result_error_toobig(context);
 15349     return;
 15350   }else{
 15351     z = sqlite3DbMallocRaw(db, (int)n);
 15352     if( z==0 ){
 15353       sqlite3_result_error_nomem(context);
 15354       return;
 15357   computeJD(&x);
 15358   computeYMD_HMS(&x);
 15359   for(i=j=0; zFmt[i]; i++){
 15360     if( zFmt[i]!='%' ){
 15361       z[j++] = zFmt[i];
 15362     }else{
 15363       i++;
 15364       switch( zFmt[i] ){
 15365         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
 15366         case 'f': {
 15367           double s = x.s;
 15368           if( s>59.999 ) s = 59.999;
 15369           sqlite3_snprintf(7, &z[j],"%06.3f", s);
 15370           j += sqlite3Strlen30(&z[j]);
 15371           break;
 15373         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
 15374         case 'W': /* Fall thru */
 15375         case 'j': {
 15376           int nDay;             /* Number of days since 1st day of year */
 15377           DateTime y = x;
 15378           y.validJD = 0;
 15379           y.M = 1;
 15380           y.D = 1;
 15381           computeJD(&y);
 15382           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
 15383           if( zFmt[i]=='W' ){
 15384             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
 15385             wd = (int)(((x.iJD+43200000)/86400000)%7);
 15386             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
 15387             j += 2;
 15388           }else{
 15389             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
 15390             j += 3;
 15392           break;
 15394         case 'J': {
 15395           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
 15396           j+=sqlite3Strlen30(&z[j]);
 15397           break;
 15399         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
 15400         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
 15401         case 's': {
 15402           sqlite3_snprintf(30,&z[j],"%lld",
 15403                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
 15404           j += sqlite3Strlen30(&z[j]);
 15405           break;
 15407         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
 15408         case 'w': {
 15409           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
 15410           break;
 15412         case 'Y': {
 15413           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
 15414           break;
 15416         default:   z[j++] = '%'; break;
 15420   z[j] = 0;
 15421   sqlite3_result_text(context, z, -1,
 15422                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
 15425 /*
 15426 ** current_time()
 15427 **
 15428 ** This function returns the same value as time('now').
 15429 */
 15430 static void ctimeFunc(
 15431   sqlite3_context *context,
 15432   int NotUsed,
 15433   sqlite3_value **NotUsed2
 15434 ){
 15435   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 15436   timeFunc(context, 0, 0);
 15439 /*
 15440 ** current_date()
 15441 **
 15442 ** This function returns the same value as date('now').
 15443 */
 15444 static void cdateFunc(
 15445   sqlite3_context *context,
 15446   int NotUsed,
 15447   sqlite3_value **NotUsed2
 15448 ){
 15449   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 15450   dateFunc(context, 0, 0);
 15453 /*
 15454 ** current_timestamp()
 15455 **
 15456 ** This function returns the same value as datetime('now').
 15457 */
 15458 static void ctimestampFunc(
 15459   sqlite3_context *context,
 15460   int NotUsed,
 15461   sqlite3_value **NotUsed2
 15462 ){
 15463   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 15464   datetimeFunc(context, 0, 0);
 15466 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
 15468 #ifdef SQLITE_OMIT_DATETIME_FUNCS
 15469 /*
 15470 ** If the library is compiled to omit the full-scale date and time
 15471 ** handling (to get a smaller binary), the following minimal version
 15472 ** of the functions current_time(), current_date() and current_timestamp()
 15473 ** are included instead. This is to support column declarations that
 15474 ** include "DEFAULT CURRENT_TIME" etc.
 15475 **
 15476 ** This function uses the C-library functions time(), gmtime()
 15477 ** and strftime(). The format string to pass to strftime() is supplied
 15478 ** as the user-data for the function.
 15479 */
 15480 static void currentTimeFunc(
 15481   sqlite3_context *context,
 15482   int argc,
 15483   sqlite3_value **argv
 15484 ){
 15485   time_t t;
 15486   char *zFormat = (char *)sqlite3_user_data(context);
 15487   sqlite3 *db;
 15488   sqlite3_int64 iT;
 15489   struct tm *pTm;
 15490   struct tm sNow;
 15491   char zBuf[20];
 15493   UNUSED_PARAMETER(argc);
 15494   UNUSED_PARAMETER(argv);
 15496   iT = sqlite3StmtCurrentTime(context);
 15497   if( iT<=0 ) return;
 15498   t = iT/1000 - 10000*(sqlite3_int64)21086676;
 15499 #ifdef HAVE_GMTIME_R
 15500   pTm = gmtime_r(&t, &sNow);
 15501 #else
 15502   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 15503   pTm = gmtime(&t);
 15504   if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
 15505   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 15506 #endif
 15507   if( pTm ){
 15508     strftime(zBuf, 20, zFormat, &sNow);
 15509     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 15512 #endif
 15514 /*
 15515 ** This function registered all of the above C functions as SQL
 15516 ** functions.  This should be the only routine in this file with
 15517 ** external linkage.
 15518 */
 15519 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
 15520   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
 15521 #ifndef SQLITE_OMIT_DATETIME_FUNCS
 15522     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
 15523     FUNCTION(date,             -1, 0, 0, dateFunc      ),
 15524     FUNCTION(time,             -1, 0, 0, timeFunc      ),
 15525     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
 15526     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
 15527     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
 15528     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
 15529     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
 15530 #else
 15531     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
 15532     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
 15533     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
 15534 #endif
 15535   };
 15536   int i;
 15537   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 15538   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
 15540   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
 15541     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 15545 /************** End of date.c ************************************************/
 15546 /************** Begin file os.c **********************************************/
 15547 /*
 15548 ** 2005 November 29
 15549 **
 15550 ** The author disclaims copyright to this source code.  In place of
 15551 ** a legal notice, here is a blessing:
 15552 **
 15553 **    May you do good and not evil.
 15554 **    May you find forgiveness for yourself and forgive others.
 15555 **    May you share freely, never taking more than you give.
 15556 **
 15557 ******************************************************************************
 15558 **
 15559 ** This file contains OS interface code that is common to all
 15560 ** architectures.
 15561 */
 15562 #define _SQLITE_OS_C_ 1
 15563 #undef _SQLITE_OS_C_
 15565 /*
 15566 ** The default SQLite sqlite3_vfs implementations do not allocate
 15567 ** memory (actually, os_unix.c allocates a small amount of memory
 15568 ** from within OsOpen()), but some third-party implementations may.
 15569 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
 15570 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
 15571 **
 15572 ** The following functions are instrumented for malloc() failure 
 15573 ** testing:
 15574 **
 15575 **     sqlite3OsRead()
 15576 **     sqlite3OsWrite()
 15577 **     sqlite3OsSync()
 15578 **     sqlite3OsFileSize()
 15579 **     sqlite3OsLock()
 15580 **     sqlite3OsCheckReservedLock()
 15581 **     sqlite3OsFileControl()
 15582 **     sqlite3OsShmMap()
 15583 **     sqlite3OsOpen()
 15584 **     sqlite3OsDelete()
 15585 **     sqlite3OsAccess()
 15586 **     sqlite3OsFullPathname()
 15587 **
 15588 */
 15589 #if defined(SQLITE_TEST)
 15590 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
 15591   #define DO_OS_MALLOC_TEST(x)                                       \
 15592   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
 15593     void *pTstAlloc = sqlite3Malloc(10);                             \
 15594     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
 15595     sqlite3_free(pTstAlloc);                                         \
 15597 #else
 15598   #define DO_OS_MALLOC_TEST(x)
 15599 #endif
 15601 /*
 15602 ** The following routines are convenience wrappers around methods
 15603 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
 15604 ** of this would be completely automatic if SQLite were coded using
 15605 ** C++ instead of plain old C.
 15606 */
 15607 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
 15608   int rc = SQLITE_OK;
 15609   if( pId->pMethods ){
 15610     rc = pId->pMethods->xClose(pId);
 15611     pId->pMethods = 0;
 15613   return rc;
 15615 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
 15616   DO_OS_MALLOC_TEST(id);
 15617   return id->pMethods->xRead(id, pBuf, amt, offset);
 15619 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
 15620   DO_OS_MALLOC_TEST(id);
 15621   return id->pMethods->xWrite(id, pBuf, amt, offset);
 15623 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
 15624   return id->pMethods->xTruncate(id, size);
 15626 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
 15627   DO_OS_MALLOC_TEST(id);
 15628   return id->pMethods->xSync(id, flags);
 15630 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
 15631   DO_OS_MALLOC_TEST(id);
 15632   return id->pMethods->xFileSize(id, pSize);
 15634 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
 15635   DO_OS_MALLOC_TEST(id);
 15636   return id->pMethods->xLock(id, lockType);
 15638 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
 15639   return id->pMethods->xUnlock(id, lockType);
 15641 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
 15642   DO_OS_MALLOC_TEST(id);
 15643   return id->pMethods->xCheckReservedLock(id, pResOut);
 15646 /*
 15647 ** Use sqlite3OsFileControl() when we are doing something that might fail
 15648 ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
 15649 ** when simply tossing information over the wall to the VFS and we do not
 15650 ** really care if the VFS receives and understands the information since it
 15651 ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 15652 ** routine has no return value since the return value would be meaningless.
 15653 */
 15654 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 15655 #ifdef SQLITE_TEST
 15656   if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
 15657     /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
 15658     ** is using a regular VFS, it is called after the corresponding 
 15659     ** transaction has been committed. Injecting a fault at this point 
 15660     ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
 15661     ** but the transaction is committed anyway.
 15662     **
 15663     ** The core must call OsFileControl() though, not OsFileControlHint(),
 15664     ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
 15665     ** means the commit really has failed and an error should be returned
 15666     ** to the user.  */
 15667     DO_OS_MALLOC_TEST(id);
 15669 #endif
 15670   return id->pMethods->xFileControl(id, op, pArg);
 15672 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 15673   (void)id->pMethods->xFileControl(id, op, pArg);
 15676 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 15677   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 15678   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 15680 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
 15681   return id->pMethods->xDeviceCharacteristics(id);
 15683 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
 15684   return id->pMethods->xShmLock(id, offset, n, flags);
 15686 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
 15687   id->pMethods->xShmBarrier(id);
 15689 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
 15690   return id->pMethods->xShmUnmap(id, deleteFlag);
 15692 SQLITE_PRIVATE int sqlite3OsShmMap(
 15693   sqlite3_file *id,               /* Database file handle */
 15694   int iPage,
 15695   int pgsz,
 15696   int bExtend,                    /* True to extend file if necessary */
 15697   void volatile **pp              /* OUT: Pointer to mapping */
 15698 ){
 15699   DO_OS_MALLOC_TEST(id);
 15700   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
 15703 #if SQLITE_MAX_MMAP_SIZE>0
 15704 /* The real implementation of xFetch and xUnfetch */
 15705 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
 15706   DO_OS_MALLOC_TEST(id);
 15707   return id->pMethods->xFetch(id, iOff, iAmt, pp);
 15709 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
 15710   return id->pMethods->xUnfetch(id, iOff, p);
 15712 #else
 15713 /* No-op stubs to use when memory-mapped I/O is disabled */
 15714 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
 15715   *pp = 0;
 15716   return SQLITE_OK;
 15718 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
 15719   return SQLITE_OK;
 15721 #endif
 15723 /*
 15724 ** The next group of routines are convenience wrappers around the
 15725 ** VFS methods.
 15726 */
 15727 SQLITE_PRIVATE int sqlite3OsOpen(
 15728   sqlite3_vfs *pVfs, 
 15729   const char *zPath, 
 15730   sqlite3_file *pFile, 
 15731   int flags, 
 15732   int *pFlagsOut
 15733 ){
 15734   int rc;
 15735   DO_OS_MALLOC_TEST(0);
 15736   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
 15737   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
 15738   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
 15739   ** reaching the VFS. */
 15740   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
 15741   assert( rc==SQLITE_OK || pFile->pMethods==0 );
 15742   return rc;
 15744 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
 15745   DO_OS_MALLOC_TEST(0);
 15746   assert( dirSync==0 || dirSync==1 );
 15747   return pVfs->xDelete(pVfs, zPath, dirSync);
 15749 SQLITE_PRIVATE int sqlite3OsAccess(
 15750   sqlite3_vfs *pVfs, 
 15751   const char *zPath, 
 15752   int flags, 
 15753   int *pResOut
 15754 ){
 15755   DO_OS_MALLOC_TEST(0);
 15756   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
 15758 SQLITE_PRIVATE int sqlite3OsFullPathname(
 15759   sqlite3_vfs *pVfs, 
 15760   const char *zPath, 
 15761   int nPathOut, 
 15762   char *zPathOut
 15763 ){
 15764   DO_OS_MALLOC_TEST(0);
 15765   zPathOut[0] = 0;
 15766   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
 15768 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 15769 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
 15770   return pVfs->xDlOpen(pVfs, zPath);
 15772 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 15773   pVfs->xDlError(pVfs, nByte, zBufOut);
 15775 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
 15776   return pVfs->xDlSym(pVfs, pHdle, zSym);
 15778 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
 15779   pVfs->xDlClose(pVfs, pHandle);
 15781 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
 15782 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
 15783   return pVfs->xRandomness(pVfs, nByte, zBufOut);
 15785 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
 15786   return pVfs->xSleep(pVfs, nMicro);
 15788 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
 15789   int rc;
 15790   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
 15791   ** method to get the current date and time if that method is available
 15792   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
 15793   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
 15794   ** unavailable.
 15795   */
 15796   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
 15797     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
 15798   }else{
 15799     double r;
 15800     rc = pVfs->xCurrentTime(pVfs, &r);
 15801     *pTimeOut = (sqlite3_int64)(r*86400000.0);
 15803   return rc;
 15806 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
 15807   sqlite3_vfs *pVfs, 
 15808   const char *zFile, 
 15809   sqlite3_file **ppFile, 
 15810   int flags,
 15811   int *pOutFlags
 15812 ){
 15813   int rc = SQLITE_NOMEM;
 15814   sqlite3_file *pFile;
 15815   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
 15816   if( pFile ){
 15817     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
 15818     if( rc!=SQLITE_OK ){
 15819       sqlite3_free(pFile);
 15820     }else{
 15821       *ppFile = pFile;
 15824   return rc;
 15826 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
 15827   int rc = SQLITE_OK;
 15828   assert( pFile );
 15829   rc = sqlite3OsClose(pFile);
 15830   sqlite3_free(pFile);
 15831   return rc;
 15834 /*
 15835 ** This function is a wrapper around the OS specific implementation of
 15836 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
 15837 ** ability to simulate a malloc failure, so that the handling of an
 15838 ** error in sqlite3_os_init() by the upper layers can be tested.
 15839 */
 15840 SQLITE_PRIVATE int sqlite3OsInit(void){
 15841   void *p = sqlite3_malloc(10);
 15842   if( p==0 ) return SQLITE_NOMEM;
 15843   sqlite3_free(p);
 15844   return sqlite3_os_init();
 15847 /*
 15848 ** The list of all registered VFS implementations.
 15849 */
 15850 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
 15851 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
 15853 /*
 15854 ** Locate a VFS by name.  If no name is given, simply return the
 15855 ** first VFS on the list.
 15856 */
 15857 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
 15858   sqlite3_vfs *pVfs = 0;
 15859 #if SQLITE_THREADSAFE
 15860   sqlite3_mutex *mutex;
 15861 #endif
 15862 #ifndef SQLITE_OMIT_AUTOINIT
 15863   int rc = sqlite3_initialize();
 15864   if( rc ) return 0;
 15865 #endif
 15866 #if SQLITE_THREADSAFE
 15867   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 15868 #endif
 15869   sqlite3_mutex_enter(mutex);
 15870   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
 15871     if( zVfs==0 ) break;
 15872     if( strcmp(zVfs, pVfs->zName)==0 ) break;
 15874   sqlite3_mutex_leave(mutex);
 15875   return pVfs;
 15878 /*
 15879 ** Unlink a VFS from the linked list
 15880 */
 15881 static void vfsUnlink(sqlite3_vfs *pVfs){
 15882   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
 15883   if( pVfs==0 ){
 15884     /* No-op */
 15885   }else if( vfsList==pVfs ){
 15886     vfsList = pVfs->pNext;
 15887   }else if( vfsList ){
 15888     sqlite3_vfs *p = vfsList;
 15889     while( p->pNext && p->pNext!=pVfs ){
 15890       p = p->pNext;
 15892     if( p->pNext==pVfs ){
 15893       p->pNext = pVfs->pNext;
 15898 /*
 15899 ** Register a VFS with the system.  It is harmless to register the same
 15900 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
 15901 ** true.
 15902 */
 15903 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
 15904   MUTEX_LOGIC(sqlite3_mutex *mutex;)
 15905 #ifndef SQLITE_OMIT_AUTOINIT
 15906   int rc = sqlite3_initialize();
 15907   if( rc ) return rc;
 15908 #endif
 15909   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 15910   sqlite3_mutex_enter(mutex);
 15911   vfsUnlink(pVfs);
 15912   if( makeDflt || vfsList==0 ){
 15913     pVfs->pNext = vfsList;
 15914     vfsList = pVfs;
 15915   }else{
 15916     pVfs->pNext = vfsList->pNext;
 15917     vfsList->pNext = pVfs;
 15919   assert(vfsList);
 15920   sqlite3_mutex_leave(mutex);
 15921   return SQLITE_OK;
 15924 /*
 15925 ** Unregister a VFS so that it is no longer accessible.
 15926 */
 15927 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
 15928 #if SQLITE_THREADSAFE
 15929   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 15930 #endif
 15931   sqlite3_mutex_enter(mutex);
 15932   vfsUnlink(pVfs);
 15933   sqlite3_mutex_leave(mutex);
 15934   return SQLITE_OK;
 15937 /************** End of os.c **************************************************/
 15938 /************** Begin file fault.c *******************************************/
 15939 /*
 15940 ** 2008 Jan 22
 15941 **
 15942 ** The author disclaims copyright to this source code.  In place of
 15943 ** a legal notice, here is a blessing:
 15944 **
 15945 **    May you do good and not evil.
 15946 **    May you find forgiveness for yourself and forgive others.
 15947 **    May you share freely, never taking more than you give.
 15948 **
 15949 *************************************************************************
 15950 **
 15951 ** This file contains code to support the concept of "benign" 
 15952 ** malloc failures (when the xMalloc() or xRealloc() method of the
 15953 ** sqlite3_mem_methods structure fails to allocate a block of memory
 15954 ** and returns 0). 
 15955 **
 15956 ** Most malloc failures are non-benign. After they occur, SQLite
 15957 ** abandons the current operation and returns an error code (usually
 15958 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
 15959 ** fatal. For example, if a malloc fails while resizing a hash table, this 
 15960 ** is completely recoverable simply by not carrying out the resize. The 
 15961 ** hash table will continue to function normally.  So a malloc failure 
 15962 ** during a hash table resize is a benign fault.
 15963 */
 15966 #ifndef SQLITE_OMIT_BUILTIN_TEST
 15968 /*
 15969 ** Global variables.
 15970 */
 15971 typedef struct BenignMallocHooks BenignMallocHooks;
 15972 static SQLITE_WSD struct BenignMallocHooks {
 15973   void (*xBenignBegin)(void);
 15974   void (*xBenignEnd)(void);
 15975 } sqlite3Hooks = { 0, 0 };
 15977 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
 15978 ** structure.  If writable static data is unsupported on the target,
 15979 ** we have to locate the state vector at run-time.  In the more common
 15980 ** case where writable static data is supported, wsdHooks can refer directly
 15981 ** to the "sqlite3Hooks" state vector declared above.
 15982 */
 15983 #ifdef SQLITE_OMIT_WSD
 15984 # define wsdHooksInit \
 15985   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
 15986 # define wsdHooks x[0]
 15987 #else
 15988 # define wsdHooksInit
 15989 # define wsdHooks sqlite3Hooks
 15990 #endif
 15993 /*
 15994 ** Register hooks to call when sqlite3BeginBenignMalloc() and
 15995 ** sqlite3EndBenignMalloc() are called, respectively.
 15996 */
 15997 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
 15998   void (*xBenignBegin)(void),
 15999   void (*xBenignEnd)(void)
 16000 ){
 16001   wsdHooksInit;
 16002   wsdHooks.xBenignBegin = xBenignBegin;
 16003   wsdHooks.xBenignEnd = xBenignEnd;
 16006 /*
 16007 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
 16008 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
 16009 ** indicates that subsequent malloc failures are non-benign.
 16010 */
 16011 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
 16012   wsdHooksInit;
 16013   if( wsdHooks.xBenignBegin ){
 16014     wsdHooks.xBenignBegin();
 16017 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
 16018   wsdHooksInit;
 16019   if( wsdHooks.xBenignEnd ){
 16020     wsdHooks.xBenignEnd();
 16024 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
 16026 /************** End of fault.c ***********************************************/
 16027 /************** Begin file mem0.c ********************************************/
 16028 /*
 16029 ** 2008 October 28
 16030 **
 16031 ** The author disclaims copyright to this source code.  In place of
 16032 ** a legal notice, here is a blessing:
 16033 **
 16034 **    May you do good and not evil.
 16035 **    May you find forgiveness for yourself and forgive others.
 16036 **    May you share freely, never taking more than you give.
 16037 **
 16038 *************************************************************************
 16039 **
 16040 ** This file contains a no-op memory allocation drivers for use when
 16041 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
 16042 ** here always fail.  SQLite will not operate with these drivers.  These
 16043 ** are merely placeholders.  Real drivers must be substituted using
 16044 ** sqlite3_config() before SQLite will operate.
 16045 */
 16047 /*
 16048 ** This version of the memory allocator is the default.  It is
 16049 ** used when no other memory allocator is specified using compile-time
 16050 ** macros.
 16051 */
 16052 #ifdef SQLITE_ZERO_MALLOC
 16054 /*
 16055 ** No-op versions of all memory allocation routines
 16056 */
 16057 static void *sqlite3MemMalloc(int nByte){ return 0; }
 16058 static void sqlite3MemFree(void *pPrior){ return; }
 16059 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
 16060 static int sqlite3MemSize(void *pPrior){ return 0; }
 16061 static int sqlite3MemRoundup(int n){ return n; }
 16062 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
 16063 static void sqlite3MemShutdown(void *NotUsed){ return; }
 16065 /*
 16066 ** This routine is the only routine in this file with external linkage.
 16067 **
 16068 ** Populate the low-level memory allocation function pointers in
 16069 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 16070 */
 16071 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 16072   static const sqlite3_mem_methods defaultMethods = {
 16073      sqlite3MemMalloc,
 16074      sqlite3MemFree,
 16075      sqlite3MemRealloc,
 16076      sqlite3MemSize,
 16077      sqlite3MemRoundup,
 16078      sqlite3MemInit,
 16079      sqlite3MemShutdown,
 16081   };
 16082   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 16085 #endif /* SQLITE_ZERO_MALLOC */
 16087 /************** End of mem0.c ************************************************/
 16088 /************** Begin file mem1.c ********************************************/
 16089 /*
 16090 ** 2007 August 14
 16091 **
 16092 ** The author disclaims copyright to this source code.  In place of
 16093 ** a legal notice, here is a blessing:
 16094 **
 16095 **    May you do good and not evil.
 16096 **    May you find forgiveness for yourself and forgive others.
 16097 **    May you share freely, never taking more than you give.
 16098 **
 16099 *************************************************************************
 16100 **
 16101 ** This file contains low-level memory allocation drivers for when
 16102 ** SQLite will use the standard C-library malloc/realloc/free interface
 16103 ** to obtain the memory it needs.
 16104 **
 16105 ** This file contains implementations of the low-level memory allocation
 16106 ** routines specified in the sqlite3_mem_methods object.  The content of
 16107 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
 16108 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
 16109 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
 16110 ** default configuration is to use memory allocation routines in this
 16111 ** file.
 16112 **
 16113 ** C-preprocessor macro summary:
 16114 **
 16115 **    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
 16116 **                                the malloc_usable_size() interface exists
 16117 **                                on the target platform.  Or, this symbol
 16118 **                                can be set manually, if desired.
 16119 **                                If an equivalent interface exists by
 16120 **                                a different name, using a separate -D
 16121 **                                option to rename it.
 16122 **
 16123 **    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
 16124 **                                memory allocator.  Set this symbol to enable
 16125 **                                building on older macs.
 16126 **
 16127 **    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
 16128 **                                _msize() on windows systems.  This might
 16129 **                                be necessary when compiling for Delphi,
 16130 **                                for example.
 16131 */
 16133 /*
 16134 ** This version of the memory allocator is the default.  It is
 16135 ** used when no other memory allocator is specified using compile-time
 16136 ** macros.
 16137 */
 16138 #ifdef SQLITE_SYSTEM_MALLOC
 16139 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 16141 /*
 16142 ** Use the zone allocator available on apple products unless the
 16143 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
 16144 */
 16145 #include <sys/sysctl.h>
 16146 #include <malloc/malloc.h>
 16147 #include <libkern/OSAtomic.h>
 16148 static malloc_zone_t* _sqliteZone_;
 16149 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
 16150 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
 16151 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
 16152 #define SQLITE_MALLOCSIZE(x) \
 16153         (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
 16155 #else /* if not __APPLE__ */
 16157 /*
 16158 ** Use standard C library malloc and free on non-Apple systems.  
 16159 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
 16160 */
 16161 #define SQLITE_MALLOC(x)             malloc(x)
 16162 #define SQLITE_FREE(x)               free(x)
 16163 #define SQLITE_REALLOC(x,y)          realloc((x),(y))
 16165 /*
 16166 ** The malloc.h header file is needed for malloc_usable_size() function
 16167 ** on some systems (e.g. Linux).
 16168 */
 16169 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE)
 16170 #  define SQLITE_USE_MALLOC_H
 16171 #  define SQLITE_USE_MALLOC_USABLE_SIZE
 16172 /*
 16173 ** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
 16174 ** use of _msize() is automatic, but can be disabled by compiling with
 16175 ** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
 16176 ** the malloc.h header file.
 16177 */
 16178 #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
 16179 #  define SQLITE_USE_MALLOC_H
 16180 #  define SQLITE_USE_MSIZE
 16181 #endif
 16183 /*
 16184 ** Include the malloc.h header file, if necessary.  Also set define macro
 16185 ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
 16186 ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
 16187 ** The memory size function can always be overridden manually by defining
 16188 ** the macro SQLITE_MALLOCSIZE to the desired function name.
 16189 */
 16190 #if defined(SQLITE_USE_MALLOC_H)
 16191 #  include <malloc.h>
 16192 #  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
 16193 #    if !defined(SQLITE_MALLOCSIZE)
 16194 #      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
 16195 #    endif
 16196 #  elif defined(SQLITE_USE_MSIZE)
 16197 #    if !defined(SQLITE_MALLOCSIZE)
 16198 #      define SQLITE_MALLOCSIZE      _msize
 16199 #    endif
 16200 #  endif
 16201 #endif /* defined(SQLITE_USE_MALLOC_H) */
 16203 #endif /* __APPLE__ or not __APPLE__ */
 16205 /*
 16206 ** Like malloc(), but remember the size of the allocation
 16207 ** so that we can find it later using sqlite3MemSize().
 16208 **
 16209 ** For this low-level routine, we are guaranteed that nByte>0 because
 16210 ** cases of nByte<=0 will be intercepted and dealt with by higher level
 16211 ** routines.
 16212 */
 16213 static void *sqlite3MemMalloc(int nByte){
 16214 #ifdef SQLITE_MALLOCSIZE
 16215   void *p = SQLITE_MALLOC( nByte );
 16216   if( p==0 ){
 16217     testcase( sqlite3GlobalConfig.xLog!=0 );
 16218     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 16220   return p;
 16221 #else
 16222   sqlite3_int64 *p;
 16223   assert( nByte>0 );
 16224   nByte = ROUND8(nByte);
 16225   p = SQLITE_MALLOC( nByte+8 );
 16226   if( p ){
 16227     p[0] = nByte;
 16228     p++;
 16229   }else{
 16230     testcase( sqlite3GlobalConfig.xLog!=0 );
 16231     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
 16233   return (void *)p;
 16234 #endif
 16237 /*
 16238 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
 16239 ** or sqlite3MemRealloc().
 16240 **
 16241 ** For this low-level routine, we already know that pPrior!=0 since
 16242 ** cases where pPrior==0 will have been intecepted and dealt with
 16243 ** by higher-level routines.
 16244 */
 16245 static void sqlite3MemFree(void *pPrior){
 16246 #ifdef SQLITE_MALLOCSIZE
 16247   SQLITE_FREE(pPrior);
 16248 #else
 16249   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 16250   assert( pPrior!=0 );
 16251   p--;
 16252   SQLITE_FREE(p);
 16253 #endif
 16256 /*
 16257 ** Report the allocated size of a prior return from xMalloc()
 16258 ** or xRealloc().
 16259 */
 16260 static int sqlite3MemSize(void *pPrior){
 16261 #ifdef SQLITE_MALLOCSIZE
 16262   return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
 16263 #else
 16264   sqlite3_int64 *p;
 16265   if( pPrior==0 ) return 0;
 16266   p = (sqlite3_int64*)pPrior;
 16267   p--;
 16268   return (int)p[0];
 16269 #endif
 16272 /*
 16273 ** Like realloc().  Resize an allocation previously obtained from
 16274 ** sqlite3MemMalloc().
 16275 **
 16276 ** For this low-level interface, we know that pPrior!=0.  Cases where
 16277 ** pPrior==0 while have been intercepted by higher-level routine and
 16278 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
 16279 ** cases where nByte<=0 will have been intercepted by higher-level
 16280 ** routines and redirected to xFree.
 16281 */
 16282 static void *sqlite3MemRealloc(void *pPrior, int nByte){
 16283 #ifdef SQLITE_MALLOCSIZE
 16284   void *p = SQLITE_REALLOC(pPrior, nByte);
 16285   if( p==0 ){
 16286     testcase( sqlite3GlobalConfig.xLog!=0 );
 16287     sqlite3_log(SQLITE_NOMEM,
 16288       "failed memory resize %u to %u bytes",
 16289       SQLITE_MALLOCSIZE(pPrior), nByte);
 16291   return p;
 16292 #else
 16293   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
 16294   assert( pPrior!=0 && nByte>0 );
 16295   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
 16296   p--;
 16297   p = SQLITE_REALLOC(p, nByte+8 );
 16298   if( p ){
 16299     p[0] = nByte;
 16300     p++;
 16301   }else{
 16302     testcase( sqlite3GlobalConfig.xLog!=0 );
 16303     sqlite3_log(SQLITE_NOMEM,
 16304       "failed memory resize %u to %u bytes",
 16305       sqlite3MemSize(pPrior), nByte);
 16307   return (void*)p;
 16308 #endif
 16311 /*
 16312 ** Round up a request size to the next valid allocation size.
 16313 */
 16314 static int sqlite3MemRoundup(int n){
 16315   return ROUND8(n);
 16318 /*
 16319 ** Initialize this module.
 16320 */
 16321 static int sqlite3MemInit(void *NotUsed){
 16322 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
 16323   int cpuCount;
 16324   size_t len;
 16325   if( _sqliteZone_ ){
 16326     return SQLITE_OK;
 16328   len = sizeof(cpuCount);
 16329   /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
 16330   sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
 16331   if( cpuCount>1 ){
 16332     /* defer MT decisions to system malloc */
 16333     _sqliteZone_ = malloc_default_zone();
 16334   }else{
 16335     /* only 1 core, use our own zone to contention over global locks, 
 16336     ** e.g. we have our own dedicated locks */
 16337     bool success;
 16338     malloc_zone_t* newzone = malloc_create_zone(4096, 0);
 16339     malloc_set_zone_name(newzone, "Sqlite_Heap");
 16340     do{
 16341       success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
 16342                                  (void * volatile *)&_sqliteZone_);
 16343     }while(!_sqliteZone_);
 16344     if( !success ){
 16345       /* somebody registered a zone first */
 16346       malloc_destroy_zone(newzone);
 16349 #endif
 16350   UNUSED_PARAMETER(NotUsed);
 16351   return SQLITE_OK;
 16354 /*
 16355 ** Deinitialize this module.
 16356 */
 16357 static void sqlite3MemShutdown(void *NotUsed){
 16358   UNUSED_PARAMETER(NotUsed);
 16359   return;
 16362 /*
 16363 ** This routine is the only routine in this file with external linkage.
 16364 **
 16365 ** Populate the low-level memory allocation function pointers in
 16366 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 16367 */
 16368 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 16369   static const sqlite3_mem_methods defaultMethods = {
 16370      sqlite3MemMalloc,
 16371      sqlite3MemFree,
 16372      sqlite3MemRealloc,
 16373      sqlite3MemSize,
 16374      sqlite3MemRoundup,
 16375      sqlite3MemInit,
 16376      sqlite3MemShutdown,
 16378   };
 16379   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 16382 #endif /* SQLITE_SYSTEM_MALLOC */
 16384 /************** End of mem1.c ************************************************/
 16385 /************** Begin file mem2.c ********************************************/
 16386 /*
 16387 ** 2007 August 15
 16388 **
 16389 ** The author disclaims copyright to this source code.  In place of
 16390 ** a legal notice, here is a blessing:
 16391 **
 16392 **    May you do good and not evil.
 16393 **    May you find forgiveness for yourself and forgive others.
 16394 **    May you share freely, never taking more than you give.
 16395 **
 16396 *************************************************************************
 16397 **
 16398 ** This file contains low-level memory allocation drivers for when
 16399 ** SQLite will use the standard C-library malloc/realloc/free interface
 16400 ** to obtain the memory it needs while adding lots of additional debugging
 16401 ** information to each allocation in order to help detect and fix memory
 16402 ** leaks and memory usage errors.
 16403 **
 16404 ** This file contains implementations of the low-level memory allocation
 16405 ** routines specified in the sqlite3_mem_methods object.
 16406 */
 16408 /*
 16409 ** This version of the memory allocator is used only if the
 16410 ** SQLITE_MEMDEBUG macro is defined
 16411 */
 16412 #ifdef SQLITE_MEMDEBUG
 16414 /*
 16415 ** The backtrace functionality is only available with GLIBC
 16416 */
 16417 #ifdef __GLIBC__
 16418   extern int backtrace(void**,int);
 16419   extern void backtrace_symbols_fd(void*const*,int,int);
 16420 #else
 16421 # define backtrace(A,B) 1
 16422 # define backtrace_symbols_fd(A,B,C)
 16423 #endif
 16424 /* #include <stdio.h> */
 16426 /*
 16427 ** Each memory allocation looks like this:
 16428 **
 16429 **  ------------------------------------------------------------------------
 16430 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
 16431 **  ------------------------------------------------------------------------
 16432 **
 16433 ** The application code sees only a pointer to the allocation.  We have
 16434 ** to back up from the allocation pointer to find the MemBlockHdr.  The
 16435 ** MemBlockHdr tells us the size of the allocation and the number of
 16436 ** backtrace pointers.  There is also a guard word at the end of the
 16437 ** MemBlockHdr.
 16438 */
 16439 struct MemBlockHdr {
 16440   i64 iSize;                          /* Size of this allocation */
 16441   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
 16442   char nBacktrace;                    /* Number of backtraces on this alloc */
 16443   char nBacktraceSlots;               /* Available backtrace slots */
 16444   u8 nTitle;                          /* Bytes of title; includes '\0' */
 16445   u8 eType;                           /* Allocation type code */
 16446   int iForeGuard;                     /* Guard word for sanity */
 16447 };
 16449 /*
 16450 ** Guard words
 16451 */
 16452 #define FOREGUARD 0x80F5E153
 16453 #define REARGUARD 0xE4676B53
 16455 /*
 16456 ** Number of malloc size increments to track.
 16457 */
 16458 #define NCSIZE  1000
 16460 /*
 16461 ** All of the static variables used by this module are collected
 16462 ** into a single structure named "mem".  This is to keep the
 16463 ** static variables organized and to reduce namespace pollution
 16464 ** when this module is combined with other in the amalgamation.
 16465 */
 16466 static struct {
 16468   /*
 16469   ** Mutex to control access to the memory allocation subsystem.
 16470   */
 16471   sqlite3_mutex *mutex;
 16473   /*
 16474   ** Head and tail of a linked list of all outstanding allocations
 16475   */
 16476   struct MemBlockHdr *pFirst;
 16477   struct MemBlockHdr *pLast;
 16479   /*
 16480   ** The number of levels of backtrace to save in new allocations.
 16481   */
 16482   int nBacktrace;
 16483   void (*xBacktrace)(int, int, void **);
 16485   /*
 16486   ** Title text to insert in front of each block
 16487   */
 16488   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
 16489   char zTitle[100];  /* The title text */
 16491   /* 
 16492   ** sqlite3MallocDisallow() increments the following counter.
 16493   ** sqlite3MallocAllow() decrements it.
 16494   */
 16495   int disallow; /* Do not allow memory allocation */
 16497   /*
 16498   ** Gather statistics on the sizes of memory allocations.
 16499   ** nAlloc[i] is the number of allocation attempts of i*8
 16500   ** bytes.  i==NCSIZE is the number of allocation attempts for
 16501   ** sizes more than NCSIZE*8 bytes.
 16502   */
 16503   int nAlloc[NCSIZE];      /* Total number of allocations */
 16504   int nCurrent[NCSIZE];    /* Current number of allocations */
 16505   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
 16507 } mem;
 16510 /*
 16511 ** Adjust memory usage statistics
 16512 */
 16513 static void adjustStats(int iSize, int increment){
 16514   int i = ROUND8(iSize)/8;
 16515   if( i>NCSIZE-1 ){
 16516     i = NCSIZE - 1;
 16518   if( increment>0 ){
 16519     mem.nAlloc[i]++;
 16520     mem.nCurrent[i]++;
 16521     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
 16522       mem.mxCurrent[i] = mem.nCurrent[i];
 16524   }else{
 16525     mem.nCurrent[i]--;
 16526     assert( mem.nCurrent[i]>=0 );
 16530 /*
 16531 ** Given an allocation, find the MemBlockHdr for that allocation.
 16532 **
 16533 ** This routine checks the guards at either end of the allocation and
 16534 ** if they are incorrect it asserts.
 16535 */
 16536 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
 16537   struct MemBlockHdr *p;
 16538   int *pInt;
 16539   u8 *pU8;
 16540   int nReserve;
 16542   p = (struct MemBlockHdr*)pAllocation;
 16543   p--;
 16544   assert( p->iForeGuard==(int)FOREGUARD );
 16545   nReserve = ROUND8(p->iSize);
 16546   pInt = (int*)pAllocation;
 16547   pU8 = (u8*)pAllocation;
 16548   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
 16549   /* This checks any of the "extra" bytes allocated due
 16550   ** to rounding up to an 8 byte boundary to ensure 
 16551   ** they haven't been overwritten.
 16552   */
 16553   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
 16554   return p;
 16557 /*
 16558 ** Return the number of bytes currently allocated at address p.
 16559 */
 16560 static int sqlite3MemSize(void *p){
 16561   struct MemBlockHdr *pHdr;
 16562   if( !p ){
 16563     return 0;
 16565   pHdr = sqlite3MemsysGetHeader(p);
 16566   return (int)pHdr->iSize;
 16569 /*
 16570 ** Initialize the memory allocation subsystem.
 16571 */
 16572 static int sqlite3MemInit(void *NotUsed){
 16573   UNUSED_PARAMETER(NotUsed);
 16574   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
 16575   if( !sqlite3GlobalConfig.bMemstat ){
 16576     /* If memory status is enabled, then the malloc.c wrapper will already
 16577     ** hold the STATIC_MEM mutex when the routines here are invoked. */
 16578     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 16580   return SQLITE_OK;
 16583 /*
 16584 ** Deinitialize the memory allocation subsystem.
 16585 */
 16586 static void sqlite3MemShutdown(void *NotUsed){
 16587   UNUSED_PARAMETER(NotUsed);
 16588   mem.mutex = 0;
 16591 /*
 16592 ** Round up a request size to the next valid allocation size.
 16593 */
 16594 static int sqlite3MemRoundup(int n){
 16595   return ROUND8(n);
 16598 /*
 16599 ** Fill a buffer with pseudo-random bytes.  This is used to preset
 16600 ** the content of a new memory allocation to unpredictable values and
 16601 ** to clear the content of a freed allocation to unpredictable values.
 16602 */
 16603 static void randomFill(char *pBuf, int nByte){
 16604   unsigned int x, y, r;
 16605   x = SQLITE_PTR_TO_INT(pBuf);
 16606   y = nByte | 1;
 16607   while( nByte >= 4 ){
 16608     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
 16609     y = y*1103515245 + 12345;
 16610     r = x ^ y;
 16611     *(int*)pBuf = r;
 16612     pBuf += 4;
 16613     nByte -= 4;
 16615   while( nByte-- > 0 ){
 16616     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
 16617     y = y*1103515245 + 12345;
 16618     r = x ^ y;
 16619     *(pBuf++) = r & 0xff;
 16623 /*
 16624 ** Allocate nByte bytes of memory.
 16625 */
 16626 static void *sqlite3MemMalloc(int nByte){
 16627   struct MemBlockHdr *pHdr;
 16628   void **pBt;
 16629   char *z;
 16630   int *pInt;
 16631   void *p = 0;
 16632   int totalSize;
 16633   int nReserve;
 16634   sqlite3_mutex_enter(mem.mutex);
 16635   assert( mem.disallow==0 );
 16636   nReserve = ROUND8(nByte);
 16637   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
 16638                mem.nBacktrace*sizeof(void*) + mem.nTitle;
 16639   p = malloc(totalSize);
 16640   if( p ){
 16641     z = p;
 16642     pBt = (void**)&z[mem.nTitle];
 16643     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
 16644     pHdr->pNext = 0;
 16645     pHdr->pPrev = mem.pLast;
 16646     if( mem.pLast ){
 16647       mem.pLast->pNext = pHdr;
 16648     }else{
 16649       mem.pFirst = pHdr;
 16651     mem.pLast = pHdr;
 16652     pHdr->iForeGuard = FOREGUARD;
 16653     pHdr->eType = MEMTYPE_HEAP;
 16654     pHdr->nBacktraceSlots = mem.nBacktrace;
 16655     pHdr->nTitle = mem.nTitle;
 16656     if( mem.nBacktrace ){
 16657       void *aAddr[40];
 16658       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
 16659       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
 16660       assert(pBt[0]);
 16661       if( mem.xBacktrace ){
 16662         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
 16664     }else{
 16665       pHdr->nBacktrace = 0;
 16667     if( mem.nTitle ){
 16668       memcpy(z, mem.zTitle, mem.nTitle);
 16670     pHdr->iSize = nByte;
 16671     adjustStats(nByte, +1);
 16672     pInt = (int*)&pHdr[1];
 16673     pInt[nReserve/sizeof(int)] = REARGUARD;
 16674     randomFill((char*)pInt, nByte);
 16675     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
 16676     p = (void*)pInt;
 16678   sqlite3_mutex_leave(mem.mutex);
 16679   return p; 
 16682 /*
 16683 ** Free memory.
 16684 */
 16685 static void sqlite3MemFree(void *pPrior){
 16686   struct MemBlockHdr *pHdr;
 16687   void **pBt;
 16688   char *z;
 16689   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
 16690        || mem.mutex!=0 );
 16691   pHdr = sqlite3MemsysGetHeader(pPrior);
 16692   pBt = (void**)pHdr;
 16693   pBt -= pHdr->nBacktraceSlots;
 16694   sqlite3_mutex_enter(mem.mutex);
 16695   if( pHdr->pPrev ){
 16696     assert( pHdr->pPrev->pNext==pHdr );
 16697     pHdr->pPrev->pNext = pHdr->pNext;
 16698   }else{
 16699     assert( mem.pFirst==pHdr );
 16700     mem.pFirst = pHdr->pNext;
 16702   if( pHdr->pNext ){
 16703     assert( pHdr->pNext->pPrev==pHdr );
 16704     pHdr->pNext->pPrev = pHdr->pPrev;
 16705   }else{
 16706     assert( mem.pLast==pHdr );
 16707     mem.pLast = pHdr->pPrev;
 16709   z = (char*)pBt;
 16710   z -= pHdr->nTitle;
 16711   adjustStats((int)pHdr->iSize, -1);
 16712   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
 16713                 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
 16714   free(z);
 16715   sqlite3_mutex_leave(mem.mutex);  
 16718 /*
 16719 ** Change the size of an existing memory allocation.
 16720 **
 16721 ** For this debugging implementation, we *always* make a copy of the
 16722 ** allocation into a new place in memory.  In this way, if the 
 16723 ** higher level code is using pointer to the old allocation, it is 
 16724 ** much more likely to break and we are much more liking to find
 16725 ** the error.
 16726 */
 16727 static void *sqlite3MemRealloc(void *pPrior, int nByte){
 16728   struct MemBlockHdr *pOldHdr;
 16729   void *pNew;
 16730   assert( mem.disallow==0 );
 16731   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
 16732   pOldHdr = sqlite3MemsysGetHeader(pPrior);
 16733   pNew = sqlite3MemMalloc(nByte);
 16734   if( pNew ){
 16735     memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
 16736     if( nByte>pOldHdr->iSize ){
 16737       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
 16739     sqlite3MemFree(pPrior);
 16741   return pNew;
 16744 /*
 16745 ** Populate the low-level memory allocation function pointers in
 16746 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
 16747 */
 16748 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 16749   static const sqlite3_mem_methods defaultMethods = {
 16750      sqlite3MemMalloc,
 16751      sqlite3MemFree,
 16752      sqlite3MemRealloc,
 16753      sqlite3MemSize,
 16754      sqlite3MemRoundup,
 16755      sqlite3MemInit,
 16756      sqlite3MemShutdown,
 16758   };
 16759   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
 16762 /*
 16763 ** Set the "type" of an allocation.
 16764 */
 16765 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
 16766   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16767     struct MemBlockHdr *pHdr;
 16768     pHdr = sqlite3MemsysGetHeader(p);
 16769     assert( pHdr->iForeGuard==FOREGUARD );
 16770     pHdr->eType = eType;
 16774 /*
 16775 ** Return TRUE if the mask of type in eType matches the type of the
 16776 ** allocation p.  Also return true if p==NULL.
 16777 **
 16778 ** This routine is designed for use within an assert() statement, to
 16779 ** verify the type of an allocation.  For example:
 16780 **
 16781 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 16782 */
 16783 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
 16784   int rc = 1;
 16785   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16786     struct MemBlockHdr *pHdr;
 16787     pHdr = sqlite3MemsysGetHeader(p);
 16788     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 16789     if( (pHdr->eType&eType)==0 ){
 16790       rc = 0;
 16793   return rc;
 16796 /*
 16797 ** Return TRUE if the mask of type in eType matches no bits of the type of the
 16798 ** allocation p.  Also return true if p==NULL.
 16799 **
 16800 ** This routine is designed for use within an assert() statement, to
 16801 ** verify the type of an allocation.  For example:
 16802 **
 16803 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 16804 */
 16805 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
 16806   int rc = 1;
 16807   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
 16808     struct MemBlockHdr *pHdr;
 16809     pHdr = sqlite3MemsysGetHeader(p);
 16810     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
 16811     if( (pHdr->eType&eType)!=0 ){
 16812       rc = 0;
 16815   return rc;
 16818 /*
 16819 ** Set the number of backtrace levels kept for each allocation.
 16820 ** A value of zero turns off backtracing.  The number is always rounded
 16821 ** up to a multiple of 2.
 16822 */
 16823 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
 16824   if( depth<0 ){ depth = 0; }
 16825   if( depth>20 ){ depth = 20; }
 16826   depth = (depth+1)&0xfe;
 16827   mem.nBacktrace = depth;
 16830 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
 16831   mem.xBacktrace = xBacktrace;
 16834 /*
 16835 ** Set the title string for subsequent allocations.
 16836 */
 16837 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
 16838   unsigned int n = sqlite3Strlen30(zTitle) + 1;
 16839   sqlite3_mutex_enter(mem.mutex);
 16840   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
 16841   memcpy(mem.zTitle, zTitle, n);
 16842   mem.zTitle[n] = 0;
 16843   mem.nTitle = ROUND8(n);
 16844   sqlite3_mutex_leave(mem.mutex);
 16847 SQLITE_PRIVATE void sqlite3MemdebugSync(){
 16848   struct MemBlockHdr *pHdr;
 16849   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 16850     void **pBt = (void**)pHdr;
 16851     pBt -= pHdr->nBacktraceSlots;
 16852     mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
 16856 /*
 16857 ** Open the file indicated and write a log of all unfreed memory 
 16858 ** allocations into that log.
 16859 */
 16860 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
 16861   FILE *out;
 16862   struct MemBlockHdr *pHdr;
 16863   void **pBt;
 16864   int i;
 16865   out = fopen(zFilename, "w");
 16866   if( out==0 ){
 16867     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 16868                     zFilename);
 16869     return;
 16871   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
 16872     char *z = (char*)pHdr;
 16873     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
 16874     fprintf(out, "**** %lld bytes at %p from %s ****\n", 
 16875             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
 16876     if( pHdr->nBacktrace ){
 16877       fflush(out);
 16878       pBt = (void**)pHdr;
 16879       pBt -= pHdr->nBacktraceSlots;
 16880       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
 16881       fprintf(out, "\n");
 16884   fprintf(out, "COUNTS:\n");
 16885   for(i=0; i<NCSIZE-1; i++){
 16886     if( mem.nAlloc[i] ){
 16887       fprintf(out, "   %5d: %10d %10d %10d\n", 
 16888             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
 16891   if( mem.nAlloc[NCSIZE-1] ){
 16892     fprintf(out, "   %5d: %10d %10d %10d\n",
 16893              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
 16894              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
 16896   fclose(out);
 16899 /*
 16900 ** Return the number of times sqlite3MemMalloc() has been called.
 16901 */
 16902 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
 16903   int i;
 16904   int nTotal = 0;
 16905   for(i=0; i<NCSIZE; i++){
 16906     nTotal += mem.nAlloc[i];
 16908   return nTotal;
 16912 #endif /* SQLITE_MEMDEBUG */
 16914 /************** End of mem2.c ************************************************/
 16915 /************** Begin file mem3.c ********************************************/
 16916 /*
 16917 ** 2007 October 14
 16918 **
 16919 ** The author disclaims copyright to this source code.  In place of
 16920 ** a legal notice, here is a blessing:
 16921 **
 16922 **    May you do good and not evil.
 16923 **    May you find forgiveness for yourself and forgive others.
 16924 **    May you share freely, never taking more than you give.
 16925 **
 16926 *************************************************************************
 16927 ** This file contains the C functions that implement a memory
 16928 ** allocation subsystem for use by SQLite. 
 16929 **
 16930 ** This version of the memory allocation subsystem omits all
 16931 ** use of malloc(). The SQLite user supplies a block of memory
 16932 ** before calling sqlite3_initialize() from which allocations
 16933 ** are made and returned by the xMalloc() and xRealloc() 
 16934 ** implementations. Once sqlite3_initialize() has been called,
 16935 ** the amount of memory available to SQLite is fixed and cannot
 16936 ** be changed.
 16937 **
 16938 ** This version of the memory allocation subsystem is included
 16939 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
 16940 */
 16942 /*
 16943 ** This version of the memory allocator is only built into the library
 16944 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
 16945 ** mean that the library will use a memory-pool by default, just that
 16946 ** it is available. The mempool allocator is activated by calling
 16947 ** sqlite3_config().
 16948 */
 16949 #ifdef SQLITE_ENABLE_MEMSYS3
 16951 /*
 16952 ** Maximum size (in Mem3Blocks) of a "small" chunk.
 16953 */
 16954 #define MX_SMALL 10
 16957 /*
 16958 ** Number of freelist hash slots
 16959 */
 16960 #define N_HASH  61
 16962 /*
 16963 ** A memory allocation (also called a "chunk") consists of two or 
 16964 ** more blocks where each block is 8 bytes.  The first 8 bytes are 
 16965 ** a header that is not returned to the user.
 16966 **
 16967 ** A chunk is two or more blocks that is either checked out or
 16968 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
 16969 ** size of the allocation in blocks if the allocation is free.
 16970 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
 16971 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
 16972 ** is true if the previous chunk is checked out and false if the
 16973 ** previous chunk is free.  The u.hdr.prevSize field is the size of
 16974 ** the previous chunk in blocks if the previous chunk is on the
 16975 ** freelist. If the previous chunk is checked out, then
 16976 ** u.hdr.prevSize can be part of the data for that chunk and should
 16977 ** not be read or written.
 16978 **
 16979 ** We often identify a chunk by its index in mem3.aPool[].  When
 16980 ** this is done, the chunk index refers to the second block of
 16981 ** the chunk.  In this way, the first chunk has an index of 1.
 16982 ** A chunk index of 0 means "no such chunk" and is the equivalent
 16983 ** of a NULL pointer.
 16984 **
 16985 ** The second block of free chunks is of the form u.list.  The
 16986 ** two fields form a double-linked list of chunks of related sizes.
 16987 ** Pointers to the head of the list are stored in mem3.aiSmall[] 
 16988 ** for smaller chunks and mem3.aiHash[] for larger chunks.
 16989 **
 16990 ** The second block of a chunk is user data if the chunk is checked 
 16991 ** out.  If a chunk is checked out, the user data may extend into
 16992 ** the u.hdr.prevSize value of the following chunk.
 16993 */
 16994 typedef struct Mem3Block Mem3Block;
 16995 struct Mem3Block {
 16996   union {
 16997     struct {
 16998       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
 16999       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
 17000     } hdr;
 17001     struct {
 17002       u32 next;       /* Index in mem3.aPool[] of next free chunk */
 17003       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
 17004     } list;
 17005   } u;
 17006 };
 17008 /*
 17009 ** All of the static variables used by this module are collected
 17010 ** into a single structure named "mem3".  This is to keep the
 17011 ** static variables organized and to reduce namespace pollution
 17012 ** when this module is combined with other in the amalgamation.
 17013 */
 17014 static SQLITE_WSD struct Mem3Global {
 17015   /*
 17016   ** Memory available for allocation. nPool is the size of the array
 17017   ** (in Mem3Blocks) pointed to by aPool less 2.
 17018   */
 17019   u32 nPool;
 17020   Mem3Block *aPool;
 17022   /*
 17023   ** True if we are evaluating an out-of-memory callback.
 17024   */
 17025   int alarmBusy;
 17027   /*
 17028   ** Mutex to control access to the memory allocation subsystem.
 17029   */
 17030   sqlite3_mutex *mutex;
 17032   /*
 17033   ** The minimum amount of free space that we have seen.
 17034   */
 17035   u32 mnMaster;
 17037   /*
 17038   ** iMaster is the index of the master chunk.  Most new allocations
 17039   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
 17040   ** of the current master.  iMaster is 0 if there is not master chunk.
 17041   ** The master chunk is not in either the aiHash[] or aiSmall[].
 17042   */
 17043   u32 iMaster;
 17044   u32 szMaster;
 17046   /*
 17047   ** Array of lists of free blocks according to the block size 
 17048   ** for smaller chunks, or a hash on the block size for larger
 17049   ** chunks.
 17050   */
 17051   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
 17052   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
 17053 } mem3 = { 97535575 };
 17055 #define mem3 GLOBAL(struct Mem3Global, mem3)
 17057 /*
 17058 ** Unlink the chunk at mem3.aPool[i] from list it is currently
 17059 ** on.  *pRoot is the list that i is a member of.
 17060 */
 17061 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
 17062   u32 next = mem3.aPool[i].u.list.next;
 17063   u32 prev = mem3.aPool[i].u.list.prev;
 17064   assert( sqlite3_mutex_held(mem3.mutex) );
 17065   if( prev==0 ){
 17066     *pRoot = next;
 17067   }else{
 17068     mem3.aPool[prev].u.list.next = next;
 17070   if( next ){
 17071     mem3.aPool[next].u.list.prev = prev;
 17073   mem3.aPool[i].u.list.next = 0;
 17074   mem3.aPool[i].u.list.prev = 0;
 17077 /*
 17078 ** Unlink the chunk at index i from 
 17079 ** whatever list is currently a member of.
 17080 */
 17081 static void memsys3Unlink(u32 i){
 17082   u32 size, hash;
 17083   assert( sqlite3_mutex_held(mem3.mutex) );
 17084   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 17085   assert( i>=1 );
 17086   size = mem3.aPool[i-1].u.hdr.size4x/4;
 17087   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 17088   assert( size>=2 );
 17089   if( size <= MX_SMALL ){
 17090     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
 17091   }else{
 17092     hash = size % N_HASH;
 17093     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 17097 /*
 17098 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
 17099 ** at *pRoot.
 17100 */
 17101 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
 17102   assert( sqlite3_mutex_held(mem3.mutex) );
 17103   mem3.aPool[i].u.list.next = *pRoot;
 17104   mem3.aPool[i].u.list.prev = 0;
 17105   if( *pRoot ){
 17106     mem3.aPool[*pRoot].u.list.prev = i;
 17108   *pRoot = i;
 17111 /*
 17112 ** Link the chunk at index i into either the appropriate
 17113 ** small chunk list, or into the large chunk hash table.
 17114 */
 17115 static void memsys3Link(u32 i){
 17116   u32 size, hash;
 17117   assert( sqlite3_mutex_held(mem3.mutex) );
 17118   assert( i>=1 );
 17119   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
 17120   size = mem3.aPool[i-1].u.hdr.size4x/4;
 17121   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
 17122   assert( size>=2 );
 17123   if( size <= MX_SMALL ){
 17124     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
 17125   }else{
 17126     hash = size % N_HASH;
 17127     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
 17131 /*
 17132 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 17133 ** will already be held (obtained by code in malloc.c) if
 17134 ** sqlite3GlobalConfig.bMemStat is true.
 17135 */
 17136 static void memsys3Enter(void){
 17137   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
 17138     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 17140   sqlite3_mutex_enter(mem3.mutex);
 17142 static void memsys3Leave(void){
 17143   sqlite3_mutex_leave(mem3.mutex);
 17146 /*
 17147 ** Called when we are unable to satisfy an allocation of nBytes.
 17148 */
 17149 static void memsys3OutOfMemory(int nByte){
 17150   if( !mem3.alarmBusy ){
 17151     mem3.alarmBusy = 1;
 17152     assert( sqlite3_mutex_held(mem3.mutex) );
 17153     sqlite3_mutex_leave(mem3.mutex);
 17154     sqlite3_release_memory(nByte);
 17155     sqlite3_mutex_enter(mem3.mutex);
 17156     mem3.alarmBusy = 0;
 17161 /*
 17162 ** Chunk i is a free chunk that has been unlinked.  Adjust its 
 17163 ** size parameters for check-out and return a pointer to the 
 17164 ** user portion of the chunk.
 17165 */
 17166 static void *memsys3Checkout(u32 i, u32 nBlock){
 17167   u32 x;
 17168   assert( sqlite3_mutex_held(mem3.mutex) );
 17169   assert( i>=1 );
 17170   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
 17171   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
 17172   x = mem3.aPool[i-1].u.hdr.size4x;
 17173   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
 17174   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
 17175   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
 17176   return &mem3.aPool[i];
 17179 /*
 17180 ** Carve a piece off of the end of the mem3.iMaster free chunk.
 17181 ** Return a pointer to the new allocation.  Or, if the master chunk
 17182 ** is not large enough, return 0.
 17183 */
 17184 static void *memsys3FromMaster(u32 nBlock){
 17185   assert( sqlite3_mutex_held(mem3.mutex) );
 17186   assert( mem3.szMaster>=nBlock );
 17187   if( nBlock>=mem3.szMaster-1 ){
 17188     /* Use the entire master */
 17189     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
 17190     mem3.iMaster = 0;
 17191     mem3.szMaster = 0;
 17192     mem3.mnMaster = 0;
 17193     return p;
 17194   }else{
 17195     /* Split the master block.  Return the tail. */
 17196     u32 newi, x;
 17197     newi = mem3.iMaster + mem3.szMaster - nBlock;
 17198     assert( newi > mem3.iMaster+1 );
 17199     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
 17200     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
 17201     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
 17202     mem3.szMaster -= nBlock;
 17203     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
 17204     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 17205     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 17206     if( mem3.szMaster < mem3.mnMaster ){
 17207       mem3.mnMaster = mem3.szMaster;
 17209     return (void*)&mem3.aPool[newi];
 17213 /*
 17214 ** *pRoot is the head of a list of free chunks of the same size
 17215 ** or same size hash.  In other words, *pRoot is an entry in either
 17216 ** mem3.aiSmall[] or mem3.aiHash[].  
 17217 **
 17218 ** This routine examines all entries on the given list and tries
 17219 ** to coalesce each entries with adjacent free chunks.  
 17220 **
 17221 ** If it sees a chunk that is larger than mem3.iMaster, it replaces 
 17222 ** the current mem3.iMaster with the new larger chunk.  In order for
 17223 ** this mem3.iMaster replacement to work, the master chunk must be
 17224 ** linked into the hash tables.  That is not the normal state of
 17225 ** affairs, of course.  The calling routine must link the master
 17226 ** chunk before invoking this routine, then must unlink the (possibly
 17227 ** changed) master chunk once this routine has finished.
 17228 */
 17229 static void memsys3Merge(u32 *pRoot){
 17230   u32 iNext, prev, size, i, x;
 17232   assert( sqlite3_mutex_held(mem3.mutex) );
 17233   for(i=*pRoot; i>0; i=iNext){
 17234     iNext = mem3.aPool[i].u.list.next;
 17235     size = mem3.aPool[i-1].u.hdr.size4x;
 17236     assert( (size&1)==0 );
 17237     if( (size&2)==0 ){
 17238       memsys3UnlinkFromList(i, pRoot);
 17239       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
 17240       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
 17241       if( prev==iNext ){
 17242         iNext = mem3.aPool[prev].u.list.next;
 17244       memsys3Unlink(prev);
 17245       size = i + size/4 - prev;
 17246       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
 17247       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
 17248       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
 17249       memsys3Link(prev);
 17250       i = prev;
 17251     }else{
 17252       size /= 4;
 17254     if( size>mem3.szMaster ){
 17255       mem3.iMaster = i;
 17256       mem3.szMaster = size;
 17261 /*
 17262 ** Return a block of memory of at least nBytes in size.
 17263 ** Return NULL if unable.
 17264 **
 17265 ** This function assumes that the necessary mutexes, if any, are
 17266 ** already held by the caller. Hence "Unsafe".
 17267 */
 17268 static void *memsys3MallocUnsafe(int nByte){
 17269   u32 i;
 17270   u32 nBlock;
 17271   u32 toFree;
 17273   assert( sqlite3_mutex_held(mem3.mutex) );
 17274   assert( sizeof(Mem3Block)==8 );
 17275   if( nByte<=12 ){
 17276     nBlock = 2;
 17277   }else{
 17278     nBlock = (nByte + 11)/8;
 17280   assert( nBlock>=2 );
 17282   /* STEP 1:
 17283   ** Look for an entry of the correct size in either the small
 17284   ** chunk table or in the large chunk hash table.  This is
 17285   ** successful most of the time (about 9 times out of 10).
 17286   */
 17287   if( nBlock <= MX_SMALL ){
 17288     i = mem3.aiSmall[nBlock-2];
 17289     if( i>0 ){
 17290       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
 17291       return memsys3Checkout(i, nBlock);
 17293   }else{
 17294     int hash = nBlock % N_HASH;
 17295     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
 17296       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
 17297         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
 17298         return memsys3Checkout(i, nBlock);
 17303   /* STEP 2:
 17304   ** Try to satisfy the allocation by carving a piece off of the end
 17305   ** of the master chunk.  This step usually works if step 1 fails.
 17306   */
 17307   if( mem3.szMaster>=nBlock ){
 17308     return memsys3FromMaster(nBlock);
 17312   /* STEP 3:  
 17313   ** Loop through the entire memory pool.  Coalesce adjacent free
 17314   ** chunks.  Recompute the master chunk as the largest free chunk.
 17315   ** Then try again to satisfy the allocation by carving a piece off
 17316   ** of the end of the master chunk.  This step happens very
 17317   ** rarely (we hope!)
 17318   */
 17319   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
 17320     memsys3OutOfMemory(toFree);
 17321     if( mem3.iMaster ){
 17322       memsys3Link(mem3.iMaster);
 17323       mem3.iMaster = 0;
 17324       mem3.szMaster = 0;
 17326     for(i=0; i<N_HASH; i++){
 17327       memsys3Merge(&mem3.aiHash[i]);
 17329     for(i=0; i<MX_SMALL-1; i++){
 17330       memsys3Merge(&mem3.aiSmall[i]);
 17332     if( mem3.szMaster ){
 17333       memsys3Unlink(mem3.iMaster);
 17334       if( mem3.szMaster>=nBlock ){
 17335         return memsys3FromMaster(nBlock);
 17340   /* If none of the above worked, then we fail. */
 17341   return 0;
 17344 /*
 17345 ** Free an outstanding memory allocation.
 17346 **
 17347 ** This function assumes that the necessary mutexes, if any, are
 17348 ** already held by the caller. Hence "Unsafe".
 17349 */
 17350 static void memsys3FreeUnsafe(void *pOld){
 17351   Mem3Block *p = (Mem3Block*)pOld;
 17352   int i;
 17353   u32 size, x;
 17354   assert( sqlite3_mutex_held(mem3.mutex) );
 17355   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
 17356   i = p - mem3.aPool;
 17357   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
 17358   size = mem3.aPool[i-1].u.hdr.size4x/4;
 17359   assert( i+size<=mem3.nPool+1 );
 17360   mem3.aPool[i-1].u.hdr.size4x &= ~1;
 17361   mem3.aPool[i+size-1].u.hdr.prevSize = size;
 17362   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
 17363   memsys3Link(i);
 17365   /* Try to expand the master using the newly freed chunk */
 17366   if( mem3.iMaster ){
 17367     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
 17368       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
 17369       mem3.iMaster -= size;
 17370       mem3.szMaster += size;
 17371       memsys3Unlink(mem3.iMaster);
 17372       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 17373       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 17374       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 17376     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
 17377     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
 17378       memsys3Unlink(mem3.iMaster+mem3.szMaster);
 17379       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
 17380       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
 17381       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
 17386 /*
 17387 ** Return the size of an outstanding allocation, in bytes.  The
 17388 ** size returned omits the 8-byte header overhead.  This only
 17389 ** works for chunks that are currently checked out.
 17390 */
 17391 static int memsys3Size(void *p){
 17392   Mem3Block *pBlock;
 17393   if( p==0 ) return 0;
 17394   pBlock = (Mem3Block*)p;
 17395   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
 17396   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
 17399 /*
 17400 ** Round up a request size to the next valid allocation size.
 17401 */
 17402 static int memsys3Roundup(int n){
 17403   if( n<=12 ){
 17404     return 12;
 17405   }else{
 17406     return ((n+11)&~7) - 4;
 17410 /*
 17411 ** Allocate nBytes of memory.
 17412 */
 17413 static void *memsys3Malloc(int nBytes){
 17414   sqlite3_int64 *p;
 17415   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
 17416   memsys3Enter();
 17417   p = memsys3MallocUnsafe(nBytes);
 17418   memsys3Leave();
 17419   return (void*)p; 
 17422 /*
 17423 ** Free memory.
 17424 */
 17425 static void memsys3Free(void *pPrior){
 17426   assert( pPrior );
 17427   memsys3Enter();
 17428   memsys3FreeUnsafe(pPrior);
 17429   memsys3Leave();
 17432 /*
 17433 ** Change the size of an existing memory allocation
 17434 */
 17435 static void *memsys3Realloc(void *pPrior, int nBytes){
 17436   int nOld;
 17437   void *p;
 17438   if( pPrior==0 ){
 17439     return sqlite3_malloc(nBytes);
 17441   if( nBytes<=0 ){
 17442     sqlite3_free(pPrior);
 17443     return 0;
 17445   nOld = memsys3Size(pPrior);
 17446   if( nBytes<=nOld && nBytes>=nOld-128 ){
 17447     return pPrior;
 17449   memsys3Enter();
 17450   p = memsys3MallocUnsafe(nBytes);
 17451   if( p ){
 17452     if( nOld<nBytes ){
 17453       memcpy(p, pPrior, nOld);
 17454     }else{
 17455       memcpy(p, pPrior, nBytes);
 17457     memsys3FreeUnsafe(pPrior);
 17459   memsys3Leave();
 17460   return p;
 17463 /*
 17464 ** Initialize this module.
 17465 */
 17466 static int memsys3Init(void *NotUsed){
 17467   UNUSED_PARAMETER(NotUsed);
 17468   if( !sqlite3GlobalConfig.pHeap ){
 17469     return SQLITE_ERROR;
 17472   /* Store a pointer to the memory block in global structure mem3. */
 17473   assert( sizeof(Mem3Block)==8 );
 17474   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
 17475   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
 17477   /* Initialize the master block. */
 17478   mem3.szMaster = mem3.nPool;
 17479   mem3.mnMaster = mem3.szMaster;
 17480   mem3.iMaster = 1;
 17481   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
 17482   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
 17483   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
 17485   return SQLITE_OK;
 17488 /*
 17489 ** Deinitialize this module.
 17490 */
 17491 static void memsys3Shutdown(void *NotUsed){
 17492   UNUSED_PARAMETER(NotUsed);
 17493   mem3.mutex = 0;
 17494   return;
 17499 /*
 17500 ** Open the file indicated and write a log of all unfreed memory 
 17501 ** allocations into that log.
 17502 */
 17503 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
 17504 #ifdef SQLITE_DEBUG
 17505   FILE *out;
 17506   u32 i, j;
 17507   u32 size;
 17508   if( zFilename==0 || zFilename[0]==0 ){
 17509     out = stdout;
 17510   }else{
 17511     out = fopen(zFilename, "w");
 17512     if( out==0 ){
 17513       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 17514                       zFilename);
 17515       return;
 17518   memsys3Enter();
 17519   fprintf(out, "CHUNKS:\n");
 17520   for(i=1; i<=mem3.nPool; i+=size/4){
 17521     size = mem3.aPool[i-1].u.hdr.size4x;
 17522     if( size/4<=1 ){
 17523       fprintf(out, "%p size error\n", &mem3.aPool[i]);
 17524       assert( 0 );
 17525       break;
 17527     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
 17528       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
 17529       assert( 0 );
 17530       break;
 17532     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
 17533       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
 17534       assert( 0 );
 17535       break;
 17537     if( size&1 ){
 17538       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
 17539     }else{
 17540       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
 17541                   i==mem3.iMaster ? " **master**" : "");
 17544   for(i=0; i<MX_SMALL-1; i++){
 17545     if( mem3.aiSmall[i]==0 ) continue;
 17546     fprintf(out, "small(%2d):", i);
 17547     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
 17548       fprintf(out, " %p(%d)", &mem3.aPool[j],
 17549               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 17551     fprintf(out, "\n"); 
 17553   for(i=0; i<N_HASH; i++){
 17554     if( mem3.aiHash[i]==0 ) continue;
 17555     fprintf(out, "hash(%2d):", i);
 17556     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
 17557       fprintf(out, " %p(%d)", &mem3.aPool[j],
 17558               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
 17560     fprintf(out, "\n"); 
 17562   fprintf(out, "master=%d\n", mem3.iMaster);
 17563   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
 17564   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
 17565   sqlite3_mutex_leave(mem3.mutex);
 17566   if( out==stdout ){
 17567     fflush(stdout);
 17568   }else{
 17569     fclose(out);
 17571 #else
 17572   UNUSED_PARAMETER(zFilename);
 17573 #endif
 17576 /*
 17577 ** This routine is the only routine in this file with external 
 17578 ** linkage.
 17579 **
 17580 ** Populate the low-level memory allocation function pointers in
 17581 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 17582 ** arguments specify the block of memory to manage.
 17583 **
 17584 ** This routine is only called by sqlite3_config(), and therefore
 17585 ** is not required to be threadsafe (it is not).
 17586 */
 17587 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
 17588   static const sqlite3_mem_methods mempoolMethods = {
 17589      memsys3Malloc,
 17590      memsys3Free,
 17591      memsys3Realloc,
 17592      memsys3Size,
 17593      memsys3Roundup,
 17594      memsys3Init,
 17595      memsys3Shutdown,
 17597   };
 17598   return &mempoolMethods;
 17601 #endif /* SQLITE_ENABLE_MEMSYS3 */
 17603 /************** End of mem3.c ************************************************/
 17604 /************** Begin file mem5.c ********************************************/
 17605 /*
 17606 ** 2007 October 14
 17607 **
 17608 ** The author disclaims copyright to this source code.  In place of
 17609 ** a legal notice, here is a blessing:
 17610 **
 17611 **    May you do good and not evil.
 17612 **    May you find forgiveness for yourself and forgive others.
 17613 **    May you share freely, never taking more than you give.
 17614 **
 17615 *************************************************************************
 17616 ** This file contains the C functions that implement a memory
 17617 ** allocation subsystem for use by SQLite. 
 17618 **
 17619 ** This version of the memory allocation subsystem omits all
 17620 ** use of malloc(). The application gives SQLite a block of memory
 17621 ** before calling sqlite3_initialize() from which allocations
 17622 ** are made and returned by the xMalloc() and xRealloc() 
 17623 ** implementations. Once sqlite3_initialize() has been called,
 17624 ** the amount of memory available to SQLite is fixed and cannot
 17625 ** be changed.
 17626 **
 17627 ** This version of the memory allocation subsystem is included
 17628 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
 17629 **
 17630 ** This memory allocator uses the following algorithm:
 17631 **
 17632 **   1.  All memory allocations sizes are rounded up to a power of 2.
 17633 **
 17634 **   2.  If two adjacent free blocks are the halves of a larger block,
 17635 **       then the two blocks are coalesed into the single larger block.
 17636 **
 17637 **   3.  New memory is allocated from the first available free block.
 17638 **
 17639 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
 17640 ** Concerning Dynamic Storage Allocation". Journal of the Association for
 17641 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
 17642 ** 
 17643 ** Let n be the size of the largest allocation divided by the minimum
 17644 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
 17645 ** be the maximum amount of memory ever outstanding at one time.  Let
 17646 ** N be the total amount of memory available for allocation.  Robson
 17647 ** proved that this memory allocator will never breakdown due to 
 17648 ** fragmentation as long as the following constraint holds:
 17649 **
 17650 **      N >=  M*(1 + log2(n)/2) - n + 1
 17651 **
 17652 ** The sqlite3_status() logic tracks the maximum values of n and M so
 17653 ** that an application can, at any time, verify this constraint.
 17654 */
 17656 /*
 17657 ** This version of the memory allocator is used only when 
 17658 ** SQLITE_ENABLE_MEMSYS5 is defined.
 17659 */
 17660 #ifdef SQLITE_ENABLE_MEMSYS5
 17662 /*
 17663 ** A minimum allocation is an instance of the following structure.
 17664 ** Larger allocations are an array of these structures where the
 17665 ** size of the array is a power of 2.
 17666 **
 17667 ** The size of this object must be a power of two.  That fact is
 17668 ** verified in memsys5Init().
 17669 */
 17670 typedef struct Mem5Link Mem5Link;
 17671 struct Mem5Link {
 17672   int next;       /* Index of next free chunk */
 17673   int prev;       /* Index of previous free chunk */
 17674 };
 17676 /*
 17677 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
 17678 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
 17679 ** it is not actually possible to reach this limit.
 17680 */
 17681 #define LOGMAX 30
 17683 /*
 17684 ** Masks used for mem5.aCtrl[] elements.
 17685 */
 17686 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
 17687 #define CTRL_FREE     0x20    /* True if not checked out */
 17689 /*
 17690 ** All of the static variables used by this module are collected
 17691 ** into a single structure named "mem5".  This is to keep the
 17692 ** static variables organized and to reduce namespace pollution
 17693 ** when this module is combined with other in the amalgamation.
 17694 */
 17695 static SQLITE_WSD struct Mem5Global {
 17696   /*
 17697   ** Memory available for allocation
 17698   */
 17699   int szAtom;      /* Smallest possible allocation in bytes */
 17700   int nBlock;      /* Number of szAtom sized blocks in zPool */
 17701   u8 *zPool;       /* Memory available to be allocated */
 17703   /*
 17704   ** Mutex to control access to the memory allocation subsystem.
 17705   */
 17706   sqlite3_mutex *mutex;
 17708   /*
 17709   ** Performance statistics
 17710   */
 17711   u64 nAlloc;         /* Total number of calls to malloc */
 17712   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
 17713   u64 totalExcess;    /* Total internal fragmentation */
 17714   u32 currentOut;     /* Current checkout, including internal fragmentation */
 17715   u32 currentCount;   /* Current number of distinct checkouts */
 17716   u32 maxOut;         /* Maximum instantaneous currentOut */
 17717   u32 maxCount;       /* Maximum instantaneous currentCount */
 17718   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
 17720   /*
 17721   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
 17722   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
 17723   ** and so forth.
 17724   */
 17725   int aiFreelist[LOGMAX+1];
 17727   /*
 17728   ** Space for tracking which blocks are checked out and the size
 17729   ** of each block.  One byte per block.
 17730   */
 17731   u8 *aCtrl;
 17733 } mem5;
 17735 /*
 17736 ** Access the static variable through a macro for SQLITE_OMIT_WSD.
 17737 */
 17738 #define mem5 GLOBAL(struct Mem5Global, mem5)
 17740 /*
 17741 ** Assuming mem5.zPool is divided up into an array of Mem5Link
 17742 ** structures, return a pointer to the idx-th such link.
 17743 */
 17744 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
 17746 /*
 17747 ** Unlink the chunk at mem5.aPool[i] from list it is currently
 17748 ** on.  It should be found on mem5.aiFreelist[iLogsize].
 17749 */
 17750 static void memsys5Unlink(int i, int iLogsize){
 17751   int next, prev;
 17752   assert( i>=0 && i<mem5.nBlock );
 17753   assert( iLogsize>=0 && iLogsize<=LOGMAX );
 17754   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 17756   next = MEM5LINK(i)->next;
 17757   prev = MEM5LINK(i)->prev;
 17758   if( prev<0 ){
 17759     mem5.aiFreelist[iLogsize] = next;
 17760   }else{
 17761     MEM5LINK(prev)->next = next;
 17763   if( next>=0 ){
 17764     MEM5LINK(next)->prev = prev;
 17768 /*
 17769 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
 17770 ** free list.
 17771 */
 17772 static void memsys5Link(int i, int iLogsize){
 17773   int x;
 17774   assert( sqlite3_mutex_held(mem5.mutex) );
 17775   assert( i>=0 && i<mem5.nBlock );
 17776   assert( iLogsize>=0 && iLogsize<=LOGMAX );
 17777   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
 17779   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
 17780   MEM5LINK(i)->prev = -1;
 17781   if( x>=0 ){
 17782     assert( x<mem5.nBlock );
 17783     MEM5LINK(x)->prev = i;
 17785   mem5.aiFreelist[iLogsize] = i;
 17788 /*
 17789 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
 17790 ** will already be held (obtained by code in malloc.c) if
 17791 ** sqlite3GlobalConfig.bMemStat is true.
 17792 */
 17793 static void memsys5Enter(void){
 17794   sqlite3_mutex_enter(mem5.mutex);
 17796 static void memsys5Leave(void){
 17797   sqlite3_mutex_leave(mem5.mutex);
 17800 /*
 17801 ** Return the size of an outstanding allocation, in bytes.  The
 17802 ** size returned omits the 8-byte header overhead.  This only
 17803 ** works for chunks that are currently checked out.
 17804 */
 17805 static int memsys5Size(void *p){
 17806   int iSize = 0;
 17807   if( p ){
 17808     int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
 17809     assert( i>=0 && i<mem5.nBlock );
 17810     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
 17812   return iSize;
 17815 /*
 17816 ** Return a block of memory of at least nBytes in size.
 17817 ** Return NULL if unable.  Return NULL if nBytes==0.
 17818 **
 17819 ** The caller guarantees that nByte is positive.
 17820 **
 17821 ** The caller has obtained a mutex prior to invoking this
 17822 ** routine so there is never any chance that two or more
 17823 ** threads can be in this routine at the same time.
 17824 */
 17825 static void *memsys5MallocUnsafe(int nByte){
 17826   int i;           /* Index of a mem5.aPool[] slot */
 17827   int iBin;        /* Index into mem5.aiFreelist[] */
 17828   int iFullSz;     /* Size of allocation rounded up to power of 2 */
 17829   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
 17831   /* nByte must be a positive */
 17832   assert( nByte>0 );
 17834   /* Keep track of the maximum allocation request.  Even unfulfilled
 17835   ** requests are counted */
 17836   if( (u32)nByte>mem5.maxRequest ){
 17837     mem5.maxRequest = nByte;
 17840   /* Abort if the requested allocation size is larger than the largest
 17841   ** power of two that we can represent using 32-bit signed integers.
 17842   */
 17843   if( nByte > 0x40000000 ){
 17844     return 0;
 17847   /* Round nByte up to the next valid power of two */
 17848   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
 17850   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
 17851   ** block.  If not, then split a block of the next larger power of
 17852   ** two in order to create a new free block of size iLogsize.
 17853   */
 17854   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
 17855   if( iBin>LOGMAX ){
 17856     testcase( sqlite3GlobalConfig.xLog!=0 );
 17857     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
 17858     return 0;
 17860   i = mem5.aiFreelist[iBin];
 17861   memsys5Unlink(i, iBin);
 17862   while( iBin>iLogsize ){
 17863     int newSize;
 17865     iBin--;
 17866     newSize = 1 << iBin;
 17867     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
 17868     memsys5Link(i+newSize, iBin);
 17870   mem5.aCtrl[i] = iLogsize;
 17872   /* Update allocator performance statistics. */
 17873   mem5.nAlloc++;
 17874   mem5.totalAlloc += iFullSz;
 17875   mem5.totalExcess += iFullSz - nByte;
 17876   mem5.currentCount++;
 17877   mem5.currentOut += iFullSz;
 17878   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
 17879   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
 17881 #ifdef SQLITE_DEBUG
 17882   /* Make sure the allocated memory does not assume that it is set to zero
 17883   ** or retains a value from a previous allocation */
 17884   memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
 17885 #endif
 17887   /* Return a pointer to the allocated memory. */
 17888   return (void*)&mem5.zPool[i*mem5.szAtom];
 17891 /*
 17892 ** Free an outstanding memory allocation.
 17893 */
 17894 static void memsys5FreeUnsafe(void *pOld){
 17895   u32 size, iLogsize;
 17896   int iBlock;
 17898   /* Set iBlock to the index of the block pointed to by pOld in 
 17899   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
 17900   */
 17901   iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
 17903   /* Check that the pointer pOld points to a valid, non-free block. */
 17904   assert( iBlock>=0 && iBlock<mem5.nBlock );
 17905   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
 17906   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
 17908   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
 17909   size = 1<<iLogsize;
 17910   assert( iBlock+size-1<(u32)mem5.nBlock );
 17912   mem5.aCtrl[iBlock] |= CTRL_FREE;
 17913   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
 17914   assert( mem5.currentCount>0 );
 17915   assert( mem5.currentOut>=(size*mem5.szAtom) );
 17916   mem5.currentCount--;
 17917   mem5.currentOut -= size*mem5.szAtom;
 17918   assert( mem5.currentOut>0 || mem5.currentCount==0 );
 17919   assert( mem5.currentCount>0 || mem5.currentOut==0 );
 17921   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 17922   while( ALWAYS(iLogsize<LOGMAX) ){
 17923     int iBuddy;
 17924     if( (iBlock>>iLogsize) & 1 ){
 17925       iBuddy = iBlock - size;
 17926     }else{
 17927       iBuddy = iBlock + size;
 17929     assert( iBuddy>=0 );
 17930     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
 17931     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
 17932     memsys5Unlink(iBuddy, iLogsize);
 17933     iLogsize++;
 17934     if( iBuddy<iBlock ){
 17935       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
 17936       mem5.aCtrl[iBlock] = 0;
 17937       iBlock = iBuddy;
 17938     }else{
 17939       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
 17940       mem5.aCtrl[iBuddy] = 0;
 17942     size *= 2;
 17945 #ifdef SQLITE_DEBUG
 17946   /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
 17947   ** not used after being freed */
 17948   memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
 17949 #endif
 17951   memsys5Link(iBlock, iLogsize);
 17954 /*
 17955 ** Allocate nBytes of memory.
 17956 */
 17957 static void *memsys5Malloc(int nBytes){
 17958   sqlite3_int64 *p = 0;
 17959   if( nBytes>0 ){
 17960     memsys5Enter();
 17961     p = memsys5MallocUnsafe(nBytes);
 17962     memsys5Leave();
 17964   return (void*)p; 
 17967 /*
 17968 ** Free memory.
 17969 **
 17970 ** The outer layer memory allocator prevents this routine from
 17971 ** being called with pPrior==0.
 17972 */
 17973 static void memsys5Free(void *pPrior){
 17974   assert( pPrior!=0 );
 17975   memsys5Enter();
 17976   memsys5FreeUnsafe(pPrior);
 17977   memsys5Leave();  
 17980 /*
 17981 ** Change the size of an existing memory allocation.
 17982 **
 17983 ** The outer layer memory allocator prevents this routine from
 17984 ** being called with pPrior==0.  
 17985 **
 17986 ** nBytes is always a value obtained from a prior call to
 17987 ** memsys5Round().  Hence nBytes is always a non-negative power
 17988 ** of two.  If nBytes==0 that means that an oversize allocation
 17989 ** (an allocation larger than 0x40000000) was requested and this
 17990 ** routine should return 0 without freeing pPrior.
 17991 */
 17992 static void *memsys5Realloc(void *pPrior, int nBytes){
 17993   int nOld;
 17994   void *p;
 17995   assert( pPrior!=0 );
 17996   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
 17997   assert( nBytes>=0 );
 17998   if( nBytes==0 ){
 17999     return 0;
 18001   nOld = memsys5Size(pPrior);
 18002   if( nBytes<=nOld ){
 18003     return pPrior;
 18005   memsys5Enter();
 18006   p = memsys5MallocUnsafe(nBytes);
 18007   if( p ){
 18008     memcpy(p, pPrior, nOld);
 18009     memsys5FreeUnsafe(pPrior);
 18011   memsys5Leave();
 18012   return p;
 18015 /*
 18016 ** Round up a request size to the next valid allocation size.  If
 18017 ** the allocation is too large to be handled by this allocation system,
 18018 ** return 0.
 18019 **
 18020 ** All allocations must be a power of two and must be expressed by a
 18021 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
 18022 ** or 1073741824 bytes.
 18023 */
 18024 static int memsys5Roundup(int n){
 18025   int iFullSz;
 18026   if( n > 0x40000000 ) return 0;
 18027   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
 18028   return iFullSz;
 18031 /*
 18032 ** Return the ceiling of the logarithm base 2 of iValue.
 18033 **
 18034 ** Examples:   memsys5Log(1) -> 0
 18035 **             memsys5Log(2) -> 1
 18036 **             memsys5Log(4) -> 2
 18037 **             memsys5Log(5) -> 3
 18038 **             memsys5Log(8) -> 3
 18039 **             memsys5Log(9) -> 4
 18040 */
 18041 static int memsys5Log(int iValue){
 18042   int iLog;
 18043   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
 18044   return iLog;
 18047 /*
 18048 ** Initialize the memory allocator.
 18049 **
 18050 ** This routine is not threadsafe.  The caller must be holding a mutex
 18051 ** to prevent multiple threads from entering at the same time.
 18052 */
 18053 static int memsys5Init(void *NotUsed){
 18054   int ii;            /* Loop counter */
 18055   int nByte;         /* Number of bytes of memory available to this allocator */
 18056   u8 *zByte;         /* Memory usable by this allocator */
 18057   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
 18058   int iOffset;       /* An offset into mem5.aCtrl[] */
 18060   UNUSED_PARAMETER(NotUsed);
 18062   /* For the purposes of this routine, disable the mutex */
 18063   mem5.mutex = 0;
 18065   /* The size of a Mem5Link object must be a power of two.  Verify that
 18066   ** this is case.
 18067   */
 18068   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
 18070   nByte = sqlite3GlobalConfig.nHeap;
 18071   zByte = (u8*)sqlite3GlobalConfig.pHeap;
 18072   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
 18074   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
 18075   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
 18076   mem5.szAtom = (1<<nMinLog);
 18077   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
 18078     mem5.szAtom = mem5.szAtom << 1;
 18081   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
 18082   mem5.zPool = zByte;
 18083   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
 18085   for(ii=0; ii<=LOGMAX; ii++){
 18086     mem5.aiFreelist[ii] = -1;
 18089   iOffset = 0;
 18090   for(ii=LOGMAX; ii>=0; ii--){
 18091     int nAlloc = (1<<ii);
 18092     if( (iOffset+nAlloc)<=mem5.nBlock ){
 18093       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
 18094       memsys5Link(iOffset, ii);
 18095       iOffset += nAlloc;
 18097     assert((iOffset+nAlloc)>mem5.nBlock);
 18100   /* If a mutex is required for normal operation, allocate one */
 18101   if( sqlite3GlobalConfig.bMemstat==0 ){
 18102     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 18105   return SQLITE_OK;
 18108 /*
 18109 ** Deinitialize this module.
 18110 */
 18111 static void memsys5Shutdown(void *NotUsed){
 18112   UNUSED_PARAMETER(NotUsed);
 18113   mem5.mutex = 0;
 18114   return;
 18117 #ifdef SQLITE_TEST
 18118 /*
 18119 ** Open the file indicated and write a log of all unfreed memory 
 18120 ** allocations into that log.
 18121 */
 18122 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
 18123   FILE *out;
 18124   int i, j, n;
 18125   int nMinLog;
 18127   if( zFilename==0 || zFilename[0]==0 ){
 18128     out = stdout;
 18129   }else{
 18130     out = fopen(zFilename, "w");
 18131     if( out==0 ){
 18132       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
 18133                       zFilename);
 18134       return;
 18137   memsys5Enter();
 18138   nMinLog = memsys5Log(mem5.szAtom);
 18139   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
 18140     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
 18141     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
 18143   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
 18144   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
 18145   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
 18146   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
 18147   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
 18148   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
 18149   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
 18150   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
 18151   memsys5Leave();
 18152   if( out==stdout ){
 18153     fflush(stdout);
 18154   }else{
 18155     fclose(out);
 18158 #endif
 18160 /*
 18161 ** This routine is the only routine in this file with external 
 18162 ** linkage. It returns a pointer to a static sqlite3_mem_methods
 18163 ** struct populated with the memsys5 methods.
 18164 */
 18165 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
 18166   static const sqlite3_mem_methods memsys5Methods = {
 18167      memsys5Malloc,
 18168      memsys5Free,
 18169      memsys5Realloc,
 18170      memsys5Size,
 18171      memsys5Roundup,
 18172      memsys5Init,
 18173      memsys5Shutdown,
 18175   };
 18176   return &memsys5Methods;
 18179 #endif /* SQLITE_ENABLE_MEMSYS5 */
 18181 /************** End of mem5.c ************************************************/
 18182 /************** Begin file mutex.c *******************************************/
 18183 /*
 18184 ** 2007 August 14
 18185 **
 18186 ** The author disclaims copyright to this source code.  In place of
 18187 ** a legal notice, here is a blessing:
 18188 **
 18189 **    May you do good and not evil.
 18190 **    May you find forgiveness for yourself and forgive others.
 18191 **    May you share freely, never taking more than you give.
 18192 **
 18193 *************************************************************************
 18194 ** This file contains the C functions that implement mutexes.
 18195 **
 18196 ** This file contains code that is common across all mutex implementations.
 18197 */
 18199 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
 18200 /*
 18201 ** For debugging purposes, record when the mutex subsystem is initialized
 18202 ** and uninitialized so that we can assert() if there is an attempt to
 18203 ** allocate a mutex while the system is uninitialized.
 18204 */
 18205 static SQLITE_WSD int mutexIsInit = 0;
 18206 #endif /* SQLITE_DEBUG */
 18209 #ifndef SQLITE_MUTEX_OMIT
 18210 /*
 18211 ** Initialize the mutex system.
 18212 */
 18213 SQLITE_PRIVATE int sqlite3MutexInit(void){ 
 18214   int rc = SQLITE_OK;
 18215   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
 18216     /* If the xMutexAlloc method has not been set, then the user did not
 18217     ** install a mutex implementation via sqlite3_config() prior to 
 18218     ** sqlite3_initialize() being called. This block copies pointers to
 18219     ** the default implementation into the sqlite3GlobalConfig structure.
 18220     */
 18221     sqlite3_mutex_methods const *pFrom;
 18222     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 18224     if( sqlite3GlobalConfig.bCoreMutex ){
 18225       pFrom = sqlite3DefaultMutex();
 18226     }else{
 18227       pFrom = sqlite3NoopMutex();
 18229     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
 18230     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
 18231            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
 18232     pTo->xMutexAlloc = pFrom->xMutexAlloc;
 18234   rc = sqlite3GlobalConfig.mutex.xMutexInit();
 18236 #ifdef SQLITE_DEBUG
 18237   GLOBAL(int, mutexIsInit) = 1;
 18238 #endif
 18240   return rc;
 18243 /*
 18244 ** Shutdown the mutex system. This call frees resources allocated by
 18245 ** sqlite3MutexInit().
 18246 */
 18247 SQLITE_PRIVATE int sqlite3MutexEnd(void){
 18248   int rc = SQLITE_OK;
 18249   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
 18250     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
 18253 #ifdef SQLITE_DEBUG
 18254   GLOBAL(int, mutexIsInit) = 0;
 18255 #endif
 18257   return rc;
 18260 /*
 18261 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
 18262 */
 18263 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
 18264 #ifndef SQLITE_OMIT_AUTOINIT
 18265   if( sqlite3_initialize() ) return 0;
 18266 #endif
 18267   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 18270 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
 18271   if( !sqlite3GlobalConfig.bCoreMutex ){
 18272     return 0;
 18274   assert( GLOBAL(int, mutexIsInit) );
 18275   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
 18278 /*
 18279 ** Free a dynamic mutex.
 18280 */
 18281 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
 18282   if( p ){
 18283     sqlite3GlobalConfig.mutex.xMutexFree(p);
 18287 /*
 18288 ** Obtain the mutex p. If some other thread already has the mutex, block
 18289 ** until it can be obtained.
 18290 */
 18291 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
 18292   if( p ){
 18293     sqlite3GlobalConfig.mutex.xMutexEnter(p);
 18297 /*
 18298 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
 18299 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
 18300 */
 18301 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
 18302   int rc = SQLITE_OK;
 18303   if( p ){
 18304     return sqlite3GlobalConfig.mutex.xMutexTry(p);
 18306   return rc;
 18309 /*
 18310 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
 18311 ** entered by the same thread.  The behavior is undefined if the mutex 
 18312 ** is not currently entered. If a NULL pointer is passed as an argument
 18313 ** this function is a no-op.
 18314 */
 18315 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
 18316   if( p ){
 18317     sqlite3GlobalConfig.mutex.xMutexLeave(p);
 18321 #ifndef NDEBUG
 18322 /*
 18323 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 18324 ** intended for use inside assert() statements.
 18325 */
 18326 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
 18327   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
 18329 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 18330   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 18332 #endif
 18334 #endif /* !defined(SQLITE_MUTEX_OMIT) */
 18336 /************** End of mutex.c ***********************************************/
 18337 /************** Begin file mutex_noop.c **************************************/
 18338 /*
 18339 ** 2008 October 07
 18340 **
 18341 ** The author disclaims copyright to this source code.  In place of
 18342 ** a legal notice, here is a blessing:
 18343 **
 18344 **    May you do good and not evil.
 18345 **    May you find forgiveness for yourself and forgive others.
 18346 **    May you share freely, never taking more than you give.
 18347 **
 18348 *************************************************************************
 18349 ** This file contains the C functions that implement mutexes.
 18350 **
 18351 ** This implementation in this file does not provide any mutual
 18352 ** exclusion and is thus suitable for use only in applications
 18353 ** that use SQLite in a single thread.  The routines defined
 18354 ** here are place-holders.  Applications can substitute working
 18355 ** mutex routines at start-time using the
 18356 **
 18357 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
 18358 **
 18359 ** interface.
 18360 **
 18361 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
 18362 ** that does error checking on mutexes to make sure they are being
 18363 ** called correctly.
 18364 */
 18366 #ifndef SQLITE_MUTEX_OMIT
 18368 #ifndef SQLITE_DEBUG
 18369 /*
 18370 ** Stub routines for all mutex methods.
 18371 **
 18372 ** This routines provide no mutual exclusion or error checking.
 18373 */
 18374 static int noopMutexInit(void){ return SQLITE_OK; }
 18375 static int noopMutexEnd(void){ return SQLITE_OK; }
 18376 static sqlite3_mutex *noopMutexAlloc(int id){ 
 18377   UNUSED_PARAMETER(id);
 18378   return (sqlite3_mutex*)8; 
 18380 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 18381 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 18382 static int noopMutexTry(sqlite3_mutex *p){
 18383   UNUSED_PARAMETER(p);
 18384   return SQLITE_OK;
 18386 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
 18388 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 18389   static const sqlite3_mutex_methods sMutex = {
 18390     noopMutexInit,
 18391     noopMutexEnd,
 18392     noopMutexAlloc,
 18393     noopMutexFree,
 18394     noopMutexEnter,
 18395     noopMutexTry,
 18396     noopMutexLeave,
 18398     0,
 18399     0,
 18400   };
 18402   return &sMutex;
 18404 #endif /* !SQLITE_DEBUG */
 18406 #ifdef SQLITE_DEBUG
 18407 /*
 18408 ** In this implementation, error checking is provided for testing
 18409 ** and debugging purposes.  The mutexes still do not provide any
 18410 ** mutual exclusion.
 18411 */
 18413 /*
 18414 ** The mutex object
 18415 */
 18416 typedef struct sqlite3_debug_mutex {
 18417   int id;     /* The mutex type */
 18418   int cnt;    /* Number of entries without a matching leave */
 18419 } sqlite3_debug_mutex;
 18421 /*
 18422 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 18423 ** intended for use inside assert() statements.
 18424 */
 18425 static int debugMutexHeld(sqlite3_mutex *pX){
 18426   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18427   return p==0 || p->cnt>0;
 18429 static int debugMutexNotheld(sqlite3_mutex *pX){
 18430   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18431   return p==0 || p->cnt==0;
 18434 /*
 18435 ** Initialize and deinitialize the mutex subsystem.
 18436 */
 18437 static int debugMutexInit(void){ return SQLITE_OK; }
 18438 static int debugMutexEnd(void){ return SQLITE_OK; }
 18440 /*
 18441 ** The sqlite3_mutex_alloc() routine allocates a new
 18442 ** mutex and returns a pointer to it.  If it returns NULL
 18443 ** that means that a mutex could not be allocated. 
 18444 */
 18445 static sqlite3_mutex *debugMutexAlloc(int id){
 18446   static sqlite3_debug_mutex aStatic[6];
 18447   sqlite3_debug_mutex *pNew = 0;
 18448   switch( id ){
 18449     case SQLITE_MUTEX_FAST:
 18450     case SQLITE_MUTEX_RECURSIVE: {
 18451       pNew = sqlite3Malloc(sizeof(*pNew));
 18452       if( pNew ){
 18453         pNew->id = id;
 18454         pNew->cnt = 0;
 18456       break;
 18458     default: {
 18459       assert( id-2 >= 0 );
 18460       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
 18461       pNew = &aStatic[id-2];
 18462       pNew->id = id;
 18463       break;
 18466   return (sqlite3_mutex*)pNew;
 18469 /*
 18470 ** This routine deallocates a previously allocated mutex.
 18471 */
 18472 static void debugMutexFree(sqlite3_mutex *pX){
 18473   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18474   assert( p->cnt==0 );
 18475   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 18476   sqlite3_free(p);
 18479 /*
 18480 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 18481 ** to enter a mutex.  If another thread is already within the mutex,
 18482 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 18483 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 18484 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 18485 ** be entered multiple times by the same thread.  In such cases the,
 18486 ** mutex must be exited an equal number of times before another thread
 18487 ** can enter.  If the same thread tries to enter any other kind of mutex
 18488 ** more than once, the behavior is undefined.
 18489 */
 18490 static void debugMutexEnter(sqlite3_mutex *pX){
 18491   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18492   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 18493   p->cnt++;
 18495 static int debugMutexTry(sqlite3_mutex *pX){
 18496   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18497   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 18498   p->cnt++;
 18499   return SQLITE_OK;
 18502 /*
 18503 ** The sqlite3_mutex_leave() routine exits a mutex that was
 18504 ** previously entered by the same thread.  The behavior
 18505 ** is undefined if the mutex is not currently entered or
 18506 ** is not currently allocated.  SQLite will never do either.
 18507 */
 18508 static void debugMutexLeave(sqlite3_mutex *pX){
 18509   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
 18510   assert( debugMutexHeld(pX) );
 18511   p->cnt--;
 18512   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
 18515 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
 18516   static const sqlite3_mutex_methods sMutex = {
 18517     debugMutexInit,
 18518     debugMutexEnd,
 18519     debugMutexAlloc,
 18520     debugMutexFree,
 18521     debugMutexEnter,
 18522     debugMutexTry,
 18523     debugMutexLeave,
 18525     debugMutexHeld,
 18526     debugMutexNotheld
 18527   };
 18529   return &sMutex;
 18531 #endif /* SQLITE_DEBUG */
 18533 /*
 18534 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
 18535 ** is used regardless of the run-time threadsafety setting.
 18536 */
 18537 #ifdef SQLITE_MUTEX_NOOP
 18538 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 18539   return sqlite3NoopMutex();
 18541 #endif /* defined(SQLITE_MUTEX_NOOP) */
 18542 #endif /* !defined(SQLITE_MUTEX_OMIT) */
 18544 /************** End of mutex_noop.c ******************************************/
 18545 /************** Begin file mutex_unix.c **************************************/
 18546 /*
 18547 ** 2007 August 28
 18548 **
 18549 ** The author disclaims copyright to this source code.  In place of
 18550 ** a legal notice, here is a blessing:
 18551 **
 18552 **    May you do good and not evil.
 18553 **    May you find forgiveness for yourself and forgive others.
 18554 **    May you share freely, never taking more than you give.
 18555 **
 18556 *************************************************************************
 18557 ** This file contains the C functions that implement mutexes for pthreads
 18558 */
 18560 /*
 18561 ** The code in this file is only used if we are compiling threadsafe
 18562 ** under unix with pthreads.
 18563 **
 18564 ** Note that this implementation requires a version of pthreads that
 18565 ** supports recursive mutexes.
 18566 */
 18567 #ifdef SQLITE_MUTEX_PTHREADS
 18569 #include <pthread.h>
 18571 /*
 18572 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
 18573 ** are necessary under two condidtions:  (1) Debug builds and (2) using
 18574 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
 18575 */
 18576 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
 18577 # define SQLITE_MUTEX_NREF 1
 18578 #else
 18579 # define SQLITE_MUTEX_NREF 0
 18580 #endif
 18582 /*
 18583 ** Each recursive mutex is an instance of the following structure.
 18584 */
 18585 struct sqlite3_mutex {
 18586   pthread_mutex_t mutex;     /* Mutex controlling the lock */
 18587 #if SQLITE_MUTEX_NREF
 18588   int id;                    /* Mutex type */
 18589   volatile int nRef;         /* Number of entrances */
 18590   volatile pthread_t owner;  /* Thread that is within this mutex */
 18591   int trace;                 /* True to trace changes */
 18592 #endif
 18593 };
 18594 #if SQLITE_MUTEX_NREF
 18595 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
 18596 #else
 18597 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
 18598 #endif
 18600 /*
 18601 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 18602 ** intended for use only inside assert() statements.  On some platforms,
 18603 ** there might be race conditions that can cause these routines to
 18604 ** deliver incorrect results.  In particular, if pthread_equal() is
 18605 ** not an atomic operation, then these routines might delivery
 18606 ** incorrect results.  On most platforms, pthread_equal() is a 
 18607 ** comparison of two integers and is therefore atomic.  But we are
 18608 ** told that HPUX is not such a platform.  If so, then these routines
 18609 ** will not always work correctly on HPUX.
 18610 **
 18611 ** On those platforms where pthread_equal() is not atomic, SQLite
 18612 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
 18613 ** make sure no assert() statements are evaluated and hence these
 18614 ** routines are never called.
 18615 */
 18616 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
 18617 static int pthreadMutexHeld(sqlite3_mutex *p){
 18618   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
 18620 static int pthreadMutexNotheld(sqlite3_mutex *p){
 18621   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
 18623 #endif
 18625 /*
 18626 ** Initialize and deinitialize the mutex subsystem.
 18627 */
 18628 static int pthreadMutexInit(void){ return SQLITE_OK; }
 18629 static int pthreadMutexEnd(void){ return SQLITE_OK; }
 18631 /*
 18632 ** The sqlite3_mutex_alloc() routine allocates a new
 18633 ** mutex and returns a pointer to it.  If it returns NULL
 18634 ** that means that a mutex could not be allocated.  SQLite
 18635 ** will unwind its stack and return an error.  The argument
 18636 ** to sqlite3_mutex_alloc() is one of these integer constants:
 18637 **
 18638 ** <ul>
 18639 ** <li>  SQLITE_MUTEX_FAST
 18640 ** <li>  SQLITE_MUTEX_RECURSIVE
 18641 ** <li>  SQLITE_MUTEX_STATIC_MASTER
 18642 ** <li>  SQLITE_MUTEX_STATIC_MEM
 18643 ** <li>  SQLITE_MUTEX_STATIC_MEM2
 18644 ** <li>  SQLITE_MUTEX_STATIC_PRNG
 18645 ** <li>  SQLITE_MUTEX_STATIC_LRU
 18646 ** <li>  SQLITE_MUTEX_STATIC_PMEM
 18647 ** </ul>
 18648 **
 18649 ** The first two constants cause sqlite3_mutex_alloc() to create
 18650 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 18651 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 18652 ** The mutex implementation does not need to make a distinction
 18653 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 18654 ** not want to.  But SQLite will only request a recursive mutex in
 18655 ** cases where it really needs one.  If a faster non-recursive mutex
 18656 ** implementation is available on the host platform, the mutex subsystem
 18657 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
 18658 **
 18659 ** The other allowed parameters to sqlite3_mutex_alloc() each return
 18660 ** a pointer to a static preexisting mutex.  Six static mutexes are
 18661 ** used by the current version of SQLite.  Future versions of SQLite
 18662 ** may add additional static mutexes.  Static mutexes are for internal
 18663 ** use by SQLite only.  Applications that use SQLite mutexes should
 18664 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 18665 ** SQLITE_MUTEX_RECURSIVE.
 18666 **
 18667 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 18668 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 18669 ** returns a different mutex on every call.  But for the static 
 18670 ** mutex types, the same mutex is returned on every call that has
 18671 ** the same type number.
 18672 */
 18673 static sqlite3_mutex *pthreadMutexAlloc(int iType){
 18674   static sqlite3_mutex staticMutexes[] = {
 18675     SQLITE3_MUTEX_INITIALIZER,
 18676     SQLITE3_MUTEX_INITIALIZER,
 18677     SQLITE3_MUTEX_INITIALIZER,
 18678     SQLITE3_MUTEX_INITIALIZER,
 18679     SQLITE3_MUTEX_INITIALIZER,
 18680     SQLITE3_MUTEX_INITIALIZER
 18681   };
 18682   sqlite3_mutex *p;
 18683   switch( iType ){
 18684     case SQLITE_MUTEX_RECURSIVE: {
 18685       p = sqlite3MallocZero( sizeof(*p) );
 18686       if( p ){
 18687 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18688         /* If recursive mutexes are not available, we will have to
 18689         ** build our own.  See below. */
 18690         pthread_mutex_init(&p->mutex, 0);
 18691 #else
 18692         /* Use a recursive mutex if it is available */
 18693         pthread_mutexattr_t recursiveAttr;
 18694         pthread_mutexattr_init(&recursiveAttr);
 18695         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 18696         pthread_mutex_init(&p->mutex, &recursiveAttr);
 18697         pthread_mutexattr_destroy(&recursiveAttr);
 18698 #endif
 18699 #if SQLITE_MUTEX_NREF
 18700         p->id = iType;
 18701 #endif
 18703       break;
 18705     case SQLITE_MUTEX_FAST: {
 18706       p = sqlite3MallocZero( sizeof(*p) );
 18707       if( p ){
 18708 #if SQLITE_MUTEX_NREF
 18709         p->id = iType;
 18710 #endif
 18711         pthread_mutex_init(&p->mutex, 0);
 18713       break;
 18715     default: {
 18716       assert( iType-2 >= 0 );
 18717       assert( iType-2 < ArraySize(staticMutexes) );
 18718       p = &staticMutexes[iType-2];
 18719 #if SQLITE_MUTEX_NREF
 18720       p->id = iType;
 18721 #endif
 18722       break;
 18725   return p;
 18729 /*
 18730 ** This routine deallocates a previously
 18731 ** allocated mutex.  SQLite is careful to deallocate every
 18732 ** mutex that it allocates.
 18733 */
 18734 static void pthreadMutexFree(sqlite3_mutex *p){
 18735   assert( p->nRef==0 );
 18736   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 18737   pthread_mutex_destroy(&p->mutex);
 18738   sqlite3_free(p);
 18741 /*
 18742 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 18743 ** to enter a mutex.  If another thread is already within the mutex,
 18744 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 18745 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 18746 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 18747 ** be entered multiple times by the same thread.  In such cases the,
 18748 ** mutex must be exited an equal number of times before another thread
 18749 ** can enter.  If the same thread tries to enter any other kind of mutex
 18750 ** more than once, the behavior is undefined.
 18751 */
 18752 static void pthreadMutexEnter(sqlite3_mutex *p){
 18753   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 18755 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18756   /* If recursive mutexes are not available, then we have to grow
 18757   ** our own.  This implementation assumes that pthread_equal()
 18758   ** is atomic - that it cannot be deceived into thinking self
 18759   ** and p->owner are equal if p->owner changes between two values
 18760   ** that are not equal to self while the comparison is taking place.
 18761   ** This implementation also assumes a coherent cache - that 
 18762   ** separate processes cannot read different values from the same
 18763   ** address at the same time.  If either of these two conditions
 18764   ** are not met, then the mutexes will fail and problems will result.
 18765   */
 18767     pthread_t self = pthread_self();
 18768     if( p->nRef>0 && pthread_equal(p->owner, self) ){
 18769       p->nRef++;
 18770     }else{
 18771       pthread_mutex_lock(&p->mutex);
 18772       assert( p->nRef==0 );
 18773       p->owner = self;
 18774       p->nRef = 1;
 18777 #else
 18778   /* Use the built-in recursive mutexes if they are available.
 18779   */
 18780   pthread_mutex_lock(&p->mutex);
 18781 #if SQLITE_MUTEX_NREF
 18782   assert( p->nRef>0 || p->owner==0 );
 18783   p->owner = pthread_self();
 18784   p->nRef++;
 18785 #endif
 18786 #endif
 18788 #ifdef SQLITE_DEBUG
 18789   if( p->trace ){
 18790     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18792 #endif
 18794 static int pthreadMutexTry(sqlite3_mutex *p){
 18795   int rc;
 18796   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 18798 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18799   /* If recursive mutexes are not available, then we have to grow
 18800   ** our own.  This implementation assumes that pthread_equal()
 18801   ** is atomic - that it cannot be deceived into thinking self
 18802   ** and p->owner are equal if p->owner changes between two values
 18803   ** that are not equal to self while the comparison is taking place.
 18804   ** This implementation also assumes a coherent cache - that 
 18805   ** separate processes cannot read different values from the same
 18806   ** address at the same time.  If either of these two conditions
 18807   ** are not met, then the mutexes will fail and problems will result.
 18808   */
 18810     pthread_t self = pthread_self();
 18811     if( p->nRef>0 && pthread_equal(p->owner, self) ){
 18812       p->nRef++;
 18813       rc = SQLITE_OK;
 18814     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
 18815       assert( p->nRef==0 );
 18816       p->owner = self;
 18817       p->nRef = 1;
 18818       rc = SQLITE_OK;
 18819     }else{
 18820       rc = SQLITE_BUSY;
 18823 #else
 18824   /* Use the built-in recursive mutexes if they are available.
 18825   */
 18826   if( pthread_mutex_trylock(&p->mutex)==0 ){
 18827 #if SQLITE_MUTEX_NREF
 18828     p->owner = pthread_self();
 18829     p->nRef++;
 18830 #endif
 18831     rc = SQLITE_OK;
 18832   }else{
 18833     rc = SQLITE_BUSY;
 18835 #endif
 18837 #ifdef SQLITE_DEBUG
 18838   if( rc==SQLITE_OK && p->trace ){
 18839     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18841 #endif
 18842   return rc;
 18845 /*
 18846 ** The sqlite3_mutex_leave() routine exits a mutex that was
 18847 ** previously entered by the same thread.  The behavior
 18848 ** is undefined if the mutex is not currently entered or
 18849 ** is not currently allocated.  SQLite will never do either.
 18850 */
 18851 static void pthreadMutexLeave(sqlite3_mutex *p){
 18852   assert( pthreadMutexHeld(p) );
 18853 #if SQLITE_MUTEX_NREF
 18854   p->nRef--;
 18855   if( p->nRef==0 ) p->owner = 0;
 18856 #endif
 18857   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 18859 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 18860   if( p->nRef==0 ){
 18861     pthread_mutex_unlock(&p->mutex);
 18863 #else
 18864   pthread_mutex_unlock(&p->mutex);
 18865 #endif
 18867 #ifdef SQLITE_DEBUG
 18868   if( p->trace ){
 18869     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 18871 #endif
 18874 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 18875   static const sqlite3_mutex_methods sMutex = {
 18876     pthreadMutexInit,
 18877     pthreadMutexEnd,
 18878     pthreadMutexAlloc,
 18879     pthreadMutexFree,
 18880     pthreadMutexEnter,
 18881     pthreadMutexTry,
 18882     pthreadMutexLeave,
 18883 #ifdef SQLITE_DEBUG
 18884     pthreadMutexHeld,
 18885     pthreadMutexNotheld
 18886 #else
 18887     0,
 18889 #endif
 18890   };
 18892   return &sMutex;
 18895 #endif /* SQLITE_MUTEX_PTHREADS */
 18897 /************** End of mutex_unix.c ******************************************/
 18898 /************** Begin file mutex_w32.c ***************************************/
 18899 /*
 18900 ** 2007 August 14
 18901 **
 18902 ** The author disclaims copyright to this source code.  In place of
 18903 ** a legal notice, here is a blessing:
 18904 **
 18905 **    May you do good and not evil.
 18906 **    May you find forgiveness for yourself and forgive others.
 18907 **    May you share freely, never taking more than you give.
 18908 **
 18909 *************************************************************************
 18910 ** This file contains the C functions that implement mutexes for win32
 18911 */
 18913 /*
 18914 ** The code in this file is only used if we are compiling multithreaded
 18915 ** on a win32 system.
 18916 */
 18917 #ifdef SQLITE_MUTEX_W32
 18919 /*
 18920 ** Each recursive mutex is an instance of the following structure.
 18921 */
 18922 struct sqlite3_mutex {
 18923   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 18924   int id;                    /* Mutex type */
 18925 #ifdef SQLITE_DEBUG
 18926   volatile int nRef;         /* Number of enterances */
 18927   volatile DWORD owner;      /* Thread holding this mutex */
 18928   int trace;                 /* True to trace changes */
 18929 #endif
 18930 };
 18931 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 18932 #ifdef SQLITE_DEBUG
 18933 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
 18934 #else
 18935 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
 18936 #endif
 18938 /*
 18939 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 18940 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 18941 **
 18942 ** Here is an interesting observation:  Win95, Win98, and WinME lack
 18943 ** the LockFileEx() API.  But we can still statically link against that
 18944 ** API as long as we don't call it win running Win95/98/ME.  A call to
 18945 ** this routine is used to determine if the host is Win95/98/ME or
 18946 ** WinNT/2K/XP so that we will know whether or not we can safely call
 18947 ** the LockFileEx() API.
 18948 **
 18949 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
 18950 ** which is only available if your application was compiled with 
 18951 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
 18952 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
 18953 ** this out as well.
 18954 */
 18955 #if 0
 18956 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 18957 # define mutexIsNT()  (1)
 18958 #else
 18959   static int mutexIsNT(void){
 18960     static int osType = 0;
 18961     if( osType==0 ){
 18962       OSVERSIONINFO sInfo;
 18963       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 18964       GetVersionEx(&sInfo);
 18965       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 18967     return osType==2;
 18969 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
 18970 #endif
 18972 #ifdef SQLITE_DEBUG
 18973 /*
 18974 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 18975 ** intended for use only inside assert() statements.
 18976 */
 18977 static int winMutexHeld(sqlite3_mutex *p){
 18978   return p->nRef!=0 && p->owner==GetCurrentThreadId();
 18980 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
 18981   return p->nRef==0 || p->owner!=tid;
 18983 static int winMutexNotheld(sqlite3_mutex *p){
 18984   DWORD tid = GetCurrentThreadId(); 
 18985   return winMutexNotheld2(p, tid);
 18987 #endif
 18990 /*
 18991 ** Initialize and deinitialize the mutex subsystem.
 18992 */
 18993 static sqlite3_mutex winMutex_staticMutexes[6] = {
 18994   SQLITE3_MUTEX_INITIALIZER,
 18995   SQLITE3_MUTEX_INITIALIZER,
 18996   SQLITE3_MUTEX_INITIALIZER,
 18997   SQLITE3_MUTEX_INITIALIZER,
 18998   SQLITE3_MUTEX_INITIALIZER,
 18999   SQLITE3_MUTEX_INITIALIZER
 19000 };
 19001 static int winMutex_isInit = 0;
 19002 /* As winMutexInit() and winMutexEnd() are called as part
 19003 ** of the sqlite3_initialize and sqlite3_shutdown()
 19004 ** processing, the "interlocked" magic is probably not
 19005 ** strictly necessary.
 19006 */
 19007 static LONG winMutex_lock = 0;
 19009 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
 19011 static int winMutexInit(void){ 
 19012   /* The first to increment to 1 does actual initialization */
 19013   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
 19014     int i;
 19015     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 19016 #if SQLITE_OS_WINRT
 19017       InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
 19018 #else
 19019       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
 19020 #endif
 19022     winMutex_isInit = 1;
 19023   }else{
 19024     /* Someone else is in the process of initing the static mutexes */
 19025     while( !winMutex_isInit ){
 19026       sqlite3_win32_sleep(1);
 19029   return SQLITE_OK; 
 19032 static int winMutexEnd(void){ 
 19033   /* The first to decrement to 0 does actual shutdown 
 19034   ** (which should be the last to shutdown.) */
 19035   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
 19036     if( winMutex_isInit==1 ){
 19037       int i;
 19038       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 19039         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
 19041       winMutex_isInit = 0;
 19044   return SQLITE_OK; 
 19047 /*
 19048 ** The sqlite3_mutex_alloc() routine allocates a new
 19049 ** mutex and returns a pointer to it.  If it returns NULL
 19050 ** that means that a mutex could not be allocated.  SQLite
 19051 ** will unwind its stack and return an error.  The argument
 19052 ** to sqlite3_mutex_alloc() is one of these integer constants:
 19053 **
 19054 ** <ul>
 19055 ** <li>  SQLITE_MUTEX_FAST
 19056 ** <li>  SQLITE_MUTEX_RECURSIVE
 19057 ** <li>  SQLITE_MUTEX_STATIC_MASTER
 19058 ** <li>  SQLITE_MUTEX_STATIC_MEM
 19059 ** <li>  SQLITE_MUTEX_STATIC_MEM2
 19060 ** <li>  SQLITE_MUTEX_STATIC_PRNG
 19061 ** <li>  SQLITE_MUTEX_STATIC_LRU
 19062 ** <li>  SQLITE_MUTEX_STATIC_PMEM
 19063 ** </ul>
 19064 **
 19065 ** The first two constants cause sqlite3_mutex_alloc() to create
 19066 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 19067 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 19068 ** The mutex implementation does not need to make a distinction
 19069 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 19070 ** not want to.  But SQLite will only request a recursive mutex in
 19071 ** cases where it really needs one.  If a faster non-recursive mutex
 19072 ** implementation is available on the host platform, the mutex subsystem
 19073 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
 19074 **
 19075 ** The other allowed parameters to sqlite3_mutex_alloc() each return
 19076 ** a pointer to a static preexisting mutex.  Six static mutexes are
 19077 ** used by the current version of SQLite.  Future versions of SQLite
 19078 ** may add additional static mutexes.  Static mutexes are for internal
 19079 ** use by SQLite only.  Applications that use SQLite mutexes should
 19080 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 19081 ** SQLITE_MUTEX_RECURSIVE.
 19082 **
 19083 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 19084 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 19085 ** returns a different mutex on every call.  But for the static 
 19086 ** mutex types, the same mutex is returned on every call that has
 19087 ** the same type number.
 19088 */
 19089 static sqlite3_mutex *winMutexAlloc(int iType){
 19090   sqlite3_mutex *p;
 19092   switch( iType ){
 19093     case SQLITE_MUTEX_FAST:
 19094     case SQLITE_MUTEX_RECURSIVE: {
 19095       p = sqlite3MallocZero( sizeof(*p) );
 19096       if( p ){  
 19097 #ifdef SQLITE_DEBUG
 19098         p->id = iType;
 19099 #endif
 19100 #if SQLITE_OS_WINRT
 19101         InitializeCriticalSectionEx(&p->mutex, 0, 0);
 19102 #else
 19103         InitializeCriticalSection(&p->mutex);
 19104 #endif
 19106       break;
 19108     default: {
 19109       assert( winMutex_isInit==1 );
 19110       assert( iType-2 >= 0 );
 19111       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
 19112       p = &winMutex_staticMutexes[iType-2];
 19113 #ifdef SQLITE_DEBUG
 19114       p->id = iType;
 19115 #endif
 19116       break;
 19119   return p;
 19123 /*
 19124 ** This routine deallocates a previously
 19125 ** allocated mutex.  SQLite is careful to deallocate every
 19126 ** mutex that it allocates.
 19127 */
 19128 static void winMutexFree(sqlite3_mutex *p){
 19129   assert( p );
 19130   assert( p->nRef==0 && p->owner==0 );
 19131   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 19132   DeleteCriticalSection(&p->mutex);
 19133   sqlite3_free(p);
 19136 /*
 19137 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 19138 ** to enter a mutex.  If another thread is already within the mutex,
 19139 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 19140 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 19141 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 19142 ** be entered multiple times by the same thread.  In such cases the,
 19143 ** mutex must be exited an equal number of times before another thread
 19144 ** can enter.  If the same thread tries to enter any other kind of mutex
 19145 ** more than once, the behavior is undefined.
 19146 */
 19147 static void winMutexEnter(sqlite3_mutex *p){
 19148 #ifdef SQLITE_DEBUG
 19149   DWORD tid = GetCurrentThreadId(); 
 19150   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 19151 #endif
 19152   EnterCriticalSection(&p->mutex);
 19153 #ifdef SQLITE_DEBUG
 19154   assert( p->nRef>0 || p->owner==0 );
 19155   p->owner = tid; 
 19156   p->nRef++;
 19157   if( p->trace ){
 19158     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 19160 #endif
 19162 static int winMutexTry(sqlite3_mutex *p){
 19163 #ifndef NDEBUG
 19164   DWORD tid = GetCurrentThreadId(); 
 19165 #endif
 19166   int rc = SQLITE_BUSY;
 19167   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 19168   /*
 19169   ** The sqlite3_mutex_try() routine is very rarely used, and when it
 19170   ** is used it is merely an optimization.  So it is OK for it to always
 19171   ** fail.  
 19172   **
 19173   ** The TryEnterCriticalSection() interface is only available on WinNT.
 19174   ** And some windows compilers complain if you try to use it without
 19175   ** first doing some #defines that prevent SQLite from building on Win98.
 19176   ** For that reason, we will omit this optimization for now.  See
 19177   ** ticket #2685.
 19178   */
 19179 #if 0
 19180   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
 19181     p->owner = tid;
 19182     p->nRef++;
 19183     rc = SQLITE_OK;
 19185 #else
 19186   UNUSED_PARAMETER(p);
 19187 #endif
 19188 #ifdef SQLITE_DEBUG
 19189   if( rc==SQLITE_OK && p->trace ){
 19190     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 19192 #endif
 19193   return rc;
 19196 /*
 19197 ** The sqlite3_mutex_leave() routine exits a mutex that was
 19198 ** previously entered by the same thread.  The behavior
 19199 ** is undefined if the mutex is not currently entered or
 19200 ** is not currently allocated.  SQLite will never do either.
 19201 */
 19202 static void winMutexLeave(sqlite3_mutex *p){
 19203 #ifndef NDEBUG
 19204   DWORD tid = GetCurrentThreadId();
 19205   assert( p->nRef>0 );
 19206   assert( p->owner==tid );
 19207   p->nRef--;
 19208   if( p->nRef==0 ) p->owner = 0;
 19209   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 19210 #endif
 19211   LeaveCriticalSection(&p->mutex);
 19212 #ifdef SQLITE_DEBUG
 19213   if( p->trace ){
 19214     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 19216 #endif
 19219 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 19220   static const sqlite3_mutex_methods sMutex = {
 19221     winMutexInit,
 19222     winMutexEnd,
 19223     winMutexAlloc,
 19224     winMutexFree,
 19225     winMutexEnter,
 19226     winMutexTry,
 19227     winMutexLeave,
 19228 #ifdef SQLITE_DEBUG
 19229     winMutexHeld,
 19230     winMutexNotheld
 19231 #else
 19232     0,
 19234 #endif
 19235   };
 19237   return &sMutex;
 19239 #endif /* SQLITE_MUTEX_W32 */
 19241 /************** End of mutex_w32.c *******************************************/
 19242 /************** Begin file malloc.c ******************************************/
 19243 /*
 19244 ** 2001 September 15
 19245 **
 19246 ** The author disclaims copyright to this source code.  In place of
 19247 ** a legal notice, here is a blessing:
 19248 **
 19249 **    May you do good and not evil.
 19250 **    May you find forgiveness for yourself and forgive others.
 19251 **    May you share freely, never taking more than you give.
 19252 **
 19253 *************************************************************************
 19254 **
 19255 ** Memory allocation functions used throughout sqlite.
 19256 */
 19257 /* #include <stdarg.h> */
 19259 /*
 19260 ** Attempt to release up to n bytes of non-essential memory currently
 19261 ** held by SQLite. An example of non-essential memory is memory used to
 19262 ** cache database pages that are not currently in use.
 19263 */
 19264 SQLITE_API int sqlite3_release_memory(int n){
 19265 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 19266   return sqlite3PcacheReleaseMemory(n);
 19267 #else
 19268   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
 19269   ** is a no-op returning zero if SQLite is not compiled with
 19270   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
 19271   UNUSED_PARAMETER(n);
 19272   return 0;
 19273 #endif
 19276 /*
 19277 ** An instance of the following object records the location of
 19278 ** each unused scratch buffer.
 19279 */
 19280 typedef struct ScratchFreeslot {
 19281   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
 19282 } ScratchFreeslot;
 19284 /*
 19285 ** State information local to the memory allocation subsystem.
 19286 */
 19287 static SQLITE_WSD struct Mem0Global {
 19288   sqlite3_mutex *mutex;         /* Mutex to serialize access */
 19290   /*
 19291   ** The alarm callback and its arguments.  The mem0.mutex lock will
 19292   ** be held while the callback is running.  Recursive calls into
 19293   ** the memory subsystem are allowed, but no new callbacks will be
 19294   ** issued.
 19295   */
 19296   sqlite3_int64 alarmThreshold;
 19297   void (*alarmCallback)(void*, sqlite3_int64,int);
 19298   void *alarmArg;
 19300   /*
 19301   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
 19302   ** (so that a range test can be used to determine if an allocation
 19303   ** being freed came from pScratch) and a pointer to the list of
 19304   ** unused scratch allocations.
 19305   */
 19306   void *pScratchEnd;
 19307   ScratchFreeslot *pScratchFree;
 19308   u32 nScratchFree;
 19310   /*
 19311   ** True if heap is nearly "full" where "full" is defined by the
 19312   ** sqlite3_soft_heap_limit() setting.
 19313   */
 19314   int nearlyFull;
 19315 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
 19317 #define mem0 GLOBAL(struct Mem0Global, mem0)
 19319 /*
 19320 ** This routine runs when the memory allocator sees that the
 19321 ** total memory allocation is about to exceed the soft heap
 19322 ** limit.
 19323 */
 19324 static void softHeapLimitEnforcer(
 19325   void *NotUsed, 
 19326   sqlite3_int64 NotUsed2,
 19327   int allocSize
 19328 ){
 19329   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 19330   sqlite3_release_memory(allocSize);
 19333 /*
 19334 ** Change the alarm callback
 19335 */
 19336 static int sqlite3MemoryAlarm(
 19337   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 19338   void *pArg,
 19339   sqlite3_int64 iThreshold
 19340 ){
 19341   int nUsed;
 19342   sqlite3_mutex_enter(mem0.mutex);
 19343   mem0.alarmCallback = xCallback;
 19344   mem0.alarmArg = pArg;
 19345   mem0.alarmThreshold = iThreshold;
 19346   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 19347   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
 19348   sqlite3_mutex_leave(mem0.mutex);
 19349   return SQLITE_OK;
 19352 #ifndef SQLITE_OMIT_DEPRECATED
 19353 /*
 19354 ** Deprecated external interface.  Internal/core SQLite code
 19355 ** should call sqlite3MemoryAlarm.
 19356 */
 19357 SQLITE_API int sqlite3_memory_alarm(
 19358   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
 19359   void *pArg,
 19360   sqlite3_int64 iThreshold
 19361 ){
 19362   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
 19364 #endif
 19366 /*
 19367 ** Set the soft heap-size limit for the library. Passing a zero or 
 19368 ** negative value indicates no limit.
 19369 */
 19370 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
 19371   sqlite3_int64 priorLimit;
 19372   sqlite3_int64 excess;
 19373 #ifndef SQLITE_OMIT_AUTOINIT
 19374   int rc = sqlite3_initialize();
 19375   if( rc ) return -1;
 19376 #endif
 19377   sqlite3_mutex_enter(mem0.mutex);
 19378   priorLimit = mem0.alarmThreshold;
 19379   sqlite3_mutex_leave(mem0.mutex);
 19380   if( n<0 ) return priorLimit;
 19381   if( n>0 ){
 19382     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
 19383   }else{
 19384     sqlite3MemoryAlarm(0, 0, 0);
 19386   excess = sqlite3_memory_used() - n;
 19387   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
 19388   return priorLimit;
 19390 SQLITE_API void sqlite3_soft_heap_limit(int n){
 19391   if( n<0 ) n = 0;
 19392   sqlite3_soft_heap_limit64(n);
 19395 /*
 19396 ** Initialize the memory allocation subsystem.
 19397 */
 19398 SQLITE_PRIVATE int sqlite3MallocInit(void){
 19399   if( sqlite3GlobalConfig.m.xMalloc==0 ){
 19400     sqlite3MemSetDefault();
 19402   memset(&mem0, 0, sizeof(mem0));
 19403   if( sqlite3GlobalConfig.bCoreMutex ){
 19404     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
 19406   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
 19407       && sqlite3GlobalConfig.nScratch>0 ){
 19408     int i, n, sz;
 19409     ScratchFreeslot *pSlot;
 19410     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
 19411     sqlite3GlobalConfig.szScratch = sz;
 19412     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
 19413     n = sqlite3GlobalConfig.nScratch;
 19414     mem0.pScratchFree = pSlot;
 19415     mem0.nScratchFree = n;
 19416     for(i=0; i<n-1; i++){
 19417       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
 19418       pSlot = pSlot->pNext;
 19420     pSlot->pNext = 0;
 19421     mem0.pScratchEnd = (void*)&pSlot[1];
 19422   }else{
 19423     mem0.pScratchEnd = 0;
 19424     sqlite3GlobalConfig.pScratch = 0;
 19425     sqlite3GlobalConfig.szScratch = 0;
 19426     sqlite3GlobalConfig.nScratch = 0;
 19428   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
 19429       || sqlite3GlobalConfig.nPage<1 ){
 19430     sqlite3GlobalConfig.pPage = 0;
 19431     sqlite3GlobalConfig.szPage = 0;
 19432     sqlite3GlobalConfig.nPage = 0;
 19434   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
 19437 /*
 19438 ** Return true if the heap is currently under memory pressure - in other
 19439 ** words if the amount of heap used is close to the limit set by
 19440 ** sqlite3_soft_heap_limit().
 19441 */
 19442 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
 19443   return mem0.nearlyFull;
 19446 /*
 19447 ** Deinitialize the memory allocation subsystem.
 19448 */
 19449 SQLITE_PRIVATE void sqlite3MallocEnd(void){
 19450   if( sqlite3GlobalConfig.m.xShutdown ){
 19451     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
 19453   memset(&mem0, 0, sizeof(mem0));
 19456 /*
 19457 ** Return the amount of memory currently checked out.
 19458 */
 19459 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
 19460   int n, mx;
 19461   sqlite3_int64 res;
 19462   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
 19463   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
 19464   return res;
 19467 /*
 19468 ** Return the maximum amount of memory that has ever been
 19469 ** checked out since either the beginning of this process
 19470 ** or since the most recent reset.
 19471 */
 19472 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
 19473   int n, mx;
 19474   sqlite3_int64 res;
 19475   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
 19476   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
 19477   return res;
 19480 /*
 19481 ** Trigger the alarm 
 19482 */
 19483 static void sqlite3MallocAlarm(int nByte){
 19484   void (*xCallback)(void*,sqlite3_int64,int);
 19485   sqlite3_int64 nowUsed;
 19486   void *pArg;
 19487   if( mem0.alarmCallback==0 ) return;
 19488   xCallback = mem0.alarmCallback;
 19489   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 19490   pArg = mem0.alarmArg;
 19491   mem0.alarmCallback = 0;
 19492   sqlite3_mutex_leave(mem0.mutex);
 19493   xCallback(pArg, nowUsed, nByte);
 19494   sqlite3_mutex_enter(mem0.mutex);
 19495   mem0.alarmCallback = xCallback;
 19496   mem0.alarmArg = pArg;
 19499 /*
 19500 ** Do a memory allocation with statistics and alarms.  Assume the
 19501 ** lock is already held.
 19502 */
 19503 static int mallocWithAlarm(int n, void **pp){
 19504   int nFull;
 19505   void *p;
 19506   assert( sqlite3_mutex_held(mem0.mutex) );
 19507   nFull = sqlite3GlobalConfig.m.xRoundup(n);
 19508   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
 19509   if( mem0.alarmCallback!=0 ){
 19510     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
 19511     if( nUsed >= mem0.alarmThreshold - nFull ){
 19512       mem0.nearlyFull = 1;
 19513       sqlite3MallocAlarm(nFull);
 19514     }else{
 19515       mem0.nearlyFull = 0;
 19518   p = sqlite3GlobalConfig.m.xMalloc(nFull);
 19519 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 19520   if( p==0 && mem0.alarmCallback ){
 19521     sqlite3MallocAlarm(nFull);
 19522     p = sqlite3GlobalConfig.m.xMalloc(nFull);
 19524 #endif
 19525   if( p ){
 19526     nFull = sqlite3MallocSize(p);
 19527     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
 19528     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
 19530   *pp = p;
 19531   return nFull;
 19534 /*
 19535 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
 19536 ** assumes the memory subsystem has already been initialized.
 19537 */
 19538 SQLITE_PRIVATE void *sqlite3Malloc(int n){
 19539   void *p;
 19540   if( n<=0               /* IMP: R-65312-04917 */ 
 19541    || n>=0x7fffff00
 19542   ){
 19543     /* A memory allocation of a number of bytes which is near the maximum
 19544     ** signed integer value might cause an integer overflow inside of the
 19545     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
 19546     ** 255 bytes of overhead.  SQLite itself will never use anything near
 19547     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
 19548     p = 0;
 19549   }else if( sqlite3GlobalConfig.bMemstat ){
 19550     sqlite3_mutex_enter(mem0.mutex);
 19551     mallocWithAlarm(n, &p);
 19552     sqlite3_mutex_leave(mem0.mutex);
 19553   }else{
 19554     p = sqlite3GlobalConfig.m.xMalloc(n);
 19556   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
 19557   return p;
 19560 /*
 19561 ** This version of the memory allocation is for use by the application.
 19562 ** First make sure the memory subsystem is initialized, then do the
 19563 ** allocation.
 19564 */
 19565 SQLITE_API void *sqlite3_malloc(int n){
 19566 #ifndef SQLITE_OMIT_AUTOINIT
 19567   if( sqlite3_initialize() ) return 0;
 19568 #endif
 19569   return sqlite3Malloc(n);
 19572 /*
 19573 ** Each thread may only have a single outstanding allocation from
 19574 ** xScratchMalloc().  We verify this constraint in the single-threaded
 19575 ** case by setting scratchAllocOut to 1 when an allocation
 19576 ** is outstanding clearing it when the allocation is freed.
 19577 */
 19578 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 19579 static int scratchAllocOut = 0;
 19580 #endif
 19583 /*
 19584 ** Allocate memory that is to be used and released right away.
 19585 ** This routine is similar to alloca() in that it is not intended
 19586 ** for situations where the memory might be held long-term.  This
 19587 ** routine is intended to get memory to old large transient data
 19588 ** structures that would not normally fit on the stack of an
 19589 ** embedded processor.
 19590 */
 19591 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
 19592   void *p;
 19593   assert( n>0 );
 19595   sqlite3_mutex_enter(mem0.mutex);
 19596   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
 19597     p = mem0.pScratchFree;
 19598     mem0.pScratchFree = mem0.pScratchFree->pNext;
 19599     mem0.nScratchFree--;
 19600     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
 19601     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 19602     sqlite3_mutex_leave(mem0.mutex);
 19603   }else{
 19604     if( sqlite3GlobalConfig.bMemstat ){
 19605       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
 19606       n = mallocWithAlarm(n, &p);
 19607       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
 19608       sqlite3_mutex_leave(mem0.mutex);
 19609     }else{
 19610       sqlite3_mutex_leave(mem0.mutex);
 19611       p = sqlite3GlobalConfig.m.xMalloc(n);
 19613     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
 19615   assert( sqlite3_mutex_notheld(mem0.mutex) );
 19618 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 19619   /* Verify that no more than two scratch allocations per thread
 19620   ** are outstanding at one time.  (This is only checked in the
 19621   ** single-threaded case since checking in the multi-threaded case
 19622   ** would be much more complicated.) */
 19623   assert( scratchAllocOut<=1 );
 19624   if( p ) scratchAllocOut++;
 19625 #endif
 19627   return p;
 19629 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
 19630   if( p ){
 19632 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
 19633     /* Verify that no more than two scratch allocation per thread
 19634     ** is outstanding at one time.  (This is only checked in the
 19635     ** single-threaded case since checking in the multi-threaded case
 19636     ** would be much more complicated.) */
 19637     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
 19638     scratchAllocOut--;
 19639 #endif
 19641     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
 19642       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
 19643       ScratchFreeslot *pSlot;
 19644       pSlot = (ScratchFreeslot*)p;
 19645       sqlite3_mutex_enter(mem0.mutex);
 19646       pSlot->pNext = mem0.pScratchFree;
 19647       mem0.pScratchFree = pSlot;
 19648       mem0.nScratchFree++;
 19649       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
 19650       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
 19651       sqlite3_mutex_leave(mem0.mutex);
 19652     }else{
 19653       /* Release memory back to the heap */
 19654       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
 19655       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
 19656       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 19657       if( sqlite3GlobalConfig.bMemstat ){
 19658         int iSize = sqlite3MallocSize(p);
 19659         sqlite3_mutex_enter(mem0.mutex);
 19660         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
 19661         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
 19662         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 19663         sqlite3GlobalConfig.m.xFree(p);
 19664         sqlite3_mutex_leave(mem0.mutex);
 19665       }else{
 19666         sqlite3GlobalConfig.m.xFree(p);
 19672 /*
 19673 ** TRUE if p is a lookaside memory allocation from db
 19674 */
 19675 #ifndef SQLITE_OMIT_LOOKASIDE
 19676 static int isLookaside(sqlite3 *db, void *p){
 19677   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
 19679 #else
 19680 #define isLookaside(A,B) 0
 19681 #endif
 19683 /*
 19684 ** Return the size of a memory allocation previously obtained from
 19685 ** sqlite3Malloc() or sqlite3_malloc().
 19686 */
 19687 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
 19688   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 19689   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 19690   return sqlite3GlobalConfig.m.xSize(p);
 19692 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
 19693   assert( db!=0 );
 19694   assert( sqlite3_mutex_held(db->mutex) );
 19695   if( isLookaside(db, p) ){
 19696     return db->lookaside.sz;
 19697   }else{
 19698     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19699     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19700     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 19701     return sqlite3GlobalConfig.m.xSize(p);
 19705 /*
 19706 ** Free memory previously obtained from sqlite3Malloc().
 19707 */
 19708 SQLITE_API void sqlite3_free(void *p){
 19709   if( p==0 ) return;  /* IMP: R-49053-54554 */
 19710   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
 19711   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
 19712   if( sqlite3GlobalConfig.bMemstat ){
 19713     sqlite3_mutex_enter(mem0.mutex);
 19714     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
 19715     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
 19716     sqlite3GlobalConfig.m.xFree(p);
 19717     sqlite3_mutex_leave(mem0.mutex);
 19718   }else{
 19719     sqlite3GlobalConfig.m.xFree(p);
 19723 /*
 19724 ** Free memory that might be associated with a particular database
 19725 ** connection.
 19726 */
 19727 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
 19728   assert( db==0 || sqlite3_mutex_held(db->mutex) );
 19729   if( p==0 ) return;
 19730   if( db ){
 19731     if( db->pnBytesFreed ){
 19732       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
 19733       return;
 19735     if( isLookaside(db, p) ){
 19736       LookasideSlot *pBuf = (LookasideSlot*)p;
 19737 #if SQLITE_DEBUG
 19738       /* Trash all content in the buffer being freed */
 19739       memset(p, 0xaa, db->lookaside.sz);
 19740 #endif
 19741       pBuf->pNext = db->lookaside.pFree;
 19742       db->lookaside.pFree = pBuf;
 19743       db->lookaside.nOut--;
 19744       return;
 19747   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19748   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19749   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
 19750   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 19751   sqlite3_free(p);
 19754 /*
 19755 ** Change the size of an existing memory allocation
 19756 */
 19757 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
 19758   int nOld, nNew, nDiff;
 19759   void *pNew;
 19760   if( pOld==0 ){
 19761     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
 19763   if( nBytes<=0 ){
 19764     sqlite3_free(pOld); /* IMP: R-31593-10574 */
 19765     return 0;
 19767   if( nBytes>=0x7fffff00 ){
 19768     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
 19769     return 0;
 19771   nOld = sqlite3MallocSize(pOld);
 19772   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
 19773   ** argument to xRealloc is always a value returned by a prior call to
 19774   ** xRoundup. */
 19775   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
 19776   if( nOld==nNew ){
 19777     pNew = pOld;
 19778   }else if( sqlite3GlobalConfig.bMemstat ){
 19779     sqlite3_mutex_enter(mem0.mutex);
 19780     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
 19781     nDiff = nNew - nOld;
 19782     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
 19783           mem0.alarmThreshold-nDiff ){
 19784       sqlite3MallocAlarm(nDiff);
 19786     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
 19787     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
 19788     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19789     if( pNew==0 && mem0.alarmCallback ){
 19790       sqlite3MallocAlarm(nBytes);
 19791       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19793     if( pNew ){
 19794       nNew = sqlite3MallocSize(pNew);
 19795       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
 19797     sqlite3_mutex_leave(mem0.mutex);
 19798   }else{
 19799     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
 19801   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
 19802   return pNew;
 19805 /*
 19806 ** The public interface to sqlite3Realloc.  Make sure that the memory
 19807 ** subsystem is initialized prior to invoking sqliteRealloc.
 19808 */
 19809 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
 19810 #ifndef SQLITE_OMIT_AUTOINIT
 19811   if( sqlite3_initialize() ) return 0;
 19812 #endif
 19813   return sqlite3Realloc(pOld, n);
 19817 /*
 19818 ** Allocate and zero memory.
 19819 */ 
 19820 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
 19821   void *p = sqlite3Malloc(n);
 19822   if( p ){
 19823     memset(p, 0, n);
 19825   return p;
 19828 /*
 19829 ** Allocate and zero memory.  If the allocation fails, make
 19830 ** the mallocFailed flag in the connection pointer.
 19831 */
 19832 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
 19833   void *p = sqlite3DbMallocRaw(db, n);
 19834   if( p ){
 19835     memset(p, 0, n);
 19837   return p;
 19840 /*
 19841 ** Allocate and zero memory.  If the allocation fails, make
 19842 ** the mallocFailed flag in the connection pointer.
 19843 **
 19844 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
 19845 ** failure on the same database connection) then always return 0.
 19846 ** Hence for a particular database connection, once malloc starts
 19847 ** failing, it fails consistently until mallocFailed is reset.
 19848 ** This is an important assumption.  There are many places in the
 19849 ** code that do things like this:
 19850 **
 19851 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
 19852 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
 19853 **         if( b ) a[10] = 9;
 19854 **
 19855 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
 19856 ** that all prior mallocs (ex: "a") worked too.
 19857 */
 19858 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
 19859   void *p;
 19860   assert( db==0 || sqlite3_mutex_held(db->mutex) );
 19861   assert( db==0 || db->pnBytesFreed==0 );
 19862 #ifndef SQLITE_OMIT_LOOKASIDE
 19863   if( db ){
 19864     LookasideSlot *pBuf;
 19865     if( db->mallocFailed ){
 19866       return 0;
 19868     if( db->lookaside.bEnabled ){
 19869       if( n>db->lookaside.sz ){
 19870         db->lookaside.anStat[1]++;
 19871       }else if( (pBuf = db->lookaside.pFree)==0 ){
 19872         db->lookaside.anStat[2]++;
 19873       }else{
 19874         db->lookaside.pFree = pBuf->pNext;
 19875         db->lookaside.nOut++;
 19876         db->lookaside.anStat[0]++;
 19877         if( db->lookaside.nOut>db->lookaside.mxOut ){
 19878           db->lookaside.mxOut = db->lookaside.nOut;
 19880         return (void*)pBuf;
 19884 #else
 19885   if( db && db->mallocFailed ){
 19886     return 0;
 19888 #endif
 19889   p = sqlite3Malloc(n);
 19890   if( !p && db ){
 19891     db->mallocFailed = 1;
 19893   sqlite3MemdebugSetType(p, MEMTYPE_DB |
 19894          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 19895   return p;
 19898 /*
 19899 ** Resize the block of memory pointed to by p to n bytes. If the
 19900 ** resize fails, set the mallocFailed flag in the connection object.
 19901 */
 19902 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
 19903   void *pNew = 0;
 19904   assert( db!=0 );
 19905   assert( sqlite3_mutex_held(db->mutex) );
 19906   if( db->mallocFailed==0 ){
 19907     if( p==0 ){
 19908       return sqlite3DbMallocRaw(db, n);
 19910     if( isLookaside(db, p) ){
 19911       if( n<=db->lookaside.sz ){
 19912         return p;
 19914       pNew = sqlite3DbMallocRaw(db, n);
 19915       if( pNew ){
 19916         memcpy(pNew, p, db->lookaside.sz);
 19917         sqlite3DbFree(db, p);
 19919     }else{
 19920       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
 19921       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
 19922       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 19923       pNew = sqlite3_realloc(p, n);
 19924       if( !pNew ){
 19925         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
 19926         db->mallocFailed = 1;
 19928       sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
 19929             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
 19932   return pNew;
 19935 /*
 19936 ** Attempt to reallocate p.  If the reallocation fails, then free p
 19937 ** and set the mallocFailed flag in the database connection.
 19938 */
 19939 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
 19940   void *pNew;
 19941   pNew = sqlite3DbRealloc(db, p, n);
 19942   if( !pNew ){
 19943     sqlite3DbFree(db, p);
 19945   return pNew;
 19948 /*
 19949 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
 19950 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
 19951 ** is because when memory debugging is turned on, these two functions are 
 19952 ** called via macros that record the current file and line number in the
 19953 ** ThreadData structure.
 19954 */
 19955 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
 19956   char *zNew;
 19957   size_t n;
 19958   if( z==0 ){
 19959     return 0;
 19961   n = sqlite3Strlen30(z) + 1;
 19962   assert( (n&0x7fffffff)==n );
 19963   zNew = sqlite3DbMallocRaw(db, (int)n);
 19964   if( zNew ){
 19965     memcpy(zNew, z, n);
 19967   return zNew;
 19969 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
 19970   char *zNew;
 19971   if( z==0 ){
 19972     return 0;
 19974   assert( (n&0x7fffffff)==n );
 19975   zNew = sqlite3DbMallocRaw(db, n+1);
 19976   if( zNew ){
 19977     memcpy(zNew, z, n);
 19978     zNew[n] = 0;
 19980   return zNew;
 19983 /*
 19984 ** Create a string from the zFromat argument and the va_list that follows.
 19985 ** Store the string in memory obtained from sqliteMalloc() and make *pz
 19986 ** point to that string.
 19987 */
 19988 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
 19989   va_list ap;
 19990   char *z;
 19992   va_start(ap, zFormat);
 19993   z = sqlite3VMPrintf(db, zFormat, ap);
 19994   va_end(ap);
 19995   sqlite3DbFree(db, *pz);
 19996   *pz = z;
 20000 /*
 20001 ** This function must be called before exiting any API function (i.e. 
 20002 ** returning control to the user) that has called sqlite3_malloc or
 20003 ** sqlite3_realloc.
 20004 **
 20005 ** The returned value is normally a copy of the second argument to this
 20006 ** function. However, if a malloc() failure has occurred since the previous
 20007 ** invocation SQLITE_NOMEM is returned instead. 
 20008 **
 20009 ** If the first argument, db, is not NULL and a malloc() error has occurred,
 20010 ** then the connection error-code (the value returned by sqlite3_errcode())
 20011 ** is set to SQLITE_NOMEM.
 20012 */
 20013 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
 20014   /* If the db handle is not NULL, then we must hold the connection handle
 20015   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
 20016   ** is unsafe, as is the call to sqlite3Error().
 20017   */
 20018   assert( !db || sqlite3_mutex_held(db->mutex) );
 20019   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
 20020     sqlite3Error(db, SQLITE_NOMEM, 0);
 20021     db->mallocFailed = 0;
 20022     rc = SQLITE_NOMEM;
 20024   return rc & (db ? db->errMask : 0xff);
 20027 /************** End of malloc.c **********************************************/
 20028 /************** Begin file printf.c ******************************************/
 20029 /*
 20030 ** The "printf" code that follows dates from the 1980's.  It is in
 20031 ** the public domain.  The original comments are included here for
 20032 ** completeness.  They are very out-of-date but might be useful as
 20033 ** an historical reference.  Most of the "enhancements" have been backed
 20034 ** out so that the functionality is now the same as standard printf().
 20035 **
 20036 **************************************************************************
 20037 **
 20038 ** This file contains code for a set of "printf"-like routines.  These
 20039 ** routines format strings much like the printf() from the standard C
 20040 ** library, though the implementation here has enhancements to support
 20041 ** SQLlite.
 20042 */
 20044 /*
 20045 ** Conversion types fall into various categories as defined by the
 20046 ** following enumeration.
 20047 */
 20048 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
 20049 #define etFLOAT       2 /* Floating point.  %f */
 20050 #define etEXP         3 /* Exponentional notation. %e and %E */
 20051 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
 20052 #define etSIZE        5 /* Return number of characters processed so far. %n */
 20053 #define etSTRING      6 /* Strings. %s */
 20054 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
 20055 #define etPERCENT     8 /* Percent symbol. %% */
 20056 #define etCHARX       9 /* Characters. %c */
 20057 /* The rest are extensions, not normally found in printf() */
 20058 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
 20059 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
 20060                           NULL pointers replaced by SQL NULL.  %Q */
 20061 #define etTOKEN      12 /* a pointer to a Token structure */
 20062 #define etSRCLIST    13 /* a pointer to a SrcList */
 20063 #define etPOINTER    14 /* The %p conversion */
 20064 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
 20065 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
 20067 #define etINVALID     0 /* Any unrecognized conversion type */
 20070 /*
 20071 ** An "etByte" is an 8-bit unsigned value.
 20072 */
 20073 typedef unsigned char etByte;
 20075 /*
 20076 ** Each builtin conversion character (ex: the 'd' in "%d") is described
 20077 ** by an instance of the following structure
 20078 */
 20079 typedef struct et_info {   /* Information about each format field */
 20080   char fmttype;            /* The format field code letter */
 20081   etByte base;             /* The base for radix conversion */
 20082   etByte flags;            /* One or more of FLAG_ constants below */
 20083   etByte type;             /* Conversion paradigm */
 20084   etByte charset;          /* Offset into aDigits[] of the digits string */
 20085   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
 20086 } et_info;
 20088 /*
 20089 ** Allowed values for et_info.flags
 20090 */
 20091 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
 20092 #define FLAG_INTERN  2     /* True if for internal use only */
 20093 #define FLAG_STRING  4     /* Allow infinity precision */
 20096 /*
 20097 ** The following table is searched linearly, so it is good to put the
 20098 ** most frequently used conversion types first.
 20099 */
 20100 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
 20101 static const char aPrefix[] = "-x0\000X0";
 20102 static const et_info fmtinfo[] = {
 20103   {  'd', 10, 1, etRADIX,      0,  0 },
 20104   {  's',  0, 4, etSTRING,     0,  0 },
 20105   {  'g',  0, 1, etGENERIC,    30, 0 },
 20106   {  'z',  0, 4, etDYNSTRING,  0,  0 },
 20107   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
 20108   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
 20109   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
 20110   {  'c',  0, 0, etCHARX,      0,  0 },
 20111   {  'o',  8, 0, etRADIX,      0,  2 },
 20112   {  'u', 10, 0, etRADIX,      0,  0 },
 20113   {  'x', 16, 0, etRADIX,      16, 1 },
 20114   {  'X', 16, 0, etRADIX,      0,  4 },
 20115 #ifndef SQLITE_OMIT_FLOATING_POINT
 20116   {  'f',  0, 1, etFLOAT,      0,  0 },
 20117   {  'e',  0, 1, etEXP,        30, 0 },
 20118   {  'E',  0, 1, etEXP,        14, 0 },
 20119   {  'G',  0, 1, etGENERIC,    14, 0 },
 20120 #endif
 20121   {  'i', 10, 1, etRADIX,      0,  0 },
 20122   {  'n',  0, 0, etSIZE,       0,  0 },
 20123   {  '%',  0, 0, etPERCENT,    0,  0 },
 20124   {  'p', 16, 0, etPOINTER,    0,  1 },
 20126 /* All the rest have the FLAG_INTERN bit set and are thus for internal
 20127 ** use only */
 20128   {  'T',  0, 2, etTOKEN,      0,  0 },
 20129   {  'S',  0, 2, etSRCLIST,    0,  0 },
 20130   {  'r', 10, 3, etORDINAL,    0,  0 },
 20131 };
 20133 /*
 20134 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
 20135 ** conversions will work.
 20136 */
 20137 #ifndef SQLITE_OMIT_FLOATING_POINT
 20138 /*
 20139 ** "*val" is a double such that 0.1 <= *val < 10.0
 20140 ** Return the ascii code for the leading digit of *val, then
 20141 ** multiply "*val" by 10.0 to renormalize.
 20142 **
 20143 ** Example:
 20144 **     input:     *val = 3.14159
 20145 **     output:    *val = 1.4159    function return = '3'
 20146 **
 20147 ** The counter *cnt is incremented each time.  After counter exceeds
 20148 ** 16 (the number of significant digits in a 64-bit float) '0' is
 20149 ** always returned.
 20150 */
 20151 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
 20152   int digit;
 20153   LONGDOUBLE_TYPE d;
 20154   if( (*cnt)<=0 ) return '0';
 20155   (*cnt)--;
 20156   digit = (int)*val;
 20157   d = digit;
 20158   digit += '0';
 20159   *val = (*val - d)*10.0;
 20160   return (char)digit;
 20162 #endif /* SQLITE_OMIT_FLOATING_POINT */
 20164 /*
 20165 ** Append N space characters to the given string buffer.
 20166 */
 20167 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
 20168   static const char zSpaces[] = "                             ";
 20169   while( N>=(int)sizeof(zSpaces)-1 ){
 20170     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
 20171     N -= sizeof(zSpaces)-1;
 20173   if( N>0 ){
 20174     sqlite3StrAccumAppend(pAccum, zSpaces, N);
 20178 /*
 20179 ** Set the StrAccum object to an error mode.
 20180 */
 20181 static void setStrAccumError(StrAccum *p, u8 eError){
 20182   p->accError = eError;
 20183   p->nAlloc = 0;
 20186 /*
 20187 ** Extra argument values from a PrintfArguments object
 20188 */
 20189 static sqlite3_int64 getIntArg(PrintfArguments *p){
 20190   if( p->nArg<=p->nUsed ) return 0;
 20191   return sqlite3_value_int64(p->apArg[p->nUsed++]);
 20193 static double getDoubleArg(PrintfArguments *p){
 20194   if( p->nArg<=p->nUsed ) return 0.0;
 20195   return sqlite3_value_double(p->apArg[p->nUsed++]);
 20197 static char *getTextArg(PrintfArguments *p){
 20198   if( p->nArg<=p->nUsed ) return 0;
 20199   return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
 20203 /*
 20204 ** On machines with a small stack size, you can redefine the
 20205 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
 20206 */
 20207 #ifndef SQLITE_PRINT_BUF_SIZE
 20208 # define SQLITE_PRINT_BUF_SIZE 70
 20209 #endif
 20210 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
 20212 /*
 20213 ** Render a string given by "fmt" into the StrAccum object.
 20214 */
 20215 SQLITE_PRIVATE void sqlite3VXPrintf(
 20216   StrAccum *pAccum,          /* Accumulate results here */
 20217   u32 bFlags,                /* SQLITE_PRINTF_* flags */
 20218   const char *fmt,           /* Format string */
 20219   va_list ap                 /* arguments */
 20220 ){
 20221   int c;                     /* Next character in the format string */
 20222   char *bufpt;               /* Pointer to the conversion buffer */
 20223   int precision;             /* Precision of the current field */
 20224   int length;                /* Length of the field */
 20225   int idx;                   /* A general purpose loop counter */
 20226   int width;                 /* Width of the current field */
 20227   etByte flag_leftjustify;   /* True if "-" flag is present */
 20228   etByte flag_plussign;      /* True if "+" flag is present */
 20229   etByte flag_blanksign;     /* True if " " flag is present */
 20230   etByte flag_alternateform; /* True if "#" flag is present */
 20231   etByte flag_altform2;      /* True if "!" flag is present */
 20232   etByte flag_zeropad;       /* True if field width constant starts with zero */
 20233   etByte flag_long;          /* True if "l" flag is present */
 20234   etByte flag_longlong;      /* True if the "ll" flag is present */
 20235   etByte done;               /* Loop termination flag */
 20236   etByte xtype = 0;          /* Conversion paradigm */
 20237   u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
 20238   u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
 20239   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
 20240   sqlite_uint64 longvalue;   /* Value for integer types */
 20241   LONGDOUBLE_TYPE realvalue; /* Value for real types */
 20242   const et_info *infop;      /* Pointer to the appropriate info structure */
 20243   char *zOut;                /* Rendering buffer */
 20244   int nOut;                  /* Size of the rendering buffer */
 20245   char *zExtra;              /* Malloced memory used by some conversion */
 20246 #ifndef SQLITE_OMIT_FLOATING_POINT
 20247   int  exp, e2;              /* exponent of real numbers */
 20248   int nsd;                   /* Number of significant digits returned */
 20249   double rounder;            /* Used for rounding floating point values */
 20250   etByte flag_dp;            /* True if decimal point should be shown */
 20251   etByte flag_rtz;           /* True if trailing zeros should be removed */
 20252 #endif
 20253   PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 20254   char buf[etBUFSIZE];       /* Conversion buffer */
 20256   bufpt = 0;
 20257   if( bFlags ){
 20258     if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
 20259       pArgList = va_arg(ap, PrintfArguments*);
 20261     useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
 20262   }else{
 20263     bArgList = useIntern = 0;
 20265   for(; (c=(*fmt))!=0; ++fmt){
 20266     if( c!='%' ){
 20267       int amt;
 20268       bufpt = (char *)fmt;
 20269       amt = 1;
 20270       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
 20271       sqlite3StrAccumAppend(pAccum, bufpt, amt);
 20272       if( c==0 ) break;
 20274     if( (c=(*++fmt))==0 ){
 20275       sqlite3StrAccumAppend(pAccum, "%", 1);
 20276       break;
 20278     /* Find out what flags are present */
 20279     flag_leftjustify = flag_plussign = flag_blanksign = 
 20280      flag_alternateform = flag_altform2 = flag_zeropad = 0;
 20281     done = 0;
 20282     do{
 20283       switch( c ){
 20284         case '-':   flag_leftjustify = 1;     break;
 20285         case '+':   flag_plussign = 1;        break;
 20286         case ' ':   flag_blanksign = 1;       break;
 20287         case '#':   flag_alternateform = 1;   break;
 20288         case '!':   flag_altform2 = 1;        break;
 20289         case '0':   flag_zeropad = 1;         break;
 20290         default:    done = 1;                 break;
 20292     }while( !done && (c=(*++fmt))!=0 );
 20293     /* Get the field width */
 20294     width = 0;
 20295     if( c=='*' ){
 20296       if( bArgList ){
 20297         width = (int)getIntArg(pArgList);
 20298       }else{
 20299         width = va_arg(ap,int);
 20301       if( width<0 ){
 20302         flag_leftjustify = 1;
 20303         width = -width;
 20305       c = *++fmt;
 20306     }else{
 20307       while( c>='0' && c<='9' ){
 20308         width = width*10 + c - '0';
 20309         c = *++fmt;
 20312     /* Get the precision */
 20313     if( c=='.' ){
 20314       precision = 0;
 20315       c = *++fmt;
 20316       if( c=='*' ){
 20317         if( bArgList ){
 20318           precision = (int)getIntArg(pArgList);
 20319         }else{
 20320           precision = va_arg(ap,int);
 20322         if( precision<0 ) precision = -precision;
 20323         c = *++fmt;
 20324       }else{
 20325         while( c>='0' && c<='9' ){
 20326           precision = precision*10 + c - '0';
 20327           c = *++fmt;
 20330     }else{
 20331       precision = -1;
 20333     /* Get the conversion type modifier */
 20334     if( c=='l' ){
 20335       flag_long = 1;
 20336       c = *++fmt;
 20337       if( c=='l' ){
 20338         flag_longlong = 1;
 20339         c = *++fmt;
 20340       }else{
 20341         flag_longlong = 0;
 20343     }else{
 20344       flag_long = flag_longlong = 0;
 20346     /* Fetch the info entry for the field */
 20347     infop = &fmtinfo[0];
 20348     xtype = etINVALID;
 20349     for(idx=0; idx<ArraySize(fmtinfo); idx++){
 20350       if( c==fmtinfo[idx].fmttype ){
 20351         infop = &fmtinfo[idx];
 20352         if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
 20353           xtype = infop->type;
 20354         }else{
 20355           return;
 20357         break;
 20360     zExtra = 0;
 20362     /*
 20363     ** At this point, variables are initialized as follows:
 20364     **
 20365     **   flag_alternateform          TRUE if a '#' is present.
 20366     **   flag_altform2               TRUE if a '!' is present.
 20367     **   flag_plussign               TRUE if a '+' is present.
 20368     **   flag_leftjustify            TRUE if a '-' is present or if the
 20369     **                               field width was negative.
 20370     **   flag_zeropad                TRUE if the width began with 0.
 20371     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
 20372     **                               the conversion character.
 20373     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
 20374     **                               the conversion character.
 20375     **   flag_blanksign              TRUE if a ' ' is present.
 20376     **   width                       The specified field width.  This is
 20377     **                               always non-negative.  Zero is the default.
 20378     **   precision                   The specified precision.  The default
 20379     **                               is -1.
 20380     **   xtype                       The class of the conversion.
 20381     **   infop                       Pointer to the appropriate info struct.
 20382     */
 20383     switch( xtype ){
 20384       case etPOINTER:
 20385         flag_longlong = sizeof(char*)==sizeof(i64);
 20386         flag_long = sizeof(char*)==sizeof(long int);
 20387         /* Fall through into the next case */
 20388       case etORDINAL:
 20389       case etRADIX:
 20390         if( infop->flags & FLAG_SIGNED ){
 20391           i64 v;
 20392           if( bArgList ){
 20393             v = getIntArg(pArgList);
 20394           }else if( flag_longlong ){
 20395             v = va_arg(ap,i64);
 20396           }else if( flag_long ){
 20397             v = va_arg(ap,long int);
 20398           }else{
 20399             v = va_arg(ap,int);
 20401           if( v<0 ){
 20402             if( v==SMALLEST_INT64 ){
 20403               longvalue = ((u64)1)<<63;
 20404             }else{
 20405               longvalue = -v;
 20407             prefix = '-';
 20408           }else{
 20409             longvalue = v;
 20410             if( flag_plussign )        prefix = '+';
 20411             else if( flag_blanksign )  prefix = ' ';
 20412             else                       prefix = 0;
 20414         }else{
 20415           if( bArgList ){
 20416             longvalue = (u64)getIntArg(pArgList);
 20417           }else if( flag_longlong ){
 20418             longvalue = va_arg(ap,u64);
 20419           }else if( flag_long ){
 20420             longvalue = va_arg(ap,unsigned long int);
 20421           }else{
 20422             longvalue = va_arg(ap,unsigned int);
 20424           prefix = 0;
 20426         if( longvalue==0 ) flag_alternateform = 0;
 20427         if( flag_zeropad && precision<width-(prefix!=0) ){
 20428           precision = width-(prefix!=0);
 20430         if( precision<etBUFSIZE-10 ){
 20431           nOut = etBUFSIZE;
 20432           zOut = buf;
 20433         }else{
 20434           nOut = precision + 10;
 20435           zOut = zExtra = sqlite3Malloc( nOut );
 20436           if( zOut==0 ){
 20437             setStrAccumError(pAccum, STRACCUM_NOMEM);
 20438             return;
 20441         bufpt = &zOut[nOut-1];
 20442         if( xtype==etORDINAL ){
 20443           static const char zOrd[] = "thstndrd";
 20444           int x = (int)(longvalue % 10);
 20445           if( x>=4 || (longvalue/10)%10==1 ){
 20446             x = 0;
 20448           *(--bufpt) = zOrd[x*2+1];
 20449           *(--bufpt) = zOrd[x*2];
 20452           register const char *cset;      /* Use registers for speed */
 20453           register int base;
 20454           cset = &aDigits[infop->charset];
 20455           base = infop->base;
 20456           do{                                           /* Convert to ascii */
 20457             *(--bufpt) = cset[longvalue%base];
 20458             longvalue = longvalue/base;
 20459           }while( longvalue>0 );
 20461         length = (int)(&zOut[nOut-1]-bufpt);
 20462         for(idx=precision-length; idx>0; idx--){
 20463           *(--bufpt) = '0';                             /* Zero pad */
 20465         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
 20466         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
 20467           const char *pre;
 20468           char x;
 20469           pre = &aPrefix[infop->prefix];
 20470           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
 20472         length = (int)(&zOut[nOut-1]-bufpt);
 20473         break;
 20474       case etFLOAT:
 20475       case etEXP:
 20476       case etGENERIC:
 20477         if( bArgList ){
 20478           realvalue = getDoubleArg(pArgList);
 20479         }else{
 20480           realvalue = va_arg(ap,double);
 20482 #ifdef SQLITE_OMIT_FLOATING_POINT
 20483         length = 0;
 20484 #else
 20485         if( precision<0 ) precision = 6;         /* Set default precision */
 20486         if( realvalue<0.0 ){
 20487           realvalue = -realvalue;
 20488           prefix = '-';
 20489         }else{
 20490           if( flag_plussign )          prefix = '+';
 20491           else if( flag_blanksign )    prefix = ' ';
 20492           else                         prefix = 0;
 20494         if( xtype==etGENERIC && precision>0 ) precision--;
 20495         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
 20496         if( xtype==etFLOAT ) realvalue += rounder;
 20497         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
 20498         exp = 0;
 20499         if( sqlite3IsNaN((double)realvalue) ){
 20500           bufpt = "NaN";
 20501           length = 3;
 20502           break;
 20504         if( realvalue>0.0 ){
 20505           LONGDOUBLE_TYPE scale = 1.0;
 20506           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
 20507           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
 20508           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
 20509           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
 20510           realvalue /= scale;
 20511           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
 20512           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
 20513           if( exp>350 ){
 20514             if( prefix=='-' ){
 20515               bufpt = "-Inf";
 20516             }else if( prefix=='+' ){
 20517               bufpt = "+Inf";
 20518             }else{
 20519               bufpt = "Inf";
 20521             length = sqlite3Strlen30(bufpt);
 20522             break;
 20525         bufpt = buf;
 20526         /*
 20527         ** If the field type is etGENERIC, then convert to either etEXP
 20528         ** or etFLOAT, as appropriate.
 20529         */
 20530         if( xtype!=etFLOAT ){
 20531           realvalue += rounder;
 20532           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
 20534         if( xtype==etGENERIC ){
 20535           flag_rtz = !flag_alternateform;
 20536           if( exp<-4 || exp>precision ){
 20537             xtype = etEXP;
 20538           }else{
 20539             precision = precision - exp;
 20540             xtype = etFLOAT;
 20542         }else{
 20543           flag_rtz = flag_altform2;
 20545         if( xtype==etEXP ){
 20546           e2 = 0;
 20547         }else{
 20548           e2 = exp;
 20550         if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
 20551           bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
 20552           if( bufpt==0 ){
 20553             setStrAccumError(pAccum, STRACCUM_NOMEM);
 20554             return;
 20557         zOut = bufpt;
 20558         nsd = 16 + flag_altform2*10;
 20559         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 20560         /* The sign in front of the number */
 20561         if( prefix ){
 20562           *(bufpt++) = prefix;
 20564         /* Digits prior to the decimal point */
 20565         if( e2<0 ){
 20566           *(bufpt++) = '0';
 20567         }else{
 20568           for(; e2>=0; e2--){
 20569             *(bufpt++) = et_getdigit(&realvalue,&nsd);
 20572         /* The decimal point */
 20573         if( flag_dp ){
 20574           *(bufpt++) = '.';
 20576         /* "0" digits after the decimal point but before the first
 20577         ** significant digit of the number */
 20578         for(e2++; e2<0; precision--, e2++){
 20579           assert( precision>0 );
 20580           *(bufpt++) = '0';
 20582         /* Significant digits after the decimal point */
 20583         while( (precision--)>0 ){
 20584           *(bufpt++) = et_getdigit(&realvalue,&nsd);
 20586         /* Remove trailing zeros and the "." if no digits follow the "." */
 20587         if( flag_rtz && flag_dp ){
 20588           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
 20589           assert( bufpt>zOut );
 20590           if( bufpt[-1]=='.' ){
 20591             if( flag_altform2 ){
 20592               *(bufpt++) = '0';
 20593             }else{
 20594               *(--bufpt) = 0;
 20598         /* Add the "eNNN" suffix */
 20599         if( xtype==etEXP ){
 20600           *(bufpt++) = aDigits[infop->charset];
 20601           if( exp<0 ){
 20602             *(bufpt++) = '-'; exp = -exp;
 20603           }else{
 20604             *(bufpt++) = '+';
 20606           if( exp>=100 ){
 20607             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
 20608             exp %= 100;
 20610           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
 20611           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
 20613         *bufpt = 0;
 20615         /* The converted number is in buf[] and zero terminated. Output it.
 20616         ** Note that the number is in the usual order, not reversed as with
 20617         ** integer conversions. */
 20618         length = (int)(bufpt-zOut);
 20619         bufpt = zOut;
 20621         /* Special case:  Add leading zeros if the flag_zeropad flag is
 20622         ** set and we are not left justified */
 20623         if( flag_zeropad && !flag_leftjustify && length < width){
 20624           int i;
 20625           int nPad = width - length;
 20626           for(i=width; i>=nPad; i--){
 20627             bufpt[i] = bufpt[i-nPad];
 20629           i = prefix!=0;
 20630           while( nPad-- ) bufpt[i++] = '0';
 20631           length = width;
 20633 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
 20634         break;
 20635       case etSIZE:
 20636         if( !bArgList ){
 20637           *(va_arg(ap,int*)) = pAccum->nChar;
 20639         length = width = 0;
 20640         break;
 20641       case etPERCENT:
 20642         buf[0] = '%';
 20643         bufpt = buf;
 20644         length = 1;
 20645         break;
 20646       case etCHARX:
 20647         if( bArgList ){
 20648           bufpt = getTextArg(pArgList);
 20649           c = bufpt ? bufpt[0] : 0;
 20650         }else{
 20651           c = va_arg(ap,int);
 20653         buf[0] = (char)c;
 20654         if( precision>=0 ){
 20655           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
 20656           length = precision;
 20657         }else{
 20658           length =1;
 20660         bufpt = buf;
 20661         break;
 20662       case etSTRING:
 20663       case etDYNSTRING:
 20664         if( bArgList ){
 20665           bufpt = getTextArg(pArgList);
 20666         }else{
 20667           bufpt = va_arg(ap,char*);
 20669         if( bufpt==0 ){
 20670           bufpt = "";
 20671         }else if( xtype==etDYNSTRING && !bArgList ){
 20672           zExtra = bufpt;
 20674         if( precision>=0 ){
 20675           for(length=0; length<precision && bufpt[length]; length++){}
 20676         }else{
 20677           length = sqlite3Strlen30(bufpt);
 20679         break;
 20680       case etSQLESCAPE:
 20681       case etSQLESCAPE2:
 20682       case etSQLESCAPE3: {
 20683         int i, j, k, n, isnull;
 20684         int needQuote;
 20685         char ch;
 20686         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 20687         char *escarg;
 20689         if( bArgList ){
 20690           escarg = getTextArg(pArgList);
 20691         }else{
 20692           escarg = va_arg(ap,char*);
 20694         isnull = escarg==0;
 20695         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
 20696         k = precision;
 20697         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 20698           if( ch==q )  n++;
 20700         needQuote = !isnull && xtype==etSQLESCAPE2;
 20701         n += i + 1 + needQuote*2;
 20702         if( n>etBUFSIZE ){
 20703           bufpt = zExtra = sqlite3Malloc( n );
 20704           if( bufpt==0 ){
 20705             setStrAccumError(pAccum, STRACCUM_NOMEM);
 20706             return;
 20708         }else{
 20709           bufpt = buf;
 20711         j = 0;
 20712         if( needQuote ) bufpt[j++] = q;
 20713         k = i;
 20714         for(i=0; i<k; i++){
 20715           bufpt[j++] = ch = escarg[i];
 20716           if( ch==q ) bufpt[j++] = ch;
 20718         if( needQuote ) bufpt[j++] = q;
 20719         bufpt[j] = 0;
 20720         length = j;
 20721         /* The precision in %q and %Q means how many input characters to
 20722         ** consume, not the length of the output...
 20723         ** if( precision>=0 && precision<length ) length = precision; */
 20724         break;
 20726       case etTOKEN: {
 20727         Token *pToken = va_arg(ap, Token*);
 20728         assert( bArgList==0 );
 20729         if( pToken && pToken->n ){
 20730           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
 20732         length = width = 0;
 20733         break;
 20735       case etSRCLIST: {
 20736         SrcList *pSrc = va_arg(ap, SrcList*);
 20737         int k = va_arg(ap, int);
 20738         struct SrcList_item *pItem = &pSrc->a[k];
 20739         assert( bArgList==0 );
 20740         assert( k>=0 && k<pSrc->nSrc );
 20741         if( pItem->zDatabase ){
 20742           sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
 20743           sqlite3StrAccumAppend(pAccum, ".", 1);
 20745         sqlite3StrAccumAppendAll(pAccum, pItem->zName);
 20746         length = width = 0;
 20747         break;
 20749       default: {
 20750         assert( xtype==etINVALID );
 20751         return;
 20753     }/* End switch over the format type */
 20754     /*
 20755     ** The text of the conversion is pointed to by "bufpt" and is
 20756     ** "length" characters long.  The field width is "width".  Do
 20757     ** the output.
 20758     */
 20759     if( !flag_leftjustify ){
 20760       register int nspace;
 20761       nspace = width-length;
 20762       if( nspace>0 ){
 20763         sqlite3AppendSpace(pAccum, nspace);
 20766     if( length>0 ){
 20767       sqlite3StrAccumAppend(pAccum, bufpt, length);
 20769     if( flag_leftjustify ){
 20770       register int nspace;
 20771       nspace = width-length;
 20772       if( nspace>0 ){
 20773         sqlite3AppendSpace(pAccum, nspace);
 20776     if( zExtra ) sqlite3_free(zExtra);
 20777   }/* End for loop over the format string */
 20778 } /* End of function */
 20780 /*
 20781 ** Append N bytes of text from z to the StrAccum object.
 20782 */
 20783 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
 20784   assert( z!=0 );
 20785   assert( p->zText!=0 || p->nChar==0 || p->accError );
 20786   assert( N>=0 );
 20787   assert( p->accError==0 || p->nAlloc==0 );
 20788   if( p->nChar+N >= p->nAlloc ){
 20789     char *zNew;
 20790     if( p->accError ){
 20791       testcase(p->accError==STRACCUM_TOOBIG);
 20792       testcase(p->accError==STRACCUM_NOMEM);
 20793       return;
 20795     if( !p->useMalloc ){
 20796       N = p->nAlloc - p->nChar - 1;
 20797       setStrAccumError(p, STRACCUM_TOOBIG);
 20798       if( N<=0 ){
 20799         return;
 20801     }else{
 20802       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
 20803       i64 szNew = p->nChar;
 20804       szNew += N + 1;
 20805       if( szNew > p->mxAlloc ){
 20806         sqlite3StrAccumReset(p);
 20807         setStrAccumError(p, STRACCUM_TOOBIG);
 20808         return;
 20809       }else{
 20810         p->nAlloc = (int)szNew;
 20812       if( p->useMalloc==1 ){
 20813         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
 20814       }else{
 20815         zNew = sqlite3_realloc(zOld, p->nAlloc);
 20817       if( zNew ){
 20818         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
 20819         p->zText = zNew;
 20820       }else{
 20821         sqlite3StrAccumReset(p);
 20822         setStrAccumError(p, STRACCUM_NOMEM);
 20823         return;
 20827   assert( p->zText );
 20828   memcpy(&p->zText[p->nChar], z, N);
 20829   p->nChar += N;
 20832 /*
 20833 ** Append the complete text of zero-terminated string z[] to the p string.
 20834 */
 20835 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
 20836   sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
 20840 /*
 20841 ** Finish off a string by making sure it is zero-terminated.
 20842 ** Return a pointer to the resulting string.  Return a NULL
 20843 ** pointer if any kind of error was encountered.
 20844 */
 20845 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
 20846   if( p->zText ){
 20847     p->zText[p->nChar] = 0;
 20848     if( p->useMalloc && p->zText==p->zBase ){
 20849       if( p->useMalloc==1 ){
 20850         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
 20851       }else{
 20852         p->zText = sqlite3_malloc(p->nChar+1);
 20854       if( p->zText ){
 20855         memcpy(p->zText, p->zBase, p->nChar+1);
 20856       }else{
 20857         setStrAccumError(p, STRACCUM_NOMEM);
 20861   return p->zText;
 20864 /*
 20865 ** Reset an StrAccum string.  Reclaim all malloced memory.
 20866 */
 20867 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
 20868   if( p->zText!=p->zBase ){
 20869     if( p->useMalloc==1 ){
 20870       sqlite3DbFree(p->db, p->zText);
 20871     }else{
 20872       sqlite3_free(p->zText);
 20875   p->zText = 0;
 20878 /*
 20879 ** Initialize a string accumulator
 20880 */
 20881 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
 20882   p->zText = p->zBase = zBase;
 20883   p->db = 0;
 20884   p->nChar = 0;
 20885   p->nAlloc = n;
 20886   p->mxAlloc = mx;
 20887   p->useMalloc = 1;
 20888   p->accError = 0;
 20891 /*
 20892 ** Print into memory obtained from sqliteMalloc().  Use the internal
 20893 ** %-conversion extensions.
 20894 */
 20895 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
 20896   char *z;
 20897   char zBase[SQLITE_PRINT_BUF_SIZE];
 20898   StrAccum acc;
 20899   assert( db!=0 );
 20900   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
 20901                       db->aLimit[SQLITE_LIMIT_LENGTH]);
 20902   acc.db = db;
 20903   sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
 20904   z = sqlite3StrAccumFinish(&acc);
 20905   if( acc.accError==STRACCUM_NOMEM ){
 20906     db->mallocFailed = 1;
 20908   return z;
 20911 /*
 20912 ** Print into memory obtained from sqliteMalloc().  Use the internal
 20913 ** %-conversion extensions.
 20914 */
 20915 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
 20916   va_list ap;
 20917   char *z;
 20918   va_start(ap, zFormat);
 20919   z = sqlite3VMPrintf(db, zFormat, ap);
 20920   va_end(ap);
 20921   return z;
 20924 /*
 20925 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
 20926 ** the string and before returnning.  This routine is intended to be used
 20927 ** to modify an existing string.  For example:
 20928 **
 20929 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
 20930 **
 20931 */
 20932 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
 20933   va_list ap;
 20934   char *z;
 20935   va_start(ap, zFormat);
 20936   z = sqlite3VMPrintf(db, zFormat, ap);
 20937   va_end(ap);
 20938   sqlite3DbFree(db, zStr);
 20939   return z;
 20942 /*
 20943 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
 20944 ** %-conversion extensions.
 20945 */
 20946 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
 20947   char *z;
 20948   char zBase[SQLITE_PRINT_BUF_SIZE];
 20949   StrAccum acc;
 20950 #ifndef SQLITE_OMIT_AUTOINIT
 20951   if( sqlite3_initialize() ) return 0;
 20952 #endif
 20953   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
 20954   acc.useMalloc = 2;
 20955   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20956   z = sqlite3StrAccumFinish(&acc);
 20957   return z;
 20960 /*
 20961 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
 20962 ** %-conversion extensions.
 20963 */
 20964 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
 20965   va_list ap;
 20966   char *z;
 20967 #ifndef SQLITE_OMIT_AUTOINIT
 20968   if( sqlite3_initialize() ) return 0;
 20969 #endif
 20970   va_start(ap, zFormat);
 20971   z = sqlite3_vmprintf(zFormat, ap);
 20972   va_end(ap);
 20973   return z;
 20976 /*
 20977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
 20978 ** current locale settings.  This is important for SQLite because we
 20979 ** are not able to use a "," as the decimal point in place of "." as
 20980 ** specified by some locales.
 20981 **
 20982 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
 20983 ** from the snprintf() standard.  Unfortunately, it is too late to change
 20984 ** this without breaking compatibility, so we just have to live with the
 20985 ** mistake.
 20986 **
 20987 ** sqlite3_vsnprintf() is the varargs version.
 20988 */
 20989 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
 20990   StrAccum acc;
 20991   if( n<=0 ) return zBuf;
 20992   sqlite3StrAccumInit(&acc, zBuf, n, 0);
 20993   acc.useMalloc = 0;
 20994   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 20995   return sqlite3StrAccumFinish(&acc);
 20997 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
 20998   char *z;
 20999   va_list ap;
 21000   va_start(ap,zFormat);
 21001   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
 21002   va_end(ap);
 21003   return z;
 21006 /*
 21007 ** This is the routine that actually formats the sqlite3_log() message.
 21008 ** We house it in a separate routine from sqlite3_log() to avoid using
 21009 ** stack space on small-stack systems when logging is disabled.
 21010 **
 21011 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
 21012 ** allocate memory because it might be called while the memory allocator
 21013 ** mutex is held.
 21014 */
 21015 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
 21016   StrAccum acc;                          /* String accumulator */
 21017   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
 21019   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
 21020   acc.useMalloc = 0;
 21021   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 21022   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
 21023                            sqlite3StrAccumFinish(&acc));
 21026 /*
 21027 ** Format and write a message to the log if logging is enabled.
 21028 */
 21029 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
 21030   va_list ap;                             /* Vararg list */
 21031   if( sqlite3GlobalConfig.xLog ){
 21032     va_start(ap, zFormat);
 21033     renderLogMsg(iErrCode, zFormat, ap);
 21034     va_end(ap);
 21038 #if defined(SQLITE_DEBUG)
 21039 /*
 21040 ** A version of printf() that understands %lld.  Used for debugging.
 21041 ** The printf() built into some versions of windows does not understand %lld
 21042 ** and segfaults if you give it a long long int.
 21043 */
 21044 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
 21045   va_list ap;
 21046   StrAccum acc;
 21047   char zBuf[500];
 21048   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
 21049   acc.useMalloc = 0;
 21050   va_start(ap,zFormat);
 21051   sqlite3VXPrintf(&acc, 0, zFormat, ap);
 21052   va_end(ap);
 21053   sqlite3StrAccumFinish(&acc);
 21054   fprintf(stdout,"%s", zBuf);
 21055   fflush(stdout);
 21057 #endif
 21059 /*
 21060 ** variable-argument wrapper around sqlite3VXPrintf().
 21061 */
 21062 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
 21063   va_list ap;
 21064   va_start(ap,zFormat);
 21065   sqlite3VXPrintf(p, bFlags, zFormat, ap);
 21066   va_end(ap);
 21069 /************** End of printf.c **********************************************/
 21070 /************** Begin file random.c ******************************************/
 21071 /*
 21072 ** 2001 September 15
 21073 **
 21074 ** The author disclaims copyright to this source code.  In place of
 21075 ** a legal notice, here is a blessing:
 21076 **
 21077 **    May you do good and not evil.
 21078 **    May you find forgiveness for yourself and forgive others.
 21079 **    May you share freely, never taking more than you give.
 21080 **
 21081 *************************************************************************
 21082 ** This file contains code to implement a pseudo-random number
 21083 ** generator (PRNG) for SQLite.
 21084 **
 21085 ** Random numbers are used by some of the database backends in order
 21086 ** to generate random integer keys for tables or random filenames.
 21087 */
 21090 /* All threads share a single random number generator.
 21091 ** This structure is the current state of the generator.
 21092 */
 21093 static SQLITE_WSD struct sqlite3PrngType {
 21094   unsigned char isInit;          /* True if initialized */
 21095   unsigned char i, j;            /* State variables */
 21096   unsigned char s[256];          /* State variables */
 21097 } sqlite3Prng;
 21099 /*
 21100 ** Return N random bytes.
 21101 */
 21102 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
 21103   unsigned char t;
 21104   unsigned char *zBuf = pBuf;
 21106   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
 21107   ** state vector.  If writable static data is unsupported on the target,
 21108   ** we have to locate the state vector at run-time.  In the more common
 21109   ** case where writable static data is supported, wsdPrng can refer directly
 21110   ** to the "sqlite3Prng" state vector declared above.
 21111   */
 21112 #ifdef SQLITE_OMIT_WSD
 21113   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
 21114 # define wsdPrng p[0]
 21115 #else
 21116 # define wsdPrng sqlite3Prng
 21117 #endif
 21119 #if SQLITE_THREADSAFE
 21120   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
 21121   sqlite3_mutex_enter(mutex);
 21122 #endif
 21124   if( N<=0 ){
 21125     wsdPrng.isInit = 0;
 21126     sqlite3_mutex_leave(mutex);
 21127     return;
 21130   /* Initialize the state of the random number generator once,
 21131   ** the first time this routine is called.  The seed value does
 21132   ** not need to contain a lot of randomness since we are not
 21133   ** trying to do secure encryption or anything like that...
 21134   **
 21135   ** Nothing in this file or anywhere else in SQLite does any kind of
 21136   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
 21137   ** number generator) not as an encryption device.
 21138   */
 21139   if( !wsdPrng.isInit ){
 21140     int i;
 21141     char k[256];
 21142     wsdPrng.j = 0;
 21143     wsdPrng.i = 0;
 21144     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
 21145     for(i=0; i<256; i++){
 21146       wsdPrng.s[i] = (u8)i;
 21148     for(i=0; i<256; i++){
 21149       wsdPrng.j += wsdPrng.s[i] + k[i];
 21150       t = wsdPrng.s[wsdPrng.j];
 21151       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
 21152       wsdPrng.s[i] = t;
 21154     wsdPrng.isInit = 1;
 21157   assert( N>0 );
 21158   do{
 21159     wsdPrng.i++;
 21160     t = wsdPrng.s[wsdPrng.i];
 21161     wsdPrng.j += t;
 21162     wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
 21163     wsdPrng.s[wsdPrng.j] = t;
 21164     t += wsdPrng.s[wsdPrng.i];
 21165     *(zBuf++) = wsdPrng.s[t];
 21166   }while( --N );
 21167   sqlite3_mutex_leave(mutex);
 21170 #ifndef SQLITE_OMIT_BUILTIN_TEST
 21171 /*
 21172 ** For testing purposes, we sometimes want to preserve the state of
 21173 ** PRNG and restore the PRNG to its saved state at a later time, or
 21174 ** to reset the PRNG to its initial state.  These routines accomplish
 21175 ** those tasks.
 21176 **
 21177 ** The sqlite3_test_control() interface calls these routines to
 21178 ** control the PRNG.
 21179 */
 21180 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
 21181 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
 21182   memcpy(
 21183     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 21184     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 21185     sizeof(sqlite3Prng)
 21186   );
 21188 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
 21189   memcpy(
 21190     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
 21191     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
 21192     sizeof(sqlite3Prng)
 21193   );
 21195 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 21197 /************** End of random.c **********************************************/
 21198 /************** Begin file utf.c *********************************************/
 21199 /*
 21200 ** 2004 April 13
 21201 **
 21202 ** The author disclaims copyright to this source code.  In place of
 21203 ** a legal notice, here is a blessing:
 21204 **
 21205 **    May you do good and not evil.
 21206 **    May you find forgiveness for yourself and forgive others.
 21207 **    May you share freely, never taking more than you give.
 21208 **
 21209 *************************************************************************
 21210 ** This file contains routines used to translate between UTF-8, 
 21211 ** UTF-16, UTF-16BE, and UTF-16LE.
 21212 **
 21213 ** Notes on UTF-8:
 21214 **
 21215 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
 21216 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
 21217 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
 21218 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
 21219 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
 21220 **
 21221 **
 21222 ** Notes on UTF-16:  (with wwww+1==uuuuu)
 21223 **
 21224 **      Word-0               Word-1          Value
 21225 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
 21226 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
 21227 **
 21228 **
 21229 ** BOM or Byte Order Mark:
 21230 **     0xff 0xfe   little-endian utf-16 follows
 21231 **     0xfe 0xff   big-endian utf-16 follows
 21232 **
 21233 */
 21234 /* #include <assert.h> */
 21236 #ifndef SQLITE_AMALGAMATION
 21237 /*
 21238 ** The following constant value is used by the SQLITE_BIGENDIAN and
 21239 ** SQLITE_LITTLEENDIAN macros.
 21240 */
 21241 SQLITE_PRIVATE const int sqlite3one = 1;
 21242 #endif /* SQLITE_AMALGAMATION */
 21244 /*
 21245 ** This lookup table is used to help decode the first byte of
 21246 ** a multi-byte UTF8 character.
 21247 */
 21248 static const unsigned char sqlite3Utf8Trans1[] = {
 21249   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 21250   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 21251   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 21252   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 21253   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 21254   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 21255   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 21256   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
 21257 };
 21260 #define WRITE_UTF8(zOut, c) {                          \
 21261   if( c<0x00080 ){                                     \
 21262     *zOut++ = (u8)(c&0xFF);                            \
 21263   }                                                    \
 21264   else if( c<0x00800 ){                                \
 21265     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
 21266     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 21267   }                                                    \
 21268   else if( c<0x10000 ){                                \
 21269     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
 21270     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 21271     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 21272   }else{                                               \
 21273     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
 21274     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
 21275     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 21276     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 21277   }                                                    \
 21280 #define WRITE_UTF16LE(zOut, c) {                                    \
 21281   if( c<=0xFFFF ){                                                  \
 21282     *zOut++ = (u8)(c&0x00FF);                                       \
 21283     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 21284   }else{                                                            \
 21285     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 21286     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 21287     *zOut++ = (u8)(c&0x00FF);                                       \
 21288     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 21289   }                                                                 \
 21292 #define WRITE_UTF16BE(zOut, c) {                                    \
 21293   if( c<=0xFFFF ){                                                  \
 21294     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
 21295     *zOut++ = (u8)(c&0x00FF);                                       \
 21296   }else{                                                            \
 21297     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
 21298     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
 21299     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
 21300     *zOut++ = (u8)(c&0x00FF);                                       \
 21301   }                                                                 \
 21304 #define READ_UTF16LE(zIn, TERM, c){                                   \
 21305   c = (*zIn++);                                                       \
 21306   c += ((*zIn++)<<8);                                                 \
 21307   if( c>=0xD800 && c<0xE000 && TERM ){                                \
 21308     int c2 = (*zIn++);                                                \
 21309     c2 += ((*zIn++)<<8);                                              \
 21310     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 21311   }                                                                   \
 21314 #define READ_UTF16BE(zIn, TERM, c){                                   \
 21315   c = ((*zIn++)<<8);                                                  \
 21316   c += (*zIn++);                                                      \
 21317   if( c>=0xD800 && c<0xE000 && TERM ){                                \
 21318     int c2 = ((*zIn++)<<8);                                           \
 21319     c2 += (*zIn++);                                                   \
 21320     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
 21321   }                                                                   \
 21324 /*
 21325 ** Translate a single UTF-8 character.  Return the unicode value.
 21326 **
 21327 ** During translation, assume that the byte that zTerm points
 21328 ** is a 0x00.
 21329 **
 21330 ** Write a pointer to the next unread byte back into *pzNext.
 21331 **
 21332 ** Notes On Invalid UTF-8:
 21333 **
 21334 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
 21335 **     be encoded as a multi-byte character.  Any multi-byte character that
 21336 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
 21337 **
 21338 **  *  This routine never allows a UTF16 surrogate value to be encoded.
 21339 **     If a multi-byte character attempts to encode a value between
 21340 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
 21341 **
 21342 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
 21343 **     byte of a character are interpreted as single-byte characters
 21344 **     and rendered as themselves even though they are technically
 21345 **     invalid characters.
 21346 **
 21347 **  *  This routine accepts an infinite number of different UTF8 encodings
 21348 **     for unicode values 0x80 and greater.  It do not change over-length
 21349 **     encodings to 0xfffd as some systems recommend.
 21350 */
 21351 #define READ_UTF8(zIn, zTerm, c)                           \
 21352   c = *(zIn++);                                            \
 21353   if( c>=0xc0 ){                                           \
 21354     c = sqlite3Utf8Trans1[c-0xc0];                         \
 21355     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
 21356       c = (c<<6) + (0x3f & *(zIn++));                      \
 21357     }                                                      \
 21358     if( c<0x80                                             \
 21359         || (c&0xFFFFF800)==0xD800                          \
 21360         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 21362 SQLITE_PRIVATE u32 sqlite3Utf8Read(
 21363   const unsigned char **pz    /* Pointer to string from which to read char */
 21364 ){
 21365   unsigned int c;
 21367   /* Same as READ_UTF8() above but without the zTerm parameter.
 21368   ** For this routine, we assume the UTF8 string is always zero-terminated.
 21369   */
 21370   c = *((*pz)++);
 21371   if( c>=0xc0 ){
 21372     c = sqlite3Utf8Trans1[c-0xc0];
 21373     while( (*(*pz) & 0xc0)==0x80 ){
 21374       c = (c<<6) + (0x3f & *((*pz)++));
 21376     if( c<0x80
 21377         || (c&0xFFFFF800)==0xD800
 21378         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
 21380   return c;
 21386 /*
 21387 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
 21388 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
 21389 */ 
 21390 /* #define TRANSLATE_TRACE 1 */
 21392 #ifndef SQLITE_OMIT_UTF16
 21393 /*
 21394 ** This routine transforms the internal text encoding used by pMem to
 21395 ** desiredEnc. It is an error if the string is already of the desired
 21396 ** encoding, or if *pMem does not contain a string value.
 21397 */
 21398 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
 21399   int len;                    /* Maximum length of output string in bytes */
 21400   unsigned char *zOut;                  /* Output buffer */
 21401   unsigned char *zIn;                   /* Input iterator */
 21402   unsigned char *zTerm;                 /* End of input */
 21403   unsigned char *z;                     /* Output iterator */
 21404   unsigned int c;
 21406   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 21407   assert( pMem->flags&MEM_Str );
 21408   assert( pMem->enc!=desiredEnc );
 21409   assert( pMem->enc!=0 );
 21410   assert( pMem->n>=0 );
 21412 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 21414     char zBuf[100];
 21415     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 21416     fprintf(stderr, "INPUT:  %s\n", zBuf);
 21418 #endif
 21420   /* If the translation is between UTF-16 little and big endian, then 
 21421   ** all that is required is to swap the byte order. This case is handled
 21422   ** differently from the others.
 21423   */
 21424   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
 21425     u8 temp;
 21426     int rc;
 21427     rc = sqlite3VdbeMemMakeWriteable(pMem);
 21428     if( rc!=SQLITE_OK ){
 21429       assert( rc==SQLITE_NOMEM );
 21430       return SQLITE_NOMEM;
 21432     zIn = (u8*)pMem->z;
 21433     zTerm = &zIn[pMem->n&~1];
 21434     while( zIn<zTerm ){
 21435       temp = *zIn;
 21436       *zIn = *(zIn+1);
 21437       zIn++;
 21438       *zIn++ = temp;
 21440     pMem->enc = desiredEnc;
 21441     goto translate_out;
 21444   /* Set len to the maximum number of bytes required in the output buffer. */
 21445   if( desiredEnc==SQLITE_UTF8 ){
 21446     /* When converting from UTF-16, the maximum growth results from
 21447     ** translating a 2-byte character to a 4-byte UTF-8 character.
 21448     ** A single byte is required for the output string
 21449     ** nul-terminator.
 21450     */
 21451     pMem->n &= ~1;
 21452     len = pMem->n * 2 + 1;
 21453   }else{
 21454     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
 21455     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
 21456     ** character. Two bytes are required in the output buffer for the
 21457     ** nul-terminator.
 21458     */
 21459     len = pMem->n * 2 + 2;
 21462   /* Set zIn to point at the start of the input buffer and zTerm to point 1
 21463   ** byte past the end.
 21464   **
 21465   ** Variable zOut is set to point at the output buffer, space obtained
 21466   ** from sqlite3_malloc().
 21467   */
 21468   zIn = (u8*)pMem->z;
 21469   zTerm = &zIn[pMem->n];
 21470   zOut = sqlite3DbMallocRaw(pMem->db, len);
 21471   if( !zOut ){
 21472     return SQLITE_NOMEM;
 21474   z = zOut;
 21476   if( pMem->enc==SQLITE_UTF8 ){
 21477     if( desiredEnc==SQLITE_UTF16LE ){
 21478       /* UTF-8 -> UTF-16 Little-endian */
 21479       while( zIn<zTerm ){
 21480         READ_UTF8(zIn, zTerm, c);
 21481         WRITE_UTF16LE(z, c);
 21483     }else{
 21484       assert( desiredEnc==SQLITE_UTF16BE );
 21485       /* UTF-8 -> UTF-16 Big-endian */
 21486       while( zIn<zTerm ){
 21487         READ_UTF8(zIn, zTerm, c);
 21488         WRITE_UTF16BE(z, c);
 21491     pMem->n = (int)(z - zOut);
 21492     *z++ = 0;
 21493   }else{
 21494     assert( desiredEnc==SQLITE_UTF8 );
 21495     if( pMem->enc==SQLITE_UTF16LE ){
 21496       /* UTF-16 Little-endian -> UTF-8 */
 21497       while( zIn<zTerm ){
 21498         READ_UTF16LE(zIn, zIn<zTerm, c); 
 21499         WRITE_UTF8(z, c);
 21501     }else{
 21502       /* UTF-16 Big-endian -> UTF-8 */
 21503       while( zIn<zTerm ){
 21504         READ_UTF16BE(zIn, zIn<zTerm, c); 
 21505         WRITE_UTF8(z, c);
 21508     pMem->n = (int)(z - zOut);
 21510   *z = 0;
 21511   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
 21513   sqlite3VdbeMemRelease(pMem);
 21514   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
 21515   pMem->enc = desiredEnc;
 21516   pMem->flags |= (MEM_Term);
 21517   pMem->z = (char*)zOut;
 21518   pMem->zMalloc = pMem->z;
 21520 translate_out:
 21521 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
 21523     char zBuf[100];
 21524     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
 21525     fprintf(stderr, "OUTPUT: %s\n", zBuf);
 21527 #endif
 21528   return SQLITE_OK;
 21531 /*
 21532 ** This routine checks for a byte-order mark at the beginning of the 
 21533 ** UTF-16 string stored in *pMem. If one is present, it is removed and
 21534 ** the encoding of the Mem adjusted. This routine does not do any
 21535 ** byte-swapping, it just sets Mem.enc appropriately.
 21536 **
 21537 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
 21538 ** changed by this function.
 21539 */
 21540 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
 21541   int rc = SQLITE_OK;
 21542   u8 bom = 0;
 21544   assert( pMem->n>=0 );
 21545   if( pMem->n>1 ){
 21546     u8 b1 = *(u8 *)pMem->z;
 21547     u8 b2 = *(((u8 *)pMem->z) + 1);
 21548     if( b1==0xFE && b2==0xFF ){
 21549       bom = SQLITE_UTF16BE;
 21551     if( b1==0xFF && b2==0xFE ){
 21552       bom = SQLITE_UTF16LE;
 21556   if( bom ){
 21557     rc = sqlite3VdbeMemMakeWriteable(pMem);
 21558     if( rc==SQLITE_OK ){
 21559       pMem->n -= 2;
 21560       memmove(pMem->z, &pMem->z[2], pMem->n);
 21561       pMem->z[pMem->n] = '\0';
 21562       pMem->z[pMem->n+1] = '\0';
 21563       pMem->flags |= MEM_Term;
 21564       pMem->enc = bom;
 21567   return rc;
 21569 #endif /* SQLITE_OMIT_UTF16 */
 21571 /*
 21572 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
 21573 ** return the number of unicode characters in pZ up to (but not including)
 21574 ** the first 0x00 byte. If nByte is not less than zero, return the
 21575 ** number of unicode characters in the first nByte of pZ (or up to 
 21576 ** the first 0x00, whichever comes first).
 21577 */
 21578 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
 21579   int r = 0;
 21580   const u8 *z = (const u8*)zIn;
 21581   const u8 *zTerm;
 21582   if( nByte>=0 ){
 21583     zTerm = &z[nByte];
 21584   }else{
 21585     zTerm = (const u8*)(-1);
 21587   assert( z<=zTerm );
 21588   while( *z!=0 && z<zTerm ){
 21589     SQLITE_SKIP_UTF8(z);
 21590     r++;
 21592   return r;
 21595 /* This test function is not currently used by the automated test-suite. 
 21596 ** Hence it is only available in debug builds.
 21597 */
 21598 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 21599 /*
 21600 ** Translate UTF-8 to UTF-8.
 21601 **
 21602 ** This has the effect of making sure that the string is well-formed
 21603 ** UTF-8.  Miscoded characters are removed.
 21604 **
 21605 ** The translation is done in-place and aborted if the output
 21606 ** overruns the input.
 21607 */
 21608 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
 21609   unsigned char *zOut = zIn;
 21610   unsigned char *zStart = zIn;
 21611   u32 c;
 21613   while( zIn[0] && zOut<=zIn ){
 21614     c = sqlite3Utf8Read((const u8**)&zIn);
 21615     if( c!=0xfffd ){
 21616       WRITE_UTF8(zOut, c);
 21619   *zOut = 0;
 21620   return (int)(zOut - zStart);
 21622 #endif
 21624 #ifndef SQLITE_OMIT_UTF16
 21625 /*
 21626 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
 21627 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
 21628 ** be freed by the calling function.
 21629 **
 21630 ** NULL is returned if there is an allocation error.
 21631 */
 21632 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
 21633   Mem m;
 21634   memset(&m, 0, sizeof(m));
 21635   m.db = db;
 21636   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
 21637   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
 21638   if( db->mallocFailed ){
 21639     sqlite3VdbeMemRelease(&m);
 21640     m.z = 0;
 21642   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
 21643   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 21644   assert( m.z || db->mallocFailed );
 21645   return m.z;
 21648 /*
 21649 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
 21650 ** Return the number of bytes in the first nChar unicode characters
 21651 ** in pZ.  nChar must be non-negative.
 21652 */
 21653 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
 21654   int c;
 21655   unsigned char const *z = zIn;
 21656   int n = 0;
 21658   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
 21659     while( n<nChar ){
 21660       READ_UTF16BE(z, 1, c);
 21661       n++;
 21663   }else{
 21664     while( n<nChar ){
 21665       READ_UTF16LE(z, 1, c);
 21666       n++;
 21669   return (int)(z-(unsigned char const *)zIn);
 21672 #if defined(SQLITE_TEST)
 21673 /*
 21674 ** This routine is called from the TCL test function "translate_selftest".
 21675 ** It checks that the primitives for serializing and deserializing
 21676 ** characters in each encoding are inverses of each other.
 21677 */
 21678 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
 21679   unsigned int i, t;
 21680   unsigned char zBuf[20];
 21681   unsigned char *z;
 21682   int n;
 21683   unsigned int c;
 21685   for(i=0; i<0x00110000; i++){
 21686     z = zBuf;
 21687     WRITE_UTF8(z, i);
 21688     n = (int)(z-zBuf);
 21689     assert( n>0 && n<=4 );
 21690     z[0] = 0;
 21691     z = zBuf;
 21692     c = sqlite3Utf8Read((const u8**)&z);
 21693     t = i;
 21694     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
 21695     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
 21696     assert( c==t );
 21697     assert( (z-zBuf)==n );
 21699   for(i=0; i<0x00110000; i++){
 21700     if( i>=0xD800 && i<0xE000 ) continue;
 21701     z = zBuf;
 21702     WRITE_UTF16LE(z, i);
 21703     n = (int)(z-zBuf);
 21704     assert( n>0 && n<=4 );
 21705     z[0] = 0;
 21706     z = zBuf;
 21707     READ_UTF16LE(z, 1, c);
 21708     assert( c==i );
 21709     assert( (z-zBuf)==n );
 21711   for(i=0; i<0x00110000; i++){
 21712     if( i>=0xD800 && i<0xE000 ) continue;
 21713     z = zBuf;
 21714     WRITE_UTF16BE(z, i);
 21715     n = (int)(z-zBuf);
 21716     assert( n>0 && n<=4 );
 21717     z[0] = 0;
 21718     z = zBuf;
 21719     READ_UTF16BE(z, 1, c);
 21720     assert( c==i );
 21721     assert( (z-zBuf)==n );
 21724 #endif /* SQLITE_TEST */
 21725 #endif /* SQLITE_OMIT_UTF16 */
 21727 /************** End of utf.c *************************************************/
 21728 /************** Begin file util.c ********************************************/
 21729 /*
 21730 ** 2001 September 15
 21731 **
 21732 ** The author disclaims copyright to this source code.  In place of
 21733 ** a legal notice, here is a blessing:
 21734 **
 21735 **    May you do good and not evil.
 21736 **    May you find forgiveness for yourself and forgive others.
 21737 **    May you share freely, never taking more than you give.
 21738 **
 21739 *************************************************************************
 21740 ** Utility functions used throughout sqlite.
 21741 **
 21742 ** This file contains functions for allocating memory, comparing
 21743 ** strings, and stuff like that.
 21744 **
 21745 */
 21746 /* #include <stdarg.h> */
 21747 #ifdef SQLITE_HAVE_ISNAN
 21748 # include <math.h>
 21749 #endif
 21751 /*
 21752 ** Routine needed to support the testcase() macro.
 21753 */
 21754 #ifdef SQLITE_COVERAGE_TEST
 21755 SQLITE_PRIVATE void sqlite3Coverage(int x){
 21756   static unsigned dummy = 0;
 21757   dummy += (unsigned)x;
 21759 #endif
 21761 #ifndef SQLITE_OMIT_FLOATING_POINT
 21762 /*
 21763 ** Return true if the floating point value is Not a Number (NaN).
 21764 **
 21765 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
 21766 ** Otherwise, we have our own implementation that works on most systems.
 21767 */
 21768 SQLITE_PRIVATE int sqlite3IsNaN(double x){
 21769   int rc;   /* The value return */
 21770 #if !defined(SQLITE_HAVE_ISNAN)
 21771   /*
 21772   ** Systems that support the isnan() library function should probably
 21773   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
 21774   ** found that many systems do not have a working isnan() function so
 21775   ** this implementation is provided as an alternative.
 21776   **
 21777   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
 21778   ** On the other hand, the use of -ffast-math comes with the following
 21779   ** warning:
 21780   **
 21781   **      This option [-ffast-math] should never be turned on by any
 21782   **      -O option since it can result in incorrect output for programs
 21783   **      which depend on an exact implementation of IEEE or ISO 
 21784   **      rules/specifications for math functions.
 21785   **
 21786   ** Under MSVC, this NaN test may fail if compiled with a floating-
 21787   ** point precision mode other than /fp:precise.  From the MSDN 
 21788   ** documentation:
 21789   **
 21790   **      The compiler [with /fp:precise] will properly handle comparisons 
 21791   **      involving NaN. For example, x != x evaluates to true if x is NaN 
 21792   **      ...
 21793   */
 21794 #ifdef __FAST_MATH__
 21795 # error SQLite will not work correctly with the -ffast-math option of GCC.
 21796 #endif
 21797   volatile double y = x;
 21798   volatile double z = y;
 21799   rc = (y!=z);
 21800 #else  /* if defined(SQLITE_HAVE_ISNAN) */
 21801   rc = isnan(x);
 21802 #endif /* SQLITE_HAVE_ISNAN */
 21803   testcase( rc );
 21804   return rc;
 21806 #endif /* SQLITE_OMIT_FLOATING_POINT */
 21808 /*
 21809 ** Compute a string length that is limited to what can be stored in
 21810 ** lower 30 bits of a 32-bit signed integer.
 21811 **
 21812 ** The value returned will never be negative.  Nor will it ever be greater
 21813 ** than the actual length of the string.  For very long strings (greater
 21814 ** than 1GiB) the value returned might be less than the true string length.
 21815 */
 21816 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
 21817   const char *z2 = z;
 21818   if( z==0 ) return 0;
 21819   while( *z2 ){ z2++; }
 21820   return 0x3fffffff & (int)(z2 - z);
 21823 /*
 21824 ** Set the most recent error code and error string for the sqlite
 21825 ** handle "db". The error code is set to "err_code".
 21826 **
 21827 ** If it is not NULL, string zFormat specifies the format of the
 21828 ** error string in the style of the printf functions: The following
 21829 ** format characters are allowed:
 21830 **
 21831 **      %s      Insert a string
 21832 **      %z      A string that should be freed after use
 21833 **      %d      Insert an integer
 21834 **      %T      Insert a token
 21835 **      %S      Insert the first element of a SrcList
 21836 **
 21837 ** zFormat and any string tokens that follow it are assumed to be
 21838 ** encoded in UTF-8.
 21839 **
 21840 ** To clear the most recent error for sqlite handle "db", sqlite3Error
 21841 ** should be called with err_code set to SQLITE_OK and zFormat set
 21842 ** to NULL.
 21843 */
 21844 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
 21845   assert( db!=0 );
 21846   db->errCode = err_code;
 21847   if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
 21848     char *z;
 21849     va_list ap;
 21850     va_start(ap, zFormat);
 21851     z = sqlite3VMPrintf(db, zFormat, ap);
 21852     va_end(ap);
 21853     sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
 21854   }else if( db->pErr ){
 21855     sqlite3ValueSetNull(db->pErr);
 21859 /*
 21860 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
 21861 ** The following formatting characters are allowed:
 21862 **
 21863 **      %s      Insert a string
 21864 **      %z      A string that should be freed after use
 21865 **      %d      Insert an integer
 21866 **      %T      Insert a token
 21867 **      %S      Insert the first element of a SrcList
 21868 **
 21869 ** This function should be used to report any error that occurs whilst
 21870 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
 21871 ** last thing the sqlite3_prepare() function does is copy the error
 21872 ** stored by this function into the database handle using sqlite3Error().
 21873 ** Function sqlite3Error() should be used during statement execution
 21874 ** (sqlite3_step() etc.).
 21875 */
 21876 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
 21877   char *zMsg;
 21878   va_list ap;
 21879   sqlite3 *db = pParse->db;
 21880   va_start(ap, zFormat);
 21881   zMsg = sqlite3VMPrintf(db, zFormat, ap);
 21882   va_end(ap);
 21883   if( db->suppressErr ){
 21884     sqlite3DbFree(db, zMsg);
 21885   }else{
 21886     pParse->nErr++;
 21887     sqlite3DbFree(db, pParse->zErrMsg);
 21888     pParse->zErrMsg = zMsg;
 21889     pParse->rc = SQLITE_ERROR;
 21893 /*
 21894 ** Convert an SQL-style quoted string into a normal string by removing
 21895 ** the quote characters.  The conversion is done in-place.  If the
 21896 ** input does not begin with a quote character, then this routine
 21897 ** is a no-op.
 21898 **
 21899 ** The input string must be zero-terminated.  A new zero-terminator
 21900 ** is added to the dequoted string.
 21901 **
 21902 ** The return value is -1 if no dequoting occurs or the length of the
 21903 ** dequoted string, exclusive of the zero terminator, if dequoting does
 21904 ** occur.
 21905 **
 21906 ** 2002-Feb-14: This routine is extended to remove MS-Access style
 21907 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
 21908 ** "a-b-c".
 21909 */
 21910 SQLITE_PRIVATE int sqlite3Dequote(char *z){
 21911   char quote;
 21912   int i, j;
 21913   if( z==0 ) return -1;
 21914   quote = z[0];
 21915   switch( quote ){
 21916     case '\'':  break;
 21917     case '"':   break;
 21918     case '`':   break;                /* For MySQL compatibility */
 21919     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
 21920     default:    return -1;
 21922   for(i=1, j=0;; i++){
 21923     assert( z[i] );
 21924     if( z[i]==quote ){
 21925       if( z[i+1]==quote ){
 21926         z[j++] = quote;
 21927         i++;
 21928       }else{
 21929         break;
 21931     }else{
 21932       z[j++] = z[i];
 21935   z[j] = 0;
 21936   return j;
 21939 /* Convenient short-hand */
 21940 #define UpperToLower sqlite3UpperToLower
 21942 /*
 21943 ** Some systems have stricmp().  Others have strcasecmp().  Because
 21944 ** there is no consistency, we will define our own.
 21945 **
 21946 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
 21947 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
 21948 ** the contents of two buffers containing UTF-8 strings in a
 21949 ** case-independent fashion, using the same definition of "case
 21950 ** independence" that SQLite uses internally when comparing identifiers.
 21951 */
 21952 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
 21953   register unsigned char *a, *b;
 21954   a = (unsigned char *)zLeft;
 21955   b = (unsigned char *)zRight;
 21956   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 21957   return UpperToLower[*a] - UpperToLower[*b];
 21959 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
 21960   register unsigned char *a, *b;
 21961   a = (unsigned char *)zLeft;
 21962   b = (unsigned char *)zRight;
 21963   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
 21964   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
 21967 /*
 21968 ** The string z[] is an text representation of a real number.
 21969 ** Convert this string to a double and write it into *pResult.
 21970 **
 21971 ** The string z[] is length bytes in length (bytes, not characters) and
 21972 ** uses the encoding enc.  The string is not necessarily zero-terminated.
 21973 **
 21974 ** Return TRUE if the result is a valid real number (or integer) and FALSE
 21975 ** if the string is empty or contains extraneous text.  Valid numbers
 21976 ** are in one of these formats:
 21977 **
 21978 **    [+-]digits[E[+-]digits]
 21979 **    [+-]digits.[digits][E[+-]digits]
 21980 **    [+-].digits[E[+-]digits]
 21981 **
 21982 ** Leading and trailing whitespace is ignored for the purpose of determining
 21983 ** validity.
 21984 **
 21985 ** If some prefix of the input string is a valid number, this routine
 21986 ** returns FALSE but it still converts the prefix and writes the result
 21987 ** into *pResult.
 21988 */
 21989 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
 21990 #ifndef SQLITE_OMIT_FLOATING_POINT
 21991   int incr;
 21992   const char *zEnd = z + length;
 21993   /* sign * significand * (10 ^ (esign * exponent)) */
 21994   int sign = 1;    /* sign of significand */
 21995   i64 s = 0;       /* significand */
 21996   int d = 0;       /* adjust exponent for shifting decimal point */
 21997   int esign = 1;   /* sign of exponent */
 21998   int e = 0;       /* exponent */
 21999   int eValid = 1;  /* True exponent is either not used or is well-formed */
 22000   double result;
 22001   int nDigits = 0;
 22002   int nonNum = 0;
 22004   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 22005   *pResult = 0.0;   /* Default return value, in case of an error */
 22007   if( enc==SQLITE_UTF8 ){
 22008     incr = 1;
 22009   }else{
 22010     int i;
 22011     incr = 2;
 22012     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
 22013     for(i=3-enc; i<length && z[i]==0; i+=2){}
 22014     nonNum = i<length;
 22015     zEnd = z+i+enc-3;
 22016     z += (enc&1);
 22019   /* skip leading spaces */
 22020   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 22021   if( z>=zEnd ) return 0;
 22023   /* get sign of significand */
 22024   if( *z=='-' ){
 22025     sign = -1;
 22026     z+=incr;
 22027   }else if( *z=='+' ){
 22028     z+=incr;
 22031   /* skip leading zeroes */
 22032   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
 22034   /* copy max significant digits to significand */
 22035   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 22036     s = s*10 + (*z - '0');
 22037     z+=incr, nDigits++;
 22040   /* skip non-significant significand digits
 22041   ** (increase exponent by d to shift decimal left) */
 22042   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
 22043   if( z>=zEnd ) goto do_atof_calc;
 22045   /* if decimal point is present */
 22046   if( *z=='.' ){
 22047     z+=incr;
 22048     /* copy digits from after decimal to significand
 22049     ** (decrease exponent by d to shift decimal right) */
 22050     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
 22051       s = s*10 + (*z - '0');
 22052       z+=incr, nDigits++, d--;
 22054     /* skip non-significant digits */
 22055     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
 22057   if( z>=zEnd ) goto do_atof_calc;
 22059   /* if exponent is present */
 22060   if( *z=='e' || *z=='E' ){
 22061     z+=incr;
 22062     eValid = 0;
 22063     if( z>=zEnd ) goto do_atof_calc;
 22064     /* get sign of exponent */
 22065     if( *z=='-' ){
 22066       esign = -1;
 22067       z+=incr;
 22068     }else if( *z=='+' ){
 22069       z+=incr;
 22071     /* copy digits to exponent */
 22072     while( z<zEnd && sqlite3Isdigit(*z) ){
 22073       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
 22074       z+=incr;
 22075       eValid = 1;
 22079   /* skip trailing spaces */
 22080   if( nDigits && eValid ){
 22081     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
 22084 do_atof_calc:
 22085   /* adjust exponent by d, and update sign */
 22086   e = (e*esign) + d;
 22087   if( e<0 ) {
 22088     esign = -1;
 22089     e *= -1;
 22090   } else {
 22091     esign = 1;
 22094   /* if 0 significand */
 22095   if( !s ) {
 22096     /* In the IEEE 754 standard, zero is signed.
 22097     ** Add the sign if we've seen at least one digit */
 22098     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
 22099   } else {
 22100     /* attempt to reduce exponent */
 22101     if( esign>0 ){
 22102       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
 22103     }else{
 22104       while( !(s%10) && e>0 ) e--,s/=10;
 22107     /* adjust the sign of significand */
 22108     s = sign<0 ? -s : s;
 22110     /* if exponent, scale significand as appropriate
 22111     ** and store in result. */
 22112     if( e ){
 22113       LONGDOUBLE_TYPE scale = 1.0;
 22114       /* attempt to handle extremely small/large numbers better */
 22115       if( e>307 && e<342 ){
 22116         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
 22117         if( esign<0 ){
 22118           result = s / scale;
 22119           result /= 1.0e+308;
 22120         }else{
 22121           result = s * scale;
 22122           result *= 1.0e+308;
 22124       }else if( e>=342 ){
 22125         if( esign<0 ){
 22126           result = 0.0*s;
 22127         }else{
 22128           result = 1e308*1e308*s;  /* Infinity */
 22130       }else{
 22131         /* 1.0e+22 is the largest power of 10 than can be 
 22132         ** represented exactly. */
 22133         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
 22134         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
 22135         if( esign<0 ){
 22136           result = s / scale;
 22137         }else{
 22138           result = s * scale;
 22141     } else {
 22142       result = (double)s;
 22146   /* store the result */
 22147   *pResult = result;
 22149   /* return true if number and no extra non-whitespace chracters after */
 22150   return z>=zEnd && nDigits>0 && eValid && nonNum==0;
 22151 #else
 22152   return !sqlite3Atoi64(z, pResult, length, enc);
 22153 #endif /* SQLITE_OMIT_FLOATING_POINT */
 22156 /*
 22157 ** Compare the 19-character string zNum against the text representation
 22158 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
 22159 ** if zNum is less than, equal to, or greater than the string.
 22160 ** Note that zNum must contain exactly 19 characters.
 22161 **
 22162 ** Unlike memcmp() this routine is guaranteed to return the difference
 22163 ** in the values of the last digit if the only difference is in the
 22164 ** last digit.  So, for example,
 22165 **
 22166 **      compare2pow63("9223372036854775800", 1)
 22167 **
 22168 ** will return -8.
 22169 */
 22170 static int compare2pow63(const char *zNum, int incr){
 22171   int c = 0;
 22172   int i;
 22173                     /* 012345678901234567 */
 22174   const char *pow63 = "922337203685477580";
 22175   for(i=0; c==0 && i<18; i++){
 22176     c = (zNum[i*incr]-pow63[i])*10;
 22178   if( c==0 ){
 22179     c = zNum[18*incr] - '8';
 22180     testcase( c==(-1) );
 22181     testcase( c==0 );
 22182     testcase( c==(+1) );
 22184   return c;
 22188 /*
 22189 ** Convert zNum to a 64-bit signed integer.
 22190 **
 22191 ** If the zNum value is representable as a 64-bit twos-complement 
 22192 ** integer, then write that value into *pNum and return 0.
 22193 **
 22194 ** If zNum is exactly 9223372036854775808, return 2.  This special
 22195 ** case is broken out because while 9223372036854775808 cannot be a 
 22196 ** signed 64-bit integer, its negative -9223372036854775808 can be.
 22197 **
 22198 ** If zNum is too big for a 64-bit integer and is not
 22199 ** 9223372036854775808  or if zNum contains any non-numeric text,
 22200 ** then return 1.
 22201 **
 22202 ** length is the number of bytes in the string (bytes, not characters).
 22203 ** The string is not necessarily zero-terminated.  The encoding is
 22204 ** given by enc.
 22205 */
 22206 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
 22207   int incr;
 22208   u64 u = 0;
 22209   int neg = 0; /* assume positive */
 22210   int i;
 22211   int c = 0;
 22212   int nonNum = 0;
 22213   const char *zStart;
 22214   const char *zEnd = zNum + length;
 22215   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
 22216   if( enc==SQLITE_UTF8 ){
 22217     incr = 1;
 22218   }else{
 22219     incr = 2;
 22220     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
 22221     for(i=3-enc; i<length && zNum[i]==0; i+=2){}
 22222     nonNum = i<length;
 22223     zEnd = zNum+i+enc-3;
 22224     zNum += (enc&1);
 22226   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
 22227   if( zNum<zEnd ){
 22228     if( *zNum=='-' ){
 22229       neg = 1;
 22230       zNum+=incr;
 22231     }else if( *zNum=='+' ){
 22232       zNum+=incr;
 22235   zStart = zNum;
 22236   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 22237   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 22238     u = u*10 + c - '0';
 22240   if( u>LARGEST_INT64 ){
 22241     *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
 22242   }else if( neg ){
 22243     *pNum = -(i64)u;
 22244   }else{
 22245     *pNum = (i64)u;
 22247   testcase( i==18 );
 22248   testcase( i==19 );
 22249   testcase( i==20 );
 22250   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
 22251     /* zNum is empty or contains non-numeric text or is longer
 22252     ** than 19 digits (thus guaranteeing that it is too large) */
 22253     return 1;
 22254   }else if( i<19*incr ){
 22255     /* Less than 19 digits, so we know that it fits in 64 bits */
 22256     assert( u<=LARGEST_INT64 );
 22257     return 0;
 22258   }else{
 22259     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 22260     c = compare2pow63(zNum, incr);
 22261     if( c<0 ){
 22262       /* zNum is less than 9223372036854775808 so it fits */
 22263       assert( u<=LARGEST_INT64 );
 22264       return 0;
 22265     }else if( c>0 ){
 22266       /* zNum is greater than 9223372036854775808 so it overflows */
 22267       return 1;
 22268     }else{
 22269       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 22270       ** special case 2 overflow if positive */
 22271       assert( u-1==LARGEST_INT64 );
 22272       return neg ? 0 : 2;
 22277 /*
 22278 ** If zNum represents an integer that will fit in 32-bits, then set
 22279 ** *pValue to that integer and return true.  Otherwise return false.
 22280 **
 22281 ** Any non-numeric characters that following zNum are ignored.
 22282 ** This is different from sqlite3Atoi64() which requires the
 22283 ** input number to be zero-terminated.
 22284 */
 22285 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
 22286   sqlite_int64 v = 0;
 22287   int i, c;
 22288   int neg = 0;
 22289   if( zNum[0]=='-' ){
 22290     neg = 1;
 22291     zNum++;
 22292   }else if( zNum[0]=='+' ){
 22293     zNum++;
 22295   while( zNum[0]=='0' ) zNum++;
 22296   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
 22297     v = v*10 + c;
 22300   /* The longest decimal representation of a 32 bit integer is 10 digits:
 22301   **
 22302   **             1234567890
 22303   **     2^31 -> 2147483648
 22304   */
 22305   testcase( i==10 );
 22306   if( i>10 ){
 22307     return 0;
 22309   testcase( v-neg==2147483647 );
 22310   if( v-neg>2147483647 ){
 22311     return 0;
 22313   if( neg ){
 22314     v = -v;
 22316   *pValue = (int)v;
 22317   return 1;
 22320 /*
 22321 ** Return a 32-bit integer value extracted from a string.  If the
 22322 ** string is not an integer, just return 0.
 22323 */
 22324 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
 22325   int x = 0;
 22326   if( z ) sqlite3GetInt32(z, &x);
 22327   return x;
 22330 /*
 22331 ** The variable-length integer encoding is as follows:
 22332 **
 22333 ** KEY:
 22334 **         A = 0xxxxxxx    7 bits of data and one flag bit
 22335 **         B = 1xxxxxxx    7 bits of data and one flag bit
 22336 **         C = xxxxxxxx    8 bits of data
 22337 **
 22338 **  7 bits - A
 22339 ** 14 bits - BA
 22340 ** 21 bits - BBA
 22341 ** 28 bits - BBBA
 22342 ** 35 bits - BBBBA
 22343 ** 42 bits - BBBBBA
 22344 ** 49 bits - BBBBBBA
 22345 ** 56 bits - BBBBBBBA
 22346 ** 64 bits - BBBBBBBBC
 22347 */
 22349 /*
 22350 ** Write a 64-bit variable-length integer to memory starting at p[0].
 22351 ** The length of data write will be between 1 and 9 bytes.  The number
 22352 ** of bytes written is returned.
 22353 **
 22354 ** A variable-length integer consists of the lower 7 bits of each byte
 22355 ** for all bytes that have the 8th bit set and one byte with the 8th
 22356 ** bit clear.  Except, if we get to the 9th byte, it stores the full
 22357 ** 8 bits and is the last byte.
 22358 */
 22359 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
 22360   int i, j, n;
 22361   u8 buf[10];
 22362   if( v & (((u64)0xff000000)<<32) ){
 22363     p[8] = (u8)v;
 22364     v >>= 8;
 22365     for(i=7; i>=0; i--){
 22366       p[i] = (u8)((v & 0x7f) | 0x80);
 22367       v >>= 7;
 22369     return 9;
 22371   n = 0;
 22372   do{
 22373     buf[n++] = (u8)((v & 0x7f) | 0x80);
 22374     v >>= 7;
 22375   }while( v!=0 );
 22376   buf[0] &= 0x7f;
 22377   assert( n<=9 );
 22378   for(i=0, j=n-1; j>=0; j--, i++){
 22379     p[i] = buf[j];
 22381   return n;
 22384 /*
 22385 ** This routine is a faster version of sqlite3PutVarint() that only
 22386 ** works for 32-bit positive integers and which is optimized for
 22387 ** the common case of small integers.  A MACRO version, putVarint32,
 22388 ** is provided which inlines the single-byte case.  All code should use
 22389 ** the MACRO version as this function assumes the single-byte case has
 22390 ** already been handled.
 22391 */
 22392 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
 22393 #ifndef putVarint32
 22394   if( (v & ~0x7f)==0 ){
 22395     p[0] = v;
 22396     return 1;
 22398 #endif
 22399   if( (v & ~0x3fff)==0 ){
 22400     p[0] = (u8)((v>>7) | 0x80);
 22401     p[1] = (u8)(v & 0x7f);
 22402     return 2;
 22404   return sqlite3PutVarint(p, v);
 22407 /*
 22408 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
 22409 ** are defined here rather than simply putting the constant expressions
 22410 ** inline in order to work around bugs in the RVT compiler.
 22411 **
 22412 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
 22413 **
 22414 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
 22415 */
 22416 #define SLOT_2_0     0x001fc07f
 22417 #define SLOT_4_2_0   0xf01fc07f
 22420 /*
 22421 ** Read a 64-bit variable-length integer from memory starting at p[0].
 22422 ** Return the number of bytes read.  The value is stored in *v.
 22423 */
 22424 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
 22425   u32 a,b,s;
 22427   a = *p;
 22428   /* a: p0 (unmasked) */
 22429   if (!(a&0x80))
 22431     *v = a;
 22432     return 1;
 22435   p++;
 22436   b = *p;
 22437   /* b: p1 (unmasked) */
 22438   if (!(b&0x80))
 22440     a &= 0x7f;
 22441     a = a<<7;
 22442     a |= b;
 22443     *v = a;
 22444     return 2;
 22447   /* Verify that constants are precomputed correctly */
 22448   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
 22449   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
 22451   p++;
 22452   a = a<<14;
 22453   a |= *p;
 22454   /* a: p0<<14 | p2 (unmasked) */
 22455   if (!(a&0x80))
 22457     a &= SLOT_2_0;
 22458     b &= 0x7f;
 22459     b = b<<7;
 22460     a |= b;
 22461     *v = a;
 22462     return 3;
 22465   /* CSE1 from below */
 22466   a &= SLOT_2_0;
 22467   p++;
 22468   b = b<<14;
 22469   b |= *p;
 22470   /* b: p1<<14 | p3 (unmasked) */
 22471   if (!(b&0x80))
 22473     b &= SLOT_2_0;
 22474     /* moved CSE1 up */
 22475     /* a &= (0x7f<<14)|(0x7f); */
 22476     a = a<<7;
 22477     a |= b;
 22478     *v = a;
 22479     return 4;
 22482   /* a: p0<<14 | p2 (masked) */
 22483   /* b: p1<<14 | p3 (unmasked) */
 22484   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 22485   /* moved CSE1 up */
 22486   /* a &= (0x7f<<14)|(0x7f); */
 22487   b &= SLOT_2_0;
 22488   s = a;
 22489   /* s: p0<<14 | p2 (masked) */
 22491   p++;
 22492   a = a<<14;
 22493   a |= *p;
 22494   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 22495   if (!(a&0x80))
 22497     /* we can skip these cause they were (effectively) done above in calc'ing s */
 22498     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 22499     /* b &= (0x7f<<14)|(0x7f); */
 22500     b = b<<7;
 22501     a |= b;
 22502     s = s>>18;
 22503     *v = ((u64)s)<<32 | a;
 22504     return 5;
 22507   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 22508   s = s<<7;
 22509   s |= b;
 22510   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
 22512   p++;
 22513   b = b<<14;
 22514   b |= *p;
 22515   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
 22516   if (!(b&0x80))
 22518     /* we can skip this cause it was (effectively) done above in calc'ing s */
 22519     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
 22520     a &= SLOT_2_0;
 22521     a = a<<7;
 22522     a |= b;
 22523     s = s>>18;
 22524     *v = ((u64)s)<<32 | a;
 22525     return 6;
 22528   p++;
 22529   a = a<<14;
 22530   a |= *p;
 22531   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
 22532   if (!(a&0x80))
 22534     a &= SLOT_4_2_0;
 22535     b &= SLOT_2_0;
 22536     b = b<<7;
 22537     a |= b;
 22538     s = s>>11;
 22539     *v = ((u64)s)<<32 | a;
 22540     return 7;
 22543   /* CSE2 from below */
 22544   a &= SLOT_2_0;
 22545   p++;
 22546   b = b<<14;
 22547   b |= *p;
 22548   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
 22549   if (!(b&0x80))
 22551     b &= SLOT_4_2_0;
 22552     /* moved CSE2 up */
 22553     /* a &= (0x7f<<14)|(0x7f); */
 22554     a = a<<7;
 22555     a |= b;
 22556     s = s>>4;
 22557     *v = ((u64)s)<<32 | a;
 22558     return 8;
 22561   p++;
 22562   a = a<<15;
 22563   a |= *p;
 22564   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
 22566   /* moved CSE2 up */
 22567   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
 22568   b &= SLOT_2_0;
 22569   b = b<<8;
 22570   a |= b;
 22572   s = s<<4;
 22573   b = p[-4];
 22574   b &= 0x7f;
 22575   b = b>>3;
 22576   s |= b;
 22578   *v = ((u64)s)<<32 | a;
 22580   return 9;
 22583 /*
 22584 ** Read a 32-bit variable-length integer from memory starting at p[0].
 22585 ** Return the number of bytes read.  The value is stored in *v.
 22586 **
 22587 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
 22588 ** integer, then set *v to 0xffffffff.
 22589 **
 22590 ** A MACRO version, getVarint32, is provided which inlines the 
 22591 ** single-byte case.  All code should use the MACRO version as 
 22592 ** this function assumes the single-byte case has already been handled.
 22593 */
 22594 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
 22595   u32 a,b;
 22597   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
 22598   ** by the getVarin32() macro */
 22599   a = *p;
 22600   /* a: p0 (unmasked) */
 22601 #ifndef getVarint32
 22602   if (!(a&0x80))
 22604     /* Values between 0 and 127 */
 22605     *v = a;
 22606     return 1;
 22608 #endif
 22610   /* The 2-byte case */
 22611   p++;
 22612   b = *p;
 22613   /* b: p1 (unmasked) */
 22614   if (!(b&0x80))
 22616     /* Values between 128 and 16383 */
 22617     a &= 0x7f;
 22618     a = a<<7;
 22619     *v = a | b;
 22620     return 2;
 22623   /* The 3-byte case */
 22624   p++;
 22625   a = a<<14;
 22626   a |= *p;
 22627   /* a: p0<<14 | p2 (unmasked) */
 22628   if (!(a&0x80))
 22630     /* Values between 16384 and 2097151 */
 22631     a &= (0x7f<<14)|(0x7f);
 22632     b &= 0x7f;
 22633     b = b<<7;
 22634     *v = a | b;
 22635     return 3;
 22638   /* A 32-bit varint is used to store size information in btrees.
 22639   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
 22640   ** A 3-byte varint is sufficient, for example, to record the size
 22641   ** of a 1048569-byte BLOB or string.
 22642   **
 22643   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
 22644   ** rare larger cases can be handled by the slower 64-bit varint
 22645   ** routine.
 22646   */
 22647 #if 1
 22649     u64 v64;
 22650     u8 n;
 22652     p -= 2;
 22653     n = sqlite3GetVarint(p, &v64);
 22654     assert( n>3 && n<=9 );
 22655     if( (v64 & SQLITE_MAX_U32)!=v64 ){
 22656       *v = 0xffffffff;
 22657     }else{
 22658       *v = (u32)v64;
 22660     return n;
 22663 #else
 22664   /* For following code (kept for historical record only) shows an
 22665   ** unrolling for the 3- and 4-byte varint cases.  This code is
 22666   ** slightly faster, but it is also larger and much harder to test.
 22667   */
 22668   p++;
 22669   b = b<<14;
 22670   b |= *p;
 22671   /* b: p1<<14 | p3 (unmasked) */
 22672   if (!(b&0x80))
 22674     /* Values between 2097152 and 268435455 */
 22675     b &= (0x7f<<14)|(0x7f);
 22676     a &= (0x7f<<14)|(0x7f);
 22677     a = a<<7;
 22678     *v = a | b;
 22679     return 4;
 22682   p++;
 22683   a = a<<14;
 22684   a |= *p;
 22685   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
 22686   if (!(a&0x80))
 22688     /* Values  between 268435456 and 34359738367 */
 22689     a &= SLOT_4_2_0;
 22690     b &= SLOT_4_2_0;
 22691     b = b<<7;
 22692     *v = a | b;
 22693     return 5;
 22696   /* We can only reach this point when reading a corrupt database
 22697   ** file.  In that case we are not in any hurry.  Use the (relatively
 22698   ** slow) general-purpose sqlite3GetVarint() routine to extract the
 22699   ** value. */
 22701     u64 v64;
 22702     u8 n;
 22704     p -= 4;
 22705     n = sqlite3GetVarint(p, &v64);
 22706     assert( n>5 && n<=9 );
 22707     *v = (u32)v64;
 22708     return n;
 22710 #endif
 22713 /*
 22714 ** Return the number of bytes that will be needed to store the given
 22715 ** 64-bit integer.
 22716 */
 22717 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
 22718   int i = 0;
 22719   do{
 22720     i++;
 22721     v >>= 7;
 22722   }while( v!=0 && ALWAYS(i<9) );
 22723   return i;
 22727 /*
 22728 ** Read or write a four-byte big-endian integer value.
 22729 */
 22730 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
 22731   testcase( p[0]&0x80 );
 22732   return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
 22734 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
 22735   p[0] = (u8)(v>>24);
 22736   p[1] = (u8)(v>>16);
 22737   p[2] = (u8)(v>>8);
 22738   p[3] = (u8)v;
 22743 /*
 22744 ** Translate a single byte of Hex into an integer.
 22745 ** This routine only works if h really is a valid hexadecimal
 22746 ** character:  0..9a..fA..F
 22747 */
 22748 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
 22749   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
 22750 #ifdef SQLITE_ASCII
 22751   h += 9*(1&(h>>6));
 22752 #endif
 22753 #ifdef SQLITE_EBCDIC
 22754   h += 9*(1&~(h>>4));
 22755 #endif
 22756   return (u8)(h & 0xf);
 22759 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
 22760 /*
 22761 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
 22762 ** value.  Return a pointer to its binary value.  Space to hold the
 22763 ** binary value has been obtained from malloc and must be freed by
 22764 ** the calling routine.
 22765 */
 22766 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
 22767   char *zBlob;
 22768   int i;
 22770   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
 22771   n--;
 22772   if( zBlob ){
 22773     for(i=0; i<n; i+=2){
 22774       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
 22776     zBlob[i/2] = 0;
 22778   return zBlob;
 22780 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
 22782 /*
 22783 ** Log an error that is an API call on a connection pointer that should
 22784 ** not have been used.  The "type" of connection pointer is given as the
 22785 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
 22786 */
 22787 static void logBadConnection(const char *zType){
 22788   sqlite3_log(SQLITE_MISUSE, 
 22789      "API call with %s database connection pointer",
 22790      zType
 22791   );
 22794 /*
 22795 ** Check to make sure we have a valid db pointer.  This test is not
 22796 ** foolproof but it does provide some measure of protection against
 22797 ** misuse of the interface such as passing in db pointers that are
 22798 ** NULL or which have been previously closed.  If this routine returns
 22799 ** 1 it means that the db pointer is valid and 0 if it should not be
 22800 ** dereferenced for any reason.  The calling function should invoke
 22801 ** SQLITE_MISUSE immediately.
 22802 **
 22803 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
 22804 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
 22805 ** open properly and is not fit for general use but which can be
 22806 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
 22807 */
 22808 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
 22809   u32 magic;
 22810   if( db==0 ){
 22811     logBadConnection("NULL");
 22812     return 0;
 22814   magic = db->magic;
 22815   if( magic!=SQLITE_MAGIC_OPEN ){
 22816     if( sqlite3SafetyCheckSickOrOk(db) ){
 22817       testcase( sqlite3GlobalConfig.xLog!=0 );
 22818       logBadConnection("unopened");
 22820     return 0;
 22821   }else{
 22822     return 1;
 22825 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
 22826   u32 magic;
 22827   magic = db->magic;
 22828   if( magic!=SQLITE_MAGIC_SICK &&
 22829       magic!=SQLITE_MAGIC_OPEN &&
 22830       magic!=SQLITE_MAGIC_BUSY ){
 22831     testcase( sqlite3GlobalConfig.xLog!=0 );
 22832     logBadConnection("invalid");
 22833     return 0;
 22834   }else{
 22835     return 1;
 22839 /*
 22840 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
 22841 ** the other 64-bit signed integer at *pA and store the result in *pA.
 22842 ** Return 0 on success.  Or if the operation would have resulted in an
 22843 ** overflow, leave *pA unchanged and return 1.
 22844 */
 22845 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
 22846   i64 iA = *pA;
 22847   testcase( iA==0 ); testcase( iA==1 );
 22848   testcase( iB==-1 ); testcase( iB==0 );
 22849   if( iB>=0 ){
 22850     testcase( iA>0 && LARGEST_INT64 - iA == iB );
 22851     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
 22852     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
 22853   }else{
 22854     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
 22855     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
 22856     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
 22858   *pA += iB;
 22859   return 0; 
 22861 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
 22862   testcase( iB==SMALLEST_INT64+1 );
 22863   if( iB==SMALLEST_INT64 ){
 22864     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
 22865     if( (*pA)>=0 ) return 1;
 22866     *pA -= iB;
 22867     return 0;
 22868   }else{
 22869     return sqlite3AddInt64(pA, -iB);
 22872 #define TWOPOWER32 (((i64)1)<<32)
 22873 #define TWOPOWER31 (((i64)1)<<31)
 22874 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
 22875   i64 iA = *pA;
 22876   i64 iA1, iA0, iB1, iB0, r;
 22878   iA1 = iA/TWOPOWER32;
 22879   iA0 = iA % TWOPOWER32;
 22880   iB1 = iB/TWOPOWER32;
 22881   iB0 = iB % TWOPOWER32;
 22882   if( iA1==0 ){
 22883     if( iB1==0 ){
 22884       *pA *= iB;
 22885       return 0;
 22887     r = iA0*iB1;
 22888   }else if( iB1==0 ){
 22889     r = iA1*iB0;
 22890   }else{
 22891     /* If both iA1 and iB1 are non-zero, overflow will result */
 22892     return 1;
 22894   testcase( r==(-TWOPOWER31)-1 );
 22895   testcase( r==(-TWOPOWER31) );
 22896   testcase( r==TWOPOWER31 );
 22897   testcase( r==TWOPOWER31-1 );
 22898   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
 22899   r *= TWOPOWER32;
 22900   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
 22901   *pA = r;
 22902   return 0;
 22905 /*
 22906 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
 22907 ** if the integer has a value of -2147483648, return +2147483647
 22908 */
 22909 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
 22910   if( x>=0 ) return x;
 22911   if( x==(int)0x80000000 ) return 0x7fffffff;
 22912   return -x;
 22915 #ifdef SQLITE_ENABLE_8_3_NAMES
 22916 /*
 22917 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
 22918 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
 22919 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
 22920 ** three characters, then shorten the suffix on z[] to be the last three
 22921 ** characters of the original suffix.
 22922 **
 22923 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
 22924 ** do the suffix shortening regardless of URI parameter.
 22925 **
 22926 ** Examples:
 22927 **
 22928 **     test.db-journal    =>   test.nal
 22929 **     test.db-wal        =>   test.wal
 22930 **     test.db-shm        =>   test.shm
 22931 **     test.db-mj7f3319fa =>   test.9fa
 22932 */
 22933 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
 22934 #if SQLITE_ENABLE_8_3_NAMES<2
 22935   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
 22936 #endif
 22938     int i, sz;
 22939     sz = sqlite3Strlen30(z);
 22940     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
 22941     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
 22944 #endif
 22946 /* 
 22947 ** Find (an approximate) sum of two LogEst values.  This computation is
 22948 ** not a simple "+" operator because LogEst is stored as a logarithmic
 22949 ** value.
 22950 ** 
 22951 */
 22952 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
 22953   static const unsigned char x[] = {
 22954      10, 10,                         /* 0,1 */
 22955       9, 9,                          /* 2,3 */
 22956       8, 8,                          /* 4,5 */
 22957       7, 7, 7,                       /* 6,7,8 */
 22958       6, 6, 6,                       /* 9,10,11 */
 22959       5, 5, 5,                       /* 12-14 */
 22960       4, 4, 4, 4,                    /* 15-18 */
 22961       3, 3, 3, 3, 3, 3,              /* 19-24 */
 22962       2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
 22963   };
 22964   if( a>=b ){
 22965     if( a>b+49 ) return a;
 22966     if( a>b+31 ) return a+1;
 22967     return a+x[a-b];
 22968   }else{
 22969     if( b>a+49 ) return b;
 22970     if( b>a+31 ) return b+1;
 22971     return b+x[b-a];
 22975 /*
 22976 ** Convert an integer into a LogEst.  In other words, compute a
 22977 ** good approximatation for 10*log2(x).
 22978 */
 22979 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
 22980   static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
 22981   LogEst y = 40;
 22982   if( x<8 ){
 22983     if( x<2 ) return 0;
 22984     while( x<8 ){  y -= 10; x <<= 1; }
 22985   }else{
 22986     while( x>255 ){ y += 40; x >>= 4; }
 22987     while( x>15 ){  y += 10; x >>= 1; }
 22989   return a[x&7] + y - 10;
 22992 #ifndef SQLITE_OMIT_VIRTUALTABLE
 22993 /*
 22994 ** Convert a double into a LogEst
 22995 ** In other words, compute an approximation for 10*log2(x).
 22996 */
 22997 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
 22998   u64 a;
 22999   LogEst e;
 23000   assert( sizeof(x)==8 && sizeof(a)==8 );
 23001   if( x<=1 ) return 0;
 23002   if( x<=2000000000 ) return sqlite3LogEst((u64)x);
 23003   memcpy(&a, &x, 8);
 23004   e = (a>>52) - 1022;
 23005   return e*10;
 23007 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 23009 /*
 23010 ** Convert a LogEst into an integer.
 23011 */
 23012 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
 23013   u64 n;
 23014   if( x<10 ) return 1;
 23015   n = x%10;
 23016   x /= 10;
 23017   if( n>=5 ) n -= 2;
 23018   else if( n>=1 ) n -= 1;
 23019   if( x>=3 ){
 23020     return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
 23022   return (n+8)>>(3-x);
 23025 /************** End of util.c ************************************************/
 23026 /************** Begin file hash.c ********************************************/
 23027 /*
 23028 ** 2001 September 22
 23029 **
 23030 ** The author disclaims copyright to this source code.  In place of
 23031 ** a legal notice, here is a blessing:
 23032 **
 23033 **    May you do good and not evil.
 23034 **    May you find forgiveness for yourself and forgive others.
 23035 **    May you share freely, never taking more than you give.
 23036 **
 23037 *************************************************************************
 23038 ** This is the implementation of generic hash-tables
 23039 ** used in SQLite.
 23040 */
 23041 /* #include <assert.h> */
 23043 /* Turn bulk memory into a hash table object by initializing the
 23044 ** fields of the Hash structure.
 23045 **
 23046 ** "pNew" is a pointer to the hash table that is to be initialized.
 23047 */
 23048 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
 23049   assert( pNew!=0 );
 23050   pNew->first = 0;
 23051   pNew->count = 0;
 23052   pNew->htsize = 0;
 23053   pNew->ht = 0;
 23056 /* Remove all entries from a hash table.  Reclaim all memory.
 23057 ** Call this routine to delete a hash table or to reset a hash table
 23058 ** to the empty state.
 23059 */
 23060 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
 23061   HashElem *elem;         /* For looping over all elements of the table */
 23063   assert( pH!=0 );
 23064   elem = pH->first;
 23065   pH->first = 0;
 23066   sqlite3_free(pH->ht);
 23067   pH->ht = 0;
 23068   pH->htsize = 0;
 23069   while( elem ){
 23070     HashElem *next_elem = elem->next;
 23071     sqlite3_free(elem);
 23072     elem = next_elem;
 23074   pH->count = 0;
 23077 /*
 23078 ** The hashing function.
 23079 */
 23080 static unsigned int strHash(const char *z, int nKey){
 23081   unsigned int h = 0;
 23082   assert( nKey>=0 );
 23083   while( nKey > 0  ){
 23084     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
 23085     nKey--;
 23087   return h;
 23091 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
 23092 ** insert pNew into the pEntry hash bucket.
 23093 */
 23094 static void insertElement(
 23095   Hash *pH,              /* The complete hash table */
 23096   struct _ht *pEntry,    /* The entry into which pNew is inserted */
 23097   HashElem *pNew         /* The element to be inserted */
 23098 ){
 23099   HashElem *pHead;       /* First element already in pEntry */
 23100   if( pEntry ){
 23101     pHead = pEntry->count ? pEntry->chain : 0;
 23102     pEntry->count++;
 23103     pEntry->chain = pNew;
 23104   }else{
 23105     pHead = 0;
 23107   if( pHead ){
 23108     pNew->next = pHead;
 23109     pNew->prev = pHead->prev;
 23110     if( pHead->prev ){ pHead->prev->next = pNew; }
 23111     else             { pH->first = pNew; }
 23112     pHead->prev = pNew;
 23113   }else{
 23114     pNew->next = pH->first;
 23115     if( pH->first ){ pH->first->prev = pNew; }
 23116     pNew->prev = 0;
 23117     pH->first = pNew;
 23122 /* Resize the hash table so that it cantains "new_size" buckets.
 23123 **
 23124 ** The hash table might fail to resize if sqlite3_malloc() fails or
 23125 ** if the new size is the same as the prior size.
 23126 ** Return TRUE if the resize occurs and false if not.
 23127 */
 23128 static int rehash(Hash *pH, unsigned int new_size){
 23129   struct _ht *new_ht;            /* The new hash table */
 23130   HashElem *elem, *next_elem;    /* For looping over existing elements */
 23132 #if SQLITE_MALLOC_SOFT_LIMIT>0
 23133   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
 23134     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
 23136   if( new_size==pH->htsize ) return 0;
 23137 #endif
 23139   /* The inability to allocates space for a larger hash table is
 23140   ** a performance hit but it is not a fatal error.  So mark the
 23141   ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of 
 23142   ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
 23143   ** only zeroes the requested number of bytes whereas this module will
 23144   ** use the actual amount of space allocated for the hash table (which
 23145   ** may be larger than the requested amount).
 23146   */
 23147   sqlite3BeginBenignMalloc();
 23148   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
 23149   sqlite3EndBenignMalloc();
 23151   if( new_ht==0 ) return 0;
 23152   sqlite3_free(pH->ht);
 23153   pH->ht = new_ht;
 23154   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
 23155   memset(new_ht, 0, new_size*sizeof(struct _ht));
 23156   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
 23157     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
 23158     next_elem = elem->next;
 23159     insertElement(pH, &new_ht[h], elem);
 23161   return 1;
 23164 /* This function (for internal use only) locates an element in an
 23165 ** hash table that matches the given key.  The hash for this key has
 23166 ** already been computed and is passed as the 4th parameter.
 23167 */
 23168 static HashElem *findElementGivenHash(
 23169   const Hash *pH,     /* The pH to be searched */
 23170   const char *pKey,   /* The key we are searching for */
 23171   int nKey,           /* Bytes in key (not counting zero terminator) */
 23172   unsigned int h      /* The hash for this key. */
 23173 ){
 23174   HashElem *elem;                /* Used to loop thru the element list */
 23175   int count;                     /* Number of elements left to test */
 23177   if( pH->ht ){
 23178     struct _ht *pEntry = &pH->ht[h];
 23179     elem = pEntry->chain;
 23180     count = pEntry->count;
 23181   }else{
 23182     elem = pH->first;
 23183     count = pH->count;
 23185   while( count-- && ALWAYS(elem) ){
 23186     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
 23187       return elem;
 23189     elem = elem->next;
 23191   return 0;
 23194 /* Remove a single entry from the hash table given a pointer to that
 23195 ** element and a hash on the element's key.
 23196 */
 23197 static void removeElementGivenHash(
 23198   Hash *pH,         /* The pH containing "elem" */
 23199   HashElem* elem,   /* The element to be removed from the pH */
 23200   unsigned int h    /* Hash value for the element */
 23201 ){
 23202   struct _ht *pEntry;
 23203   if( elem->prev ){
 23204     elem->prev->next = elem->next; 
 23205   }else{
 23206     pH->first = elem->next;
 23208   if( elem->next ){
 23209     elem->next->prev = elem->prev;
 23211   if( pH->ht ){
 23212     pEntry = &pH->ht[h];
 23213     if( pEntry->chain==elem ){
 23214       pEntry->chain = elem->next;
 23216     pEntry->count--;
 23217     assert( pEntry->count>=0 );
 23219   sqlite3_free( elem );
 23220   pH->count--;
 23221   if( pH->count==0 ){
 23222     assert( pH->first==0 );
 23223     assert( pH->count==0 );
 23224     sqlite3HashClear(pH);
 23228 /* Attempt to locate an element of the hash table pH with a key
 23229 ** that matches pKey,nKey.  Return the data for this element if it is
 23230 ** found, or NULL if there is no match.
 23231 */
 23232 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
 23233   HashElem *elem;    /* The element that matches key */
 23234   unsigned int h;    /* A hash on key */
 23236   assert( pH!=0 );
 23237   assert( pKey!=0 );
 23238   assert( nKey>=0 );
 23239   if( pH->ht ){
 23240     h = strHash(pKey, nKey) % pH->htsize;
 23241   }else{
 23242     h = 0;
 23244   elem = findElementGivenHash(pH, pKey, nKey, h);
 23245   return elem ? elem->data : 0;
 23248 /* Insert an element into the hash table pH.  The key is pKey,nKey
 23249 ** and the data is "data".
 23250 **
 23251 ** If no element exists with a matching key, then a new
 23252 ** element is created and NULL is returned.
 23253 **
 23254 ** If another element already exists with the same key, then the
 23255 ** new data replaces the old data and the old data is returned.
 23256 ** The key is not copied in this instance.  If a malloc fails, then
 23257 ** the new data is returned and the hash table is unchanged.
 23258 **
 23259 ** If the "data" parameter to this function is NULL, then the
 23260 ** element corresponding to "key" is removed from the hash table.
 23261 */
 23262 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
 23263   unsigned int h;       /* the hash of the key modulo hash table size */
 23264   HashElem *elem;       /* Used to loop thru the element list */
 23265   HashElem *new_elem;   /* New element added to the pH */
 23267   assert( pH!=0 );
 23268   assert( pKey!=0 );
 23269   assert( nKey>=0 );
 23270   if( pH->htsize ){
 23271     h = strHash(pKey, nKey) % pH->htsize;
 23272   }else{
 23273     h = 0;
 23275   elem = findElementGivenHash(pH,pKey,nKey,h);
 23276   if( elem ){
 23277     void *old_data = elem->data;
 23278     if( data==0 ){
 23279       removeElementGivenHash(pH,elem,h);
 23280     }else{
 23281       elem->data = data;
 23282       elem->pKey = pKey;
 23283       assert(nKey==elem->nKey);
 23285     return old_data;
 23287   if( data==0 ) return 0;
 23288   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
 23289   if( new_elem==0 ) return data;
 23290   new_elem->pKey = pKey;
 23291   new_elem->nKey = nKey;
 23292   new_elem->data = data;
 23293   pH->count++;
 23294   if( pH->count>=10 && pH->count > 2*pH->htsize ){
 23295     if( rehash(pH, pH->count*2) ){
 23296       assert( pH->htsize>0 );
 23297       h = strHash(pKey, nKey) % pH->htsize;
 23300   if( pH->ht ){
 23301     insertElement(pH, &pH->ht[h], new_elem);
 23302   }else{
 23303     insertElement(pH, 0, new_elem);
 23305   return 0;
 23308 /************** End of hash.c ************************************************/
 23309 /************** Begin file opcodes.c *****************************************/
 23310 /* Automatically generated.  Do not edit */
 23311 /* See the mkopcodec.awk script for details. */
 23312 #if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 23313 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
 23314 # define OpHelp(X) "\0" X
 23315 #else
 23316 # define OpHelp(X)
 23317 #endif
 23318 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
 23319  static const char *const azName[] = { "?",
 23320      /*   1 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 23321      /*   2 */ "Savepoint"        OpHelp(""),
 23322      /*   3 */ "AutoCommit"       OpHelp(""),
 23323      /*   4 */ "Transaction"      OpHelp(""),
 23324      /*   5 */ "SorterNext"       OpHelp(""),
 23325      /*   6 */ "PrevIfOpen"       OpHelp(""),
 23326      /*   7 */ "NextIfOpen"       OpHelp(""),
 23327      /*   8 */ "Prev"             OpHelp(""),
 23328      /*   9 */ "Next"             OpHelp(""),
 23329      /*  10 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 23330      /*  11 */ "Checkpoint"       OpHelp(""),
 23331      /*  12 */ "JournalMode"      OpHelp(""),
 23332      /*  13 */ "Vacuum"           OpHelp(""),
 23333      /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
 23334      /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
 23335      /*  16 */ "Goto"             OpHelp(""),
 23336      /*  17 */ "Gosub"            OpHelp(""),
 23337      /*  18 */ "Return"           OpHelp(""),
 23338      /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
 23339      /*  20 */ "InitCoroutine"    OpHelp(""),
 23340      /*  21 */ "EndCoroutine"     OpHelp(""),
 23341      /*  22 */ "Yield"            OpHelp(""),
 23342      /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
 23343      /*  24 */ "Halt"             OpHelp(""),
 23344      /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
 23345      /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
 23346      /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
 23347      /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
 23348      /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
 23349      /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
 23350      /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
 23351      /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
 23352      /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
 23353      /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
 23354      /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
 23355      /*  36 */ "CollSeq"          OpHelp(""),
 23356      /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
 23357      /*  38 */ "MustBeInt"        OpHelp(""),
 23358      /*  39 */ "RealAffinity"     OpHelp(""),
 23359      /*  40 */ "Permutation"      OpHelp(""),
 23360      /*  41 */ "Compare"          OpHelp(""),
 23361      /*  42 */ "Jump"             OpHelp(""),
 23362      /*  43 */ "Once"             OpHelp(""),
 23363      /*  44 */ "If"               OpHelp(""),
 23364      /*  45 */ "IfNot"            OpHelp(""),
 23365      /*  46 */ "Column"           OpHelp("r[P3]=PX"),
 23366      /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 23367      /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 23368      /*  49 */ "Count"            OpHelp("r[P2]=count()"),
 23369      /*  50 */ "ReadCookie"       OpHelp(""),
 23370      /*  51 */ "SetCookie"        OpHelp(""),
 23371      /*  52 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 23372      /*  53 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 23373      /*  54 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 23374      /*  55 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 23375      /*  56 */ "SorterOpen"       OpHelp(""),
 23376      /*  57 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 23377      /*  58 */ "Close"            OpHelp(""),
 23378      /*  59 */ "SeekLT"           OpHelp(""),
 23379      /*  60 */ "SeekLE"           OpHelp(""),
 23380      /*  61 */ "SeekGE"           OpHelp(""),
 23381      /*  62 */ "SeekGT"           OpHelp(""),
 23382      /*  63 */ "Seek"             OpHelp("intkey=r[P2]"),
 23383      /*  64 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
 23384      /*  65 */ "NotFound"         OpHelp("key=r[P3@P4]"),
 23385      /*  66 */ "Found"            OpHelp("key=r[P3@P4]"),
 23386      /*  67 */ "NotExists"        OpHelp("intkey=r[P3]"),
 23387      /*  68 */ "Sequence"         OpHelp("r[P2]=rowid"),
 23388      /*  69 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 23389      /*  70 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 23390      /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
 23391      /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
 23392      /*  73 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 23393      /*  74 */ "Delete"           OpHelp(""),
 23394      /*  75 */ "ResetCount"       OpHelp(""),
 23395      /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
 23396      /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
 23397      /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
 23398      /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
 23399      /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
 23400      /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
 23401      /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
 23402      /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
 23403      /*  84 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
 23404      /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
 23405      /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
 23406      /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
 23407      /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
 23408      /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
 23409      /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 23410      /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 23411      /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 23412      /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 23413      /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 23414      /*  95 */ "SorterData"       OpHelp("r[P2]=data"),
 23415      /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 23416      /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 23417      /*  98 */ "RowKey"           OpHelp("r[P2]=key"),
 23418      /*  99 */ "RowData"          OpHelp("r[P2]=data"),
 23419      /* 100 */ "Rowid"            OpHelp("r[P2]=rowid"),
 23420      /* 101 */ "NullRow"          OpHelp(""),
 23421      /* 102 */ "Last"             OpHelp(""),
 23422      /* 103 */ "SorterSort"       OpHelp(""),
 23423      /* 104 */ "Sort"             OpHelp(""),
 23424      /* 105 */ "Rewind"           OpHelp(""),
 23425      /* 106 */ "SorterInsert"     OpHelp(""),
 23426      /* 107 */ "IdxInsert"        OpHelp("key=r[P2]"),
 23427      /* 108 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 23428      /* 109 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
 23429      /* 110 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
 23430      /* 111 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
 23431      /* 112 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
 23432      /* 113 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
 23433      /* 114 */ "Destroy"          OpHelp(""),
 23434      /* 115 */ "Clear"            OpHelp(""),
 23435      /* 116 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
 23436      /* 117 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
 23437      /* 118 */ "ParseSchema"      OpHelp(""),
 23438      /* 119 */ "LoadAnalysis"     OpHelp(""),
 23439      /* 120 */ "DropTable"        OpHelp(""),
 23440      /* 121 */ "DropIndex"        OpHelp(""),
 23441      /* 122 */ "DropTrigger"      OpHelp(""),
 23442      /* 123 */ "IntegrityCk"      OpHelp(""),
 23443      /* 124 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 23444      /* 125 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
 23445      /* 126 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
 23446      /* 127 */ "Program"          OpHelp(""),
 23447      /* 128 */ "Param"            OpHelp(""),
 23448      /* 129 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 23449      /* 130 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
 23450      /* 131 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 23451      /* 132 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
 23452      /* 133 */ "Real"             OpHelp("r[P2]=P4"),
 23453      /* 134 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
 23454      /* 135 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
 23455      /* 136 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 23456      /* 137 */ "IncrVacuum"       OpHelp(""),
 23457      /* 138 */ "Expire"           OpHelp(""),
 23458      /* 139 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 23459      /* 140 */ "VBegin"           OpHelp(""),
 23460      /* 141 */ "VCreate"          OpHelp(""),
 23461      /* 142 */ "VDestroy"         OpHelp(""),
 23462      /* 143 */ "ToText"           OpHelp(""),
 23463      /* 144 */ "ToBlob"           OpHelp(""),
 23464      /* 145 */ "ToNumeric"        OpHelp(""),
 23465      /* 146 */ "ToInt"            OpHelp(""),
 23466      /* 147 */ "ToReal"           OpHelp(""),
 23467      /* 148 */ "VOpen"            OpHelp(""),
 23468      /* 149 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 23469      /* 150 */ "VNext"            OpHelp(""),
 23470      /* 151 */ "VRename"          OpHelp(""),
 23471      /* 152 */ "Pagecount"        OpHelp(""),
 23472      /* 153 */ "MaxPgcnt"         OpHelp(""),
 23473      /* 154 */ "Init"             OpHelp("Start at P2"),
 23474      /* 155 */ "Noop"             OpHelp(""),
 23475      /* 156 */ "Explain"          OpHelp(""),
 23476   };
 23477   return azName[i];
 23479 #endif
 23481 /************** End of opcodes.c *********************************************/
 23482 /************** Begin file os_unix.c *****************************************/
 23483 /*
 23484 ** 2004 May 22
 23485 **
 23486 ** The author disclaims copyright to this source code.  In place of
 23487 ** a legal notice, here is a blessing:
 23488 **
 23489 **    May you do good and not evil.
 23490 **    May you find forgiveness for yourself and forgive others.
 23491 **    May you share freely, never taking more than you give.
 23492 **
 23493 ******************************************************************************
 23494 **
 23495 ** This file contains the VFS implementation for unix-like operating systems
 23496 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
 23497 **
 23498 ** There are actually several different VFS implementations in this file.
 23499 ** The differences are in the way that file locking is done.  The default
 23500 ** implementation uses Posix Advisory Locks.  Alternative implementations
 23501 ** use flock(), dot-files, various proprietary locking schemas, or simply
 23502 ** skip locking all together.
 23503 **
 23504 ** This source file is organized into divisions where the logic for various
 23505 ** subfunctions is contained within the appropriate division.  PLEASE
 23506 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
 23507 ** in the correct division and should be clearly labeled.
 23508 **
 23509 ** The layout of divisions is as follows:
 23510 **
 23511 **   *  General-purpose declarations and utility functions.
 23512 **   *  Unique file ID logic used by VxWorks.
 23513 **   *  Various locking primitive implementations (all except proxy locking):
 23514 **      + for Posix Advisory Locks
 23515 **      + for no-op locks
 23516 **      + for dot-file locks
 23517 **      + for flock() locking
 23518 **      + for named semaphore locks (VxWorks only)
 23519 **      + for AFP filesystem locks (MacOSX only)
 23520 **   *  sqlite3_file methods not associated with locking.
 23521 **   *  Definitions of sqlite3_io_methods objects for all locking
 23522 **      methods plus "finder" functions for each locking method.
 23523 **   *  sqlite3_vfs method implementations.
 23524 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
 23525 **   *  Definitions of sqlite3_vfs objects for all locking methods
 23526 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
 23527 */
 23528 #if SQLITE_OS_UNIX              /* This file is used on unix only */
 23530 /*
 23531 ** There are various methods for file locking used for concurrency
 23532 ** control:
 23533 **
 23534 **   1. POSIX locking (the default),
 23535 **   2. No locking,
 23536 **   3. Dot-file locking,
 23537 **   4. flock() locking,
 23538 **   5. AFP locking (OSX only),
 23539 **   6. Named POSIX semaphores (VXWorks only),
 23540 **   7. proxy locking. (OSX only)
 23541 **
 23542 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
 23543 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
 23544 ** selection of the appropriate locking style based on the filesystem
 23545 ** where the database is located.  
 23546 */
 23547 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 23548 #  if defined(__APPLE__)
 23549 #    define SQLITE_ENABLE_LOCKING_STYLE 1
 23550 #  else
 23551 #    define SQLITE_ENABLE_LOCKING_STYLE 0
 23552 #  endif
 23553 #endif
 23555 /*
 23556 ** Define the OS_VXWORKS pre-processor macro to 1 if building on 
 23557 ** vxworks, or 0 otherwise.
 23558 */
 23559 #ifndef OS_VXWORKS
 23560 #  if defined(__RTP__) || defined(_WRS_KERNEL)
 23561 #    define OS_VXWORKS 1
 23562 #  else
 23563 #    define OS_VXWORKS 0
 23564 #  endif
 23565 #endif
 23567 /*
 23568 ** standard include files.
 23569 */
 23570 #include <sys/types.h>
 23571 #include <sys/stat.h>
 23572 #include <fcntl.h>
 23573 #include <unistd.h>
 23574 /* #include <time.h> */
 23575 #include <sys/time.h>
 23576 #include <errno.h>
 23577 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 23578 #include <sys/mman.h>
 23579 #endif
 23582 #if SQLITE_ENABLE_LOCKING_STYLE
 23583 # include <sys/ioctl.h>
 23584 # if OS_VXWORKS
 23585 #  include <semaphore.h>
 23586 #  include <limits.h>
 23587 # else
 23588 #  include <sys/file.h>
 23589 #  include <sys/param.h>
 23590 # endif
 23591 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 23593 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
 23594 # include <sys/mount.h>
 23595 #endif
 23597 #ifdef HAVE_UTIME
 23598 # include <utime.h>
 23599 #endif
 23601 /*
 23602 ** Allowed values of unixFile.fsFlags
 23603 */
 23604 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
 23606 /*
 23607 ** If we are to be thread-safe, include the pthreads header and define
 23608 ** the SQLITE_UNIX_THREADS macro.
 23609 */
 23610 #if SQLITE_THREADSAFE
 23611 /* # include <pthread.h> */
 23612 # define SQLITE_UNIX_THREADS 1
 23613 #endif
 23615 /*
 23616 ** Default permissions when creating a new file
 23617 */
 23618 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 23619 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
 23620 #endif
 23622 /*
 23623 ** Default permissions when creating auto proxy dir
 23624 */
 23625 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 23626 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
 23627 #endif
 23629 /*
 23630 ** Maximum supported path-length.
 23631 */
 23632 #define MAX_PATHNAME 512
 23634 /*
 23635 ** Only set the lastErrno if the error code is a real error and not 
 23636 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
 23637 */
 23638 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
 23640 /* Forward references */
 23641 typedef struct unixShm unixShm;               /* Connection shared memory */
 23642 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
 23643 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
 23644 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
 23646 /*
 23647 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
 23648 ** cannot be closed immediately. In these cases, instances of the following
 23649 ** structure are used to store the file descriptor while waiting for an
 23650 ** opportunity to either close or reuse it.
 23651 */
 23652 struct UnixUnusedFd {
 23653   int fd;                   /* File descriptor to close */
 23654   int flags;                /* Flags this file descriptor was opened with */
 23655   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
 23656 };
 23658 /*
 23659 ** The unixFile structure is subclass of sqlite3_file specific to the unix
 23660 ** VFS implementations.
 23661 */
 23662 typedef struct unixFile unixFile;
 23663 struct unixFile {
 23664   sqlite3_io_methods const *pMethod;  /* Always the first entry */
 23665   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
 23666   unixInodeInfo *pInode;              /* Info about locks on this inode */
 23667   int h;                              /* The file descriptor */
 23668   unsigned char eFileLock;            /* The type of lock held on this fd */
 23669   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
 23670   int lastErrno;                      /* The unix errno from last I/O error */
 23671   void *lockingContext;               /* Locking style specific state */
 23672   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
 23673   const char *zPath;                  /* Name of the file */
 23674   unixShm *pShm;                      /* Shared memory segment information */
 23675   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
 23676 #if SQLITE_MAX_MMAP_SIZE>0
 23677   int nFetchOut;                      /* Number of outstanding xFetch refs */
 23678   sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
 23679   sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
 23680   sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
 23681   void *pMapRegion;                   /* Memory mapped region */
 23682 #endif
 23683 #ifdef __QNXNTO__
 23684   int sectorSize;                     /* Device sector size */
 23685   int deviceCharacteristics;          /* Precomputed device characteristics */
 23686 #endif
 23687 #if SQLITE_ENABLE_LOCKING_STYLE
 23688   int openFlags;                      /* The flags specified at open() */
 23689 #endif
 23690 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 23691   unsigned fsFlags;                   /* cached details from statfs() */
 23692 #endif
 23693 #if OS_VXWORKS
 23694   struct vxworksFileId *pId;          /* Unique file ID */
 23695 #endif
 23696 #ifdef SQLITE_DEBUG
 23697   /* The next group of variables are used to track whether or not the
 23698   ** transaction counter in bytes 24-27 of database files are updated
 23699   ** whenever any part of the database changes.  An assertion fault will
 23700   ** occur if a file is updated without also updating the transaction
 23701   ** counter.  This test is made to avoid new problems similar to the
 23702   ** one described by ticket #3584. 
 23703   */
 23704   unsigned char transCntrChng;   /* True if the transaction counter changed */
 23705   unsigned char dbUpdate;        /* True if any part of database file changed */
 23706   unsigned char inNormalWrite;   /* True if in a normal write operation */
 23708 #endif
 23710 #ifdef SQLITE_TEST
 23711   /* In test mode, increase the size of this structure a bit so that 
 23712   ** it is larger than the struct CrashFile defined in test6.c.
 23713   */
 23714   char aPadding[32];
 23715 #endif
 23716 };
 23718 /* This variable holds the process id (pid) from when the xRandomness()
 23719 ** method was called.  If xOpen() is called from a different process id,
 23720 ** indicating that a fork() has occurred, the PRNG will be reset.
 23721 */
 23722 static int randomnessPid = 0;
 23724 /*
 23725 ** Allowed values for the unixFile.ctrlFlags bitmask:
 23726 */
 23727 #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
 23728 #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
 23729 #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
 23730 #ifndef SQLITE_DISABLE_DIRSYNC
 23731 # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
 23732 #else
 23733 # define UNIXFILE_DIRSYNC    0x00
 23734 #endif
 23735 #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 23736 #define UNIXFILE_DELETE      0x20     /* Delete on close */
 23737 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
 23738 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
 23739 #define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings have been issued */
 23741 /*
 23742 ** Include code that is common to all os_*.c files
 23743 */
 23744 /************** Include os_common.h in the middle of os_unix.c ***************/
 23745 /************** Begin file os_common.h ***************************************/
 23746 /*
 23747 ** 2004 May 22
 23748 **
 23749 ** The author disclaims copyright to this source code.  In place of
 23750 ** a legal notice, here is a blessing:
 23751 **
 23752 **    May you do good and not evil.
 23753 **    May you find forgiveness for yourself and forgive others.
 23754 **    May you share freely, never taking more than you give.
 23755 **
 23756 ******************************************************************************
 23757 **
 23758 ** This file contains macros and a little bit of code that is common to
 23759 ** all of the platform-specific files (os_*.c) and is #included into those
 23760 ** files.
 23761 **
 23762 ** This file should be #included by the os_*.c files only.  It is not a
 23763 ** general purpose header file.
 23764 */
 23765 #ifndef _OS_COMMON_H_
 23766 #define _OS_COMMON_H_
 23768 /*
 23769 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 23770 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 23771 ** switch.  The following code should catch this problem at compile-time.
 23772 */
 23773 #ifdef MEMORY_DEBUG
 23774 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 23775 #endif
 23777 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 23778 # ifndef SQLITE_DEBUG_OS_TRACE
 23779 #   define SQLITE_DEBUG_OS_TRACE 0
 23780 # endif
 23781   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 23782 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 23783 #else
 23784 # define OSTRACE(X)
 23785 #endif
 23787 /*
 23788 ** Macros for performance tracing.  Normally turned off.  Only works
 23789 ** on i486 hardware.
 23790 */
 23791 #ifdef SQLITE_PERFORMANCE_TRACE
 23793 /* 
 23794 ** hwtime.h contains inline assembler code for implementing 
 23795 ** high-performance timing routines.
 23796 */
 23797 /************** Include hwtime.h in the middle of os_common.h ****************/
 23798 /************** Begin file hwtime.h ******************************************/
 23799 /*
 23800 ** 2008 May 27
 23801 **
 23802 ** The author disclaims copyright to this source code.  In place of
 23803 ** a legal notice, here is a blessing:
 23804 **
 23805 **    May you do good and not evil.
 23806 **    May you find forgiveness for yourself and forgive others.
 23807 **    May you share freely, never taking more than you give.
 23808 **
 23809 ******************************************************************************
 23810 **
 23811 ** This file contains inline asm code for retrieving "high-performance"
 23812 ** counters for x86 class CPUs.
 23813 */
 23814 #ifndef _HWTIME_H_
 23815 #define _HWTIME_H_
 23817 /*
 23818 ** The following routine only works on pentium-class (or newer) processors.
 23819 ** It uses the RDTSC opcode to read the cycle count value out of the
 23820 ** processor and returns that value.  This can be used for high-res
 23821 ** profiling.
 23822 */
 23823 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 23824       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 23826   #if defined(__GNUC__)
 23828   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23829      unsigned int lo, hi;
 23830      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 23831      return (sqlite_uint64)hi << 32 | lo;
 23834   #elif defined(_MSC_VER)
 23836   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 23837      __asm {
 23838         rdtsc
 23839         ret       ; return value at EDX:EAX
 23843   #endif
 23845 #elif (defined(__GNUC__) && defined(__x86_64__))
 23847   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23848       unsigned long val;
 23849       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 23850       return val;
 23853 #elif (defined(__GNUC__) && defined(__ppc__))
 23855   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 23856       unsigned long long retval;
 23857       unsigned long junk;
 23858       __asm__ __volatile__ ("\n\
 23859           1:      mftbu   %1\n\
 23860                   mftb    %L0\n\
 23861                   mftbu   %0\n\
 23862                   cmpw    %0,%1\n\
 23863                   bne     1b"
 23864                   : "=r" (retval), "=r" (junk));
 23865       return retval;
 23868 #else
 23870   #error Need implementation of sqlite3Hwtime() for your platform.
 23872   /*
 23873   ** To compile without implementing sqlite3Hwtime() for your platform,
 23874   ** you can remove the above #error and use the following
 23875   ** stub function.  You will lose timing support for many
 23876   ** of the debugging and testing utilities, but it should at
 23877   ** least compile and run.
 23878   */
 23879 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 23881 #endif
 23883 #endif /* !defined(_HWTIME_H_) */
 23885 /************** End of hwtime.h **********************************************/
 23886 /************** Continuing where we left off in os_common.h ******************/
 23888 static sqlite_uint64 g_start;
 23889 static sqlite_uint64 g_elapsed;
 23890 #define TIMER_START       g_start=sqlite3Hwtime()
 23891 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 23892 #define TIMER_ELAPSED     g_elapsed
 23893 #else
 23894 #define TIMER_START
 23895 #define TIMER_END
 23896 #define TIMER_ELAPSED     ((sqlite_uint64)0)
 23897 #endif
 23899 /*
 23900 ** If we compile with the SQLITE_TEST macro set, then the following block
 23901 ** of code will give us the ability to simulate a disk I/O error.  This
 23902 ** is used for testing the I/O recovery logic.
 23903 */
 23904 #ifdef SQLITE_TEST
 23905 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 23906 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 23907 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 23908 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 23909 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 23910 SQLITE_API int sqlite3_diskfull_pending = 0;
 23911 SQLITE_API int sqlite3_diskfull = 0;
 23912 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 23913 #define SimulateIOError(CODE)  \
 23914   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 23915        || sqlite3_io_error_pending-- == 1 )  \
 23916               { local_ioerr(); CODE; }
 23917 static void local_ioerr(){
 23918   IOTRACE(("IOERR\n"));
 23919   sqlite3_io_error_hit++;
 23920   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 23922 #define SimulateDiskfullError(CODE) \
 23923    if( sqlite3_diskfull_pending ){ \
 23924      if( sqlite3_diskfull_pending == 1 ){ \
 23925        local_ioerr(); \
 23926        sqlite3_diskfull = 1; \
 23927        sqlite3_io_error_hit = 1; \
 23928        CODE; \
 23929      }else{ \
 23930        sqlite3_diskfull_pending--; \
 23931      } \
 23933 #else
 23934 #define SimulateIOErrorBenign(X)
 23935 #define SimulateIOError(A)
 23936 #define SimulateDiskfullError(A)
 23937 #endif
 23939 /*
 23940 ** When testing, keep a count of the number of open files.
 23941 */
 23942 #ifdef SQLITE_TEST
 23943 SQLITE_API int sqlite3_open_file_count = 0;
 23944 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 23945 #else
 23946 #define OpenCounter(X)
 23947 #endif
 23949 #endif /* !defined(_OS_COMMON_H_) */
 23951 /************** End of os_common.h *******************************************/
 23952 /************** Continuing where we left off in os_unix.c ********************/
 23954 /*
 23955 ** Define various macros that are missing from some systems.
 23956 */
 23957 #ifndef O_LARGEFILE
 23958 # define O_LARGEFILE 0
 23959 #endif
 23960 #ifdef SQLITE_DISABLE_LFS
 23961 # undef O_LARGEFILE
 23962 # define O_LARGEFILE 0
 23963 #endif
 23964 #ifndef O_NOFOLLOW
 23965 # define O_NOFOLLOW 0
 23966 #endif
 23967 #ifndef O_BINARY
 23968 # define O_BINARY 0
 23969 #endif
 23971 /*
 23972 ** The threadid macro resolves to the thread-id or to 0.  Used for
 23973 ** testing and debugging only.
 23974 */
 23975 #if SQLITE_THREADSAFE
 23976 #define threadid pthread_self()
 23977 #else
 23978 #define threadid 0
 23979 #endif
 23981 /*
 23982 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
 23983 */
 23984 #if !defined(HAVE_MREMAP)
 23985 # if defined(__linux__) && defined(_GNU_SOURCE)
 23986 #  define HAVE_MREMAP 1
 23987 # else
 23988 #  define HAVE_MREMAP 0
 23989 # endif
 23990 #endif
 23992 /*
 23993 ** Different Unix systems declare open() in different ways.  Same use
 23994 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
 23995 ** The difference is important when using a pointer to the function.
 23996 **
 23997 ** The safest way to deal with the problem is to always use this wrapper
 23998 ** which always has the same well-defined interface.
 23999 */
 24000 static int posixOpen(const char *zFile, int flags, int mode){
 24001   return open(zFile, flags, mode);
 24004 /*
 24005 ** On some systems, calls to fchown() will trigger a message in a security
 24006 ** log if they come from non-root processes.  So avoid calling fchown() if
 24007 ** we are not running as root.
 24008 */
 24009 static int posixFchown(int fd, uid_t uid, gid_t gid){
 24010   return geteuid() ? 0 : fchown(fd,uid,gid);
 24013 /* Forward reference */
 24014 static int openDirectory(const char*, int*);
 24016 /*
 24017 ** Many system calls are accessed through pointer-to-functions so that
 24018 ** they may be overridden at runtime to facilitate fault injection during
 24019 ** testing and sandboxing.  The following array holds the names and pointers
 24020 ** to all overrideable system calls.
 24021 */
 24022 static struct unix_syscall {
 24023   const char *zName;            /* Name of the system call */
 24024   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 24025   sqlite3_syscall_ptr pDefault; /* Default value */
 24026 } aSyscall[] = {
 24027   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
 24028 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
 24030   { "close",        (sqlite3_syscall_ptr)close,      0  },
 24031 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
 24033   { "access",       (sqlite3_syscall_ptr)access,     0  },
 24034 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
 24036   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
 24037 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
 24039   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
 24040 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
 24042 /*
 24043 ** The DJGPP compiler environment looks mostly like Unix, but it
 24044 ** lacks the fcntl() system call.  So redefine fcntl() to be something
 24045 ** that always succeeds.  This means that locking does not occur under
 24046 ** DJGPP.  But it is DOS - what did you expect?
 24047 */
 24048 #ifdef __DJGPP__
 24049   { "fstat",        0,                 0  },
 24050 #define osFstat(a,b,c)    0
 24051 #else     
 24052   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
 24053 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
 24054 #endif
 24056   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
 24057 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
 24059   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
 24060 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
 24062   { "read",         (sqlite3_syscall_ptr)read,       0  },
 24063 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
 24065 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 24066   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
 24067 #else
 24068   { "pread",        (sqlite3_syscall_ptr)0,          0  },
 24069 #endif
 24070 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
 24072 #if defined(USE_PREAD64)
 24073   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
 24074 #else
 24075   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
 24076 #endif
 24077 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
 24079   { "write",        (sqlite3_syscall_ptr)write,      0  },
 24080 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
 24082 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
 24083   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
 24084 #else
 24085   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
 24086 #endif
 24087 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
 24088                     aSyscall[12].pCurrent)
 24090 #if defined(USE_PREAD64)
 24091   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
 24092 #else
 24093   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
 24094 #endif
 24095 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
 24096                     aSyscall[13].pCurrent)
 24098   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
 24099 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
 24101 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 24102   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
 24103 #else
 24104   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
 24105 #endif
 24106 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
 24108   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
 24109 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
 24111   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
 24112 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
 24114   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
 24115 #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
 24117   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
 24118 #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
 24120   { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
 24121 #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 24123 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 24124   { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
 24125 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
 24127   { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
 24128 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
 24130 #if HAVE_MREMAP
 24131   { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
 24132 #else
 24133   { "mremap",       (sqlite3_syscall_ptr)0,               0 },
 24134 #endif
 24135 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
 24136 #endif
 24138 }; /* End of the overrideable system calls */
 24140 /*
 24141 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 24142 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
 24143 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 24144 ** system call named zName.
 24145 */
 24146 static int unixSetSystemCall(
 24147   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 24148   const char *zName,            /* Name of system call to override */
 24149   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 24150 ){
 24151   unsigned int i;
 24152   int rc = SQLITE_NOTFOUND;
 24154   UNUSED_PARAMETER(pNotUsed);
 24155   if( zName==0 ){
 24156     /* If no zName is given, restore all system calls to their default
 24157     ** settings and return NULL
 24158     */
 24159     rc = SQLITE_OK;
 24160     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 24161       if( aSyscall[i].pDefault ){
 24162         aSyscall[i].pCurrent = aSyscall[i].pDefault;
 24165   }else{
 24166     /* If zName is specified, operate on only the one system call
 24167     ** specified.
 24168     */
 24169     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 24170       if( strcmp(zName, aSyscall[i].zName)==0 ){
 24171         if( aSyscall[i].pDefault==0 ){
 24172           aSyscall[i].pDefault = aSyscall[i].pCurrent;
 24174         rc = SQLITE_OK;
 24175         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 24176         aSyscall[i].pCurrent = pNewFunc;
 24177         break;
 24181   return rc;
 24184 /*
 24185 ** Return the value of a system call.  Return NULL if zName is not a
 24186 ** recognized system call name.  NULL is also returned if the system call
 24187 ** is currently undefined.
 24188 */
 24189 static sqlite3_syscall_ptr unixGetSystemCall(
 24190   sqlite3_vfs *pNotUsed,
 24191   const char *zName
 24192 ){
 24193   unsigned int i;
 24195   UNUSED_PARAMETER(pNotUsed);
 24196   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 24197     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 24199   return 0;
 24202 /*
 24203 ** Return the name of the first system call after zName.  If zName==NULL
 24204 ** then return the name of the first system call.  Return NULL if zName
 24205 ** is the last system call or if zName is not the name of a valid
 24206 ** system call.
 24207 */
 24208 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
 24209   int i = -1;
 24211   UNUSED_PARAMETER(p);
 24212   if( zName ){
 24213     for(i=0; i<ArraySize(aSyscall)-1; i++){
 24214       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 24217   for(i++; i<ArraySize(aSyscall); i++){
 24218     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 24220   return 0;
 24223 /*
 24224 ** Do not accept any file descriptor less than this value, in order to avoid
 24225 ** opening database file using file descriptors that are commonly used for 
 24226 ** standard input, output, and error.
 24227 */
 24228 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
 24229 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
 24230 #endif
 24232 /*
 24233 ** Invoke open().  Do so multiple times, until it either succeeds or
 24234 ** fails for some reason other than EINTR.
 24235 **
 24236 ** If the file creation mode "m" is 0 then set it to the default for
 24237 ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
 24238 ** 0644) as modified by the system umask.  If m is not 0, then
 24239 ** make the file creation mode be exactly m ignoring the umask.
 24240 **
 24241 ** The m parameter will be non-zero only when creating -wal, -journal,
 24242 ** and -shm files.  We want those files to have *exactly* the same
 24243 ** permissions as their original database, unadulterated by the umask.
 24244 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
 24245 ** transaction crashes and leaves behind hot journals, then any
 24246 ** process that is able to write to the database will also be able to
 24247 ** recover the hot journals.
 24248 */
 24249 static int robust_open(const char *z, int f, mode_t m){
 24250   int fd;
 24251   mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
 24252   while(1){
 24253 #if defined(O_CLOEXEC)
 24254     fd = osOpen(z,f|O_CLOEXEC,m2);
 24255 #else
 24256     fd = osOpen(z,f,m2);
 24257 #endif
 24258     if( fd<0 ){
 24259       if( errno==EINTR ) continue;
 24260       break;
 24262     if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
 24263     osClose(fd);
 24264     sqlite3_log(SQLITE_WARNING, 
 24265                 "attempt to open \"%s\" as file descriptor %d", z, fd);
 24266     fd = -1;
 24267     if( osOpen("/dev/null", f, m)<0 ) break;
 24269   if( fd>=0 ){
 24270     if( m!=0 ){
 24271       struct stat statbuf;
 24272       if( osFstat(fd, &statbuf)==0 
 24273        && statbuf.st_size==0
 24274        && (statbuf.st_mode&0777)!=m 
 24275       ){
 24276         osFchmod(fd, m);
 24279 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
 24280     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 24281 #endif
 24283   return fd;
 24286 /*
 24287 ** Helper functions to obtain and relinquish the global mutex. The
 24288 ** global mutex is used to protect the unixInodeInfo and
 24289 ** vxworksFileId objects used by this file, all of which may be 
 24290 ** shared by multiple threads.
 24291 **
 24292 ** Function unixMutexHeld() is used to assert() that the global mutex 
 24293 ** is held when required. This function is only used as part of assert() 
 24294 ** statements. e.g.
 24295 **
 24296 **   unixEnterMutex()
 24297 **     assert( unixMutexHeld() );
 24298 **   unixEnterLeave()
 24299 */
 24300 static void unixEnterMutex(void){
 24301   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 24303 static void unixLeaveMutex(void){
 24304   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 24306 #ifdef SQLITE_DEBUG
 24307 static int unixMutexHeld(void) {
 24308   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 24310 #endif
 24313 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 24314 /*
 24315 ** Helper function for printing out trace information from debugging
 24316 ** binaries. This returns the string represetation of the supplied
 24317 ** integer lock-type.
 24318 */
 24319 static const char *azFileLock(int eFileLock){
 24320   switch( eFileLock ){
 24321     case NO_LOCK: return "NONE";
 24322     case SHARED_LOCK: return "SHARED";
 24323     case RESERVED_LOCK: return "RESERVED";
 24324     case PENDING_LOCK: return "PENDING";
 24325     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
 24327   return "ERROR";
 24329 #endif
 24331 #ifdef SQLITE_LOCK_TRACE
 24332 /*
 24333 ** Print out information about all locking operations.
 24334 **
 24335 ** This routine is used for troubleshooting locks on multithreaded
 24336 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
 24337 ** command-line option on the compiler.  This code is normally
 24338 ** turned off.
 24339 */
 24340 static int lockTrace(int fd, int op, struct flock *p){
 24341   char *zOpName, *zType;
 24342   int s;
 24343   int savedErrno;
 24344   if( op==F_GETLK ){
 24345     zOpName = "GETLK";
 24346   }else if( op==F_SETLK ){
 24347     zOpName = "SETLK";
 24348   }else{
 24349     s = osFcntl(fd, op, p);
 24350     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
 24351     return s;
 24353   if( p->l_type==F_RDLCK ){
 24354     zType = "RDLCK";
 24355   }else if( p->l_type==F_WRLCK ){
 24356     zType = "WRLCK";
 24357   }else if( p->l_type==F_UNLCK ){
 24358     zType = "UNLCK";
 24359   }else{
 24360     assert( 0 );
 24362   assert( p->l_whence==SEEK_SET );
 24363   s = osFcntl(fd, op, p);
 24364   savedErrno = errno;
 24365   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
 24366      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
 24367      (int)p->l_pid, s);
 24368   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
 24369     struct flock l2;
 24370     l2 = *p;
 24371     osFcntl(fd, F_GETLK, &l2);
 24372     if( l2.l_type==F_RDLCK ){
 24373       zType = "RDLCK";
 24374     }else if( l2.l_type==F_WRLCK ){
 24375       zType = "WRLCK";
 24376     }else if( l2.l_type==F_UNLCK ){
 24377       zType = "UNLCK";
 24378     }else{
 24379       assert( 0 );
 24381     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
 24382        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
 24384   errno = savedErrno;
 24385   return s;
 24387 #undef osFcntl
 24388 #define osFcntl lockTrace
 24389 #endif /* SQLITE_LOCK_TRACE */
 24391 /*
 24392 ** Retry ftruncate() calls that fail due to EINTR
 24393 */
 24394 static int robust_ftruncate(int h, sqlite3_int64 sz){
 24395   int rc;
 24396   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
 24397   return rc;
 24400 /*
 24401 ** This routine translates a standard POSIX errno code into something
 24402 ** useful to the clients of the sqlite3 functions.  Specifically, it is
 24403 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
 24404 ** and a variety of "please close the file descriptor NOW" errors into 
 24405 ** SQLITE_IOERR
 24406 ** 
 24407 ** Errors during initialization of locks, or file system support for locks,
 24408 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
 24409 */
 24410 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
 24411   switch (posixError) {
 24412 #if 0
 24413   /* At one point this code was not commented out. In theory, this branch
 24414   ** should never be hit, as this function should only be called after
 24415   ** a locking-related function (i.e. fcntl()) has returned non-zero with
 24416   ** the value of errno as the first argument. Since a system call has failed,
 24417   ** errno should be non-zero.
 24418   **
 24419   ** Despite this, if errno really is zero, we still don't want to return
 24420   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
 24421   ** propagated back to the caller. Commenting this branch out means errno==0
 24422   ** will be handled by the "default:" case below.
 24423   */
 24424   case 0: 
 24425     return SQLITE_OK;
 24426 #endif
 24428   case EAGAIN:
 24429   case ETIMEDOUT:
 24430   case EBUSY:
 24431   case EINTR:
 24432   case ENOLCK:  
 24433     /* random NFS retry error, unless during file system support 
 24434      * introspection, in which it actually means what it says */
 24435     return SQLITE_BUSY;
 24437   case EACCES: 
 24438     /* EACCES is like EAGAIN during locking operations, but not any other time*/
 24439     if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
 24440         (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
 24441         (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
 24442         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
 24443       return SQLITE_BUSY;
 24445     /* else fall through */
 24446   case EPERM: 
 24447     return SQLITE_PERM;
 24449   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
 24450   ** this module never makes such a call. And the code in SQLite itself 
 24451   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
 24452   ** this case is also commented out. If the system does set errno to EDEADLK,
 24453   ** the default SQLITE_IOERR_XXX code will be returned. */
 24454 #if 0
 24455   case EDEADLK:
 24456     return SQLITE_IOERR_BLOCKED;
 24457 #endif
 24459 #if EOPNOTSUPP!=ENOTSUP
 24460   case EOPNOTSUPP: 
 24461     /* something went terribly awry, unless during file system support 
 24462      * introspection, in which it actually means what it says */
 24463 #endif
 24464 #ifdef ENOTSUP
 24465   case ENOTSUP: 
 24466     /* invalid fd, unless during file system support introspection, in which 
 24467      * it actually means what it says */
 24468 #endif
 24469   case EIO:
 24470   case EBADF:
 24471   case EINVAL:
 24472   case ENOTCONN:
 24473   case ENODEV:
 24474   case ENXIO:
 24475   case ENOENT:
 24476 #ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
 24477   case ESTALE:
 24478 #endif
 24479   case ENOSYS:
 24480     /* these should force the client to close the file and reconnect */
 24482   default: 
 24483     return sqliteIOErr;
 24488 /******************************************************************************
 24489 ****************** Begin Unique File ID Utility Used By VxWorks ***************
 24490 **
 24491 ** On most versions of unix, we can get a unique ID for a file by concatenating
 24492 ** the device number and the inode number.  But this does not work on VxWorks.
 24493 ** On VxWorks, a unique file id must be based on the canonical filename.
 24494 **
 24495 ** A pointer to an instance of the following structure can be used as a
 24496 ** unique file ID in VxWorks.  Each instance of this structure contains
 24497 ** a copy of the canonical filename.  There is also a reference count.  
 24498 ** The structure is reclaimed when the number of pointers to it drops to
 24499 ** zero.
 24500 **
 24501 ** There are never very many files open at one time and lookups are not
 24502 ** a performance-critical path, so it is sufficient to put these
 24503 ** structures on a linked list.
 24504 */
 24505 struct vxworksFileId {
 24506   struct vxworksFileId *pNext;  /* Next in a list of them all */
 24507   int nRef;                     /* Number of references to this one */
 24508   int nName;                    /* Length of the zCanonicalName[] string */
 24509   char *zCanonicalName;         /* Canonical filename */
 24510 };
 24512 #if OS_VXWORKS
 24513 /* 
 24514 ** All unique filenames are held on a linked list headed by this
 24515 ** variable:
 24516 */
 24517 static struct vxworksFileId *vxworksFileList = 0;
 24519 /*
 24520 ** Simplify a filename into its canonical form
 24521 ** by making the following changes:
 24522 **
 24523 **  * removing any trailing and duplicate /
 24524 **  * convert /./ into just /
 24525 **  * convert /A/../ where A is any simple name into just /
 24526 **
 24527 ** Changes are made in-place.  Return the new name length.
 24528 **
 24529 ** The original filename is in z[0..n-1].  Return the number of
 24530 ** characters in the simplified name.
 24531 */
 24532 static int vxworksSimplifyName(char *z, int n){
 24533   int i, j;
 24534   while( n>1 && z[n-1]=='/' ){ n--; }
 24535   for(i=j=0; i<n; i++){
 24536     if( z[i]=='/' ){
 24537       if( z[i+1]=='/' ) continue;
 24538       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
 24539         i += 1;
 24540         continue;
 24542       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
 24543         while( j>0 && z[j-1]!='/' ){ j--; }
 24544         if( j>0 ){ j--; }
 24545         i += 2;
 24546         continue;
 24549     z[j++] = z[i];
 24551   z[j] = 0;
 24552   return j;
 24555 /*
 24556 ** Find a unique file ID for the given absolute pathname.  Return
 24557 ** a pointer to the vxworksFileId object.  This pointer is the unique
 24558 ** file ID.
 24559 **
 24560 ** The nRef field of the vxworksFileId object is incremented before
 24561 ** the object is returned.  A new vxworksFileId object is created
 24562 ** and added to the global list if necessary.
 24563 **
 24564 ** If a memory allocation error occurs, return NULL.
 24565 */
 24566 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
 24567   struct vxworksFileId *pNew;         /* search key and new file ID */
 24568   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
 24569   int n;                              /* Length of zAbsoluteName string */
 24571   assert( zAbsoluteName[0]=='/' );
 24572   n = (int)strlen(zAbsoluteName);
 24573   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
 24574   if( pNew==0 ) return 0;
 24575   pNew->zCanonicalName = (char*)&pNew[1];
 24576   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
 24577   n = vxworksSimplifyName(pNew->zCanonicalName, n);
 24579   /* Search for an existing entry that matching the canonical name.
 24580   ** If found, increment the reference count and return a pointer to
 24581   ** the existing file ID.
 24582   */
 24583   unixEnterMutex();
 24584   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
 24585     if( pCandidate->nName==n 
 24586      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
 24587     ){
 24588        sqlite3_free(pNew);
 24589        pCandidate->nRef++;
 24590        unixLeaveMutex();
 24591        return pCandidate;
 24595   /* No match was found.  We will make a new file ID */
 24596   pNew->nRef = 1;
 24597   pNew->nName = n;
 24598   pNew->pNext = vxworksFileList;
 24599   vxworksFileList = pNew;
 24600   unixLeaveMutex();
 24601   return pNew;
 24604 /*
 24605 ** Decrement the reference count on a vxworksFileId object.  Free
 24606 ** the object when the reference count reaches zero.
 24607 */
 24608 static void vxworksReleaseFileId(struct vxworksFileId *pId){
 24609   unixEnterMutex();
 24610   assert( pId->nRef>0 );
 24611   pId->nRef--;
 24612   if( pId->nRef==0 ){
 24613     struct vxworksFileId **pp;
 24614     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
 24615     assert( *pp==pId );
 24616     *pp = pId->pNext;
 24617     sqlite3_free(pId);
 24619   unixLeaveMutex();
 24621 #endif /* OS_VXWORKS */
 24622 /*************** End of Unique File ID Utility Used By VxWorks ****************
 24623 ******************************************************************************/
 24626 /******************************************************************************
 24627 *************************** Posix Advisory Locking ****************************
 24628 **
 24629 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
 24630 ** section 6.5.2.2 lines 483 through 490 specify that when a process
 24631 ** sets or clears a lock, that operation overrides any prior locks set
 24632 ** by the same process.  It does not explicitly say so, but this implies
 24633 ** that it overrides locks set by the same process using a different
 24634 ** file descriptor.  Consider this test case:
 24635 **
 24636 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
 24637 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
 24638 **
 24639 ** Suppose ./file1 and ./file2 are really the same file (because
 24640 ** one is a hard or symbolic link to the other) then if you set
 24641 ** an exclusive lock on fd1, then try to get an exclusive lock
 24642 ** on fd2, it works.  I would have expected the second lock to
 24643 ** fail since there was already a lock on the file due to fd1.
 24644 ** But not so.  Since both locks came from the same process, the
 24645 ** second overrides the first, even though they were on different
 24646 ** file descriptors opened on different file names.
 24647 **
 24648 ** This means that we cannot use POSIX locks to synchronize file access
 24649 ** among competing threads of the same process.  POSIX locks will work fine
 24650 ** to synchronize access for threads in separate processes, but not
 24651 ** threads within the same process.
 24652 **
 24653 ** To work around the problem, SQLite has to manage file locks internally
 24654 ** on its own.  Whenever a new database is opened, we have to find the
 24655 ** specific inode of the database file (the inode is determined by the
 24656 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
 24657 ** and check for locks already existing on that inode.  When locks are
 24658 ** created or removed, we have to look at our own internal record of the
 24659 ** locks to see if another thread has previously set a lock on that same
 24660 ** inode.
 24661 **
 24662 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
 24663 ** For VxWorks, we have to use the alternative unique ID system based on
 24664 ** canonical filename and implemented in the previous division.)
 24665 **
 24666 ** The sqlite3_file structure for POSIX is no longer just an integer file
 24667 ** descriptor.  It is now a structure that holds the integer file
 24668 ** descriptor and a pointer to a structure that describes the internal
 24669 ** locks on the corresponding inode.  There is one locking structure
 24670 ** per inode, so if the same inode is opened twice, both unixFile structures
 24671 ** point to the same locking structure.  The locking structure keeps
 24672 ** a reference count (so we will know when to delete it) and a "cnt"
 24673 ** field that tells us its internal lock status.  cnt==0 means the
 24674 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
 24675 ** cnt>0 means there are cnt shared locks on the file.
 24676 **
 24677 ** Any attempt to lock or unlock a file first checks the locking
 24678 ** structure.  The fcntl() system call is only invoked to set a 
 24679 ** POSIX lock if the internal lock structure transitions between
 24680 ** a locked and an unlocked state.
 24681 **
 24682 ** But wait:  there are yet more problems with POSIX advisory locks.
 24683 **
 24684 ** If you close a file descriptor that points to a file that has locks,
 24685 ** all locks on that file that are owned by the current process are
 24686 ** released.  To work around this problem, each unixInodeInfo object
 24687 ** maintains a count of the number of pending locks on tha inode.
 24688 ** When an attempt is made to close an unixFile, if there are
 24689 ** other unixFile open on the same inode that are holding locks, the call
 24690 ** to close() the file descriptor is deferred until all of the locks clear.
 24691 ** The unixInodeInfo structure keeps a list of file descriptors that need to
 24692 ** be closed and that list is walked (and cleared) when the last lock
 24693 ** clears.
 24694 **
 24695 ** Yet another problem:  LinuxThreads do not play well with posix locks.
 24696 **
 24697 ** Many older versions of linux use the LinuxThreads library which is
 24698 ** not posix compliant.  Under LinuxThreads, a lock created by thread
 24699 ** A cannot be modified or overridden by a different thread B.
 24700 ** Only thread A can modify the lock.  Locking behavior is correct
 24701 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
 24702 ** on linux - with NPTL a lock created by thread A can override locks
 24703 ** in thread B.  But there is no way to know at compile-time which
 24704 ** threading library is being used.  So there is no way to know at
 24705 ** compile-time whether or not thread A can override locks on thread B.
 24706 ** One has to do a run-time check to discover the behavior of the
 24707 ** current process.
 24708 **
 24709 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
 24710 ** was dropped beginning with version 3.7.0.  SQLite will still work with
 24711 ** LinuxThreads provided that (1) there is no more than one connection 
 24712 ** per database file in the same process and (2) database connections
 24713 ** do not move across threads.
 24714 */
 24716 /*
 24717 ** An instance of the following structure serves as the key used
 24718 ** to locate a particular unixInodeInfo object.
 24719 */
 24720 struct unixFileId {
 24721   dev_t dev;                  /* Device number */
 24722 #if OS_VXWORKS
 24723   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
 24724 #else
 24725   ino_t ino;                  /* Inode number */
 24726 #endif
 24727 };
 24729 /*
 24730 ** An instance of the following structure is allocated for each open
 24731 ** inode.  Or, on LinuxThreads, there is one of these structures for
 24732 ** each inode opened by each thread.
 24733 **
 24734 ** A single inode can have multiple file descriptors, so each unixFile
 24735 ** structure contains a pointer to an instance of this object and this
 24736 ** object keeps a count of the number of unixFile pointing to it.
 24737 */
 24738 struct unixInodeInfo {
 24739   struct unixFileId fileId;       /* The lookup key */
 24740   int nShared;                    /* Number of SHARED locks held */
 24741   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
 24742   unsigned char bProcessLock;     /* An exclusive process lock is held */
 24743   int nRef;                       /* Number of pointers to this structure */
 24744   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
 24745   int nLock;                      /* Number of outstanding file locks */
 24746   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
 24747   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
 24748   unixInodeInfo *pPrev;           /*    .... doubly linked */
 24749 #if SQLITE_ENABLE_LOCKING_STYLE
 24750   unsigned long long sharedByte;  /* for AFP simulated shared lock */
 24751 #endif
 24752 #if OS_VXWORKS
 24753   sem_t *pSem;                    /* Named POSIX semaphore */
 24754   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
 24755 #endif
 24756 };
 24758 /*
 24759 ** A lists of all unixInodeInfo objects.
 24760 */
 24761 static unixInodeInfo *inodeList = 0;
 24763 /*
 24764 **
 24765 ** This function - unixLogError_x(), is only ever called via the macro
 24766 ** unixLogError().
 24767 **
 24768 ** It is invoked after an error occurs in an OS function and errno has been
 24769 ** set. It logs a message using sqlite3_log() containing the current value of
 24770 ** errno and, if possible, the human-readable equivalent from strerror() or
 24771 ** strerror_r().
 24772 **
 24773 ** The first argument passed to the macro should be the error code that
 24774 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 24775 ** The two subsequent arguments should be the name of the OS function that
 24776 ** failed (e.g. "unlink", "open") and the associated file-system path,
 24777 ** if any.
 24778 */
 24779 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
 24780 static int unixLogErrorAtLine(
 24781   int errcode,                    /* SQLite error code */
 24782   const char *zFunc,              /* Name of OS function that failed */
 24783   const char *zPath,              /* File path associated with error */
 24784   int iLine                       /* Source line number where error occurred */
 24785 ){
 24786   char *zErr;                     /* Message from strerror() or equivalent */
 24787   int iErrno = errno;             /* Saved syscall error number */
 24789   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
 24790   ** the strerror() function to obtain the human-readable error message
 24791   ** equivalent to errno. Otherwise, use strerror_r().
 24792   */ 
 24793 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
 24794   char aErr[80];
 24795   memset(aErr, 0, sizeof(aErr));
 24796   zErr = aErr;
 24798   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
 24799   ** assume that the system provides the GNU version of strerror_r() that
 24800   ** returns a pointer to a buffer containing the error message. That pointer 
 24801   ** may point to aErr[], or it may point to some static storage somewhere. 
 24802   ** Otherwise, assume that the system provides the POSIX version of 
 24803   ** strerror_r(), which always writes an error message into aErr[].
 24804   **
 24805   ** If the code incorrectly assumes that it is the POSIX version that is
 24806   ** available, the error message will often be an empty string. Not a
 24807   ** huge problem. Incorrectly concluding that the GNU version is available 
 24808   ** could lead to a segfault though.
 24809   */
 24810 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
 24811   zErr = 
 24812 # endif
 24813   strerror_r(iErrno, aErr, sizeof(aErr)-1);
 24815 #elif SQLITE_THREADSAFE
 24816   /* This is a threadsafe build, but strerror_r() is not available. */
 24817   zErr = "";
 24818 #else
 24819   /* Non-threadsafe build, use strerror(). */
 24820   zErr = strerror(iErrno);
 24821 #endif
 24823   if( zPath==0 ) zPath = "";
 24824   sqlite3_log(errcode,
 24825       "os_unix.c:%d: (%d) %s(%s) - %s",
 24826       iLine, iErrno, zFunc, zPath, zErr
 24827   );
 24829   return errcode;
 24832 /*
 24833 ** Close a file descriptor.
 24834 **
 24835 ** We assume that close() almost always works, since it is only in a
 24836 ** very sick application or on a very sick platform that it might fail.
 24837 ** If it does fail, simply leak the file descriptor, but do log the
 24838 ** error.
 24839 **
 24840 ** Note that it is not safe to retry close() after EINTR since the
 24841 ** file descriptor might have already been reused by another thread.
 24842 ** So we don't even try to recover from an EINTR.  Just log the error
 24843 ** and move on.
 24844 */
 24845 static void robust_close(unixFile *pFile, int h, int lineno){
 24846   if( osClose(h) ){
 24847     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
 24848                        pFile ? pFile->zPath : 0, lineno);
 24852 /*
 24853 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
 24854 */ 
 24855 static void closePendingFds(unixFile *pFile){
 24856   unixInodeInfo *pInode = pFile->pInode;
 24857   UnixUnusedFd *p;
 24858   UnixUnusedFd *pNext;
 24859   for(p=pInode->pUnused; p; p=pNext){
 24860     pNext = p->pNext;
 24861     robust_close(pFile, p->fd, __LINE__);
 24862     sqlite3_free(p);
 24864   pInode->pUnused = 0;
 24867 /*
 24868 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 24869 **
 24870 ** The mutex entered using the unixEnterMutex() function must be held
 24871 ** when this function is called.
 24872 */
 24873 static void releaseInodeInfo(unixFile *pFile){
 24874   unixInodeInfo *pInode = pFile->pInode;
 24875   assert( unixMutexHeld() );
 24876   if( ALWAYS(pInode) ){
 24877     pInode->nRef--;
 24878     if( pInode->nRef==0 ){
 24879       assert( pInode->pShmNode==0 );
 24880       closePendingFds(pFile);
 24881       if( pInode->pPrev ){
 24882         assert( pInode->pPrev->pNext==pInode );
 24883         pInode->pPrev->pNext = pInode->pNext;
 24884       }else{
 24885         assert( inodeList==pInode );
 24886         inodeList = pInode->pNext;
 24888       if( pInode->pNext ){
 24889         assert( pInode->pNext->pPrev==pInode );
 24890         pInode->pNext->pPrev = pInode->pPrev;
 24892       sqlite3_free(pInode);
 24897 /*
 24898 ** Given a file descriptor, locate the unixInodeInfo object that
 24899 ** describes that file descriptor.  Create a new one if necessary.  The
 24900 ** return value might be uninitialized if an error occurs.
 24901 **
 24902 ** The mutex entered using the unixEnterMutex() function must be held
 24903 ** when this function is called.
 24904 **
 24905 ** Return an appropriate error code.
 24906 */
 24907 static int findInodeInfo(
 24908   unixFile *pFile,               /* Unix file with file desc used in the key */
 24909   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
 24910 ){
 24911   int rc;                        /* System call return code */
 24912   int fd;                        /* The file descriptor for pFile */
 24913   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
 24914   struct stat statbuf;           /* Low-level file information */
 24915   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
 24917   assert( unixMutexHeld() );
 24919   /* Get low-level information about the file that we can used to
 24920   ** create a unique name for the file.
 24921   */
 24922   fd = pFile->h;
 24923   rc = osFstat(fd, &statbuf);
 24924   if( rc!=0 ){
 24925     pFile->lastErrno = errno;
 24926 #ifdef EOVERFLOW
 24927     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
 24928 #endif
 24929     return SQLITE_IOERR;
 24932 #ifdef __APPLE__
 24933   /* On OS X on an msdos filesystem, the inode number is reported
 24934   ** incorrectly for zero-size files.  See ticket #3260.  To work
 24935   ** around this problem (we consider it a bug in OS X, not SQLite)
 24936   ** we always increase the file size to 1 by writing a single byte
 24937   ** prior to accessing the inode number.  The one byte written is
 24938   ** an ASCII 'S' character which also happens to be the first byte
 24939   ** in the header of every SQLite database.  In this way, if there
 24940   ** is a race condition such that another thread has already populated
 24941   ** the first page of the database, no damage is done.
 24942   */
 24943   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
 24944     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
 24945     if( rc!=1 ){
 24946       pFile->lastErrno = errno;
 24947       return SQLITE_IOERR;
 24949     rc = osFstat(fd, &statbuf);
 24950     if( rc!=0 ){
 24951       pFile->lastErrno = errno;
 24952       return SQLITE_IOERR;
 24955 #endif
 24957   memset(&fileId, 0, sizeof(fileId));
 24958   fileId.dev = statbuf.st_dev;
 24959 #if OS_VXWORKS
 24960   fileId.pId = pFile->pId;
 24961 #else
 24962   fileId.ino = statbuf.st_ino;
 24963 #endif
 24964   pInode = inodeList;
 24965   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
 24966     pInode = pInode->pNext;
 24968   if( pInode==0 ){
 24969     pInode = sqlite3_malloc( sizeof(*pInode) );
 24970     if( pInode==0 ){
 24971       return SQLITE_NOMEM;
 24973     memset(pInode, 0, sizeof(*pInode));
 24974     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
 24975     pInode->nRef = 1;
 24976     pInode->pNext = inodeList;
 24977     pInode->pPrev = 0;
 24978     if( inodeList ) inodeList->pPrev = pInode;
 24979     inodeList = pInode;
 24980   }else{
 24981     pInode->nRef++;
 24983   *ppInode = pInode;
 24984   return SQLITE_OK;
 24987 /*
 24988 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
 24989 */
 24990 static int fileHasMoved(unixFile *pFile){
 24991   struct stat buf;
 24992   return pFile->pInode!=0 &&
 24993          (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
 24997 /*
 24998 ** Check a unixFile that is a database.  Verify the following:
 24999 **
 25000 ** (1) There is exactly one hard link on the file
 25001 ** (2) The file is not a symbolic link
 25002 ** (3) The file has not been renamed or unlinked
 25003 **
 25004 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
 25005 */
 25006 static void verifyDbFile(unixFile *pFile){
 25007   struct stat buf;
 25008   int rc;
 25009   if( pFile->ctrlFlags & UNIXFILE_WARNED ){
 25010     /* One or more of the following warnings have already been issued.  Do not
 25011     ** repeat them so as not to clutter the error log */
 25012     return;
 25014   rc = osFstat(pFile->h, &buf);
 25015   if( rc!=0 ){
 25016     sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
 25017     pFile->ctrlFlags |= UNIXFILE_WARNED;
 25018     return;
 25020   if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
 25021     sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
 25022     pFile->ctrlFlags |= UNIXFILE_WARNED;
 25023     return;
 25025   if( buf.st_nlink>1 ){
 25026     sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
 25027     pFile->ctrlFlags |= UNIXFILE_WARNED;
 25028     return;
 25030   if( fileHasMoved(pFile) ){
 25031     sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
 25032     pFile->ctrlFlags |= UNIXFILE_WARNED;
 25033     return;
 25038 /*
 25039 ** This routine checks if there is a RESERVED lock held on the specified
 25040 ** file by this or any other process. If such a lock is held, set *pResOut
 25041 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25042 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25043 */
 25044 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
 25045   int rc = SQLITE_OK;
 25046   int reserved = 0;
 25047   unixFile *pFile = (unixFile*)id;
 25049   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25051   assert( pFile );
 25052   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 25054   /* Check if a thread in this process holds such a lock */
 25055   if( pFile->pInode->eFileLock>SHARED_LOCK ){
 25056     reserved = 1;
 25059   /* Otherwise see if some other process holds it.
 25060   */
 25061 #ifndef __DJGPP__
 25062   if( !reserved && !pFile->pInode->bProcessLock ){
 25063     struct flock lock;
 25064     lock.l_whence = SEEK_SET;
 25065     lock.l_start = RESERVED_BYTE;
 25066     lock.l_len = 1;
 25067     lock.l_type = F_WRLCK;
 25068     if( osFcntl(pFile->h, F_GETLK, &lock) ){
 25069       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
 25070       pFile->lastErrno = errno;
 25071     } else if( lock.l_type!=F_UNLCK ){
 25072       reserved = 1;
 25075 #endif
 25077   unixLeaveMutex();
 25078   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 25080   *pResOut = reserved;
 25081   return rc;
 25084 /*
 25085 ** Attempt to set a system-lock on the file pFile.  The lock is 
 25086 ** described by pLock.
 25087 **
 25088 ** If the pFile was opened read/write from unix-excl, then the only lock
 25089 ** ever obtained is an exclusive lock, and it is obtained exactly once
 25090 ** the first time any lock is attempted.  All subsequent system locking
 25091 ** operations become no-ops.  Locking operations still happen internally,
 25092 ** in order to coordinate access between separate database connections
 25093 ** within this process, but all of that is handled in memory and the
 25094 ** operating system does not participate.
 25095 **
 25096 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
 25097 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
 25098 ** and is read-only.
 25099 **
 25100 ** Zero is returned if the call completes successfully, or -1 if a call
 25101 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
 25102 */
 25103 static int unixFileLock(unixFile *pFile, struct flock *pLock){
 25104   int rc;
 25105   unixInodeInfo *pInode = pFile->pInode;
 25106   assert( unixMutexHeld() );
 25107   assert( pInode!=0 );
 25108   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
 25109    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
 25110   ){
 25111     if( pInode->bProcessLock==0 ){
 25112       struct flock lock;
 25113       assert( pInode->nLock==0 );
 25114       lock.l_whence = SEEK_SET;
 25115       lock.l_start = SHARED_FIRST;
 25116       lock.l_len = SHARED_SIZE;
 25117       lock.l_type = F_WRLCK;
 25118       rc = osFcntl(pFile->h, F_SETLK, &lock);
 25119       if( rc<0 ) return rc;
 25120       pInode->bProcessLock = 1;
 25121       pInode->nLock++;
 25122     }else{
 25123       rc = 0;
 25125   }else{
 25126     rc = osFcntl(pFile->h, F_SETLK, pLock);
 25128   return rc;
 25131 /*
 25132 ** Lock the file with the lock specified by parameter eFileLock - one
 25133 ** of the following:
 25134 **
 25135 **     (1) SHARED_LOCK
 25136 **     (2) RESERVED_LOCK
 25137 **     (3) PENDING_LOCK
 25138 **     (4) EXCLUSIVE_LOCK
 25139 **
 25140 ** Sometimes when requesting one lock state, additional lock states
 25141 ** are inserted in between.  The locking might fail on one of the later
 25142 ** transitions leaving the lock state different from what it started but
 25143 ** still short of its goal.  The following chart shows the allowed
 25144 ** transitions and the inserted intermediate states:
 25145 **
 25146 **    UNLOCKED -> SHARED
 25147 **    SHARED -> RESERVED
 25148 **    SHARED -> (PENDING) -> EXCLUSIVE
 25149 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25150 **    PENDING -> EXCLUSIVE
 25151 **
 25152 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 25153 ** routine to lower a locking level.
 25154 */
 25155 static int unixLock(sqlite3_file *id, int eFileLock){
 25156   /* The following describes the implementation of the various locks and
 25157   ** lock transitions in terms of the POSIX advisory shared and exclusive
 25158   ** lock primitives (called read-locks and write-locks below, to avoid
 25159   ** confusion with SQLite lock names). The algorithms are complicated
 25160   ** slightly in order to be compatible with windows systems simultaneously
 25161   ** accessing the same database file, in case that is ever required.
 25162   **
 25163   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
 25164   ** byte', each single bytes at well known offsets, and the 'shared byte
 25165   ** range', a range of 510 bytes at a well known offset.
 25166   **
 25167   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
 25168   ** byte'.  If this is successful, a random byte from the 'shared byte
 25169   ** range' is read-locked and the lock on the 'pending byte' released.
 25170   **
 25171   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
 25172   ** A RESERVED lock is implemented by grabbing a write-lock on the
 25173   ** 'reserved byte'. 
 25174   **
 25175   ** A process may only obtain a PENDING lock after it has obtained a
 25176   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
 25177   ** on the 'pending byte'. This ensures that no new SHARED locks can be
 25178   ** obtained, but existing SHARED locks are allowed to persist. A process
 25179   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
 25180   ** This property is used by the algorithm for rolling back a journal file
 25181   ** after a crash.
 25182   **
 25183   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
 25184   ** implemented by obtaining a write-lock on the entire 'shared byte
 25185   ** range'. Since all other locks require a read-lock on one of the bytes
 25186   ** within this range, this ensures that no other locks are held on the
 25187   ** database. 
 25188   **
 25189   ** The reason a single byte cannot be used instead of the 'shared byte
 25190   ** range' is that some versions of windows do not support read-locks. By
 25191   ** locking a random byte from a range, concurrent SHARED locks may exist
 25192   ** even if the locking primitive used is always a write-lock.
 25193   */
 25194   int rc = SQLITE_OK;
 25195   unixFile *pFile = (unixFile*)id;
 25196   unixInodeInfo *pInode;
 25197   struct flock lock;
 25198   int tErrno = 0;
 25200   assert( pFile );
 25201   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
 25202       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 25203       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
 25205   /* If there is already a lock of this type or more restrictive on the
 25206   ** unixFile, do nothing. Don't use the end_lock: exit path, as
 25207   ** unixEnterMutex() hasn't been called yet.
 25208   */
 25209   if( pFile->eFileLock>=eFileLock ){
 25210     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
 25211             azFileLock(eFileLock)));
 25212     return SQLITE_OK;
 25215   /* Make sure the locking sequence is correct.
 25216   **  (1) We never move from unlocked to anything higher than shared lock.
 25217   **  (2) SQLite never explicitly requests a pendig lock.
 25218   **  (3) A shared lock is always held when a reserve lock is requested.
 25219   */
 25220   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 25221   assert( eFileLock!=PENDING_LOCK );
 25222   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 25224   /* This mutex is needed because pFile->pInode is shared across threads
 25225   */
 25226   unixEnterMutex();
 25227   pInode = pFile->pInode;
 25229   /* If some thread using this PID has a lock via a different unixFile*
 25230   ** handle that precludes the requested lock, return BUSY.
 25231   */
 25232   if( (pFile->eFileLock!=pInode->eFileLock && 
 25233           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 25234   ){
 25235     rc = SQLITE_BUSY;
 25236     goto end_lock;
 25239   /* If a SHARED lock is requested, and some thread using this PID already
 25240   ** has a SHARED or RESERVED lock, then increment reference counts and
 25241   ** return SQLITE_OK.
 25242   */
 25243   if( eFileLock==SHARED_LOCK && 
 25244       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 25245     assert( eFileLock==SHARED_LOCK );
 25246     assert( pFile->eFileLock==0 );
 25247     assert( pInode->nShared>0 );
 25248     pFile->eFileLock = SHARED_LOCK;
 25249     pInode->nShared++;
 25250     pInode->nLock++;
 25251     goto end_lock;
 25255   /* A PENDING lock is needed before acquiring a SHARED lock and before
 25256   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 25257   ** be released.
 25258   */
 25259   lock.l_len = 1L;
 25260   lock.l_whence = SEEK_SET;
 25261   if( eFileLock==SHARED_LOCK 
 25262       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 25263   ){
 25264     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
 25265     lock.l_start = PENDING_BYTE;
 25266     if( unixFileLock(pFile, &lock) ){
 25267       tErrno = errno;
 25268       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 25269       if( rc!=SQLITE_BUSY ){
 25270         pFile->lastErrno = tErrno;
 25272       goto end_lock;
 25277   /* If control gets to this point, then actually go ahead and make
 25278   ** operating system calls for the specified lock.
 25279   */
 25280   if( eFileLock==SHARED_LOCK ){
 25281     assert( pInode->nShared==0 );
 25282     assert( pInode->eFileLock==0 );
 25283     assert( rc==SQLITE_OK );
 25285     /* Now get the read-lock */
 25286     lock.l_start = SHARED_FIRST;
 25287     lock.l_len = SHARED_SIZE;
 25288     if( unixFileLock(pFile, &lock) ){
 25289       tErrno = errno;
 25290       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 25293     /* Drop the temporary PENDING lock */
 25294     lock.l_start = PENDING_BYTE;
 25295     lock.l_len = 1L;
 25296     lock.l_type = F_UNLCK;
 25297     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
 25298       /* This could happen with a network mount */
 25299       tErrno = errno;
 25300       rc = SQLITE_IOERR_UNLOCK; 
 25303     if( rc ){
 25304       if( rc!=SQLITE_BUSY ){
 25305         pFile->lastErrno = tErrno;
 25307       goto end_lock;
 25308     }else{
 25309       pFile->eFileLock = SHARED_LOCK;
 25310       pInode->nLock++;
 25311       pInode->nShared = 1;
 25313   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 25314     /* We are trying for an exclusive lock but another thread in this
 25315     ** same process is still holding a shared lock. */
 25316     rc = SQLITE_BUSY;
 25317   }else{
 25318     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 25319     ** assumed that there is a SHARED or greater lock on the file
 25320     ** already.
 25321     */
 25322     assert( 0!=pFile->eFileLock );
 25323     lock.l_type = F_WRLCK;
 25325     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
 25326     if( eFileLock==RESERVED_LOCK ){
 25327       lock.l_start = RESERVED_BYTE;
 25328       lock.l_len = 1L;
 25329     }else{
 25330       lock.l_start = SHARED_FIRST;
 25331       lock.l_len = SHARED_SIZE;
 25334     if( unixFileLock(pFile, &lock) ){
 25335       tErrno = errno;
 25336       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 25337       if( rc!=SQLITE_BUSY ){
 25338         pFile->lastErrno = tErrno;
 25344 #ifdef SQLITE_DEBUG
 25345   /* Set up the transaction-counter change checking flags when
 25346   ** transitioning from a SHARED to a RESERVED lock.  The change
 25347   ** from SHARED to RESERVED marks the beginning of a normal
 25348   ** write operation (not a hot journal rollback).
 25349   */
 25350   if( rc==SQLITE_OK
 25351    && pFile->eFileLock<=SHARED_LOCK
 25352    && eFileLock==RESERVED_LOCK
 25353   ){
 25354     pFile->transCntrChng = 0;
 25355     pFile->dbUpdate = 0;
 25356     pFile->inNormalWrite = 1;
 25358 #endif
 25361   if( rc==SQLITE_OK ){
 25362     pFile->eFileLock = eFileLock;
 25363     pInode->eFileLock = eFileLock;
 25364   }else if( eFileLock==EXCLUSIVE_LOCK ){
 25365     pFile->eFileLock = PENDING_LOCK;
 25366     pInode->eFileLock = PENDING_LOCK;
 25369 end_lock:
 25370   unixLeaveMutex();
 25371   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
 25372       rc==SQLITE_OK ? "ok" : "failed"));
 25373   return rc;
 25376 /*
 25377 ** Add the file descriptor used by file handle pFile to the corresponding
 25378 ** pUnused list.
 25379 */
 25380 static void setPendingFd(unixFile *pFile){
 25381   unixInodeInfo *pInode = pFile->pInode;
 25382   UnixUnusedFd *p = pFile->pUnused;
 25383   p->pNext = pInode->pUnused;
 25384   pInode->pUnused = p;
 25385   pFile->h = -1;
 25386   pFile->pUnused = 0;
 25389 /*
 25390 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25391 ** must be either NO_LOCK or SHARED_LOCK.
 25392 **
 25393 ** If the locking level of the file descriptor is already at or below
 25394 ** the requested locking level, this routine is a no-op.
 25395 ** 
 25396 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
 25397 ** the byte range is divided into 2 parts and the first part is unlocked then
 25398 ** set to a read lock, then the other part is simply unlocked.  This works 
 25399 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
 25400 ** remove the write lock on a region when a read lock is set.
 25401 */
 25402 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
 25403   unixFile *pFile = (unixFile*)id;
 25404   unixInodeInfo *pInode;
 25405   struct flock lock;
 25406   int rc = SQLITE_OK;
 25408   assert( pFile );
 25409   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
 25410       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 25411       getpid()));
 25413   assert( eFileLock<=SHARED_LOCK );
 25414   if( pFile->eFileLock<=eFileLock ){
 25415     return SQLITE_OK;
 25417   unixEnterMutex();
 25418   pInode = pFile->pInode;
 25419   assert( pInode->nShared!=0 );
 25420   if( pFile->eFileLock>SHARED_LOCK ){
 25421     assert( pInode->eFileLock==pFile->eFileLock );
 25423 #ifdef SQLITE_DEBUG
 25424     /* When reducing a lock such that other processes can start
 25425     ** reading the database file again, make sure that the
 25426     ** transaction counter was updated if any part of the database
 25427     ** file changed.  If the transaction counter is not updated,
 25428     ** other connections to the same file might not realize that
 25429     ** the file has changed and hence might not know to flush their
 25430     ** cache.  The use of a stale cache can lead to database corruption.
 25431     */
 25432     pFile->inNormalWrite = 0;
 25433 #endif
 25435     /* downgrading to a shared lock on NFS involves clearing the write lock
 25436     ** before establishing the readlock - to avoid a race condition we downgrade
 25437     ** the lock in 2 blocks, so that part of the range will be covered by a 
 25438     ** write lock until the rest is covered by a read lock:
 25439     **  1:   [WWWWW]
 25440     **  2:   [....W]
 25441     **  3:   [RRRRW]
 25442     **  4:   [RRRR.]
 25443     */
 25444     if( eFileLock==SHARED_LOCK ){
 25446 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
 25447       (void)handleNFSUnlock;
 25448       assert( handleNFSUnlock==0 );
 25449 #endif
 25450 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 25451       if( handleNFSUnlock ){
 25452         int tErrno;               /* Error code from system call errors */
 25453         off_t divSize = SHARED_SIZE - 1;
 25455         lock.l_type = F_UNLCK;
 25456         lock.l_whence = SEEK_SET;
 25457         lock.l_start = SHARED_FIRST;
 25458         lock.l_len = divSize;
 25459         if( unixFileLock(pFile, &lock)==(-1) ){
 25460           tErrno = errno;
 25461           rc = SQLITE_IOERR_UNLOCK;
 25462           if( IS_LOCK_ERROR(rc) ){
 25463             pFile->lastErrno = tErrno;
 25465           goto end_unlock;
 25467         lock.l_type = F_RDLCK;
 25468         lock.l_whence = SEEK_SET;
 25469         lock.l_start = SHARED_FIRST;
 25470         lock.l_len = divSize;
 25471         if( unixFileLock(pFile, &lock)==(-1) ){
 25472           tErrno = errno;
 25473           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
 25474           if( IS_LOCK_ERROR(rc) ){
 25475             pFile->lastErrno = tErrno;
 25477           goto end_unlock;
 25479         lock.l_type = F_UNLCK;
 25480         lock.l_whence = SEEK_SET;
 25481         lock.l_start = SHARED_FIRST+divSize;
 25482         lock.l_len = SHARED_SIZE-divSize;
 25483         if( unixFileLock(pFile, &lock)==(-1) ){
 25484           tErrno = errno;
 25485           rc = SQLITE_IOERR_UNLOCK;
 25486           if( IS_LOCK_ERROR(rc) ){
 25487             pFile->lastErrno = tErrno;
 25489           goto end_unlock;
 25491       }else
 25492 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 25494         lock.l_type = F_RDLCK;
 25495         lock.l_whence = SEEK_SET;
 25496         lock.l_start = SHARED_FIRST;
 25497         lock.l_len = SHARED_SIZE;
 25498         if( unixFileLock(pFile, &lock) ){
 25499           /* In theory, the call to unixFileLock() cannot fail because another
 25500           ** process is holding an incompatible lock. If it does, this 
 25501           ** indicates that the other process is not following the locking
 25502           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
 25503           ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
 25504           ** an assert to fail). */ 
 25505           rc = SQLITE_IOERR_RDLOCK;
 25506           pFile->lastErrno = errno;
 25507           goto end_unlock;
 25511     lock.l_type = F_UNLCK;
 25512     lock.l_whence = SEEK_SET;
 25513     lock.l_start = PENDING_BYTE;
 25514     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
 25515     if( unixFileLock(pFile, &lock)==0 ){
 25516       pInode->eFileLock = SHARED_LOCK;
 25517     }else{
 25518       rc = SQLITE_IOERR_UNLOCK;
 25519       pFile->lastErrno = errno;
 25520       goto end_unlock;
 25523   if( eFileLock==NO_LOCK ){
 25524     /* Decrement the shared lock counter.  Release the lock using an
 25525     ** OS call only when all threads in this same process have released
 25526     ** the lock.
 25527     */
 25528     pInode->nShared--;
 25529     if( pInode->nShared==0 ){
 25530       lock.l_type = F_UNLCK;
 25531       lock.l_whence = SEEK_SET;
 25532       lock.l_start = lock.l_len = 0L;
 25533       if( unixFileLock(pFile, &lock)==0 ){
 25534         pInode->eFileLock = NO_LOCK;
 25535       }else{
 25536         rc = SQLITE_IOERR_UNLOCK;
 25537         pFile->lastErrno = errno;
 25538         pInode->eFileLock = NO_LOCK;
 25539         pFile->eFileLock = NO_LOCK;
 25543     /* Decrement the count of locks against this same file.  When the
 25544     ** count reaches zero, close any other file descriptors whose close
 25545     ** was deferred because of outstanding locks.
 25546     */
 25547     pInode->nLock--;
 25548     assert( pInode->nLock>=0 );
 25549     if( pInode->nLock==0 ){
 25550       closePendingFds(pFile);
 25554 end_unlock:
 25555   unixLeaveMutex();
 25556   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 25557   return rc;
 25560 /*
 25561 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25562 ** must be either NO_LOCK or SHARED_LOCK.
 25563 **
 25564 ** If the locking level of the file descriptor is already at or below
 25565 ** the requested locking level, this routine is a no-op.
 25566 */
 25567 static int unixUnlock(sqlite3_file *id, int eFileLock){
 25568 #if SQLITE_MAX_MMAP_SIZE>0
 25569   assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
 25570 #endif
 25571   return posixUnlock(id, eFileLock, 0);
 25574 #if SQLITE_MAX_MMAP_SIZE>0
 25575 static int unixMapfile(unixFile *pFd, i64 nByte);
 25576 static void unixUnmapfile(unixFile *pFd);
 25577 #endif
 25579 /*
 25580 ** This function performs the parts of the "close file" operation 
 25581 ** common to all locking schemes. It closes the directory and file
 25582 ** handles, if they are valid, and sets all fields of the unixFile
 25583 ** structure to 0.
 25584 **
 25585 ** It is *not* necessary to hold the mutex when this routine is called,
 25586 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
 25587 ** vxworksReleaseFileId() routine.
 25588 */
 25589 static int closeUnixFile(sqlite3_file *id){
 25590   unixFile *pFile = (unixFile*)id;
 25591 #if SQLITE_MAX_MMAP_SIZE>0
 25592   unixUnmapfile(pFile);
 25593 #endif
 25594   if( pFile->h>=0 ){
 25595     robust_close(pFile, pFile->h, __LINE__);
 25596     pFile->h = -1;
 25598 #if OS_VXWORKS
 25599   if( pFile->pId ){
 25600     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
 25601       osUnlink(pFile->pId->zCanonicalName);
 25603     vxworksReleaseFileId(pFile->pId);
 25604     pFile->pId = 0;
 25606 #endif
 25607   OSTRACE(("CLOSE   %-3d\n", pFile->h));
 25608   OpenCounter(-1);
 25609   sqlite3_free(pFile->pUnused);
 25610   memset(pFile, 0, sizeof(unixFile));
 25611   return SQLITE_OK;
 25614 /*
 25615 ** Close a file.
 25616 */
 25617 static int unixClose(sqlite3_file *id){
 25618   int rc = SQLITE_OK;
 25619   unixFile *pFile = (unixFile *)id;
 25620   verifyDbFile(pFile);
 25621   unixUnlock(id, NO_LOCK);
 25622   unixEnterMutex();
 25624   /* unixFile.pInode is always valid here. Otherwise, a different close
 25625   ** routine (e.g. nolockClose()) would be called instead.
 25626   */
 25627   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
 25628   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
 25629     /* If there are outstanding locks, do not actually close the file just
 25630     ** yet because that would clear those locks.  Instead, add the file
 25631     ** descriptor to pInode->pUnused list.  It will be automatically closed 
 25632     ** when the last lock is cleared.
 25633     */
 25634     setPendingFd(pFile);
 25636   releaseInodeInfo(pFile);
 25637   rc = closeUnixFile(id);
 25638   unixLeaveMutex();
 25639   return rc;
 25642 /************** End of the posix advisory lock implementation *****************
 25643 ******************************************************************************/
 25645 /******************************************************************************
 25646 ****************************** No-op Locking **********************************
 25647 **
 25648 ** Of the various locking implementations available, this is by far the
 25649 ** simplest:  locking is ignored.  No attempt is made to lock the database
 25650 ** file for reading or writing.
 25651 **
 25652 ** This locking mode is appropriate for use on read-only databases
 25653 ** (ex: databases that are burned into CD-ROM, for example.)  It can
 25654 ** also be used if the application employs some external mechanism to
 25655 ** prevent simultaneous access of the same database by two or more
 25656 ** database connections.  But there is a serious risk of database
 25657 ** corruption if this locking mode is used in situations where multiple
 25658 ** database connections are accessing the same database file at the same
 25659 ** time and one or more of those connections are writing.
 25660 */
 25662 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
 25663   UNUSED_PARAMETER(NotUsed);
 25664   *pResOut = 0;
 25665   return SQLITE_OK;
 25667 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
 25668   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 25669   return SQLITE_OK;
 25671 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
 25672   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 25673   return SQLITE_OK;
 25676 /*
 25677 ** Close the file.
 25678 */
 25679 static int nolockClose(sqlite3_file *id) {
 25680   return closeUnixFile(id);
 25683 /******************* End of the no-op lock implementation *********************
 25684 ******************************************************************************/
 25686 /******************************************************************************
 25687 ************************* Begin dot-file Locking ******************************
 25688 **
 25689 ** The dotfile locking implementation uses the existence of separate lock
 25690 ** files (really a directory) to control access to the database.  This works
 25691 ** on just about every filesystem imaginable.  But there are serious downsides:
 25692 **
 25693 **    (1)  There is zero concurrency.  A single reader blocks all other
 25694 **         connections from reading or writing the database.
 25695 **
 25696 **    (2)  An application crash or power loss can leave stale lock files
 25697 **         sitting around that need to be cleared manually.
 25698 **
 25699 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
 25700 ** other locking strategy is available.
 25701 **
 25702 ** Dotfile locking works by creating a subdirectory in the same directory as
 25703 ** the database and with the same name but with a ".lock" extension added.
 25704 ** The existence of a lock directory implies an EXCLUSIVE lock.  All other
 25705 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
 25706 */
 25708 /*
 25709 ** The file suffix added to the data base filename in order to create the
 25710 ** lock directory.
 25711 */
 25712 #define DOTLOCK_SUFFIX ".lock"
 25714 /*
 25715 ** This routine checks if there is a RESERVED lock held on the specified
 25716 ** file by this or any other process. If such a lock is held, set *pResOut
 25717 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25718 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25719 **
 25720 ** In dotfile locking, either a lock exists or it does not.  So in this
 25721 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
 25722 ** is held on the file and false if the file is unlocked.
 25723 */
 25724 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
 25725   int rc = SQLITE_OK;
 25726   int reserved = 0;
 25727   unixFile *pFile = (unixFile*)id;
 25729   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25731   assert( pFile );
 25733   /* Check if a thread in this process holds such a lock */
 25734   if( pFile->eFileLock>SHARED_LOCK ){
 25735     /* Either this connection or some other connection in the same process
 25736     ** holds a lock on the file.  No need to check further. */
 25737     reserved = 1;
 25738   }else{
 25739     /* The lock is held if and only if the lockfile exists */
 25740     const char *zLockFile = (const char*)pFile->lockingContext;
 25741     reserved = osAccess(zLockFile, 0)==0;
 25743   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
 25744   *pResOut = reserved;
 25745   return rc;
 25748 /*
 25749 ** Lock the file with the lock specified by parameter eFileLock - one
 25750 ** of the following:
 25751 **
 25752 **     (1) SHARED_LOCK
 25753 **     (2) RESERVED_LOCK
 25754 **     (3) PENDING_LOCK
 25755 **     (4) EXCLUSIVE_LOCK
 25756 **
 25757 ** Sometimes when requesting one lock state, additional lock states
 25758 ** are inserted in between.  The locking might fail on one of the later
 25759 ** transitions leaving the lock state different from what it started but
 25760 ** still short of its goal.  The following chart shows the allowed
 25761 ** transitions and the inserted intermediate states:
 25762 **
 25763 **    UNLOCKED -> SHARED
 25764 **    SHARED -> RESERVED
 25765 **    SHARED -> (PENDING) -> EXCLUSIVE
 25766 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25767 **    PENDING -> EXCLUSIVE
 25768 **
 25769 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 25770 ** routine to lower a locking level.
 25771 **
 25772 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
 25773 ** But we track the other locking levels internally.
 25774 */
 25775 static int dotlockLock(sqlite3_file *id, int eFileLock) {
 25776   unixFile *pFile = (unixFile*)id;
 25777   char *zLockFile = (char *)pFile->lockingContext;
 25778   int rc = SQLITE_OK;
 25781   /* If we have any lock, then the lock file already exists.  All we have
 25782   ** to do is adjust our internal record of the lock level.
 25783   */
 25784   if( pFile->eFileLock > NO_LOCK ){
 25785     pFile->eFileLock = eFileLock;
 25786     /* Always update the timestamp on the old file */
 25787 #ifdef HAVE_UTIME
 25788     utime(zLockFile, NULL);
 25789 #else
 25790     utimes(zLockFile, NULL);
 25791 #endif
 25792     return SQLITE_OK;
 25795   /* grab an exclusive lock */
 25796   rc = osMkdir(zLockFile, 0777);
 25797   if( rc<0 ){
 25798     /* failed to open/create the lock directory */
 25799     int tErrno = errno;
 25800     if( EEXIST == tErrno ){
 25801       rc = SQLITE_BUSY;
 25802     } else {
 25803       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 25804       if( IS_LOCK_ERROR(rc) ){
 25805         pFile->lastErrno = tErrno;
 25808     return rc;
 25811   /* got it, set the type and return ok */
 25812   pFile->eFileLock = eFileLock;
 25813   return rc;
 25816 /*
 25817 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25818 ** must be either NO_LOCK or SHARED_LOCK.
 25819 **
 25820 ** If the locking level of the file descriptor is already at or below
 25821 ** the requested locking level, this routine is a no-op.
 25822 **
 25823 ** When the locking level reaches NO_LOCK, delete the lock file.
 25824 */
 25825 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
 25826   unixFile *pFile = (unixFile*)id;
 25827   char *zLockFile = (char *)pFile->lockingContext;
 25828   int rc;
 25830   assert( pFile );
 25831   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
 25832            pFile->eFileLock, getpid()));
 25833   assert( eFileLock<=SHARED_LOCK );
 25835   /* no-op if possible */
 25836   if( pFile->eFileLock==eFileLock ){
 25837     return SQLITE_OK;
 25840   /* To downgrade to shared, simply update our internal notion of the
 25841   ** lock state.  No need to mess with the file on disk.
 25842   */
 25843   if( eFileLock==SHARED_LOCK ){
 25844     pFile->eFileLock = SHARED_LOCK;
 25845     return SQLITE_OK;
 25848   /* To fully unlock the database, delete the lock file */
 25849   assert( eFileLock==NO_LOCK );
 25850   rc = osRmdir(zLockFile);
 25851   if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
 25852   if( rc<0 ){
 25853     int tErrno = errno;
 25854     rc = 0;
 25855     if( ENOENT != tErrno ){
 25856       rc = SQLITE_IOERR_UNLOCK;
 25858     if( IS_LOCK_ERROR(rc) ){
 25859       pFile->lastErrno = tErrno;
 25861     return rc; 
 25863   pFile->eFileLock = NO_LOCK;
 25864   return SQLITE_OK;
 25867 /*
 25868 ** Close a file.  Make sure the lock has been released before closing.
 25869 */
 25870 static int dotlockClose(sqlite3_file *id) {
 25871   int rc = SQLITE_OK;
 25872   if( id ){
 25873     unixFile *pFile = (unixFile*)id;
 25874     dotlockUnlock(id, NO_LOCK);
 25875     sqlite3_free(pFile->lockingContext);
 25876     rc = closeUnixFile(id);
 25878   return rc;
 25880 /****************** End of the dot-file lock implementation *******************
 25881 ******************************************************************************/
 25883 /******************************************************************************
 25884 ************************** Begin flock Locking ********************************
 25885 **
 25886 ** Use the flock() system call to do file locking.
 25887 **
 25888 ** flock() locking is like dot-file locking in that the various
 25889 ** fine-grain locking levels supported by SQLite are collapsed into
 25890 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
 25891 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
 25892 ** still works when you do this, but concurrency is reduced since
 25893 ** only a single process can be reading the database at a time.
 25894 **
 25895 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
 25896 ** compiling for VXWORKS.
 25897 */
 25898 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 25900 /*
 25901 ** Retry flock() calls that fail with EINTR
 25902 */
 25903 #ifdef EINTR
 25904 static int robust_flock(int fd, int op){
 25905   int rc;
 25906   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
 25907   return rc;
 25909 #else
 25910 # define robust_flock(a,b) flock(a,b)
 25911 #endif
 25914 /*
 25915 ** This routine checks if there is a RESERVED lock held on the specified
 25916 ** file by this or any other process. If such a lock is held, set *pResOut
 25917 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 25918 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 25919 */
 25920 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
 25921   int rc = SQLITE_OK;
 25922   int reserved = 0;
 25923   unixFile *pFile = (unixFile*)id;
 25925   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 25927   assert( pFile );
 25929   /* Check if a thread in this process holds such a lock */
 25930   if( pFile->eFileLock>SHARED_LOCK ){
 25931     reserved = 1;
 25934   /* Otherwise see if some other process holds it. */
 25935   if( !reserved ){
 25936     /* attempt to get the lock */
 25937     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
 25938     if( !lrc ){
 25939       /* got the lock, unlock it */
 25940       lrc = robust_flock(pFile->h, LOCK_UN);
 25941       if ( lrc ) {
 25942         int tErrno = errno;
 25943         /* unlock failed with an error */
 25944         lrc = SQLITE_IOERR_UNLOCK; 
 25945         if( IS_LOCK_ERROR(lrc) ){
 25946           pFile->lastErrno = tErrno;
 25947           rc = lrc;
 25950     } else {
 25951       int tErrno = errno;
 25952       reserved = 1;
 25953       /* someone else might have it reserved */
 25954       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
 25955       if( IS_LOCK_ERROR(lrc) ){
 25956         pFile->lastErrno = tErrno;
 25957         rc = lrc;
 25961   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
 25963 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 25964   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 25965     rc = SQLITE_OK;
 25966     reserved=1;
 25968 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 25969   *pResOut = reserved;
 25970   return rc;
 25973 /*
 25974 ** Lock the file with the lock specified by parameter eFileLock - one
 25975 ** of the following:
 25976 **
 25977 **     (1) SHARED_LOCK
 25978 **     (2) RESERVED_LOCK
 25979 **     (3) PENDING_LOCK
 25980 **     (4) EXCLUSIVE_LOCK
 25981 **
 25982 ** Sometimes when requesting one lock state, additional lock states
 25983 ** are inserted in between.  The locking might fail on one of the later
 25984 ** transitions leaving the lock state different from what it started but
 25985 ** still short of its goal.  The following chart shows the allowed
 25986 ** transitions and the inserted intermediate states:
 25987 **
 25988 **    UNLOCKED -> SHARED
 25989 **    SHARED -> RESERVED
 25990 **    SHARED -> (PENDING) -> EXCLUSIVE
 25991 **    RESERVED -> (PENDING) -> EXCLUSIVE
 25992 **    PENDING -> EXCLUSIVE
 25993 **
 25994 ** flock() only really support EXCLUSIVE locks.  We track intermediate
 25995 ** lock states in the sqlite3_file structure, but all locks SHARED or
 25996 ** above are really EXCLUSIVE locks and exclude all other processes from
 25997 ** access the file.
 25998 **
 25999 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 26000 ** routine to lower a locking level.
 26001 */
 26002 static int flockLock(sqlite3_file *id, int eFileLock) {
 26003   int rc = SQLITE_OK;
 26004   unixFile *pFile = (unixFile*)id;
 26006   assert( pFile );
 26008   /* if we already have a lock, it is exclusive.  
 26009   ** Just adjust level and punt on outta here. */
 26010   if (pFile->eFileLock > NO_LOCK) {
 26011     pFile->eFileLock = eFileLock;
 26012     return SQLITE_OK;
 26015   /* grab an exclusive lock */
 26017   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
 26018     int tErrno = errno;
 26019     /* didn't get, must be busy */
 26020     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 26021     if( IS_LOCK_ERROR(rc) ){
 26022       pFile->lastErrno = tErrno;
 26024   } else {
 26025     /* got it, set the type and return ok */
 26026     pFile->eFileLock = eFileLock;
 26028   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
 26029            rc==SQLITE_OK ? "ok" : "failed"));
 26030 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 26031   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
 26032     rc = SQLITE_BUSY;
 26034 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 26035   return rc;
 26039 /*
 26040 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 26041 ** must be either NO_LOCK or SHARED_LOCK.
 26042 **
 26043 ** If the locking level of the file descriptor is already at or below
 26044 ** the requested locking level, this routine is a no-op.
 26045 */
 26046 static int flockUnlock(sqlite3_file *id, int eFileLock) {
 26047   unixFile *pFile = (unixFile*)id;
 26049   assert( pFile );
 26050   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
 26051            pFile->eFileLock, getpid()));
 26052   assert( eFileLock<=SHARED_LOCK );
 26054   /* no-op if possible */
 26055   if( pFile->eFileLock==eFileLock ){
 26056     return SQLITE_OK;
 26059   /* shared can just be set because we always have an exclusive */
 26060   if (eFileLock==SHARED_LOCK) {
 26061     pFile->eFileLock = eFileLock;
 26062     return SQLITE_OK;
 26065   /* no, really, unlock. */
 26066   if( robust_flock(pFile->h, LOCK_UN) ){
 26067 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 26068     return SQLITE_OK;
 26069 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 26070     return SQLITE_IOERR_UNLOCK;
 26071   }else{
 26072     pFile->eFileLock = NO_LOCK;
 26073     return SQLITE_OK;
 26077 /*
 26078 ** Close a file.
 26079 */
 26080 static int flockClose(sqlite3_file *id) {
 26081   int rc = SQLITE_OK;
 26082   if( id ){
 26083     flockUnlock(id, NO_LOCK);
 26084     rc = closeUnixFile(id);
 26086   return rc;
 26089 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
 26091 /******************* End of the flock lock implementation *********************
 26092 ******************************************************************************/
 26094 /******************************************************************************
 26095 ************************ Begin Named Semaphore Locking ************************
 26096 **
 26097 ** Named semaphore locking is only supported on VxWorks.
 26098 **
 26099 ** Semaphore locking is like dot-lock and flock in that it really only
 26100 ** supports EXCLUSIVE locking.  Only a single process can read or write
 26101 ** the database file at a time.  This reduces potential concurrency, but
 26102 ** makes the lock implementation much easier.
 26103 */
 26104 #if OS_VXWORKS
 26106 /*
 26107 ** This routine checks if there is a RESERVED lock held on the specified
 26108 ** file by this or any other process. If such a lock is held, set *pResOut
 26109 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 26110 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 26111 */
 26112 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
 26113   int rc = SQLITE_OK;
 26114   int reserved = 0;
 26115   unixFile *pFile = (unixFile*)id;
 26117   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 26119   assert( pFile );
 26121   /* Check if a thread in this process holds such a lock */
 26122   if( pFile->eFileLock>SHARED_LOCK ){
 26123     reserved = 1;
 26126   /* Otherwise see if some other process holds it. */
 26127   if( !reserved ){
 26128     sem_t *pSem = pFile->pInode->pSem;
 26129     struct stat statBuf;
 26131     if( sem_trywait(pSem)==-1 ){
 26132       int tErrno = errno;
 26133       if( EAGAIN != tErrno ){
 26134         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
 26135         pFile->lastErrno = tErrno;
 26136       } else {
 26137         /* someone else has the lock when we are in NO_LOCK */
 26138         reserved = (pFile->eFileLock < SHARED_LOCK);
 26140     }else{
 26141       /* we could have it if we want it */
 26142       sem_post(pSem);
 26145   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
 26147   *pResOut = reserved;
 26148   return rc;
 26151 /*
 26152 ** Lock the file with the lock specified by parameter eFileLock - one
 26153 ** of the following:
 26154 **
 26155 **     (1) SHARED_LOCK
 26156 **     (2) RESERVED_LOCK
 26157 **     (3) PENDING_LOCK
 26158 **     (4) EXCLUSIVE_LOCK
 26159 **
 26160 ** Sometimes when requesting one lock state, additional lock states
 26161 ** are inserted in between.  The locking might fail on one of the later
 26162 ** transitions leaving the lock state different from what it started but
 26163 ** still short of its goal.  The following chart shows the allowed
 26164 ** transitions and the inserted intermediate states:
 26165 **
 26166 **    UNLOCKED -> SHARED
 26167 **    SHARED -> RESERVED
 26168 **    SHARED -> (PENDING) -> EXCLUSIVE
 26169 **    RESERVED -> (PENDING) -> EXCLUSIVE
 26170 **    PENDING -> EXCLUSIVE
 26171 **
 26172 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
 26173 ** lock states in the sqlite3_file structure, but all locks SHARED or
 26174 ** above are really EXCLUSIVE locks and exclude all other processes from
 26175 ** access the file.
 26176 **
 26177 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 26178 ** routine to lower a locking level.
 26179 */
 26180 static int semLock(sqlite3_file *id, int eFileLock) {
 26181   unixFile *pFile = (unixFile*)id;
 26182   int fd;
 26183   sem_t *pSem = pFile->pInode->pSem;
 26184   int rc = SQLITE_OK;
 26186   /* if we already have a lock, it is exclusive.  
 26187   ** Just adjust level and punt on outta here. */
 26188   if (pFile->eFileLock > NO_LOCK) {
 26189     pFile->eFileLock = eFileLock;
 26190     rc = SQLITE_OK;
 26191     goto sem_end_lock;
 26194   /* lock semaphore now but bail out when already locked. */
 26195   if( sem_trywait(pSem)==-1 ){
 26196     rc = SQLITE_BUSY;
 26197     goto sem_end_lock;
 26200   /* got it, set the type and return ok */
 26201   pFile->eFileLock = eFileLock;
 26203  sem_end_lock:
 26204   return rc;
 26207 /*
 26208 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 26209 ** must be either NO_LOCK or SHARED_LOCK.
 26210 **
 26211 ** If the locking level of the file descriptor is already at or below
 26212 ** the requested locking level, this routine is a no-op.
 26213 */
 26214 static int semUnlock(sqlite3_file *id, int eFileLock) {
 26215   unixFile *pFile = (unixFile*)id;
 26216   sem_t *pSem = pFile->pInode->pSem;
 26218   assert( pFile );
 26219   assert( pSem );
 26220   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
 26221            pFile->eFileLock, getpid()));
 26222   assert( eFileLock<=SHARED_LOCK );
 26224   /* no-op if possible */
 26225   if( pFile->eFileLock==eFileLock ){
 26226     return SQLITE_OK;
 26229   /* shared can just be set because we always have an exclusive */
 26230   if (eFileLock==SHARED_LOCK) {
 26231     pFile->eFileLock = eFileLock;
 26232     return SQLITE_OK;
 26235   /* no, really unlock. */
 26236   if ( sem_post(pSem)==-1 ) {
 26237     int rc, tErrno = errno;
 26238     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 26239     if( IS_LOCK_ERROR(rc) ){
 26240       pFile->lastErrno = tErrno;
 26242     return rc; 
 26244   pFile->eFileLock = NO_LOCK;
 26245   return SQLITE_OK;
 26248 /*
 26249  ** Close a file.
 26250  */
 26251 static int semClose(sqlite3_file *id) {
 26252   if( id ){
 26253     unixFile *pFile = (unixFile*)id;
 26254     semUnlock(id, NO_LOCK);
 26255     assert( pFile );
 26256     unixEnterMutex();
 26257     releaseInodeInfo(pFile);
 26258     unixLeaveMutex();
 26259     closeUnixFile(id);
 26261   return SQLITE_OK;
 26264 #endif /* OS_VXWORKS */
 26265 /*
 26266 ** Named semaphore locking is only available on VxWorks.
 26267 **
 26268 *************** End of the named semaphore lock implementation ****************
 26269 ******************************************************************************/
 26272 /******************************************************************************
 26273 *************************** Begin AFP Locking *********************************
 26274 **
 26275 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
 26276 ** on Apple Macintosh computers - both OS9 and OSX.
 26277 **
 26278 ** Third-party implementations of AFP are available.  But this code here
 26279 ** only works on OSX.
 26280 */
 26282 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 26283 /*
 26284 ** The afpLockingContext structure contains all afp lock specific state
 26285 */
 26286 typedef struct afpLockingContext afpLockingContext;
 26287 struct afpLockingContext {
 26288   int reserved;
 26289   const char *dbPath;             /* Name of the open file */
 26290 };
 26292 struct ByteRangeLockPB2
 26294   unsigned long long offset;        /* offset to first byte to lock */
 26295   unsigned long long length;        /* nbr of bytes to lock */
 26296   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
 26297   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
 26298   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
 26299   int fd;                           /* file desc to assoc this lock with */
 26300 };
 26302 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
 26304 /*
 26305 ** This is a utility for setting or clearing a bit-range lock on an
 26306 ** AFP filesystem.
 26307 ** 
 26308 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
 26309 */
 26310 static int afpSetLock(
 26311   const char *path,              /* Name of the file to be locked or unlocked */
 26312   unixFile *pFile,               /* Open file descriptor on path */
 26313   unsigned long long offset,     /* First byte to be locked */
 26314   unsigned long long length,     /* Number of bytes to lock */
 26315   int setLockFlag                /* True to set lock.  False to clear lock */
 26316 ){
 26317   struct ByteRangeLockPB2 pb;
 26318   int err;
 26320   pb.unLockFlag = setLockFlag ? 0 : 1;
 26321   pb.startEndFlag = 0;
 26322   pb.offset = offset;
 26323   pb.length = length; 
 26324   pb.fd = pFile->h;
 26326   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
 26327     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
 26328     offset, length));
 26329   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
 26330   if ( err==-1 ) {
 26331     int rc;
 26332     int tErrno = errno;
 26333     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
 26334              path, tErrno, strerror(tErrno)));
 26335 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
 26336     rc = SQLITE_BUSY;
 26337 #else
 26338     rc = sqliteErrorFromPosixError(tErrno,
 26339                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
 26340 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
 26341     if( IS_LOCK_ERROR(rc) ){
 26342       pFile->lastErrno = tErrno;
 26344     return rc;
 26345   } else {
 26346     return SQLITE_OK;
 26350 /*
 26351 ** This routine checks if there is a RESERVED lock held on the specified
 26352 ** file by this or any other process. If such a lock is held, set *pResOut
 26353 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 26354 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 26355 */
 26356 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
 26357   int rc = SQLITE_OK;
 26358   int reserved = 0;
 26359   unixFile *pFile = (unixFile*)id;
 26360   afpLockingContext *context;
 26362   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 26364   assert( pFile );
 26365   context = (afpLockingContext *) pFile->lockingContext;
 26366   if( context->reserved ){
 26367     *pResOut = 1;
 26368     return SQLITE_OK;
 26370   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 26372   /* Check if a thread in this process holds such a lock */
 26373   if( pFile->pInode->eFileLock>SHARED_LOCK ){
 26374     reserved = 1;
 26377   /* Otherwise see if some other process holds it.
 26378    */
 26379   if( !reserved ){
 26380     /* lock the RESERVED byte */
 26381     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
 26382     if( SQLITE_OK==lrc ){
 26383       /* if we succeeded in taking the reserved lock, unlock it to restore
 26384       ** the original state */
 26385       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 26386     } else {
 26387       /* if we failed to get the lock then someone else must have it */
 26388       reserved = 1;
 26390     if( IS_LOCK_ERROR(lrc) ){
 26391       rc=lrc;
 26395   unixLeaveMutex();
 26396   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
 26398   *pResOut = reserved;
 26399   return rc;
 26402 /*
 26403 ** Lock the file with the lock specified by parameter eFileLock - one
 26404 ** of the following:
 26405 **
 26406 **     (1) SHARED_LOCK
 26407 **     (2) RESERVED_LOCK
 26408 **     (3) PENDING_LOCK
 26409 **     (4) EXCLUSIVE_LOCK
 26410 **
 26411 ** Sometimes when requesting one lock state, additional lock states
 26412 ** are inserted in between.  The locking might fail on one of the later
 26413 ** transitions leaving the lock state different from what it started but
 26414 ** still short of its goal.  The following chart shows the allowed
 26415 ** transitions and the inserted intermediate states:
 26416 **
 26417 **    UNLOCKED -> SHARED
 26418 **    SHARED -> RESERVED
 26419 **    SHARED -> (PENDING) -> EXCLUSIVE
 26420 **    RESERVED -> (PENDING) -> EXCLUSIVE
 26421 **    PENDING -> EXCLUSIVE
 26422 **
 26423 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 26424 ** routine to lower a locking level.
 26425 */
 26426 static int afpLock(sqlite3_file *id, int eFileLock){
 26427   int rc = SQLITE_OK;
 26428   unixFile *pFile = (unixFile*)id;
 26429   unixInodeInfo *pInode = pFile->pInode;
 26430   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 26432   assert( pFile );
 26433   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
 26434            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 26435            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
 26437   /* If there is already a lock of this type or more restrictive on the
 26438   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
 26439   ** unixEnterMutex() hasn't been called yet.
 26440   */
 26441   if( pFile->eFileLock>=eFileLock ){
 26442     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
 26443            azFileLock(eFileLock)));
 26444     return SQLITE_OK;
 26447   /* Make sure the locking sequence is correct
 26448   **  (1) We never move from unlocked to anything higher than shared lock.
 26449   **  (2) SQLite never explicitly requests a pendig lock.
 26450   **  (3) A shared lock is always held when a reserve lock is requested.
 26451   */
 26452   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
 26453   assert( eFileLock!=PENDING_LOCK );
 26454   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
 26456   /* This mutex is needed because pFile->pInode is shared across threads
 26457   */
 26458   unixEnterMutex();
 26459   pInode = pFile->pInode;
 26461   /* If some thread using this PID has a lock via a different unixFile*
 26462   ** handle that precludes the requested lock, return BUSY.
 26463   */
 26464   if( (pFile->eFileLock!=pInode->eFileLock && 
 26465        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
 26466      ){
 26467     rc = SQLITE_BUSY;
 26468     goto afp_end_lock;
 26471   /* If a SHARED lock is requested, and some thread using this PID already
 26472   ** has a SHARED or RESERVED lock, then increment reference counts and
 26473   ** return SQLITE_OK.
 26474   */
 26475   if( eFileLock==SHARED_LOCK && 
 26476      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
 26477     assert( eFileLock==SHARED_LOCK );
 26478     assert( pFile->eFileLock==0 );
 26479     assert( pInode->nShared>0 );
 26480     pFile->eFileLock = SHARED_LOCK;
 26481     pInode->nShared++;
 26482     pInode->nLock++;
 26483     goto afp_end_lock;
 26486   /* A PENDING lock is needed before acquiring a SHARED lock and before
 26487   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
 26488   ** be released.
 26489   */
 26490   if( eFileLock==SHARED_LOCK 
 26491       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 26492   ){
 26493     int failed;
 26494     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
 26495     if (failed) {
 26496       rc = failed;
 26497       goto afp_end_lock;
 26501   /* If control gets to this point, then actually go ahead and make
 26502   ** operating system calls for the specified lock.
 26503   */
 26504   if( eFileLock==SHARED_LOCK ){
 26505     int lrc1, lrc2, lrc1Errno = 0;
 26506     long lk, mask;
 26508     assert( pInode->nShared==0 );
 26509     assert( pInode->eFileLock==0 );
 26511     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
 26512     /* Now get the read-lock SHARED_LOCK */
 26513     /* note that the quality of the randomness doesn't matter that much */
 26514     lk = random(); 
 26515     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
 26516     lrc1 = afpSetLock(context->dbPath, pFile, 
 26517           SHARED_FIRST+pInode->sharedByte, 1, 1);
 26518     if( IS_LOCK_ERROR(lrc1) ){
 26519       lrc1Errno = pFile->lastErrno;
 26521     /* Drop the temporary PENDING lock */
 26522     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 26524     if( IS_LOCK_ERROR(lrc1) ) {
 26525       pFile->lastErrno = lrc1Errno;
 26526       rc = lrc1;
 26527       goto afp_end_lock;
 26528     } else if( IS_LOCK_ERROR(lrc2) ){
 26529       rc = lrc2;
 26530       goto afp_end_lock;
 26531     } else if( lrc1 != SQLITE_OK ) {
 26532       rc = lrc1;
 26533     } else {
 26534       pFile->eFileLock = SHARED_LOCK;
 26535       pInode->nLock++;
 26536       pInode->nShared = 1;
 26538   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 26539     /* We are trying for an exclusive lock but another thread in this
 26540      ** same process is still holding a shared lock. */
 26541     rc = SQLITE_BUSY;
 26542   }else{
 26543     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 26544     ** assumed that there is a SHARED or greater lock on the file
 26545     ** already.
 26546     */
 26547     int failed = 0;
 26548     assert( 0!=pFile->eFileLock );
 26549     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
 26550         /* Acquire a RESERVED lock */
 26551         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
 26552       if( !failed ){
 26553         context->reserved = 1;
 26556     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
 26557       /* Acquire an EXCLUSIVE lock */
 26559       /* Remove the shared lock before trying the range.  we'll need to 
 26560       ** reestablish the shared lock if we can't get the  afpUnlock
 26561       */
 26562       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
 26563                          pInode->sharedByte, 1, 0)) ){
 26564         int failed2 = SQLITE_OK;
 26565         /* now attemmpt to get the exclusive lock range */
 26566         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
 26567                                SHARED_SIZE, 1);
 26568         if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
 26569                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
 26570           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
 26571           ** a critical I/O error
 26572           */
 26573           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
 26574                SQLITE_IOERR_LOCK;
 26575           goto afp_end_lock;
 26577       }else{
 26578         rc = failed; 
 26581     if( failed ){
 26582       rc = failed;
 26586   if( rc==SQLITE_OK ){
 26587     pFile->eFileLock = eFileLock;
 26588     pInode->eFileLock = eFileLock;
 26589   }else if( eFileLock==EXCLUSIVE_LOCK ){
 26590     pFile->eFileLock = PENDING_LOCK;
 26591     pInode->eFileLock = PENDING_LOCK;
 26594 afp_end_lock:
 26595   unixLeaveMutex();
 26596   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
 26597          rc==SQLITE_OK ? "ok" : "failed"));
 26598   return rc;
 26601 /*
 26602 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 26603 ** must be either NO_LOCK or SHARED_LOCK.
 26604 **
 26605 ** If the locking level of the file descriptor is already at or below
 26606 ** the requested locking level, this routine is a no-op.
 26607 */
 26608 static int afpUnlock(sqlite3_file *id, int eFileLock) {
 26609   int rc = SQLITE_OK;
 26610   unixFile *pFile = (unixFile*)id;
 26611   unixInodeInfo *pInode;
 26612   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
 26613   int skipShared = 0;
 26614 #ifdef SQLITE_TEST
 26615   int h = pFile->h;
 26616 #endif
 26618   assert( pFile );
 26619   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
 26620            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 26621            getpid()));
 26623   assert( eFileLock<=SHARED_LOCK );
 26624   if( pFile->eFileLock<=eFileLock ){
 26625     return SQLITE_OK;
 26627   unixEnterMutex();
 26628   pInode = pFile->pInode;
 26629   assert( pInode->nShared!=0 );
 26630   if( pFile->eFileLock>SHARED_LOCK ){
 26631     assert( pInode->eFileLock==pFile->eFileLock );
 26632     SimulateIOErrorBenign(1);
 26633     SimulateIOError( h=(-1) )
 26634     SimulateIOErrorBenign(0);
 26636 #ifdef SQLITE_DEBUG
 26637     /* When reducing a lock such that other processes can start
 26638     ** reading the database file again, make sure that the
 26639     ** transaction counter was updated if any part of the database
 26640     ** file changed.  If the transaction counter is not updated,
 26641     ** other connections to the same file might not realize that
 26642     ** the file has changed and hence might not know to flush their
 26643     ** cache.  The use of a stale cache can lead to database corruption.
 26644     */
 26645     assert( pFile->inNormalWrite==0
 26646            || pFile->dbUpdate==0
 26647            || pFile->transCntrChng==1 );
 26648     pFile->inNormalWrite = 0;
 26649 #endif
 26651     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
 26652       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
 26653       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
 26654         /* only re-establish the shared lock if necessary */
 26655         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 26656         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
 26657       } else {
 26658         skipShared = 1;
 26661     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
 26662       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
 26664     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
 26665       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
 26666       if( !rc ){ 
 26667         context->reserved = 0; 
 26670     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
 26671       pInode->eFileLock = SHARED_LOCK;
 26674   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
 26676     /* Decrement the shared lock counter.  Release the lock using an
 26677     ** OS call only when all threads in this same process have released
 26678     ** the lock.
 26679     */
 26680     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
 26681     pInode->nShared--;
 26682     if( pInode->nShared==0 ){
 26683       SimulateIOErrorBenign(1);
 26684       SimulateIOError( h=(-1) )
 26685       SimulateIOErrorBenign(0);
 26686       if( !skipShared ){
 26687         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
 26689       if( !rc ){
 26690         pInode->eFileLock = NO_LOCK;
 26691         pFile->eFileLock = NO_LOCK;
 26694     if( rc==SQLITE_OK ){
 26695       pInode->nLock--;
 26696       assert( pInode->nLock>=0 );
 26697       if( pInode->nLock==0 ){
 26698         closePendingFds(pFile);
 26703   unixLeaveMutex();
 26704   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 26705   return rc;
 26708 /*
 26709 ** Close a file & cleanup AFP specific locking context 
 26710 */
 26711 static int afpClose(sqlite3_file *id) {
 26712   int rc = SQLITE_OK;
 26713   if( id ){
 26714     unixFile *pFile = (unixFile*)id;
 26715     afpUnlock(id, NO_LOCK);
 26716     unixEnterMutex();
 26717     if( pFile->pInode && pFile->pInode->nLock ){
 26718       /* If there are outstanding locks, do not actually close the file just
 26719       ** yet because that would clear those locks.  Instead, add the file
 26720       ** descriptor to pInode->aPending.  It will be automatically closed when
 26721       ** the last lock is cleared.
 26722       */
 26723       setPendingFd(pFile);
 26725     releaseInodeInfo(pFile);
 26726     sqlite3_free(pFile->lockingContext);
 26727     rc = closeUnixFile(id);
 26728     unixLeaveMutex();
 26730   return rc;
 26733 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 26734 /*
 26735 ** The code above is the AFP lock implementation.  The code is specific
 26736 ** to MacOSX and does not work on other unix platforms.  No alternative
 26737 ** is available.  If you don't compile for a mac, then the "unix-afp"
 26738 ** VFS is not available.
 26739 **
 26740 ********************* End of the AFP lock implementation **********************
 26741 ******************************************************************************/
 26743 /******************************************************************************
 26744 *************************** Begin NFS Locking ********************************/
 26746 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 26747 /*
 26748  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 26749  ** must be either NO_LOCK or SHARED_LOCK.
 26750  **
 26751  ** If the locking level of the file descriptor is already at or below
 26752  ** the requested locking level, this routine is a no-op.
 26753  */
 26754 static int nfsUnlock(sqlite3_file *id, int eFileLock){
 26755   return posixUnlock(id, eFileLock, 1);
 26758 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 26759 /*
 26760 ** The code above is the NFS lock implementation.  The code is specific
 26761 ** to MacOSX and does not work on other unix platforms.  No alternative
 26762 ** is available.  
 26763 **
 26764 ********************* End of the NFS lock implementation **********************
 26765 ******************************************************************************/
 26767 /******************************************************************************
 26768 **************** Non-locking sqlite3_file methods *****************************
 26769 **
 26770 ** The next division contains implementations for all methods of the 
 26771 ** sqlite3_file object other than the locking methods.  The locking
 26772 ** methods were defined in divisions above (one locking method per
 26773 ** division).  Those methods that are common to all locking modes
 26774 ** are gather together into this division.
 26775 */
 26777 /*
 26778 ** Seek to the offset passed as the second argument, then read cnt 
 26779 ** bytes into pBuf. Return the number of bytes actually read.
 26780 **
 26781 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
 26782 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
 26783 ** one system to another.  Since SQLite does not define USE_PREAD
 26784 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
 26785 ** See tickets #2741 and #2681.
 26786 **
 26787 ** To avoid stomping the errno value on a failed read the lastErrno value
 26788 ** is set before returning.
 26789 */
 26790 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
 26791   int got;
 26792   int prior = 0;
 26793 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 26794   i64 newOffset;
 26795 #endif
 26796   TIMER_START;
 26797   assert( cnt==(cnt&0x1ffff) );
 26798   assert( id->h>2 );
 26799   cnt &= 0x1ffff;
 26800   do{
 26801 #if defined(USE_PREAD)
 26802     got = osPread(id->h, pBuf, cnt, offset);
 26803     SimulateIOError( got = -1 );
 26804 #elif defined(USE_PREAD64)
 26805     got = osPread64(id->h, pBuf, cnt, offset);
 26806     SimulateIOError( got = -1 );
 26807 #else
 26808     newOffset = lseek(id->h, offset, SEEK_SET);
 26809     SimulateIOError( newOffset-- );
 26810     if( newOffset!=offset ){
 26811       if( newOffset == -1 ){
 26812         ((unixFile*)id)->lastErrno = errno;
 26813       }else{
 26814         ((unixFile*)id)->lastErrno = 0;
 26816       return -1;
 26818     got = osRead(id->h, pBuf, cnt);
 26819 #endif
 26820     if( got==cnt ) break;
 26821     if( got<0 ){
 26822       if( errno==EINTR ){ got = 1; continue; }
 26823       prior = 0;
 26824       ((unixFile*)id)->lastErrno = errno;
 26825       break;
 26826     }else if( got>0 ){
 26827       cnt -= got;
 26828       offset += got;
 26829       prior += got;
 26830       pBuf = (void*)(got + (char*)pBuf);
 26832   }while( got>0 );
 26833   TIMER_END;
 26834   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
 26835             id->h, got+prior, offset-prior, TIMER_ELAPSED));
 26836   return got+prior;
 26839 /*
 26840 ** Read data from a file into a buffer.  Return SQLITE_OK if all
 26841 ** bytes were read successfully and SQLITE_IOERR if anything goes
 26842 ** wrong.
 26843 */
 26844 static int unixRead(
 26845   sqlite3_file *id, 
 26846   void *pBuf, 
 26847   int amt,
 26848   sqlite3_int64 offset
 26849 ){
 26850   unixFile *pFile = (unixFile *)id;
 26851   int got;
 26852   assert( id );
 26853   assert( offset>=0 );
 26854   assert( amt>0 );
 26856   /* If this is a database file (not a journal, master-journal or temp
 26857   ** file), the bytes in the locking range should never be read or written. */
 26858 #if 0
 26859   assert( pFile->pUnused==0
 26860        || offset>=PENDING_BYTE+512
 26861        || offset+amt<=PENDING_BYTE 
 26862   );
 26863 #endif
 26865 #if SQLITE_MAX_MMAP_SIZE>0
 26866   /* Deal with as much of this read request as possible by transfering
 26867   ** data from the memory mapping using memcpy().  */
 26868   if( offset<pFile->mmapSize ){
 26869     if( offset+amt <= pFile->mmapSize ){
 26870       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
 26871       return SQLITE_OK;
 26872     }else{
 26873       int nCopy = pFile->mmapSize - offset;
 26874       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
 26875       pBuf = &((u8 *)pBuf)[nCopy];
 26876       amt -= nCopy;
 26877       offset += nCopy;
 26880 #endif
 26882   got = seekAndRead(pFile, offset, pBuf, amt);
 26883   if( got==amt ){
 26884     return SQLITE_OK;
 26885   }else if( got<0 ){
 26886     /* lastErrno set by seekAndRead */
 26887     return SQLITE_IOERR_READ;
 26888   }else{
 26889     pFile->lastErrno = 0; /* not a system error */
 26890     /* Unread parts of the buffer must be zero-filled */
 26891     memset(&((char*)pBuf)[got], 0, amt-got);
 26892     return SQLITE_IOERR_SHORT_READ;
 26896 /*
 26897 ** Attempt to seek the file-descriptor passed as the first argument to
 26898 ** absolute offset iOff, then attempt to write nBuf bytes of data from
 26899 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, 
 26900 ** return the actual number of bytes written (which may be less than
 26901 ** nBuf).
 26902 */
 26903 static int seekAndWriteFd(
 26904   int fd,                         /* File descriptor to write to */
 26905   i64 iOff,                       /* File offset to begin writing at */
 26906   const void *pBuf,               /* Copy data from this buffer to the file */
 26907   int nBuf,                       /* Size of buffer pBuf in bytes */
 26908   int *piErrno                    /* OUT: Error number if error occurs */
 26909 ){
 26910   int rc = 0;                     /* Value returned by system call */
 26912   assert( nBuf==(nBuf&0x1ffff) );
 26913   assert( fd>2 );
 26914   nBuf &= 0x1ffff;
 26915   TIMER_START;
 26917 #if defined(USE_PREAD)
 26918   do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
 26919 #elif defined(USE_PREAD64)
 26920   do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
 26921 #else
 26922   do{
 26923     i64 iSeek = lseek(fd, iOff, SEEK_SET);
 26924     SimulateIOError( iSeek-- );
 26926     if( iSeek!=iOff ){
 26927       if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
 26928       return -1;
 26930     rc = osWrite(fd, pBuf, nBuf);
 26931   }while( rc<0 && errno==EINTR );
 26932 #endif
 26934   TIMER_END;
 26935   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
 26937   if( rc<0 && piErrno ) *piErrno = errno;
 26938   return rc;
 26942 /*
 26943 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
 26944 ** Return the number of bytes actually read.  Update the offset.
 26945 **
 26946 ** To avoid stomping the errno value on a failed write the lastErrno value
 26947 ** is set before returning.
 26948 */
 26949 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
 26950   return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
 26954 /*
 26955 ** Write data from a buffer into a file.  Return SQLITE_OK on success
 26956 ** or some other error code on failure.
 26957 */
 26958 static int unixWrite(
 26959   sqlite3_file *id, 
 26960   const void *pBuf, 
 26961   int amt,
 26962   sqlite3_int64 offset 
 26963 ){
 26964   unixFile *pFile = (unixFile*)id;
 26965   int wrote = 0;
 26966   assert( id );
 26967   assert( amt>0 );
 26969   /* If this is a database file (not a journal, master-journal or temp
 26970   ** file), the bytes in the locking range should never be read or written. */
 26971 #if 0
 26972   assert( pFile->pUnused==0
 26973        || offset>=PENDING_BYTE+512
 26974        || offset+amt<=PENDING_BYTE 
 26975   );
 26976 #endif
 26978 #ifdef SQLITE_DEBUG
 26979   /* If we are doing a normal write to a database file (as opposed to
 26980   ** doing a hot-journal rollback or a write to some file other than a
 26981   ** normal database file) then record the fact that the database
 26982   ** has changed.  If the transaction counter is modified, record that
 26983   ** fact too.
 26984   */
 26985   if( pFile->inNormalWrite ){
 26986     pFile->dbUpdate = 1;  /* The database has been modified */
 26987     if( offset<=24 && offset+amt>=27 ){
 26988       int rc;
 26989       char oldCntr[4];
 26990       SimulateIOErrorBenign(1);
 26991       rc = seekAndRead(pFile, 24, oldCntr, 4);
 26992       SimulateIOErrorBenign(0);
 26993       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
 26994         pFile->transCntrChng = 1;  /* The transaction counter has changed */
 26998 #endif
 27000 #if SQLITE_MAX_MMAP_SIZE>0
 27001   /* Deal with as much of this write request as possible by transfering
 27002   ** data from the memory mapping using memcpy().  */
 27003   if( offset<pFile->mmapSize ){
 27004     if( offset+amt <= pFile->mmapSize ){
 27005       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
 27006       return SQLITE_OK;
 27007     }else{
 27008       int nCopy = pFile->mmapSize - offset;
 27009       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
 27010       pBuf = &((u8 *)pBuf)[nCopy];
 27011       amt -= nCopy;
 27012       offset += nCopy;
 27015 #endif
 27017   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
 27018     amt -= wrote;
 27019     offset += wrote;
 27020     pBuf = &((char*)pBuf)[wrote];
 27022   SimulateIOError(( wrote=(-1), amt=1 ));
 27023   SimulateDiskfullError(( wrote=0, amt=1 ));
 27025   if( amt>0 ){
 27026     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
 27027       /* lastErrno set by seekAndWrite */
 27028       return SQLITE_IOERR_WRITE;
 27029     }else{
 27030       pFile->lastErrno = 0; /* not a system error */
 27031       return SQLITE_FULL;
 27035   return SQLITE_OK;
 27038 #ifdef SQLITE_TEST
 27039 /*
 27040 ** Count the number of fullsyncs and normal syncs.  This is used to test
 27041 ** that syncs and fullsyncs are occurring at the right times.
 27042 */
 27043 SQLITE_API int sqlite3_sync_count = 0;
 27044 SQLITE_API int sqlite3_fullsync_count = 0;
 27045 #endif
 27047 /*
 27048 ** We do not trust systems to provide a working fdatasync().  Some do.
 27049 ** Others do no.  To be safe, we will stick with the (slightly slower)
 27050 ** fsync(). If you know that your system does support fdatasync() correctly,
 27051 ** then simply compile with -Dfdatasync=fdatasync
 27052 */
 27053 #if !defined(fdatasync)
 27054 # define fdatasync fsync
 27055 #endif
 27057 /*
 27058 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
 27059 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
 27060 ** only available on Mac OS X.  But that could change.
 27061 */
 27062 #ifdef F_FULLFSYNC
 27063 # define HAVE_FULLFSYNC 1
 27064 #else
 27065 # define HAVE_FULLFSYNC 0
 27066 #endif
 27069 /*
 27070 ** The fsync() system call does not work as advertised on many
 27071 ** unix systems.  The following procedure is an attempt to make
 27072 ** it work better.
 27073 **
 27074 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
 27075 ** for testing when we want to run through the test suite quickly.
 27076 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
 27077 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
 27078 ** or power failure will likely corrupt the database file.
 27079 **
 27080 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
 27081 ** The idea behind dataOnly is that it should only write the file content
 27082 ** to disk, not the inode.  We only set dataOnly if the file size is 
 27083 ** unchanged since the file size is part of the inode.  However, 
 27084 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
 27085 ** file size has changed.  The only real difference between fdatasync()
 27086 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
 27087 ** inode if the mtime or owner or other inode attributes have changed.
 27088 ** We only care about the file size, not the other file attributes, so
 27089 ** as far as SQLite is concerned, an fdatasync() is always adequate.
 27090 ** So, we always use fdatasync() if it is available, regardless of
 27091 ** the value of the dataOnly flag.
 27092 */
 27093 static int full_fsync(int fd, int fullSync, int dataOnly){
 27094   int rc;
 27096   /* The following "ifdef/elif/else/" block has the same structure as
 27097   ** the one below. It is replicated here solely to avoid cluttering 
 27098   ** up the real code with the UNUSED_PARAMETER() macros.
 27099   */
 27100 #ifdef SQLITE_NO_SYNC
 27101   UNUSED_PARAMETER(fd);
 27102   UNUSED_PARAMETER(fullSync);
 27103   UNUSED_PARAMETER(dataOnly);
 27104 #elif HAVE_FULLFSYNC
 27105   UNUSED_PARAMETER(dataOnly);
 27106 #else
 27107   UNUSED_PARAMETER(fullSync);
 27108   UNUSED_PARAMETER(dataOnly);
 27109 #endif
 27111   /* Record the number of times that we do a normal fsync() and 
 27112   ** FULLSYNC.  This is used during testing to verify that this procedure
 27113   ** gets called with the correct arguments.
 27114   */
 27115 #ifdef SQLITE_TEST
 27116   if( fullSync ) sqlite3_fullsync_count++;
 27117   sqlite3_sync_count++;
 27118 #endif
 27120   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 27121   ** no-op
 27122   */
 27123 #ifdef SQLITE_NO_SYNC
 27124   rc = SQLITE_OK;
 27125 #elif HAVE_FULLFSYNC
 27126   if( fullSync ){
 27127     rc = osFcntl(fd, F_FULLFSYNC, 0);
 27128   }else{
 27129     rc = 1;
 27131   /* If the FULLFSYNC failed, fall back to attempting an fsync().
 27132   ** It shouldn't be possible for fullfsync to fail on the local 
 27133   ** file system (on OSX), so failure indicates that FULLFSYNC
 27134   ** isn't supported for this file system. So, attempt an fsync 
 27135   ** and (for now) ignore the overhead of a superfluous fcntl call.  
 27136   ** It'd be better to detect fullfsync support once and avoid 
 27137   ** the fcntl call every time sync is called.
 27138   */
 27139   if( rc ) rc = fsync(fd);
 27141 #elif defined(__APPLE__)
 27142   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
 27143   ** so currently we default to the macro that redefines fdatasync to fsync
 27144   */
 27145   rc = fsync(fd);
 27146 #else 
 27147   rc = fdatasync(fd);
 27148 #if OS_VXWORKS
 27149   if( rc==-1 && errno==ENOTSUP ){
 27150     rc = fsync(fd);
 27152 #endif /* OS_VXWORKS */
 27153 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
 27155   if( OS_VXWORKS && rc!= -1 ){
 27156     rc = 0;
 27158   return rc;
 27161 /*
 27162 ** Open a file descriptor to the directory containing file zFilename.
 27163 ** If successful, *pFd is set to the opened file descriptor and
 27164 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
 27165 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
 27166 ** value.
 27167 **
 27168 ** The directory file descriptor is used for only one thing - to
 27169 ** fsync() a directory to make sure file creation and deletion events
 27170 ** are flushed to disk.  Such fsyncs are not needed on newer
 27171 ** journaling filesystems, but are required on older filesystems.
 27172 **
 27173 ** This routine can be overridden using the xSetSysCall interface.
 27174 ** The ability to override this routine was added in support of the
 27175 ** chromium sandbox.  Opening a directory is a security risk (we are
 27176 ** told) so making it overrideable allows the chromium sandbox to
 27177 ** replace this routine with a harmless no-op.  To make this routine
 27178 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
 27179 ** *pFd set to a negative number.
 27180 **
 27181 ** If SQLITE_OK is returned, the caller is responsible for closing
 27182 ** the file descriptor *pFd using close().
 27183 */
 27184 static int openDirectory(const char *zFilename, int *pFd){
 27185   int ii;
 27186   int fd = -1;
 27187   char zDirname[MAX_PATHNAME+1];
 27189   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
 27190   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
 27191   if( ii>0 ){
 27192     zDirname[ii] = '\0';
 27193     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
 27194     if( fd>=0 ){
 27195       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
 27198   *pFd = fd;
 27199   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
 27202 /*
 27203 ** Make sure all writes to a particular file are committed to disk.
 27204 **
 27205 ** If dataOnly==0 then both the file itself and its metadata (file
 27206 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
 27207 ** file data is synced.
 27208 **
 27209 ** Under Unix, also make sure that the directory entry for the file
 27210 ** has been created by fsync-ing the directory that contains the file.
 27211 ** If we do not do this and we encounter a power failure, the directory
 27212 ** entry for the journal might not exist after we reboot.  The next
 27213 ** SQLite to access the file will not know that the journal exists (because
 27214 ** the directory entry for the journal was never created) and the transaction
 27215 ** will not roll back - possibly leading to database corruption.
 27216 */
 27217 static int unixSync(sqlite3_file *id, int flags){
 27218   int rc;
 27219   unixFile *pFile = (unixFile*)id;
 27221   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
 27222   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
 27224   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 27225   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 27226       || (flags&0x0F)==SQLITE_SYNC_FULL
 27227   );
 27229   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 27230   ** line is to test that doing so does not cause any problems.
 27231   */
 27232   SimulateDiskfullError( return SQLITE_FULL );
 27234   assert( pFile );
 27235   OSTRACE(("SYNC    %-3d\n", pFile->h));
 27236   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
 27237   SimulateIOError( rc=1 );
 27238   if( rc ){
 27239     pFile->lastErrno = errno;
 27240     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
 27243   /* Also fsync the directory containing the file if the DIRSYNC flag
 27244   ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
 27245   ** are unable to fsync a directory, so ignore errors on the fsync.
 27246   */
 27247   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
 27248     int dirfd;
 27249     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
 27250             HAVE_FULLFSYNC, isFullsync));
 27251     rc = osOpenDirectory(pFile->zPath, &dirfd);
 27252     if( rc==SQLITE_OK && dirfd>=0 ){
 27253       full_fsync(dirfd, 0, 0);
 27254       robust_close(pFile, dirfd, __LINE__);
 27255     }else if( rc==SQLITE_CANTOPEN ){
 27256       rc = SQLITE_OK;
 27258     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
 27260   return rc;
 27263 /*
 27264 ** Truncate an open file to a specified size
 27265 */
 27266 static int unixTruncate(sqlite3_file *id, i64 nByte){
 27267   unixFile *pFile = (unixFile *)id;
 27268   int rc;
 27269   assert( pFile );
 27270   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
 27272   /* If the user has configured a chunk-size for this file, truncate the
 27273   ** file so that it consists of an integer number of chunks (i.e. the
 27274   ** actual file size after the operation may be larger than the requested
 27275   ** size).
 27276   */
 27277   if( pFile->szChunk>0 ){
 27278     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 27281   rc = robust_ftruncate(pFile->h, (off_t)nByte);
 27282   if( rc ){
 27283     pFile->lastErrno = errno;
 27284     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 27285   }else{
 27286 #ifdef SQLITE_DEBUG
 27287     /* If we are doing a normal write to a database file (as opposed to
 27288     ** doing a hot-journal rollback or a write to some file other than a
 27289     ** normal database file) and we truncate the file to zero length,
 27290     ** that effectively updates the change counter.  This might happen
 27291     ** when restoring a database using the backup API from a zero-length
 27292     ** source.
 27293     */
 27294     if( pFile->inNormalWrite && nByte==0 ){
 27295       pFile->transCntrChng = 1;
 27297 #endif
 27299 #if SQLITE_MAX_MMAP_SIZE>0
 27300     /* If the file was just truncated to a size smaller than the currently
 27301     ** mapped region, reduce the effective mapping size as well. SQLite will
 27302     ** use read() and write() to access data beyond this point from now on.  
 27303     */
 27304     if( nByte<pFile->mmapSize ){
 27305       pFile->mmapSize = nByte;
 27307 #endif
 27309     return SQLITE_OK;
 27313 /*
 27314 ** Determine the current size of a file in bytes
 27315 */
 27316 static int unixFileSize(sqlite3_file *id, i64 *pSize){
 27317   int rc;
 27318   struct stat buf;
 27319   assert( id );
 27320   rc = osFstat(((unixFile*)id)->h, &buf);
 27321   SimulateIOError( rc=1 );
 27322   if( rc!=0 ){
 27323     ((unixFile*)id)->lastErrno = errno;
 27324     return SQLITE_IOERR_FSTAT;
 27326   *pSize = buf.st_size;
 27328   /* When opening a zero-size database, the findInodeInfo() procedure
 27329   ** writes a single byte into that file in order to work around a bug
 27330   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
 27331   ** layers, we need to report this file size as zero even though it is
 27332   ** really 1.   Ticket #3260.
 27333   */
 27334   if( *pSize==1 ) *pSize = 0;
 27337   return SQLITE_OK;
 27340 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27341 /*
 27342 ** Handler for proxy-locking file-control verbs.  Defined below in the
 27343 ** proxying locking division.
 27344 */
 27345 static int proxyFileControl(sqlite3_file*,int,void*);
 27346 #endif
 27348 /* 
 27349 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
 27350 ** file-control operation.  Enlarge the database to nBytes in size
 27351 ** (rounded up to the next chunk-size).  If the database is already
 27352 ** nBytes or larger, this routine is a no-op.
 27353 */
 27354 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
 27355   if( pFile->szChunk>0 ){
 27356     i64 nSize;                    /* Required file size */
 27357     struct stat buf;              /* Used to hold return values of fstat() */
 27359     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
 27361     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 27362     if( nSize>(i64)buf.st_size ){
 27364 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 27365       /* The code below is handling the return value of osFallocate() 
 27366       ** correctly. posix_fallocate() is defined to "returns zero on success, 
 27367       ** or an error number on  failure". See the manpage for details. */
 27368       int err;
 27369       do{
 27370         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
 27371       }while( err==EINTR );
 27372       if( err ) return SQLITE_IOERR_WRITE;
 27373 #else
 27374       /* If the OS does not have posix_fallocate(), fake it. First use
 27375       ** ftruncate() to set the file size, then write a single byte to
 27376       ** the last byte in each block within the extended region. This
 27377       ** is the same technique used by glibc to implement posix_fallocate()
 27378       ** on systems that do not have a real fallocate() system call.
 27379       */
 27380       int nBlk = buf.st_blksize;  /* File-system block size */
 27381       i64 iWrite;                 /* Next offset to write to */
 27383       if( robust_ftruncate(pFile->h, nSize) ){
 27384         pFile->lastErrno = errno;
 27385         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 27387       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
 27388       while( iWrite<nSize ){
 27389         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
 27390         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
 27391         iWrite += nBlk;
 27393 #endif
 27397 #if SQLITE_MAX_MMAP_SIZE>0
 27398   if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
 27399     int rc;
 27400     if( pFile->szChunk<=0 ){
 27401       if( robust_ftruncate(pFile->h, nByte) ){
 27402         pFile->lastErrno = errno;
 27403         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 27407     rc = unixMapfile(pFile, nByte);
 27408     return rc;
 27410 #endif
 27412   return SQLITE_OK;
 27415 /*
 27416 ** If *pArg is inititially negative then this is a query.  Set *pArg to
 27417 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 27418 **
 27419 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 27420 */
 27421 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
 27422   if( *pArg<0 ){
 27423     *pArg = (pFile->ctrlFlags & mask)!=0;
 27424   }else if( (*pArg)==0 ){
 27425     pFile->ctrlFlags &= ~mask;
 27426   }else{
 27427     pFile->ctrlFlags |= mask;
 27431 /* Forward declaration */
 27432 static int unixGetTempname(int nBuf, char *zBuf);
 27434 /*
 27435 ** Information and control of an open file handle.
 27436 */
 27437 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
 27438   unixFile *pFile = (unixFile*)id;
 27439   switch( op ){
 27440     case SQLITE_FCNTL_LOCKSTATE: {
 27441       *(int*)pArg = pFile->eFileLock;
 27442       return SQLITE_OK;
 27444     case SQLITE_LAST_ERRNO: {
 27445       *(int*)pArg = pFile->lastErrno;
 27446       return SQLITE_OK;
 27448     case SQLITE_FCNTL_CHUNK_SIZE: {
 27449       pFile->szChunk = *(int *)pArg;
 27450       return SQLITE_OK;
 27452     case SQLITE_FCNTL_SIZE_HINT: {
 27453       int rc;
 27454       SimulateIOErrorBenign(1);
 27455       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
 27456       SimulateIOErrorBenign(0);
 27457       return rc;
 27459     case SQLITE_FCNTL_PERSIST_WAL: {
 27460       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
 27461       return SQLITE_OK;
 27463     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 27464       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
 27465       return SQLITE_OK;
 27467     case SQLITE_FCNTL_VFSNAME: {
 27468       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 27469       return SQLITE_OK;
 27471     case SQLITE_FCNTL_TEMPFILENAME: {
 27472       char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
 27473       if( zTFile ){
 27474         unixGetTempname(pFile->pVfs->mxPathname, zTFile);
 27475         *(char**)pArg = zTFile;
 27477       return SQLITE_OK;
 27479     case SQLITE_FCNTL_HAS_MOVED: {
 27480       *(int*)pArg = fileHasMoved(pFile);
 27481       return SQLITE_OK;
 27483 #if SQLITE_MAX_MMAP_SIZE>0
 27484     case SQLITE_FCNTL_MMAP_SIZE: {
 27485       i64 newLimit = *(i64*)pArg;
 27486       int rc = SQLITE_OK;
 27487       if( newLimit>sqlite3GlobalConfig.mxMmap ){
 27488         newLimit = sqlite3GlobalConfig.mxMmap;
 27490       *(i64*)pArg = pFile->mmapSizeMax;
 27491       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
 27492         pFile->mmapSizeMax = newLimit;
 27493         if( pFile->mmapSize>0 ){
 27494           unixUnmapfile(pFile);
 27495           rc = unixMapfile(pFile, -1);
 27498       return rc;
 27500 #endif
 27501 #ifdef SQLITE_DEBUG
 27502     /* The pager calls this method to signal that it has done
 27503     ** a rollback and that the database is therefore unchanged and
 27504     ** it hence it is OK for the transaction change counter to be
 27505     ** unchanged.
 27506     */
 27507     case SQLITE_FCNTL_DB_UNCHANGED: {
 27508       ((unixFile*)id)->dbUpdate = 0;
 27509       return SQLITE_OK;
 27511 #endif
 27512 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27513     case SQLITE_SET_LOCKPROXYFILE:
 27514     case SQLITE_GET_LOCKPROXYFILE: {
 27515       return proxyFileControl(id,op,pArg);
 27517 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 27519   return SQLITE_NOTFOUND;
 27522 /*
 27523 ** Return the sector size in bytes of the underlying block device for
 27524 ** the specified file. This is almost always 512 bytes, but may be
 27525 ** larger for some devices.
 27526 **
 27527 ** SQLite code assumes this function cannot fail. It also assumes that
 27528 ** if two files are created in the same file-system directory (i.e.
 27529 ** a database and its journal file) that the sector size will be the
 27530 ** same for both.
 27531 */
 27532 #ifndef __QNXNTO__ 
 27533 static int unixSectorSize(sqlite3_file *NotUsed){
 27534   UNUSED_PARAMETER(NotUsed);
 27535   return SQLITE_DEFAULT_SECTOR_SIZE;
 27537 #endif
 27539 /*
 27540 ** The following version of unixSectorSize() is optimized for QNX.
 27541 */
 27542 #ifdef __QNXNTO__
 27543 #include <sys/dcmd_blk.h>
 27544 #include <sys/statvfs.h>
 27545 static int unixSectorSize(sqlite3_file *id){
 27546   unixFile *pFile = (unixFile*)id;
 27547   if( pFile->sectorSize == 0 ){
 27548     struct statvfs fsInfo;
 27550     /* Set defaults for non-supported filesystems */
 27551     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 27552     pFile->deviceCharacteristics = 0;
 27553     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
 27554       return pFile->sectorSize;
 27557     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
 27558       pFile->sectorSize = fsInfo.f_bsize;
 27559       pFile->deviceCharacteristics =
 27560         SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
 27561         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 27562                                       ** the write succeeds */
 27563         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 27564                                       ** so it is ordered */
 27565         0;
 27566     }else if( strstr(fsInfo.f_basetype, "etfs") ){
 27567       pFile->sectorSize = fsInfo.f_bsize;
 27568       pFile->deviceCharacteristics =
 27569         /* etfs cluster size writes are atomic */
 27570         (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
 27571         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 27572                                       ** the write succeeds */
 27573         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 27574                                       ** so it is ordered */
 27575         0;
 27576     }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
 27577       pFile->sectorSize = fsInfo.f_bsize;
 27578       pFile->deviceCharacteristics =
 27579         SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
 27580         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 27581                                       ** the write succeeds */
 27582         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 27583                                       ** so it is ordered */
 27584         0;
 27585     }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
 27586       pFile->sectorSize = fsInfo.f_bsize;
 27587       pFile->deviceCharacteristics =
 27588         /* full bitset of atomics from max sector size and smaller */
 27589         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 27590         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 27591                                       ** so it is ordered */
 27592         0;
 27593     }else if( strstr(fsInfo.f_basetype, "dos") ){
 27594       pFile->sectorSize = fsInfo.f_bsize;
 27595       pFile->deviceCharacteristics =
 27596         /* full bitset of atomics from max sector size and smaller */
 27597         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
 27598         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
 27599                                       ** so it is ordered */
 27600         0;
 27601     }else{
 27602       pFile->deviceCharacteristics =
 27603         SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
 27604         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
 27605                                       ** the write succeeds */
 27606         0;
 27609   /* Last chance verification.  If the sector size isn't a multiple of 512
 27610   ** then it isn't valid.*/
 27611   if( pFile->sectorSize % 512 != 0 ){
 27612     pFile->deviceCharacteristics = 0;
 27613     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
 27615   return pFile->sectorSize;
 27617 #endif /* __QNXNTO__ */
 27619 /*
 27620 ** Return the device characteristics for the file.
 27621 **
 27622 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
 27623 ** However, that choice is contraversial since technically the underlying
 27624 ** file system does not always provide powersafe overwrites.  (In other
 27625 ** words, after a power-loss event, parts of the file that were never
 27626 ** written might end up being altered.)  However, non-PSOW behavior is very,
 27627 ** very rare.  And asserting PSOW makes a large reduction in the amount
 27628 ** of required I/O for journaling, since a lot of padding is eliminated.
 27629 **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
 27630 ** available to turn it off and URI query parameter available to turn it off.
 27631 */
 27632 static int unixDeviceCharacteristics(sqlite3_file *id){
 27633   unixFile *p = (unixFile*)id;
 27634   int rc = 0;
 27635 #ifdef __QNXNTO__
 27636   if( p->sectorSize==0 ) unixSectorSize(id);
 27637   rc = p->deviceCharacteristics;
 27638 #endif
 27639   if( p->ctrlFlags & UNIXFILE_PSOW ){
 27640     rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
 27642   return rc;
 27645 #ifndef SQLITE_OMIT_WAL
 27648 /*
 27649 ** Object used to represent an shared memory buffer.  
 27650 **
 27651 ** When multiple threads all reference the same wal-index, each thread
 27652 ** has its own unixShm object, but they all point to a single instance
 27653 ** of this unixShmNode object.  In other words, each wal-index is opened
 27654 ** only once per process.
 27655 **
 27656 ** Each unixShmNode object is connected to a single unixInodeInfo object.
 27657 ** We could coalesce this object into unixInodeInfo, but that would mean
 27658 ** every open file that does not use shared memory (in other words, most
 27659 ** open files) would have to carry around this extra information.  So
 27660 ** the unixInodeInfo object contains a pointer to this unixShmNode object
 27661 ** and the unixShmNode object is created only when needed.
 27662 **
 27663 ** unixMutexHeld() must be true when creating or destroying
 27664 ** this object or while reading or writing the following fields:
 27665 **
 27666 **      nRef
 27667 **
 27668 ** The following fields are read-only after the object is created:
 27669 ** 
 27670 **      fid
 27671 **      zFilename
 27672 **
 27673 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
 27674 ** unixMutexHeld() is true when reading or writing any other field
 27675 ** in this structure.
 27676 */
 27677 struct unixShmNode {
 27678   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
 27679   sqlite3_mutex *mutex;      /* Mutex to access this object */
 27680   char *zFilename;           /* Name of the mmapped file */
 27681   int h;                     /* Open file descriptor */
 27682   int szRegion;              /* Size of shared-memory regions */
 27683   u16 nRegion;               /* Size of array apRegion */
 27684   u8 isReadonly;             /* True if read-only */
 27685   char **apRegion;           /* Array of mapped shared-memory regions */
 27686   int nRef;                  /* Number of unixShm objects pointing to this */
 27687   unixShm *pFirst;           /* All unixShm objects pointing to this */
 27688 #ifdef SQLITE_DEBUG
 27689   u8 exclMask;               /* Mask of exclusive locks held */
 27690   u8 sharedMask;             /* Mask of shared locks held */
 27691   u8 nextShmId;              /* Next available unixShm.id value */
 27692 #endif
 27693 };
 27695 /*
 27696 ** Structure used internally by this VFS to record the state of an
 27697 ** open shared memory connection.
 27698 **
 27699 ** The following fields are initialized when this object is created and
 27700 ** are read-only thereafter:
 27701 **
 27702 **    unixShm.pFile
 27703 **    unixShm.id
 27704 **
 27705 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
 27706 ** while accessing any read/write fields.
 27707 */
 27708 struct unixShm {
 27709   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
 27710   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
 27711   u8 hasMutex;               /* True if holding the unixShmNode mutex */
 27712   u8 id;                     /* Id of this connection within its unixShmNode */
 27713   u16 sharedMask;            /* Mask of shared locks held */
 27714   u16 exclMask;              /* Mask of exclusive locks held */
 27715 };
 27717 /*
 27718 ** Constants used for locking
 27719 */
 27720 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
 27721 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 27723 /*
 27724 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
 27725 **
 27726 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
 27727 ** otherwise.
 27728 */
 27729 static int unixShmSystemLock(
 27730   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
 27731   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
 27732   int ofst,              /* First byte of the locking range */
 27733   int n                  /* Number of bytes to lock */
 27734 ){
 27735   struct flock f;       /* The posix advisory locking structure */
 27736   int rc = SQLITE_OK;   /* Result code form fcntl() */
 27738   /* Access to the unixShmNode object is serialized by the caller */
 27739   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
 27741   /* Shared locks never span more than one byte */
 27742   assert( n==1 || lockType!=F_RDLCK );
 27744   /* Locks are within range */
 27745   assert( n>=1 && n<SQLITE_SHM_NLOCK );
 27747   if( pShmNode->h>=0 ){
 27748     /* Initialize the locking parameters */
 27749     memset(&f, 0, sizeof(f));
 27750     f.l_type = lockType;
 27751     f.l_whence = SEEK_SET;
 27752     f.l_start = ofst;
 27753     f.l_len = n;
 27755     rc = osFcntl(pShmNode->h, F_SETLK, &f);
 27756     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 27759   /* Update the global lock state and do debug tracing */
 27760 #ifdef SQLITE_DEBUG
 27761   { u16 mask;
 27762   OSTRACE(("SHM-LOCK "));
 27763   mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
 27764   if( rc==SQLITE_OK ){
 27765     if( lockType==F_UNLCK ){
 27766       OSTRACE(("unlock %d ok", ofst));
 27767       pShmNode->exclMask &= ~mask;
 27768       pShmNode->sharedMask &= ~mask;
 27769     }else if( lockType==F_RDLCK ){
 27770       OSTRACE(("read-lock %d ok", ofst));
 27771       pShmNode->exclMask &= ~mask;
 27772       pShmNode->sharedMask |= mask;
 27773     }else{
 27774       assert( lockType==F_WRLCK );
 27775       OSTRACE(("write-lock %d ok", ofst));
 27776       pShmNode->exclMask |= mask;
 27777       pShmNode->sharedMask &= ~mask;
 27779   }else{
 27780     if( lockType==F_UNLCK ){
 27781       OSTRACE(("unlock %d failed", ofst));
 27782     }else if( lockType==F_RDLCK ){
 27783       OSTRACE(("read-lock failed"));
 27784     }else{
 27785       assert( lockType==F_WRLCK );
 27786       OSTRACE(("write-lock %d failed", ofst));
 27789   OSTRACE((" - afterwards %03x,%03x\n",
 27790            pShmNode->sharedMask, pShmNode->exclMask));
 27792 #endif
 27794   return rc;        
 27798 /*
 27799 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
 27800 **
 27801 ** This is not a VFS shared-memory method; it is a utility function called
 27802 ** by VFS shared-memory methods.
 27803 */
 27804 static void unixShmPurge(unixFile *pFd){
 27805   unixShmNode *p = pFd->pInode->pShmNode;
 27806   assert( unixMutexHeld() );
 27807   if( p && p->nRef==0 ){
 27808     int i;
 27809     assert( p->pInode==pFd->pInode );
 27810     sqlite3_mutex_free(p->mutex);
 27811     for(i=0; i<p->nRegion; i++){
 27812       if( p->h>=0 ){
 27813         osMunmap(p->apRegion[i], p->szRegion);
 27814       }else{
 27815         sqlite3_free(p->apRegion[i]);
 27818     sqlite3_free(p->apRegion);
 27819     if( p->h>=0 ){
 27820       robust_close(pFd, p->h, __LINE__);
 27821       p->h = -1;
 27823     p->pInode->pShmNode = 0;
 27824     sqlite3_free(p);
 27828 /*
 27829 ** Open a shared-memory area associated with open database file pDbFd.  
 27830 ** This particular implementation uses mmapped files.
 27831 **
 27832 ** The file used to implement shared-memory is in the same directory
 27833 ** as the open database file and has the same name as the open database
 27834 ** file with the "-shm" suffix added.  For example, if the database file
 27835 ** is "/home/user1/config.db" then the file that is created and mmapped
 27836 ** for shared memory will be called "/home/user1/config.db-shm".  
 27837 **
 27838 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
 27839 ** some other tmpfs mount. But if a file in a different directory
 27840 ** from the database file is used, then differing access permissions
 27841 ** or a chroot() might cause two different processes on the same
 27842 ** database to end up using different files for shared memory - 
 27843 ** meaning that their memory would not really be shared - resulting
 27844 ** in database corruption.  Nevertheless, this tmpfs file usage
 27845 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
 27846 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
 27847 ** option results in an incompatible build of SQLite;  builds of SQLite
 27848 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
 27849 ** same database file at the same time, database corruption will likely
 27850 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
 27851 ** "unsupported" and may go away in a future SQLite release.
 27852 **
 27853 ** When opening a new shared-memory file, if no other instances of that
 27854 ** file are currently open, in this process or in other processes, then
 27855 ** the file must be truncated to zero length or have its header cleared.
 27856 **
 27857 ** If the original database file (pDbFd) is using the "unix-excl" VFS
 27858 ** that means that an exclusive lock is held on the database file and
 27859 ** that no other processes are able to read or write the database.  In
 27860 ** that case, we do not really need shared memory.  No shared memory
 27861 ** file is created.  The shared memory will be simulated with heap memory.
 27862 */
 27863 static int unixOpenSharedMemory(unixFile *pDbFd){
 27864   struct unixShm *p = 0;          /* The connection to be opened */
 27865   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 27866   int rc;                         /* Result code */
 27867   unixInodeInfo *pInode;          /* The inode of fd */
 27868   char *zShmFilename;             /* Name of the file used for SHM */
 27869   int nShmFilename;               /* Size of the SHM filename in bytes */
 27871   /* Allocate space for the new unixShm object. */
 27872   p = sqlite3_malloc( sizeof(*p) );
 27873   if( p==0 ) return SQLITE_NOMEM;
 27874   memset(p, 0, sizeof(*p));
 27875   assert( pDbFd->pShm==0 );
 27877   /* Check to see if a unixShmNode object already exists. Reuse an existing
 27878   ** one if present. Create a new one if necessary.
 27879   */
 27880   unixEnterMutex();
 27881   pInode = pDbFd->pInode;
 27882   pShmNode = pInode->pShmNode;
 27883   if( pShmNode==0 ){
 27884     struct stat sStat;                 /* fstat() info for database file */
 27886     /* Call fstat() to figure out the permissions on the database file. If
 27887     ** a new *-shm file is created, an attempt will be made to create it
 27888     ** with the same permissions.
 27889     */
 27890     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
 27891       rc = SQLITE_IOERR_FSTAT;
 27892       goto shm_open_err;
 27895 #ifdef SQLITE_SHM_DIRECTORY
 27896     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
 27897 #else
 27898     nShmFilename = 6 + (int)strlen(pDbFd->zPath);
 27899 #endif
 27900     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
 27901     if( pShmNode==0 ){
 27902       rc = SQLITE_NOMEM;
 27903       goto shm_open_err;
 27905     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
 27906     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
 27907 #ifdef SQLITE_SHM_DIRECTORY
 27908     sqlite3_snprintf(nShmFilename, zShmFilename, 
 27909                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
 27910                      (u32)sStat.st_ino, (u32)sStat.st_dev);
 27911 #else
 27912     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
 27913     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
 27914 #endif
 27915     pShmNode->h = -1;
 27916     pDbFd->pInode->pShmNode = pShmNode;
 27917     pShmNode->pInode = pDbFd->pInode;
 27918     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 27919     if( pShmNode->mutex==0 ){
 27920       rc = SQLITE_NOMEM;
 27921       goto shm_open_err;
 27924     if( pInode->bProcessLock==0 ){
 27925       int openFlags = O_RDWR | O_CREAT;
 27926       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 27927         openFlags = O_RDONLY;
 27928         pShmNode->isReadonly = 1;
 27930       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
 27931       if( pShmNode->h<0 ){
 27932         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
 27933         goto shm_open_err;
 27936       /* If this process is running as root, make sure that the SHM file
 27937       ** is owned by the same user that owns the original database.  Otherwise,
 27938       ** the original owner will not be able to connect.
 27939       */
 27940       osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
 27942       /* Check to see if another process is holding the dead-man switch.
 27943       ** If not, truncate the file to zero length. 
 27944       */
 27945       rc = SQLITE_OK;
 27946       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
 27947         if( robust_ftruncate(pShmNode->h, 0) ){
 27948           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
 27951       if( rc==SQLITE_OK ){
 27952         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
 27954       if( rc ) goto shm_open_err;
 27958   /* Make the new connection a child of the unixShmNode */
 27959   p->pShmNode = pShmNode;
 27960 #ifdef SQLITE_DEBUG
 27961   p->id = pShmNode->nextShmId++;
 27962 #endif
 27963   pShmNode->nRef++;
 27964   pDbFd->pShm = p;
 27965   unixLeaveMutex();
 27967   /* The reference count on pShmNode has already been incremented under
 27968   ** the cover of the unixEnterMutex() mutex and the pointer from the
 27969   ** new (struct unixShm) object to the pShmNode has been set. All that is
 27970   ** left to do is to link the new object into the linked list starting
 27971   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 27972   ** mutex.
 27973   */
 27974   sqlite3_mutex_enter(pShmNode->mutex);
 27975   p->pNext = pShmNode->pFirst;
 27976   pShmNode->pFirst = p;
 27977   sqlite3_mutex_leave(pShmNode->mutex);
 27978   return SQLITE_OK;
 27980   /* Jump here on any error */
 27981 shm_open_err:
 27982   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
 27983   sqlite3_free(p);
 27984   unixLeaveMutex();
 27985   return rc;
 27988 /*
 27989 ** This function is called to obtain a pointer to region iRegion of the 
 27990 ** shared-memory associated with the database file fd. Shared-memory regions 
 27991 ** are numbered starting from zero. Each shared-memory region is szRegion 
 27992 ** bytes in size.
 27993 **
 27994 ** If an error occurs, an error code is returned and *pp is set to NULL.
 27995 **
 27996 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
 27997 ** region has not been allocated (by any client, including one running in a
 27998 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 27999 ** bExtend is non-zero and the requested shared-memory region has not yet 
 28000 ** been allocated, it is allocated by this function.
 28001 **
 28002 ** If the shared-memory region has already been allocated or is allocated by
 28003 ** this call as described above, then it is mapped into this processes 
 28004 ** address space (if it is not already), *pp is set to point to the mapped 
 28005 ** memory and SQLITE_OK returned.
 28006 */
 28007 static int unixShmMap(
 28008   sqlite3_file *fd,               /* Handle open on database file */
 28009   int iRegion,                    /* Region to retrieve */
 28010   int szRegion,                   /* Size of regions */
 28011   int bExtend,                    /* True to extend file if necessary */
 28012   void volatile **pp              /* OUT: Mapped memory */
 28013 ){
 28014   unixFile *pDbFd = (unixFile*)fd;
 28015   unixShm *p;
 28016   unixShmNode *pShmNode;
 28017   int rc = SQLITE_OK;
 28019   /* If the shared-memory file has not yet been opened, open it now. */
 28020   if( pDbFd->pShm==0 ){
 28021     rc = unixOpenSharedMemory(pDbFd);
 28022     if( rc!=SQLITE_OK ) return rc;
 28025   p = pDbFd->pShm;
 28026   pShmNode = p->pShmNode;
 28027   sqlite3_mutex_enter(pShmNode->mutex);
 28028   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 28029   assert( pShmNode->pInode==pDbFd->pInode );
 28030   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 28031   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 28033   if( pShmNode->nRegion<=iRegion ){
 28034     char **apNew;                      /* New apRegion[] array */
 28035     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 28036     struct stat sStat;                 /* Used by fstat() */
 28038     pShmNode->szRegion = szRegion;
 28040     if( pShmNode->h>=0 ){
 28041       /* The requested region is not mapped into this processes address space.
 28042       ** Check to see if it has been allocated (i.e. if the wal-index file is
 28043       ** large enough to contain the requested region).
 28044       */
 28045       if( osFstat(pShmNode->h, &sStat) ){
 28046         rc = SQLITE_IOERR_SHMSIZE;
 28047         goto shmpage_out;
 28050       if( sStat.st_size<nByte ){
 28051         /* The requested memory region does not exist. If bExtend is set to
 28052         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
 28053         */
 28054         if( !bExtend ){
 28055           goto shmpage_out;
 28058         /* Alternatively, if bExtend is true, extend the file. Do this by
 28059         ** writing a single byte to the end of each (OS) page being
 28060         ** allocated or extended. Technically, we need only write to the
 28061         ** last page in order to extend the file. But writing to all new
 28062         ** pages forces the OS to allocate them immediately, which reduces
 28063         ** the chances of SIGBUS while accessing the mapped region later on.
 28064         */
 28065         else{
 28066           static const int pgsz = 4096;
 28067           int iPg;
 28069           /* Write to the last byte of each newly allocated or extended page */
 28070           assert( (nByte % pgsz)==0 );
 28071           for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
 28072             if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
 28073               const char *zFile = pShmNode->zFilename;
 28074               rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
 28075               goto shmpage_out;
 28082     /* Map the requested memory region into this processes address space. */
 28083     apNew = (char **)sqlite3_realloc(
 28084         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
 28085     );
 28086     if( !apNew ){
 28087       rc = SQLITE_IOERR_NOMEM;
 28088       goto shmpage_out;
 28090     pShmNode->apRegion = apNew;
 28091     while(pShmNode->nRegion<=iRegion){
 28092       void *pMem;
 28093       if( pShmNode->h>=0 ){
 28094         pMem = osMmap(0, szRegion,
 28095             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
 28096             MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
 28097         );
 28098         if( pMem==MAP_FAILED ){
 28099           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
 28100           goto shmpage_out;
 28102       }else{
 28103         pMem = sqlite3_malloc(szRegion);
 28104         if( pMem==0 ){
 28105           rc = SQLITE_NOMEM;
 28106           goto shmpage_out;
 28108         memset(pMem, 0, szRegion);
 28110       pShmNode->apRegion[pShmNode->nRegion] = pMem;
 28111       pShmNode->nRegion++;
 28115 shmpage_out:
 28116   if( pShmNode->nRegion>iRegion ){
 28117     *pp = pShmNode->apRegion[iRegion];
 28118   }else{
 28119     *pp = 0;
 28121   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 28122   sqlite3_mutex_leave(pShmNode->mutex);
 28123   return rc;
 28126 /*
 28127 ** Change the lock state for a shared-memory segment.
 28128 **
 28129 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
 28130 ** different here than in posix.  In xShmLock(), one can go from unlocked
 28131 ** to shared and back or from unlocked to exclusive and back.  But one may
 28132 ** not go from shared to exclusive or from exclusive to shared.
 28133 */
 28134 static int unixShmLock(
 28135   sqlite3_file *fd,          /* Database file holding the shared memory */
 28136   int ofst,                  /* First lock to acquire or release */
 28137   int n,                     /* Number of locks to acquire or release */
 28138   int flags                  /* What to do with the lock */
 28139 ){
 28140   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
 28141   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
 28142   unixShm *pX;                          /* For looping over all siblings */
 28143   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
 28144   int rc = SQLITE_OK;                   /* Result code */
 28145   u16 mask;                             /* Mask of locks to take or release */
 28147   assert( pShmNode==pDbFd->pInode->pShmNode );
 28148   assert( pShmNode->pInode==pDbFd->pInode );
 28149   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 28150   assert( n>=1 );
 28151   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 28152        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 28153        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 28154        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 28155   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 28156   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
 28157   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 28159   mask = (1<<(ofst+n)) - (1<<ofst);
 28160   assert( n>1 || mask==(1<<ofst) );
 28161   sqlite3_mutex_enter(pShmNode->mutex);
 28162   if( flags & SQLITE_SHM_UNLOCK ){
 28163     u16 allMask = 0; /* Mask of locks held by siblings */
 28165     /* See if any siblings hold this same lock */
 28166     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 28167       if( pX==p ) continue;
 28168       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 28169       allMask |= pX->sharedMask;
 28172     /* Unlock the system-level locks */
 28173     if( (mask & allMask)==0 ){
 28174       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
 28175     }else{
 28176       rc = SQLITE_OK;
 28179     /* Undo the local locks */
 28180     if( rc==SQLITE_OK ){
 28181       p->exclMask &= ~mask;
 28182       p->sharedMask &= ~mask;
 28184   }else if( flags & SQLITE_SHM_SHARED ){
 28185     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 28187     /* Find out which shared locks are already held by sibling connections.
 28188     ** If any sibling already holds an exclusive lock, go ahead and return
 28189     ** SQLITE_BUSY.
 28190     */
 28191     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 28192       if( (pX->exclMask & mask)!=0 ){
 28193         rc = SQLITE_BUSY;
 28194         break;
 28196       allShared |= pX->sharedMask;
 28199     /* Get shared locks at the system level, if necessary */
 28200     if( rc==SQLITE_OK ){
 28201       if( (allShared & mask)==0 ){
 28202         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
 28203       }else{
 28204         rc = SQLITE_OK;
 28208     /* Get the local shared locks */
 28209     if( rc==SQLITE_OK ){
 28210       p->sharedMask |= mask;
 28212   }else{
 28213     /* Make sure no sibling connections hold locks that will block this
 28214     ** lock.  If any do, return SQLITE_BUSY right away.
 28215     */
 28216     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 28217       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 28218         rc = SQLITE_BUSY;
 28219         break;
 28223     /* Get the exclusive locks at the system level.  Then if successful
 28224     ** also mark the local connection as being locked.
 28225     */
 28226     if( rc==SQLITE_OK ){
 28227       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
 28228       if( rc==SQLITE_OK ){
 28229         assert( (p->sharedMask & mask)==0 );
 28230         p->exclMask |= mask;
 28234   sqlite3_mutex_leave(pShmNode->mutex);
 28235   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
 28236            p->id, getpid(), p->sharedMask, p->exclMask));
 28237   return rc;
 28240 /*
 28241 ** Implement a memory barrier or memory fence on shared memory.  
 28242 **
 28243 ** All loads and stores begun before the barrier must complete before
 28244 ** any load or store begun after the barrier.
 28245 */
 28246 static void unixShmBarrier(
 28247   sqlite3_file *fd                /* Database file holding the shared memory */
 28248 ){
 28249   UNUSED_PARAMETER(fd);
 28250   unixEnterMutex();
 28251   unixLeaveMutex();
 28254 /*
 28255 ** Close a connection to shared-memory.  Delete the underlying 
 28256 ** storage if deleteFlag is true.
 28257 **
 28258 ** If there is no shared memory associated with the connection then this
 28259 ** routine is a harmless no-op.
 28260 */
 28261 static int unixShmUnmap(
 28262   sqlite3_file *fd,               /* The underlying database file */
 28263   int deleteFlag                  /* Delete shared-memory if true */
 28264 ){
 28265   unixShm *p;                     /* The connection to be closed */
 28266   unixShmNode *pShmNode;          /* The underlying shared-memory file */
 28267   unixShm **pp;                   /* For looping over sibling connections */
 28268   unixFile *pDbFd;                /* The underlying database file */
 28270   pDbFd = (unixFile*)fd;
 28271   p = pDbFd->pShm;
 28272   if( p==0 ) return SQLITE_OK;
 28273   pShmNode = p->pShmNode;
 28275   assert( pShmNode==pDbFd->pInode->pShmNode );
 28276   assert( pShmNode->pInode==pDbFd->pInode );
 28278   /* Remove connection p from the set of connections associated
 28279   ** with pShmNode */
 28280   sqlite3_mutex_enter(pShmNode->mutex);
 28281   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 28282   *pp = p->pNext;
 28284   /* Free the connection p */
 28285   sqlite3_free(p);
 28286   pDbFd->pShm = 0;
 28287   sqlite3_mutex_leave(pShmNode->mutex);
 28289   /* If pShmNode->nRef has reached 0, then close the underlying
 28290   ** shared-memory file, too */
 28291   unixEnterMutex();
 28292   assert( pShmNode->nRef>0 );
 28293   pShmNode->nRef--;
 28294   if( pShmNode->nRef==0 ){
 28295     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
 28296     unixShmPurge(pDbFd);
 28298   unixLeaveMutex();
 28300   return SQLITE_OK;
 28304 #else
 28305 # define unixShmMap     0
 28306 # define unixShmLock    0
 28307 # define unixShmBarrier 0
 28308 # define unixShmUnmap   0
 28309 #endif /* #ifndef SQLITE_OMIT_WAL */
 28311 #if SQLITE_MAX_MMAP_SIZE>0
 28312 /*
 28313 ** If it is currently memory mapped, unmap file pFd.
 28314 */
 28315 static void unixUnmapfile(unixFile *pFd){
 28316   assert( pFd->nFetchOut==0 );
 28317   if( pFd->pMapRegion ){
 28318     osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
 28319     pFd->pMapRegion = 0;
 28320     pFd->mmapSize = 0;
 28321     pFd->mmapSizeActual = 0;
 28325 /*
 28326 ** Return the system page size.
 28327 */
 28328 static int unixGetPagesize(void){
 28329 #if HAVE_MREMAP
 28330   return 512;
 28331 #elif defined(_BSD_SOURCE)
 28332   return getpagesize();
 28333 #else
 28334   return (int)sysconf(_SC_PAGESIZE);
 28335 #endif
 28338 /*
 28339 ** Attempt to set the size of the memory mapping maintained by file 
 28340 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
 28341 **
 28342 ** If successful, this function sets the following variables:
 28343 **
 28344 **       unixFile.pMapRegion
 28345 **       unixFile.mmapSize
 28346 **       unixFile.mmapSizeActual
 28347 **
 28348 ** If unsuccessful, an error message is logged via sqlite3_log() and
 28349 ** the three variables above are zeroed. In this case SQLite should
 28350 ** continue accessing the database using the xRead() and xWrite()
 28351 ** methods.
 28352 */
 28353 static void unixRemapfile(
 28354   unixFile *pFd,                  /* File descriptor object */
 28355   i64 nNew                        /* Required mapping size */
 28356 ){
 28357   const char *zErr = "mmap";
 28358   int h = pFd->h;                      /* File descriptor open on db file */
 28359   u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
 28360   i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
 28361   u8 *pNew = 0;                        /* Location of new mapping */
 28362   int flags = PROT_READ;               /* Flags to pass to mmap() */
 28364   assert( pFd->nFetchOut==0 );
 28365   assert( nNew>pFd->mmapSize );
 28366   assert( nNew<=pFd->mmapSizeMax );
 28367   assert( nNew>0 );
 28368   assert( pFd->mmapSizeActual>=pFd->mmapSize );
 28369   assert( MAP_FAILED!=0 );
 28371   if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
 28373   if( pOrig ){
 28374     const int szSyspage = unixGetPagesize();
 28375     i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
 28376     u8 *pReq = &pOrig[nReuse];
 28378     /* Unmap any pages of the existing mapping that cannot be reused. */
 28379     if( nReuse!=nOrig ){
 28380       osMunmap(pReq, nOrig-nReuse);
 28383 #if HAVE_MREMAP
 28384     pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
 28385     zErr = "mremap";
 28386 #else
 28387     pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
 28388     if( pNew!=MAP_FAILED ){
 28389       if( pNew!=pReq ){
 28390         osMunmap(pNew, nNew - nReuse);
 28391         pNew = 0;
 28392       }else{
 28393         pNew = pOrig;
 28396 #endif
 28398     /* The attempt to extend the existing mapping failed. Free it. */
 28399     if( pNew==MAP_FAILED || pNew==0 ){
 28400       osMunmap(pOrig, nReuse);
 28404   /* If pNew is still NULL, try to create an entirely new mapping. */
 28405   if( pNew==0 ){
 28406     pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
 28409   if( pNew==MAP_FAILED ){
 28410     pNew = 0;
 28411     nNew = 0;
 28412     unixLogError(SQLITE_OK, zErr, pFd->zPath);
 28414     /* If the mmap() above failed, assume that all subsequent mmap() calls
 28415     ** will probably fail too. Fall back to using xRead/xWrite exclusively
 28416     ** in this case.  */
 28417     pFd->mmapSizeMax = 0;
 28419   pFd->pMapRegion = (void *)pNew;
 28420   pFd->mmapSize = pFd->mmapSizeActual = nNew;
 28423 /*
 28424 ** Memory map or remap the file opened by file-descriptor pFd (if the file
 28425 ** is already mapped, the existing mapping is replaced by the new). Or, if 
 28426 ** there already exists a mapping for this file, and there are still 
 28427 ** outstanding xFetch() references to it, this function is a no-op.
 28428 **
 28429 ** If parameter nByte is non-negative, then it is the requested size of 
 28430 ** the mapping to create. Otherwise, if nByte is less than zero, then the 
 28431 ** requested size is the size of the file on disk. The actual size of the
 28432 ** created mapping is either the requested size or the value configured 
 28433 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
 28434 **
 28435 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
 28436 ** recreated as a result of outstanding references) or an SQLite error
 28437 ** code otherwise.
 28438 */
 28439 static int unixMapfile(unixFile *pFd, i64 nByte){
 28440   i64 nMap = nByte;
 28441   int rc;
 28443   assert( nMap>=0 || pFd->nFetchOut==0 );
 28444   if( pFd->nFetchOut>0 ) return SQLITE_OK;
 28446   if( nMap<0 ){
 28447     struct stat statbuf;          /* Low-level file information */
 28448     rc = osFstat(pFd->h, &statbuf);
 28449     if( rc!=SQLITE_OK ){
 28450       return SQLITE_IOERR_FSTAT;
 28452     nMap = statbuf.st_size;
 28454   if( nMap>pFd->mmapSizeMax ){
 28455     nMap = pFd->mmapSizeMax;
 28458   if( nMap!=pFd->mmapSize ){
 28459     if( nMap>0 ){
 28460       unixRemapfile(pFd, nMap);
 28461     }else{
 28462       unixUnmapfile(pFd);
 28466   return SQLITE_OK;
 28468 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
 28470 /*
 28471 ** If possible, return a pointer to a mapping of file fd starting at offset
 28472 ** iOff. The mapping must be valid for at least nAmt bytes.
 28473 **
 28474 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
 28475 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
 28476 ** Finally, if an error does occur, return an SQLite error code. The final
 28477 ** value of *pp is undefined in this case.
 28478 **
 28479 ** If this function does return a pointer, the caller must eventually 
 28480 ** release the reference by calling unixUnfetch().
 28481 */
 28482 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
 28483 #if SQLITE_MAX_MMAP_SIZE>0
 28484   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
 28485 #endif
 28486   *pp = 0;
 28488 #if SQLITE_MAX_MMAP_SIZE>0
 28489   if( pFd->mmapSizeMax>0 ){
 28490     if( pFd->pMapRegion==0 ){
 28491       int rc = unixMapfile(pFd, -1);
 28492       if( rc!=SQLITE_OK ) return rc;
 28494     if( pFd->mmapSize >= iOff+nAmt ){
 28495       *pp = &((u8 *)pFd->pMapRegion)[iOff];
 28496       pFd->nFetchOut++;
 28499 #endif
 28500   return SQLITE_OK;
 28503 /*
 28504 ** If the third argument is non-NULL, then this function releases a 
 28505 ** reference obtained by an earlier call to unixFetch(). The second
 28506 ** argument passed to this function must be the same as the corresponding
 28507 ** argument that was passed to the unixFetch() invocation. 
 28508 **
 28509 ** Or, if the third argument is NULL, then this function is being called 
 28510 ** to inform the VFS layer that, according to POSIX, any existing mapping 
 28511 ** may now be invalid and should be unmapped.
 28512 */
 28513 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
 28514 #if SQLITE_MAX_MMAP_SIZE>0
 28515   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
 28516   UNUSED_PARAMETER(iOff);
 28518   /* If p==0 (unmap the entire file) then there must be no outstanding 
 28519   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
 28520   ** then there must be at least one outstanding.  */
 28521   assert( (p==0)==(pFd->nFetchOut==0) );
 28523   /* If p!=0, it must match the iOff value. */
 28524   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
 28526   if( p ){
 28527     pFd->nFetchOut--;
 28528   }else{
 28529     unixUnmapfile(pFd);
 28532   assert( pFd->nFetchOut>=0 );
 28533 #else
 28534   UNUSED_PARAMETER(fd);
 28535   UNUSED_PARAMETER(p);
 28536   UNUSED_PARAMETER(iOff);
 28537 #endif
 28538   return SQLITE_OK;
 28541 /*
 28542 ** Here ends the implementation of all sqlite3_file methods.
 28543 **
 28544 ********************** End sqlite3_file Methods *******************************
 28545 ******************************************************************************/
 28547 /*
 28548 ** This division contains definitions of sqlite3_io_methods objects that
 28549 ** implement various file locking strategies.  It also contains definitions
 28550 ** of "finder" functions.  A finder-function is used to locate the appropriate
 28551 ** sqlite3_io_methods object for a particular database file.  The pAppData
 28552 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
 28553 ** the correct finder-function for that VFS.
 28554 **
 28555 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
 28556 ** object.  The only interesting finder-function is autolockIoFinder, which
 28557 ** looks at the filesystem type and tries to guess the best locking
 28558 ** strategy from that.
 28559 **
 28560 ** For finder-funtion F, two objects are created:
 28561 **
 28562 **    (1) The real finder-function named "FImpt()".
 28563 **
 28564 **    (2) A constant pointer to this function named just "F".
 28565 **
 28566 **
 28567 ** A pointer to the F pointer is used as the pAppData value for VFS
 28568 ** objects.  We have to do this instead of letting pAppData point
 28569 ** directly at the finder-function since C90 rules prevent a void*
 28570 ** from be cast into a function pointer.
 28571 **
 28572 **
 28573 ** Each instance of this macro generates two objects:
 28574 **
 28575 **   *  A constant sqlite3_io_methods object call METHOD that has locking
 28576 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
 28577 **
 28578 **   *  An I/O method finder function called FINDER that returns a pointer
 28579 **      to the METHOD object in the previous bullet.
 28580 */
 28581 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
 28582 static const sqlite3_io_methods METHOD = {                                   \
 28583    VERSION,                    /* iVersion */                                \
 28584    CLOSE,                      /* xClose */                                  \
 28585    unixRead,                   /* xRead */                                   \
 28586    unixWrite,                  /* xWrite */                                  \
 28587    unixTruncate,               /* xTruncate */                               \
 28588    unixSync,                   /* xSync */                                   \
 28589    unixFileSize,               /* xFileSize */                               \
 28590    LOCK,                       /* xLock */                                   \
 28591    UNLOCK,                     /* xUnlock */                                 \
 28592    CKLOCK,                     /* xCheckReservedLock */                      \
 28593    unixFileControl,            /* xFileControl */                            \
 28594    unixSectorSize,             /* xSectorSize */                             \
 28595    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
 28596    unixShmMap,                 /* xShmMap */                                 \
 28597    unixShmLock,                /* xShmLock */                                \
 28598    unixShmBarrier,             /* xShmBarrier */                             \
 28599    unixShmUnmap,               /* xShmUnmap */                               \
 28600    unixFetch,                  /* xFetch */                                  \
 28601    unixUnfetch,                /* xUnfetch */                                \
 28602 };                                                                           \
 28603 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
 28604   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
 28605   return &METHOD;                                                            \
 28606 }                                                                            \
 28607 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
 28608     = FINDER##Impl;
 28610 /*
 28611 ** Here are all of the sqlite3_io_methods objects for each of the
 28612 ** locking strategies.  Functions that return pointers to these methods
 28613 ** are also created.
 28614 */
 28615 IOMETHODS(
 28616   posixIoFinder,            /* Finder function name */
 28617   posixIoMethods,           /* sqlite3_io_methods object name */
 28618   3,                        /* shared memory and mmap are enabled */
 28619   unixClose,                /* xClose method */
 28620   unixLock,                 /* xLock method */
 28621   unixUnlock,               /* xUnlock method */
 28622   unixCheckReservedLock     /* xCheckReservedLock method */
 28624 IOMETHODS(
 28625   nolockIoFinder,           /* Finder function name */
 28626   nolockIoMethods,          /* sqlite3_io_methods object name */
 28627   1,                        /* shared memory is disabled */
 28628   nolockClose,              /* xClose method */
 28629   nolockLock,               /* xLock method */
 28630   nolockUnlock,             /* xUnlock method */
 28631   nolockCheckReservedLock   /* xCheckReservedLock method */
 28633 IOMETHODS(
 28634   dotlockIoFinder,          /* Finder function name */
 28635   dotlockIoMethods,         /* sqlite3_io_methods object name */
 28636   1,                        /* shared memory is disabled */
 28637   dotlockClose,             /* xClose method */
 28638   dotlockLock,              /* xLock method */
 28639   dotlockUnlock,            /* xUnlock method */
 28640   dotlockCheckReservedLock  /* xCheckReservedLock method */
 28643 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 28644 IOMETHODS(
 28645   flockIoFinder,            /* Finder function name */
 28646   flockIoMethods,           /* sqlite3_io_methods object name */
 28647   1,                        /* shared memory is disabled */
 28648   flockClose,               /* xClose method */
 28649   flockLock,                /* xLock method */
 28650   flockUnlock,              /* xUnlock method */
 28651   flockCheckReservedLock    /* xCheckReservedLock method */
 28653 #endif
 28655 #if OS_VXWORKS
 28656 IOMETHODS(
 28657   semIoFinder,              /* Finder function name */
 28658   semIoMethods,             /* sqlite3_io_methods object name */
 28659   1,                        /* shared memory is disabled */
 28660   semClose,                 /* xClose method */
 28661   semLock,                  /* xLock method */
 28662   semUnlock,                /* xUnlock method */
 28663   semCheckReservedLock      /* xCheckReservedLock method */
 28665 #endif
 28667 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28668 IOMETHODS(
 28669   afpIoFinder,              /* Finder function name */
 28670   afpIoMethods,             /* sqlite3_io_methods object name */
 28671   1,                        /* shared memory is disabled */
 28672   afpClose,                 /* xClose method */
 28673   afpLock,                  /* xLock method */
 28674   afpUnlock,                /* xUnlock method */
 28675   afpCheckReservedLock      /* xCheckReservedLock method */
 28677 #endif
 28679 /*
 28680 ** The proxy locking method is a "super-method" in the sense that it
 28681 ** opens secondary file descriptors for the conch and lock files and
 28682 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
 28683 ** secondary files.  For this reason, the division that implements
 28684 ** proxy locking is located much further down in the file.  But we need
 28685 ** to go ahead and define the sqlite3_io_methods and finder function
 28686 ** for proxy locking here.  So we forward declare the I/O methods.
 28687 */
 28688 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28689 static int proxyClose(sqlite3_file*);
 28690 static int proxyLock(sqlite3_file*, int);
 28691 static int proxyUnlock(sqlite3_file*, int);
 28692 static int proxyCheckReservedLock(sqlite3_file*, int*);
 28693 IOMETHODS(
 28694   proxyIoFinder,            /* Finder function name */
 28695   proxyIoMethods,           /* sqlite3_io_methods object name */
 28696   1,                        /* shared memory is disabled */
 28697   proxyClose,               /* xClose method */
 28698   proxyLock,                /* xLock method */
 28699   proxyUnlock,              /* xUnlock method */
 28700   proxyCheckReservedLock    /* xCheckReservedLock method */
 28702 #endif
 28704 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
 28705 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28706 IOMETHODS(
 28707   nfsIoFinder,               /* Finder function name */
 28708   nfsIoMethods,              /* sqlite3_io_methods object name */
 28709   1,                         /* shared memory is disabled */
 28710   unixClose,                 /* xClose method */
 28711   unixLock,                  /* xLock method */
 28712   nfsUnlock,                 /* xUnlock method */
 28713   unixCheckReservedLock      /* xCheckReservedLock method */
 28715 #endif
 28717 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28718 /* 
 28719 ** This "finder" function attempts to determine the best locking strategy 
 28720 ** for the database file "filePath".  It then returns the sqlite3_io_methods
 28721 ** object that implements that strategy.
 28722 **
 28723 ** This is for MacOSX only.
 28724 */
 28725 static const sqlite3_io_methods *autolockIoFinderImpl(
 28726   const char *filePath,    /* name of the database file */
 28727   unixFile *pNew           /* open file object for the database file */
 28728 ){
 28729   static const struct Mapping {
 28730     const char *zFilesystem;              /* Filesystem type name */
 28731     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
 28732   } aMap[] = {
 28733     { "hfs",    &posixIoMethods },
 28734     { "ufs",    &posixIoMethods },
 28735     { "afpfs",  &afpIoMethods },
 28736     { "smbfs",  &afpIoMethods },
 28737     { "webdav", &nolockIoMethods },
 28738     { 0, 0 }
 28739   };
 28740   int i;
 28741   struct statfs fsInfo;
 28742   struct flock lockInfo;
 28744   if( !filePath ){
 28745     /* If filePath==NULL that means we are dealing with a transient file
 28746     ** that does not need to be locked. */
 28747     return &nolockIoMethods;
 28749   if( statfs(filePath, &fsInfo) != -1 ){
 28750     if( fsInfo.f_flags & MNT_RDONLY ){
 28751       return &nolockIoMethods;
 28753     for(i=0; aMap[i].zFilesystem; i++){
 28754       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
 28755         return aMap[i].pMethods;
 28760   /* Default case. Handles, amongst others, "nfs".
 28761   ** Test byte-range lock using fcntl(). If the call succeeds, 
 28762   ** assume that the file-system supports POSIX style locks. 
 28763   */
 28764   lockInfo.l_len = 1;
 28765   lockInfo.l_start = 0;
 28766   lockInfo.l_whence = SEEK_SET;
 28767   lockInfo.l_type = F_RDLCK;
 28768   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 28769     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
 28770       return &nfsIoMethods;
 28771     } else {
 28772       return &posixIoMethods;
 28774   }else{
 28775     return &dotlockIoMethods;
 28778 static const sqlite3_io_methods 
 28779   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 28781 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 28783 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
 28784 /* 
 28785 ** This "finder" function attempts to determine the best locking strategy 
 28786 ** for the database file "filePath".  It then returns the sqlite3_io_methods
 28787 ** object that implements that strategy.
 28788 **
 28789 ** This is for VXWorks only.
 28790 */
 28791 static const sqlite3_io_methods *autolockIoFinderImpl(
 28792   const char *filePath,    /* name of the database file */
 28793   unixFile *pNew           /* the open file object */
 28794 ){
 28795   struct flock lockInfo;
 28797   if( !filePath ){
 28798     /* If filePath==NULL that means we are dealing with a transient file
 28799     ** that does not need to be locked. */
 28800     return &nolockIoMethods;
 28803   /* Test if fcntl() is supported and use POSIX style locks.
 28804   ** Otherwise fall back to the named semaphore method.
 28805   */
 28806   lockInfo.l_len = 1;
 28807   lockInfo.l_start = 0;
 28808   lockInfo.l_whence = SEEK_SET;
 28809   lockInfo.l_type = F_RDLCK;
 28810   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 28811     return &posixIoMethods;
 28812   }else{
 28813     return &semIoMethods;
 28816 static const sqlite3_io_methods 
 28817   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
 28819 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
 28821 /*
 28822 ** An abstract type for a pointer to a IO method finder function:
 28823 */
 28824 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
 28827 /****************************************************************************
 28828 **************************** sqlite3_vfs methods ****************************
 28829 **
 28830 ** This division contains the implementation of methods on the
 28831 ** sqlite3_vfs object.
 28832 */
 28834 /*
 28835 ** Initialize the contents of the unixFile structure pointed to by pId.
 28836 */
 28837 static int fillInUnixFile(
 28838   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
 28839   int h,                  /* Open file descriptor of file being opened */
 28840   sqlite3_file *pId,      /* Write to the unixFile structure here */
 28841   const char *zFilename,  /* Name of the file being opened */
 28842   int ctrlFlags           /* Zero or more UNIXFILE_* values */
 28843 ){
 28844   const sqlite3_io_methods *pLockingStyle;
 28845   unixFile *pNew = (unixFile *)pId;
 28846   int rc = SQLITE_OK;
 28848   assert( pNew->pInode==NULL );
 28850   /* Usually the path zFilename should not be a relative pathname. The
 28851   ** exception is when opening the proxy "conch" file in builds that
 28852   ** include the special Apple locking styles.
 28853   */
 28854 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28855   assert( zFilename==0 || zFilename[0]=='/' 
 28856     || pVfs->pAppData==(void*)&autolockIoFinder );
 28857 #else
 28858   assert( zFilename==0 || zFilename[0]=='/' );
 28859 #endif
 28861   /* No locking occurs in temporary files */
 28862   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
 28864   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 28865   pNew->h = h;
 28866   pNew->pVfs = pVfs;
 28867   pNew->zPath = zFilename;
 28868   pNew->ctrlFlags = (u8)ctrlFlags;
 28869 #if SQLITE_MAX_MMAP_SIZE>0
 28870   pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 28871 #endif
 28872   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
 28873                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 28874     pNew->ctrlFlags |= UNIXFILE_PSOW;
 28876   if( strcmp(pVfs->zName,"unix-excl")==0 ){
 28877     pNew->ctrlFlags |= UNIXFILE_EXCL;
 28880 #if OS_VXWORKS
 28881   pNew->pId = vxworksFindFileId(zFilename);
 28882   if( pNew->pId==0 ){
 28883     ctrlFlags |= UNIXFILE_NOLOCK;
 28884     rc = SQLITE_NOMEM;
 28886 #endif
 28888   if( ctrlFlags & UNIXFILE_NOLOCK ){
 28889     pLockingStyle = &nolockIoMethods;
 28890   }else{
 28891     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
 28892 #if SQLITE_ENABLE_LOCKING_STYLE
 28893     /* Cache zFilename in the locking context (AFP and dotlock override) for
 28894     ** proxyLock activation is possible (remote proxy is based on db name)
 28895     ** zFilename remains valid until file is closed, to support */
 28896     pNew->lockingContext = (void*)zFilename;
 28897 #endif
 28900   if( pLockingStyle == &posixIoMethods
 28901 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 28902     || pLockingStyle == &nfsIoMethods
 28903 #endif
 28904   ){
 28905     unixEnterMutex();
 28906     rc = findInodeInfo(pNew, &pNew->pInode);
 28907     if( rc!=SQLITE_OK ){
 28908       /* If an error occurred in findInodeInfo(), close the file descriptor
 28909       ** immediately, before releasing the mutex. findInodeInfo() may fail
 28910       ** in two scenarios:
 28911       **
 28912       **   (a) A call to fstat() failed.
 28913       **   (b) A malloc failed.
 28914       **
 28915       ** Scenario (b) may only occur if the process is holding no other
 28916       ** file descriptors open on the same file. If there were other file
 28917       ** descriptors on this file, then no malloc would be required by
 28918       ** findInodeInfo(). If this is the case, it is quite safe to close
 28919       ** handle h - as it is guaranteed that no posix locks will be released
 28920       ** by doing so.
 28921       **
 28922       ** If scenario (a) caused the error then things are not so safe. The
 28923       ** implicit assumption here is that if fstat() fails, things are in
 28924       ** such bad shape that dropping a lock or two doesn't matter much.
 28925       */
 28926       robust_close(pNew, h, __LINE__);
 28927       h = -1;
 28929     unixLeaveMutex();
 28932 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 28933   else if( pLockingStyle == &afpIoMethods ){
 28934     /* AFP locking uses the file path so it needs to be included in
 28935     ** the afpLockingContext.
 28936     */
 28937     afpLockingContext *pCtx;
 28938     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
 28939     if( pCtx==0 ){
 28940       rc = SQLITE_NOMEM;
 28941     }else{
 28942       /* NB: zFilename exists and remains valid until the file is closed
 28943       ** according to requirement F11141.  So we do not need to make a
 28944       ** copy of the filename. */
 28945       pCtx->dbPath = zFilename;
 28946       pCtx->reserved = 0;
 28947       srandomdev();
 28948       unixEnterMutex();
 28949       rc = findInodeInfo(pNew, &pNew->pInode);
 28950       if( rc!=SQLITE_OK ){
 28951         sqlite3_free(pNew->lockingContext);
 28952         robust_close(pNew, h, __LINE__);
 28953         h = -1;
 28955       unixLeaveMutex();        
 28958 #endif
 28960   else if( pLockingStyle == &dotlockIoMethods ){
 28961     /* Dotfile locking uses the file path so it needs to be included in
 28962     ** the dotlockLockingContext 
 28963     */
 28964     char *zLockFile;
 28965     int nFilename;
 28966     assert( zFilename!=0 );
 28967     nFilename = (int)strlen(zFilename) + 6;
 28968     zLockFile = (char *)sqlite3_malloc(nFilename);
 28969     if( zLockFile==0 ){
 28970       rc = SQLITE_NOMEM;
 28971     }else{
 28972       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
 28974     pNew->lockingContext = zLockFile;
 28977 #if OS_VXWORKS
 28978   else if( pLockingStyle == &semIoMethods ){
 28979     /* Named semaphore locking uses the file path so it needs to be
 28980     ** included in the semLockingContext
 28981     */
 28982     unixEnterMutex();
 28983     rc = findInodeInfo(pNew, &pNew->pInode);
 28984     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
 28985       char *zSemName = pNew->pInode->aSemName;
 28986       int n;
 28987       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
 28988                        pNew->pId->zCanonicalName);
 28989       for( n=1; zSemName[n]; n++ )
 28990         if( zSemName[n]=='/' ) zSemName[n] = '_';
 28991       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
 28992       if( pNew->pInode->pSem == SEM_FAILED ){
 28993         rc = SQLITE_NOMEM;
 28994         pNew->pInode->aSemName[0] = '\0';
 28997     unixLeaveMutex();
 28999 #endif
 29001   pNew->lastErrno = 0;
 29002 #if OS_VXWORKS
 29003   if( rc!=SQLITE_OK ){
 29004     if( h>=0 ) robust_close(pNew, h, __LINE__);
 29005     h = -1;
 29006     osUnlink(zFilename);
 29007     pNew->ctrlFlags |= UNIXFILE_DELETE;
 29009 #endif
 29010   if( rc!=SQLITE_OK ){
 29011     if( h>=0 ) robust_close(pNew, h, __LINE__);
 29012   }else{
 29013     pNew->pMethod = pLockingStyle;
 29014     OpenCounter(+1);
 29015     verifyDbFile(pNew);
 29017   return rc;
 29020 /*
 29021 ** Return the name of a directory in which to put temporary files.
 29022 ** If no suitable temporary file directory can be found, return NULL.
 29023 */
 29024 static const char *unixTempFileDir(void){
 29025   static const char *azDirs[] = {
 29026      0,
 29027      0,
 29028      0,
 29029      "/var/tmp",
 29030      "/usr/tmp",
 29031      "/tmp",
 29032      0        /* List terminator */
 29033   };
 29034   unsigned int i;
 29035   struct stat buf;
 29036   const char *zDir = 0;
 29038   azDirs[0] = sqlite3_temp_directory;
 29039   if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
 29040   if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
 29041   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
 29042     if( zDir==0 ) continue;
 29043     if( osStat(zDir, &buf) ) continue;
 29044     if( !S_ISDIR(buf.st_mode) ) continue;
 29045     if( osAccess(zDir, 07) ) continue;
 29046     break;
 29048   return zDir;
 29051 /*
 29052 ** Create a temporary file name in zBuf.  zBuf must be allocated
 29053 ** by the calling process and must be big enough to hold at least
 29054 ** pVfs->mxPathname bytes.
 29055 */
 29056 static int unixGetTempname(int nBuf, char *zBuf){
 29057   static const unsigned char zChars[] =
 29058     "abcdefghijklmnopqrstuvwxyz"
 29059     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 29060     "0123456789";
 29061   unsigned int i, j;
 29062   const char *zDir;
 29064   /* It's odd to simulate an io-error here, but really this is just
 29065   ** using the io-error infrastructure to test that SQLite handles this
 29066   ** function failing. 
 29067   */
 29068   SimulateIOError( return SQLITE_IOERR );
 29070   zDir = unixTempFileDir();
 29071   if( zDir==0 ) zDir = ".";
 29073   /* Check that the output buffer is large enough for the temporary file 
 29074   ** name. If it is not, return SQLITE_ERROR.
 29075   */
 29076   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
 29077     return SQLITE_ERROR;
 29080   do{
 29081     sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
 29082     j = (int)strlen(zBuf);
 29083     sqlite3_randomness(15, &zBuf[j]);
 29084     for(i=0; i<15; i++, j++){
 29085       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 29087     zBuf[j] = 0;
 29088     zBuf[j+1] = 0;
 29089   }while( osAccess(zBuf,0)==0 );
 29090   return SQLITE_OK;
 29093 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 29094 /*
 29095 ** Routine to transform a unixFile into a proxy-locking unixFile.
 29096 ** Implementation in the proxy-lock division, but used by unixOpen()
 29097 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
 29098 */
 29099 static int proxyTransformUnixFile(unixFile*, const char*);
 29100 #endif
 29102 /*
 29103 ** Search for an unused file descriptor that was opened on the database 
 29104 ** file (not a journal or master-journal file) identified by pathname
 29105 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
 29106 ** argument to this function.
 29107 **
 29108 ** Such a file descriptor may exist if a database connection was closed
 29109 ** but the associated file descriptor could not be closed because some
 29110 ** other file descriptor open on the same file is holding a file-lock.
 29111 ** Refer to comments in the unixClose() function and the lengthy comment
 29112 ** describing "Posix Advisory Locking" at the start of this file for 
 29113 ** further details. Also, ticket #4018.
 29114 **
 29115 ** If a suitable file descriptor is found, then it is returned. If no
 29116 ** such file descriptor is located, -1 is returned.
 29117 */
 29118 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
 29119   UnixUnusedFd *pUnused = 0;
 29121   /* Do not search for an unused file descriptor on vxworks. Not because
 29122   ** vxworks would not benefit from the change (it might, we're not sure),
 29123   ** but because no way to test it is currently available. It is better 
 29124   ** not to risk breaking vxworks support for the sake of such an obscure 
 29125   ** feature.  */
 29126 #if !OS_VXWORKS
 29127   struct stat sStat;                   /* Results of stat() call */
 29129   /* A stat() call may fail for various reasons. If this happens, it is
 29130   ** almost certain that an open() call on the same path will also fail.
 29131   ** For this reason, if an error occurs in the stat() call here, it is
 29132   ** ignored and -1 is returned. The caller will try to open a new file
 29133   ** descriptor on the same path, fail, and return an error to SQLite.
 29134   **
 29135   ** Even if a subsequent open() call does succeed, the consequences of
 29136   ** not searching for a resusable file descriptor are not dire.  */
 29137   if( 0==osStat(zPath, &sStat) ){
 29138     unixInodeInfo *pInode;
 29140     unixEnterMutex();
 29141     pInode = inodeList;
 29142     while( pInode && (pInode->fileId.dev!=sStat.st_dev
 29143                      || pInode->fileId.ino!=sStat.st_ino) ){
 29144        pInode = pInode->pNext;
 29146     if( pInode ){
 29147       UnixUnusedFd **pp;
 29148       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
 29149       pUnused = *pp;
 29150       if( pUnused ){
 29151         *pp = pUnused->pNext;
 29154     unixLeaveMutex();
 29156 #endif    /* if !OS_VXWORKS */
 29157   return pUnused;
 29160 /*
 29161 ** This function is called by unixOpen() to determine the unix permissions
 29162 ** to create new files with. If no error occurs, then SQLITE_OK is returned
 29163 ** and a value suitable for passing as the third argument to open(2) is
 29164 ** written to *pMode. If an IO error occurs, an SQLite error code is 
 29165 ** returned and the value of *pMode is not modified.
 29166 **
 29167 ** In most cases cases, this routine sets *pMode to 0, which will become
 29168 ** an indication to robust_open() to create the file using
 29169 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
 29170 ** But if the file being opened is a WAL or regular journal file, then 
 29171 ** this function queries the file-system for the permissions on the 
 29172 ** corresponding database file and sets *pMode to this value. Whenever 
 29173 ** possible, WAL and journal files are created using the same permissions 
 29174 ** as the associated database file.
 29175 **
 29176 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
 29177 ** original filename is unavailable.  But 8_3_NAMES is only used for
 29178 ** FAT filesystems and permissions do not matter there, so just use
 29179 ** the default permissions.
 29180 */
 29181 static int findCreateFileMode(
 29182   const char *zPath,              /* Path of file (possibly) being created */
 29183   int flags,                      /* Flags passed as 4th argument to xOpen() */
 29184   mode_t *pMode,                  /* OUT: Permissions to open file with */
 29185   uid_t *pUid,                    /* OUT: uid to set on the file */
 29186   gid_t *pGid                     /* OUT: gid to set on the file */
 29187 ){
 29188   int rc = SQLITE_OK;             /* Return Code */
 29189   *pMode = 0;
 29190   *pUid = 0;
 29191   *pGid = 0;
 29192   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 29193     char zDb[MAX_PATHNAME+1];     /* Database file path */
 29194     int nDb;                      /* Number of valid bytes in zDb */
 29195     struct stat sStat;            /* Output of stat() on database file */
 29197     /* zPath is a path to a WAL or journal file. The following block derives
 29198     ** the path to the associated database file from zPath. This block handles
 29199     ** the following naming conventions:
 29200     **
 29201     **   "<path to db>-journal"
 29202     **   "<path to db>-wal"
 29203     **   "<path to db>-journalNN"
 29204     **   "<path to db>-walNN"
 29205     **
 29206     ** where NN is a decimal number. The NN naming schemes are 
 29207     ** used by the test_multiplex.c module.
 29208     */
 29209     nDb = sqlite3Strlen30(zPath) - 1; 
 29210 #ifdef SQLITE_ENABLE_8_3_NAMES
 29211     while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
 29212     if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
 29213 #else
 29214     while( zPath[nDb]!='-' ){
 29215       assert( nDb>0 );
 29216       assert( zPath[nDb]!='\n' );
 29217       nDb--;
 29219 #endif
 29220     memcpy(zDb, zPath, nDb);
 29221     zDb[nDb] = '\0';
 29223     if( 0==osStat(zDb, &sStat) ){
 29224       *pMode = sStat.st_mode & 0777;
 29225       *pUid = sStat.st_uid;
 29226       *pGid = sStat.st_gid;
 29227     }else{
 29228       rc = SQLITE_IOERR_FSTAT;
 29230   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 29231     *pMode = 0600;
 29233   return rc;
 29236 /*
 29237 ** Open the file zPath.
 29238 ** 
 29239 ** Previously, the SQLite OS layer used three functions in place of this
 29240 ** one:
 29241 **
 29242 **     sqlite3OsOpenReadWrite();
 29243 **     sqlite3OsOpenReadOnly();
 29244 **     sqlite3OsOpenExclusive();
 29245 **
 29246 ** These calls correspond to the following combinations of flags:
 29247 **
 29248 **     ReadWrite() ->     (READWRITE | CREATE)
 29249 **     ReadOnly()  ->     (READONLY) 
 29250 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
 29251 **
 29252 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
 29253 ** true, the file was configured to be automatically deleted when the
 29254 ** file handle closed. To achieve the same effect using this new 
 29255 ** interface, add the DELETEONCLOSE flag to those specified above for 
 29256 ** OpenExclusive().
 29257 */
 29258 static int unixOpen(
 29259   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
 29260   const char *zPath,           /* Pathname of file to be opened */
 29261   sqlite3_file *pFile,         /* The file descriptor to be filled in */
 29262   int flags,                   /* Input flags to control the opening */
 29263   int *pOutFlags               /* Output flags returned to SQLite core */
 29264 ){
 29265   unixFile *p = (unixFile *)pFile;
 29266   int fd = -1;                   /* File descriptor returned by open() */
 29267   int openFlags = 0;             /* Flags to pass to open() */
 29268   int eType = flags&0xFFFFFF00;  /* Type of file to open */
 29269   int noLock;                    /* True to omit locking primitives */
 29270   int rc = SQLITE_OK;            /* Function Return Code */
 29271   int ctrlFlags = 0;             /* UNIXFILE_* flags */
 29273   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 29274   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 29275   int isCreate     = (flags & SQLITE_OPEN_CREATE);
 29276   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 29277   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 29278 #if SQLITE_ENABLE_LOCKING_STYLE
 29279   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
 29280 #endif
 29281 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 29282   struct statfs fsInfo;
 29283 #endif
 29285   /* If creating a master or main-file journal, this function will open
 29286   ** a file-descriptor on the directory too. The first time unixSync()
 29287   ** is called the directory file descriptor will be fsync()ed and close()d.
 29288   */
 29289   int syncDir = (isCreate && (
 29290         eType==SQLITE_OPEN_MASTER_JOURNAL 
 29291      || eType==SQLITE_OPEN_MAIN_JOURNAL 
 29292      || eType==SQLITE_OPEN_WAL
 29293   ));
 29295   /* If argument zPath is a NULL pointer, this function is required to open
 29296   ** a temporary file. Use this buffer to store the file name in.
 29297   */
 29298   char zTmpname[MAX_PATHNAME+2];
 29299   const char *zName = zPath;
 29301   /* Check the following statements are true: 
 29302   **
 29303   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 29304   **   (b) if CREATE is set, then READWRITE must also be set, and
 29305   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 29306   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 29307   */
 29308   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 29309   assert(isCreate==0 || isReadWrite);
 29310   assert(isExclusive==0 || isCreate);
 29311   assert(isDelete==0 || isCreate);
 29313   /* The main DB, main journal, WAL file and master journal are never 
 29314   ** automatically deleted. Nor are they ever temporary files.  */
 29315   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 29316   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 29317   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 29318   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 29320   /* Assert that the upper layer has set one of the "file-type" flags. */
 29321   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 29322        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 29323        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 29324        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 29325   );
 29327   /* Detect a pid change and reset the PRNG.  There is a race condition
 29328   ** here such that two or more threads all trying to open databases at
 29329   ** the same instant might all reset the PRNG.  But multiple resets
 29330   ** are harmless.
 29331   */
 29332   if( randomnessPid!=getpid() ){
 29333     randomnessPid = getpid();
 29334     sqlite3_randomness(0,0);
 29337   memset(p, 0, sizeof(unixFile));
 29339   if( eType==SQLITE_OPEN_MAIN_DB ){
 29340     UnixUnusedFd *pUnused;
 29341     pUnused = findReusableFd(zName, flags);
 29342     if( pUnused ){
 29343       fd = pUnused->fd;
 29344     }else{
 29345       pUnused = sqlite3_malloc(sizeof(*pUnused));
 29346       if( !pUnused ){
 29347         return SQLITE_NOMEM;
 29350     p->pUnused = pUnused;
 29352     /* Database filenames are double-zero terminated if they are not
 29353     ** URIs with parameters.  Hence, they can always be passed into
 29354     ** sqlite3_uri_parameter(). */
 29355     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
 29357   }else if( !zName ){
 29358     /* If zName is NULL, the upper layer is requesting a temp file. */
 29359     assert(isDelete && !syncDir);
 29360     rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
 29361     if( rc!=SQLITE_OK ){
 29362       return rc;
 29364     zName = zTmpname;
 29366     /* Generated temporary filenames are always double-zero terminated
 29367     ** for use by sqlite3_uri_parameter(). */
 29368     assert( zName[strlen(zName)+1]==0 );
 29371   /* Determine the value of the flags parameter passed to POSIX function
 29372   ** open(). These must be calculated even if open() is not called, as
 29373   ** they may be stored as part of the file handle and used by the 
 29374   ** 'conch file' locking functions later on.  */
 29375   if( isReadonly )  openFlags |= O_RDONLY;
 29376   if( isReadWrite ) openFlags |= O_RDWR;
 29377   if( isCreate )    openFlags |= O_CREAT;
 29378   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
 29379   openFlags |= (O_LARGEFILE|O_BINARY);
 29381   if( fd<0 ){
 29382     mode_t openMode;              /* Permissions to create file with */
 29383     uid_t uid;                    /* Userid for the file */
 29384     gid_t gid;                    /* Groupid for the file */
 29385     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
 29386     if( rc!=SQLITE_OK ){
 29387       assert( !p->pUnused );
 29388       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 29389       return rc;
 29391     fd = robust_open(zName, openFlags, openMode);
 29392     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 29393     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
 29394       /* Failed to open the file for read/write access. Try read-only. */
 29395       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
 29396       openFlags &= ~(O_RDWR|O_CREAT);
 29397       flags |= SQLITE_OPEN_READONLY;
 29398       openFlags |= O_RDONLY;
 29399       isReadonly = 1;
 29400       fd = robust_open(zName, openFlags, openMode);
 29402     if( fd<0 ){
 29403       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
 29404       goto open_finished;
 29407     /* If this process is running as root and if creating a new rollback
 29408     ** journal or WAL file, set the ownership of the journal or WAL to be
 29409     ** the same as the original database.
 29410     */
 29411     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
 29412       osFchown(fd, uid, gid);
 29415   assert( fd>=0 );
 29416   if( pOutFlags ){
 29417     *pOutFlags = flags;
 29420   if( p->pUnused ){
 29421     p->pUnused->fd = fd;
 29422     p->pUnused->flags = flags;
 29425   if( isDelete ){
 29426 #if OS_VXWORKS
 29427     zPath = zName;
 29428 #else
 29429     osUnlink(zName);
 29430 #endif
 29432 #if SQLITE_ENABLE_LOCKING_STYLE
 29433   else{
 29434     p->openFlags = openFlags;
 29436 #endif
 29438   noLock = eType!=SQLITE_OPEN_MAIN_DB;
 29441 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 29442   if( fstatfs(fd, &fsInfo) == -1 ){
 29443     ((unixFile*)pFile)->lastErrno = errno;
 29444     robust_close(p, fd, __LINE__);
 29445     return SQLITE_IOERR_ACCESS;
 29447   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
 29448     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
 29450 #endif
 29452   /* Set up appropriate ctrlFlags */
 29453   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
 29454   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
 29455   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
 29456   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
 29457   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
 29459 #if SQLITE_ENABLE_LOCKING_STYLE
 29460 #if SQLITE_PREFER_PROXY_LOCKING
 29461   isAutoProxy = 1;
 29462 #endif
 29463   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
 29464     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
 29465     int useProxy = 0;
 29467     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
 29468     ** never use proxy, NULL means use proxy for non-local files only.  */
 29469     if( envforce!=NULL ){
 29470       useProxy = atoi(envforce)>0;
 29471     }else{
 29472       if( statfs(zPath, &fsInfo) == -1 ){
 29473         /* In theory, the close(fd) call is sub-optimal. If the file opened
 29474         ** with fd is a database file, and there are other connections open
 29475         ** on that file that are currently holding advisory locks on it,
 29476         ** then the call to close() will cancel those locks. In practice,
 29477         ** we're assuming that statfs() doesn't fail very often. At least
 29478         ** not while other file descriptors opened by the same process on
 29479         ** the same file are working.  */
 29480         p->lastErrno = errno;
 29481         robust_close(p, fd, __LINE__);
 29482         rc = SQLITE_IOERR_ACCESS;
 29483         goto open_finished;
 29485       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
 29487     if( useProxy ){
 29488       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 29489       if( rc==SQLITE_OK ){
 29490         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
 29491         if( rc!=SQLITE_OK ){
 29492           /* Use unixClose to clean up the resources added in fillInUnixFile 
 29493           ** and clear all the structure's references.  Specifically, 
 29494           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
 29495           */
 29496           unixClose(pFile);
 29497           return rc;
 29500       goto open_finished;
 29503 #endif
 29505   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 29507 open_finished:
 29508   if( rc!=SQLITE_OK ){
 29509     sqlite3_free(p->pUnused);
 29511   return rc;
 29515 /*
 29516 ** Delete the file at zPath. If the dirSync argument is true, fsync()
 29517 ** the directory after deleting the file.
 29518 */
 29519 static int unixDelete(
 29520   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
 29521   const char *zPath,        /* Name of file to be deleted */
 29522   int dirSync               /* If true, fsync() directory after deleting file */
 29523 ){
 29524   int rc = SQLITE_OK;
 29525   UNUSED_PARAMETER(NotUsed);
 29526   SimulateIOError(return SQLITE_IOERR_DELETE);
 29527   if( osUnlink(zPath)==(-1) ){
 29528     if( errno==ENOENT ){
 29529       rc = SQLITE_IOERR_DELETE_NOENT;
 29530     }else{
 29531       rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
 29533     return rc;
 29535 #ifndef SQLITE_DISABLE_DIRSYNC
 29536   if( (dirSync & 1)!=0 ){
 29537     int fd;
 29538     rc = osOpenDirectory(zPath, &fd);
 29539     if( rc==SQLITE_OK ){
 29540 #if OS_VXWORKS
 29541       if( fsync(fd)==-1 )
 29542 #else
 29543       if( fsync(fd) )
 29544 #endif
 29546         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
 29548       robust_close(0, fd, __LINE__);
 29549     }else if( rc==SQLITE_CANTOPEN ){
 29550       rc = SQLITE_OK;
 29553 #endif
 29554   return rc;
 29557 /*
 29558 ** Test the existence of or access permissions of file zPath. The
 29559 ** test performed depends on the value of flags:
 29560 **
 29561 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
 29562 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
 29563 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
 29564 **
 29565 ** Otherwise return 0.
 29566 */
 29567 static int unixAccess(
 29568   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
 29569   const char *zPath,      /* Path of the file to examine */
 29570   int flags,              /* What do we want to learn about the zPath file? */
 29571   int *pResOut            /* Write result boolean here */
 29572 ){
 29573   int amode = 0;
 29574   UNUSED_PARAMETER(NotUsed);
 29575   SimulateIOError( return SQLITE_IOERR_ACCESS; );
 29576   switch( flags ){
 29577     case SQLITE_ACCESS_EXISTS:
 29578       amode = F_OK;
 29579       break;
 29580     case SQLITE_ACCESS_READWRITE:
 29581       amode = W_OK|R_OK;
 29582       break;
 29583     case SQLITE_ACCESS_READ:
 29584       amode = R_OK;
 29585       break;
 29587     default:
 29588       assert(!"Invalid flags argument");
 29590   *pResOut = (osAccess(zPath, amode)==0);
 29591   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
 29592     struct stat buf;
 29593     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
 29594       *pResOut = 0;
 29597   return SQLITE_OK;
 29601 /*
 29602 ** Turn a relative pathname into a full pathname. The relative path
 29603 ** is stored as a nul-terminated string in the buffer pointed to by
 29604 ** zPath. 
 29605 **
 29606 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
 29607 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
 29608 ** this buffer before returning.
 29609 */
 29610 static int unixFullPathname(
 29611   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 29612   const char *zPath,            /* Possibly relative input path */
 29613   int nOut,                     /* Size of output buffer in bytes */
 29614   char *zOut                    /* Output buffer */
 29615 ){
 29617   /* It's odd to simulate an io-error here, but really this is just
 29618   ** using the io-error infrastructure to test that SQLite handles this
 29619   ** function failing. This function could fail if, for example, the
 29620   ** current working directory has been unlinked.
 29621   */
 29622   SimulateIOError( return SQLITE_ERROR );
 29624   assert( pVfs->mxPathname==MAX_PATHNAME );
 29625   UNUSED_PARAMETER(pVfs);
 29627   zOut[nOut-1] = '\0';
 29628   if( zPath[0]=='/' ){
 29629     sqlite3_snprintf(nOut, zOut, "%s", zPath);
 29630   }else{
 29631     int nCwd;
 29632     if( osGetcwd(zOut, nOut-1)==0 ){
 29633       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
 29635     nCwd = (int)strlen(zOut);
 29636     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
 29638   return SQLITE_OK;
 29642 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 29643 /*
 29644 ** Interfaces for opening a shared library, finding entry points
 29645 ** within the shared library, and closing the shared library.
 29646 */
 29647 #include <dlfcn.h>
 29648 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
 29649   UNUSED_PARAMETER(NotUsed);
 29650   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
 29653 /*
 29654 ** SQLite calls this function immediately after a call to unixDlSym() or
 29655 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
 29656 ** message is available, it is written to zBufOut. If no error message
 29657 ** is available, zBufOut is left unmodified and SQLite uses a default
 29658 ** error message.
 29659 */
 29660 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
 29661   const char *zErr;
 29662   UNUSED_PARAMETER(NotUsed);
 29663   unixEnterMutex();
 29664   zErr = dlerror();
 29665   if( zErr ){
 29666     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
 29668   unixLeaveMutex();
 29670 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
 29671   /* 
 29672   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
 29673   ** cast into a pointer to a function.  And yet the library dlsym() routine
 29674   ** returns a void* which is really a pointer to a function.  So how do we
 29675   ** use dlsym() with -pedantic-errors?
 29676   **
 29677   ** Variable x below is defined to be a pointer to a function taking
 29678   ** parameters void* and const char* and returning a pointer to a function.
 29679   ** We initialize x by assigning it a pointer to the dlsym() function.
 29680   ** (That assignment requires a cast.)  Then we call the function that
 29681   ** x points to.  
 29682   **
 29683   ** This work-around is unlikely to work correctly on any system where
 29684   ** you really cannot cast a function pointer into void*.  But then, on the
 29685   ** other hand, dlsym() will not work on such a system either, so we have
 29686   ** not really lost anything.
 29687   */
 29688   void (*(*x)(void*,const char*))(void);
 29689   UNUSED_PARAMETER(NotUsed);
 29690   x = (void(*(*)(void*,const char*))(void))dlsym;
 29691   return (*x)(p, zSym);
 29693 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
 29694   UNUSED_PARAMETER(NotUsed);
 29695   dlclose(pHandle);
 29697 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 29698   #define unixDlOpen  0
 29699   #define unixDlError 0
 29700   #define unixDlSym   0
 29701   #define unixDlClose 0
 29702 #endif
 29704 /*
 29705 ** Write nBuf bytes of random data to the supplied buffer zBuf.
 29706 */
 29707 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
 29708   UNUSED_PARAMETER(NotUsed);
 29709   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
 29711   /* We have to initialize zBuf to prevent valgrind from reporting
 29712   ** errors.  The reports issued by valgrind are incorrect - we would
 29713   ** prefer that the randomness be increased by making use of the
 29714   ** uninitialized space in zBuf - but valgrind errors tend to worry
 29715   ** some users.  Rather than argue, it seems easier just to initialize
 29716   ** the whole array and silence valgrind, even if that means less randomness
 29717   ** in the random seed.
 29718   **
 29719   ** When testing, initializing zBuf[] to zero is all we do.  That means
 29720   ** that we always use the same random number sequence.  This makes the
 29721   ** tests repeatable.
 29722   */
 29723   memset(zBuf, 0, nBuf);
 29724   randomnessPid = getpid();  
 29725 #if !defined(SQLITE_TEST)
 29727     int fd, got;
 29728     fd = robust_open("/dev/urandom", O_RDONLY, 0);
 29729     if( fd<0 ){
 29730       time_t t;
 29731       time(&t);
 29732       memcpy(zBuf, &t, sizeof(t));
 29733       memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
 29734       assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
 29735       nBuf = sizeof(t) + sizeof(randomnessPid);
 29736     }else{
 29737       do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
 29738       robust_close(0, fd, __LINE__);
 29741 #endif
 29742   return nBuf;
 29746 /*
 29747 ** Sleep for a little while.  Return the amount of time slept.
 29748 ** The argument is the number of microseconds we want to sleep.
 29749 ** The return value is the number of microseconds of sleep actually
 29750 ** requested from the underlying operating system, a number which
 29751 ** might be greater than or equal to the argument, but not less
 29752 ** than the argument.
 29753 */
 29754 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
 29755 #if OS_VXWORKS
 29756   struct timespec sp;
 29758   sp.tv_sec = microseconds / 1000000;
 29759   sp.tv_nsec = (microseconds % 1000000) * 1000;
 29760   nanosleep(&sp, NULL);
 29761   UNUSED_PARAMETER(NotUsed);
 29762   return microseconds;
 29763 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
 29764   usleep(microseconds);
 29765   UNUSED_PARAMETER(NotUsed);
 29766   return microseconds;
 29767 #else
 29768   int seconds = (microseconds+999999)/1000000;
 29769   sleep(seconds);
 29770   UNUSED_PARAMETER(NotUsed);
 29771   return seconds*1000000;
 29772 #endif
 29775 /*
 29776 ** The following variable, if set to a non-zero value, is interpreted as
 29777 ** the number of seconds since 1970 and is used to set the result of
 29778 ** sqlite3OsCurrentTime() during testing.
 29779 */
 29780 #ifdef SQLITE_TEST
 29781 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 29782 #endif
 29784 /*
 29785 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 29786 ** the current time and date as a Julian Day number times 86_400_000.  In
 29787 ** other words, write into *piNow the number of milliseconds since the Julian
 29788 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 29789 ** proleptic Gregorian calendar.
 29790 **
 29791 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 29792 ** cannot be found.
 29793 */
 29794 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
 29795   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 29796   int rc = SQLITE_OK;
 29797 #if defined(NO_GETTOD)
 29798   time_t t;
 29799   time(&t);
 29800   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
 29801 #elif OS_VXWORKS
 29802   struct timespec sNow;
 29803   clock_gettime(CLOCK_REALTIME, &sNow);
 29804   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
 29805 #else
 29806   struct timeval sNow;
 29807   if( gettimeofday(&sNow, 0)==0 ){
 29808     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
 29809   }else{
 29810     rc = SQLITE_ERROR;
 29812 #endif
 29814 #ifdef SQLITE_TEST
 29815   if( sqlite3_current_time ){
 29816     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 29818 #endif
 29819   UNUSED_PARAMETER(NotUsed);
 29820   return rc;
 29823 /*
 29824 ** Find the current time (in Universal Coordinated Time).  Write the
 29825 ** current time and date as a Julian Day number into *prNow and
 29826 ** return 0.  Return 1 if the time and date cannot be found.
 29827 */
 29828 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
 29829   sqlite3_int64 i = 0;
 29830   int rc;
 29831   UNUSED_PARAMETER(NotUsed);
 29832   rc = unixCurrentTimeInt64(0, &i);
 29833   *prNow = i/86400000.0;
 29834   return rc;
 29837 /*
 29838 ** We added the xGetLastError() method with the intention of providing
 29839 ** better low-level error messages when operating-system problems come up
 29840 ** during SQLite operation.  But so far, none of that has been implemented
 29841 ** in the core.  So this routine is never called.  For now, it is merely
 29842 ** a place-holder.
 29843 */
 29844 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
 29845   UNUSED_PARAMETER(NotUsed);
 29846   UNUSED_PARAMETER(NotUsed2);
 29847   UNUSED_PARAMETER(NotUsed3);
 29848   return 0;
 29852 /*
 29853 ************************ End of sqlite3_vfs methods ***************************
 29854 ******************************************************************************/
 29856 /******************************************************************************
 29857 ************************** Begin Proxy Locking ********************************
 29858 **
 29859 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
 29860 ** other locking methods on secondary lock files.  Proxy locking is a
 29861 ** meta-layer over top of the primitive locking implemented above.  For
 29862 ** this reason, the division that implements of proxy locking is deferred
 29863 ** until late in the file (here) after all of the other I/O methods have
 29864 ** been defined - so that the primitive locking methods are available
 29865 ** as services to help with the implementation of proxy locking.
 29866 **
 29867 ****
 29868 **
 29869 ** The default locking schemes in SQLite use byte-range locks on the
 29870 ** database file to coordinate safe, concurrent access by multiple readers
 29871 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
 29872 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
 29873 ** as POSIX read & write locks over fixed set of locations (via fsctl),
 29874 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
 29875 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
 29876 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
 29877 ** address in the shared range is taken for a SHARED lock, the entire
 29878 ** shared range is taken for an EXCLUSIVE lock):
 29879 **
 29880 **      PENDING_BYTE        0x40000000
 29881 **      RESERVED_BYTE       0x40000001
 29882 **      SHARED_RANGE        0x40000002 -> 0x40000200
 29883 **
 29884 ** This works well on the local file system, but shows a nearly 100x
 29885 ** slowdown in read performance on AFP because the AFP client disables
 29886 ** the read cache when byte-range locks are present.  Enabling the read
 29887 ** cache exposes a cache coherency problem that is present on all OS X
 29888 ** supported network file systems.  NFS and AFP both observe the
 29889 ** close-to-open semantics for ensuring cache coherency
 29890 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
 29891 ** address the requirements for concurrent database access by multiple
 29892 ** readers and writers
 29893 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
 29894 **
 29895 ** To address the performance and cache coherency issues, proxy file locking
 29896 ** changes the way database access is controlled by limiting access to a
 29897 ** single host at a time and moving file locks off of the database file
 29898 ** and onto a proxy file on the local file system.  
 29899 **
 29900 **
 29901 ** Using proxy locks
 29902 ** -----------------
 29903 **
 29904 ** C APIs
 29905 **
 29906 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
 29907 **                       <proxy_path> | ":auto:");
 29908 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
 29909 **
 29910 **
 29911 ** SQL pragmas
 29912 **
 29913 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
 29914 **  PRAGMA [database.]lock_proxy_file
 29915 **
 29916 ** Specifying ":auto:" means that if there is a conch file with a matching
 29917 ** host ID in it, the proxy path in the conch file will be used, otherwise
 29918 ** a proxy path based on the user's temp dir
 29919 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
 29920 ** actual proxy file name is generated from the name and path of the
 29921 ** database file.  For example:
 29922 **
 29923 **       For database path "/Users/me/foo.db" 
 29924 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
 29925 **
 29926 ** Once a lock proxy is configured for a database connection, it can not
 29927 ** be removed, however it may be switched to a different proxy path via
 29928 ** the above APIs (assuming the conch file is not being held by another
 29929 ** connection or process). 
 29930 **
 29931 **
 29932 ** How proxy locking works
 29933 ** -----------------------
 29934 **
 29935 ** Proxy file locking relies primarily on two new supporting files: 
 29936 **
 29937 **   *  conch file to limit access to the database file to a single host
 29938 **      at a time
 29939 **
 29940 **   *  proxy file to act as a proxy for the advisory locks normally
 29941 **      taken on the database
 29942 **
 29943 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
 29944 ** by taking an sqlite-style shared lock on the conch file, reading the
 29945 ** contents and comparing the host's unique host ID (see below) and lock
 29946 ** proxy path against the values stored in the conch.  The conch file is
 29947 ** stored in the same directory as the database file and the file name
 29948 ** is patterned after the database file name as ".<databasename>-conch".
 29949 ** If the conch file does not exist, or it's contents do not match the
 29950 ** host ID and/or proxy path, then the lock is escalated to an exclusive
 29951 ** lock and the conch file contents is updated with the host ID and proxy
 29952 ** path and the lock is downgraded to a shared lock again.  If the conch
 29953 ** is held by another process (with a shared lock), the exclusive lock
 29954 ** will fail and SQLITE_BUSY is returned.
 29955 **
 29956 ** The proxy file - a single-byte file used for all advisory file locks
 29957 ** normally taken on the database file.   This allows for safe sharing
 29958 ** of the database file for multiple readers and writers on the same
 29959 ** host (the conch ensures that they all use the same local lock file).
 29960 **
 29961 ** Requesting the lock proxy does not immediately take the conch, it is
 29962 ** only taken when the first request to lock database file is made.  
 29963 ** This matches the semantics of the traditional locking behavior, where
 29964 ** opening a connection to a database file does not take a lock on it.
 29965 ** The shared lock and an open file descriptor are maintained until 
 29966 ** the connection to the database is closed. 
 29967 **
 29968 ** The proxy file and the lock file are never deleted so they only need
 29969 ** to be created the first time they are used.
 29970 **
 29971 ** Configuration options
 29972 ** ---------------------
 29973 **
 29974 **  SQLITE_PREFER_PROXY_LOCKING
 29975 **
 29976 **       Database files accessed on non-local file systems are
 29977 **       automatically configured for proxy locking, lock files are
 29978 **       named automatically using the same logic as
 29979 **       PRAGMA lock_proxy_file=":auto:"
 29980 **    
 29981 **  SQLITE_PROXY_DEBUG
 29982 **
 29983 **       Enables the logging of error messages during host id file
 29984 **       retrieval and creation
 29985 **
 29986 **  LOCKPROXYDIR
 29987 **
 29988 **       Overrides the default directory used for lock proxy files that
 29989 **       are named automatically via the ":auto:" setting
 29990 **
 29991 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 29992 **
 29993 **       Permissions to use when creating a directory for storing the
 29994 **       lock proxy files, only used when LOCKPROXYDIR is not set.
 29995 **    
 29996 **    
 29997 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
 29998 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
 29999 ** force proxy locking to be used for every database file opened, and 0
 30000 ** will force automatic proxy locking to be disabled for all database
 30001 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
 30002 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
 30003 */
 30005 /*
 30006 ** Proxy locking is only available on MacOSX 
 30007 */
 30008 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 30010 /*
 30011 ** The proxyLockingContext has the path and file structures for the remote 
 30012 ** and local proxy files in it
 30013 */
 30014 typedef struct proxyLockingContext proxyLockingContext;
 30015 struct proxyLockingContext {
 30016   unixFile *conchFile;         /* Open conch file */
 30017   char *conchFilePath;         /* Name of the conch file */
 30018   unixFile *lockProxy;         /* Open proxy lock file */
 30019   char *lockProxyPath;         /* Name of the proxy lock file */
 30020   char *dbPath;                /* Name of the open file */
 30021   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
 30022   void *oldLockingContext;     /* Original lockingcontext to restore on close */
 30023   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
 30024 };
 30026 /* 
 30027 ** The proxy lock file path for the database at dbPath is written into lPath, 
 30028 ** which must point to valid, writable memory large enough for a maxLen length
 30029 ** file path. 
 30030 */
 30031 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
 30032   int len;
 30033   int dbLen;
 30034   int i;
 30036 #ifdef LOCKPROXYDIR
 30037   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
 30038 #else
 30039 # ifdef _CS_DARWIN_USER_TEMP_DIR
 30041     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
 30042       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
 30043                lPath, errno, getpid()));
 30044       return SQLITE_IOERR_LOCK;
 30046     len = strlcat(lPath, "sqliteplocks", maxLen);    
 30048 # else
 30049   len = strlcpy(lPath, "/tmp/", maxLen);
 30050 # endif
 30051 #endif
 30053   if( lPath[len-1]!='/' ){
 30054     len = strlcat(lPath, "/", maxLen);
 30057   /* transform the db path to a unique cache name */
 30058   dbLen = (int)strlen(dbPath);
 30059   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
 30060     char c = dbPath[i];
 30061     lPath[i+len] = (c=='/')?'_':c;
 30063   lPath[i+len]='\0';
 30064   strlcat(lPath, ":auto:", maxLen);
 30065   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
 30066   return SQLITE_OK;
 30069 /* 
 30070  ** Creates the lock file and any missing directories in lockPath
 30071  */
 30072 static int proxyCreateLockPath(const char *lockPath){
 30073   int i, len;
 30074   char buf[MAXPATHLEN];
 30075   int start = 0;
 30077   assert(lockPath!=NULL);
 30078   /* try to create all the intermediate directories */
 30079   len = (int)strlen(lockPath);
 30080   buf[0] = lockPath[0];
 30081   for( i=1; i<len; i++ ){
 30082     if( lockPath[i] == '/' && (i - start > 0) ){
 30083       /* only mkdir if leaf dir != "." or "/" or ".." */
 30084       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 
 30085          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
 30086         buf[i]='\0';
 30087         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
 30088           int err=errno;
 30089           if( err!=EEXIST ) {
 30090             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
 30091                      "'%s' proxy lock path=%s pid=%d\n",
 30092                      buf, strerror(err), lockPath, getpid()));
 30093             return err;
 30097       start=i+1;
 30099     buf[i] = lockPath[i];
 30101   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
 30102   return 0;
 30105 /*
 30106 ** Create a new VFS file descriptor (stored in memory obtained from
 30107 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
 30108 **
 30109 ** The caller is responsible not only for closing the file descriptor
 30110 ** but also for freeing the memory associated with the file descriptor.
 30111 */
 30112 static int proxyCreateUnixFile(
 30113     const char *path,        /* path for the new unixFile */
 30114     unixFile **ppFile,       /* unixFile created and returned by ref */
 30115     int islockfile           /* if non zero missing dirs will be created */
 30116 ) {
 30117   int fd = -1;
 30118   unixFile *pNew;
 30119   int rc = SQLITE_OK;
 30120   int openFlags = O_RDWR | O_CREAT;
 30121   sqlite3_vfs dummyVfs;
 30122   int terrno = 0;
 30123   UnixUnusedFd *pUnused = NULL;
 30125   /* 1. first try to open/create the file
 30126   ** 2. if that fails, and this is a lock file (not-conch), try creating
 30127   ** the parent directories and then try again.
 30128   ** 3. if that fails, try to open the file read-only
 30129   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
 30130   */
 30131   pUnused = findReusableFd(path, openFlags);
 30132   if( pUnused ){
 30133     fd = pUnused->fd;
 30134   }else{
 30135     pUnused = sqlite3_malloc(sizeof(*pUnused));
 30136     if( !pUnused ){
 30137       return SQLITE_NOMEM;
 30140   if( fd<0 ){
 30141     fd = robust_open(path, openFlags, 0);
 30142     terrno = errno;
 30143     if( fd<0 && errno==ENOENT && islockfile ){
 30144       if( proxyCreateLockPath(path) == SQLITE_OK ){
 30145         fd = robust_open(path, openFlags, 0);
 30149   if( fd<0 ){
 30150     openFlags = O_RDONLY;
 30151     fd = robust_open(path, openFlags, 0);
 30152     terrno = errno;
 30154   if( fd<0 ){
 30155     if( islockfile ){
 30156       return SQLITE_BUSY;
 30158     switch (terrno) {
 30159       case EACCES:
 30160         return SQLITE_PERM;
 30161       case EIO: 
 30162         return SQLITE_IOERR_LOCK; /* even though it is the conch */
 30163       default:
 30164         return SQLITE_CANTOPEN_BKPT;
 30168   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
 30169   if( pNew==NULL ){
 30170     rc = SQLITE_NOMEM;
 30171     goto end_create_proxy;
 30173   memset(pNew, 0, sizeof(unixFile));
 30174   pNew->openFlags = openFlags;
 30175   memset(&dummyVfs, 0, sizeof(dummyVfs));
 30176   dummyVfs.pAppData = (void*)&autolockIoFinder;
 30177   dummyVfs.zName = "dummy";
 30178   pUnused->fd = fd;
 30179   pUnused->flags = openFlags;
 30180   pNew->pUnused = pUnused;
 30182   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
 30183   if( rc==SQLITE_OK ){
 30184     *ppFile = pNew;
 30185     return SQLITE_OK;
 30187 end_create_proxy:    
 30188   robust_close(pNew, fd, __LINE__);
 30189   sqlite3_free(pNew);
 30190   sqlite3_free(pUnused);
 30191   return rc;
 30194 #ifdef SQLITE_TEST
 30195 /* simulate multiple hosts by creating unique hostid file paths */
 30196 SQLITE_API int sqlite3_hostid_num = 0;
 30197 #endif
 30199 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
 30201 /* Not always defined in the headers as it ought to be */
 30202 extern int gethostuuid(uuid_t id, const struct timespec *wait);
 30204 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
 30205 ** bytes of writable memory.
 30206 */
 30207 static int proxyGetHostID(unsigned char *pHostID, int *pError){
 30208   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
 30209   memset(pHostID, 0, PROXY_HOSTIDLEN);
 30210 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
 30211                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
 30213     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
 30214     if( gethostuuid(pHostID, &timeout) ){
 30215       int err = errno;
 30216       if( pError ){
 30217         *pError = err;
 30219       return SQLITE_IOERR;
 30222 #else
 30223   UNUSED_PARAMETER(pError);
 30224 #endif
 30225 #ifdef SQLITE_TEST
 30226   /* simulate multiple hosts by creating unique hostid file paths */
 30227   if( sqlite3_hostid_num != 0){
 30228     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
 30230 #endif
 30232   return SQLITE_OK;
 30235 /* The conch file contains the header, host id and lock file path
 30236  */
 30237 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
 30238 #define PROXY_HEADERLEN    1   /* conch file header length */
 30239 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
 30240 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
 30242 /* 
 30243 ** Takes an open conch file, copies the contents to a new path and then moves 
 30244 ** it back.  The newly created file's file descriptor is assigned to the
 30245 ** conch file structure and finally the original conch file descriptor is 
 30246 ** closed.  Returns zero if successful.
 30247 */
 30248 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
 30249   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 30250   unixFile *conchFile = pCtx->conchFile;
 30251   char tPath[MAXPATHLEN];
 30252   char buf[PROXY_MAXCONCHLEN];
 30253   char *cPath = pCtx->conchFilePath;
 30254   size_t readLen = 0;
 30255   size_t pathLen = 0;
 30256   char errmsg[64] = "";
 30257   int fd = -1;
 30258   int rc = -1;
 30259   UNUSED_PARAMETER(myHostID);
 30261   /* create a new path by replace the trailing '-conch' with '-break' */
 30262   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
 30263   if( pathLen>MAXPATHLEN || pathLen<6 || 
 30264      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
 30265     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
 30266     goto end_breaklock;
 30268   /* read the conch content */
 30269   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
 30270   if( readLen<PROXY_PATHINDEX ){
 30271     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
 30272     goto end_breaklock;
 30274   /* write it out to the temporary break file */
 30275   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
 30276   if( fd<0 ){
 30277     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
 30278     goto end_breaklock;
 30280   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
 30281     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
 30282     goto end_breaklock;
 30284   if( rename(tPath, cPath) ){
 30285     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
 30286     goto end_breaklock;
 30288   rc = 0;
 30289   fprintf(stderr, "broke stale lock on %s\n", cPath);
 30290   robust_close(pFile, conchFile->h, __LINE__);
 30291   conchFile->h = fd;
 30292   conchFile->openFlags = O_RDWR | O_CREAT;
 30294 end_breaklock:
 30295   if( rc ){
 30296     if( fd>=0 ){
 30297       osUnlink(tPath);
 30298       robust_close(pFile, fd, __LINE__);
 30300     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
 30302   return rc;
 30305 /* Take the requested lock on the conch file and break a stale lock if the 
 30306 ** host id matches.
 30307 */
 30308 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
 30309   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 30310   unixFile *conchFile = pCtx->conchFile;
 30311   int rc = SQLITE_OK;
 30312   int nTries = 0;
 30313   struct timespec conchModTime;
 30315   memset(&conchModTime, 0, sizeof(conchModTime));
 30316   do {
 30317     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 30318     nTries ++;
 30319     if( rc==SQLITE_BUSY ){
 30320       /* If the lock failed (busy):
 30321        * 1st try: get the mod time of the conch, wait 0.5s and try again. 
 30322        * 2nd try: fail if the mod time changed or host id is different, wait 
 30323        *           10 sec and try again
 30324        * 3rd try: break the lock unless the mod time has changed.
 30325        */
 30326       struct stat buf;
 30327       if( osFstat(conchFile->h, &buf) ){
 30328         pFile->lastErrno = errno;
 30329         return SQLITE_IOERR_LOCK;
 30332       if( nTries==1 ){
 30333         conchModTime = buf.st_mtimespec;
 30334         usleep(500000); /* wait 0.5 sec and try the lock again*/
 30335         continue;  
 30338       assert( nTries>1 );
 30339       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
 30340          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
 30341         return SQLITE_BUSY;
 30344       if( nTries==2 ){  
 30345         char tBuf[PROXY_MAXCONCHLEN];
 30346         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
 30347         if( len<0 ){
 30348           pFile->lastErrno = errno;
 30349           return SQLITE_IOERR_LOCK;
 30351         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
 30352           /* don't break the lock if the host id doesn't match */
 30353           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
 30354             return SQLITE_BUSY;
 30356         }else{
 30357           /* don't break the lock on short read or a version mismatch */
 30358           return SQLITE_BUSY;
 30360         usleep(10000000); /* wait 10 sec and try the lock again */
 30361         continue; 
 30364       assert( nTries==3 );
 30365       if( 0==proxyBreakConchLock(pFile, myHostID) ){
 30366         rc = SQLITE_OK;
 30367         if( lockType==EXCLUSIVE_LOCK ){
 30368           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);          
 30370         if( !rc ){
 30371           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
 30375   } while( rc==SQLITE_BUSY && nTries<3 );
 30377   return rc;
 30380 /* Takes the conch by taking a shared lock and read the contents conch, if 
 30381 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
 30382 ** lockPath means that the lockPath in the conch file will be used if the 
 30383 ** host IDs match, or a new lock path will be generated automatically 
 30384 ** and written to the conch file.
 30385 */
 30386 static int proxyTakeConch(unixFile *pFile){
 30387   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
 30389   if( pCtx->conchHeld!=0 ){
 30390     return SQLITE_OK;
 30391   }else{
 30392     unixFile *conchFile = pCtx->conchFile;
 30393     uuid_t myHostID;
 30394     int pError = 0;
 30395     char readBuf[PROXY_MAXCONCHLEN];
 30396     char lockPath[MAXPATHLEN];
 30397     char *tempLockPath = NULL;
 30398     int rc = SQLITE_OK;
 30399     int createConch = 0;
 30400     int hostIdMatch = 0;
 30401     int readLen = 0;
 30402     int tryOldLockPath = 0;
 30403     int forceNewLockPath = 0;
 30405     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
 30406              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
 30408     rc = proxyGetHostID(myHostID, &pError);
 30409     if( (rc&0xff)==SQLITE_IOERR ){
 30410       pFile->lastErrno = pError;
 30411       goto end_takeconch;
 30413     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
 30414     if( rc!=SQLITE_OK ){
 30415       goto end_takeconch;
 30417     /* read the existing conch file */
 30418     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
 30419     if( readLen<0 ){
 30420       /* I/O error: lastErrno set by seekAndRead */
 30421       pFile->lastErrno = conchFile->lastErrno;
 30422       rc = SQLITE_IOERR_READ;
 30423       goto end_takeconch;
 30424     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || 
 30425              readBuf[0]!=(char)PROXY_CONCHVERSION ){
 30426       /* a short read or version format mismatch means we need to create a new 
 30427       ** conch file. 
 30428       */
 30429       createConch = 1;
 30431     /* if the host id matches and the lock path already exists in the conch
 30432     ** we'll try to use the path there, if we can't open that path, we'll 
 30433     ** retry with a new auto-generated path 
 30434     */
 30435     do { /* in case we need to try again for an :auto: named lock file */
 30437       if( !createConch && !forceNewLockPath ){
 30438         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, 
 30439                                   PROXY_HOSTIDLEN);
 30440         /* if the conch has data compare the contents */
 30441         if( !pCtx->lockProxyPath ){
 30442           /* for auto-named local lock file, just check the host ID and we'll
 30443            ** use the local lock file path that's already in there
 30444            */
 30445           if( hostIdMatch ){
 30446             size_t pathLen = (readLen - PROXY_PATHINDEX);
 30448             if( pathLen>=MAXPATHLEN ){
 30449               pathLen=MAXPATHLEN-1;
 30451             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
 30452             lockPath[pathLen] = 0;
 30453             tempLockPath = lockPath;
 30454             tryOldLockPath = 1;
 30455             /* create a copy of the lock path if the conch is taken */
 30456             goto end_takeconch;
 30458         }else if( hostIdMatch
 30459                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
 30460                            readLen-PROXY_PATHINDEX)
 30461         ){
 30462           /* conch host and lock path match */
 30463           goto end_takeconch; 
 30467       /* if the conch isn't writable and doesn't match, we can't take it */
 30468       if( (conchFile->openFlags&O_RDWR) == 0 ){
 30469         rc = SQLITE_BUSY;
 30470         goto end_takeconch;
 30473       /* either the conch didn't match or we need to create a new one */
 30474       if( !pCtx->lockProxyPath ){
 30475         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
 30476         tempLockPath = lockPath;
 30477         /* create a copy of the lock path _only_ if the conch is taken */
 30480       /* update conch with host and path (this will fail if other process
 30481       ** has a shared lock already), if the host id matches, use the big
 30482       ** stick.
 30483       */
 30484       futimes(conchFile->h, NULL);
 30485       if( hostIdMatch && !createConch ){
 30486         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
 30487           /* We are trying for an exclusive lock but another thread in this
 30488            ** same process is still holding a shared lock. */
 30489           rc = SQLITE_BUSY;
 30490         } else {          
 30491           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
 30493       }else{
 30494         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
 30496       if( rc==SQLITE_OK ){
 30497         char writeBuffer[PROXY_MAXCONCHLEN];
 30498         int writeSize = 0;
 30500         writeBuffer[0] = (char)PROXY_CONCHVERSION;
 30501         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
 30502         if( pCtx->lockProxyPath!=NULL ){
 30503           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
 30504         }else{
 30505           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
 30507         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
 30508         robust_ftruncate(conchFile->h, writeSize);
 30509         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
 30510         fsync(conchFile->h);
 30511         /* If we created a new conch file (not just updated the contents of a 
 30512          ** valid conch file), try to match the permissions of the database 
 30513          */
 30514         if( rc==SQLITE_OK && createConch ){
 30515           struct stat buf;
 30516           int err = osFstat(pFile->h, &buf);
 30517           if( err==0 ){
 30518             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
 30519                                         S_IROTH|S_IWOTH);
 30520             /* try to match the database file R/W permissions, ignore failure */
 30521 #ifndef SQLITE_PROXY_DEBUG
 30522             osFchmod(conchFile->h, cmode);
 30523 #else
 30524             do{
 30525               rc = osFchmod(conchFile->h, cmode);
 30526             }while( rc==(-1) && errno==EINTR );
 30527             if( rc!=0 ){
 30528               int code = errno;
 30529               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
 30530                       cmode, code, strerror(code));
 30531             } else {
 30532               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
 30534           }else{
 30535             int code = errno;
 30536             fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
 30537                     err, code, strerror(code));
 30538 #endif
 30542       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
 30544     end_takeconch:
 30545       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
 30546       if( rc==SQLITE_OK && pFile->openFlags ){
 30547         int fd;
 30548         if( pFile->h>=0 ){
 30549           robust_close(pFile, pFile->h, __LINE__);
 30551         pFile->h = -1;
 30552         fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
 30553         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
 30554         if( fd>=0 ){
 30555           pFile->h = fd;
 30556         }else{
 30557           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
 30558            during locking */
 30561       if( rc==SQLITE_OK && !pCtx->lockProxy ){
 30562         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
 30563         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
 30564         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
 30565           /* we couldn't create the proxy lock file with the old lock file path
 30566            ** so try again via auto-naming 
 30567            */
 30568           forceNewLockPath = 1;
 30569           tryOldLockPath = 0;
 30570           continue; /* go back to the do {} while start point, try again */
 30573       if( rc==SQLITE_OK ){
 30574         /* Need to make a copy of path if we extracted the value
 30575          ** from the conch file or the path was allocated on the stack
 30576          */
 30577         if( tempLockPath ){
 30578           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
 30579           if( !pCtx->lockProxyPath ){
 30580             rc = SQLITE_NOMEM;
 30584       if( rc==SQLITE_OK ){
 30585         pCtx->conchHeld = 1;
 30587         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
 30588           afpLockingContext *afpCtx;
 30589           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
 30590           afpCtx->dbPath = pCtx->lockProxyPath;
 30592       } else {
 30593         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 30595       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
 30596                rc==SQLITE_OK?"ok":"failed"));
 30597       return rc;
 30598     } while (1); /* in case we need to retry the :auto: lock file - 
 30599                  ** we should never get here except via the 'continue' call. */
 30603 /*
 30604 ** If pFile holds a lock on a conch file, then release that lock.
 30605 */
 30606 static int proxyReleaseConch(unixFile *pFile){
 30607   int rc = SQLITE_OK;         /* Subroutine return code */
 30608   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
 30609   unixFile *conchFile;        /* Name of the conch file */
 30611   pCtx = (proxyLockingContext *)pFile->lockingContext;
 30612   conchFile = pCtx->conchFile;
 30613   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
 30614            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
 30615            getpid()));
 30616   if( pCtx->conchHeld>0 ){
 30617     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
 30619   pCtx->conchHeld = 0;
 30620   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
 30621            (rc==SQLITE_OK ? "ok" : "failed")));
 30622   return rc;
 30625 /*
 30626 ** Given the name of a database file, compute the name of its conch file.
 30627 ** Store the conch filename in memory obtained from sqlite3_malloc().
 30628 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
 30629 ** or SQLITE_NOMEM if unable to obtain memory.
 30630 **
 30631 ** The caller is responsible for ensuring that the allocated memory
 30632 ** space is eventually freed.
 30633 **
 30634 ** *pConchPath is set to NULL if a memory allocation error occurs.
 30635 */
 30636 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
 30637   int i;                        /* Loop counter */
 30638   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
 30639   char *conchPath;              /* buffer in which to construct conch name */
 30641   /* Allocate space for the conch filename and initialize the name to
 30642   ** the name of the original database file. */  
 30643   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
 30644   if( conchPath==0 ){
 30645     return SQLITE_NOMEM;
 30647   memcpy(conchPath, dbPath, len+1);
 30649   /* now insert a "." before the last / character */
 30650   for( i=(len-1); i>=0; i-- ){
 30651     if( conchPath[i]=='/' ){
 30652       i++;
 30653       break;
 30656   conchPath[i]='.';
 30657   while ( i<len ){
 30658     conchPath[i+1]=dbPath[i];
 30659     i++;
 30662   /* append the "-conch" suffix to the file */
 30663   memcpy(&conchPath[i+1], "-conch", 7);
 30664   assert( (int)strlen(conchPath) == len+7 );
 30666   return SQLITE_OK;
 30670 /* Takes a fully configured proxy locking-style unix file and switches
 30671 ** the local lock file path 
 30672 */
 30673 static int switchLockProxyPath(unixFile *pFile, const char *path) {
 30674   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 30675   char *oldPath = pCtx->lockProxyPath;
 30676   int rc = SQLITE_OK;
 30678   if( pFile->eFileLock!=NO_LOCK ){
 30679     return SQLITE_BUSY;
 30682   /* nothing to do if the path is NULL, :auto: or matches the existing path */
 30683   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
 30684     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
 30685     return SQLITE_OK;
 30686   }else{
 30687     unixFile *lockProxy = pCtx->lockProxy;
 30688     pCtx->lockProxy=NULL;
 30689     pCtx->conchHeld = 0;
 30690     if( lockProxy!=NULL ){
 30691       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
 30692       if( rc ) return rc;
 30693       sqlite3_free(lockProxy);
 30695     sqlite3_free(oldPath);
 30696     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
 30699   return rc;
 30702 /*
 30703 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
 30704 ** is a string buffer at least MAXPATHLEN+1 characters in size.
 30705 **
 30706 ** This routine find the filename associated with pFile and writes it
 30707 ** int dbPath.
 30708 */
 30709 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
 30710 #if defined(__APPLE__)
 30711   if( pFile->pMethod == &afpIoMethods ){
 30712     /* afp style keeps a reference to the db path in the filePath field 
 30713     ** of the struct */
 30714     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 30715     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
 30716   } else
 30717 #endif
 30718   if( pFile->pMethod == &dotlockIoMethods ){
 30719     /* dot lock style uses the locking context to store the dot lock
 30720     ** file path */
 30721     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
 30722     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
 30723   }else{
 30724     /* all other styles use the locking context to store the db file path */
 30725     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
 30726     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
 30728   return SQLITE_OK;
 30731 /*
 30732 ** Takes an already filled in unix file and alters it so all file locking 
 30733 ** will be performed on the local proxy lock file.  The following fields
 30734 ** are preserved in the locking context so that they can be restored and 
 30735 ** the unix structure properly cleaned up at close time:
 30736 **  ->lockingContext
 30737 **  ->pMethod
 30738 */
 30739 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
 30740   proxyLockingContext *pCtx;
 30741   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
 30742   char *lockPath=NULL;
 30743   int rc = SQLITE_OK;
 30745   if( pFile->eFileLock!=NO_LOCK ){
 30746     return SQLITE_BUSY;
 30748   proxyGetDbPathForUnixFile(pFile, dbPath);
 30749   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
 30750     lockPath=NULL;
 30751   }else{
 30752     lockPath=(char *)path;
 30755   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
 30756            (lockPath ? lockPath : ":auto:"), getpid()));
 30758   pCtx = sqlite3_malloc( sizeof(*pCtx) );
 30759   if( pCtx==0 ){
 30760     return SQLITE_NOMEM;
 30762   memset(pCtx, 0, sizeof(*pCtx));
 30764   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
 30765   if( rc==SQLITE_OK ){
 30766     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
 30767     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
 30768       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
 30769       ** (c) the file system is read-only, then enable no-locking access.
 30770       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
 30771       ** that openFlags will have only one of O_RDONLY or O_RDWR.
 30772       */
 30773       struct statfs fsInfo;
 30774       struct stat conchInfo;
 30775       int goLockless = 0;
 30777       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
 30778         int err = errno;
 30779         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
 30780           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
 30783       if( goLockless ){
 30784         pCtx->conchHeld = -1; /* read only FS/ lockless */
 30785         rc = SQLITE_OK;
 30789   if( rc==SQLITE_OK && lockPath ){
 30790     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
 30793   if( rc==SQLITE_OK ){
 30794     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
 30795     if( pCtx->dbPath==NULL ){
 30796       rc = SQLITE_NOMEM;
 30799   if( rc==SQLITE_OK ){
 30800     /* all memory is allocated, proxys are created and assigned, 
 30801     ** switch the locking context and pMethod then return.
 30802     */
 30803     pCtx->oldLockingContext = pFile->lockingContext;
 30804     pFile->lockingContext = pCtx;
 30805     pCtx->pOldMethod = pFile->pMethod;
 30806     pFile->pMethod = &proxyIoMethods;
 30807   }else{
 30808     if( pCtx->conchFile ){ 
 30809       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
 30810       sqlite3_free(pCtx->conchFile);
 30812     sqlite3DbFree(0, pCtx->lockProxyPath);
 30813     sqlite3_free(pCtx->conchFilePath); 
 30814     sqlite3_free(pCtx);
 30816   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
 30817            (rc==SQLITE_OK ? "ok" : "failed")));
 30818   return rc;
 30822 /*
 30823 ** This routine handles sqlite3_file_control() calls that are specific
 30824 ** to proxy locking.
 30825 */
 30826 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
 30827   switch( op ){
 30828     case SQLITE_GET_LOCKPROXYFILE: {
 30829       unixFile *pFile = (unixFile*)id;
 30830       if( pFile->pMethod == &proxyIoMethods ){
 30831         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
 30832         proxyTakeConch(pFile);
 30833         if( pCtx->lockProxyPath ){
 30834           *(const char **)pArg = pCtx->lockProxyPath;
 30835         }else{
 30836           *(const char **)pArg = ":auto: (not held)";
 30838       } else {
 30839         *(const char **)pArg = NULL;
 30841       return SQLITE_OK;
 30843     case SQLITE_SET_LOCKPROXYFILE: {
 30844       unixFile *pFile = (unixFile*)id;
 30845       int rc = SQLITE_OK;
 30846       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
 30847       if( pArg==NULL || (const char *)pArg==0 ){
 30848         if( isProxyStyle ){
 30849           /* turn off proxy locking - not supported */
 30850           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
 30851         }else{
 30852           /* turn off proxy locking - already off - NOOP */
 30853           rc = SQLITE_OK;
 30855       }else{
 30856         const char *proxyPath = (const char *)pArg;
 30857         if( isProxyStyle ){
 30858           proxyLockingContext *pCtx = 
 30859             (proxyLockingContext*)pFile->lockingContext;
 30860           if( !strcmp(pArg, ":auto:") 
 30861            || (pCtx->lockProxyPath &&
 30862                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
 30863           ){
 30864             rc = SQLITE_OK;
 30865           }else{
 30866             rc = switchLockProxyPath(pFile, proxyPath);
 30868         }else{
 30869           /* turn on proxy file locking */
 30870           rc = proxyTransformUnixFile(pFile, proxyPath);
 30873       return rc;
 30875     default: {
 30876       assert( 0 );  /* The call assures that only valid opcodes are sent */
 30879   /*NOTREACHED*/
 30880   return SQLITE_ERROR;
 30883 /*
 30884 ** Within this division (the proxying locking implementation) the procedures
 30885 ** above this point are all utilities.  The lock-related methods of the
 30886 ** proxy-locking sqlite3_io_method object follow.
 30887 */
 30890 /*
 30891 ** This routine checks if there is a RESERVED lock held on the specified
 30892 ** file by this or any other process. If such a lock is held, set *pResOut
 30893 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 30894 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 30895 */
 30896 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
 30897   unixFile *pFile = (unixFile*)id;
 30898   int rc = proxyTakeConch(pFile);
 30899   if( rc==SQLITE_OK ){
 30900     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 30901     if( pCtx->conchHeld>0 ){
 30902       unixFile *proxy = pCtx->lockProxy;
 30903       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
 30904     }else{ /* conchHeld < 0 is lockless */
 30905       pResOut=0;
 30908   return rc;
 30911 /*
 30912 ** Lock the file with the lock specified by parameter eFileLock - one
 30913 ** of the following:
 30914 **
 30915 **     (1) SHARED_LOCK
 30916 **     (2) RESERVED_LOCK
 30917 **     (3) PENDING_LOCK
 30918 **     (4) EXCLUSIVE_LOCK
 30919 **
 30920 ** Sometimes when requesting one lock state, additional lock states
 30921 ** are inserted in between.  The locking might fail on one of the later
 30922 ** transitions leaving the lock state different from what it started but
 30923 ** still short of its goal.  The following chart shows the allowed
 30924 ** transitions and the inserted intermediate states:
 30925 **
 30926 **    UNLOCKED -> SHARED
 30927 **    SHARED -> RESERVED
 30928 **    SHARED -> (PENDING) -> EXCLUSIVE
 30929 **    RESERVED -> (PENDING) -> EXCLUSIVE
 30930 **    PENDING -> EXCLUSIVE
 30931 **
 30932 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 30933 ** routine to lower a locking level.
 30934 */
 30935 static int proxyLock(sqlite3_file *id, int eFileLock) {
 30936   unixFile *pFile = (unixFile*)id;
 30937   int rc = proxyTakeConch(pFile);
 30938   if( rc==SQLITE_OK ){
 30939     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 30940     if( pCtx->conchHeld>0 ){
 30941       unixFile *proxy = pCtx->lockProxy;
 30942       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
 30943       pFile->eFileLock = proxy->eFileLock;
 30944     }else{
 30945       /* conchHeld < 0 is lockless */
 30948   return rc;
 30952 /*
 30953 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 30954 ** must be either NO_LOCK or SHARED_LOCK.
 30955 **
 30956 ** If the locking level of the file descriptor is already at or below
 30957 ** the requested locking level, this routine is a no-op.
 30958 */
 30959 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
 30960   unixFile *pFile = (unixFile*)id;
 30961   int rc = proxyTakeConch(pFile);
 30962   if( rc==SQLITE_OK ){
 30963     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 30964     if( pCtx->conchHeld>0 ){
 30965       unixFile *proxy = pCtx->lockProxy;
 30966       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
 30967       pFile->eFileLock = proxy->eFileLock;
 30968     }else{
 30969       /* conchHeld < 0 is lockless */
 30972   return rc;
 30975 /*
 30976 ** Close a file that uses proxy locks.
 30977 */
 30978 static int proxyClose(sqlite3_file *id) {
 30979   if( id ){
 30980     unixFile *pFile = (unixFile*)id;
 30981     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
 30982     unixFile *lockProxy = pCtx->lockProxy;
 30983     unixFile *conchFile = pCtx->conchFile;
 30984     int rc = SQLITE_OK;
 30986     if( lockProxy ){
 30987       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
 30988       if( rc ) return rc;
 30989       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
 30990       if( rc ) return rc;
 30991       sqlite3_free(lockProxy);
 30992       pCtx->lockProxy = 0;
 30994     if( conchFile ){
 30995       if( pCtx->conchHeld ){
 30996         rc = proxyReleaseConch(pFile);
 30997         if( rc ) return rc;
 30999       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
 31000       if( rc ) return rc;
 31001       sqlite3_free(conchFile);
 31003     sqlite3DbFree(0, pCtx->lockProxyPath);
 31004     sqlite3_free(pCtx->conchFilePath);
 31005     sqlite3DbFree(0, pCtx->dbPath);
 31006     /* restore the original locking context and pMethod then close it */
 31007     pFile->lockingContext = pCtx->oldLockingContext;
 31008     pFile->pMethod = pCtx->pOldMethod;
 31009     sqlite3_free(pCtx);
 31010     return pFile->pMethod->xClose(id);
 31012   return SQLITE_OK;
 31017 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 31018 /*
 31019 ** The proxy locking style is intended for use with AFP filesystems.
 31020 ** And since AFP is only supported on MacOSX, the proxy locking is also
 31021 ** restricted to MacOSX.
 31022 ** 
 31023 **
 31024 ******************* End of the proxy lock implementation **********************
 31025 ******************************************************************************/
 31027 /*
 31028 ** Initialize the operating system interface.
 31029 **
 31030 ** This routine registers all VFS implementations for unix-like operating
 31031 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
 31032 ** should be the only routines in this file that are visible from other
 31033 ** files.
 31034 **
 31035 ** This routine is called once during SQLite initialization and by a
 31036 ** single thread.  The memory allocation and mutex subsystems have not
 31037 ** necessarily been initialized when this routine is called, and so they
 31038 ** should not be used.
 31039 */
 31040 SQLITE_API int sqlite3_os_init(void){ 
 31041   /* 
 31042   ** The following macro defines an initializer for an sqlite3_vfs object.
 31043   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
 31044   ** to the "finder" function.  (pAppData is a pointer to a pointer because
 31045   ** silly C90 rules prohibit a void* from being cast to a function pointer
 31046   ** and so we have to go through the intermediate pointer to avoid problems
 31047   ** when compiling with -pedantic-errors on GCC.)
 31048   **
 31049   ** The FINDER parameter to this macro is the name of the pointer to the
 31050   ** finder-function.  The finder-function returns a pointer to the
 31051   ** sqlite_io_methods object that implements the desired locking
 31052   ** behaviors.  See the division above that contains the IOMETHODS
 31053   ** macro for addition information on finder-functions.
 31054   **
 31055   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
 31056   ** object.  But the "autolockIoFinder" available on MacOSX does a little
 31057   ** more than that; it looks at the filesystem type that hosts the 
 31058   ** database file and tries to choose an locking method appropriate for
 31059   ** that filesystem time.
 31060   */
 31061   #define UNIXVFS(VFSNAME, FINDER) {                        \
 31062     3,                    /* iVersion */                    \
 31063     sizeof(unixFile),     /* szOsFile */                    \
 31064     MAX_PATHNAME,         /* mxPathname */                  \
 31065     0,                    /* pNext */                       \
 31066     VFSNAME,              /* zName */                       \
 31067     (void*)&FINDER,       /* pAppData */                    \
 31068     unixOpen,             /* xOpen */                       \
 31069     unixDelete,           /* xDelete */                     \
 31070     unixAccess,           /* xAccess */                     \
 31071     unixFullPathname,     /* xFullPathname */               \
 31072     unixDlOpen,           /* xDlOpen */                     \
 31073     unixDlError,          /* xDlError */                    \
 31074     unixDlSym,            /* xDlSym */                      \
 31075     unixDlClose,          /* xDlClose */                    \
 31076     unixRandomness,       /* xRandomness */                 \
 31077     unixSleep,            /* xSleep */                      \
 31078     unixCurrentTime,      /* xCurrentTime */                \
 31079     unixGetLastError,     /* xGetLastError */               \
 31080     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
 31081     unixSetSystemCall,    /* xSetSystemCall */              \
 31082     unixGetSystemCall,    /* xGetSystemCall */              \
 31083     unixNextSystemCall,   /* xNextSystemCall */             \
 31086   /*
 31087   ** All default VFSes for unix are contained in the following array.
 31088   **
 31089   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
 31090   ** by the SQLite core when the VFS is registered.  So the following
 31091   ** array cannot be const.
 31092   */
 31093   static sqlite3_vfs aVfs[] = {
 31094 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
 31095     UNIXVFS("unix",          autolockIoFinder ),
 31096 #else
 31097     UNIXVFS("unix",          posixIoFinder ),
 31098 #endif
 31099     UNIXVFS("unix-none",     nolockIoFinder ),
 31100     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
 31101     UNIXVFS("unix-excl",     posixIoFinder ),
 31102 #if OS_VXWORKS
 31103     UNIXVFS("unix-namedsem", semIoFinder ),
 31104 #endif
 31105 #if SQLITE_ENABLE_LOCKING_STYLE
 31106     UNIXVFS("unix-posix",    posixIoFinder ),
 31107 #if !OS_VXWORKS
 31108     UNIXVFS("unix-flock",    flockIoFinder ),
 31109 #endif
 31110 #endif
 31111 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 31112     UNIXVFS("unix-afp",      afpIoFinder ),
 31113     UNIXVFS("unix-nfs",      nfsIoFinder ),
 31114     UNIXVFS("unix-proxy",    proxyIoFinder ),
 31115 #endif
 31116   };
 31117   unsigned int i;          /* Loop counter */
 31119   /* Double-check that the aSyscall[] array has been constructed
 31120   ** correctly.  See ticket [bb3a86e890c8e96ab] */
 31121   assert( ArraySize(aSyscall)==24 );
 31123   /* Register all VFSes defined in the aVfs[] array */
 31124   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 31125     sqlite3_vfs_register(&aVfs[i], i==0);
 31127   return SQLITE_OK; 
 31130 /*
 31131 ** Shutdown the operating system interface.
 31132 **
 31133 ** Some operating systems might need to do some cleanup in this routine,
 31134 ** to release dynamically allocated objects.  But not on unix.
 31135 ** This routine is a no-op for unix.
 31136 */
 31137 SQLITE_API int sqlite3_os_end(void){ 
 31138   return SQLITE_OK; 
 31141 #endif /* SQLITE_OS_UNIX */
 31143 /************** End of os_unix.c *********************************************/
 31144 /************** Begin file os_win.c ******************************************/
 31145 /*
 31146 ** 2004 May 22
 31147 **
 31148 ** The author disclaims copyright to this source code.  In place of
 31149 ** a legal notice, here is a blessing:
 31150 **
 31151 **    May you do good and not evil.
 31152 **    May you find forgiveness for yourself and forgive others.
 31153 **    May you share freely, never taking more than you give.
 31154 **
 31155 ******************************************************************************
 31156 **
 31157 ** This file contains code that is specific to Windows.
 31158 */
 31159 #if SQLITE_OS_WIN               /* This file is used for Windows only */
 31161 #ifdef __CYGWIN__
 31162 # include <sys/cygwin.h>
 31163 # include <errno.h> /* amalgamator: keep */
 31164 #endif
 31166 /*
 31167 ** Include code that is common to all os_*.c files
 31168 */
 31169 /************** Include os_common.h in the middle of os_win.c ****************/
 31170 /************** Begin file os_common.h ***************************************/
 31171 /*
 31172 ** 2004 May 22
 31173 **
 31174 ** The author disclaims copyright to this source code.  In place of
 31175 ** a legal notice, here is a blessing:
 31176 **
 31177 **    May you do good and not evil.
 31178 **    May you find forgiveness for yourself and forgive others.
 31179 **    May you share freely, never taking more than you give.
 31180 **
 31181 ******************************************************************************
 31182 **
 31183 ** This file contains macros and a little bit of code that is common to
 31184 ** all of the platform-specific files (os_*.c) and is #included into those
 31185 ** files.
 31186 **
 31187 ** This file should be #included by the os_*.c files only.  It is not a
 31188 ** general purpose header file.
 31189 */
 31190 #ifndef _OS_COMMON_H_
 31191 #define _OS_COMMON_H_
 31193 /*
 31194 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 31195 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 31196 ** switch.  The following code should catch this problem at compile-time.
 31197 */
 31198 #ifdef MEMORY_DEBUG
 31199 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 31200 #endif
 31202 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 31203 # ifndef SQLITE_DEBUG_OS_TRACE
 31204 #   define SQLITE_DEBUG_OS_TRACE 0
 31205 # endif
 31206   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 31207 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 31208 #else
 31209 # define OSTRACE(X)
 31210 #endif
 31212 /*
 31213 ** Macros for performance tracing.  Normally turned off.  Only works
 31214 ** on i486 hardware.
 31215 */
 31216 #ifdef SQLITE_PERFORMANCE_TRACE
 31218 /* 
 31219 ** hwtime.h contains inline assembler code for implementing 
 31220 ** high-performance timing routines.
 31221 */
 31222 /************** Include hwtime.h in the middle of os_common.h ****************/
 31223 /************** Begin file hwtime.h ******************************************/
 31224 /*
 31225 ** 2008 May 27
 31226 **
 31227 ** The author disclaims copyright to this source code.  In place of
 31228 ** a legal notice, here is a blessing:
 31229 **
 31230 **    May you do good and not evil.
 31231 **    May you find forgiveness for yourself and forgive others.
 31232 **    May you share freely, never taking more than you give.
 31233 **
 31234 ******************************************************************************
 31235 **
 31236 ** This file contains inline asm code for retrieving "high-performance"
 31237 ** counters for x86 class CPUs.
 31238 */
 31239 #ifndef _HWTIME_H_
 31240 #define _HWTIME_H_
 31242 /*
 31243 ** The following routine only works on pentium-class (or newer) processors.
 31244 ** It uses the RDTSC opcode to read the cycle count value out of the
 31245 ** processor and returns that value.  This can be used for high-res
 31246 ** profiling.
 31247 */
 31248 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 31249       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 31251   #if defined(__GNUC__)
 31253   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 31254      unsigned int lo, hi;
 31255      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 31256      return (sqlite_uint64)hi << 32 | lo;
 31259   #elif defined(_MSC_VER)
 31261   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 31262      __asm {
 31263         rdtsc
 31264         ret       ; return value at EDX:EAX
 31268   #endif
 31270 #elif (defined(__GNUC__) && defined(__x86_64__))
 31272   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 31273       unsigned long val;
 31274       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 31275       return val;
 31278 #elif (defined(__GNUC__) && defined(__ppc__))
 31280   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 31281       unsigned long long retval;
 31282       unsigned long junk;
 31283       __asm__ __volatile__ ("\n\
 31284           1:      mftbu   %1\n\
 31285                   mftb    %L0\n\
 31286                   mftbu   %0\n\
 31287                   cmpw    %0,%1\n\
 31288                   bne     1b"
 31289                   : "=r" (retval), "=r" (junk));
 31290       return retval;
 31293 #else
 31295   #error Need implementation of sqlite3Hwtime() for your platform.
 31297   /*
 31298   ** To compile without implementing sqlite3Hwtime() for your platform,
 31299   ** you can remove the above #error and use the following
 31300   ** stub function.  You will lose timing support for many
 31301   ** of the debugging and testing utilities, but it should at
 31302   ** least compile and run.
 31303   */
 31304 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 31306 #endif
 31308 #endif /* !defined(_HWTIME_H_) */
 31310 /************** End of hwtime.h **********************************************/
 31311 /************** Continuing where we left off in os_common.h ******************/
 31313 static sqlite_uint64 g_start;
 31314 static sqlite_uint64 g_elapsed;
 31315 #define TIMER_START       g_start=sqlite3Hwtime()
 31316 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 31317 #define TIMER_ELAPSED     g_elapsed
 31318 #else
 31319 #define TIMER_START
 31320 #define TIMER_END
 31321 #define TIMER_ELAPSED     ((sqlite_uint64)0)
 31322 #endif
 31324 /*
 31325 ** If we compile with the SQLITE_TEST macro set, then the following block
 31326 ** of code will give us the ability to simulate a disk I/O error.  This
 31327 ** is used for testing the I/O recovery logic.
 31328 */
 31329 #ifdef SQLITE_TEST
 31330 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 31331 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 31332 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 31333 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 31334 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 31335 SQLITE_API int sqlite3_diskfull_pending = 0;
 31336 SQLITE_API int sqlite3_diskfull = 0;
 31337 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 31338 #define SimulateIOError(CODE)  \
 31339   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 31340        || sqlite3_io_error_pending-- == 1 )  \
 31341               { local_ioerr(); CODE; }
 31342 static void local_ioerr(){
 31343   IOTRACE(("IOERR\n"));
 31344   sqlite3_io_error_hit++;
 31345   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 31347 #define SimulateDiskfullError(CODE) \
 31348    if( sqlite3_diskfull_pending ){ \
 31349      if( sqlite3_diskfull_pending == 1 ){ \
 31350        local_ioerr(); \
 31351        sqlite3_diskfull = 1; \
 31352        sqlite3_io_error_hit = 1; \
 31353        CODE; \
 31354      }else{ \
 31355        sqlite3_diskfull_pending--; \
 31356      } \
 31358 #else
 31359 #define SimulateIOErrorBenign(X)
 31360 #define SimulateIOError(A)
 31361 #define SimulateDiskfullError(A)
 31362 #endif
 31364 /*
 31365 ** When testing, keep a count of the number of open files.
 31366 */
 31367 #ifdef SQLITE_TEST
 31368 SQLITE_API int sqlite3_open_file_count = 0;
 31369 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 31370 #else
 31371 #define OpenCounter(X)
 31372 #endif
 31374 #endif /* !defined(_OS_COMMON_H_) */
 31376 /************** End of os_common.h *******************************************/
 31377 /************** Continuing where we left off in os_win.c *********************/
 31379 /*
 31380 ** Compiling and using WAL mode requires several APIs that are only
 31381 ** available in Windows platforms based on the NT kernel.
 31382 */
 31383 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
 31384 #  error "WAL mode requires support from the Windows NT kernel, compile\
 31385  with SQLITE_OMIT_WAL."
 31386 #endif
 31388 /*
 31389 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
 31390 ** based on the sub-platform)?
 31391 */
 31392 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
 31393 #  define SQLITE_WIN32_HAS_ANSI
 31394 #endif
 31396 /*
 31397 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
 31398 ** based on the sub-platform)?
 31399 */
 31400 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
 31401     !defined(SQLITE_WIN32_NO_WIDE)
 31402 #  define SQLITE_WIN32_HAS_WIDE
 31403 #endif
 31405 /*
 31406 ** Make sure at least one set of Win32 APIs is available.
 31407 */
 31408 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
 31409 #  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
 31410  must be defined."
 31411 #endif
 31413 /*
 31414 ** Define the required Windows SDK version constants if they are not
 31415 ** already available.
 31416 */
 31417 #ifndef NTDDI_WIN8
 31418 #  define NTDDI_WIN8                        0x06020000
 31419 #endif
 31421 #ifndef NTDDI_WINBLUE
 31422 #  define NTDDI_WINBLUE                     0x06030000
 31423 #endif
 31425 /*
 31426 ** Check if the GetVersionEx[AW] functions should be considered deprecated
 31427 ** and avoid using them in that case.  It should be noted here that if the
 31428 ** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
 31429 ** (whether via this block or via being manually specified), that implies
 31430 ** the underlying operating system will always be based on the Windows NT
 31431 ** Kernel.
 31432 */
 31433 #ifndef SQLITE_WIN32_GETVERSIONEX
 31434 #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
 31435 #    define SQLITE_WIN32_GETVERSIONEX   0
 31436 #  else
 31437 #    define SQLITE_WIN32_GETVERSIONEX   1
 31438 #  endif
 31439 #endif
 31441 /*
 31442 ** This constant should already be defined (in the "WinDef.h" SDK file).
 31443 */
 31444 #ifndef MAX_PATH
 31445 #  define MAX_PATH                      (260)
 31446 #endif
 31448 /*
 31449 ** Maximum pathname length (in chars) for Win32.  This should normally be
 31450 ** MAX_PATH.
 31451 */
 31452 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
 31453 #  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
 31454 #endif
 31456 /*
 31457 ** This constant should already be defined (in the "WinNT.h" SDK file).
 31458 */
 31459 #ifndef UNICODE_STRING_MAX_CHARS
 31460 #  define UNICODE_STRING_MAX_CHARS      (32767)
 31461 #endif
 31463 /*
 31464 ** Maximum pathname length (in chars) for WinNT.  This should normally be
 31465 ** UNICODE_STRING_MAX_CHARS.
 31466 */
 31467 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
 31468 #  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
 31469 #endif
 31471 /*
 31472 ** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
 31473 ** characters, so we allocate 4 bytes per character assuming worst-case of
 31474 ** 4-bytes-per-character for UTF8.
 31475 */
 31476 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
 31477 #  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
 31478 #endif
 31480 /*
 31481 ** Maximum pathname length (in bytes) for WinNT.  This should normally be
 31482 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
 31483 */
 31484 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
 31485 #  define SQLITE_WINNT_MAX_PATH_BYTES   \
 31486                             (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
 31487 #endif
 31489 /*
 31490 ** Maximum error message length (in chars) for WinRT.
 31491 */
 31492 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
 31493 #  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
 31494 #endif
 31496 /*
 31497 ** Returns non-zero if the character should be treated as a directory
 31498 ** separator.
 31499 */
 31500 #ifndef winIsDirSep
 31501 #  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
 31502 #endif
 31504 /*
 31505 ** This macro is used when a local variable is set to a value that is
 31506 ** [sometimes] not used by the code (e.g. via conditional compilation).
 31507 */
 31508 #ifndef UNUSED_VARIABLE_VALUE
 31509 #  define UNUSED_VARIABLE_VALUE(x) (void)(x)
 31510 #endif
 31512 /*
 31513 ** Returns the character that should be used as the directory separator.
 31514 */
 31515 #ifndef winGetDirSep
 31516 #  define winGetDirSep()                '\\'
 31517 #endif
 31519 /*
 31520 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
 31521 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
 31522 ** are not present in the header file)?
 31523 */
 31524 #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
 31525 /*
 31526 ** Two of the file mapping APIs are different under WinRT.  Figure out which
 31527 ** set we need.
 31528 */
 31529 #if SQLITE_OS_WINRT
 31530 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
 31531         LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
 31533 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
 31534 #else
 31535 #if defined(SQLITE_WIN32_HAS_ANSI)
 31536 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
 31537         DWORD, DWORD, DWORD, LPCSTR);
 31538 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
 31540 #if defined(SQLITE_WIN32_HAS_WIDE)
 31541 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
 31542         DWORD, DWORD, DWORD, LPCWSTR);
 31543 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
 31545 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
 31546 #endif /* SQLITE_OS_WINRT */
 31548 /*
 31549 ** This file mapping API is common to both Win32 and WinRT.
 31550 */
 31551 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
 31552 #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
 31554 /*
 31555 ** Some Microsoft compilers lack this definition.
 31556 */
 31557 #ifndef INVALID_FILE_ATTRIBUTES
 31558 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
 31559 #endif
 31561 #ifndef FILE_FLAG_MASK
 31562 # define FILE_FLAG_MASK          (0xFF3C0000)
 31563 #endif
 31565 #ifndef FILE_ATTRIBUTE_MASK
 31566 # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
 31567 #endif
 31569 #ifndef SQLITE_OMIT_WAL
 31570 /* Forward references to structures used for WAL */
 31571 typedef struct winShm winShm;           /* A connection to shared-memory */
 31572 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
 31573 #endif
 31575 /*
 31576 ** WinCE lacks native support for file locking so we have to fake it
 31577 ** with some code of our own.
 31578 */
 31579 #if SQLITE_OS_WINCE
 31580 typedef struct winceLock {
 31581   int nReaders;       /* Number of reader locks obtained */
 31582   BOOL bPending;      /* Indicates a pending lock has been obtained */
 31583   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
 31584   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
 31585 } winceLock;
 31586 #endif
 31588 /*
 31589 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
 31590 ** portability layer.
 31591 */
 31592 typedef struct winFile winFile;
 31593 struct winFile {
 31594   const sqlite3_io_methods *pMethod; /*** Must be first ***/
 31595   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
 31596   HANDLE h;               /* Handle for accessing the file */
 31597   u8 locktype;            /* Type of lock currently held on this file */
 31598   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
 31599   u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
 31600   DWORD lastErrno;        /* The Windows errno from the last I/O error */
 31601 #ifndef SQLITE_OMIT_WAL
 31602   winShm *pShm;           /* Instance of shared memory on this file */
 31603 #endif
 31604   const char *zPath;      /* Full pathname of this file */
 31605   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
 31606 #if SQLITE_OS_WINCE
 31607   LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
 31608   HANDLE hMutex;          /* Mutex used to control access to shared lock */  
 31609   HANDLE hShared;         /* Shared memory segment used for locking */
 31610   winceLock local;        /* Locks obtained by this instance of winFile */
 31611   winceLock *shared;      /* Global shared lock memory for the file  */
 31612 #endif
 31613 #if SQLITE_MAX_MMAP_SIZE>0
 31614   int nFetchOut;                /* Number of outstanding xFetch references */
 31615   HANDLE hMap;                  /* Handle for accessing memory mapping */
 31616   void *pMapRegion;             /* Area memory mapped */
 31617   sqlite3_int64 mmapSize;       /* Usable size of mapped region */
 31618   sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
 31619   sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
 31620 #endif
 31621 };
 31623 /*
 31624 ** Allowed values for winFile.ctrlFlags
 31625 */
 31626 #define WINFILE_RDONLY          0x02   /* Connection is read only */
 31627 #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
 31628 #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
 31630 /*
 31631  * The size of the buffer used by sqlite3_win32_write_debug().
 31632  */
 31633 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
 31634 #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
 31635 #endif
 31637 /*
 31638  * The value used with sqlite3_win32_set_directory() to specify that
 31639  * the data directory should be changed.
 31640  */
 31641 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
 31642 #  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
 31643 #endif
 31645 /*
 31646  * The value used with sqlite3_win32_set_directory() to specify that
 31647  * the temporary directory should be changed.
 31648  */
 31649 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 31650 #  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
 31651 #endif
 31653 /*
 31654  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
 31655  * various Win32 API heap functions instead of our own.
 31656  */
 31657 #ifdef SQLITE_WIN32_MALLOC
 31659 /*
 31660  * If this is non-zero, an isolated heap will be created by the native Win32
 31661  * allocator subsystem; otherwise, the default process heap will be used.  This
 31662  * setting has no effect when compiling for WinRT.  By default, this is enabled
 31663  * and an isolated heap will be created to store all allocated data.
 31665  ******************************************************************************
 31666  * WARNING: It is important to note that when this setting is non-zero and the
 31667  *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
 31668  *          function), all data that was allocated using the isolated heap will
 31669  *          be freed immediately and any attempt to access any of that freed
 31670  *          data will almost certainly result in an immediate access violation.
 31671  ******************************************************************************
 31672  */
 31673 #ifndef SQLITE_WIN32_HEAP_CREATE
 31674 #  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
 31675 #endif
 31677 /*
 31678  * The initial size of the Win32-specific heap.  This value may be zero.
 31679  */
 31680 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
 31681 #  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
 31682                                        (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
 31683 #endif
 31685 /*
 31686  * The maximum size of the Win32-specific heap.  This value may be zero.
 31687  */
 31688 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
 31689 #  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
 31690 #endif
 31692 /*
 31693  * The extra flags to use in calls to the Win32 heap APIs.  This value may be
 31694  * zero for the default behavior.
 31695  */
 31696 #ifndef SQLITE_WIN32_HEAP_FLAGS
 31697 #  define SQLITE_WIN32_HEAP_FLAGS     (0)
 31698 #endif
 31701 /*
 31702 ** The winMemData structure stores information required by the Win32-specific
 31703 ** sqlite3_mem_methods implementation.
 31704 */
 31705 typedef struct winMemData winMemData;
 31706 struct winMemData {
 31707 #ifndef NDEBUG
 31708   u32 magic1;   /* Magic number to detect structure corruption. */
 31709 #endif
 31710   HANDLE hHeap; /* The handle to our heap. */
 31711   BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
 31712 #ifndef NDEBUG
 31713   u32 magic2;   /* Magic number to detect structure corruption. */
 31714 #endif
 31715 };
 31717 #ifndef NDEBUG
 31718 #define WINMEM_MAGIC1     0x42b2830b
 31719 #define WINMEM_MAGIC2     0xbd4d7cf4
 31720 #endif
 31722 static struct winMemData win_mem_data = {
 31723 #ifndef NDEBUG
 31724   WINMEM_MAGIC1,
 31725 #endif
 31726   NULL, FALSE
 31727 #ifndef NDEBUG
 31728   ,WINMEM_MAGIC2
 31729 #endif
 31730 };
 31732 #ifndef NDEBUG
 31733 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
 31734 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
 31735 #define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
 31736 #else
 31737 #define winMemAssertMagic()
 31738 #endif
 31740 #define winMemGetDataPtr()  &win_mem_data
 31741 #define winMemGetHeap()     win_mem_data.hHeap
 31742 #define winMemGetOwned()    win_mem_data.bOwned
 31744 static void *winMemMalloc(int nBytes);
 31745 static void winMemFree(void *pPrior);
 31746 static void *winMemRealloc(void *pPrior, int nBytes);
 31747 static int winMemSize(void *p);
 31748 static int winMemRoundup(int n);
 31749 static int winMemInit(void *pAppData);
 31750 static void winMemShutdown(void *pAppData);
 31752 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
 31753 #endif /* SQLITE_WIN32_MALLOC */
 31755 /*
 31756 ** The following variable is (normally) set once and never changes
 31757 ** thereafter.  It records whether the operating system is Win9x
 31758 ** or WinNT.
 31759 **
 31760 ** 0:   Operating system unknown.
 31761 ** 1:   Operating system is Win9x.
 31762 ** 2:   Operating system is WinNT.
 31763 **
 31764 ** In order to facilitate testing on a WinNT system, the test fixture
 31765 ** can manually set this value to 1 to emulate Win98 behavior.
 31766 */
 31767 #ifdef SQLITE_TEST
 31768 SQLITE_API int sqlite3_os_type = 0;
 31769 #elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
 31770       defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
 31771 static int sqlite3_os_type = 0;
 31772 #endif
 31774 #ifndef SYSCALL
 31775 #  define SYSCALL sqlite3_syscall_ptr
 31776 #endif
 31778 /*
 31779 ** This function is not available on Windows CE or WinRT.
 31780  */
 31782 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
 31783 #  define osAreFileApisANSI()       1
 31784 #endif
 31786 /*
 31787 ** Many system calls are accessed through pointer-to-functions so that
 31788 ** they may be overridden at runtime to facilitate fault injection during
 31789 ** testing and sandboxing.  The following array holds the names and pointers
 31790 ** to all overrideable system calls.
 31791 */
 31792 static struct win_syscall {
 31793   const char *zName;            /* Name of the system call */
 31794   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
 31795   sqlite3_syscall_ptr pDefault; /* Default value */
 31796 } aSyscall[] = {
 31797 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 31798   { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
 31799 #else
 31800   { "AreFileApisANSI",         (SYSCALL)0,                       0 },
 31801 #endif
 31803 #ifndef osAreFileApisANSI
 31804 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
 31805 #endif
 31807 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 31808   { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
 31809 #else
 31810   { "CharLowerW",              (SYSCALL)0,                       0 },
 31811 #endif
 31813 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
 31815 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
 31816   { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
 31817 #else
 31818   { "CharUpperW",              (SYSCALL)0,                       0 },
 31819 #endif
 31821 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
 31823   { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
 31825 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
 31827 #if defined(SQLITE_WIN32_HAS_ANSI)
 31828   { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
 31829 #else
 31830   { "CreateFileA",             (SYSCALL)0,                       0 },
 31831 #endif
 31833 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
 31834         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
 31836 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 31837   { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
 31838 #else
 31839   { "CreateFileW",             (SYSCALL)0,                       0 },
 31840 #endif
 31842 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
 31843         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
 31845 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
 31846         !defined(SQLITE_OMIT_WAL))
 31847   { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
 31848 #else
 31849   { "CreateFileMappingA",      (SYSCALL)0,                       0 },
 31850 #endif
 31852 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 31853         DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
 31855 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 31856         !defined(SQLITE_OMIT_WAL))
 31857   { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
 31858 #else
 31859   { "CreateFileMappingW",      (SYSCALL)0,                       0 },
 31860 #endif
 31862 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
 31863         DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
 31865 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 31866   { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
 31867 #else
 31868   { "CreateMutexW",            (SYSCALL)0,                       0 },
 31869 #endif
 31871 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
 31872         LPCWSTR))aSyscall[8].pCurrent)
 31874 #if defined(SQLITE_WIN32_HAS_ANSI)
 31875   { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
 31876 #else
 31877   { "DeleteFileA",             (SYSCALL)0,                       0 },
 31878 #endif
 31880 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
 31882 #if defined(SQLITE_WIN32_HAS_WIDE)
 31883   { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
 31884 #else
 31885   { "DeleteFileW",             (SYSCALL)0,                       0 },
 31886 #endif
 31888 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
 31890 #if SQLITE_OS_WINCE
 31891   { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
 31892 #else
 31893   { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
 31894 #endif
 31896 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 31897         LPFILETIME))aSyscall[11].pCurrent)
 31899 #if SQLITE_OS_WINCE
 31900   { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
 31901 #else
 31902   { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
 31903 #endif
 31905 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
 31906         LPSYSTEMTIME))aSyscall[12].pCurrent)
 31908   { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
 31910 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
 31912 #if defined(SQLITE_WIN32_HAS_ANSI)
 31913   { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
 31914 #else
 31915   { "FormatMessageA",          (SYSCALL)0,                       0 },
 31916 #endif
 31918 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
 31919         DWORD,va_list*))aSyscall[14].pCurrent)
 31921 #if defined(SQLITE_WIN32_HAS_WIDE)
 31922   { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
 31923 #else
 31924   { "FormatMessageW",          (SYSCALL)0,                       0 },
 31925 #endif
 31927 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
 31928         DWORD,va_list*))aSyscall[15].pCurrent)
 31930 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 31931   { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
 31932 #else
 31933   { "FreeLibrary",             (SYSCALL)0,                       0 },
 31934 #endif
 31936 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
 31938   { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
 31940 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
 31942 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 31943   { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
 31944 #else
 31945   { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
 31946 #endif
 31948 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
 31949         LPDWORD))aSyscall[18].pCurrent)
 31951 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 31952   { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
 31953 #else
 31954   { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
 31955 #endif
 31957 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
 31958         LPDWORD))aSyscall[19].pCurrent)
 31960 #if defined(SQLITE_WIN32_HAS_ANSI)
 31961   { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
 31962 #else
 31963   { "GetFileAttributesA",      (SYSCALL)0,                       0 },
 31964 #endif
 31966 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
 31968 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 31969   { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
 31970 #else
 31971   { "GetFileAttributesW",      (SYSCALL)0,                       0 },
 31972 #endif
 31974 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
 31976 #if defined(SQLITE_WIN32_HAS_WIDE)
 31977   { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
 31978 #else
 31979   { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
 31980 #endif
 31982 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
 31983         LPVOID))aSyscall[22].pCurrent)
 31985 #if !SQLITE_OS_WINRT
 31986   { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
 31987 #else
 31988   { "GetFileSize",             (SYSCALL)0,                       0 },
 31989 #endif
 31991 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
 31993 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
 31994   { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
 31995 #else
 31996   { "GetFullPathNameA",        (SYSCALL)0,                       0 },
 31997 #endif
 31999 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
 32000         LPSTR*))aSyscall[24].pCurrent)
 32002 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 32003   { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
 32004 #else
 32005   { "GetFullPathNameW",        (SYSCALL)0,                       0 },
 32006 #endif
 32008 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
 32009         LPWSTR*))aSyscall[25].pCurrent)
 32011   { "GetLastError",            (SYSCALL)GetLastError,            0 },
 32013 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
 32015 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 32016 #if SQLITE_OS_WINCE
 32017   /* The GetProcAddressA() routine is only available on Windows CE. */
 32018   { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
 32019 #else
 32020   /* All other Windows platforms expect GetProcAddress() to take
 32021   ** an ANSI string regardless of the _UNICODE setting */
 32022   { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
 32023 #endif
 32024 #else
 32025   { "GetProcAddressA",         (SYSCALL)0,                       0 },
 32026 #endif
 32028 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
 32029         LPCSTR))aSyscall[27].pCurrent)
 32031 #if !SQLITE_OS_WINRT
 32032   { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
 32033 #else
 32034   { "GetSystemInfo",           (SYSCALL)0,                       0 },
 32035 #endif
 32037 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
 32039   { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
 32041 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
 32043 #if !SQLITE_OS_WINCE
 32044   { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
 32045 #else
 32046   { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
 32047 #endif
 32049 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
 32050         LPFILETIME))aSyscall[30].pCurrent)
 32052 #if defined(SQLITE_WIN32_HAS_ANSI)
 32053   { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
 32054 #else
 32055   { "GetTempPathA",            (SYSCALL)0,                       0 },
 32056 #endif
 32058 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
 32060 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
 32061   { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
 32062 #else
 32063   { "GetTempPathW",            (SYSCALL)0,                       0 },
 32064 #endif
 32066 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
 32068 #if !SQLITE_OS_WINRT
 32069   { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
 32070 #else
 32071   { "GetTickCount",            (SYSCALL)0,                       0 },
 32072 #endif
 32074 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
 32076 #if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
 32077         SQLITE_WIN32_GETVERSIONEX
 32078   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
 32079 #else
 32080   { "GetVersionExA",           (SYSCALL)0,                       0 },
 32081 #endif
 32083 #define osGetVersionExA ((BOOL(WINAPI*)( \
 32084         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
 32086 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 32087         defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
 32088   { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
 32089 #else
 32090   { "GetVersionExW",           (SYSCALL)0,                       0 },
 32091 #endif
 32093 #define osGetVersionExW ((BOOL(WINAPI*)( \
 32094         LPOSVERSIONINFOW))aSyscall[35].pCurrent)
 32096   { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
 32098 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
 32099         SIZE_T))aSyscall[36].pCurrent)
 32101 #if !SQLITE_OS_WINRT
 32102   { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
 32103 #else
 32104   { "HeapCreate",              (SYSCALL)0,                       0 },
 32105 #endif
 32107 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
 32108         SIZE_T))aSyscall[37].pCurrent)
 32110 #if !SQLITE_OS_WINRT
 32111   { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
 32112 #else
 32113   { "HeapDestroy",             (SYSCALL)0,                       0 },
 32114 #endif
 32116 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
 32118   { "HeapFree",                (SYSCALL)HeapFree,                0 },
 32120 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
 32122   { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
 32124 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
 32125         SIZE_T))aSyscall[40].pCurrent)
 32127   { "HeapSize",                (SYSCALL)HeapSize,                0 },
 32129 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
 32130         LPCVOID))aSyscall[41].pCurrent)
 32132 #if !SQLITE_OS_WINRT
 32133   { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
 32134 #else
 32135   { "HeapValidate",            (SYSCALL)0,                       0 },
 32136 #endif
 32138 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
 32139         LPCVOID))aSyscall[42].pCurrent)
 32141 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 32142   { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
 32143 #else
 32144   { "HeapCompact",             (SYSCALL)0,                       0 },
 32145 #endif
 32147 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
 32149 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 32150   { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
 32151 #else
 32152   { "LoadLibraryA",            (SYSCALL)0,                       0 },
 32153 #endif
 32155 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
 32157 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
 32158         !defined(SQLITE_OMIT_LOAD_EXTENSION)
 32159   { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
 32160 #else
 32161   { "LoadLibraryW",            (SYSCALL)0,                       0 },
 32162 #endif
 32164 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
 32166 #if !SQLITE_OS_WINRT
 32167   { "LocalFree",               (SYSCALL)LocalFree,               0 },
 32168 #else
 32169   { "LocalFree",               (SYSCALL)0,                       0 },
 32170 #endif
 32172 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
 32174 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 32175   { "LockFile",                (SYSCALL)LockFile,                0 },
 32176 #else
 32177   { "LockFile",                (SYSCALL)0,                       0 },
 32178 #endif
 32180 #ifndef osLockFile
 32181 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 32182         DWORD))aSyscall[47].pCurrent)
 32183 #endif
 32185 #if !SQLITE_OS_WINCE
 32186   { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
 32187 #else
 32188   { "LockFileEx",              (SYSCALL)0,                       0 },
 32189 #endif
 32191 #ifndef osLockFileEx
 32192 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
 32193         LPOVERLAPPED))aSyscall[48].pCurrent)
 32194 #endif
 32196 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
 32197   { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
 32198 #else
 32199   { "MapViewOfFile",           (SYSCALL)0,                       0 },
 32200 #endif
 32202 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 32203         SIZE_T))aSyscall[49].pCurrent)
 32205   { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
 32207 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
 32208         int))aSyscall[50].pCurrent)
 32210   { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
 32212 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
 32213         LARGE_INTEGER*))aSyscall[51].pCurrent)
 32215   { "ReadFile",                (SYSCALL)ReadFile,                0 },
 32217 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
 32218         LPOVERLAPPED))aSyscall[52].pCurrent)
 32220   { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
 32222 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
 32224 #if !SQLITE_OS_WINRT
 32225   { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
 32226 #else
 32227   { "SetFilePointer",          (SYSCALL)0,                       0 },
 32228 #endif
 32230 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
 32231         DWORD))aSyscall[54].pCurrent)
 32233 #if !SQLITE_OS_WINRT
 32234   { "Sleep",                   (SYSCALL)Sleep,                   0 },
 32235 #else
 32236   { "Sleep",                   (SYSCALL)0,                       0 },
 32237 #endif
 32239 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
 32241   { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
 32243 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
 32244         LPFILETIME))aSyscall[56].pCurrent)
 32246 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 32247   { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
 32248 #else
 32249   { "UnlockFile",              (SYSCALL)0,                       0 },
 32250 #endif
 32252 #ifndef osUnlockFile
 32253 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 32254         DWORD))aSyscall[57].pCurrent)
 32255 #endif
 32257 #if !SQLITE_OS_WINCE
 32258   { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
 32259 #else
 32260   { "UnlockFileEx",            (SYSCALL)0,                       0 },
 32261 #endif
 32263 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
 32264         LPOVERLAPPED))aSyscall[58].pCurrent)
 32266 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
 32267   { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
 32268 #else
 32269   { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
 32270 #endif
 32272 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
 32274   { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
 32276 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
 32277         LPCSTR,LPBOOL))aSyscall[60].pCurrent)
 32279   { "WriteFile",               (SYSCALL)WriteFile,               0 },
 32281 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
 32282         LPOVERLAPPED))aSyscall[61].pCurrent)
 32284 #if SQLITE_OS_WINRT
 32285   { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
 32286 #else
 32287   { "CreateEventExW",          (SYSCALL)0,                       0 },
 32288 #endif
 32290 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
 32291         DWORD,DWORD))aSyscall[62].pCurrent)
 32293 #if !SQLITE_OS_WINRT
 32294   { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
 32295 #else
 32296   { "WaitForSingleObject",     (SYSCALL)0,                       0 },
 32297 #endif
 32299 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
 32300         DWORD))aSyscall[63].pCurrent)
 32302 #if SQLITE_OS_WINRT
 32303   { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
 32304 #else
 32305   { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
 32306 #endif
 32308 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
 32309         BOOL))aSyscall[64].pCurrent)
 32311 #if SQLITE_OS_WINRT
 32312   { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
 32313 #else
 32314   { "SetFilePointerEx",        (SYSCALL)0,                       0 },
 32315 #endif
 32317 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
 32318         PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
 32320 #if SQLITE_OS_WINRT
 32321   { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
 32322 #else
 32323   { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
 32324 #endif
 32326 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
 32327         FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
 32329 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 32330   { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
 32331 #else
 32332   { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
 32333 #endif
 32335 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
 32336         SIZE_T))aSyscall[67].pCurrent)
 32338 #if SQLITE_OS_WINRT
 32339   { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
 32340 #else
 32341   { "CreateFile2",             (SYSCALL)0,                       0 },
 32342 #endif
 32344 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
 32345         LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
 32347 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
 32348   { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
 32349 #else
 32350   { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
 32351 #endif
 32353 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
 32354         DWORD))aSyscall[69].pCurrent)
 32356 #if SQLITE_OS_WINRT
 32357   { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
 32358 #else
 32359   { "GetTickCount64",          (SYSCALL)0,                       0 },
 32360 #endif
 32362 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
 32364 #if SQLITE_OS_WINRT
 32365   { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
 32366 #else
 32367   { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
 32368 #endif
 32370 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
 32371         LPSYSTEM_INFO))aSyscall[71].pCurrent)
 32373 #if defined(SQLITE_WIN32_HAS_ANSI)
 32374   { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
 32375 #else
 32376   { "OutputDebugStringA",      (SYSCALL)0,                       0 },
 32377 #endif
 32379 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
 32381 #if defined(SQLITE_WIN32_HAS_WIDE)
 32382   { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
 32383 #else
 32384   { "OutputDebugStringW",      (SYSCALL)0,                       0 },
 32385 #endif
 32387 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
 32389   { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
 32391 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
 32393 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
 32394   { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
 32395 #else
 32396   { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
 32397 #endif
 32399 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
 32400         LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
 32402 }; /* End of the overrideable system calls */
 32404 /*
 32405 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
 32406 ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
 32407 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
 32408 ** system call named zName.
 32409 */
 32410 static int winSetSystemCall(
 32411   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
 32412   const char *zName,            /* Name of system call to override */
 32413   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
 32414 ){
 32415   unsigned int i;
 32416   int rc = SQLITE_NOTFOUND;
 32418   UNUSED_PARAMETER(pNotUsed);
 32419   if( zName==0 ){
 32420     /* If no zName is given, restore all system calls to their default
 32421     ** settings and return NULL
 32422     */
 32423     rc = SQLITE_OK;
 32424     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 32425       if( aSyscall[i].pDefault ){
 32426         aSyscall[i].pCurrent = aSyscall[i].pDefault;
 32429   }else{
 32430     /* If zName is specified, operate on only the one system call
 32431     ** specified.
 32432     */
 32433     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 32434       if( strcmp(zName, aSyscall[i].zName)==0 ){
 32435         if( aSyscall[i].pDefault==0 ){
 32436           aSyscall[i].pDefault = aSyscall[i].pCurrent;
 32438         rc = SQLITE_OK;
 32439         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
 32440         aSyscall[i].pCurrent = pNewFunc;
 32441         break;
 32445   return rc;
 32448 /*
 32449 ** Return the value of a system call.  Return NULL if zName is not a
 32450 ** recognized system call name.  NULL is also returned if the system call
 32451 ** is currently undefined.
 32452 */
 32453 static sqlite3_syscall_ptr winGetSystemCall(
 32454   sqlite3_vfs *pNotUsed,
 32455   const char *zName
 32456 ){
 32457   unsigned int i;
 32459   UNUSED_PARAMETER(pNotUsed);
 32460   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
 32461     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
 32463   return 0;
 32466 /*
 32467 ** Return the name of the first system call after zName.  If zName==NULL
 32468 ** then return the name of the first system call.  Return NULL if zName
 32469 ** is the last system call or if zName is not the name of a valid
 32470 ** system call.
 32471 */
 32472 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
 32473   int i = -1;
 32475   UNUSED_PARAMETER(p);
 32476   if( zName ){
 32477     for(i=0; i<ArraySize(aSyscall)-1; i++){
 32478       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
 32481   for(i++; i<ArraySize(aSyscall); i++){
 32482     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
 32484   return 0;
 32487 #ifdef SQLITE_WIN32_MALLOC
 32488 /*
 32489 ** If a Win32 native heap has been configured, this function will attempt to
 32490 ** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
 32491 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
 32492 ** "pnLargest" argument, if non-zero, will be used to return the size of the
 32493 ** largest committed free block in the heap, in bytes.
 32494 */
 32495 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
 32496   int rc = SQLITE_OK;
 32497   UINT nLargest = 0;
 32498   HANDLE hHeap;
 32500   winMemAssertMagic();
 32501   hHeap = winMemGetHeap();
 32502   assert( hHeap!=0 );
 32503   assert( hHeap!=INVALID_HANDLE_VALUE );
 32504 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32505   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 32506 #endif
 32507 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
 32508   if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
 32509     DWORD lastErrno = osGetLastError();
 32510     if( lastErrno==NO_ERROR ){
 32511       sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
 32512                   (void*)hHeap);
 32513       rc = SQLITE_NOMEM;
 32514     }else{
 32515       sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
 32516                   osGetLastError(), (void*)hHeap);
 32517       rc = SQLITE_ERROR;
 32520 #else
 32521   sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
 32522               (void*)hHeap);
 32523   rc = SQLITE_NOTFOUND;
 32524 #endif
 32525   if( pnLargest ) *pnLargest = nLargest;
 32526   return rc;
 32529 /*
 32530 ** If a Win32 native heap has been configured, this function will attempt to
 32531 ** destroy and recreate it.  If the Win32 native heap is not isolated and/or
 32532 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
 32533 ** be returned and no changes will be made to the Win32 native heap.
 32534 */
 32535 SQLITE_API int sqlite3_win32_reset_heap(){
 32536   int rc;
 32537   MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
 32538   MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
 32539   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 32540   MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
 32541   sqlite3_mutex_enter(pMaster);
 32542   sqlite3_mutex_enter(pMem);
 32543   winMemAssertMagic();
 32544   if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
 32545     /*
 32546     ** At this point, there should be no outstanding memory allocations on
 32547     ** the heap.  Also, since both the master and memsys locks are currently
 32548     ** being held by us, no other function (i.e. from another thread) should
 32549     ** be able to even access the heap.  Attempt to destroy and recreate our
 32550     ** isolated Win32 native heap now.
 32551     */
 32552     assert( winMemGetHeap()!=NULL );
 32553     assert( winMemGetOwned() );
 32554     assert( sqlite3_memory_used()==0 );
 32555     winMemShutdown(winMemGetDataPtr());
 32556     assert( winMemGetHeap()==NULL );
 32557     assert( !winMemGetOwned() );
 32558     assert( sqlite3_memory_used()==0 );
 32559     rc = winMemInit(winMemGetDataPtr());
 32560     assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
 32561     assert( rc!=SQLITE_OK || winMemGetOwned() );
 32562     assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
 32563   }else{
 32564     /*
 32565     ** The Win32 native heap cannot be modified because it may be in use.
 32566     */
 32567     rc = SQLITE_BUSY;
 32569   sqlite3_mutex_leave(pMem);
 32570   sqlite3_mutex_leave(pMaster);
 32571   return rc;
 32573 #endif /* SQLITE_WIN32_MALLOC */
 32575 /*
 32576 ** This function outputs the specified (ANSI) string to the Win32 debugger
 32577 ** (if available).
 32578 */
 32580 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
 32581   char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
 32582   int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
 32583   if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
 32584   assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
 32585 #if defined(SQLITE_WIN32_HAS_ANSI)
 32586   if( nMin>0 ){
 32587     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 32588     memcpy(zDbgBuf, zBuf, nMin);
 32589     osOutputDebugStringA(zDbgBuf);
 32590   }else{
 32591     osOutputDebugStringA(zBuf);
 32593 #elif defined(SQLITE_WIN32_HAS_WIDE)
 32594   memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 32595   if ( osMultiByteToWideChar(
 32596           osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
 32597           nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
 32598     return;
 32600   osOutputDebugStringW((LPCWSTR)zDbgBuf);
 32601 #else
 32602   if( nMin>0 ){
 32603     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
 32604     memcpy(zDbgBuf, zBuf, nMin);
 32605     fprintf(stderr, "%s", zDbgBuf);
 32606   }else{
 32607     fprintf(stderr, "%s", zBuf);
 32609 #endif
 32612 /*
 32613 ** The following routine suspends the current thread for at least ms
 32614 ** milliseconds.  This is equivalent to the Win32 Sleep() interface.
 32615 */
 32616 #if SQLITE_OS_WINRT
 32617 static HANDLE sleepObj = NULL;
 32618 #endif
 32620 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
 32621 #if SQLITE_OS_WINRT
 32622   if ( sleepObj==NULL ){
 32623     sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
 32624                                 SYNCHRONIZE);
 32626   assert( sleepObj!=NULL );
 32627   osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
 32628 #else
 32629   osSleep(milliseconds);
 32630 #endif
 32633 /*
 32634 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 32635 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
 32636 **
 32637 ** Here is an interesting observation:  Win95, Win98, and WinME lack
 32638 ** the LockFileEx() API.  But we can still statically link against that
 32639 ** API as long as we don't call it when running Win95/98/ME.  A call to
 32640 ** this routine is used to determine if the host is Win95/98/ME or
 32641 ** WinNT/2K/XP so that we will know whether or not we can safely call
 32642 ** the LockFileEx() API.
 32643 */
 32645 #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
 32646 # define osIsNT()  (1)
 32647 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
 32648 # define osIsNT()  (1)
 32649 #elif !defined(SQLITE_WIN32_HAS_WIDE)
 32650 # define osIsNT()  (0)
 32651 #else
 32652   static int osIsNT(void){
 32653     if( sqlite3_os_type==0 ){
 32654 #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
 32655       OSVERSIONINFOW sInfo;
 32656       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 32657       osGetVersionExW(&sInfo);
 32658 #else
 32659       OSVERSIONINFOA sInfo;
 32660       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
 32661       osGetVersionExA(&sInfo);
 32662 #endif
 32663       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
 32665     return sqlite3_os_type==2;
 32667 #endif
 32669 #ifdef SQLITE_WIN32_MALLOC
 32670 /*
 32671 ** Allocate nBytes of memory.
 32672 */
 32673 static void *winMemMalloc(int nBytes){
 32674   HANDLE hHeap;
 32675   void *p;
 32677   winMemAssertMagic();
 32678   hHeap = winMemGetHeap();
 32679   assert( hHeap!=0 );
 32680   assert( hHeap!=INVALID_HANDLE_VALUE );
 32681 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32682   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 32683 #endif
 32684   assert( nBytes>=0 );
 32685   p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 32686   if( !p ){
 32687     sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
 32688                 nBytes, osGetLastError(), (void*)hHeap);
 32690   return p;
 32693 /*
 32694 ** Free memory.
 32695 */
 32696 static void winMemFree(void *pPrior){
 32697   HANDLE hHeap;
 32699   winMemAssertMagic();
 32700   hHeap = winMemGetHeap();
 32701   assert( hHeap!=0 );
 32702   assert( hHeap!=INVALID_HANDLE_VALUE );
 32703 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32704   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 32705 #endif
 32706   if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
 32707   if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
 32708     sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
 32709                 pPrior, osGetLastError(), (void*)hHeap);
 32713 /*
 32714 ** Change the size of an existing memory allocation
 32715 */
 32716 static void *winMemRealloc(void *pPrior, int nBytes){
 32717   HANDLE hHeap;
 32718   void *p;
 32720   winMemAssertMagic();
 32721   hHeap = winMemGetHeap();
 32722   assert( hHeap!=0 );
 32723   assert( hHeap!=INVALID_HANDLE_VALUE );
 32724 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32725   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
 32726 #endif
 32727   assert( nBytes>=0 );
 32728   if( !pPrior ){
 32729     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
 32730   }else{
 32731     p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
 32733   if( !p ){
 32734     sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
 32735                 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
 32736                 (void*)hHeap);
 32738   return p;
 32741 /*
 32742 ** Return the size of an outstanding allocation, in bytes.
 32743 */
 32744 static int winMemSize(void *p){
 32745   HANDLE hHeap;
 32746   SIZE_T n;
 32748   winMemAssertMagic();
 32749   hHeap = winMemGetHeap();
 32750   assert( hHeap!=0 );
 32751   assert( hHeap!=INVALID_HANDLE_VALUE );
 32752 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32753   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
 32754 #endif
 32755   if( !p ) return 0;
 32756   n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
 32757   if( n==(SIZE_T)-1 ){
 32758     sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
 32759                 p, osGetLastError(), (void*)hHeap);
 32760     return 0;
 32762   return (int)n;
 32765 /*
 32766 ** Round up a request size to the next valid allocation size.
 32767 */
 32768 static int winMemRoundup(int n){
 32769   return n;
 32772 /*
 32773 ** Initialize this module.
 32774 */
 32775 static int winMemInit(void *pAppData){
 32776   winMemData *pWinMemData = (winMemData *)pAppData;
 32778   if( !pWinMemData ) return SQLITE_ERROR;
 32779   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
 32780   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
 32782 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
 32783   if( !pWinMemData->hHeap ){
 32784     DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
 32785     DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
 32786     if( dwMaximumSize==0 ){
 32787       dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
 32788     }else if( dwInitialSize>dwMaximumSize ){
 32789       dwInitialSize = dwMaximumSize;
 32791     pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
 32792                                       dwInitialSize, dwMaximumSize);
 32793     if( !pWinMemData->hHeap ){
 32794       sqlite3_log(SQLITE_NOMEM,
 32795           "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
 32796           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
 32797           dwMaximumSize);
 32798       return SQLITE_NOMEM;
 32800     pWinMemData->bOwned = TRUE;
 32801     assert( pWinMemData->bOwned );
 32803 #else
 32804   pWinMemData->hHeap = osGetProcessHeap();
 32805   if( !pWinMemData->hHeap ){
 32806     sqlite3_log(SQLITE_NOMEM,
 32807         "failed to GetProcessHeap (%lu)", osGetLastError());
 32808     return SQLITE_NOMEM;
 32810   pWinMemData->bOwned = FALSE;
 32811   assert( !pWinMemData->bOwned );
 32812 #endif
 32813   assert( pWinMemData->hHeap!=0 );
 32814   assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 32815 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32816   assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 32817 #endif
 32818   return SQLITE_OK;
 32821 /*
 32822 ** Deinitialize this module.
 32823 */
 32824 static void winMemShutdown(void *pAppData){
 32825   winMemData *pWinMemData = (winMemData *)pAppData;
 32827   if( !pWinMemData ) return;
 32828   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
 32829   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
 32831   if( pWinMemData->hHeap ){
 32832     assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
 32833 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
 32834     assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
 32835 #endif
 32836     if( pWinMemData->bOwned ){
 32837       if( !osHeapDestroy(pWinMemData->hHeap) ){
 32838         sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
 32839                     osGetLastError(), (void*)pWinMemData->hHeap);
 32841       pWinMemData->bOwned = FALSE;
 32843     pWinMemData->hHeap = NULL;
 32847 /*
 32848 ** Populate the low-level memory allocation function pointers in
 32849 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
 32850 ** arguments specify the block of memory to manage.
 32851 **
 32852 ** This routine is only called by sqlite3_config(), and therefore
 32853 ** is not required to be threadsafe (it is not).
 32854 */
 32855 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
 32856   static const sqlite3_mem_methods winMemMethods = {
 32857     winMemMalloc,
 32858     winMemFree,
 32859     winMemRealloc,
 32860     winMemSize,
 32861     winMemRoundup,
 32862     winMemInit,
 32863     winMemShutdown,
 32864     &win_mem_data
 32865   };
 32866   return &winMemMethods;
 32869 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
 32870   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
 32872 #endif /* SQLITE_WIN32_MALLOC */
 32874 /*
 32875 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
 32876 **
 32877 ** Space to hold the returned string is obtained from malloc.
 32878 */
 32879 static LPWSTR winUtf8ToUnicode(const char *zFilename){
 32880   int nChar;
 32881   LPWSTR zWideFilename;
 32883   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
 32884   if( nChar==0 ){
 32885     return 0;
 32887   zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
 32888   if( zWideFilename==0 ){
 32889     return 0;
 32891   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
 32892                                 nChar);
 32893   if( nChar==0 ){
 32894     sqlite3_free(zWideFilename);
 32895     zWideFilename = 0;
 32897   return zWideFilename;
 32900 /*
 32901 ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
 32902 ** obtained from sqlite3_malloc().
 32903 */
 32904 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
 32905   int nByte;
 32906   char *zFilename;
 32908   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
 32909   if( nByte == 0 ){
 32910     return 0;
 32912   zFilename = sqlite3MallocZero( nByte );
 32913   if( zFilename==0 ){
 32914     return 0;
 32916   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
 32917                                 0, 0);
 32918   if( nByte == 0 ){
 32919     sqlite3_free(zFilename);
 32920     zFilename = 0;
 32922   return zFilename;
 32925 /*
 32926 ** Convert an ANSI string to Microsoft Unicode, based on the
 32927 ** current codepage settings for file apis.
 32928 ** 
 32929 ** Space to hold the returned string is obtained
 32930 ** from sqlite3_malloc.
 32931 */
 32932 static LPWSTR winMbcsToUnicode(const char *zFilename){
 32933   int nByte;
 32934   LPWSTR zMbcsFilename;
 32935   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 32937   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
 32938                                 0)*sizeof(WCHAR);
 32939   if( nByte==0 ){
 32940     return 0;
 32942   zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
 32943   if( zMbcsFilename==0 ){
 32944     return 0;
 32946   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
 32947                                 nByte);
 32948   if( nByte==0 ){
 32949     sqlite3_free(zMbcsFilename);
 32950     zMbcsFilename = 0;
 32952   return zMbcsFilename;
 32955 /*
 32956 ** Convert Microsoft Unicode to multi-byte character string, based on the
 32957 ** user's ANSI codepage.
 32958 **
 32959 ** Space to hold the returned string is obtained from
 32960 ** sqlite3_malloc().
 32961 */
 32962 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
 32963   int nByte;
 32964   char *zFilename;
 32965   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
 32967   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
 32968   if( nByte == 0 ){
 32969     return 0;
 32971   zFilename = sqlite3MallocZero( nByte );
 32972   if( zFilename==0 ){
 32973     return 0;
 32975   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
 32976                                 nByte, 0, 0);
 32977   if( nByte == 0 ){
 32978     sqlite3_free(zFilename);
 32979     zFilename = 0;
 32981   return zFilename;
 32984 /*
 32985 ** Convert multibyte character string to UTF-8.  Space to hold the
 32986 ** returned string is obtained from sqlite3_malloc().
 32987 */
 32988 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
 32989   char *zFilenameUtf8;
 32990   LPWSTR zTmpWide;
 32992   zTmpWide = winMbcsToUnicode(zFilename);
 32993   if( zTmpWide==0 ){
 32994     return 0;
 32996   zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
 32997   sqlite3_free(zTmpWide);
 32998   return zFilenameUtf8;
 33001 /*
 33002 ** Convert UTF-8 to multibyte character string.  Space to hold the 
 33003 ** returned string is obtained from sqlite3_malloc().
 33004 */
 33005 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
 33006   char *zFilenameMbcs;
 33007   LPWSTR zTmpWide;
 33009   zTmpWide = winUtf8ToUnicode(zFilename);
 33010   if( zTmpWide==0 ){
 33011     return 0;
 33013   zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
 33014   sqlite3_free(zTmpWide);
 33015   return zFilenameMbcs;
 33018 /*
 33019 ** This function sets the data directory or the temporary directory based on
 33020 ** the provided arguments.  The type argument must be 1 in order to set the
 33021 ** data directory or 2 in order to set the temporary directory.  The zValue
 33022 ** argument is the name of the directory to use.  The return value will be
 33023 ** SQLITE_OK if successful.
 33024 */
 33025 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
 33026   char **ppDirectory = 0;
 33027 #ifndef SQLITE_OMIT_AUTOINIT
 33028   int rc = sqlite3_initialize();
 33029   if( rc ) return rc;
 33030 #endif
 33031   if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
 33032     ppDirectory = &sqlite3_data_directory;
 33033   }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
 33034     ppDirectory = &sqlite3_temp_directory;
 33036   assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
 33037           || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
 33038   );
 33039   assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
 33040   if( ppDirectory ){
 33041     char *zValueUtf8 = 0;
 33042     if( zValue && zValue[0] ){
 33043       zValueUtf8 = winUnicodeToUtf8(zValue);
 33044       if ( zValueUtf8==0 ){
 33045         return SQLITE_NOMEM;
 33048     sqlite3_free(*ppDirectory);
 33049     *ppDirectory = zValueUtf8;
 33050     return SQLITE_OK;
 33052   return SQLITE_ERROR;
 33055 /*
 33056 ** The return value of winGetLastErrorMsg
 33057 ** is zero if the error message fits in the buffer, or non-zero
 33058 ** otherwise (if the message was truncated).
 33059 */
 33060 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
 33061   /* FormatMessage returns 0 on failure.  Otherwise it
 33062   ** returns the number of TCHARs written to the output
 33063   ** buffer, excluding the terminating null char.
 33064   */
 33065   DWORD dwLen = 0;
 33066   char *zOut = 0;
 33068   if( osIsNT() ){
 33069 #if SQLITE_OS_WINRT
 33070     WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
 33071     dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
 33072                              FORMAT_MESSAGE_IGNORE_INSERTS,
 33073                              NULL,
 33074                              lastErrno,
 33075                              0,
 33076                              zTempWide,
 33077                              SQLITE_WIN32_MAX_ERRMSG_CHARS,
 33078                              0);
 33079 #else
 33080     LPWSTR zTempWide = NULL;
 33081     dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 33082                              FORMAT_MESSAGE_FROM_SYSTEM |
 33083                              FORMAT_MESSAGE_IGNORE_INSERTS,
 33084                              NULL,
 33085                              lastErrno,
 33086                              0,
 33087                              (LPWSTR) &zTempWide,
 33088                              0,
 33089                              0);
 33090 #endif
 33091     if( dwLen > 0 ){
 33092       /* allocate a buffer and convert to UTF8 */
 33093       sqlite3BeginBenignMalloc();
 33094       zOut = winUnicodeToUtf8(zTempWide);
 33095       sqlite3EndBenignMalloc();
 33096 #if !SQLITE_OS_WINRT
 33097       /* free the system buffer allocated by FormatMessage */
 33098       osLocalFree(zTempWide);
 33099 #endif
 33102 #ifdef SQLITE_WIN32_HAS_ANSI
 33103   else{
 33104     char *zTemp = NULL;
 33105     dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
 33106                              FORMAT_MESSAGE_FROM_SYSTEM |
 33107                              FORMAT_MESSAGE_IGNORE_INSERTS,
 33108                              NULL,
 33109                              lastErrno,
 33110                              0,
 33111                              (LPSTR) &zTemp,
 33112                              0,
 33113                              0);
 33114     if( dwLen > 0 ){
 33115       /* allocate a buffer and convert to UTF8 */
 33116       sqlite3BeginBenignMalloc();
 33117       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 33118       sqlite3EndBenignMalloc();
 33119       /* free the system buffer allocated by FormatMessage */
 33120       osLocalFree(zTemp);
 33123 #endif
 33124   if( 0 == dwLen ){
 33125     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
 33126   }else{
 33127     /* copy a maximum of nBuf chars to output buffer */
 33128     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
 33129     /* free the UTF8 buffer */
 33130     sqlite3_free(zOut);
 33132   return 0;
 33135 /*
 33136 **
 33137 ** This function - winLogErrorAtLine() - is only ever called via the macro
 33138 ** winLogError().
 33139 **
 33140 ** This routine is invoked after an error occurs in an OS function.
 33141 ** It logs a message using sqlite3_log() containing the current value of
 33142 ** error code and, if possible, the human-readable equivalent from 
 33143 ** FormatMessage.
 33144 **
 33145 ** The first argument passed to the macro should be the error code that
 33146 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
 33147 ** The two subsequent arguments should be the name of the OS function that
 33148 ** failed and the associated file-system path, if any.
 33149 */
 33150 #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
 33151 static int winLogErrorAtLine(
 33152   int errcode,                    /* SQLite error code */
 33153   DWORD lastErrno,                /* Win32 last error */
 33154   const char *zFunc,              /* Name of OS function that failed */
 33155   const char *zPath,              /* File path associated with error */
 33156   int iLine                       /* Source line number where error occurred */
 33157 ){
 33158   char zMsg[500];                 /* Human readable error text */
 33159   int i;                          /* Loop counter */
 33161   zMsg[0] = 0;
 33162   winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
 33163   assert( errcode!=SQLITE_OK );
 33164   if( zPath==0 ) zPath = "";
 33165   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
 33166   zMsg[i] = 0;
 33167   sqlite3_log(errcode,
 33168       "os_win.c:%d: (%lu) %s(%s) - %s",
 33169       iLine, lastErrno, zFunc, zPath, zMsg
 33170   );
 33172   return errcode;
 33175 /*
 33176 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
 33177 ** will be retried following a locking error - probably caused by 
 33178 ** antivirus software.  Also the initial delay before the first retry.
 33179 ** The delay increases linearly with each retry.
 33180 */
 33181 #ifndef SQLITE_WIN32_IOERR_RETRY
 33182 # define SQLITE_WIN32_IOERR_RETRY 10
 33183 #endif
 33184 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
 33185 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
 33186 #endif
 33187 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
 33188 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
 33190 /*
 33191 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
 33192 ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
 33193 ** to give up with an error.
 33194 */
 33195 static int winRetryIoerr(int *pnRetry, DWORD *pError){
 33196   DWORD e = osGetLastError();
 33197   if( *pnRetry>=winIoerrRetry ){
 33198     if( pError ){
 33199       *pError = e;
 33201     return 0;
 33203   if( e==ERROR_ACCESS_DENIED ||
 33204       e==ERROR_LOCK_VIOLATION ||
 33205       e==ERROR_SHARING_VIOLATION ){
 33206     sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
 33207     ++*pnRetry;
 33208     return 1;
 33210   if( pError ){
 33211     *pError = e;
 33213   return 0;
 33216 /*
 33217 ** Log a I/O error retry episode.
 33218 */
 33219 static void winLogIoerr(int nRetry){
 33220   if( nRetry ){
 33221     sqlite3_log(SQLITE_IOERR, 
 33222       "delayed %dms for lock/sharing conflict",
 33223       winIoerrRetryDelay*nRetry*(nRetry+1)/2
 33224     );
 33228 #if SQLITE_OS_WINCE
 33229 /*************************************************************************
 33230 ** This section contains code for WinCE only.
 33231 */
 33232 #if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
 33233 /*
 33234 ** The MSVC CRT on Windows CE may not have a localtime() function.  So
 33235 ** create a substitute.
 33236 */
 33237 /* #include <time.h> */
 33238 struct tm *__cdecl localtime(const time_t *t)
 33240   static struct tm y;
 33241   FILETIME uTm, lTm;
 33242   SYSTEMTIME pTm;
 33243   sqlite3_int64 t64;
 33244   t64 = *t;
 33245   t64 = (t64 + 11644473600)*10000000;
 33246   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
 33247   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
 33248   osFileTimeToLocalFileTime(&uTm,&lTm);
 33249   osFileTimeToSystemTime(&lTm,&pTm);
 33250   y.tm_year = pTm.wYear - 1900;
 33251   y.tm_mon = pTm.wMonth - 1;
 33252   y.tm_wday = pTm.wDayOfWeek;
 33253   y.tm_mday = pTm.wDay;
 33254   y.tm_hour = pTm.wHour;
 33255   y.tm_min = pTm.wMinute;
 33256   y.tm_sec = pTm.wSecond;
 33257   return &y;
 33259 #endif
 33261 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
 33263 /*
 33264 ** Acquire a lock on the handle h
 33265 */
 33266 static void winceMutexAcquire(HANDLE h){
 33267    DWORD dwErr;
 33268    do {
 33269      dwErr = osWaitForSingleObject(h, INFINITE);
 33270    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
 33272 /*
 33273 ** Release a lock acquired by winceMutexAcquire()
 33274 */
 33275 #define winceMutexRelease(h) ReleaseMutex(h)
 33277 /*
 33278 ** Create the mutex and shared memory used for locking in the file
 33279 ** descriptor pFile
 33280 */
 33281 static int winceCreateLock(const char *zFilename, winFile *pFile){
 33282   LPWSTR zTok;
 33283   LPWSTR zName;
 33284   DWORD lastErrno;
 33285   BOOL bLogged = FALSE;
 33286   BOOL bInit = TRUE;
 33288   zName = winUtf8ToUnicode(zFilename);
 33289   if( zName==0 ){
 33290     /* out of memory */
 33291     return SQLITE_IOERR_NOMEM;
 33294   /* Initialize the local lockdata */
 33295   memset(&pFile->local, 0, sizeof(pFile->local));
 33297   /* Replace the backslashes from the filename and lowercase it
 33298   ** to derive a mutex name. */
 33299   zTok = osCharLowerW(zName);
 33300   for (;*zTok;zTok++){
 33301     if (*zTok == '\\') *zTok = '_';
 33304   /* Create/open the named mutex */
 33305   pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
 33306   if (!pFile->hMutex){
 33307     pFile->lastErrno = osGetLastError();
 33308     sqlite3_free(zName);
 33309     return winLogError(SQLITE_IOERR, pFile->lastErrno,
 33310                        "winceCreateLock1", zFilename);
 33313   /* Acquire the mutex before continuing */
 33314   winceMutexAcquire(pFile->hMutex);
 33316   /* Since the names of named mutexes, semaphores, file mappings etc are 
 33317   ** case-sensitive, take advantage of that by uppercasing the mutex name
 33318   ** and using that as the shared filemapping name.
 33319   */
 33320   osCharUpperW(zName);
 33321   pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
 33322                                         PAGE_READWRITE, 0, sizeof(winceLock),
 33323                                         zName);  
 33325   /* Set a flag that indicates we're the first to create the memory so it 
 33326   ** must be zero-initialized */
 33327   lastErrno = osGetLastError();
 33328   if (lastErrno == ERROR_ALREADY_EXISTS){
 33329     bInit = FALSE;
 33332   sqlite3_free(zName);
 33334   /* If we succeeded in making the shared memory handle, map it. */
 33335   if( pFile->hShared ){
 33336     pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
 33337              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
 33338     /* If mapping failed, close the shared memory handle and erase it */
 33339     if( !pFile->shared ){
 33340       pFile->lastErrno = osGetLastError();
 33341       winLogError(SQLITE_IOERR, pFile->lastErrno,
 33342                   "winceCreateLock2", zFilename);
 33343       bLogged = TRUE;
 33344       osCloseHandle(pFile->hShared);
 33345       pFile->hShared = NULL;
 33349   /* If shared memory could not be created, then close the mutex and fail */
 33350   if( pFile->hShared==NULL ){
 33351     if( !bLogged ){
 33352       pFile->lastErrno = lastErrno;
 33353       winLogError(SQLITE_IOERR, pFile->lastErrno,
 33354                   "winceCreateLock3", zFilename);
 33355       bLogged = TRUE;
 33357     winceMutexRelease(pFile->hMutex);
 33358     osCloseHandle(pFile->hMutex);
 33359     pFile->hMutex = NULL;
 33360     return SQLITE_IOERR;
 33363   /* Initialize the shared memory if we're supposed to */
 33364   if( bInit ){
 33365     memset(pFile->shared, 0, sizeof(winceLock));
 33368   winceMutexRelease(pFile->hMutex);
 33369   return SQLITE_OK;
 33372 /*
 33373 ** Destroy the part of winFile that deals with wince locks
 33374 */
 33375 static void winceDestroyLock(winFile *pFile){
 33376   if (pFile->hMutex){
 33377     /* Acquire the mutex */
 33378     winceMutexAcquire(pFile->hMutex);
 33380     /* The following blocks should probably assert in debug mode, but they
 33381        are to cleanup in case any locks remained open */
 33382     if (pFile->local.nReaders){
 33383       pFile->shared->nReaders --;
 33385     if (pFile->local.bReserved){
 33386       pFile->shared->bReserved = FALSE;
 33388     if (pFile->local.bPending){
 33389       pFile->shared->bPending = FALSE;
 33391     if (pFile->local.bExclusive){
 33392       pFile->shared->bExclusive = FALSE;
 33395     /* De-reference and close our copy of the shared memory handle */
 33396     osUnmapViewOfFile(pFile->shared);
 33397     osCloseHandle(pFile->hShared);
 33399     /* Done with the mutex */
 33400     winceMutexRelease(pFile->hMutex);    
 33401     osCloseHandle(pFile->hMutex);
 33402     pFile->hMutex = NULL;
 33406 /* 
 33407 ** An implementation of the LockFile() API of Windows for CE
 33408 */
 33409 static BOOL winceLockFile(
 33410   LPHANDLE phFile,
 33411   DWORD dwFileOffsetLow,
 33412   DWORD dwFileOffsetHigh,
 33413   DWORD nNumberOfBytesToLockLow,
 33414   DWORD nNumberOfBytesToLockHigh
 33415 ){
 33416   winFile *pFile = HANDLE_TO_WINFILE(phFile);
 33417   BOOL bReturn = FALSE;
 33419   UNUSED_PARAMETER(dwFileOffsetHigh);
 33420   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
 33422   if (!pFile->hMutex) return TRUE;
 33423   winceMutexAcquire(pFile->hMutex);
 33425   /* Wanting an exclusive lock? */
 33426   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
 33427        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
 33428     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
 33429        pFile->shared->bExclusive = TRUE;
 33430        pFile->local.bExclusive = TRUE;
 33431        bReturn = TRUE;
 33435   /* Want a read-only lock? */
 33436   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
 33437            nNumberOfBytesToLockLow == 1){
 33438     if (pFile->shared->bExclusive == 0){
 33439       pFile->local.nReaders ++;
 33440       if (pFile->local.nReaders == 1){
 33441         pFile->shared->nReaders ++;
 33443       bReturn = TRUE;
 33447   /* Want a pending lock? */
 33448   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
 33449            && nNumberOfBytesToLockLow == 1){
 33450     /* If no pending lock has been acquired, then acquire it */
 33451     if (pFile->shared->bPending == 0) {
 33452       pFile->shared->bPending = TRUE;
 33453       pFile->local.bPending = TRUE;
 33454       bReturn = TRUE;
 33458   /* Want a reserved lock? */
 33459   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
 33460            && nNumberOfBytesToLockLow == 1){
 33461     if (pFile->shared->bReserved == 0) {
 33462       pFile->shared->bReserved = TRUE;
 33463       pFile->local.bReserved = TRUE;
 33464       bReturn = TRUE;
 33468   winceMutexRelease(pFile->hMutex);
 33469   return bReturn;
 33472 /*
 33473 ** An implementation of the UnlockFile API of Windows for CE
 33474 */
 33475 static BOOL winceUnlockFile(
 33476   LPHANDLE phFile,
 33477   DWORD dwFileOffsetLow,
 33478   DWORD dwFileOffsetHigh,
 33479   DWORD nNumberOfBytesToUnlockLow,
 33480   DWORD nNumberOfBytesToUnlockHigh
 33481 ){
 33482   winFile *pFile = HANDLE_TO_WINFILE(phFile);
 33483   BOOL bReturn = FALSE;
 33485   UNUSED_PARAMETER(dwFileOffsetHigh);
 33486   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
 33488   if (!pFile->hMutex) return TRUE;
 33489   winceMutexAcquire(pFile->hMutex);
 33491   /* Releasing a reader lock or an exclusive lock */
 33492   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
 33493     /* Did we have an exclusive lock? */
 33494     if (pFile->local.bExclusive){
 33495       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
 33496       pFile->local.bExclusive = FALSE;
 33497       pFile->shared->bExclusive = FALSE;
 33498       bReturn = TRUE;
 33501     /* Did we just have a reader lock? */
 33502     else if (pFile->local.nReaders){
 33503       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
 33504              || nNumberOfBytesToUnlockLow == 1);
 33505       pFile->local.nReaders --;
 33506       if (pFile->local.nReaders == 0)
 33508         pFile->shared->nReaders --;
 33510       bReturn = TRUE;
 33514   /* Releasing a pending lock */
 33515   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
 33516            && nNumberOfBytesToUnlockLow == 1){
 33517     if (pFile->local.bPending){
 33518       pFile->local.bPending = FALSE;
 33519       pFile->shared->bPending = FALSE;
 33520       bReturn = TRUE;
 33523   /* Releasing a reserved lock */
 33524   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
 33525            && nNumberOfBytesToUnlockLow == 1){
 33526     if (pFile->local.bReserved) {
 33527       pFile->local.bReserved = FALSE;
 33528       pFile->shared->bReserved = FALSE;
 33529       bReturn = TRUE;
 33533   winceMutexRelease(pFile->hMutex);
 33534   return bReturn;
 33536 /*
 33537 ** End of the special code for wince
 33538 *****************************************************************************/
 33539 #endif /* SQLITE_OS_WINCE */
 33541 /*
 33542 ** Lock a file region.
 33543 */
 33544 static BOOL winLockFile(
 33545   LPHANDLE phFile,
 33546   DWORD flags,
 33547   DWORD offsetLow,
 33548   DWORD offsetHigh,
 33549   DWORD numBytesLow,
 33550   DWORD numBytesHigh
 33551 ){
 33552 #if SQLITE_OS_WINCE
 33553   /*
 33554   ** NOTE: Windows CE is handled differently here due its lack of the Win32
 33555   **       API LockFile.
 33556   */
 33557   return winceLockFile(phFile, offsetLow, offsetHigh,
 33558                        numBytesLow, numBytesHigh);
 33559 #else
 33560   if( osIsNT() ){
 33561     OVERLAPPED ovlp;
 33562     memset(&ovlp, 0, sizeof(OVERLAPPED));
 33563     ovlp.Offset = offsetLow;
 33564     ovlp.OffsetHigh = offsetHigh;
 33565     return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
 33566   }else{
 33567     return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 33568                       numBytesHigh);
 33570 #endif
 33573 /*
 33574 ** Unlock a file region.
 33575  */
 33576 static BOOL winUnlockFile(
 33577   LPHANDLE phFile,
 33578   DWORD offsetLow,
 33579   DWORD offsetHigh,
 33580   DWORD numBytesLow,
 33581   DWORD numBytesHigh
 33582 ){
 33583 #if SQLITE_OS_WINCE
 33584   /*
 33585   ** NOTE: Windows CE is handled differently here due its lack of the Win32
 33586   **       API UnlockFile.
 33587   */
 33588   return winceUnlockFile(phFile, offsetLow, offsetHigh,
 33589                          numBytesLow, numBytesHigh);
 33590 #else
 33591   if( osIsNT() ){
 33592     OVERLAPPED ovlp;
 33593     memset(&ovlp, 0, sizeof(OVERLAPPED));
 33594     ovlp.Offset = offsetLow;
 33595     ovlp.OffsetHigh = offsetHigh;
 33596     return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
 33597   }else{
 33598     return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
 33599                         numBytesHigh);
 33601 #endif
 33604 /*****************************************************************************
 33605 ** The next group of routines implement the I/O methods specified
 33606 ** by the sqlite3_io_methods object.
 33607 ******************************************************************************/
 33609 /*
 33610 ** Some Microsoft compilers lack this definition.
 33611 */
 33612 #ifndef INVALID_SET_FILE_POINTER
 33613 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
 33614 #endif
 33616 /*
 33617 ** Move the current position of the file handle passed as the first 
 33618 ** argument to offset iOffset within the file. If successful, return 0. 
 33619 ** Otherwise, set pFile->lastErrno and return non-zero.
 33620 */
 33621 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
 33622 #if !SQLITE_OS_WINRT
 33623   LONG upperBits;                 /* Most sig. 32 bits of new offset */
 33624   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
 33625   DWORD dwRet;                    /* Value returned by SetFilePointer() */
 33626   DWORD lastErrno;                /* Value returned by GetLastError() */
 33628   OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
 33630   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
 33631   lowerBits = (LONG)(iOffset & 0xffffffff);
 33633   /* API oddity: If successful, SetFilePointer() returns a dword 
 33634   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
 33635   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
 33636   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
 33637   ** whether an error has actually occurred, it is also necessary to call 
 33638   ** GetLastError().
 33639   */
 33640   dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
 33642   if( (dwRet==INVALID_SET_FILE_POINTER
 33643       && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
 33644     pFile->lastErrno = lastErrno;
 33645     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 33646                 "winSeekFile", pFile->zPath);
 33647     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
 33648     return 1;
 33651   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
 33652   return 0;
 33653 #else
 33654   /*
 33655   ** Same as above, except that this implementation works for WinRT.
 33656   */
 33658   LARGE_INTEGER x;                /* The new offset */
 33659   BOOL bRet;                      /* Value returned by SetFilePointerEx() */
 33661   x.QuadPart = iOffset;
 33662   bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
 33664   if(!bRet){
 33665     pFile->lastErrno = osGetLastError();
 33666     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
 33667                 "winSeekFile", pFile->zPath);
 33668     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
 33669     return 1;
 33672   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
 33673   return 0;
 33674 #endif
 33677 #if SQLITE_MAX_MMAP_SIZE>0
 33678 /* Forward references to VFS helper methods used for memory mapped files */
 33679 static int winMapfile(winFile*, sqlite3_int64);
 33680 static int winUnmapfile(winFile*);
 33681 #endif
 33683 /*
 33684 ** Close a file.
 33685 **
 33686 ** It is reported that an attempt to close a handle might sometimes
 33687 ** fail.  This is a very unreasonable result, but Windows is notorious
 33688 ** for being unreasonable so I do not doubt that it might happen.  If
 33689 ** the close fails, we pause for 100 milliseconds and try again.  As
 33690 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
 33691 ** giving up and returning an error.
 33692 */
 33693 #define MX_CLOSE_ATTEMPT 3
 33694 static int winClose(sqlite3_file *id){
 33695   int rc, cnt = 0;
 33696   winFile *pFile = (winFile*)id;
 33698   assert( id!=0 );
 33699 #ifndef SQLITE_OMIT_WAL
 33700   assert( pFile->pShm==0 );
 33701 #endif
 33702   assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
 33703   OSTRACE(("CLOSE file=%p\n", pFile->h));
 33705 #if SQLITE_MAX_MMAP_SIZE>0
 33706   winUnmapfile(pFile);
 33707 #endif
 33709   do{
 33710     rc = osCloseHandle(pFile->h);
 33711     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
 33712   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
 33713 #if SQLITE_OS_WINCE
 33714 #define WINCE_DELETION_ATTEMPTS 3
 33715   winceDestroyLock(pFile);
 33716   if( pFile->zDeleteOnClose ){
 33717     int cnt = 0;
 33718     while(
 33719            osDeleteFileW(pFile->zDeleteOnClose)==0
 33720         && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
 33721         && cnt++ < WINCE_DELETION_ATTEMPTS
 33722     ){
 33723        sqlite3_win32_sleep(100);  /* Wait a little before trying again */
 33725     sqlite3_free(pFile->zDeleteOnClose);
 33727 #endif
 33728   if( rc ){
 33729     pFile->h = NULL;
 33731   OpenCounter(-1);
 33732   OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
 33733   return rc ? SQLITE_OK
 33734             : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
 33735                           "winClose", pFile->zPath);
 33738 /*
 33739 ** Read data from a file into a buffer.  Return SQLITE_OK if all
 33740 ** bytes were read successfully and SQLITE_IOERR if anything goes
 33741 ** wrong.
 33742 */
 33743 static int winRead(
 33744   sqlite3_file *id,          /* File to read from */
 33745   void *pBuf,                /* Write content into this buffer */
 33746   int amt,                   /* Number of bytes to read */
 33747   sqlite3_int64 offset       /* Begin reading at this offset */
 33748 ){
 33749 #if !SQLITE_OS_WINCE
 33750   OVERLAPPED overlapped;          /* The offset for ReadFile. */
 33751 #endif
 33752   winFile *pFile = (winFile*)id;  /* file handle */
 33753   DWORD nRead;                    /* Number of bytes actually read from file */
 33754   int nRetry = 0;                 /* Number of retrys */
 33756   assert( id!=0 );
 33757   assert( amt>0 );
 33758   assert( offset>=0 );
 33759   SimulateIOError(return SQLITE_IOERR_READ);
 33760   OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
 33761            pFile->h, pBuf, amt, offset, pFile->locktype));
 33763 #if SQLITE_MAX_MMAP_SIZE>0
 33764   /* Deal with as much of this read request as possible by transfering
 33765   ** data from the memory mapping using memcpy().  */
 33766   if( offset<pFile->mmapSize ){
 33767     if( offset+amt <= pFile->mmapSize ){
 33768       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
 33769       OSTRACE(("READ-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
 33770       return SQLITE_OK;
 33771     }else{
 33772       int nCopy = (int)(pFile->mmapSize - offset);
 33773       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
 33774       pBuf = &((u8 *)pBuf)[nCopy];
 33775       amt -= nCopy;
 33776       offset += nCopy;
 33779 #endif
 33781 #if SQLITE_OS_WINCE
 33782   if( winSeekFile(pFile, offset) ){
 33783     OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
 33784     return SQLITE_FULL;
 33786   while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
 33787 #else
 33788   memset(&overlapped, 0, sizeof(OVERLAPPED));
 33789   overlapped.Offset = (LONG)(offset & 0xffffffff);
 33790   overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 33791   while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
 33792          osGetLastError()!=ERROR_HANDLE_EOF ){
 33793 #endif
 33794     DWORD lastErrno;
 33795     if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
 33796     pFile->lastErrno = lastErrno;
 33797     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
 33798     return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
 33799                        "winRead", pFile->zPath);
 33801   winLogIoerr(nRetry);
 33802   if( nRead<(DWORD)amt ){
 33803     /* Unread parts of the buffer must be zero-filled */
 33804     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
 33805     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
 33806     return SQLITE_IOERR_SHORT_READ;
 33809   OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
 33810   return SQLITE_OK;
 33813 /*
 33814 ** Write data from a buffer into a file.  Return SQLITE_OK on success
 33815 ** or some other error code on failure.
 33816 */
 33817 static int winWrite(
 33818   sqlite3_file *id,               /* File to write into */
 33819   const void *pBuf,               /* The bytes to be written */
 33820   int amt,                        /* Number of bytes to write */
 33821   sqlite3_int64 offset            /* Offset into the file to begin writing at */
 33822 ){
 33823   int rc = 0;                     /* True if error has occurred, else false */
 33824   winFile *pFile = (winFile*)id;  /* File handle */
 33825   int nRetry = 0;                 /* Number of retries */
 33827   assert( amt>0 );
 33828   assert( pFile );
 33829   SimulateIOError(return SQLITE_IOERR_WRITE);
 33830   SimulateDiskfullError(return SQLITE_FULL);
 33832   OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
 33833            pFile->h, pBuf, amt, offset, pFile->locktype));
 33835 #if SQLITE_MAX_MMAP_SIZE>0
 33836   /* Deal with as much of this write request as possible by transfering
 33837   ** data from the memory mapping using memcpy().  */
 33838   if( offset<pFile->mmapSize ){
 33839     if( offset+amt <= pFile->mmapSize ){
 33840       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
 33841       OSTRACE(("WRITE-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
 33842       return SQLITE_OK;
 33843     }else{
 33844       int nCopy = (int)(pFile->mmapSize - offset);
 33845       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
 33846       pBuf = &((u8 *)pBuf)[nCopy];
 33847       amt -= nCopy;
 33848       offset += nCopy;
 33851 #endif
 33853 #if SQLITE_OS_WINCE
 33854   rc = winSeekFile(pFile, offset);
 33855   if( rc==0 ){
 33856 #else
 33858 #endif
 33859 #if !SQLITE_OS_WINCE
 33860     OVERLAPPED overlapped;        /* The offset for WriteFile. */
 33861 #endif
 33862     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
 33863     int nRem = amt;               /* Number of bytes yet to be written */
 33864     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
 33865     DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
 33867 #if !SQLITE_OS_WINCE
 33868     memset(&overlapped, 0, sizeof(OVERLAPPED));
 33869     overlapped.Offset = (LONG)(offset & 0xffffffff);
 33870     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 33871 #endif
 33873     while( nRem>0 ){
 33874 #if SQLITE_OS_WINCE
 33875       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
 33876 #else
 33877       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
 33878 #endif
 33879         if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
 33880         break;
 33882       assert( nWrite==0 || nWrite<=(DWORD)nRem );
 33883       if( nWrite==0 || nWrite>(DWORD)nRem ){
 33884         lastErrno = osGetLastError();
 33885         break;
 33887 #if !SQLITE_OS_WINCE
 33888       offset += nWrite;
 33889       overlapped.Offset = (LONG)(offset & 0xffffffff);
 33890       overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
 33891 #endif
 33892       aRem += nWrite;
 33893       nRem -= nWrite;
 33895     if( nRem>0 ){
 33896       pFile->lastErrno = lastErrno;
 33897       rc = 1;
 33901   if( rc ){
 33902     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
 33903        || ( pFile->lastErrno==ERROR_DISK_FULL )){
 33904       OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
 33905       return winLogError(SQLITE_FULL, pFile->lastErrno,
 33906                          "winWrite1", pFile->zPath);
 33908     OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
 33909     return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
 33910                        "winWrite2", pFile->zPath);
 33911   }else{
 33912     winLogIoerr(nRetry);
 33914   OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
 33915   return SQLITE_OK;
 33918 /*
 33919 ** Truncate an open file to a specified size
 33920 */
 33921 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
 33922   winFile *pFile = (winFile*)id;  /* File handle object */
 33923   int rc = SQLITE_OK;             /* Return code for this function */
 33924   DWORD lastErrno;
 33926   assert( pFile );
 33927   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
 33928   OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
 33929            pFile->h, nByte, pFile->locktype));
 33931   /* If the user has configured a chunk-size for this file, truncate the
 33932   ** file so that it consists of an integer number of chunks (i.e. the
 33933   ** actual file size after the operation may be larger than the requested
 33934   ** size).
 33935   */
 33936   if( pFile->szChunk>0 ){
 33937     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 33940   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
 33941   if( winSeekFile(pFile, nByte) ){
 33942     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 33943                      "winTruncate1", pFile->zPath);
 33944   }else if( 0==osSetEndOfFile(pFile->h) &&
 33945             ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
 33946     pFile->lastErrno = lastErrno;
 33947     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
 33948                      "winTruncate2", pFile->zPath);
 33951 #if SQLITE_MAX_MMAP_SIZE>0
 33952   /* If the file was truncated to a size smaller than the currently
 33953   ** mapped region, reduce the effective mapping size as well. SQLite will
 33954   ** use read() and write() to access data beyond this point from now on.
 33955   */
 33956   if( pFile->pMapRegion && nByte<pFile->mmapSize ){
 33957     pFile->mmapSize = nByte;
 33959 #endif
 33961   OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
 33962   return rc;
 33965 #ifdef SQLITE_TEST
 33966 /*
 33967 ** Count the number of fullsyncs and normal syncs.  This is used to test
 33968 ** that syncs and fullsyncs are occuring at the right times.
 33969 */
 33970 SQLITE_API int sqlite3_sync_count = 0;
 33971 SQLITE_API int sqlite3_fullsync_count = 0;
 33972 #endif
 33974 /*
 33975 ** Make sure all writes to a particular file are committed to disk.
 33976 */
 33977 static int winSync(sqlite3_file *id, int flags){
 33978 #ifndef SQLITE_NO_SYNC
 33979   /*
 33980   ** Used only when SQLITE_NO_SYNC is not defined.
 33981    */
 33982   BOOL rc;
 33983 #endif
 33984 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
 33985     (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
 33986   /*
 33987   ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
 33988   ** OSTRACE() macros.
 33989    */
 33990   winFile *pFile = (winFile*)id;
 33991 #else
 33992   UNUSED_PARAMETER(id);
 33993 #endif
 33995   assert( pFile );
 33996   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
 33997   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
 33998       || (flags&0x0F)==SQLITE_SYNC_FULL
 33999   );
 34001   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
 34002   ** line is to test that doing so does not cause any problems.
 34003   */
 34004   SimulateDiskfullError( return SQLITE_FULL );
 34006   OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
 34007            pFile->h, flags, pFile->locktype));
 34009 #ifndef SQLITE_TEST
 34010   UNUSED_PARAMETER(flags);
 34011 #else
 34012   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
 34013     sqlite3_fullsync_count++;
 34015   sqlite3_sync_count++;
 34016 #endif
 34018   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 34019   ** no-op
 34020   */
 34021 #ifdef SQLITE_NO_SYNC
 34022   OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
 34023   return SQLITE_OK;
 34024 #else
 34025   rc = osFlushFileBuffers(pFile->h);
 34026   SimulateIOError( rc=FALSE );
 34027   if( rc ){
 34028     OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
 34029     return SQLITE_OK;
 34030   }else{
 34031     pFile->lastErrno = osGetLastError();
 34032     OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
 34033     return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
 34034                        "winSync", pFile->zPath);
 34036 #endif
 34039 /*
 34040 ** Determine the current size of a file in bytes
 34041 */
 34042 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
 34043   winFile *pFile = (winFile*)id;
 34044   int rc = SQLITE_OK;
 34046   assert( id!=0 );
 34047   assert( pSize!=0 );
 34048   SimulateIOError(return SQLITE_IOERR_FSTAT);
 34049   OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
 34051 #if SQLITE_OS_WINRT
 34053     FILE_STANDARD_INFO info;
 34054     if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
 34055                                      &info, sizeof(info)) ){
 34056       *pSize = info.EndOfFile.QuadPart;
 34057     }else{
 34058       pFile->lastErrno = osGetLastError();
 34059       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 34060                        "winFileSize", pFile->zPath);
 34063 #else
 34065     DWORD upperBits;
 34066     DWORD lowerBits;
 34067     DWORD lastErrno;
 34069     lowerBits = osGetFileSize(pFile->h, &upperBits);
 34070     *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
 34071     if(   (lowerBits == INVALID_FILE_SIZE)
 34072        && ((lastErrno = osGetLastError())!=NO_ERROR) ){
 34073       pFile->lastErrno = lastErrno;
 34074       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
 34075                        "winFileSize", pFile->zPath);
 34078 #endif
 34079   OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
 34080            pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
 34081   return rc;
 34084 /*
 34085 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
 34086 */
 34087 #ifndef LOCKFILE_FAIL_IMMEDIATELY
 34088 # define LOCKFILE_FAIL_IMMEDIATELY 1
 34089 #endif
 34091 #ifndef LOCKFILE_EXCLUSIVE_LOCK
 34092 # define LOCKFILE_EXCLUSIVE_LOCK 2
 34093 #endif
 34095 /*
 34096 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
 34097 ** When the LockFile function was used, it was always expected to fail
 34098 ** immediately if the lock could not be obtained.  Also, it always expected to
 34099 ** obtain an exclusive lock.  These flags are used with the LockFileEx function
 34100 ** and reflect those expectations; therefore, they should not be changed.
 34101 */
 34102 #ifndef SQLITE_LOCKFILE_FLAGS
 34103 # define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
 34104                                   LOCKFILE_EXCLUSIVE_LOCK)
 34105 #endif
 34107 /*
 34108 ** Currently, SQLite never calls the LockFileEx function without wanting the
 34109 ** call to fail immediately if the lock cannot be obtained.
 34110 */
 34111 #ifndef SQLITE_LOCKFILEEX_FLAGS
 34112 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
 34113 #endif
 34115 /*
 34116 ** Acquire a reader lock.
 34117 ** Different API routines are called depending on whether or not this
 34118 ** is Win9x or WinNT.
 34119 */
 34120 static int winGetReadLock(winFile *pFile){
 34121   int res;
 34122   OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
 34123   if( osIsNT() ){
 34124 #if SQLITE_OS_WINCE
 34125     /*
 34126     ** NOTE: Windows CE is handled differently here due its lack of the Win32
 34127     **       API LockFileEx.
 34128     */
 34129     res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
 34130 #else
 34131     res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
 34132                       SHARED_SIZE, 0);
 34133 #endif
 34135 #ifdef SQLITE_WIN32_HAS_ANSI
 34136   else{
 34137     int lk;
 34138     sqlite3_randomness(sizeof(lk), &lk);
 34139     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
 34140     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 34141                       SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 34143 #endif
 34144   if( res == 0 ){
 34145     pFile->lastErrno = osGetLastError();
 34146     /* No need to log a failure to lock */
 34148   OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
 34149   return res;
 34152 /*
 34153 ** Undo a readlock
 34154 */
 34155 static int winUnlockReadLock(winFile *pFile){
 34156   int res;
 34157   DWORD lastErrno;
 34158   OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
 34159   if( osIsNT() ){
 34160     res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 34162 #ifdef SQLITE_WIN32_HAS_ANSI
 34163   else{
 34164     res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
 34166 #endif
 34167   if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
 34168     pFile->lastErrno = lastErrno;
 34169     winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
 34170                 "winUnlockReadLock", pFile->zPath);
 34172   OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
 34173   return res;
 34176 /*
 34177 ** Lock the file with the lock specified by parameter locktype - one
 34178 ** of the following:
 34179 **
 34180 **     (1) SHARED_LOCK
 34181 **     (2) RESERVED_LOCK
 34182 **     (3) PENDING_LOCK
 34183 **     (4) EXCLUSIVE_LOCK
 34184 **
 34185 ** Sometimes when requesting one lock state, additional lock states
 34186 ** are inserted in between.  The locking might fail on one of the later
 34187 ** transitions leaving the lock state different from what it started but
 34188 ** still short of its goal.  The following chart shows the allowed
 34189 ** transitions and the inserted intermediate states:
 34190 **
 34191 **    UNLOCKED -> SHARED
 34192 **    SHARED -> RESERVED
 34193 **    SHARED -> (PENDING) -> EXCLUSIVE
 34194 **    RESERVED -> (PENDING) -> EXCLUSIVE
 34195 **    PENDING -> EXCLUSIVE
 34196 **
 34197 ** This routine will only increase a lock.  The winUnlock() routine
 34198 ** erases all locks at once and returns us immediately to locking level 0.
 34199 ** It is not possible to lower the locking level one step at a time.  You
 34200 ** must go straight to locking level 0.
 34201 */
 34202 static int winLock(sqlite3_file *id, int locktype){
 34203   int rc = SQLITE_OK;    /* Return code from subroutines */
 34204   int res = 1;           /* Result of a Windows lock call */
 34205   int newLocktype;       /* Set pFile->locktype to this value before exiting */
 34206   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
 34207   winFile *pFile = (winFile*)id;
 34208   DWORD lastErrno = NO_ERROR;
 34210   assert( id!=0 );
 34211   OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
 34212            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
 34214   /* If there is already a lock of this type or more restrictive on the
 34215   ** OsFile, do nothing. Don't use the end_lock: exit path, as
 34216   ** sqlite3OsEnterMutex() hasn't been called yet.
 34217   */
 34218   if( pFile->locktype>=locktype ){
 34219     OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
 34220     return SQLITE_OK;
 34223   /* Make sure the locking sequence is correct
 34224   */
 34225   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
 34226   assert( locktype!=PENDING_LOCK );
 34227   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 34229   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
 34230   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
 34231   ** the PENDING_LOCK byte is temporary.
 34232   */
 34233   newLocktype = pFile->locktype;
 34234   if(   (pFile->locktype==NO_LOCK)
 34235      || (   (locktype==EXCLUSIVE_LOCK)
 34236          && (pFile->locktype==RESERVED_LOCK))
 34237   ){
 34238     int cnt = 3;
 34239     while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
 34240                                          PENDING_BYTE, 0, 1, 0))==0 ){
 34241       /* Try 3 times to get the pending lock.  This is needed to work
 34242       ** around problems caused by indexing and/or anti-virus software on
 34243       ** Windows systems.
 34244       ** If you are using this code as a model for alternative VFSes, do not
 34245       ** copy this retry logic.  It is a hack intended for Windows only.
 34246       */
 34247       OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
 34248                pFile->h, cnt, sqlite3ErrName(res)));
 34249       if( cnt ) sqlite3_win32_sleep(1);
 34251     gotPendingLock = res;
 34252     if( !res ){
 34253       lastErrno = osGetLastError();
 34257   /* Acquire a shared lock
 34258   */
 34259   if( locktype==SHARED_LOCK && res ){
 34260     assert( pFile->locktype==NO_LOCK );
 34261     res = winGetReadLock(pFile);
 34262     if( res ){
 34263       newLocktype = SHARED_LOCK;
 34264     }else{
 34265       lastErrno = osGetLastError();
 34269   /* Acquire a RESERVED lock
 34270   */
 34271   if( locktype==RESERVED_LOCK && res ){
 34272     assert( pFile->locktype==SHARED_LOCK );
 34273     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
 34274     if( res ){
 34275       newLocktype = RESERVED_LOCK;
 34276     }else{
 34277       lastErrno = osGetLastError();
 34281   /* Acquire a PENDING lock
 34282   */
 34283   if( locktype==EXCLUSIVE_LOCK && res ){
 34284     newLocktype = PENDING_LOCK;
 34285     gotPendingLock = 0;
 34288   /* Acquire an EXCLUSIVE lock
 34289   */
 34290   if( locktype==EXCLUSIVE_LOCK && res ){
 34291     assert( pFile->locktype>=SHARED_LOCK );
 34292     res = winUnlockReadLock(pFile);
 34293     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
 34294                       SHARED_SIZE, 0);
 34295     if( res ){
 34296       newLocktype = EXCLUSIVE_LOCK;
 34297     }else{
 34298       lastErrno = osGetLastError();
 34299       winGetReadLock(pFile);
 34303   /* If we are holding a PENDING lock that ought to be released, then
 34304   ** release it now.
 34305   */
 34306   if( gotPendingLock && locktype==SHARED_LOCK ){
 34307     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 34310   /* Update the state of the lock has held in the file descriptor then
 34311   ** return the appropriate result code.
 34312   */
 34313   if( res ){
 34314     rc = SQLITE_OK;
 34315   }else{
 34316     pFile->lastErrno = lastErrno;
 34317     rc = SQLITE_BUSY;
 34318     OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
 34319              pFile->h, locktype, newLocktype));
 34321   pFile->locktype = (u8)newLocktype;
 34322   OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
 34323            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
 34324   return rc;
 34327 /*
 34328 ** This routine checks if there is a RESERVED lock held on the specified
 34329 ** file by this or any other process. If such a lock is held, return
 34330 ** non-zero, otherwise zero.
 34331 */
 34332 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
 34333   int rc;
 34334   winFile *pFile = (winFile*)id;
 34336   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 34337   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
 34339   assert( id!=0 );
 34340   if( pFile->locktype>=RESERVED_LOCK ){
 34341     rc = 1;
 34342     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
 34343   }else{
 34344     rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
 34345     if( rc ){
 34346       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 34348     rc = !rc;
 34349     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
 34351   *pResOut = rc;
 34352   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
 34353            pFile->h, pResOut, *pResOut));
 34354   return SQLITE_OK;
 34357 /*
 34358 ** Lower the locking level on file descriptor id to locktype.  locktype
 34359 ** must be either NO_LOCK or SHARED_LOCK.
 34360 **
 34361 ** If the locking level of the file descriptor is already at or below
 34362 ** the requested locking level, this routine is a no-op.
 34363 **
 34364 ** It is not possible for this routine to fail if the second argument
 34365 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
 34366 ** might return SQLITE_IOERR;
 34367 */
 34368 static int winUnlock(sqlite3_file *id, int locktype){
 34369   int type;
 34370   winFile *pFile = (winFile*)id;
 34371   int rc = SQLITE_OK;
 34372   assert( pFile!=0 );
 34373   assert( locktype<=SHARED_LOCK );
 34374   OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
 34375            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
 34376   type = pFile->locktype;
 34377   if( type>=EXCLUSIVE_LOCK ){
 34378     winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
 34379     if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
 34380       /* This should never happen.  We should always be able to
 34381       ** reacquire the read lock */
 34382       rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
 34383                        "winUnlock", pFile->zPath);
 34386   if( type>=RESERVED_LOCK ){
 34387     winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
 34389   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
 34390     winUnlockReadLock(pFile);
 34392   if( type>=PENDING_LOCK ){
 34393     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
 34395   pFile->locktype = (u8)locktype;
 34396   OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
 34397            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
 34398   return rc;
 34401 /*
 34402 ** If *pArg is inititially negative then this is a query.  Set *pArg to
 34403 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
 34404 **
 34405 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
 34406 */
 34407 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
 34408   if( *pArg<0 ){
 34409     *pArg = (pFile->ctrlFlags & mask)!=0;
 34410   }else if( (*pArg)==0 ){
 34411     pFile->ctrlFlags &= ~mask;
 34412   }else{
 34413     pFile->ctrlFlags |= mask;
 34417 /* Forward references to VFS helper methods used for temporary files */
 34418 static int winGetTempname(sqlite3_vfs *, char **);
 34419 static int winIsDir(const void *);
 34420 static BOOL winIsDriveLetterAndColon(const char *);
 34422 /*
 34423 ** Control and query of the open file handle.
 34424 */
 34425 static int winFileControl(sqlite3_file *id, int op, void *pArg){
 34426   winFile *pFile = (winFile*)id;
 34427   OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
 34428   switch( op ){
 34429     case SQLITE_FCNTL_LOCKSTATE: {
 34430       *(int*)pArg = pFile->locktype;
 34431       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34432       return SQLITE_OK;
 34434     case SQLITE_LAST_ERRNO: {
 34435       *(int*)pArg = (int)pFile->lastErrno;
 34436       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34437       return SQLITE_OK;
 34439     case SQLITE_FCNTL_CHUNK_SIZE: {
 34440       pFile->szChunk = *(int *)pArg;
 34441       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34442       return SQLITE_OK;
 34444     case SQLITE_FCNTL_SIZE_HINT: {
 34445       if( pFile->szChunk>0 ){
 34446         sqlite3_int64 oldSz;
 34447         int rc = winFileSize(id, &oldSz);
 34448         if( rc==SQLITE_OK ){
 34449           sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
 34450           if( newSz>oldSz ){
 34451             SimulateIOErrorBenign(1);
 34452             rc = winTruncate(id, newSz);
 34453             SimulateIOErrorBenign(0);
 34456         OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
 34457         return rc;
 34459       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34460       return SQLITE_OK;
 34462     case SQLITE_FCNTL_PERSIST_WAL: {
 34463       winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
 34464       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34465       return SQLITE_OK;
 34467     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
 34468       winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
 34469       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34470       return SQLITE_OK;
 34472     case SQLITE_FCNTL_VFSNAME: {
 34473       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
 34474       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34475       return SQLITE_OK;
 34477     case SQLITE_FCNTL_WIN32_AV_RETRY: {
 34478       int *a = (int*)pArg;
 34479       if( a[0]>0 ){
 34480         winIoerrRetry = a[0];
 34481       }else{
 34482         a[0] = winIoerrRetry;
 34484       if( a[1]>0 ){
 34485         winIoerrRetryDelay = a[1];
 34486       }else{
 34487         a[1] = winIoerrRetryDelay;
 34489       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
 34490       return SQLITE_OK;
 34492     case SQLITE_FCNTL_TEMPFILENAME: {
 34493       char *zTFile = 0;
 34494       int rc = winGetTempname(pFile->pVfs, &zTFile);
 34495       if( rc==SQLITE_OK ){
 34496         *(char**)pArg = zTFile;
 34498       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
 34499       return rc;
 34501 #if SQLITE_MAX_MMAP_SIZE>0
 34502     case SQLITE_FCNTL_MMAP_SIZE: {
 34503       i64 newLimit = *(i64*)pArg;
 34504       int rc = SQLITE_OK;
 34505       if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34506         newLimit = sqlite3GlobalConfig.mxMmap;
 34508       *(i64*)pArg = pFile->mmapSizeMax;
 34509       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
 34510         pFile->mmapSizeMax = newLimit;
 34511         if( pFile->mmapSize>0 ){
 34512           winUnmapfile(pFile);
 34513           rc = winMapfile(pFile, -1);
 34516       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
 34517       return rc;
 34519 #endif
 34521   OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
 34522   return SQLITE_NOTFOUND;
 34525 /*
 34526 ** Return the sector size in bytes of the underlying block device for
 34527 ** the specified file. This is almost always 512 bytes, but may be
 34528 ** larger for some devices.
 34529 **
 34530 ** SQLite code assumes this function cannot fail. It also assumes that
 34531 ** if two files are created in the same file-system directory (i.e.
 34532 ** a database and its journal file) that the sector size will be the
 34533 ** same for both.
 34534 */
 34535 static int winSectorSize(sqlite3_file *id){
 34536   (void)id;
 34537   return SQLITE_DEFAULT_SECTOR_SIZE;
 34540 /*
 34541 ** Return a vector of device characteristics.
 34542 */
 34543 static int winDeviceCharacteristics(sqlite3_file *id){
 34544   winFile *p = (winFile*)id;
 34545   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
 34546          ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
 34549 /* 
 34550 ** Windows will only let you create file view mappings
 34551 ** on allocation size granularity boundaries.
 34552 ** During sqlite3_os_init() we do a GetSystemInfo()
 34553 ** to get the granularity size.
 34554 */
 34555 static SYSTEM_INFO winSysInfo;
 34557 #ifndef SQLITE_OMIT_WAL
 34559 /*
 34560 ** Helper functions to obtain and relinquish the global mutex. The
 34561 ** global mutex is used to protect the winLockInfo objects used by 
 34562 ** this file, all of which may be shared by multiple threads.
 34563 **
 34564 ** Function winShmMutexHeld() is used to assert() that the global mutex 
 34565 ** is held when required. This function is only used as part of assert() 
 34566 ** statements. e.g.
 34567 **
 34568 **   winShmEnterMutex()
 34569 **     assert( winShmMutexHeld() );
 34570 **   winShmLeaveMutex()
 34571 */
 34572 static void winShmEnterMutex(void){
 34573   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 34575 static void winShmLeaveMutex(void){
 34576   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 34578 #ifndef NDEBUG
 34579 static int winShmMutexHeld(void) {
 34580   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 34582 #endif
 34584 /*
 34585 ** Object used to represent a single file opened and mmapped to provide
 34586 ** shared memory.  When multiple threads all reference the same
 34587 ** log-summary, each thread has its own winFile object, but they all
 34588 ** point to a single instance of this object.  In other words, each
 34589 ** log-summary is opened only once per process.
 34590 **
 34591 ** winShmMutexHeld() must be true when creating or destroying
 34592 ** this object or while reading or writing the following fields:
 34593 **
 34594 **      nRef
 34595 **      pNext 
 34596 **
 34597 ** The following fields are read-only after the object is created:
 34598 ** 
 34599 **      fid
 34600 **      zFilename
 34601 **
 34602 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
 34603 ** winShmMutexHeld() is true when reading or writing any other field
 34604 ** in this structure.
 34605 **
 34606 */
 34607 struct winShmNode {
 34608   sqlite3_mutex *mutex;      /* Mutex to access this object */
 34609   char *zFilename;           /* Name of the file */
 34610   winFile hFile;             /* File handle from winOpen */
 34612   int szRegion;              /* Size of shared-memory regions */
 34613   int nRegion;               /* Size of array apRegion */
 34614   struct ShmRegion {
 34615     HANDLE hMap;             /* File handle from CreateFileMapping */
 34616     void *pMap;
 34617   } *aRegion;
 34618   DWORD lastErrno;           /* The Windows errno from the last I/O error */
 34620   int nRef;                  /* Number of winShm objects pointing to this */
 34621   winShm *pFirst;            /* All winShm objects pointing to this */
 34622   winShmNode *pNext;         /* Next in list of all winShmNode objects */
 34623 #ifdef SQLITE_DEBUG
 34624   u8 nextShmId;              /* Next available winShm.id value */
 34625 #endif
 34626 };
 34628 /*
 34629 ** A global array of all winShmNode objects.
 34630 **
 34631 ** The winShmMutexHeld() must be true while reading or writing this list.
 34632 */
 34633 static winShmNode *winShmNodeList = 0;
 34635 /*
 34636 ** Structure used internally by this VFS to record the state of an
 34637 ** open shared memory connection.
 34638 **
 34639 ** The following fields are initialized when this object is created and
 34640 ** are read-only thereafter:
 34641 **
 34642 **    winShm.pShmNode
 34643 **    winShm.id
 34644 **
 34645 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
 34646 ** while accessing any read/write fields.
 34647 */
 34648 struct winShm {
 34649   winShmNode *pShmNode;      /* The underlying winShmNode object */
 34650   winShm *pNext;             /* Next winShm with the same winShmNode */
 34651   u8 hasMutex;               /* True if holding the winShmNode mutex */
 34652   u16 sharedMask;            /* Mask of shared locks held */
 34653   u16 exclMask;              /* Mask of exclusive locks held */
 34654 #ifdef SQLITE_DEBUG
 34655   u8 id;                     /* Id of this connection with its winShmNode */
 34656 #endif
 34657 };
 34659 /*
 34660 ** Constants used for locking
 34661 */
 34662 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
 34663 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 34665 /*
 34666 ** Apply advisory locks for all n bytes beginning at ofst.
 34667 */
 34668 #define _SHM_UNLCK  1
 34669 #define _SHM_RDLCK  2
 34670 #define _SHM_WRLCK  3
 34671 static int winShmSystemLock(
 34672   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
 34673   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
 34674   int ofst,             /* Offset to first byte to be locked/unlocked */
 34675   int nByte             /* Number of bytes to lock or unlock */
 34676 ){
 34677   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
 34679   /* Access to the winShmNode object is serialized by the caller */
 34680   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
 34682   OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
 34683            pFile->hFile.h, lockType, ofst, nByte));
 34685   /* Release/Acquire the system-level lock */
 34686   if( lockType==_SHM_UNLCK ){
 34687     rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
 34688   }else{
 34689     /* Initialize the locking parameters */
 34690     DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
 34691     if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
 34692     rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
 34695   if( rc!= 0 ){
 34696     rc = SQLITE_OK;
 34697   }else{
 34698     pFile->lastErrno =  osGetLastError();
 34699     rc = SQLITE_BUSY;
 34702   OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
 34703            pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
 34704            "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
 34706   return rc;
 34709 /* Forward references to VFS methods */
 34710 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
 34711 static int winDelete(sqlite3_vfs *,const char*,int);
 34713 /*
 34714 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
 34715 **
 34716 ** This is not a VFS shared-memory method; it is a utility function called
 34717 ** by VFS shared-memory methods.
 34718 */
 34719 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
 34720   winShmNode **pp;
 34721   winShmNode *p;
 34722   assert( winShmMutexHeld() );
 34723   OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
 34724            osGetCurrentProcessId(), deleteFlag));
 34725   pp = &winShmNodeList;
 34726   while( (p = *pp)!=0 ){
 34727     if( p->nRef==0 ){
 34728       int i;
 34729       if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
 34730       for(i=0; i<p->nRegion; i++){
 34731         BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
 34732         OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
 34733                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
 34734         UNUSED_VARIABLE_VALUE(bRc);
 34735         bRc = osCloseHandle(p->aRegion[i].hMap);
 34736         OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
 34737                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
 34738         UNUSED_VARIABLE_VALUE(bRc);
 34740       if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
 34741         SimulateIOErrorBenign(1);
 34742         winClose((sqlite3_file *)&p->hFile);
 34743         SimulateIOErrorBenign(0);
 34745       if( deleteFlag ){
 34746         SimulateIOErrorBenign(1);
 34747         sqlite3BeginBenignMalloc();
 34748         winDelete(pVfs, p->zFilename, 0);
 34749         sqlite3EndBenignMalloc();
 34750         SimulateIOErrorBenign(0);
 34752       *pp = p->pNext;
 34753       sqlite3_free(p->aRegion);
 34754       sqlite3_free(p);
 34755     }else{
 34756       pp = &p->pNext;
 34761 /*
 34762 ** Open the shared-memory area associated with database file pDbFd.
 34763 **
 34764 ** When opening a new shared-memory file, if no other instances of that
 34765 ** file are currently open, in this process or in other processes, then
 34766 ** the file must be truncated to zero length or have its header cleared.
 34767 */
 34768 static int winOpenSharedMemory(winFile *pDbFd){
 34769   struct winShm *p;                  /* The connection to be opened */
 34770   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
 34771   int rc;                            /* Result code */
 34772   struct winShmNode *pNew;           /* Newly allocated winShmNode */
 34773   int nName;                         /* Size of zName in bytes */
 34775   assert( pDbFd->pShm==0 );    /* Not previously opened */
 34777   /* Allocate space for the new sqlite3_shm object.  Also speculatively
 34778   ** allocate space for a new winShmNode and filename.
 34779   */
 34780   p = sqlite3MallocZero( sizeof(*p) );
 34781   if( p==0 ) return SQLITE_IOERR_NOMEM;
 34782   nName = sqlite3Strlen30(pDbFd->zPath);
 34783   pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
 34784   if( pNew==0 ){
 34785     sqlite3_free(p);
 34786     return SQLITE_IOERR_NOMEM;
 34788   pNew->zFilename = (char*)&pNew[1];
 34789   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
 34790   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
 34792   /* Look to see if there is an existing winShmNode that can be used.
 34793   ** If no matching winShmNode currently exists, create a new one.
 34794   */
 34795   winShmEnterMutex();
 34796   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
 34797     /* TBD need to come up with better match here.  Perhaps
 34798     ** use FILE_ID_BOTH_DIR_INFO Structure.
 34799     */
 34800     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
 34802   if( pShmNode ){
 34803     sqlite3_free(pNew);
 34804   }else{
 34805     pShmNode = pNew;
 34806     pNew = 0;
 34807     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
 34808     pShmNode->pNext = winShmNodeList;
 34809     winShmNodeList = pShmNode;
 34811     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 34812     if( pShmNode->mutex==0 ){
 34813       rc = SQLITE_IOERR_NOMEM;
 34814       goto shm_open_err;
 34817     rc = winOpen(pDbFd->pVfs,
 34818                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
 34819                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
 34820                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
 34821                  0);
 34822     if( SQLITE_OK!=rc ){
 34823       goto shm_open_err;
 34826     /* Check to see if another process is holding the dead-man switch.
 34827     ** If not, truncate the file to zero length. 
 34828     */
 34829     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
 34830       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
 34831       if( rc!=SQLITE_OK ){
 34832         rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
 34833                          "winOpenShm", pDbFd->zPath);
 34836     if( rc==SQLITE_OK ){
 34837       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 34838       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
 34840     if( rc ) goto shm_open_err;
 34843   /* Make the new connection a child of the winShmNode */
 34844   p->pShmNode = pShmNode;
 34845 #ifdef SQLITE_DEBUG
 34846   p->id = pShmNode->nextShmId++;
 34847 #endif
 34848   pShmNode->nRef++;
 34849   pDbFd->pShm = p;
 34850   winShmLeaveMutex();
 34852   /* The reference count on pShmNode has already been incremented under
 34853   ** the cover of the winShmEnterMutex() mutex and the pointer from the
 34854   ** new (struct winShm) object to the pShmNode has been set. All that is
 34855   ** left to do is to link the new object into the linked list starting
 34856   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
 34857   ** mutex.
 34858   */
 34859   sqlite3_mutex_enter(pShmNode->mutex);
 34860   p->pNext = pShmNode->pFirst;
 34861   pShmNode->pFirst = p;
 34862   sqlite3_mutex_leave(pShmNode->mutex);
 34863   return SQLITE_OK;
 34865   /* Jump here on any error */
 34866 shm_open_err:
 34867   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
 34868   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
 34869   sqlite3_free(p);
 34870   sqlite3_free(pNew);
 34871   winShmLeaveMutex();
 34872   return rc;
 34875 /*
 34876 ** Close a connection to shared-memory.  Delete the underlying 
 34877 ** storage if deleteFlag is true.
 34878 */
 34879 static int winShmUnmap(
 34880   sqlite3_file *fd,          /* Database holding shared memory */
 34881   int deleteFlag             /* Delete after closing if true */
 34882 ){
 34883   winFile *pDbFd;       /* Database holding shared-memory */
 34884   winShm *p;            /* The connection to be closed */
 34885   winShmNode *pShmNode; /* The underlying shared-memory file */
 34886   winShm **pp;          /* For looping over sibling connections */
 34888   pDbFd = (winFile*)fd;
 34889   p = pDbFd->pShm;
 34890   if( p==0 ) return SQLITE_OK;
 34891   pShmNode = p->pShmNode;
 34893   /* Remove connection p from the set of connections associated
 34894   ** with pShmNode */
 34895   sqlite3_mutex_enter(pShmNode->mutex);
 34896   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
 34897   *pp = p->pNext;
 34899   /* Free the connection p */
 34900   sqlite3_free(p);
 34901   pDbFd->pShm = 0;
 34902   sqlite3_mutex_leave(pShmNode->mutex);
 34904   /* If pShmNode->nRef has reached 0, then close the underlying
 34905   ** shared-memory file, too */
 34906   winShmEnterMutex();
 34907   assert( pShmNode->nRef>0 );
 34908   pShmNode->nRef--;
 34909   if( pShmNode->nRef==0 ){
 34910     winShmPurge(pDbFd->pVfs, deleteFlag);
 34912   winShmLeaveMutex();
 34914   return SQLITE_OK;
 34917 /*
 34918 ** Change the lock state for a shared-memory segment.
 34919 */
 34920 static int winShmLock(
 34921   sqlite3_file *fd,          /* Database file holding the shared memory */
 34922   int ofst,                  /* First lock to acquire or release */
 34923   int n,                     /* Number of locks to acquire or release */
 34924   int flags                  /* What to do with the lock */
 34925 ){
 34926   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
 34927   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
 34928   winShm *pX;                           /* For looping over all siblings */
 34929   winShmNode *pShmNode = p->pShmNode;
 34930   int rc = SQLITE_OK;                   /* Result code */
 34931   u16 mask;                             /* Mask of locks to take or release */
 34933   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 34934   assert( n>=1 );
 34935   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 34936        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 34937        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 34938        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 34939   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 34941   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
 34942   assert( n>1 || mask==(1<<ofst) );
 34943   sqlite3_mutex_enter(pShmNode->mutex);
 34944   if( flags & SQLITE_SHM_UNLOCK ){
 34945     u16 allMask = 0; /* Mask of locks held by siblings */
 34947     /* See if any siblings hold this same lock */
 34948     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 34949       if( pX==p ) continue;
 34950       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 34951       allMask |= pX->sharedMask;
 34954     /* Unlock the system-level locks */
 34955     if( (mask & allMask)==0 ){
 34956       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
 34957     }else{
 34958       rc = SQLITE_OK;
 34961     /* Undo the local locks */
 34962     if( rc==SQLITE_OK ){
 34963       p->exclMask &= ~mask;
 34964       p->sharedMask &= ~mask;
 34966   }else if( flags & SQLITE_SHM_SHARED ){
 34967     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
 34969     /* Find out which shared locks are already held by sibling connections.
 34970     ** If any sibling already holds an exclusive lock, go ahead and return
 34971     ** SQLITE_BUSY.
 34972     */
 34973     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 34974       if( (pX->exclMask & mask)!=0 ){
 34975         rc = SQLITE_BUSY;
 34976         break;
 34978       allShared |= pX->sharedMask;
 34981     /* Get shared locks at the system level, if necessary */
 34982     if( rc==SQLITE_OK ){
 34983       if( (allShared & mask)==0 ){
 34984         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
 34985       }else{
 34986         rc = SQLITE_OK;
 34990     /* Get the local shared locks */
 34991     if( rc==SQLITE_OK ){
 34992       p->sharedMask |= mask;
 34994   }else{
 34995     /* Make sure no sibling connections hold locks that will block this
 34996     ** lock.  If any do, return SQLITE_BUSY right away.
 34997     */
 34998     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 34999       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 35000         rc = SQLITE_BUSY;
 35001         break;
 35005     /* Get the exclusive locks at the system level.  Then if successful
 35006     ** also mark the local connection as being locked.
 35007     */
 35008     if( rc==SQLITE_OK ){
 35009       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
 35010       if( rc==SQLITE_OK ){
 35011         assert( (p->sharedMask & mask)==0 );
 35012         p->exclMask |= mask;
 35016   sqlite3_mutex_leave(pShmNode->mutex);
 35017   OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
 35018            osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
 35019            sqlite3ErrName(rc)));
 35020   return rc;
 35023 /*
 35024 ** Implement a memory barrier or memory fence on shared memory.  
 35025 **
 35026 ** All loads and stores begun before the barrier must complete before
 35027 ** any load or store begun after the barrier.
 35028 */
 35029 static void winShmBarrier(
 35030   sqlite3_file *fd          /* Database holding the shared memory */
 35031 ){
 35032   UNUSED_PARAMETER(fd);
 35033   /* MemoryBarrier(); // does not work -- do not know why not */
 35034   winShmEnterMutex();
 35035   winShmLeaveMutex();
 35038 /*
 35039 ** This function is called to obtain a pointer to region iRegion of the 
 35040 ** shared-memory associated with the database file fd. Shared-memory regions 
 35041 ** are numbered starting from zero. Each shared-memory region is szRegion 
 35042 ** bytes in size.
 35043 **
 35044 ** If an error occurs, an error code is returned and *pp is set to NULL.
 35045 **
 35046 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
 35047 ** region has not been allocated (by any client, including one running in a
 35048 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
 35049 ** isWrite is non-zero and the requested shared-memory region has not yet 
 35050 ** been allocated, it is allocated by this function.
 35051 **
 35052 ** If the shared-memory region has already been allocated or is allocated by
 35053 ** this call as described above, then it is mapped into this processes 
 35054 ** address space (if it is not already), *pp is set to point to the mapped 
 35055 ** memory and SQLITE_OK returned.
 35056 */
 35057 static int winShmMap(
 35058   sqlite3_file *fd,               /* Handle open on database file */
 35059   int iRegion,                    /* Region to retrieve */
 35060   int szRegion,                   /* Size of regions */
 35061   int isWrite,                    /* True to extend file if necessary */
 35062   void volatile **pp              /* OUT: Mapped memory */
 35063 ){
 35064   winFile *pDbFd = (winFile*)fd;
 35065   winShm *p = pDbFd->pShm;
 35066   winShmNode *pShmNode;
 35067   int rc = SQLITE_OK;
 35069   if( !p ){
 35070     rc = winOpenSharedMemory(pDbFd);
 35071     if( rc!=SQLITE_OK ) return rc;
 35072     p = pDbFd->pShm;
 35074   pShmNode = p->pShmNode;
 35076   sqlite3_mutex_enter(pShmNode->mutex);
 35077   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 35079   if( pShmNode->nRegion<=iRegion ){
 35080     struct ShmRegion *apNew;           /* New aRegion[] array */
 35081     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 35082     sqlite3_int64 sz;                  /* Current size of wal-index file */
 35084     pShmNode->szRegion = szRegion;
 35086     /* The requested region is not mapped into this processes address space.
 35087     ** Check to see if it has been allocated (i.e. if the wal-index file is
 35088     ** large enough to contain the requested region).
 35089     */
 35090     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
 35091     if( rc!=SQLITE_OK ){
 35092       rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 35093                        "winShmMap1", pDbFd->zPath);
 35094       goto shmpage_out;
 35097     if( sz<nByte ){
 35098       /* The requested memory region does not exist. If isWrite is set to
 35099       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
 35100       **
 35101       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
 35102       ** the requested memory region.
 35103       */
 35104       if( !isWrite ) goto shmpage_out;
 35105       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
 35106       if( rc!=SQLITE_OK ){
 35107         rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
 35108                          "winShmMap2", pDbFd->zPath);
 35109         goto shmpage_out;
 35113     /* Map the requested memory region into this processes address space. */
 35114     apNew = (struct ShmRegion *)sqlite3_realloc(
 35115         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
 35116     );
 35117     if( !apNew ){
 35118       rc = SQLITE_IOERR_NOMEM;
 35119       goto shmpage_out;
 35121     pShmNode->aRegion = apNew;
 35123     while( pShmNode->nRegion<=iRegion ){
 35124       HANDLE hMap = NULL;         /* file-mapping handle */
 35125       void *pMap = 0;             /* Mapped memory region */
 35127 #if SQLITE_OS_WINRT
 35128       hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
 35129           NULL, PAGE_READWRITE, nByte, NULL
 35130       );
 35131 #elif defined(SQLITE_WIN32_HAS_WIDE)
 35132       hMap = osCreateFileMappingW(pShmNode->hFile.h, 
 35133           NULL, PAGE_READWRITE, 0, nByte, NULL
 35134       );
 35135 #elif defined(SQLITE_WIN32_HAS_ANSI)
 35136       hMap = osCreateFileMappingA(pShmNode->hFile.h, 
 35137           NULL, PAGE_READWRITE, 0, nByte, NULL
 35138       );
 35139 #endif
 35140       OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
 35141                osGetCurrentProcessId(), pShmNode->nRegion, nByte,
 35142                hMap ? "ok" : "failed"));
 35143       if( hMap ){
 35144         int iOffset = pShmNode->nRegion*szRegion;
 35145         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 35146 #if SQLITE_OS_WINRT
 35147         pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 35148             iOffset - iOffsetShift, szRegion + iOffsetShift
 35149         );
 35150 #else
 35151         pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
 35152             0, iOffset - iOffsetShift, szRegion + iOffsetShift
 35153         );
 35154 #endif
 35155         OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
 35156                  osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
 35157                  szRegion, pMap ? "ok" : "failed"));
 35159       if( !pMap ){
 35160         pShmNode->lastErrno = osGetLastError();
 35161         rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
 35162                          "winShmMap3", pDbFd->zPath);
 35163         if( hMap ) osCloseHandle(hMap);
 35164         goto shmpage_out;
 35167       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
 35168       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
 35169       pShmNode->nRegion++;
 35173 shmpage_out:
 35174   if( pShmNode->nRegion>iRegion ){
 35175     int iOffset = iRegion*szRegion;
 35176     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
 35177     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
 35178     *pp = (void *)&p[iOffsetShift];
 35179   }else{
 35180     *pp = 0;
 35182   sqlite3_mutex_leave(pShmNode->mutex);
 35183   return rc;
 35186 #else
 35187 # define winShmMap     0
 35188 # define winShmLock    0
 35189 # define winShmBarrier 0
 35190 # define winShmUnmap   0
 35191 #endif /* #ifndef SQLITE_OMIT_WAL */
 35193 /*
 35194 ** Cleans up the mapped region of the specified file, if any.
 35195 */
 35196 #if SQLITE_MAX_MMAP_SIZE>0
 35197 static int winUnmapfile(winFile *pFile){
 35198   assert( pFile!=0 );
 35199   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
 35200            "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
 35201            osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
 35202            pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
 35203   if( pFile->pMapRegion ){
 35204     if( !osUnmapViewOfFile(pFile->pMapRegion) ){
 35205       pFile->lastErrno = osGetLastError();
 35206       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
 35207                "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
 35208                pFile->pMapRegion));
 35209       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
 35210                          "winUnmapfile1", pFile->zPath);
 35212     pFile->pMapRegion = 0;
 35213     pFile->mmapSize = 0;
 35214     pFile->mmapSizeActual = 0;
 35216   if( pFile->hMap!=NULL ){
 35217     if( !osCloseHandle(pFile->hMap) ){
 35218       pFile->lastErrno = osGetLastError();
 35219       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
 35220                osGetCurrentProcessId(), pFile, pFile->hMap));
 35221       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
 35222                          "winUnmapfile2", pFile->zPath);
 35224     pFile->hMap = NULL;
 35226   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 35227            osGetCurrentProcessId(), pFile));
 35228   return SQLITE_OK;
 35231 /*
 35232 ** Memory map or remap the file opened by file-descriptor pFd (if the file
 35233 ** is already mapped, the existing mapping is replaced by the new). Or, if 
 35234 ** there already exists a mapping for this file, and there are still 
 35235 ** outstanding xFetch() references to it, this function is a no-op.
 35236 **
 35237 ** If parameter nByte is non-negative, then it is the requested size of 
 35238 ** the mapping to create. Otherwise, if nByte is less than zero, then the 
 35239 ** requested size is the size of the file on disk. The actual size of the
 35240 ** created mapping is either the requested size or the value configured 
 35241 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
 35242 **
 35243 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
 35244 ** recreated as a result of outstanding references) or an SQLite error
 35245 ** code otherwise.
 35246 */
 35247 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
 35248   sqlite3_int64 nMap = nByte;
 35249   int rc;
 35251   assert( nMap>=0 || pFd->nFetchOut==0 );
 35252   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
 35253            osGetCurrentProcessId(), pFd, nByte));
 35255   if( pFd->nFetchOut>0 ) return SQLITE_OK;
 35257   if( nMap<0 ){
 35258     rc = winFileSize((sqlite3_file*)pFd, &nMap);
 35259     if( rc ){
 35260       OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
 35261                osGetCurrentProcessId(), pFd));
 35262       return SQLITE_IOERR_FSTAT;
 35265   if( nMap>pFd->mmapSizeMax ){
 35266     nMap = pFd->mmapSizeMax;
 35268   nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
 35270   if( nMap==0 && pFd->mmapSize>0 ){
 35271     winUnmapfile(pFd);
 35273   if( nMap!=pFd->mmapSize ){
 35274     void *pNew = 0;
 35275     DWORD protect = PAGE_READONLY;
 35276     DWORD flags = FILE_MAP_READ;
 35278     winUnmapfile(pFd);
 35279     if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
 35280       protect = PAGE_READWRITE;
 35281       flags |= FILE_MAP_WRITE;
 35283 #if SQLITE_OS_WINRT
 35284     pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
 35285 #elif defined(SQLITE_WIN32_HAS_WIDE)
 35286     pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
 35287                                 (DWORD)((nMap>>32) & 0xffffffff),
 35288                                 (DWORD)(nMap & 0xffffffff), NULL);
 35289 #elif defined(SQLITE_WIN32_HAS_ANSI)
 35290     pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
 35291                                 (DWORD)((nMap>>32) & 0xffffffff),
 35292                                 (DWORD)(nMap & 0xffffffff), NULL);
 35293 #endif
 35294     if( pFd->hMap==NULL ){
 35295       pFd->lastErrno = osGetLastError();
 35296       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
 35297                        "winMapfile1", pFd->zPath);
 35298       /* Log the error, but continue normal operation using xRead/xWrite */
 35299       OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
 35300                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 35301       return SQLITE_OK;
 35303     assert( (nMap % winSysInfo.dwPageSize)==0 );
 35304     assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
 35305 #if SQLITE_OS_WINRT
 35306     pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
 35307 #else
 35308     pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
 35309 #endif
 35310     if( pNew==NULL ){
 35311       osCloseHandle(pFd->hMap);
 35312       pFd->hMap = NULL;
 35313       pFd->lastErrno = osGetLastError();
 35314       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
 35315                        "winMapfile2", pFd->zPath);
 35316       /* Log the error, but continue normal operation using xRead/xWrite */
 35317       OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
 35318                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 35319       return SQLITE_OK;
 35321     pFd->pMapRegion = pNew;
 35322     pFd->mmapSize = nMap;
 35323     pFd->mmapSizeActual = nMap;
 35326   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 35327            osGetCurrentProcessId(), pFd));
 35328   return SQLITE_OK;
 35330 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
 35332 /*
 35333 ** If possible, return a pointer to a mapping of file fd starting at offset
 35334 ** iOff. The mapping must be valid for at least nAmt bytes.
 35335 **
 35336 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
 35337 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
 35338 ** Finally, if an error does occur, return an SQLite error code. The final
 35339 ** value of *pp is undefined in this case.
 35340 **
 35341 ** If this function does return a pointer, the caller must eventually 
 35342 ** release the reference by calling winUnfetch().
 35343 */
 35344 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
 35345 #if SQLITE_MAX_MMAP_SIZE>0
 35346   winFile *pFd = (winFile*)fd;   /* The underlying database file */
 35347 #endif
 35348   *pp = 0;
 35350   OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
 35351            osGetCurrentProcessId(), fd, iOff, nAmt, pp));
 35353 #if SQLITE_MAX_MMAP_SIZE>0
 35354   if( pFd->mmapSizeMax>0 ){
 35355     if( pFd->pMapRegion==0 ){
 35356       int rc = winMapfile(pFd, -1);
 35357       if( rc!=SQLITE_OK ){
 35358         OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
 35359                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
 35360         return rc;
 35363     if( pFd->mmapSize >= iOff+nAmt ){
 35364       *pp = &((u8 *)pFd->pMapRegion)[iOff];
 35365       pFd->nFetchOut++;
 35368 #endif
 35370   OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
 35371            osGetCurrentProcessId(), fd, pp, *pp));
 35372   return SQLITE_OK;
 35375 /*
 35376 ** If the third argument is non-NULL, then this function releases a 
 35377 ** reference obtained by an earlier call to winFetch(). The second
 35378 ** argument passed to this function must be the same as the corresponding
 35379 ** argument that was passed to the winFetch() invocation. 
 35380 **
 35381 ** Or, if the third argument is NULL, then this function is being called 
 35382 ** to inform the VFS layer that, according to POSIX, any existing mapping 
 35383 ** may now be invalid and should be unmapped.
 35384 */
 35385 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
 35386 #if SQLITE_MAX_MMAP_SIZE>0
 35387   winFile *pFd = (winFile*)fd;   /* The underlying database file */
 35389   /* If p==0 (unmap the entire file) then there must be no outstanding 
 35390   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
 35391   ** then there must be at least one outstanding.  */
 35392   assert( (p==0)==(pFd->nFetchOut==0) );
 35394   /* If p!=0, it must match the iOff value. */
 35395   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
 35397   OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
 35398            osGetCurrentProcessId(), pFd, iOff, p));
 35400   if( p ){
 35401     pFd->nFetchOut--;
 35402   }else{
 35403     /* FIXME:  If Windows truly always prevents truncating or deleting a
 35404     ** file while a mapping is held, then the following winUnmapfile() call
 35405     ** is unnecessary can can be omitted - potentially improving
 35406     ** performance.  */
 35407     winUnmapfile(pFd);
 35410   assert( pFd->nFetchOut>=0 );
 35411 #endif
 35413   OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
 35414            osGetCurrentProcessId(), fd));
 35415   return SQLITE_OK;
 35418 /*
 35419 ** Here ends the implementation of all sqlite3_file methods.
 35420 **
 35421 ********************** End sqlite3_file Methods *******************************
 35422 ******************************************************************************/
 35424 /*
 35425 ** This vector defines all the methods that can operate on an
 35426 ** sqlite3_file for win32.
 35427 */
 35428 static const sqlite3_io_methods winIoMethod = {
 35429   3,                              /* iVersion */
 35430   winClose,                       /* xClose */
 35431   winRead,                        /* xRead */
 35432   winWrite,                       /* xWrite */
 35433   winTruncate,                    /* xTruncate */
 35434   winSync,                        /* xSync */
 35435   winFileSize,                    /* xFileSize */
 35436   winLock,                        /* xLock */
 35437   winUnlock,                      /* xUnlock */
 35438   winCheckReservedLock,           /* xCheckReservedLock */
 35439   winFileControl,                 /* xFileControl */
 35440   winSectorSize,                  /* xSectorSize */
 35441   winDeviceCharacteristics,       /* xDeviceCharacteristics */
 35442   winShmMap,                      /* xShmMap */
 35443   winShmLock,                     /* xShmLock */
 35444   winShmBarrier,                  /* xShmBarrier */
 35445   winShmUnmap,                    /* xShmUnmap */
 35446   winFetch,                       /* xFetch */
 35447   winUnfetch                      /* xUnfetch */
 35448 };
 35450 /****************************************************************************
 35451 **************************** sqlite3_vfs methods ****************************
 35452 **
 35453 ** This division contains the implementation of methods on the
 35454 ** sqlite3_vfs object.
 35455 */
 35457 #if defined(__CYGWIN__)
 35458 /*
 35459 ** Convert a filename from whatever the underlying operating system
 35460 ** supports for filenames into UTF-8.  Space to hold the result is
 35461 ** obtained from malloc and must be freed by the calling function.
 35462 */
 35463 static char *winConvertToUtf8Filename(const void *zFilename){
 35464   char *zConverted = 0;
 35465   if( osIsNT() ){
 35466     zConverted = winUnicodeToUtf8(zFilename);
 35468 #ifdef SQLITE_WIN32_HAS_ANSI
 35469   else{
 35470     zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
 35472 #endif
 35473   /* caller will handle out of memory */
 35474   return zConverted;
 35476 #endif
 35478 /*
 35479 ** Convert a UTF-8 filename into whatever form the underlying
 35480 ** operating system wants filenames in.  Space to hold the result
 35481 ** is obtained from malloc and must be freed by the calling
 35482 ** function.
 35483 */
 35484 static void *winConvertFromUtf8Filename(const char *zFilename){
 35485   void *zConverted = 0;
 35486   if( osIsNT() ){
 35487     zConverted = winUtf8ToUnicode(zFilename);
 35489 #ifdef SQLITE_WIN32_HAS_ANSI
 35490   else{
 35491     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
 35493 #endif
 35494   /* caller will handle out of memory */
 35495   return zConverted;
 35498 /*
 35499 ** This function returns non-zero if the specified UTF-8 string buffer
 35500 ** ends with a directory separator character or one was successfully
 35501 ** added to it.
 35502 */
 35503 static int winMakeEndInDirSep(int nBuf, char *zBuf){
 35504   if( zBuf ){
 35505     int nLen = sqlite3Strlen30(zBuf);
 35506     if( nLen>0 ){
 35507       if( winIsDirSep(zBuf[nLen-1]) ){
 35508         return 1;
 35509       }else if( nLen+1<nBuf ){
 35510         zBuf[nLen] = winGetDirSep();
 35511         zBuf[nLen+1] = '\0';
 35512         return 1;
 35516   return 0;
 35519 /*
 35520 ** Create a temporary file name and store the resulting pointer into pzBuf.
 35521 ** The pointer returned in pzBuf must be freed via sqlite3_free().
 35522 */
 35523 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
 35524   static char zChars[] =
 35525     "abcdefghijklmnopqrstuvwxyz"
 35526     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 35527     "0123456789";
 35528   size_t i, j;
 35529   int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
 35530   int nMax, nBuf, nDir, nLen;
 35531   char *zBuf;
 35533   /* It's odd to simulate an io-error here, but really this is just
 35534   ** using the io-error infrastructure to test that SQLite handles this
 35535   ** function failing. 
 35536   */
 35537   SimulateIOError( return SQLITE_IOERR );
 35539   /* Allocate a temporary buffer to store the fully qualified file
 35540   ** name for the temporary file.  If this fails, we cannot continue.
 35541   */
 35542   nMax = pVfs->mxPathname; nBuf = nMax + 2;
 35543   zBuf = sqlite3MallocZero( nBuf );
 35544   if( !zBuf ){
 35545     OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35546     return SQLITE_IOERR_NOMEM;
 35549   /* Figure out the effective temporary directory.  First, check if one
 35550   ** has been explicitly set by the application; otherwise, use the one
 35551   ** configured by the operating system.
 35552   */
 35553   nDir = nMax - (nPre + 15);
 35554   assert( nDir>0 );
 35555   if( sqlite3_temp_directory ){
 35556     int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
 35557     if( nDirLen>0 ){
 35558       if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
 35559         nDirLen++;
 35561       if( nDirLen>nDir ){
 35562         sqlite3_free(zBuf);
 35563         OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
 35564         return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
 35566       sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
 35569 #if defined(__CYGWIN__)
 35570   else{
 35571     static const char *azDirs[] = {
 35572        0, /* getenv("SQLITE_TMPDIR") */
 35573        0, /* getenv("TMPDIR") */
 35574        0, /* getenv("TMP") */
 35575        0, /* getenv("TEMP") */
 35576        0, /* getenv("USERPROFILE") */
 35577        "/var/tmp",
 35578        "/usr/tmp",
 35579        "/tmp",
 35580        ".",
 35581        0        /* List terminator */
 35582     };
 35583     unsigned int i;
 35584     const char *zDir = 0;
 35586     if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
 35587     if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
 35588     if( !azDirs[2] ) azDirs[2] = getenv("TMP");
 35589     if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
 35590     if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
 35591     for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
 35592       void *zConverted;
 35593       if( zDir==0 ) continue;
 35594       /* If the path starts with a drive letter followed by the colon
 35595       ** character, assume it is already a native Win32 path; otherwise,
 35596       ** it must be converted to a native Win32 path via the Cygwin API
 35597       ** prior to using it.
 35598       */
 35599       if( winIsDriveLetterAndColon(zDir) ){
 35600         zConverted = winConvertFromUtf8Filename(zDir);
 35601         if( !zConverted ){
 35602           sqlite3_free(zBuf);
 35603           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35604           return SQLITE_IOERR_NOMEM;
 35606         if( winIsDir(zConverted) ){
 35607           sqlite3_snprintf(nMax, zBuf, "%s", zDir);
 35608           sqlite3_free(zConverted);
 35609           break;
 35611         sqlite3_free(zConverted);
 35612       }else{
 35613         zConverted = sqlite3MallocZero( nMax+1 );
 35614         if( !zConverted ){
 35615           sqlite3_free(zBuf);
 35616           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35617           return SQLITE_IOERR_NOMEM;
 35619         if( cygwin_conv_path(
 35620                 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
 35621                 zConverted, nMax+1)<0 ){
 35622           sqlite3_free(zConverted);
 35623           sqlite3_free(zBuf);
 35624           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
 35625           return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
 35626                              "winGetTempname2", zDir);
 35628         if( winIsDir(zConverted) ){
 35629           /* At this point, we know the candidate directory exists and should
 35630           ** be used.  However, we may need to convert the string containing
 35631           ** its name into UTF-8 (i.e. if it is UTF-16 right now).
 35632           */
 35633           char *zUtf8 = winConvertToUtf8Filename(zConverted);
 35634           if( !zUtf8 ){
 35635             sqlite3_free(zConverted);
 35636             sqlite3_free(zBuf);
 35637             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35638             return SQLITE_IOERR_NOMEM;
 35640           sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
 35641           sqlite3_free(zUtf8);
 35642           sqlite3_free(zConverted);
 35643           break;
 35645         sqlite3_free(zConverted);
 35649 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
 35650   else if( osIsNT() ){
 35651     char *zMulti;
 35652     LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
 35653     if( !zWidePath ){
 35654       sqlite3_free(zBuf);
 35655       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35656       return SQLITE_IOERR_NOMEM;
 35658     if( osGetTempPathW(nMax, zWidePath)==0 ){
 35659       sqlite3_free(zWidePath);
 35660       sqlite3_free(zBuf);
 35661       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
 35662       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
 35663                          "winGetTempname2", 0);
 35665     zMulti = winUnicodeToUtf8(zWidePath);
 35666     if( zMulti ){
 35667       sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
 35668       sqlite3_free(zMulti);
 35669       sqlite3_free(zWidePath);
 35670     }else{
 35671       sqlite3_free(zWidePath);
 35672       sqlite3_free(zBuf);
 35673       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35674       return SQLITE_IOERR_NOMEM;
 35677 #ifdef SQLITE_WIN32_HAS_ANSI
 35678   else{
 35679     char *zUtf8;
 35680     char *zMbcsPath = sqlite3MallocZero( nMax );
 35681     if( !zMbcsPath ){
 35682       sqlite3_free(zBuf);
 35683       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35684       return SQLITE_IOERR_NOMEM;
 35686     if( osGetTempPathA(nMax, zMbcsPath)==0 ){
 35687       sqlite3_free(zBuf);
 35688       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
 35689       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
 35690                          "winGetTempname3", 0);
 35692     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
 35693     if( zUtf8 ){
 35694       sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
 35695       sqlite3_free(zUtf8);
 35696     }else{
 35697       sqlite3_free(zBuf);
 35698       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
 35699       return SQLITE_IOERR_NOMEM;
 35702 #endif /* SQLITE_WIN32_HAS_ANSI */
 35703 #endif /* !SQLITE_OS_WINRT */
 35705   /*
 35706   ** Check to make sure the temporary directory ends with an appropriate
 35707   ** separator.  If it does not and there is not enough space left to add
 35708   ** one, fail.
 35709   */
 35710   if( !winMakeEndInDirSep(nDir+1, zBuf) ){
 35711     sqlite3_free(zBuf);
 35712     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
 35713     return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
 35716   /*
 35717   ** Check that the output buffer is large enough for the temporary file 
 35718   ** name in the following format:
 35719   **
 35720   **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
 35721   **
 35722   ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
 35723   ** account for the space used by the 15 character random suffix and the
 35724   ** two trailing NUL characters.  The final directory separator character
 35725   ** has already added if it was not already present.
 35726   */
 35727   nLen = sqlite3Strlen30(zBuf);
 35728   if( (nLen + nPre + 17) > nBuf ){
 35729     sqlite3_free(zBuf);
 35730     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
 35731     return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
 35734   sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
 35736   j = sqlite3Strlen30(zBuf);
 35737   sqlite3_randomness(15, &zBuf[j]);
 35738   for(i=0; i<15; i++, j++){
 35739     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 35741   zBuf[j] = 0;
 35742   zBuf[j+1] = 0;
 35743   *pzBuf = zBuf;
 35745   OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
 35746   return SQLITE_OK;
 35749 /*
 35750 ** Return TRUE if the named file is really a directory.  Return false if
 35751 ** it is something other than a directory, or if there is any kind of memory
 35752 ** allocation failure.
 35753 */
 35754 static int winIsDir(const void *zConverted){
 35755   DWORD attr;
 35756   int rc = 0;
 35757   DWORD lastErrno;
 35759   if( osIsNT() ){
 35760     int cnt = 0;
 35761     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 35762     memset(&sAttrData, 0, sizeof(sAttrData));
 35763     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 35764                              GetFileExInfoStandard,
 35765                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
 35766     if( !rc ){
 35767       return 0; /* Invalid name? */
 35769     attr = sAttrData.dwFileAttributes;
 35770 #if SQLITE_OS_WINCE==0
 35771   }else{
 35772     attr = osGetFileAttributesA((char*)zConverted);
 35773 #endif
 35775   return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
 35778 /*
 35779 ** Open a file.
 35780 */
 35781 static int winOpen(
 35782   sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
 35783   const char *zName,        /* Name of the file (UTF-8) */
 35784   sqlite3_file *id,         /* Write the SQLite file handle here */
 35785   int flags,                /* Open mode flags */
 35786   int *pOutFlags            /* Status return flags */
 35787 ){
 35788   HANDLE h;
 35789   DWORD lastErrno = 0;
 35790   DWORD dwDesiredAccess;
 35791   DWORD dwShareMode;
 35792   DWORD dwCreationDisposition;
 35793   DWORD dwFlagsAndAttributes = 0;
 35794 #if SQLITE_OS_WINCE
 35795   int isTemp = 0;
 35796 #endif
 35797   winFile *pFile = (winFile*)id;
 35798   void *zConverted;              /* Filename in OS encoding */
 35799   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
 35800   int cnt = 0;
 35802   /* If argument zPath is a NULL pointer, this function is required to open
 35803   ** a temporary file. Use this buffer to store the file name in.
 35804   */
 35805   char *zTmpname = 0; /* For temporary filename, if necessary. */
 35807   int rc = SQLITE_OK;            /* Function Return Code */
 35808 #if !defined(NDEBUG) || SQLITE_OS_WINCE
 35809   int eType = flags&0xFFFFFF00;  /* Type of file to open */
 35810 #endif
 35812   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 35813   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 35814   int isCreate     = (flags & SQLITE_OPEN_CREATE);
 35815   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 35816   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 35818 #ifndef NDEBUG
 35819   int isOpenJournal = (isCreate && (
 35820         eType==SQLITE_OPEN_MASTER_JOURNAL 
 35821      || eType==SQLITE_OPEN_MAIN_JOURNAL 
 35822      || eType==SQLITE_OPEN_WAL
 35823   ));
 35824 #endif
 35826   OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
 35827            zUtf8Name, id, flags, pOutFlags));
 35829   /* Check the following statements are true: 
 35830   **
 35831   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 35832   **   (b) if CREATE is set, then READWRITE must also be set, and
 35833   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 35834   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 35835   */
 35836   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 35837   assert(isCreate==0 || isReadWrite);
 35838   assert(isExclusive==0 || isCreate);
 35839   assert(isDelete==0 || isCreate);
 35841   /* The main DB, main journal, WAL file and master journal are never 
 35842   ** automatically deleted. Nor are they ever temporary files.  */
 35843   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 35844   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 35845   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 35846   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 35848   /* Assert that the upper layer has set one of the "file-type" flags. */
 35849   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 35850        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 35851        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 35852        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 35853   );
 35855   assert( pFile!=0 );
 35856   memset(pFile, 0, sizeof(winFile));
 35857   pFile->h = INVALID_HANDLE_VALUE;
 35859 #if SQLITE_OS_WINRT
 35860   if( !zUtf8Name && !sqlite3_temp_directory ){
 35861     sqlite3_log(SQLITE_ERROR,
 35862         "sqlite3_temp_directory variable should be set for WinRT");
 35864 #endif
 35866   /* If the second argument to this function is NULL, generate a 
 35867   ** temporary file name to use 
 35868   */
 35869   if( !zUtf8Name ){
 35870     assert( isDelete && !isOpenJournal );
 35871     rc = winGetTempname(pVfs, &zTmpname);
 35872     if( rc!=SQLITE_OK ){
 35873       OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
 35874       return rc;
 35876     zUtf8Name = zTmpname;
 35879   /* Database filenames are double-zero terminated if they are not
 35880   ** URIs with parameters.  Hence, they can always be passed into
 35881   ** sqlite3_uri_parameter().
 35882   */
 35883   assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
 35884        zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
 35886   /* Convert the filename to the system encoding. */
 35887   zConverted = winConvertFromUtf8Filename(zUtf8Name);
 35888   if( zConverted==0 ){
 35889     sqlite3_free(zTmpname);
 35890     OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
 35891     return SQLITE_IOERR_NOMEM;
 35894   if( winIsDir(zConverted) ){
 35895     sqlite3_free(zConverted);
 35896     sqlite3_free(zTmpname);
 35897     OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
 35898     return SQLITE_CANTOPEN_ISDIR;
 35901   if( isReadWrite ){
 35902     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
 35903   }else{
 35904     dwDesiredAccess = GENERIC_READ;
 35907   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
 35908   ** created. SQLite doesn't use it to indicate "exclusive access" 
 35909   ** as it is usually understood.
 35910   */
 35911   if( isExclusive ){
 35912     /* Creates a new file, only if it does not already exist. */
 35913     /* If the file exists, it fails. */
 35914     dwCreationDisposition = CREATE_NEW;
 35915   }else if( isCreate ){
 35916     /* Open existing file, or create if it doesn't exist */
 35917     dwCreationDisposition = OPEN_ALWAYS;
 35918   }else{
 35919     /* Opens a file, only if it exists. */
 35920     dwCreationDisposition = OPEN_EXISTING;
 35923   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
 35925   if( isDelete ){
 35926 #if SQLITE_OS_WINCE
 35927     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
 35928     isTemp = 1;
 35929 #else
 35930     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
 35931                                | FILE_ATTRIBUTE_HIDDEN
 35932                                | FILE_FLAG_DELETE_ON_CLOSE;
 35933 #endif
 35934   }else{
 35935     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 35937   /* Reports from the internet are that performance is always
 35938   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
 35939 #if SQLITE_OS_WINCE
 35940   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
 35941 #endif
 35943   if( osIsNT() ){
 35944 #if SQLITE_OS_WINRT
 35945     CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
 35946     extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
 35947     extendedParameters.dwFileAttributes =
 35948             dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
 35949     extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
 35950     extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
 35951     extendedParameters.lpSecurityAttributes = NULL;
 35952     extendedParameters.hTemplateFile = NULL;
 35953     while( (h = osCreateFile2((LPCWSTR)zConverted,
 35954                               dwDesiredAccess,
 35955                               dwShareMode,
 35956                               dwCreationDisposition,
 35957                               &extendedParameters))==INVALID_HANDLE_VALUE &&
 35958                               winRetryIoerr(&cnt, &lastErrno) ){
 35959                /* Noop */
 35961 #else
 35962     while( (h = osCreateFileW((LPCWSTR)zConverted,
 35963                               dwDesiredAccess,
 35964                               dwShareMode, NULL,
 35965                               dwCreationDisposition,
 35966                               dwFlagsAndAttributes,
 35967                               NULL))==INVALID_HANDLE_VALUE &&
 35968                               winRetryIoerr(&cnt, &lastErrno) ){
 35969                /* Noop */
 35971 #endif
 35973 #ifdef SQLITE_WIN32_HAS_ANSI
 35974   else{
 35975     while( (h = osCreateFileA((LPCSTR)zConverted,
 35976                               dwDesiredAccess,
 35977                               dwShareMode, NULL,
 35978                               dwCreationDisposition,
 35979                               dwFlagsAndAttributes,
 35980                               NULL))==INVALID_HANDLE_VALUE &&
 35981                               winRetryIoerr(&cnt, &lastErrno) ){
 35982                /* Noop */
 35985 #endif
 35986   winLogIoerr(cnt);
 35988   OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
 35989            dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
 35991   if( h==INVALID_HANDLE_VALUE ){
 35992     pFile->lastErrno = lastErrno;
 35993     winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
 35994     sqlite3_free(zConverted);
 35995     sqlite3_free(zTmpname);
 35996     if( isReadWrite && !isExclusive ){
 35997       return winOpen(pVfs, zName, id, 
 35998          ((flags|SQLITE_OPEN_READONLY) &
 35999                      ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
 36000          pOutFlags);
 36001     }else{
 36002       return SQLITE_CANTOPEN_BKPT;
 36006   if( pOutFlags ){
 36007     if( isReadWrite ){
 36008       *pOutFlags = SQLITE_OPEN_READWRITE;
 36009     }else{
 36010       *pOutFlags = SQLITE_OPEN_READONLY;
 36014   OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
 36015            "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
 36016            *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
 36018 #if SQLITE_OS_WINCE
 36019   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
 36020        && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
 36021   ){
 36022     osCloseHandle(h);
 36023     sqlite3_free(zConverted);
 36024     sqlite3_free(zTmpname);
 36025     OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
 36026     return rc;
 36028   if( isTemp ){
 36029     pFile->zDeleteOnClose = zConverted;
 36030   }else
 36031 #endif
 36033     sqlite3_free(zConverted);
 36036   sqlite3_free(zTmpname);
 36037   pFile->pMethod = &winIoMethod;
 36038   pFile->pVfs = pVfs;
 36039   pFile->h = h;
 36040   if( isReadonly ){
 36041     pFile->ctrlFlags |= WINFILE_RDONLY;
 36043   if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
 36044     pFile->ctrlFlags |= WINFILE_PSOW;
 36046   pFile->lastErrno = NO_ERROR;
 36047   pFile->zPath = zName;
 36048 #if SQLITE_MAX_MMAP_SIZE>0
 36049   pFile->hMap = NULL;
 36050   pFile->pMapRegion = 0;
 36051   pFile->mmapSize = 0;
 36052   pFile->mmapSizeActual = 0;
 36053   pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 36054 #endif
 36056   OpenCounter(+1);
 36057   return rc;
 36060 /*
 36061 ** Delete the named file.
 36062 **
 36063 ** Note that Windows does not allow a file to be deleted if some other
 36064 ** process has it open.  Sometimes a virus scanner or indexing program
 36065 ** will open a journal file shortly after it is created in order to do
 36066 ** whatever it does.  While this other process is holding the
 36067 ** file open, we will be unable to delete it.  To work around this
 36068 ** problem, we delay 100 milliseconds and try to delete again.  Up
 36069 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
 36070 ** up and returning an error.
 36071 */
 36072 static int winDelete(
 36073   sqlite3_vfs *pVfs,          /* Not used on win32 */
 36074   const char *zFilename,      /* Name of file to delete */
 36075   int syncDir                 /* Not used on win32 */
 36076 ){
 36077   int cnt = 0;
 36078   int rc;
 36079   DWORD attr;
 36080   DWORD lastErrno = 0;
 36081   void *zConverted;
 36082   UNUSED_PARAMETER(pVfs);
 36083   UNUSED_PARAMETER(syncDir);
 36085   SimulateIOError(return SQLITE_IOERR_DELETE);
 36086   OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
 36088   zConverted = winConvertFromUtf8Filename(zFilename);
 36089   if( zConverted==0 ){
 36090     OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
 36091     return SQLITE_IOERR_NOMEM;
 36093   if( osIsNT() ){
 36094     do {
 36095 #if SQLITE_OS_WINRT
 36096       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 36097       memset(&sAttrData, 0, sizeof(sAttrData));
 36098       if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
 36099                                   &sAttrData) ){
 36100         attr = sAttrData.dwFileAttributes;
 36101       }else{
 36102         lastErrno = osGetLastError();
 36103         if( lastErrno==ERROR_FILE_NOT_FOUND
 36104          || lastErrno==ERROR_PATH_NOT_FOUND ){
 36105           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 36106         }else{
 36107           rc = SQLITE_ERROR;
 36109         break;
 36111 #else
 36112       attr = osGetFileAttributesW(zConverted);
 36113 #endif
 36114       if ( attr==INVALID_FILE_ATTRIBUTES ){
 36115         lastErrno = osGetLastError();
 36116         if( lastErrno==ERROR_FILE_NOT_FOUND
 36117          || lastErrno==ERROR_PATH_NOT_FOUND ){
 36118           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 36119         }else{
 36120           rc = SQLITE_ERROR;
 36122         break;
 36124       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 36125         rc = SQLITE_ERROR; /* Files only. */
 36126         break;
 36128       if ( osDeleteFileW(zConverted) ){
 36129         rc = SQLITE_OK; /* Deleted OK. */
 36130         break;
 36132       if ( !winRetryIoerr(&cnt, &lastErrno) ){
 36133         rc = SQLITE_ERROR; /* No more retries. */
 36134         break;
 36136     } while(1);
 36138 #ifdef SQLITE_WIN32_HAS_ANSI
 36139   else{
 36140     do {
 36141       attr = osGetFileAttributesA(zConverted);
 36142       if ( attr==INVALID_FILE_ATTRIBUTES ){
 36143         lastErrno = osGetLastError();
 36144         if( lastErrno==ERROR_FILE_NOT_FOUND
 36145          || lastErrno==ERROR_PATH_NOT_FOUND ){
 36146           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
 36147         }else{
 36148           rc = SQLITE_ERROR;
 36150         break;
 36152       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
 36153         rc = SQLITE_ERROR; /* Files only. */
 36154         break;
 36156       if ( osDeleteFileA(zConverted) ){
 36157         rc = SQLITE_OK; /* Deleted OK. */
 36158         break;
 36160       if ( !winRetryIoerr(&cnt, &lastErrno) ){
 36161         rc = SQLITE_ERROR; /* No more retries. */
 36162         break;
 36164     } while(1);
 36166 #endif
 36167   if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
 36168     rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
 36169   }else{
 36170     winLogIoerr(cnt);
 36172   sqlite3_free(zConverted);
 36173   OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
 36174   return rc;
 36177 /*
 36178 ** Check the existence and status of a file.
 36179 */
 36180 static int winAccess(
 36181   sqlite3_vfs *pVfs,         /* Not used on win32 */
 36182   const char *zFilename,     /* Name of file to check */
 36183   int flags,                 /* Type of test to make on this file */
 36184   int *pResOut               /* OUT: Result */
 36185 ){
 36186   DWORD attr;
 36187   int rc = 0;
 36188   DWORD lastErrno = 0;
 36189   void *zConverted;
 36190   UNUSED_PARAMETER(pVfs);
 36192   SimulateIOError( return SQLITE_IOERR_ACCESS; );
 36193   OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
 36194            zFilename, flags, pResOut));
 36196   zConverted = winConvertFromUtf8Filename(zFilename);
 36197   if( zConverted==0 ){
 36198     OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
 36199     return SQLITE_IOERR_NOMEM;
 36201   if( osIsNT() ){
 36202     int cnt = 0;
 36203     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
 36204     memset(&sAttrData, 0, sizeof(sAttrData));
 36205     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
 36206                              GetFileExInfoStandard, 
 36207                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
 36208     if( rc ){
 36209       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
 36210       ** as if it does not exist.
 36211       */
 36212       if(    flags==SQLITE_ACCESS_EXISTS
 36213           && sAttrData.nFileSizeHigh==0 
 36214           && sAttrData.nFileSizeLow==0 ){
 36215         attr = INVALID_FILE_ATTRIBUTES;
 36216       }else{
 36217         attr = sAttrData.dwFileAttributes;
 36219     }else{
 36220       winLogIoerr(cnt);
 36221       if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
 36222         sqlite3_free(zConverted);
 36223         return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
 36224                            zFilename);
 36225       }else{
 36226         attr = INVALID_FILE_ATTRIBUTES;
 36230 #ifdef SQLITE_WIN32_HAS_ANSI
 36231   else{
 36232     attr = osGetFileAttributesA((char*)zConverted);
 36234 #endif
 36235   sqlite3_free(zConverted);
 36236   switch( flags ){
 36237     case SQLITE_ACCESS_READ:
 36238     case SQLITE_ACCESS_EXISTS:
 36239       rc = attr!=INVALID_FILE_ATTRIBUTES;
 36240       break;
 36241     case SQLITE_ACCESS_READWRITE:
 36242       rc = attr!=INVALID_FILE_ATTRIBUTES &&
 36243              (attr & FILE_ATTRIBUTE_READONLY)==0;
 36244       break;
 36245     default:
 36246       assert(!"Invalid flags argument");
 36248   *pResOut = rc;
 36249   OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
 36250            zFilename, pResOut, *pResOut));
 36251   return SQLITE_OK;
 36254 /*
 36255 ** Returns non-zero if the specified path name starts with a drive letter
 36256 ** followed by a colon character.
 36257 */
 36258 static BOOL winIsDriveLetterAndColon(
 36259   const char *zPathname
 36260 ){
 36261   return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
 36264 /*
 36265 ** Returns non-zero if the specified path name should be used verbatim.  If
 36266 ** non-zero is returned from this function, the calling function must simply
 36267 ** use the provided path name verbatim -OR- resolve it into a full path name
 36268 ** using the GetFullPathName Win32 API function (if available).
 36269 */
 36270 static BOOL winIsVerbatimPathname(
 36271   const char *zPathname
 36272 ){
 36273   /*
 36274   ** If the path name starts with a forward slash or a backslash, it is either
 36275   ** a legal UNC name, a volume relative path, or an absolute path name in the
 36276   ** "Unix" format on Windows.  There is no easy way to differentiate between
 36277   ** the final two cases; therefore, we return the safer return value of TRUE
 36278   ** so that callers of this function will simply use it verbatim.
 36279   */
 36280   if ( winIsDirSep(zPathname[0]) ){
 36281     return TRUE;
 36284   /*
 36285   ** If the path name starts with a letter and a colon it is either a volume
 36286   ** relative path or an absolute path.  Callers of this function must not
 36287   ** attempt to treat it as a relative path name (i.e. they should simply use
 36288   ** it verbatim).
 36289   */
 36290   if ( winIsDriveLetterAndColon(zPathname) ){
 36291     return TRUE;
 36294   /*
 36295   ** If we get to this point, the path name should almost certainly be a purely
 36296   ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
 36297   */
 36298   return FALSE;
 36301 /*
 36302 ** Turn a relative pathname into a full pathname.  Write the full
 36303 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
 36304 ** bytes in size.
 36305 */
 36306 static int winFullPathname(
 36307   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
 36308   const char *zRelative,        /* Possibly relative input path */
 36309   int nFull,                    /* Size of output buffer in bytes */
 36310   char *zFull                   /* Output buffer */
 36311 ){
 36313 #if defined(__CYGWIN__)
 36314   SimulateIOError( return SQLITE_ERROR );
 36315   UNUSED_PARAMETER(nFull);
 36316   assert( nFull>=pVfs->mxPathname );
 36317   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 36318     /*
 36319     ** NOTE: We are dealing with a relative path name and the data
 36320     **       directory has been set.  Therefore, use it as the basis
 36321     **       for converting the relative path name to an absolute
 36322     **       one by prepending the data directory and a slash.
 36323     */
 36324     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
 36325     if( !zOut ){
 36326       return SQLITE_IOERR_NOMEM;
 36328     if( cygwin_conv_path(
 36329             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
 36330             CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
 36331       sqlite3_free(zOut);
 36332       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
 36333                          "winFullPathname1", zRelative);
 36334     }else{
 36335       char *zUtf8 = winConvertToUtf8Filename(zOut);
 36336       if( !zUtf8 ){
 36337         sqlite3_free(zOut);
 36338         return SQLITE_IOERR_NOMEM;
 36340       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
 36341                        sqlite3_data_directory, winGetDirSep(), zUtf8);
 36342       sqlite3_free(zUtf8);
 36343       sqlite3_free(zOut);
 36345   }else{
 36346     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
 36347     if( !zOut ){
 36348       return SQLITE_IOERR_NOMEM;
 36350     if( cygwin_conv_path(
 36351             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
 36352             zRelative, zOut, pVfs->mxPathname+1)<0 ){
 36353       sqlite3_free(zOut);
 36354       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
 36355                          "winFullPathname2", zRelative);
 36356     }else{
 36357       char *zUtf8 = winConvertToUtf8Filename(zOut);
 36358       if( !zUtf8 ){
 36359         sqlite3_free(zOut);
 36360         return SQLITE_IOERR_NOMEM;
 36362       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
 36363       sqlite3_free(zUtf8);
 36364       sqlite3_free(zOut);
 36367   return SQLITE_OK;
 36368 #endif
 36370 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
 36371   SimulateIOError( return SQLITE_ERROR );
 36372   /* WinCE has no concept of a relative pathname, or so I am told. */
 36373   /* WinRT has no way to convert a relative path to an absolute one. */
 36374   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 36375     /*
 36376     ** NOTE: We are dealing with a relative path name and the data
 36377     **       directory has been set.  Therefore, use it as the basis
 36378     **       for converting the relative path name to an absolute
 36379     **       one by prepending the data directory and a backslash.
 36380     */
 36381     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
 36382                      sqlite3_data_directory, winGetDirSep(), zRelative);
 36383   }else{
 36384     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
 36386   return SQLITE_OK;
 36387 #endif
 36389 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
 36390   DWORD nByte;
 36391   void *zConverted;
 36392   char *zOut;
 36394   /* If this path name begins with "/X:", where "X" is any alphabetic
 36395   ** character, discard the initial "/" from the pathname.
 36396   */
 36397   if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
 36398     zRelative++;
 36401   /* It's odd to simulate an io-error here, but really this is just
 36402   ** using the io-error infrastructure to test that SQLite handles this
 36403   ** function failing. This function could fail if, for example, the
 36404   ** current working directory has been unlinked.
 36405   */
 36406   SimulateIOError( return SQLITE_ERROR );
 36407   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
 36408     /*
 36409     ** NOTE: We are dealing with a relative path name and the data
 36410     **       directory has been set.  Therefore, use it as the basis
 36411     **       for converting the relative path name to an absolute
 36412     **       one by prepending the data directory and a backslash.
 36413     */
 36414     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
 36415                      sqlite3_data_directory, winGetDirSep(), zRelative);
 36416     return SQLITE_OK;
 36418   zConverted = winConvertFromUtf8Filename(zRelative);
 36419   if( zConverted==0 ){
 36420     return SQLITE_IOERR_NOMEM;
 36422   if( osIsNT() ){
 36423     LPWSTR zTemp;
 36424     nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
 36425     if( nByte==0 ){
 36426       sqlite3_free(zConverted);
 36427       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
 36428                          "winFullPathname1", zRelative);
 36430     nByte += 3;
 36431     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 36432     if( zTemp==0 ){
 36433       sqlite3_free(zConverted);
 36434       return SQLITE_IOERR_NOMEM;
 36436     nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
 36437     if( nByte==0 ){
 36438       sqlite3_free(zConverted);
 36439       sqlite3_free(zTemp);
 36440       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
 36441                          "winFullPathname2", zRelative);
 36443     sqlite3_free(zConverted);
 36444     zOut = winUnicodeToUtf8(zTemp);
 36445     sqlite3_free(zTemp);
 36447 #ifdef SQLITE_WIN32_HAS_ANSI
 36448   else{
 36449     char *zTemp;
 36450     nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
 36451     if( nByte==0 ){
 36452       sqlite3_free(zConverted);
 36453       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
 36454                          "winFullPathname3", zRelative);
 36456     nByte += 3;
 36457     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
 36458     if( zTemp==0 ){
 36459       sqlite3_free(zConverted);
 36460       return SQLITE_IOERR_NOMEM;
 36462     nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
 36463     if( nByte==0 ){
 36464       sqlite3_free(zConverted);
 36465       sqlite3_free(zTemp);
 36466       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
 36467                          "winFullPathname4", zRelative);
 36469     sqlite3_free(zConverted);
 36470     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 36471     sqlite3_free(zTemp);
 36473 #endif
 36474   if( zOut ){
 36475     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
 36476     sqlite3_free(zOut);
 36477     return SQLITE_OK;
 36478   }else{
 36479     return SQLITE_IOERR_NOMEM;
 36481 #endif
 36484 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 36485 /*
 36486 ** Interfaces for opening a shared library, finding entry points
 36487 ** within the shared library, and closing the shared library.
 36488 */
 36489 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 36490   HANDLE h;
 36491 #if defined(__CYGWIN__)
 36492   int nFull = pVfs->mxPathname+1;
 36493   char *zFull = sqlite3MallocZero( nFull );
 36494   void *zConverted = 0;
 36495   if( zFull==0 ){
 36496     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
 36497     return 0;
 36499   if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
 36500     sqlite3_free(zFull);
 36501     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
 36502     return 0;
 36504   zConverted = winConvertFromUtf8Filename(zFull);
 36505   sqlite3_free(zFull);
 36506 #else
 36507   void *zConverted = winConvertFromUtf8Filename(zFilename);
 36508   UNUSED_PARAMETER(pVfs);
 36509 #endif
 36510   if( zConverted==0 ){
 36511     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
 36512     return 0;
 36514   if( osIsNT() ){
 36515 #if SQLITE_OS_WINRT
 36516     h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
 36517 #else
 36518     h = osLoadLibraryW((LPCWSTR)zConverted);
 36519 #endif
 36521 #ifdef SQLITE_WIN32_HAS_ANSI
 36522   else{
 36523     h = osLoadLibraryA((char*)zConverted);
 36525 #endif
 36526   OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
 36527   sqlite3_free(zConverted);
 36528   return (void*)h;
 36530 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 36531   UNUSED_PARAMETER(pVfs);
 36532   winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
 36534 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
 36535   FARPROC proc;
 36536   UNUSED_PARAMETER(pVfs);
 36537   proc = osGetProcAddressA((HANDLE)pH, zSym);
 36538   OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
 36539            (void*)pH, zSym, (void*)proc));
 36540   return (void(*)(void))proc;
 36542 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
 36543   UNUSED_PARAMETER(pVfs);
 36544   osFreeLibrary((HANDLE)pHandle);
 36545   OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
 36547 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 36548   #define winDlOpen  0
 36549   #define winDlError 0
 36550   #define winDlSym   0
 36551   #define winDlClose 0
 36552 #endif
 36555 /*
 36556 ** Write up to nBuf bytes of randomness into zBuf.
 36557 */
 36558 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 36559   int n = 0;
 36560   UNUSED_PARAMETER(pVfs);
 36561 #if defined(SQLITE_TEST)
 36562   n = nBuf;
 36563   memset(zBuf, 0, nBuf);
 36564 #else
 36565   if( sizeof(SYSTEMTIME)<=nBuf-n ){
 36566     SYSTEMTIME x;
 36567     osGetSystemTime(&x);
 36568     memcpy(&zBuf[n], &x, sizeof(x));
 36569     n += sizeof(x);
 36571   if( sizeof(DWORD)<=nBuf-n ){
 36572     DWORD pid = osGetCurrentProcessId();
 36573     memcpy(&zBuf[n], &pid, sizeof(pid));
 36574     n += sizeof(pid);
 36576 #if SQLITE_OS_WINRT
 36577   if( sizeof(ULONGLONG)<=nBuf-n ){
 36578     ULONGLONG cnt = osGetTickCount64();
 36579     memcpy(&zBuf[n], &cnt, sizeof(cnt));
 36580     n += sizeof(cnt);
 36582 #else
 36583   if( sizeof(DWORD)<=nBuf-n ){
 36584     DWORD cnt = osGetTickCount();
 36585     memcpy(&zBuf[n], &cnt, sizeof(cnt));
 36586     n += sizeof(cnt);
 36588 #endif
 36589   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
 36590     LARGE_INTEGER i;
 36591     osQueryPerformanceCounter(&i);
 36592     memcpy(&zBuf[n], &i, sizeof(i));
 36593     n += sizeof(i);
 36595 #endif
 36596   return n;
 36600 /*
 36601 ** Sleep for a little while.  Return the amount of time slept.
 36602 */
 36603 static int winSleep(sqlite3_vfs *pVfs, int microsec){
 36604   sqlite3_win32_sleep((microsec+999)/1000);
 36605   UNUSED_PARAMETER(pVfs);
 36606   return ((microsec+999)/1000)*1000;
 36609 /*
 36610 ** The following variable, if set to a non-zero value, is interpreted as
 36611 ** the number of seconds since 1970 and is used to set the result of
 36612 ** sqlite3OsCurrentTime() during testing.
 36613 */
 36614 #ifdef SQLITE_TEST
 36615 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
 36616 #endif
 36618 /*
 36619 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
 36620 ** the current time and date as a Julian Day number times 86_400_000.  In
 36621 ** other words, write into *piNow the number of milliseconds since the Julian
 36622 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 36623 ** proleptic Gregorian calendar.
 36624 **
 36625 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
 36626 ** cannot be found.
 36627 */
 36628 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 36629   /* FILETIME structure is a 64-bit value representing the number of 
 36630      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
 36631   */
 36632   FILETIME ft;
 36633   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
 36634 #ifdef SQLITE_TEST
 36635   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 36636 #endif
 36637   /* 2^32 - to avoid use of LL and warnings in gcc */
 36638   static const sqlite3_int64 max32BitValue = 
 36639       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
 36640       (sqlite3_int64)294967296;
 36642 #if SQLITE_OS_WINCE
 36643   SYSTEMTIME time;
 36644   osGetSystemTime(&time);
 36645   /* if SystemTimeToFileTime() fails, it returns zero. */
 36646   if (!osSystemTimeToFileTime(&time,&ft)){
 36647     return SQLITE_ERROR;
 36649 #else
 36650   osGetSystemTimeAsFileTime( &ft );
 36651 #endif
 36653   *piNow = winFiletimeEpoch +
 36654             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
 36655                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
 36657 #ifdef SQLITE_TEST
 36658   if( sqlite3_current_time ){
 36659     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 36661 #endif
 36662   UNUSED_PARAMETER(pVfs);
 36663   return SQLITE_OK;
 36666 /*
 36667 ** Find the current time (in Universal Coordinated Time).  Write the
 36668 ** current time and date as a Julian Day number into *prNow and
 36669 ** return 0.  Return 1 if the time and date cannot be found.
 36670 */
 36671 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
 36672   int rc;
 36673   sqlite3_int64 i;
 36674   rc = winCurrentTimeInt64(pVfs, &i);
 36675   if( !rc ){
 36676     *prNow = i/86400000.0;
 36678   return rc;
 36681 /*
 36682 ** The idea is that this function works like a combination of
 36683 ** GetLastError() and FormatMessage() on Windows (or errno and
 36684 ** strerror_r() on Unix). After an error is returned by an OS
 36685 ** function, SQLite calls this function with zBuf pointing to
 36686 ** a buffer of nBuf bytes. The OS layer should populate the
 36687 ** buffer with a nul-terminated UTF-8 encoded error message
 36688 ** describing the last IO error to have occurred within the calling
 36689 ** thread.
 36690 **
 36691 ** If the error message is too large for the supplied buffer,
 36692 ** it should be truncated. The return value of xGetLastError
 36693 ** is zero if the error message fits in the buffer, or non-zero
 36694 ** otherwise (if the message was truncated). If non-zero is returned,
 36695 ** then it is not necessary to include the nul-terminator character
 36696 ** in the output buffer.
 36697 **
 36698 ** Not supplying an error message will have no adverse effect
 36699 ** on SQLite. It is fine to have an implementation that never
 36700 ** returns an error message:
 36701 **
 36702 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 36703 **     assert(zBuf[0]=='\0');
 36704 **     return 0;
 36705 **   }
 36706 **
 36707 ** However if an error message is supplied, it will be incorporated
 36708 ** by sqlite into the error message available to the user using
 36709 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
 36710 */
 36711 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 36712   UNUSED_PARAMETER(pVfs);
 36713   return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
 36716 /*
 36717 ** Initialize and deinitialize the operating system interface.
 36718 */
 36719 SQLITE_API int sqlite3_os_init(void){
 36720   static sqlite3_vfs winVfs = {
 36721     3,                   /* iVersion */
 36722     sizeof(winFile),     /* szOsFile */
 36723     SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
 36724     0,                   /* pNext */
 36725     "win32",             /* zName */
 36726     0,                   /* pAppData */
 36727     winOpen,             /* xOpen */
 36728     winDelete,           /* xDelete */
 36729     winAccess,           /* xAccess */
 36730     winFullPathname,     /* xFullPathname */
 36731     winDlOpen,           /* xDlOpen */
 36732     winDlError,          /* xDlError */
 36733     winDlSym,            /* xDlSym */
 36734     winDlClose,          /* xDlClose */
 36735     winRandomness,       /* xRandomness */
 36736     winSleep,            /* xSleep */
 36737     winCurrentTime,      /* xCurrentTime */
 36738     winGetLastError,     /* xGetLastError */
 36739     winCurrentTimeInt64, /* xCurrentTimeInt64 */
 36740     winSetSystemCall,    /* xSetSystemCall */
 36741     winGetSystemCall,    /* xGetSystemCall */
 36742     winNextSystemCall,   /* xNextSystemCall */
 36743   };
 36744 #if defined(SQLITE_WIN32_HAS_WIDE)
 36745   static sqlite3_vfs winLongPathVfs = {
 36746     3,                   /* iVersion */
 36747     sizeof(winFile),     /* szOsFile */
 36748     SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
 36749     0,                   /* pNext */
 36750     "win32-longpath",    /* zName */
 36751     0,                   /* pAppData */
 36752     winOpen,             /* xOpen */
 36753     winDelete,           /* xDelete */
 36754     winAccess,           /* xAccess */
 36755     winFullPathname,     /* xFullPathname */
 36756     winDlOpen,           /* xDlOpen */
 36757     winDlError,          /* xDlError */
 36758     winDlSym,            /* xDlSym */
 36759     winDlClose,          /* xDlClose */
 36760     winRandomness,       /* xRandomness */
 36761     winSleep,            /* xSleep */
 36762     winCurrentTime,      /* xCurrentTime */
 36763     winGetLastError,     /* xGetLastError */
 36764     winCurrentTimeInt64, /* xCurrentTimeInt64 */
 36765     winSetSystemCall,    /* xSetSystemCall */
 36766     winGetSystemCall,    /* xGetSystemCall */
 36767     winNextSystemCall,   /* xNextSystemCall */
 36768   };
 36769 #endif
 36771   /* Double-check that the aSyscall[] array has been constructed
 36772   ** correctly.  See ticket [bb3a86e890c8e96ab] */
 36773   assert( ArraySize(aSyscall)==76 );
 36775   /* get memory map allocation granularity */
 36776   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
 36777 #if SQLITE_OS_WINRT
 36778   osGetNativeSystemInfo(&winSysInfo);
 36779 #else
 36780   osGetSystemInfo(&winSysInfo);
 36781 #endif
 36782   assert( winSysInfo.dwAllocationGranularity>0 );
 36783   assert( winSysInfo.dwPageSize>0 );
 36785   sqlite3_vfs_register(&winVfs, 1);
 36787 #if defined(SQLITE_WIN32_HAS_WIDE)
 36788   sqlite3_vfs_register(&winLongPathVfs, 0);
 36789 #endif
 36791   return SQLITE_OK; 
 36794 SQLITE_API int sqlite3_os_end(void){ 
 36795 #if SQLITE_OS_WINRT
 36796   if( sleepObj!=NULL ){
 36797     osCloseHandle(sleepObj);
 36798     sleepObj = NULL;
 36800 #endif
 36801   return SQLITE_OK;
 36804 #endif /* SQLITE_OS_WIN */
 36806 /************** End of os_win.c **********************************************/
 36807 /************** Begin file bitvec.c ******************************************/
 36808 /*
 36809 ** 2008 February 16
 36810 **
 36811 ** The author disclaims copyright to this source code.  In place of
 36812 ** a legal notice, here is a blessing:
 36813 **
 36814 **    May you do good and not evil.
 36815 **    May you find forgiveness for yourself and forgive others.
 36816 **    May you share freely, never taking more than you give.
 36817 **
 36818 *************************************************************************
 36819 ** This file implements an object that represents a fixed-length
 36820 ** bitmap.  Bits are numbered starting with 1.
 36821 **
 36822 ** A bitmap is used to record which pages of a database file have been
 36823 ** journalled during a transaction, or which pages have the "dont-write"
 36824 ** property.  Usually only a few pages are meet either condition.
 36825 ** So the bitmap is usually sparse and has low cardinality.
 36826 ** But sometimes (for example when during a DROP of a large table) most
 36827 ** or all of the pages in a database can get journalled.  In those cases, 
 36828 ** the bitmap becomes dense with high cardinality.  The algorithm needs 
 36829 ** to handle both cases well.
 36830 **
 36831 ** The size of the bitmap is fixed when the object is created.
 36832 **
 36833 ** All bits are clear when the bitmap is created.  Individual bits
 36834 ** may be set or cleared one at a time.
 36835 **
 36836 ** Test operations are about 100 times more common that set operations.
 36837 ** Clear operations are exceedingly rare.  There are usually between
 36838 ** 5 and 500 set operations per Bitvec object, though the number of sets can
 36839 ** sometimes grow into tens of thousands or larger.  The size of the
 36840 ** Bitvec object is the number of pages in the database file at the
 36841 ** start of a transaction, and is thus usually less than a few thousand,
 36842 ** but can be as large as 2 billion for a really big database.
 36843 */
 36845 /* Size of the Bitvec structure in bytes. */
 36846 #define BITVEC_SZ        512
 36848 /* Round the union size down to the nearest pointer boundary, since that's how 
 36849 ** it will be aligned within the Bitvec struct. */
 36850 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
 36852 /* Type of the array "element" for the bitmap representation. 
 36853 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
 36854 ** Setting this to the "natural word" size of your CPU may improve
 36855 ** performance. */
 36856 #define BITVEC_TELEM     u8
 36857 /* Size, in bits, of the bitmap element. */
 36858 #define BITVEC_SZELEM    8
 36859 /* Number of elements in a bitmap array. */
 36860 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
 36861 /* Number of bits in the bitmap array. */
 36862 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
 36864 /* Number of u32 values in hash table. */
 36865 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
 36866 /* Maximum number of entries in hash table before 
 36867 ** sub-dividing and re-hashing. */
 36868 #define BITVEC_MXHASH    (BITVEC_NINT/2)
 36869 /* Hashing function for the aHash representation.
 36870 ** Empirical testing showed that the *37 multiplier 
 36871 ** (an arbitrary prime)in the hash function provided 
 36872 ** no fewer collisions than the no-op *1. */
 36873 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
 36875 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
 36878 /*
 36879 ** A bitmap is an instance of the following structure.
 36880 **
 36881 ** This bitmap records the existence of zero or more bits
 36882 ** with values between 1 and iSize, inclusive.
 36883 **
 36884 ** There are three possible representations of the bitmap.
 36885 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
 36886 ** bitmap.  The least significant bit is bit 1.
 36887 **
 36888 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
 36889 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
 36890 **
 36891 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
 36892 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
 36893 ** handles up to iDivisor separate values of i.  apSub[0] holds
 36894 ** values between 1 and iDivisor.  apSub[1] holds values between
 36895 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
 36896 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
 36897 ** to hold deal with values between 1 and iDivisor.
 36898 */
 36899 struct Bitvec {
 36900   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
 36901   u32 nSet;       /* Number of bits that are set - only valid for aHash
 36902                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
 36903                   ** this would be 125. */
 36904   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
 36905                   /* Should >=0 for apSub element. */
 36906                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
 36907                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
 36908   union {
 36909     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
 36910     u32 aHash[BITVEC_NINT];      /* Hash table representation */
 36911     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
 36912   } u;
 36913 };
 36915 /*
 36916 ** Create a new bitmap object able to handle bits between 0 and iSize,
 36917 ** inclusive.  Return a pointer to the new object.  Return NULL if 
 36918 ** malloc fails.
 36919 */
 36920 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
 36921   Bitvec *p;
 36922   assert( sizeof(*p)==BITVEC_SZ );
 36923   p = sqlite3MallocZero( sizeof(*p) );
 36924   if( p ){
 36925     p->iSize = iSize;
 36927   return p;
 36930 /*
 36931 ** Check to see if the i-th bit is set.  Return true or false.
 36932 ** If p is NULL (if the bitmap has not been created) or if
 36933 ** i is out of range, then return false.
 36934 */
 36935 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
 36936   if( p==0 ) return 0;
 36937   if( i>p->iSize || i==0 ) return 0;
 36938   i--;
 36939   while( p->iDivisor ){
 36940     u32 bin = i/p->iDivisor;
 36941     i = i%p->iDivisor;
 36942     p = p->u.apSub[bin];
 36943     if (!p) {
 36944       return 0;
 36947   if( p->iSize<=BITVEC_NBIT ){
 36948     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
 36949   } else{
 36950     u32 h = BITVEC_HASH(i++);
 36951     while( p->u.aHash[h] ){
 36952       if( p->u.aHash[h]==i ) return 1;
 36953       h = (h+1) % BITVEC_NINT;
 36955     return 0;
 36959 /*
 36960 ** Set the i-th bit.  Return 0 on success and an error code if
 36961 ** anything goes wrong.
 36962 **
 36963 ** This routine might cause sub-bitmaps to be allocated.  Failing
 36964 ** to get the memory needed to hold the sub-bitmap is the only
 36965 ** that can go wrong with an insert, assuming p and i are valid.
 36966 **
 36967 ** The calling function must ensure that p is a valid Bitvec object
 36968 ** and that the value for "i" is within range of the Bitvec object.
 36969 ** Otherwise the behavior is undefined.
 36970 */
 36971 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
 36972   u32 h;
 36973   if( p==0 ) return SQLITE_OK;
 36974   assert( i>0 );
 36975   assert( i<=p->iSize );
 36976   i--;
 36977   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
 36978     u32 bin = i/p->iDivisor;
 36979     i = i%p->iDivisor;
 36980     if( p->u.apSub[bin]==0 ){
 36981       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
 36982       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
 36984     p = p->u.apSub[bin];
 36986   if( p->iSize<=BITVEC_NBIT ){
 36987     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
 36988     return SQLITE_OK;
 36990   h = BITVEC_HASH(i++);
 36991   /* if there wasn't a hash collision, and this doesn't */
 36992   /* completely fill the hash, then just add it without */
 36993   /* worring about sub-dividing and re-hashing. */
 36994   if( !p->u.aHash[h] ){
 36995     if (p->nSet<(BITVEC_NINT-1)) {
 36996       goto bitvec_set_end;
 36997     } else {
 36998       goto bitvec_set_rehash;
 37001   /* there was a collision, check to see if it's already */
 37002   /* in hash, if not, try to find a spot for it */
 37003   do {
 37004     if( p->u.aHash[h]==i ) return SQLITE_OK;
 37005     h++;
 37006     if( h>=BITVEC_NINT ) h = 0;
 37007   } while( p->u.aHash[h] );
 37008   /* we didn't find it in the hash.  h points to the first */
 37009   /* available free spot. check to see if this is going to */
 37010   /* make our hash too "full".  */
 37011 bitvec_set_rehash:
 37012   if( p->nSet>=BITVEC_MXHASH ){
 37013     unsigned int j;
 37014     int rc;
 37015     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
 37016     if( aiValues==0 ){
 37017       return SQLITE_NOMEM;
 37018     }else{
 37019       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 37020       memset(p->u.apSub, 0, sizeof(p->u.apSub));
 37021       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
 37022       rc = sqlite3BitvecSet(p, i);
 37023       for(j=0; j<BITVEC_NINT; j++){
 37024         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
 37026       sqlite3StackFree(0, aiValues);
 37027       return rc;
 37030 bitvec_set_end:
 37031   p->nSet++;
 37032   p->u.aHash[h] = i;
 37033   return SQLITE_OK;
 37036 /*
 37037 ** Clear the i-th bit.
 37038 **
 37039 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
 37040 ** that BitvecClear can use to rebuilt its hash table.
 37041 */
 37042 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
 37043   if( p==0 ) return;
 37044   assert( i>0 );
 37045   i--;
 37046   while( p->iDivisor ){
 37047     u32 bin = i/p->iDivisor;
 37048     i = i%p->iDivisor;
 37049     p = p->u.apSub[bin];
 37050     if (!p) {
 37051       return;
 37054   if( p->iSize<=BITVEC_NBIT ){
 37055     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
 37056   }else{
 37057     unsigned int j;
 37058     u32 *aiValues = pBuf;
 37059     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
 37060     memset(p->u.aHash, 0, sizeof(p->u.aHash));
 37061     p->nSet = 0;
 37062     for(j=0; j<BITVEC_NINT; j++){
 37063       if( aiValues[j] && aiValues[j]!=(i+1) ){
 37064         u32 h = BITVEC_HASH(aiValues[j]-1);
 37065         p->nSet++;
 37066         while( p->u.aHash[h] ){
 37067           h++;
 37068           if( h>=BITVEC_NINT ) h = 0;
 37070         p->u.aHash[h] = aiValues[j];
 37076 /*
 37077 ** Destroy a bitmap object.  Reclaim all memory used.
 37078 */
 37079 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
 37080   if( p==0 ) return;
 37081   if( p->iDivisor ){
 37082     unsigned int i;
 37083     for(i=0; i<BITVEC_NPTR; i++){
 37084       sqlite3BitvecDestroy(p->u.apSub[i]);
 37087   sqlite3_free(p);
 37090 /*
 37091 ** Return the value of the iSize parameter specified when Bitvec *p
 37092 ** was created.
 37093 */
 37094 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
 37095   return p->iSize;
 37098 #ifndef SQLITE_OMIT_BUILTIN_TEST
 37099 /*
 37100 ** Let V[] be an array of unsigned characters sufficient to hold
 37101 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
 37102 ** Then the following macros can be used to set, clear, or test
 37103 ** individual bits within V.
 37104 */
 37105 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
 37106 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
 37107 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
 37109 /*
 37110 ** This routine runs an extensive test of the Bitvec code.
 37111 **
 37112 ** The input is an array of integers that acts as a program
 37113 ** to test the Bitvec.  The integers are opcodes followed
 37114 ** by 0, 1, or 3 operands, depending on the opcode.  Another
 37115 ** opcode follows immediately after the last operand.
 37116 **
 37117 ** There are 6 opcodes numbered from 0 through 5.  0 is the
 37118 ** "halt" opcode and causes the test to end.
 37119 **
 37120 **    0          Halt and return the number of errors
 37121 **    1 N S X    Set N bits beginning with S and incrementing by X
 37122 **    2 N S X    Clear N bits beginning with S and incrementing by X
 37123 **    3 N        Set N randomly chosen bits
 37124 **    4 N        Clear N randomly chosen bits
 37125 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
 37126 **
 37127 ** The opcodes 1 through 4 perform set and clear operations are performed
 37128 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
 37129 ** Opcode 5 works on the linear array only, not on the Bitvec.
 37130 ** Opcode 5 is used to deliberately induce a fault in order to
 37131 ** confirm that error detection works.
 37132 **
 37133 ** At the conclusion of the test the linear array is compared
 37134 ** against the Bitvec object.  If there are any differences,
 37135 ** an error is returned.  If they are the same, zero is returned.
 37136 **
 37137 ** If a memory allocation error occurs, return -1.
 37138 */
 37139 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
 37140   Bitvec *pBitvec = 0;
 37141   unsigned char *pV = 0;
 37142   int rc = -1;
 37143   int i, nx, pc, op;
 37144   void *pTmpSpace;
 37146   /* Allocate the Bitvec to be tested and a linear array of
 37147   ** bits to act as the reference */
 37148   pBitvec = sqlite3BitvecCreate( sz );
 37149   pV = sqlite3MallocZero( (sz+7)/8 + 1 );
 37150   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
 37151   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
 37153   /* NULL pBitvec tests */
 37154   sqlite3BitvecSet(0, 1);
 37155   sqlite3BitvecClear(0, 1, pTmpSpace);
 37157   /* Run the program */
 37158   pc = 0;
 37159   while( (op = aOp[pc])!=0 ){
 37160     switch( op ){
 37161       case 1:
 37162       case 2:
 37163       case 5: {
 37164         nx = 4;
 37165         i = aOp[pc+2] - 1;
 37166         aOp[pc+2] += aOp[pc+3];
 37167         break;
 37169       case 3:
 37170       case 4: 
 37171       default: {
 37172         nx = 2;
 37173         sqlite3_randomness(sizeof(i), &i);
 37174         break;
 37177     if( (--aOp[pc+1]) > 0 ) nx = 0;
 37178     pc += nx;
 37179     i = (i & 0x7fffffff)%sz;
 37180     if( (op & 1)!=0 ){
 37181       SETBIT(pV, (i+1));
 37182       if( op!=5 ){
 37183         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
 37185     }else{
 37186       CLEARBIT(pV, (i+1));
 37187       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
 37191   /* Test to make sure the linear array exactly matches the
 37192   ** Bitvec object.  Start with the assumption that they do
 37193   ** match (rc==0).  Change rc to non-zero if a discrepancy
 37194   ** is found.
 37195   */
 37196   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
 37197           + sqlite3BitvecTest(pBitvec, 0)
 37198           + (sqlite3BitvecSize(pBitvec) - sz);
 37199   for(i=1; i<=sz; i++){
 37200     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
 37201       rc = i;
 37202       break;
 37206   /* Free allocated structure */
 37207 bitvec_end:
 37208   sqlite3_free(pTmpSpace);
 37209   sqlite3_free(pV);
 37210   sqlite3BitvecDestroy(pBitvec);
 37211   return rc;
 37213 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 37215 /************** End of bitvec.c **********************************************/
 37216 /************** Begin file pcache.c ******************************************/
 37217 /*
 37218 ** 2008 August 05
 37219 **
 37220 ** The author disclaims copyright to this source code.  In place of
 37221 ** a legal notice, here is a blessing:
 37222 **
 37223 **    May you do good and not evil.
 37224 **    May you find forgiveness for yourself and forgive others.
 37225 **    May you share freely, never taking more than you give.
 37226 **
 37227 *************************************************************************
 37228 ** This file implements that page cache.
 37229 */
 37231 /*
 37232 ** A complete page cache is an instance of this structure.
 37233 */
 37234 struct PCache {
 37235   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
 37236   PgHdr *pSynced;                     /* Last synced page in dirty page list */
 37237   int nRef;                           /* Number of referenced pages */
 37238   int szCache;                        /* Configured cache size */
 37239   int szPage;                         /* Size of every page in this cache */
 37240   int szExtra;                        /* Size of extra space for each page */
 37241   u8 bPurgeable;                      /* True if pages are on backing store */
 37242   u8 eCreate;                         /* eCreate value for for xFetch() */
 37243   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
 37244   void *pStress;                      /* Argument to xStress */
 37245   sqlite3_pcache *pCache;             /* Pluggable cache module */
 37246   PgHdr *pPage1;                      /* Reference to page 1 */
 37247 };
 37249 /*
 37250 ** Some of the assert() macros in this code are too expensive to run
 37251 ** even during normal debugging.  Use them only rarely on long-running
 37252 ** tests.  Enable the expensive asserts using the
 37253 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
 37254 */
 37255 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 37256 # define expensive_assert(X)  assert(X)
 37257 #else
 37258 # define expensive_assert(X)
 37259 #endif
 37261 /********************************** Linked List Management ********************/
 37263 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
 37264 /*
 37265 ** Check that the pCache->pSynced variable is set correctly. If it
 37266 ** is not, either fail an assert or return zero. Otherwise, return
 37267 ** non-zero. This is only used in debugging builds, as follows:
 37268 **
 37269 **   expensive_assert( pcacheCheckSynced(pCache) );
 37270 */
 37271 static int pcacheCheckSynced(PCache *pCache){
 37272   PgHdr *p;
 37273   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
 37274     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
 37276   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
 37278 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
 37280 /*
 37281 ** Remove page pPage from the list of dirty pages.
 37282 */
 37283 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
 37284   PCache *p = pPage->pCache;
 37286   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
 37287   assert( pPage->pDirtyPrev || pPage==p->pDirty );
 37289   /* Update the PCache1.pSynced variable if necessary. */
 37290   if( p->pSynced==pPage ){
 37291     PgHdr *pSynced = pPage->pDirtyPrev;
 37292     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
 37293       pSynced = pSynced->pDirtyPrev;
 37295     p->pSynced = pSynced;
 37298   if( pPage->pDirtyNext ){
 37299     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
 37300   }else{
 37301     assert( pPage==p->pDirtyTail );
 37302     p->pDirtyTail = pPage->pDirtyPrev;
 37304   if( pPage->pDirtyPrev ){
 37305     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
 37306   }else{
 37307     assert( pPage==p->pDirty );
 37308     p->pDirty = pPage->pDirtyNext;
 37309     if( p->pDirty==0 && p->bPurgeable ){
 37310       assert( p->eCreate==1 );
 37311       p->eCreate = 2;
 37314   pPage->pDirtyNext = 0;
 37315   pPage->pDirtyPrev = 0;
 37317   expensive_assert( pcacheCheckSynced(p) );
 37320 /*
 37321 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
 37322 ** pPage).
 37323 */
 37324 static void pcacheAddToDirtyList(PgHdr *pPage){
 37325   PCache *p = pPage->pCache;
 37327   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
 37329   pPage->pDirtyNext = p->pDirty;
 37330   if( pPage->pDirtyNext ){
 37331     assert( pPage->pDirtyNext->pDirtyPrev==0 );
 37332     pPage->pDirtyNext->pDirtyPrev = pPage;
 37333   }else if( p->bPurgeable ){
 37334     assert( p->eCreate==2 );
 37335     p->eCreate = 1;
 37337   p->pDirty = pPage;
 37338   if( !p->pDirtyTail ){
 37339     p->pDirtyTail = pPage;
 37341   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
 37342     p->pSynced = pPage;
 37344   expensive_assert( pcacheCheckSynced(p) );
 37347 /*
 37348 ** Wrapper around the pluggable caches xUnpin method. If the cache is
 37349 ** being used for an in-memory database, this function is a no-op.
 37350 */
 37351 static void pcacheUnpin(PgHdr *p){
 37352   PCache *pCache = p->pCache;
 37353   if( pCache->bPurgeable ){
 37354     if( p->pgno==1 ){
 37355       pCache->pPage1 = 0;
 37357     sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
 37361 /*************************************************** General Interfaces ******
 37362 **
 37363 ** Initialize and shutdown the page cache subsystem. Neither of these 
 37364 ** functions are threadsafe.
 37365 */
 37366 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
 37367   if( sqlite3GlobalConfig.pcache2.xInit==0 ){
 37368     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
 37369     ** built-in default page cache is used instead of the application defined
 37370     ** page cache. */
 37371     sqlite3PCacheSetDefault();
 37373   return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
 37375 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
 37376   if( sqlite3GlobalConfig.pcache2.xShutdown ){
 37377     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
 37378     sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
 37382 /*
 37383 ** Return the size in bytes of a PCache object.
 37384 */
 37385 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
 37387 /*
 37388 ** Create a new PCache object. Storage space to hold the object
 37389 ** has already been allocated and is passed in as the p pointer. 
 37390 ** The caller discovers how much space needs to be allocated by 
 37391 ** calling sqlite3PcacheSize().
 37392 */
 37393 SQLITE_PRIVATE void sqlite3PcacheOpen(
 37394   int szPage,                  /* Size of every page */
 37395   int szExtra,                 /* Extra space associated with each page */
 37396   int bPurgeable,              /* True if pages are on backing store */
 37397   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
 37398   void *pStress,               /* Argument to xStress */
 37399   PCache *p                    /* Preallocated space for the PCache */
 37400 ){
 37401   memset(p, 0, sizeof(PCache));
 37402   p->szPage = szPage;
 37403   p->szExtra = szExtra;
 37404   p->bPurgeable = bPurgeable;
 37405   p->eCreate = 2;
 37406   p->xStress = xStress;
 37407   p->pStress = pStress;
 37408   p->szCache = 100;
 37411 /*
 37412 ** Change the page size for PCache object. The caller must ensure that there
 37413 ** are no outstanding page references when this function is called.
 37414 */
 37415 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
 37416   assert( pCache->nRef==0 && pCache->pDirty==0 );
 37417   if( pCache->pCache ){
 37418     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 37419     pCache->pCache = 0;
 37420     pCache->pPage1 = 0;
 37422   pCache->szPage = szPage;
 37425 /*
 37426 ** Compute the number of pages of cache requested.
 37427 */
 37428 static int numberOfCachePages(PCache *p){
 37429   if( p->szCache>=0 ){
 37430     return p->szCache;
 37431   }else{
 37432     return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
 37436 /*
 37437 ** Try to obtain a page from the cache.
 37438 */
 37439 SQLITE_PRIVATE int sqlite3PcacheFetch(
 37440   PCache *pCache,       /* Obtain the page from this cache */
 37441   Pgno pgno,            /* Page number to obtain */
 37442   int createFlag,       /* If true, create page if it does not exist already */
 37443   PgHdr **ppPage        /* Write the page here */
 37444 ){
 37445   sqlite3_pcache_page *pPage;
 37446   PgHdr *pPgHdr = 0;
 37447   int eCreate;
 37449   assert( pCache!=0 );
 37450   assert( createFlag==1 || createFlag==0 );
 37451   assert( pgno>0 );
 37453   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
 37454   ** allocate it now.
 37455   */
 37456   if( !pCache->pCache ){
 37457     sqlite3_pcache *p;
 37458     if( !createFlag ){
 37459       *ppPage = 0;
 37460       return SQLITE_OK;
 37462     p = sqlite3GlobalConfig.pcache2.xCreate(
 37463         pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
 37464     );
 37465     if( !p ){
 37466       return SQLITE_NOMEM;
 37468     sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
 37469     pCache->pCache = p;
 37472   /* eCreate defines what to do if the page does not exist.
 37473   **    0     Do not allocate a new page.  (createFlag==0)
 37474   **    1     Allocate a new page if doing so is inexpensive.
 37475   **          (createFlag==1 AND bPurgeable AND pDirty)
 37476   **    2     Allocate a new page even it doing so is difficult.
 37477   **          (createFlag==1 AND !(bPurgeable AND pDirty)
 37478   */
 37479   eCreate = createFlag==0 ? 0 : pCache->eCreate;
 37480   assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
 37481   pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
 37482   if( !pPage && eCreate==1 ){
 37483     PgHdr *pPg;
 37485     /* Find a dirty page to write-out and recycle. First try to find a 
 37486     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
 37487     ** cleared), but if that is not possible settle for any other 
 37488     ** unreferenced dirty page.
 37489     */
 37490     expensive_assert( pcacheCheckSynced(pCache) );
 37491     for(pPg=pCache->pSynced; 
 37492         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
 37493         pPg=pPg->pDirtyPrev
 37494     );
 37495     pCache->pSynced = pPg;
 37496     if( !pPg ){
 37497       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
 37499     if( pPg ){
 37500       int rc;
 37501 #ifdef SQLITE_LOG_CACHE_SPILL
 37502       sqlite3_log(SQLITE_FULL, 
 37503                   "spill page %d making room for %d - cache used: %d/%d",
 37504                   pPg->pgno, pgno,
 37505                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
 37506                   numberOfCachePages(pCache));
 37507 #endif
 37508       rc = pCache->xStress(pCache->pStress, pPg);
 37509       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 37510         return rc;
 37514     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
 37517   if( pPage ){
 37518     pPgHdr = (PgHdr *)pPage->pExtra;
 37520     if( !pPgHdr->pPage ){
 37521       memset(pPgHdr, 0, sizeof(PgHdr));
 37522       pPgHdr->pPage = pPage;
 37523       pPgHdr->pData = pPage->pBuf;
 37524       pPgHdr->pExtra = (void *)&pPgHdr[1];
 37525       memset(pPgHdr->pExtra, 0, pCache->szExtra);
 37526       pPgHdr->pCache = pCache;
 37527       pPgHdr->pgno = pgno;
 37529     assert( pPgHdr->pCache==pCache );
 37530     assert( pPgHdr->pgno==pgno );
 37531     assert( pPgHdr->pData==pPage->pBuf );
 37532     assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
 37534     if( 0==pPgHdr->nRef ){
 37535       pCache->nRef++;
 37537     pPgHdr->nRef++;
 37538     if( pgno==1 ){
 37539       pCache->pPage1 = pPgHdr;
 37542   *ppPage = pPgHdr;
 37543   return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
 37546 /*
 37547 ** Decrement the reference count on a page. If the page is clean and the
 37548 ** reference count drops to 0, then it is made elible for recycling.
 37549 */
 37550 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
 37551   assert( p->nRef>0 );
 37552   p->nRef--;
 37553   if( p->nRef==0 ){
 37554     PCache *pCache = p->pCache;
 37555     pCache->nRef--;
 37556     if( (p->flags&PGHDR_DIRTY)==0 ){
 37557       pcacheUnpin(p);
 37558     }else{
 37559       /* Move the page to the head of the dirty list. */
 37560       pcacheRemoveFromDirtyList(p);
 37561       pcacheAddToDirtyList(p);
 37566 /*
 37567 ** Increase the reference count of a supplied page by 1.
 37568 */
 37569 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
 37570   assert(p->nRef>0);
 37571   p->nRef++;
 37574 /*
 37575 ** Drop a page from the cache. There must be exactly one reference to the
 37576 ** page. This function deletes that reference, so after it returns the
 37577 ** page pointed to by p is invalid.
 37578 */
 37579 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
 37580   PCache *pCache;
 37581   assert( p->nRef==1 );
 37582   if( p->flags&PGHDR_DIRTY ){
 37583     pcacheRemoveFromDirtyList(p);
 37585   pCache = p->pCache;
 37586   pCache->nRef--;
 37587   if( p->pgno==1 ){
 37588     pCache->pPage1 = 0;
 37590   sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
 37593 /*
 37594 ** Make sure the page is marked as dirty. If it isn't dirty already,
 37595 ** make it so.
 37596 */
 37597 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
 37598   p->flags &= ~PGHDR_DONT_WRITE;
 37599   assert( p->nRef>0 );
 37600   if( 0==(p->flags & PGHDR_DIRTY) ){
 37601     p->flags |= PGHDR_DIRTY;
 37602     pcacheAddToDirtyList( p);
 37606 /*
 37607 ** Make sure the page is marked as clean. If it isn't clean already,
 37608 ** make it so.
 37609 */
 37610 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
 37611   if( (p->flags & PGHDR_DIRTY) ){
 37612     pcacheRemoveFromDirtyList(p);
 37613     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
 37614     if( p->nRef==0 ){
 37615       pcacheUnpin(p);
 37620 /*
 37621 ** Make every page in the cache clean.
 37622 */
 37623 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
 37624   PgHdr *p;
 37625   while( (p = pCache->pDirty)!=0 ){
 37626     sqlite3PcacheMakeClean(p);
 37630 /*
 37631 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
 37632 */
 37633 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
 37634   PgHdr *p;
 37635   for(p=pCache->pDirty; p; p=p->pDirtyNext){
 37636     p->flags &= ~PGHDR_NEED_SYNC;
 37638   pCache->pSynced = pCache->pDirtyTail;
 37641 /*
 37642 ** Change the page number of page p to newPgno. 
 37643 */
 37644 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
 37645   PCache *pCache = p->pCache;
 37646   assert( p->nRef>0 );
 37647   assert( newPgno>0 );
 37648   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
 37649   p->pgno = newPgno;
 37650   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
 37651     pcacheRemoveFromDirtyList(p);
 37652     pcacheAddToDirtyList(p);
 37656 /*
 37657 ** Drop every cache entry whose page number is greater than "pgno". The
 37658 ** caller must ensure that there are no outstanding references to any pages
 37659 ** other than page 1 with a page number greater than pgno.
 37660 **
 37661 ** If there is a reference to page 1 and the pgno parameter passed to this
 37662 ** function is 0, then the data area associated with page 1 is zeroed, but
 37663 ** the page object is not dropped.
 37664 */
 37665 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
 37666   if( pCache->pCache ){
 37667     PgHdr *p;
 37668     PgHdr *pNext;
 37669     for(p=pCache->pDirty; p; p=pNext){
 37670       pNext = p->pDirtyNext;
 37671       /* This routine never gets call with a positive pgno except right
 37672       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
 37673       ** it must be that pgno==0.
 37674       */
 37675       assert( p->pgno>0 );
 37676       if( ALWAYS(p->pgno>pgno) ){
 37677         assert( p->flags&PGHDR_DIRTY );
 37678         sqlite3PcacheMakeClean(p);
 37681     if( pgno==0 && pCache->pPage1 ){
 37682       memset(pCache->pPage1->pData, 0, pCache->szPage);
 37683       pgno = 1;
 37685     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
 37689 /*
 37690 ** Close a cache.
 37691 */
 37692 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
 37693   if( pCache->pCache ){
 37694     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
 37698 /* 
 37699 ** Discard the contents of the cache.
 37700 */
 37701 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
 37702   sqlite3PcacheTruncate(pCache, 0);
 37705 /*
 37706 ** Merge two lists of pages connected by pDirty and in pgno order.
 37707 ** Do not both fixing the pDirtyPrev pointers.
 37708 */
 37709 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
 37710   PgHdr result, *pTail;
 37711   pTail = &result;
 37712   while( pA && pB ){
 37713     if( pA->pgno<pB->pgno ){
 37714       pTail->pDirty = pA;
 37715       pTail = pA;
 37716       pA = pA->pDirty;
 37717     }else{
 37718       pTail->pDirty = pB;
 37719       pTail = pB;
 37720       pB = pB->pDirty;
 37723   if( pA ){
 37724     pTail->pDirty = pA;
 37725   }else if( pB ){
 37726     pTail->pDirty = pB;
 37727   }else{
 37728     pTail->pDirty = 0;
 37730   return result.pDirty;
 37733 /*
 37734 ** Sort the list of pages in accending order by pgno.  Pages are
 37735 ** connected by pDirty pointers.  The pDirtyPrev pointers are
 37736 ** corrupted by this sort.
 37737 **
 37738 ** Since there cannot be more than 2^31 distinct pages in a database,
 37739 ** there cannot be more than 31 buckets required by the merge sorter.
 37740 ** One extra bucket is added to catch overflow in case something
 37741 ** ever changes to make the previous sentence incorrect.
 37742 */
 37743 #define N_SORT_BUCKET  32
 37744 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
 37745   PgHdr *a[N_SORT_BUCKET], *p;
 37746   int i;
 37747   memset(a, 0, sizeof(a));
 37748   while( pIn ){
 37749     p = pIn;
 37750     pIn = p->pDirty;
 37751     p->pDirty = 0;
 37752     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
 37753       if( a[i]==0 ){
 37754         a[i] = p;
 37755         break;
 37756       }else{
 37757         p = pcacheMergeDirtyList(a[i], p);
 37758         a[i] = 0;
 37761     if( NEVER(i==N_SORT_BUCKET-1) ){
 37762       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
 37763       ** the input list.  But that is impossible.
 37764       */
 37765       a[i] = pcacheMergeDirtyList(a[i], p);
 37768   p = a[0];
 37769   for(i=1; i<N_SORT_BUCKET; i++){
 37770     p = pcacheMergeDirtyList(p, a[i]);
 37772   return p;
 37775 /*
 37776 ** Return a list of all dirty pages in the cache, sorted by page number.
 37777 */
 37778 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
 37779   PgHdr *p;
 37780   for(p=pCache->pDirty; p; p=p->pDirtyNext){
 37781     p->pDirty = p->pDirtyNext;
 37783   return pcacheSortDirtyList(pCache->pDirty);
 37786 /* 
 37787 ** Return the total number of referenced pages held by the cache.
 37788 */
 37789 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
 37790   return pCache->nRef;
 37793 /*
 37794 ** Return the number of references to the page supplied as an argument.
 37795 */
 37796 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
 37797   return p->nRef;
 37800 /* 
 37801 ** Return the total number of pages in the cache.
 37802 */
 37803 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
 37804   int nPage = 0;
 37805   if( pCache->pCache ){
 37806     nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
 37808   return nPage;
 37811 #ifdef SQLITE_TEST
 37812 /*
 37813 ** Get the suggested cache-size value.
 37814 */
 37815 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
 37816   return numberOfCachePages(pCache);
 37818 #endif
 37820 /*
 37821 ** Set the suggested cache-size value.
 37822 */
 37823 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
 37824   pCache->szCache = mxPage;
 37825   if( pCache->pCache ){
 37826     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
 37827                                            numberOfCachePages(pCache));
 37831 /*
 37832 ** Free up as much memory as possible from the page cache.
 37833 */
 37834 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
 37835   if( pCache->pCache ){
 37836     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
 37840 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
 37841 /*
 37842 ** For all dirty pages currently in the cache, invoke the specified
 37843 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
 37844 ** defined.
 37845 */
 37846 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
 37847   PgHdr *pDirty;
 37848   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
 37849     xIter(pDirty);
 37852 #endif
 37854 /************** End of pcache.c **********************************************/
 37855 /************** Begin file pcache1.c *****************************************/
 37856 /*
 37857 ** 2008 November 05
 37858 **
 37859 ** The author disclaims copyright to this source code.  In place of
 37860 ** a legal notice, here is a blessing:
 37861 **
 37862 **    May you do good and not evil.
 37863 **    May you find forgiveness for yourself and forgive others.
 37864 **    May you share freely, never taking more than you give.
 37865 **
 37866 *************************************************************************
 37867 **
 37868 ** This file implements the default page cache implementation (the
 37869 ** sqlite3_pcache interface). It also contains part of the implementation
 37870 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
 37871 ** If the default page cache implementation is overriden, then neither of
 37872 ** these two features are available.
 37873 */
 37876 typedef struct PCache1 PCache1;
 37877 typedef struct PgHdr1 PgHdr1;
 37878 typedef struct PgFreeslot PgFreeslot;
 37879 typedef struct PGroup PGroup;
 37881 /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
 37882 ** of one or more PCaches that are able to recycle each others unpinned
 37883 ** pages when they are under memory pressure.  A PGroup is an instance of
 37884 ** the following object.
 37885 **
 37886 ** This page cache implementation works in one of two modes:
 37887 **
 37888 **   (1)  Every PCache is the sole member of its own PGroup.  There is
 37889 **        one PGroup per PCache.
 37890 **
 37891 **   (2)  There is a single global PGroup that all PCaches are a member
 37892 **        of.
 37893 **
 37894 ** Mode 1 uses more memory (since PCache instances are not able to rob
 37895 ** unused pages from other PCaches) but it also operates without a mutex,
 37896 ** and is therefore often faster.  Mode 2 requires a mutex in order to be
 37897 ** threadsafe, but recycles pages more efficiently.
 37898 **
 37899 ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
 37900 ** PGroup which is the pcache1.grp global variable and its mutex is
 37901 ** SQLITE_MUTEX_STATIC_LRU.
 37902 */
 37903 struct PGroup {
 37904   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
 37905   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
 37906   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
 37907   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
 37908   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
 37909   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
 37910 };
 37912 /* Each page cache is an instance of the following object.  Every
 37913 ** open database file (including each in-memory database and each
 37914 ** temporary or transient database) has a single page cache which
 37915 ** is an instance of this object.
 37916 **
 37917 ** Pointers to structures of this type are cast and returned as 
 37918 ** opaque sqlite3_pcache* handles.
 37919 */
 37920 struct PCache1 {
 37921   /* Cache configuration parameters. Page size (szPage) and the purgeable
 37922   ** flag (bPurgeable) are set when the cache is created. nMax may be 
 37923   ** modified at any time by a call to the pcache1Cachesize() method.
 37924   ** The PGroup mutex must be held when accessing nMax.
 37925   */
 37926   PGroup *pGroup;                     /* PGroup this cache belongs to */
 37927   int szPage;                         /* Size of allocated pages in bytes */
 37928   int szExtra;                        /* Size of extra space in bytes */
 37929   int bPurgeable;                     /* True if cache is purgeable */
 37930   unsigned int nMin;                  /* Minimum number of pages reserved */
 37931   unsigned int nMax;                  /* Configured "cache_size" value */
 37932   unsigned int n90pct;                /* nMax*9/10 */
 37933   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
 37935   /* Hash table of all pages. The following variables may only be accessed
 37936   ** when the accessor is holding the PGroup mutex.
 37937   */
 37938   unsigned int nRecyclable;           /* Number of pages in the LRU list */
 37939   unsigned int nPage;                 /* Total number of pages in apHash */
 37940   unsigned int nHash;                 /* Number of slots in apHash[] */
 37941   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
 37942 };
 37944 /*
 37945 ** Each cache entry is represented by an instance of the following 
 37946 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
 37947 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
 37948 ** in memory.
 37949 */
 37950 struct PgHdr1 {
 37951   sqlite3_pcache_page page;
 37952   unsigned int iKey;             /* Key value (page number) */
 37953   u8 isPinned;                   /* Page in use, not on the LRU list */
 37954   PgHdr1 *pNext;                 /* Next in hash table chain */
 37955   PCache1 *pCache;               /* Cache that currently owns this page */
 37956   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
 37957   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
 37958 };
 37960 /*
 37961 ** Free slots in the allocator used to divide up the buffer provided using
 37962 ** the SQLITE_CONFIG_PAGECACHE mechanism.
 37963 */
 37964 struct PgFreeslot {
 37965   PgFreeslot *pNext;  /* Next free slot */
 37966 };
 37968 /*
 37969 ** Global data used by this cache.
 37970 */
 37971 static SQLITE_WSD struct PCacheGlobal {
 37972   PGroup grp;                    /* The global PGroup for mode (2) */
 37974   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
 37975   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
 37976   ** fixed at sqlite3_initialize() time and do not require mutex protection.
 37977   ** The nFreeSlot and pFree values do require mutex protection.
 37978   */
 37979   int isInit;                    /* True if initialized */
 37980   int szSlot;                    /* Size of each free slot */
 37981   int nSlot;                     /* The number of pcache slots */
 37982   int nReserve;                  /* Try to keep nFreeSlot above this */
 37983   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
 37984   /* Above requires no mutex.  Use mutex below for variable that follow. */
 37985   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
 37986   PgFreeslot *pFree;             /* Free page blocks */
 37987   int nFreeSlot;                 /* Number of unused pcache slots */
 37988   /* The following value requires a mutex to change.  We skip the mutex on
 37989   ** reading because (1) most platforms read a 32-bit integer atomically and
 37990   ** (2) even if an incorrect value is read, no great harm is done since this
 37991   ** is really just an optimization. */
 37992   int bUnderPressure;            /* True if low on PAGECACHE memory */
 37993 } pcache1_g;
 37995 /*
 37996 ** All code in this file should access the global structure above via the
 37997 ** alias "pcache1". This ensures that the WSD emulation is used when
 37998 ** compiling for systems that do not support real WSD.
 37999 */
 38000 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
 38002 /*
 38003 ** Macros to enter and leave the PCache LRU mutex.
 38004 */
 38005 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
 38006 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
 38008 /******************************************************************************/
 38009 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
 38011 /*
 38012 ** This function is called during initialization if a static buffer is 
 38013 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
 38014 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
 38015 ** enough to contain 'n' buffers of 'sz' bytes each.
 38016 **
 38017 ** This routine is called from sqlite3_initialize() and so it is guaranteed
 38018 ** to be serialized already.  There is no need for further mutexing.
 38019 */
 38020 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 38021   if( pcache1.isInit ){
 38022     PgFreeslot *p;
 38023     sz = ROUNDDOWN8(sz);
 38024     pcache1.szSlot = sz;
 38025     pcache1.nSlot = pcache1.nFreeSlot = n;
 38026     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
 38027     pcache1.pStart = pBuf;
 38028     pcache1.pFree = 0;
 38029     pcache1.bUnderPressure = 0;
 38030     while( n-- ){
 38031       p = (PgFreeslot*)pBuf;
 38032       p->pNext = pcache1.pFree;
 38033       pcache1.pFree = p;
 38034       pBuf = (void*)&((char*)pBuf)[sz];
 38036     pcache1.pEnd = pBuf;
 38040 /*
 38041 ** Malloc function used within this file to allocate space from the buffer
 38042 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
 38043 ** such buffer exists or there is no space left in it, this function falls 
 38044 ** back to sqlite3Malloc().
 38045 **
 38046 ** Multiple threads can run this routine at the same time.  Global variables
 38047 ** in pcache1 need to be protected via mutex.
 38048 */
 38049 static void *pcache1Alloc(int nByte){
 38050   void *p = 0;
 38051   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 38052   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
 38053   if( nByte<=pcache1.szSlot ){
 38054     sqlite3_mutex_enter(pcache1.mutex);
 38055     p = (PgHdr1 *)pcache1.pFree;
 38056     if( p ){
 38057       pcache1.pFree = pcache1.pFree->pNext;
 38058       pcache1.nFreeSlot--;
 38059       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 38060       assert( pcache1.nFreeSlot>=0 );
 38061       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
 38063     sqlite3_mutex_leave(pcache1.mutex);
 38065   if( p==0 ){
 38066     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
 38067     ** it from sqlite3Malloc instead.
 38068     */
 38069     p = sqlite3Malloc(nByte);
 38070 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 38071     if( p ){
 38072       int sz = sqlite3MallocSize(p);
 38073       sqlite3_mutex_enter(pcache1.mutex);
 38074       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 38075       sqlite3_mutex_leave(pcache1.mutex);
 38077 #endif
 38078     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 38080   return p;
 38083 /*
 38084 ** Free an allocated buffer obtained from pcache1Alloc().
 38085 */
 38086 static int pcache1Free(void *p){
 38087   int nFreed = 0;
 38088   if( p==0 ) return 0;
 38089   if( p>=pcache1.pStart && p<pcache1.pEnd ){
 38090     PgFreeslot *pSlot;
 38091     sqlite3_mutex_enter(pcache1.mutex);
 38092     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
 38093     pSlot = (PgFreeslot*)p;
 38094     pSlot->pNext = pcache1.pFree;
 38095     pcache1.pFree = pSlot;
 38096     pcache1.nFreeSlot++;
 38097     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
 38098     assert( pcache1.nFreeSlot<=pcache1.nSlot );
 38099     sqlite3_mutex_leave(pcache1.mutex);
 38100   }else{
 38101     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 38102     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 38103     nFreed = sqlite3MallocSize(p);
 38104 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 38105     sqlite3_mutex_enter(pcache1.mutex);
 38106     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
 38107     sqlite3_mutex_leave(pcache1.mutex);
 38108 #endif
 38109     sqlite3_free(p);
 38111   return nFreed;
 38114 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 38115 /*
 38116 ** Return the size of a pcache allocation
 38117 */
 38118 static int pcache1MemSize(void *p){
 38119   if( p>=pcache1.pStart && p<pcache1.pEnd ){
 38120     return pcache1.szSlot;
 38121   }else{
 38122     int iSize;
 38123     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 38124     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 38125     iSize = sqlite3MallocSize(p);
 38126     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 38127     return iSize;
 38130 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 38132 /*
 38133 ** Allocate a new page object initially associated with cache pCache.
 38134 */
 38135 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
 38136   PgHdr1 *p = 0;
 38137   void *pPg;
 38139   /* The group mutex must be released before pcache1Alloc() is called. This
 38140   ** is because it may call sqlite3_release_memory(), which assumes that 
 38141   ** this mutex is not held. */
 38142   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 38143   pcache1LeaveMutex(pCache->pGroup);
 38144 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 38145   pPg = pcache1Alloc(pCache->szPage);
 38146   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
 38147   if( !pPg || !p ){
 38148     pcache1Free(pPg);
 38149     sqlite3_free(p);
 38150     pPg = 0;
 38152 #else
 38153   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
 38154   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
 38155 #endif
 38156   pcache1EnterMutex(pCache->pGroup);
 38158   if( pPg ){
 38159     p->page.pBuf = pPg;
 38160     p->page.pExtra = &p[1];
 38161     if( pCache->bPurgeable ){
 38162       pCache->pGroup->nCurrentPage++;
 38164     return p;
 38166   return 0;
 38169 /*
 38170 ** Free a page object allocated by pcache1AllocPage().
 38171 **
 38172 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
 38173 ** that the current implementation happens to never call this routine
 38174 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
 38175 */
 38176 static void pcache1FreePage(PgHdr1 *p){
 38177   if( ALWAYS(p) ){
 38178     PCache1 *pCache = p->pCache;
 38179     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
 38180     pcache1Free(p->page.pBuf);
 38181 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 38182     sqlite3_free(p);
 38183 #endif
 38184     if( pCache->bPurgeable ){
 38185       pCache->pGroup->nCurrentPage--;
 38190 /*
 38191 ** Malloc function used by SQLite to obtain space from the buffer configured
 38192 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
 38193 ** exists, this function falls back to sqlite3Malloc().
 38194 */
 38195 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
 38196   return pcache1Alloc(sz);
 38199 /*
 38200 ** Free an allocated buffer obtained from sqlite3PageMalloc().
 38201 */
 38202 SQLITE_PRIVATE void sqlite3PageFree(void *p){
 38203   pcache1Free(p);
 38207 /*
 38208 ** Return true if it desirable to avoid allocating a new page cache
 38209 ** entry.
 38210 **
 38211 ** If memory was allocated specifically to the page cache using
 38212 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
 38213 ** it is desirable to avoid allocating a new page cache entry because
 38214 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
 38215 ** for all page cache needs and we should not need to spill the
 38216 ** allocation onto the heap.
 38217 **
 38218 ** Or, the heap is used for all page cache memory but the heap is
 38219 ** under memory pressure, then again it is desirable to avoid
 38220 ** allocating a new page cache entry in order to avoid stressing
 38221 ** the heap even further.
 38222 */
 38223 static int pcache1UnderMemoryPressure(PCache1 *pCache){
 38224   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
 38225     return pcache1.bUnderPressure;
 38226   }else{
 38227     return sqlite3HeapNearlyFull();
 38231 /******************************************************************************/
 38232 /******** General Implementation Functions ************************************/
 38234 /*
 38235 ** This function is used to resize the hash table used by the cache passed
 38236 ** as the first argument.
 38237 **
 38238 ** The PCache mutex must be held when this function is called.
 38239 */
 38240 static int pcache1ResizeHash(PCache1 *p){
 38241   PgHdr1 **apNew;
 38242   unsigned int nNew;
 38243   unsigned int i;
 38245   assert( sqlite3_mutex_held(p->pGroup->mutex) );
 38247   nNew = p->nHash*2;
 38248   if( nNew<256 ){
 38249     nNew = 256;
 38252   pcache1LeaveMutex(p->pGroup);
 38253   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
 38254   apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
 38255   if( p->nHash ){ sqlite3EndBenignMalloc(); }
 38256   pcache1EnterMutex(p->pGroup);
 38257   if( apNew ){
 38258     for(i=0; i<p->nHash; i++){
 38259       PgHdr1 *pPage;
 38260       PgHdr1 *pNext = p->apHash[i];
 38261       while( (pPage = pNext)!=0 ){
 38262         unsigned int h = pPage->iKey % nNew;
 38263         pNext = pPage->pNext;
 38264         pPage->pNext = apNew[h];
 38265         apNew[h] = pPage;
 38268     sqlite3_free(p->apHash);
 38269     p->apHash = apNew;
 38270     p->nHash = nNew;
 38273   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
 38276 /*
 38277 ** This function is used internally to remove the page pPage from the 
 38278 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
 38279 ** LRU list, then this function is a no-op.
 38280 **
 38281 ** The PGroup mutex must be held when this function is called.
 38282 */
 38283 static void pcache1PinPage(PgHdr1 *pPage){
 38284   PCache1 *pCache;
 38285   PGroup *pGroup;
 38287   assert( pPage!=0 );
 38288   assert( pPage->isPinned==0 );
 38289   pCache = pPage->pCache;
 38290   pGroup = pCache->pGroup;
 38291   assert( pPage->pLruNext || pPage==pGroup->pLruTail );
 38292   assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
 38293   assert( sqlite3_mutex_held(pGroup->mutex) );
 38294   if( pPage->pLruPrev ){
 38295     pPage->pLruPrev->pLruNext = pPage->pLruNext;
 38296   }else{
 38297     pGroup->pLruHead = pPage->pLruNext;
 38299   if( pPage->pLruNext ){
 38300     pPage->pLruNext->pLruPrev = pPage->pLruPrev;
 38301   }else{
 38302     pGroup->pLruTail = pPage->pLruPrev;
 38304   pPage->pLruNext = 0;
 38305   pPage->pLruPrev = 0;
 38306   pPage->isPinned = 1;
 38307   pCache->nRecyclable--;
 38311 /*
 38312 ** Remove the page supplied as an argument from the hash table 
 38313 ** (PCache1.apHash structure) that it is currently stored in.
 38314 **
 38315 ** The PGroup mutex must be held when this function is called.
 38316 */
 38317 static void pcache1RemoveFromHash(PgHdr1 *pPage){
 38318   unsigned int h;
 38319   PCache1 *pCache = pPage->pCache;
 38320   PgHdr1 **pp;
 38322   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 38323   h = pPage->iKey % pCache->nHash;
 38324   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
 38325   *pp = (*pp)->pNext;
 38327   pCache->nPage--;
 38330 /*
 38331 ** If there are currently more than nMaxPage pages allocated, try
 38332 ** to recycle pages to reduce the number allocated to nMaxPage.
 38333 */
 38334 static void pcache1EnforceMaxPage(PGroup *pGroup){
 38335   assert( sqlite3_mutex_held(pGroup->mutex) );
 38336   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
 38337     PgHdr1 *p = pGroup->pLruTail;
 38338     assert( p->pCache->pGroup==pGroup );
 38339     assert( p->isPinned==0 );
 38340     pcache1PinPage(p);
 38341     pcache1RemoveFromHash(p);
 38342     pcache1FreePage(p);
 38346 /*
 38347 ** Discard all pages from cache pCache with a page number (key value) 
 38348 ** greater than or equal to iLimit. Any pinned pages that meet this 
 38349 ** criteria are unpinned before they are discarded.
 38350 **
 38351 ** The PCache mutex must be held when this function is called.
 38352 */
 38353 static void pcache1TruncateUnsafe(
 38354   PCache1 *pCache,             /* The cache to truncate */
 38355   unsigned int iLimit          /* Drop pages with this pgno or larger */
 38356 ){
 38357   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
 38358   unsigned int h;
 38359   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
 38360   for(h=0; h<pCache->nHash; h++){
 38361     PgHdr1 **pp = &pCache->apHash[h]; 
 38362     PgHdr1 *pPage;
 38363     while( (pPage = *pp)!=0 ){
 38364       if( pPage->iKey>=iLimit ){
 38365         pCache->nPage--;
 38366         *pp = pPage->pNext;
 38367         if( !pPage->isPinned ) pcache1PinPage(pPage);
 38368         pcache1FreePage(pPage);
 38369       }else{
 38370         pp = &pPage->pNext;
 38371         TESTONLY( nPage++; )
 38375   assert( pCache->nPage==nPage );
 38378 /******************************************************************************/
 38379 /******** sqlite3_pcache Methods **********************************************/
 38381 /*
 38382 ** Implementation of the sqlite3_pcache.xInit method.
 38383 */
 38384 static int pcache1Init(void *NotUsed){
 38385   UNUSED_PARAMETER(NotUsed);
 38386   assert( pcache1.isInit==0 );
 38387   memset(&pcache1, 0, sizeof(pcache1));
 38388   if( sqlite3GlobalConfig.bCoreMutex ){
 38389     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
 38390     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
 38392   pcache1.grp.mxPinned = 10;
 38393   pcache1.isInit = 1;
 38394   return SQLITE_OK;
 38397 /*
 38398 ** Implementation of the sqlite3_pcache.xShutdown method.
 38399 ** Note that the static mutex allocated in xInit does 
 38400 ** not need to be freed.
 38401 */
 38402 static void pcache1Shutdown(void *NotUsed){
 38403   UNUSED_PARAMETER(NotUsed);
 38404   assert( pcache1.isInit!=0 );
 38405   memset(&pcache1, 0, sizeof(pcache1));
 38408 /*
 38409 ** Implementation of the sqlite3_pcache.xCreate method.
 38410 **
 38411 ** Allocate a new cache.
 38412 */
 38413 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
 38414   PCache1 *pCache;      /* The newly created page cache */
 38415   PGroup *pGroup;       /* The group the new page cache will belong to */
 38416   int sz;               /* Bytes of memory required to allocate the new cache */
 38418   /*
 38419   ** The separateCache variable is true if each PCache has its own private
 38420   ** PGroup.  In other words, separateCache is true for mode (1) where no
 38421   ** mutexing is required.
 38422   **
 38423   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
 38424   **
 38425   **   *  Always use a unified cache in single-threaded applications
 38426   **
 38427   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
 38428   **      use separate caches (mode-1)
 38429   */
 38430 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
 38431   const int separateCache = 0;
 38432 #else
 38433   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
 38434 #endif
 38436   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
 38437   assert( szExtra < 300 );
 38439   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
 38440   pCache = (PCache1 *)sqlite3MallocZero(sz);
 38441   if( pCache ){
 38442     if( separateCache ){
 38443       pGroup = (PGroup*)&pCache[1];
 38444       pGroup->mxPinned = 10;
 38445     }else{
 38446       pGroup = &pcache1.grp;
 38448     pCache->pGroup = pGroup;
 38449     pCache->szPage = szPage;
 38450     pCache->szExtra = szExtra;
 38451     pCache->bPurgeable = (bPurgeable ? 1 : 0);
 38452     if( bPurgeable ){
 38453       pCache->nMin = 10;
 38454       pcache1EnterMutex(pGroup);
 38455       pGroup->nMinPage += pCache->nMin;
 38456       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 38457       pcache1LeaveMutex(pGroup);
 38460   return (sqlite3_pcache *)pCache;
 38463 /*
 38464 ** Implementation of the sqlite3_pcache.xCachesize method. 
 38465 **
 38466 ** Configure the cache_size limit for a cache.
 38467 */
 38468 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
 38469   PCache1 *pCache = (PCache1 *)p;
 38470   if( pCache->bPurgeable ){
 38471     PGroup *pGroup = pCache->pGroup;
 38472     pcache1EnterMutex(pGroup);
 38473     pGroup->nMaxPage += (nMax - pCache->nMax);
 38474     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 38475     pCache->nMax = nMax;
 38476     pCache->n90pct = pCache->nMax*9/10;
 38477     pcache1EnforceMaxPage(pGroup);
 38478     pcache1LeaveMutex(pGroup);
 38482 /*
 38483 ** Implementation of the sqlite3_pcache.xShrink method. 
 38484 **
 38485 ** Free up as much memory as possible.
 38486 */
 38487 static void pcache1Shrink(sqlite3_pcache *p){
 38488   PCache1 *pCache = (PCache1*)p;
 38489   if( pCache->bPurgeable ){
 38490     PGroup *pGroup = pCache->pGroup;
 38491     int savedMaxPage;
 38492     pcache1EnterMutex(pGroup);
 38493     savedMaxPage = pGroup->nMaxPage;
 38494     pGroup->nMaxPage = 0;
 38495     pcache1EnforceMaxPage(pGroup);
 38496     pGroup->nMaxPage = savedMaxPage;
 38497     pcache1LeaveMutex(pGroup);
 38501 /*
 38502 ** Implementation of the sqlite3_pcache.xPagecount method. 
 38503 */
 38504 static int pcache1Pagecount(sqlite3_pcache *p){
 38505   int n;
 38506   PCache1 *pCache = (PCache1*)p;
 38507   pcache1EnterMutex(pCache->pGroup);
 38508   n = pCache->nPage;
 38509   pcache1LeaveMutex(pCache->pGroup);
 38510   return n;
 38513 /*
 38514 ** Implementation of the sqlite3_pcache.xFetch method. 
 38515 **
 38516 ** Fetch a page by key value.
 38517 **
 38518 ** Whether or not a new page may be allocated by this function depends on
 38519 ** the value of the createFlag argument.  0 means do not allocate a new
 38520 ** page.  1 means allocate a new page if space is easily available.  2 
 38521 ** means to try really hard to allocate a new page.
 38522 **
 38523 ** For a non-purgeable cache (a cache used as the storage for an in-memory
 38524 ** database) there is really no difference between createFlag 1 and 2.  So
 38525 ** the calling function (pcache.c) will never have a createFlag of 1 on
 38526 ** a non-purgeable cache.
 38527 **
 38528 ** There are three different approaches to obtaining space for a page,
 38529 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
 38530 **
 38531 **   1. Regardless of the value of createFlag, the cache is searched for a 
 38532 **      copy of the requested page. If one is found, it is returned.
 38533 **
 38534 **   2. If createFlag==0 and the page is not already in the cache, NULL is
 38535 **      returned.
 38536 **
 38537 **   3. If createFlag is 1, and the page is not already in the cache, then
 38538 **      return NULL (do not allocate a new page) if any of the following
 38539 **      conditions are true:
 38540 **
 38541 **       (a) the number of pages pinned by the cache is greater than
 38542 **           PCache1.nMax, or
 38543 **
 38544 **       (b) the number of pages pinned by the cache is greater than
 38545 **           the sum of nMax for all purgeable caches, less the sum of 
 38546 **           nMin for all other purgeable caches, or
 38547 **
 38548 **   4. If none of the first three conditions apply and the cache is marked
 38549 **      as purgeable, and if one of the following is true:
 38550 **
 38551 **       (a) The number of pages allocated for the cache is already 
 38552 **           PCache1.nMax, or
 38553 **
 38554 **       (b) The number of pages allocated for all purgeable caches is
 38555 **           already equal to or greater than the sum of nMax for all
 38556 **           purgeable caches,
 38557 **
 38558 **       (c) The system is under memory pressure and wants to avoid
 38559 **           unnecessary pages cache entry allocations
 38560 **
 38561 **      then attempt to recycle a page from the LRU list. If it is the right
 38562 **      size, return the recycled buffer. Otherwise, free the buffer and
 38563 **      proceed to step 5. 
 38564 **
 38565 **   5. Otherwise, allocate and return a new page buffer.
 38566 */
 38567 static sqlite3_pcache_page *pcache1Fetch(
 38568   sqlite3_pcache *p, 
 38569   unsigned int iKey, 
 38570   int createFlag
 38571 ){
 38572   unsigned int nPinned;
 38573   PCache1 *pCache = (PCache1 *)p;
 38574   PGroup *pGroup;
 38575   PgHdr1 *pPage = 0;
 38577   assert( offsetof(PgHdr1,page)==0 );
 38578   assert( pCache->bPurgeable || createFlag!=1 );
 38579   assert( pCache->bPurgeable || pCache->nMin==0 );
 38580   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
 38581   assert( pCache->nMin==0 || pCache->bPurgeable );
 38582   pcache1EnterMutex(pGroup = pCache->pGroup);
 38584   /* Step 1: Search the hash table for an existing entry. */
 38585   if( pCache->nHash>0 ){
 38586     unsigned int h = iKey % pCache->nHash;
 38587     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
 38590   /* Step 2: Abort if no existing page is found and createFlag is 0 */
 38591   if( pPage ){
 38592     if( !pPage->isPinned ) pcache1PinPage(pPage);
 38593     goto fetch_out;
 38595   if( createFlag==0 ){
 38596     goto fetch_out;
 38599   /* The pGroup local variable will normally be initialized by the
 38600   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
 38601   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
 38602   ** local variable here.  Delaying the initialization of pGroup is an
 38603   ** optimization:  The common case is to exit the module before reaching
 38604   ** this point.
 38605   */
 38606 #ifdef SQLITE_MUTEX_OMIT
 38607   pGroup = pCache->pGroup;
 38608 #endif
 38610   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
 38611   assert( pCache->nPage >= pCache->nRecyclable );
 38612   nPinned = pCache->nPage - pCache->nRecyclable;
 38613   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
 38614   assert( pCache->n90pct == pCache->nMax*9/10 );
 38615   if( createFlag==1 && (
 38616         nPinned>=pGroup->mxPinned
 38617      || nPinned>=pCache->n90pct
 38618      || pcache1UnderMemoryPressure(pCache)
 38619   )){
 38620     goto fetch_out;
 38623   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
 38624     goto fetch_out;
 38626   assert( pCache->nHash>0 && pCache->apHash );
 38628   /* Step 4. Try to recycle a page. */
 38629   if( pCache->bPurgeable && pGroup->pLruTail && (
 38630          (pCache->nPage+1>=pCache->nMax)
 38631       || pGroup->nCurrentPage>=pGroup->nMaxPage
 38632       || pcache1UnderMemoryPressure(pCache)
 38633   )){
 38634     PCache1 *pOther;
 38635     pPage = pGroup->pLruTail;
 38636     assert( pPage->isPinned==0 );
 38637     pcache1RemoveFromHash(pPage);
 38638     pcache1PinPage(pPage);
 38639     pOther = pPage->pCache;
 38641     /* We want to verify that szPage and szExtra are the same for pOther
 38642     ** and pCache.  Assert that we can verify this by comparing sums. */
 38643     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
 38644     assert( pCache->szExtra<512 );
 38645     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
 38646     assert( pOther->szExtra<512 );
 38648     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
 38649       pcache1FreePage(pPage);
 38650       pPage = 0;
 38651     }else{
 38652       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
 38656   /* Step 5. If a usable page buffer has still not been found, 
 38657   ** attempt to allocate a new one. 
 38658   */
 38659   if( !pPage ){
 38660     if( createFlag==1 ) sqlite3BeginBenignMalloc();
 38661     pPage = pcache1AllocPage(pCache);
 38662     if( createFlag==1 ) sqlite3EndBenignMalloc();
 38665   if( pPage ){
 38666     unsigned int h = iKey % pCache->nHash;
 38667     pCache->nPage++;
 38668     pPage->iKey = iKey;
 38669     pPage->pNext = pCache->apHash[h];
 38670     pPage->pCache = pCache;
 38671     pPage->pLruPrev = 0;
 38672     pPage->pLruNext = 0;
 38673     pPage->isPinned = 1;
 38674     *(void **)pPage->page.pExtra = 0;
 38675     pCache->apHash[h] = pPage;
 38678 fetch_out:
 38679   if( pPage && iKey>pCache->iMaxKey ){
 38680     pCache->iMaxKey = iKey;
 38682   pcache1LeaveMutex(pGroup);
 38683   return (sqlite3_pcache_page*)pPage;
 38687 /*
 38688 ** Implementation of the sqlite3_pcache.xUnpin method.
 38689 **
 38690 ** Mark a page as unpinned (eligible for asynchronous recycling).
 38691 */
 38692 static void pcache1Unpin(
 38693   sqlite3_pcache *p, 
 38694   sqlite3_pcache_page *pPg, 
 38695   int reuseUnlikely
 38696 ){
 38697   PCache1 *pCache = (PCache1 *)p;
 38698   PgHdr1 *pPage = (PgHdr1 *)pPg;
 38699   PGroup *pGroup = pCache->pGroup;
 38701   assert( pPage->pCache==pCache );
 38702   pcache1EnterMutex(pGroup);
 38704   /* It is an error to call this function if the page is already 
 38705   ** part of the PGroup LRU list.
 38706   */
 38707   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
 38708   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
 38709   assert( pPage->isPinned==1 );
 38711   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
 38712     pcache1RemoveFromHash(pPage);
 38713     pcache1FreePage(pPage);
 38714   }else{
 38715     /* Add the page to the PGroup LRU list. */
 38716     if( pGroup->pLruHead ){
 38717       pGroup->pLruHead->pLruPrev = pPage;
 38718       pPage->pLruNext = pGroup->pLruHead;
 38719       pGroup->pLruHead = pPage;
 38720     }else{
 38721       pGroup->pLruTail = pPage;
 38722       pGroup->pLruHead = pPage;
 38724     pCache->nRecyclable++;
 38725     pPage->isPinned = 0;
 38728   pcache1LeaveMutex(pCache->pGroup);
 38731 /*
 38732 ** Implementation of the sqlite3_pcache.xRekey method. 
 38733 */
 38734 static void pcache1Rekey(
 38735   sqlite3_pcache *p,
 38736   sqlite3_pcache_page *pPg,
 38737   unsigned int iOld,
 38738   unsigned int iNew
 38739 ){
 38740   PCache1 *pCache = (PCache1 *)p;
 38741   PgHdr1 *pPage = (PgHdr1 *)pPg;
 38742   PgHdr1 **pp;
 38743   unsigned int h; 
 38744   assert( pPage->iKey==iOld );
 38745   assert( pPage->pCache==pCache );
 38747   pcache1EnterMutex(pCache->pGroup);
 38749   h = iOld%pCache->nHash;
 38750   pp = &pCache->apHash[h];
 38751   while( (*pp)!=pPage ){
 38752     pp = &(*pp)->pNext;
 38754   *pp = pPage->pNext;
 38756   h = iNew%pCache->nHash;
 38757   pPage->iKey = iNew;
 38758   pPage->pNext = pCache->apHash[h];
 38759   pCache->apHash[h] = pPage;
 38760   if( iNew>pCache->iMaxKey ){
 38761     pCache->iMaxKey = iNew;
 38764   pcache1LeaveMutex(pCache->pGroup);
 38767 /*
 38768 ** Implementation of the sqlite3_pcache.xTruncate method. 
 38769 **
 38770 ** Discard all unpinned pages in the cache with a page number equal to
 38771 ** or greater than parameter iLimit. Any pinned pages with a page number
 38772 ** equal to or greater than iLimit are implicitly unpinned.
 38773 */
 38774 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
 38775   PCache1 *pCache = (PCache1 *)p;
 38776   pcache1EnterMutex(pCache->pGroup);
 38777   if( iLimit<=pCache->iMaxKey ){
 38778     pcache1TruncateUnsafe(pCache, iLimit);
 38779     pCache->iMaxKey = iLimit-1;
 38781   pcache1LeaveMutex(pCache->pGroup);
 38784 /*
 38785 ** Implementation of the sqlite3_pcache.xDestroy method. 
 38786 **
 38787 ** Destroy a cache allocated using pcache1Create().
 38788 */
 38789 static void pcache1Destroy(sqlite3_pcache *p){
 38790   PCache1 *pCache = (PCache1 *)p;
 38791   PGroup *pGroup = pCache->pGroup;
 38792   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
 38793   pcache1EnterMutex(pGroup);
 38794   pcache1TruncateUnsafe(pCache, 0);
 38795   assert( pGroup->nMaxPage >= pCache->nMax );
 38796   pGroup->nMaxPage -= pCache->nMax;
 38797   assert( pGroup->nMinPage >= pCache->nMin );
 38798   pGroup->nMinPage -= pCache->nMin;
 38799   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
 38800   pcache1EnforceMaxPage(pGroup);
 38801   pcache1LeaveMutex(pGroup);
 38802   sqlite3_free(pCache->apHash);
 38803   sqlite3_free(pCache);
 38806 /*
 38807 ** This function is called during initialization (sqlite3_initialize()) to
 38808 ** install the default pluggable cache module, assuming the user has not
 38809 ** already provided an alternative.
 38810 */
 38811 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
 38812   static const sqlite3_pcache_methods2 defaultMethods = {
 38813     1,                       /* iVersion */
 38814     0,                       /* pArg */
 38815     pcache1Init,             /* xInit */
 38816     pcache1Shutdown,         /* xShutdown */
 38817     pcache1Create,           /* xCreate */
 38818     pcache1Cachesize,        /* xCachesize */
 38819     pcache1Pagecount,        /* xPagecount */
 38820     pcache1Fetch,            /* xFetch */
 38821     pcache1Unpin,            /* xUnpin */
 38822     pcache1Rekey,            /* xRekey */
 38823     pcache1Truncate,         /* xTruncate */
 38824     pcache1Destroy,          /* xDestroy */
 38825     pcache1Shrink            /* xShrink */
 38826   };
 38827   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
 38830 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 38831 /*
 38832 ** This function is called to free superfluous dynamically allocated memory
 38833 ** held by the pager system. Memory in use by any SQLite pager allocated
 38834 ** by the current thread may be sqlite3_free()ed.
 38835 **
 38836 ** nReq is the number of bytes of memory required. Once this much has
 38837 ** been released, the function returns. The return value is the total number 
 38838 ** of bytes of memory released.
 38839 */
 38840 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
 38841   int nFree = 0;
 38842   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
 38843   assert( sqlite3_mutex_notheld(pcache1.mutex) );
 38844   if( pcache1.pStart==0 ){
 38845     PgHdr1 *p;
 38846     pcache1EnterMutex(&pcache1.grp);
 38847     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
 38848       nFree += pcache1MemSize(p->page.pBuf);
 38849 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
 38850       nFree += sqlite3MemSize(p);
 38851 #endif
 38852       assert( p->isPinned==0 );
 38853       pcache1PinPage(p);
 38854       pcache1RemoveFromHash(p);
 38855       pcache1FreePage(p);
 38857     pcache1LeaveMutex(&pcache1.grp);
 38859   return nFree;
 38861 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 38863 #ifdef SQLITE_TEST
 38864 /*
 38865 ** This function is used by test procedures to inspect the internal state
 38866 ** of the global cache.
 38867 */
 38868 SQLITE_PRIVATE void sqlite3PcacheStats(
 38869   int *pnCurrent,      /* OUT: Total number of pages cached */
 38870   int *pnMax,          /* OUT: Global maximum cache size */
 38871   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
 38872   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
 38873 ){
 38874   PgHdr1 *p;
 38875   int nRecyclable = 0;
 38876   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
 38877     assert( p->isPinned==0 );
 38878     nRecyclable++;
 38880   *pnCurrent = pcache1.grp.nCurrentPage;
 38881   *pnMax = (int)pcache1.grp.nMaxPage;
 38882   *pnMin = (int)pcache1.grp.nMinPage;
 38883   *pnRecyclable = nRecyclable;
 38885 #endif
 38887 /************** End of pcache1.c *********************************************/
 38888 /************** Begin file rowset.c ******************************************/
 38889 /*
 38890 ** 2008 December 3
 38891 **
 38892 ** The author disclaims copyright to this source code.  In place of
 38893 ** a legal notice, here is a blessing:
 38894 **
 38895 **    May you do good and not evil.
 38896 **    May you find forgiveness for yourself and forgive others.
 38897 **    May you share freely, never taking more than you give.
 38898 **
 38899 *************************************************************************
 38900 **
 38901 ** This module implements an object we call a "RowSet".
 38902 **
 38903 ** The RowSet object is a collection of rowids.  Rowids
 38904 ** are inserted into the RowSet in an arbitrary order.  Inserts
 38905 ** can be intermixed with tests to see if a given rowid has been
 38906 ** previously inserted into the RowSet.
 38907 **
 38908 ** After all inserts are finished, it is possible to extract the
 38909 ** elements of the RowSet in sorted order.  Once this extraction
 38910 ** process has started, no new elements may be inserted.
 38911 **
 38912 ** Hence, the primitive operations for a RowSet are:
 38913 **
 38914 **    CREATE
 38915 **    INSERT
 38916 **    TEST
 38917 **    SMALLEST
 38918 **    DESTROY
 38919 **
 38920 ** The CREATE and DESTROY primitives are the constructor and destructor,
 38921 ** obviously.  The INSERT primitive adds a new element to the RowSet.
 38922 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
 38923 ** extracts the least value from the RowSet.
 38924 **
 38925 ** The INSERT primitive might allocate additional memory.  Memory is
 38926 ** allocated in chunks so most INSERTs do no allocation.  There is an 
 38927 ** upper bound on the size of allocated memory.  No memory is freed
 38928 ** until DESTROY.
 38929 **
 38930 ** The TEST primitive includes a "batch" number.  The TEST primitive
 38931 ** will only see elements that were inserted before the last change
 38932 ** in the batch number.  In other words, if an INSERT occurs between
 38933 ** two TESTs where the TESTs have the same batch nubmer, then the
 38934 ** value added by the INSERT will not be visible to the second TEST.
 38935 ** The initial batch number is zero, so if the very first TEST contains
 38936 ** a non-zero batch number, it will see all prior INSERTs.
 38937 **
 38938 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
 38939 ** that is attempted.
 38940 **
 38941 ** The cost of an INSERT is roughly constant.  (Sometime new memory
 38942 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
 38943 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
 38944 ** The cost of a TEST using the same batch number is O(logN).  The cost
 38945 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
 38946 ** primitives are constant time.  The cost of DESTROY is O(N).
 38947 **
 38948 ** There is an added cost of O(N) when switching between TEST and
 38949 ** SMALLEST primitives.
 38950 */
 38953 /*
 38954 ** Target size for allocation chunks.
 38955 */
 38956 #define ROWSET_ALLOCATION_SIZE 1024
 38958 /*
 38959 ** The number of rowset entries per allocation chunk.
 38960 */
 38961 #define ROWSET_ENTRY_PER_CHUNK  \
 38962                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
 38964 /*
 38965 ** Each entry in a RowSet is an instance of the following object.
 38966 **
 38967 ** This same object is reused to store a linked list of trees of RowSetEntry
 38968 ** objects.  In that alternative use, pRight points to the next entry
 38969 ** in the list, pLeft points to the tree, and v is unused.  The
 38970 ** RowSet.pForest value points to the head of this forest list.
 38971 */
 38972 struct RowSetEntry {            
 38973   i64 v;                        /* ROWID value for this entry */
 38974   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
 38975   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
 38976 };
 38978 /*
 38979 ** RowSetEntry objects are allocated in large chunks (instances of the
 38980 ** following structure) to reduce memory allocation overhead.  The
 38981 ** chunks are kept on a linked list so that they can be deallocated
 38982 ** when the RowSet is destroyed.
 38983 */
 38984 struct RowSetChunk {
 38985   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
 38986   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
 38987 };
 38989 /*
 38990 ** A RowSet in an instance of the following structure.
 38991 **
 38992 ** A typedef of this structure if found in sqliteInt.h.
 38993 */
 38994 struct RowSet {
 38995   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
 38996   sqlite3 *db;                   /* The database connection */
 38997   struct RowSetEntry *pEntry;    /* List of entries using pRight */
 38998   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
 38999   struct RowSetEntry *pFresh;    /* Source of new entry objects */
 39000   struct RowSetEntry *pForest;   /* List of binary trees of entries */
 39001   u16 nFresh;                    /* Number of objects on pFresh */
 39002   u8 rsFlags;                    /* Various flags */
 39003   u8 iBatch;                     /* Current insert batch */
 39004 };
 39006 /*
 39007 ** Allowed values for RowSet.rsFlags
 39008 */
 39009 #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
 39010 #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
 39012 /*
 39013 ** Turn bulk memory into a RowSet object.  N bytes of memory
 39014 ** are available at pSpace.  The db pointer is used as a memory context
 39015 ** for any subsequent allocations that need to occur.
 39016 ** Return a pointer to the new RowSet object.
 39017 **
 39018 ** It must be the case that N is sufficient to make a Rowset.  If not
 39019 ** an assertion fault occurs.
 39020 ** 
 39021 ** If N is larger than the minimum, use the surplus as an initial
 39022 ** allocation of entries available to be filled.
 39023 */
 39024 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
 39025   RowSet *p;
 39026   assert( N >= ROUND8(sizeof(*p)) );
 39027   p = pSpace;
 39028   p->pChunk = 0;
 39029   p->db = db;
 39030   p->pEntry = 0;
 39031   p->pLast = 0;
 39032   p->pForest = 0;
 39033   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
 39034   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
 39035   p->rsFlags = ROWSET_SORTED;
 39036   p->iBatch = 0;
 39037   return p;
 39040 /*
 39041 ** Deallocate all chunks from a RowSet.  This frees all memory that
 39042 ** the RowSet has allocated over its lifetime.  This routine is
 39043 ** the destructor for the RowSet.
 39044 */
 39045 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
 39046   struct RowSetChunk *pChunk, *pNextChunk;
 39047   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
 39048     pNextChunk = pChunk->pNextChunk;
 39049     sqlite3DbFree(p->db, pChunk);
 39051   p->pChunk = 0;
 39052   p->nFresh = 0;
 39053   p->pEntry = 0;
 39054   p->pLast = 0;
 39055   p->pForest = 0;
 39056   p->rsFlags = ROWSET_SORTED;
 39059 /*
 39060 ** Allocate a new RowSetEntry object that is associated with the
 39061 ** given RowSet.  Return a pointer to the new and completely uninitialized
 39062 ** objected.
 39063 **
 39064 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
 39065 ** routine returns NULL.
 39066 */
 39067 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
 39068   assert( p!=0 );
 39069   if( p->nFresh==0 ){
 39070     struct RowSetChunk *pNew;
 39071     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
 39072     if( pNew==0 ){
 39073       return 0;
 39075     pNew->pNextChunk = p->pChunk;
 39076     p->pChunk = pNew;
 39077     p->pFresh = pNew->aEntry;
 39078     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
 39080   p->nFresh--;
 39081   return p->pFresh++;
 39084 /*
 39085 ** Insert a new value into a RowSet.
 39086 **
 39087 ** The mallocFailed flag of the database connection is set if a
 39088 ** memory allocation fails.
 39089 */
 39090 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
 39091   struct RowSetEntry *pEntry;  /* The new entry */
 39092   struct RowSetEntry *pLast;   /* The last prior entry */
 39094   /* This routine is never called after sqlite3RowSetNext() */
 39095   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 39097   pEntry = rowSetEntryAlloc(p);
 39098   if( pEntry==0 ) return;
 39099   pEntry->v = rowid;
 39100   pEntry->pRight = 0;
 39101   pLast = p->pLast;
 39102   if( pLast ){
 39103     if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
 39104       p->rsFlags &= ~ROWSET_SORTED;
 39106     pLast->pRight = pEntry;
 39107   }else{
 39108     p->pEntry = pEntry;
 39110   p->pLast = pEntry;
 39113 /*
 39114 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
 39115 **
 39116 ** The input lists are connected via pRight pointers and are 
 39117 ** assumed to each already be in sorted order.
 39118 */
 39119 static struct RowSetEntry *rowSetEntryMerge(
 39120   struct RowSetEntry *pA,    /* First sorted list to be merged */
 39121   struct RowSetEntry *pB     /* Second sorted list to be merged */
 39122 ){
 39123   struct RowSetEntry head;
 39124   struct RowSetEntry *pTail;
 39126   pTail = &head;
 39127   while( pA && pB ){
 39128     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 39129     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
 39130     if( pA->v<pB->v ){
 39131       pTail->pRight = pA;
 39132       pA = pA->pRight;
 39133       pTail = pTail->pRight;
 39134     }else if( pB->v<pA->v ){
 39135       pTail->pRight = pB;
 39136       pB = pB->pRight;
 39137       pTail = pTail->pRight;
 39138     }else{
 39139       pA = pA->pRight;
 39142   if( pA ){
 39143     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
 39144     pTail->pRight = pA;
 39145   }else{
 39146     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
 39147     pTail->pRight = pB;
 39149   return head.pRight;
 39152 /*
 39153 ** Sort all elements on the list of RowSetEntry objects into order of
 39154 ** increasing v.
 39155 */ 
 39156 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
 39157   unsigned int i;
 39158   struct RowSetEntry *pNext, *aBucket[40];
 39160   memset(aBucket, 0, sizeof(aBucket));
 39161   while( pIn ){
 39162     pNext = pIn->pRight;
 39163     pIn->pRight = 0;
 39164     for(i=0; aBucket[i]; i++){
 39165       pIn = rowSetEntryMerge(aBucket[i], pIn);
 39166       aBucket[i] = 0;
 39168     aBucket[i] = pIn;
 39169     pIn = pNext;
 39171   pIn = 0;
 39172   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
 39173     pIn = rowSetEntryMerge(pIn, aBucket[i]);
 39175   return pIn;
 39179 /*
 39180 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
 39181 ** Convert this tree into a linked list connected by the pRight pointers
 39182 ** and return pointers to the first and last elements of the new list.
 39183 */
 39184 static void rowSetTreeToList(
 39185   struct RowSetEntry *pIn,         /* Root of the input tree */
 39186   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
 39187   struct RowSetEntry **ppLast      /* Write tail of the output list here */
 39188 ){
 39189   assert( pIn!=0 );
 39190   if( pIn->pLeft ){
 39191     struct RowSetEntry *p;
 39192     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
 39193     p->pRight = pIn;
 39194   }else{
 39195     *ppFirst = pIn;
 39197   if( pIn->pRight ){
 39198     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
 39199   }else{
 39200     *ppLast = pIn;
 39202   assert( (*ppLast)->pRight==0 );
 39206 /*
 39207 ** Convert a sorted list of elements (connected by pRight) into a binary
 39208 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
 39209 ** node taken from the head of *ppList.  A depth of 2 means a tree with
 39210 ** three nodes.  And so forth.
 39211 **
 39212 ** Use as many entries from the input list as required and update the
 39213 ** *ppList to point to the unused elements of the list.  If the input
 39214 ** list contains too few elements, then construct an incomplete tree
 39215 ** and leave *ppList set to NULL.
 39216 **
 39217 ** Return a pointer to the root of the constructed binary tree.
 39218 */
 39219 static struct RowSetEntry *rowSetNDeepTree(
 39220   struct RowSetEntry **ppList,
 39221   int iDepth
 39222 ){
 39223   struct RowSetEntry *p;         /* Root of the new tree */
 39224   struct RowSetEntry *pLeft;     /* Left subtree */
 39225   if( *ppList==0 ){
 39226     return 0;
 39228   if( iDepth==1 ){
 39229     p = *ppList;
 39230     *ppList = p->pRight;
 39231     p->pLeft = p->pRight = 0;
 39232     return p;
 39234   pLeft = rowSetNDeepTree(ppList, iDepth-1);
 39235   p = *ppList;
 39236   if( p==0 ){
 39237     return pLeft;
 39239   p->pLeft = pLeft;
 39240   *ppList = p->pRight;
 39241   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
 39242   return p;
 39245 /*
 39246 ** Convert a sorted list of elements into a binary tree. Make the tree
 39247 ** as deep as it needs to be in order to contain the entire list.
 39248 */
 39249 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
 39250   int iDepth;           /* Depth of the tree so far */
 39251   struct RowSetEntry *p;       /* Current tree root */
 39252   struct RowSetEntry *pLeft;   /* Left subtree */
 39254   assert( pList!=0 );
 39255   p = pList;
 39256   pList = p->pRight;
 39257   p->pLeft = p->pRight = 0;
 39258   for(iDepth=1; pList; iDepth++){
 39259     pLeft = p;
 39260     p = pList;
 39261     pList = p->pRight;
 39262     p->pLeft = pLeft;
 39263     p->pRight = rowSetNDeepTree(&pList, iDepth);
 39265   return p;
 39268 /*
 39269 ** Take all the entries on p->pEntry and on the trees in p->pForest and
 39270 ** sort them all together into one big ordered list on p->pEntry.
 39271 **
 39272 ** This routine should only be called once in the life of a RowSet.
 39273 */
 39274 static void rowSetToList(RowSet *p){
 39276   /* This routine is called only once */
 39277   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
 39279   if( (p->rsFlags & ROWSET_SORTED)==0 ){
 39280     p->pEntry = rowSetEntrySort(p->pEntry);
 39283   /* While this module could theoretically support it, sqlite3RowSetNext()
 39284   ** is never called after sqlite3RowSetText() for the same RowSet.  So
 39285   ** there is never a forest to deal with.  Should this change, simply
 39286   ** remove the assert() and the #if 0. */
 39287   assert( p->pForest==0 );
 39288 #if 0
 39289   while( p->pForest ){
 39290     struct RowSetEntry *pTree = p->pForest->pLeft;
 39291     if( pTree ){
 39292       struct RowSetEntry *pHead, *pTail;
 39293       rowSetTreeToList(pTree, &pHead, &pTail);
 39294       p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
 39296     p->pForest = p->pForest->pRight;
 39298 #endif
 39299   p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
 39302 /*
 39303 ** Extract the smallest element from the RowSet.
 39304 ** Write the element into *pRowid.  Return 1 on success.  Return
 39305 ** 0 if the RowSet is already empty.
 39306 **
 39307 ** After this routine has been called, the sqlite3RowSetInsert()
 39308 ** routine may not be called again.  
 39309 */
 39310 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
 39311   assert( p!=0 );
 39313   /* Merge the forest into a single sorted list on first call */
 39314   if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
 39316   /* Return the next entry on the list */
 39317   if( p->pEntry ){
 39318     *pRowid = p->pEntry->v;
 39319     p->pEntry = p->pEntry->pRight;
 39320     if( p->pEntry==0 ){
 39321       sqlite3RowSetClear(p);
 39323     return 1;
 39324   }else{
 39325     return 0;
 39329 /*
 39330 ** Check to see if element iRowid was inserted into the rowset as
 39331 ** part of any insert batch prior to iBatch.  Return 1 or 0.
 39332 **
 39333 ** If this is the first test of a new batch and if there exist entires
 39334 ** on pRowSet->pEntry, then sort those entires into the forest at
 39335 ** pRowSet->pForest so that they can be tested.
 39336 */
 39337 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
 39338   struct RowSetEntry *p, *pTree;
 39340   /* This routine is never called after sqlite3RowSetNext() */
 39341   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
 39343   /* Sort entries into the forest on the first test of a new batch 
 39344   */
 39345   if( iBatch!=pRowSet->iBatch ){
 39346     p = pRowSet->pEntry;
 39347     if( p ){
 39348       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
 39349       if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
 39350         p = rowSetEntrySort(p);
 39352       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 39353         ppPrevTree = &pTree->pRight;
 39354         if( pTree->pLeft==0 ){
 39355           pTree->pLeft = rowSetListToTree(p);
 39356           break;
 39357         }else{
 39358           struct RowSetEntry *pAux, *pTail;
 39359           rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
 39360           pTree->pLeft = 0;
 39361           p = rowSetEntryMerge(pAux, p);
 39364       if( pTree==0 ){
 39365         *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
 39366         if( pTree ){
 39367           pTree->v = 0;
 39368           pTree->pRight = 0;
 39369           pTree->pLeft = rowSetListToTree(p);
 39372       pRowSet->pEntry = 0;
 39373       pRowSet->pLast = 0;
 39374       pRowSet->rsFlags |= ROWSET_SORTED;
 39376     pRowSet->iBatch = iBatch;
 39379   /* Test to see if the iRowid value appears anywhere in the forest.
 39380   ** Return 1 if it does and 0 if not.
 39381   */
 39382   for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
 39383     p = pTree->pLeft;
 39384     while( p ){
 39385       if( p->v<iRowid ){
 39386         p = p->pRight;
 39387       }else if( p->v>iRowid ){
 39388         p = p->pLeft;
 39389       }else{
 39390         return 1;
 39394   return 0;
 39397 /************** End of rowset.c **********************************************/
 39398 /************** Begin file pager.c *******************************************/
 39399 /*
 39400 ** 2001 September 15
 39401 **
 39402 ** The author disclaims copyright to this source code.  In place of
 39403 ** a legal notice, here is a blessing:
 39404 **
 39405 **    May you do good and not evil.
 39406 **    May you find forgiveness for yourself and forgive others.
 39407 **    May you share freely, never taking more than you give.
 39408 **
 39409 *************************************************************************
 39410 ** This is the implementation of the page cache subsystem or "pager".
 39411 ** 
 39412 ** The pager is used to access a database disk file.  It implements
 39413 ** atomic commit and rollback through the use of a journal file that
 39414 ** is separate from the database file.  The pager also implements file
 39415 ** locking to prevent two processes from writing the same database
 39416 ** file simultaneously, or one process from reading the database while
 39417 ** another is writing.
 39418 */
 39419 #ifndef SQLITE_OMIT_DISKIO
 39420 /************** Include wal.h in the middle of pager.c ***********************/
 39421 /************** Begin file wal.h *********************************************/
 39422 /*
 39423 ** 2010 February 1
 39424 **
 39425 ** The author disclaims copyright to this source code.  In place of
 39426 ** a legal notice, here is a blessing:
 39427 **
 39428 **    May you do good and not evil.
 39429 **    May you find forgiveness for yourself and forgive others.
 39430 **    May you share freely, never taking more than you give.
 39431 **
 39432 *************************************************************************
 39433 ** This header file defines the interface to the write-ahead logging 
 39434 ** system. Refer to the comments below and the header comment attached to 
 39435 ** the implementation of each function in log.c for further details.
 39436 */
 39438 #ifndef _WAL_H_
 39439 #define _WAL_H_
 39442 /* Additional values that can be added to the sync_flags argument of
 39443 ** sqlite3WalFrames():
 39444 */
 39445 #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
 39446 #define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
 39448 #ifdef SQLITE_OMIT_WAL
 39449 # define sqlite3WalOpen(x,y,z)                   0
 39450 # define sqlite3WalLimit(x,y)
 39451 # define sqlite3WalClose(w,x,y,z)                0
 39452 # define sqlite3WalBeginReadTransaction(y,z)     0
 39453 # define sqlite3WalEndReadTransaction(z)
 39454 # define sqlite3WalDbsize(y)                     0
 39455 # define sqlite3WalBeginWriteTransaction(y)      0
 39456 # define sqlite3WalEndWriteTransaction(x)        0
 39457 # define sqlite3WalUndo(x,y,z)                   0
 39458 # define sqlite3WalSavepoint(y,z)
 39459 # define sqlite3WalSavepointUndo(y,z)            0
 39460 # define sqlite3WalFrames(u,v,w,x,y,z)           0
 39461 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
 39462 # define sqlite3WalCallback(z)                   0
 39463 # define sqlite3WalExclusiveMode(y,z)            0
 39464 # define sqlite3WalHeapMemory(z)                 0
 39465 # define sqlite3WalFramesize(z)                  0
 39466 # define sqlite3WalFindFrame(x,y,z)              0
 39467 #else
 39469 #define WAL_SAVEPOINT_NDATA 4
 39471 /* Connection to a write-ahead log (WAL) file. 
 39472 ** There is one object of this type for each pager. 
 39473 */
 39474 typedef struct Wal Wal;
 39476 /* Open and close a connection to a write-ahead log. */
 39477 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
 39478 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
 39480 /* Set the limiting size of a WAL file. */
 39481 SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
 39483 /* Used by readers to open (lock) and close (unlock) a snapshot.  A 
 39484 ** snapshot is like a read-transaction.  It is the state of the database
 39485 ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
 39486 ** preserves the current state even if the other threads or processes
 39487 ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
 39488 ** transaction and releases the lock.
 39489 */
 39490 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
 39491 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
 39493 /* Read a page from the write-ahead log, if it is present. */
 39494 SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
 39495 SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
 39497 /* If the WAL is not empty, return the size of the database. */
 39498 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
 39500 /* Obtain or release the WRITER lock. */
 39501 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
 39502 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
 39504 /* Undo any frames written (but not committed) to the log */
 39505 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
 39507 /* Return an integer that records the current (uncommitted) write
 39508 ** position in the WAL */
 39509 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
 39511 /* Move the write position of the WAL back to iFrame.  Called in
 39512 ** response to a ROLLBACK TO command. */
 39513 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
 39515 /* Write a frame or frames to the log. */
 39516 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
 39518 /* Copy pages from the log to the database file */ 
 39519 SQLITE_PRIVATE int sqlite3WalCheckpoint(
 39520   Wal *pWal,                      /* Write-ahead log connection */
 39521   int eMode,                      /* One of PASSIVE, FULL and RESTART */
 39522   int (*xBusy)(void*),            /* Function to call when busy */
 39523   void *pBusyArg,                 /* Context argument for xBusyHandler */
 39524   int sync_flags,                 /* Flags to sync db file with (or 0) */
 39525   int nBuf,                       /* Size of buffer nBuf */
 39526   u8 *zBuf,                       /* Temporary buffer to use */
 39527   int *pnLog,                     /* OUT: Number of frames in WAL */
 39528   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 39529 );
 39531 /* Return the value to pass to a sqlite3_wal_hook callback, the
 39532 ** number of frames in the WAL at the point of the last commit since
 39533 ** sqlite3WalCallback() was called.  If no commits have occurred since
 39534 ** the last call, then return 0.
 39535 */
 39536 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
 39538 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
 39539 ** by the pager layer on the database file.
 39540 */
 39541 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
 39543 /* Return true if the argument is non-NULL and the WAL module is using
 39544 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 39545 ** WAL module is using shared-memory, return false. 
 39546 */
 39547 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
 39549 #ifdef SQLITE_ENABLE_ZIPVFS
 39550 /* If the WAL file is not empty, return the number of bytes of content
 39551 ** stored in each frame (i.e. the db page-size when the WAL was created).
 39552 */
 39553 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
 39554 #endif
 39556 #endif /* ifndef SQLITE_OMIT_WAL */
 39557 #endif /* _WAL_H_ */
 39559 /************** End of wal.h *************************************************/
 39560 /************** Continuing where we left off in pager.c **********************/
 39563 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
 39564 **
 39565 ** This comment block describes invariants that hold when using a rollback
 39566 ** journal.  These invariants do not apply for journal_mode=WAL,
 39567 ** journal_mode=MEMORY, or journal_mode=OFF.
 39568 **
 39569 ** Within this comment block, a page is deemed to have been synced
 39570 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
 39571 ** Otherwise, the page is not synced until the xSync method of the VFS
 39572 ** is called successfully on the file containing the page.
 39573 **
 39574 ** Definition:  A page of the database file is said to be "overwriteable" if
 39575 ** one or more of the following are true about the page:
 39576 ** 
 39577 **     (a)  The original content of the page as it was at the beginning of
 39578 **          the transaction has been written into the rollback journal and
 39579 **          synced.
 39580 ** 
 39581 **     (b)  The page was a freelist leaf page at the start of the transaction.
 39582 ** 
 39583 **     (c)  The page number is greater than the largest page that existed in
 39584 **          the database file at the start of the transaction.
 39585 ** 
 39586 ** (1) A page of the database file is never overwritten unless one of the
 39587 **     following are true:
 39588 ** 
 39589 **     (a) The page and all other pages on the same sector are overwriteable.
 39590 ** 
 39591 **     (b) The atomic page write optimization is enabled, and the entire
 39592 **         transaction other than the update of the transaction sequence
 39593 **         number consists of a single page change.
 39594 ** 
 39595 ** (2) The content of a page written into the rollback journal exactly matches
 39596 **     both the content in the database when the rollback journal was written
 39597 **     and the content in the database at the beginning of the current
 39598 **     transaction.
 39599 ** 
 39600 ** (3) Writes to the database file are an integer multiple of the page size
 39601 **     in length and are aligned on a page boundary.
 39602 ** 
 39603 ** (4) Reads from the database file are either aligned on a page boundary and
 39604 **     an integer multiple of the page size in length or are taken from the
 39605 **     first 100 bytes of the database file.
 39606 ** 
 39607 ** (5) All writes to the database file are synced prior to the rollback journal
 39608 **     being deleted, truncated, or zeroed.
 39609 ** 
 39610 ** (6) If a master journal file is used, then all writes to the database file
 39611 **     are synced prior to the master journal being deleted.
 39612 ** 
 39613 ** Definition: Two databases (or the same database at two points it time)
 39614 ** are said to be "logically equivalent" if they give the same answer to
 39615 ** all queries.  Note in particular the content of freelist leaf
 39616 ** pages can be changed arbitarily without effecting the logical equivalence
 39617 ** of the database.
 39618 ** 
 39619 ** (7) At any time, if any subset, including the empty set and the total set,
 39620 **     of the unsynced changes to a rollback journal are removed and the 
 39621 **     journal is rolled back, the resulting database file will be logical
 39622 **     equivalent to the database file at the beginning of the transaction.
 39623 ** 
 39624 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
 39625 **     is called to restore the database file to the same size it was at
 39626 **     the beginning of the transaction.  (In some VFSes, the xTruncate
 39627 **     method is a no-op, but that does not change the fact the SQLite will
 39628 **     invoke it.)
 39629 ** 
 39630 ** (9) Whenever the database file is modified, at least one bit in the range
 39631 **     of bytes from 24 through 39 inclusive will be changed prior to releasing
 39632 **     the EXCLUSIVE lock, thus signaling other connections on the same
 39633 **     database to flush their caches.
 39634 **
 39635 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
 39636 **      than one billion transactions.
 39637 **
 39638 ** (11) A database file is well-formed at the beginning and at the conclusion
 39639 **      of every transaction.
 39640 **
 39641 ** (12) An EXCLUSIVE lock is held on the database file when writing to
 39642 **      the database file.
 39643 **
 39644 ** (13) A SHARED lock is held on the database file while reading any
 39645 **      content out of the database file.
 39646 **
 39647 ******************************************************************************/
 39649 /*
 39650 ** Macros for troubleshooting.  Normally turned off
 39651 */
 39652 #if 0
 39653 int sqlite3PagerTrace=1;  /* True to enable tracing */
 39654 #define sqlite3DebugPrintf printf
 39655 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
 39656 #else
 39657 #define PAGERTRACE(X)
 39658 #endif
 39660 /*
 39661 ** The following two macros are used within the PAGERTRACE() macros above
 39662 ** to print out file-descriptors. 
 39663 **
 39664 ** PAGERID() takes a pointer to a Pager struct as its argument. The
 39665 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
 39666 ** struct as its argument.
 39667 */
 39668 #define PAGERID(p) ((int)(p->fd))
 39669 #define FILEHANDLEID(fd) ((int)fd)
 39671 /*
 39672 ** The Pager.eState variable stores the current 'state' of a pager. A
 39673 ** pager may be in any one of the seven states shown in the following
 39674 ** state diagram.
 39675 **
 39676 **                            OPEN <------+------+
 39677 **                              |         |      |
 39678 **                              V         |      |
 39679 **               +---------> READER-------+      |
 39680 **               |              |                |
 39681 **               |              V                |
 39682 **               |<-------WRITER_LOCKED------> ERROR
 39683 **               |              |                ^  
 39684 **               |              V                |
 39685 **               |<------WRITER_CACHEMOD-------->|
 39686 **               |              |                |
 39687 **               |              V                |
 39688 **               |<-------WRITER_DBMOD---------->|
 39689 **               |              |                |
 39690 **               |              V                |
 39691 **               +<------WRITER_FINISHED-------->+
 39692 **
 39693 **
 39694 ** List of state transitions and the C [function] that performs each:
 39695 ** 
 39696 **   OPEN              -> READER              [sqlite3PagerSharedLock]
 39697 **   READER            -> OPEN                [pager_unlock]
 39698 **
 39699 **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
 39700 **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
 39701 **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
 39702 **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
 39703 **   WRITER_***        -> READER              [pager_end_transaction]
 39704 **
 39705 **   WRITER_***        -> ERROR               [pager_error]
 39706 **   ERROR             -> OPEN                [pager_unlock]
 39707 ** 
 39708 **
 39709 **  OPEN:
 39710 **
 39711 **    The pager starts up in this state. Nothing is guaranteed in this
 39712 **    state - the file may or may not be locked and the database size is
 39713 **    unknown. The database may not be read or written.
 39714 **
 39715 **    * No read or write transaction is active.
 39716 **    * Any lock, or no lock at all, may be held on the database file.
 39717 **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
 39718 **
 39719 **  READER:
 39720 **
 39721 **    In this state all the requirements for reading the database in 
 39722 **    rollback (non-WAL) mode are met. Unless the pager is (or recently
 39723 **    was) in exclusive-locking mode, a user-level read transaction is 
 39724 **    open. The database size is known in this state.
 39725 **
 39726 **    A connection running with locking_mode=normal enters this state when
 39727 **    it opens a read-transaction on the database and returns to state
 39728 **    OPEN after the read-transaction is completed. However a connection
 39729 **    running in locking_mode=exclusive (including temp databases) remains in
 39730 **    this state even after the read-transaction is closed. The only way
 39731 **    a locking_mode=exclusive connection can transition from READER to OPEN
 39732 **    is via the ERROR state (see below).
 39733 ** 
 39734 **    * A read transaction may be active (but a write-transaction cannot).
 39735 **    * A SHARED or greater lock is held on the database file.
 39736 **    * The dbSize variable may be trusted (even if a user-level read 
 39737 **      transaction is not active). The dbOrigSize and dbFileSize variables
 39738 **      may not be trusted at this point.
 39739 **    * If the database is a WAL database, then the WAL connection is open.
 39740 **    * Even if a read-transaction is not open, it is guaranteed that 
 39741 **      there is no hot-journal in the file-system.
 39742 **
 39743 **  WRITER_LOCKED:
 39744 **
 39745 **    The pager moves to this state from READER when a write-transaction
 39746 **    is first opened on the database. In WRITER_LOCKED state, all locks 
 39747 **    required to start a write-transaction are held, but no actual 
 39748 **    modifications to the cache or database have taken place.
 39749 **
 39750 **    In rollback mode, a RESERVED or (if the transaction was opened with 
 39751 **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
 39752 **    moving to this state, but the journal file is not written to or opened 
 39753 **    to in this state. If the transaction is committed or rolled back while 
 39754 **    in WRITER_LOCKED state, all that is required is to unlock the database 
 39755 **    file.
 39756 **
 39757 **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
 39758 **    If the connection is running with locking_mode=exclusive, an attempt
 39759 **    is made to obtain an EXCLUSIVE lock on the database file.
 39760 **
 39761 **    * A write transaction is active.
 39762 **    * If the connection is open in rollback-mode, a RESERVED or greater 
 39763 **      lock is held on the database file.
 39764 **    * If the connection is open in WAL-mode, a WAL write transaction
 39765 **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
 39766 **      called).
 39767 **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
 39768 **    * The contents of the pager cache have not been modified.
 39769 **    * The journal file may or may not be open.
 39770 **    * Nothing (not even the first header) has been written to the journal.
 39771 **
 39772 **  WRITER_CACHEMOD:
 39773 **
 39774 **    A pager moves from WRITER_LOCKED state to this state when a page is
 39775 **    first modified by the upper layer. In rollback mode the journal file
 39776 **    is opened (if it is not already open) and a header written to the
 39777 **    start of it. The database file on disk has not been modified.
 39778 **
 39779 **    * A write transaction is active.
 39780 **    * A RESERVED or greater lock is held on the database file.
 39781 **    * The journal file is open and the first header has been written 
 39782 **      to it, but the header has not been synced to disk.
 39783 **    * The contents of the page cache have been modified.
 39784 **
 39785 **  WRITER_DBMOD:
 39786 **
 39787 **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
 39788 **    when it modifies the contents of the database file. WAL connections
 39789 **    never enter this state (since they do not modify the database file,
 39790 **    just the log file).
 39791 **
 39792 **    * A write transaction is active.
 39793 **    * An EXCLUSIVE or greater lock is held on the database file.
 39794 **    * The journal file is open and the first header has been written 
 39795 **      and synced to disk.
 39796 **    * The contents of the page cache have been modified (and possibly
 39797 **      written to disk).
 39798 **
 39799 **  WRITER_FINISHED:
 39800 **
 39801 **    It is not possible for a WAL connection to enter this state.
 39802 **
 39803 **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
 39804 **    state after the entire transaction has been successfully written into the
 39805 **    database file. In this state the transaction may be committed simply
 39806 **    by finalizing the journal file. Once in WRITER_FINISHED state, it is 
 39807 **    not possible to modify the database further. At this point, the upper 
 39808 **    layer must either commit or rollback the transaction.
 39809 **
 39810 **    * A write transaction is active.
 39811 **    * An EXCLUSIVE or greater lock is held on the database file.
 39812 **    * All writing and syncing of journal and database data has finished.
 39813 **      If no error occurred, all that remains is to finalize the journal to
 39814 **      commit the transaction. If an error did occur, the caller will need
 39815 **      to rollback the transaction. 
 39816 **
 39817 **  ERROR:
 39818 **
 39819 **    The ERROR state is entered when an IO or disk-full error (including
 39820 **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 
 39821 **    difficult to be sure that the in-memory pager state (cache contents, 
 39822 **    db size etc.) are consistent with the contents of the file-system.
 39823 **
 39824 **    Temporary pager files may enter the ERROR state, but in-memory pagers
 39825 **    cannot.
 39826 **
 39827 **    For example, if an IO error occurs while performing a rollback, 
 39828 **    the contents of the page-cache may be left in an inconsistent state.
 39829 **    At this point it would be dangerous to change back to READER state
 39830 **    (as usually happens after a rollback). Any subsequent readers might
 39831 **    report database corruption (due to the inconsistent cache), and if
 39832 **    they upgrade to writers, they may inadvertently corrupt the database
 39833 **    file. To avoid this hazard, the pager switches into the ERROR state
 39834 **    instead of READER following such an error.
 39835 **
 39836 **    Once it has entered the ERROR state, any attempt to use the pager
 39837 **    to read or write data returns an error. Eventually, once all 
 39838 **    outstanding transactions have been abandoned, the pager is able to
 39839 **    transition back to OPEN state, discarding the contents of the 
 39840 **    page-cache and any other in-memory state at the same time. Everything
 39841 **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
 39842 **    when a read-transaction is next opened on the pager (transitioning
 39843 **    the pager into READER state). At that point the system has recovered 
 39844 **    from the error.
 39845 **
 39846 **    Specifically, the pager jumps into the ERROR state if:
 39847 **
 39848 **      1. An error occurs while attempting a rollback. This happens in
 39849 **         function sqlite3PagerRollback().
 39850 **
 39851 **      2. An error occurs while attempting to finalize a journal file
 39852 **         following a commit in function sqlite3PagerCommitPhaseTwo().
 39853 **
 39854 **      3. An error occurs while attempting to write to the journal or
 39855 **         database file in function pagerStress() in order to free up
 39856 **         memory.
 39857 **
 39858 **    In other cases, the error is returned to the b-tree layer. The b-tree
 39859 **    layer then attempts a rollback operation. If the error condition 
 39860 **    persists, the pager enters the ERROR state via condition (1) above.
 39861 **
 39862 **    Condition (3) is necessary because it can be triggered by a read-only
 39863 **    statement executed within a transaction. In this case, if the error
 39864 **    code were simply returned to the user, the b-tree layer would not
 39865 **    automatically attempt a rollback, as it assumes that an error in a
 39866 **    read-only statement cannot leave the pager in an internally inconsistent 
 39867 **    state.
 39868 **
 39869 **    * The Pager.errCode variable is set to something other than SQLITE_OK.
 39870 **    * There are one or more outstanding references to pages (after the
 39871 **      last reference is dropped the pager should move back to OPEN state).
 39872 **    * The pager is not an in-memory pager.
 39873 **    
 39874 **
 39875 ** Notes:
 39876 **
 39877 **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
 39878 **     connection is open in WAL mode. A WAL connection is always in one
 39879 **     of the first four states.
 39880 **
 39881 **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
 39882 **     state. There are two exceptions: immediately after exclusive-mode has
 39883 **     been turned on (and before any read or write transactions are 
 39884 **     executed), and when the pager is leaving the "error state".
 39885 **
 39886 **   * See also: assert_pager_state().
 39887 */
 39888 #define PAGER_OPEN                  0
 39889 #define PAGER_READER                1
 39890 #define PAGER_WRITER_LOCKED         2
 39891 #define PAGER_WRITER_CACHEMOD       3
 39892 #define PAGER_WRITER_DBMOD          4
 39893 #define PAGER_WRITER_FINISHED       5
 39894 #define PAGER_ERROR                 6
 39896 /*
 39897 ** The Pager.eLock variable is almost always set to one of the 
 39898 ** following locking-states, according to the lock currently held on
 39899 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 39900 ** This variable is kept up to date as locks are taken and released by
 39901 ** the pagerLockDb() and pagerUnlockDb() wrappers.
 39902 **
 39903 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
 39904 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
 39905 ** the operation was successful. In these circumstances pagerLockDb() and
 39906 ** pagerUnlockDb() take a conservative approach - eLock is always updated
 39907 ** when unlocking the file, and only updated when locking the file if the
 39908 ** VFS call is successful. This way, the Pager.eLock variable may be set
 39909 ** to a less exclusive (lower) value than the lock that is actually held
 39910 ** at the system level, but it is never set to a more exclusive value.
 39911 **
 39912 ** This is usually safe. If an xUnlock fails or appears to fail, there may 
 39913 ** be a few redundant xLock() calls or a lock may be held for longer than
 39914 ** required, but nothing really goes wrong.
 39915 **
 39916 ** The exception is when the database file is unlocked as the pager moves
 39917 ** from ERROR to OPEN state. At this point there may be a hot-journal file 
 39918 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
 39919 ** transition, by the same pager or any other). If the call to xUnlock()
 39920 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
 39921 ** can confuse the call to xCheckReservedLock() call made later as part
 39922 ** of hot-journal detection.
 39923 **
 39924 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
 39925 ** lock held by this process or any others". So xCheckReservedLock may 
 39926 ** return true because the caller itself is holding an EXCLUSIVE lock (but
 39927 ** doesn't know it because of a previous error in xUnlock). If this happens
 39928 ** a hot-journal may be mistaken for a journal being created by an active
 39929 ** transaction in another process, causing SQLite to read from the database
 39930 ** without rolling it back.
 39931 **
 39932 ** To work around this, if a call to xUnlock() fails when unlocking the
 39933 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
 39934 ** is only changed back to a real locking state after a successful call
 39935 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
 39936 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK 
 39937 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
 39938 ** lock on the database file before attempting to roll it back. See function
 39939 ** PagerSharedLock() for more detail.
 39940 **
 39941 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in 
 39942 ** PAGER_OPEN state.
 39943 */
 39944 #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
 39946 /*
 39947 ** A macro used for invoking the codec if there is one
 39948 */
 39949 #ifdef SQLITE_HAS_CODEC
 39950 # define CODEC1(P,D,N,X,E) \
 39951     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
 39952 # define CODEC2(P,D,N,X,E,O) \
 39953     if( P->xCodec==0 ){ O=(char*)D; }else \
 39954     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
 39955 #else
 39956 # define CODEC1(P,D,N,X,E)   /* NO-OP */
 39957 # define CODEC2(P,D,N,X,E,O) O=(char*)D
 39958 #endif
 39960 /*
 39961 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
 39962 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
 39963 ** This could conceivably cause corruption following a power failure on
 39964 ** such a system. This is currently an undocumented limit.
 39965 */
 39966 #define MAX_SECTOR_SIZE 0x10000
 39968 /*
 39969 ** An instance of the following structure is allocated for each active
 39970 ** savepoint and statement transaction in the system. All such structures
 39971 ** are stored in the Pager.aSavepoint[] array, which is allocated and
 39972 ** resized using sqlite3Realloc().
 39973 **
 39974 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
 39975 ** set to 0. If a journal-header is written into the main journal while
 39976 ** the savepoint is active, then iHdrOffset is set to the byte offset 
 39977 ** immediately following the last journal record written into the main
 39978 ** journal before the journal-header. This is required during savepoint
 39979 ** rollback (see pagerPlaybackSavepoint()).
 39980 */
 39981 typedef struct PagerSavepoint PagerSavepoint;
 39982 struct PagerSavepoint {
 39983   i64 iOffset;                 /* Starting offset in main journal */
 39984   i64 iHdrOffset;              /* See above */
 39985   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
 39986   Pgno nOrig;                  /* Original number of pages in file */
 39987   Pgno iSubRec;                /* Index of first record in sub-journal */
 39988 #ifndef SQLITE_OMIT_WAL
 39989   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
 39990 #endif
 39991 };
 39993 /*
 39994 ** Bits of the Pager.doNotSpill flag.  See further description below.
 39995 */
 39996 #define SPILLFLAG_OFF         0x01      /* Never spill cache.  Set via pragma */
 39997 #define SPILLFLAG_ROLLBACK    0x02      /* Current rolling back, so do not spill */
 39998 #define SPILLFLAG_NOSYNC      0x04      /* Spill is ok, but do not sync */
 40000 /*
 40001 ** A open page cache is an instance of struct Pager. A description of
 40002 ** some of the more important member variables follows:
 40003 **
 40004 ** eState
 40005 **
 40006 **   The current 'state' of the pager object. See the comment and state
 40007 **   diagram above for a description of the pager state.
 40008 **
 40009 ** eLock
 40010 **
 40011 **   For a real on-disk database, the current lock held on the database file -
 40012 **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
 40013 **
 40014 **   For a temporary or in-memory database (neither of which require any
 40015 **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
 40016 **   databases always have Pager.exclusiveMode==1, this tricks the pager
 40017 **   logic into thinking that it already has all the locks it will ever
 40018 **   need (and no reason to release them).
 40019 **
 40020 **   In some (obscure) circumstances, this variable may also be set to
 40021 **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
 40022 **   details.
 40023 **
 40024 ** changeCountDone
 40025 **
 40026 **   This boolean variable is used to make sure that the change-counter 
 40027 **   (the 4-byte header field at byte offset 24 of the database file) is 
 40028 **   not updated more often than necessary. 
 40029 **
 40030 **   It is set to true when the change-counter field is updated, which 
 40031 **   can only happen if an exclusive lock is held on the database file.
 40032 **   It is cleared (set to false) whenever an exclusive lock is 
 40033 **   relinquished on the database file. Each time a transaction is committed,
 40034 **   The changeCountDone flag is inspected. If it is true, the work of
 40035 **   updating the change-counter is omitted for the current transaction.
 40036 **
 40037 **   This mechanism means that when running in exclusive mode, a connection 
 40038 **   need only update the change-counter once, for the first transaction
 40039 **   committed.
 40040 **
 40041 ** setMaster
 40042 **
 40043 **   When PagerCommitPhaseOne() is called to commit a transaction, it may
 40044 **   (or may not) specify a master-journal name to be written into the 
 40045 **   journal file before it is synced to disk.
 40046 **
 40047 **   Whether or not a journal file contains a master-journal pointer affects 
 40048 **   the way in which the journal file is finalized after the transaction is 
 40049 **   committed or rolled back when running in "journal_mode=PERSIST" mode.
 40050 **   If a journal file does not contain a master-journal pointer, it is
 40051 **   finalized by overwriting the first journal header with zeroes. If
 40052 **   it does contain a master-journal pointer the journal file is finalized 
 40053 **   by truncating it to zero bytes, just as if the connection were 
 40054 **   running in "journal_mode=truncate" mode.
 40055 **
 40056 **   Journal files that contain master journal pointers cannot be finalized
 40057 **   simply by overwriting the first journal-header with zeroes, as the
 40058 **   master journal pointer could interfere with hot-journal rollback of any
 40059 **   subsequently interrupted transaction that reuses the journal file.
 40060 **
 40061 **   The flag is cleared as soon as the journal file is finalized (either
 40062 **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
 40063 **   journal file from being successfully finalized, the setMaster flag
 40064 **   is cleared anyway (and the pager will move to ERROR state).
 40065 **
 40066 ** doNotSpill
 40067 **
 40068 **   This variables control the behavior of cache-spills  (calls made by
 40069 **   the pcache module to the pagerStress() routine to write cached data
 40070 **   to the file-system in order to free up memory).
 40071 **
 40072 **   When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
 40073 **   writing to the database from pagerStress() is disabled altogether.
 40074 **   The SPILLFLAG_ROLLBACK case is done in a very obscure case that
 40075 **   comes up during savepoint rollback that requires the pcache module
 40076 **   to allocate a new page to prevent the journal file from being written
 40077 **   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
 40078 **   case is a user preference.
 40079 ** 
 40080 **   If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
 40081 **   is permitted, but syncing the journal file is not. This flag is set
 40082 **   by sqlite3PagerWrite() when the file-system sector-size is larger than
 40083 **   the database page-size in order to prevent a journal sync from happening 
 40084 **   in between the journalling of two pages on the same sector. 
 40085 **
 40086 ** subjInMemory
 40087 **
 40088 **   This is a boolean variable. If true, then any required sub-journal
 40089 **   is opened as an in-memory journal file. If false, then in-memory
 40090 **   sub-journals are only used for in-memory pager files.
 40091 **
 40092 **   This variable is updated by the upper layer each time a new 
 40093 **   write-transaction is opened.
 40094 **
 40095 ** dbSize, dbOrigSize, dbFileSize
 40096 **
 40097 **   Variable dbSize is set to the number of pages in the database file.
 40098 **   It is valid in PAGER_READER and higher states (all states except for
 40099 **   OPEN and ERROR). 
 40100 **
 40101 **   dbSize is set based on the size of the database file, which may be 
 40102 **   larger than the size of the database (the value stored at offset
 40103 **   28 of the database header by the btree). If the size of the file
 40104 **   is not an integer multiple of the page-size, the value stored in
 40105 **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
 40106 **   Except, any file that is greater than 0 bytes in size is considered
 40107 **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
 40108 **   to dbSize==1).
 40109 **
 40110 **   During a write-transaction, if pages with page-numbers greater than
 40111 **   dbSize are modified in the cache, dbSize is updated accordingly.
 40112 **   Similarly, if the database is truncated using PagerTruncateImage(), 
 40113 **   dbSize is updated.
 40114 **
 40115 **   Variables dbOrigSize and dbFileSize are valid in states 
 40116 **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
 40117 **   variable at the start of the transaction. It is used during rollback,
 40118 **   and to determine whether or not pages need to be journalled before
 40119 **   being modified.
 40120 **
 40121 **   Throughout a write-transaction, dbFileSize contains the size of
 40122 **   the file on disk in pages. It is set to a copy of dbSize when the
 40123 **   write-transaction is first opened, and updated when VFS calls are made
 40124 **   to write or truncate the database file on disk. 
 40125 **
 40126 **   The only reason the dbFileSize variable is required is to suppress 
 40127 **   unnecessary calls to xTruncate() after committing a transaction. If, 
 40128 **   when a transaction is committed, the dbFileSize variable indicates 
 40129 **   that the database file is larger than the database image (Pager.dbSize), 
 40130 **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
 40131 **   to measure the database file on disk, and then truncates it if required.
 40132 **   dbFileSize is not used when rolling back a transaction. In this case
 40133 **   pager_truncate() is called unconditionally (which means there may be
 40134 **   a call to xFilesize() that is not strictly required). In either case,
 40135 **   pager_truncate() may cause the file to become smaller or larger.
 40136 **
 40137 ** dbHintSize
 40138 **
 40139 **   The dbHintSize variable is used to limit the number of calls made to
 40140 **   the VFS xFileControl(FCNTL_SIZE_HINT) method. 
 40141 **
 40142 **   dbHintSize is set to a copy of the dbSize variable when a
 40143 **   write-transaction is opened (at the same time as dbFileSize and
 40144 **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
 40145 **   dbHintSize is increased to the number of pages that correspond to the
 40146 **   size-hint passed to the method call. See pager_write_pagelist() for 
 40147 **   details.
 40148 **
 40149 ** errCode
 40150 **
 40151 **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
 40152 **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
 40153 **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
 40154 **   sub-codes.
 40155 */
 40156 struct Pager {
 40157   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
 40158   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
 40159   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
 40160   u8 useJournal;              /* Use a rollback journal on this file */
 40161   u8 noSync;                  /* Do not sync the journal if true */
 40162   u8 fullSync;                /* Do extra syncs of the journal for robustness */
 40163   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 40164   u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
 40165   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
 40166   u8 tempFile;                /* zFilename is a temporary file */
 40167   u8 readOnly;                /* True for a read-only database */
 40168   u8 memDb;                   /* True to inhibit all file I/O */
 40170   /**************************************************************************
 40171   ** The following block contains those class members that change during
 40172   ** routine opertion.  Class members not in this block are either fixed
 40173   ** when the pager is first created or else only change when there is a
 40174   ** significant mode change (such as changing the page_size, locking_mode,
 40175   ** or the journal_mode).  From another view, these class members describe
 40176   ** the "state" of the pager, while other class members describe the
 40177   ** "configuration" of the pager.
 40178   */
 40179   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
 40180   u8 eLock;                   /* Current lock held on database file */
 40181   u8 changeCountDone;         /* Set after incrementing the change-counter */
 40182   u8 setMaster;               /* True if a m-j name has been written to jrnl */
 40183   u8 doNotSpill;              /* Do not spill the cache when non-zero */
 40184   u8 subjInMemory;            /* True to use in-memory sub-journals */
 40185   Pgno dbSize;                /* Number of pages in the database */
 40186   Pgno dbOrigSize;            /* dbSize before the current transaction */
 40187   Pgno dbFileSize;            /* Number of pages in the database file */
 40188   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
 40189   int errCode;                /* One of several kinds of errors */
 40190   int nRec;                   /* Pages journalled since last j-header written */
 40191   u32 cksumInit;              /* Quasi-random value added to every checksum */
 40192   u32 nSubRec;                /* Number of records written to sub-journal */
 40193   Bitvec *pInJournal;         /* One bit for each page in the database file */
 40194   sqlite3_file *fd;           /* File descriptor for database */
 40195   sqlite3_file *jfd;          /* File descriptor for main journal */
 40196   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
 40197   i64 journalOff;             /* Current write offset in the journal file */
 40198   i64 journalHdr;             /* Byte offset to previous journal header */
 40199   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
 40200   PagerSavepoint *aSavepoint; /* Array of active savepoints */
 40201   int nSavepoint;             /* Number of elements in aSavepoint[] */
 40202   char dbFileVers[16];        /* Changes whenever database file changes */
 40204   u8 bUseFetch;               /* True to use xFetch() */
 40205   int nMmapOut;               /* Number of mmap pages currently outstanding */
 40206   sqlite3_int64 szMmap;       /* Desired maximum mmap size */
 40207   PgHdr *pMmapFreelist;       /* List of free mmap page headers (pDirty) */
 40208   /*
 40209   ** End of the routinely-changing class members
 40210   ***************************************************************************/
 40212   u16 nExtra;                 /* Add this many bytes to each in-memory page */
 40213   i16 nReserve;               /* Number of unused bytes at end of each page */
 40214   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
 40215   u32 sectorSize;             /* Assumed sector size during rollback */
 40216   int pageSize;               /* Number of bytes in a page */
 40217   Pgno mxPgno;                /* Maximum allowed size of the database */
 40218   i64 journalSizeLimit;       /* Size limit for persistent journal files */
 40219   char *zFilename;            /* Name of the database file */
 40220   char *zJournal;             /* Name of the journal file */
 40221   int (*xBusyHandler)(void*); /* Function to call when busy */
 40222   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 40223   int aStat[3];               /* Total cache hits, misses and writes */
 40224 #ifdef SQLITE_TEST
 40225   int nRead;                  /* Database pages read */
 40226 #endif
 40227   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 40228 #ifdef SQLITE_HAS_CODEC
 40229   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
 40230   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
 40231   void (*xCodecFree)(void*);             /* Destructor for the codec */
 40232   void *pCodec;               /* First argument to xCodec... methods */
 40233 #endif
 40234   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
 40235   PCache *pPCache;            /* Pointer to page cache object */
 40236 #ifndef SQLITE_OMIT_WAL
 40237   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
 40238   char *zWal;                 /* File name for write-ahead log */
 40239 #endif
 40240 };
 40242 /*
 40243 ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 40244 ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 40245 ** or CACHE_WRITE to sqlite3_db_status().
 40246 */
 40247 #define PAGER_STAT_HIT   0
 40248 #define PAGER_STAT_MISS  1
 40249 #define PAGER_STAT_WRITE 2
 40251 /*
 40252 ** The following global variables hold counters used for
 40253 ** testing purposes only.  These variables do not exist in
 40254 ** a non-testing build.  These variables are not thread-safe.
 40255 */
 40256 #ifdef SQLITE_TEST
 40257 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
 40258 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
 40259 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
 40260 # define PAGER_INCR(v)  v++
 40261 #else
 40262 # define PAGER_INCR(v)
 40263 #endif
 40267 /*
 40268 ** Journal files begin with the following magic string.  The data
 40269 ** was obtained from /dev/random.  It is used only as a sanity check.
 40270 **
 40271 ** Since version 2.8.0, the journal format contains additional sanity
 40272 ** checking information.  If the power fails while the journal is being
 40273 ** written, semi-random garbage data might appear in the journal
 40274 ** file after power is restored.  If an attempt is then made
 40275 ** to roll the journal back, the database could be corrupted.  The additional
 40276 ** sanity checking data is an attempt to discover the garbage in the
 40277 ** journal and ignore it.
 40278 **
 40279 ** The sanity checking information for the new journal format consists
 40280 ** of a 32-bit checksum on each page of data.  The checksum covers both
 40281 ** the page number and the pPager->pageSize bytes of data for the page.
 40282 ** This cksum is initialized to a 32-bit random value that appears in the
 40283 ** journal file right after the header.  The random initializer is important,
 40284 ** because garbage data that appears at the end of a journal is likely
 40285 ** data that was once in other files that have now been deleted.  If the
 40286 ** garbage data came from an obsolete journal file, the checksums might
 40287 ** be correct.  But by initializing the checksum to random value which
 40288 ** is different for every journal, we minimize that risk.
 40289 */
 40290 static const unsigned char aJournalMagic[] = {
 40291   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
 40292 };
 40294 /*
 40295 ** The size of the of each page record in the journal is given by
 40296 ** the following macro.
 40297 */
 40298 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
 40300 /*
 40301 ** The journal header size for this pager. This is usually the same 
 40302 ** size as a single disk sector. See also setSectorSize().
 40303 */
 40304 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
 40306 /*
 40307 ** The macro MEMDB is true if we are dealing with an in-memory database.
 40308 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
 40309 ** the value of MEMDB will be a constant and the compiler will optimize
 40310 ** out code that would never execute.
 40311 */
 40312 #ifdef SQLITE_OMIT_MEMORYDB
 40313 # define MEMDB 0
 40314 #else
 40315 # define MEMDB pPager->memDb
 40316 #endif
 40318 /*
 40319 ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
 40320 ** interfaces to access the database using memory-mapped I/O.
 40321 */
 40322 #if SQLITE_MAX_MMAP_SIZE>0
 40323 # define USEFETCH(x) ((x)->bUseFetch)
 40324 #else
 40325 # define USEFETCH(x) 0
 40326 #endif
 40328 /*
 40329 ** The maximum legal page number is (2^31 - 1).
 40330 */
 40331 #define PAGER_MAX_PGNO 2147483647
 40333 /*
 40334 ** The argument to this macro is a file descriptor (type sqlite3_file*).
 40335 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
 40336 **
 40337 ** This is so that expressions can be written as:
 40338 **
 40339 **   if( isOpen(pPager->jfd) ){ ...
 40340 **
 40341 ** instead of
 40342 **
 40343 **   if( pPager->jfd->pMethods ){ ...
 40344 */
 40345 #define isOpen(pFd) ((pFd)->pMethods)
 40347 /*
 40348 ** Return true if this pager uses a write-ahead log instead of the usual
 40349 ** rollback journal. Otherwise false.
 40350 */
 40351 #ifndef SQLITE_OMIT_WAL
 40352 static int pagerUseWal(Pager *pPager){
 40353   return (pPager->pWal!=0);
 40355 #else
 40356 # define pagerUseWal(x) 0
 40357 # define pagerRollbackWal(x) 0
 40358 # define pagerWalFrames(v,w,x,y) 0
 40359 # define pagerOpenWalIfPresent(z) SQLITE_OK
 40360 # define pagerBeginReadTransaction(z) SQLITE_OK
 40361 #endif
 40363 #ifndef NDEBUG 
 40364 /*
 40365 ** Usage:
 40366 **
 40367 **   assert( assert_pager_state(pPager) );
 40368 **
 40369 ** This function runs many asserts to try to find inconsistencies in
 40370 ** the internal state of the Pager object.
 40371 */
 40372 static int assert_pager_state(Pager *p){
 40373   Pager *pPager = p;
 40375   /* State must be valid. */
 40376   assert( p->eState==PAGER_OPEN
 40377        || p->eState==PAGER_READER
 40378        || p->eState==PAGER_WRITER_LOCKED
 40379        || p->eState==PAGER_WRITER_CACHEMOD
 40380        || p->eState==PAGER_WRITER_DBMOD
 40381        || p->eState==PAGER_WRITER_FINISHED
 40382        || p->eState==PAGER_ERROR
 40383   );
 40385   /* Regardless of the current state, a temp-file connection always behaves
 40386   ** as if it has an exclusive lock on the database file. It never updates
 40387   ** the change-counter field, so the changeCountDone flag is always set.
 40388   */
 40389   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
 40390   assert( p->tempFile==0 || pPager->changeCountDone );
 40392   /* If the useJournal flag is clear, the journal-mode must be "OFF". 
 40393   ** And if the journal-mode is "OFF", the journal file must not be open.
 40394   */
 40395   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
 40396   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
 40398   /* Check that MEMDB implies noSync. And an in-memory journal. Since 
 40399   ** this means an in-memory pager performs no IO at all, it cannot encounter 
 40400   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing 
 40401   ** a journal file. (although the in-memory journal implementation may 
 40402   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It 
 40403   ** is therefore not possible for an in-memory pager to enter the ERROR 
 40404   ** state.
 40405   */
 40406   if( MEMDB ){
 40407     assert( p->noSync );
 40408     assert( p->journalMode==PAGER_JOURNALMODE_OFF 
 40409          || p->journalMode==PAGER_JOURNALMODE_MEMORY 
 40410     );
 40411     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
 40412     assert( pagerUseWal(p)==0 );
 40415   /* If changeCountDone is set, a RESERVED lock or greater must be held
 40416   ** on the file.
 40417   */
 40418   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
 40419   assert( p->eLock!=PENDING_LOCK );
 40421   switch( p->eState ){
 40422     case PAGER_OPEN:
 40423       assert( !MEMDB );
 40424       assert( pPager->errCode==SQLITE_OK );
 40425       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
 40426       break;
 40428     case PAGER_READER:
 40429       assert( pPager->errCode==SQLITE_OK );
 40430       assert( p->eLock!=UNKNOWN_LOCK );
 40431       assert( p->eLock>=SHARED_LOCK );
 40432       break;
 40434     case PAGER_WRITER_LOCKED:
 40435       assert( p->eLock!=UNKNOWN_LOCK );
 40436       assert( pPager->errCode==SQLITE_OK );
 40437       if( !pagerUseWal(pPager) ){
 40438         assert( p->eLock>=RESERVED_LOCK );
 40440       assert( pPager->dbSize==pPager->dbOrigSize );
 40441       assert( pPager->dbOrigSize==pPager->dbFileSize );
 40442       assert( pPager->dbOrigSize==pPager->dbHintSize );
 40443       assert( pPager->setMaster==0 );
 40444       break;
 40446     case PAGER_WRITER_CACHEMOD:
 40447       assert( p->eLock!=UNKNOWN_LOCK );
 40448       assert( pPager->errCode==SQLITE_OK );
 40449       if( !pagerUseWal(pPager) ){
 40450         /* It is possible that if journal_mode=wal here that neither the
 40451         ** journal file nor the WAL file are open. This happens during
 40452         ** a rollback transaction that switches from journal_mode=off
 40453         ** to journal_mode=wal.
 40454         */
 40455         assert( p->eLock>=RESERVED_LOCK );
 40456         assert( isOpen(p->jfd) 
 40457              || p->journalMode==PAGER_JOURNALMODE_OFF 
 40458              || p->journalMode==PAGER_JOURNALMODE_WAL 
 40459         );
 40461       assert( pPager->dbOrigSize==pPager->dbFileSize );
 40462       assert( pPager->dbOrigSize==pPager->dbHintSize );
 40463       break;
 40465     case PAGER_WRITER_DBMOD:
 40466       assert( p->eLock==EXCLUSIVE_LOCK );
 40467       assert( pPager->errCode==SQLITE_OK );
 40468       assert( !pagerUseWal(pPager) );
 40469       assert( p->eLock>=EXCLUSIVE_LOCK );
 40470       assert( isOpen(p->jfd) 
 40471            || p->journalMode==PAGER_JOURNALMODE_OFF 
 40472            || p->journalMode==PAGER_JOURNALMODE_WAL 
 40473       );
 40474       assert( pPager->dbOrigSize<=pPager->dbHintSize );
 40475       break;
 40477     case PAGER_WRITER_FINISHED:
 40478       assert( p->eLock==EXCLUSIVE_LOCK );
 40479       assert( pPager->errCode==SQLITE_OK );
 40480       assert( !pagerUseWal(pPager) );
 40481       assert( isOpen(p->jfd) 
 40482            || p->journalMode==PAGER_JOURNALMODE_OFF 
 40483            || p->journalMode==PAGER_JOURNALMODE_WAL 
 40484       );
 40485       break;
 40487     case PAGER_ERROR:
 40488       /* There must be at least one outstanding reference to the pager if
 40489       ** in ERROR state. Otherwise the pager should have already dropped
 40490       ** back to OPEN state.
 40491       */
 40492       assert( pPager->errCode!=SQLITE_OK );
 40493       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
 40494       break;
 40497   return 1;
 40499 #endif /* ifndef NDEBUG */
 40501 #ifdef SQLITE_DEBUG 
 40502 /*
 40503 ** Return a pointer to a human readable string in a static buffer
 40504 ** containing the state of the Pager object passed as an argument. This
 40505 ** is intended to be used within debuggers. For example, as an alternative
 40506 ** to "print *pPager" in gdb:
 40507 **
 40508 ** (gdb) printf "%s", print_pager_state(pPager)
 40509 */
 40510 static char *print_pager_state(Pager *p){
 40511   static char zRet[1024];
 40513   sqlite3_snprintf(1024, zRet,
 40514       "Filename:      %s\n"
 40515       "State:         %s errCode=%d\n"
 40516       "Lock:          %s\n"
 40517       "Locking mode:  locking_mode=%s\n"
 40518       "Journal mode:  journal_mode=%s\n"
 40519       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
 40520       "Journal:       journalOff=%lld journalHdr=%lld\n"
 40521       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
 40522       , p->zFilename
 40523       , p->eState==PAGER_OPEN            ? "OPEN" :
 40524         p->eState==PAGER_READER          ? "READER" :
 40525         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
 40526         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
 40527         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
 40528         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
 40529         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
 40530       , (int)p->errCode
 40531       , p->eLock==NO_LOCK         ? "NO_LOCK" :
 40532         p->eLock==RESERVED_LOCK   ? "RESERVED" :
 40533         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
 40534         p->eLock==SHARED_LOCK     ? "SHARED" :
 40535         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
 40536       , p->exclusiveMode ? "exclusive" : "normal"
 40537       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
 40538         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
 40539         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
 40540         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
 40541         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
 40542         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
 40543       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
 40544       , p->journalOff, p->journalHdr
 40545       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
 40546   );
 40548   return zRet;
 40550 #endif
 40552 /*
 40553 ** Return true if it is necessary to write page *pPg into the sub-journal.
 40554 ** A page needs to be written into the sub-journal if there exists one
 40555 ** or more open savepoints for which:
 40556 **
 40557 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
 40558 **   * The bit corresponding to the page-number is not set in
 40559 **     PagerSavepoint.pInSavepoint.
 40560 */
 40561 static int subjRequiresPage(PgHdr *pPg){
 40562   Pager *pPager = pPg->pPager;
 40563   PagerSavepoint *p;
 40564   Pgno pgno = pPg->pgno;
 40565   int i;
 40566   for(i=0; i<pPager->nSavepoint; i++){
 40567     p = &pPager->aSavepoint[i];
 40568     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
 40569       return 1;
 40572   return 0;
 40575 /*
 40576 ** Return true if the page is already in the journal file.
 40577 */
 40578 static int pageInJournal(Pager *pPager, PgHdr *pPg){
 40579   return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
 40582 /*
 40583 ** Read a 32-bit integer from the given file descriptor.  Store the integer
 40584 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
 40585 ** error code is something goes wrong.
 40586 **
 40587 ** All values are stored on disk as big-endian.
 40588 */
 40589 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
 40590   unsigned char ac[4];
 40591   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
 40592   if( rc==SQLITE_OK ){
 40593     *pRes = sqlite3Get4byte(ac);
 40595   return rc;
 40598 /*
 40599 ** Write a 32-bit integer into a string buffer in big-endian byte order.
 40600 */
 40601 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
 40604 /*
 40605 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
 40606 ** on success or an error code is something goes wrong.
 40607 */
 40608 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
 40609   char ac[4];
 40610   put32bits(ac, val);
 40611   return sqlite3OsWrite(fd, ac, 4, offset);
 40614 /*
 40615 ** Unlock the database file to level eLock, which must be either NO_LOCK
 40616 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
 40617 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
 40618 **
 40619 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
 40620 ** called, do not modify it. See the comment above the #define of 
 40621 ** UNKNOWN_LOCK for an explanation of this.
 40622 */
 40623 static int pagerUnlockDb(Pager *pPager, int eLock){
 40624   int rc = SQLITE_OK;
 40626   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
 40627   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
 40628   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
 40629   if( isOpen(pPager->fd) ){
 40630     assert( pPager->eLock>=eLock );
 40631     rc = sqlite3OsUnlock(pPager->fd, eLock);
 40632     if( pPager->eLock!=UNKNOWN_LOCK ){
 40633       pPager->eLock = (u8)eLock;
 40635     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
 40637   return rc;
 40640 /*
 40641 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
 40642 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
 40643 ** Pager.eLock variable to the new locking state. 
 40644 **
 40645 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 
 40646 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 
 40647 ** See the comment above the #define of UNKNOWN_LOCK for an explanation 
 40648 ** of this.
 40649 */
 40650 static int pagerLockDb(Pager *pPager, int eLock){
 40651   int rc = SQLITE_OK;
 40653   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
 40654   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
 40655     rc = sqlite3OsLock(pPager->fd, eLock);
 40656     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
 40657       pPager->eLock = (u8)eLock;
 40658       IOTRACE(("LOCK %p %d\n", pPager, eLock))
 40661   return rc;
 40664 /*
 40665 ** This function determines whether or not the atomic-write optimization
 40666 ** can be used with this pager. The optimization can be used if:
 40667 **
 40668 **  (a) the value returned by OsDeviceCharacteristics() indicates that
 40669 **      a database page may be written atomically, and
 40670 **  (b) the value returned by OsSectorSize() is less than or equal
 40671 **      to the page size.
 40672 **
 40673 ** The optimization is also always enabled for temporary files. It is
 40674 ** an error to call this function if pPager is opened on an in-memory
 40675 ** database.
 40676 **
 40677 ** If the optimization cannot be used, 0 is returned. If it can be used,
 40678 ** then the value returned is the size of the journal file when it
 40679 ** contains rollback data for exactly one page.
 40680 */
 40681 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 40682 static int jrnlBufferSize(Pager *pPager){
 40683   assert( !MEMDB );
 40684   if( !pPager->tempFile ){
 40685     int dc;                           /* Device characteristics */
 40686     int nSector;                      /* Sector size */
 40687     int szPage;                       /* Page size */
 40689     assert( isOpen(pPager->fd) );
 40690     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 40691     nSector = pPager->sectorSize;
 40692     szPage = pPager->pageSize;
 40694     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 40695     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 40696     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
 40697       return 0;
 40701   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
 40703 #endif
 40705 /*
 40706 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
 40707 ** on the cache using a hash function.  This is used for testing
 40708 ** and debugging only.
 40709 */
 40710 #ifdef SQLITE_CHECK_PAGES
 40711 /*
 40712 ** Return a 32-bit hash of the page data for pPage.
 40713 */
 40714 static u32 pager_datahash(int nByte, unsigned char *pData){
 40715   u32 hash = 0;
 40716   int i;
 40717   for(i=0; i<nByte; i++){
 40718     hash = (hash*1039) + pData[i];
 40720   return hash;
 40722 static u32 pager_pagehash(PgHdr *pPage){
 40723   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
 40725 static void pager_set_pagehash(PgHdr *pPage){
 40726   pPage->pageHash = pager_pagehash(pPage);
 40729 /*
 40730 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
 40731 ** is defined, and NDEBUG is not defined, an assert() statement checks
 40732 ** that the page is either dirty or still matches the calculated page-hash.
 40733 */
 40734 #define CHECK_PAGE(x) checkPage(x)
 40735 static void checkPage(PgHdr *pPg){
 40736   Pager *pPager = pPg->pPager;
 40737   assert( pPager->eState!=PAGER_ERROR );
 40738   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
 40741 #else
 40742 #define pager_datahash(X,Y)  0
 40743 #define pager_pagehash(X)  0
 40744 #define pager_set_pagehash(X)
 40745 #define CHECK_PAGE(x)
 40746 #endif  /* SQLITE_CHECK_PAGES */
 40748 /*
 40749 ** When this is called the journal file for pager pPager must be open.
 40750 ** This function attempts to read a master journal file name from the 
 40751 ** end of the file and, if successful, copies it into memory supplied 
 40752 ** by the caller. See comments above writeMasterJournal() for the format
 40753 ** used to store a master journal file name at the end of a journal file.
 40754 **
 40755 ** zMaster must point to a buffer of at least nMaster bytes allocated by
 40756 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
 40757 ** enough space to write the master journal name). If the master journal
 40758 ** name in the journal is longer than nMaster bytes (including a
 40759 ** nul-terminator), then this is handled as if no master journal name
 40760 ** were present in the journal.
 40761 **
 40762 ** If a master journal file name is present at the end of the journal
 40763 ** file, then it is copied into the buffer pointed to by zMaster. A
 40764 ** nul-terminator byte is appended to the buffer following the master
 40765 ** journal file name.
 40766 **
 40767 ** If it is determined that no master journal file name is present 
 40768 ** zMaster[0] is set to 0 and SQLITE_OK returned.
 40769 **
 40770 ** If an error occurs while reading from the journal file, an SQLite
 40771 ** error code is returned.
 40772 */
 40773 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
 40774   int rc;                    /* Return code */
 40775   u32 len;                   /* Length in bytes of master journal name */
 40776   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
 40777   u32 cksum;                 /* MJ checksum value read from journal */
 40778   u32 u;                     /* Unsigned loop counter */
 40779   unsigned char aMagic[8];   /* A buffer to hold the magic header */
 40780   zMaster[0] = '\0';
 40782   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
 40783    || szJ<16
 40784    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
 40785    || len>=nMaster 
 40786    || len==0 
 40787    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
 40788    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
 40789    || memcmp(aMagic, aJournalMagic, 8)
 40790    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
 40791   ){
 40792     return rc;
 40795   /* See if the checksum matches the master journal name */
 40796   for(u=0; u<len; u++){
 40797     cksum -= zMaster[u];
 40799   if( cksum ){
 40800     /* If the checksum doesn't add up, then one or more of the disk sectors
 40801     ** containing the master journal filename is corrupted. This means
 40802     ** definitely roll back, so just return SQLITE_OK and report a (nul)
 40803     ** master-journal filename.
 40804     */
 40805     len = 0;
 40807   zMaster[len] = '\0';
 40809   return SQLITE_OK;
 40812 /*
 40813 ** Return the offset of the sector boundary at or immediately 
 40814 ** following the value in pPager->journalOff, assuming a sector 
 40815 ** size of pPager->sectorSize bytes.
 40816 **
 40817 ** i.e for a sector size of 512:
 40818 **
 40819 **   Pager.journalOff          Return value
 40820 **   ---------------------------------------
 40821 **   0                         0
 40822 **   512                       512
 40823 **   100                       512
 40824 **   2000                      2048
 40825 ** 
 40826 */
 40827 static i64 journalHdrOffset(Pager *pPager){
 40828   i64 offset = 0;
 40829   i64 c = pPager->journalOff;
 40830   if( c ){
 40831     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
 40833   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
 40834   assert( offset>=c );
 40835   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
 40836   return offset;
 40839 /*
 40840 ** The journal file must be open when this function is called.
 40841 **
 40842 ** This function is a no-op if the journal file has not been written to
 40843 ** within the current transaction (i.e. if Pager.journalOff==0).
 40844 **
 40845 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
 40846 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
 40847 ** zero the 28-byte header at the start of the journal file. In either case, 
 40848 ** if the pager is not in no-sync mode, sync the journal file immediately 
 40849 ** after writing or truncating it.
 40850 **
 40851 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
 40852 ** following the truncation or zeroing described above the size of the 
 40853 ** journal file in bytes is larger than this value, then truncate the
 40854 ** journal file to Pager.journalSizeLimit bytes. The journal file does
 40855 ** not need to be synced following this operation.
 40856 **
 40857 ** If an IO error occurs, abandon processing and return the IO error code.
 40858 ** Otherwise, return SQLITE_OK.
 40859 */
 40860 static int zeroJournalHdr(Pager *pPager, int doTruncate){
 40861   int rc = SQLITE_OK;                               /* Return code */
 40862   assert( isOpen(pPager->jfd) );
 40863   if( pPager->journalOff ){
 40864     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
 40866     IOTRACE(("JZEROHDR %p\n", pPager))
 40867     if( doTruncate || iLimit==0 ){
 40868       rc = sqlite3OsTruncate(pPager->jfd, 0);
 40869     }else{
 40870       static const char zeroHdr[28] = {0};
 40871       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
 40873     if( rc==SQLITE_OK && !pPager->noSync ){
 40874       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
 40877     /* At this point the transaction is committed but the write lock 
 40878     ** is still held on the file. If there is a size limit configured for 
 40879     ** the persistent journal and the journal file currently consumes more
 40880     ** space than that limit allows for, truncate it now. There is no need
 40881     ** to sync the file following this operation.
 40882     */
 40883     if( rc==SQLITE_OK && iLimit>0 ){
 40884       i64 sz;
 40885       rc = sqlite3OsFileSize(pPager->jfd, &sz);
 40886       if( rc==SQLITE_OK && sz>iLimit ){
 40887         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
 40891   return rc;
 40894 /*
 40895 ** The journal file must be open when this routine is called. A journal
 40896 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
 40897 ** current location.
 40898 **
 40899 ** The format for the journal header is as follows:
 40900 ** - 8 bytes: Magic identifying journal format.
 40901 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
 40902 ** - 4 bytes: Random number used for page hash.
 40903 ** - 4 bytes: Initial database page count.
 40904 ** - 4 bytes: Sector size used by the process that wrote this journal.
 40905 ** - 4 bytes: Database page size.
 40906 ** 
 40907 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
 40908 */
 40909 static int writeJournalHdr(Pager *pPager){
 40910   int rc = SQLITE_OK;                 /* Return code */
 40911   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
 40912   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
 40913   u32 nWrite;                         /* Bytes of header sector written */
 40914   int ii;                             /* Loop counter */
 40916   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 40918   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
 40919     nHeader = JOURNAL_HDR_SZ(pPager);
 40922   /* If there are active savepoints and any of them were created 
 40923   ** since the most recent journal header was written, update the 
 40924   ** PagerSavepoint.iHdrOffset fields now.
 40925   */
 40926   for(ii=0; ii<pPager->nSavepoint; ii++){
 40927     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
 40928       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
 40932   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
 40934   /* 
 40935   ** Write the nRec Field - the number of page records that follow this
 40936   ** journal header. Normally, zero is written to this value at this time.
 40937   ** After the records are added to the journal (and the journal synced, 
 40938   ** if in full-sync mode), the zero is overwritten with the true number
 40939   ** of records (see syncJournal()).
 40940   **
 40941   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
 40942   ** reading the journal this value tells SQLite to assume that the
 40943   ** rest of the journal file contains valid page records. This assumption
 40944   ** is dangerous, as if a failure occurred whilst writing to the journal
 40945   ** file it may contain some garbage data. There are two scenarios
 40946   ** where this risk can be ignored:
 40947   **
 40948   **   * When the pager is in no-sync mode. Corruption can follow a
 40949   **     power failure in this case anyway.
 40950   **
 40951   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
 40952   **     that garbage data is never appended to the journal file.
 40953   */
 40954   assert( isOpen(pPager->fd) || pPager->noSync );
 40955   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
 40956    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
 40957   ){
 40958     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 40959     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
 40960   }else{
 40961     memset(zHeader, 0, sizeof(aJournalMagic)+4);
 40964   /* The random check-hash initializer */ 
 40965   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
 40966   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
 40967   /* The initial database size */
 40968   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
 40969   /* The assumed sector size for this process */
 40970   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
 40972   /* The page size */
 40973   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
 40975   /* Initializing the tail of the buffer is not necessary.  Everything
 40976   ** works find if the following memset() is omitted.  But initializing
 40977   ** the memory prevents valgrind from complaining, so we are willing to
 40978   ** take the performance hit.
 40979   */
 40980   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
 40981          nHeader-(sizeof(aJournalMagic)+20));
 40983   /* In theory, it is only necessary to write the 28 bytes that the 
 40984   ** journal header consumes to the journal file here. Then increment the 
 40985   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
 40986   ** record is written to the following sector (leaving a gap in the file
 40987   ** that will be implicitly filled in by the OS).
 40988   **
 40989   ** However it has been discovered that on some systems this pattern can 
 40990   ** be significantly slower than contiguously writing data to the file,
 40991   ** even if that means explicitly writing data to the block of 
 40992   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
 40993   ** is done. 
 40994   **
 40995   ** The loop is required here in case the sector-size is larger than the 
 40996   ** database page size. Since the zHeader buffer is only Pager.pageSize
 40997   ** bytes in size, more than one call to sqlite3OsWrite() may be required
 40998   ** to populate the entire journal header sector.
 40999   */ 
 41000   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
 41001     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
 41002     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
 41003     assert( pPager->journalHdr <= pPager->journalOff );
 41004     pPager->journalOff += nHeader;
 41007   return rc;
 41010 /*
 41011 ** The journal file must be open when this is called. A journal header file
 41012 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
 41013 ** file. The current location in the journal file is given by
 41014 ** pPager->journalOff. See comments above function writeJournalHdr() for
 41015 ** a description of the journal header format.
 41016 **
 41017 ** If the header is read successfully, *pNRec is set to the number of
 41018 ** page records following this header and *pDbSize is set to the size of the
 41019 ** database before the transaction began, in pages. Also, pPager->cksumInit
 41020 ** is set to the value read from the journal header. SQLITE_OK is returned
 41021 ** in this case.
 41022 **
 41023 ** If the journal header file appears to be corrupted, SQLITE_DONE is
 41024 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
 41025 ** cannot be read from the journal file an error code is returned.
 41026 */
 41027 static int readJournalHdr(
 41028   Pager *pPager,               /* Pager object */
 41029   int isHot,
 41030   i64 journalSize,             /* Size of the open journal file in bytes */
 41031   u32 *pNRec,                  /* OUT: Value read from the nRec field */
 41032   u32 *pDbSize                 /* OUT: Value of original database size field */
 41033 ){
 41034   int rc;                      /* Return code */
 41035   unsigned char aMagic[8];     /* A buffer to hold the magic header */
 41036   i64 iHdrOff;                 /* Offset of journal header being read */
 41038   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
 41040   /* Advance Pager.journalOff to the start of the next sector. If the
 41041   ** journal file is too small for there to be a header stored at this
 41042   ** point, return SQLITE_DONE.
 41043   */
 41044   pPager->journalOff = journalHdrOffset(pPager);
 41045   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
 41046     return SQLITE_DONE;
 41048   iHdrOff = pPager->journalOff;
 41050   /* Read in the first 8 bytes of the journal header. If they do not match
 41051   ** the  magic string found at the start of each journal header, return
 41052   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
 41053   ** proceed.
 41054   */
 41055   if( isHot || iHdrOff!=pPager->journalHdr ){
 41056     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
 41057     if( rc ){
 41058       return rc;
 41060     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
 41061       return SQLITE_DONE;
 41065   /* Read the first three 32-bit fields of the journal header: The nRec
 41066   ** field, the checksum-initializer and the database size at the start
 41067   ** of the transaction. Return an error code if anything goes wrong.
 41068   */
 41069   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
 41070    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
 41071    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
 41072   ){
 41073     return rc;
 41076   if( pPager->journalOff==0 ){
 41077     u32 iPageSize;               /* Page-size field of journal header */
 41078     u32 iSectorSize;             /* Sector-size field of journal header */
 41080     /* Read the page-size and sector-size journal header fields. */
 41081     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
 41082      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
 41083     ){
 41084       return rc;
 41087     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
 41088     ** journal header to zero. In this case, assume that the Pager.pageSize
 41089     ** variable is already set to the correct page size.
 41090     */
 41091     if( iPageSize==0 ){
 41092       iPageSize = pPager->pageSize;
 41095     /* Check that the values read from the page-size and sector-size fields
 41096     ** are within range. To be 'in range', both values need to be a power
 41097     ** of two greater than or equal to 512 or 32, and not greater than their 
 41098     ** respective compile time maximum limits.
 41099     */
 41100     if( iPageSize<512                  || iSectorSize<32
 41101      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
 41102      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
 41103     ){
 41104       /* If the either the page-size or sector-size in the journal-header is 
 41105       ** invalid, then the process that wrote the journal-header must have 
 41106       ** crashed before the header was synced. In this case stop reading 
 41107       ** the journal file here.
 41108       */
 41109       return SQLITE_DONE;
 41112     /* Update the page-size to match the value read from the journal. 
 41113     ** Use a testcase() macro to make sure that malloc failure within 
 41114     ** PagerSetPagesize() is tested.
 41115     */
 41116     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
 41117     testcase( rc!=SQLITE_OK );
 41119     /* Update the assumed sector-size to match the value used by 
 41120     ** the process that created this journal. If this journal was
 41121     ** created by a process other than this one, then this routine
 41122     ** is being called from within pager_playback(). The local value
 41123     ** of Pager.sectorSize is restored at the end of that routine.
 41124     */
 41125     pPager->sectorSize = iSectorSize;
 41128   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
 41129   return rc;
 41133 /*
 41134 ** Write the supplied master journal name into the journal file for pager
 41135 ** pPager at the current location. The master journal name must be the last
 41136 ** thing written to a journal file. If the pager is in full-sync mode, the
 41137 ** journal file descriptor is advanced to the next sector boundary before
 41138 ** anything is written. The format is:
 41139 **
 41140 **   + 4 bytes: PAGER_MJ_PGNO.
 41141 **   + N bytes: Master journal filename in utf-8.
 41142 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
 41143 **   + 4 bytes: Master journal name checksum.
 41144 **   + 8 bytes: aJournalMagic[].
 41145 **
 41146 ** The master journal page checksum is the sum of the bytes in the master
 41147 ** journal name, where each byte is interpreted as a signed 8-bit integer.
 41148 **
 41149 ** If zMaster is a NULL pointer (occurs for a single database transaction), 
 41150 ** this call is a no-op.
 41151 */
 41152 static int writeMasterJournal(Pager *pPager, const char *zMaster){
 41153   int rc;                          /* Return code */
 41154   int nMaster;                     /* Length of string zMaster */
 41155   i64 iHdrOff;                     /* Offset of header in journal file */
 41156   i64 jrnlSize;                    /* Size of journal file on disk */
 41157   u32 cksum = 0;                   /* Checksum of string zMaster */
 41159   assert( pPager->setMaster==0 );
 41160   assert( !pagerUseWal(pPager) );
 41162   if( !zMaster 
 41163    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 41164    || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 41165   ){
 41166     return SQLITE_OK;
 41168   pPager->setMaster = 1;
 41169   assert( isOpen(pPager->jfd) );
 41170   assert( pPager->journalHdr <= pPager->journalOff );
 41172   /* Calculate the length in bytes and the checksum of zMaster */
 41173   for(nMaster=0; zMaster[nMaster]; nMaster++){
 41174     cksum += zMaster[nMaster];
 41177   /* If in full-sync mode, advance to the next disk sector before writing
 41178   ** the master journal name. This is in case the previous page written to
 41179   ** the journal has already been synced.
 41180   */
 41181   if( pPager->fullSync ){
 41182     pPager->journalOff = journalHdrOffset(pPager);
 41184   iHdrOff = pPager->journalOff;
 41186   /* Write the master journal data to the end of the journal file. If
 41187   ** an error occurs, return the error code to the caller.
 41188   */
 41189   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
 41190    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
 41191    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
 41192    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
 41193    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
 41194   ){
 41195     return rc;
 41197   pPager->journalOff += (nMaster+20);
 41199   /* If the pager is in peristent-journal mode, then the physical 
 41200   ** journal-file may extend past the end of the master-journal name
 41201   ** and 8 bytes of magic data just written to the file. This is 
 41202   ** dangerous because the code to rollback a hot-journal file
 41203   ** will not be able to find the master-journal name to determine 
 41204   ** whether or not the journal is hot. 
 41205   **
 41206   ** Easiest thing to do in this scenario is to truncate the journal 
 41207   ** file to the required size.
 41208   */ 
 41209   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
 41210    && jrnlSize>pPager->journalOff
 41211   ){
 41212     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
 41214   return rc;
 41217 /*
 41218 ** Find a page in the hash table given its page number. Return
 41219 ** a pointer to the page or NULL if the requested page is not 
 41220 ** already in memory.
 41221 */
 41222 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
 41223   PgHdr *p = 0;                     /* Return value */
 41225   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
 41226   ** fail, since no attempt to allocate dynamic memory will be made.
 41227   */
 41228   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
 41229   return p;
 41232 /*
 41233 ** Discard the entire contents of the in-memory page-cache.
 41234 */
 41235 static void pager_reset(Pager *pPager){
 41236   sqlite3BackupRestart(pPager->pBackup);
 41237   sqlite3PcacheClear(pPager->pPCache);
 41240 /*
 41241 ** Free all structures in the Pager.aSavepoint[] array and set both
 41242 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
 41243 ** if it is open and the pager is not in exclusive mode.
 41244 */
 41245 static void releaseAllSavepoints(Pager *pPager){
 41246   int ii;               /* Iterator for looping through Pager.aSavepoint */
 41247   for(ii=0; ii<pPager->nSavepoint; ii++){
 41248     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 41250   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
 41251     sqlite3OsClose(pPager->sjfd);
 41253   sqlite3_free(pPager->aSavepoint);
 41254   pPager->aSavepoint = 0;
 41255   pPager->nSavepoint = 0;
 41256   pPager->nSubRec = 0;
 41259 /*
 41260 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
 41261 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
 41262 ** or SQLITE_NOMEM if a malloc failure occurs.
 41263 */
 41264 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
 41265   int ii;                   /* Loop counter */
 41266   int rc = SQLITE_OK;       /* Result code */
 41268   for(ii=0; ii<pPager->nSavepoint; ii++){
 41269     PagerSavepoint *p = &pPager->aSavepoint[ii];
 41270     if( pgno<=p->nOrig ){
 41271       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
 41272       testcase( rc==SQLITE_NOMEM );
 41273       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 41276   return rc;
 41279 /*
 41280 ** This function is a no-op if the pager is in exclusive mode and not
 41281 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
 41282 ** state.
 41283 **
 41284 ** If the pager is not in exclusive-access mode, the database file is
 41285 ** completely unlocked. If the file is unlocked and the file-system does
 41286 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
 41287 ** closed (if it is open).
 41288 **
 41289 ** If the pager is in ERROR state when this function is called, the 
 41290 ** contents of the pager cache are discarded before switching back to 
 41291 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
 41292 ** or not, any journal file left in the file-system will be treated
 41293 ** as a hot-journal and rolled back the next time a read-transaction
 41294 ** is opened (by this or by any other connection).
 41295 */
 41296 static void pager_unlock(Pager *pPager){
 41298   assert( pPager->eState==PAGER_READER 
 41299        || pPager->eState==PAGER_OPEN 
 41300        || pPager->eState==PAGER_ERROR 
 41301   );
 41303   sqlite3BitvecDestroy(pPager->pInJournal);
 41304   pPager->pInJournal = 0;
 41305   releaseAllSavepoints(pPager);
 41307   if( pagerUseWal(pPager) ){
 41308     assert( !isOpen(pPager->jfd) );
 41309     sqlite3WalEndReadTransaction(pPager->pWal);
 41310     pPager->eState = PAGER_OPEN;
 41311   }else if( !pPager->exclusiveMode ){
 41312     int rc;                       /* Error code returned by pagerUnlockDb() */
 41313     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
 41315     /* If the operating system support deletion of open files, then
 41316     ** close the journal file when dropping the database lock.  Otherwise
 41317     ** another connection with journal_mode=delete might delete the file
 41318     ** out from under us.
 41319     */
 41320     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
 41321     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
 41322     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
 41323     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
 41324     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 41325     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
 41326     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
 41327      || 1!=(pPager->journalMode & 5)
 41328     ){
 41329       sqlite3OsClose(pPager->jfd);
 41332     /* If the pager is in the ERROR state and the call to unlock the database
 41333     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
 41334     ** above the #define for UNKNOWN_LOCK for an explanation of why this
 41335     ** is necessary.
 41336     */
 41337     rc = pagerUnlockDb(pPager, NO_LOCK);
 41338     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
 41339       pPager->eLock = UNKNOWN_LOCK;
 41342     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
 41343     ** without clearing the error code. This is intentional - the error
 41344     ** code is cleared and the cache reset in the block below.
 41345     */
 41346     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
 41347     pPager->changeCountDone = 0;
 41348     pPager->eState = PAGER_OPEN;
 41351   /* If Pager.errCode is set, the contents of the pager cache cannot be
 41352   ** trusted. Now that there are no outstanding references to the pager,
 41353   ** it can safely move back to PAGER_OPEN state. This happens in both
 41354   ** normal and exclusive-locking mode.
 41355   */
 41356   if( pPager->errCode ){
 41357     assert( !MEMDB );
 41358     pager_reset(pPager);
 41359     pPager->changeCountDone = pPager->tempFile;
 41360     pPager->eState = PAGER_OPEN;
 41361     pPager->errCode = SQLITE_OK;
 41362     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
 41365   pPager->journalOff = 0;
 41366   pPager->journalHdr = 0;
 41367   pPager->setMaster = 0;
 41370 /*
 41371 ** This function is called whenever an IOERR or FULL error that requires
 41372 ** the pager to transition into the ERROR state may ahve occurred.
 41373 ** The first argument is a pointer to the pager structure, the second 
 41374 ** the error-code about to be returned by a pager API function. The 
 41375 ** value returned is a copy of the second argument to this function. 
 41376 **
 41377 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
 41378 ** IOERR sub-codes, the pager enters the ERROR state and the error code
 41379 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
 41380 ** all major API calls on the Pager will immediately return Pager.errCode.
 41381 **
 41382 ** The ERROR state indicates that the contents of the pager-cache 
 41383 ** cannot be trusted. This state can be cleared by completely discarding 
 41384 ** the contents of the pager-cache. If a transaction was active when
 41385 ** the persistent error occurred, then the rollback journal may need
 41386 ** to be replayed to restore the contents of the database file (as if
 41387 ** it were a hot-journal).
 41388 */
 41389 static int pager_error(Pager *pPager, int rc){
 41390   int rc2 = rc & 0xff;
 41391   assert( rc==SQLITE_OK || !MEMDB );
 41392   assert(
 41393        pPager->errCode==SQLITE_FULL ||
 41394        pPager->errCode==SQLITE_OK ||
 41395        (pPager->errCode & 0xff)==SQLITE_IOERR
 41396   );
 41397   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
 41398     pPager->errCode = rc;
 41399     pPager->eState = PAGER_ERROR;
 41401   return rc;
 41404 static int pager_truncate(Pager *pPager, Pgno nPage);
 41406 /*
 41407 ** This routine ends a transaction. A transaction is usually ended by 
 41408 ** either a COMMIT or a ROLLBACK operation. This routine may be called 
 41409 ** after rollback of a hot-journal, or if an error occurs while opening
 41410 ** the journal file or writing the very first journal-header of a
 41411 ** database transaction.
 41412 ** 
 41413 ** This routine is never called in PAGER_ERROR state. If it is called
 41414 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
 41415 ** exclusive than a RESERVED lock, it is a no-op.
 41416 **
 41417 ** Otherwise, any active savepoints are released.
 41418 **
 41419 ** If the journal file is open, then it is "finalized". Once a journal 
 41420 ** file has been finalized it is not possible to use it to roll back a 
 41421 ** transaction. Nor will it be considered to be a hot-journal by this
 41422 ** or any other database connection. Exactly how a journal is finalized
 41423 ** depends on whether or not the pager is running in exclusive mode and
 41424 ** the current journal-mode (Pager.journalMode value), as follows:
 41425 **
 41426 **   journalMode==MEMORY
 41427 **     Journal file descriptor is simply closed. This destroys an 
 41428 **     in-memory journal.
 41429 **
 41430 **   journalMode==TRUNCATE
 41431 **     Journal file is truncated to zero bytes in size.
 41432 **
 41433 **   journalMode==PERSIST
 41434 **     The first 28 bytes of the journal file are zeroed. This invalidates
 41435 **     the first journal header in the file, and hence the entire journal
 41436 **     file. An invalid journal file cannot be rolled back.
 41437 **
 41438 **   journalMode==DELETE
 41439 **     The journal file is closed and deleted using sqlite3OsDelete().
 41440 **
 41441 **     If the pager is running in exclusive mode, this method of finalizing
 41442 **     the journal file is never used. Instead, if the journalMode is
 41443 **     DELETE and the pager is in exclusive mode, the method described under
 41444 **     journalMode==PERSIST is used instead.
 41445 **
 41446 ** After the journal is finalized, the pager moves to PAGER_READER state.
 41447 ** If running in non-exclusive rollback mode, the lock on the file is 
 41448 ** downgraded to a SHARED_LOCK.
 41449 **
 41450 ** SQLITE_OK is returned if no error occurs. If an error occurs during
 41451 ** any of the IO operations to finalize the journal file or unlock the
 41452 ** database then the IO error code is returned to the user. If the 
 41453 ** operation to finalize the journal file fails, then the code still
 41454 ** tries to unlock the database file if not in exclusive mode. If the
 41455 ** unlock operation fails as well, then the first error code related
 41456 ** to the first error encountered (the journal finalization one) is
 41457 ** returned.
 41458 */
 41459 static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
 41460   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
 41461   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
 41463   /* Do nothing if the pager does not have an open write transaction
 41464   ** or at least a RESERVED lock. This function may be called when there
 41465   ** is no write-transaction active but a RESERVED or greater lock is
 41466   ** held under two circumstances:
 41467   **
 41468   **   1. After a successful hot-journal rollback, it is called with
 41469   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
 41470   **
 41471   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
 41472   **      lock switches back to locking_mode=normal and then executes a
 41473   **      read-transaction, this function is called with eState==PAGER_READER 
 41474   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
 41475   */
 41476   assert( assert_pager_state(pPager) );
 41477   assert( pPager->eState!=PAGER_ERROR );
 41478   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
 41479     return SQLITE_OK;
 41482   releaseAllSavepoints(pPager);
 41483   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
 41484   if( isOpen(pPager->jfd) ){
 41485     assert( !pagerUseWal(pPager) );
 41487     /* Finalize the journal file. */
 41488     if( sqlite3IsMemJournal(pPager->jfd) ){
 41489       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
 41490       sqlite3OsClose(pPager->jfd);
 41491     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
 41492       if( pPager->journalOff==0 ){
 41493         rc = SQLITE_OK;
 41494       }else{
 41495         rc = sqlite3OsTruncate(pPager->jfd, 0);
 41497       pPager->journalOff = 0;
 41498     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 41499       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
 41500     ){
 41501       rc = zeroJournalHdr(pPager, hasMaster);
 41502       pPager->journalOff = 0;
 41503     }else{
 41504       /* This branch may be executed with Pager.journalMode==MEMORY if
 41505       ** a hot-journal was just rolled back. In this case the journal
 41506       ** file should be closed and deleted. If this connection writes to
 41507       ** the database file, it will do so using an in-memory journal. 
 41508       */
 41509       int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
 41510       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
 41511            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
 41512            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 41513       );
 41514       sqlite3OsClose(pPager->jfd);
 41515       if( bDelete ){
 41516         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 41521 #ifdef SQLITE_CHECK_PAGES
 41522   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
 41523   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
 41524     PgHdr *p = pager_lookup(pPager, 1);
 41525     if( p ){
 41526       p->pageHash = 0;
 41527       sqlite3PagerUnrefNotNull(p);
 41530 #endif
 41532   sqlite3BitvecDestroy(pPager->pInJournal);
 41533   pPager->pInJournal = 0;
 41534   pPager->nRec = 0;
 41535   sqlite3PcacheCleanAll(pPager->pPCache);
 41536   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
 41538   if( pagerUseWal(pPager) ){
 41539     /* Drop the WAL write-lock, if any. Also, if the connection was in 
 41540     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
 41541     ** lock held on the database file.
 41542     */
 41543     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
 41544     assert( rc2==SQLITE_OK );
 41545   }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
 41546     /* This branch is taken when committing a transaction in rollback-journal
 41547     ** mode if the database file on disk is larger than the database image.
 41548     ** At this point the journal has been finalized and the transaction 
 41549     ** successfully committed, but the EXCLUSIVE lock is still held on the
 41550     ** file. So it is safe to truncate the database file to its minimum
 41551     ** required size.  */
 41552     assert( pPager->eLock==EXCLUSIVE_LOCK );
 41553     rc = pager_truncate(pPager, pPager->dbSize);
 41556   if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
 41557     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
 41558     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 41561   if( !pPager->exclusiveMode 
 41562    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 41563   ){
 41564     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
 41565     pPager->changeCountDone = 0;
 41567   pPager->eState = PAGER_READER;
 41568   pPager->setMaster = 0;
 41570   return (rc==SQLITE_OK?rc2:rc);
 41573 /*
 41574 ** Execute a rollback if a transaction is active and unlock the 
 41575 ** database file. 
 41576 **
 41577 ** If the pager has already entered the ERROR state, do not attempt 
 41578 ** the rollback at this time. Instead, pager_unlock() is called. The
 41579 ** call to pager_unlock() will discard all in-memory pages, unlock
 41580 ** the database file and move the pager back to OPEN state. If this 
 41581 ** means that there is a hot-journal left in the file-system, the next 
 41582 ** connection to obtain a shared lock on the pager (which may be this one) 
 41583 ** will roll it back.
 41584 **
 41585 ** If the pager has not already entered the ERROR state, but an IO or
 41586 ** malloc error occurs during a rollback, then this will itself cause 
 41587 ** the pager to enter the ERROR state. Which will be cleared by the
 41588 ** call to pager_unlock(), as described above.
 41589 */
 41590 static void pagerUnlockAndRollback(Pager *pPager){
 41591   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
 41592     assert( assert_pager_state(pPager) );
 41593     if( pPager->eState>=PAGER_WRITER_LOCKED ){
 41594       sqlite3BeginBenignMalloc();
 41595       sqlite3PagerRollback(pPager);
 41596       sqlite3EndBenignMalloc();
 41597     }else if( !pPager->exclusiveMode ){
 41598       assert( pPager->eState==PAGER_READER );
 41599       pager_end_transaction(pPager, 0, 0);
 41602   pager_unlock(pPager);
 41605 /*
 41606 ** Parameter aData must point to a buffer of pPager->pageSize bytes
 41607 ** of data. Compute and return a checksum based ont the contents of the 
 41608 ** page of data and the current value of pPager->cksumInit.
 41609 **
 41610 ** This is not a real checksum. It is really just the sum of the 
 41611 ** random initial value (pPager->cksumInit) and every 200th byte
 41612 ** of the page data, starting with byte offset (pPager->pageSize%200).
 41613 ** Each byte is interpreted as an 8-bit unsigned integer.
 41614 **
 41615 ** Changing the formula used to compute this checksum results in an
 41616 ** incompatible journal file format.
 41617 **
 41618 ** If journal corruption occurs due to a power failure, the most likely 
 41619 ** scenario is that one end or the other of the record will be changed. 
 41620 ** It is much less likely that the two ends of the journal record will be
 41621 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
 41622 ** though fast and simple, catches the mostly likely kind of corruption.
 41623 */
 41624 static u32 pager_cksum(Pager *pPager, const u8 *aData){
 41625   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
 41626   int i = pPager->pageSize-200;          /* Loop counter */
 41627   while( i>0 ){
 41628     cksum += aData[i];
 41629     i -= 200;
 41631   return cksum;
 41634 /*
 41635 ** Report the current page size and number of reserved bytes back
 41636 ** to the codec.
 41637 */
 41638 #ifdef SQLITE_HAS_CODEC
 41639 static void pagerReportSize(Pager *pPager){
 41640   if( pPager->xCodecSizeChng ){
 41641     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
 41642                            (int)pPager->nReserve);
 41645 #else
 41646 # define pagerReportSize(X)     /* No-op if we do not support a codec */
 41647 #endif
 41649 /*
 41650 ** Read a single page from either the journal file (if isMainJrnl==1) or
 41651 ** from the sub-journal (if isMainJrnl==0) and playback that page.
 41652 ** The page begins at offset *pOffset into the file. The *pOffset
 41653 ** value is increased to the start of the next page in the journal.
 41654 **
 41655 ** The main rollback journal uses checksums - the statement journal does 
 41656 ** not.
 41657 **
 41658 ** If the page number of the page record read from the (sub-)journal file
 41659 ** is greater than the current value of Pager.dbSize, then playback is
 41660 ** skipped and SQLITE_OK is returned.
 41661 **
 41662 ** If pDone is not NULL, then it is a record of pages that have already
 41663 ** been played back.  If the page at *pOffset has already been played back
 41664 ** (if the corresponding pDone bit is set) then skip the playback.
 41665 ** Make sure the pDone bit corresponding to the *pOffset page is set
 41666 ** prior to returning.
 41667 **
 41668 ** If the page record is successfully read from the (sub-)journal file
 41669 ** and played back, then SQLITE_OK is returned. If an IO error occurs
 41670 ** while reading the record from the (sub-)journal file or while writing
 41671 ** to the database file, then the IO error code is returned. If data
 41672 ** is successfully read from the (sub-)journal file but appears to be
 41673 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
 41674 ** two circumstances:
 41675 ** 
 41676 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
 41677 **   * If the record is being rolled back from the main journal file
 41678 **     and the checksum field does not match the record content.
 41679 **
 41680 ** Neither of these two scenarios are possible during a savepoint rollback.
 41681 **
 41682 ** If this is a savepoint rollback, then memory may have to be dynamically
 41683 ** allocated by this function. If this is the case and an allocation fails,
 41684 ** SQLITE_NOMEM is returned.
 41685 */
 41686 static int pager_playback_one_page(
 41687   Pager *pPager,                /* The pager being played back */
 41688   i64 *pOffset,                 /* Offset of record to playback */
 41689   Bitvec *pDone,                /* Bitvec of pages already played back */
 41690   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
 41691   int isSavepnt                 /* True for a savepoint rollback */
 41692 ){
 41693   int rc;
 41694   PgHdr *pPg;                   /* An existing page in the cache */
 41695   Pgno pgno;                    /* The page number of a page in journal */
 41696   u32 cksum;                    /* Checksum used for sanity checking */
 41697   char *aData;                  /* Temporary storage for the page */
 41698   sqlite3_file *jfd;            /* The file descriptor for the journal file */
 41699   int isSynced;                 /* True if journal page is synced */
 41701   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
 41702   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
 41703   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
 41704   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
 41706   aData = pPager->pTmpSpace;
 41707   assert( aData );         /* Temp storage must have already been allocated */
 41708   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
 41710   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction 
 41711   ** or savepoint rollback done at the request of the caller) or this is
 41712   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
 41713   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
 41714   ** only reads from the main journal, not the sub-journal.
 41715   */
 41716   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
 41717        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
 41718   );
 41719   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
 41721   /* Read the page number and page data from the journal or sub-journal
 41722   ** file. Return an error code to the caller if an IO error occurs.
 41723   */
 41724   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
 41725   rc = read32bits(jfd, *pOffset, &pgno);
 41726   if( rc!=SQLITE_OK ) return rc;
 41727   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
 41728   if( rc!=SQLITE_OK ) return rc;
 41729   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
 41731   /* Sanity checking on the page.  This is more important that I originally
 41732   ** thought.  If a power failure occurs while the journal is being written,
 41733   ** it could cause invalid data to be written into the journal.  We need to
 41734   ** detect this invalid data (with high probability) and ignore it.
 41735   */
 41736   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
 41737     assert( !isSavepnt );
 41738     return SQLITE_DONE;
 41740   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
 41741     return SQLITE_OK;
 41743   if( isMainJrnl ){
 41744     rc = read32bits(jfd, (*pOffset)-4, &cksum);
 41745     if( rc ) return rc;
 41746     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
 41747       return SQLITE_DONE;
 41751   /* If this page has already been played by before during the current
 41752   ** rollback, then don't bother to play it back again.
 41753   */
 41754   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
 41755     return rc;
 41758   /* When playing back page 1, restore the nReserve setting
 41759   */
 41760   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
 41761     pPager->nReserve = ((u8*)aData)[20];
 41762     pagerReportSize(pPager);
 41765   /* If the pager is in CACHEMOD state, then there must be a copy of this
 41766   ** page in the pager cache. In this case just update the pager cache,
 41767   ** not the database file. The page is left marked dirty in this case.
 41768   **
 41769   ** An exception to the above rule: If the database is in no-sync mode
 41770   ** and a page is moved during an incremental vacuum then the page may
 41771   ** not be in the pager cache. Later: if a malloc() or IO error occurs
 41772   ** during a Movepage() call, then the page may not be in the cache
 41773   ** either. So the condition described in the above paragraph is not
 41774   ** assert()able.
 41775   **
 41776   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
 41777   ** pager cache if it exists and the main file. The page is then marked 
 41778   ** not dirty. Since this code is only executed in PAGER_OPEN state for
 41779   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
 41780   ** if the pager is in OPEN state.
 41781   **
 41782   ** Ticket #1171:  The statement journal might contain page content that is
 41783   ** different from the page content at the start of the transaction.
 41784   ** This occurs when a page is changed prior to the start of a statement
 41785   ** then changed again within the statement.  When rolling back such a
 41786   ** statement we must not write to the original database unless we know
 41787   ** for certain that original page contents are synced into the main rollback
 41788   ** journal.  Otherwise, a power loss might leave modified data in the
 41789   ** database file without an entry in the rollback journal that can
 41790   ** restore the database to its original form.  Two conditions must be
 41791   ** met before writing to the database files. (1) the database must be
 41792   ** locked.  (2) we know that the original page content is fully synced
 41793   ** in the main journal either because the page is not in cache or else
 41794   ** the page is marked as needSync==0.
 41795   **
 41796   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
 41797   ** is possible to fail a statement on a database that does not yet exist.
 41798   ** Do not attempt to write if database file has never been opened.
 41799   */
 41800   if( pagerUseWal(pPager) ){
 41801     pPg = 0;
 41802   }else{
 41803     pPg = pager_lookup(pPager, pgno);
 41805   assert( pPg || !MEMDB );
 41806   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
 41807   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
 41808            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
 41809            (isMainJrnl?"main-journal":"sub-journal")
 41810   ));
 41811   if( isMainJrnl ){
 41812     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
 41813   }else{
 41814     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
 41816   if( isOpen(pPager->fd)
 41817    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 41818    && isSynced
 41819   ){
 41820     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
 41821     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
 41822     assert( !pagerUseWal(pPager) );
 41823     rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
 41824     if( pgno>pPager->dbFileSize ){
 41825       pPager->dbFileSize = pgno;
 41827     if( pPager->pBackup ){
 41828       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
 41829       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
 41830       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
 41832   }else if( !isMainJrnl && pPg==0 ){
 41833     /* If this is a rollback of a savepoint and data was not written to
 41834     ** the database and the page is not in-memory, there is a potential
 41835     ** problem. When the page is next fetched by the b-tree layer, it 
 41836     ** will be read from the database file, which may or may not be 
 41837     ** current. 
 41838     **
 41839     ** There are a couple of different ways this can happen. All are quite
 41840     ** obscure. When running in synchronous mode, this can only happen 
 41841     ** if the page is on the free-list at the start of the transaction, then
 41842     ** populated, then moved using sqlite3PagerMovepage().
 41843     **
 41844     ** The solution is to add an in-memory page to the cache containing
 41845     ** the data just read from the sub-journal. Mark the page as dirty 
 41846     ** and if the pager requires a journal-sync, then mark the page as 
 41847     ** requiring a journal-sync before it is written.
 41848     */
 41849     assert( isSavepnt );
 41850     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
 41851     pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
 41852     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
 41853     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
 41854     pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
 41855     if( rc!=SQLITE_OK ) return rc;
 41856     pPg->flags &= ~PGHDR_NEED_READ;
 41857     sqlite3PcacheMakeDirty(pPg);
 41859   if( pPg ){
 41860     /* No page should ever be explicitly rolled back that is in use, except
 41861     ** for page 1 which is held in use in order to keep the lock on the
 41862     ** database active. However such a page may be rolled back as a result
 41863     ** of an internal error resulting in an automatic call to
 41864     ** sqlite3PagerRollback().
 41865     */
 41866     void *pData;
 41867     pData = pPg->pData;
 41868     memcpy(pData, (u8*)aData, pPager->pageSize);
 41869     pPager->xReiniter(pPg);
 41870     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
 41871       /* If the contents of this page were just restored from the main 
 41872       ** journal file, then its content must be as they were when the 
 41873       ** transaction was first opened. In this case we can mark the page
 41874       ** as clean, since there will be no need to write it out to the
 41875       ** database.
 41876       **
 41877       ** There is one exception to this rule. If the page is being rolled
 41878       ** back as part of a savepoint (or statement) rollback from an 
 41879       ** unsynced portion of the main journal file, then it is not safe
 41880       ** to mark the page as clean. This is because marking the page as
 41881       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
 41882       ** already in the journal file (recorded in Pager.pInJournal) and
 41883       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
 41884       ** again within this transaction, it will be marked as dirty but
 41885       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
 41886       ** be written out into the database file before its journal file
 41887       ** segment is synced. If a crash occurs during or following this,
 41888       ** database corruption may ensue.
 41889       */
 41890       assert( !pagerUseWal(pPager) );
 41891       sqlite3PcacheMakeClean(pPg);
 41893     pager_set_pagehash(pPg);
 41895     /* If this was page 1, then restore the value of Pager.dbFileVers.
 41896     ** Do this before any decoding. */
 41897     if( pgno==1 ){
 41898       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
 41901     /* Decode the page just read from disk */
 41902     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
 41903     sqlite3PcacheRelease(pPg);
 41905   return rc;
 41908 /*
 41909 ** Parameter zMaster is the name of a master journal file. A single journal
 41910 ** file that referred to the master journal file has just been rolled back.
 41911 ** This routine checks if it is possible to delete the master journal file,
 41912 ** and does so if it is.
 41913 **
 41914 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
 41915 ** available for use within this function.
 41916 **
 41917 ** When a master journal file is created, it is populated with the names 
 41918 ** of all of its child journals, one after another, formatted as utf-8 
 41919 ** encoded text. The end of each child journal file is marked with a 
 41920 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
 41921 ** file for a transaction involving two databases might be:
 41922 **
 41923 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
 41924 **
 41925 ** A master journal file may only be deleted once all of its child 
 41926 ** journals have been rolled back.
 41927 **
 41928 ** This function reads the contents of the master-journal file into 
 41929 ** memory and loops through each of the child journal names. For
 41930 ** each child journal, it checks if:
 41931 **
 41932 **   * if the child journal exists, and if so
 41933 **   * if the child journal contains a reference to master journal 
 41934 **     file zMaster
 41935 **
 41936 ** If a child journal can be found that matches both of the criteria
 41937 ** above, this function returns without doing anything. Otherwise, if
 41938 ** no such child journal can be found, file zMaster is deleted from
 41939 ** the file-system using sqlite3OsDelete().
 41940 **
 41941 ** If an IO error within this function, an error code is returned. This
 41942 ** function allocates memory by calling sqlite3Malloc(). If an allocation
 41943 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
 41944 ** occur, SQLITE_OK is returned.
 41945 **
 41946 ** TODO: This function allocates a single block of memory to load
 41947 ** the entire contents of the master journal file. This could be
 41948 ** a couple of kilobytes or so - potentially larger than the page 
 41949 ** size.
 41950 */
 41951 static int pager_delmaster(Pager *pPager, const char *zMaster){
 41952   sqlite3_vfs *pVfs = pPager->pVfs;
 41953   int rc;                   /* Return code */
 41954   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
 41955   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
 41956   char *zMasterJournal = 0; /* Contents of master journal file */
 41957   i64 nMasterJournal;       /* Size of master journal file */
 41958   char *zJournal;           /* Pointer to one journal within MJ file */
 41959   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
 41960   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
 41962   /* Allocate space for both the pJournal and pMaster file descriptors.
 41963   ** If successful, open the master journal file for reading.
 41964   */
 41965   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
 41966   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
 41967   if( !pMaster ){
 41968     rc = SQLITE_NOMEM;
 41969   }else{
 41970     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
 41971     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
 41973   if( rc!=SQLITE_OK ) goto delmaster_out;
 41975   /* Load the entire master journal file into space obtained from
 41976   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
 41977   ** sufficient space (in zMasterPtr) to hold the names of master
 41978   ** journal files extracted from regular rollback-journals.
 41979   */
 41980   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
 41981   if( rc!=SQLITE_OK ) goto delmaster_out;
 41982   nMasterPtr = pVfs->mxPathname+1;
 41983   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
 41984   if( !zMasterJournal ){
 41985     rc = SQLITE_NOMEM;
 41986     goto delmaster_out;
 41988   zMasterPtr = &zMasterJournal[nMasterJournal+1];
 41989   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
 41990   if( rc!=SQLITE_OK ) goto delmaster_out;
 41991   zMasterJournal[nMasterJournal] = 0;
 41993   zJournal = zMasterJournal;
 41994   while( (zJournal-zMasterJournal)<nMasterJournal ){
 41995     int exists;
 41996     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
 41997     if( rc!=SQLITE_OK ){
 41998       goto delmaster_out;
 42000     if( exists ){
 42001       /* One of the journals pointed to by the master journal exists.
 42002       ** Open it and check if it points at the master journal. If
 42003       ** so, return without deleting the master journal file.
 42004       */
 42005       int c;
 42006       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
 42007       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
 42008       if( rc!=SQLITE_OK ){
 42009         goto delmaster_out;
 42012       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
 42013       sqlite3OsClose(pJournal);
 42014       if( rc!=SQLITE_OK ){
 42015         goto delmaster_out;
 42018       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
 42019       if( c ){
 42020         /* We have a match. Do not delete the master journal file. */
 42021         goto delmaster_out;
 42024     zJournal += (sqlite3Strlen30(zJournal)+1);
 42027   sqlite3OsClose(pMaster);
 42028   rc = sqlite3OsDelete(pVfs, zMaster, 0);
 42030 delmaster_out:
 42031   sqlite3_free(zMasterJournal);
 42032   if( pMaster ){
 42033     sqlite3OsClose(pMaster);
 42034     assert( !isOpen(pJournal) );
 42035     sqlite3_free(pMaster);
 42037   return rc;
 42041 /*
 42042 ** This function is used to change the actual size of the database 
 42043 ** file in the file-system. This only happens when committing a transaction,
 42044 ** or rolling back a transaction (including rolling back a hot-journal).
 42045 **
 42046 ** If the main database file is not open, or the pager is not in either
 42047 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
 42048 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
 42049 ** If the file on disk is currently larger than nPage pages, then use the VFS
 42050 ** xTruncate() method to truncate it.
 42051 **
 42052 ** Or, it might might be the case that the file on disk is smaller than 
 42053 ** nPage pages. Some operating system implementations can get confused if 
 42054 ** you try to truncate a file to some size that is larger than it 
 42055 ** currently is, so detect this case and write a single zero byte to 
 42056 ** the end of the new file instead.
 42057 **
 42058 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
 42059 ** the database file, return the error code to the caller.
 42060 */
 42061 static int pager_truncate(Pager *pPager, Pgno nPage){
 42062   int rc = SQLITE_OK;
 42063   assert( pPager->eState!=PAGER_ERROR );
 42064   assert( pPager->eState!=PAGER_READER );
 42066   if( isOpen(pPager->fd) 
 42067    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 
 42068   ){
 42069     i64 currentSize, newSize;
 42070     int szPage = pPager->pageSize;
 42071     assert( pPager->eLock==EXCLUSIVE_LOCK );
 42072     /* TODO: Is it safe to use Pager.dbFileSize here? */
 42073     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
 42074     newSize = szPage*(i64)nPage;
 42075     if( rc==SQLITE_OK && currentSize!=newSize ){
 42076       if( currentSize>newSize ){
 42077         rc = sqlite3OsTruncate(pPager->fd, newSize);
 42078       }else if( (currentSize+szPage)<=newSize ){
 42079         char *pTmp = pPager->pTmpSpace;
 42080         memset(pTmp, 0, szPage);
 42081         testcase( (newSize-szPage) == currentSize );
 42082         testcase( (newSize-szPage) >  currentSize );
 42083         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
 42085       if( rc==SQLITE_OK ){
 42086         pPager->dbFileSize = nPage;
 42090   return rc;
 42093 /*
 42094 ** Return a sanitized version of the sector-size of OS file pFile. The
 42095 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
 42096 */
 42097 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
 42098   int iRet = sqlite3OsSectorSize(pFile);
 42099   if( iRet<32 ){
 42100     iRet = 512;
 42101   }else if( iRet>MAX_SECTOR_SIZE ){
 42102     assert( MAX_SECTOR_SIZE>=512 );
 42103     iRet = MAX_SECTOR_SIZE;
 42105   return iRet;
 42108 /*
 42109 ** Set the value of the Pager.sectorSize variable for the given
 42110 ** pager based on the value returned by the xSectorSize method
 42111 ** of the open database file. The sector size will be used used 
 42112 ** to determine the size and alignment of journal header and 
 42113 ** master journal pointers within created journal files.
 42114 **
 42115 ** For temporary files the effective sector size is always 512 bytes.
 42116 **
 42117 ** Otherwise, for non-temporary files, the effective sector size is
 42118 ** the value returned by the xSectorSize() method rounded up to 32 if
 42119 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
 42120 ** is greater than MAX_SECTOR_SIZE.
 42121 **
 42122 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
 42123 ** the effective sector size to its minimum value (512).  The purpose of
 42124 ** pPager->sectorSize is to define the "blast radius" of bytes that
 42125 ** might change if a crash occurs while writing to a single byte in
 42126 ** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
 42127 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
 42128 ** size.  For backwards compatibility of the rollback journal file format,
 42129 ** we cannot reduce the effective sector size below 512.
 42130 */
 42131 static void setSectorSize(Pager *pPager){
 42132   assert( isOpen(pPager->fd) || pPager->tempFile );
 42134   if( pPager->tempFile
 42135    || (sqlite3OsDeviceCharacteristics(pPager->fd) & 
 42136               SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
 42137   ){
 42138     /* Sector size doesn't matter for temporary files. Also, the file
 42139     ** may not have been opened yet, in which case the OsSectorSize()
 42140     ** call will segfault. */
 42141     pPager->sectorSize = 512;
 42142   }else{
 42143     pPager->sectorSize = sqlite3SectorSize(pPager->fd);
 42147 /*
 42148 ** Playback the journal and thus restore the database file to
 42149 ** the state it was in before we started making changes.  
 42150 **
 42151 ** The journal file format is as follows: 
 42152 **
 42153 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
 42154 **  (2)  4 byte big-endian integer which is the number of valid page records
 42155 **       in the journal.  If this value is 0xffffffff, then compute the
 42156 **       number of page records from the journal size.
 42157 **  (3)  4 byte big-endian integer which is the initial value for the 
 42158 **       sanity checksum.
 42159 **  (4)  4 byte integer which is the number of pages to truncate the
 42160 **       database to during a rollback.
 42161 **  (5)  4 byte big-endian integer which is the sector size.  The header
 42162 **       is this many bytes in size.
 42163 **  (6)  4 byte big-endian integer which is the page size.
 42164 **  (7)  zero padding out to the next sector size.
 42165 **  (8)  Zero or more pages instances, each as follows:
 42166 **        +  4 byte page number.
 42167 **        +  pPager->pageSize bytes of data.
 42168 **        +  4 byte checksum
 42169 **
 42170 ** When we speak of the journal header, we mean the first 7 items above.
 42171 ** Each entry in the journal is an instance of the 8th item.
 42172 **
 42173 ** Call the value from the second bullet "nRec".  nRec is the number of
 42174 ** valid page entries in the journal.  In most cases, you can compute the
 42175 ** value of nRec from the size of the journal file.  But if a power
 42176 ** failure occurred while the journal was being written, it could be the
 42177 ** case that the size of the journal file had already been increased but
 42178 ** the extra entries had not yet made it safely to disk.  In such a case,
 42179 ** the value of nRec computed from the file size would be too large.  For
 42180 ** that reason, we always use the nRec value in the header.
 42181 **
 42182 ** If the nRec value is 0xffffffff it means that nRec should be computed
 42183 ** from the file size.  This value is used when the user selects the
 42184 ** no-sync option for the journal.  A power failure could lead to corruption
 42185 ** in this case.  But for things like temporary table (which will be
 42186 ** deleted when the power is restored) we don't care.  
 42187 **
 42188 ** If the file opened as the journal file is not a well-formed
 42189 ** journal file then all pages up to the first corrupted page are rolled
 42190 ** back (or no pages if the journal header is corrupted). The journal file
 42191 ** is then deleted and SQLITE_OK returned, just as if no corruption had
 42192 ** been encountered.
 42193 **
 42194 ** If an I/O or malloc() error occurs, the journal-file is not deleted
 42195 ** and an error code is returned.
 42196 **
 42197 ** The isHot parameter indicates that we are trying to rollback a journal
 42198 ** that might be a hot journal.  Or, it could be that the journal is 
 42199 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
 42200 ** If the journal really is hot, reset the pager cache prior rolling
 42201 ** back any content.  If the journal is merely persistent, no reset is
 42202 ** needed.
 42203 */
 42204 static int pager_playback(Pager *pPager, int isHot){
 42205   sqlite3_vfs *pVfs = pPager->pVfs;
 42206   i64 szJ;                 /* Size of the journal file in bytes */
 42207   u32 nRec;                /* Number of Records in the journal */
 42208   u32 u;                   /* Unsigned loop counter */
 42209   Pgno mxPg = 0;           /* Size of the original file in pages */
 42210   int rc;                  /* Result code of a subroutine */
 42211   int res = 1;             /* Value returned by sqlite3OsAccess() */
 42212   char *zMaster = 0;       /* Name of master journal file if any */
 42213   int needPagerReset;      /* True to reset page prior to first page rollback */
 42214   int nPlayback = 0;       /* Total number of pages restored from journal */
 42216   /* Figure out how many records are in the journal.  Abort early if
 42217   ** the journal is empty.
 42218   */
 42219   assert( isOpen(pPager->jfd) );
 42220   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
 42221   if( rc!=SQLITE_OK ){
 42222     goto end_playback;
 42225   /* Read the master journal name from the journal, if it is present.
 42226   ** If a master journal file name is specified, but the file is not
 42227   ** present on disk, then the journal is not hot and does not need to be
 42228   ** played back.
 42229   **
 42230   ** TODO: Technically the following is an error because it assumes that
 42231   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
 42232   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
 42233   **  mxPathname is 512, which is the same as the minimum allowable value
 42234   ** for pageSize.
 42235   */
 42236   zMaster = pPager->pTmpSpace;
 42237   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 42238   if( rc==SQLITE_OK && zMaster[0] ){
 42239     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 42241   zMaster = 0;
 42242   if( rc!=SQLITE_OK || !res ){
 42243     goto end_playback;
 42245   pPager->journalOff = 0;
 42246   needPagerReset = isHot;
 42248   /* This loop terminates either when a readJournalHdr() or 
 42249   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
 42250   ** occurs. 
 42251   */
 42252   while( 1 ){
 42253     /* Read the next journal header from the journal file.  If there are
 42254     ** not enough bytes left in the journal file for a complete header, or
 42255     ** it is corrupted, then a process must have failed while writing it.
 42256     ** This indicates nothing more needs to be rolled back.
 42257     */
 42258     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
 42259     if( rc!=SQLITE_OK ){ 
 42260       if( rc==SQLITE_DONE ){
 42261         rc = SQLITE_OK;
 42263       goto end_playback;
 42266     /* If nRec is 0xffffffff, then this journal was created by a process
 42267     ** working in no-sync mode. This means that the rest of the journal
 42268     ** file consists of pages, there are no more journal headers. Compute
 42269     ** the value of nRec based on this assumption.
 42270     */
 42271     if( nRec==0xffffffff ){
 42272       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
 42273       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
 42276     /* If nRec is 0 and this rollback is of a transaction created by this
 42277     ** process and if this is the final header in the journal, then it means
 42278     ** that this part of the journal was being filled but has not yet been
 42279     ** synced to disk.  Compute the number of pages based on the remaining
 42280     ** size of the file.
 42281     **
 42282     ** The third term of the test was added to fix ticket #2565.
 42283     ** When rolling back a hot journal, nRec==0 always means that the next
 42284     ** chunk of the journal contains zero pages to be rolled back.  But
 42285     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
 42286     ** the journal, it means that the journal might contain additional
 42287     ** pages that need to be rolled back and that the number of pages 
 42288     ** should be computed based on the journal file size.
 42289     */
 42290     if( nRec==0 && !isHot &&
 42291         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
 42292       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
 42295     /* If this is the first header read from the journal, truncate the
 42296     ** database file back to its original size.
 42297     */
 42298     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
 42299       rc = pager_truncate(pPager, mxPg);
 42300       if( rc!=SQLITE_OK ){
 42301         goto end_playback;
 42303       pPager->dbSize = mxPg;
 42306     /* Copy original pages out of the journal and back into the 
 42307     ** database file and/or page cache.
 42308     */
 42309     for(u=0; u<nRec; u++){
 42310       if( needPagerReset ){
 42311         pager_reset(pPager);
 42312         needPagerReset = 0;
 42314       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
 42315       if( rc==SQLITE_OK ){
 42316         nPlayback++;
 42317       }else{
 42318         if( rc==SQLITE_DONE ){
 42319           pPager->journalOff = szJ;
 42320           break;
 42321         }else if( rc==SQLITE_IOERR_SHORT_READ ){
 42322           /* If the journal has been truncated, simply stop reading and
 42323           ** processing the journal. This might happen if the journal was
 42324           ** not completely written and synced prior to a crash.  In that
 42325           ** case, the database should have never been written in the
 42326           ** first place so it is OK to simply abandon the rollback. */
 42327           rc = SQLITE_OK;
 42328           goto end_playback;
 42329         }else{
 42330           /* If we are unable to rollback, quit and return the error
 42331           ** code.  This will cause the pager to enter the error state
 42332           ** so that no further harm will be done.  Perhaps the next
 42333           ** process to come along will be able to rollback the database.
 42334           */
 42335           goto end_playback;
 42340   /*NOTREACHED*/
 42341   assert( 0 );
 42343 end_playback:
 42344   /* Following a rollback, the database file should be back in its original
 42345   ** state prior to the start of the transaction, so invoke the
 42346   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 42347   ** assertion that the transaction counter was modified.
 42348   */
 42349 #ifdef SQLITE_DEBUG
 42350   if( pPager->fd->pMethods ){
 42351     sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 42353 #endif
 42355   /* If this playback is happening automatically as a result of an IO or 
 42356   ** malloc error that occurred after the change-counter was updated but 
 42357   ** before the transaction was committed, then the change-counter 
 42358   ** modification may just have been reverted. If this happens in exclusive 
 42359   ** mode, then subsequent transactions performed by the connection will not
 42360   ** update the change-counter at all. This may lead to cache inconsistency
 42361   ** problems for other processes at some point in the future. So, just
 42362   ** in case this has happened, clear the changeCountDone flag now.
 42363   */
 42364   pPager->changeCountDone = pPager->tempFile;
 42366   if( rc==SQLITE_OK ){
 42367     zMaster = pPager->pTmpSpace;
 42368     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
 42369     testcase( rc!=SQLITE_OK );
 42371   if( rc==SQLITE_OK
 42372    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
 42373   ){
 42374     rc = sqlite3PagerSync(pPager, 0);
 42376   if( rc==SQLITE_OK ){
 42377     rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
 42378     testcase( rc!=SQLITE_OK );
 42380   if( rc==SQLITE_OK && zMaster[0] && res ){
 42381     /* If there was a master journal and this routine will return success,
 42382     ** see if it is possible to delete the master journal.
 42383     */
 42384     rc = pager_delmaster(pPager, zMaster);
 42385     testcase( rc!=SQLITE_OK );
 42387   if( isHot && nPlayback ){
 42388     sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
 42389                 nPlayback, pPager->zJournal);
 42392   /* The Pager.sectorSize variable may have been updated while rolling
 42393   ** back a journal created by a process with a different sector size
 42394   ** value. Reset it to the correct value for this process.
 42395   */
 42396   setSectorSize(pPager);
 42397   return rc;
 42401 /*
 42402 ** Read the content for page pPg out of the database file and into 
 42403 ** pPg->pData. A shared lock or greater must be held on the database
 42404 ** file before this function is called.
 42405 **
 42406 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
 42407 ** the value read from the database file.
 42408 **
 42409 ** If an IO error occurs, then the IO error is returned to the caller.
 42410 ** Otherwise, SQLITE_OK is returned.
 42411 */
 42412 static int readDbPage(PgHdr *pPg, u32 iFrame){
 42413   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
 42414   Pgno pgno = pPg->pgno;       /* Page number to read */
 42415   int rc = SQLITE_OK;          /* Return code */
 42416   int pgsz = pPager->pageSize; /* Number of bytes to read */
 42418   assert( pPager->eState>=PAGER_READER && !MEMDB );
 42419   assert( isOpen(pPager->fd) );
 42421 #ifndef SQLITE_OMIT_WAL
 42422   if( iFrame ){
 42423     /* Try to pull the page from the write-ahead log. */
 42424     rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
 42425   }else
 42426 #endif
 42428     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
 42429     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
 42430     if( rc==SQLITE_IOERR_SHORT_READ ){
 42431       rc = SQLITE_OK;
 42435   if( pgno==1 ){
 42436     if( rc ){
 42437       /* If the read is unsuccessful, set the dbFileVers[] to something
 42438       ** that will never be a valid file version.  dbFileVers[] is a copy
 42439       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
 42440       ** zero or the size of the database in page. Bytes 32..35 and 35..39
 42441       ** should be page numbers which are never 0xffffffff.  So filling
 42442       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
 42443       **
 42444       ** For an encrypted database, the situation is more complex:  bytes
 42445       ** 24..39 of the database are white noise.  But the probability of
 42446       ** white noising equaling 16 bytes of 0xff is vanishingly small so
 42447       ** we should still be ok.
 42448       */
 42449       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
 42450     }else{
 42451       u8 *dbFileVers = &((u8*)pPg->pData)[24];
 42452       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
 42455   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
 42457   PAGER_INCR(sqlite3_pager_readdb_count);
 42458   PAGER_INCR(pPager->nRead);
 42459   IOTRACE(("PGIN %p %d\n", pPager, pgno));
 42460   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
 42461                PAGERID(pPager), pgno, pager_pagehash(pPg)));
 42463   return rc;
 42466 /*
 42467 ** Update the value of the change-counter at offsets 24 and 92 in
 42468 ** the header and the sqlite version number at offset 96.
 42469 **
 42470 ** This is an unconditional update.  See also the pager_incr_changecounter()
 42471 ** routine which only updates the change-counter if the update is actually
 42472 ** needed, as determined by the pPager->changeCountDone state variable.
 42473 */
 42474 static void pager_write_changecounter(PgHdr *pPg){
 42475   u32 change_counter;
 42477   /* Increment the value just read and write it back to byte 24. */
 42478   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
 42479   put32bits(((char*)pPg->pData)+24, change_counter);
 42481   /* Also store the SQLite version number in bytes 96..99 and in
 42482   ** bytes 92..95 store the change counter for which the version number
 42483   ** is valid. */
 42484   put32bits(((char*)pPg->pData)+92, change_counter);
 42485   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
 42488 #ifndef SQLITE_OMIT_WAL
 42489 /*
 42490 ** This function is invoked once for each page that has already been 
 42491 ** written into the log file when a WAL transaction is rolled back.
 42492 ** Parameter iPg is the page number of said page. The pCtx argument 
 42493 ** is actually a pointer to the Pager structure.
 42494 **
 42495 ** If page iPg is present in the cache, and has no outstanding references,
 42496 ** it is discarded. Otherwise, if there are one or more outstanding
 42497 ** references, the page content is reloaded from the database. If the
 42498 ** attempt to reload content from the database is required and fails, 
 42499 ** return an SQLite error code. Otherwise, SQLITE_OK.
 42500 */
 42501 static int pagerUndoCallback(void *pCtx, Pgno iPg){
 42502   int rc = SQLITE_OK;
 42503   Pager *pPager = (Pager *)pCtx;
 42504   PgHdr *pPg;
 42506   assert( pagerUseWal(pPager) );
 42507   pPg = sqlite3PagerLookup(pPager, iPg);
 42508   if( pPg ){
 42509     if( sqlite3PcachePageRefcount(pPg)==1 ){
 42510       sqlite3PcacheDrop(pPg);
 42511     }else{
 42512       u32 iFrame = 0;
 42513       rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
 42514       if( rc==SQLITE_OK ){
 42515         rc = readDbPage(pPg, iFrame);
 42517       if( rc==SQLITE_OK ){
 42518         pPager->xReiniter(pPg);
 42520       sqlite3PagerUnrefNotNull(pPg);
 42524   /* Normally, if a transaction is rolled back, any backup processes are
 42525   ** updated as data is copied out of the rollback journal and into the
 42526   ** database. This is not generally possible with a WAL database, as
 42527   ** rollback involves simply truncating the log file. Therefore, if one
 42528   ** or more frames have already been written to the log (and therefore 
 42529   ** also copied into the backup databases) as part of this transaction,
 42530   ** the backups must be restarted.
 42531   */
 42532   sqlite3BackupRestart(pPager->pBackup);
 42534   return rc;
 42537 /*
 42538 ** This function is called to rollback a transaction on a WAL database.
 42539 */
 42540 static int pagerRollbackWal(Pager *pPager){
 42541   int rc;                         /* Return Code */
 42542   PgHdr *pList;                   /* List of dirty pages to revert */
 42544   /* For all pages in the cache that are currently dirty or have already
 42545   ** been written (but not committed) to the log file, do one of the 
 42546   ** following:
 42547   **
 42548   **   + Discard the cached page (if refcount==0), or
 42549   **   + Reload page content from the database (if refcount>0).
 42550   */
 42551   pPager->dbSize = pPager->dbOrigSize;
 42552   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
 42553   pList = sqlite3PcacheDirtyList(pPager->pPCache);
 42554   while( pList && rc==SQLITE_OK ){
 42555     PgHdr *pNext = pList->pDirty;
 42556     rc = pagerUndoCallback((void *)pPager, pList->pgno);
 42557     pList = pNext;
 42560   return rc;
 42563 /*
 42564 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
 42565 ** the contents of the list of pages headed by pList (connected by pDirty),
 42566 ** this function notifies any active backup processes that the pages have
 42567 ** changed. 
 42568 **
 42569 ** The list of pages passed into this routine is always sorted by page number.
 42570 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
 42571 */ 
 42572 static int pagerWalFrames(
 42573   Pager *pPager,                  /* Pager object */
 42574   PgHdr *pList,                   /* List of frames to log */
 42575   Pgno nTruncate,                 /* Database size after this commit */
 42576   int isCommit                    /* True if this is a commit */
 42577 ){
 42578   int rc;                         /* Return code */
 42579   int nList;                      /* Number of pages in pList */
 42580 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
 42581   PgHdr *p;                       /* For looping over pages */
 42582 #endif
 42584   assert( pPager->pWal );
 42585   assert( pList );
 42586 #ifdef SQLITE_DEBUG
 42587   /* Verify that the page list is in accending order */
 42588   for(p=pList; p && p->pDirty; p=p->pDirty){
 42589     assert( p->pgno < p->pDirty->pgno );
 42591 #endif
 42593   assert( pList->pDirty==0 || isCommit );
 42594   if( isCommit ){
 42595     /* If a WAL transaction is being committed, there is no point in writing
 42596     ** any pages with page numbers greater than nTruncate into the WAL file.
 42597     ** They will never be read by any client. So remove them from the pDirty
 42598     ** list here. */
 42599     PgHdr *p;
 42600     PgHdr **ppNext = &pList;
 42601     nList = 0;
 42602     for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
 42603       if( p->pgno<=nTruncate ){
 42604         ppNext = &p->pDirty;
 42605         nList++;
 42608     assert( pList );
 42609   }else{
 42610     nList = 1;
 42612   pPager->aStat[PAGER_STAT_WRITE] += nList;
 42614   if( pList->pgno==1 ) pager_write_changecounter(pList);
 42615   rc = sqlite3WalFrames(pPager->pWal, 
 42616       pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
 42617   );
 42618   if( rc==SQLITE_OK && pPager->pBackup ){
 42619     PgHdr *p;
 42620     for(p=pList; p; p=p->pDirty){
 42621       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
 42625 #ifdef SQLITE_CHECK_PAGES
 42626   pList = sqlite3PcacheDirtyList(pPager->pPCache);
 42627   for(p=pList; p; p=p->pDirty){
 42628     pager_set_pagehash(p);
 42630 #endif
 42632   return rc;
 42635 /*
 42636 ** Begin a read transaction on the WAL.
 42637 **
 42638 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
 42639 ** makes a snapshot of the database at the current point in time and preserves
 42640 ** that snapshot for use by the reader in spite of concurrently changes by
 42641 ** other writers or checkpointers.
 42642 */
 42643 static int pagerBeginReadTransaction(Pager *pPager){
 42644   int rc;                         /* Return code */
 42645   int changed = 0;                /* True if cache must be reset */
 42647   assert( pagerUseWal(pPager) );
 42648   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 42650   /* sqlite3WalEndReadTransaction() was not called for the previous
 42651   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
 42652   ** are in locking_mode=NORMAL and EndRead() was previously called,
 42653   ** the duplicate call is harmless.
 42654   */
 42655   sqlite3WalEndReadTransaction(pPager->pWal);
 42657   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
 42658   if( rc!=SQLITE_OK || changed ){
 42659     pager_reset(pPager);
 42660     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
 42663   return rc;
 42665 #endif
 42667 /*
 42668 ** This function is called as part of the transition from PAGER_OPEN
 42669 ** to PAGER_READER state to determine the size of the database file
 42670 ** in pages (assuming the page size currently stored in Pager.pageSize).
 42671 **
 42672 ** If no error occurs, SQLITE_OK is returned and the size of the database
 42673 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
 42674 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
 42675 */
 42676 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
 42677   Pgno nPage;                     /* Value to return via *pnPage */
 42679   /* Query the WAL sub-system for the database size. The WalDbsize()
 42680   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
 42681   ** if the database size is not available. The database size is not
 42682   ** available from the WAL sub-system if the log file is empty or
 42683   ** contains no valid committed transactions.
 42684   */
 42685   assert( pPager->eState==PAGER_OPEN );
 42686   assert( pPager->eLock>=SHARED_LOCK );
 42687   nPage = sqlite3WalDbsize(pPager->pWal);
 42689   /* If the database size was not available from the WAL sub-system,
 42690   ** determine it based on the size of the database file. If the size
 42691   ** of the database file is not an integer multiple of the page-size,
 42692   ** round down to the nearest page. Except, any file larger than 0
 42693   ** bytes in size is considered to contain at least one page.
 42694   */
 42695   if( nPage==0 ){
 42696     i64 n = 0;                    /* Size of db file in bytes */
 42697     assert( isOpen(pPager->fd) || pPager->tempFile );
 42698     if( isOpen(pPager->fd) ){
 42699       int rc = sqlite3OsFileSize(pPager->fd, &n);
 42700       if( rc!=SQLITE_OK ){
 42701         return rc;
 42704     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
 42707   /* If the current number of pages in the file is greater than the
 42708   ** configured maximum pager number, increase the allowed limit so
 42709   ** that the file can be read.
 42710   */
 42711   if( nPage>pPager->mxPgno ){
 42712     pPager->mxPgno = (Pgno)nPage;
 42715   *pnPage = nPage;
 42716   return SQLITE_OK;
 42719 #ifndef SQLITE_OMIT_WAL
 42720 /*
 42721 ** Check if the *-wal file that corresponds to the database opened by pPager
 42722 ** exists if the database is not empy, or verify that the *-wal file does
 42723 ** not exist (by deleting it) if the database file is empty.
 42724 **
 42725 ** If the database is not empty and the *-wal file exists, open the pager
 42726 ** in WAL mode.  If the database is empty or if no *-wal file exists and
 42727 ** if no error occurs, make sure Pager.journalMode is not set to
 42728 ** PAGER_JOURNALMODE_WAL.
 42729 **
 42730 ** Return SQLITE_OK or an error code.
 42731 **
 42732 ** The caller must hold a SHARED lock on the database file to call this
 42733 ** function. Because an EXCLUSIVE lock on the db file is required to delete 
 42734 ** a WAL on a none-empty database, this ensures there is no race condition 
 42735 ** between the xAccess() below and an xDelete() being executed by some 
 42736 ** other connection.
 42737 */
 42738 static int pagerOpenWalIfPresent(Pager *pPager){
 42739   int rc = SQLITE_OK;
 42740   assert( pPager->eState==PAGER_OPEN );
 42741   assert( pPager->eLock>=SHARED_LOCK );
 42743   if( !pPager->tempFile ){
 42744     int isWal;                    /* True if WAL file exists */
 42745     Pgno nPage;                   /* Size of the database file */
 42747     rc = pagerPagecount(pPager, &nPage);
 42748     if( rc ) return rc;
 42749     if( nPage==0 ){
 42750       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
 42751       if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
 42752       isWal = 0;
 42753     }else{
 42754       rc = sqlite3OsAccess(
 42755           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
 42756       );
 42758     if( rc==SQLITE_OK ){
 42759       if( isWal ){
 42760         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
 42761         rc = sqlite3PagerOpenWal(pPager, 0);
 42762       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
 42763         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
 42767   return rc;
 42769 #endif
 42771 /*
 42772 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
 42773 ** the entire master journal file. The case pSavepoint==NULL occurs when 
 42774 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
 42775 ** savepoint.
 42776 **
 42777 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
 42778 ** being rolled back), then the rollback consists of up to three stages,
 42779 ** performed in the order specified:
 42780 **
 42781 **   * Pages are played back from the main journal starting at byte
 42782 **     offset PagerSavepoint.iOffset and continuing to 
 42783 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
 42784 **     file if PagerSavepoint.iHdrOffset is zero.
 42785 **
 42786 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
 42787 **     back starting from the journal header immediately following 
 42788 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
 42789 **
 42790 **   * Pages are then played back from the sub-journal file, starting
 42791 **     with the PagerSavepoint.iSubRec and continuing to the end of
 42792 **     the journal file.
 42793 **
 42794 ** Throughout the rollback process, each time a page is rolled back, the
 42795 ** corresponding bit is set in a bitvec structure (variable pDone in the
 42796 ** implementation below). This is used to ensure that a page is only
 42797 ** rolled back the first time it is encountered in either journal.
 42798 **
 42799 ** If pSavepoint is NULL, then pages are only played back from the main
 42800 ** journal file. There is no need for a bitvec in this case.
 42801 **
 42802 ** In either case, before playback commences the Pager.dbSize variable
 42803 ** is reset to the value that it held at the start of the savepoint 
 42804 ** (or transaction). No page with a page-number greater than this value
 42805 ** is played back. If one is encountered it is simply skipped.
 42806 */
 42807 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
 42808   i64 szJ;                 /* Effective size of the main journal */
 42809   i64 iHdrOff;             /* End of first segment of main-journal records */
 42810   int rc = SQLITE_OK;      /* Return code */
 42811   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
 42813   assert( pPager->eState!=PAGER_ERROR );
 42814   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 42816   /* Allocate a bitvec to use to store the set of pages rolled back */
 42817   if( pSavepoint ){
 42818     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
 42819     if( !pDone ){
 42820       return SQLITE_NOMEM;
 42824   /* Set the database size back to the value it was before the savepoint 
 42825   ** being reverted was opened.
 42826   */
 42827   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
 42828   pPager->changeCountDone = pPager->tempFile;
 42830   if( !pSavepoint && pagerUseWal(pPager) ){
 42831     return pagerRollbackWal(pPager);
 42834   /* Use pPager->journalOff as the effective size of the main rollback
 42835   ** journal.  The actual file might be larger than this in
 42836   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
 42837   ** past pPager->journalOff is off-limits to us.
 42838   */
 42839   szJ = pPager->journalOff;
 42840   assert( pagerUseWal(pPager)==0 || szJ==0 );
 42842   /* Begin by rolling back records from the main journal starting at
 42843   ** PagerSavepoint.iOffset and continuing to the next journal header.
 42844   ** There might be records in the main journal that have a page number
 42845   ** greater than the current database size (pPager->dbSize) but those
 42846   ** will be skipped automatically.  Pages are added to pDone as they
 42847   ** are played back.
 42848   */
 42849   if( pSavepoint && !pagerUseWal(pPager) ){
 42850     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
 42851     pPager->journalOff = pSavepoint->iOffset;
 42852     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
 42853       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 42855     assert( rc!=SQLITE_DONE );
 42856   }else{
 42857     pPager->journalOff = 0;
 42860   /* Continue rolling back records out of the main journal starting at
 42861   ** the first journal header seen and continuing until the effective end
 42862   ** of the main journal file.  Continue to skip out-of-range pages and
 42863   ** continue adding pages rolled back to pDone.
 42864   */
 42865   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
 42866     u32 ii;            /* Loop counter */
 42867     u32 nJRec = 0;     /* Number of Journal Records */
 42868     u32 dummy;
 42869     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
 42870     assert( rc!=SQLITE_DONE );
 42872     /*
 42873     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
 42874     ** test is related to ticket #2565.  See the discussion in the
 42875     ** pager_playback() function for additional information.
 42876     */
 42877     if( nJRec==0 
 42878      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
 42879     ){
 42880       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
 42882     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
 42883       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
 42885     assert( rc!=SQLITE_DONE );
 42887   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
 42889   /* Finally,  rollback pages from the sub-journal.  Page that were
 42890   ** previously rolled back out of the main journal (and are hence in pDone)
 42891   ** will be skipped.  Out-of-range pages are also skipped.
 42892   */
 42893   if( pSavepoint ){
 42894     u32 ii;            /* Loop counter */
 42895     i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
 42897     if( pagerUseWal(pPager) ){
 42898       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
 42900     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
 42901       assert( offset==(i64)ii*(4+pPager->pageSize) );
 42902       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
 42904     assert( rc!=SQLITE_DONE );
 42907   sqlite3BitvecDestroy(pDone);
 42908   if( rc==SQLITE_OK ){
 42909     pPager->journalOff = szJ;
 42912   return rc;
 42915 /*
 42916 ** Change the maximum number of in-memory pages that are allowed.
 42917 */
 42918 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
 42919   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
 42922 /*
 42923 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
 42924 */
 42925 static void pagerFixMaplimit(Pager *pPager){
 42926 #if SQLITE_MAX_MMAP_SIZE>0
 42927   sqlite3_file *fd = pPager->fd;
 42928   if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
 42929     sqlite3_int64 sz;
 42930     sz = pPager->szMmap;
 42931     pPager->bUseFetch = (sz>0);
 42932     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
 42934 #endif
 42937 /*
 42938 ** Change the maximum size of any memory mapping made of the database file.
 42939 */
 42940 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
 42941   pPager->szMmap = szMmap;
 42942   pagerFixMaplimit(pPager);
 42945 /*
 42946 ** Free as much memory as possible from the pager.
 42947 */
 42948 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
 42949   sqlite3PcacheShrink(pPager->pPCache);
 42952 /*
 42953 ** Adjust settings of the pager to those specified in the pgFlags parameter.
 42954 **
 42955 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
 42956 ** of the database to damage due to OS crashes or power failures by
 42957 ** changing the number of syncs()s when writing the journals.
 42958 ** There are three levels:
 42959 **
 42960 **    OFF       sqlite3OsSync() is never called.  This is the default
 42961 **              for temporary and transient files.
 42962 **
 42963 **    NORMAL    The journal is synced once before writes begin on the
 42964 **              database.  This is normally adequate protection, but
 42965 **              it is theoretically possible, though very unlikely,
 42966 **              that an inopertune power failure could leave the journal
 42967 **              in a state which would cause damage to the database
 42968 **              when it is rolled back.
 42969 **
 42970 **    FULL      The journal is synced twice before writes begin on the
 42971 **              database (with some additional information - the nRec field
 42972 **              of the journal header - being written in between the two
 42973 **              syncs).  If we assume that writing a
 42974 **              single disk sector is atomic, then this mode provides
 42975 **              assurance that the journal will not be corrupted to the
 42976 **              point of causing damage to the database during rollback.
 42977 **
 42978 ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
 42979 ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
 42980 ** prior to the start of checkpoint and that the database file is synced
 42981 ** at the conclusion of the checkpoint if the entire content of the WAL
 42982 ** was written back into the database.  But no sync operations occur for
 42983 ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
 42984 ** file is synced following each commit operation, in addition to the
 42985 ** syncs associated with NORMAL.
 42986 **
 42987 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
 42988 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
 42989 ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
 42990 ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
 42991 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
 42992 ** synchronous=FULL versus synchronous=NORMAL setting determines when
 42993 ** the xSync primitive is called and is relevant to all platforms.
 42994 **
 42995 ** Numeric values associated with these states are OFF==1, NORMAL=2,
 42996 ** and FULL=3.
 42997 */
 42998 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 42999 SQLITE_PRIVATE void sqlite3PagerSetFlags(
 43000   Pager *pPager,        /* The pager to set safety level for */
 43001   unsigned pgFlags      /* Various flags */
 43002 ){
 43003   unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
 43004   assert( level>=1 && level<=3 );
 43005   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
 43006   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
 43007   if( pPager->noSync ){
 43008     pPager->syncFlags = 0;
 43009     pPager->ckptSyncFlags = 0;
 43010   }else if( pgFlags & PAGER_FULLFSYNC ){
 43011     pPager->syncFlags = SQLITE_SYNC_FULL;
 43012     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 43013   }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
 43014     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 43015     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
 43016   }else{
 43017     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 43018     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 43020   pPager->walSyncFlags = pPager->syncFlags;
 43021   if( pPager->fullSync ){
 43022     pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
 43024   if( pgFlags & PAGER_CACHESPILL ){
 43025     pPager->doNotSpill &= ~SPILLFLAG_OFF;
 43026   }else{
 43027     pPager->doNotSpill |= SPILLFLAG_OFF;
 43030 #endif
 43032 /*
 43033 ** The following global variable is incremented whenever the library
 43034 ** attempts to open a temporary file.  This information is used for
 43035 ** testing and analysis only.  
 43036 */
 43037 #ifdef SQLITE_TEST
 43038 SQLITE_API int sqlite3_opentemp_count = 0;
 43039 #endif
 43041 /*
 43042 ** Open a temporary file.
 43043 **
 43044 ** Write the file descriptor into *pFile. Return SQLITE_OK on success 
 43045 ** or some other error code if we fail. The OS will automatically 
 43046 ** delete the temporary file when it is closed.
 43047 **
 43048 ** The flags passed to the VFS layer xOpen() call are those specified
 43049 ** by parameter vfsFlags ORed with the following:
 43050 **
 43051 **     SQLITE_OPEN_READWRITE
 43052 **     SQLITE_OPEN_CREATE
 43053 **     SQLITE_OPEN_EXCLUSIVE
 43054 **     SQLITE_OPEN_DELETEONCLOSE
 43055 */
 43056 static int pagerOpentemp(
 43057   Pager *pPager,        /* The pager object */
 43058   sqlite3_file *pFile,  /* Write the file descriptor here */
 43059   int vfsFlags          /* Flags passed through to the VFS */
 43060 ){
 43061   int rc;               /* Return code */
 43063 #ifdef SQLITE_TEST
 43064   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
 43065 #endif
 43067   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
 43068             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
 43069   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
 43070   assert( rc!=SQLITE_OK || isOpen(pFile) );
 43071   return rc;
 43074 /*
 43075 ** Set the busy handler function.
 43076 **
 43077 ** The pager invokes the busy-handler if sqlite3OsLock() returns 
 43078 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
 43079 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
 43080 ** lock. It does *not* invoke the busy handler when upgrading from
 43081 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
 43082 ** (which occurs during hot-journal rollback). Summary:
 43083 **
 43084 **   Transition                        | Invokes xBusyHandler
 43085 **   --------------------------------------------------------
 43086 **   NO_LOCK       -> SHARED_LOCK      | Yes
 43087 **   SHARED_LOCK   -> RESERVED_LOCK    | No
 43088 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 43089 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 43090 **
 43091 ** If the busy-handler callback returns non-zero, the lock is 
 43092 ** retried. If it returns zero, then the SQLITE_BUSY error is
 43093 ** returned to the caller of the pager API function.
 43094 */
 43095 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
 43096   Pager *pPager,                       /* Pager object */
 43097   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 43098   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 43099 ){
 43100   pPager->xBusyHandler = xBusyHandler;
 43101   pPager->pBusyHandlerArg = pBusyHandlerArg;
 43103   if( isOpen(pPager->fd) ){
 43104     void **ap = (void **)&pPager->xBusyHandler;
 43105     assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 43106     assert( ap[1]==pBusyHandlerArg );
 43107     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 43111 /*
 43112 ** Change the page size used by the Pager object. The new page size 
 43113 ** is passed in *pPageSize.
 43114 **
 43115 ** If the pager is in the error state when this function is called, it
 43116 ** is a no-op. The value returned is the error state error code (i.e. 
 43117 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
 43118 **
 43119 ** Otherwise, if all of the following are true:
 43120 **
 43121 **   * the new page size (value of *pPageSize) is valid (a power 
 43122 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
 43123 **
 43124 **   * there are no outstanding page references, and
 43125 **
 43126 **   * the database is either not an in-memory database or it is
 43127 **     an in-memory database that currently consists of zero pages.
 43128 **
 43129 ** then the pager object page size is set to *pPageSize.
 43130 **
 43131 ** If the page size is changed, then this function uses sqlite3PagerMalloc() 
 43132 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
 43133 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
 43134 ** In all other cases, SQLITE_OK is returned.
 43135 **
 43136 ** If the page size is not changed, either because one of the enumerated
 43137 ** conditions above is not true, the pager was in error state when this
 43138 ** function was called, or because the memory allocation attempt failed, 
 43139 ** then *pPageSize is set to the old, retained page size before returning.
 43140 */
 43141 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
 43142   int rc = SQLITE_OK;
 43144   /* It is not possible to do a full assert_pager_state() here, as this
 43145   ** function may be called from within PagerOpen(), before the state
 43146   ** of the Pager object is internally consistent.
 43147   **
 43148   ** At one point this function returned an error if the pager was in 
 43149   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
 43150   ** there is at least one outstanding page reference, this function
 43151   ** is a no-op for that case anyhow.
 43152   */
 43154   u32 pageSize = *pPageSize;
 43155   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
 43156   if( (pPager->memDb==0 || pPager->dbSize==0)
 43157    && sqlite3PcacheRefCount(pPager->pPCache)==0 
 43158    && pageSize && pageSize!=(u32)pPager->pageSize 
 43159   ){
 43160     char *pNew = NULL;             /* New temp space */
 43161     i64 nByte = 0;
 43163     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
 43164       rc = sqlite3OsFileSize(pPager->fd, &nByte);
 43166     if( rc==SQLITE_OK ){
 43167       pNew = (char *)sqlite3PageMalloc(pageSize);
 43168       if( !pNew ) rc = SQLITE_NOMEM;
 43171     if( rc==SQLITE_OK ){
 43172       pager_reset(pPager);
 43173       pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
 43174       pPager->pageSize = pageSize;
 43175       sqlite3PageFree(pPager->pTmpSpace);
 43176       pPager->pTmpSpace = pNew;
 43177       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
 43181   *pPageSize = pPager->pageSize;
 43182   if( rc==SQLITE_OK ){
 43183     if( nReserve<0 ) nReserve = pPager->nReserve;
 43184     assert( nReserve>=0 && nReserve<1000 );
 43185     pPager->nReserve = (i16)nReserve;
 43186     pagerReportSize(pPager);
 43187     pagerFixMaplimit(pPager);
 43189   return rc;
 43192 /*
 43193 ** Return a pointer to the "temporary page" buffer held internally
 43194 ** by the pager.  This is a buffer that is big enough to hold the
 43195 ** entire content of a database page.  This buffer is used internally
 43196 ** during rollback and will be overwritten whenever a rollback
 43197 ** occurs.  But other modules are free to use it too, as long as
 43198 ** no rollbacks are happening.
 43199 */
 43200 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
 43201   return pPager->pTmpSpace;
 43204 /*
 43205 ** Attempt to set the maximum database page count if mxPage is positive. 
 43206 ** Make no changes if mxPage is zero or negative.  And never reduce the
 43207 ** maximum page count below the current size of the database.
 43208 **
 43209 ** Regardless of mxPage, return the current maximum page count.
 43210 */
 43211 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
 43212   if( mxPage>0 ){
 43213     pPager->mxPgno = mxPage;
 43215   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
 43216   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
 43217   return pPager->mxPgno;
 43220 /*
 43221 ** The following set of routines are used to disable the simulated
 43222 ** I/O error mechanism.  These routines are used to avoid simulated
 43223 ** errors in places where we do not care about errors.
 43224 **
 43225 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
 43226 ** and generate no code.
 43227 */
 43228 #ifdef SQLITE_TEST
 43229 SQLITE_API extern int sqlite3_io_error_pending;
 43230 SQLITE_API extern int sqlite3_io_error_hit;
 43231 static int saved_cnt;
 43232 void disable_simulated_io_errors(void){
 43233   saved_cnt = sqlite3_io_error_pending;
 43234   sqlite3_io_error_pending = -1;
 43236 void enable_simulated_io_errors(void){
 43237   sqlite3_io_error_pending = saved_cnt;
 43239 #else
 43240 # define disable_simulated_io_errors()
 43241 # define enable_simulated_io_errors()
 43242 #endif
 43244 /*
 43245 ** Read the first N bytes from the beginning of the file into memory
 43246 ** that pDest points to. 
 43247 **
 43248 ** If the pager was opened on a transient file (zFilename==""), or
 43249 ** opened on a file less than N bytes in size, the output buffer is
 43250 ** zeroed and SQLITE_OK returned. The rationale for this is that this 
 43251 ** function is used to read database headers, and a new transient or
 43252 ** zero sized database has a header than consists entirely of zeroes.
 43253 **
 43254 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
 43255 ** the error code is returned to the caller and the contents of the
 43256 ** output buffer undefined.
 43257 */
 43258 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
 43259   int rc = SQLITE_OK;
 43260   memset(pDest, 0, N);
 43261   assert( isOpen(pPager->fd) || pPager->tempFile );
 43263   /* This routine is only called by btree immediately after creating
 43264   ** the Pager object.  There has not been an opportunity to transition
 43265   ** to WAL mode yet.
 43266   */
 43267   assert( !pagerUseWal(pPager) );
 43269   if( isOpen(pPager->fd) ){
 43270     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
 43271     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
 43272     if( rc==SQLITE_IOERR_SHORT_READ ){
 43273       rc = SQLITE_OK;
 43276   return rc;
 43279 /*
 43280 ** This function may only be called when a read-transaction is open on
 43281 ** the pager. It returns the total number of pages in the database.
 43282 **
 43283 ** However, if the file is between 1 and <page-size> bytes in size, then 
 43284 ** this is considered a 1 page file.
 43285 */
 43286 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
 43287   assert( pPager->eState>=PAGER_READER );
 43288   assert( pPager->eState!=PAGER_WRITER_FINISHED );
 43289   *pnPage = (int)pPager->dbSize;
 43293 /*
 43294 ** Try to obtain a lock of type locktype on the database file. If
 43295 ** a similar or greater lock is already held, this function is a no-op
 43296 ** (returning SQLITE_OK immediately).
 43297 **
 43298 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
 43299 ** the busy callback if the lock is currently not available. Repeat 
 43300 ** until the busy callback returns false or until the attempt to 
 43301 ** obtain the lock succeeds.
 43302 **
 43303 ** Return SQLITE_OK on success and an error code if we cannot obtain
 43304 ** the lock. If the lock is obtained successfully, set the Pager.state 
 43305 ** variable to locktype before returning.
 43306 */
 43307 static int pager_wait_on_lock(Pager *pPager, int locktype){
 43308   int rc;                              /* Return code */
 43310   /* Check that this is either a no-op (because the requested lock is 
 43311   ** already held, or one of the transistions that the busy-handler
 43312   ** may be invoked during, according to the comment above
 43313   ** sqlite3PagerSetBusyhandler().
 43314   */
 43315   assert( (pPager->eLock>=locktype)
 43316        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
 43317        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
 43318   );
 43320   do {
 43321     rc = pagerLockDb(pPager, locktype);
 43322   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
 43323   return rc;
 43326 /*
 43327 ** Function assertTruncateConstraint(pPager) checks that one of the 
 43328 ** following is true for all dirty pages currently in the page-cache:
 43329 **
 43330 **   a) The page number is less than or equal to the size of the 
 43331 **      current database image, in pages, OR
 43332 **
 43333 **   b) if the page content were written at this time, it would not
 43334 **      be necessary to write the current content out to the sub-journal
 43335 **      (as determined by function subjRequiresPage()).
 43336 **
 43337 ** If the condition asserted by this function were not true, and the
 43338 ** dirty page were to be discarded from the cache via the pagerStress()
 43339 ** routine, pagerStress() would not write the current page content to
 43340 ** the database file. If a savepoint transaction were rolled back after
 43341 ** this happened, the correct behavior would be to restore the current
 43342 ** content of the page. However, since this content is not present in either
 43343 ** the database file or the portion of the rollback journal and 
 43344 ** sub-journal rolled back the content could not be restored and the
 43345 ** database image would become corrupt. It is therefore fortunate that 
 43346 ** this circumstance cannot arise.
 43347 */
 43348 #if defined(SQLITE_DEBUG)
 43349 static void assertTruncateConstraintCb(PgHdr *pPg){
 43350   assert( pPg->flags&PGHDR_DIRTY );
 43351   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
 43353 static void assertTruncateConstraint(Pager *pPager){
 43354   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
 43356 #else
 43357 # define assertTruncateConstraint(pPager)
 43358 #endif
 43360 /*
 43361 ** Truncate the in-memory database file image to nPage pages. This 
 43362 ** function does not actually modify the database file on disk. It 
 43363 ** just sets the internal state of the pager object so that the 
 43364 ** truncation will be done when the current transaction is committed.
 43365 **
 43366 ** This function is only called right before committing a transaction.
 43367 ** Once this function has been called, the transaction must either be
 43368 ** rolled back or committed. It is not safe to call this function and
 43369 ** then continue writing to the database.
 43370 */
 43371 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
 43372   assert( pPager->dbSize>=nPage );
 43373   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 43374   pPager->dbSize = nPage;
 43376   /* At one point the code here called assertTruncateConstraint() to
 43377   ** ensure that all pages being truncated away by this operation are,
 43378   ** if one or more savepoints are open, present in the savepoint 
 43379   ** journal so that they can be restored if the savepoint is rolled
 43380   ** back. This is no longer necessary as this function is now only
 43381   ** called right before committing a transaction. So although the 
 43382   ** Pager object may still have open savepoints (Pager.nSavepoint!=0), 
 43383   ** they cannot be rolled back. So the assertTruncateConstraint() call
 43384   ** is no longer correct. */
 43388 /*
 43389 ** This function is called before attempting a hot-journal rollback. It
 43390 ** syncs the journal file to disk, then sets pPager->journalHdr to the
 43391 ** size of the journal file so that the pager_playback() routine knows
 43392 ** that the entire journal file has been synced.
 43393 **
 43394 ** Syncing a hot-journal to disk before attempting to roll it back ensures 
 43395 ** that if a power-failure occurs during the rollback, the process that
 43396 ** attempts rollback following system recovery sees the same journal
 43397 ** content as this process.
 43398 **
 43399 ** If everything goes as planned, SQLITE_OK is returned. Otherwise, 
 43400 ** an SQLite error code.
 43401 */
 43402 static int pagerSyncHotJournal(Pager *pPager){
 43403   int rc = SQLITE_OK;
 43404   if( !pPager->noSync ){
 43405     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
 43407   if( rc==SQLITE_OK ){
 43408     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
 43410   return rc;
 43413 /*
 43414 ** Obtain a reference to a memory mapped page object for page number pgno. 
 43415 ** The new object will use the pointer pData, obtained from xFetch().
 43416 ** If successful, set *ppPage to point to the new page reference
 43417 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set
 43418 ** *ppPage to zero.
 43419 **
 43420 ** Page references obtained by calling this function should be released
 43421 ** by calling pagerReleaseMapPage().
 43422 */
 43423 static int pagerAcquireMapPage(
 43424   Pager *pPager,                  /* Pager object */
 43425   Pgno pgno,                      /* Page number */
 43426   void *pData,                    /* xFetch()'d data for this page */
 43427   PgHdr **ppPage                  /* OUT: Acquired page object */
 43428 ){
 43429   PgHdr *p;                       /* Memory mapped page to return */
 43431   if( pPager->pMmapFreelist ){
 43432     *ppPage = p = pPager->pMmapFreelist;
 43433     pPager->pMmapFreelist = p->pDirty;
 43434     p->pDirty = 0;
 43435     memset(p->pExtra, 0, pPager->nExtra);
 43436   }else{
 43437     *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
 43438     if( p==0 ){
 43439       sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
 43440       return SQLITE_NOMEM;
 43442     p->pExtra = (void *)&p[1];
 43443     p->flags = PGHDR_MMAP;
 43444     p->nRef = 1;
 43445     p->pPager = pPager;
 43448   assert( p->pExtra==(void *)&p[1] );
 43449   assert( p->pPage==0 );
 43450   assert( p->flags==PGHDR_MMAP );
 43451   assert( p->pPager==pPager );
 43452   assert( p->nRef==1 );
 43454   p->pgno = pgno;
 43455   p->pData = pData;
 43456   pPager->nMmapOut++;
 43458   return SQLITE_OK;
 43461 /*
 43462 ** Release a reference to page pPg. pPg must have been returned by an 
 43463 ** earlier call to pagerAcquireMapPage().
 43464 */
 43465 static void pagerReleaseMapPage(PgHdr *pPg){
 43466   Pager *pPager = pPg->pPager;
 43467   pPager->nMmapOut--;
 43468   pPg->pDirty = pPager->pMmapFreelist;
 43469   pPager->pMmapFreelist = pPg;
 43471   assert( pPager->fd->pMethods->iVersion>=3 );
 43472   sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
 43475 /*
 43476 ** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
 43477 */
 43478 static void pagerFreeMapHdrs(Pager *pPager){
 43479   PgHdr *p;
 43480   PgHdr *pNext;
 43481   for(p=pPager->pMmapFreelist; p; p=pNext){
 43482     pNext = p->pDirty;
 43483     sqlite3_free(p);
 43488 /*
 43489 ** Shutdown the page cache.  Free all memory and close all files.
 43490 **
 43491 ** If a transaction was in progress when this routine is called, that
 43492 ** transaction is rolled back.  All outstanding pages are invalidated
 43493 ** and their memory is freed.  Any attempt to use a page associated
 43494 ** with this page cache after this function returns will likely
 43495 ** result in a coredump.
 43496 **
 43497 ** This function always succeeds. If a transaction is active an attempt
 43498 ** is made to roll it back. If an error occurs during the rollback 
 43499 ** a hot journal may be left in the filesystem but no error is returned
 43500 ** to the caller.
 43501 */
 43502 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
 43503   u8 *pTmp = (u8 *)pPager->pTmpSpace;
 43505   assert( assert_pager_state(pPager) );
 43506   disable_simulated_io_errors();
 43507   sqlite3BeginBenignMalloc();
 43508   pagerFreeMapHdrs(pPager);
 43509   /* pPager->errCode = 0; */
 43510   pPager->exclusiveMode = 0;
 43511 #ifndef SQLITE_OMIT_WAL
 43512   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
 43513   pPager->pWal = 0;
 43514 #endif
 43515   pager_reset(pPager);
 43516   if( MEMDB ){
 43517     pager_unlock(pPager);
 43518   }else{
 43519     /* If it is open, sync the journal file before calling UnlockAndRollback.
 43520     ** If this is not done, then an unsynced portion of the open journal 
 43521     ** file may be played back into the database. If a power failure occurs 
 43522     ** while this is happening, the database could become corrupt.
 43523     **
 43524     ** If an error occurs while trying to sync the journal, shift the pager
 43525     ** into the ERROR state. This causes UnlockAndRollback to unlock the
 43526     ** database and close the journal file without attempting to roll it
 43527     ** back or finalize it. The next database user will have to do hot-journal
 43528     ** rollback before accessing the database file.
 43529     */
 43530     if( isOpen(pPager->jfd) ){
 43531       pager_error(pPager, pagerSyncHotJournal(pPager));
 43533     pagerUnlockAndRollback(pPager);
 43535   sqlite3EndBenignMalloc();
 43536   enable_simulated_io_errors();
 43537   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
 43538   IOTRACE(("CLOSE %p\n", pPager))
 43539   sqlite3OsClose(pPager->jfd);
 43540   sqlite3OsClose(pPager->fd);
 43541   sqlite3PageFree(pTmp);
 43542   sqlite3PcacheClose(pPager->pPCache);
 43544 #ifdef SQLITE_HAS_CODEC
 43545   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 43546 #endif
 43548   assert( !pPager->aSavepoint && !pPager->pInJournal );
 43549   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
 43551   sqlite3_free(pPager);
 43552   return SQLITE_OK;
 43555 #if !defined(NDEBUG) || defined(SQLITE_TEST)
 43556 /*
 43557 ** Return the page number for page pPg.
 43558 */
 43559 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
 43560   return pPg->pgno;
 43562 #endif
 43564 /*
 43565 ** Increment the reference count for page pPg.
 43566 */
 43567 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
 43568   sqlite3PcacheRef(pPg);
 43571 /*
 43572 ** Sync the journal. In other words, make sure all the pages that have
 43573 ** been written to the journal have actually reached the surface of the
 43574 ** disk and can be restored in the event of a hot-journal rollback.
 43575 **
 43576 ** If the Pager.noSync flag is set, then this function is a no-op.
 43577 ** Otherwise, the actions required depend on the journal-mode and the 
 43578 ** device characteristics of the file-system, as follows:
 43579 **
 43580 **   * If the journal file is an in-memory journal file, no action need
 43581 **     be taken.
 43582 **
 43583 **   * Otherwise, if the device does not support the SAFE_APPEND property,
 43584 **     then the nRec field of the most recently written journal header
 43585 **     is updated to contain the number of journal records that have
 43586 **     been written following it. If the pager is operating in full-sync
 43587 **     mode, then the journal file is synced before this field is updated.
 43588 **
 43589 **   * If the device does not support the SEQUENTIAL property, then 
 43590 **     journal file is synced.
 43591 **
 43592 ** Or, in pseudo-code:
 43593 **
 43594 **   if( NOT <in-memory journal> ){
 43595 **     if( NOT SAFE_APPEND ){
 43596 **       if( <full-sync mode> ) xSync(<journal file>);
 43597 **       <update nRec field>
 43598 **     } 
 43599 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
 43600 **   }
 43601 **
 43602 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
 43603 ** page currently held in memory before returning SQLITE_OK. If an IO
 43604 ** error is encountered, then the IO error code is returned to the caller.
 43605 */
 43606 static int syncJournal(Pager *pPager, int newHdr){
 43607   int rc;                         /* Return code */
 43609   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 43610        || pPager->eState==PAGER_WRITER_DBMOD
 43611   );
 43612   assert( assert_pager_state(pPager) );
 43613   assert( !pagerUseWal(pPager) );
 43615   rc = sqlite3PagerExclusiveLock(pPager);
 43616   if( rc!=SQLITE_OK ) return rc;
 43618   if( !pPager->noSync ){
 43619     assert( !pPager->tempFile );
 43620     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
 43621       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 43622       assert( isOpen(pPager->jfd) );
 43624       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 43625         /* This block deals with an obscure problem. If the last connection
 43626         ** that wrote to this database was operating in persistent-journal
 43627         ** mode, then the journal file may at this point actually be larger
 43628         ** than Pager.journalOff bytes. If the next thing in the journal
 43629         ** file happens to be a journal-header (written as part of the
 43630         ** previous connection's transaction), and a crash or power-failure 
 43631         ** occurs after nRec is updated but before this connection writes 
 43632         ** anything else to the journal file (or commits/rolls back its 
 43633         ** transaction), then SQLite may become confused when doing the 
 43634         ** hot-journal rollback following recovery. It may roll back all
 43635         ** of this connections data, then proceed to rolling back the old,
 43636         ** out-of-date data that follows it. Database corruption.
 43637         **
 43638         ** To work around this, if the journal file does appear to contain
 43639         ** a valid header following Pager.journalOff, then write a 0x00
 43640         ** byte to the start of it to prevent it from being recognized.
 43641         **
 43642         ** Variable iNextHdrOffset is set to the offset at which this
 43643         ** problematic header will occur, if it exists. aMagic is used 
 43644         ** as a temporary buffer to inspect the first couple of bytes of
 43645         ** the potential journal header.
 43646         */
 43647         i64 iNextHdrOffset;
 43648         u8 aMagic[8];
 43649         u8 zHeader[sizeof(aJournalMagic)+4];
 43651         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
 43652         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
 43654         iNextHdrOffset = journalHdrOffset(pPager);
 43655         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
 43656         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
 43657           static const u8 zerobyte = 0;
 43658           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
 43660         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
 43661           return rc;
 43664         /* Write the nRec value into the journal file header. If in
 43665         ** full-synchronous mode, sync the journal first. This ensures that
 43666         ** all data has really hit the disk before nRec is updated to mark
 43667         ** it as a candidate for rollback.
 43668         **
 43669         ** This is not required if the persistent media supports the
 43670         ** SAFE_APPEND property. Because in this case it is not possible 
 43671         ** for garbage data to be appended to the file, the nRec field
 43672         ** is populated with 0xFFFFFFFF when the journal header is written
 43673         ** and never needs to be updated.
 43674         */
 43675         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 43676           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 43677           IOTRACE(("JSYNC %p\n", pPager))
 43678           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
 43679           if( rc!=SQLITE_OK ) return rc;
 43681         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
 43682         rc = sqlite3OsWrite(
 43683             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
 43684         );
 43685         if( rc!=SQLITE_OK ) return rc;
 43687       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
 43688         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
 43689         IOTRACE(("JSYNC %p\n", pPager))
 43690         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
 43691           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
 43692         );
 43693         if( rc!=SQLITE_OK ) return rc;
 43696       pPager->journalHdr = pPager->journalOff;
 43697       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
 43698         pPager->nRec = 0;
 43699         rc = writeJournalHdr(pPager);
 43700         if( rc!=SQLITE_OK ) return rc;
 43702     }else{
 43703       pPager->journalHdr = pPager->journalOff;
 43707   /* Unless the pager is in noSync mode, the journal file was just 
 43708   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
 43709   ** all pages.
 43710   */
 43711   sqlite3PcacheClearSyncFlags(pPager->pPCache);
 43712   pPager->eState = PAGER_WRITER_DBMOD;
 43713   assert( assert_pager_state(pPager) );
 43714   return SQLITE_OK;
 43717 /*
 43718 ** The argument is the first in a linked list of dirty pages connected
 43719 ** by the PgHdr.pDirty pointer. This function writes each one of the
 43720 ** in-memory pages in the list to the database file. The argument may
 43721 ** be NULL, representing an empty list. In this case this function is
 43722 ** a no-op.
 43723 **
 43724 ** The pager must hold at least a RESERVED lock when this function
 43725 ** is called. Before writing anything to the database file, this lock
 43726 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
 43727 ** SQLITE_BUSY is returned and no data is written to the database file.
 43728 ** 
 43729 ** If the pager is a temp-file pager and the actual file-system file
 43730 ** is not yet open, it is created and opened before any data is 
 43731 ** written out.
 43732 **
 43733 ** Once the lock has been upgraded and, if necessary, the file opened,
 43734 ** the pages are written out to the database file in list order. Writing
 43735 ** a page is skipped if it meets either of the following criteria:
 43736 **
 43737 **   * The page number is greater than Pager.dbSize, or
 43738 **   * The PGHDR_DONT_WRITE flag is set on the page.
 43739 **
 43740 ** If writing out a page causes the database file to grow, Pager.dbFileSize
 43741 ** is updated accordingly. If page 1 is written out, then the value cached
 43742 ** in Pager.dbFileVers[] is updated to match the new value stored in
 43743 ** the database file.
 43744 **
 43745 ** If everything is successful, SQLITE_OK is returned. If an IO error 
 43746 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
 43747 ** be obtained, SQLITE_BUSY is returned.
 43748 */
 43749 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
 43750   int rc = SQLITE_OK;                  /* Return code */
 43752   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
 43753   assert( !pagerUseWal(pPager) );
 43754   assert( pPager->eState==PAGER_WRITER_DBMOD );
 43755   assert( pPager->eLock==EXCLUSIVE_LOCK );
 43757   /* If the file is a temp-file has not yet been opened, open it now. It
 43758   ** is not possible for rc to be other than SQLITE_OK if this branch
 43759   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
 43760   */
 43761   if( !isOpen(pPager->fd) ){
 43762     assert( pPager->tempFile && rc==SQLITE_OK );
 43763     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
 43766   /* Before the first write, give the VFS a hint of what the final
 43767   ** file size will be.
 43768   */
 43769   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
 43770   if( rc==SQLITE_OK 
 43771    && pPager->dbHintSize<pPager->dbSize
 43772    && (pList->pDirty || pList->pgno>pPager->dbHintSize)
 43773   ){
 43774     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
 43775     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
 43776     pPager->dbHintSize = pPager->dbSize;
 43779   while( rc==SQLITE_OK && pList ){
 43780     Pgno pgno = pList->pgno;
 43782     /* If there are dirty pages in the page cache with page numbers greater
 43783     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
 43784     ** make the file smaller (presumably by auto-vacuum code). Do not write
 43785     ** any such pages to the file.
 43786     **
 43787     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
 43788     ** set (set by sqlite3PagerDontWrite()).
 43789     */
 43790     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
 43791       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
 43792       char *pData;                                   /* Data to write */    
 43794       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
 43795       if( pList->pgno==1 ) pager_write_changecounter(pList);
 43797       /* Encode the database */
 43798       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
 43800       /* Write out the page data. */
 43801       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
 43803       /* If page 1 was just written, update Pager.dbFileVers to match
 43804       ** the value now stored in the database file. If writing this 
 43805       ** page caused the database file to grow, update dbFileSize. 
 43806       */
 43807       if( pgno==1 ){
 43808         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
 43810       if( pgno>pPager->dbFileSize ){
 43811         pPager->dbFileSize = pgno;
 43813       pPager->aStat[PAGER_STAT_WRITE]++;
 43815       /* Update any backup objects copying the contents of this pager. */
 43816       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
 43818       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
 43819                    PAGERID(pPager), pgno, pager_pagehash(pList)));
 43820       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
 43821       PAGER_INCR(sqlite3_pager_writedb_count);
 43822     }else{
 43823       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
 43825     pager_set_pagehash(pList);
 43826     pList = pList->pDirty;
 43829   return rc;
 43832 /*
 43833 ** Ensure that the sub-journal file is open. If it is already open, this 
 43834 ** function is a no-op.
 43835 **
 43836 ** SQLITE_OK is returned if everything goes according to plan. An 
 43837 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() 
 43838 ** fails.
 43839 */
 43840 static int openSubJournal(Pager *pPager){
 43841   int rc = SQLITE_OK;
 43842   if( !isOpen(pPager->sjfd) ){
 43843     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
 43844       sqlite3MemJournalOpen(pPager->sjfd);
 43845     }else{
 43846       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
 43849   return rc;
 43852 /*
 43853 ** Append a record of the current state of page pPg to the sub-journal. 
 43854 ** It is the callers responsibility to use subjRequiresPage() to check 
 43855 ** that it is really required before calling this function.
 43856 **
 43857 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
 43858 ** for all open savepoints before returning.
 43859 **
 43860 ** This function returns SQLITE_OK if everything is successful, an IO
 43861 ** error code if the attempt to write to the sub-journal fails, or 
 43862 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
 43863 ** bitvec.
 43864 */
 43865 static int subjournalPage(PgHdr *pPg){
 43866   int rc = SQLITE_OK;
 43867   Pager *pPager = pPg->pPager;
 43868   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 43870     /* Open the sub-journal, if it has not already been opened */
 43871     assert( pPager->useJournal );
 43872     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
 43873     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
 43874     assert( pagerUseWal(pPager) 
 43875          || pageInJournal(pPager, pPg) 
 43876          || pPg->pgno>pPager->dbOrigSize 
 43877     );
 43878     rc = openSubJournal(pPager);
 43880     /* If the sub-journal was opened successfully (or was already open),
 43881     ** write the journal record into the file.  */
 43882     if( rc==SQLITE_OK ){
 43883       void *pData = pPg->pData;
 43884       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
 43885       char *pData2;
 43887       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 43888       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
 43889       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
 43890       if( rc==SQLITE_OK ){
 43891         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
 43895   if( rc==SQLITE_OK ){
 43896     pPager->nSubRec++;
 43897     assert( pPager->nSavepoint>0 );
 43898     rc = addToSavepointBitvecs(pPager, pPg->pgno);
 43900   return rc;
 43903 /*
 43904 ** This function is called by the pcache layer when it has reached some
 43905 ** soft memory limit. The first argument is a pointer to a Pager object
 43906 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
 43907 ** database). The second argument is a reference to a page that is 
 43908 ** currently dirty but has no outstanding references. The page
 43909 ** is always associated with the Pager object passed as the first 
 43910 ** argument.
 43911 **
 43912 ** The job of this function is to make pPg clean by writing its contents
 43913 ** out to the database file, if possible. This may involve syncing the
 43914 ** journal file. 
 43915 **
 43916 ** If successful, sqlite3PcacheMakeClean() is called on the page and
 43917 ** SQLITE_OK returned. If an IO error occurs while trying to make the
 43918 ** page clean, the IO error code is returned. If the page cannot be
 43919 ** made clean for some other reason, but no error occurs, then SQLITE_OK
 43920 ** is returned by sqlite3PcacheMakeClean() is not called.
 43921 */
 43922 static int pagerStress(void *p, PgHdr *pPg){
 43923   Pager *pPager = (Pager *)p;
 43924   int rc = SQLITE_OK;
 43926   assert( pPg->pPager==pPager );
 43927   assert( pPg->flags&PGHDR_DIRTY );
 43929   /* The doNotSpill NOSYNC bit is set during times when doing a sync of
 43930   ** journal (and adding a new header) is not allowed.  This occurs
 43931   ** during calls to sqlite3PagerWrite() while trying to journal multiple
 43932   ** pages belonging to the same sector.
 43933   **
 43934   ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
 43935   ** regardless of whether or not a sync is required.  This is set during
 43936   ** a rollback or by user request, respectively.
 43937   **
 43938   ** Spilling is also prohibited when in an error state since that could
 43939   ** lead to database corruption.   In the current implementaton it 
 43940   ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
 43941   ** while in the error state, hence it is impossible for this routine to
 43942   ** be called in the error state.  Nevertheless, we include a NEVER()
 43943   ** test for the error state as a safeguard against future changes.
 43944   */
 43945   if( NEVER(pPager->errCode) ) return SQLITE_OK;
 43946   testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
 43947   testcase( pPager->doNotSpill & SPILLFLAG_OFF );
 43948   testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
 43949   if( pPager->doNotSpill
 43950    && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 43951       || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 43952   ){
 43953     return SQLITE_OK;
 43956   pPg->pDirty = 0;
 43957   if( pagerUseWal(pPager) ){
 43958     /* Write a single frame for this page to the log. */
 43959     if( subjRequiresPage(pPg) ){ 
 43960       rc = subjournalPage(pPg); 
 43962     if( rc==SQLITE_OK ){
 43963       rc = pagerWalFrames(pPager, pPg, 0, 0);
 43965   }else{
 43967     /* Sync the journal file if required. */
 43968     if( pPg->flags&PGHDR_NEED_SYNC 
 43969      || pPager->eState==PAGER_WRITER_CACHEMOD
 43970     ){
 43971       rc = syncJournal(pPager, 1);
 43974     /* If the page number of this page is larger than the current size of
 43975     ** the database image, it may need to be written to the sub-journal.
 43976     ** This is because the call to pager_write_pagelist() below will not
 43977     ** actually write data to the file in this case.
 43978     **
 43979     ** Consider the following sequence of events:
 43980     **
 43981     **   BEGIN;
 43982     **     <journal page X>
 43983     **     <modify page X>
 43984     **     SAVEPOINT sp;
 43985     **       <shrink database file to Y pages>
 43986     **       pagerStress(page X)
 43987     **     ROLLBACK TO sp;
 43988     **
 43989     ** If (X>Y), then when pagerStress is called page X will not be written
 43990     ** out to the database file, but will be dropped from the cache. Then,
 43991     ** following the "ROLLBACK TO sp" statement, reading page X will read
 43992     ** data from the database file. This will be the copy of page X as it
 43993     ** was when the transaction started, not as it was when "SAVEPOINT sp"
 43994     ** was executed.
 43995     **
 43996     ** The solution is to write the current data for page X into the 
 43997     ** sub-journal file now (if it is not already there), so that it will
 43998     ** be restored to its current value when the "ROLLBACK TO sp" is 
 43999     ** executed.
 44000     */
 44001     if( NEVER(
 44002         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
 44003     ) ){
 44004       rc = subjournalPage(pPg);
 44007     /* Write the contents of the page out to the database file. */
 44008     if( rc==SQLITE_OK ){
 44009       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
 44010       rc = pager_write_pagelist(pPager, pPg);
 44014   /* Mark the page as clean. */
 44015   if( rc==SQLITE_OK ){
 44016     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
 44017     sqlite3PcacheMakeClean(pPg);
 44020   return pager_error(pPager, rc); 
 44024 /*
 44025 ** Allocate and initialize a new Pager object and put a pointer to it
 44026 ** in *ppPager. The pager should eventually be freed by passing it
 44027 ** to sqlite3PagerClose().
 44028 **
 44029 ** The zFilename argument is the path to the database file to open.
 44030 ** If zFilename is NULL then a randomly-named temporary file is created
 44031 ** and used as the file to be cached. Temporary files are be deleted
 44032 ** automatically when they are closed. If zFilename is ":memory:" then 
 44033 ** all information is held in cache. It is never written to disk. 
 44034 ** This can be used to implement an in-memory database.
 44035 **
 44036 ** The nExtra parameter specifies the number of bytes of space allocated
 44037 ** along with each page reference. This space is available to the user
 44038 ** via the sqlite3PagerGetExtra() API.
 44039 **
 44040 ** The flags argument is used to specify properties that affect the
 44041 ** operation of the pager. It should be passed some bitwise combination
 44042 ** of the PAGER_* flags.
 44043 **
 44044 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
 44045 ** of the xOpen() method of the supplied VFS when opening files. 
 44046 **
 44047 ** If the pager object is allocated and the specified file opened 
 44048 ** successfully, SQLITE_OK is returned and *ppPager set to point to
 44049 ** the new pager object. If an error occurs, *ppPager is set to NULL
 44050 ** and error code returned. This function may return SQLITE_NOMEM
 44051 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
 44052 ** various SQLITE_IO_XXX errors.
 44053 */
 44054 SQLITE_PRIVATE int sqlite3PagerOpen(
 44055   sqlite3_vfs *pVfs,       /* The virtual file system to use */
 44056   Pager **ppPager,         /* OUT: Return the Pager structure here */
 44057   const char *zFilename,   /* Name of the database file to open */
 44058   int nExtra,              /* Extra bytes append to each in-memory page */
 44059   int flags,               /* flags controlling this file */
 44060   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
 44061   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 44062 ){
 44063   u8 *pPtr;
 44064   Pager *pPager = 0;       /* Pager object to allocate and return */
 44065   int rc = SQLITE_OK;      /* Return code */
 44066   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 44067   int memDb = 0;           /* True if this is an in-memory file */
 44068   int readOnly = 0;        /* True if this is a read-only file */
 44069   int journalFileSize;     /* Bytes to allocate for each journal fd */
 44070   char *zPathname = 0;     /* Full path to database file */
 44071   int nPathname = 0;       /* Number of bytes in zPathname */
 44072   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 44073   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 44074   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
 44075   const char *zUri = 0;    /* URI args to copy */
 44076   int nUri = 0;            /* Number of bytes of URI args at *zUri */
 44078   /* Figure out how much space is required for each journal file-handle
 44079   ** (there are two of them, the main journal and the sub-journal). This
 44080   ** is the maximum space required for an in-memory journal file handle 
 44081   ** and a regular journal file-handle. Note that a "regular journal-handle"
 44082   ** may be a wrapper capable of caching the first portion of the journal
 44083   ** file in memory to implement the atomic-write optimization (see 
 44084   ** source file journal.c).
 44085   */
 44086   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
 44087     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
 44088   }else{
 44089     journalFileSize = ROUND8(sqlite3MemJournalSize());
 44092   /* Set the output variable to NULL in case an error occurs. */
 44093   *ppPager = 0;
 44095 #ifndef SQLITE_OMIT_MEMORYDB
 44096   if( flags & PAGER_MEMORY ){
 44097     memDb = 1;
 44098     if( zFilename && zFilename[0] ){
 44099       zPathname = sqlite3DbStrDup(0, zFilename);
 44100       if( zPathname==0  ) return SQLITE_NOMEM;
 44101       nPathname = sqlite3Strlen30(zPathname);
 44102       zFilename = 0;
 44105 #endif
 44107   /* Compute and store the full pathname in an allocated buffer pointed
 44108   ** to by zPathname, length nPathname. Or, if this is a temporary file,
 44109   ** leave both nPathname and zPathname set to 0.
 44110   */
 44111   if( zFilename && zFilename[0] ){
 44112     const char *z;
 44113     nPathname = pVfs->mxPathname+1;
 44114     zPathname = sqlite3DbMallocRaw(0, nPathname*2);
 44115     if( zPathname==0 ){
 44116       return SQLITE_NOMEM;
 44118     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
 44119     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
 44120     nPathname = sqlite3Strlen30(zPathname);
 44121     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
 44122     while( *z ){
 44123       z += sqlite3Strlen30(z)+1;
 44124       z += sqlite3Strlen30(z)+1;
 44126     nUri = (int)(&z[1] - zUri);
 44127     assert( nUri>=0 );
 44128     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
 44129       /* This branch is taken when the journal path required by
 44130       ** the database being opened will be more than pVfs->mxPathname
 44131       ** bytes in length. This means the database cannot be opened,
 44132       ** as it will not be possible to open the journal file or even
 44133       ** check for a hot-journal before reading.
 44134       */
 44135       rc = SQLITE_CANTOPEN_BKPT;
 44137     if( rc!=SQLITE_OK ){
 44138       sqlite3DbFree(0, zPathname);
 44139       return rc;
 44143   /* Allocate memory for the Pager structure, PCache object, the
 44144   ** three file descriptors, the database file name and the journal 
 44145   ** file name. The layout in memory is as follows:
 44146   **
 44147   **     Pager object                    (sizeof(Pager) bytes)
 44148   **     PCache object                   (sqlite3PcacheSize() bytes)
 44149   **     Database file handle            (pVfs->szOsFile bytes)
 44150   **     Sub-journal file handle         (journalFileSize bytes)
 44151   **     Main journal file handle        (journalFileSize bytes)
 44152   **     Database file name              (nPathname+1 bytes)
 44153   **     Journal file name               (nPathname+8+1 bytes)
 44154   */
 44155   pPtr = (u8 *)sqlite3MallocZero(
 44156     ROUND8(sizeof(*pPager)) +      /* Pager structure */
 44157     ROUND8(pcacheSize) +           /* PCache object */
 44158     ROUND8(pVfs->szOsFile) +       /* The main db file */
 44159     journalFileSize * 2 +          /* The two journal files */ 
 44160     nPathname + 1 + nUri +         /* zFilename */
 44161     nPathname + 8 + 2              /* zJournal */
 44162 #ifndef SQLITE_OMIT_WAL
 44163     + nPathname + 4 + 2            /* zWal */
 44164 #endif
 44165   );
 44166   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
 44167   if( !pPtr ){
 44168     sqlite3DbFree(0, zPathname);
 44169     return SQLITE_NOMEM;
 44171   pPager =              (Pager*)(pPtr);
 44172   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
 44173   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
 44174   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
 44175   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
 44176   pPager->zFilename =    (char*)(pPtr += journalFileSize);
 44177   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
 44179   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
 44180   if( zPathname ){
 44181     assert( nPathname>0 );
 44182     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
 44183     memcpy(pPager->zFilename, zPathname, nPathname);
 44184     if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
 44185     memcpy(pPager->zJournal, zPathname, nPathname);
 44186     memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
 44187     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
 44188 #ifndef SQLITE_OMIT_WAL
 44189     pPager->zWal = &pPager->zJournal[nPathname+8+1];
 44190     memcpy(pPager->zWal, zPathname, nPathname);
 44191     memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
 44192     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
 44193 #endif
 44194     sqlite3DbFree(0, zPathname);
 44196   pPager->pVfs = pVfs;
 44197   pPager->vfsFlags = vfsFlags;
 44199   /* Open the pager file.
 44200   */
 44201   if( zFilename && zFilename[0] ){
 44202     int fout = 0;                    /* VFS flags returned by xOpen() */
 44203     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 44204     assert( !memDb );
 44205     readOnly = (fout&SQLITE_OPEN_READONLY);
 44207     /* If the file was successfully opened for read/write access,
 44208     ** choose a default page size in case we have to create the
 44209     ** database file. The default page size is the maximum of:
 44210     **
 44211     **    + SQLITE_DEFAULT_PAGE_SIZE,
 44212     **    + The value returned by sqlite3OsSectorSize()
 44213     **    + The largest page size that can be written atomically.
 44214     */
 44215     if( rc==SQLITE_OK && !readOnly ){
 44216       setSectorSize(pPager);
 44217       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
 44218       if( szPageDflt<pPager->sectorSize ){
 44219         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
 44220           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
 44221         }else{
 44222           szPageDflt = (u32)pPager->sectorSize;
 44225 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 44227         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
 44228         int ii;
 44229         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
 44230         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
 44231         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
 44232         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
 44233           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
 44234             szPageDflt = ii;
 44238 #endif
 44240   }else{
 44241     /* If a temporary file is requested, it is not opened immediately.
 44242     ** In this case we accept the default page size and delay actually
 44243     ** opening the file until the first call to OsWrite().
 44244     **
 44245     ** This branch is also run for an in-memory database. An in-memory
 44246     ** database is the same as a temp-file that is never written out to
 44247     ** disk and uses an in-memory rollback journal.
 44248     */ 
 44249     tempFile = 1;
 44250     pPager->eState = PAGER_READER;
 44251     pPager->eLock = EXCLUSIVE_LOCK;
 44252     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
 44255   /* The following call to PagerSetPagesize() serves to set the value of 
 44256   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
 44257   */
 44258   if( rc==SQLITE_OK ){
 44259     assert( pPager->memDb==0 );
 44260     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
 44261     testcase( rc!=SQLITE_OK );
 44264   /* If an error occurred in either of the blocks above, free the 
 44265   ** Pager structure and close the file.
 44266   */
 44267   if( rc!=SQLITE_OK ){
 44268     assert( !pPager->pTmpSpace );
 44269     sqlite3OsClose(pPager->fd);
 44270     sqlite3_free(pPager);
 44271     return rc;
 44274   /* Initialize the PCache object. */
 44275   assert( nExtra<1000 );
 44276   nExtra = ROUND8(nExtra);
 44277   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
 44278                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
 44280   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
 44281   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
 44283   pPager->useJournal = (u8)useJournal;
 44284   /* pPager->stmtOpen = 0; */
 44285   /* pPager->stmtInUse = 0; */
 44286   /* pPager->nRef = 0; */
 44287   /* pPager->stmtSize = 0; */
 44288   /* pPager->stmtJSize = 0; */
 44289   /* pPager->nPage = 0; */
 44290   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
 44291   /* pPager->state = PAGER_UNLOCK; */
 44292 #if 0
 44293   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
 44294 #endif
 44295   /* pPager->errMask = 0; */
 44296   pPager->tempFile = (u8)tempFile;
 44297   assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
 44298           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
 44299   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
 44300   pPager->exclusiveMode = (u8)tempFile; 
 44301   pPager->changeCountDone = pPager->tempFile;
 44302   pPager->memDb = (u8)memDb;
 44303   pPager->readOnly = (u8)readOnly;
 44304   assert( useJournal || pPager->tempFile );
 44305   pPager->noSync = pPager->tempFile;
 44306   if( pPager->noSync ){
 44307     assert( pPager->fullSync==0 );
 44308     assert( pPager->syncFlags==0 );
 44309     assert( pPager->walSyncFlags==0 );
 44310     assert( pPager->ckptSyncFlags==0 );
 44311   }else{
 44312     pPager->fullSync = 1;
 44313     pPager->syncFlags = SQLITE_SYNC_NORMAL;
 44314     pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
 44315     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
 44317   /* pPager->pFirst = 0; */
 44318   /* pPager->pFirstSynced = 0; */
 44319   /* pPager->pLast = 0; */
 44320   pPager->nExtra = (u16)nExtra;
 44321   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 44322   assert( isOpen(pPager->fd) || tempFile );
 44323   setSectorSize(pPager);
 44324   if( !useJournal ){
 44325     pPager->journalMode = PAGER_JOURNALMODE_OFF;
 44326   }else if( memDb ){
 44327     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 44329   /* pPager->xBusyHandler = 0; */
 44330   /* pPager->pBusyHandlerArg = 0; */
 44331   pPager->xReiniter = xReinit;
 44332   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 44333   /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
 44335   *ppPager = pPager;
 44336   return SQLITE_OK;
 44340 /* Verify that the database file has not be deleted or renamed out from
 44341 ** under the pager.  Return SQLITE_OK if the database is still were it ought
 44342 ** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
 44343 ** code from sqlite3OsAccess()) if the database has gone missing.
 44344 */
 44345 static int databaseIsUnmoved(Pager *pPager){
 44346   int bHasMoved = 0;
 44347   int rc;
 44349   if( pPager->tempFile ) return SQLITE_OK;
 44350   if( pPager->dbSize==0 ) return SQLITE_OK;
 44351   assert( pPager->zFilename && pPager->zFilename[0] );
 44352   rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
 44353   if( rc==SQLITE_NOTFOUND ){
 44354     /* If the HAS_MOVED file-control is unimplemented, assume that the file
 44355     ** has not been moved.  That is the historical behavior of SQLite: prior to
 44356     ** version 3.8.3, it never checked */
 44357     rc = SQLITE_OK;
 44358   }else if( rc==SQLITE_OK && bHasMoved ){
 44359     rc = SQLITE_READONLY_DBMOVED;
 44361   return rc;
 44365 /*
 44366 ** This function is called after transitioning from PAGER_UNLOCK to
 44367 ** PAGER_SHARED state. It tests if there is a hot journal present in
 44368 ** the file-system for the given pager. A hot journal is one that 
 44369 ** needs to be played back. According to this function, a hot-journal
 44370 ** file exists if the following criteria are met:
 44371 **
 44372 **   * The journal file exists in the file system, and
 44373 **   * No process holds a RESERVED or greater lock on the database file, and
 44374 **   * The database file itself is greater than 0 bytes in size, and
 44375 **   * The first byte of the journal file exists and is not 0x00.
 44376 **
 44377 ** If the current size of the database file is 0 but a journal file
 44378 ** exists, that is probably an old journal left over from a prior
 44379 ** database with the same name. In this case the journal file is
 44380 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
 44381 ** is returned.
 44382 **
 44383 ** This routine does not check if there is a master journal filename
 44384 ** at the end of the file. If there is, and that master journal file
 44385 ** does not exist, then the journal file is not really hot. In this
 44386 ** case this routine will return a false-positive. The pager_playback()
 44387 ** routine will discover that the journal file is not really hot and 
 44388 ** will not roll it back. 
 44389 **
 44390 ** If a hot-journal file is found to exist, *pExists is set to 1 and 
 44391 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
 44392 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
 44393 ** to determine whether or not a hot-journal file exists, the IO error
 44394 ** code is returned and the value of *pExists is undefined.
 44395 */
 44396 static int hasHotJournal(Pager *pPager, int *pExists){
 44397   sqlite3_vfs * const pVfs = pPager->pVfs;
 44398   int rc = SQLITE_OK;           /* Return code */
 44399   int exists = 1;               /* True if a journal file is present */
 44400   int jrnlOpen = !!isOpen(pPager->jfd);
 44402   assert( pPager->useJournal );
 44403   assert( isOpen(pPager->fd) );
 44404   assert( pPager->eState==PAGER_OPEN );
 44406   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
 44407     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
 44408   ));
 44410   *pExists = 0;
 44411   if( !jrnlOpen ){
 44412     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
 44414   if( rc==SQLITE_OK && exists ){
 44415     int locked = 0;             /* True if some process holds a RESERVED lock */
 44417     /* Race condition here:  Another process might have been holding the
 44418     ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
 44419     ** call above, but then delete the journal and drop the lock before
 44420     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
 44421     ** is the case, this routine might think there is a hot journal when
 44422     ** in fact there is none.  This results in a false-positive which will
 44423     ** be dealt with by the playback routine.  Ticket #3883.
 44424     */
 44425     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
 44426     if( rc==SQLITE_OK && !locked ){
 44427       Pgno nPage;                 /* Number of pages in database file */
 44429       rc = pagerPagecount(pPager, &nPage);
 44430       if( rc==SQLITE_OK ){
 44431         /* If the database is zero pages in size, that means that either (1) the
 44432         ** journal is a remnant from a prior database with the same name where
 44433         ** the database file but not the journal was deleted, or (2) the initial
 44434         ** transaction that populates a new database is being rolled back.
 44435         ** In either case, the journal file can be deleted.  However, take care
 44436         ** not to delete the journal file if it is already open due to
 44437         ** journal_mode=PERSIST.
 44438         */
 44439         if( nPage==0 && !jrnlOpen ){
 44440           sqlite3BeginBenignMalloc();
 44441           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
 44442             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
 44443             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
 44445           sqlite3EndBenignMalloc();
 44446         }else{
 44447           /* The journal file exists and no other connection has a reserved
 44448           ** or greater lock on the database file. Now check that there is
 44449           ** at least one non-zero bytes at the start of the journal file.
 44450           ** If there is, then we consider this journal to be hot. If not, 
 44451           ** it can be ignored.
 44452           */
 44453           if( !jrnlOpen ){
 44454             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
 44455             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
 44457           if( rc==SQLITE_OK ){
 44458             u8 first = 0;
 44459             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
 44460             if( rc==SQLITE_IOERR_SHORT_READ ){
 44461               rc = SQLITE_OK;
 44463             if( !jrnlOpen ){
 44464               sqlite3OsClose(pPager->jfd);
 44466             *pExists = (first!=0);
 44467           }else if( rc==SQLITE_CANTOPEN ){
 44468             /* If we cannot open the rollback journal file in order to see if
 44469             ** its has a zero header, that might be due to an I/O error, or
 44470             ** it might be due to the race condition described above and in
 44471             ** ticket #3883.  Either way, assume that the journal is hot.
 44472             ** This might be a false positive.  But if it is, then the
 44473             ** automatic journal playback and recovery mechanism will deal
 44474             ** with it under an EXCLUSIVE lock where we do not need to
 44475             ** worry so much with race conditions.
 44476             */
 44477             *pExists = 1;
 44478             rc = SQLITE_OK;
 44485   return rc;
 44488 /*
 44489 ** This function is called to obtain a shared lock on the database file.
 44490 ** It is illegal to call sqlite3PagerAcquire() until after this function
 44491 ** has been successfully called. If a shared-lock is already held when
 44492 ** this function is called, it is a no-op.
 44493 **
 44494 ** The following operations are also performed by this function.
 44495 **
 44496 **   1) If the pager is currently in PAGER_OPEN state (no lock held
 44497 **      on the database file), then an attempt is made to obtain a
 44498 **      SHARED lock on the database file. Immediately after obtaining
 44499 **      the SHARED lock, the file-system is checked for a hot-journal,
 44500 **      which is played back if present. Following any hot-journal 
 44501 **      rollback, the contents of the cache are validated by checking
 44502 **      the 'change-counter' field of the database file header and
 44503 **      discarded if they are found to be invalid.
 44504 **
 44505 **   2) If the pager is running in exclusive-mode, and there are currently
 44506 **      no outstanding references to any pages, and is in the error state,
 44507 **      then an attempt is made to clear the error state by discarding
 44508 **      the contents of the page cache and rolling back any open journal
 44509 **      file.
 44510 **
 44511 ** If everything is successful, SQLITE_OK is returned. If an IO error 
 44512 ** occurs while locking the database, checking for a hot-journal file or 
 44513 ** rolling back a journal file, the IO error code is returned.
 44514 */
 44515 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
 44516   int rc = SQLITE_OK;                /* Return code */
 44518   /* This routine is only called from b-tree and only when there are no
 44519   ** outstanding pages. This implies that the pager state should either
 44520   ** be OPEN or READER. READER is only possible if the pager is or was in 
 44521   ** exclusive access mode.
 44522   */
 44523   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
 44524   assert( assert_pager_state(pPager) );
 44525   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
 44526   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
 44528   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
 44529     int bHotJournal = 1;          /* True if there exists a hot journal-file */
 44531     assert( !MEMDB );
 44533     rc = pager_wait_on_lock(pPager, SHARED_LOCK);
 44534     if( rc!=SQLITE_OK ){
 44535       assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
 44536       goto failed;
 44539     /* If a journal file exists, and there is no RESERVED lock on the
 44540     ** database file, then it either needs to be played back or deleted.
 44541     */
 44542     if( pPager->eLock<=SHARED_LOCK ){
 44543       rc = hasHotJournal(pPager, &bHotJournal);
 44545     if( rc!=SQLITE_OK ){
 44546       goto failed;
 44548     if( bHotJournal ){
 44549       if( pPager->readOnly ){
 44550         rc = SQLITE_READONLY_ROLLBACK;
 44551         goto failed;
 44554       /* Get an EXCLUSIVE lock on the database file. At this point it is
 44555       ** important that a RESERVED lock is not obtained on the way to the
 44556       ** EXCLUSIVE lock. If it were, another process might open the
 44557       ** database file, detect the RESERVED lock, and conclude that the
 44558       ** database is safe to read while this process is still rolling the 
 44559       ** hot-journal back.
 44560       ** 
 44561       ** Because the intermediate RESERVED lock is not requested, any
 44562       ** other process attempting to access the database file will get to 
 44563       ** this point in the code and fail to obtain its own EXCLUSIVE lock 
 44564       ** on the database file.
 44565       **
 44566       ** Unless the pager is in locking_mode=exclusive mode, the lock is
 44567       ** downgraded to SHARED_LOCK before this function returns.
 44568       */
 44569       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 44570       if( rc!=SQLITE_OK ){
 44571         goto failed;
 44574       /* If it is not already open and the file exists on disk, open the 
 44575       ** journal for read/write access. Write access is required because 
 44576       ** in exclusive-access mode the file descriptor will be kept open 
 44577       ** and possibly used for a transaction later on. Also, write-access 
 44578       ** is usually required to finalize the journal in journal_mode=persist 
 44579       ** mode (and also for journal_mode=truncate on some systems).
 44580       **
 44581       ** If the journal does not exist, it usually means that some 
 44582       ** other connection managed to get in and roll it back before 
 44583       ** this connection obtained the exclusive lock above. Or, it 
 44584       ** may mean that the pager was in the error-state when this
 44585       ** function was called and the journal file does not exist.
 44586       */
 44587       if( !isOpen(pPager->jfd) ){
 44588         sqlite3_vfs * const pVfs = pPager->pVfs;
 44589         int bExists;              /* True if journal file exists */
 44590         rc = sqlite3OsAccess(
 44591             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
 44592         if( rc==SQLITE_OK && bExists ){
 44593           int fout = 0;
 44594           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
 44595           assert( !pPager->tempFile );
 44596           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
 44597           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 44598           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
 44599             rc = SQLITE_CANTOPEN_BKPT;
 44600             sqlite3OsClose(pPager->jfd);
 44605       /* Playback and delete the journal.  Drop the database write
 44606       ** lock and reacquire the read lock. Purge the cache before
 44607       ** playing back the hot-journal so that we don't end up with
 44608       ** an inconsistent cache.  Sync the hot journal before playing
 44609       ** it back since the process that crashed and left the hot journal
 44610       ** probably did not sync it and we are required to always sync
 44611       ** the journal before playing it back.
 44612       */
 44613       if( isOpen(pPager->jfd) ){
 44614         assert( rc==SQLITE_OK );
 44615         rc = pagerSyncHotJournal(pPager);
 44616         if( rc==SQLITE_OK ){
 44617           rc = pager_playback(pPager, 1);
 44618           pPager->eState = PAGER_OPEN;
 44620       }else if( !pPager->exclusiveMode ){
 44621         pagerUnlockDb(pPager, SHARED_LOCK);
 44624       if( rc!=SQLITE_OK ){
 44625         /* This branch is taken if an error occurs while trying to open
 44626         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
 44627         ** pager_unlock() routine will be called before returning to unlock
 44628         ** the file. If the unlock attempt fails, then Pager.eLock must be
 44629         ** set to UNKNOWN_LOCK (see the comment above the #define for 
 44630         ** UNKNOWN_LOCK above for an explanation). 
 44631         **
 44632         ** In order to get pager_unlock() to do this, set Pager.eState to
 44633         ** PAGER_ERROR now. This is not actually counted as a transition
 44634         ** to ERROR state in the state diagram at the top of this file,
 44635         ** since we know that the same call to pager_unlock() will very
 44636         ** shortly transition the pager object to the OPEN state. Calling
 44637         ** assert_pager_state() would fail now, as it should not be possible
 44638         ** to be in ERROR state when there are zero outstanding page 
 44639         ** references.
 44640         */
 44641         pager_error(pPager, rc);
 44642         goto failed;
 44645       assert( pPager->eState==PAGER_OPEN );
 44646       assert( (pPager->eLock==SHARED_LOCK)
 44647            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
 44648       );
 44651     if( !pPager->tempFile && (
 44652         pPager->pBackup 
 44653      || sqlite3PcachePagecount(pPager->pPCache)>0 
 44654      || USEFETCH(pPager)
 44655     )){
 44656       /* The shared-lock has just been acquired on the database file
 44657       ** and there are already pages in the cache (from a previous
 44658       ** read or write transaction).  Check to see if the database
 44659       ** has been modified.  If the database has changed, flush the
 44660       ** cache.
 44661       **
 44662       ** Database changes is detected by looking at 15 bytes beginning
 44663       ** at offset 24 into the file.  The first 4 of these 16 bytes are
 44664       ** a 32-bit counter that is incremented with each change.  The
 44665       ** other bytes change randomly with each file change when
 44666       ** a codec is in use.
 44667       ** 
 44668       ** There is a vanishingly small chance that a change will not be 
 44669       ** detected.  The chance of an undetected change is so small that
 44670       ** it can be neglected.
 44671       */
 44672       Pgno nPage = 0;
 44673       char dbFileVers[sizeof(pPager->dbFileVers)];
 44675       rc = pagerPagecount(pPager, &nPage);
 44676       if( rc ) goto failed;
 44678       if( nPage>0 ){
 44679         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
 44680         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
 44681         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
 44682           goto failed;
 44684       }else{
 44685         memset(dbFileVers, 0, sizeof(dbFileVers));
 44688       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
 44689         pager_reset(pPager);
 44691         /* Unmap the database file. It is possible that external processes
 44692         ** may have truncated the database file and then extended it back
 44693         ** to its original size while this process was not holding a lock.
 44694         ** In this case there may exist a Pager.pMap mapping that appears
 44695         ** to be the right size but is not actually valid. Avoid this
 44696         ** possibility by unmapping the db here. */
 44697         if( USEFETCH(pPager) ){
 44698           sqlite3OsUnfetch(pPager->fd, 0, 0);
 44703     /* If there is a WAL file in the file-system, open this database in WAL
 44704     ** mode. Otherwise, the following function call is a no-op.
 44705     */
 44706     rc = pagerOpenWalIfPresent(pPager);
 44707 #ifndef SQLITE_OMIT_WAL
 44708     assert( pPager->pWal==0 || rc==SQLITE_OK );
 44709 #endif
 44712   if( pagerUseWal(pPager) ){
 44713     assert( rc==SQLITE_OK );
 44714     rc = pagerBeginReadTransaction(pPager);
 44717   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
 44718     rc = pagerPagecount(pPager, &pPager->dbSize);
 44721  failed:
 44722   if( rc!=SQLITE_OK ){
 44723     assert( !MEMDB );
 44724     pager_unlock(pPager);
 44725     assert( pPager->eState==PAGER_OPEN );
 44726   }else{
 44727     pPager->eState = PAGER_READER;
 44729   return rc;
 44732 /*
 44733 ** If the reference count has reached zero, rollback any active
 44734 ** transaction and unlock the pager.
 44735 **
 44736 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
 44737 ** the rollback journal, the unlock is not performed and there is
 44738 ** nothing to rollback, so this routine is a no-op.
 44739 */ 
 44740 static void pagerUnlockIfUnused(Pager *pPager){
 44741   if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
 44742     pagerUnlockAndRollback(pPager);
 44746 /*
 44747 ** Acquire a reference to page number pgno in pager pPager (a page
 44748 ** reference has type DbPage*). If the requested reference is 
 44749 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
 44750 **
 44751 ** If the requested page is already in the cache, it is returned. 
 44752 ** Otherwise, a new page object is allocated and populated with data
 44753 ** read from the database file. In some cases, the pcache module may
 44754 ** choose not to allocate a new page object and may reuse an existing
 44755 ** object with no outstanding references.
 44756 **
 44757 ** The extra data appended to a page is always initialized to zeros the 
 44758 ** first time a page is loaded into memory. If the page requested is 
 44759 ** already in the cache when this function is called, then the extra
 44760 ** data is left as it was when the page object was last used.
 44761 **
 44762 ** If the database image is smaller than the requested page or if a 
 44763 ** non-zero value is passed as the noContent parameter and the 
 44764 ** requested page is not already stored in the cache, then no 
 44765 ** actual disk read occurs. In this case the memory image of the 
 44766 ** page is initialized to all zeros. 
 44767 **
 44768 ** If noContent is true, it means that we do not care about the contents
 44769 ** of the page. This occurs in two scenarios:
 44770 **
 44771 **   a) When reading a free-list leaf page from the database, and
 44772 **
 44773 **   b) When a savepoint is being rolled back and we need to load
 44774 **      a new page into the cache to be filled with the data read
 44775 **      from the savepoint journal.
 44776 **
 44777 ** If noContent is true, then the data returned is zeroed instead of
 44778 ** being read from the database. Additionally, the bits corresponding
 44779 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
 44780 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
 44781 ** savepoints are set. This means if the page is made writable at any
 44782 ** point in the future, using a call to sqlite3PagerWrite(), its contents
 44783 ** will not be journaled. This saves IO.
 44784 **
 44785 ** The acquisition might fail for several reasons.  In all cases,
 44786 ** an appropriate error code is returned and *ppPage is set to NULL.
 44787 **
 44788 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
 44789 ** to find a page in the in-memory cache first.  If the page is not already
 44790 ** in memory, this routine goes to disk to read it in whereas Lookup()
 44791 ** just returns 0.  This routine acquires a read-lock the first time it
 44792 ** has to go to disk, and could also playback an old journal if necessary.
 44793 ** Since Lookup() never goes to disk, it never has to deal with locks
 44794 ** or journal files.
 44795 */
 44796 SQLITE_PRIVATE int sqlite3PagerAcquire(
 44797   Pager *pPager,      /* The pager open on the database file */
 44798   Pgno pgno,          /* Page number to fetch */
 44799   DbPage **ppPage,    /* Write a pointer to the page here */
 44800   int flags           /* PAGER_GET_XXX flags */
 44801 ){
 44802   int rc = SQLITE_OK;
 44803   PgHdr *pPg = 0;
 44804   u32 iFrame = 0;                 /* Frame to read from WAL file */
 44805   const int noContent = (flags & PAGER_GET_NOCONTENT);
 44807   /* It is acceptable to use a read-only (mmap) page for any page except
 44808   ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
 44809   ** flag was specified by the caller. And so long as the db is not a 
 44810   ** temporary or in-memory database.  */
 44811   const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
 44812    && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
 44813 #ifdef SQLITE_HAS_CODEC
 44814    && pPager->xCodec==0
 44815 #endif
 44816   );
 44818   assert( pPager->eState>=PAGER_READER );
 44819   assert( assert_pager_state(pPager) );
 44820   assert( noContent==0 || bMmapOk==0 );
 44822   if( pgno==0 ){
 44823     return SQLITE_CORRUPT_BKPT;
 44826   /* If the pager is in the error state, return an error immediately. 
 44827   ** Otherwise, request the page from the PCache layer. */
 44828   if( pPager->errCode!=SQLITE_OK ){
 44829     rc = pPager->errCode;
 44830   }else{
 44832     if( bMmapOk && pagerUseWal(pPager) ){
 44833       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
 44834       if( rc!=SQLITE_OK ) goto pager_acquire_err;
 44837     if( bMmapOk && iFrame==0 ){
 44838       void *pData = 0;
 44840       rc = sqlite3OsFetch(pPager->fd, 
 44841           (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
 44842       );
 44844       if( rc==SQLITE_OK && pData ){
 44845         if( pPager->eState>PAGER_READER ){
 44846           (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
 44848         if( pPg==0 ){
 44849           rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
 44850         }else{
 44851           sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
 44853         if( pPg ){
 44854           assert( rc==SQLITE_OK );
 44855           *ppPage = pPg;
 44856           return SQLITE_OK;
 44859       if( rc!=SQLITE_OK ){
 44860         goto pager_acquire_err;
 44864     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
 44867   if( rc!=SQLITE_OK ){
 44868     /* Either the call to sqlite3PcacheFetch() returned an error or the
 44869     ** pager was already in the error-state when this function was called.
 44870     ** Set pPg to 0 and jump to the exception handler.  */
 44871     pPg = 0;
 44872     goto pager_acquire_err;
 44874   assert( (*ppPage)->pgno==pgno );
 44875   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
 44877   if( (*ppPage)->pPager && !noContent ){
 44878     /* In this case the pcache already contains an initialized copy of
 44879     ** the page. Return without further ado.  */
 44880     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
 44881     pPager->aStat[PAGER_STAT_HIT]++;
 44882     return SQLITE_OK;
 44884   }else{
 44885     /* The pager cache has created a new page. Its content needs to 
 44886     ** be initialized.  */
 44888     pPg = *ppPage;
 44889     pPg->pPager = pPager;
 44891     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
 44892     ** number greater than this, or the unused locking-page, is requested. */
 44893     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
 44894       rc = SQLITE_CORRUPT_BKPT;
 44895       goto pager_acquire_err;
 44898     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
 44899       if( pgno>pPager->mxPgno ){
 44900         rc = SQLITE_FULL;
 44901         goto pager_acquire_err;
 44903       if( noContent ){
 44904         /* Failure to set the bits in the InJournal bit-vectors is benign.
 44905         ** It merely means that we might do some extra work to journal a 
 44906         ** page that does not need to be journaled.  Nevertheless, be sure 
 44907         ** to test the case where a malloc error occurs while trying to set 
 44908         ** a bit in a bit vector.
 44909         */
 44910         sqlite3BeginBenignMalloc();
 44911         if( pgno<=pPager->dbOrigSize ){
 44912           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
 44913           testcase( rc==SQLITE_NOMEM );
 44915         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
 44916         testcase( rc==SQLITE_NOMEM );
 44917         sqlite3EndBenignMalloc();
 44919       memset(pPg->pData, 0, pPager->pageSize);
 44920       IOTRACE(("ZERO %p %d\n", pPager, pgno));
 44921     }else{
 44922       if( pagerUseWal(pPager) && bMmapOk==0 ){
 44923         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
 44924         if( rc!=SQLITE_OK ) goto pager_acquire_err;
 44926       assert( pPg->pPager==pPager );
 44927       pPager->aStat[PAGER_STAT_MISS]++;
 44928       rc = readDbPage(pPg, iFrame);
 44929       if( rc!=SQLITE_OK ){
 44930         goto pager_acquire_err;
 44933     pager_set_pagehash(pPg);
 44936   return SQLITE_OK;
 44938 pager_acquire_err:
 44939   assert( rc!=SQLITE_OK );
 44940   if( pPg ){
 44941     sqlite3PcacheDrop(pPg);
 44943   pagerUnlockIfUnused(pPager);
 44945   *ppPage = 0;
 44946   return rc;
 44949 /*
 44950 ** Acquire a page if it is already in the in-memory cache.  Do
 44951 ** not read the page from disk.  Return a pointer to the page,
 44952 ** or 0 if the page is not in cache. 
 44953 **
 44954 ** See also sqlite3PagerGet().  The difference between this routine
 44955 ** and sqlite3PagerGet() is that _get() will go to the disk and read
 44956 ** in the page if the page is not already in cache.  This routine
 44957 ** returns NULL if the page is not in cache or if a disk I/O error 
 44958 ** has ever happened.
 44959 */
 44960 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
 44961   PgHdr *pPg = 0;
 44962   assert( pPager!=0 );
 44963   assert( pgno!=0 );
 44964   assert( pPager->pPCache!=0 );
 44965   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
 44966   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
 44967   return pPg;
 44970 /*
 44971 ** Release a page reference.
 44972 **
 44973 ** If the number of references to the page drop to zero, then the
 44974 ** page is added to the LRU list.  When all references to all pages
 44975 ** are released, a rollback occurs and the lock on the database is
 44976 ** removed.
 44977 */
 44978 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
 44979   Pager *pPager;
 44980   assert( pPg!=0 );
 44981   pPager = pPg->pPager;
 44982   if( pPg->flags & PGHDR_MMAP ){
 44983     pagerReleaseMapPage(pPg);
 44984   }else{
 44985     sqlite3PcacheRelease(pPg);
 44987   pagerUnlockIfUnused(pPager);
 44989 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
 44990   if( pPg ) sqlite3PagerUnrefNotNull(pPg);
 44993 /*
 44994 ** This function is called at the start of every write transaction.
 44995 ** There must already be a RESERVED or EXCLUSIVE lock on the database 
 44996 ** file when this routine is called.
 44997 **
 44998 ** Open the journal file for pager pPager and write a journal header
 44999 ** to the start of it. If there are active savepoints, open the sub-journal
 45000 ** as well. This function is only used when the journal file is being 
 45001 ** opened to write a rollback log for a transaction. It is not used 
 45002 ** when opening a hot journal file to roll it back.
 45003 **
 45004 ** If the journal file is already open (as it may be in exclusive mode),
 45005 ** then this function just writes a journal header to the start of the
 45006 ** already open file. 
 45007 **
 45008 ** Whether or not the journal file is opened by this function, the
 45009 ** Pager.pInJournal bitvec structure is allocated.
 45010 **
 45011 ** Return SQLITE_OK if everything is successful. Otherwise, return 
 45012 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
 45013 ** an IO error code if opening or writing the journal file fails.
 45014 */
 45015 static int pager_open_journal(Pager *pPager){
 45016   int rc = SQLITE_OK;                        /* Return code */
 45017   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
 45019   assert( pPager->eState==PAGER_WRITER_LOCKED );
 45020   assert( assert_pager_state(pPager) );
 45021   assert( pPager->pInJournal==0 );
 45023   /* If already in the error state, this function is a no-op.  But on
 45024   ** the other hand, this routine is never called if we are already in
 45025   ** an error state. */
 45026   if( NEVER(pPager->errCode) ) return pPager->errCode;
 45028   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 45029     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
 45030     if( pPager->pInJournal==0 ){
 45031       return SQLITE_NOMEM;
 45034     /* Open the journal file if it is not already open. */
 45035     if( !isOpen(pPager->jfd) ){
 45036       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
 45037         sqlite3MemJournalOpen(pPager->jfd);
 45038       }else{
 45039         const int flags =                   /* VFS flags to open journal file */
 45040           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 45041           (pPager->tempFile ? 
 45042             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
 45043             (SQLITE_OPEN_MAIN_JOURNAL)
 45044           );
 45046         /* Verify that the database still has the same name as it did when
 45047         ** it was originally opened. */
 45048         rc = databaseIsUnmoved(pPager);
 45049         if( rc==SQLITE_OK ){
 45050 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 45051           rc = sqlite3JournalOpen(
 45052               pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
 45053           );
 45054 #else
 45055           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
 45056 #endif
 45059       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
 45063     /* Write the first journal header to the journal file and open 
 45064     ** the sub-journal if necessary.
 45065     */
 45066     if( rc==SQLITE_OK ){
 45067       /* TODO: Check if all of these are really required. */
 45068       pPager->nRec = 0;
 45069       pPager->journalOff = 0;
 45070       pPager->setMaster = 0;
 45071       pPager->journalHdr = 0;
 45072       rc = writeJournalHdr(pPager);
 45076   if( rc!=SQLITE_OK ){
 45077     sqlite3BitvecDestroy(pPager->pInJournal);
 45078     pPager->pInJournal = 0;
 45079   }else{
 45080     assert( pPager->eState==PAGER_WRITER_LOCKED );
 45081     pPager->eState = PAGER_WRITER_CACHEMOD;
 45084   return rc;
 45087 /*
 45088 ** Begin a write-transaction on the specified pager object. If a 
 45089 ** write-transaction has already been opened, this function is a no-op.
 45090 **
 45091 ** If the exFlag argument is false, then acquire at least a RESERVED
 45092 ** lock on the database file. If exFlag is true, then acquire at least
 45093 ** an EXCLUSIVE lock. If such a lock is already held, no locking 
 45094 ** functions need be called.
 45095 **
 45096 ** If the subjInMemory argument is non-zero, then any sub-journal opened
 45097 ** within this transaction will be opened as an in-memory file. This
 45098 ** has no effect if the sub-journal is already opened (as it may be when
 45099 ** running in exclusive mode) or if the transaction does not require a
 45100 ** sub-journal. If the subjInMemory argument is zero, then any required
 45101 ** sub-journal is implemented in-memory if pPager is an in-memory database, 
 45102 ** or using a temporary file otherwise.
 45103 */
 45104 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
 45105   int rc = SQLITE_OK;
 45107   if( pPager->errCode ) return pPager->errCode;
 45108   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
 45109   pPager->subjInMemory = (u8)subjInMemory;
 45111   if( ALWAYS(pPager->eState==PAGER_READER) ){
 45112     assert( pPager->pInJournal==0 );
 45114     if( pagerUseWal(pPager) ){
 45115       /* If the pager is configured to use locking_mode=exclusive, and an
 45116       ** exclusive lock on the database is not already held, obtain it now.
 45117       */
 45118       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
 45119         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 45120         if( rc!=SQLITE_OK ){
 45121           return rc;
 45123         sqlite3WalExclusiveMode(pPager->pWal, 1);
 45126       /* Grab the write lock on the log file. If successful, upgrade to
 45127       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
 45128       ** The busy-handler is not invoked if another connection already
 45129       ** holds the write-lock. If possible, the upper layer will call it.
 45130       */
 45131       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
 45132     }else{
 45133       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
 45134       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
 45135       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
 45136       ** lock, but not when obtaining the RESERVED lock.
 45137       */
 45138       rc = pagerLockDb(pPager, RESERVED_LOCK);
 45139       if( rc==SQLITE_OK && exFlag ){
 45140         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 45144     if( rc==SQLITE_OK ){
 45145       /* Change to WRITER_LOCKED state.
 45146       **
 45147       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
 45148       ** when it has an open transaction, but never to DBMOD or FINISHED.
 45149       ** This is because in those states the code to roll back savepoint 
 45150       ** transactions may copy data from the sub-journal into the database 
 45151       ** file as well as into the page cache. Which would be incorrect in 
 45152       ** WAL mode.
 45153       */
 45154       pPager->eState = PAGER_WRITER_LOCKED;
 45155       pPager->dbHintSize = pPager->dbSize;
 45156       pPager->dbFileSize = pPager->dbSize;
 45157       pPager->dbOrigSize = pPager->dbSize;
 45158       pPager->journalOff = 0;
 45161     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
 45162     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
 45163     assert( assert_pager_state(pPager) );
 45166   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
 45167   return rc;
 45170 /*
 45171 ** Mark a single data page as writeable. The page is written into the 
 45172 ** main journal or sub-journal as required. If the page is written into
 45173 ** one of the journals, the corresponding bit is set in the 
 45174 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
 45175 ** of any open savepoints as appropriate.
 45176 */
 45177 static int pager_write(PgHdr *pPg){
 45178   Pager *pPager = pPg->pPager;
 45179   int rc = SQLITE_OK;
 45180   int inJournal;
 45182   /* This routine is not called unless a write-transaction has already 
 45183   ** been started. The journal file may or may not be open at this point.
 45184   ** It is never called in the ERROR state.
 45185   */
 45186   assert( pPager->eState==PAGER_WRITER_LOCKED
 45187        || pPager->eState==PAGER_WRITER_CACHEMOD
 45188        || pPager->eState==PAGER_WRITER_DBMOD
 45189   );
 45190   assert( assert_pager_state(pPager) );
 45191   assert( pPager->errCode==0 );
 45192   assert( pPager->readOnly==0 );
 45194   CHECK_PAGE(pPg);
 45196   /* The journal file needs to be opened. Higher level routines have already
 45197   ** obtained the necessary locks to begin the write-transaction, but the
 45198   ** rollback journal might not yet be open. Open it now if this is the case.
 45199   **
 45200   ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
 45201   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
 45202   ** an error might occur and the pager would end up in WRITER_LOCKED state
 45203   ** with pages marked as dirty in the cache.
 45204   */
 45205   if( pPager->eState==PAGER_WRITER_LOCKED ){
 45206     rc = pager_open_journal(pPager);
 45207     if( rc!=SQLITE_OK ) return rc;
 45209   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
 45210   assert( assert_pager_state(pPager) );
 45212   /* Mark the page as dirty.  If the page has already been written
 45213   ** to the journal then we can return right away.
 45214   */
 45215   sqlite3PcacheMakeDirty(pPg);
 45216   inJournal = pageInJournal(pPager, pPg);
 45217   if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
 45218     assert( !pagerUseWal(pPager) );
 45219   }else{
 45221     /* The transaction journal now exists and we have a RESERVED or an
 45222     ** EXCLUSIVE lock on the main database file.  Write the current page to
 45223     ** the transaction journal if it is not there already.
 45224     */
 45225     if( !inJournal && !pagerUseWal(pPager) ){
 45226       assert( pagerUseWal(pPager)==0 );
 45227       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
 45228         u32 cksum;
 45229         char *pData2;
 45230         i64 iOff = pPager->journalOff;
 45232         /* We should never write to the journal file the page that
 45233         ** contains the database locks.  The following assert verifies
 45234         ** that we do not. */
 45235         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
 45237         assert( pPager->journalHdr<=pPager->journalOff );
 45238         CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
 45239         cksum = pager_cksum(pPager, (u8*)pData2);
 45241         /* Even if an IO or diskfull error occurs while journalling the
 45242         ** page in the block above, set the need-sync flag for the page.
 45243         ** Otherwise, when the transaction is rolled back, the logic in
 45244         ** playback_one_page() will think that the page needs to be restored
 45245         ** in the database file. And if an IO error occurs while doing so,
 45246         ** then corruption may follow.
 45247         */
 45248         pPg->flags |= PGHDR_NEED_SYNC;
 45250         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
 45251         if( rc!=SQLITE_OK ) return rc;
 45252         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
 45253         if( rc!=SQLITE_OK ) return rc;
 45254         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
 45255         if( rc!=SQLITE_OK ) return rc;
 45257         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
 45258                  pPager->journalOff, pPager->pageSize));
 45259         PAGER_INCR(sqlite3_pager_writej_count);
 45260         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
 45261              PAGERID(pPager), pPg->pgno, 
 45262              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
 45264         pPager->journalOff += 8 + pPager->pageSize;
 45265         pPager->nRec++;
 45266         assert( pPager->pInJournal!=0 );
 45267         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
 45268         testcase( rc==SQLITE_NOMEM );
 45269         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
 45270         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
 45271         if( rc!=SQLITE_OK ){
 45272           assert( rc==SQLITE_NOMEM );
 45273           return rc;
 45275       }else{
 45276         if( pPager->eState!=PAGER_WRITER_DBMOD ){
 45277           pPg->flags |= PGHDR_NEED_SYNC;
 45279         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
 45280                 PAGERID(pPager), pPg->pgno,
 45281                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
 45285     /* If the statement journal is open and the page is not in it,
 45286     ** then write the current page to the statement journal.  Note that
 45287     ** the statement journal format differs from the standard journal format
 45288     ** in that it omits the checksums and the header.
 45289     */
 45290     if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
 45291       rc = subjournalPage(pPg);
 45295   /* Update the database size and return.
 45296   */
 45297   if( pPager->dbSize<pPg->pgno ){
 45298     pPager->dbSize = pPg->pgno;
 45300   return rc;
 45303 /*
 45304 ** Mark a data page as writeable. This routine must be called before 
 45305 ** making changes to a page. The caller must check the return value 
 45306 ** of this function and be careful not to change any page data unless 
 45307 ** this routine returns SQLITE_OK.
 45308 **
 45309 ** The difference between this function and pager_write() is that this
 45310 ** function also deals with the special case where 2 or more pages
 45311 ** fit on a single disk sector. In this case all co-resident pages
 45312 ** must have been written to the journal file before returning.
 45313 **
 45314 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
 45315 ** as appropriate. Otherwise, SQLITE_OK.
 45316 */
 45317 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
 45318   int rc = SQLITE_OK;
 45320   PgHdr *pPg = pDbPage;
 45321   Pager *pPager = pPg->pPager;
 45323   assert( (pPg->flags & PGHDR_MMAP)==0 );
 45324   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 45325   assert( pPager->eState!=PAGER_ERROR );
 45326   assert( assert_pager_state(pPager) );
 45328   if( pPager->sectorSize > (u32)pPager->pageSize ){
 45329     Pgno nPageCount;          /* Total number of pages in database file */
 45330     Pgno pg1;                 /* First page of the sector pPg is located on. */
 45331     int nPage = 0;            /* Number of pages starting at pg1 to journal */
 45332     int ii;                   /* Loop counter */
 45333     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
 45334     Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
 45336     /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
 45337     ** a journal header to be written between the pages journaled by
 45338     ** this function.
 45339     */
 45340     assert( !MEMDB );
 45341     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
 45342     pPager->doNotSpill |= SPILLFLAG_NOSYNC;
 45344     /* This trick assumes that both the page-size and sector-size are
 45345     ** an integer power of 2. It sets variable pg1 to the identifier
 45346     ** of the first page of the sector pPg is located on.
 45347     */
 45348     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
 45350     nPageCount = pPager->dbSize;
 45351     if( pPg->pgno>nPageCount ){
 45352       nPage = (pPg->pgno - pg1)+1;
 45353     }else if( (pg1+nPagePerSector-1)>nPageCount ){
 45354       nPage = nPageCount+1-pg1;
 45355     }else{
 45356       nPage = nPagePerSector;
 45358     assert(nPage>0);
 45359     assert(pg1<=pPg->pgno);
 45360     assert((pg1+nPage)>pPg->pgno);
 45362     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
 45363       Pgno pg = pg1+ii;
 45364       PgHdr *pPage;
 45365       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
 45366         if( pg!=PAGER_MJ_PGNO(pPager) ){
 45367           rc = sqlite3PagerGet(pPager, pg, &pPage);
 45368           if( rc==SQLITE_OK ){
 45369             rc = pager_write(pPage);
 45370             if( pPage->flags&PGHDR_NEED_SYNC ){
 45371               needSync = 1;
 45373             sqlite3PagerUnrefNotNull(pPage);
 45376       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
 45377         if( pPage->flags&PGHDR_NEED_SYNC ){
 45378           needSync = 1;
 45380         sqlite3PagerUnrefNotNull(pPage);
 45384     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
 45385     ** starting at pg1, then it needs to be set for all of them. Because
 45386     ** writing to any of these nPage pages may damage the others, the
 45387     ** journal file must contain sync()ed copies of all of them
 45388     ** before any of them can be written out to the database file.
 45389     */
 45390     if( rc==SQLITE_OK && needSync ){
 45391       assert( !MEMDB );
 45392       for(ii=0; ii<nPage; ii++){
 45393         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
 45394         if( pPage ){
 45395           pPage->flags |= PGHDR_NEED_SYNC;
 45396           sqlite3PagerUnrefNotNull(pPage);
 45401     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
 45402     pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
 45403   }else{
 45404     rc = pager_write(pDbPage);
 45406   return rc;
 45409 /*
 45410 ** Return TRUE if the page given in the argument was previously passed
 45411 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
 45412 ** to change the content of the page.
 45413 */
 45414 #ifndef NDEBUG
 45415 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
 45416   return pPg->flags&PGHDR_DIRTY;
 45418 #endif
 45420 /*
 45421 ** A call to this routine tells the pager that it is not necessary to
 45422 ** write the information on page pPg back to the disk, even though
 45423 ** that page might be marked as dirty.  This happens, for example, when
 45424 ** the page has been added as a leaf of the freelist and so its
 45425 ** content no longer matters.
 45426 **
 45427 ** The overlying software layer calls this routine when all of the data
 45428 ** on the given page is unused. The pager marks the page as clean so
 45429 ** that it does not get written to disk.
 45430 **
 45431 ** Tests show that this optimization can quadruple the speed of large 
 45432 ** DELETE operations.
 45433 */
 45434 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
 45435   Pager *pPager = pPg->pPager;
 45436   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
 45437     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
 45438     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
 45439     pPg->flags |= PGHDR_DONT_WRITE;
 45440     pager_set_pagehash(pPg);
 45444 /*
 45445 ** This routine is called to increment the value of the database file 
 45446 ** change-counter, stored as a 4-byte big-endian integer starting at 
 45447 ** byte offset 24 of the pager file.  The secondary change counter at
 45448 ** 92 is also updated, as is the SQLite version number at offset 96.
 45449 **
 45450 ** But this only happens if the pPager->changeCountDone flag is false.
 45451 ** To avoid excess churning of page 1, the update only happens once.
 45452 ** See also the pager_write_changecounter() routine that does an 
 45453 ** unconditional update of the change counters.
 45454 **
 45455 ** If the isDirectMode flag is zero, then this is done by calling 
 45456 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
 45457 ** page data. In this case the file will be updated when the current
 45458 ** transaction is committed.
 45459 **
 45460 ** The isDirectMode flag may only be non-zero if the library was compiled
 45461 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
 45462 ** if isDirect is non-zero, then the database file is updated directly
 45463 ** by writing an updated version of page 1 using a call to the 
 45464 ** sqlite3OsWrite() function.
 45465 */
 45466 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
 45467   int rc = SQLITE_OK;
 45469   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 45470        || pPager->eState==PAGER_WRITER_DBMOD
 45471   );
 45472   assert( assert_pager_state(pPager) );
 45474   /* Declare and initialize constant integer 'isDirect'. If the
 45475   ** atomic-write optimization is enabled in this build, then isDirect
 45476   ** is initialized to the value passed as the isDirectMode parameter
 45477   ** to this function. Otherwise, it is always set to zero.
 45478   **
 45479   ** The idea is that if the atomic-write optimization is not
 45480   ** enabled at compile time, the compiler can omit the tests of
 45481   ** 'isDirect' below, as well as the block enclosed in the
 45482   ** "if( isDirect )" condition.
 45483   */
 45484 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
 45485 # define DIRECT_MODE 0
 45486   assert( isDirectMode==0 );
 45487   UNUSED_PARAMETER(isDirectMode);
 45488 #else
 45489 # define DIRECT_MODE isDirectMode
 45490 #endif
 45492   if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
 45493     PgHdr *pPgHdr;                /* Reference to page 1 */
 45495     assert( !pPager->tempFile && isOpen(pPager->fd) );
 45497     /* Open page 1 of the file for writing. */
 45498     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
 45499     assert( pPgHdr==0 || rc==SQLITE_OK );
 45501     /* If page one was fetched successfully, and this function is not
 45502     ** operating in direct-mode, make page 1 writable.  When not in 
 45503     ** direct mode, page 1 is always held in cache and hence the PagerGet()
 45504     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
 45505     */
 45506     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
 45507       rc = sqlite3PagerWrite(pPgHdr);
 45510     if( rc==SQLITE_OK ){
 45511       /* Actually do the update of the change counter */
 45512       pager_write_changecounter(pPgHdr);
 45514       /* If running in direct mode, write the contents of page 1 to the file. */
 45515       if( DIRECT_MODE ){
 45516         const void *zBuf;
 45517         assert( pPager->dbFileSize>0 );
 45518         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
 45519         if( rc==SQLITE_OK ){
 45520           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
 45521           pPager->aStat[PAGER_STAT_WRITE]++;
 45523         if( rc==SQLITE_OK ){
 45524           /* Update the pager's copy of the change-counter. Otherwise, the
 45525           ** next time a read transaction is opened the cache will be
 45526           ** flushed (as the change-counter values will not match).  */
 45527           const void *pCopy = (const void *)&((const char *)zBuf)[24];
 45528           memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
 45529           pPager->changeCountDone = 1;
 45531       }else{
 45532         pPager->changeCountDone = 1;
 45536     /* Release the page reference. */
 45537     sqlite3PagerUnref(pPgHdr);
 45539   return rc;
 45542 /*
 45543 ** Sync the database file to disk. This is a no-op for in-memory databases
 45544 ** or pages with the Pager.noSync flag set.
 45545 **
 45546 ** If successful, or if called on a pager for which it is a no-op, this
 45547 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 45548 */
 45549 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
 45550   int rc = SQLITE_OK;
 45552   if( isOpen(pPager->fd) ){
 45553     void *pArg = (void*)zMaster;
 45554     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
 45555     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 45557   if( rc==SQLITE_OK && !pPager->noSync ){
 45558     assert( !MEMDB );
 45559     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 45561   return rc;
 45564 /*
 45565 ** This function may only be called while a write-transaction is active in
 45566 ** rollback. If the connection is in WAL mode, this call is a no-op. 
 45567 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on 
 45568 ** the database file, an attempt is made to obtain one.
 45569 **
 45570 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
 45571 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
 45572 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is 
 45573 ** returned.
 45574 */
 45575 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
 45576   int rc = SQLITE_OK;
 45577   assert( pPager->eState==PAGER_WRITER_CACHEMOD 
 45578        || pPager->eState==PAGER_WRITER_DBMOD 
 45579        || pPager->eState==PAGER_WRITER_LOCKED 
 45580   );
 45581   assert( assert_pager_state(pPager) );
 45582   if( 0==pagerUseWal(pPager) ){
 45583     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 45585   return rc;
 45588 /*
 45589 ** Sync the database file for the pager pPager. zMaster points to the name
 45590 ** of a master journal file that should be written into the individual
 45591 ** journal file. zMaster may be NULL, which is interpreted as no master
 45592 ** journal (a single database transaction).
 45593 **
 45594 ** This routine ensures that:
 45595 **
 45596 **   * The database file change-counter is updated,
 45597 **   * the journal is synced (unless the atomic-write optimization is used),
 45598 **   * all dirty pages are written to the database file, 
 45599 **   * the database file is truncated (if required), and
 45600 **   * the database file synced. 
 45601 **
 45602 ** The only thing that remains to commit the transaction is to finalize 
 45603 ** (delete, truncate or zero the first part of) the journal file (or 
 45604 ** delete the master journal file if specified).
 45605 **
 45606 ** Note that if zMaster==NULL, this does not overwrite a previous value
 45607 ** passed to an sqlite3PagerCommitPhaseOne() call.
 45608 **
 45609 ** If the final parameter - noSync - is true, then the database file itself
 45610 ** is not synced. The caller must call sqlite3PagerSync() directly to
 45611 ** sync the database file before calling CommitPhaseTwo() to delete the
 45612 ** journal file in this case.
 45613 */
 45614 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
 45615   Pager *pPager,                  /* Pager object */
 45616   const char *zMaster,            /* If not NULL, the master journal name */
 45617   int noSync                      /* True to omit the xSync on the db file */
 45618 ){
 45619   int rc = SQLITE_OK;             /* Return code */
 45621   assert( pPager->eState==PAGER_WRITER_LOCKED
 45622        || pPager->eState==PAGER_WRITER_CACHEMOD
 45623        || pPager->eState==PAGER_WRITER_DBMOD
 45624        || pPager->eState==PAGER_ERROR
 45625   );
 45626   assert( assert_pager_state(pPager) );
 45628   /* If a prior error occurred, report that error again. */
 45629   if( NEVER(pPager->errCode) ) return pPager->errCode;
 45631   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
 45632       pPager->zFilename, zMaster, pPager->dbSize));
 45634   /* If no database changes have been made, return early. */
 45635   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
 45637   if( MEMDB ){
 45638     /* If this is an in-memory db, or no pages have been written to, or this
 45639     ** function has already been called, it is mostly a no-op.  However, any
 45640     ** backup in progress needs to be restarted.
 45641     */
 45642     sqlite3BackupRestart(pPager->pBackup);
 45643   }else{
 45644     if( pagerUseWal(pPager) ){
 45645       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
 45646       PgHdr *pPageOne = 0;
 45647       if( pList==0 ){
 45648         /* Must have at least one page for the WAL commit flag.
 45649         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
 45650         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
 45651         pList = pPageOne;
 45652         pList->pDirty = 0;
 45654       assert( rc==SQLITE_OK );
 45655       if( ALWAYS(pList) ){
 45656         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
 45658       sqlite3PagerUnref(pPageOne);
 45659       if( rc==SQLITE_OK ){
 45660         sqlite3PcacheCleanAll(pPager->pPCache);
 45662     }else{
 45663       /* The following block updates the change-counter. Exactly how it
 45664       ** does this depends on whether or not the atomic-update optimization
 45665       ** was enabled at compile time, and if this transaction meets the 
 45666       ** runtime criteria to use the operation: 
 45667       **
 45668       **    * The file-system supports the atomic-write property for
 45669       **      blocks of size page-size, and 
 45670       **    * This commit is not part of a multi-file transaction, and
 45671       **    * Exactly one page has been modified and store in the journal file.
 45672       **
 45673       ** If the optimization was not enabled at compile time, then the
 45674       ** pager_incr_changecounter() function is called to update the change
 45675       ** counter in 'indirect-mode'. If the optimization is compiled in but
 45676       ** is not applicable to this transaction, call sqlite3JournalCreate()
 45677       ** to make sure the journal file has actually been created, then call
 45678       ** pager_incr_changecounter() to update the change-counter in indirect
 45679       ** mode. 
 45680       **
 45681       ** Otherwise, if the optimization is both enabled and applicable,
 45682       ** then call pager_incr_changecounter() to update the change-counter
 45683       ** in 'direct' mode. In this case the journal file will never be
 45684       ** created for this transaction.
 45685       */
 45686   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 45687       PgHdr *pPg;
 45688       assert( isOpen(pPager->jfd) 
 45689            || pPager->journalMode==PAGER_JOURNALMODE_OFF 
 45690            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
 45691       );
 45692       if( !zMaster && isOpen(pPager->jfd) 
 45693        && pPager->journalOff==jrnlBufferSize(pPager) 
 45694        && pPager->dbSize>=pPager->dbOrigSize
 45695        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
 45696       ){
 45697         /* Update the db file change counter via the direct-write method. The 
 45698         ** following call will modify the in-memory representation of page 1 
 45699         ** to include the updated change counter and then write page 1 
 45700         ** directly to the database file. Because of the atomic-write 
 45701         ** property of the host file-system, this is safe.
 45702         */
 45703         rc = pager_incr_changecounter(pPager, 1);
 45704       }else{
 45705         rc = sqlite3JournalCreate(pPager->jfd);
 45706         if( rc==SQLITE_OK ){
 45707           rc = pager_incr_changecounter(pPager, 0);
 45710   #else
 45711       rc = pager_incr_changecounter(pPager, 0);
 45712   #endif
 45713       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 45715       /* Write the master journal name into the journal file. If a master 
 45716       ** journal file name has already been written to the journal file, 
 45717       ** or if zMaster is NULL (no master journal), then this call is a no-op.
 45718       */
 45719       rc = writeMasterJournal(pPager, zMaster);
 45720       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 45722       /* Sync the journal file and write all dirty pages to the database.
 45723       ** If the atomic-update optimization is being used, this sync will not 
 45724       ** create the journal file or perform any real IO.
 45725       **
 45726       ** Because the change-counter page was just modified, unless the
 45727       ** atomic-update optimization is used it is almost certain that the
 45728       ** journal requires a sync here. However, in locking_mode=exclusive
 45729       ** on a system under memory pressure it is just possible that this is 
 45730       ** not the case. In this case it is likely enough that the redundant
 45731       ** xSync() call will be changed to a no-op by the OS anyhow. 
 45732       */
 45733       rc = syncJournal(pPager, 0);
 45734       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 45736       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 45737       if( rc!=SQLITE_OK ){
 45738         assert( rc!=SQLITE_IOERR_BLOCKED );
 45739         goto commit_phase_one_exit;
 45741       sqlite3PcacheCleanAll(pPager->pPCache);
 45743       /* If the file on disk is smaller than the database image, use 
 45744       ** pager_truncate to grow the file here. This can happen if the database
 45745       ** image was extended as part of the current transaction and then the
 45746       ** last page in the db image moved to the free-list. In this case the
 45747       ** last page is never written out to disk, leaving the database file
 45748       ** undersized. Fix this now if it is the case.  */
 45749       if( pPager->dbSize>pPager->dbFileSize ){
 45750         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
 45751         assert( pPager->eState==PAGER_WRITER_DBMOD );
 45752         rc = pager_truncate(pPager, nNew);
 45753         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 45756       /* Finally, sync the database file. */
 45757       if( !noSync ){
 45758         rc = sqlite3PagerSync(pPager, zMaster);
 45760       IOTRACE(("DBSYNC %p\n", pPager))
 45764 commit_phase_one_exit:
 45765   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
 45766     pPager->eState = PAGER_WRITER_FINISHED;
 45768   return rc;
 45772 /*
 45773 ** When this function is called, the database file has been completely
 45774 ** updated to reflect the changes made by the current transaction and
 45775 ** synced to disk. The journal file still exists in the file-system 
 45776 ** though, and if a failure occurs at this point it will eventually
 45777 ** be used as a hot-journal and the current transaction rolled back.
 45778 **
 45779 ** This function finalizes the journal file, either by deleting, 
 45780 ** truncating or partially zeroing it, so that it cannot be used 
 45781 ** for hot-journal rollback. Once this is done the transaction is
 45782 ** irrevocably committed.
 45783 **
 45784 ** If an error occurs, an IO error code is returned and the pager
 45785 ** moves into the error state. Otherwise, SQLITE_OK is returned.
 45786 */
 45787 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
 45788   int rc = SQLITE_OK;                  /* Return code */
 45790   /* This routine should not be called if a prior error has occurred.
 45791   ** But if (due to a coding error elsewhere in the system) it does get
 45792   ** called, just return the same error code without doing anything. */
 45793   if( NEVER(pPager->errCode) ) return pPager->errCode;
 45795   assert( pPager->eState==PAGER_WRITER_LOCKED
 45796        || pPager->eState==PAGER_WRITER_FINISHED
 45797        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
 45798   );
 45799   assert( assert_pager_state(pPager) );
 45801   /* An optimization. If the database was not actually modified during
 45802   ** this transaction, the pager is running in exclusive-mode and is
 45803   ** using persistent journals, then this function is a no-op.
 45804   **
 45805   ** The start of the journal file currently contains a single journal 
 45806   ** header with the nRec field set to 0. If such a journal is used as
 45807   ** a hot-journal during hot-journal rollback, 0 changes will be made
 45808   ** to the database file. So there is no need to zero the journal 
 45809   ** header. Since the pager is in exclusive mode, there is no need
 45810   ** to drop any locks either.
 45811   */
 45812   if( pPager->eState==PAGER_WRITER_LOCKED 
 45813    && pPager->exclusiveMode 
 45814    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
 45815   ){
 45816     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
 45817     pPager->eState = PAGER_READER;
 45818     return SQLITE_OK;
 45821   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
 45822   rc = pager_end_transaction(pPager, pPager->setMaster, 1);
 45823   return pager_error(pPager, rc);
 45826 /*
 45827 ** If a write transaction is open, then all changes made within the 
 45828 ** transaction are reverted and the current write-transaction is closed.
 45829 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
 45830 ** state if an error occurs.
 45831 **
 45832 ** If the pager is already in PAGER_ERROR state when this function is called,
 45833 ** it returns Pager.errCode immediately. No work is performed in this case.
 45834 **
 45835 ** Otherwise, in rollback mode, this function performs two functions:
 45836 **
 45837 **   1) It rolls back the journal file, restoring all database file and 
 45838 **      in-memory cache pages to the state they were in when the transaction
 45839 **      was opened, and
 45840 **
 45841 **   2) It finalizes the journal file, so that it is not used for hot
 45842 **      rollback at any point in the future.
 45843 **
 45844 ** Finalization of the journal file (task 2) is only performed if the 
 45845 ** rollback is successful.
 45846 **
 45847 ** In WAL mode, all cache-entries containing data modified within the
 45848 ** current transaction are either expelled from the cache or reverted to
 45849 ** their pre-transaction state by re-reading data from the database or
 45850 ** WAL files. The WAL transaction is then closed.
 45851 */
 45852 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
 45853   int rc = SQLITE_OK;                  /* Return code */
 45854   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
 45856   /* PagerRollback() is a no-op if called in READER or OPEN state. If
 45857   ** the pager is already in the ERROR state, the rollback is not 
 45858   ** attempted here. Instead, the error code is returned to the caller.
 45859   */
 45860   assert( assert_pager_state(pPager) );
 45861   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
 45862   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
 45864   if( pagerUseWal(pPager) ){
 45865     int rc2;
 45866     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
 45867     rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
 45868     if( rc==SQLITE_OK ) rc = rc2;
 45869   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
 45870     int eState = pPager->eState;
 45871     rc = pager_end_transaction(pPager, 0, 0);
 45872     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
 45873       /* This can happen using journal_mode=off. Move the pager to the error 
 45874       ** state to indicate that the contents of the cache may not be trusted.
 45875       ** Any active readers will get SQLITE_ABORT.
 45876       */
 45877       pPager->errCode = SQLITE_ABORT;
 45878       pPager->eState = PAGER_ERROR;
 45879       return rc;
 45881   }else{
 45882     rc = pager_playback(pPager, 0);
 45885   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
 45886   assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
 45887           || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR 
 45888           || rc==SQLITE_CANTOPEN
 45889   );
 45891   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
 45892   ** cache. So call pager_error() on the way out to make any error persistent.
 45893   */
 45894   return pager_error(pPager, rc);
 45897 /*
 45898 ** Return TRUE if the database file is opened read-only.  Return FALSE
 45899 ** if the database is (in theory) writable.
 45900 */
 45901 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
 45902   return pPager->readOnly;
 45905 /*
 45906 ** Return the number of references to the pager.
 45907 */
 45908 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
 45909   return sqlite3PcacheRefCount(pPager->pPCache);
 45912 /*
 45913 ** Return the approximate number of bytes of memory currently
 45914 ** used by the pager and its associated cache.
 45915 */
 45916 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
 45917   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
 45918                                      + 5*sizeof(void*);
 45919   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
 45920            + sqlite3MallocSize(pPager)
 45921            + pPager->pageSize;
 45924 /*
 45925 ** Return the number of references to the specified page.
 45926 */
 45927 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
 45928   return sqlite3PcachePageRefcount(pPage);
 45931 #ifdef SQLITE_TEST
 45932 /*
 45933 ** This routine is used for testing and analysis only.
 45934 */
 45935 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
 45936   static int a[11];
 45937   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
 45938   a[1] = sqlite3PcachePagecount(pPager->pPCache);
 45939   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
 45940   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
 45941   a[4] = pPager->eState;
 45942   a[5] = pPager->errCode;
 45943   a[6] = pPager->aStat[PAGER_STAT_HIT];
 45944   a[7] = pPager->aStat[PAGER_STAT_MISS];
 45945   a[8] = 0;  /* Used to be pPager->nOvfl */
 45946   a[9] = pPager->nRead;
 45947   a[10] = pPager->aStat[PAGER_STAT_WRITE];
 45948   return a;
 45950 #endif
 45952 /*
 45953 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 45954 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
 45955 ** current cache hit or miss count, according to the value of eStat. If the 
 45956 ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 45957 ** returning.
 45958 */
 45959 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 45961   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 45962        || eStat==SQLITE_DBSTATUS_CACHE_MISS
 45963        || eStat==SQLITE_DBSTATUS_CACHE_WRITE
 45964   );
 45966   assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 45967   assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 45968   assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
 45970   *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
 45971   if( reset ){
 45972     pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
 45976 /*
 45977 ** Return true if this is an in-memory pager.
 45978 */
 45979 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
 45980   return MEMDB;
 45983 /*
 45984 ** Check that there are at least nSavepoint savepoints open. If there are
 45985 ** currently less than nSavepoints open, then open one or more savepoints
 45986 ** to make up the difference. If the number of savepoints is already
 45987 ** equal to nSavepoint, then this function is a no-op.
 45988 **
 45989 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
 45990 ** occurs while opening the sub-journal file, then an IO error code is
 45991 ** returned. Otherwise, SQLITE_OK.
 45992 */
 45993 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
 45994   int rc = SQLITE_OK;                       /* Return code */
 45995   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
 45997   assert( pPager->eState>=PAGER_WRITER_LOCKED );
 45998   assert( assert_pager_state(pPager) );
 46000   if( nSavepoint>nCurrent && pPager->useJournal ){
 46001     int ii;                                 /* Iterator variable */
 46002     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
 46004     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
 46005     ** if the allocation fails. Otherwise, zero the new portion in case a 
 46006     ** malloc failure occurs while populating it in the for(...) loop below.
 46007     */
 46008     aNew = (PagerSavepoint *)sqlite3Realloc(
 46009         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
 46010     );
 46011     if( !aNew ){
 46012       return SQLITE_NOMEM;
 46014     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
 46015     pPager->aSavepoint = aNew;
 46017     /* Populate the PagerSavepoint structures just allocated. */
 46018     for(ii=nCurrent; ii<nSavepoint; ii++){
 46019       aNew[ii].nOrig = pPager->dbSize;
 46020       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
 46021         aNew[ii].iOffset = pPager->journalOff;
 46022       }else{
 46023         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
 46025       aNew[ii].iSubRec = pPager->nSubRec;
 46026       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
 46027       if( !aNew[ii].pInSavepoint ){
 46028         return SQLITE_NOMEM;
 46030       if( pagerUseWal(pPager) ){
 46031         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
 46033       pPager->nSavepoint = ii+1;
 46035     assert( pPager->nSavepoint==nSavepoint );
 46036     assertTruncateConstraint(pPager);
 46039   return rc;
 46042 /*
 46043 ** This function is called to rollback or release (commit) a savepoint.
 46044 ** The savepoint to release or rollback need not be the most recently 
 46045 ** created savepoint.
 46046 **
 46047 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
 46048 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
 46049 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
 46050 ** that have occurred since the specified savepoint was created.
 46051 **
 46052 ** The savepoint to rollback or release is identified by parameter 
 46053 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
 46054 ** (the first created). A value of (Pager.nSavepoint-1) means operate
 46055 ** on the most recently created savepoint. If iSavepoint is greater than
 46056 ** (Pager.nSavepoint-1), then this function is a no-op.
 46057 **
 46058 ** If a negative value is passed to this function, then the current
 46059 ** transaction is rolled back. This is different to calling 
 46060 ** sqlite3PagerRollback() because this function does not terminate
 46061 ** the transaction or unlock the database, it just restores the 
 46062 ** contents of the database to its original state. 
 46063 **
 46064 ** In any case, all savepoints with an index greater than iSavepoint 
 46065 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
 46066 ** then savepoint iSavepoint is also destroyed.
 46067 **
 46068 ** This function may return SQLITE_NOMEM if a memory allocation fails,
 46069 ** or an IO error code if an IO error occurs while rolling back a 
 46070 ** savepoint. If no errors occur, SQLITE_OK is returned.
 46071 */ 
 46072 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
 46073   int rc = pPager->errCode;       /* Return code */
 46075   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 46076   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
 46078   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
 46079     int ii;            /* Iterator variable */
 46080     int nNew;          /* Number of remaining savepoints after this op. */
 46082     /* Figure out how many savepoints will still be active after this
 46083     ** operation. Store this value in nNew. Then free resources associated 
 46084     ** with any savepoints that are destroyed by this operation.
 46085     */
 46086     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
 46087     for(ii=nNew; ii<pPager->nSavepoint; ii++){
 46088       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
 46090     pPager->nSavepoint = nNew;
 46092     /* If this is a release of the outermost savepoint, truncate 
 46093     ** the sub-journal to zero bytes in size. */
 46094     if( op==SAVEPOINT_RELEASE ){
 46095       if( nNew==0 && isOpen(pPager->sjfd) ){
 46096         /* Only truncate if it is an in-memory sub-journal. */
 46097         if( sqlite3IsMemJournal(pPager->sjfd) ){
 46098           rc = sqlite3OsTruncate(pPager->sjfd, 0);
 46099           assert( rc==SQLITE_OK );
 46101         pPager->nSubRec = 0;
 46104     /* Else this is a rollback operation, playback the specified savepoint.
 46105     ** If this is a temp-file, it is possible that the journal file has
 46106     ** not yet been opened. In this case there have been no changes to
 46107     ** the database file, so the playback operation can be skipped.
 46108     */
 46109     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
 46110       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
 46111       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
 46112       assert(rc!=SQLITE_DONE);
 46116   return rc;
 46119 /*
 46120 ** Return the full pathname of the database file.
 46121 **
 46122 ** Except, if the pager is in-memory only, then return an empty string if
 46123 ** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
 46124 ** used to report the filename to the user, for compatibility with legacy
 46125 ** behavior.  But when the Btree needs to know the filename for matching to
 46126 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
 46127 ** participate in shared-cache.
 46128 */
 46129 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
 46130   return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
 46133 /*
 46134 ** Return the VFS structure for the pager.
 46135 */
 46136 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
 46137   return pPager->pVfs;
 46140 /*
 46141 ** Return the file handle for the database file associated
 46142 ** with the pager.  This might return NULL if the file has
 46143 ** not yet been opened.
 46144 */
 46145 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 46146   return pPager->fd;
 46149 /*
 46150 ** Return the full pathname of the journal file.
 46151 */
 46152 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
 46153   return pPager->zJournal;
 46156 /*
 46157 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
 46158 ** if fsync()s are executed normally.
 46159 */
 46160 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
 46161   return pPager->noSync;
 46164 #ifdef SQLITE_HAS_CODEC
 46165 /*
 46166 ** Set or retrieve the codec for this pager
 46167 */
 46168 SQLITE_PRIVATE void sqlite3PagerSetCodec(
 46169   Pager *pPager,
 46170   void *(*xCodec)(void*,void*,Pgno,int),
 46171   void (*xCodecSizeChng)(void*,int,int),
 46172   void (*xCodecFree)(void*),
 46173   void *pCodec
 46174 ){
 46175   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
 46176   pPager->xCodec = pPager->memDb ? 0 : xCodec;
 46177   pPager->xCodecSizeChng = xCodecSizeChng;
 46178   pPager->xCodecFree = xCodecFree;
 46179   pPager->pCodec = pCodec;
 46180   pagerReportSize(pPager);
 46182 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
 46183   return pPager->pCodec;
 46186 /*
 46187 ** This function is called by the wal module when writing page content
 46188 ** into the log file.
 46189 **
 46190 ** This function returns a pointer to a buffer containing the encrypted
 46191 ** page content. If a malloc fails, this function may return NULL.
 46192 */
 46193 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
 46194   void *aData = 0;
 46195   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
 46196   return aData;
 46199 /*
 46200 ** Return the current pager state
 46201 */
 46202 SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
 46203   return pPager->eState;
 46205 #endif /* SQLITE_HAS_CODEC */
 46207 #ifndef SQLITE_OMIT_AUTOVACUUM
 46208 /*
 46209 ** Move the page pPg to location pgno in the file.
 46210 **
 46211 ** There must be no references to the page previously located at
 46212 ** pgno (which we call pPgOld) though that page is allowed to be
 46213 ** in cache.  If the page previously located at pgno is not already
 46214 ** in the rollback journal, it is not put there by by this routine.
 46215 **
 46216 ** References to the page pPg remain valid. Updating any
 46217 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
 46218 ** allocated along with the page) is the responsibility of the caller.
 46219 **
 46220 ** A transaction must be active when this routine is called. It used to be
 46221 ** required that a statement transaction was not active, but this restriction
 46222 ** has been removed (CREATE INDEX needs to move a page when a statement
 46223 ** transaction is active).
 46224 **
 46225 ** If the fourth argument, isCommit, is non-zero, then this page is being
 46226 ** moved as part of a database reorganization just before the transaction 
 46227 ** is being committed. In this case, it is guaranteed that the database page 
 46228 ** pPg refers to will not be written to again within this transaction.
 46229 **
 46230 ** This function may return SQLITE_NOMEM or an IO error code if an error
 46231 ** occurs. Otherwise, it returns SQLITE_OK.
 46232 */
 46233 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
 46234   PgHdr *pPgOld;               /* The page being overwritten. */
 46235   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
 46236   int rc;                      /* Return code */
 46237   Pgno origPgno;               /* The original page number */
 46239   assert( pPg->nRef>0 );
 46240   assert( pPager->eState==PAGER_WRITER_CACHEMOD
 46241        || pPager->eState==PAGER_WRITER_DBMOD
 46242   );
 46243   assert( assert_pager_state(pPager) );
 46245   /* In order to be able to rollback, an in-memory database must journal
 46246   ** the page we are moving from.
 46247   */
 46248   if( MEMDB ){
 46249     rc = sqlite3PagerWrite(pPg);
 46250     if( rc ) return rc;
 46253   /* If the page being moved is dirty and has not been saved by the latest
 46254   ** savepoint, then save the current contents of the page into the 
 46255   ** sub-journal now. This is required to handle the following scenario:
 46256   **
 46257   **   BEGIN;
 46258   **     <journal page X, then modify it in memory>
 46259   **     SAVEPOINT one;
 46260   **       <Move page X to location Y>
 46261   **     ROLLBACK TO one;
 46262   **
 46263   ** If page X were not written to the sub-journal here, it would not
 46264   ** be possible to restore its contents when the "ROLLBACK TO one"
 46265   ** statement were is processed.
 46266   **
 46267   ** subjournalPage() may need to allocate space to store pPg->pgno into
 46268   ** one or more savepoint bitvecs. This is the reason this function
 46269   ** may return SQLITE_NOMEM.
 46270   */
 46271   if( pPg->flags&PGHDR_DIRTY
 46272    && subjRequiresPage(pPg)
 46273    && SQLITE_OK!=(rc = subjournalPage(pPg))
 46274   ){
 46275     return rc;
 46278   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
 46279       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
 46280   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
 46282   /* If the journal needs to be sync()ed before page pPg->pgno can
 46283   ** be written to, store pPg->pgno in local variable needSyncPgno.
 46284   **
 46285   ** If the isCommit flag is set, there is no need to remember that
 46286   ** the journal needs to be sync()ed before database page pPg->pgno 
 46287   ** can be written to. The caller has already promised not to write to it.
 46288   */
 46289   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
 46290     needSyncPgno = pPg->pgno;
 46291     assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
 46292             pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
 46293     assert( pPg->flags&PGHDR_DIRTY );
 46296   /* If the cache contains a page with page-number pgno, remove it
 46297   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
 46298   ** page pgno before the 'move' operation, it needs to be retained 
 46299   ** for the page moved there.
 46300   */
 46301   pPg->flags &= ~PGHDR_NEED_SYNC;
 46302   pPgOld = pager_lookup(pPager, pgno);
 46303   assert( !pPgOld || pPgOld->nRef==1 );
 46304   if( pPgOld ){
 46305     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
 46306     if( MEMDB ){
 46307       /* Do not discard pages from an in-memory database since we might
 46308       ** need to rollback later.  Just move the page out of the way. */
 46309       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
 46310     }else{
 46311       sqlite3PcacheDrop(pPgOld);
 46315   origPgno = pPg->pgno;
 46316   sqlite3PcacheMove(pPg, pgno);
 46317   sqlite3PcacheMakeDirty(pPg);
 46319   /* For an in-memory database, make sure the original page continues
 46320   ** to exist, in case the transaction needs to roll back.  Use pPgOld
 46321   ** as the original page since it has already been allocated.
 46322   */
 46323   if( MEMDB ){
 46324     assert( pPgOld );
 46325     sqlite3PcacheMove(pPgOld, origPgno);
 46326     sqlite3PagerUnrefNotNull(pPgOld);
 46329   if( needSyncPgno ){
 46330     /* If needSyncPgno is non-zero, then the journal file needs to be 
 46331     ** sync()ed before any data is written to database file page needSyncPgno.
 46332     ** Currently, no such page exists in the page-cache and the 
 46333     ** "is journaled" bitvec flag has been set. This needs to be remedied by
 46334     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
 46335     ** flag.
 46336     **
 46337     ** If the attempt to load the page into the page-cache fails, (due
 46338     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
 46339     ** array. Otherwise, if the page is loaded and written again in
 46340     ** this transaction, it may be written to the database file before
 46341     ** it is synced into the journal file. This way, it may end up in
 46342     ** the journal file twice, but that is not a problem.
 46343     */
 46344     PgHdr *pPgHdr;
 46345     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
 46346     if( rc!=SQLITE_OK ){
 46347       if( needSyncPgno<=pPager->dbOrigSize ){
 46348         assert( pPager->pTmpSpace!=0 );
 46349         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
 46351       return rc;
 46353     pPgHdr->flags |= PGHDR_NEED_SYNC;
 46354     sqlite3PcacheMakeDirty(pPgHdr);
 46355     sqlite3PagerUnrefNotNull(pPgHdr);
 46358   return SQLITE_OK;
 46360 #endif
 46362 /*
 46363 ** Return a pointer to the data for the specified page.
 46364 */
 46365 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
 46366   assert( pPg->nRef>0 || pPg->pPager->memDb );
 46367   return pPg->pData;
 46370 /*
 46371 ** Return a pointer to the Pager.nExtra bytes of "extra" space 
 46372 ** allocated along with the specified page.
 46373 */
 46374 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
 46375   return pPg->pExtra;
 46378 /*
 46379 ** Get/set the locking-mode for this pager. Parameter eMode must be one
 46380 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
 46381 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
 46382 ** the locking-mode is set to the value specified.
 46383 **
 46384 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
 46385 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
 46386 ** locking-mode.
 46387 */
 46388 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
 46389   assert( eMode==PAGER_LOCKINGMODE_QUERY
 46390             || eMode==PAGER_LOCKINGMODE_NORMAL
 46391             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
 46392   assert( PAGER_LOCKINGMODE_QUERY<0 );
 46393   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
 46394   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
 46395   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
 46396     pPager->exclusiveMode = (u8)eMode;
 46398   return (int)pPager->exclusiveMode;
 46401 /*
 46402 ** Set the journal-mode for this pager. Parameter eMode must be one of:
 46403 **
 46404 **    PAGER_JOURNALMODE_DELETE
 46405 **    PAGER_JOURNALMODE_TRUNCATE
 46406 **    PAGER_JOURNALMODE_PERSIST
 46407 **    PAGER_JOURNALMODE_OFF
 46408 **    PAGER_JOURNALMODE_MEMORY
 46409 **    PAGER_JOURNALMODE_WAL
 46410 **
 46411 ** The journalmode is set to the value specified if the change is allowed.
 46412 ** The change may be disallowed for the following reasons:
 46413 **
 46414 **   *  An in-memory database can only have its journal_mode set to _OFF
 46415 **      or _MEMORY.
 46416 **
 46417 **   *  Temporary databases cannot have _WAL journalmode.
 46418 **
 46419 ** The returned indicate the current (possibly updated) journal-mode.
 46420 */
 46421 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
 46422   u8 eOld = pPager->journalMode;    /* Prior journalmode */
 46424 #ifdef SQLITE_DEBUG
 46425   /* The print_pager_state() routine is intended to be used by the debugger
 46426   ** only.  We invoke it once here to suppress a compiler warning. */
 46427   print_pager_state(pPager);
 46428 #endif
 46431   /* The eMode parameter is always valid */
 46432   assert(      eMode==PAGER_JOURNALMODE_DELETE
 46433             || eMode==PAGER_JOURNALMODE_TRUNCATE
 46434             || eMode==PAGER_JOURNALMODE_PERSIST
 46435             || eMode==PAGER_JOURNALMODE_OFF 
 46436             || eMode==PAGER_JOURNALMODE_WAL 
 46437             || eMode==PAGER_JOURNALMODE_MEMORY );
 46439   /* This routine is only called from the OP_JournalMode opcode, and
 46440   ** the logic there will never allow a temporary file to be changed
 46441   ** to WAL mode.
 46442   */
 46443   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
 46445   /* Do allow the journalmode of an in-memory database to be set to
 46446   ** anything other than MEMORY or OFF
 46447   */
 46448   if( MEMDB ){
 46449     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
 46450     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
 46451       eMode = eOld;
 46455   if( eMode!=eOld ){
 46457     /* Change the journal mode. */
 46458     assert( pPager->eState!=PAGER_ERROR );
 46459     pPager->journalMode = (u8)eMode;
 46461     /* When transistioning from TRUNCATE or PERSIST to any other journal
 46462     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
 46463     ** delete the journal file.
 46464     */
 46465     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
 46466     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
 46467     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
 46468     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
 46469     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
 46470     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
 46472     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
 46473     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
 46475       /* In this case we would like to delete the journal file. If it is
 46476       ** not possible, then that is not a problem. Deleting the journal file
 46477       ** here is an optimization only.
 46478       **
 46479       ** Before deleting the journal file, obtain a RESERVED lock on the
 46480       ** database file. This ensures that the journal file is not deleted
 46481       ** while it is in use by some other client.
 46482       */
 46483       sqlite3OsClose(pPager->jfd);
 46484       if( pPager->eLock>=RESERVED_LOCK ){
 46485         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 46486       }else{
 46487         int rc = SQLITE_OK;
 46488         int state = pPager->eState;
 46489         assert( state==PAGER_OPEN || state==PAGER_READER );
 46490         if( state==PAGER_OPEN ){
 46491           rc = sqlite3PagerSharedLock(pPager);
 46493         if( pPager->eState==PAGER_READER ){
 46494           assert( rc==SQLITE_OK );
 46495           rc = pagerLockDb(pPager, RESERVED_LOCK);
 46497         if( rc==SQLITE_OK ){
 46498           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 46500         if( rc==SQLITE_OK && state==PAGER_READER ){
 46501           pagerUnlockDb(pPager, SHARED_LOCK);
 46502         }else if( state==PAGER_OPEN ){
 46503           pager_unlock(pPager);
 46505         assert( state==pPager->eState );
 46510   /* Return the new journal mode */
 46511   return (int)pPager->journalMode;
 46514 /*
 46515 ** Return the current journal mode.
 46516 */
 46517 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
 46518   return (int)pPager->journalMode;
 46521 /*
 46522 ** Return TRUE if the pager is in a state where it is OK to change the
 46523 ** journalmode.  Journalmode changes can only happen when the database
 46524 ** is unmodified.
 46525 */
 46526 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
 46527   assert( assert_pager_state(pPager) );
 46528   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
 46529   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
 46530   return 1;
 46533 /*
 46534 ** Get/set the size-limit used for persistent journal files.
 46535 **
 46536 ** Setting the size limit to -1 means no limit is enforced.
 46537 ** An attempt to set a limit smaller than -1 is a no-op.
 46538 */
 46539 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
 46540   if( iLimit>=-1 ){
 46541     pPager->journalSizeLimit = iLimit;
 46542     sqlite3WalLimit(pPager->pWal, iLimit);
 46544   return pPager->journalSizeLimit;
 46547 /*
 46548 ** Return a pointer to the pPager->pBackup variable. The backup module
 46549 ** in backup.c maintains the content of this variable. This module
 46550 ** uses it opaquely as an argument to sqlite3BackupRestart() and
 46551 ** sqlite3BackupUpdate() only.
 46552 */
 46553 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
 46554   return &pPager->pBackup;
 46557 #ifndef SQLITE_OMIT_VACUUM
 46558 /*
 46559 ** Unless this is an in-memory or temporary database, clear the pager cache.
 46560 */
 46561 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
 46562   if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
 46564 #endif
 46566 #ifndef SQLITE_OMIT_WAL
 46567 /*
 46568 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
 46569 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
 46570 ** or wal_blocking_checkpoint() API functions.
 46571 **
 46572 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 46573 */
 46574 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
 46575   int rc = SQLITE_OK;
 46576   if( pPager->pWal ){
 46577     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
 46578         pPager->xBusyHandler, pPager->pBusyHandlerArg,
 46579         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 46580         pnLog, pnCkpt
 46581     );
 46583   return rc;
 46586 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 46587   return sqlite3WalCallback(pPager->pWal);
 46590 /*
 46591 ** Return true if the underlying VFS for the given pager supports the
 46592 ** primitives necessary for write-ahead logging.
 46593 */
 46594 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
 46595   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
 46596   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
 46599 /*
 46600 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
 46601 ** is obtained instead, immediately release it.
 46602 */
 46603 static int pagerExclusiveLock(Pager *pPager){
 46604   int rc;                         /* Return code */
 46606   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 46607   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 46608   if( rc!=SQLITE_OK ){
 46609     /* If the attempt to grab the exclusive lock failed, release the 
 46610     ** pending lock that may have been obtained instead.  */
 46611     pagerUnlockDb(pPager, SHARED_LOCK);
 46614   return rc;
 46617 /*
 46618 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 
 46619 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
 46620 ** lock on the database file and use heap-memory to store the wal-index
 46621 ** in. Otherwise, use the normal shared-memory.
 46622 */
 46623 static int pagerOpenWal(Pager *pPager){
 46624   int rc = SQLITE_OK;
 46626   assert( pPager->pWal==0 && pPager->tempFile==0 );
 46627   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 46629   /* If the pager is already in exclusive-mode, the WAL module will use 
 46630   ** heap-memory for the wal-index instead of the VFS shared-memory 
 46631   ** implementation. Take the exclusive lock now, before opening the WAL
 46632   ** file, to make sure this is safe.
 46633   */
 46634   if( pPager->exclusiveMode ){
 46635     rc = pagerExclusiveLock(pPager);
 46638   /* Open the connection to the log file. If this operation fails, 
 46639   ** (e.g. due to malloc() failure), return an error code.
 46640   */
 46641   if( rc==SQLITE_OK ){
 46642     rc = sqlite3WalOpen(pPager->pVfs,
 46643         pPager->fd, pPager->zWal, pPager->exclusiveMode,
 46644         pPager->journalSizeLimit, &pPager->pWal
 46645     );
 46647   pagerFixMaplimit(pPager);
 46649   return rc;
 46653 /*
 46654 ** The caller must be holding a SHARED lock on the database file to call
 46655 ** this function.
 46656 **
 46657 ** If the pager passed as the first argument is open on a real database
 46658 ** file (not a temp file or an in-memory database), and the WAL file
 46659 ** is not already open, make an attempt to open it now. If successful,
 46660 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does 
 46661 ** not support the xShmXXX() methods, return an error code. *pbOpen is
 46662 ** not modified in either case.
 46663 **
 46664 ** If the pager is open on a temp-file (or in-memory database), or if
 46665 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
 46666 ** without doing anything.
 46667 */
 46668 SQLITE_PRIVATE int sqlite3PagerOpenWal(
 46669   Pager *pPager,                  /* Pager object */
 46670   int *pbOpen                     /* OUT: Set to true if call is a no-op */
 46671 ){
 46672   int rc = SQLITE_OK;             /* Return code */
 46674   assert( assert_pager_state(pPager) );
 46675   assert( pPager->eState==PAGER_OPEN   || pbOpen );
 46676   assert( pPager->eState==PAGER_READER || !pbOpen );
 46677   assert( pbOpen==0 || *pbOpen==0 );
 46678   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
 46680   if( !pPager->tempFile && !pPager->pWal ){
 46681     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
 46683     /* Close any rollback journal previously open */
 46684     sqlite3OsClose(pPager->jfd);
 46686     rc = pagerOpenWal(pPager);
 46687     if( rc==SQLITE_OK ){
 46688       pPager->journalMode = PAGER_JOURNALMODE_WAL;
 46689       pPager->eState = PAGER_OPEN;
 46691   }else{
 46692     *pbOpen = 1;
 46695   return rc;
 46698 /*
 46699 ** This function is called to close the connection to the log file prior
 46700 ** to switching from WAL to rollback mode.
 46701 **
 46702 ** Before closing the log file, this function attempts to take an 
 46703 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
 46704 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
 46705 ** If successful, the EXCLUSIVE lock is not released before returning.
 46706 */
 46707 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
 46708   int rc = SQLITE_OK;
 46710   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
 46712   /* If the log file is not already open, but does exist in the file-system,
 46713   ** it may need to be checkpointed before the connection can switch to
 46714   ** rollback mode. Open it now so this can happen.
 46715   */
 46716   if( !pPager->pWal ){
 46717     int logexists = 0;
 46718     rc = pagerLockDb(pPager, SHARED_LOCK);
 46719     if( rc==SQLITE_OK ){
 46720       rc = sqlite3OsAccess(
 46721           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
 46722       );
 46724     if( rc==SQLITE_OK && logexists ){
 46725       rc = pagerOpenWal(pPager);
 46729   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
 46730   ** the database file, the log and log-summary files will be deleted.
 46731   */
 46732   if( rc==SQLITE_OK && pPager->pWal ){
 46733     rc = pagerExclusiveLock(pPager);
 46734     if( rc==SQLITE_OK ){
 46735       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
 46736                            pPager->pageSize, (u8*)pPager->pTmpSpace);
 46737       pPager->pWal = 0;
 46738       pagerFixMaplimit(pPager);
 46741   return rc;
 46744 #endif /* !SQLITE_OMIT_WAL */
 46746 #ifdef SQLITE_ENABLE_ZIPVFS
 46747 /*
 46748 ** A read-lock must be held on the pager when this function is called. If
 46749 ** the pager is in WAL mode and the WAL file currently contains one or more
 46750 ** frames, return the size in bytes of the page images stored within the
 46751 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
 46752 ** is empty, return 0.
 46753 */
 46754 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
 46755   assert( pPager->eState==PAGER_READER );
 46756   return sqlite3WalFramesize(pPager->pWal);
 46758 #endif
 46760 #endif /* SQLITE_OMIT_DISKIO */
 46762 /************** End of pager.c ***********************************************/
 46763 /************** Begin file wal.c *********************************************/
 46764 /*
 46765 ** 2010 February 1
 46766 **
 46767 ** The author disclaims copyright to this source code.  In place of
 46768 ** a legal notice, here is a blessing:
 46769 **
 46770 **    May you do good and not evil.
 46771 **    May you find forgiveness for yourself and forgive others.
 46772 **    May you share freely, never taking more than you give.
 46773 **
 46774 *************************************************************************
 46775 **
 46776 ** This file contains the implementation of a write-ahead log (WAL) used in 
 46777 ** "journal_mode=WAL" mode.
 46778 **
 46779 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
 46780 **
 46781 ** A WAL file consists of a header followed by zero or more "frames".
 46782 ** Each frame records the revised content of a single page from the
 46783 ** database file.  All changes to the database are recorded by writing
 46784 ** frames into the WAL.  Transactions commit when a frame is written that
 46785 ** contains a commit marker.  A single WAL can and usually does record 
 46786 ** multiple transactions.  Periodically, the content of the WAL is
 46787 ** transferred back into the database file in an operation called a
 46788 ** "checkpoint".
 46789 **
 46790 ** A single WAL file can be used multiple times.  In other words, the
 46791 ** WAL can fill up with frames and then be checkpointed and then new
 46792 ** frames can overwrite the old ones.  A WAL always grows from beginning
 46793 ** toward the end.  Checksums and counters attached to each frame are
 46794 ** used to determine which frames within the WAL are valid and which
 46795 ** are leftovers from prior checkpoints.
 46796 **
 46797 ** The WAL header is 32 bytes in size and consists of the following eight
 46798 ** big-endian 32-bit unsigned integer values:
 46799 **
 46800 **     0: Magic number.  0x377f0682 or 0x377f0683
 46801 **     4: File format version.  Currently 3007000
 46802 **     8: Database page size.  Example: 1024
 46803 **    12: Checkpoint sequence number
 46804 **    16: Salt-1, random integer incremented with each checkpoint
 46805 **    20: Salt-2, a different random integer changing with each ckpt
 46806 **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
 46807 **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
 46808 **
 46809 ** Immediately following the wal-header are zero or more frames. Each
 46810 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
 46811 ** of page data. The frame-header is six big-endian 32-bit unsigned 
 46812 ** integer values, as follows:
 46813 **
 46814 **     0: Page number.
 46815 **     4: For commit records, the size of the database image in pages 
 46816 **        after the commit. For all other records, zero.
 46817 **     8: Salt-1 (copied from the header)
 46818 **    12: Salt-2 (copied from the header)
 46819 **    16: Checksum-1.
 46820 **    20: Checksum-2.
 46821 **
 46822 ** A frame is considered valid if and only if the following conditions are
 46823 ** true:
 46824 **
 46825 **    (1) The salt-1 and salt-2 values in the frame-header match
 46826 **        salt values in the wal-header
 46827 **
 46828 **    (2) The checksum values in the final 8 bytes of the frame-header
 46829 **        exactly match the checksum computed consecutively on the
 46830 **        WAL header and the first 8 bytes and the content of all frames
 46831 **        up to and including the current frame.
 46832 **
 46833 ** The checksum is computed using 32-bit big-endian integers if the
 46834 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
 46835 ** is computed using little-endian if the magic number is 0x377f0682.
 46836 ** The checksum values are always stored in the frame header in a
 46837 ** big-endian format regardless of which byte order is used to compute
 46838 ** the checksum.  The checksum is computed by interpreting the input as
 46839 ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
 46840 ** algorithm used for the checksum is as follows:
 46841 ** 
 46842 **   for i from 0 to n-1 step 2:
 46843 **     s0 += x[i] + s1;
 46844 **     s1 += x[i+1] + s0;
 46845 **   endfor
 46846 **
 46847 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
 46848 ** in reverse order (the largest fibonacci weight occurs on the first element
 46849 ** of the sequence being summed.)  The s1 value spans all 32-bit 
 46850 ** terms of the sequence whereas s0 omits the final term.
 46851 **
 46852 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
 46853 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
 46854 ** The VFS.xSync operations serve as write barriers - all writes launched
 46855 ** before the xSync must complete before any write that launches after the
 46856 ** xSync begins.
 46857 **
 46858 ** After each checkpoint, the salt-1 value is incremented and the salt-2
 46859 ** value is randomized.  This prevents old and new frames in the WAL from
 46860 ** being considered valid at the same time and being checkpointing together
 46861 ** following a crash.
 46862 **
 46863 ** READER ALGORITHM
 46864 **
 46865 ** To read a page from the database (call it page number P), a reader
 46866 ** first checks the WAL to see if it contains page P.  If so, then the
 46867 ** last valid instance of page P that is a followed by a commit frame
 46868 ** or is a commit frame itself becomes the value read.  If the WAL
 46869 ** contains no copies of page P that are valid and which are a commit
 46870 ** frame or are followed by a commit frame, then page P is read from
 46871 ** the database file.
 46872 **
 46873 ** To start a read transaction, the reader records the index of the last
 46874 ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
 46875 ** for all subsequent read operations.  New transactions can be appended
 46876 ** to the WAL, but as long as the reader uses its original mxFrame value
 46877 ** and ignores the newly appended content, it will see a consistent snapshot
 46878 ** of the database from a single point in time.  This technique allows
 46879 ** multiple concurrent readers to view different versions of the database
 46880 ** content simultaneously.
 46881 **
 46882 ** The reader algorithm in the previous paragraphs works correctly, but 
 46883 ** because frames for page P can appear anywhere within the WAL, the
 46884 ** reader has to scan the entire WAL looking for page P frames.  If the
 46885 ** WAL is large (multiple megabytes is typical) that scan can be slow,
 46886 ** and read performance suffers.  To overcome this problem, a separate
 46887 ** data structure called the wal-index is maintained to expedite the
 46888 ** search for frames of a particular page.
 46889 ** 
 46890 ** WAL-INDEX FORMAT
 46891 **
 46892 ** Conceptually, the wal-index is shared memory, though VFS implementations
 46893 ** might choose to implement the wal-index using a mmapped file.  Because
 46894 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
 46895 ** on a network filesystem.  All users of the database must be able to
 46896 ** share memory.
 46897 **
 46898 ** The wal-index is transient.  After a crash, the wal-index can (and should
 46899 ** be) reconstructed from the original WAL file.  In fact, the VFS is required
 46900 ** to either truncate or zero the header of the wal-index when the last
 46901 ** connection to it closes.  Because the wal-index is transient, it can
 46902 ** use an architecture-specific format; it does not have to be cross-platform.
 46903 ** Hence, unlike the database and WAL file formats which store all values
 46904 ** as big endian, the wal-index can store multi-byte values in the native
 46905 ** byte order of the host computer.
 46906 **
 46907 ** The purpose of the wal-index is to answer this question quickly:  Given
 46908 ** a page number P and a maximum frame index M, return the index of the 
 46909 ** last frame in the wal before frame M for page P in the WAL, or return
 46910 ** NULL if there are no frames for page P in the WAL prior to M.
 46911 **
 46912 ** The wal-index consists of a header region, followed by an one or
 46913 ** more index blocks.  
 46914 **
 46915 ** The wal-index header contains the total number of frames within the WAL
 46916 ** in the mxFrame field.
 46917 **
 46918 ** Each index block except for the first contains information on 
 46919 ** HASHTABLE_NPAGE frames. The first index block contains information on
 46920 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and 
 46921 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
 46922 ** first index block are the same size as all other index blocks in the
 46923 ** wal-index.
 46924 **
 46925 ** Each index block contains two sections, a page-mapping that contains the
 46926 ** database page number associated with each wal frame, and a hash-table 
 46927 ** that allows readers to query an index block for a specific page number.
 46928 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
 46929 ** for the first index block) 32-bit page numbers. The first entry in the 
 46930 ** first index-block contains the database page number corresponding to the
 46931 ** first frame in the WAL file. The first entry in the second index block
 46932 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
 46933 ** the log, and so on.
 46934 **
 46935 ** The last index block in a wal-index usually contains less than the full
 46936 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
 46937 ** depending on the contents of the WAL file. This does not change the
 46938 ** allocated size of the page-mapping array - the page-mapping array merely
 46939 ** contains unused entries.
 46940 **
 46941 ** Even without using the hash table, the last frame for page P
 46942 ** can be found by scanning the page-mapping sections of each index block
 46943 ** starting with the last index block and moving toward the first, and
 46944 ** within each index block, starting at the end and moving toward the
 46945 ** beginning.  The first entry that equals P corresponds to the frame
 46946 ** holding the content for that page.
 46947 **
 46948 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
 46949 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
 46950 ** hash table for each page number in the mapping section, so the hash 
 46951 ** table is never more than half full.  The expected number of collisions 
 46952 ** prior to finding a match is 1.  Each entry of the hash table is an
 46953 ** 1-based index of an entry in the mapping section of the same
 46954 ** index block.   Let K be the 1-based index of the largest entry in
 46955 ** the mapping section.  (For index blocks other than the last, K will
 46956 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
 46957 ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
 46958 ** contain a value of 0.
 46959 **
 46960 ** To look for page P in the hash table, first compute a hash iKey on
 46961 ** P as follows:
 46962 **
 46963 **      iKey = (P * 383) % HASHTABLE_NSLOT
 46964 **
 46965 ** Then start scanning entries of the hash table, starting with iKey
 46966 ** (wrapping around to the beginning when the end of the hash table is
 46967 ** reached) until an unused hash slot is found. Let the first unused slot
 46968 ** be at index iUnused.  (iUnused might be less than iKey if there was
 46969 ** wrap-around.) Because the hash table is never more than half full,
 46970 ** the search is guaranteed to eventually hit an unused entry.  Let 
 46971 ** iMax be the value between iKey and iUnused, closest to iUnused,
 46972 ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
 46973 ** no hash slot such that aHash[i]==p) then page P is not in the
 46974 ** current index block.  Otherwise the iMax-th mapping entry of the
 46975 ** current index block corresponds to the last entry that references 
 46976 ** page P.
 46977 **
 46978 ** A hash search begins with the last index block and moves toward the
 46979 ** first index block, looking for entries corresponding to page P.  On
 46980 ** average, only two or three slots in each index block need to be
 46981 ** examined in order to either find the last entry for page P, or to
 46982 ** establish that no such entry exists in the block.  Each index block
 46983 ** holds over 4000 entries.  So two or three index blocks are sufficient
 46984 ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
 46985 ** comparisons (on average) suffice to either locate a frame in the
 46986 ** WAL or to establish that the frame does not exist in the WAL.  This
 46987 ** is much faster than scanning the entire 10MB WAL.
 46988 **
 46989 ** Note that entries are added in order of increasing K.  Hence, one
 46990 ** reader might be using some value K0 and a second reader that started
 46991 ** at a later time (after additional transactions were added to the WAL
 46992 ** and to the wal-index) might be using a different value K1, where K1>K0.
 46993 ** Both readers can use the same hash table and mapping section to get
 46994 ** the correct result.  There may be entries in the hash table with
 46995 ** K>K0 but to the first reader, those entries will appear to be unused
 46996 ** slots in the hash table and so the first reader will get an answer as
 46997 ** if no values greater than K0 had ever been inserted into the hash table
 46998 ** in the first place - which is what reader one wants.  Meanwhile, the
 46999 ** second reader using K1 will see additional values that were inserted
 47000 ** later, which is exactly what reader two wants.  
 47001 **
 47002 ** When a rollback occurs, the value of K is decreased. Hash table entries
 47003 ** that correspond to frames greater than the new K value are removed
 47004 ** from the hash table at this point.
 47005 */
 47006 #ifndef SQLITE_OMIT_WAL
 47009 /*
 47010 ** Trace output macros
 47011 */
 47012 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 47013 SQLITE_PRIVATE int sqlite3WalTrace = 0;
 47014 # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
 47015 #else
 47016 # define WALTRACE(X)
 47017 #endif
 47019 /*
 47020 ** The maximum (and only) versions of the wal and wal-index formats
 47021 ** that may be interpreted by this version of SQLite.
 47022 **
 47023 ** If a client begins recovering a WAL file and finds that (a) the checksum
 47024 ** values in the wal-header are correct and (b) the version field is not
 47025 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
 47026 **
 47027 ** Similarly, if a client successfully reads a wal-index header (i.e. the 
 47028 ** checksum test is successful) and finds that the version field is not
 47029 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
 47030 ** returns SQLITE_CANTOPEN.
 47031 */
 47032 #define WAL_MAX_VERSION      3007000
 47033 #define WALINDEX_MAX_VERSION 3007000
 47035 /*
 47036 ** Indices of various locking bytes.   WAL_NREADER is the number
 47037 ** of available reader locks and should be at least 3.
 47038 */
 47039 #define WAL_WRITE_LOCK         0
 47040 #define WAL_ALL_BUT_WRITE      1
 47041 #define WAL_CKPT_LOCK          1
 47042 #define WAL_RECOVER_LOCK       2
 47043 #define WAL_READ_LOCK(I)       (3+(I))
 47044 #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
 47047 /* Object declarations */
 47048 typedef struct WalIndexHdr WalIndexHdr;
 47049 typedef struct WalIterator WalIterator;
 47050 typedef struct WalCkptInfo WalCkptInfo;
 47053 /*
 47054 ** The following object holds a copy of the wal-index header content.
 47055 **
 47056 ** The actual header in the wal-index consists of two copies of this
 47057 ** object.
 47058 **
 47059 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
 47060 ** Or it can be 1 to represent a 65536-byte page.  The latter case was
 47061 ** added in 3.7.1 when support for 64K pages was added.  
 47062 */
 47063 struct WalIndexHdr {
 47064   u32 iVersion;                   /* Wal-index version */
 47065   u32 unused;                     /* Unused (padding) field */
 47066   u32 iChange;                    /* Counter incremented each transaction */
 47067   u8 isInit;                      /* 1 when initialized */
 47068   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
 47069   u16 szPage;                     /* Database page size in bytes. 1==64K */
 47070   u32 mxFrame;                    /* Index of last valid frame in the WAL */
 47071   u32 nPage;                      /* Size of database in pages */
 47072   u32 aFrameCksum[2];             /* Checksum of last frame in log */
 47073   u32 aSalt[2];                   /* Two salt values copied from WAL header */
 47074   u32 aCksum[2];                  /* Checksum over all prior fields */
 47075 };
 47077 /*
 47078 ** A copy of the following object occurs in the wal-index immediately
 47079 ** following the second copy of the WalIndexHdr.  This object stores
 47080 ** information used by checkpoint.
 47081 **
 47082 ** nBackfill is the number of frames in the WAL that have been written
 47083 ** back into the database. (We call the act of moving content from WAL to
 47084 ** database "backfilling".)  The nBackfill number is never greater than
 47085 ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
 47086 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
 47087 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
 47088 ** mxFrame back to zero when the WAL is reset.
 47089 **
 47090 ** There is one entry in aReadMark[] for each reader lock.  If a reader
 47091 ** holds read-lock K, then the value in aReadMark[K] is no greater than
 47092 ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
 47093 ** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
 47094 ** a special case; its value is never used and it exists as a place-holder
 47095 ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
 47096 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
 47097 ** directly from the database.
 47098 **
 47099 ** The value of aReadMark[K] may only be changed by a thread that
 47100 ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
 47101 ** aReadMark[K] cannot changed while there is a reader is using that mark
 47102 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
 47103 **
 47104 ** The checkpointer may only transfer frames from WAL to database where
 47105 ** the frame numbers are less than or equal to every aReadMark[] that is
 47106 ** in use (that is, every aReadMark[j] for which there is a corresponding
 47107 ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
 47108 ** largest value and will increase an unused aReadMark[] to mxFrame if there
 47109 ** is not already an aReadMark[] equal to mxFrame.  The exception to the
 47110 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
 47111 ** in the WAL has been backfilled into the database) then new readers
 47112 ** will choose aReadMark[0] which has value 0 and hence such reader will
 47113 ** get all their all content directly from the database file and ignore 
 47114 ** the WAL.
 47115 **
 47116 ** Writers normally append new frames to the end of the WAL.  However,
 47117 ** if nBackfill equals mxFrame (meaning that all WAL content has been
 47118 ** written back into the database) and if no readers are using the WAL
 47119 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
 47120 ** the writer will first "reset" the WAL back to the beginning and start
 47121 ** writing new content beginning at frame 1.
 47122 **
 47123 ** We assume that 32-bit loads are atomic and so no locks are needed in
 47124 ** order to read from any aReadMark[] entries.
 47125 */
 47126 struct WalCkptInfo {
 47127   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
 47128   u32 aReadMark[WAL_NREADER];     /* Reader marks */
 47129 };
 47130 #define READMARK_NOT_USED  0xffffffff
 47133 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
 47134 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
 47135 ** only support mandatory file-locks, we do not read or write data
 47136 ** from the region of the file on which locks are applied.
 47137 */
 47138 #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
 47139 #define WALINDEX_LOCK_RESERVED 16
 47140 #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
 47142 /* Size of header before each frame in wal */
 47143 #define WAL_FRAME_HDRSIZE 24
 47145 /* Size of write ahead log header, including checksum. */
 47146 /* #define WAL_HDRSIZE 24 */
 47147 #define WAL_HDRSIZE 32
 47149 /* WAL magic value. Either this value, or the same value with the least
 47150 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
 47151 ** big-endian format in the first 4 bytes of a WAL file.
 47152 **
 47153 ** If the LSB is set, then the checksums for each frame within the WAL
 47154 ** file are calculated by treating all data as an array of 32-bit 
 47155 ** big-endian words. Otherwise, they are calculated by interpreting 
 47156 ** all data as 32-bit little-endian words.
 47157 */
 47158 #define WAL_MAGIC 0x377f0682
 47160 /*
 47161 ** Return the offset of frame iFrame in the write-ahead log file, 
 47162 ** assuming a database page size of szPage bytes. The offset returned
 47163 ** is to the start of the write-ahead log frame-header.
 47164 */
 47165 #define walFrameOffset(iFrame, szPage) (                               \
 47166   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
 47169 /*
 47170 ** An open write-ahead log file is represented by an instance of the
 47171 ** following object.
 47172 */
 47173 struct Wal {
 47174   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
 47175   sqlite3_file *pDbFd;       /* File handle for the database file */
 47176   sqlite3_file *pWalFd;      /* File handle for WAL file */
 47177   u32 iCallback;             /* Value to pass to log callback (or 0) */
 47178   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
 47179   int nWiData;               /* Size of array apWiData */
 47180   int szFirstBlock;          /* Size of first block written to WAL file */
 47181   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
 47182   u32 szPage;                /* Database page size */
 47183   i16 readLock;              /* Which read lock is being held.  -1 for none */
 47184   u8 syncFlags;              /* Flags to use to sync header writes */
 47185   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
 47186   u8 writeLock;              /* True if in a write transaction */
 47187   u8 ckptLock;               /* True if holding a checkpoint lock */
 47188   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
 47189   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
 47190   u8 syncHeader;             /* Fsync the WAL header if true */
 47191   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
 47192   WalIndexHdr hdr;           /* Wal-index header for current transaction */
 47193   const char *zWalName;      /* Name of WAL file */
 47194   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
 47195 #ifdef SQLITE_DEBUG
 47196   u8 lockError;              /* True if a locking error has occurred */
 47197 #endif
 47198 };
 47200 /*
 47201 ** Candidate values for Wal.exclusiveMode.
 47202 */
 47203 #define WAL_NORMAL_MODE     0
 47204 #define WAL_EXCLUSIVE_MODE  1     
 47205 #define WAL_HEAPMEMORY_MODE 2
 47207 /*
 47208 ** Possible values for WAL.readOnly
 47209 */
 47210 #define WAL_RDWR        0    /* Normal read/write connection */
 47211 #define WAL_RDONLY      1    /* The WAL file is readonly */
 47212 #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
 47214 /*
 47215 ** Each page of the wal-index mapping contains a hash-table made up of
 47216 ** an array of HASHTABLE_NSLOT elements of the following type.
 47217 */
 47218 typedef u16 ht_slot;
 47220 /*
 47221 ** This structure is used to implement an iterator that loops through
 47222 ** all frames in the WAL in database page order. Where two or more frames
 47223 ** correspond to the same database page, the iterator visits only the 
 47224 ** frame most recently written to the WAL (in other words, the frame with
 47225 ** the largest index).
 47226 **
 47227 ** The internals of this structure are only accessed by:
 47228 **
 47229 **   walIteratorInit() - Create a new iterator,
 47230 **   walIteratorNext() - Step an iterator,
 47231 **   walIteratorFree() - Free an iterator.
 47232 **
 47233 ** This functionality is used by the checkpoint code (see walCheckpoint()).
 47234 */
 47235 struct WalIterator {
 47236   int iPrior;                     /* Last result returned from the iterator */
 47237   int nSegment;                   /* Number of entries in aSegment[] */
 47238   struct WalSegment {
 47239     int iNext;                    /* Next slot in aIndex[] not yet returned */
 47240     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
 47241     u32 *aPgno;                   /* Array of page numbers. */
 47242     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
 47243     int iZero;                    /* Frame number associated with aPgno[0] */
 47244   } aSegment[1];                  /* One for every 32KB page in the wal-index */
 47245 };
 47247 /*
 47248 ** Define the parameters of the hash tables in the wal-index file. There
 47249 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
 47250 ** wal-index.
 47251 **
 47252 ** Changing any of these constants will alter the wal-index format and
 47253 ** create incompatibilities.
 47254 */
 47255 #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
 47256 #define HASHTABLE_HASH_1     383                  /* Should be prime */
 47257 #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
 47259 /* 
 47260 ** The block of page numbers associated with the first hash-table in a
 47261 ** wal-index is smaller than usual. This is so that there is a complete
 47262 ** hash-table on each aligned 32KB page of the wal-index.
 47263 */
 47264 #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
 47266 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
 47267 #define WALINDEX_PGSZ   (                                         \
 47268     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
 47271 /*
 47272 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
 47273 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
 47274 ** numbered from zero.
 47275 **
 47276 ** If this call is successful, *ppPage is set to point to the wal-index
 47277 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 47278 ** then an SQLite error code is returned and *ppPage is set to 0.
 47279 */
 47280 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 47281   int rc = SQLITE_OK;
 47283   /* Enlarge the pWal->apWiData[] array if required */
 47284   if( pWal->nWiData<=iPage ){
 47285     int nByte = sizeof(u32*)*(iPage+1);
 47286     volatile u32 **apNew;
 47287     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
 47288     if( !apNew ){
 47289       *ppPage = 0;
 47290       return SQLITE_NOMEM;
 47292     memset((void*)&apNew[pWal->nWiData], 0,
 47293            sizeof(u32*)*(iPage+1-pWal->nWiData));
 47294     pWal->apWiData = apNew;
 47295     pWal->nWiData = iPage+1;
 47298   /* Request a pointer to the required page from the VFS */
 47299   if( pWal->apWiData[iPage]==0 ){
 47300     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 47301       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 47302       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
 47303     }else{
 47304       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 47305           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 47306       );
 47307       if( rc==SQLITE_READONLY ){
 47308         pWal->readOnly |= WAL_SHM_RDONLY;
 47309         rc = SQLITE_OK;
 47314   *ppPage = pWal->apWiData[iPage];
 47315   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 47316   return rc;
 47319 /*
 47320 ** Return a pointer to the WalCkptInfo structure in the wal-index.
 47321 */
 47322 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
 47323   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 47324   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
 47327 /*
 47328 ** Return a pointer to the WalIndexHdr structure in the wal-index.
 47329 */
 47330 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
 47331   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 47332   return (volatile WalIndexHdr*)pWal->apWiData[0];
 47335 /*
 47336 ** The argument to this macro must be of type u32. On a little-endian
 47337 ** architecture, it returns the u32 value that results from interpreting
 47338 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
 47339 ** returns the value that would be produced by intepreting the 4 bytes
 47340 ** of the input value as a little-endian integer.
 47341 */
 47342 #define BYTESWAP32(x) ( \
 47343     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
 47344   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
 47347 /*
 47348 ** Generate or extend an 8 byte checksum based on the data in 
 47349 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
 47350 ** initial values of 0 and 0 if aIn==NULL).
 47351 **
 47352 ** The checksum is written back into aOut[] before returning.
 47353 **
 47354 ** nByte must be a positive multiple of 8.
 47355 */
 47356 static void walChecksumBytes(
 47357   int nativeCksum, /* True for native byte-order, false for non-native */
 47358   u8 *a,           /* Content to be checksummed */
 47359   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
 47360   const u32 *aIn,  /* Initial checksum value input */
 47361   u32 *aOut        /* OUT: Final checksum value output */
 47362 ){
 47363   u32 s1, s2;
 47364   u32 *aData = (u32 *)a;
 47365   u32 *aEnd = (u32 *)&a[nByte];
 47367   if( aIn ){
 47368     s1 = aIn[0];
 47369     s2 = aIn[1];
 47370   }else{
 47371     s1 = s2 = 0;
 47374   assert( nByte>=8 );
 47375   assert( (nByte&0x00000007)==0 );
 47377   if( nativeCksum ){
 47378     do {
 47379       s1 += *aData++ + s2;
 47380       s2 += *aData++ + s1;
 47381     }while( aData<aEnd );
 47382   }else{
 47383     do {
 47384       s1 += BYTESWAP32(aData[0]) + s2;
 47385       s2 += BYTESWAP32(aData[1]) + s1;
 47386       aData += 2;
 47387     }while( aData<aEnd );
 47390   aOut[0] = s1;
 47391   aOut[1] = s2;
 47394 static void walShmBarrier(Wal *pWal){
 47395   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
 47396     sqlite3OsShmBarrier(pWal->pDbFd);
 47400 /*
 47401 ** Write the header information in pWal->hdr into the wal-index.
 47402 **
 47403 ** The checksum on pWal->hdr is updated before it is written.
 47404 */
 47405 static void walIndexWriteHdr(Wal *pWal){
 47406   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
 47407   const int nCksum = offsetof(WalIndexHdr, aCksum);
 47409   assert( pWal->writeLock );
 47410   pWal->hdr.isInit = 1;
 47411   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
 47412   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
 47413   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 47414   walShmBarrier(pWal);
 47415   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
 47418 /*
 47419 ** This function encodes a single frame header and writes it to a buffer
 47420 ** supplied by the caller. A frame-header is made up of a series of 
 47421 ** 4-byte big-endian integers, as follows:
 47422 **
 47423 **     0: Page number.
 47424 **     4: For commit records, the size of the database image in pages 
 47425 **        after the commit. For all other records, zero.
 47426 **     8: Salt-1 (copied from the wal-header)
 47427 **    12: Salt-2 (copied from the wal-header)
 47428 **    16: Checksum-1.
 47429 **    20: Checksum-2.
 47430 */
 47431 static void walEncodeFrame(
 47432   Wal *pWal,                      /* The write-ahead log */
 47433   u32 iPage,                      /* Database page number for frame */
 47434   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
 47435   u8 *aData,                      /* Pointer to page data */
 47436   u8 *aFrame                      /* OUT: Write encoded frame here */
 47437 ){
 47438   int nativeCksum;                /* True for native byte-order checksums */
 47439   u32 *aCksum = pWal->hdr.aFrameCksum;
 47440   assert( WAL_FRAME_HDRSIZE==24 );
 47441   sqlite3Put4byte(&aFrame[0], iPage);
 47442   sqlite3Put4byte(&aFrame[4], nTruncate);
 47443   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
 47445   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 47446   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 47447   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 47449   sqlite3Put4byte(&aFrame[16], aCksum[0]);
 47450   sqlite3Put4byte(&aFrame[20], aCksum[1]);
 47453 /*
 47454 ** Check to see if the frame with header in aFrame[] and content
 47455 ** in aData[] is valid.  If it is a valid frame, fill *piPage and
 47456 ** *pnTruncate and return true.  Return if the frame is not valid.
 47457 */
 47458 static int walDecodeFrame(
 47459   Wal *pWal,                      /* The write-ahead log */
 47460   u32 *piPage,                    /* OUT: Database page number for frame */
 47461   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
 47462   u8 *aData,                      /* Pointer to page data (for checksum) */
 47463   u8 *aFrame                      /* Frame data */
 47464 ){
 47465   int nativeCksum;                /* True for native byte-order checksums */
 47466   u32 *aCksum = pWal->hdr.aFrameCksum;
 47467   u32 pgno;                       /* Page number of the frame */
 47468   assert( WAL_FRAME_HDRSIZE==24 );
 47470   /* A frame is only valid if the salt values in the frame-header
 47471   ** match the salt values in the wal-header. 
 47472   */
 47473   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
 47474     return 0;
 47477   /* A frame is only valid if the page number is creater than zero.
 47478   */
 47479   pgno = sqlite3Get4byte(&aFrame[0]);
 47480   if( pgno==0 ){
 47481     return 0;
 47484   /* A frame is only valid if a checksum of the WAL header,
 47485   ** all prior frams, the first 16 bytes of this frame-header, 
 47486   ** and the frame-data matches the checksum in the last 8 
 47487   ** bytes of this frame-header.
 47488   */
 47489   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
 47490   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
 47491   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
 47492   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) 
 47493    || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) 
 47494   ){
 47495     /* Checksum failed. */
 47496     return 0;
 47499   /* If we reach this point, the frame is valid.  Return the page number
 47500   ** and the new database size.
 47501   */
 47502   *piPage = pgno;
 47503   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
 47504   return 1;
 47508 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 47509 /*
 47510 ** Names of locks.  This routine is used to provide debugging output and is not
 47511 ** a part of an ordinary build.
 47512 */
 47513 static const char *walLockName(int lockIdx){
 47514   if( lockIdx==WAL_WRITE_LOCK ){
 47515     return "WRITE-LOCK";
 47516   }else if( lockIdx==WAL_CKPT_LOCK ){
 47517     return "CKPT-LOCK";
 47518   }else if( lockIdx==WAL_RECOVER_LOCK ){
 47519     return "RECOVER-LOCK";
 47520   }else{
 47521     static char zName[15];
 47522     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
 47523                      lockIdx-WAL_READ_LOCK(0));
 47524     return zName;
 47527 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
 47530 /*
 47531 ** Set or release locks on the WAL.  Locks are either shared or exclusive.
 47532 ** A lock cannot be moved directly between shared and exclusive - it must go
 47533 ** through the unlocked state first.
 47534 **
 47535 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
 47536 */
 47537 static int walLockShared(Wal *pWal, int lockIdx){
 47538   int rc;
 47539   if( pWal->exclusiveMode ) return SQLITE_OK;
 47540   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 47541                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
 47542   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
 47543             walLockName(lockIdx), rc ? "failed" : "ok"));
 47544   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 47545   return rc;
 47547 static void walUnlockShared(Wal *pWal, int lockIdx){
 47548   if( pWal->exclusiveMode ) return;
 47549   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
 47550                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
 47551   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
 47553 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
 47554   int rc;
 47555   if( pWal->exclusiveMode ) return SQLITE_OK;
 47556   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 47557                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
 47558   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
 47559             walLockName(lockIdx), n, rc ? "failed" : "ok"));
 47560   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
 47561   return rc;
 47563 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
 47564   if( pWal->exclusiveMode ) return;
 47565   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
 47566                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
 47567   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
 47568              walLockName(lockIdx), n));
 47571 /*
 47572 ** Compute a hash on a page number.  The resulting hash value must land
 47573 ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
 47574 ** the hash to the next value in the event of a collision.
 47575 */
 47576 static int walHash(u32 iPage){
 47577   assert( iPage>0 );
 47578   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
 47579   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
 47581 static int walNextHash(int iPriorHash){
 47582   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
 47585 /* 
 47586 ** Return pointers to the hash table and page number array stored on
 47587 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
 47588 ** numbered starting from 0.
 47589 **
 47590 ** Set output variable *paHash to point to the start of the hash table
 47591 ** in the wal-index file. Set *piZero to one less than the frame 
 47592 ** number of the first frame indexed by this hash table. If a
 47593 ** slot in the hash table is set to N, it refers to frame number 
 47594 ** (*piZero+N) in the log.
 47595 **
 47596 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
 47597 ** first frame indexed by the hash table, frame (*piZero+1).
 47598 */
 47599 static int walHashGet(
 47600   Wal *pWal,                      /* WAL handle */
 47601   int iHash,                      /* Find the iHash'th table */
 47602   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
 47603   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
 47604   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
 47605 ){
 47606   int rc;                         /* Return code */
 47607   volatile u32 *aPgno;
 47609   rc = walIndexPage(pWal, iHash, &aPgno);
 47610   assert( rc==SQLITE_OK || iHash>0 );
 47612   if( rc==SQLITE_OK ){
 47613     u32 iZero;
 47614     volatile ht_slot *aHash;
 47616     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
 47617     if( iHash==0 ){
 47618       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
 47619       iZero = 0;
 47620     }else{
 47621       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
 47624     *paPgno = &aPgno[-1];
 47625     *paHash = aHash;
 47626     *piZero = iZero;
 47628   return rc;
 47631 /*
 47632 ** Return the number of the wal-index page that contains the hash-table
 47633 ** and page-number array that contain entries corresponding to WAL frame
 47634 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages 
 47635 ** are numbered starting from 0.
 47636 */
 47637 static int walFramePage(u32 iFrame){
 47638   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
 47639   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
 47640        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
 47641        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
 47642        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
 47643        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
 47644   );
 47645   return iHash;
 47648 /*
 47649 ** Return the page number associated with frame iFrame in this WAL.
 47650 */
 47651 static u32 walFramePgno(Wal *pWal, u32 iFrame){
 47652   int iHash = walFramePage(iFrame);
 47653   if( iHash==0 ){
 47654     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
 47656   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
 47659 /*
 47660 ** Remove entries from the hash table that point to WAL slots greater
 47661 ** than pWal->hdr.mxFrame.
 47662 **
 47663 ** This function is called whenever pWal->hdr.mxFrame is decreased due
 47664 ** to a rollback or savepoint.
 47665 **
 47666 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
 47667 ** updated.  Any later hash tables will be automatically cleared when
 47668 ** pWal->hdr.mxFrame advances to the point where those hash tables are
 47669 ** actually needed.
 47670 */
 47671 static void walCleanupHash(Wal *pWal){
 47672   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
 47673   volatile u32 *aPgno = 0;        /* Page number array for hash table */
 47674   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
 47675   int iLimit = 0;                 /* Zero values greater than this */
 47676   int nByte;                      /* Number of bytes to zero in aPgno[] */
 47677   int i;                          /* Used to iterate through aHash[] */
 47679   assert( pWal->writeLock );
 47680   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
 47681   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
 47682   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
 47684   if( pWal->hdr.mxFrame==0 ) return;
 47686   /* Obtain pointers to the hash-table and page-number array containing 
 47687   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
 47688   ** that the page said hash-table and array reside on is already mapped.
 47689   */
 47690   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
 47691   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
 47692   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
 47694   /* Zero all hash-table entries that correspond to frame numbers greater
 47695   ** than pWal->hdr.mxFrame.
 47696   */
 47697   iLimit = pWal->hdr.mxFrame - iZero;
 47698   assert( iLimit>0 );
 47699   for(i=0; i<HASHTABLE_NSLOT; i++){
 47700     if( aHash[i]>iLimit ){
 47701       aHash[i] = 0;
 47705   /* Zero the entries in the aPgno array that correspond to frames with
 47706   ** frame numbers greater than pWal->hdr.mxFrame. 
 47707   */
 47708   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
 47709   memset((void *)&aPgno[iLimit+1], 0, nByte);
 47711 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 47712   /* Verify that the every entry in the mapping region is still reachable
 47713   ** via the hash table even after the cleanup.
 47714   */
 47715   if( iLimit ){
 47716     int i;           /* Loop counter */
 47717     int iKey;        /* Hash key */
 47718     for(i=1; i<=iLimit; i++){
 47719       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 47720         if( aHash[iKey]==i ) break;
 47722       assert( aHash[iKey]==i );
 47725 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 47729 /*
 47730 ** Set an entry in the wal-index that will map database page number
 47731 ** pPage into WAL frame iFrame.
 47732 */
 47733 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
 47734   int rc;                         /* Return code */
 47735   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
 47736   volatile u32 *aPgno = 0;        /* Page number array */
 47737   volatile ht_slot *aHash = 0;    /* Hash table */
 47739   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
 47741   /* Assuming the wal-index file was successfully mapped, populate the
 47742   ** page number array and hash table entry.
 47743   */
 47744   if( rc==SQLITE_OK ){
 47745     int iKey;                     /* Hash table key */
 47746     int idx;                      /* Value to write to hash-table slot */
 47747     int nCollide;                 /* Number of hash collisions */
 47749     idx = iFrame - iZero;
 47750     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
 47752     /* If this is the first entry to be added to this hash-table, zero the
 47753     ** entire hash table and aPgno[] array before proceding. 
 47754     */
 47755     if( idx==1 ){
 47756       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
 47757       memset((void*)&aPgno[1], 0, nByte);
 47760     /* If the entry in aPgno[] is already set, then the previous writer
 47761     ** must have exited unexpectedly in the middle of a transaction (after
 47762     ** writing one or more dirty pages to the WAL to free up memory). 
 47763     ** Remove the remnants of that writers uncommitted transaction from 
 47764     ** the hash-table before writing any new entries.
 47765     */
 47766     if( aPgno[idx] ){
 47767       walCleanupHash(pWal);
 47768       assert( !aPgno[idx] );
 47771     /* Write the aPgno[] array entry and the hash-table slot. */
 47772     nCollide = idx;
 47773     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
 47774       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
 47776     aPgno[idx] = iPage;
 47777     aHash[iKey] = (ht_slot)idx;
 47779 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 47780     /* Verify that the number of entries in the hash table exactly equals
 47781     ** the number of entries in the mapping region.
 47782     */
 47784       int i;           /* Loop counter */
 47785       int nEntry = 0;  /* Number of entries in the hash table */
 47786       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
 47787       assert( nEntry==idx );
 47790     /* Verify that the every entry in the mapping region is reachable
 47791     ** via the hash table.  This turns out to be a really, really expensive
 47792     ** thing to check, so only do this occasionally - not on every
 47793     ** iteration.
 47794     */
 47795     if( (idx&0x3ff)==0 ){
 47796       int i;           /* Loop counter */
 47797       for(i=1; i<=idx; i++){
 47798         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
 47799           if( aHash[iKey]==i ) break;
 47801         assert( aHash[iKey]==i );
 47804 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
 47808   return rc;
 47812 /*
 47813 ** Recover the wal-index by reading the write-ahead log file. 
 47814 **
 47815 ** This routine first tries to establish an exclusive lock on the
 47816 ** wal-index to prevent other threads/processes from doing anything
 47817 ** with the WAL or wal-index while recovery is running.  The
 47818 ** WAL_RECOVER_LOCK is also held so that other threads will know
 47819 ** that this thread is running recovery.  If unable to establish
 47820 ** the necessary locks, this routine returns SQLITE_BUSY.
 47821 */
 47822 static int walIndexRecover(Wal *pWal){
 47823   int rc;                         /* Return Code */
 47824   i64 nSize;                      /* Size of log file */
 47825   u32 aFrameCksum[2] = {0, 0};
 47826   int iLock;                      /* Lock offset to lock for checkpoint */
 47827   int nLock;                      /* Number of locks to hold */
 47829   /* Obtain an exclusive lock on all byte in the locking range not already
 47830   ** locked by the caller. The caller is guaranteed to have locked the
 47831   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
 47832   ** If successful, the same bytes that are locked here are unlocked before
 47833   ** this function returns.
 47834   */
 47835   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
 47836   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
 47837   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
 47838   assert( pWal->writeLock );
 47839   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
 47840   nLock = SQLITE_SHM_NLOCK - iLock;
 47841   rc = walLockExclusive(pWal, iLock, nLock);
 47842   if( rc ){
 47843     return rc;
 47845   WALTRACE(("WAL%p: recovery begin...\n", pWal));
 47847   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 47849   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
 47850   if( rc!=SQLITE_OK ){
 47851     goto recovery_error;
 47854   if( nSize>WAL_HDRSIZE ){
 47855     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
 47856     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
 47857     int szFrame;                  /* Number of bytes in buffer aFrame[] */
 47858     u8 *aData;                    /* Pointer to data part of aFrame buffer */
 47859     int iFrame;                   /* Index of last frame read */
 47860     i64 iOffset;                  /* Next offset to read from log file */
 47861     int szPage;                   /* Page size according to the log */
 47862     u32 magic;                    /* Magic value read from WAL header */
 47863     u32 version;                  /* Magic value read from WAL header */
 47864     int isValid;                  /* True if this frame is valid */
 47866     /* Read in the WAL header. */
 47867     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
 47868     if( rc!=SQLITE_OK ){
 47869       goto recovery_error;
 47872     /* If the database page size is not a power of two, or is greater than
 47873     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid 
 47874     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
 47875     ** WAL file.
 47876     */
 47877     magic = sqlite3Get4byte(&aBuf[0]);
 47878     szPage = sqlite3Get4byte(&aBuf[8]);
 47879     if( (magic&0xFFFFFFFE)!=WAL_MAGIC 
 47880      || szPage&(szPage-1) 
 47881      || szPage>SQLITE_MAX_PAGE_SIZE 
 47882      || szPage<512 
 47883     ){
 47884       goto finished;
 47886     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
 47887     pWal->szPage = szPage;
 47888     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
 47889     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
 47891     /* Verify that the WAL header checksum is correct */
 47892     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, 
 47893         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
 47894     );
 47895     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
 47896      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
 47897     ){
 47898       goto finished;
 47901     /* Verify that the version number on the WAL format is one that
 47902     ** are able to understand */
 47903     version = sqlite3Get4byte(&aBuf[4]);
 47904     if( version!=WAL_MAX_VERSION ){
 47905       rc = SQLITE_CANTOPEN_BKPT;
 47906       goto finished;
 47909     /* Malloc a buffer to read frames into. */
 47910     szFrame = szPage + WAL_FRAME_HDRSIZE;
 47911     aFrame = (u8 *)sqlite3_malloc(szFrame);
 47912     if( !aFrame ){
 47913       rc = SQLITE_NOMEM;
 47914       goto recovery_error;
 47916     aData = &aFrame[WAL_FRAME_HDRSIZE];
 47918     /* Read all frames from the log file. */
 47919     iFrame = 0;
 47920     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
 47921       u32 pgno;                   /* Database page number for frame */
 47922       u32 nTruncate;              /* dbsize field from frame header */
 47924       /* Read and decode the next log frame. */
 47925       iFrame++;
 47926       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
 47927       if( rc!=SQLITE_OK ) break;
 47928       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
 47929       if( !isValid ) break;
 47930       rc = walIndexAppend(pWal, iFrame, pgno);
 47931       if( rc!=SQLITE_OK ) break;
 47933       /* If nTruncate is non-zero, this is a commit record. */
 47934       if( nTruncate ){
 47935         pWal->hdr.mxFrame = iFrame;
 47936         pWal->hdr.nPage = nTruncate;
 47937         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 47938         testcase( szPage<=32768 );
 47939         testcase( szPage>=65536 );
 47940         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
 47941         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
 47945     sqlite3_free(aFrame);
 47948 finished:
 47949   if( rc==SQLITE_OK ){
 47950     volatile WalCkptInfo *pInfo;
 47951     int i;
 47952     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
 47953     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
 47954     walIndexWriteHdr(pWal);
 47956     /* Reset the checkpoint-header. This is safe because this thread is 
 47957     ** currently holding locks that exclude all other readers, writers and
 47958     ** checkpointers.
 47959     */
 47960     pInfo = walCkptInfo(pWal);
 47961     pInfo->nBackfill = 0;
 47962     pInfo->aReadMark[0] = 0;
 47963     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 47964     if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
 47966     /* If more than one frame was recovered from the log file, report an
 47967     ** event via sqlite3_log(). This is to help with identifying performance
 47968     ** problems caused by applications routinely shutting down without
 47969     ** checkpointing the log file.
 47970     */
 47971     if( pWal->hdr.nPage ){
 47972       sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
 47973           "recovered %d frames from WAL file %s",
 47974           pWal->hdr.mxFrame, pWal->zWalName
 47975       );
 47979 recovery_error:
 47980   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
 47981   walUnlockExclusive(pWal, iLock, nLock);
 47982   return rc;
 47985 /*
 47986 ** Close an open wal-index.
 47987 */
 47988 static void walIndexClose(Wal *pWal, int isDelete){
 47989   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 47990     int i;
 47991     for(i=0; i<pWal->nWiData; i++){
 47992       sqlite3_free((void *)pWal->apWiData[i]);
 47993       pWal->apWiData[i] = 0;
 47995   }else{
 47996     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
 48000 /* 
 48001 ** Open a connection to the WAL file zWalName. The database file must 
 48002 ** already be opened on connection pDbFd. The buffer that zWalName points
 48003 ** to must remain valid for the lifetime of the returned Wal* handle.
 48004 **
 48005 ** A SHARED lock should be held on the database file when this function
 48006 ** is called. The purpose of this SHARED lock is to prevent any other
 48007 ** client from unlinking the WAL or wal-index file. If another process
 48008 ** were to do this just after this client opened one of these files, the
 48009 ** system would be badly broken.
 48010 **
 48011 ** If the log file is successfully opened, SQLITE_OK is returned and 
 48012 ** *ppWal is set to point to a new WAL handle. If an error occurs,
 48013 ** an SQLite error code is returned and *ppWal is left unmodified.
 48014 */
 48015 SQLITE_PRIVATE int sqlite3WalOpen(
 48016   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
 48017   sqlite3_file *pDbFd,            /* The open database file */
 48018   const char *zWalName,           /* Name of the WAL file */
 48019   int bNoShm,                     /* True to run in heap-memory mode */
 48020   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
 48021   Wal **ppWal                     /* OUT: Allocated Wal handle */
 48022 ){
 48023   int rc;                         /* Return Code */
 48024   Wal *pRet;                      /* Object to allocate and return */
 48025   int flags;                      /* Flags passed to OsOpen() */
 48027   assert( zWalName && zWalName[0] );
 48028   assert( pDbFd );
 48030   /* In the amalgamation, the os_unix.c and os_win.c source files come before
 48031   ** this source file.  Verify that the #defines of the locking byte offsets
 48032   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
 48033   */
 48034 #ifdef WIN_SHM_BASE
 48035   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
 48036 #endif
 48037 #ifdef UNIX_SHM_BASE
 48038   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
 48039 #endif
 48042   /* Allocate an instance of struct Wal to return. */
 48043   *ppWal = 0;
 48044   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
 48045   if( !pRet ){
 48046     return SQLITE_NOMEM;
 48049   pRet->pVfs = pVfs;
 48050   pRet->pWalFd = (sqlite3_file *)&pRet[1];
 48051   pRet->pDbFd = pDbFd;
 48052   pRet->readLock = -1;
 48053   pRet->mxWalSize = mxWalSize;
 48054   pRet->zWalName = zWalName;
 48055   pRet->syncHeader = 1;
 48056   pRet->padToSectorBoundary = 1;
 48057   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
 48059   /* Open file handle on the write-ahead log file. */
 48060   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
 48061   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
 48062   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
 48063     pRet->readOnly = WAL_RDONLY;
 48066   if( rc!=SQLITE_OK ){
 48067     walIndexClose(pRet, 0);
 48068     sqlite3OsClose(pRet->pWalFd);
 48069     sqlite3_free(pRet);
 48070   }else{
 48071     int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
 48072     if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
 48073     if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
 48074       pRet->padToSectorBoundary = 0;
 48076     *ppWal = pRet;
 48077     WALTRACE(("WAL%d: opened\n", pRet));
 48079   return rc;
 48082 /*
 48083 ** Change the size to which the WAL file is trucated on each reset.
 48084 */
 48085 SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
 48086   if( pWal ) pWal->mxWalSize = iLimit;
 48089 /*
 48090 ** Find the smallest page number out of all pages held in the WAL that
 48091 ** has not been returned by any prior invocation of this method on the
 48092 ** same WalIterator object.   Write into *piFrame the frame index where
 48093 ** that page was last written into the WAL.  Write into *piPage the page
 48094 ** number.
 48095 **
 48096 ** Return 0 on success.  If there are no pages in the WAL with a page
 48097 ** number larger than *piPage, then return 1.
 48098 */
 48099 static int walIteratorNext(
 48100   WalIterator *p,               /* Iterator */
 48101   u32 *piPage,                  /* OUT: The page number of the next page */
 48102   u32 *piFrame                  /* OUT: Wal frame index of next page */
 48103 ){
 48104   u32 iMin;                     /* Result pgno must be greater than iMin */
 48105   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
 48106   int i;                        /* For looping through segments */
 48108   iMin = p->iPrior;
 48109   assert( iMin<0xffffffff );
 48110   for(i=p->nSegment-1; i>=0; i--){
 48111     struct WalSegment *pSegment = &p->aSegment[i];
 48112     while( pSegment->iNext<pSegment->nEntry ){
 48113       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
 48114       if( iPg>iMin ){
 48115         if( iPg<iRet ){
 48116           iRet = iPg;
 48117           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
 48119         break;
 48121       pSegment->iNext++;
 48125   *piPage = p->iPrior = iRet;
 48126   return (iRet==0xFFFFFFFF);
 48129 /*
 48130 ** This function merges two sorted lists into a single sorted list.
 48131 **
 48132 ** aLeft[] and aRight[] are arrays of indices.  The sort key is
 48133 ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
 48134 ** is guaranteed for all J<K:
 48135 **
 48136 **        aContent[aLeft[J]] < aContent[aLeft[K]]
 48137 **        aContent[aRight[J]] < aContent[aRight[K]]
 48138 **
 48139 ** This routine overwrites aRight[] with a new (probably longer) sequence
 48140 ** of indices such that the aRight[] contains every index that appears in
 48141 ** either aLeft[] or the old aRight[] and such that the second condition
 48142 ** above is still met.
 48143 **
 48144 ** The aContent[aLeft[X]] values will be unique for all X.  And the
 48145 ** aContent[aRight[X]] values will be unique too.  But there might be
 48146 ** one or more combinations of X and Y such that
 48147 **
 48148 **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
 48149 **
 48150 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
 48151 */
 48152 static void walMerge(
 48153   const u32 *aContent,            /* Pages in wal - keys for the sort */
 48154   ht_slot *aLeft,                 /* IN: Left hand input list */
 48155   int nLeft,                      /* IN: Elements in array *paLeft */
 48156   ht_slot **paRight,              /* IN/OUT: Right hand input list */
 48157   int *pnRight,                   /* IN/OUT: Elements in *paRight */
 48158   ht_slot *aTmp                   /* Temporary buffer */
 48159 ){
 48160   int iLeft = 0;                  /* Current index in aLeft */
 48161   int iRight = 0;                 /* Current index in aRight */
 48162   int iOut = 0;                   /* Current index in output buffer */
 48163   int nRight = *pnRight;
 48164   ht_slot *aRight = *paRight;
 48166   assert( nLeft>0 && nRight>0 );
 48167   while( iRight<nRight || iLeft<nLeft ){
 48168     ht_slot logpage;
 48169     Pgno dbpage;
 48171     if( (iLeft<nLeft) 
 48172      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
 48173     ){
 48174       logpage = aLeft[iLeft++];
 48175     }else{
 48176       logpage = aRight[iRight++];
 48178     dbpage = aContent[logpage];
 48180     aTmp[iOut++] = logpage;
 48181     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
 48183     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
 48184     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
 48187   *paRight = aLeft;
 48188   *pnRight = iOut;
 48189   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
 48192 /*
 48193 ** Sort the elements in list aList using aContent[] as the sort key.
 48194 ** Remove elements with duplicate keys, preferring to keep the
 48195 ** larger aList[] values.
 48196 **
 48197 ** The aList[] entries are indices into aContent[].  The values in
 48198 ** aList[] are to be sorted so that for all J<K:
 48199 **
 48200 **      aContent[aList[J]] < aContent[aList[K]]
 48201 **
 48202 ** For any X and Y such that
 48203 **
 48204 **      aContent[aList[X]] == aContent[aList[Y]]
 48205 **
 48206 ** Keep the larger of the two values aList[X] and aList[Y] and discard
 48207 ** the smaller.
 48208 */
 48209 static void walMergesort(
 48210   const u32 *aContent,            /* Pages in wal */
 48211   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
 48212   ht_slot *aList,                 /* IN/OUT: List to sort */
 48213   int *pnList                     /* IN/OUT: Number of elements in aList[] */
 48214 ){
 48215   struct Sublist {
 48216     int nList;                    /* Number of elements in aList */
 48217     ht_slot *aList;               /* Pointer to sub-list content */
 48218   };
 48220   const int nList = *pnList;      /* Size of input list */
 48221   int nMerge = 0;                 /* Number of elements in list aMerge */
 48222   ht_slot *aMerge = 0;            /* List to be merged */
 48223   int iList;                      /* Index into input list */
 48224   int iSub = 0;                   /* Index into aSub array */
 48225   struct Sublist aSub[13];        /* Array of sub-lists */
 48227   memset(aSub, 0, sizeof(aSub));
 48228   assert( nList<=HASHTABLE_NPAGE && nList>0 );
 48229   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
 48231   for(iList=0; iList<nList; iList++){
 48232     nMerge = 1;
 48233     aMerge = &aList[iList];
 48234     for(iSub=0; iList & (1<<iSub); iSub++){
 48235       struct Sublist *p = &aSub[iSub];
 48236       assert( p->aList && p->nList<=(1<<iSub) );
 48237       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
 48238       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 48240     aSub[iSub].aList = aMerge;
 48241     aSub[iSub].nList = nMerge;
 48244   for(iSub++; iSub<ArraySize(aSub); iSub++){
 48245     if( nList & (1<<iSub) ){
 48246       struct Sublist *p = &aSub[iSub];
 48247       assert( p->nList<=(1<<iSub) );
 48248       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
 48249       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
 48252   assert( aMerge==aList );
 48253   *pnList = nMerge;
 48255 #ifdef SQLITE_DEBUG
 48257     int i;
 48258     for(i=1; i<*pnList; i++){
 48259       assert( aContent[aList[i]] > aContent[aList[i-1]] );
 48262 #endif
 48265 /* 
 48266 ** Free an iterator allocated by walIteratorInit().
 48267 */
 48268 static void walIteratorFree(WalIterator *p){
 48269   sqlite3ScratchFree(p);
 48272 /*
 48273 ** Construct a WalInterator object that can be used to loop over all 
 48274 ** pages in the WAL in ascending order. The caller must hold the checkpoint
 48275 ** lock.
 48276 **
 48277 ** On success, make *pp point to the newly allocated WalInterator object
 48278 ** return SQLITE_OK. Otherwise, return an error code. If this routine
 48279 ** returns an error, the value of *pp is undefined.
 48280 **
 48281 ** The calling routine should invoke walIteratorFree() to destroy the
 48282 ** WalIterator object when it has finished with it.
 48283 */
 48284 static int walIteratorInit(Wal *pWal, WalIterator **pp){
 48285   WalIterator *p;                 /* Return value */
 48286   int nSegment;                   /* Number of segments to merge */
 48287   u32 iLast;                      /* Last frame in log */
 48288   int nByte;                      /* Number of bytes to allocate */
 48289   int i;                          /* Iterator variable */
 48290   ht_slot *aTmp;                  /* Temp space used by merge-sort */
 48291   int rc = SQLITE_OK;             /* Return Code */
 48293   /* This routine only runs while holding the checkpoint lock. And
 48294   ** it only runs if there is actually content in the log (mxFrame>0).
 48295   */
 48296   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
 48297   iLast = pWal->hdr.mxFrame;
 48299   /* Allocate space for the WalIterator object. */
 48300   nSegment = walFramePage(iLast) + 1;
 48301   nByte = sizeof(WalIterator) 
 48302         + (nSegment-1)*sizeof(struct WalSegment)
 48303         + iLast*sizeof(ht_slot);
 48304   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
 48305   if( !p ){
 48306     return SQLITE_NOMEM;
 48308   memset(p, 0, nByte);
 48309   p->nSegment = nSegment;
 48311   /* Allocate temporary space used by the merge-sort routine. This block
 48312   ** of memory will be freed before this function returns.
 48313   */
 48314   aTmp = (ht_slot *)sqlite3ScratchMalloc(
 48315       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
 48316   );
 48317   if( !aTmp ){
 48318     rc = SQLITE_NOMEM;
 48321   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
 48322     volatile ht_slot *aHash;
 48323     u32 iZero;
 48324     volatile u32 *aPgno;
 48326     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
 48327     if( rc==SQLITE_OK ){
 48328       int j;                      /* Counter variable */
 48329       int nEntry;                 /* Number of entries in this segment */
 48330       ht_slot *aIndex;            /* Sorted index for this segment */
 48332       aPgno++;
 48333       if( (i+1)==nSegment ){
 48334         nEntry = (int)(iLast - iZero);
 48335       }else{
 48336         nEntry = (int)((u32*)aHash - (u32*)aPgno);
 48338       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
 48339       iZero++;
 48341       for(j=0; j<nEntry; j++){
 48342         aIndex[j] = (ht_slot)j;
 48344       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
 48345       p->aSegment[i].iZero = iZero;
 48346       p->aSegment[i].nEntry = nEntry;
 48347       p->aSegment[i].aIndex = aIndex;
 48348       p->aSegment[i].aPgno = (u32 *)aPgno;
 48351   sqlite3ScratchFree(aTmp);
 48353   if( rc!=SQLITE_OK ){
 48354     walIteratorFree(p);
 48356   *pp = p;
 48357   return rc;
 48360 /*
 48361 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
 48362 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
 48363 ** busy-handler function. Invoke it and retry the lock until either the
 48364 ** lock is successfully obtained or the busy-handler returns 0.
 48365 */
 48366 static int walBusyLock(
 48367   Wal *pWal,                      /* WAL connection */
 48368   int (*xBusy)(void*),            /* Function to call when busy */
 48369   void *pBusyArg,                 /* Context argument for xBusyHandler */
 48370   int lockIdx,                    /* Offset of first byte to lock */
 48371   int n                           /* Number of bytes to lock */
 48372 ){
 48373   int rc;
 48374   do {
 48375     rc = walLockExclusive(pWal, lockIdx, n);
 48376   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
 48377   return rc;
 48380 /*
 48381 ** The cache of the wal-index header must be valid to call this function.
 48382 ** Return the page-size in bytes used by the database.
 48383 */
 48384 static int walPagesize(Wal *pWal){
 48385   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 48388 /*
 48389 ** Copy as much content as we can from the WAL back into the database file
 48390 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
 48391 **
 48392 ** The amount of information copies from WAL to database might be limited
 48393 ** by active readers.  This routine will never overwrite a database page
 48394 ** that a concurrent reader might be using.
 48395 **
 48396 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
 48397 ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if 
 48398 ** checkpoints are always run by a background thread or background 
 48399 ** process, foreground threads will never block on a lengthy fsync call.
 48400 **
 48401 ** Fsync is called on the WAL before writing content out of the WAL and
 48402 ** into the database.  This ensures that if the new content is persistent
 48403 ** in the WAL and can be recovered following a power-loss or hard reset.
 48404 **
 48405 ** Fsync is also called on the database file if (and only if) the entire
 48406 ** WAL content is copied into the database file.  This second fsync makes
 48407 ** it safe to delete the WAL since the new content will persist in the
 48408 ** database file.
 48409 **
 48410 ** This routine uses and updates the nBackfill field of the wal-index header.
 48411 ** This is the only routine tha will increase the value of nBackfill.  
 48412 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
 48413 ** its value.)
 48414 **
 48415 ** The caller must be holding sufficient locks to ensure that no other
 48416 ** checkpoint is running (in any other thread or process) at the same
 48417 ** time.
 48418 */
 48419 static int walCheckpoint(
 48420   Wal *pWal,                      /* Wal connection */
 48421   int eMode,                      /* One of PASSIVE, FULL or RESTART */
 48422   int (*xBusyCall)(void*),        /* Function to call when busy */
 48423   void *pBusyArg,                 /* Context argument for xBusyHandler */
 48424   int sync_flags,                 /* Flags for OsSync() (or 0) */
 48425   u8 *zBuf                        /* Temporary buffer to use */
 48426 ){
 48427   int rc;                         /* Return code */
 48428   int szPage;                     /* Database page-size */
 48429   WalIterator *pIter = 0;         /* Wal iterator context */
 48430   u32 iDbpage = 0;                /* Next database page to write */
 48431   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
 48432   u32 mxSafeFrame;                /* Max frame that can be backfilled */
 48433   u32 mxPage;                     /* Max database page to write */
 48434   int i;                          /* Loop counter */
 48435   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
 48436   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
 48438   szPage = walPagesize(pWal);
 48439   testcase( szPage<=32768 );
 48440   testcase( szPage>=65536 );
 48441   pInfo = walCkptInfo(pWal);
 48442   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
 48444   /* Allocate the iterator */
 48445   rc = walIteratorInit(pWal, &pIter);
 48446   if( rc!=SQLITE_OK ){
 48447     return rc;
 48449   assert( pIter );
 48451   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
 48453   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 48454   ** safe to write into the database.  Frames beyond mxSafeFrame might
 48455   ** overwrite database pages that are in use by active readers and thus
 48456   ** cannot be backfilled from the WAL.
 48457   */
 48458   mxSafeFrame = pWal->hdr.mxFrame;
 48459   mxPage = pWal->hdr.nPage;
 48460   for(i=1; i<WAL_NREADER; i++){
 48461     u32 y = pInfo->aReadMark[i];
 48462     if( mxSafeFrame>y ){
 48463       assert( y<=pWal->hdr.mxFrame );
 48464       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
 48465       if( rc==SQLITE_OK ){
 48466         pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
 48467         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 48468       }else if( rc==SQLITE_BUSY ){
 48469         mxSafeFrame = y;
 48470         xBusy = 0;
 48471       }else{
 48472         goto walcheckpoint_out;
 48477   if( pInfo->nBackfill<mxSafeFrame
 48478    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
 48479   ){
 48480     i64 nSize;                    /* Current size of database file */
 48481     u32 nBackfill = pInfo->nBackfill;
 48483     /* Sync the WAL to disk */
 48484     if( sync_flags ){
 48485       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
 48488     /* If the database may grow as a result of this checkpoint, hint
 48489     ** about the eventual size of the db file to the VFS layer.
 48490     */
 48491     if( rc==SQLITE_OK ){
 48492       i64 nReq = ((i64)mxPage * szPage);
 48493       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
 48494       if( rc==SQLITE_OK && nSize<nReq ){
 48495         sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
 48500     /* Iterate through the contents of the WAL, copying data to the db file. */
 48501     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
 48502       i64 iOffset;
 48503       assert( walFramePgno(pWal, iFrame)==iDbpage );
 48504       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
 48505       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
 48506       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
 48507       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
 48508       if( rc!=SQLITE_OK ) break;
 48509       iOffset = (iDbpage-1)*(i64)szPage;
 48510       testcase( IS_BIG_INT(iOffset) );
 48511       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
 48512       if( rc!=SQLITE_OK ) break;
 48515     /* If work was actually accomplished... */
 48516     if( rc==SQLITE_OK ){
 48517       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
 48518         i64 szDb = pWal->hdr.nPage*(i64)szPage;
 48519         testcase( IS_BIG_INT(szDb) );
 48520         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
 48521         if( rc==SQLITE_OK && sync_flags ){
 48522           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
 48525       if( rc==SQLITE_OK ){
 48526         pInfo->nBackfill = mxSafeFrame;
 48530     /* Release the reader lock held while backfilling */
 48531     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
 48534   if( rc==SQLITE_BUSY ){
 48535     /* Reset the return code so as not to report a checkpoint failure
 48536     ** just because there are active readers.  */
 48537     rc = SQLITE_OK;
 48540   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
 48541   ** file has been copied into the database file, then block until all
 48542   ** readers have finished using the wal file. This ensures that the next
 48543   ** process to write to the database restarts the wal file.
 48544   */
 48545   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 48546     assert( pWal->writeLock );
 48547     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
 48548       rc = SQLITE_BUSY;
 48549     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
 48550       assert( mxSafeFrame==pWal->hdr.mxFrame );
 48551       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
 48552       if( rc==SQLITE_OK ){
 48553         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 48558  walcheckpoint_out:
 48559   walIteratorFree(pIter);
 48560   return rc;
 48563 /*
 48564 ** If the WAL file is currently larger than nMax bytes in size, truncate
 48565 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
 48566 */
 48567 static void walLimitSize(Wal *pWal, i64 nMax){
 48568   i64 sz;
 48569   int rx;
 48570   sqlite3BeginBenignMalloc();
 48571   rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
 48572   if( rx==SQLITE_OK && (sz > nMax ) ){
 48573     rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
 48575   sqlite3EndBenignMalloc();
 48576   if( rx ){
 48577     sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
 48581 /*
 48582 ** Close a connection to a log file.
 48583 */
 48584 SQLITE_PRIVATE int sqlite3WalClose(
 48585   Wal *pWal,                      /* Wal to close */
 48586   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
 48587   int nBuf,
 48588   u8 *zBuf                        /* Buffer of at least nBuf bytes */
 48589 ){
 48590   int rc = SQLITE_OK;
 48591   if( pWal ){
 48592     int isDelete = 0;             /* True to unlink wal and wal-index files */
 48594     /* If an EXCLUSIVE lock can be obtained on the database file (using the
 48595     ** ordinary, rollback-mode locking methods, this guarantees that the
 48596     ** connection associated with this log file is the only connection to
 48597     ** the database. In this case checkpoint the database and unlink both
 48598     ** the wal and wal-index files.
 48599     **
 48600     ** The EXCLUSIVE lock is not released before returning.
 48601     */
 48602     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
 48603     if( rc==SQLITE_OK ){
 48604       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
 48605         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 48607       rc = sqlite3WalCheckpoint(
 48608           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
 48609       );
 48610       if( rc==SQLITE_OK ){
 48611         int bPersist = -1;
 48612         sqlite3OsFileControlHint(
 48613             pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
 48614         );
 48615         if( bPersist!=1 ){
 48616           /* Try to delete the WAL file if the checkpoint completed and
 48617           ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
 48618           ** mode (!bPersist) */
 48619           isDelete = 1;
 48620         }else if( pWal->mxWalSize>=0 ){
 48621           /* Try to truncate the WAL file to zero bytes if the checkpoint
 48622           ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
 48623           ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
 48624           ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
 48625           ** to zero bytes as truncating to the journal_size_limit might
 48626           ** leave a corrupt WAL file on disk. */
 48627           walLimitSize(pWal, 0);
 48632     walIndexClose(pWal, isDelete);
 48633     sqlite3OsClose(pWal->pWalFd);
 48634     if( isDelete ){
 48635       sqlite3BeginBenignMalloc();
 48636       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
 48637       sqlite3EndBenignMalloc();
 48639     WALTRACE(("WAL%p: closed\n", pWal));
 48640     sqlite3_free((void *)pWal->apWiData);
 48641     sqlite3_free(pWal);
 48643   return rc;
 48646 /*
 48647 ** Try to read the wal-index header.  Return 0 on success and 1 if
 48648 ** there is a problem.
 48649 **
 48650 ** The wal-index is in shared memory.  Another thread or process might
 48651 ** be writing the header at the same time this procedure is trying to
 48652 ** read it, which might result in inconsistency.  A dirty read is detected
 48653 ** by verifying that both copies of the header are the same and also by
 48654 ** a checksum on the header.
 48655 **
 48656 ** If and only if the read is consistent and the header is different from
 48657 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
 48658 ** and *pChanged is set to 1.
 48659 **
 48660 ** If the checksum cannot be verified return non-zero. If the header
 48661 ** is read successfully and the checksum verified, return zero.
 48662 */
 48663 static int walIndexTryHdr(Wal *pWal, int *pChanged){
 48664   u32 aCksum[2];                  /* Checksum on the header content */
 48665   WalIndexHdr h1, h2;             /* Two copies of the header content */
 48666   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
 48668   /* The first page of the wal-index must be mapped at this point. */
 48669   assert( pWal->nWiData>0 && pWal->apWiData[0] );
 48671   /* Read the header. This might happen concurrently with a write to the
 48672   ** same area of shared memory on a different CPU in a SMP,
 48673   ** meaning it is possible that an inconsistent snapshot is read
 48674   ** from the file. If this happens, return non-zero.
 48675   **
 48676   ** There are two copies of the header at the beginning of the wal-index.
 48677   ** When reading, read [0] first then [1].  Writes are in the reverse order.
 48678   ** Memory barriers are used to prevent the compiler or the hardware from
 48679   ** reordering the reads and writes.
 48680   */
 48681   aHdr = walIndexHdr(pWal);
 48682   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
 48683   walShmBarrier(pWal);
 48684   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
 48686   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
 48687     return 1;   /* Dirty read */
 48689   if( h1.isInit==0 ){
 48690     return 1;   /* Malformed header - probably all zeros */
 48692   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
 48693   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
 48694     return 1;   /* Checksum does not match */
 48697   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
 48698     *pChanged = 1;
 48699     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
 48700     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
 48701     testcase( pWal->szPage<=32768 );
 48702     testcase( pWal->szPage>=65536 );
 48705   /* The header was successfully read. Return zero. */
 48706   return 0;
 48709 /*
 48710 ** Read the wal-index header from the wal-index and into pWal->hdr.
 48711 ** If the wal-header appears to be corrupt, try to reconstruct the
 48712 ** wal-index from the WAL before returning.
 48713 **
 48714 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
 48715 ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
 48716 ** to 0.
 48717 **
 48718 ** If the wal-index header is successfully read, return SQLITE_OK. 
 48719 ** Otherwise an SQLite error code.
 48720 */
 48721 static int walIndexReadHdr(Wal *pWal, int *pChanged){
 48722   int rc;                         /* Return code */
 48723   int badHdr;                     /* True if a header read failed */
 48724   volatile u32 *page0;            /* Chunk of wal-index containing header */
 48726   /* Ensure that page 0 of the wal-index (the page that contains the 
 48727   ** wal-index header) is mapped. Return early if an error occurs here.
 48728   */
 48729   assert( pChanged );
 48730   rc = walIndexPage(pWal, 0, &page0);
 48731   if( rc!=SQLITE_OK ){
 48732     return rc;
 48733   };
 48734   assert( page0 || pWal->writeLock==0 );
 48736   /* If the first page of the wal-index has been mapped, try to read the
 48737   ** wal-index header immediately, without holding any lock. This usually
 48738   ** works, but may fail if the wal-index header is corrupt or currently 
 48739   ** being modified by another thread or process.
 48740   */
 48741   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
 48743   /* If the first attempt failed, it might have been due to a race
 48744   ** with a writer.  So get a WRITE lock and try again.
 48745   */
 48746   assert( badHdr==0 || pWal->writeLock==0 );
 48747   if( badHdr ){
 48748     if( pWal->readOnly & WAL_SHM_RDONLY ){
 48749       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
 48750         walUnlockShared(pWal, WAL_WRITE_LOCK);
 48751         rc = SQLITE_READONLY_RECOVERY;
 48753     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
 48754       pWal->writeLock = 1;
 48755       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
 48756         badHdr = walIndexTryHdr(pWal, pChanged);
 48757         if( badHdr ){
 48758           /* If the wal-index header is still malformed even while holding
 48759           ** a WRITE lock, it can only mean that the header is corrupted and
 48760           ** needs to be reconstructed.  So run recovery to do exactly that.
 48761           */
 48762           rc = walIndexRecover(pWal);
 48763           *pChanged = 1;
 48766       pWal->writeLock = 0;
 48767       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 48771   /* If the header is read successfully, check the version number to make
 48772   ** sure the wal-index was not constructed with some future format that
 48773   ** this version of SQLite cannot understand.
 48774   */
 48775   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
 48776     rc = SQLITE_CANTOPEN_BKPT;
 48779   return rc;
 48782 /*
 48783 ** This is the value that walTryBeginRead returns when it needs to
 48784 ** be retried.
 48785 */
 48786 #define WAL_RETRY  (-1)
 48788 /*
 48789 ** Attempt to start a read transaction.  This might fail due to a race or
 48790 ** other transient condition.  When that happens, it returns WAL_RETRY to
 48791 ** indicate to the caller that it is safe to retry immediately.
 48792 **
 48793 ** On success return SQLITE_OK.  On a permanent failure (such an
 48794 ** I/O error or an SQLITE_BUSY because another process is running
 48795 ** recovery) return a positive error code.
 48796 **
 48797 ** The useWal parameter is true to force the use of the WAL and disable
 48798 ** the case where the WAL is bypassed because it has been completely
 48799 ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
 48800 ** to make a copy of the wal-index header into pWal->hdr.  If the 
 48801 ** wal-index header has changed, *pChanged is set to 1 (as an indication 
 48802 ** to the caller that the local paget cache is obsolete and needs to be 
 48803 ** flushed.)  When useWal==1, the wal-index header is assumed to already
 48804 ** be loaded and the pChanged parameter is unused.
 48805 **
 48806 ** The caller must set the cnt parameter to the number of prior calls to
 48807 ** this routine during the current read attempt that returned WAL_RETRY.
 48808 ** This routine will start taking more aggressive measures to clear the
 48809 ** race conditions after multiple WAL_RETRY returns, and after an excessive
 48810 ** number of errors will ultimately return SQLITE_PROTOCOL.  The
 48811 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
 48812 ** and is not honoring the locking protocol.  There is a vanishingly small
 48813 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
 48814 ** bad luck when there is lots of contention for the wal-index, but that
 48815 ** possibility is so small that it can be safely neglected, we believe.
 48816 **
 48817 ** On success, this routine obtains a read lock on 
 48818 ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
 48819 ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
 48820 ** that means the Wal does not hold any read lock.  The reader must not
 48821 ** access any database page that is modified by a WAL frame up to and
 48822 ** including frame number aReadMark[pWal->readLock].  The reader will
 48823 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
 48824 ** Or if pWal->readLock==0, then the reader will ignore the WAL
 48825 ** completely and get all content directly from the database file.
 48826 ** If the useWal parameter is 1 then the WAL will never be ignored and
 48827 ** this routine will always set pWal->readLock>0 on success.
 48828 ** When the read transaction is completed, the caller must release the
 48829 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
 48830 **
 48831 ** This routine uses the nBackfill and aReadMark[] fields of the header
 48832 ** to select a particular WAL_READ_LOCK() that strives to let the
 48833 ** checkpoint process do as much work as possible.  This routine might
 48834 ** update values of the aReadMark[] array in the header, but if it does
 48835 ** so it takes care to hold an exclusive lock on the corresponding
 48836 ** WAL_READ_LOCK() while changing values.
 48837 */
 48838 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
 48839   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
 48840   u32 mxReadMark;                 /* Largest aReadMark[] value */
 48841   int mxI;                        /* Index of largest aReadMark[] value */
 48842   int i;                          /* Loop counter */
 48843   int rc = SQLITE_OK;             /* Return code  */
 48845   assert( pWal->readLock<0 );     /* Not currently locked */
 48847   /* Take steps to avoid spinning forever if there is a protocol error.
 48848   **
 48849   ** Circumstances that cause a RETRY should only last for the briefest
 48850   ** instances of time.  No I/O or other system calls are done while the
 48851   ** locks are held, so the locks should not be held for very long. But 
 48852   ** if we are unlucky, another process that is holding a lock might get
 48853   ** paged out or take a page-fault that is time-consuming to resolve, 
 48854   ** during the few nanoseconds that it is holding the lock.  In that case,
 48855   ** it might take longer than normal for the lock to free.
 48856   **
 48857   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
 48858   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
 48859   ** is more of a scheduler yield than an actual delay.  But on the 10th
 48860   ** an subsequent retries, the delays start becoming longer and longer, 
 48861   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
 48862   ** The total delay time before giving up is less than 1 second.
 48863   */
 48864   if( cnt>5 ){
 48865     int nDelay = 1;                      /* Pause time in microseconds */
 48866     if( cnt>100 ){
 48867       VVA_ONLY( pWal->lockError = 1; )
 48868       return SQLITE_PROTOCOL;
 48870     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
 48871     sqlite3OsSleep(pWal->pVfs, nDelay);
 48874   if( !useWal ){
 48875     rc = walIndexReadHdr(pWal, pChanged);
 48876     if( rc==SQLITE_BUSY ){
 48877       /* If there is not a recovery running in another thread or process
 48878       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
 48879       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
 48880       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
 48881       ** would be technically correct.  But the race is benign since with
 48882       ** WAL_RETRY this routine will be called again and will probably be
 48883       ** right on the second iteration.
 48884       */
 48885       if( pWal->apWiData[0]==0 ){
 48886         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
 48887         ** We assume this is a transient condition, so return WAL_RETRY. The
 48888         ** xShmMap() implementation used by the default unix and win32 VFS 
 48889         ** modules may return SQLITE_BUSY due to a race condition in the 
 48890         ** code that determines whether or not the shared-memory region 
 48891         ** must be zeroed before the requested page is returned.
 48892         */
 48893         rc = WAL_RETRY;
 48894       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
 48895         walUnlockShared(pWal, WAL_RECOVER_LOCK);
 48896         rc = WAL_RETRY;
 48897       }else if( rc==SQLITE_BUSY ){
 48898         rc = SQLITE_BUSY_RECOVERY;
 48901     if( rc!=SQLITE_OK ){
 48902       return rc;
 48906   pInfo = walCkptInfo(pWal);
 48907   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
 48908     /* The WAL has been completely backfilled (or it is empty).
 48909     ** and can be safely ignored.
 48910     */
 48911     rc = walLockShared(pWal, WAL_READ_LOCK(0));
 48912     walShmBarrier(pWal);
 48913     if( rc==SQLITE_OK ){
 48914       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
 48915         /* It is not safe to allow the reader to continue here if frames
 48916         ** may have been appended to the log before READ_LOCK(0) was obtained.
 48917         ** When holding READ_LOCK(0), the reader ignores the entire log file,
 48918         ** which implies that the database file contains a trustworthy
 48919         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
 48920         ** happening, this is usually correct.
 48921         **
 48922         ** However, if frames have been appended to the log (or if the log 
 48923         ** is wrapped and written for that matter) before the READ_LOCK(0)
 48924         ** is obtained, that is not necessarily true. A checkpointer may
 48925         ** have started to backfill the appended frames but crashed before
 48926         ** it finished. Leaving a corrupt image in the database file.
 48927         */
 48928         walUnlockShared(pWal, WAL_READ_LOCK(0));
 48929         return WAL_RETRY;
 48931       pWal->readLock = 0;
 48932       return SQLITE_OK;
 48933     }else if( rc!=SQLITE_BUSY ){
 48934       return rc;
 48938   /* If we get this far, it means that the reader will want to use
 48939   ** the WAL to get at content from recent commits.  The job now is
 48940   ** to select one of the aReadMark[] entries that is closest to
 48941   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
 48942   */
 48943   mxReadMark = 0;
 48944   mxI = 0;
 48945   for(i=1; i<WAL_NREADER; i++){
 48946     u32 thisMark = pInfo->aReadMark[i];
 48947     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
 48948       assert( thisMark!=READMARK_NOT_USED );
 48949       mxReadMark = thisMark;
 48950       mxI = i;
 48953   /* There was once an "if" here. The extra "{" is to preserve indentation. */
 48955     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
 48956      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
 48957     ){
 48958       for(i=1; i<WAL_NREADER; i++){
 48959         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
 48960         if( rc==SQLITE_OK ){
 48961           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
 48962           mxI = i;
 48963           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 48964           break;
 48965         }else if( rc!=SQLITE_BUSY ){
 48966           return rc;
 48970     if( mxI==0 ){
 48971       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
 48972       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
 48975     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
 48976     if( rc ){
 48977       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
 48979     /* Now that the read-lock has been obtained, check that neither the
 48980     ** value in the aReadMark[] array or the contents of the wal-index
 48981     ** header have changed.
 48982     **
 48983     ** It is necessary to check that the wal-index header did not change
 48984     ** between the time it was read and when the shared-lock was obtained
 48985     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
 48986     ** that the log file may have been wrapped by a writer, or that frames
 48987     ** that occur later in the log than pWal->hdr.mxFrame may have been
 48988     ** copied into the database by a checkpointer. If either of these things
 48989     ** happened, then reading the database with the current value of
 48990     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
 48991     ** instead.
 48992     **
 48993     ** This does not guarantee that the copy of the wal-index header is up to
 48994     ** date before proceeding. That would not be possible without somehow
 48995     ** blocking writers. It only guarantees that a dangerous checkpoint or 
 48996     ** log-wrap (either of which would require an exclusive lock on
 48997     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
 48998     */
 48999     walShmBarrier(pWal);
 49000     if( pInfo->aReadMark[mxI]!=mxReadMark
 49001      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
 49002     ){
 49003       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
 49004       return WAL_RETRY;
 49005     }else{
 49006       assert( mxReadMark<=pWal->hdr.mxFrame );
 49007       pWal->readLock = (i16)mxI;
 49010   return rc;
 49013 /*
 49014 ** Begin a read transaction on the database.
 49015 **
 49016 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
 49017 ** it takes a snapshot of the state of the WAL and wal-index for the current
 49018 ** instant in time.  The current thread will continue to use this snapshot.
 49019 ** Other threads might append new content to the WAL and wal-index but
 49020 ** that extra content is ignored by the current thread.
 49021 **
 49022 ** If the database contents have changes since the previous read
 49023 ** transaction, then *pChanged is set to 1 before returning.  The
 49024 ** Pager layer will use this to know that is cache is stale and
 49025 ** needs to be flushed.
 49026 */
 49027 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
 49028   int rc;                         /* Return code */
 49029   int cnt = 0;                    /* Number of TryBeginRead attempts */
 49031   do{
 49032     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
 49033   }while( rc==WAL_RETRY );
 49034   testcase( (rc&0xff)==SQLITE_BUSY );
 49035   testcase( (rc&0xff)==SQLITE_IOERR );
 49036   testcase( rc==SQLITE_PROTOCOL );
 49037   testcase( rc==SQLITE_OK );
 49038   return rc;
 49041 /*
 49042 ** Finish with a read transaction.  All this does is release the
 49043 ** read-lock.
 49044 */
 49045 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
 49046   sqlite3WalEndWriteTransaction(pWal);
 49047   if( pWal->readLock>=0 ){
 49048     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 49049     pWal->readLock = -1;
 49053 /*
 49054 ** Search the wal file for page pgno. If found, set *piRead to the frame that
 49055 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
 49056 ** to zero.
 49057 **
 49058 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
 49059 ** error does occur, the final value of *piRead is undefined.
 49060 */
 49061 SQLITE_PRIVATE int sqlite3WalFindFrame(
 49062   Wal *pWal,                      /* WAL handle */
 49063   Pgno pgno,                      /* Database page number to read data for */
 49064   u32 *piRead                     /* OUT: Frame number (or zero) */
 49065 ){
 49066   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
 49067   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
 49068   int iHash;                      /* Used to loop through N hash tables */
 49070   /* This routine is only be called from within a read transaction. */
 49071   assert( pWal->readLock>=0 || pWal->lockError );
 49073   /* If the "last page" field of the wal-index header snapshot is 0, then
 49074   ** no data will be read from the wal under any circumstances. Return early
 49075   ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
 49076   ** then the WAL is ignored by the reader so return early, as if the 
 49077   ** WAL were empty.
 49078   */
 49079   if( iLast==0 || pWal->readLock==0 ){
 49080     *piRead = 0;
 49081     return SQLITE_OK;
 49084   /* Search the hash table or tables for an entry matching page number
 49085   ** pgno. Each iteration of the following for() loop searches one
 49086   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
 49087   **
 49088   ** This code might run concurrently to the code in walIndexAppend()
 49089   ** that adds entries to the wal-index (and possibly to this hash 
 49090   ** table). This means the value just read from the hash 
 49091   ** slot (aHash[iKey]) may have been added before or after the 
 49092   ** current read transaction was opened. Values added after the
 49093   ** read transaction was opened may have been written incorrectly -
 49094   ** i.e. these slots may contain garbage data. However, we assume
 49095   ** that any slots written before the current read transaction was
 49096   ** opened remain unmodified.
 49097   **
 49098   ** For the reasons above, the if(...) condition featured in the inner
 49099   ** loop of the following block is more stringent that would be required 
 49100   ** if we had exclusive access to the hash-table:
 49101   **
 49102   **   (aPgno[iFrame]==pgno): 
 49103   **     This condition filters out normal hash-table collisions.
 49104   **
 49105   **   (iFrame<=iLast): 
 49106   **     This condition filters out entries that were added to the hash
 49107   **     table after the current read-transaction had started.
 49108   */
 49109   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
 49110     volatile ht_slot *aHash;      /* Pointer to hash table */
 49111     volatile u32 *aPgno;          /* Pointer to array of page numbers */
 49112     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
 49113     int iKey;                     /* Hash slot index */
 49114     int nCollide;                 /* Number of hash collisions remaining */
 49115     int rc;                       /* Error code */
 49117     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
 49118     if( rc!=SQLITE_OK ){
 49119       return rc;
 49121     nCollide = HASHTABLE_NSLOT;
 49122     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
 49123       u32 iFrame = aHash[iKey] + iZero;
 49124       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
 49125         /* assert( iFrame>iRead ); -- not true if there is corruption */
 49126         iRead = iFrame;
 49128       if( (nCollide--)==0 ){
 49129         return SQLITE_CORRUPT_BKPT;
 49134 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
 49135   /* If expensive assert() statements are available, do a linear search
 49136   ** of the wal-index file content. Make sure the results agree with the
 49137   ** result obtained using the hash indexes above.  */
 49139     u32 iRead2 = 0;
 49140     u32 iTest;
 49141     for(iTest=iLast; iTest>0; iTest--){
 49142       if( walFramePgno(pWal, iTest)==pgno ){
 49143         iRead2 = iTest;
 49144         break;
 49147     assert( iRead==iRead2 );
 49149 #endif
 49151   *piRead = iRead;
 49152   return SQLITE_OK;
 49155 /*
 49156 ** Read the contents of frame iRead from the wal file into buffer pOut
 49157 ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
 49158 ** error code otherwise.
 49159 */
 49160 SQLITE_PRIVATE int sqlite3WalReadFrame(
 49161   Wal *pWal,                      /* WAL handle */
 49162   u32 iRead,                      /* Frame to read */
 49163   int nOut,                       /* Size of buffer pOut in bytes */
 49164   u8 *pOut                        /* Buffer to write page data to */
 49165 ){
 49166   int sz;
 49167   i64 iOffset;
 49168   sz = pWal->hdr.szPage;
 49169   sz = (sz&0xfe00) + ((sz&0x0001)<<16);
 49170   testcase( sz<=32768 );
 49171   testcase( sz>=65536 );
 49172   iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
 49173   /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
 49174   return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
 49177 /* 
 49178 ** Return the size of the database in pages (or zero, if unknown).
 49179 */
 49180 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
 49181   if( pWal && ALWAYS(pWal->readLock>=0) ){
 49182     return pWal->hdr.nPage;
 49184   return 0;
 49188 /* 
 49189 ** This function starts a write transaction on the WAL.
 49190 **
 49191 ** A read transaction must have already been started by a prior call
 49192 ** to sqlite3WalBeginReadTransaction().
 49193 **
 49194 ** If another thread or process has written into the database since
 49195 ** the read transaction was started, then it is not possible for this
 49196 ** thread to write as doing so would cause a fork.  So this routine
 49197 ** returns SQLITE_BUSY in that case and no write transaction is started.
 49198 **
 49199 ** There can only be a single writer active at a time.
 49200 */
 49201 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
 49202   int rc;
 49204   /* Cannot start a write transaction without first holding a read
 49205   ** transaction. */
 49206   assert( pWal->readLock>=0 );
 49208   if( pWal->readOnly ){
 49209     return SQLITE_READONLY;
 49212   /* Only one writer allowed at a time.  Get the write lock.  Return
 49213   ** SQLITE_BUSY if unable.
 49214   */
 49215   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
 49216   if( rc ){
 49217     return rc;
 49219   pWal->writeLock = 1;
 49221   /* If another connection has written to the database file since the
 49222   ** time the read transaction on this connection was started, then
 49223   ** the write is disallowed.
 49224   */
 49225   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
 49226     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 49227     pWal->writeLock = 0;
 49228     rc = SQLITE_BUSY_SNAPSHOT;
 49231   return rc;
 49234 /*
 49235 ** End a write transaction.  The commit has already been done.  This
 49236 ** routine merely releases the lock.
 49237 */
 49238 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
 49239   if( pWal->writeLock ){
 49240     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
 49241     pWal->writeLock = 0;
 49242     pWal->truncateOnCommit = 0;
 49244   return SQLITE_OK;
 49247 /*
 49248 ** If any data has been written (but not committed) to the log file, this
 49249 ** function moves the write-pointer back to the start of the transaction.
 49250 **
 49251 ** Additionally, the callback function is invoked for each frame written
 49252 ** to the WAL since the start of the transaction. If the callback returns
 49253 ** other than SQLITE_OK, it is not invoked again and the error code is
 49254 ** returned to the caller.
 49255 **
 49256 ** Otherwise, if the callback function does not return an error, this
 49257 ** function returns SQLITE_OK.
 49258 */
 49259 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
 49260   int rc = SQLITE_OK;
 49261   if( ALWAYS(pWal->writeLock) ){
 49262     Pgno iMax = pWal->hdr.mxFrame;
 49263     Pgno iFrame;
 49265     /* Restore the clients cache of the wal-index header to the state it
 49266     ** was in before the client began writing to the database. 
 49267     */
 49268     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
 49270     for(iFrame=pWal->hdr.mxFrame+1; 
 49271         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; 
 49272         iFrame++
 49273     ){
 49274       /* This call cannot fail. Unless the page for which the page number
 49275       ** is passed as the second argument is (a) in the cache and 
 49276       ** (b) has an outstanding reference, then xUndo is either a no-op
 49277       ** (if (a) is false) or simply expels the page from the cache (if (b)
 49278       ** is false).
 49279       **
 49280       ** If the upper layer is doing a rollback, it is guaranteed that there
 49281       ** are no outstanding references to any page other than page 1. And
 49282       ** page 1 is never written to the log until the transaction is
 49283       ** committed. As a result, the call to xUndo may not fail.
 49284       */
 49285       assert( walFramePgno(pWal, iFrame)!=1 );
 49286       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
 49288     if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
 49290   assert( rc==SQLITE_OK );
 49291   return rc;
 49294 /* 
 49295 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 
 49296 ** values. This function populates the array with values required to 
 49297 ** "rollback" the write position of the WAL handle back to the current 
 49298 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
 49299 */
 49300 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
 49301   assert( pWal->writeLock );
 49302   aWalData[0] = pWal->hdr.mxFrame;
 49303   aWalData[1] = pWal->hdr.aFrameCksum[0];
 49304   aWalData[2] = pWal->hdr.aFrameCksum[1];
 49305   aWalData[3] = pWal->nCkpt;
 49308 /* 
 49309 ** Move the write position of the WAL back to the point identified by
 49310 ** the values in the aWalData[] array. aWalData must point to an array
 49311 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
 49312 ** by a call to WalSavepoint().
 49313 */
 49314 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
 49315   int rc = SQLITE_OK;
 49317   assert( pWal->writeLock );
 49318   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
 49320   if( aWalData[3]!=pWal->nCkpt ){
 49321     /* This savepoint was opened immediately after the write-transaction
 49322     ** was started. Right after that, the writer decided to wrap around
 49323     ** to the start of the log. Update the savepoint values to match.
 49324     */
 49325     aWalData[0] = 0;
 49326     aWalData[3] = pWal->nCkpt;
 49329   if( aWalData[0]<pWal->hdr.mxFrame ){
 49330     pWal->hdr.mxFrame = aWalData[0];
 49331     pWal->hdr.aFrameCksum[0] = aWalData[1];
 49332     pWal->hdr.aFrameCksum[1] = aWalData[2];
 49333     walCleanupHash(pWal);
 49336   return rc;
 49340 /*
 49341 ** This function is called just before writing a set of frames to the log
 49342 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
 49343 ** to the current log file, it is possible to overwrite the start of the
 49344 ** existing log file with the new frames (i.e. "reset" the log). If so,
 49345 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
 49346 ** unchanged.
 49347 **
 49348 ** SQLITE_OK is returned if no error is encountered (regardless of whether
 49349 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
 49350 ** if an error occurs.
 49351 */
 49352 static int walRestartLog(Wal *pWal){
 49353   int rc = SQLITE_OK;
 49354   int cnt;
 49356   if( pWal->readLock==0 ){
 49357     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
 49358     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
 49359     if( pInfo->nBackfill>0 ){
 49360       u32 salt1;
 49361       sqlite3_randomness(4, &salt1);
 49362       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 49363       if( rc==SQLITE_OK ){
 49364         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
 49365         ** readers are currently using the WAL), then the transactions
 49366         ** frames will overwrite the start of the existing log. Update the
 49367         ** wal-index header to reflect this.
 49368         **
 49369         ** In theory it would be Ok to update the cache of the header only
 49370         ** at this point. But updating the actual wal-index header is also
 49371         ** safe and means there is no special case for sqlite3WalUndo()
 49372         ** to handle if this transaction is rolled back.
 49373         */
 49374         int i;                    /* Loop counter */
 49375         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
 49377         pWal->nCkpt++;
 49378         pWal->hdr.mxFrame = 0;
 49379         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
 49380         aSalt[1] = salt1;
 49381         walIndexWriteHdr(pWal);
 49382         pInfo->nBackfill = 0;
 49383         pInfo->aReadMark[1] = 0;
 49384         for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 49385         assert( pInfo->aReadMark[0]==0 );
 49386         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 49387       }else if( rc!=SQLITE_BUSY ){
 49388         return rc;
 49391     walUnlockShared(pWal, WAL_READ_LOCK(0));
 49392     pWal->readLock = -1;
 49393     cnt = 0;
 49394     do{
 49395       int notUsed;
 49396       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
 49397     }while( rc==WAL_RETRY );
 49398     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
 49399     testcase( (rc&0xff)==SQLITE_IOERR );
 49400     testcase( rc==SQLITE_PROTOCOL );
 49401     testcase( rc==SQLITE_OK );
 49403   return rc;
 49406 /*
 49407 ** Information about the current state of the WAL file and where
 49408 ** the next fsync should occur - passed from sqlite3WalFrames() into
 49409 ** walWriteToLog().
 49410 */
 49411 typedef struct WalWriter {
 49412   Wal *pWal;                   /* The complete WAL information */
 49413   sqlite3_file *pFd;           /* The WAL file to which we write */
 49414   sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
 49415   int syncFlags;               /* Flags for the fsync */
 49416   int szPage;                  /* Size of one page */
 49417 } WalWriter;
 49419 /*
 49420 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
 49421 ** Do a sync when crossing the p->iSyncPoint boundary.
 49422 **
 49423 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
 49424 ** first write the part before iSyncPoint, then sync, then write the
 49425 ** rest.
 49426 */
 49427 static int walWriteToLog(
 49428   WalWriter *p,              /* WAL to write to */
 49429   void *pContent,            /* Content to be written */
 49430   int iAmt,                  /* Number of bytes to write */
 49431   sqlite3_int64 iOffset      /* Start writing at this offset */
 49432 ){
 49433   int rc;
 49434   if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
 49435     int iFirstAmt = (int)(p->iSyncPoint - iOffset);
 49436     rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
 49437     if( rc ) return rc;
 49438     iOffset += iFirstAmt;
 49439     iAmt -= iFirstAmt;
 49440     pContent = (void*)(iFirstAmt + (char*)pContent);
 49441     assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
 49442     rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
 49443     if( iAmt==0 || rc ) return rc;
 49445   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
 49446   return rc;
 49449 /*
 49450 ** Write out a single frame of the WAL
 49451 */
 49452 static int walWriteOneFrame(
 49453   WalWriter *p,               /* Where to write the frame */
 49454   PgHdr *pPage,               /* The page of the frame to be written */
 49455   int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
 49456   sqlite3_int64 iOffset       /* Byte offset at which to write */
 49457 ){
 49458   int rc;                         /* Result code from subfunctions */
 49459   void *pData;                    /* Data actually written */
 49460   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
 49461 #if defined(SQLITE_HAS_CODEC)
 49462   if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
 49463 #else
 49464   pData = pPage->pData;
 49465 #endif
 49466   walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
 49467   rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
 49468   if( rc ) return rc;
 49469   /* Write the page data */
 49470   rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
 49471   return rc;
 49474 /* 
 49475 ** Write a set of frames to the log. The caller must hold the write-lock
 49476 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
 49477 */
 49478 SQLITE_PRIVATE int sqlite3WalFrames(
 49479   Wal *pWal,                      /* Wal handle to write to */
 49480   int szPage,                     /* Database page-size in bytes */
 49481   PgHdr *pList,                   /* List of dirty pages to write */
 49482   Pgno nTruncate,                 /* Database size after this commit */
 49483   int isCommit,                   /* True if this is a commit */
 49484   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
 49485 ){
 49486   int rc;                         /* Used to catch return codes */
 49487   u32 iFrame;                     /* Next frame address */
 49488   PgHdr *p;                       /* Iterator to run through pList with. */
 49489   PgHdr *pLast = 0;               /* Last frame in list */
 49490   int nExtra = 0;                 /* Number of extra copies of last page */
 49491   int szFrame;                    /* The size of a single frame */
 49492   i64 iOffset;                    /* Next byte to write in WAL file */
 49493   WalWriter w;                    /* The writer */
 49495   assert( pList );
 49496   assert( pWal->writeLock );
 49498   /* If this frame set completes a transaction, then nTruncate>0.  If
 49499   ** nTruncate==0 then this frame set does not complete the transaction. */
 49500   assert( (isCommit!=0)==(nTruncate!=0) );
 49502 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 49503   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
 49504     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
 49505               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
 49507 #endif
 49509   /* See if it is possible to write these frames into the start of the
 49510   ** log file, instead of appending to it at pWal->hdr.mxFrame.
 49511   */
 49512   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
 49513     return rc;
 49516   /* If this is the first frame written into the log, write the WAL
 49517   ** header to the start of the WAL file. See comments at the top of
 49518   ** this source file for a description of the WAL header format.
 49519   */
 49520   iFrame = pWal->hdr.mxFrame;
 49521   if( iFrame==0 ){
 49522     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
 49523     u32 aCksum[2];                /* Checksum for wal-header */
 49525     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
 49526     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
 49527     sqlite3Put4byte(&aWalHdr[8], szPage);
 49528     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
 49529     if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
 49530     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
 49531     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
 49532     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
 49533     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
 49535     pWal->szPage = szPage;
 49536     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
 49537     pWal->hdr.aFrameCksum[0] = aCksum[0];
 49538     pWal->hdr.aFrameCksum[1] = aCksum[1];
 49539     pWal->truncateOnCommit = 1;
 49541     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
 49542     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
 49543     if( rc!=SQLITE_OK ){
 49544       return rc;
 49547     /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
 49548     ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
 49549     ** an out-of-order write following a WAL restart could result in
 49550     ** database corruption.  See the ticket:
 49551     **
 49552     **     http://localhost:591/sqlite/info/ff5be73dee
 49553     */
 49554     if( pWal->syncHeader && sync_flags ){
 49555       rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
 49556       if( rc ) return rc;
 49559   assert( (int)pWal->szPage==szPage );
 49561   /* Setup information needed to write frames into the WAL */
 49562   w.pWal = pWal;
 49563   w.pFd = pWal->pWalFd;
 49564   w.iSyncPoint = 0;
 49565   w.syncFlags = sync_flags;
 49566   w.szPage = szPage;
 49567   iOffset = walFrameOffset(iFrame+1, szPage);
 49568   szFrame = szPage + WAL_FRAME_HDRSIZE;
 49570   /* Write all frames into the log file exactly once */
 49571   for(p=pList; p; p=p->pDirty){
 49572     int nDbSize;   /* 0 normally.  Positive == commit flag */
 49573     iFrame++;
 49574     assert( iOffset==walFrameOffset(iFrame, szPage) );
 49575     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
 49576     rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
 49577     if( rc ) return rc;
 49578     pLast = p;
 49579     iOffset += szFrame;
 49582   /* If this is the end of a transaction, then we might need to pad
 49583   ** the transaction and/or sync the WAL file.
 49584   **
 49585   ** Padding and syncing only occur if this set of frames complete a
 49586   ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
 49587   ** or synchonous==OFF, then no padding or syncing are needed.
 49588   **
 49589   ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
 49590   ** needed and only the sync is done.  If padding is needed, then the
 49591   ** final frame is repeated (with its commit mark) until the next sector
 49592   ** boundary is crossed.  Only the part of the WAL prior to the last
 49593   ** sector boundary is synced; the part of the last frame that extends
 49594   ** past the sector boundary is written after the sync.
 49595   */
 49596   if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
 49597     if( pWal->padToSectorBoundary ){
 49598       int sectorSize = sqlite3SectorSize(pWal->pWalFd);
 49599       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
 49600       while( iOffset<w.iSyncPoint ){
 49601         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
 49602         if( rc ) return rc;
 49603         iOffset += szFrame;
 49604         nExtra++;
 49606     }else{
 49607       rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
 49611   /* If this frame set completes the first transaction in the WAL and
 49612   ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
 49613   ** journal size limit, if possible.
 49614   */
 49615   if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
 49616     i64 sz = pWal->mxWalSize;
 49617     if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
 49618       sz = walFrameOffset(iFrame+nExtra+1, szPage);
 49620     walLimitSize(pWal, sz);
 49621     pWal->truncateOnCommit = 0;
 49624   /* Append data to the wal-index. It is not necessary to lock the 
 49625   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
 49626   ** guarantees that there are no other writers, and no data that may
 49627   ** be in use by existing readers is being overwritten.
 49628   */
 49629   iFrame = pWal->hdr.mxFrame;
 49630   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
 49631     iFrame++;
 49632     rc = walIndexAppend(pWal, iFrame, p->pgno);
 49634   while( rc==SQLITE_OK && nExtra>0 ){
 49635     iFrame++;
 49636     nExtra--;
 49637     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
 49640   if( rc==SQLITE_OK ){
 49641     /* Update the private copy of the header. */
 49642     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
 49643     testcase( szPage<=32768 );
 49644     testcase( szPage>=65536 );
 49645     pWal->hdr.mxFrame = iFrame;
 49646     if( isCommit ){
 49647       pWal->hdr.iChange++;
 49648       pWal->hdr.nPage = nTruncate;
 49650     /* If this is a commit, update the wal-index header too. */
 49651     if( isCommit ){
 49652       walIndexWriteHdr(pWal);
 49653       pWal->iCallback = iFrame;
 49657   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
 49658   return rc;
 49661 /* 
 49662 ** This routine is called to implement sqlite3_wal_checkpoint() and
 49663 ** related interfaces.
 49664 **
 49665 ** Obtain a CHECKPOINT lock and then backfill as much information as
 49666 ** we can from WAL into the database.
 49667 **
 49668 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
 49669 ** callback. In this case this function runs a blocking checkpoint.
 49670 */
 49671 SQLITE_PRIVATE int sqlite3WalCheckpoint(
 49672   Wal *pWal,                      /* Wal connection */
 49673   int eMode,                      /* PASSIVE, FULL or RESTART */
 49674   int (*xBusy)(void*),            /* Function to call when busy */
 49675   void *pBusyArg,                 /* Context argument for xBusyHandler */
 49676   int sync_flags,                 /* Flags to sync db file with (or 0) */
 49677   int nBuf,                       /* Size of temporary buffer */
 49678   u8 *zBuf,                       /* Temporary buffer to use */
 49679   int *pnLog,                     /* OUT: Number of frames in WAL */
 49680   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 49681 ){
 49682   int rc;                         /* Return code */
 49683   int isChanged = 0;              /* True if a new wal-index header is loaded */
 49684   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
 49686   assert( pWal->ckptLock==0 );
 49687   assert( pWal->writeLock==0 );
 49689   if( pWal->readOnly ) return SQLITE_READONLY;
 49690   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
 49691   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
 49692   if( rc ){
 49693     /* Usually this is SQLITE_BUSY meaning that another thread or process
 49694     ** is already running a checkpoint, or maybe a recovery.  But it might
 49695     ** also be SQLITE_IOERR. */
 49696     return rc;
 49698   pWal->ckptLock = 1;
 49700   /* If this is a blocking-checkpoint, then obtain the write-lock as well
 49701   ** to prevent any writers from running while the checkpoint is underway.
 49702   ** This has to be done before the call to walIndexReadHdr() below.
 49703   **
 49704   ** If the writer lock cannot be obtained, then a passive checkpoint is
 49705   ** run instead. Since the checkpointer is not holding the writer lock,
 49706   ** there is no point in blocking waiting for any readers. Assuming no 
 49707   ** other error occurs, this function will return SQLITE_BUSY to the caller.
 49708   */
 49709   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
 49710     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
 49711     if( rc==SQLITE_OK ){
 49712       pWal->writeLock = 1;
 49713     }else if( rc==SQLITE_BUSY ){
 49714       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
 49715       rc = SQLITE_OK;
 49719   /* Read the wal-index header. */
 49720   if( rc==SQLITE_OK ){
 49721     rc = walIndexReadHdr(pWal, &isChanged);
 49722     if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
 49723       sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
 49727   /* Copy data from the log to the database file. */
 49728   if( rc==SQLITE_OK ){
 49729     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
 49730       rc = SQLITE_CORRUPT_BKPT;
 49731     }else{
 49732       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
 49735     /* If no error occurred, set the output variables. */
 49736     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
 49737       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
 49738       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
 49742   if( isChanged ){
 49743     /* If a new wal-index header was loaded before the checkpoint was 
 49744     ** performed, then the pager-cache associated with pWal is now
 49745     ** out of date. So zero the cached wal-index header to ensure that
 49746     ** next time the pager opens a snapshot on this database it knows that
 49747     ** the cache needs to be reset.
 49748     */
 49749     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 49752   /* Release the locks. */
 49753   sqlite3WalEndWriteTransaction(pWal);
 49754   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
 49755   pWal->ckptLock = 0;
 49756   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
 49757   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
 49760 /* Return the value to pass to a sqlite3_wal_hook callback, the
 49761 ** number of frames in the WAL at the point of the last commit since
 49762 ** sqlite3WalCallback() was called.  If no commits have occurred since
 49763 ** the last call, then return 0.
 49764 */
 49765 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
 49766   u32 ret = 0;
 49767   if( pWal ){
 49768     ret = pWal->iCallback;
 49769     pWal->iCallback = 0;
 49771   return (int)ret;
 49774 /*
 49775 ** This function is called to change the WAL subsystem into or out
 49776 ** of locking_mode=EXCLUSIVE.
 49777 **
 49778 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
 49779 ** into locking_mode=NORMAL.  This means that we must acquire a lock
 49780 ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
 49781 ** or if the acquisition of the lock fails, then return 0.  If the
 49782 ** transition out of exclusive-mode is successful, return 1.  This
 49783 ** operation must occur while the pager is still holding the exclusive
 49784 ** lock on the main database file.
 49785 **
 49786 ** If op is one, then change from locking_mode=NORMAL into 
 49787 ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
 49788 ** be released.  Return 1 if the transition is made and 0 if the
 49789 ** WAL is already in exclusive-locking mode - meaning that this
 49790 ** routine is a no-op.  The pager must already hold the exclusive lock
 49791 ** on the main database file before invoking this operation.
 49792 **
 49793 ** If op is negative, then do a dry-run of the op==1 case but do
 49794 ** not actually change anything. The pager uses this to see if it
 49795 ** should acquire the database exclusive lock prior to invoking
 49796 ** the op==1 case.
 49797 */
 49798 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
 49799   int rc;
 49800   assert( pWal->writeLock==0 );
 49801   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
 49803   /* pWal->readLock is usually set, but might be -1 if there was a 
 49804   ** prior error while attempting to acquire are read-lock. This cannot 
 49805   ** happen if the connection is actually in exclusive mode (as no xShmLock
 49806   ** locks are taken in this case). Nor should the pager attempt to
 49807   ** upgrade to exclusive-mode following such an error.
 49808   */
 49809   assert( pWal->readLock>=0 || pWal->lockError );
 49810   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
 49812   if( op==0 ){
 49813     if( pWal->exclusiveMode ){
 49814       pWal->exclusiveMode = 0;
 49815       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
 49816         pWal->exclusiveMode = 1;
 49818       rc = pWal->exclusiveMode==0;
 49819     }else{
 49820       /* Already in locking_mode=NORMAL */
 49821       rc = 0;
 49823   }else if( op>0 ){
 49824     assert( pWal->exclusiveMode==0 );
 49825     assert( pWal->readLock>=0 );
 49826     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
 49827     pWal->exclusiveMode = 1;
 49828     rc = 1;
 49829   }else{
 49830     rc = pWal->exclusiveMode==0;
 49832   return rc;
 49835 /* 
 49836 ** Return true if the argument is non-NULL and the WAL module is using
 49837 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
 49838 ** WAL module is using shared-memory, return false. 
 49839 */
 49840 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
 49841   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
 49844 #ifdef SQLITE_ENABLE_ZIPVFS
 49845 /*
 49846 ** If the argument is not NULL, it points to a Wal object that holds a
 49847 ** read-lock. This function returns the database page-size if it is known,
 49848 ** or zero if it is not (or if pWal is NULL).
 49849 */
 49850 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
 49851   assert( pWal==0 || pWal->readLock>=0 );
 49852   return (pWal ? pWal->szPage : 0);
 49854 #endif
 49856 #endif /* #ifndef SQLITE_OMIT_WAL */
 49858 /************** End of wal.c *************************************************/
 49859 /************** Begin file btmutex.c *****************************************/
 49860 /*
 49861 ** 2007 August 27
 49862 **
 49863 ** The author disclaims copyright to this source code.  In place of
 49864 ** a legal notice, here is a blessing:
 49865 **
 49866 **    May you do good and not evil.
 49867 **    May you find forgiveness for yourself and forgive others.
 49868 **    May you share freely, never taking more than you give.
 49869 **
 49870 *************************************************************************
 49871 **
 49872 ** This file contains code used to implement mutexes on Btree objects.
 49873 ** This code really belongs in btree.c.  But btree.c is getting too
 49874 ** big and we want to break it down some.  This packaged seemed like
 49875 ** a good breakout.
 49876 */
 49877 /************** Include btreeInt.h in the middle of btmutex.c ****************/
 49878 /************** Begin file btreeInt.h ****************************************/
 49879 /*
 49880 ** 2004 April 6
 49881 **
 49882 ** The author disclaims copyright to this source code.  In place of
 49883 ** a legal notice, here is a blessing:
 49884 **
 49885 **    May you do good and not evil.
 49886 **    May you find forgiveness for yourself and forgive others.
 49887 **    May you share freely, never taking more than you give.
 49888 **
 49889 *************************************************************************
 49890 ** This file implements a external (disk-based) database using BTrees.
 49891 ** For a detailed discussion of BTrees, refer to
 49892 **
 49893 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
 49894 **     "Sorting And Searching", pages 473-480. Addison-Wesley
 49895 **     Publishing Company, Reading, Massachusetts.
 49896 **
 49897 ** The basic idea is that each page of the file contains N database
 49898 ** entries and N+1 pointers to subpages.
 49899 **
 49900 **   ----------------------------------------------------------------
 49901 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
 49902 **   ----------------------------------------------------------------
 49903 **
 49904 ** All of the keys on the page that Ptr(0) points to have values less
 49905 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
 49906 ** values greater than Key(0) and less than Key(1).  All of the keys
 49907 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
 49908 ** so forth.
 49909 **
 49910 ** Finding a particular key requires reading O(log(M)) pages from the 
 49911 ** disk where M is the number of entries in the tree.
 49912 **
 49913 ** In this implementation, a single file can hold one or more separate 
 49914 ** BTrees.  Each BTree is identified by the index of its root page.  The
 49915 ** key and data for any entry are combined to form the "payload".  A
 49916 ** fixed amount of payload can be carried directly on the database
 49917 ** page.  If the payload is larger than the preset amount then surplus
 49918 ** bytes are stored on overflow pages.  The payload for an entry
 49919 ** and the preceding pointer are combined to form a "Cell".  Each 
 49920 ** page has a small header which contains the Ptr(N) pointer and other
 49921 ** information such as the size of key and data.
 49922 **
 49923 ** FORMAT DETAILS
 49924 **
 49925 ** The file is divided into pages.  The first page is called page 1,
 49926 ** the second is page 2, and so forth.  A page number of zero indicates
 49927 ** "no such page".  The page size can be any power of 2 between 512 and 65536.
 49928 ** Each page can be either a btree page, a freelist page, an overflow
 49929 ** page, or a pointer-map page.
 49930 **
 49931 ** The first page is always a btree page.  The first 100 bytes of the first
 49932 ** page contain a special header (the "file header") that describes the file.
 49933 ** The format of the file header is as follows:
 49934 **
 49935 **   OFFSET   SIZE    DESCRIPTION
 49936 **      0      16     Header string: "SQLite format 3\000"
 49937 **     16       2     Page size in bytes.  (1 means 65536)
 49938 **     18       1     File format write version
 49939 **     19       1     File format read version
 49940 **     20       1     Bytes of unused space at the end of each page
 49941 **     21       1     Max embedded payload fraction (must be 64)
 49942 **     22       1     Min embedded payload fraction (must be 32)
 49943 **     23       1     Min leaf payload fraction (must be 32)
 49944 **     24       4     File change counter
 49945 **     28       4     Reserved for future use
 49946 **     32       4     First freelist page
 49947 **     36       4     Number of freelist pages in the file
 49948 **     40      60     15 4-byte meta values passed to higher layers
 49949 **
 49950 **     40       4     Schema cookie
 49951 **     44       4     File format of schema layer
 49952 **     48       4     Size of page cache
 49953 **     52       4     Largest root-page (auto/incr_vacuum)
 49954 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
 49955 **     60       4     User version
 49956 **     64       4     Incremental vacuum mode
 49957 **     68       4     Application-ID
 49958 **     72      20     unused
 49959 **     92       4     The version-valid-for number
 49960 **     96       4     SQLITE_VERSION_NUMBER
 49961 **
 49962 ** All of the integer values are big-endian (most significant byte first).
 49963 **
 49964 ** The file change counter is incremented when the database is changed
 49965 ** This counter allows other processes to know when the file has changed
 49966 ** and thus when they need to flush their cache.
 49967 **
 49968 ** The max embedded payload fraction is the amount of the total usable
 49969 ** space in a page that can be consumed by a single cell for standard
 49970 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
 49971 ** is to limit the maximum cell size so that at least 4 cells will fit
 49972 ** on one page.  Thus the default max embedded payload fraction is 64.
 49973 **
 49974 ** If the payload for a cell is larger than the max payload, then extra
 49975 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
 49976 ** as many bytes as possible are moved into the overflow pages without letting
 49977 ** the cell size drop below the min embedded payload fraction.
 49978 **
 49979 ** The min leaf payload fraction is like the min embedded payload fraction
 49980 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
 49981 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
 49982 ** not specified in the header.
 49983 **
 49984 ** Each btree pages is divided into three sections:  The header, the
 49985 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
 49986 ** file header that occurs before the page header.
 49987 **
 49988 **      |----------------|
 49989 **      | file header    |   100 bytes.  Page 1 only.
 49990 **      |----------------|
 49991 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
 49992 **      |----------------|
 49993 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
 49994 **      | array          |   |  Grows downward
 49995 **      |                |   v
 49996 **      |----------------|
 49997 **      | unallocated    |
 49998 **      | space          |
 49999 **      |----------------|   ^  Grows upwards
 50000 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
 50001 **      | area           |   |  and free space fragments.
 50002 **      |----------------|
 50003 **
 50004 ** The page headers looks like this:
 50005 **
 50006 **   OFFSET   SIZE     DESCRIPTION
 50007 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
 50008 **      1       2      byte offset to the first freeblock
 50009 **      3       2      number of cells on this page
 50010 **      5       2      first byte of the cell content area
 50011 **      7       1      number of fragmented free bytes
 50012 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
 50013 **
 50014 ** The flags define the format of this btree page.  The leaf flag means that
 50015 ** this page has no children.  The zerodata flag means that this page carries
 50016 ** only keys and no data.  The intkey flag means that the key is a integer
 50017 ** which is stored in the key size entry of the cell header rather than in
 50018 ** the payload area.
 50019 **
 50020 ** The cell pointer array begins on the first byte after the page header.
 50021 ** The cell pointer array contains zero or more 2-byte numbers which are
 50022 ** offsets from the beginning of the page to the cell content in the cell
 50023 ** content area.  The cell pointers occur in sorted order.  The system strives
 50024 ** to keep free space after the last cell pointer so that new cells can
 50025 ** be easily added without having to defragment the page.
 50026 **
 50027 ** Cell content is stored at the very end of the page and grows toward the
 50028 ** beginning of the page.
 50029 **
 50030 ** Unused space within the cell content area is collected into a linked list of
 50031 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
 50032 ** to the first freeblock is given in the header.  Freeblocks occur in
 50033 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
 50034 ** any group of 3 or fewer unused bytes in the cell content area cannot
 50035 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
 50036 ** a fragment.  The total number of bytes in all fragments is recorded.
 50037 ** in the page header at offset 7.
 50038 **
 50039 **    SIZE    DESCRIPTION
 50040 **      2     Byte offset of the next freeblock
 50041 **      2     Bytes in this freeblock
 50042 **
 50043 ** Cells are of variable length.  Cells are stored in the cell content area at
 50044 ** the end of the page.  Pointers to the cells are in the cell pointer array
 50045 ** that immediately follows the page header.  Cells is not necessarily
 50046 ** contiguous or in order, but cell pointers are contiguous and in order.
 50047 **
 50048 ** Cell content makes use of variable length integers.  A variable
 50049 ** length integer is 1 to 9 bytes where the lower 7 bits of each 
 50050 ** byte are used.  The integer consists of all bytes that have bit 8 set and
 50051 ** the first byte with bit 8 clear.  The most significant byte of the integer
 50052 ** appears first.  A variable-length integer may not be more than 9 bytes long.
 50053 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
 50054 ** allows a 64-bit integer to be encoded in 9 bytes.
 50055 **
 50056 **    0x00                      becomes  0x00000000
 50057 **    0x7f                      becomes  0x0000007f
 50058 **    0x81 0x00                 becomes  0x00000080
 50059 **    0x82 0x00                 becomes  0x00000100
 50060 **    0x80 0x7f                 becomes  0x0000007f
 50061 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
 50062 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
 50063 **
 50064 ** Variable length integers are used for rowids and to hold the number of
 50065 ** bytes of key and data in a btree cell.
 50066 **
 50067 ** The content of a cell looks like this:
 50068 **
 50069 **    SIZE    DESCRIPTION
 50070 **      4     Page number of the left child. Omitted if leaf flag is set.
 50071 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
 50072 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
 50073 **      *     Payload
 50074 **      4     First page of the overflow chain.  Omitted if no overflow
 50075 **
 50076 ** Overflow pages form a linked list.  Each page except the last is completely
 50077 ** filled with data (pagesize - 4 bytes).  The last page can have as little
 50078 ** as 1 byte of data.
 50079 **
 50080 **    SIZE    DESCRIPTION
 50081 **      4     Page number of next overflow page
 50082 **      *     Data
 50083 **
 50084 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
 50085 ** file header points to the first in a linked list of trunk page.  Each trunk
 50086 ** page points to multiple leaf pages.  The content of a leaf page is
 50087 ** unspecified.  A trunk page looks like this:
 50088 **
 50089 **    SIZE    DESCRIPTION
 50090 **      4     Page number of next trunk page
 50091 **      4     Number of leaf pointers on this page
 50092 **      *     zero or more pages numbers of leaves
 50093 */
 50096 /* The following value is the maximum cell size assuming a maximum page
 50097 ** size give above.
 50098 */
 50099 #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
 50101 /* The maximum number of cells on a single page of the database.  This
 50102 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
 50103 ** plus 2 bytes for the index to the cell in the page header).  Such
 50104 ** small cells will be rare, but they are possible.
 50105 */
 50106 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
 50108 /* Forward declarations */
 50109 typedef struct MemPage MemPage;
 50110 typedef struct BtLock BtLock;
 50112 /*
 50113 ** This is a magic string that appears at the beginning of every
 50114 ** SQLite database in order to identify the file as a real database.
 50115 **
 50116 ** You can change this value at compile-time by specifying a
 50117 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
 50118 ** header must be exactly 16 bytes including the zero-terminator so
 50119 ** the string itself should be 15 characters long.  If you change
 50120 ** the header, then your custom library will not be able to read 
 50121 ** databases generated by the standard tools and the standard tools
 50122 ** will not be able to read databases created by your custom library.
 50123 */
 50124 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
 50125 #  define SQLITE_FILE_HEADER "SQLite format 3"
 50126 #endif
 50128 /*
 50129 ** Page type flags.  An ORed combination of these flags appear as the
 50130 ** first byte of on-disk image of every BTree page.
 50131 */
 50132 #define PTF_INTKEY    0x01
 50133 #define PTF_ZERODATA  0x02
 50134 #define PTF_LEAFDATA  0x04
 50135 #define PTF_LEAF      0x08
 50137 /*
 50138 ** As each page of the file is loaded into memory, an instance of the following
 50139 ** structure is appended and initialized to zero.  This structure stores
 50140 ** information about the page that is decoded from the raw file page.
 50141 **
 50142 ** The pParent field points back to the parent page.  This allows us to
 50143 ** walk up the BTree from any leaf to the root.  Care must be taken to
 50144 ** unref() the parent page pointer when this page is no longer referenced.
 50145 ** The pageDestructor() routine handles that chore.
 50146 **
 50147 ** Access to all fields of this structure is controlled by the mutex
 50148 ** stored in MemPage.pBt->mutex.
 50149 */
 50150 struct MemPage {
 50151   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
 50152   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
 50153   u8 intKey;           /* True if intkey flag is set */
 50154   u8 leaf;             /* True if leaf flag is set */
 50155   u8 hasData;          /* True if this page stores data */
 50156   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
 50157   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
 50158   u8 max1bytePayload;  /* min(maxLocal,127) */
 50159   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
 50160   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
 50161   u16 cellOffset;      /* Index in aData of first cell pointer */
 50162   u16 nFree;           /* Number of free bytes on the page */
 50163   u16 nCell;           /* Number of cells on this page, local and ovfl */
 50164   u16 maskPage;        /* Mask for page offset */
 50165   u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
 50166                        ** non-overflow cell */
 50167   u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
 50168   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
 50169   u8 *aData;           /* Pointer to disk image of the page data */
 50170   u8 *aDataEnd;        /* One byte past the end of usable data */
 50171   u8 *aCellIdx;        /* The cell index area */
 50172   DbPage *pDbPage;     /* Pager page handle */
 50173   Pgno pgno;           /* Page number for this page */
 50174 };
 50176 /*
 50177 ** The in-memory image of a disk page has the auxiliary information appended
 50178 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
 50179 ** that extra information.
 50180 */
 50181 #define EXTRA_SIZE sizeof(MemPage)
 50183 /*
 50184 ** A linked list of the following structures is stored at BtShared.pLock.
 50185 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
 50186 ** is opened on the table with root page BtShared.iTable. Locks are removed
 50187 ** from this list when a transaction is committed or rolled back, or when
 50188 ** a btree handle is closed.
 50189 */
 50190 struct BtLock {
 50191   Btree *pBtree;        /* Btree handle holding this lock */
 50192   Pgno iTable;          /* Root page of table */
 50193   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
 50194   BtLock *pNext;        /* Next in BtShared.pLock list */
 50195 };
 50197 /* Candidate values for BtLock.eLock */
 50198 #define READ_LOCK     1
 50199 #define WRITE_LOCK    2
 50201 /* A Btree handle
 50202 **
 50203 ** A database connection contains a pointer to an instance of
 50204 ** this object for every database file that it has open.  This structure
 50205 ** is opaque to the database connection.  The database connection cannot
 50206 ** see the internals of this structure and only deals with pointers to
 50207 ** this structure.
 50208 **
 50209 ** For some database files, the same underlying database cache might be 
 50210 ** shared between multiple connections.  In that case, each connection
 50211 ** has it own instance of this object.  But each instance of this object
 50212 ** points to the same BtShared object.  The database cache and the
 50213 ** schema associated with the database file are all contained within
 50214 ** the BtShared object.
 50215 **
 50216 ** All fields in this structure are accessed under sqlite3.mutex.
 50217 ** The pBt pointer itself may not be changed while there exists cursors 
 50218 ** in the referenced BtShared that point back to this Btree since those
 50219 ** cursors have to go through this Btree to find their BtShared and
 50220 ** they often do so without holding sqlite3.mutex.
 50221 */
 50222 struct Btree {
 50223   sqlite3 *db;       /* The database connection holding this btree */
 50224   BtShared *pBt;     /* Sharable content of this btree */
 50225   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
 50226   u8 sharable;       /* True if we can share pBt with another db */
 50227   u8 locked;         /* True if db currently has pBt locked */
 50228   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
 50229   int nBackup;       /* Number of backup operations reading this btree */
 50230   Btree *pNext;      /* List of other sharable Btrees from the same db */
 50231   Btree *pPrev;      /* Back pointer of the same list */
 50232 #ifndef SQLITE_OMIT_SHARED_CACHE
 50233   BtLock lock;       /* Object used to lock page 1 */
 50234 #endif
 50235 };
 50237 /*
 50238 ** Btree.inTrans may take one of the following values.
 50239 **
 50240 ** If the shared-data extension is enabled, there may be multiple users
 50241 ** of the Btree structure. At most one of these may open a write transaction,
 50242 ** but any number may have active read transactions.
 50243 */
 50244 #define TRANS_NONE  0
 50245 #define TRANS_READ  1
 50246 #define TRANS_WRITE 2
 50248 /*
 50249 ** An instance of this object represents a single database file.
 50250 ** 
 50251 ** A single database file can be in use at the same time by two
 50252 ** or more database connections.  When two or more connections are
 50253 ** sharing the same database file, each connection has it own
 50254 ** private Btree object for the file and each of those Btrees points
 50255 ** to this one BtShared object.  BtShared.nRef is the number of
 50256 ** connections currently sharing this database file.
 50257 **
 50258 ** Fields in this structure are accessed under the BtShared.mutex
 50259 ** mutex, except for nRef and pNext which are accessed under the
 50260 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
 50261 ** may not be modified once it is initially set as long as nRef>0.
 50262 ** The pSchema field may be set once under BtShared.mutex and
 50263 ** thereafter is unchanged as long as nRef>0.
 50264 **
 50265 ** isPending:
 50266 **
 50267 **   If a BtShared client fails to obtain a write-lock on a database
 50268 **   table (because there exists one or more read-locks on the table),
 50269 **   the shared-cache enters 'pending-lock' state and isPending is
 50270 **   set to true.
 50271 **
 50272 **   The shared-cache leaves the 'pending lock' state when either of
 50273 **   the following occur:
 50274 **
 50275 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
 50276 **     2) The number of locks held by other connections drops to zero.
 50277 **
 50278 **   while in the 'pending-lock' state, no connection may start a new
 50279 **   transaction.
 50280 **
 50281 **   This feature is included to help prevent writer-starvation.
 50282 */
 50283 struct BtShared {
 50284   Pager *pPager;        /* The page cache */
 50285   sqlite3 *db;          /* Database connection currently using this Btree */
 50286   BtCursor *pCursor;    /* A list of all open cursors */
 50287   MemPage *pPage1;      /* First page of the database */
 50288   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
 50289 #ifndef SQLITE_OMIT_AUTOVACUUM
 50290   u8 autoVacuum;        /* True if auto-vacuum is enabled */
 50291   u8 incrVacuum;        /* True if incr-vacuum is enabled */
 50292   u8 bDoTruncate;       /* True to truncate db on commit */
 50293 #endif
 50294   u8 inTransaction;     /* Transaction state */
 50295   u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
 50296   u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
 50297   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
 50298   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
 50299   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
 50300   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
 50301   u32 pageSize;         /* Total number of bytes on a page */
 50302   u32 usableSize;       /* Number of usable bytes on each page */
 50303   int nTransaction;     /* Number of open transactions (read + write) */
 50304   u32 nPage;            /* Number of pages in the database */
 50305   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
 50306   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
 50307   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
 50308   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
 50309 #ifndef SQLITE_OMIT_SHARED_CACHE
 50310   int nRef;             /* Number of references to this structure */
 50311   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
 50312   BtLock *pLock;        /* List of locks held on this shared-btree struct */
 50313   Btree *pWriter;       /* Btree with currently open write transaction */
 50314 #endif
 50315   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
 50316 };
 50318 /*
 50319 ** Allowed values for BtShared.btsFlags
 50320 */
 50321 #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
 50322 #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
 50323 #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
 50324 #define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
 50325 #define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
 50326 #define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
 50327 #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
 50329 /*
 50330 ** An instance of the following structure is used to hold information
 50331 ** about a cell.  The parseCellPtr() function fills in this structure
 50332 ** based on information extract from the raw disk page.
 50333 */
 50334 typedef struct CellInfo CellInfo;
 50335 struct CellInfo {
 50336   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
 50337   u8 *pCell;     /* Pointer to the start of cell content */
 50338   u32 nData;     /* Number of bytes of data */
 50339   u32 nPayload;  /* Total amount of payload */
 50340   u16 nHeader;   /* Size of the cell content header in bytes */
 50341   u16 nLocal;    /* Amount of payload held locally */
 50342   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
 50343   u16 nSize;     /* Size of the cell content on the main b-tree page */
 50344 };
 50346 /*
 50347 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
 50348 ** this will be declared corrupt. This value is calculated based on a
 50349 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
 50350 ** root-node and 3 for all other internal nodes.
 50351 **
 50352 ** If a tree that appears to be taller than this is encountered, it is
 50353 ** assumed that the database is corrupt.
 50354 */
 50355 #define BTCURSOR_MAX_DEPTH 20
 50357 /*
 50358 ** A cursor is a pointer to a particular entry within a particular
 50359 ** b-tree within a database file.
 50360 **
 50361 ** The entry is identified by its MemPage and the index in
 50362 ** MemPage.aCell[] of the entry.
 50363 **
 50364 ** A single database file can be shared by two more database connections,
 50365 ** but cursors cannot be shared.  Each cursor is associated with a
 50366 ** particular database connection identified BtCursor.pBtree.db.
 50367 **
 50368 ** Fields in this structure are accessed under the BtShared.mutex
 50369 ** found at self->pBt->mutex. 
 50370 */
 50371 struct BtCursor {
 50372   Btree *pBtree;            /* The Btree to which this cursor belongs */
 50373   BtShared *pBt;            /* The BtShared this cursor points to */
 50374   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
 50375   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
 50376 #ifndef SQLITE_OMIT_INCRBLOB
 50377   Pgno *aOverflow;          /* Cache of overflow page locations */
 50378 #endif
 50379   Pgno pgnoRoot;            /* The root page of this tree */
 50380   CellInfo info;            /* A parse of the cell we are pointing at */
 50381   i64 nKey;        /* Size of pKey, or last integer key */
 50382   void *pKey;      /* Saved key that was cursor's last known position */
 50383   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
 50384   u8 wrFlag;                /* True if writable */
 50385   u8 atLast;                /* Cursor pointing to the last entry */
 50386   u8 validNKey;             /* True if info.nKey is valid */
 50387   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
 50388 #ifndef SQLITE_OMIT_INCRBLOB
 50389   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
 50390 #endif
 50391   u8 hints;                             /* As configured by CursorSetHints() */
 50392   i16 iPage;                            /* Index of current page in apPage */
 50393   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
 50394   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
 50395 };
 50397 /*
 50398 ** Potential values for BtCursor.eState.
 50399 **
 50400 ** CURSOR_INVALID:
 50401 **   Cursor does not point to a valid entry. This can happen (for example) 
 50402 **   because the table is empty or because BtreeCursorFirst() has not been
 50403 **   called.
 50404 **
 50405 ** CURSOR_VALID:
 50406 **   Cursor points to a valid entry. getPayload() etc. may be called.
 50407 **
 50408 ** CURSOR_SKIPNEXT:
 50409 **   Cursor is valid except that the Cursor.skipNext field is non-zero
 50410 **   indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
 50411 **   operation should be a no-op.
 50412 **
 50413 ** CURSOR_REQUIRESEEK:
 50414 **   The table that this cursor was opened on still exists, but has been 
 50415 **   modified since the cursor was last used. The cursor position is saved
 50416 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
 50417 **   this state, restoreCursorPosition() can be called to attempt to
 50418 **   seek the cursor to the saved position.
 50419 **
 50420 ** CURSOR_FAULT:
 50421 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
 50422 **   on a different connection that shares the BtShared cache with this
 50423 **   cursor.  The error has left the cache in an inconsistent state.
 50424 **   Do nothing else with this cursor.  Any attempt to use the cursor
 50425 **   should return the error code stored in BtCursor.skip
 50426 */
 50427 #define CURSOR_INVALID           0
 50428 #define CURSOR_VALID             1
 50429 #define CURSOR_SKIPNEXT          2
 50430 #define CURSOR_REQUIRESEEK       3
 50431 #define CURSOR_FAULT             4
 50433 /* 
 50434 ** The database page the PENDING_BYTE occupies. This page is never used.
 50435 */
 50436 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
 50438 /*
 50439 ** These macros define the location of the pointer-map entry for a 
 50440 ** database page. The first argument to each is the number of usable
 50441 ** bytes on each page of the database (often 1024). The second is the
 50442 ** page number to look up in the pointer map.
 50443 **
 50444 ** PTRMAP_PAGENO returns the database page number of the pointer-map
 50445 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
 50446 ** the offset of the requested map entry.
 50447 **
 50448 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
 50449 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
 50450 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
 50451 ** this test.
 50452 */
 50453 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
 50454 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
 50455 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
 50457 /*
 50458 ** The pointer map is a lookup table that identifies the parent page for
 50459 ** each child page in the database file.  The parent page is the page that
 50460 ** contains a pointer to the child.  Every page in the database contains
 50461 ** 0 or 1 parent pages.  (In this context 'database page' refers
 50462 ** to any page that is not part of the pointer map itself.)  Each pointer map
 50463 ** entry consists of a single byte 'type' and a 4 byte parent page number.
 50464 ** The PTRMAP_XXX identifiers below are the valid types.
 50465 **
 50466 ** The purpose of the pointer map is to facility moving pages from one
 50467 ** position in the file to another as part of autovacuum.  When a page
 50468 ** is moved, the pointer in its parent must be updated to point to the
 50469 ** new location.  The pointer map is used to locate the parent page quickly.
 50470 **
 50471 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
 50472 **                  used in this case.
 50473 **
 50474 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
 50475 **                  is not used in this case.
 50476 **
 50477 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
 50478 **                   overflow pages. The page number identifies the page that
 50479 **                   contains the cell with a pointer to this overflow page.
 50480 **
 50481 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
 50482 **                   overflow pages. The page-number identifies the previous
 50483 **                   page in the overflow page list.
 50484 **
 50485 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
 50486 **               identifies the parent page in the btree.
 50487 */
 50488 #define PTRMAP_ROOTPAGE 1
 50489 #define PTRMAP_FREEPAGE 2
 50490 #define PTRMAP_OVERFLOW1 3
 50491 #define PTRMAP_OVERFLOW2 4
 50492 #define PTRMAP_BTREE 5
 50494 /* A bunch of assert() statements to check the transaction state variables
 50495 ** of handle p (type Btree*) are internally consistent.
 50496 */
 50497 #define btreeIntegrity(p) \
 50498   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
 50499   assert( p->pBt->inTransaction>=p->inTrans ); 
 50502 /*
 50503 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
 50504 ** if the database supports auto-vacuum or not. Because it is used
 50505 ** within an expression that is an argument to another macro 
 50506 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
 50507 ** So, this macro is defined instead.
 50508 */
 50509 #ifndef SQLITE_OMIT_AUTOVACUUM
 50510 #define ISAUTOVACUUM (pBt->autoVacuum)
 50511 #else
 50512 #define ISAUTOVACUUM 0
 50513 #endif
 50516 /*
 50517 ** This structure is passed around through all the sanity checking routines
 50518 ** in order to keep track of some global state information.
 50519 **
 50520 ** The aRef[] array is allocated so that there is 1 bit for each page in
 50521 ** the database. As the integrity-check proceeds, for each page used in
 50522 ** the database the corresponding bit is set. This allows integrity-check to 
 50523 ** detect pages that are used twice and orphaned pages (both of which 
 50524 ** indicate corruption).
 50525 */
 50526 typedef struct IntegrityCk IntegrityCk;
 50527 struct IntegrityCk {
 50528   BtShared *pBt;    /* The tree being checked out */
 50529   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
 50530   u8 *aPgRef;       /* 1 bit per page in the db (see above) */
 50531   Pgno nPage;       /* Number of pages in the database */
 50532   int mxErr;        /* Stop accumulating errors when this reaches zero */
 50533   int nErr;         /* Number of messages written to zErrMsg so far */
 50534   int mallocFailed; /* A memory allocation error has occurred */
 50535   StrAccum errMsg;  /* Accumulate the error message text here */
 50536 };
 50538 /*
 50539 ** Routines to read or write a two- and four-byte big-endian integer values.
 50540 */
 50541 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
 50542 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
 50543 #define get4byte sqlite3Get4byte
 50544 #define put4byte sqlite3Put4byte
 50546 /************** End of btreeInt.h ********************************************/
 50547 /************** Continuing where we left off in btmutex.c ********************/
 50548 #ifndef SQLITE_OMIT_SHARED_CACHE
 50549 #if SQLITE_THREADSAFE
 50551 /*
 50552 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
 50553 ** set BtShared.db to the database handle associated with p and the
 50554 ** p->locked boolean to true.
 50555 */
 50556 static void lockBtreeMutex(Btree *p){
 50557   assert( p->locked==0 );
 50558   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
 50559   assert( sqlite3_mutex_held(p->db->mutex) );
 50561   sqlite3_mutex_enter(p->pBt->mutex);
 50562   p->pBt->db = p->db;
 50563   p->locked = 1;
 50566 /*
 50567 ** Release the BtShared mutex associated with B-Tree handle p and
 50568 ** clear the p->locked boolean.
 50569 */
 50570 static void unlockBtreeMutex(Btree *p){
 50571   BtShared *pBt = p->pBt;
 50572   assert( p->locked==1 );
 50573   assert( sqlite3_mutex_held(pBt->mutex) );
 50574   assert( sqlite3_mutex_held(p->db->mutex) );
 50575   assert( p->db==pBt->db );
 50577   sqlite3_mutex_leave(pBt->mutex);
 50578   p->locked = 0;
 50581 /*
 50582 ** Enter a mutex on the given BTree object.
 50583 **
 50584 ** If the object is not sharable, then no mutex is ever required
 50585 ** and this routine is a no-op.  The underlying mutex is non-recursive.
 50586 ** But we keep a reference count in Btree.wantToLock so the behavior
 50587 ** of this interface is recursive.
 50588 **
 50589 ** To avoid deadlocks, multiple Btrees are locked in the same order
 50590 ** by all database connections.  The p->pNext is a list of other
 50591 ** Btrees belonging to the same database connection as the p Btree
 50592 ** which need to be locked after p.  If we cannot get a lock on
 50593 ** p, then first unlock all of the others on p->pNext, then wait
 50594 ** for the lock to become available on p, then relock all of the
 50595 ** subsequent Btrees that desire a lock.
 50596 */
 50597 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 50598   Btree *pLater;
 50600   /* Some basic sanity checking on the Btree.  The list of Btrees
 50601   ** connected by pNext and pPrev should be in sorted order by
 50602   ** Btree.pBt value. All elements of the list should belong to
 50603   ** the same connection. Only shared Btrees are on the list. */
 50604   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
 50605   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
 50606   assert( p->pNext==0 || p->pNext->db==p->db );
 50607   assert( p->pPrev==0 || p->pPrev->db==p->db );
 50608   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
 50610   /* Check for locking consistency */
 50611   assert( !p->locked || p->wantToLock>0 );
 50612   assert( p->sharable || p->wantToLock==0 );
 50614   /* We should already hold a lock on the database connection */
 50615   assert( sqlite3_mutex_held(p->db->mutex) );
 50617   /* Unless the database is sharable and unlocked, then BtShared.db
 50618   ** should already be set correctly. */
 50619   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
 50621   if( !p->sharable ) return;
 50622   p->wantToLock++;
 50623   if( p->locked ) return;
 50625   /* In most cases, we should be able to acquire the lock we
 50626   ** want without having to go throught the ascending lock
 50627   ** procedure that follows.  Just be sure not to block.
 50628   */
 50629   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
 50630     p->pBt->db = p->db;
 50631     p->locked = 1;
 50632     return;
 50635   /* To avoid deadlock, first release all locks with a larger
 50636   ** BtShared address.  Then acquire our lock.  Then reacquire
 50637   ** the other BtShared locks that we used to hold in ascending
 50638   ** order.
 50639   */
 50640   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 50641     assert( pLater->sharable );
 50642     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
 50643     assert( !pLater->locked || pLater->wantToLock>0 );
 50644     if( pLater->locked ){
 50645       unlockBtreeMutex(pLater);
 50648   lockBtreeMutex(p);
 50649   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
 50650     if( pLater->wantToLock ){
 50651       lockBtreeMutex(pLater);
 50656 /*
 50657 ** Exit the recursive mutex on a Btree.
 50658 */
 50659 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
 50660   if( p->sharable ){
 50661     assert( p->wantToLock>0 );
 50662     p->wantToLock--;
 50663     if( p->wantToLock==0 ){
 50664       unlockBtreeMutex(p);
 50669 #ifndef NDEBUG
 50670 /*
 50671 ** Return true if the BtShared mutex is held on the btree, or if the
 50672 ** B-Tree is not marked as sharable.
 50673 **
 50674 ** This routine is used only from within assert() statements.
 50675 */
 50676 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
 50677   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
 50678   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
 50679   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
 50680   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
 50682   return (p->sharable==0 || p->locked);
 50684 #endif
 50687 #ifndef SQLITE_OMIT_INCRBLOB
 50688 /*
 50689 ** Enter and leave a mutex on a Btree given a cursor owned by that
 50690 ** Btree.  These entry points are used by incremental I/O and can be
 50691 ** omitted if that module is not used.
 50692 */
 50693 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
 50694   sqlite3BtreeEnter(pCur->pBtree);
 50696 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
 50697   sqlite3BtreeLeave(pCur->pBtree);
 50699 #endif /* SQLITE_OMIT_INCRBLOB */
 50702 /*
 50703 ** Enter the mutex on every Btree associated with a database
 50704 ** connection.  This is needed (for example) prior to parsing
 50705 ** a statement since we will be comparing table and column names
 50706 ** against all schemas and we do not want those schemas being
 50707 ** reset out from under us.
 50708 **
 50709 ** There is a corresponding leave-all procedures.
 50710 **
 50711 ** Enter the mutexes in accending order by BtShared pointer address
 50712 ** to avoid the possibility of deadlock when two threads with
 50713 ** two or more btrees in common both try to lock all their btrees
 50714 ** at the same instant.
 50715 */
 50716 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 50717   int i;
 50718   Btree *p;
 50719   assert( sqlite3_mutex_held(db->mutex) );
 50720   for(i=0; i<db->nDb; i++){
 50721     p = db->aDb[i].pBt;
 50722     if( p ) sqlite3BtreeEnter(p);
 50725 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 50726   int i;
 50727   Btree *p;
 50728   assert( sqlite3_mutex_held(db->mutex) );
 50729   for(i=0; i<db->nDb; i++){
 50730     p = db->aDb[i].pBt;
 50731     if( p ) sqlite3BtreeLeave(p);
 50735 /*
 50736 ** Return true if a particular Btree requires a lock.  Return FALSE if
 50737 ** no lock is ever required since it is not sharable.
 50738 */
 50739 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
 50740   return p->sharable;
 50743 #ifndef NDEBUG
 50744 /*
 50745 ** Return true if the current thread holds the database connection
 50746 ** mutex and all required BtShared mutexes.
 50747 **
 50748 ** This routine is used inside assert() statements only.
 50749 */
 50750 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
 50751   int i;
 50752   if( !sqlite3_mutex_held(db->mutex) ){
 50753     return 0;
 50755   for(i=0; i<db->nDb; i++){
 50756     Btree *p;
 50757     p = db->aDb[i].pBt;
 50758     if( p && p->sharable &&
 50759          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
 50760       return 0;
 50763   return 1;
 50765 #endif /* NDEBUG */
 50767 #ifndef NDEBUG
 50768 /*
 50769 ** Return true if the correct mutexes are held for accessing the
 50770 ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
 50771 ** access are:
 50772 **
 50773 **   (1) The mutex on db
 50774 **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
 50775 **
 50776 ** If pSchema is not NULL, then iDb is computed from pSchema and
 50777 ** db using sqlite3SchemaToIndex().
 50778 */
 50779 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
 50780   Btree *p;
 50781   assert( db!=0 );
 50782   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
 50783   assert( iDb>=0 && iDb<db->nDb );
 50784   if( !sqlite3_mutex_held(db->mutex) ) return 0;
 50785   if( iDb==1 ) return 1;
 50786   p = db->aDb[iDb].pBt;
 50787   assert( p!=0 );
 50788   return p->sharable==0 || p->locked==1;
 50790 #endif /* NDEBUG */
 50792 #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
 50793 /*
 50794 ** The following are special cases for mutex enter routines for use
 50795 ** in single threaded applications that use shared cache.  Except for
 50796 ** these two routines, all mutex operations are no-ops in that case and
 50797 ** are null #defines in btree.h.
 50798 **
 50799 ** If shared cache is disabled, then all btree mutex routines, including
 50800 ** the ones below, are no-ops and are null #defines in btree.h.
 50801 */
 50803 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 50804   p->pBt->db = p->db;
 50806 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 50807   int i;
 50808   for(i=0; i<db->nDb; i++){
 50809     Btree *p = db->aDb[i].pBt;
 50810     if( p ){
 50811       p->pBt->db = p->db;
 50815 #endif /* if SQLITE_THREADSAFE */
 50816 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
 50818 /************** End of btmutex.c *********************************************/
 50819 /************** Begin file btree.c *******************************************/
 50820 /*
 50821 ** 2004 April 6
 50822 **
 50823 ** The author disclaims copyright to this source code.  In place of
 50824 ** a legal notice, here is a blessing:
 50825 **
 50826 **    May you do good and not evil.
 50827 **    May you find forgiveness for yourself and forgive others.
 50828 **    May you share freely, never taking more than you give.
 50829 **
 50830 *************************************************************************
 50831 ** This file implements a external (disk-based) database using BTrees.
 50832 ** See the header comment on "btreeInt.h" for additional information.
 50833 ** Including a description of file format and an overview of operation.
 50834 */
 50836 /*
 50837 ** The header string that appears at the beginning of every
 50838 ** SQLite database.
 50839 */
 50840 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
 50842 /*
 50843 ** Set this global variable to 1 to enable tracing using the TRACE
 50844 ** macro.
 50845 */
 50846 #if 0
 50847 int sqlite3BtreeTrace=1;  /* True to enable tracing */
 50848 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
 50849 #else
 50850 # define TRACE(X)
 50851 #endif
 50853 /*
 50854 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
 50855 ** But if the value is zero, make it 65536.
 50856 **
 50857 ** This routine is used to extract the "offset to cell content area" value
 50858 ** from the header of a btree page.  If the page size is 65536 and the page
 50859 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
 50860 ** This routine makes the necessary adjustment to 65536.
 50861 */
 50862 #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
 50864 /*
 50865 ** Values passed as the 5th argument to allocateBtreePage()
 50866 */
 50867 #define BTALLOC_ANY   0           /* Allocate any page */
 50868 #define BTALLOC_EXACT 1           /* Allocate exact page if possible */
 50869 #define BTALLOC_LE    2           /* Allocate any page <= the parameter */
 50871 /*
 50872 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not 
 50873 ** defined, or 0 if it is. For example:
 50874 **
 50875 **   bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
 50876 */
 50877 #ifndef SQLITE_OMIT_AUTOVACUUM
 50878 #define IfNotOmitAV(expr) (expr)
 50879 #else
 50880 #define IfNotOmitAV(expr) 0
 50881 #endif
 50883 #ifndef SQLITE_OMIT_SHARED_CACHE
 50884 /*
 50885 ** A list of BtShared objects that are eligible for participation
 50886 ** in shared cache.  This variable has file scope during normal builds,
 50887 ** but the test harness needs to access it so we make it global for 
 50888 ** test builds.
 50889 **
 50890 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
 50891 */
 50892 #ifdef SQLITE_TEST
 50893 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 50894 #else
 50895 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
 50896 #endif
 50897 #endif /* SQLITE_OMIT_SHARED_CACHE */
 50899 #ifndef SQLITE_OMIT_SHARED_CACHE
 50900 /*
 50901 ** Enable or disable the shared pager and schema features.
 50902 **
 50903 ** This routine has no effect on existing database connections.
 50904 ** The shared cache setting effects only future calls to
 50905 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
 50906 */
 50907 SQLITE_API int sqlite3_enable_shared_cache(int enable){
 50908   sqlite3GlobalConfig.sharedCacheEnabled = enable;
 50909   return SQLITE_OK;
 50911 #endif
 50915 #ifdef SQLITE_OMIT_SHARED_CACHE
 50916   /*
 50917   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
 50918   ** and clearAllSharedCacheTableLocks()
 50919   ** manipulate entries in the BtShared.pLock linked list used to store
 50920   ** shared-cache table level locks. If the library is compiled with the
 50921   ** shared-cache feature disabled, then there is only ever one user
 50922   ** of each BtShared structure and so this locking is not necessary. 
 50923   ** So define the lock related functions as no-ops.
 50924   */
 50925   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
 50926   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
 50927   #define clearAllSharedCacheTableLocks(a)
 50928   #define downgradeAllSharedCacheTableLocks(a)
 50929   #define hasSharedCacheTableLock(a,b,c,d) 1
 50930   #define hasReadConflicts(a, b) 0
 50931 #endif
 50933 #ifndef SQLITE_OMIT_SHARED_CACHE
 50935 #ifdef SQLITE_DEBUG
 50936 /*
 50937 **** This function is only used as part of an assert() statement. ***
 50938 **
 50939 ** Check to see if pBtree holds the required locks to read or write to the 
 50940 ** table with root page iRoot.   Return 1 if it does and 0 if not.
 50941 **
 50942 ** For example, when writing to a table with root-page iRoot via 
 50943 ** Btree connection pBtree:
 50944 **
 50945 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
 50946 **
 50947 ** When writing to an index that resides in a sharable database, the 
 50948 ** caller should have first obtained a lock specifying the root page of
 50949 ** the corresponding table. This makes things a bit more complicated,
 50950 ** as this module treats each table as a separate structure. To determine
 50951 ** the table corresponding to the index being written, this
 50952 ** function has to search through the database schema.
 50953 **
 50954 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
 50955 ** hold a write-lock on the schema table (root page 1). This is also
 50956 ** acceptable.
 50957 */
 50958 static int hasSharedCacheTableLock(
 50959   Btree *pBtree,         /* Handle that must hold lock */
 50960   Pgno iRoot,            /* Root page of b-tree */
 50961   int isIndex,           /* True if iRoot is the root of an index b-tree */
 50962   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
 50963 ){
 50964   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
 50965   Pgno iTab = 0;
 50966   BtLock *pLock;
 50968   /* If this database is not shareable, or if the client is reading
 50969   ** and has the read-uncommitted flag set, then no lock is required. 
 50970   ** Return true immediately.
 50971   */
 50972   if( (pBtree->sharable==0)
 50973    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
 50974   ){
 50975     return 1;
 50978   /* If the client is reading  or writing an index and the schema is
 50979   ** not loaded, then it is too difficult to actually check to see if
 50980   ** the correct locks are held.  So do not bother - just return true.
 50981   ** This case does not come up very often anyhow.
 50982   */
 50983   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
 50984     return 1;
 50987   /* Figure out the root-page that the lock should be held on. For table
 50988   ** b-trees, this is just the root page of the b-tree being read or
 50989   ** written. For index b-trees, it is the root page of the associated
 50990   ** table.  */
 50991   if( isIndex ){
 50992     HashElem *p;
 50993     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
 50994       Index *pIdx = (Index *)sqliteHashData(p);
 50995       if( pIdx->tnum==(int)iRoot ){
 50996         iTab = pIdx->pTable->tnum;
 50999   }else{
 51000     iTab = iRoot;
 51003   /* Search for the required lock. Either a write-lock on root-page iTab, a 
 51004   ** write-lock on the schema table, or (if the client is reading) a
 51005   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
 51006   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
 51007     if( pLock->pBtree==pBtree 
 51008      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
 51009      && pLock->eLock>=eLockType 
 51010     ){
 51011       return 1;
 51015   /* Failed to find the required lock. */
 51016   return 0;
 51018 #endif /* SQLITE_DEBUG */
 51020 #ifdef SQLITE_DEBUG
 51021 /*
 51022 **** This function may be used as part of assert() statements only. ****
 51023 **
 51024 ** Return true if it would be illegal for pBtree to write into the
 51025 ** table or index rooted at iRoot because other shared connections are
 51026 ** simultaneously reading that same table or index.
 51027 **
 51028 ** It is illegal for pBtree to write if some other Btree object that
 51029 ** shares the same BtShared object is currently reading or writing
 51030 ** the iRoot table.  Except, if the other Btree object has the
 51031 ** read-uncommitted flag set, then it is OK for the other object to
 51032 ** have a read cursor.
 51033 **
 51034 ** For example, before writing to any part of the table or index
 51035 ** rooted at page iRoot, one should call:
 51036 **
 51037 **    assert( !hasReadConflicts(pBtree, iRoot) );
 51038 */
 51039 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
 51040   BtCursor *p;
 51041   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 51042     if( p->pgnoRoot==iRoot 
 51043      && p->pBtree!=pBtree
 51044      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
 51045     ){
 51046       return 1;
 51049   return 0;
 51051 #endif    /* #ifdef SQLITE_DEBUG */
 51053 /*
 51054 ** Query to see if Btree handle p may obtain a lock of type eLock 
 51055 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
 51056 ** SQLITE_OK if the lock may be obtained (by calling
 51057 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
 51058 */
 51059 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
 51060   BtShared *pBt = p->pBt;
 51061   BtLock *pIter;
 51063   assert( sqlite3BtreeHoldsMutex(p) );
 51064   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 51065   assert( p->db!=0 );
 51066   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
 51068   /* If requesting a write-lock, then the Btree must have an open write
 51069   ** transaction on this file. And, obviously, for this to be so there 
 51070   ** must be an open write transaction on the file itself.
 51071   */
 51072   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
 51073   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
 51075   /* This routine is a no-op if the shared-cache is not enabled */
 51076   if( !p->sharable ){
 51077     return SQLITE_OK;
 51080   /* If some other connection is holding an exclusive lock, the
 51081   ** requested lock may not be obtained.
 51082   */
 51083   if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
 51084     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
 51085     return SQLITE_LOCKED_SHAREDCACHE;
 51088   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 51089     /* The condition (pIter->eLock!=eLock) in the following if(...) 
 51090     ** statement is a simplification of:
 51091     **
 51092     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
 51093     **
 51094     ** since we know that if eLock==WRITE_LOCK, then no other connection
 51095     ** may hold a WRITE_LOCK on any table in this file (since there can
 51096     ** only be a single writer).
 51097     */
 51098     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
 51099     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
 51100     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
 51101       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
 51102       if( eLock==WRITE_LOCK ){
 51103         assert( p==pBt->pWriter );
 51104         pBt->btsFlags |= BTS_PENDING;
 51106       return SQLITE_LOCKED_SHAREDCACHE;
 51109   return SQLITE_OK;
 51111 #endif /* !SQLITE_OMIT_SHARED_CACHE */
 51113 #ifndef SQLITE_OMIT_SHARED_CACHE
 51114 /*
 51115 ** Add a lock on the table with root-page iTable to the shared-btree used
 51116 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
 51117 ** WRITE_LOCK.
 51118 **
 51119 ** This function assumes the following:
 51120 **
 51121 **   (a) The specified Btree object p is connected to a sharable
 51122 **       database (one with the BtShared.sharable flag set), and
 51123 **
 51124 **   (b) No other Btree objects hold a lock that conflicts
 51125 **       with the requested lock (i.e. querySharedCacheTableLock() has
 51126 **       already been called and returned SQLITE_OK).
 51127 **
 51128 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
 51129 ** is returned if a malloc attempt fails.
 51130 */
 51131 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
 51132   BtShared *pBt = p->pBt;
 51133   BtLock *pLock = 0;
 51134   BtLock *pIter;
 51136   assert( sqlite3BtreeHoldsMutex(p) );
 51137   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
 51138   assert( p->db!=0 );
 51140   /* A connection with the read-uncommitted flag set will never try to
 51141   ** obtain a read-lock using this function. The only read-lock obtained
 51142   ** by a connection in read-uncommitted mode is on the sqlite_master 
 51143   ** table, and that lock is obtained in BtreeBeginTrans().  */
 51144   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
 51146   /* This function should only be called on a sharable b-tree after it 
 51147   ** has been determined that no other b-tree holds a conflicting lock.  */
 51148   assert( p->sharable );
 51149   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
 51151   /* First search the list for an existing lock on this table. */
 51152   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 51153     if( pIter->iTable==iTable && pIter->pBtree==p ){
 51154       pLock = pIter;
 51155       break;
 51159   /* If the above search did not find a BtLock struct associating Btree p
 51160   ** with table iTable, allocate one and link it into the list.
 51161   */
 51162   if( !pLock ){
 51163     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
 51164     if( !pLock ){
 51165       return SQLITE_NOMEM;
 51167     pLock->iTable = iTable;
 51168     pLock->pBtree = p;
 51169     pLock->pNext = pBt->pLock;
 51170     pBt->pLock = pLock;
 51173   /* Set the BtLock.eLock variable to the maximum of the current lock
 51174   ** and the requested lock. This means if a write-lock was already held
 51175   ** and a read-lock requested, we don't incorrectly downgrade the lock.
 51176   */
 51177   assert( WRITE_LOCK>READ_LOCK );
 51178   if( eLock>pLock->eLock ){
 51179     pLock->eLock = eLock;
 51182   return SQLITE_OK;
 51184 #endif /* !SQLITE_OMIT_SHARED_CACHE */
 51186 #ifndef SQLITE_OMIT_SHARED_CACHE
 51187 /*
 51188 ** Release all the table locks (locks obtained via calls to
 51189 ** the setSharedCacheTableLock() procedure) held by Btree object p.
 51190 **
 51191 ** This function assumes that Btree p has an open read or write 
 51192 ** transaction. If it does not, then the BTS_PENDING flag
 51193 ** may be incorrectly cleared.
 51194 */
 51195 static void clearAllSharedCacheTableLocks(Btree *p){
 51196   BtShared *pBt = p->pBt;
 51197   BtLock **ppIter = &pBt->pLock;
 51199   assert( sqlite3BtreeHoldsMutex(p) );
 51200   assert( p->sharable || 0==*ppIter );
 51201   assert( p->inTrans>0 );
 51203   while( *ppIter ){
 51204     BtLock *pLock = *ppIter;
 51205     assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
 51206     assert( pLock->pBtree->inTrans>=pLock->eLock );
 51207     if( pLock->pBtree==p ){
 51208       *ppIter = pLock->pNext;
 51209       assert( pLock->iTable!=1 || pLock==&p->lock );
 51210       if( pLock->iTable!=1 ){
 51211         sqlite3_free(pLock);
 51213     }else{
 51214       ppIter = &pLock->pNext;
 51218   assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
 51219   if( pBt->pWriter==p ){
 51220     pBt->pWriter = 0;
 51221     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 51222   }else if( pBt->nTransaction==2 ){
 51223     /* This function is called when Btree p is concluding its 
 51224     ** transaction. If there currently exists a writer, and p is not
 51225     ** that writer, then the number of locks held by connections other
 51226     ** than the writer must be about to drop to zero. In this case
 51227     ** set the BTS_PENDING flag to 0.
 51228     **
 51229     ** If there is not currently a writer, then BTS_PENDING must
 51230     ** be zero already. So this next line is harmless in that case.
 51231     */
 51232     pBt->btsFlags &= ~BTS_PENDING;
 51236 /*
 51237 ** This function changes all write-locks held by Btree p into read-locks.
 51238 */
 51239 static void downgradeAllSharedCacheTableLocks(Btree *p){
 51240   BtShared *pBt = p->pBt;
 51241   if( pBt->pWriter==p ){
 51242     BtLock *pLock;
 51243     pBt->pWriter = 0;
 51244     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 51245     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
 51246       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
 51247       pLock->eLock = READ_LOCK;
 51252 #endif /* SQLITE_OMIT_SHARED_CACHE */
 51254 static void releasePage(MemPage *pPage);  /* Forward reference */
 51256 /*
 51257 ***** This routine is used inside of assert() only ****
 51258 **
 51259 ** Verify that the cursor holds the mutex on its BtShared
 51260 */
 51261 #ifdef SQLITE_DEBUG
 51262 static int cursorHoldsMutex(BtCursor *p){
 51263   return sqlite3_mutex_held(p->pBt->mutex);
 51265 #endif
 51268 #ifndef SQLITE_OMIT_INCRBLOB
 51269 /*
 51270 ** Invalidate the overflow page-list cache for cursor pCur, if any.
 51271 */
 51272 static void invalidateOverflowCache(BtCursor *pCur){
 51273   assert( cursorHoldsMutex(pCur) );
 51274   sqlite3_free(pCur->aOverflow);
 51275   pCur->aOverflow = 0;
 51278 /*
 51279 ** Invalidate the overflow page-list cache for all cursors opened
 51280 ** on the shared btree structure pBt.
 51281 */
 51282 static void invalidateAllOverflowCache(BtShared *pBt){
 51283   BtCursor *p;
 51284   assert( sqlite3_mutex_held(pBt->mutex) );
 51285   for(p=pBt->pCursor; p; p=p->pNext){
 51286     invalidateOverflowCache(p);
 51290 /*
 51291 ** This function is called before modifying the contents of a table
 51292 ** to invalidate any incrblob cursors that are open on the
 51293 ** row or one of the rows being modified.
 51294 **
 51295 ** If argument isClearTable is true, then the entire contents of the
 51296 ** table is about to be deleted. In this case invalidate all incrblob
 51297 ** cursors open on any row within the table with root-page pgnoRoot.
 51298 **
 51299 ** Otherwise, if argument isClearTable is false, then the row with
 51300 ** rowid iRow is being replaced or deleted. In this case invalidate
 51301 ** only those incrblob cursors open on that specific row.
 51302 */
 51303 static void invalidateIncrblobCursors(
 51304   Btree *pBtree,          /* The database file to check */
 51305   i64 iRow,               /* The rowid that might be changing */
 51306   int isClearTable        /* True if all rows are being deleted */
 51307 ){
 51308   BtCursor *p;
 51309   BtShared *pBt = pBtree->pBt;
 51310   assert( sqlite3BtreeHoldsMutex(pBtree) );
 51311   for(p=pBt->pCursor; p; p=p->pNext){
 51312     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
 51313       p->eState = CURSOR_INVALID;
 51318 #else
 51319   /* Stub functions when INCRBLOB is omitted */
 51320   #define invalidateOverflowCache(x)
 51321   #define invalidateAllOverflowCache(x)
 51322   #define invalidateIncrblobCursors(x,y,z)
 51323 #endif /* SQLITE_OMIT_INCRBLOB */
 51325 /*
 51326 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
 51327 ** when a page that previously contained data becomes a free-list leaf 
 51328 ** page.
 51329 **
 51330 ** The BtShared.pHasContent bitvec exists to work around an obscure
 51331 ** bug caused by the interaction of two useful IO optimizations surrounding
 51332 ** free-list leaf pages:
 51333 **
 51334 **   1) When all data is deleted from a page and the page becomes
 51335 **      a free-list leaf page, the page is not written to the database
 51336 **      (as free-list leaf pages contain no meaningful data). Sometimes
 51337 **      such a page is not even journalled (as it will not be modified,
 51338 **      why bother journalling it?).
 51339 **
 51340 **   2) When a free-list leaf page is reused, its content is not read
 51341 **      from the database or written to the journal file (why should it
 51342 **      be, if it is not at all meaningful?).
 51343 **
 51344 ** By themselves, these optimizations work fine and provide a handy
 51345 ** performance boost to bulk delete or insert operations. However, if
 51346 ** a page is moved to the free-list and then reused within the same
 51347 ** transaction, a problem comes up. If the page is not journalled when
 51348 ** it is moved to the free-list and it is also not journalled when it
 51349 ** is extracted from the free-list and reused, then the original data
 51350 ** may be lost. In the event of a rollback, it may not be possible
 51351 ** to restore the database to its original configuration.
 51352 **
 51353 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
 51354 ** moved to become a free-list leaf page, the corresponding bit is
 51355 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
 51356 ** optimization 2 above is omitted if the corresponding bit is already
 51357 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
 51358 ** at the end of every transaction.
 51359 */
 51360 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
 51361   int rc = SQLITE_OK;
 51362   if( !pBt->pHasContent ){
 51363     assert( pgno<=pBt->nPage );
 51364     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
 51365     if( !pBt->pHasContent ){
 51366       rc = SQLITE_NOMEM;
 51369   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
 51370     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
 51372   return rc;
 51375 /*
 51376 ** Query the BtShared.pHasContent vector.
 51377 **
 51378 ** This function is called when a free-list leaf page is removed from the
 51379 ** free-list for reuse. It returns false if it is safe to retrieve the
 51380 ** page from the pager layer with the 'no-content' flag set. True otherwise.
 51381 */
 51382 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
 51383   Bitvec *p = pBt->pHasContent;
 51384   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
 51387 /*
 51388 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
 51389 ** invoked at the conclusion of each write-transaction.
 51390 */
 51391 static void btreeClearHasContent(BtShared *pBt){
 51392   sqlite3BitvecDestroy(pBt->pHasContent);
 51393   pBt->pHasContent = 0;
 51396 /*
 51397 ** Release all of the apPage[] pages for a cursor.
 51398 */
 51399 static void btreeReleaseAllCursorPages(BtCursor *pCur){
 51400   int i;
 51401   for(i=0; i<=pCur->iPage; i++){
 51402     releasePage(pCur->apPage[i]);
 51403     pCur->apPage[i] = 0;
 51405   pCur->iPage = -1;
 51409 /*
 51410 ** Save the current cursor position in the variables BtCursor.nKey 
 51411 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
 51412 **
 51413 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
 51414 ** prior to calling this routine.  
 51415 */
 51416 static int saveCursorPosition(BtCursor *pCur){
 51417   int rc;
 51419   assert( CURSOR_VALID==pCur->eState );
 51420   assert( 0==pCur->pKey );
 51421   assert( cursorHoldsMutex(pCur) );
 51423   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
 51424   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
 51426   /* If this is an intKey table, then the above call to BtreeKeySize()
 51427   ** stores the integer key in pCur->nKey. In this case this value is
 51428   ** all that is required. Otherwise, if pCur is not open on an intKey
 51429   ** table, then malloc space for and store the pCur->nKey bytes of key 
 51430   ** data.
 51431   */
 51432   if( 0==pCur->apPage[0]->intKey ){
 51433     void *pKey = sqlite3Malloc( (int)pCur->nKey );
 51434     if( pKey ){
 51435       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
 51436       if( rc==SQLITE_OK ){
 51437         pCur->pKey = pKey;
 51438       }else{
 51439         sqlite3_free(pKey);
 51441     }else{
 51442       rc = SQLITE_NOMEM;
 51445   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
 51447   if( rc==SQLITE_OK ){
 51448     btreeReleaseAllCursorPages(pCur);
 51449     pCur->eState = CURSOR_REQUIRESEEK;
 51452   invalidateOverflowCache(pCur);
 51453   return rc;
 51456 /*
 51457 ** Save the positions of all cursors (except pExcept) that are open on
 51458 ** the table  with root-page iRoot. Usually, this is called just before cursor
 51459 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
 51460 */
 51461 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
 51462   BtCursor *p;
 51463   assert( sqlite3_mutex_held(pBt->mutex) );
 51464   assert( pExcept==0 || pExcept->pBt==pBt );
 51465   for(p=pBt->pCursor; p; p=p->pNext){
 51466     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
 51467       if( p->eState==CURSOR_VALID ){
 51468         int rc = saveCursorPosition(p);
 51469         if( SQLITE_OK!=rc ){
 51470           return rc;
 51472       }else{
 51473         testcase( p->iPage>0 );
 51474         btreeReleaseAllCursorPages(p);
 51478   return SQLITE_OK;
 51481 /*
 51482 ** Clear the current cursor position.
 51483 */
 51484 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
 51485   assert( cursorHoldsMutex(pCur) );
 51486   sqlite3_free(pCur->pKey);
 51487   pCur->pKey = 0;
 51488   pCur->eState = CURSOR_INVALID;
 51491 /*
 51492 ** In this version of BtreeMoveto, pKey is a packed index record
 51493 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
 51494 ** record and then call BtreeMovetoUnpacked() to do the work.
 51495 */
 51496 static int btreeMoveto(
 51497   BtCursor *pCur,     /* Cursor open on the btree to be searched */
 51498   const void *pKey,   /* Packed key if the btree is an index */
 51499   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
 51500   int bias,           /* Bias search to the high end */
 51501   int *pRes           /* Write search results here */
 51502 ){
 51503   int rc;                    /* Status code */
 51504   UnpackedRecord *pIdxKey;   /* Unpacked index key */
 51505   char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
 51506   char *pFree = 0;
 51508   if( pKey ){
 51509     assert( nKey==(i64)(int)nKey );
 51510     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 51511         pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
 51512     );
 51513     if( pIdxKey==0 ) return SQLITE_NOMEM;
 51514     sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
 51515     if( pIdxKey->nField==0 ){
 51516       sqlite3DbFree(pCur->pKeyInfo->db, pFree);
 51517       return SQLITE_CORRUPT_BKPT;
 51519   }else{
 51520     pIdxKey = 0;
 51522   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
 51523   if( pFree ){
 51524     sqlite3DbFree(pCur->pKeyInfo->db, pFree);
 51526   return rc;
 51529 /*
 51530 ** Restore the cursor to the position it was in (or as close to as possible)
 51531 ** when saveCursorPosition() was called. Note that this call deletes the 
 51532 ** saved position info stored by saveCursorPosition(), so there can be
 51533 ** at most one effective restoreCursorPosition() call after each 
 51534 ** saveCursorPosition().
 51535 */
 51536 static int btreeRestoreCursorPosition(BtCursor *pCur){
 51537   int rc;
 51538   assert( cursorHoldsMutex(pCur) );
 51539   assert( pCur->eState>=CURSOR_REQUIRESEEK );
 51540   if( pCur->eState==CURSOR_FAULT ){
 51541     return pCur->skipNext;
 51543   pCur->eState = CURSOR_INVALID;
 51544   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
 51545   if( rc==SQLITE_OK ){
 51546     sqlite3_free(pCur->pKey);
 51547     pCur->pKey = 0;
 51548     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 51549     if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
 51550       pCur->eState = CURSOR_SKIPNEXT;
 51553   return rc;
 51556 #define restoreCursorPosition(p) \
 51557   (p->eState>=CURSOR_REQUIRESEEK ? \
 51558          btreeRestoreCursorPosition(p) : \
 51559          SQLITE_OK)
 51561 /*
 51562 ** Determine whether or not a cursor has moved from the position it
 51563 ** was last placed at.  Cursors can move when the row they are pointing
 51564 ** at is deleted out from under them.
 51565 **
 51566 ** This routine returns an error code if something goes wrong.  The
 51567 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
 51568 */
 51569 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
 51570   int rc;
 51572   rc = restoreCursorPosition(pCur);
 51573   if( rc ){
 51574     *pHasMoved = 1;
 51575     return rc;
 51577   if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
 51578     *pHasMoved = 1;
 51579   }else{
 51580     *pHasMoved = 0;
 51582   return SQLITE_OK;
 51585 #ifndef SQLITE_OMIT_AUTOVACUUM
 51586 /*
 51587 ** Given a page number of a regular database page, return the page
 51588 ** number for the pointer-map page that contains the entry for the
 51589 ** input page number.
 51590 **
 51591 ** Return 0 (not a valid page) for pgno==1 since there is
 51592 ** no pointer map associated with page 1.  The integrity_check logic
 51593 ** requires that ptrmapPageno(*,1)!=1.
 51594 */
 51595 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
 51596   int nPagesPerMapPage;
 51597   Pgno iPtrMap, ret;
 51598   assert( sqlite3_mutex_held(pBt->mutex) );
 51599   if( pgno<2 ) return 0;
 51600   nPagesPerMapPage = (pBt->usableSize/5)+1;
 51601   iPtrMap = (pgno-2)/nPagesPerMapPage;
 51602   ret = (iPtrMap*nPagesPerMapPage) + 2; 
 51603   if( ret==PENDING_BYTE_PAGE(pBt) ){
 51604     ret++;
 51606   return ret;
 51609 /*
 51610 ** Write an entry into the pointer map.
 51611 **
 51612 ** This routine updates the pointer map entry for page number 'key'
 51613 ** so that it maps to type 'eType' and parent page number 'pgno'.
 51614 **
 51615 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
 51616 ** a no-op.  If an error occurs, the appropriate error code is written
 51617 ** into *pRC.
 51618 */
 51619 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
 51620   DbPage *pDbPage;  /* The pointer map page */
 51621   u8 *pPtrmap;      /* The pointer map data */
 51622   Pgno iPtrmap;     /* The pointer map page number */
 51623   int offset;       /* Offset in pointer map page */
 51624   int rc;           /* Return code from subfunctions */
 51626   if( *pRC ) return;
 51628   assert( sqlite3_mutex_held(pBt->mutex) );
 51629   /* The master-journal page number must never be used as a pointer map page */
 51630   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
 51632   assert( pBt->autoVacuum );
 51633   if( key==0 ){
 51634     *pRC = SQLITE_CORRUPT_BKPT;
 51635     return;
 51637   iPtrmap = PTRMAP_PAGENO(pBt, key);
 51638   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 51639   if( rc!=SQLITE_OK ){
 51640     *pRC = rc;
 51641     return;
 51643   offset = PTRMAP_PTROFFSET(iPtrmap, key);
 51644   if( offset<0 ){
 51645     *pRC = SQLITE_CORRUPT_BKPT;
 51646     goto ptrmap_exit;
 51648   assert( offset <= (int)pBt->usableSize-5 );
 51649   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 51651   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
 51652     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
 51653     *pRC= rc = sqlite3PagerWrite(pDbPage);
 51654     if( rc==SQLITE_OK ){
 51655       pPtrmap[offset] = eType;
 51656       put4byte(&pPtrmap[offset+1], parent);
 51660 ptrmap_exit:
 51661   sqlite3PagerUnref(pDbPage);
 51664 /*
 51665 ** Read an entry from the pointer map.
 51666 **
 51667 ** This routine retrieves the pointer map entry for page 'key', writing
 51668 ** the type and parent page number to *pEType and *pPgno respectively.
 51669 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
 51670 */
 51671 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
 51672   DbPage *pDbPage;   /* The pointer map page */
 51673   int iPtrmap;       /* Pointer map page index */
 51674   u8 *pPtrmap;       /* Pointer map page data */
 51675   int offset;        /* Offset of entry in pointer map */
 51676   int rc;
 51678   assert( sqlite3_mutex_held(pBt->mutex) );
 51680   iPtrmap = PTRMAP_PAGENO(pBt, key);
 51681   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
 51682   if( rc!=0 ){
 51683     return rc;
 51685   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
 51687   offset = PTRMAP_PTROFFSET(iPtrmap, key);
 51688   if( offset<0 ){
 51689     sqlite3PagerUnref(pDbPage);
 51690     return SQLITE_CORRUPT_BKPT;
 51692   assert( offset <= (int)pBt->usableSize-5 );
 51693   assert( pEType!=0 );
 51694   *pEType = pPtrmap[offset];
 51695   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
 51697   sqlite3PagerUnref(pDbPage);
 51698   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
 51699   return SQLITE_OK;
 51702 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
 51703   #define ptrmapPut(w,x,y,z,rc)
 51704   #define ptrmapGet(w,x,y,z) SQLITE_OK
 51705   #define ptrmapPutOvflPtr(x, y, rc)
 51706 #endif
 51708 /*
 51709 ** Given a btree page and a cell index (0 means the first cell on
 51710 ** the page, 1 means the second cell, and so forth) return a pointer
 51711 ** to the cell content.
 51712 **
 51713 ** This routine works only for pages that do not contain overflow cells.
 51714 */
 51715 #define findCell(P,I) \
 51716   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
 51717 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
 51720 /*
 51721 ** This a more complex version of findCell() that works for
 51722 ** pages that do contain overflow cells.
 51723 */
 51724 static u8 *findOverflowCell(MemPage *pPage, int iCell){
 51725   int i;
 51726   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51727   for(i=pPage->nOverflow-1; i>=0; i--){
 51728     int k;
 51729     k = pPage->aiOvfl[i];
 51730     if( k<=iCell ){
 51731       if( k==iCell ){
 51732         return pPage->apOvfl[i];
 51734       iCell--;
 51737   return findCell(pPage, iCell);
 51740 /*
 51741 ** Parse a cell content block and fill in the CellInfo structure.  There
 51742 ** are two versions of this function.  btreeParseCell() takes a 
 51743 ** cell index as the second argument and btreeParseCellPtr() 
 51744 ** takes a pointer to the body of the cell as its second argument.
 51745 **
 51746 ** Within this file, the parseCell() macro can be called instead of
 51747 ** btreeParseCellPtr(). Using some compilers, this will be faster.
 51748 */
 51749 static void btreeParseCellPtr(
 51750   MemPage *pPage,         /* Page containing the cell */
 51751   u8 *pCell,              /* Pointer to the cell text. */
 51752   CellInfo *pInfo         /* Fill in this structure */
 51753 ){
 51754   u16 n;                  /* Number bytes in cell content header */
 51755   u32 nPayload;           /* Number of bytes of cell payload */
 51757   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51759   pInfo->pCell = pCell;
 51760   assert( pPage->leaf==0 || pPage->leaf==1 );
 51761   n = pPage->childPtrSize;
 51762   assert( n==4-4*pPage->leaf );
 51763   if( pPage->intKey ){
 51764     if( pPage->hasData ){
 51765       assert( n==0 );
 51766       n = getVarint32(pCell, nPayload);
 51767     }else{
 51768       nPayload = 0;
 51770     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
 51771     pInfo->nData = nPayload;
 51772   }else{
 51773     pInfo->nData = 0;
 51774     n += getVarint32(&pCell[n], nPayload);
 51775     pInfo->nKey = nPayload;
 51777   pInfo->nPayload = nPayload;
 51778   pInfo->nHeader = n;
 51779   testcase( nPayload==pPage->maxLocal );
 51780   testcase( nPayload==pPage->maxLocal+1 );
 51781   if( likely(nPayload<=pPage->maxLocal) ){
 51782     /* This is the (easy) common case where the entire payload fits
 51783     ** on the local page.  No overflow is required.
 51784     */
 51785     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
 51786     pInfo->nLocal = (u16)nPayload;
 51787     pInfo->iOverflow = 0;
 51788   }else{
 51789     /* If the payload will not fit completely on the local page, we have
 51790     ** to decide how much to store locally and how much to spill onto
 51791     ** overflow pages.  The strategy is to minimize the amount of unused
 51792     ** space on overflow pages while keeping the amount of local storage
 51793     ** in between minLocal and maxLocal.
 51794     **
 51795     ** Warning:  changing the way overflow payload is distributed in any
 51796     ** way will result in an incompatible file format.
 51797     */
 51798     int minLocal;  /* Minimum amount of payload held locally */
 51799     int maxLocal;  /* Maximum amount of payload held locally */
 51800     int surplus;   /* Overflow payload available for local storage */
 51802     minLocal = pPage->minLocal;
 51803     maxLocal = pPage->maxLocal;
 51804     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
 51805     testcase( surplus==maxLocal );
 51806     testcase( surplus==maxLocal+1 );
 51807     if( surplus <= maxLocal ){
 51808       pInfo->nLocal = (u16)surplus;
 51809     }else{
 51810       pInfo->nLocal = (u16)minLocal;
 51812     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
 51813     pInfo->nSize = pInfo->iOverflow + 4;
 51816 #define parseCell(pPage, iCell, pInfo) \
 51817   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
 51818 static void btreeParseCell(
 51819   MemPage *pPage,         /* Page containing the cell */
 51820   int iCell,              /* The cell index.  First cell is 0 */
 51821   CellInfo *pInfo         /* Fill in this structure */
 51822 ){
 51823   parseCell(pPage, iCell, pInfo);
 51826 /*
 51827 ** Compute the total number of bytes that a Cell needs in the cell
 51828 ** data area of the btree-page.  The return number includes the cell
 51829 ** data header and the local payload, but not any overflow page or
 51830 ** the space used by the cell pointer.
 51831 */
 51832 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
 51833   u8 *pIter = &pCell[pPage->childPtrSize];
 51834   u32 nSize;
 51836 #ifdef SQLITE_DEBUG
 51837   /* The value returned by this function should always be the same as
 51838   ** the (CellInfo.nSize) value found by doing a full parse of the
 51839   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
 51840   ** this function verifies that this invariant is not violated. */
 51841   CellInfo debuginfo;
 51842   btreeParseCellPtr(pPage, pCell, &debuginfo);
 51843 #endif
 51845   if( pPage->intKey ){
 51846     u8 *pEnd;
 51847     if( pPage->hasData ){
 51848       pIter += getVarint32(pIter, nSize);
 51849     }else{
 51850       nSize = 0;
 51853     /* pIter now points at the 64-bit integer key value, a variable length 
 51854     ** integer. The following block moves pIter to point at the first byte
 51855     ** past the end of the key value. */
 51856     pEnd = &pIter[9];
 51857     while( (*pIter++)&0x80 && pIter<pEnd );
 51858   }else{
 51859     pIter += getVarint32(pIter, nSize);
 51862   testcase( nSize==pPage->maxLocal );
 51863   testcase( nSize==pPage->maxLocal+1 );
 51864   if( nSize>pPage->maxLocal ){
 51865     int minLocal = pPage->minLocal;
 51866     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
 51867     testcase( nSize==pPage->maxLocal );
 51868     testcase( nSize==pPage->maxLocal+1 );
 51869     if( nSize>pPage->maxLocal ){
 51870       nSize = minLocal;
 51872     nSize += 4;
 51874   nSize += (u32)(pIter - pCell);
 51876   /* The minimum size of any cell is 4 bytes. */
 51877   if( nSize<4 ){
 51878     nSize = 4;
 51881   assert( nSize==debuginfo.nSize );
 51882   return (u16)nSize;
 51885 #ifdef SQLITE_DEBUG
 51886 /* This variation on cellSizePtr() is used inside of assert() statements
 51887 ** only. */
 51888 static u16 cellSize(MemPage *pPage, int iCell){
 51889   return cellSizePtr(pPage, findCell(pPage, iCell));
 51891 #endif
 51893 #ifndef SQLITE_OMIT_AUTOVACUUM
 51894 /*
 51895 ** If the cell pCell, part of page pPage contains a pointer
 51896 ** to an overflow page, insert an entry into the pointer-map
 51897 ** for the overflow page.
 51898 */
 51899 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
 51900   CellInfo info;
 51901   if( *pRC ) return;
 51902   assert( pCell!=0 );
 51903   btreeParseCellPtr(pPage, pCell, &info);
 51904   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
 51905   if( info.iOverflow ){
 51906     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
 51907     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
 51910 #endif
 51913 /*
 51914 ** Defragment the page given.  All Cells are moved to the
 51915 ** end of the page and all free space is collected into one
 51916 ** big FreeBlk that occurs in between the header and cell
 51917 ** pointer array and the cell content area.
 51918 */
 51919 static int defragmentPage(MemPage *pPage){
 51920   int i;                     /* Loop counter */
 51921   int pc;                    /* Address of a i-th cell */
 51922   int hdr;                   /* Offset to the page header */
 51923   int size;                  /* Size of a cell */
 51924   int usableSize;            /* Number of usable bytes on a page */
 51925   int cellOffset;            /* Offset to the cell pointer array */
 51926   int cbrk;                  /* Offset to the cell content area */
 51927   int nCell;                 /* Number of cells on the page */
 51928   unsigned char *data;       /* The page data */
 51929   unsigned char *temp;       /* Temp area for cell content */
 51930   int iCellFirst;            /* First allowable cell index */
 51931   int iCellLast;             /* Last possible cell index */
 51934   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 51935   assert( pPage->pBt!=0 );
 51936   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
 51937   assert( pPage->nOverflow==0 );
 51938   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51939   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
 51940   data = pPage->aData;
 51941   hdr = pPage->hdrOffset;
 51942   cellOffset = pPage->cellOffset;
 51943   nCell = pPage->nCell;
 51944   assert( nCell==get2byte(&data[hdr+3]) );
 51945   usableSize = pPage->pBt->usableSize;
 51946   cbrk = get2byte(&data[hdr+5]);
 51947   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
 51948   cbrk = usableSize;
 51949   iCellFirst = cellOffset + 2*nCell;
 51950   iCellLast = usableSize - 4;
 51951   for(i=0; i<nCell; i++){
 51952     u8 *pAddr;     /* The i-th cell pointer */
 51953     pAddr = &data[cellOffset + i*2];
 51954     pc = get2byte(pAddr);
 51955     testcase( pc==iCellFirst );
 51956     testcase( pc==iCellLast );
 51957 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 51958     /* These conditions have already been verified in btreeInitPage()
 51959     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
 51960     */
 51961     if( pc<iCellFirst || pc>iCellLast ){
 51962       return SQLITE_CORRUPT_BKPT;
 51964 #endif
 51965     assert( pc>=iCellFirst && pc<=iCellLast );
 51966     size = cellSizePtr(pPage, &temp[pc]);
 51967     cbrk -= size;
 51968 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 51969     if( cbrk<iCellFirst ){
 51970       return SQLITE_CORRUPT_BKPT;
 51972 #else
 51973     if( cbrk<iCellFirst || pc+size>usableSize ){
 51974       return SQLITE_CORRUPT_BKPT;
 51976 #endif
 51977     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 51978     testcase( cbrk+size==usableSize );
 51979     testcase( pc+size==usableSize );
 51980     memcpy(&data[cbrk], &temp[pc], size);
 51981     put2byte(pAddr, cbrk);
 51983   assert( cbrk>=iCellFirst );
 51984   put2byte(&data[hdr+5], cbrk);
 51985   data[hdr+1] = 0;
 51986   data[hdr+2] = 0;
 51987   data[hdr+7] = 0;
 51988   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 51989   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 51990   if( cbrk-iCellFirst!=pPage->nFree ){
 51991     return SQLITE_CORRUPT_BKPT;
 51993   return SQLITE_OK;
 51996 /*
 51997 ** Allocate nByte bytes of space from within the B-Tree page passed
 51998 ** as the first argument. Write into *pIdx the index into pPage->aData[]
 51999 ** of the first byte of allocated space. Return either SQLITE_OK or
 52000 ** an error code (usually SQLITE_CORRUPT).
 52001 **
 52002 ** The caller guarantees that there is sufficient space to make the
 52003 ** allocation.  This routine might need to defragment in order to bring
 52004 ** all the space together, however.  This routine will avoid using
 52005 ** the first two bytes past the cell pointer area since presumably this
 52006 ** allocation is being made in order to insert a new cell, so we will
 52007 ** also end up needing a new cell pointer.
 52008 */
 52009 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
 52010   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
 52011   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
 52012   int nFrag;                           /* Number of fragmented bytes on pPage */
 52013   int top;                             /* First byte of cell content area */
 52014   int gap;        /* First byte of gap between cell pointers and cell content */
 52015   int rc;         /* Integer return code */
 52016   int usableSize; /* Usable size of the page */
 52018   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 52019   assert( pPage->pBt );
 52020   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52021   assert( nByte>=0 );  /* Minimum cell size is 4 */
 52022   assert( pPage->nFree>=nByte );
 52023   assert( pPage->nOverflow==0 );
 52024   usableSize = pPage->pBt->usableSize;
 52025   assert( nByte < usableSize-8 );
 52027   nFrag = data[hdr+7];
 52028   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
 52029   gap = pPage->cellOffset + 2*pPage->nCell;
 52030   top = get2byteNotZero(&data[hdr+5]);
 52031   if( gap>top ) return SQLITE_CORRUPT_BKPT;
 52032   testcase( gap+2==top );
 52033   testcase( gap+1==top );
 52034   testcase( gap==top );
 52036   if( nFrag>=60 ){
 52037     /* Always defragment highly fragmented pages */
 52038     rc = defragmentPage(pPage);
 52039     if( rc ) return rc;
 52040     top = get2byteNotZero(&data[hdr+5]);
 52041   }else if( gap+2<=top ){
 52042     /* Search the freelist looking for a free slot big enough to satisfy 
 52043     ** the request. The allocation is made from the first free slot in 
 52044     ** the list that is large enough to accommodate it.
 52045     */
 52046     int pc, addr;
 52047     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
 52048       int size;            /* Size of the free slot */
 52049       if( pc>usableSize-4 || pc<addr+4 ){
 52050         return SQLITE_CORRUPT_BKPT;
 52052       size = get2byte(&data[pc+2]);
 52053       if( size>=nByte ){
 52054         int x = size - nByte;
 52055         testcase( x==4 );
 52056         testcase( x==3 );
 52057         if( x<4 ){
 52058           /* Remove the slot from the free-list. Update the number of
 52059           ** fragmented bytes within the page. */
 52060           memcpy(&data[addr], &data[pc], 2);
 52061           data[hdr+7] = (u8)(nFrag + x);
 52062         }else if( size+pc > usableSize ){
 52063           return SQLITE_CORRUPT_BKPT;
 52064         }else{
 52065           /* The slot remains on the free-list. Reduce its size to account
 52066           ** for the portion used by the new allocation. */
 52067           put2byte(&data[pc+2], x);
 52069         *pIdx = pc + x;
 52070         return SQLITE_OK;
 52075   /* Check to make sure there is enough space in the gap to satisfy
 52076   ** the allocation.  If not, defragment.
 52077   */
 52078   testcase( gap+2+nByte==top );
 52079   if( gap+2+nByte>top ){
 52080     rc = defragmentPage(pPage);
 52081     if( rc ) return rc;
 52082     top = get2byteNotZero(&data[hdr+5]);
 52083     assert( gap+nByte<=top );
 52087   /* Allocate memory from the gap in between the cell pointer array
 52088   ** and the cell content area.  The btreeInitPage() call has already
 52089   ** validated the freelist.  Given that the freelist is valid, there
 52090   ** is no way that the allocation can extend off the end of the page.
 52091   ** The assert() below verifies the previous sentence.
 52092   */
 52093   top -= nByte;
 52094   put2byte(&data[hdr+5], top);
 52095   assert( top+nByte <= (int)pPage->pBt->usableSize );
 52096   *pIdx = top;
 52097   return SQLITE_OK;
 52100 /*
 52101 ** Return a section of the pPage->aData to the freelist.
 52102 ** The first byte of the new free block is pPage->aDisk[start]
 52103 ** and the size of the block is "size" bytes.
 52104 **
 52105 ** Most of the effort here is involved in coalesing adjacent
 52106 ** free blocks into a single big free block.
 52107 */
 52108 static int freeSpace(MemPage *pPage, int start, int size){
 52109   int addr, pbegin, hdr;
 52110   int iLast;                        /* Largest possible freeblock offset */
 52111   unsigned char *data = pPage->aData;
 52113   assert( pPage->pBt!=0 );
 52114   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 52115   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
 52116   assert( (start + size) <= (int)pPage->pBt->usableSize );
 52117   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52118   assert( size>=0 );   /* Minimum cell size is 4 */
 52120   if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
 52121     /* Overwrite deleted information with zeros when the secure_delete
 52122     ** option is enabled */
 52123     memset(&data[start], 0, size);
 52126   /* Add the space back into the linked list of freeblocks.  Note that
 52127   ** even though the freeblock list was checked by btreeInitPage(),
 52128   ** btreeInitPage() did not detect overlapping cells or
 52129   ** freeblocks that overlapped cells.   Nor does it detect when the
 52130   ** cell content area exceeds the value in the page header.  If these
 52131   ** situations arise, then subsequent insert operations might corrupt
 52132   ** the freelist.  So we do need to check for corruption while scanning
 52133   ** the freelist.
 52134   */
 52135   hdr = pPage->hdrOffset;
 52136   addr = hdr + 1;
 52137   iLast = pPage->pBt->usableSize - 4;
 52138   assert( start<=iLast );
 52139   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
 52140     if( pbegin<addr+4 ){
 52141       return SQLITE_CORRUPT_BKPT;
 52143     addr = pbegin;
 52145   if( pbegin>iLast ){
 52146     return SQLITE_CORRUPT_BKPT;
 52148   assert( pbegin>addr || pbegin==0 );
 52149   put2byte(&data[addr], start);
 52150   put2byte(&data[start], pbegin);
 52151   put2byte(&data[start+2], size);
 52152   pPage->nFree = pPage->nFree + (u16)size;
 52154   /* Coalesce adjacent free blocks */
 52155   addr = hdr + 1;
 52156   while( (pbegin = get2byte(&data[addr]))>0 ){
 52157     int pnext, psize, x;
 52158     assert( pbegin>addr );
 52159     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
 52160     pnext = get2byte(&data[pbegin]);
 52161     psize = get2byte(&data[pbegin+2]);
 52162     if( pbegin + psize + 3 >= pnext && pnext>0 ){
 52163       int frag = pnext - (pbegin+psize);
 52164       if( (frag<0) || (frag>(int)data[hdr+7]) ){
 52165         return SQLITE_CORRUPT_BKPT;
 52167       data[hdr+7] -= (u8)frag;
 52168       x = get2byte(&data[pnext]);
 52169       put2byte(&data[pbegin], x);
 52170       x = pnext + get2byte(&data[pnext+2]) - pbegin;
 52171       put2byte(&data[pbegin+2], x);
 52172     }else{
 52173       addr = pbegin;
 52177   /* If the cell content area begins with a freeblock, remove it. */
 52178   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
 52179     int top;
 52180     pbegin = get2byte(&data[hdr+1]);
 52181     memcpy(&data[hdr+1], &data[pbegin], 2);
 52182     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
 52183     put2byte(&data[hdr+5], top);
 52185   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 52186   return SQLITE_OK;
 52189 /*
 52190 ** Decode the flags byte (the first byte of the header) for a page
 52191 ** and initialize fields of the MemPage structure accordingly.
 52192 **
 52193 ** Only the following combinations are supported.  Anything different
 52194 ** indicates a corrupt database files:
 52195 **
 52196 **         PTF_ZERODATA
 52197 **         PTF_ZERODATA | PTF_LEAF
 52198 **         PTF_LEAFDATA | PTF_INTKEY
 52199 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
 52200 */
 52201 static int decodeFlags(MemPage *pPage, int flagByte){
 52202   BtShared *pBt;     /* A copy of pPage->pBt */
 52204   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
 52205   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52206   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
 52207   flagByte &= ~PTF_LEAF;
 52208   pPage->childPtrSize = 4-4*pPage->leaf;
 52209   pBt = pPage->pBt;
 52210   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
 52211     pPage->intKey = 1;
 52212     pPage->hasData = pPage->leaf;
 52213     pPage->maxLocal = pBt->maxLeaf;
 52214     pPage->minLocal = pBt->minLeaf;
 52215   }else if( flagByte==PTF_ZERODATA ){
 52216     pPage->intKey = 0;
 52217     pPage->hasData = 0;
 52218     pPage->maxLocal = pBt->maxLocal;
 52219     pPage->minLocal = pBt->minLocal;
 52220   }else{
 52221     return SQLITE_CORRUPT_BKPT;
 52223   pPage->max1bytePayload = pBt->max1bytePayload;
 52224   return SQLITE_OK;
 52227 /*
 52228 ** Initialize the auxiliary information for a disk block.
 52229 **
 52230 ** Return SQLITE_OK on success.  If we see that the page does
 52231 ** not contain a well-formed database page, then return 
 52232 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
 52233 ** guarantee that the page is well-formed.  It only shows that
 52234 ** we failed to detect any corruption.
 52235 */
 52236 static int btreeInitPage(MemPage *pPage){
 52238   assert( pPage->pBt!=0 );
 52239   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52240   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
 52241   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
 52242   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
 52244   if( !pPage->isInit ){
 52245     u16 pc;            /* Address of a freeblock within pPage->aData[] */
 52246     u8 hdr;            /* Offset to beginning of page header */
 52247     u8 *data;          /* Equal to pPage->aData */
 52248     BtShared *pBt;        /* The main btree structure */
 52249     int usableSize;    /* Amount of usable space on each page */
 52250     u16 cellOffset;    /* Offset from start of page to first cell pointer */
 52251     int nFree;         /* Number of unused bytes on the page */
 52252     int top;           /* First byte of the cell content area */
 52253     int iCellFirst;    /* First allowable cell or freeblock offset */
 52254     int iCellLast;     /* Last possible cell or freeblock offset */
 52256     pBt = pPage->pBt;
 52258     hdr = pPage->hdrOffset;
 52259     data = pPage->aData;
 52260     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
 52261     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 52262     pPage->maskPage = (u16)(pBt->pageSize - 1);
 52263     pPage->nOverflow = 0;
 52264     usableSize = pBt->usableSize;
 52265     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
 52266     pPage->aDataEnd = &data[usableSize];
 52267     pPage->aCellIdx = &data[cellOffset];
 52268     top = get2byteNotZero(&data[hdr+5]);
 52269     pPage->nCell = get2byte(&data[hdr+3]);
 52270     if( pPage->nCell>MX_CELL(pBt) ){
 52271       /* To many cells for a single page.  The page must be corrupt */
 52272       return SQLITE_CORRUPT_BKPT;
 52274     testcase( pPage->nCell==MX_CELL(pBt) );
 52276     /* A malformed database page might cause us to read past the end
 52277     ** of page when parsing a cell.  
 52278     **
 52279     ** The following block of code checks early to see if a cell extends
 52280     ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
 52281     ** returned if it does.
 52282     */
 52283     iCellFirst = cellOffset + 2*pPage->nCell;
 52284     iCellLast = usableSize - 4;
 52285 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
 52287       int i;            /* Index into the cell pointer array */
 52288       int sz;           /* Size of a cell */
 52290       if( !pPage->leaf ) iCellLast--;
 52291       for(i=0; i<pPage->nCell; i++){
 52292         pc = get2byte(&data[cellOffset+i*2]);
 52293         testcase( pc==iCellFirst );
 52294         testcase( pc==iCellLast );
 52295         if( pc<iCellFirst || pc>iCellLast ){
 52296           return SQLITE_CORRUPT_BKPT;
 52298         sz = cellSizePtr(pPage, &data[pc]);
 52299         testcase( pc+sz==usableSize );
 52300         if( pc+sz>usableSize ){
 52301           return SQLITE_CORRUPT_BKPT;
 52304       if( !pPage->leaf ) iCellLast++;
 52306 #endif
 52308     /* Compute the total free space on the page */
 52309     pc = get2byte(&data[hdr+1]);
 52310     nFree = data[hdr+7] + top;
 52311     while( pc>0 ){
 52312       u16 next, size;
 52313       if( pc<iCellFirst || pc>iCellLast ){
 52314         /* Start of free block is off the page */
 52315         return SQLITE_CORRUPT_BKPT; 
 52317       next = get2byte(&data[pc]);
 52318       size = get2byte(&data[pc+2]);
 52319       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
 52320         /* Free blocks must be in ascending order. And the last byte of
 52321         ** the free-block must lie on the database page.  */
 52322         return SQLITE_CORRUPT_BKPT; 
 52324       nFree = nFree + size;
 52325       pc = next;
 52328     /* At this point, nFree contains the sum of the offset to the start
 52329     ** of the cell-content area plus the number of free bytes within
 52330     ** the cell-content area. If this is greater than the usable-size
 52331     ** of the page, then the page must be corrupted. This check also
 52332     ** serves to verify that the offset to the start of the cell-content
 52333     ** area, according to the page header, lies within the page.
 52334     */
 52335     if( nFree>usableSize ){
 52336       return SQLITE_CORRUPT_BKPT; 
 52338     pPage->nFree = (u16)(nFree - iCellFirst);
 52339     pPage->isInit = 1;
 52341   return SQLITE_OK;
 52344 /*
 52345 ** Set up a raw page so that it looks like a database page holding
 52346 ** no entries.
 52347 */
 52348 static void zeroPage(MemPage *pPage, int flags){
 52349   unsigned char *data = pPage->aData;
 52350   BtShared *pBt = pPage->pBt;
 52351   u8 hdr = pPage->hdrOffset;
 52352   u16 first;
 52354   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
 52355   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 52356   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
 52357   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 52358   assert( sqlite3_mutex_held(pBt->mutex) );
 52359   if( pBt->btsFlags & BTS_SECURE_DELETE ){
 52360     memset(&data[hdr], 0, pBt->usableSize - hdr);
 52362   data[hdr] = (char)flags;
 52363   first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
 52364   memset(&data[hdr+1], 0, 4);
 52365   data[hdr+7] = 0;
 52366   put2byte(&data[hdr+5], pBt->usableSize);
 52367   pPage->nFree = (u16)(pBt->usableSize - first);
 52368   decodeFlags(pPage, flags);
 52369   pPage->cellOffset = first;
 52370   pPage->aDataEnd = &data[pBt->usableSize];
 52371   pPage->aCellIdx = &data[first];
 52372   pPage->nOverflow = 0;
 52373   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 52374   pPage->maskPage = (u16)(pBt->pageSize - 1);
 52375   pPage->nCell = 0;
 52376   pPage->isInit = 1;
 52380 /*
 52381 ** Convert a DbPage obtained from the pager into a MemPage used by
 52382 ** the btree layer.
 52383 */
 52384 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
 52385   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
 52386   pPage->aData = sqlite3PagerGetData(pDbPage);
 52387   pPage->pDbPage = pDbPage;
 52388   pPage->pBt = pBt;
 52389   pPage->pgno = pgno;
 52390   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
 52391   return pPage; 
 52394 /*
 52395 ** Get a page from the pager.  Initialize the MemPage.pBt and
 52396 ** MemPage.aData elements if needed.
 52397 **
 52398 ** If the noContent flag is set, it means that we do not care about
 52399 ** the content of the page at this time.  So do not go to the disk
 52400 ** to fetch the content.  Just fill in the content with zeros for now.
 52401 ** If in the future we call sqlite3PagerWrite() on this page, that
 52402 ** means we have started to be concerned about content and the disk
 52403 ** read should occur at that point.
 52404 */
 52405 static int btreeGetPage(
 52406   BtShared *pBt,       /* The btree */
 52407   Pgno pgno,           /* Number of the page to fetch */
 52408   MemPage **ppPage,    /* Return the page in this parameter */
 52409   int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
 52410 ){
 52411   int rc;
 52412   DbPage *pDbPage;
 52414   assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
 52415   assert( sqlite3_mutex_held(pBt->mutex) );
 52416   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
 52417   if( rc ) return rc;
 52418   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
 52419   return SQLITE_OK;
 52422 /*
 52423 ** Retrieve a page from the pager cache. If the requested page is not
 52424 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
 52425 ** MemPage.aData elements if needed.
 52426 */
 52427 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
 52428   DbPage *pDbPage;
 52429   assert( sqlite3_mutex_held(pBt->mutex) );
 52430   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
 52431   if( pDbPage ){
 52432     return btreePageFromDbPage(pDbPage, pgno, pBt);
 52434   return 0;
 52437 /*
 52438 ** Return the size of the database file in pages. If there is any kind of
 52439 ** error, return ((unsigned int)-1).
 52440 */
 52441 static Pgno btreePagecount(BtShared *pBt){
 52442   return pBt->nPage;
 52444 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
 52445   assert( sqlite3BtreeHoldsMutex(p) );
 52446   assert( ((p->pBt->nPage)&0x8000000)==0 );
 52447   return (int)btreePagecount(p->pBt);
 52450 /*
 52451 ** Get a page from the pager and initialize it.  This routine is just a
 52452 ** convenience wrapper around separate calls to btreeGetPage() and 
 52453 ** btreeInitPage().
 52454 **
 52455 ** If an error occurs, then the value *ppPage is set to is undefined. It
 52456 ** may remain unchanged, or it may be set to an invalid value.
 52457 */
 52458 static int getAndInitPage(
 52459   BtShared *pBt,                  /* The database file */
 52460   Pgno pgno,                      /* Number of the page to get */
 52461   MemPage **ppPage,               /* Write the page pointer here */
 52462   int bReadonly                   /* PAGER_GET_READONLY or 0 */
 52463 ){
 52464   int rc;
 52465   assert( sqlite3_mutex_held(pBt->mutex) );
 52466   assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
 52468   if( pgno>btreePagecount(pBt) ){
 52469     rc = SQLITE_CORRUPT_BKPT;
 52470   }else{
 52471     rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
 52472     if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
 52473       rc = btreeInitPage(*ppPage);
 52474       if( rc!=SQLITE_OK ){
 52475         releasePage(*ppPage);
 52480   testcase( pgno==0 );
 52481   assert( pgno!=0 || rc==SQLITE_CORRUPT );
 52482   return rc;
 52485 /*
 52486 ** Release a MemPage.  This should be called once for each prior
 52487 ** call to btreeGetPage.
 52488 */
 52489 static void releasePage(MemPage *pPage){
 52490   if( pPage ){
 52491     assert( pPage->aData );
 52492     assert( pPage->pBt );
 52493     assert( pPage->pDbPage!=0 );
 52494     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
 52495     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
 52496     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52497     sqlite3PagerUnrefNotNull(pPage->pDbPage);
 52501 /*
 52502 ** During a rollback, when the pager reloads information into the cache
 52503 ** so that the cache is restored to its original state at the start of
 52504 ** the transaction, for each page restored this routine is called.
 52505 **
 52506 ** This routine needs to reset the extra data section at the end of the
 52507 ** page to agree with the restored data.
 52508 */
 52509 static void pageReinit(DbPage *pData){
 52510   MemPage *pPage;
 52511   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
 52512   assert( sqlite3PagerPageRefcount(pData)>0 );
 52513   if( pPage->isInit ){
 52514     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 52515     pPage->isInit = 0;
 52516     if( sqlite3PagerPageRefcount(pData)>1 ){
 52517       /* pPage might not be a btree page;  it might be an overflow page
 52518       ** or ptrmap page or a free page.  In those cases, the following
 52519       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
 52520       ** But no harm is done by this.  And it is very important that
 52521       ** btreeInitPage() be called on every btree page so we make
 52522       ** the call for every page that comes in for re-initing. */
 52523       btreeInitPage(pPage);
 52528 /*
 52529 ** Invoke the busy handler for a btree.
 52530 */
 52531 static int btreeInvokeBusyHandler(void *pArg){
 52532   BtShared *pBt = (BtShared*)pArg;
 52533   assert( pBt->db );
 52534   assert( sqlite3_mutex_held(pBt->db->mutex) );
 52535   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
 52538 /*
 52539 ** Open a database file.
 52540 ** 
 52541 ** zFilename is the name of the database file.  If zFilename is NULL
 52542 ** then an ephemeral database is created.  The ephemeral database might
 52543 ** be exclusively in memory, or it might use a disk-based memory cache.
 52544 ** Either way, the ephemeral database will be automatically deleted 
 52545 ** when sqlite3BtreeClose() is called.
 52546 **
 52547 ** If zFilename is ":memory:" then an in-memory database is created
 52548 ** that is automatically destroyed when it is closed.
 52549 **
 52550 ** The "flags" parameter is a bitmask that might contain bits like
 52551 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
 52552 **
 52553 ** If the database is already opened in the same database connection
 52554 ** and we are in shared cache mode, then the open will fail with an
 52555 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
 52556 ** objects in the same database connection since doing so will lead
 52557 ** to problems with locking.
 52558 */
 52559 SQLITE_PRIVATE int sqlite3BtreeOpen(
 52560   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
 52561   const char *zFilename,  /* Name of the file containing the BTree database */
 52562   sqlite3 *db,            /* Associated database handle */
 52563   Btree **ppBtree,        /* Pointer to new Btree object written here */
 52564   int flags,              /* Options */
 52565   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
 52566 ){
 52567   BtShared *pBt = 0;             /* Shared part of btree structure */
 52568   Btree *p;                      /* Handle to return */
 52569   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
 52570   int rc = SQLITE_OK;            /* Result code from this function */
 52571   u8 nReserve;                   /* Byte of unused space on each page */
 52572   unsigned char zDbHeader[100];  /* Database header content */
 52574   /* True if opening an ephemeral, temporary database */
 52575   const int isTempDb = zFilename==0 || zFilename[0]==0;
 52577   /* Set the variable isMemdb to true for an in-memory database, or 
 52578   ** false for a file-based database.
 52579   */
 52580 #ifdef SQLITE_OMIT_MEMORYDB
 52581   const int isMemdb = 0;
 52582 #else
 52583   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
 52584                        || (isTempDb && sqlite3TempInMemory(db))
 52585                        || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
 52586 #endif
 52588   assert( db!=0 );
 52589   assert( pVfs!=0 );
 52590   assert( sqlite3_mutex_held(db->mutex) );
 52591   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
 52593   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
 52594   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
 52596   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
 52597   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
 52599   if( isMemdb ){
 52600     flags |= BTREE_MEMORY;
 52602   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
 52603     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
 52605   p = sqlite3MallocZero(sizeof(Btree));
 52606   if( !p ){
 52607     return SQLITE_NOMEM;
 52609   p->inTrans = TRANS_NONE;
 52610   p->db = db;
 52611 #ifndef SQLITE_OMIT_SHARED_CACHE
 52612   p->lock.pBtree = p;
 52613   p->lock.iTable = 1;
 52614 #endif
 52616 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 52617   /*
 52618   ** If this Btree is a candidate for shared cache, try to find an
 52619   ** existing BtShared object that we can share with
 52620   */
 52621   if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
 52622     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
 52623       int nFullPathname = pVfs->mxPathname+1;
 52624       char *zFullPathname = sqlite3Malloc(nFullPathname);
 52625       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 52626       p->sharable = 1;
 52627       if( !zFullPathname ){
 52628         sqlite3_free(p);
 52629         return SQLITE_NOMEM;
 52631       if( isMemdb ){
 52632         memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
 52633       }else{
 52634         rc = sqlite3OsFullPathname(pVfs, zFilename,
 52635                                    nFullPathname, zFullPathname);
 52636         if( rc ){
 52637           sqlite3_free(zFullPathname);
 52638           sqlite3_free(p);
 52639           return rc;
 52642 #if SQLITE_THREADSAFE
 52643       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
 52644       sqlite3_mutex_enter(mutexOpen);
 52645       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 52646       sqlite3_mutex_enter(mutexShared);
 52647 #endif
 52648       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
 52649         assert( pBt->nRef>0 );
 52650         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
 52651                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
 52652           int iDb;
 52653           for(iDb=db->nDb-1; iDb>=0; iDb--){
 52654             Btree *pExisting = db->aDb[iDb].pBt;
 52655             if( pExisting && pExisting->pBt==pBt ){
 52656               sqlite3_mutex_leave(mutexShared);
 52657               sqlite3_mutex_leave(mutexOpen);
 52658               sqlite3_free(zFullPathname);
 52659               sqlite3_free(p);
 52660               return SQLITE_CONSTRAINT;
 52663           p->pBt = pBt;
 52664           pBt->nRef++;
 52665           break;
 52668       sqlite3_mutex_leave(mutexShared);
 52669       sqlite3_free(zFullPathname);
 52671 #ifdef SQLITE_DEBUG
 52672     else{
 52673       /* In debug mode, we mark all persistent databases as sharable
 52674       ** even when they are not.  This exercises the locking code and
 52675       ** gives more opportunity for asserts(sqlite3_mutex_held())
 52676       ** statements to find locking problems.
 52677       */
 52678       p->sharable = 1;
 52680 #endif
 52682 #endif
 52683   if( pBt==0 ){
 52684     /*
 52685     ** The following asserts make sure that structures used by the btree are
 52686     ** the right size.  This is to guard against size changes that result
 52687     ** when compiling on a different architecture.
 52688     */
 52689     assert( sizeof(i64)==8 || sizeof(i64)==4 );
 52690     assert( sizeof(u64)==8 || sizeof(u64)==4 );
 52691     assert( sizeof(u32)==4 );
 52692     assert( sizeof(u16)==2 );
 52693     assert( sizeof(Pgno)==4 );
 52695     pBt = sqlite3MallocZero( sizeof(*pBt) );
 52696     if( pBt==0 ){
 52697       rc = SQLITE_NOMEM;
 52698       goto btree_open_out;
 52700     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
 52701                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
 52702     if( rc==SQLITE_OK ){
 52703       sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
 52704       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
 52706     if( rc!=SQLITE_OK ){
 52707       goto btree_open_out;
 52709     pBt->openFlags = (u8)flags;
 52710     pBt->db = db;
 52711     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
 52712     p->pBt = pBt;
 52714     pBt->pCursor = 0;
 52715     pBt->pPage1 = 0;
 52716     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
 52717 #ifdef SQLITE_SECURE_DELETE
 52718     pBt->btsFlags |= BTS_SECURE_DELETE;
 52719 #endif
 52720     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
 52721     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
 52722          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
 52723       pBt->pageSize = 0;
 52724 #ifndef SQLITE_OMIT_AUTOVACUUM
 52725       /* If the magic name ":memory:" will create an in-memory database, then
 52726       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
 52727       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
 52728       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
 52729       ** regular file-name. In this case the auto-vacuum applies as per normal.
 52730       */
 52731       if( zFilename && !isMemdb ){
 52732         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
 52733         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
 52735 #endif
 52736       nReserve = 0;
 52737     }else{
 52738       nReserve = zDbHeader[20];
 52739       pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 52740 #ifndef SQLITE_OMIT_AUTOVACUUM
 52741       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
 52742       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
 52743 #endif
 52745     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 52746     if( rc ) goto btree_open_out;
 52747     pBt->usableSize = pBt->pageSize - nReserve;
 52748     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
 52750 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 52751     /* Add the new BtShared object to the linked list sharable BtShareds.
 52752     */
 52753     if( p->sharable ){
 52754       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
 52755       pBt->nRef = 1;
 52756       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
 52757       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
 52758         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
 52759         if( pBt->mutex==0 ){
 52760           rc = SQLITE_NOMEM;
 52761           db->mallocFailed = 0;
 52762           goto btree_open_out;
 52765       sqlite3_mutex_enter(mutexShared);
 52766       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
 52767       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
 52768       sqlite3_mutex_leave(mutexShared);
 52770 #endif
 52773 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
 52774   /* If the new Btree uses a sharable pBtShared, then link the new
 52775   ** Btree into the list of all sharable Btrees for the same connection.
 52776   ** The list is kept in ascending order by pBt address.
 52777   */
 52778   if( p->sharable ){
 52779     int i;
 52780     Btree *pSib;
 52781     for(i=0; i<db->nDb; i++){
 52782       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
 52783         while( pSib->pPrev ){ pSib = pSib->pPrev; }
 52784         if( p->pBt<pSib->pBt ){
 52785           p->pNext = pSib;
 52786           p->pPrev = 0;
 52787           pSib->pPrev = p;
 52788         }else{
 52789           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
 52790             pSib = pSib->pNext;
 52792           p->pNext = pSib->pNext;
 52793           p->pPrev = pSib;
 52794           if( p->pNext ){
 52795             p->pNext->pPrev = p;
 52797           pSib->pNext = p;
 52799         break;
 52803 #endif
 52804   *ppBtree = p;
 52806 btree_open_out:
 52807   if( rc!=SQLITE_OK ){
 52808     if( pBt && pBt->pPager ){
 52809       sqlite3PagerClose(pBt->pPager);
 52811     sqlite3_free(pBt);
 52812     sqlite3_free(p);
 52813     *ppBtree = 0;
 52814   }else{
 52815     /* If the B-Tree was successfully opened, set the pager-cache size to the
 52816     ** default value. Except, when opening on an existing shared pager-cache,
 52817     ** do not change the pager-cache size.
 52818     */
 52819     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
 52820       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
 52823   if( mutexOpen ){
 52824     assert( sqlite3_mutex_held(mutexOpen) );
 52825     sqlite3_mutex_leave(mutexOpen);
 52827   return rc;
 52830 /*
 52831 ** Decrement the BtShared.nRef counter.  When it reaches zero,
 52832 ** remove the BtShared structure from the sharing list.  Return
 52833 ** true if the BtShared.nRef counter reaches zero and return
 52834 ** false if it is still positive.
 52835 */
 52836 static int removeFromSharingList(BtShared *pBt){
 52837 #ifndef SQLITE_OMIT_SHARED_CACHE
 52838   MUTEX_LOGIC( sqlite3_mutex *pMaster; )
 52839   BtShared *pList;
 52840   int removed = 0;
 52842   assert( sqlite3_mutex_notheld(pBt->mutex) );
 52843   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 52844   sqlite3_mutex_enter(pMaster);
 52845   pBt->nRef--;
 52846   if( pBt->nRef<=0 ){
 52847     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
 52848       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
 52849     }else{
 52850       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
 52851       while( ALWAYS(pList) && pList->pNext!=pBt ){
 52852         pList=pList->pNext;
 52854       if( ALWAYS(pList) ){
 52855         pList->pNext = pBt->pNext;
 52858     if( SQLITE_THREADSAFE ){
 52859       sqlite3_mutex_free(pBt->mutex);
 52861     removed = 1;
 52863   sqlite3_mutex_leave(pMaster);
 52864   return removed;
 52865 #else
 52866   return 1;
 52867 #endif
 52870 /*
 52871 ** Make sure pBt->pTmpSpace points to an allocation of 
 52872 ** MX_CELL_SIZE(pBt) bytes.
 52873 */
 52874 static void allocateTempSpace(BtShared *pBt){
 52875   if( !pBt->pTmpSpace ){
 52876     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
 52878     /* One of the uses of pBt->pTmpSpace is to format cells before
 52879     ** inserting them into a leaf page (function fillInCell()). If
 52880     ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
 52881     ** by the various routines that manipulate binary cells. Which
 52882     ** can mean that fillInCell() only initializes the first 2 or 3
 52883     ** bytes of pTmpSpace, but that the first 4 bytes are copied from
 52884     ** it into a database page. This is not actually a problem, but it
 52885     ** does cause a valgrind error when the 1 or 2 bytes of unitialized 
 52886     ** data is passed to system call write(). So to avoid this error,
 52887     ** zero the first 4 bytes of temp space here.  */
 52888     if( pBt->pTmpSpace ) memset(pBt->pTmpSpace, 0, 4);
 52892 /*
 52893 ** Free the pBt->pTmpSpace allocation
 52894 */
 52895 static void freeTempSpace(BtShared *pBt){
 52896   sqlite3PageFree( pBt->pTmpSpace);
 52897   pBt->pTmpSpace = 0;
 52900 /*
 52901 ** Close an open database and invalidate all cursors.
 52902 */
 52903 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
 52904   BtShared *pBt = p->pBt;
 52905   BtCursor *pCur;
 52907   /* Close all cursors opened via this handle.  */
 52908   assert( sqlite3_mutex_held(p->db->mutex) );
 52909   sqlite3BtreeEnter(p);
 52910   pCur = pBt->pCursor;
 52911   while( pCur ){
 52912     BtCursor *pTmp = pCur;
 52913     pCur = pCur->pNext;
 52914     if( pTmp->pBtree==p ){
 52915       sqlite3BtreeCloseCursor(pTmp);
 52919   /* Rollback any active transaction and free the handle structure.
 52920   ** The call to sqlite3BtreeRollback() drops any table-locks held by
 52921   ** this handle.
 52922   */
 52923   sqlite3BtreeRollback(p, SQLITE_OK);
 52924   sqlite3BtreeLeave(p);
 52926   /* If there are still other outstanding references to the shared-btree
 52927   ** structure, return now. The remainder of this procedure cleans 
 52928   ** up the shared-btree.
 52929   */
 52930   assert( p->wantToLock==0 && p->locked==0 );
 52931   if( !p->sharable || removeFromSharingList(pBt) ){
 52932     /* The pBt is no longer on the sharing list, so we can access
 52933     ** it without having to hold the mutex.
 52934     **
 52935     ** Clean out and delete the BtShared object.
 52936     */
 52937     assert( !pBt->pCursor );
 52938     sqlite3PagerClose(pBt->pPager);
 52939     if( pBt->xFreeSchema && pBt->pSchema ){
 52940       pBt->xFreeSchema(pBt->pSchema);
 52942     sqlite3DbFree(0, pBt->pSchema);
 52943     freeTempSpace(pBt);
 52944     sqlite3_free(pBt);
 52947 #ifndef SQLITE_OMIT_SHARED_CACHE
 52948   assert( p->wantToLock==0 );
 52949   assert( p->locked==0 );
 52950   if( p->pPrev ) p->pPrev->pNext = p->pNext;
 52951   if( p->pNext ) p->pNext->pPrev = p->pPrev;
 52952 #endif
 52954   sqlite3_free(p);
 52955   return SQLITE_OK;
 52958 /*
 52959 ** Change the limit on the number of pages allowed in the cache.
 52960 **
 52961 ** The maximum number of cache pages is set to the absolute
 52962 ** value of mxPage.  If mxPage is negative, the pager will
 52963 ** operate asynchronously - it will not stop to do fsync()s
 52964 ** to insure data is written to the disk surface before
 52965 ** continuing.  Transactions still work if synchronous is off,
 52966 ** and the database cannot be corrupted if this program
 52967 ** crashes.  But if the operating system crashes or there is
 52968 ** an abrupt power failure when synchronous is off, the database
 52969 ** could be left in an inconsistent and unrecoverable state.
 52970 ** Synchronous is on by default so database corruption is not
 52971 ** normally a worry.
 52972 */
 52973 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
 52974   BtShared *pBt = p->pBt;
 52975   assert( sqlite3_mutex_held(p->db->mutex) );
 52976   sqlite3BtreeEnter(p);
 52977   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
 52978   sqlite3BtreeLeave(p);
 52979   return SQLITE_OK;
 52982 /*
 52983 ** Change the limit on the amount of the database file that may be
 52984 ** memory mapped.
 52985 */
 52986 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
 52987   BtShared *pBt = p->pBt;
 52988   assert( sqlite3_mutex_held(p->db->mutex) );
 52989   sqlite3BtreeEnter(p);
 52990   sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
 52991   sqlite3BtreeLeave(p);
 52992   return SQLITE_OK;
 52995 /*
 52996 ** Change the way data is synced to disk in order to increase or decrease
 52997 ** how well the database resists damage due to OS crashes and power
 52998 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
 52999 ** there is a high probability of damage)  Level 2 is the default.  There
 53000 ** is a very low but non-zero probability of damage.  Level 3 reduces the
 53001 ** probability of damage to near zero but with a write performance reduction.
 53002 */
 53003 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 53004 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
 53005   Btree *p,              /* The btree to set the safety level on */
 53006   unsigned pgFlags       /* Various PAGER_* flags */
 53007 ){
 53008   BtShared *pBt = p->pBt;
 53009   assert( sqlite3_mutex_held(p->db->mutex) );
 53010   sqlite3BtreeEnter(p);
 53011   sqlite3PagerSetFlags(pBt->pPager, pgFlags);
 53012   sqlite3BtreeLeave(p);
 53013   return SQLITE_OK;
 53015 #endif
 53017 /*
 53018 ** Return TRUE if the given btree is set to safety level 1.  In other
 53019 ** words, return TRUE if no sync() occurs on the disk files.
 53020 */
 53021 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
 53022   BtShared *pBt = p->pBt;
 53023   int rc;
 53024   assert( sqlite3_mutex_held(p->db->mutex) );  
 53025   sqlite3BtreeEnter(p);
 53026   assert( pBt && pBt->pPager );
 53027   rc = sqlite3PagerNosync(pBt->pPager);
 53028   sqlite3BtreeLeave(p);
 53029   return rc;
 53032 /*
 53033 ** Change the default pages size and the number of reserved bytes per page.
 53034 ** Or, if the page size has already been fixed, return SQLITE_READONLY 
 53035 ** without changing anything.
 53036 **
 53037 ** The page size must be a power of 2 between 512 and 65536.  If the page
 53038 ** size supplied does not meet this constraint then the page size is not
 53039 ** changed.
 53040 **
 53041 ** Page sizes are constrained to be a power of two so that the region
 53042 ** of the database file used for locking (beginning at PENDING_BYTE,
 53043 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
 53044 ** at the beginning of a page.
 53045 **
 53046 ** If parameter nReserve is less than zero, then the number of reserved
 53047 ** bytes per page is left unchanged.
 53048 **
 53049 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
 53050 ** and autovacuum mode can no longer be changed.
 53051 */
 53052 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
 53053   int rc = SQLITE_OK;
 53054   BtShared *pBt = p->pBt;
 53055   assert( nReserve>=-1 && nReserve<=255 );
 53056   sqlite3BtreeEnter(p);
 53057   if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
 53058     sqlite3BtreeLeave(p);
 53059     return SQLITE_READONLY;
 53061   if( nReserve<0 ){
 53062     nReserve = pBt->pageSize - pBt->usableSize;
 53064   assert( nReserve>=0 && nReserve<=255 );
 53065   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
 53066         ((pageSize-1)&pageSize)==0 ){
 53067     assert( (pageSize & 7)==0 );
 53068     assert( !pBt->pPage1 && !pBt->pCursor );
 53069     pBt->pageSize = (u32)pageSize;
 53070     freeTempSpace(pBt);
 53072   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
 53073   pBt->usableSize = pBt->pageSize - (u16)nReserve;
 53074   if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 53075   sqlite3BtreeLeave(p);
 53076   return rc;
 53079 /*
 53080 ** Return the currently defined page size
 53081 */
 53082 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
 53083   return p->pBt->pageSize;
 53086 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
 53087 /*
 53088 ** This function is similar to sqlite3BtreeGetReserve(), except that it
 53089 ** may only be called if it is guaranteed that the b-tree mutex is already
 53090 ** held.
 53091 **
 53092 ** This is useful in one special case in the backup API code where it is
 53093 ** known that the shared b-tree mutex is held, but the mutex on the 
 53094 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
 53095 ** were to be called, it might collide with some other operation on the
 53096 ** database handle that owns *p, causing undefined behavior.
 53097 */
 53098 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
 53099   assert( sqlite3_mutex_held(p->pBt->mutex) );
 53100   return p->pBt->pageSize - p->pBt->usableSize;
 53102 #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
 53104 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 53105 /*
 53106 ** Return the number of bytes of space at the end of every page that
 53107 ** are intentually left unused.  This is the "reserved" space that is
 53108 ** sometimes used by extensions.
 53109 */
 53110 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
 53111   int n;
 53112   sqlite3BtreeEnter(p);
 53113   n = p->pBt->pageSize - p->pBt->usableSize;
 53114   sqlite3BtreeLeave(p);
 53115   return n;
 53118 /*
 53119 ** Set the maximum page count for a database if mxPage is positive.
 53120 ** No changes are made if mxPage is 0 or negative.
 53121 ** Regardless of the value of mxPage, return the maximum page count.
 53122 */
 53123 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
 53124   int n;
 53125   sqlite3BtreeEnter(p);
 53126   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
 53127   sqlite3BtreeLeave(p);
 53128   return n;
 53131 /*
 53132 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
 53133 ** then make no changes.  Always return the value of the BTS_SECURE_DELETE
 53134 ** setting after the change.
 53135 */
 53136 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
 53137   int b;
 53138   if( p==0 ) return 0;
 53139   sqlite3BtreeEnter(p);
 53140   if( newFlag>=0 ){
 53141     p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
 53142     if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
 53144   b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
 53145   sqlite3BtreeLeave(p);
 53146   return b;
 53148 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
 53150 /*
 53151 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
 53152 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
 53153 ** is disabled. The default value for the auto-vacuum property is 
 53154 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
 53155 */
 53156 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
 53157 #ifdef SQLITE_OMIT_AUTOVACUUM
 53158   return SQLITE_READONLY;
 53159 #else
 53160   BtShared *pBt = p->pBt;
 53161   int rc = SQLITE_OK;
 53162   u8 av = (u8)autoVacuum;
 53164   sqlite3BtreeEnter(p);
 53165   if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
 53166     rc = SQLITE_READONLY;
 53167   }else{
 53168     pBt->autoVacuum = av ?1:0;
 53169     pBt->incrVacuum = av==2 ?1:0;
 53171   sqlite3BtreeLeave(p);
 53172   return rc;
 53173 #endif
 53176 /*
 53177 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
 53178 ** enabled 1 is returned. Otherwise 0.
 53179 */
 53180 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
 53181 #ifdef SQLITE_OMIT_AUTOVACUUM
 53182   return BTREE_AUTOVACUUM_NONE;
 53183 #else
 53184   int rc;
 53185   sqlite3BtreeEnter(p);
 53186   rc = (
 53187     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
 53188     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
 53189     BTREE_AUTOVACUUM_INCR
 53190   );
 53191   sqlite3BtreeLeave(p);
 53192   return rc;
 53193 #endif
 53197 /*
 53198 ** Get a reference to pPage1 of the database file.  This will
 53199 ** also acquire a readlock on that file.
 53200 **
 53201 ** SQLITE_OK is returned on success.  If the file is not a
 53202 ** well-formed database file, then SQLITE_CORRUPT is returned.
 53203 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
 53204 ** is returned if we run out of memory. 
 53205 */
 53206 static int lockBtree(BtShared *pBt){
 53207   int rc;              /* Result code from subfunctions */
 53208   MemPage *pPage1;     /* Page 1 of the database file */
 53209   int nPage;           /* Number of pages in the database */
 53210   int nPageFile = 0;   /* Number of pages in the database file */
 53211   int nPageHeader;     /* Number of pages in the database according to hdr */
 53213   assert( sqlite3_mutex_held(pBt->mutex) );
 53214   assert( pBt->pPage1==0 );
 53215   rc = sqlite3PagerSharedLock(pBt->pPager);
 53216   if( rc!=SQLITE_OK ) return rc;
 53217   rc = btreeGetPage(pBt, 1, &pPage1, 0);
 53218   if( rc!=SQLITE_OK ) return rc;
 53220   /* Do some checking to help insure the file we opened really is
 53221   ** a valid database file. 
 53222   */
 53223   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
 53224   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 53225   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
 53226     nPage = nPageFile;
 53228   if( nPage>0 ){
 53229     u32 pageSize;
 53230     u32 usableSize;
 53231     u8 *page1 = pPage1->aData;
 53232     rc = SQLITE_NOTADB;
 53233     if( memcmp(page1, zMagicHeader, 16)!=0 ){
 53234       goto page1_init_failed;
 53237 #ifdef SQLITE_OMIT_WAL
 53238     if( page1[18]>1 ){
 53239       pBt->btsFlags |= BTS_READ_ONLY;
 53241     if( page1[19]>1 ){
 53242       goto page1_init_failed;
 53244 #else
 53245     if( page1[18]>2 ){
 53246       pBt->btsFlags |= BTS_READ_ONLY;
 53248     if( page1[19]>2 ){
 53249       goto page1_init_failed;
 53252     /* If the write version is set to 2, this database should be accessed
 53253     ** in WAL mode. If the log is not already open, open it now. Then 
 53254     ** return SQLITE_OK and return without populating BtShared.pPage1.
 53255     ** The caller detects this and calls this function again. This is
 53256     ** required as the version of page 1 currently in the page1 buffer
 53257     ** may not be the latest version - there may be a newer one in the log
 53258     ** file.
 53259     */
 53260     if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
 53261       int isOpen = 0;
 53262       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
 53263       if( rc!=SQLITE_OK ){
 53264         goto page1_init_failed;
 53265       }else if( isOpen==0 ){
 53266         releasePage(pPage1);
 53267         return SQLITE_OK;
 53269       rc = SQLITE_NOTADB;
 53271 #endif
 53273     /* The maximum embedded fraction must be exactly 25%.  And the minimum
 53274     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
 53275     ** The original design allowed these amounts to vary, but as of
 53276     ** version 3.6.0, we require them to be fixed.
 53277     */
 53278     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
 53279       goto page1_init_failed;
 53281     pageSize = (page1[16]<<8) | (page1[17]<<16);
 53282     if( ((pageSize-1)&pageSize)!=0
 53283      || pageSize>SQLITE_MAX_PAGE_SIZE 
 53284      || pageSize<=256 
 53285     ){
 53286       goto page1_init_failed;
 53288     assert( (pageSize & 7)==0 );
 53289     usableSize = pageSize - page1[20];
 53290     if( (u32)pageSize!=pBt->pageSize ){
 53291       /* After reading the first page of the database assuming a page size
 53292       ** of BtShared.pageSize, we have discovered that the page-size is
 53293       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
 53294       ** zero and return SQLITE_OK. The caller will call this function
 53295       ** again with the correct page-size.
 53296       */
 53297       releasePage(pPage1);
 53298       pBt->usableSize = usableSize;
 53299       pBt->pageSize = pageSize;
 53300       freeTempSpace(pBt);
 53301       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 53302                                    pageSize-usableSize);
 53303       return rc;
 53305     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
 53306       rc = SQLITE_CORRUPT_BKPT;
 53307       goto page1_init_failed;
 53309     if( usableSize<480 ){
 53310       goto page1_init_failed;
 53312     pBt->pageSize = pageSize;
 53313     pBt->usableSize = usableSize;
 53314 #ifndef SQLITE_OMIT_AUTOVACUUM
 53315     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
 53316     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
 53317 #endif
 53320   /* maxLocal is the maximum amount of payload to store locally for
 53321   ** a cell.  Make sure it is small enough so that at least minFanout
 53322   ** cells can will fit on one page.  We assume a 10-byte page header.
 53323   ** Besides the payload, the cell must store:
 53324   **     2-byte pointer to the cell
 53325   **     4-byte child pointer
 53326   **     9-byte nKey value
 53327   **     4-byte nData value
 53328   **     4-byte overflow page pointer
 53329   ** So a cell consists of a 2-byte pointer, a header which is as much as
 53330   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
 53331   ** page pointer.
 53332   */
 53333   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
 53334   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
 53335   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
 53336   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
 53337   if( pBt->maxLocal>127 ){
 53338     pBt->max1bytePayload = 127;
 53339   }else{
 53340     pBt->max1bytePayload = (u8)pBt->maxLocal;
 53342   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
 53343   pBt->pPage1 = pPage1;
 53344   pBt->nPage = nPage;
 53345   return SQLITE_OK;
 53347 page1_init_failed:
 53348   releasePage(pPage1);
 53349   pBt->pPage1 = 0;
 53350   return rc;
 53353 #ifndef NDEBUG
 53354 /*
 53355 ** Return the number of cursors open on pBt. This is for use
 53356 ** in assert() expressions, so it is only compiled if NDEBUG is not
 53357 ** defined.
 53358 **
 53359 ** Only write cursors are counted if wrOnly is true.  If wrOnly is
 53360 ** false then all cursors are counted.
 53361 **
 53362 ** For the purposes of this routine, a cursor is any cursor that
 53363 ** is capable of reading or writing to the databse.  Cursors that
 53364 ** have been tripped into the CURSOR_FAULT state are not counted.
 53365 */
 53366 static int countValidCursors(BtShared *pBt, int wrOnly){
 53367   BtCursor *pCur;
 53368   int r = 0;
 53369   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
 53370     if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++; 
 53372   return r;
 53374 #endif
 53376 /*
 53377 ** If there are no outstanding cursors and we are not in the middle
 53378 ** of a transaction but there is a read lock on the database, then
 53379 ** this routine unrefs the first page of the database file which 
 53380 ** has the effect of releasing the read lock.
 53381 **
 53382 ** If there is a transaction in progress, this routine is a no-op.
 53383 */
 53384 static void unlockBtreeIfUnused(BtShared *pBt){
 53385   assert( sqlite3_mutex_held(pBt->mutex) );
 53386   assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
 53387   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
 53388     assert( pBt->pPage1->aData );
 53389     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
 53390     assert( pBt->pPage1->aData );
 53391     releasePage(pBt->pPage1);
 53392     pBt->pPage1 = 0;
 53396 /*
 53397 ** If pBt points to an empty file then convert that empty file
 53398 ** into a new empty database by initializing the first page of
 53399 ** the database.
 53400 */
 53401 static int newDatabase(BtShared *pBt){
 53402   MemPage *pP1;
 53403   unsigned char *data;
 53404   int rc;
 53406   assert( sqlite3_mutex_held(pBt->mutex) );
 53407   if( pBt->nPage>0 ){
 53408     return SQLITE_OK;
 53410   pP1 = pBt->pPage1;
 53411   assert( pP1!=0 );
 53412   data = pP1->aData;
 53413   rc = sqlite3PagerWrite(pP1->pDbPage);
 53414   if( rc ) return rc;
 53415   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
 53416   assert( sizeof(zMagicHeader)==16 );
 53417   data[16] = (u8)((pBt->pageSize>>8)&0xff);
 53418   data[17] = (u8)((pBt->pageSize>>16)&0xff);
 53419   data[18] = 1;
 53420   data[19] = 1;
 53421   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
 53422   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
 53423   data[21] = 64;
 53424   data[22] = 32;
 53425   data[23] = 32;
 53426   memset(&data[24], 0, 100-24);
 53427   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
 53428   pBt->btsFlags |= BTS_PAGESIZE_FIXED;
 53429 #ifndef SQLITE_OMIT_AUTOVACUUM
 53430   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
 53431   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
 53432   put4byte(&data[36 + 4*4], pBt->autoVacuum);
 53433   put4byte(&data[36 + 7*4], pBt->incrVacuum);
 53434 #endif
 53435   pBt->nPage = 1;
 53436   data[31] = 1;
 53437   return SQLITE_OK;
 53440 /*
 53441 ** Initialize the first page of the database file (creating a database
 53442 ** consisting of a single page and no schema objects). Return SQLITE_OK
 53443 ** if successful, or an SQLite error code otherwise.
 53444 */
 53445 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
 53446   int rc;
 53447   sqlite3BtreeEnter(p);
 53448   p->pBt->nPage = 0;
 53449   rc = newDatabase(p->pBt);
 53450   sqlite3BtreeLeave(p);
 53451   return rc;
 53454 /*
 53455 ** Attempt to start a new transaction. A write-transaction
 53456 ** is started if the second argument is nonzero, otherwise a read-
 53457 ** transaction.  If the second argument is 2 or more and exclusive
 53458 ** transaction is started, meaning that no other process is allowed
 53459 ** to access the database.  A preexisting transaction may not be
 53460 ** upgraded to exclusive by calling this routine a second time - the
 53461 ** exclusivity flag only works for a new transaction.
 53462 **
 53463 ** A write-transaction must be started before attempting any 
 53464 ** changes to the database.  None of the following routines 
 53465 ** will work unless a transaction is started first:
 53466 **
 53467 **      sqlite3BtreeCreateTable()
 53468 **      sqlite3BtreeCreateIndex()
 53469 **      sqlite3BtreeClearTable()
 53470 **      sqlite3BtreeDropTable()
 53471 **      sqlite3BtreeInsert()
 53472 **      sqlite3BtreeDelete()
 53473 **      sqlite3BtreeUpdateMeta()
 53474 **
 53475 ** If an initial attempt to acquire the lock fails because of lock contention
 53476 ** and the database was previously unlocked, then invoke the busy handler
 53477 ** if there is one.  But if there was previously a read-lock, do not
 53478 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
 53479 ** returned when there is already a read-lock in order to avoid a deadlock.
 53480 **
 53481 ** Suppose there are two processes A and B.  A has a read lock and B has
 53482 ** a reserved lock.  B tries to promote to exclusive but is blocked because
 53483 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
 53484 ** One or the other of the two processes must give way or there can be
 53485 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
 53486 ** when A already has a read lock, we encourage A to give up and let B
 53487 ** proceed.
 53488 */
 53489 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
 53490   sqlite3 *pBlock = 0;
 53491   BtShared *pBt = p->pBt;
 53492   int rc = SQLITE_OK;
 53494   sqlite3BtreeEnter(p);
 53495   btreeIntegrity(p);
 53497   /* If the btree is already in a write-transaction, or it
 53498   ** is already in a read-transaction and a read-transaction
 53499   ** is requested, this is a no-op.
 53500   */
 53501   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
 53502     goto trans_begun;
 53504   assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
 53506   /* Write transactions are not possible on a read-only database */
 53507   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
 53508     rc = SQLITE_READONLY;
 53509     goto trans_begun;
 53512 #ifndef SQLITE_OMIT_SHARED_CACHE
 53513   /* If another database handle has already opened a write transaction 
 53514   ** on this shared-btree structure and a second write transaction is
 53515   ** requested, return SQLITE_LOCKED.
 53516   */
 53517   if( (wrflag && pBt->inTransaction==TRANS_WRITE)
 53518    || (pBt->btsFlags & BTS_PENDING)!=0
 53519   ){
 53520     pBlock = pBt->pWriter->db;
 53521   }else if( wrflag>1 ){
 53522     BtLock *pIter;
 53523     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
 53524       if( pIter->pBtree!=p ){
 53525         pBlock = pIter->pBtree->db;
 53526         break;
 53530   if( pBlock ){
 53531     sqlite3ConnectionBlocked(p->db, pBlock);
 53532     rc = SQLITE_LOCKED_SHAREDCACHE;
 53533     goto trans_begun;
 53535 #endif
 53537   /* Any read-only or read-write transaction implies a read-lock on 
 53538   ** page 1. So if some other shared-cache client already has a write-lock 
 53539   ** on page 1, the transaction cannot be opened. */
 53540   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 53541   if( SQLITE_OK!=rc ) goto trans_begun;
 53543   pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
 53544   if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
 53545   do {
 53546     /* Call lockBtree() until either pBt->pPage1 is populated or
 53547     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
 53548     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
 53549     ** reading page 1 it discovers that the page-size of the database 
 53550     ** file is not pBt->pageSize. In this case lockBtree() will update
 53551     ** pBt->pageSize to the page-size of the file on disk.
 53552     */
 53553     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
 53555     if( rc==SQLITE_OK && wrflag ){
 53556       if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
 53557         rc = SQLITE_READONLY;
 53558       }else{
 53559         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
 53560         if( rc==SQLITE_OK ){
 53561           rc = newDatabase(pBt);
 53566     if( rc!=SQLITE_OK ){
 53567       unlockBtreeIfUnused(pBt);
 53569   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
 53570           btreeInvokeBusyHandler(pBt) );
 53572   if( rc==SQLITE_OK ){
 53573     if( p->inTrans==TRANS_NONE ){
 53574       pBt->nTransaction++;
 53575 #ifndef SQLITE_OMIT_SHARED_CACHE
 53576       if( p->sharable ){
 53577         assert( p->lock.pBtree==p && p->lock.iTable==1 );
 53578         p->lock.eLock = READ_LOCK;
 53579         p->lock.pNext = pBt->pLock;
 53580         pBt->pLock = &p->lock;
 53582 #endif
 53584     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
 53585     if( p->inTrans>pBt->inTransaction ){
 53586       pBt->inTransaction = p->inTrans;
 53588     if( wrflag ){
 53589       MemPage *pPage1 = pBt->pPage1;
 53590 #ifndef SQLITE_OMIT_SHARED_CACHE
 53591       assert( !pBt->pWriter );
 53592       pBt->pWriter = p;
 53593       pBt->btsFlags &= ~BTS_EXCLUSIVE;
 53594       if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
 53595 #endif
 53597       /* If the db-size header field is incorrect (as it may be if an old
 53598       ** client has been writing the database file), update it now. Doing
 53599       ** this sooner rather than later means the database size can safely 
 53600       ** re-read the database size from page 1 if a savepoint or transaction
 53601       ** rollback occurs within the transaction.
 53602       */
 53603       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
 53604         rc = sqlite3PagerWrite(pPage1->pDbPage);
 53605         if( rc==SQLITE_OK ){
 53606           put4byte(&pPage1->aData[28], pBt->nPage);
 53613 trans_begun:
 53614   if( rc==SQLITE_OK && wrflag ){
 53615     /* This call makes sure that the pager has the correct number of
 53616     ** open savepoints. If the second parameter is greater than 0 and
 53617     ** the sub-journal is not already open, then it will be opened here.
 53618     */
 53619     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
 53622   btreeIntegrity(p);
 53623   sqlite3BtreeLeave(p);
 53624   return rc;
 53627 #ifndef SQLITE_OMIT_AUTOVACUUM
 53629 /*
 53630 ** Set the pointer-map entries for all children of page pPage. Also, if
 53631 ** pPage contains cells that point to overflow pages, set the pointer
 53632 ** map entries for the overflow pages as well.
 53633 */
 53634 static int setChildPtrmaps(MemPage *pPage){
 53635   int i;                             /* Counter variable */
 53636   int nCell;                         /* Number of cells in page pPage */
 53637   int rc;                            /* Return code */
 53638   BtShared *pBt = pPage->pBt;
 53639   u8 isInitOrig = pPage->isInit;
 53640   Pgno pgno = pPage->pgno;
 53642   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53643   rc = btreeInitPage(pPage);
 53644   if( rc!=SQLITE_OK ){
 53645     goto set_child_ptrmaps_out;
 53647   nCell = pPage->nCell;
 53649   for(i=0; i<nCell; i++){
 53650     u8 *pCell = findCell(pPage, i);
 53652     ptrmapPutOvflPtr(pPage, pCell, &rc);
 53654     if( !pPage->leaf ){
 53655       Pgno childPgno = get4byte(pCell);
 53656       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 53660   if( !pPage->leaf ){
 53661     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 53662     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
 53665 set_child_ptrmaps_out:
 53666   pPage->isInit = isInitOrig;
 53667   return rc;
 53670 /*
 53671 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
 53672 ** that it points to iTo. Parameter eType describes the type of pointer to
 53673 ** be modified, as  follows:
 53674 **
 53675 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
 53676 **                   page of pPage.
 53677 **
 53678 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
 53679 **                   page pointed to by one of the cells on pPage.
 53680 **
 53681 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
 53682 **                   overflow page in the list.
 53683 */
 53684 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
 53685   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 53686   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 53687   if( eType==PTRMAP_OVERFLOW2 ){
 53688     /* The pointer is always the first 4 bytes of the page in this case.  */
 53689     if( get4byte(pPage->aData)!=iFrom ){
 53690       return SQLITE_CORRUPT_BKPT;
 53692     put4byte(pPage->aData, iTo);
 53693   }else{
 53694     u8 isInitOrig = pPage->isInit;
 53695     int i;
 53696     int nCell;
 53698     btreeInitPage(pPage);
 53699     nCell = pPage->nCell;
 53701     for(i=0; i<nCell; i++){
 53702       u8 *pCell = findCell(pPage, i);
 53703       if( eType==PTRMAP_OVERFLOW1 ){
 53704         CellInfo info;
 53705         btreeParseCellPtr(pPage, pCell, &info);
 53706         if( info.iOverflow
 53707          && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
 53708          && iFrom==get4byte(&pCell[info.iOverflow])
 53709         ){
 53710           put4byte(&pCell[info.iOverflow], iTo);
 53711           break;
 53713       }else{
 53714         if( get4byte(pCell)==iFrom ){
 53715           put4byte(pCell, iTo);
 53716           break;
 53721     if( i==nCell ){
 53722       if( eType!=PTRMAP_BTREE || 
 53723           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
 53724         return SQLITE_CORRUPT_BKPT;
 53726       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
 53729     pPage->isInit = isInitOrig;
 53731   return SQLITE_OK;
 53735 /*
 53736 ** Move the open database page pDbPage to location iFreePage in the 
 53737 ** database. The pDbPage reference remains valid.
 53738 **
 53739 ** The isCommit flag indicates that there is no need to remember that
 53740 ** the journal needs to be sync()ed before database page pDbPage->pgno 
 53741 ** can be written to. The caller has already promised not to write to that
 53742 ** page.
 53743 */
 53744 static int relocatePage(
 53745   BtShared *pBt,           /* Btree */
 53746   MemPage *pDbPage,        /* Open page to move */
 53747   u8 eType,                /* Pointer map 'type' entry for pDbPage */
 53748   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
 53749   Pgno iFreePage,          /* The location to move pDbPage to */
 53750   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
 53751 ){
 53752   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
 53753   Pgno iDbPage = pDbPage->pgno;
 53754   Pager *pPager = pBt->pPager;
 53755   int rc;
 53757   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
 53758       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
 53759   assert( sqlite3_mutex_held(pBt->mutex) );
 53760   assert( pDbPage->pBt==pBt );
 53762   /* Move page iDbPage from its current location to page number iFreePage */
 53763   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
 53764       iDbPage, iFreePage, iPtrPage, eType));
 53765   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
 53766   if( rc!=SQLITE_OK ){
 53767     return rc;
 53769   pDbPage->pgno = iFreePage;
 53771   /* If pDbPage was a btree-page, then it may have child pages and/or cells
 53772   ** that point to overflow pages. The pointer map entries for all these
 53773   ** pages need to be changed.
 53774   **
 53775   ** If pDbPage is an overflow page, then the first 4 bytes may store a
 53776   ** pointer to a subsequent overflow page. If this is the case, then
 53777   ** the pointer map needs to be updated for the subsequent overflow page.
 53778   */
 53779   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
 53780     rc = setChildPtrmaps(pDbPage);
 53781     if( rc!=SQLITE_OK ){
 53782       return rc;
 53784   }else{
 53785     Pgno nextOvfl = get4byte(pDbPage->aData);
 53786     if( nextOvfl!=0 ){
 53787       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
 53788       if( rc!=SQLITE_OK ){
 53789         return rc;
 53794   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
 53795   ** that it points at iFreePage. Also fix the pointer map entry for
 53796   ** iPtrPage.
 53797   */
 53798   if( eType!=PTRMAP_ROOTPAGE ){
 53799     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
 53800     if( rc!=SQLITE_OK ){
 53801       return rc;
 53803     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
 53804     if( rc!=SQLITE_OK ){
 53805       releasePage(pPtrPage);
 53806       return rc;
 53808     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
 53809     releasePage(pPtrPage);
 53810     if( rc==SQLITE_OK ){
 53811       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
 53814   return rc;
 53817 /* Forward declaration required by incrVacuumStep(). */
 53818 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
 53820 /*
 53821 ** Perform a single step of an incremental-vacuum. If successful, return
 53822 ** SQLITE_OK. If there is no work to do (and therefore no point in 
 53823 ** calling this function again), return SQLITE_DONE. Or, if an error 
 53824 ** occurs, return some other error code.
 53825 **
 53826 ** More specificly, this function attempts to re-organize the database so 
 53827 ** that the last page of the file currently in use is no longer in use.
 53828 **
 53829 ** Parameter nFin is the number of pages that this database would contain
 53830 ** were this function called until it returns SQLITE_DONE.
 53831 **
 53832 ** If the bCommit parameter is non-zero, this function assumes that the 
 53833 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE 
 53834 ** or an error. bCommit is passed true for an auto-vacuum-on-commmit 
 53835 ** operation, or false for an incremental vacuum.
 53836 */
 53837 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
 53838   Pgno nFreeList;           /* Number of pages still on the free-list */
 53839   int rc;
 53841   assert( sqlite3_mutex_held(pBt->mutex) );
 53842   assert( iLastPg>nFin );
 53844   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
 53845     u8 eType;
 53846     Pgno iPtrPage;
 53848     nFreeList = get4byte(&pBt->pPage1->aData[36]);
 53849     if( nFreeList==0 ){
 53850       return SQLITE_DONE;
 53853     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
 53854     if( rc!=SQLITE_OK ){
 53855       return rc;
 53857     if( eType==PTRMAP_ROOTPAGE ){
 53858       return SQLITE_CORRUPT_BKPT;
 53861     if( eType==PTRMAP_FREEPAGE ){
 53862       if( bCommit==0 ){
 53863         /* Remove the page from the files free-list. This is not required
 53864         ** if bCommit is non-zero. In that case, the free-list will be
 53865         ** truncated to zero after this function returns, so it doesn't 
 53866         ** matter if it still contains some garbage entries.
 53867         */
 53868         Pgno iFreePg;
 53869         MemPage *pFreePg;
 53870         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
 53871         if( rc!=SQLITE_OK ){
 53872           return rc;
 53874         assert( iFreePg==iLastPg );
 53875         releasePage(pFreePg);
 53877     } else {
 53878       Pgno iFreePg;             /* Index of free page to move pLastPg to */
 53879       MemPage *pLastPg;
 53880       u8 eMode = BTALLOC_ANY;   /* Mode parameter for allocateBtreePage() */
 53881       Pgno iNear = 0;           /* nearby parameter for allocateBtreePage() */
 53883       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
 53884       if( rc!=SQLITE_OK ){
 53885         return rc;
 53888       /* If bCommit is zero, this loop runs exactly once and page pLastPg
 53889       ** is swapped with the first free page pulled off the free list.
 53890       **
 53891       ** On the other hand, if bCommit is greater than zero, then keep
 53892       ** looping until a free-page located within the first nFin pages
 53893       ** of the file is found.
 53894       */
 53895       if( bCommit==0 ){
 53896         eMode = BTALLOC_LE;
 53897         iNear = nFin;
 53899       do {
 53900         MemPage *pFreePg;
 53901         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
 53902         if( rc!=SQLITE_OK ){
 53903           releasePage(pLastPg);
 53904           return rc;
 53906         releasePage(pFreePg);
 53907       }while( bCommit && iFreePg>nFin );
 53908       assert( iFreePg<iLastPg );
 53910       rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
 53911       releasePage(pLastPg);
 53912       if( rc!=SQLITE_OK ){
 53913         return rc;
 53918   if( bCommit==0 ){
 53919     do {
 53920       iLastPg--;
 53921     }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
 53922     pBt->bDoTruncate = 1;
 53923     pBt->nPage = iLastPg;
 53925   return SQLITE_OK;
 53928 /*
 53929 ** The database opened by the first argument is an auto-vacuum database
 53930 ** nOrig pages in size containing nFree free pages. Return the expected 
 53931 ** size of the database in pages following an auto-vacuum operation.
 53932 */
 53933 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
 53934   int nEntry;                     /* Number of entries on one ptrmap page */
 53935   Pgno nPtrmap;                   /* Number of PtrMap pages to be freed */
 53936   Pgno nFin;                      /* Return value */
 53938   nEntry = pBt->usableSize/5;
 53939   nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
 53940   nFin = nOrig - nFree - nPtrmap;
 53941   if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
 53942     nFin--;
 53944   while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
 53945     nFin--;
 53948   return nFin;
 53951 /*
 53952 ** A write-transaction must be opened before calling this function.
 53953 ** It performs a single unit of work towards an incremental vacuum.
 53954 **
 53955 ** If the incremental vacuum is finished after this function has run,
 53956 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
 53957 ** SQLITE_OK is returned. Otherwise an SQLite error code. 
 53958 */
 53959 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
 53960   int rc;
 53961   BtShared *pBt = p->pBt;
 53963   sqlite3BtreeEnter(p);
 53964   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
 53965   if( !pBt->autoVacuum ){
 53966     rc = SQLITE_DONE;
 53967   }else{
 53968     Pgno nOrig = btreePagecount(pBt);
 53969     Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
 53970     Pgno nFin = finalDbSize(pBt, nOrig, nFree);
 53972     if( nOrig<nFin ){
 53973       rc = SQLITE_CORRUPT_BKPT;
 53974     }else if( nFree>0 ){
 53975       rc = saveAllCursors(pBt, 0, 0);
 53976       if( rc==SQLITE_OK ){
 53977         invalidateAllOverflowCache(pBt);
 53978         rc = incrVacuumStep(pBt, nFin, nOrig, 0);
 53980       if( rc==SQLITE_OK ){
 53981         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 53982         put4byte(&pBt->pPage1->aData[28], pBt->nPage);
 53984     }else{
 53985       rc = SQLITE_DONE;
 53988   sqlite3BtreeLeave(p);
 53989   return rc;
 53992 /*
 53993 ** This routine is called prior to sqlite3PagerCommit when a transaction
 53994 ** is committed for an auto-vacuum database.
 53995 **
 53996 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
 53997 ** the database file should be truncated to during the commit process. 
 53998 ** i.e. the database has been reorganized so that only the first *pnTrunc
 53999 ** pages are in use.
 54000 */
 54001 static int autoVacuumCommit(BtShared *pBt){
 54002   int rc = SQLITE_OK;
 54003   Pager *pPager = pBt->pPager;
 54004   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
 54006   assert( sqlite3_mutex_held(pBt->mutex) );
 54007   invalidateAllOverflowCache(pBt);
 54008   assert(pBt->autoVacuum);
 54009   if( !pBt->incrVacuum ){
 54010     Pgno nFin;         /* Number of pages in database after autovacuuming */
 54011     Pgno nFree;        /* Number of pages on the freelist initially */
 54012     Pgno iFree;        /* The next page to be freed */
 54013     Pgno nOrig;        /* Database size before freeing */
 54015     nOrig = btreePagecount(pBt);
 54016     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
 54017       /* It is not possible to create a database for which the final page
 54018       ** is either a pointer-map page or the pending-byte page. If one
 54019       ** is encountered, this indicates corruption.
 54020       */
 54021       return SQLITE_CORRUPT_BKPT;
 54024     nFree = get4byte(&pBt->pPage1->aData[36]);
 54025     nFin = finalDbSize(pBt, nOrig, nFree);
 54026     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
 54027     if( nFin<nOrig ){
 54028       rc = saveAllCursors(pBt, 0, 0);
 54030     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
 54031       rc = incrVacuumStep(pBt, nFin, iFree, 1);
 54033     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
 54034       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 54035       put4byte(&pBt->pPage1->aData[32], 0);
 54036       put4byte(&pBt->pPage1->aData[36], 0);
 54037       put4byte(&pBt->pPage1->aData[28], nFin);
 54038       pBt->bDoTruncate = 1;
 54039       pBt->nPage = nFin;
 54041     if( rc!=SQLITE_OK ){
 54042       sqlite3PagerRollback(pPager);
 54046   assert( nRef>=sqlite3PagerRefcount(pPager) );
 54047   return rc;
 54050 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
 54051 # define setChildPtrmaps(x) SQLITE_OK
 54052 #endif
 54054 /*
 54055 ** This routine does the first phase of a two-phase commit.  This routine
 54056 ** causes a rollback journal to be created (if it does not already exist)
 54057 ** and populated with enough information so that if a power loss occurs
 54058 ** the database can be restored to its original state by playing back
 54059 ** the journal.  Then the contents of the journal are flushed out to
 54060 ** the disk.  After the journal is safely on oxide, the changes to the
 54061 ** database are written into the database file and flushed to oxide.
 54062 ** At the end of this call, the rollback journal still exists on the
 54063 ** disk and we are still holding all locks, so the transaction has not
 54064 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
 54065 ** commit process.
 54066 **
 54067 ** This call is a no-op if no write-transaction is currently active on pBt.
 54068 **
 54069 ** Otherwise, sync the database file for the btree pBt. zMaster points to
 54070 ** the name of a master journal file that should be written into the
 54071 ** individual journal file, or is NULL, indicating no master journal file 
 54072 ** (single database transaction).
 54073 **
 54074 ** When this is called, the master journal should already have been
 54075 ** created, populated with this journal pointer and synced to disk.
 54076 **
 54077 ** Once this is routine has returned, the only thing required to commit
 54078 ** the write-transaction for this database file is to delete the journal.
 54079 */
 54080 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
 54081   int rc = SQLITE_OK;
 54082   if( p->inTrans==TRANS_WRITE ){
 54083     BtShared *pBt = p->pBt;
 54084     sqlite3BtreeEnter(p);
 54085 #ifndef SQLITE_OMIT_AUTOVACUUM
 54086     if( pBt->autoVacuum ){
 54087       rc = autoVacuumCommit(pBt);
 54088       if( rc!=SQLITE_OK ){
 54089         sqlite3BtreeLeave(p);
 54090         return rc;
 54093     if( pBt->bDoTruncate ){
 54094       sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
 54096 #endif
 54097     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
 54098     sqlite3BtreeLeave(p);
 54100   return rc;
 54103 /*
 54104 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
 54105 ** at the conclusion of a transaction.
 54106 */
 54107 static void btreeEndTransaction(Btree *p){
 54108   BtShared *pBt = p->pBt;
 54109   sqlite3 *db = p->db;
 54110   assert( sqlite3BtreeHoldsMutex(p) );
 54112 #ifndef SQLITE_OMIT_AUTOVACUUM
 54113   pBt->bDoTruncate = 0;
 54114 #endif
 54115   if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
 54116     /* If there are other active statements that belong to this database
 54117     ** handle, downgrade to a read-only transaction. The other statements
 54118     ** may still be reading from the database.  */
 54119     downgradeAllSharedCacheTableLocks(p);
 54120     p->inTrans = TRANS_READ;
 54121   }else{
 54122     /* If the handle had any kind of transaction open, decrement the 
 54123     ** transaction count of the shared btree. If the transaction count 
 54124     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
 54125     ** call below will unlock the pager.  */
 54126     if( p->inTrans!=TRANS_NONE ){
 54127       clearAllSharedCacheTableLocks(p);
 54128       pBt->nTransaction--;
 54129       if( 0==pBt->nTransaction ){
 54130         pBt->inTransaction = TRANS_NONE;
 54134     /* Set the current transaction state to TRANS_NONE and unlock the 
 54135     ** pager if this call closed the only read or write transaction.  */
 54136     p->inTrans = TRANS_NONE;
 54137     unlockBtreeIfUnused(pBt);
 54140   btreeIntegrity(p);
 54143 /*
 54144 ** Commit the transaction currently in progress.
 54145 **
 54146 ** This routine implements the second phase of a 2-phase commit.  The
 54147 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
 54148 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
 54149 ** routine did all the work of writing information out to disk and flushing the
 54150 ** contents so that they are written onto the disk platter.  All this
 54151 ** routine has to do is delete or truncate or zero the header in the
 54152 ** the rollback journal (which causes the transaction to commit) and
 54153 ** drop locks.
 54154 **
 54155 ** Normally, if an error occurs while the pager layer is attempting to 
 54156 ** finalize the underlying journal file, this function returns an error and
 54157 ** the upper layer will attempt a rollback. However, if the second argument
 54158 ** is non-zero then this b-tree transaction is part of a multi-file 
 54159 ** transaction. In this case, the transaction has already been committed 
 54160 ** (by deleting a master journal file) and the caller will ignore this 
 54161 ** functions return code. So, even if an error occurs in the pager layer,
 54162 ** reset the b-tree objects internal state to indicate that the write
 54163 ** transaction has been closed. This is quite safe, as the pager will have
 54164 ** transitioned to the error state.
 54165 **
 54166 ** This will release the write lock on the database file.  If there
 54167 ** are no active cursors, it also releases the read lock.
 54168 */
 54169 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
 54171   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
 54172   sqlite3BtreeEnter(p);
 54173   btreeIntegrity(p);
 54175   /* If the handle has a write-transaction open, commit the shared-btrees 
 54176   ** transaction and set the shared state to TRANS_READ.
 54177   */
 54178   if( p->inTrans==TRANS_WRITE ){
 54179     int rc;
 54180     BtShared *pBt = p->pBt;
 54181     assert( pBt->inTransaction==TRANS_WRITE );
 54182     assert( pBt->nTransaction>0 );
 54183     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
 54184     if( rc!=SQLITE_OK && bCleanup==0 ){
 54185       sqlite3BtreeLeave(p);
 54186       return rc;
 54188     pBt->inTransaction = TRANS_READ;
 54189     btreeClearHasContent(pBt);
 54192   btreeEndTransaction(p);
 54193   sqlite3BtreeLeave(p);
 54194   return SQLITE_OK;
 54197 /*
 54198 ** Do both phases of a commit.
 54199 */
 54200 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
 54201   int rc;
 54202   sqlite3BtreeEnter(p);
 54203   rc = sqlite3BtreeCommitPhaseOne(p, 0);
 54204   if( rc==SQLITE_OK ){
 54205     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
 54207   sqlite3BtreeLeave(p);
 54208   return rc;
 54211 /*
 54212 ** This routine sets the state to CURSOR_FAULT and the error
 54213 ** code to errCode for every cursor on BtShared that pBtree
 54214 ** references.
 54215 **
 54216 ** Every cursor is tripped, including cursors that belong
 54217 ** to other database connections that happen to be sharing
 54218 ** the cache with pBtree.
 54219 **
 54220 ** This routine gets called when a rollback occurs.
 54221 ** All cursors using the same cache must be tripped
 54222 ** to prevent them from trying to use the btree after
 54223 ** the rollback.  The rollback may have deleted tables
 54224 ** or moved root pages, so it is not sufficient to
 54225 ** save the state of the cursor.  The cursor must be
 54226 ** invalidated.
 54227 */
 54228 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
 54229   BtCursor *p;
 54230   if( pBtree==0 ) return;
 54231   sqlite3BtreeEnter(pBtree);
 54232   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 54233     int i;
 54234     sqlite3BtreeClearCursor(p);
 54235     p->eState = CURSOR_FAULT;
 54236     p->skipNext = errCode;
 54237     for(i=0; i<=p->iPage; i++){
 54238       releasePage(p->apPage[i]);
 54239       p->apPage[i] = 0;
 54242   sqlite3BtreeLeave(pBtree);
 54245 /*
 54246 ** Rollback the transaction in progress.  All cursors will be
 54247 ** invalided by this operation.  Any attempt to use a cursor
 54248 ** that was open at the beginning of this operation will result
 54249 ** in an error.
 54250 **
 54251 ** This will release the write lock on the database file.  If there
 54252 ** are no active cursors, it also releases the read lock.
 54253 */
 54254 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
 54255   int rc;
 54256   BtShared *pBt = p->pBt;
 54257   MemPage *pPage1;
 54259   sqlite3BtreeEnter(p);
 54260   if( tripCode==SQLITE_OK ){
 54261     rc = tripCode = saveAllCursors(pBt, 0, 0);
 54262   }else{
 54263     rc = SQLITE_OK;
 54265   if( tripCode ){
 54266     sqlite3BtreeTripAllCursors(p, tripCode);
 54268   btreeIntegrity(p);
 54270   if( p->inTrans==TRANS_WRITE ){
 54271     int rc2;
 54273     assert( TRANS_WRITE==pBt->inTransaction );
 54274     rc2 = sqlite3PagerRollback(pBt->pPager);
 54275     if( rc2!=SQLITE_OK ){
 54276       rc = rc2;
 54279     /* The rollback may have destroyed the pPage1->aData value.  So
 54280     ** call btreeGetPage() on page 1 again to make
 54281     ** sure pPage1->aData is set correctly. */
 54282     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
 54283       int nPage = get4byte(28+(u8*)pPage1->aData);
 54284       testcase( nPage==0 );
 54285       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
 54286       testcase( pBt->nPage!=nPage );
 54287       pBt->nPage = nPage;
 54288       releasePage(pPage1);
 54290     assert( countValidCursors(pBt, 1)==0 );
 54291     pBt->inTransaction = TRANS_READ;
 54292     btreeClearHasContent(pBt);
 54295   btreeEndTransaction(p);
 54296   sqlite3BtreeLeave(p);
 54297   return rc;
 54300 /*
 54301 ** Start a statement subtransaction. The subtransaction can can be rolled
 54302 ** back independently of the main transaction. You must start a transaction 
 54303 ** before starting a subtransaction. The subtransaction is ended automatically 
 54304 ** if the main transaction commits or rolls back.
 54305 **
 54306 ** Statement subtransactions are used around individual SQL statements
 54307 ** that are contained within a BEGIN...COMMIT block.  If a constraint
 54308 ** error occurs within the statement, the effect of that one statement
 54309 ** can be rolled back without having to rollback the entire transaction.
 54310 **
 54311 ** A statement sub-transaction is implemented as an anonymous savepoint. The
 54312 ** value passed as the second parameter is the total number of savepoints,
 54313 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
 54314 ** are no active savepoints and no other statement-transactions open,
 54315 ** iStatement is 1. This anonymous savepoint can be released or rolled back
 54316 ** using the sqlite3BtreeSavepoint() function.
 54317 */
 54318 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
 54319   int rc;
 54320   BtShared *pBt = p->pBt;
 54321   sqlite3BtreeEnter(p);
 54322   assert( p->inTrans==TRANS_WRITE );
 54323   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 54324   assert( iStatement>0 );
 54325   assert( iStatement>p->db->nSavepoint );
 54326   assert( pBt->inTransaction==TRANS_WRITE );
 54327   /* At the pager level, a statement transaction is a savepoint with
 54328   ** an index greater than all savepoints created explicitly using
 54329   ** SQL statements. It is illegal to open, release or rollback any
 54330   ** such savepoints while the statement transaction savepoint is active.
 54331   */
 54332   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
 54333   sqlite3BtreeLeave(p);
 54334   return rc;
 54337 /*
 54338 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
 54339 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
 54340 ** savepoint identified by parameter iSavepoint, depending on the value 
 54341 ** of op.
 54342 **
 54343 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
 54344 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
 54345 ** contents of the entire transaction are rolled back. This is different
 54346 ** from a normal transaction rollback, as no locks are released and the
 54347 ** transaction remains open.
 54348 */
 54349 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
 54350   int rc = SQLITE_OK;
 54351   if( p && p->inTrans==TRANS_WRITE ){
 54352     BtShared *pBt = p->pBt;
 54353     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
 54354     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
 54355     sqlite3BtreeEnter(p);
 54356     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
 54357     if( rc==SQLITE_OK ){
 54358       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
 54359         pBt->nPage = 0;
 54361       rc = newDatabase(pBt);
 54362       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
 54364       /* The database size was written into the offset 28 of the header
 54365       ** when the transaction started, so we know that the value at offset
 54366       ** 28 is nonzero. */
 54367       assert( pBt->nPage>0 );
 54369     sqlite3BtreeLeave(p);
 54371   return rc;
 54374 /*
 54375 ** Create a new cursor for the BTree whose root is on the page
 54376 ** iTable. If a read-only cursor is requested, it is assumed that
 54377 ** the caller already has at least a read-only transaction open
 54378 ** on the database already. If a write-cursor is requested, then
 54379 ** the caller is assumed to have an open write transaction.
 54380 **
 54381 ** If wrFlag==0, then the cursor can only be used for reading.
 54382 ** If wrFlag==1, then the cursor can be used for reading or for
 54383 ** writing if other conditions for writing are also met.  These
 54384 ** are the conditions that must be met in order for writing to
 54385 ** be allowed:
 54386 **
 54387 ** 1:  The cursor must have been opened with wrFlag==1
 54388 **
 54389 ** 2:  Other database connections that share the same pager cache
 54390 **     but which are not in the READ_UNCOMMITTED state may not have
 54391 **     cursors open with wrFlag==0 on the same table.  Otherwise
 54392 **     the changes made by this write cursor would be visible to
 54393 **     the read cursors in the other database connection.
 54394 **
 54395 ** 3:  The database must be writable (not on read-only media)
 54396 **
 54397 ** 4:  There must be an active transaction.
 54398 **
 54399 ** No checking is done to make sure that page iTable really is the
 54400 ** root page of a b-tree.  If it is not, then the cursor acquired
 54401 ** will not work correctly.
 54402 **
 54403 ** It is assumed that the sqlite3BtreeCursorZero() has been called
 54404 ** on pCur to initialize the memory space prior to invoking this routine.
 54405 */
 54406 static int btreeCursor(
 54407   Btree *p,                              /* The btree */
 54408   int iTable,                            /* Root page of table to open */
 54409   int wrFlag,                            /* 1 to write. 0 read-only */
 54410   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
 54411   BtCursor *pCur                         /* Space for new cursor */
 54412 ){
 54413   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
 54415   assert( sqlite3BtreeHoldsMutex(p) );
 54416   assert( wrFlag==0 || wrFlag==1 );
 54418   /* The following assert statements verify that if this is a sharable 
 54419   ** b-tree database, the connection is holding the required table locks, 
 54420   ** and that no other connection has any open cursor that conflicts with 
 54421   ** this lock.  */
 54422   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
 54423   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
 54425   /* Assert that the caller has opened the required transaction. */
 54426   assert( p->inTrans>TRANS_NONE );
 54427   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
 54428   assert( pBt->pPage1 && pBt->pPage1->aData );
 54430   if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
 54431     return SQLITE_READONLY;
 54433   if( iTable==1 && btreePagecount(pBt)==0 ){
 54434     assert( wrFlag==0 );
 54435     iTable = 0;
 54438   /* Now that no other errors can occur, finish filling in the BtCursor
 54439   ** variables and link the cursor into the BtShared list.  */
 54440   pCur->pgnoRoot = (Pgno)iTable;
 54441   pCur->iPage = -1;
 54442   pCur->pKeyInfo = pKeyInfo;
 54443   pCur->pBtree = p;
 54444   pCur->pBt = pBt;
 54445   pCur->wrFlag = (u8)wrFlag;
 54446   pCur->pNext = pBt->pCursor;
 54447   if( pCur->pNext ){
 54448     pCur->pNext->pPrev = pCur;
 54450   pBt->pCursor = pCur;
 54451   pCur->eState = CURSOR_INVALID;
 54452   return SQLITE_OK;
 54454 SQLITE_PRIVATE int sqlite3BtreeCursor(
 54455   Btree *p,                                   /* The btree */
 54456   int iTable,                                 /* Root page of table to open */
 54457   int wrFlag,                                 /* 1 to write. 0 read-only */
 54458   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
 54459   BtCursor *pCur                              /* Write new cursor here */
 54460 ){
 54461   int rc;
 54462   sqlite3BtreeEnter(p);
 54463   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
 54464   sqlite3BtreeLeave(p);
 54465   return rc;
 54468 /*
 54469 ** Return the size of a BtCursor object in bytes.
 54470 **
 54471 ** This interfaces is needed so that users of cursors can preallocate
 54472 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
 54473 ** to users so they cannot do the sizeof() themselves - they must call
 54474 ** this routine.
 54475 */
 54476 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
 54477   return ROUND8(sizeof(BtCursor));
 54480 /*
 54481 ** Initialize memory that will be converted into a BtCursor object.
 54482 **
 54483 ** The simple approach here would be to memset() the entire object
 54484 ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
 54485 ** do not need to be zeroed and they are large, so we can save a lot
 54486 ** of run-time by skipping the initialization of those elements.
 54487 */
 54488 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
 54489   memset(p, 0, offsetof(BtCursor, iPage));
 54492 /*
 54493 ** Close a cursor.  The read lock on the database file is released
 54494 ** when the last cursor is closed.
 54495 */
 54496 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
 54497   Btree *pBtree = pCur->pBtree;
 54498   if( pBtree ){
 54499     int i;
 54500     BtShared *pBt = pCur->pBt;
 54501     sqlite3BtreeEnter(pBtree);
 54502     sqlite3BtreeClearCursor(pCur);
 54503     if( pCur->pPrev ){
 54504       pCur->pPrev->pNext = pCur->pNext;
 54505     }else{
 54506       pBt->pCursor = pCur->pNext;
 54508     if( pCur->pNext ){
 54509       pCur->pNext->pPrev = pCur->pPrev;
 54511     for(i=0; i<=pCur->iPage; i++){
 54512       releasePage(pCur->apPage[i]);
 54514     unlockBtreeIfUnused(pBt);
 54515     invalidateOverflowCache(pCur);
 54516     /* sqlite3_free(pCur); */
 54517     sqlite3BtreeLeave(pBtree);
 54519   return SQLITE_OK;
 54522 /*
 54523 ** Make sure the BtCursor* given in the argument has a valid
 54524 ** BtCursor.info structure.  If it is not already valid, call
 54525 ** btreeParseCell() to fill it in.
 54526 **
 54527 ** BtCursor.info is a cache of the information in the current cell.
 54528 ** Using this cache reduces the number of calls to btreeParseCell().
 54529 **
 54530 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
 54531 ** compiler to crash when getCellInfo() is implemented as a macro.
 54532 ** But there is a measureable speed advantage to using the macro on gcc
 54533 ** (when less compiler optimizations like -Os or -O0 are used and the
 54534 ** compiler is not doing agressive inlining.)  So we use a real function
 54535 ** for MSVC and a macro for everything else.  Ticket #2457.
 54536 */
 54537 #ifndef NDEBUG
 54538   static void assertCellInfo(BtCursor *pCur){
 54539     CellInfo info;
 54540     int iPage = pCur->iPage;
 54541     memset(&info, 0, sizeof(info));
 54542     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
 54543     assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
 54545 #else
 54546   #define assertCellInfo(x)
 54547 #endif
 54548 #ifdef _MSC_VER
 54549   /* Use a real function in MSVC to work around bugs in that compiler. */
 54550   static void getCellInfo(BtCursor *pCur){
 54551     if( pCur->info.nSize==0 ){
 54552       int iPage = pCur->iPage;
 54553       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
 54554       pCur->validNKey = 1;
 54555     }else{
 54556       assertCellInfo(pCur);
 54559 #else /* if not _MSC_VER */
 54560   /* Use a macro in all other compilers so that the function is inlined */
 54561 #define getCellInfo(pCur)                                                      \
 54562   if( pCur->info.nSize==0 ){                                                   \
 54563     int iPage = pCur->iPage;                                                   \
 54564     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
 54565     pCur->validNKey = 1;                                                       \
 54566   }else{                                                                       \
 54567     assertCellInfo(pCur);                                                      \
 54569 #endif /* _MSC_VER */
 54571 #ifndef NDEBUG  /* The next routine used only within assert() statements */
 54572 /*
 54573 ** Return true if the given BtCursor is valid.  A valid cursor is one
 54574 ** that is currently pointing to a row in a (non-empty) table.
 54575 ** This is a verification routine is used only within assert() statements.
 54576 */
 54577 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
 54578   return pCur && pCur->eState==CURSOR_VALID;
 54580 #endif /* NDEBUG */
 54582 /*
 54583 ** Set *pSize to the size of the buffer needed to hold the value of
 54584 ** the key for the current entry.  If the cursor is not pointing
 54585 ** to a valid entry, *pSize is set to 0. 
 54586 **
 54587 ** For a table with the INTKEY flag set, this routine returns the key
 54588 ** itself, not the number of bytes in the key.
 54589 **
 54590 ** The caller must position the cursor prior to invoking this routine.
 54591 ** 
 54592 ** This routine cannot fail.  It always returns SQLITE_OK.  
 54593 */
 54594 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
 54595   assert( cursorHoldsMutex(pCur) );
 54596   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
 54597   if( pCur->eState!=CURSOR_VALID ){
 54598     *pSize = 0;
 54599   }else{
 54600     getCellInfo(pCur);
 54601     *pSize = pCur->info.nKey;
 54603   return SQLITE_OK;
 54606 /*
 54607 ** Set *pSize to the number of bytes of data in the entry the
 54608 ** cursor currently points to.
 54609 **
 54610 ** The caller must guarantee that the cursor is pointing to a non-NULL
 54611 ** valid entry.  In other words, the calling procedure must guarantee
 54612 ** that the cursor has Cursor.eState==CURSOR_VALID.
 54613 **
 54614 ** Failure is not possible.  This function always returns SQLITE_OK.
 54615 ** It might just as well be a procedure (returning void) but we continue
 54616 ** to return an integer result code for historical reasons.
 54617 */
 54618 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
 54619   assert( cursorHoldsMutex(pCur) );
 54620   assert( pCur->eState==CURSOR_VALID );
 54621   getCellInfo(pCur);
 54622   *pSize = pCur->info.nData;
 54623   return SQLITE_OK;
 54626 /*
 54627 ** Given the page number of an overflow page in the database (parameter
 54628 ** ovfl), this function finds the page number of the next page in the 
 54629 ** linked list of overflow pages. If possible, it uses the auto-vacuum
 54630 ** pointer-map data instead of reading the content of page ovfl to do so. 
 54631 **
 54632 ** If an error occurs an SQLite error code is returned. Otherwise:
 54633 **
 54634 ** The page number of the next overflow page in the linked list is 
 54635 ** written to *pPgnoNext. If page ovfl is the last page in its linked 
 54636 ** list, *pPgnoNext is set to zero. 
 54637 **
 54638 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
 54639 ** to page number pOvfl was obtained, then *ppPage is set to point to that
 54640 ** reference. It is the responsibility of the caller to call releasePage()
 54641 ** on *ppPage to free the reference. In no reference was obtained (because
 54642 ** the pointer-map was used to obtain the value for *pPgnoNext), then
 54643 ** *ppPage is set to zero.
 54644 */
 54645 static int getOverflowPage(
 54646   BtShared *pBt,               /* The database file */
 54647   Pgno ovfl,                   /* Current overflow page number */
 54648   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
 54649   Pgno *pPgnoNext              /* OUT: Next overflow page number */
 54650 ){
 54651   Pgno next = 0;
 54652   MemPage *pPage = 0;
 54653   int rc = SQLITE_OK;
 54655   assert( sqlite3_mutex_held(pBt->mutex) );
 54656   assert(pPgnoNext);
 54658 #ifndef SQLITE_OMIT_AUTOVACUUM
 54659   /* Try to find the next page in the overflow list using the
 54660   ** autovacuum pointer-map pages. Guess that the next page in 
 54661   ** the overflow list is page number (ovfl+1). If that guess turns 
 54662   ** out to be wrong, fall back to loading the data of page 
 54663   ** number ovfl to determine the next page number.
 54664   */
 54665   if( pBt->autoVacuum ){
 54666     Pgno pgno;
 54667     Pgno iGuess = ovfl+1;
 54668     u8 eType;
 54670     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
 54671       iGuess++;
 54674     if( iGuess<=btreePagecount(pBt) ){
 54675       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
 54676       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
 54677         next = iGuess;
 54678         rc = SQLITE_DONE;
 54682 #endif
 54684   assert( next==0 || rc==SQLITE_DONE );
 54685   if( rc==SQLITE_OK ){
 54686     rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
 54687     assert( rc==SQLITE_OK || pPage==0 );
 54688     if( rc==SQLITE_OK ){
 54689       next = get4byte(pPage->aData);
 54693   *pPgnoNext = next;
 54694   if( ppPage ){
 54695     *ppPage = pPage;
 54696   }else{
 54697     releasePage(pPage);
 54699   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 54702 /*
 54703 ** Copy data from a buffer to a page, or from a page to a buffer.
 54704 **
 54705 ** pPayload is a pointer to data stored on database page pDbPage.
 54706 ** If argument eOp is false, then nByte bytes of data are copied
 54707 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
 54708 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
 54709 ** of data are copied from the buffer pBuf to pPayload.
 54710 **
 54711 ** SQLITE_OK is returned on success, otherwise an error code.
 54712 */
 54713 static int copyPayload(
 54714   void *pPayload,           /* Pointer to page data */
 54715   void *pBuf,               /* Pointer to buffer */
 54716   int nByte,                /* Number of bytes to copy */
 54717   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
 54718   DbPage *pDbPage           /* Page containing pPayload */
 54719 ){
 54720   if( eOp ){
 54721     /* Copy data from buffer to page (a write operation) */
 54722     int rc = sqlite3PagerWrite(pDbPage);
 54723     if( rc!=SQLITE_OK ){
 54724       return rc;
 54726     memcpy(pPayload, pBuf, nByte);
 54727   }else{
 54728     /* Copy data from page to buffer (a read operation) */
 54729     memcpy(pBuf, pPayload, nByte);
 54731   return SQLITE_OK;
 54734 /*
 54735 ** This function is used to read or overwrite payload information
 54736 ** for the entry that the pCur cursor is pointing to. If the eOp
 54737 ** parameter is 0, this is a read operation (data copied into
 54738 ** buffer pBuf). If it is non-zero, a write (data copied from
 54739 ** buffer pBuf).
 54740 **
 54741 ** A total of "amt" bytes are read or written beginning at "offset".
 54742 ** Data is read to or from the buffer pBuf.
 54743 **
 54744 ** The content being read or written might appear on the main page
 54745 ** or be scattered out on multiple overflow pages.
 54746 **
 54747 ** If the BtCursor.isIncrblobHandle flag is set, and the current
 54748 ** cursor entry uses one or more overflow pages, this function
 54749 ** allocates space for and lazily popluates the overflow page-list 
 54750 ** cache array (BtCursor.aOverflow). Subsequent calls use this
 54751 ** cache to make seeking to the supplied offset more efficient.
 54752 **
 54753 ** Once an overflow page-list cache has been allocated, it may be
 54754 ** invalidated if some other cursor writes to the same table, or if
 54755 ** the cursor is moved to a different row. Additionally, in auto-vacuum
 54756 ** mode, the following events may invalidate an overflow page-list cache.
 54757 **
 54758 **   * An incremental vacuum,
 54759 **   * A commit in auto_vacuum="full" mode,
 54760 **   * Creating a table (may require moving an overflow page).
 54761 */
 54762 static int accessPayload(
 54763   BtCursor *pCur,      /* Cursor pointing to entry to read from */
 54764   u32 offset,          /* Begin reading this far into payload */
 54765   u32 amt,             /* Read this many bytes */
 54766   unsigned char *pBuf, /* Write the bytes into this buffer */ 
 54767   int eOp              /* zero to read. non-zero to write. */
 54768 ){
 54769   unsigned char *aPayload;
 54770   int rc = SQLITE_OK;
 54771   u32 nKey;
 54772   int iIdx = 0;
 54773   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
 54774   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
 54776   assert( pPage );
 54777   assert( pCur->eState==CURSOR_VALID );
 54778   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 54779   assert( cursorHoldsMutex(pCur) );
 54781   getCellInfo(pCur);
 54782   aPayload = pCur->info.pCell + pCur->info.nHeader;
 54783   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
 54785   if( NEVER(offset+amt > nKey+pCur->info.nData) 
 54786    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 54787   ){
 54788     /* Trying to read or write past the end of the data is an error */
 54789     return SQLITE_CORRUPT_BKPT;
 54792   /* Check if data must be read/written to/from the btree page itself. */
 54793   if( offset<pCur->info.nLocal ){
 54794     int a = amt;
 54795     if( a+offset>pCur->info.nLocal ){
 54796       a = pCur->info.nLocal - offset;
 54798     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
 54799     offset = 0;
 54800     pBuf += a;
 54801     amt -= a;
 54802   }else{
 54803     offset -= pCur->info.nLocal;
 54806   if( rc==SQLITE_OK && amt>0 ){
 54807     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
 54808     Pgno nextPage;
 54810     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
 54812 #ifndef SQLITE_OMIT_INCRBLOB
 54813     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
 54814     ** has not been allocated, allocate it now. The array is sized at
 54815     ** one entry for each overflow page in the overflow chain. The
 54816     ** page number of the first overflow page is stored in aOverflow[0],
 54817     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
 54818     ** (the cache is lazily populated).
 54819     */
 54820     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
 54821       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
 54822       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
 54823       /* nOvfl is always positive.  If it were zero, fetchPayload would have
 54824       ** been used instead of this routine. */
 54825       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
 54826         rc = SQLITE_NOMEM;
 54830     /* If the overflow page-list cache has been allocated and the
 54831     ** entry for the first required overflow page is valid, skip
 54832     ** directly to it.
 54833     */
 54834     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
 54835       iIdx = (offset/ovflSize);
 54836       nextPage = pCur->aOverflow[iIdx];
 54837       offset = (offset%ovflSize);
 54839 #endif
 54841     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
 54843 #ifndef SQLITE_OMIT_INCRBLOB
 54844       /* If required, populate the overflow page-list cache. */
 54845       if( pCur->aOverflow ){
 54846         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
 54847         pCur->aOverflow[iIdx] = nextPage;
 54849 #endif
 54851       if( offset>=ovflSize ){
 54852         /* The only reason to read this page is to obtain the page
 54853         ** number for the next page in the overflow chain. The page
 54854         ** data is not required. So first try to lookup the overflow
 54855         ** page-list cache, if any, then fall back to the getOverflowPage()
 54856         ** function.
 54857         */
 54858 #ifndef SQLITE_OMIT_INCRBLOB
 54859         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
 54860           nextPage = pCur->aOverflow[iIdx+1];
 54861         } else 
 54862 #endif
 54863           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
 54864         offset -= ovflSize;
 54865       }else{
 54866         /* Need to read this page properly. It contains some of the
 54867         ** range of data that is being read (eOp==0) or written (eOp!=0).
 54868         */
 54869 #ifdef SQLITE_DIRECT_OVERFLOW_READ
 54870         sqlite3_file *fd;
 54871 #endif
 54872         int a = amt;
 54873         if( a + offset > ovflSize ){
 54874           a = ovflSize - offset;
 54877 #ifdef SQLITE_DIRECT_OVERFLOW_READ
 54878         /* If all the following are true:
 54879         **
 54880         **   1) this is a read operation, and 
 54881         **   2) data is required from the start of this overflow page, and
 54882         **   3) the database is file-backed, and
 54883         **   4) there is no open write-transaction, and
 54884         **   5) the database is not a WAL database,
 54885         **
 54886         ** then data can be read directly from the database file into the
 54887         ** output buffer, bypassing the page-cache altogether. This speeds
 54888         ** up loading large records that span many overflow pages.
 54889         */
 54890         if( eOp==0                                             /* (1) */
 54891          && offset==0                                          /* (2) */
 54892          && pBt->inTransaction==TRANS_READ                     /* (4) */
 54893          && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
 54894          && pBt->pPage1->aData[19]==0x01                       /* (5) */
 54895         ){
 54896           u8 aSave[4];
 54897           u8 *aWrite = &pBuf[-4];
 54898           memcpy(aSave, aWrite, 4);
 54899           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
 54900           nextPage = get4byte(aWrite);
 54901           memcpy(aWrite, aSave, 4);
 54902         }else
 54903 #endif
 54906           DbPage *pDbPage;
 54907           rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
 54908               (eOp==0 ? PAGER_GET_READONLY : 0)
 54909           );
 54910           if( rc==SQLITE_OK ){
 54911             aPayload = sqlite3PagerGetData(pDbPage);
 54912             nextPage = get4byte(aPayload);
 54913             rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
 54914             sqlite3PagerUnref(pDbPage);
 54915             offset = 0;
 54918         amt -= a;
 54919         pBuf += a;
 54924   if( rc==SQLITE_OK && amt>0 ){
 54925     return SQLITE_CORRUPT_BKPT;
 54927   return rc;
 54930 /*
 54931 ** Read part of the key associated with cursor pCur.  Exactly
 54932 ** "amt" bytes will be transfered into pBuf[].  The transfer
 54933 ** begins at "offset".
 54934 **
 54935 ** The caller must ensure that pCur is pointing to a valid row
 54936 ** in the table.
 54937 **
 54938 ** Return SQLITE_OK on success or an error code if anything goes
 54939 ** wrong.  An error is returned if "offset+amt" is larger than
 54940 ** the available payload.
 54941 */
 54942 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 54943   assert( cursorHoldsMutex(pCur) );
 54944   assert( pCur->eState==CURSOR_VALID );
 54945   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 54946   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 54947   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
 54950 /*
 54951 ** Read part of the data associated with cursor pCur.  Exactly
 54952 ** "amt" bytes will be transfered into pBuf[].  The transfer
 54953 ** begins at "offset".
 54954 **
 54955 ** Return SQLITE_OK on success or an error code if anything goes
 54956 ** wrong.  An error is returned if "offset+amt" is larger than
 54957 ** the available payload.
 54958 */
 54959 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
 54960   int rc;
 54962 #ifndef SQLITE_OMIT_INCRBLOB
 54963   if ( pCur->eState==CURSOR_INVALID ){
 54964     return SQLITE_ABORT;
 54966 #endif
 54968   assert( cursorHoldsMutex(pCur) );
 54969   rc = restoreCursorPosition(pCur);
 54970   if( rc==SQLITE_OK ){
 54971     assert( pCur->eState==CURSOR_VALID );
 54972     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
 54973     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 54974     rc = accessPayload(pCur, offset, amt, pBuf, 0);
 54976   return rc;
 54979 /*
 54980 ** Return a pointer to payload information from the entry that the 
 54981 ** pCur cursor is pointing to.  The pointer is to the beginning of
 54982 ** the key if index btrees (pPage->intKey==0) and is the data for
 54983 ** table btrees (pPage->intKey==1). The number of bytes of available
 54984 ** key/data is written into *pAmt.  If *pAmt==0, then the value
 54985 ** returned will not be a valid pointer.
 54986 **
 54987 ** This routine is an optimization.  It is common for the entire key
 54988 ** and data to fit on the local page and for there to be no overflow
 54989 ** pages.  When that is so, this routine can be used to access the
 54990 ** key and data without making a copy.  If the key and/or data spills
 54991 ** onto overflow pages, then accessPayload() must be used to reassemble
 54992 ** the key/data and copy it into a preallocated buffer.
 54993 **
 54994 ** The pointer returned by this routine looks directly into the cached
 54995 ** page of the database.  The data might change or move the next time
 54996 ** any btree routine is called.
 54997 */
 54998 static const void *fetchPayload(
 54999   BtCursor *pCur,      /* Cursor pointing to entry to read from */
 55000   u32 *pAmt            /* Write the number of available bytes here */
 55001 ){
 55002   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
 55003   assert( pCur->eState==CURSOR_VALID );
 55004   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55005   assert( cursorHoldsMutex(pCur) );
 55006   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 55007   if( pCur->info.nSize==0 ){
 55008     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
 55009                    &pCur->info);
 55011   *pAmt = pCur->info.nLocal;
 55012   return (void*)(pCur->info.pCell + pCur->info.nHeader);
 55016 /*
 55017 ** For the entry that cursor pCur is point to, return as
 55018 ** many bytes of the key or data as are available on the local
 55019 ** b-tree page.  Write the number of available bytes into *pAmt.
 55020 **
 55021 ** The pointer returned is ephemeral.  The key/data may move
 55022 ** or be destroyed on the next call to any Btree routine,
 55023 ** including calls from other threads against the same cache.
 55024 ** Hence, a mutex on the BtShared should be held prior to calling
 55025 ** this routine.
 55026 **
 55027 ** These routines is used to get quick access to key and data
 55028 ** in the common case where no overflow pages are used.
 55029 */
 55030 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
 55031   return fetchPayload(pCur, pAmt);
 55033 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
 55034   return fetchPayload(pCur, pAmt);
 55038 /*
 55039 ** Move the cursor down to a new child page.  The newPgno argument is the
 55040 ** page number of the child page to move to.
 55041 **
 55042 ** This function returns SQLITE_CORRUPT if the page-header flags field of
 55043 ** the new child page does not match the flags field of the parent (i.e.
 55044 ** if an intkey page appears to be the parent of a non-intkey page, or
 55045 ** vice-versa).
 55046 */
 55047 static int moveToChild(BtCursor *pCur, u32 newPgno){
 55048   int rc;
 55049   int i = pCur->iPage;
 55050   MemPage *pNewPage;
 55051   BtShared *pBt = pCur->pBt;
 55053   assert( cursorHoldsMutex(pCur) );
 55054   assert( pCur->eState==CURSOR_VALID );
 55055   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 55056   assert( pCur->iPage>=0 );
 55057   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
 55058     return SQLITE_CORRUPT_BKPT;
 55060   rc = getAndInitPage(pBt, newPgno, &pNewPage,
 55061                pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
 55062   if( rc ) return rc;
 55063   pCur->apPage[i+1] = pNewPage;
 55064   pCur->aiIdx[i+1] = 0;
 55065   pCur->iPage++;
 55067   pCur->info.nSize = 0;
 55068   pCur->validNKey = 0;
 55069   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
 55070     return SQLITE_CORRUPT_BKPT;
 55072   return SQLITE_OK;
 55075 #if 0
 55076 /*
 55077 ** Page pParent is an internal (non-leaf) tree page. This function 
 55078 ** asserts that page number iChild is the left-child if the iIdx'th
 55079 ** cell in page pParent. Or, if iIdx is equal to the total number of
 55080 ** cells in pParent, that page number iChild is the right-child of
 55081 ** the page.
 55082 */
 55083 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
 55084   assert( iIdx<=pParent->nCell );
 55085   if( iIdx==pParent->nCell ){
 55086     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
 55087   }else{
 55088     assert( get4byte(findCell(pParent, iIdx))==iChild );
 55091 #else
 55092 #  define assertParentIndex(x,y,z) 
 55093 #endif
 55095 /*
 55096 ** Move the cursor up to the parent page.
 55097 **
 55098 ** pCur->idx is set to the cell index that contains the pointer
 55099 ** to the page we are coming from.  If we are coming from the
 55100 ** right-most child page then pCur->idx is set to one more than
 55101 ** the largest cell index.
 55102 */
 55103 static void moveToParent(BtCursor *pCur){
 55104   assert( cursorHoldsMutex(pCur) );
 55105   assert( pCur->eState==CURSOR_VALID );
 55106   assert( pCur->iPage>0 );
 55107   assert( pCur->apPage[pCur->iPage] );
 55109   /* UPDATE: It is actually possible for the condition tested by the assert
 55110   ** below to be untrue if the database file is corrupt. This can occur if
 55111   ** one cursor has modified page pParent while a reference to it is held 
 55112   ** by a second cursor. Which can only happen if a single page is linked
 55113   ** into more than one b-tree structure in a corrupt database.  */
 55114 #if 0
 55115   assertParentIndex(
 55116     pCur->apPage[pCur->iPage-1], 
 55117     pCur->aiIdx[pCur->iPage-1], 
 55118     pCur->apPage[pCur->iPage]->pgno
 55119   );
 55120 #endif
 55121   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
 55123   releasePage(pCur->apPage[pCur->iPage]);
 55124   pCur->iPage--;
 55125   pCur->info.nSize = 0;
 55126   pCur->validNKey = 0;
 55129 /*
 55130 ** Move the cursor to point to the root page of its b-tree structure.
 55131 **
 55132 ** If the table has a virtual root page, then the cursor is moved to point
 55133 ** to the virtual root page instead of the actual root page. A table has a
 55134 ** virtual root page when the actual root page contains no cells and a 
 55135 ** single child page. This can only happen with the table rooted at page 1.
 55136 **
 55137 ** If the b-tree structure is empty, the cursor state is set to 
 55138 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
 55139 ** cell located on the root (or virtual root) page and the cursor state
 55140 ** is set to CURSOR_VALID.
 55141 **
 55142 ** If this function returns successfully, it may be assumed that the
 55143 ** page-header flags indicate that the [virtual] root-page is the expected 
 55144 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
 55145 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
 55146 ** indicating a table b-tree, or if the caller did specify a KeyInfo 
 55147 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
 55148 ** b-tree).
 55149 */
 55150 static int moveToRoot(BtCursor *pCur){
 55151   MemPage *pRoot;
 55152   int rc = SQLITE_OK;
 55154   assert( cursorHoldsMutex(pCur) );
 55155   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
 55156   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
 55157   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
 55158   if( pCur->eState>=CURSOR_REQUIRESEEK ){
 55159     if( pCur->eState==CURSOR_FAULT ){
 55160       assert( pCur->skipNext!=SQLITE_OK );
 55161       return pCur->skipNext;
 55163     sqlite3BtreeClearCursor(pCur);
 55166   if( pCur->iPage>=0 ){
 55167     while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
 55168   }else if( pCur->pgnoRoot==0 ){
 55169     pCur->eState = CURSOR_INVALID;
 55170     return SQLITE_OK;
 55171   }else{
 55172     rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
 55173                         pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
 55174     if( rc!=SQLITE_OK ){
 55175       pCur->eState = CURSOR_INVALID;
 55176       return rc;
 55178     pCur->iPage = 0;
 55180   pRoot = pCur->apPage[0];
 55181   assert( pRoot->pgno==pCur->pgnoRoot );
 55183   /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
 55184   ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
 55185   ** NULL, the caller expects a table b-tree. If this is not the case,
 55186   ** return an SQLITE_CORRUPT error. 
 55187   **
 55188   ** Earlier versions of SQLite assumed that this test could not fail
 55189   ** if the root page was already loaded when this function was called (i.e.
 55190   ** if pCur->iPage>=0). But this is not so if the database is corrupted 
 55191   ** in such a way that page pRoot is linked into a second b-tree table 
 55192   ** (or the freelist).  */
 55193   assert( pRoot->intKey==1 || pRoot->intKey==0 );
 55194   if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
 55195     return SQLITE_CORRUPT_BKPT;
 55198   pCur->aiIdx[0] = 0;
 55199   pCur->info.nSize = 0;
 55200   pCur->atLast = 0;
 55201   pCur->validNKey = 0;
 55203   if( pRoot->nCell>0 ){
 55204     pCur->eState = CURSOR_VALID;
 55205   }else if( !pRoot->leaf ){
 55206     Pgno subpage;
 55207     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
 55208     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
 55209     pCur->eState = CURSOR_VALID;
 55210     rc = moveToChild(pCur, subpage);
 55211   }else{
 55212     pCur->eState = CURSOR_INVALID;
 55214   return rc;
 55217 /*
 55218 ** Move the cursor down to the left-most leaf entry beneath the
 55219 ** entry to which it is currently pointing.
 55220 **
 55221 ** The left-most leaf is the one with the smallest key - the first
 55222 ** in ascending order.
 55223 */
 55224 static int moveToLeftmost(BtCursor *pCur){
 55225   Pgno pgno;
 55226   int rc = SQLITE_OK;
 55227   MemPage *pPage;
 55229   assert( cursorHoldsMutex(pCur) );
 55230   assert( pCur->eState==CURSOR_VALID );
 55231   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 55232     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 55233     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
 55234     rc = moveToChild(pCur, pgno);
 55236   return rc;
 55239 /*
 55240 ** Move the cursor down to the right-most leaf entry beneath the
 55241 ** page to which it is currently pointing.  Notice the difference
 55242 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
 55243 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
 55244 ** finds the right-most entry beneath the *page*.
 55245 **
 55246 ** The right-most entry is the one with the largest key - the last
 55247 ** key in ascending order.
 55248 */
 55249 static int moveToRightmost(BtCursor *pCur){
 55250   Pgno pgno;
 55251   int rc = SQLITE_OK;
 55252   MemPage *pPage = 0;
 55254   assert( cursorHoldsMutex(pCur) );
 55255   assert( pCur->eState==CURSOR_VALID );
 55256   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
 55257     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 55258     pCur->aiIdx[pCur->iPage] = pPage->nCell;
 55259     rc = moveToChild(pCur, pgno);
 55261   if( rc==SQLITE_OK ){
 55262     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
 55263     pCur->info.nSize = 0;
 55264     pCur->validNKey = 0;
 55266   return rc;
 55269 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
 55270 ** on success.  Set *pRes to 0 if the cursor actually points to something
 55271 ** or set *pRes to 1 if the table is empty.
 55272 */
 55273 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
 55274   int rc;
 55276   assert( cursorHoldsMutex(pCur) );
 55277   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55278   rc = moveToRoot(pCur);
 55279   if( rc==SQLITE_OK ){
 55280     if( pCur->eState==CURSOR_INVALID ){
 55281       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 55282       *pRes = 1;
 55283     }else{
 55284       assert( pCur->apPage[pCur->iPage]->nCell>0 );
 55285       *pRes = 0;
 55286       rc = moveToLeftmost(pCur);
 55289   return rc;
 55292 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
 55293 ** on success.  Set *pRes to 0 if the cursor actually points to something
 55294 ** or set *pRes to 1 if the table is empty.
 55295 */
 55296 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
 55297   int rc;
 55299   assert( cursorHoldsMutex(pCur) );
 55300   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55302   /* If the cursor already points to the last entry, this is a no-op. */
 55303   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
 55304 #ifdef SQLITE_DEBUG
 55305     /* This block serves to assert() that the cursor really does point 
 55306     ** to the last entry in the b-tree. */
 55307     int ii;
 55308     for(ii=0; ii<pCur->iPage; ii++){
 55309       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
 55311     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
 55312     assert( pCur->apPage[pCur->iPage]->leaf );
 55313 #endif
 55314     return SQLITE_OK;
 55317   rc = moveToRoot(pCur);
 55318   if( rc==SQLITE_OK ){
 55319     if( CURSOR_INVALID==pCur->eState ){
 55320       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 55321       *pRes = 1;
 55322     }else{
 55323       assert( pCur->eState==CURSOR_VALID );
 55324       *pRes = 0;
 55325       rc = moveToRightmost(pCur);
 55326       pCur->atLast = rc==SQLITE_OK ?1:0;
 55329   return rc;
 55332 /* Move the cursor so that it points to an entry near the key 
 55333 ** specified by pIdxKey or intKey.   Return a success code.
 55334 **
 55335 ** For INTKEY tables, the intKey parameter is used.  pIdxKey 
 55336 ** must be NULL.  For index tables, pIdxKey is used and intKey
 55337 ** is ignored.
 55338 **
 55339 ** If an exact match is not found, then the cursor is always
 55340 ** left pointing at a leaf page which would hold the entry if it
 55341 ** were present.  The cursor might point to an entry that comes
 55342 ** before or after the key.
 55343 **
 55344 ** An integer is written into *pRes which is the result of
 55345 ** comparing the key with the entry to which the cursor is 
 55346 ** pointing.  The meaning of the integer written into
 55347 ** *pRes is as follows:
 55348 **
 55349 **     *pRes<0      The cursor is left pointing at an entry that
 55350 **                  is smaller than intKey/pIdxKey or if the table is empty
 55351 **                  and the cursor is therefore left point to nothing.
 55352 **
 55353 **     *pRes==0     The cursor is left pointing at an entry that
 55354 **                  exactly matches intKey/pIdxKey.
 55355 **
 55356 **     *pRes>0      The cursor is left pointing at an entry that
 55357 **                  is larger than intKey/pIdxKey.
 55358 **
 55359 */
 55360 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
 55361   BtCursor *pCur,          /* The cursor to be moved */
 55362   UnpackedRecord *pIdxKey, /* Unpacked index key */
 55363   i64 intKey,              /* The table key */
 55364   int biasRight,           /* If true, bias the search to the high end */
 55365   int *pRes                /* Write search results here */
 55366 ){
 55367   int rc;
 55368   RecordCompare xRecordCompare;
 55370   assert( cursorHoldsMutex(pCur) );
 55371   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 55372   assert( pRes );
 55373   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
 55375   /* If the cursor is already positioned at the point we are trying
 55376   ** to move to, then just return without doing any work */
 55377   if( pCur->eState==CURSOR_VALID && pCur->validNKey 
 55378    && pCur->apPage[0]->intKey 
 55379   ){
 55380     if( pCur->info.nKey==intKey ){
 55381       *pRes = 0;
 55382       return SQLITE_OK;
 55384     if( pCur->atLast && pCur->info.nKey<intKey ){
 55385       *pRes = -1;
 55386       return SQLITE_OK;
 55390   if( pIdxKey ){
 55391     xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
 55392     assert( pIdxKey->default_rc==1 
 55393          || pIdxKey->default_rc==0 
 55394          || pIdxKey->default_rc==-1
 55395     );
 55396   }else{
 55397     xRecordCompare = 0; /* All keys are integers */
 55400   rc = moveToRoot(pCur);
 55401   if( rc ){
 55402     return rc;
 55404   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
 55405   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
 55406   assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
 55407   if( pCur->eState==CURSOR_INVALID ){
 55408     *pRes = -1;
 55409     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
 55410     return SQLITE_OK;
 55412   assert( pCur->apPage[0]->intKey || pIdxKey );
 55413   for(;;){
 55414     int lwr, upr, idx, c;
 55415     Pgno chldPg;
 55416     MemPage *pPage = pCur->apPage[pCur->iPage];
 55417     u8 *pCell;                          /* Pointer to current cell in pPage */
 55419     /* pPage->nCell must be greater than zero. If this is the root-page
 55420     ** the cursor would have been INVALID above and this for(;;) loop
 55421     ** not run. If this is not the root-page, then the moveToChild() routine
 55422     ** would have already detected db corruption. Similarly, pPage must
 55423     ** be the right kind (index or table) of b-tree page. Otherwise
 55424     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
 55425     assert( pPage->nCell>0 );
 55426     assert( pPage->intKey==(pIdxKey==0) );
 55427     lwr = 0;
 55428     upr = pPage->nCell-1;
 55429     assert( biasRight==0 || biasRight==1 );
 55430     idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
 55431     pCur->aiIdx[pCur->iPage] = (u16)idx;
 55432     if( xRecordCompare==0 ){
 55433       for(;;){
 55434         i64 nCellKey;
 55435         pCell = findCell(pPage, idx) + pPage->childPtrSize;
 55436         if( pPage->hasData ){
 55437           while( 0x80 <= *(pCell++) ){
 55438             if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
 55441         getVarint(pCell, (u64*)&nCellKey);
 55442         if( nCellKey<intKey ){
 55443           lwr = idx+1;
 55444           if( lwr>upr ){ c = -1; break; }
 55445         }else if( nCellKey>intKey ){
 55446           upr = idx-1;
 55447           if( lwr>upr ){ c = +1; break; }
 55448         }else{
 55449           assert( nCellKey==intKey );
 55450           pCur->validNKey = 1;
 55451           pCur->info.nKey = nCellKey;
 55452           pCur->aiIdx[pCur->iPage] = (u16)idx;
 55453           if( !pPage->leaf ){
 55454             lwr = idx;
 55455             goto moveto_next_layer;
 55456           }else{
 55457             *pRes = 0;
 55458             rc = SQLITE_OK;
 55459             goto moveto_finish;
 55462         assert( lwr+upr>=0 );
 55463         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
 55465     }else{
 55466       for(;;){
 55467         int nCell;
 55468         pCell = findCell(pPage, idx) + pPage->childPtrSize;
 55470         /* The maximum supported page-size is 65536 bytes. This means that
 55471         ** the maximum number of record bytes stored on an index B-Tree
 55472         ** page is less than 16384 bytes and may be stored as a 2-byte
 55473         ** varint. This information is used to attempt to avoid parsing 
 55474         ** the entire cell by checking for the cases where the record is 
 55475         ** stored entirely within the b-tree page by inspecting the first 
 55476         ** 2 bytes of the cell.
 55477         */
 55478         nCell = pCell[0];
 55479         if( nCell<=pPage->max1bytePayload ){
 55480           /* This branch runs if the record-size field of the cell is a
 55481           ** single byte varint and the record fits entirely on the main
 55482           ** b-tree page.  */
 55483           testcase( pCell+nCell+1==pPage->aDataEnd );
 55484           c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
 55485         }else if( !(pCell[1] & 0x80) 
 55486           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
 55487         ){
 55488           /* The record-size field is a 2 byte varint and the record 
 55489           ** fits entirely on the main b-tree page.  */
 55490           testcase( pCell+nCell+2==pPage->aDataEnd );
 55491           c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
 55492         }else{
 55493           /* The record flows over onto one or more overflow pages. In
 55494           ** this case the whole cell needs to be parsed, a buffer allocated
 55495           ** and accessPayload() used to retrieve the record into the
 55496           ** buffer before VdbeRecordCompare() can be called. */
 55497           void *pCellKey;
 55498           u8 * const pCellBody = pCell - pPage->childPtrSize;
 55499           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
 55500           nCell = (int)pCur->info.nKey;
 55501           pCellKey = sqlite3Malloc( nCell );
 55502           if( pCellKey==0 ){
 55503             rc = SQLITE_NOMEM;
 55504             goto moveto_finish;
 55506           pCur->aiIdx[pCur->iPage] = (u16)idx;
 55507           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
 55508           if( rc ){
 55509             sqlite3_free(pCellKey);
 55510             goto moveto_finish;
 55512           c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
 55513           sqlite3_free(pCellKey);
 55515         if( c<0 ){
 55516           lwr = idx+1;
 55517         }else if( c>0 ){
 55518           upr = idx-1;
 55519         }else{
 55520           assert( c==0 );
 55521           *pRes = 0;
 55522           rc = SQLITE_OK;
 55523           pCur->aiIdx[pCur->iPage] = (u16)idx;
 55524           goto moveto_finish;
 55526         if( lwr>upr ) break;
 55527         assert( lwr+upr>=0 );
 55528         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
 55531     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
 55532     assert( pPage->isInit );
 55533     if( pPage->leaf ){
 55534       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
 55535       pCur->aiIdx[pCur->iPage] = (u16)idx;
 55536       *pRes = c;
 55537       rc = SQLITE_OK;
 55538       goto moveto_finish;
 55540 moveto_next_layer:
 55541     if( lwr>=pPage->nCell ){
 55542       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 55543     }else{
 55544       chldPg = get4byte(findCell(pPage, lwr));
 55546     pCur->aiIdx[pCur->iPage] = (u16)lwr;
 55547     rc = moveToChild(pCur, chldPg);
 55548     if( rc ) break;
 55550 moveto_finish:
 55551   pCur->info.nSize = 0;
 55552   pCur->validNKey = 0;
 55553   return rc;
 55557 /*
 55558 ** Return TRUE if the cursor is not pointing at an entry of the table.
 55559 **
 55560 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
 55561 ** past the last entry in the table or sqlite3BtreePrev() moves past
 55562 ** the first entry.  TRUE is also returned if the table is empty.
 55563 */
 55564 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
 55565   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
 55566   ** have been deleted? This API will need to change to return an error code
 55567   ** as well as the boolean result value.
 55568   */
 55569   return (CURSOR_VALID!=pCur->eState);
 55572 /*
 55573 ** Advance the cursor to the next entry in the database.  If
 55574 ** successful then set *pRes=0.  If the cursor
 55575 ** was already pointing to the last entry in the database before
 55576 ** this routine was called, then set *pRes=1.
 55577 **
 55578 ** The calling function will set *pRes to 0 or 1.  The initial *pRes value
 55579 ** will be 1 if the cursor being stepped corresponds to an SQL index and
 55580 ** if this routine could have been skipped if that SQL index had been
 55581 ** a unique index.  Otherwise the caller will have set *pRes to zero.
 55582 ** Zero is the common case. The btree implementation is free to use the
 55583 ** initial *pRes value as a hint to improve performance, but the current
 55584 ** SQLite btree implementation does not. (Note that the comdb2 btree
 55585 ** implementation does use this hint, however.)
 55586 */
 55587 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
 55588   int rc;
 55589   int idx;
 55590   MemPage *pPage;
 55592   assert( cursorHoldsMutex(pCur) );
 55593   assert( pRes!=0 );
 55594   assert( *pRes==0 || *pRes==1 );
 55595   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55596   if( pCur->eState!=CURSOR_VALID ){
 55597     rc = restoreCursorPosition(pCur);
 55598     if( rc!=SQLITE_OK ){
 55599       *pRes = 0;
 55600       return rc;
 55602     if( CURSOR_INVALID==pCur->eState ){
 55603       *pRes = 1;
 55604       return SQLITE_OK;
 55606     if( pCur->skipNext ){
 55607       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
 55608       pCur->eState = CURSOR_VALID;
 55609       if( pCur->skipNext>0 ){
 55610         pCur->skipNext = 0;
 55611         *pRes = 0;
 55612         return SQLITE_OK;
 55614       pCur->skipNext = 0;
 55618   pPage = pCur->apPage[pCur->iPage];
 55619   idx = ++pCur->aiIdx[pCur->iPage];
 55620   assert( pPage->isInit );
 55622   /* If the database file is corrupt, it is possible for the value of idx 
 55623   ** to be invalid here. This can only occur if a second cursor modifies
 55624   ** the page while cursor pCur is holding a reference to it. Which can
 55625   ** only happen if the database is corrupt in such a way as to link the
 55626   ** page into more than one b-tree structure. */
 55627   testcase( idx>pPage->nCell );
 55629   pCur->info.nSize = 0;
 55630   pCur->validNKey = 0;
 55631   if( idx>=pPage->nCell ){
 55632     if( !pPage->leaf ){
 55633       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 55634       if( rc ){
 55635         *pRes = 0;
 55636         return rc;
 55638       rc = moveToLeftmost(pCur);
 55639       *pRes = 0;
 55640       return rc;
 55642     do{
 55643       if( pCur->iPage==0 ){
 55644         *pRes = 1;
 55645         pCur->eState = CURSOR_INVALID;
 55646         return SQLITE_OK;
 55648       moveToParent(pCur);
 55649       pPage = pCur->apPage[pCur->iPage];
 55650     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
 55651     *pRes = 0;
 55652     if( pPage->intKey ){
 55653       rc = sqlite3BtreeNext(pCur, pRes);
 55654     }else{
 55655       rc = SQLITE_OK;
 55657     return rc;
 55659   *pRes = 0;
 55660   if( pPage->leaf ){
 55661     return SQLITE_OK;
 55663   rc = moveToLeftmost(pCur);
 55664   return rc;
 55668 /*
 55669 ** Step the cursor to the back to the previous entry in the database.  If
 55670 ** successful then set *pRes=0.  If the cursor
 55671 ** was already pointing to the first entry in the database before
 55672 ** this routine was called, then set *pRes=1.
 55673 **
 55674 ** The calling function will set *pRes to 0 or 1.  The initial *pRes value
 55675 ** will be 1 if the cursor being stepped corresponds to an SQL index and
 55676 ** if this routine could have been skipped if that SQL index had been
 55677 ** a unique index.  Otherwise the caller will have set *pRes to zero.
 55678 ** Zero is the common case. The btree implementation is free to use the
 55679 ** initial *pRes value as a hint to improve performance, but the current
 55680 ** SQLite btree implementation does not. (Note that the comdb2 btree
 55681 ** implementation does use this hint, however.)
 55682 */
 55683 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
 55684   int rc;
 55685   MemPage *pPage;
 55687   assert( cursorHoldsMutex(pCur) );
 55688   assert( pRes!=0 );
 55689   assert( *pRes==0 || *pRes==1 );
 55690   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 55691   pCur->atLast = 0;
 55692   if( pCur->eState!=CURSOR_VALID ){
 55693     if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
 55694       rc = btreeRestoreCursorPosition(pCur);
 55695       if( rc!=SQLITE_OK ){
 55696         *pRes = 0;
 55697         return rc;
 55700     if( CURSOR_INVALID==pCur->eState ){
 55701       *pRes = 1;
 55702       return SQLITE_OK;
 55704     if( pCur->skipNext ){
 55705       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
 55706       pCur->eState = CURSOR_VALID;
 55707       if( pCur->skipNext<0 ){
 55708         pCur->skipNext = 0;
 55709         *pRes = 0;
 55710         return SQLITE_OK;
 55712       pCur->skipNext = 0;
 55716   pPage = pCur->apPage[pCur->iPage];
 55717   assert( pPage->isInit );
 55718   if( !pPage->leaf ){
 55719     int idx = pCur->aiIdx[pCur->iPage];
 55720     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
 55721     if( rc ){
 55722       *pRes = 0;
 55723       return rc;
 55725     rc = moveToRightmost(pCur);
 55726   }else{
 55727     while( pCur->aiIdx[pCur->iPage]==0 ){
 55728       if( pCur->iPage==0 ){
 55729         pCur->eState = CURSOR_INVALID;
 55730         *pRes = 1;
 55731         return SQLITE_OK;
 55733       moveToParent(pCur);
 55735     pCur->info.nSize = 0;
 55736     pCur->validNKey = 0;
 55738     pCur->aiIdx[pCur->iPage]--;
 55739     pPage = pCur->apPage[pCur->iPage];
 55740     if( pPage->intKey && !pPage->leaf ){
 55741       rc = sqlite3BtreePrevious(pCur, pRes);
 55742     }else{
 55743       rc = SQLITE_OK;
 55746   *pRes = 0;
 55747   return rc;
 55750 /*
 55751 ** Allocate a new page from the database file.
 55752 **
 55753 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
 55754 ** has already been called on the new page.)  The new page has also
 55755 ** been referenced and the calling routine is responsible for calling
 55756 ** sqlite3PagerUnref() on the new page when it is done.
 55757 **
 55758 ** SQLITE_OK is returned on success.  Any other return value indicates
 55759 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
 55760 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
 55761 **
 55762 ** If the "nearby" parameter is not 0, then an effort is made to 
 55763 ** locate a page close to the page number "nearby".  This can be used in an
 55764 ** attempt to keep related pages close to each other in the database file,
 55765 ** which in turn can make database access faster.
 55766 **
 55767 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
 55768 ** anywhere on the free-list, then it is guaranteed to be returned.  If
 55769 ** eMode is BTALLOC_LT then the page returned will be less than or equal
 55770 ** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
 55771 ** are no restrictions on which page is returned.
 55772 */
 55773 static int allocateBtreePage(
 55774   BtShared *pBt,         /* The btree */
 55775   MemPage **ppPage,      /* Store pointer to the allocated page here */
 55776   Pgno *pPgno,           /* Store the page number here */
 55777   Pgno nearby,           /* Search for a page near this one */
 55778   u8 eMode               /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
 55779 ){
 55780   MemPage *pPage1;
 55781   int rc;
 55782   u32 n;     /* Number of pages on the freelist */
 55783   u32 k;     /* Number of leaves on the trunk of the freelist */
 55784   MemPage *pTrunk = 0;
 55785   MemPage *pPrevTrunk = 0;
 55786   Pgno mxPage;     /* Total size of the database file */
 55788   assert( sqlite3_mutex_held(pBt->mutex) );
 55789   assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
 55790   pPage1 = pBt->pPage1;
 55791   mxPage = btreePagecount(pBt);
 55792   n = get4byte(&pPage1->aData[36]);
 55793   testcase( n==mxPage-1 );
 55794   if( n>=mxPage ){
 55795     return SQLITE_CORRUPT_BKPT;
 55797   if( n>0 ){
 55798     /* There are pages on the freelist.  Reuse one of those pages. */
 55799     Pgno iTrunk;
 55800     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
 55802     /* If eMode==BTALLOC_EXACT and a query of the pointer-map
 55803     ** shows that the page 'nearby' is somewhere on the free-list, then
 55804     ** the entire-list will be searched for that page.
 55805     */
 55806 #ifndef SQLITE_OMIT_AUTOVACUUM
 55807     if( eMode==BTALLOC_EXACT ){
 55808       if( nearby<=mxPage ){
 55809         u8 eType;
 55810         assert( nearby>0 );
 55811         assert( pBt->autoVacuum );
 55812         rc = ptrmapGet(pBt, nearby, &eType, 0);
 55813         if( rc ) return rc;
 55814         if( eType==PTRMAP_FREEPAGE ){
 55815           searchList = 1;
 55818     }else if( eMode==BTALLOC_LE ){
 55819       searchList = 1;
 55821 #endif
 55823     /* Decrement the free-list count by 1. Set iTrunk to the index of the
 55824     ** first free-list trunk page. iPrevTrunk is initially 1.
 55825     */
 55826     rc = sqlite3PagerWrite(pPage1->pDbPage);
 55827     if( rc ) return rc;
 55828     put4byte(&pPage1->aData[36], n-1);
 55830     /* The code within this loop is run only once if the 'searchList' variable
 55831     ** is not true. Otherwise, it runs once for each trunk-page on the
 55832     ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
 55833     ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
 55834     */
 55835     do {
 55836       pPrevTrunk = pTrunk;
 55837       if( pPrevTrunk ){
 55838         iTrunk = get4byte(&pPrevTrunk->aData[0]);
 55839       }else{
 55840         iTrunk = get4byte(&pPage1->aData[32]);
 55842       testcase( iTrunk==mxPage );
 55843       if( iTrunk>mxPage ){
 55844         rc = SQLITE_CORRUPT_BKPT;
 55845       }else{
 55846         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 55848       if( rc ){
 55849         pTrunk = 0;
 55850         goto end_allocate_page;
 55852       assert( pTrunk!=0 );
 55853       assert( pTrunk->aData!=0 );
 55855       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
 55856       if( k==0 && !searchList ){
 55857         /* The trunk has no leaves and the list is not being searched. 
 55858         ** So extract the trunk page itself and use it as the newly 
 55859         ** allocated page */
 55860         assert( pPrevTrunk==0 );
 55861         rc = sqlite3PagerWrite(pTrunk->pDbPage);
 55862         if( rc ){
 55863           goto end_allocate_page;
 55865         *pPgno = iTrunk;
 55866         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 55867         *ppPage = pTrunk;
 55868         pTrunk = 0;
 55869         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 55870       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
 55871         /* Value of k is out of range.  Database corruption */
 55872         rc = SQLITE_CORRUPT_BKPT;
 55873         goto end_allocate_page;
 55874 #ifndef SQLITE_OMIT_AUTOVACUUM
 55875       }else if( searchList 
 55876             && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) 
 55877       ){
 55878         /* The list is being searched and this trunk page is the page
 55879         ** to allocate, regardless of whether it has leaves.
 55880         */
 55881         *pPgno = iTrunk;
 55882         *ppPage = pTrunk;
 55883         searchList = 0;
 55884         rc = sqlite3PagerWrite(pTrunk->pDbPage);
 55885         if( rc ){
 55886           goto end_allocate_page;
 55888         if( k==0 ){
 55889           if( !pPrevTrunk ){
 55890             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
 55891           }else{
 55892             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 55893             if( rc!=SQLITE_OK ){
 55894               goto end_allocate_page;
 55896             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
 55898         }else{
 55899           /* The trunk page is required by the caller but it contains 
 55900           ** pointers to free-list leaves. The first leaf becomes a trunk
 55901           ** page in this case.
 55902           */
 55903           MemPage *pNewTrunk;
 55904           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
 55905           if( iNewTrunk>mxPage ){ 
 55906             rc = SQLITE_CORRUPT_BKPT;
 55907             goto end_allocate_page;
 55909           testcase( iNewTrunk==mxPage );
 55910           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
 55911           if( rc!=SQLITE_OK ){
 55912             goto end_allocate_page;
 55914           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
 55915           if( rc!=SQLITE_OK ){
 55916             releasePage(pNewTrunk);
 55917             goto end_allocate_page;
 55919           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
 55920           put4byte(&pNewTrunk->aData[4], k-1);
 55921           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
 55922           releasePage(pNewTrunk);
 55923           if( !pPrevTrunk ){
 55924             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
 55925             put4byte(&pPage1->aData[32], iNewTrunk);
 55926           }else{
 55927             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
 55928             if( rc ){
 55929               goto end_allocate_page;
 55931             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
 55934         pTrunk = 0;
 55935         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 55936 #endif
 55937       }else if( k>0 ){
 55938         /* Extract a leaf from the trunk */
 55939         u32 closest;
 55940         Pgno iPage;
 55941         unsigned char *aData = pTrunk->aData;
 55942         if( nearby>0 ){
 55943           u32 i;
 55944           closest = 0;
 55945           if( eMode==BTALLOC_LE ){
 55946             for(i=0; i<k; i++){
 55947               iPage = get4byte(&aData[8+i*4]);
 55948               if( iPage<=nearby ){
 55949                 closest = i;
 55950                 break;
 55953           }else{
 55954             int dist;
 55955             dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
 55956             for(i=1; i<k; i++){
 55957               int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
 55958               if( d2<dist ){
 55959                 closest = i;
 55960                 dist = d2;
 55964         }else{
 55965           closest = 0;
 55968         iPage = get4byte(&aData[8+closest*4]);
 55969         testcase( iPage==mxPage );
 55970         if( iPage>mxPage ){
 55971           rc = SQLITE_CORRUPT_BKPT;
 55972           goto end_allocate_page;
 55974         testcase( iPage==mxPage );
 55975         if( !searchList 
 55976          || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) 
 55977         ){
 55978           int noContent;
 55979           *pPgno = iPage;
 55980           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
 55981                  ": %d more free pages\n",
 55982                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
 55983           rc = sqlite3PagerWrite(pTrunk->pDbPage);
 55984           if( rc ) goto end_allocate_page;
 55985           if( closest<k-1 ){
 55986             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
 55988           put4byte(&aData[4], k-1);
 55989           noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
 55990           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
 55991           if( rc==SQLITE_OK ){
 55992             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 55993             if( rc!=SQLITE_OK ){
 55994               releasePage(*ppPage);
 55997           searchList = 0;
 56000       releasePage(pPrevTrunk);
 56001       pPrevTrunk = 0;
 56002     }while( searchList );
 56003   }else{
 56004     /* There are no pages on the freelist, so append a new page to the
 56005     ** database image.
 56006     **
 56007     ** Normally, new pages allocated by this block can be requested from the
 56008     ** pager layer with the 'no-content' flag set. This prevents the pager
 56009     ** from trying to read the pages content from disk. However, if the
 56010     ** current transaction has already run one or more incremental-vacuum
 56011     ** steps, then the page we are about to allocate may contain content
 56012     ** that is required in the event of a rollback. In this case, do
 56013     ** not set the no-content flag. This causes the pager to load and journal
 56014     ** the current page content before overwriting it.
 56015     **
 56016     ** Note that the pager will not actually attempt to load or journal 
 56017     ** content for any page that really does lie past the end of the database
 56018     ** file on disk. So the effects of disabling the no-content optimization
 56019     ** here are confined to those pages that lie between the end of the
 56020     ** database image and the end of the database file.
 56021     */
 56022     int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
 56024     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 56025     if( rc ) return rc;
 56026     pBt->nPage++;
 56027     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
 56029 #ifndef SQLITE_OMIT_AUTOVACUUM
 56030     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
 56031       /* If *pPgno refers to a pointer-map page, allocate two new pages
 56032       ** at the end of the file instead of one. The first allocated page
 56033       ** becomes a new pointer-map page, the second is used by the caller.
 56034       */
 56035       MemPage *pPg = 0;
 56036       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
 56037       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
 56038       rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
 56039       if( rc==SQLITE_OK ){
 56040         rc = sqlite3PagerWrite(pPg->pDbPage);
 56041         releasePage(pPg);
 56043       if( rc ) return rc;
 56044       pBt->nPage++;
 56045       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
 56047 #endif
 56048     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
 56049     *pPgno = pBt->nPage;
 56051     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 56052     rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
 56053     if( rc ) return rc;
 56054     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 56055     if( rc!=SQLITE_OK ){
 56056       releasePage(*ppPage);
 56058     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
 56061   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
 56063 end_allocate_page:
 56064   releasePage(pTrunk);
 56065   releasePage(pPrevTrunk);
 56066   if( rc==SQLITE_OK ){
 56067     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
 56068       releasePage(*ppPage);
 56069       *ppPage = 0;
 56070       return SQLITE_CORRUPT_BKPT;
 56072     (*ppPage)->isInit = 0;
 56073   }else{
 56074     *ppPage = 0;
 56076   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
 56077   return rc;
 56080 /*
 56081 ** This function is used to add page iPage to the database file free-list. 
 56082 ** It is assumed that the page is not already a part of the free-list.
 56083 **
 56084 ** The value passed as the second argument to this function is optional.
 56085 ** If the caller happens to have a pointer to the MemPage object 
 56086 ** corresponding to page iPage handy, it may pass it as the second value. 
 56087 ** Otherwise, it may pass NULL.
 56088 **
 56089 ** If a pointer to a MemPage object is passed as the second argument,
 56090 ** its reference count is not altered by this function.
 56091 */
 56092 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
 56093   MemPage *pTrunk = 0;                /* Free-list trunk page */
 56094   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
 56095   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
 56096   MemPage *pPage;                     /* Page being freed. May be NULL. */
 56097   int rc;                             /* Return Code */
 56098   int nFree;                          /* Initial number of pages on free-list */
 56100   assert( sqlite3_mutex_held(pBt->mutex) );
 56101   assert( iPage>1 );
 56102   assert( !pMemPage || pMemPage->pgno==iPage );
 56104   if( pMemPage ){
 56105     pPage = pMemPage;
 56106     sqlite3PagerRef(pPage->pDbPage);
 56107   }else{
 56108     pPage = btreePageLookup(pBt, iPage);
 56111   /* Increment the free page count on pPage1 */
 56112   rc = sqlite3PagerWrite(pPage1->pDbPage);
 56113   if( rc ) goto freepage_out;
 56114   nFree = get4byte(&pPage1->aData[36]);
 56115   put4byte(&pPage1->aData[36], nFree+1);
 56117   if( pBt->btsFlags & BTS_SECURE_DELETE ){
 56118     /* If the secure_delete option is enabled, then
 56119     ** always fully overwrite deleted information with zeros.
 56120     */
 56121     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
 56122      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
 56123     ){
 56124       goto freepage_out;
 56126     memset(pPage->aData, 0, pPage->pBt->pageSize);
 56129   /* If the database supports auto-vacuum, write an entry in the pointer-map
 56130   ** to indicate that the page is free.
 56131   */
 56132   if( ISAUTOVACUUM ){
 56133     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
 56134     if( rc ) goto freepage_out;
 56137   /* Now manipulate the actual database free-list structure. There are two
 56138   ** possibilities. If the free-list is currently empty, or if the first
 56139   ** trunk page in the free-list is full, then this page will become a
 56140   ** new free-list trunk page. Otherwise, it will become a leaf of the
 56141   ** first trunk page in the current free-list. This block tests if it
 56142   ** is possible to add the page as a new free-list leaf.
 56143   */
 56144   if( nFree!=0 ){
 56145     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
 56147     iTrunk = get4byte(&pPage1->aData[32]);
 56148     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 56149     if( rc!=SQLITE_OK ){
 56150       goto freepage_out;
 56153     nLeaf = get4byte(&pTrunk->aData[4]);
 56154     assert( pBt->usableSize>32 );
 56155     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
 56156       rc = SQLITE_CORRUPT_BKPT;
 56157       goto freepage_out;
 56159     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
 56160       /* In this case there is room on the trunk page to insert the page
 56161       ** being freed as a new leaf.
 56162       **
 56163       ** Note that the trunk page is not really full until it contains
 56164       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
 56165       ** coded.  But due to a coding error in versions of SQLite prior to
 56166       ** 3.6.0, databases with freelist trunk pages holding more than
 56167       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
 56168       ** to maintain backwards compatibility with older versions of SQLite,
 56169       ** we will continue to restrict the number of entries to usableSize/4 - 8
 56170       ** for now.  At some point in the future (once everyone has upgraded
 56171       ** to 3.6.0 or later) we should consider fixing the conditional above
 56172       ** to read "usableSize/4-2" instead of "usableSize/4-8".
 56173       */
 56174       rc = sqlite3PagerWrite(pTrunk->pDbPage);
 56175       if( rc==SQLITE_OK ){
 56176         put4byte(&pTrunk->aData[4], nLeaf+1);
 56177         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
 56178         if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
 56179           sqlite3PagerDontWrite(pPage->pDbPage);
 56181         rc = btreeSetHasContent(pBt, iPage);
 56183       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
 56184       goto freepage_out;
 56188   /* If control flows to this point, then it was not possible to add the
 56189   ** the page being freed as a leaf page of the first trunk in the free-list.
 56190   ** Possibly because the free-list is empty, or possibly because the 
 56191   ** first trunk in the free-list is full. Either way, the page being freed
 56192   ** will become the new first trunk page in the free-list.
 56193   */
 56194   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
 56195     goto freepage_out;
 56197   rc = sqlite3PagerWrite(pPage->pDbPage);
 56198   if( rc!=SQLITE_OK ){
 56199     goto freepage_out;
 56201   put4byte(pPage->aData, iTrunk);
 56202   put4byte(&pPage->aData[4], 0);
 56203   put4byte(&pPage1->aData[32], iPage);
 56204   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
 56206 freepage_out:
 56207   if( pPage ){
 56208     pPage->isInit = 0;
 56210   releasePage(pPage);
 56211   releasePage(pTrunk);
 56212   return rc;
 56214 static void freePage(MemPage *pPage, int *pRC){
 56215   if( (*pRC)==SQLITE_OK ){
 56216     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
 56220 /*
 56221 ** Free any overflow pages associated with the given Cell.
 56222 */
 56223 static int clearCell(MemPage *pPage, unsigned char *pCell){
 56224   BtShared *pBt = pPage->pBt;
 56225   CellInfo info;
 56226   Pgno ovflPgno;
 56227   int rc;
 56228   int nOvfl;
 56229   u32 ovflPageSize;
 56231   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56232   btreeParseCellPtr(pPage, pCell, &info);
 56233   if( info.iOverflow==0 ){
 56234     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 56236   if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
 56237     return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
 56239   ovflPgno = get4byte(&pCell[info.iOverflow]);
 56240   assert( pBt->usableSize > 4 );
 56241   ovflPageSize = pBt->usableSize - 4;
 56242   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
 56243   assert( ovflPgno==0 || nOvfl>0 );
 56244   while( nOvfl-- ){
 56245     Pgno iNext = 0;
 56246     MemPage *pOvfl = 0;
 56247     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
 56248       /* 0 is not a legal page number and page 1 cannot be an 
 56249       ** overflow page. Therefore if ovflPgno<2 or past the end of the 
 56250       ** file the database must be corrupt. */
 56251       return SQLITE_CORRUPT_BKPT;
 56253     if( nOvfl ){
 56254       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
 56255       if( rc ) return rc;
 56258     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
 56259      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
 56260     ){
 56261       /* There is no reason any cursor should have an outstanding reference 
 56262       ** to an overflow page belonging to a cell that is being deleted/updated.
 56263       ** So if there exists more than one reference to this page, then it 
 56264       ** must not really be an overflow page and the database must be corrupt. 
 56265       ** It is helpful to detect this before calling freePage2(), as 
 56266       ** freePage2() may zero the page contents if secure-delete mode is
 56267       ** enabled. If this 'overflow' page happens to be a page that the
 56268       ** caller is iterating through or using in some other way, this
 56269       ** can be problematic.
 56270       */
 56271       rc = SQLITE_CORRUPT_BKPT;
 56272     }else{
 56273       rc = freePage2(pBt, pOvfl, ovflPgno);
 56276     if( pOvfl ){
 56277       sqlite3PagerUnref(pOvfl->pDbPage);
 56279     if( rc ) return rc;
 56280     ovflPgno = iNext;
 56282   return SQLITE_OK;
 56285 /*
 56286 ** Create the byte sequence used to represent a cell on page pPage
 56287 ** and write that byte sequence into pCell[].  Overflow pages are
 56288 ** allocated and filled in as necessary.  The calling procedure
 56289 ** is responsible for making sure sufficient space has been allocated
 56290 ** for pCell[].
 56291 **
 56292 ** Note that pCell does not necessary need to point to the pPage->aData
 56293 ** area.  pCell might point to some temporary storage.  The cell will
 56294 ** be constructed in this temporary area then copied into pPage->aData
 56295 ** later.
 56296 */
 56297 static int fillInCell(
 56298   MemPage *pPage,                /* The page that contains the cell */
 56299   unsigned char *pCell,          /* Complete text of the cell */
 56300   const void *pKey, i64 nKey,    /* The key */
 56301   const void *pData,int nData,   /* The data */
 56302   int nZero,                     /* Extra zero bytes to append to pData */
 56303   int *pnSize                    /* Write cell size here */
 56304 ){
 56305   int nPayload;
 56306   const u8 *pSrc;
 56307   int nSrc, n, rc;
 56308   int spaceLeft;
 56309   MemPage *pOvfl = 0;
 56310   MemPage *pToRelease = 0;
 56311   unsigned char *pPrior;
 56312   unsigned char *pPayload;
 56313   BtShared *pBt = pPage->pBt;
 56314   Pgno pgnoOvfl = 0;
 56315   int nHeader;
 56316   CellInfo info;
 56318   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56320   /* pPage is not necessarily writeable since pCell might be auxiliary
 56321   ** buffer space that is separate from the pPage buffer area */
 56322   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
 56323             || sqlite3PagerIswriteable(pPage->pDbPage) );
 56325   /* Fill in the header. */
 56326   nHeader = 0;
 56327   if( !pPage->leaf ){
 56328     nHeader += 4;
 56330   if( pPage->hasData ){
 56331     nHeader += putVarint32(&pCell[nHeader], nData+nZero);
 56332   }else{
 56333     nData = nZero = 0;
 56335   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
 56336   btreeParseCellPtr(pPage, pCell, &info);
 56337   assert( info.nHeader==nHeader );
 56338   assert( info.nKey==nKey );
 56339   assert( info.nData==(u32)(nData+nZero) );
 56341   /* Fill in the payload */
 56342   nPayload = nData + nZero;
 56343   if( pPage->intKey ){
 56344     pSrc = pData;
 56345     nSrc = nData;
 56346     nData = 0;
 56347   }else{ 
 56348     if( NEVER(nKey>0x7fffffff || pKey==0) ){
 56349       return SQLITE_CORRUPT_BKPT;
 56351     nPayload += (int)nKey;
 56352     pSrc = pKey;
 56353     nSrc = (int)nKey;
 56355   *pnSize = info.nSize;
 56356   spaceLeft = info.nLocal;
 56357   pPayload = &pCell[nHeader];
 56358   pPrior = &pCell[info.iOverflow];
 56360   while( nPayload>0 ){
 56361     if( spaceLeft==0 ){
 56362 #ifndef SQLITE_OMIT_AUTOVACUUM
 56363       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
 56364       if( pBt->autoVacuum ){
 56365         do{
 56366           pgnoOvfl++;
 56367         } while( 
 56368           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
 56369         );
 56371 #endif
 56372       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
 56373 #ifndef SQLITE_OMIT_AUTOVACUUM
 56374       /* If the database supports auto-vacuum, and the second or subsequent
 56375       ** overflow page is being allocated, add an entry to the pointer-map
 56376       ** for that page now. 
 56377       **
 56378       ** If this is the first overflow page, then write a partial entry 
 56379       ** to the pointer-map. If we write nothing to this pointer-map slot,
 56380       ** then the optimistic overflow chain processing in clearCell()
 56381       ** may misinterpret the uninitialized values and delete the
 56382       ** wrong pages from the database.
 56383       */
 56384       if( pBt->autoVacuum && rc==SQLITE_OK ){
 56385         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
 56386         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
 56387         if( rc ){
 56388           releasePage(pOvfl);
 56391 #endif
 56392       if( rc ){
 56393         releasePage(pToRelease);
 56394         return rc;
 56397       /* If pToRelease is not zero than pPrior points into the data area
 56398       ** of pToRelease.  Make sure pToRelease is still writeable. */
 56399       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 56401       /* If pPrior is part of the data area of pPage, then make sure pPage
 56402       ** is still writeable */
 56403       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
 56404             || sqlite3PagerIswriteable(pPage->pDbPage) );
 56406       put4byte(pPrior, pgnoOvfl);
 56407       releasePage(pToRelease);
 56408       pToRelease = pOvfl;
 56409       pPrior = pOvfl->aData;
 56410       put4byte(pPrior, 0);
 56411       pPayload = &pOvfl->aData[4];
 56412       spaceLeft = pBt->usableSize - 4;
 56414     n = nPayload;
 56415     if( n>spaceLeft ) n = spaceLeft;
 56417     /* If pToRelease is not zero than pPayload points into the data area
 56418     ** of pToRelease.  Make sure pToRelease is still writeable. */
 56419     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
 56421     /* If pPayload is part of the data area of pPage, then make sure pPage
 56422     ** is still writeable */
 56423     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
 56424             || sqlite3PagerIswriteable(pPage->pDbPage) );
 56426     if( nSrc>0 ){
 56427       if( n>nSrc ) n = nSrc;
 56428       assert( pSrc );
 56429       memcpy(pPayload, pSrc, n);
 56430     }else{
 56431       memset(pPayload, 0, n);
 56433     nPayload -= n;
 56434     pPayload += n;
 56435     pSrc += n;
 56436     nSrc -= n;
 56437     spaceLeft -= n;
 56438     if( nSrc==0 ){
 56439       nSrc = nData;
 56440       pSrc = pData;
 56443   releasePage(pToRelease);
 56444   return SQLITE_OK;
 56447 /*
 56448 ** Remove the i-th cell from pPage.  This routine effects pPage only.
 56449 ** The cell content is not freed or deallocated.  It is assumed that
 56450 ** the cell content has been copied someplace else.  This routine just
 56451 ** removes the reference to the cell from pPage.
 56452 **
 56453 ** "sz" must be the number of bytes in the cell.
 56454 */
 56455 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
 56456   u32 pc;         /* Offset to cell content of cell being deleted */
 56457   u8 *data;       /* pPage->aData */
 56458   u8 *ptr;        /* Used to move bytes around within data[] */
 56459   int rc;         /* The return code */
 56460   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
 56462   if( *pRC ) return;
 56464   assert( idx>=0 && idx<pPage->nCell );
 56465   assert( sz==cellSize(pPage, idx) );
 56466   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 56467   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56468   data = pPage->aData;
 56469   ptr = &pPage->aCellIdx[2*idx];
 56470   pc = get2byte(ptr);
 56471   hdr = pPage->hdrOffset;
 56472   testcase( pc==get2byte(&data[hdr+5]) );
 56473   testcase( pc+sz==pPage->pBt->usableSize );
 56474   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
 56475     *pRC = SQLITE_CORRUPT_BKPT;
 56476     return;
 56478   rc = freeSpace(pPage, pc, sz);
 56479   if( rc ){
 56480     *pRC = rc;
 56481     return;
 56483   pPage->nCell--;
 56484   memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
 56485   put2byte(&data[hdr+3], pPage->nCell);
 56486   pPage->nFree += 2;
 56489 /*
 56490 ** Insert a new cell on pPage at cell index "i".  pCell points to the
 56491 ** content of the cell.
 56492 **
 56493 ** If the cell content will fit on the page, then put it there.  If it
 56494 ** will not fit, then make a copy of the cell content into pTemp if
 56495 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
 56496 ** in pPage->apOvfl[] and make it point to the cell content (either
 56497 ** in pTemp or the original pCell) and also record its index. 
 56498 ** Allocating a new entry in pPage->aCell[] implies that 
 56499 ** pPage->nOverflow is incremented.
 56500 **
 56501 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
 56502 ** cell. The caller will overwrite them after this function returns. If
 56503 ** nSkip is non-zero, then pCell may not point to an invalid memory location 
 56504 ** (but pCell+nSkip is always valid).
 56505 */
 56506 static void insertCell(
 56507   MemPage *pPage,   /* Page into which we are copying */
 56508   int i,            /* New cell becomes the i-th cell of the page */
 56509   u8 *pCell,        /* Content of the new cell */
 56510   int sz,           /* Bytes of content in pCell */
 56511   u8 *pTemp,        /* Temp storage space for pCell, if needed */
 56512   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
 56513   int *pRC          /* Read and write return code from here */
 56514 ){
 56515   int idx = 0;      /* Where to write new cell content in data[] */
 56516   int j;            /* Loop counter */
 56517   int end;          /* First byte past the last cell pointer in data[] */
 56518   int ins;          /* Index in data[] where new cell pointer is inserted */
 56519   int cellOffset;   /* Address of first cell pointer in data[] */
 56520   u8 *data;         /* The content of the whole page */
 56521   int nSkip = (iChild ? 4 : 0);
 56523   if( *pRC ) return;
 56525   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
 56526   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
 56527   assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
 56528   assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
 56529   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56530   /* The cell should normally be sized correctly.  However, when moving a
 56531   ** malformed cell from a leaf page to an interior page, if the cell size
 56532   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
 56533   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
 56534   ** the term after the || in the following assert(). */
 56535   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
 56536   if( pPage->nOverflow || sz+2>pPage->nFree ){
 56537     if( pTemp ){
 56538       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
 56539       pCell = pTemp;
 56541     if( iChild ){
 56542       put4byte(pCell, iChild);
 56544     j = pPage->nOverflow++;
 56545     assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
 56546     pPage->apOvfl[j] = pCell;
 56547     pPage->aiOvfl[j] = (u16)i;
 56548   }else{
 56549     int rc = sqlite3PagerWrite(pPage->pDbPage);
 56550     if( rc!=SQLITE_OK ){
 56551       *pRC = rc;
 56552       return;
 56554     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 56555     data = pPage->aData;
 56556     cellOffset = pPage->cellOffset;
 56557     end = cellOffset + 2*pPage->nCell;
 56558     ins = cellOffset + 2*i;
 56559     rc = allocateSpace(pPage, sz, &idx);
 56560     if( rc ){ *pRC = rc; return; }
 56561     /* The allocateSpace() routine guarantees the following two properties
 56562     ** if it returns success */
 56563     assert( idx >= end+2 );
 56564     assert( idx+sz <= (int)pPage->pBt->usableSize );
 56565     pPage->nCell++;
 56566     pPage->nFree -= (u16)(2 + sz);
 56567     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
 56568     if( iChild ){
 56569       put4byte(&data[idx], iChild);
 56571     memmove(&data[ins+2], &data[ins], end-ins);
 56572     put2byte(&data[ins], idx);
 56573     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
 56574 #ifndef SQLITE_OMIT_AUTOVACUUM
 56575     if( pPage->pBt->autoVacuum ){
 56576       /* The cell may contain a pointer to an overflow page. If so, write
 56577       ** the entry for the overflow page into the pointer map.
 56578       */
 56579       ptrmapPutOvflPtr(pPage, pCell, pRC);
 56581 #endif
 56585 /*
 56586 ** Add a list of cells to a page.  The page should be initially empty.
 56587 ** The cells are guaranteed to fit on the page.
 56588 */
 56589 static void assemblePage(
 56590   MemPage *pPage,   /* The page to be assemblied */
 56591   int nCell,        /* The number of cells to add to this page */
 56592   u8 **apCell,      /* Pointers to cell bodies */
 56593   u16 *aSize        /* Sizes of the cells */
 56594 ){
 56595   int i;            /* Loop counter */
 56596   u8 *pCellptr;     /* Address of next cell pointer */
 56597   int cellbody;     /* Address of next cell body */
 56598   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
 56599   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
 56600   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
 56602   assert( pPage->nOverflow==0 );
 56603   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56604   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
 56605             && (int)MX_CELL(pPage->pBt)<=10921);
 56606   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 56608   /* Check that the page has just been zeroed by zeroPage() */
 56609   assert( pPage->nCell==0 );
 56610   assert( get2byteNotZero(&data[hdr+5])==nUsable );
 56612   pCellptr = &pPage->aCellIdx[nCell*2];
 56613   cellbody = nUsable;
 56614   for(i=nCell-1; i>=0; i--){
 56615     u16 sz = aSize[i];
 56616     pCellptr -= 2;
 56617     cellbody -= sz;
 56618     put2byte(pCellptr, cellbody);
 56619     memcpy(&data[cellbody], apCell[i], sz);
 56621   put2byte(&data[hdr+3], nCell);
 56622   put2byte(&data[hdr+5], cellbody);
 56623   pPage->nFree -= (nCell*2 + nUsable - cellbody);
 56624   pPage->nCell = (u16)nCell;
 56627 /*
 56628 ** The following parameters determine how many adjacent pages get involved
 56629 ** in a balancing operation.  NN is the number of neighbors on either side
 56630 ** of the page that participate in the balancing operation.  NB is the
 56631 ** total number of pages that participate, including the target page and
 56632 ** NN neighbors on either side.
 56633 **
 56634 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
 56635 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
 56636 ** in exchange for a larger degradation in INSERT and UPDATE performance.
 56637 ** The value of NN appears to give the best results overall.
 56638 */
 56639 #define NN 1             /* Number of neighbors on either side of pPage */
 56640 #define NB (NN*2+1)      /* Total pages involved in the balance */
 56643 #ifndef SQLITE_OMIT_QUICKBALANCE
 56644 /*
 56645 ** This version of balance() handles the common special case where
 56646 ** a new entry is being inserted on the extreme right-end of the
 56647 ** tree, in other words, when the new entry will become the largest
 56648 ** entry in the tree.
 56649 **
 56650 ** Instead of trying to balance the 3 right-most leaf pages, just add
 56651 ** a new page to the right-hand side and put the one new entry in
 56652 ** that page.  This leaves the right side of the tree somewhat
 56653 ** unbalanced.  But odds are that we will be inserting new entries
 56654 ** at the end soon afterwards so the nearly empty page will quickly
 56655 ** fill up.  On average.
 56656 **
 56657 ** pPage is the leaf page which is the right-most page in the tree.
 56658 ** pParent is its parent.  pPage must have a single overflow entry
 56659 ** which is also the right-most entry on the page.
 56660 **
 56661 ** The pSpace buffer is used to store a temporary copy of the divider
 56662 ** cell that will be inserted into pParent. Such a cell consists of a 4
 56663 ** byte page number followed by a variable length integer. In other
 56664 ** words, at most 13 bytes. Hence the pSpace buffer must be at
 56665 ** least 13 bytes in size.
 56666 */
 56667 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
 56668   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
 56669   MemPage *pNew;                       /* Newly allocated page */
 56670   int rc;                              /* Return Code */
 56671   Pgno pgnoNew;                        /* Page number of pNew */
 56673   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 56674   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 56675   assert( pPage->nOverflow==1 );
 56677   /* This error condition is now caught prior to reaching this function */
 56678   if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
 56680   /* Allocate a new page. This page will become the right-sibling of 
 56681   ** pPage. Make the parent page writable, so that the new divider cell
 56682   ** may be inserted. If both these operations are successful, proceed.
 56683   */
 56684   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
 56686   if( rc==SQLITE_OK ){
 56688     u8 *pOut = &pSpace[4];
 56689     u8 *pCell = pPage->apOvfl[0];
 56690     u16 szCell = cellSizePtr(pPage, pCell);
 56691     u8 *pStop;
 56693     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
 56694     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
 56695     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
 56696     assemblePage(pNew, 1, &pCell, &szCell);
 56698     /* If this is an auto-vacuum database, update the pointer map
 56699     ** with entries for the new page, and any pointer from the 
 56700     ** cell on the page to an overflow page. If either of these
 56701     ** operations fails, the return code is set, but the contents
 56702     ** of the parent page are still manipulated by thh code below.
 56703     ** That is Ok, at this point the parent page is guaranteed to
 56704     ** be marked as dirty. Returning an error code will cause a
 56705     ** rollback, undoing any changes made to the parent page.
 56706     */
 56707     if( ISAUTOVACUUM ){
 56708       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
 56709       if( szCell>pNew->minLocal ){
 56710         ptrmapPutOvflPtr(pNew, pCell, &rc);
 56714     /* Create a divider cell to insert into pParent. The divider cell
 56715     ** consists of a 4-byte page number (the page number of pPage) and
 56716     ** a variable length key value (which must be the same value as the
 56717     ** largest key on pPage).
 56718     **
 56719     ** To find the largest key value on pPage, first find the right-most 
 56720     ** cell on pPage. The first two fields of this cell are the 
 56721     ** record-length (a variable length integer at most 32-bits in size)
 56722     ** and the key value (a variable length integer, may have any value).
 56723     ** The first of the while(...) loops below skips over the record-length
 56724     ** field. The second while(...) loop copies the key value from the
 56725     ** cell on pPage into the pSpace buffer.
 56726     */
 56727     pCell = findCell(pPage, pPage->nCell-1);
 56728     pStop = &pCell[9];
 56729     while( (*(pCell++)&0x80) && pCell<pStop );
 56730     pStop = &pCell[9];
 56731     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
 56733     /* Insert the new divider cell into pParent. */
 56734     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
 56735                0, pPage->pgno, &rc);
 56737     /* Set the right-child pointer of pParent to point to the new page. */
 56738     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
 56740     /* Release the reference to the new page. */
 56741     releasePage(pNew);
 56744   return rc;
 56746 #endif /* SQLITE_OMIT_QUICKBALANCE */
 56748 #if 0
 56749 /*
 56750 ** This function does not contribute anything to the operation of SQLite.
 56751 ** it is sometimes activated temporarily while debugging code responsible 
 56752 ** for setting pointer-map entries.
 56753 */
 56754 static int ptrmapCheckPages(MemPage **apPage, int nPage){
 56755   int i, j;
 56756   for(i=0; i<nPage; i++){
 56757     Pgno n;
 56758     u8 e;
 56759     MemPage *pPage = apPage[i];
 56760     BtShared *pBt = pPage->pBt;
 56761     assert( pPage->isInit );
 56763     for(j=0; j<pPage->nCell; j++){
 56764       CellInfo info;
 56765       u8 *z;
 56767       z = findCell(pPage, j);
 56768       btreeParseCellPtr(pPage, z, &info);
 56769       if( info.iOverflow ){
 56770         Pgno ovfl = get4byte(&z[info.iOverflow]);
 56771         ptrmapGet(pBt, ovfl, &e, &n);
 56772         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
 56774       if( !pPage->leaf ){
 56775         Pgno child = get4byte(z);
 56776         ptrmapGet(pBt, child, &e, &n);
 56777         assert( n==pPage->pgno && e==PTRMAP_BTREE );
 56780     if( !pPage->leaf ){
 56781       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 56782       ptrmapGet(pBt, child, &e, &n);
 56783       assert( n==pPage->pgno && e==PTRMAP_BTREE );
 56786   return 1;
 56788 #endif
 56790 /*
 56791 ** This function is used to copy the contents of the b-tree node stored 
 56792 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
 56793 ** the pointer-map entries for each child page are updated so that the
 56794 ** parent page stored in the pointer map is page pTo. If pFrom contained
 56795 ** any cells with overflow page pointers, then the corresponding pointer
 56796 ** map entries are also updated so that the parent page is page pTo.
 56797 **
 56798 ** If pFrom is currently carrying any overflow cells (entries in the
 56799 ** MemPage.apOvfl[] array), they are not copied to pTo. 
 56800 **
 56801 ** Before returning, page pTo is reinitialized using btreeInitPage().
 56802 **
 56803 ** The performance of this function is not critical. It is only used by 
 56804 ** the balance_shallower() and balance_deeper() procedures, neither of
 56805 ** which are called often under normal circumstances.
 56806 */
 56807 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
 56808   if( (*pRC)==SQLITE_OK ){
 56809     BtShared * const pBt = pFrom->pBt;
 56810     u8 * const aFrom = pFrom->aData;
 56811     u8 * const aTo = pTo->aData;
 56812     int const iFromHdr = pFrom->hdrOffset;
 56813     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
 56814     int rc;
 56815     int iData;
 56818     assert( pFrom->isInit );
 56819     assert( pFrom->nFree>=iToHdr );
 56820     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
 56822     /* Copy the b-tree node content from page pFrom to page pTo. */
 56823     iData = get2byte(&aFrom[iFromHdr+5]);
 56824     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
 56825     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
 56827     /* Reinitialize page pTo so that the contents of the MemPage structure
 56828     ** match the new data. The initialization of pTo can actually fail under
 56829     ** fairly obscure circumstances, even though it is a copy of initialized 
 56830     ** page pFrom.
 56831     */
 56832     pTo->isInit = 0;
 56833     rc = btreeInitPage(pTo);
 56834     if( rc!=SQLITE_OK ){
 56835       *pRC = rc;
 56836       return;
 56839     /* If this is an auto-vacuum database, update the pointer-map entries
 56840     ** for any b-tree or overflow pages that pTo now contains the pointers to.
 56841     */
 56842     if( ISAUTOVACUUM ){
 56843       *pRC = setChildPtrmaps(pTo);
 56848 /*
 56849 ** This routine redistributes cells on the iParentIdx'th child of pParent
 56850 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
 56851 ** same amount of free space. Usually a single sibling on either side of the
 56852 ** page are used in the balancing, though both siblings might come from one
 56853 ** side if the page is the first or last child of its parent. If the page 
 56854 ** has fewer than 2 siblings (something which can only happen if the page
 56855 ** is a root page or a child of a root page) then all available siblings
 56856 ** participate in the balancing.
 56857 **
 56858 ** The number of siblings of the page might be increased or decreased by 
 56859 ** one or two in an effort to keep pages nearly full but not over full. 
 56860 **
 56861 ** Note that when this routine is called, some of the cells on the page
 56862 ** might not actually be stored in MemPage.aData[]. This can happen
 56863 ** if the page is overfull. This routine ensures that all cells allocated
 56864 ** to the page and its siblings fit into MemPage.aData[] before returning.
 56865 **
 56866 ** In the course of balancing the page and its siblings, cells may be
 56867 ** inserted into or removed from the parent page (pParent). Doing so
 56868 ** may cause the parent page to become overfull or underfull. If this
 56869 ** happens, it is the responsibility of the caller to invoke the correct
 56870 ** balancing routine to fix this problem (see the balance() routine). 
 56871 **
 56872 ** If this routine fails for any reason, it might leave the database
 56873 ** in a corrupted state. So if this routine fails, the database should
 56874 ** be rolled back.
 56875 **
 56876 ** The third argument to this function, aOvflSpace, is a pointer to a
 56877 ** buffer big enough to hold one page. If while inserting cells into the parent
 56878 ** page (pParent) the parent page becomes overfull, this buffer is
 56879 ** used to store the parent's overflow cells. Because this function inserts
 56880 ** a maximum of four divider cells into the parent page, and the maximum
 56881 ** size of a cell stored within an internal node is always less than 1/4
 56882 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
 56883 ** enough for all overflow cells.
 56884 **
 56885 ** If aOvflSpace is set to a null pointer, this function returns 
 56886 ** SQLITE_NOMEM.
 56887 */
 56888 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 56889 #pragma optimize("", off)
 56890 #endif
 56891 static int balance_nonroot(
 56892   MemPage *pParent,               /* Parent page of siblings being balanced */
 56893   int iParentIdx,                 /* Index of "the page" in pParent */
 56894   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
 56895   int isRoot,                     /* True if pParent is a root-page */
 56896   int bBulk                       /* True if this call is part of a bulk load */
 56897 ){
 56898   BtShared *pBt;               /* The whole database */
 56899   int nCell = 0;               /* Number of cells in apCell[] */
 56900   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
 56901   int nNew = 0;                /* Number of pages in apNew[] */
 56902   int nOld;                    /* Number of pages in apOld[] */
 56903   int i, j, k;                 /* Loop counters */
 56904   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
 56905   int rc = SQLITE_OK;          /* The return code */
 56906   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
 56907   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
 56908   int usableSpace;             /* Bytes in pPage beyond the header */
 56909   int pageFlags;               /* Value of pPage->aData[0] */
 56910   int subtotal;                /* Subtotal of bytes in cells on one page */
 56911   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
 56912   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
 56913   int szScratch;               /* Size of scratch memory requested */
 56914   MemPage *apOld[NB];          /* pPage and up to two siblings */
 56915   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
 56916   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
 56917   u8 *pRight;                  /* Location in parent of right-sibling pointer */
 56918   u8 *apDiv[NB-1];             /* Divider cells in pParent */
 56919   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
 56920   int szNew[NB+2];             /* Combined size of cells place on i-th page */
 56921   u8 **apCell = 0;             /* All cells begin balanced */
 56922   u16 *szCell;                 /* Local size of all cells in apCell[] */
 56923   u8 *aSpace1;                 /* Space for copies of dividers cells */
 56924   Pgno pgno;                   /* Temp var to store a page number in */
 56926   pBt = pParent->pBt;
 56927   assert( sqlite3_mutex_held(pBt->mutex) );
 56928   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 56930 #if 0
 56931   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
 56932 #endif
 56934   /* At this point pParent may have at most one overflow cell. And if
 56935   ** this overflow cell is present, it must be the cell with 
 56936   ** index iParentIdx. This scenario comes about when this function
 56937   ** is called (indirectly) from sqlite3BtreeDelete().
 56938   */
 56939   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
 56940   assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
 56942   if( !aOvflSpace ){
 56943     return SQLITE_NOMEM;
 56946   /* Find the sibling pages to balance. Also locate the cells in pParent 
 56947   ** that divide the siblings. An attempt is made to find NN siblings on 
 56948   ** either side of pPage. More siblings are taken from one side, however, 
 56949   ** if there are fewer than NN siblings on the other side. If pParent
 56950   ** has NB or fewer children then all children of pParent are taken.  
 56951   **
 56952   ** This loop also drops the divider cells from the parent page. This
 56953   ** way, the remainder of the function does not have to deal with any
 56954   ** overflow cells in the parent page, since if any existed they will
 56955   ** have already been removed.
 56956   */
 56957   i = pParent->nOverflow + pParent->nCell;
 56958   if( i<2 ){
 56959     nxDiv = 0;
 56960   }else{
 56961     assert( bBulk==0 || bBulk==1 );
 56962     if( iParentIdx==0 ){                 
 56963       nxDiv = 0;
 56964     }else if( iParentIdx==i ){
 56965       nxDiv = i-2+bBulk;
 56966     }else{
 56967       assert( bBulk==0 );
 56968       nxDiv = iParentIdx-1;
 56970     i = 2-bBulk;
 56972   nOld = i+1;
 56973   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
 56974     pRight = &pParent->aData[pParent->hdrOffset+8];
 56975   }else{
 56976     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
 56978   pgno = get4byte(pRight);
 56979   while( 1 ){
 56980     rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
 56981     if( rc ){
 56982       memset(apOld, 0, (i+1)*sizeof(MemPage*));
 56983       goto balance_cleanup;
 56985     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
 56986     if( (i--)==0 ) break;
 56988     if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
 56989       apDiv[i] = pParent->apOvfl[0];
 56990       pgno = get4byte(apDiv[i]);
 56991       szNew[i] = cellSizePtr(pParent, apDiv[i]);
 56992       pParent->nOverflow = 0;
 56993     }else{
 56994       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
 56995       pgno = get4byte(apDiv[i]);
 56996       szNew[i] = cellSizePtr(pParent, apDiv[i]);
 56998       /* Drop the cell from the parent page. apDiv[i] still points to
 56999       ** the cell within the parent, even though it has been dropped.
 57000       ** This is safe because dropping a cell only overwrites the first
 57001       ** four bytes of it, and this function does not need the first
 57002       ** four bytes of the divider cell. So the pointer is safe to use
 57003       ** later on.  
 57004       **
 57005       ** But not if we are in secure-delete mode. In secure-delete mode,
 57006       ** the dropCell() routine will overwrite the entire cell with zeroes.
 57007       ** In this case, temporarily copy the cell into the aOvflSpace[]
 57008       ** buffer. It will be copied out again as soon as the aSpace[] buffer
 57009       ** is allocated.  */
 57010       if( pBt->btsFlags & BTS_SECURE_DELETE ){
 57011         int iOff;
 57013         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
 57014         if( (iOff+szNew[i])>(int)pBt->usableSize ){
 57015           rc = SQLITE_CORRUPT_BKPT;
 57016           memset(apOld, 0, (i+1)*sizeof(MemPage*));
 57017           goto balance_cleanup;
 57018         }else{
 57019           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
 57020           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
 57023       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
 57027   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
 57028   ** alignment */
 57029   nMaxCells = (nMaxCells + 3)&~3;
 57031   /*
 57032   ** Allocate space for memory structures
 57033   */
 57034   k = pBt->pageSize + ROUND8(sizeof(MemPage));
 57035   szScratch =
 57036        nMaxCells*sizeof(u8*)                       /* apCell */
 57037      + nMaxCells*sizeof(u16)                       /* szCell */
 57038      + pBt->pageSize                               /* aSpace1 */
 57039      + k*nOld;                                     /* Page copies (apCopy) */
 57040   apCell = sqlite3ScratchMalloc( szScratch ); 
 57041   if( apCell==0 ){
 57042     rc = SQLITE_NOMEM;
 57043     goto balance_cleanup;
 57045   szCell = (u16*)&apCell[nMaxCells];
 57046   aSpace1 = (u8*)&szCell[nMaxCells];
 57047   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
 57049   /*
 57050   ** Load pointers to all cells on sibling pages and the divider cells
 57051   ** into the local apCell[] array.  Make copies of the divider cells
 57052   ** into space obtained from aSpace1[] and remove the divider cells
 57053   ** from pParent.
 57054   **
 57055   ** If the siblings are on leaf pages, then the child pointers of the
 57056   ** divider cells are stripped from the cells before they are copied
 57057   ** into aSpace1[].  In this way, all cells in apCell[] are without
 57058   ** child pointers.  If siblings are not leaves, then all cell in
 57059   ** apCell[] include child pointers.  Either way, all cells in apCell[]
 57060   ** are alike.
 57061   **
 57062   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
 57063   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
 57064   */
 57065   leafCorrection = apOld[0]->leaf*4;
 57066   leafData = apOld[0]->hasData;
 57067   for(i=0; i<nOld; i++){
 57068     int limit;
 57070     /* Before doing anything else, take a copy of the i'th original sibling
 57071     ** The rest of this function will use data from the copies rather
 57072     ** that the original pages since the original pages will be in the
 57073     ** process of being overwritten.  */
 57074     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
 57075     memcpy(pOld, apOld[i], sizeof(MemPage));
 57076     pOld->aData = (void*)&pOld[1];
 57077     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
 57079     limit = pOld->nCell+pOld->nOverflow;
 57080     if( pOld->nOverflow>0 ){
 57081       for(j=0; j<limit; j++){
 57082         assert( nCell<nMaxCells );
 57083         apCell[nCell] = findOverflowCell(pOld, j);
 57084         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 57085         nCell++;
 57087     }else{
 57088       u8 *aData = pOld->aData;
 57089       u16 maskPage = pOld->maskPage;
 57090       u16 cellOffset = pOld->cellOffset;
 57091       for(j=0; j<limit; j++){
 57092         assert( nCell<nMaxCells );
 57093         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
 57094         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
 57095         nCell++;
 57098     if( i<nOld-1 && !leafData){
 57099       u16 sz = (u16)szNew[i];
 57100       u8 *pTemp;
 57101       assert( nCell<nMaxCells );
 57102       szCell[nCell] = sz;
 57103       pTemp = &aSpace1[iSpace1];
 57104       iSpace1 += sz;
 57105       assert( sz<=pBt->maxLocal+23 );
 57106       assert( iSpace1 <= (int)pBt->pageSize );
 57107       memcpy(pTemp, apDiv[i], sz);
 57108       apCell[nCell] = pTemp+leafCorrection;
 57109       assert( leafCorrection==0 || leafCorrection==4 );
 57110       szCell[nCell] = szCell[nCell] - leafCorrection;
 57111       if( !pOld->leaf ){
 57112         assert( leafCorrection==0 );
 57113         assert( pOld->hdrOffset==0 );
 57114         /* The right pointer of the child page pOld becomes the left
 57115         ** pointer of the divider cell */
 57116         memcpy(apCell[nCell], &pOld->aData[8], 4);
 57117       }else{
 57118         assert( leafCorrection==4 );
 57119         if( szCell[nCell]<4 ){
 57120           /* Do not allow any cells smaller than 4 bytes. */
 57121           szCell[nCell] = 4;
 57124       nCell++;
 57128   /*
 57129   ** Figure out the number of pages needed to hold all nCell cells.
 57130   ** Store this number in "k".  Also compute szNew[] which is the total
 57131   ** size of all cells on the i-th page and cntNew[] which is the index
 57132   ** in apCell[] of the cell that divides page i from page i+1.  
 57133   ** cntNew[k] should equal nCell.
 57134   **
 57135   ** Values computed by this block:
 57136   **
 57137   **           k: The total number of sibling pages
 57138   **    szNew[i]: Spaced used on the i-th sibling page.
 57139   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
 57140   **              the right of the i-th sibling page.
 57141   ** usableSpace: Number of bytes of space available on each sibling.
 57142   ** 
 57143   */
 57144   usableSpace = pBt->usableSize - 12 + leafCorrection;
 57145   for(subtotal=k=i=0; i<nCell; i++){
 57146     assert( i<nMaxCells );
 57147     subtotal += szCell[i] + 2;
 57148     if( subtotal > usableSpace ){
 57149       szNew[k] = subtotal - szCell[i];
 57150       cntNew[k] = i;
 57151       if( leafData ){ i--; }
 57152       subtotal = 0;
 57153       k++;
 57154       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
 57157   szNew[k] = subtotal;
 57158   cntNew[k] = nCell;
 57159   k++;
 57161   /*
 57162   ** The packing computed by the previous block is biased toward the siblings
 57163   ** on the left side.  The left siblings are always nearly full, while the
 57164   ** right-most sibling might be nearly empty.  This block of code attempts
 57165   ** to adjust the packing of siblings to get a better balance.
 57166   **
 57167   ** This adjustment is more than an optimization.  The packing above might
 57168   ** be so out of balance as to be illegal.  For example, the right-most
 57169   ** sibling might be completely empty.  This adjustment is not optional.
 57170   */
 57171   for(i=k-1; i>0; i--){
 57172     int szRight = szNew[i];  /* Size of sibling on the right */
 57173     int szLeft = szNew[i-1]; /* Size of sibling on the left */
 57174     int r;              /* Index of right-most cell in left sibling */
 57175     int d;              /* Index of first cell to the left of right sibling */
 57177     r = cntNew[i-1] - 1;
 57178     d = r + 1 - leafData;
 57179     assert( d<nMaxCells );
 57180     assert( r<nMaxCells );
 57181     while( szRight==0 
 57182        || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
 57183     ){
 57184       szRight += szCell[d] + 2;
 57185       szLeft -= szCell[r] + 2;
 57186       cntNew[i-1]--;
 57187       r = cntNew[i-1] - 1;
 57188       d = r + 1 - leafData;
 57190     szNew[i] = szRight;
 57191     szNew[i-1] = szLeft;
 57194   /* Either we found one or more cells (cntnew[0])>0) or pPage is
 57195   ** a virtual root page.  A virtual root page is when the real root
 57196   ** page is page 1 and we are the only child of that page.
 57197   **
 57198   ** UPDATE:  The assert() below is not necessarily true if the database
 57199   ** file is corrupt.  The corruption will be detected and reported later
 57200   ** in this procedure so there is no need to act upon it now.
 57201   */
 57202 #if 0
 57203   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
 57204 #endif
 57206   TRACE(("BALANCE: old: %d %d %d  ",
 57207     apOld[0]->pgno, 
 57208     nOld>=2 ? apOld[1]->pgno : 0,
 57209     nOld>=3 ? apOld[2]->pgno : 0
 57210   ));
 57212   /*
 57213   ** Allocate k new pages.  Reuse old pages where possible.
 57214   */
 57215   if( apOld[0]->pgno<=1 ){
 57216     rc = SQLITE_CORRUPT_BKPT;
 57217     goto balance_cleanup;
 57219   pageFlags = apOld[0]->aData[0];
 57220   for(i=0; i<k; i++){
 57221     MemPage *pNew;
 57222     if( i<nOld ){
 57223       pNew = apNew[i] = apOld[i];
 57224       apOld[i] = 0;
 57225       rc = sqlite3PagerWrite(pNew->pDbPage);
 57226       nNew++;
 57227       if( rc ) goto balance_cleanup;
 57228     }else{
 57229       assert( i>0 );
 57230       rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
 57231       if( rc ) goto balance_cleanup;
 57232       apNew[i] = pNew;
 57233       nNew++;
 57235       /* Set the pointer-map entry for the new sibling page. */
 57236       if( ISAUTOVACUUM ){
 57237         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
 57238         if( rc!=SQLITE_OK ){
 57239           goto balance_cleanup;
 57245   /* Free any old pages that were not reused as new pages.
 57246   */
 57247   while( i<nOld ){
 57248     freePage(apOld[i], &rc);
 57249     if( rc ) goto balance_cleanup;
 57250     releasePage(apOld[i]);
 57251     apOld[i] = 0;
 57252     i++;
 57255   /*
 57256   ** Put the new pages in accending order.  This helps to
 57257   ** keep entries in the disk file in order so that a scan
 57258   ** of the table is a linear scan through the file.  That
 57259   ** in turn helps the operating system to deliver pages
 57260   ** from the disk more rapidly.
 57261   **
 57262   ** An O(n^2) insertion sort algorithm is used, but since
 57263   ** n is never more than NB (a small constant), that should
 57264   ** not be a problem.
 57265   **
 57266   ** When NB==3, this one optimization makes the database
 57267   ** about 25% faster for large insertions and deletions.
 57268   */
 57269   for(i=0; i<k-1; i++){
 57270     int minV = apNew[i]->pgno;
 57271     int minI = i;
 57272     for(j=i+1; j<k; j++){
 57273       if( apNew[j]->pgno<(unsigned)minV ){
 57274         minI = j;
 57275         minV = apNew[j]->pgno;
 57278     if( minI>i ){
 57279       MemPage *pT;
 57280       pT = apNew[i];
 57281       apNew[i] = apNew[minI];
 57282       apNew[minI] = pT;
 57285   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
 57286     apNew[0]->pgno, szNew[0],
 57287     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
 57288     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
 57289     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
 57290     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
 57292   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 57293   put4byte(pRight, apNew[nNew-1]->pgno);
 57295   /*
 57296   ** Evenly distribute the data in apCell[] across the new pages.
 57297   ** Insert divider cells into pParent as necessary.
 57298   */
 57299   j = 0;
 57300   for(i=0; i<nNew; i++){
 57301     /* Assemble the new sibling page. */
 57302     MemPage *pNew = apNew[i];
 57303     assert( j<nMaxCells );
 57304     zeroPage(pNew, pageFlags);
 57305     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
 57306     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
 57307     assert( pNew->nOverflow==0 );
 57309     j = cntNew[i];
 57311     /* If the sibling page assembled above was not the right-most sibling,
 57312     ** insert a divider cell into the parent page.
 57313     */
 57314     assert( i<nNew-1 || j==nCell );
 57315     if( j<nCell ){
 57316       u8 *pCell;
 57317       u8 *pTemp;
 57318       int sz;
 57320       assert( j<nMaxCells );
 57321       pCell = apCell[j];
 57322       sz = szCell[j] + leafCorrection;
 57323       pTemp = &aOvflSpace[iOvflSpace];
 57324       if( !pNew->leaf ){
 57325         memcpy(&pNew->aData[8], pCell, 4);
 57326       }else if( leafData ){
 57327         /* If the tree is a leaf-data tree, and the siblings are leaves, 
 57328         ** then there is no divider cell in apCell[]. Instead, the divider 
 57329         ** cell consists of the integer key for the right-most cell of 
 57330         ** the sibling-page assembled above only.
 57331         */
 57332         CellInfo info;
 57333         j--;
 57334         btreeParseCellPtr(pNew, apCell[j], &info);
 57335         pCell = pTemp;
 57336         sz = 4 + putVarint(&pCell[4], info.nKey);
 57337         pTemp = 0;
 57338       }else{
 57339         pCell -= 4;
 57340         /* Obscure case for non-leaf-data trees: If the cell at pCell was
 57341         ** previously stored on a leaf node, and its reported size was 4
 57342         ** bytes, then it may actually be smaller than this 
 57343         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
 57344         ** any cell). But it is important to pass the correct size to 
 57345         ** insertCell(), so reparse the cell now.
 57346         **
 57347         ** Note that this can never happen in an SQLite data file, as all
 57348         ** cells are at least 4 bytes. It only happens in b-trees used
 57349         ** to evaluate "IN (SELECT ...)" and similar clauses.
 57350         */
 57351         if( szCell[j]==4 ){
 57352           assert(leafCorrection==4);
 57353           sz = cellSizePtr(pParent, pCell);
 57356       iOvflSpace += sz;
 57357       assert( sz<=pBt->maxLocal+23 );
 57358       assert( iOvflSpace <= (int)pBt->pageSize );
 57359       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
 57360       if( rc!=SQLITE_OK ) goto balance_cleanup;
 57361       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 57363       j++;
 57364       nxDiv++;
 57367   assert( j==nCell );
 57368   assert( nOld>0 );
 57369   assert( nNew>0 );
 57370   if( (pageFlags & PTF_LEAF)==0 ){
 57371     u8 *zChild = &apCopy[nOld-1]->aData[8];
 57372     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
 57375   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
 57376     /* The root page of the b-tree now contains no cells. The only sibling
 57377     ** page is the right-child of the parent. Copy the contents of the
 57378     ** child page into the parent, decreasing the overall height of the
 57379     ** b-tree structure by one. This is described as the "balance-shallower"
 57380     ** sub-algorithm in some documentation.
 57381     **
 57382     ** If this is an auto-vacuum database, the call to copyNodeContent() 
 57383     ** sets all pointer-map entries corresponding to database image pages 
 57384     ** for which the pointer is stored within the content being copied.
 57385     **
 57386     ** The second assert below verifies that the child page is defragmented
 57387     ** (it must be, as it was just reconstructed using assemblePage()). This
 57388     ** is important if the parent page happens to be page 1 of the database
 57389     ** image.  */
 57390     assert( nNew==1 );
 57391     assert( apNew[0]->nFree == 
 57392         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
 57393     );
 57394     copyNodeContent(apNew[0], pParent, &rc);
 57395     freePage(apNew[0], &rc);
 57396   }else if( ISAUTOVACUUM ){
 57397     /* Fix the pointer-map entries for all the cells that were shifted around. 
 57398     ** There are several different types of pointer-map entries that need to
 57399     ** be dealt with by this routine. Some of these have been set already, but
 57400     ** many have not. The following is a summary:
 57401     **
 57402     **   1) The entries associated with new sibling pages that were not
 57403     **      siblings when this function was called. These have already
 57404     **      been set. We don't need to worry about old siblings that were
 57405     **      moved to the free-list - the freePage() code has taken care
 57406     **      of those.
 57407     **
 57408     **   2) The pointer-map entries associated with the first overflow
 57409     **      page in any overflow chains used by new divider cells. These 
 57410     **      have also already been taken care of by the insertCell() code.
 57411     **
 57412     **   3) If the sibling pages are not leaves, then the child pages of
 57413     **      cells stored on the sibling pages may need to be updated.
 57414     **
 57415     **   4) If the sibling pages are not internal intkey nodes, then any
 57416     **      overflow pages used by these cells may need to be updated
 57417     **      (internal intkey nodes never contain pointers to overflow pages).
 57418     **
 57419     **   5) If the sibling pages are not leaves, then the pointer-map
 57420     **      entries for the right-child pages of each sibling may need
 57421     **      to be updated.
 57422     **
 57423     ** Cases 1 and 2 are dealt with above by other code. The next
 57424     ** block deals with cases 3 and 4 and the one after that, case 5. Since
 57425     ** setting a pointer map entry is a relatively expensive operation, this
 57426     ** code only sets pointer map entries for child or overflow pages that have
 57427     ** actually moved between pages.  */
 57428     MemPage *pNew = apNew[0];
 57429     MemPage *pOld = apCopy[0];
 57430     int nOverflow = pOld->nOverflow;
 57431     int iNextOld = pOld->nCell + nOverflow;
 57432     int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
 57433     j = 0;                             /* Current 'old' sibling page */
 57434     k = 0;                             /* Current 'new' sibling page */
 57435     for(i=0; i<nCell; i++){
 57436       int isDivider = 0;
 57437       while( i==iNextOld ){
 57438         /* Cell i is the cell immediately following the last cell on old
 57439         ** sibling page j. If the siblings are not leaf pages of an
 57440         ** intkey b-tree, then cell i was a divider cell. */
 57441         assert( j+1 < ArraySize(apCopy) );
 57442         assert( j+1 < nOld );
 57443         pOld = apCopy[++j];
 57444         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
 57445         if( pOld->nOverflow ){
 57446           nOverflow = pOld->nOverflow;
 57447           iOverflow = i + !leafData + pOld->aiOvfl[0];
 57449         isDivider = !leafData;  
 57452       assert(nOverflow>0 || iOverflow<i );
 57453       assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
 57454       assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
 57455       if( i==iOverflow ){
 57456         isDivider = 1;
 57457         if( (--nOverflow)>0 ){
 57458           iOverflow++;
 57462       if( i==cntNew[k] ){
 57463         /* Cell i is the cell immediately following the last cell on new
 57464         ** sibling page k. If the siblings are not leaf pages of an
 57465         ** intkey b-tree, then cell i is a divider cell.  */
 57466         pNew = apNew[++k];
 57467         if( !leafData ) continue;
 57469       assert( j<nOld );
 57470       assert( k<nNew );
 57472       /* If the cell was originally divider cell (and is not now) or
 57473       ** an overflow cell, or if the cell was located on a different sibling
 57474       ** page before the balancing, then the pointer map entries associated
 57475       ** with any child or overflow pages need to be updated.  */
 57476       if( isDivider || pOld->pgno!=pNew->pgno ){
 57477         if( !leafCorrection ){
 57478           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
 57480         if( szCell[i]>pNew->minLocal ){
 57481           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
 57486     if( !leafCorrection ){
 57487       for(i=0; i<nNew; i++){
 57488         u32 key = get4byte(&apNew[i]->aData[8]);
 57489         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
 57493 #if 0
 57494     /* The ptrmapCheckPages() contains assert() statements that verify that
 57495     ** all pointer map pages are set correctly. This is helpful while 
 57496     ** debugging. This is usually disabled because a corrupt database may
 57497     ** cause an assert() statement to fail.  */
 57498     ptrmapCheckPages(apNew, nNew);
 57499     ptrmapCheckPages(&pParent, 1);
 57500 #endif
 57503   assert( pParent->isInit );
 57504   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
 57505           nOld, nNew, nCell));
 57507   /*
 57508   ** Cleanup before returning.
 57509   */
 57510 balance_cleanup:
 57511   sqlite3ScratchFree(apCell);
 57512   for(i=0; i<nOld; i++){
 57513     releasePage(apOld[i]);
 57515   for(i=0; i<nNew; i++){
 57516     releasePage(apNew[i]);
 57519   return rc;
 57521 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
 57522 #pragma optimize("", on)
 57523 #endif
 57526 /*
 57527 ** This function is called when the root page of a b-tree structure is
 57528 ** overfull (has one or more overflow pages).
 57529 **
 57530 ** A new child page is allocated and the contents of the current root
 57531 ** page, including overflow cells, are copied into the child. The root
 57532 ** page is then overwritten to make it an empty page with the right-child 
 57533 ** pointer pointing to the new page.
 57534 **
 57535 ** Before returning, all pointer-map entries corresponding to pages 
 57536 ** that the new child-page now contains pointers to are updated. The
 57537 ** entry corresponding to the new right-child pointer of the root
 57538 ** page is also updated.
 57539 **
 57540 ** If successful, *ppChild is set to contain a reference to the child 
 57541 ** page and SQLITE_OK is returned. In this case the caller is required
 57542 ** to call releasePage() on *ppChild exactly once. If an error occurs,
 57543 ** an error code is returned and *ppChild is set to 0.
 57544 */
 57545 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
 57546   int rc;                        /* Return value from subprocedures */
 57547   MemPage *pChild = 0;           /* Pointer to a new child page */
 57548   Pgno pgnoChild = 0;            /* Page number of the new child page */
 57549   BtShared *pBt = pRoot->pBt;    /* The BTree */
 57551   assert( pRoot->nOverflow>0 );
 57552   assert( sqlite3_mutex_held(pBt->mutex) );
 57554   /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
 57555   ** page that will become the new right-child of pPage. Copy the contents
 57556   ** of the node stored on pRoot into the new child page.
 57557   */
 57558   rc = sqlite3PagerWrite(pRoot->pDbPage);
 57559   if( rc==SQLITE_OK ){
 57560     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
 57561     copyNodeContent(pRoot, pChild, &rc);
 57562     if( ISAUTOVACUUM ){
 57563       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
 57566   if( rc ){
 57567     *ppChild = 0;
 57568     releasePage(pChild);
 57569     return rc;
 57571   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
 57572   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 57573   assert( pChild->nCell==pRoot->nCell );
 57575   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
 57577   /* Copy the overflow cells from pRoot to pChild */
 57578   memcpy(pChild->aiOvfl, pRoot->aiOvfl,
 57579          pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
 57580   memcpy(pChild->apOvfl, pRoot->apOvfl,
 57581          pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
 57582   pChild->nOverflow = pRoot->nOverflow;
 57584   /* Zero the contents of pRoot. Then install pChild as the right-child. */
 57585   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
 57586   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
 57588   *ppChild = pChild;
 57589   return SQLITE_OK;
 57592 /*
 57593 ** The page that pCur currently points to has just been modified in
 57594 ** some way. This function figures out if this modification means the
 57595 ** tree needs to be balanced, and if so calls the appropriate balancing 
 57596 ** routine. Balancing routines are:
 57597 **
 57598 **   balance_quick()
 57599 **   balance_deeper()
 57600 **   balance_nonroot()
 57601 */
 57602 static int balance(BtCursor *pCur){
 57603   int rc = SQLITE_OK;
 57604   const int nMin = pCur->pBt->usableSize * 2 / 3;
 57605   u8 aBalanceQuickSpace[13];
 57606   u8 *pFree = 0;
 57608   TESTONLY( int balance_quick_called = 0 );
 57609   TESTONLY( int balance_deeper_called = 0 );
 57611   do {
 57612     int iPage = pCur->iPage;
 57613     MemPage *pPage = pCur->apPage[iPage];
 57615     if( iPage==0 ){
 57616       if( pPage->nOverflow ){
 57617         /* The root page of the b-tree is overfull. In this case call the
 57618         ** balance_deeper() function to create a new child for the root-page
 57619         ** and copy the current contents of the root-page to it. The
 57620         ** next iteration of the do-loop will balance the child page.
 57621         */ 
 57622         assert( (balance_deeper_called++)==0 );
 57623         rc = balance_deeper(pPage, &pCur->apPage[1]);
 57624         if( rc==SQLITE_OK ){
 57625           pCur->iPage = 1;
 57626           pCur->aiIdx[0] = 0;
 57627           pCur->aiIdx[1] = 0;
 57628           assert( pCur->apPage[1]->nOverflow );
 57630       }else{
 57631         break;
 57633     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
 57634       break;
 57635     }else{
 57636       MemPage * const pParent = pCur->apPage[iPage-1];
 57637       int const iIdx = pCur->aiIdx[iPage-1];
 57639       rc = sqlite3PagerWrite(pParent->pDbPage);
 57640       if( rc==SQLITE_OK ){
 57641 #ifndef SQLITE_OMIT_QUICKBALANCE
 57642         if( pPage->hasData
 57643          && pPage->nOverflow==1
 57644          && pPage->aiOvfl[0]==pPage->nCell
 57645          && pParent->pgno!=1
 57646          && pParent->nCell==iIdx
 57647         ){
 57648           /* Call balance_quick() to create a new sibling of pPage on which
 57649           ** to store the overflow cell. balance_quick() inserts a new cell
 57650           ** into pParent, which may cause pParent overflow. If this
 57651           ** happens, the next interation of the do-loop will balance pParent 
 57652           ** use either balance_nonroot() or balance_deeper(). Until this
 57653           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
 57654           ** buffer. 
 57655           **
 57656           ** The purpose of the following assert() is to check that only a
 57657           ** single call to balance_quick() is made for each call to this
 57658           ** function. If this were not verified, a subtle bug involving reuse
 57659           ** of the aBalanceQuickSpace[] might sneak in.
 57660           */
 57661           assert( (balance_quick_called++)==0 );
 57662           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
 57663         }else
 57664 #endif
 57666           /* In this case, call balance_nonroot() to redistribute cells
 57667           ** between pPage and up to 2 of its sibling pages. This involves
 57668           ** modifying the contents of pParent, which may cause pParent to
 57669           ** become overfull or underfull. The next iteration of the do-loop
 57670           ** will balance the parent page to correct this.
 57671           ** 
 57672           ** If the parent page becomes overfull, the overflow cell or cells
 57673           ** are stored in the pSpace buffer allocated immediately below. 
 57674           ** A subsequent iteration of the do-loop will deal with this by
 57675           ** calling balance_nonroot() (balance_deeper() may be called first,
 57676           ** but it doesn't deal with overflow cells - just moves them to a
 57677           ** different page). Once this subsequent call to balance_nonroot() 
 57678           ** has completed, it is safe to release the pSpace buffer used by
 57679           ** the previous call, as the overflow cell data will have been 
 57680           ** copied either into the body of a database page or into the new
 57681           ** pSpace buffer passed to the latter call to balance_nonroot().
 57682           */
 57683           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
 57684           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
 57685           if( pFree ){
 57686             /* If pFree is not NULL, it points to the pSpace buffer used 
 57687             ** by a previous call to balance_nonroot(). Its contents are
 57688             ** now stored either on real database pages or within the 
 57689             ** new pSpace buffer, so it may be safely freed here. */
 57690             sqlite3PageFree(pFree);
 57693           /* The pSpace buffer will be freed after the next call to
 57694           ** balance_nonroot(), or just before this function returns, whichever
 57695           ** comes first. */
 57696           pFree = pSpace;
 57700       pPage->nOverflow = 0;
 57702       /* The next iteration of the do-loop balances the parent page. */
 57703       releasePage(pPage);
 57704       pCur->iPage--;
 57706   }while( rc==SQLITE_OK );
 57708   if( pFree ){
 57709     sqlite3PageFree(pFree);
 57711   return rc;
 57715 /*
 57716 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
 57717 ** and the data is given by (pData,nData).  The cursor is used only to
 57718 ** define what table the record should be inserted into.  The cursor
 57719 ** is left pointing at a random location.
 57720 **
 57721 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
 57722 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
 57723 **
 57724 ** If the seekResult parameter is non-zero, then a successful call to
 57725 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
 57726 ** been performed. seekResult is the search result returned (a negative
 57727 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
 57728 ** a positive value if pCur points at an etry that is larger than 
 57729 ** (pKey, nKey)). 
 57730 **
 57731 ** If the seekResult parameter is non-zero, then the caller guarantees that
 57732 ** cursor pCur is pointing at the existing copy of a row that is to be
 57733 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
 57734 ** point to any entry or to no entry at all and so this function has to seek
 57735 ** the cursor before the new key can be inserted.
 57736 */
 57737 SQLITE_PRIVATE int sqlite3BtreeInsert(
 57738   BtCursor *pCur,                /* Insert data into the table of this cursor */
 57739   const void *pKey, i64 nKey,    /* The key of the new record */
 57740   const void *pData, int nData,  /* The data of the new record */
 57741   int nZero,                     /* Number of extra 0 bytes to append to data */
 57742   int appendBias,                /* True if this is likely an append */
 57743   int seekResult                 /* Result of prior MovetoUnpacked() call */
 57744 ){
 57745   int rc;
 57746   int loc = seekResult;          /* -1: before desired location  +1: after */
 57747   int szNew = 0;
 57748   int idx;
 57749   MemPage *pPage;
 57750   Btree *p = pCur->pBtree;
 57751   BtShared *pBt = p->pBt;
 57752   unsigned char *oldCell;
 57753   unsigned char *newCell = 0;
 57755   if( pCur->eState==CURSOR_FAULT ){
 57756     assert( pCur->skipNext!=SQLITE_OK );
 57757     return pCur->skipNext;
 57760   assert( cursorHoldsMutex(pCur) );
 57761   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
 57762               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
 57763   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 57765   /* Assert that the caller has been consistent. If this cursor was opened
 57766   ** expecting an index b-tree, then the caller should be inserting blob
 57767   ** keys with no associated data. If the cursor was opened expecting an
 57768   ** intkey table, the caller should be inserting integer keys with a
 57769   ** blob of associated data.  */
 57770   assert( (pKey==0)==(pCur->pKeyInfo==0) );
 57772   /* Save the positions of any other cursors open on this table.
 57773   **
 57774   ** In some cases, the call to btreeMoveto() below is a no-op. For
 57775   ** example, when inserting data into a table with auto-generated integer
 57776   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
 57777   ** integer key to use. It then calls this function to actually insert the 
 57778   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
 57779   ** that the cursor is already where it needs to be and returns without
 57780   ** doing any work. To avoid thwarting these optimizations, it is important
 57781   ** not to clear the cursor here.
 57782   */
 57783   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 57784   if( rc ) return rc;
 57786   if( pCur->pKeyInfo==0 ){
 57787     /* If this is an insert into a table b-tree, invalidate any incrblob 
 57788     ** cursors open on the row being replaced */
 57789     invalidateIncrblobCursors(p, nKey, 0);
 57791     /* If the cursor is currently on the last row and we are appending a
 57792     ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
 57793     ** call */
 57794     if( pCur->validNKey && nKey>0 && pCur->info.nKey==nKey-1 ){
 57795       loc = -1;
 57799   if( !loc ){
 57800     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
 57801     if( rc ) return rc;
 57803   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
 57805   pPage = pCur->apPage[pCur->iPage];
 57806   assert( pPage->intKey || nKey>=0 );
 57807   assert( pPage->leaf || !pPage->intKey );
 57809   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
 57810           pCur->pgnoRoot, nKey, nData, pPage->pgno,
 57811           loc==0 ? "overwrite" : "new entry"));
 57812   assert( pPage->isInit );
 57813   allocateTempSpace(pBt);
 57814   newCell = pBt->pTmpSpace;
 57815   if( newCell==0 ) return SQLITE_NOMEM;
 57816   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
 57817   if( rc ) goto end_insert;
 57818   assert( szNew==cellSizePtr(pPage, newCell) );
 57819   assert( szNew <= MX_CELL_SIZE(pBt) );
 57820   idx = pCur->aiIdx[pCur->iPage];
 57821   if( loc==0 ){
 57822     u16 szOld;
 57823     assert( idx<pPage->nCell );
 57824     rc = sqlite3PagerWrite(pPage->pDbPage);
 57825     if( rc ){
 57826       goto end_insert;
 57828     oldCell = findCell(pPage, idx);
 57829     if( !pPage->leaf ){
 57830       memcpy(newCell, oldCell, 4);
 57832     szOld = cellSizePtr(pPage, oldCell);
 57833     rc = clearCell(pPage, oldCell);
 57834     dropCell(pPage, idx, szOld, &rc);
 57835     if( rc ) goto end_insert;
 57836   }else if( loc<0 && pPage->nCell>0 ){
 57837     assert( pPage->leaf );
 57838     idx = ++pCur->aiIdx[pCur->iPage];
 57839   }else{
 57840     assert( pPage->leaf );
 57842   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
 57843   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
 57845   /* If no error has occurred and pPage has an overflow cell, call balance() 
 57846   ** to redistribute the cells within the tree. Since balance() may move
 57847   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
 57848   ** variables.
 57849   **
 57850   ** Previous versions of SQLite called moveToRoot() to move the cursor
 57851   ** back to the root page as balance() used to invalidate the contents
 57852   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
 57853   ** set the cursor state to "invalid". This makes common insert operations
 57854   ** slightly faster.
 57855   **
 57856   ** There is a subtle but important optimization here too. When inserting
 57857   ** multiple records into an intkey b-tree using a single cursor (as can
 57858   ** happen while processing an "INSERT INTO ... SELECT" statement), it
 57859   ** is advantageous to leave the cursor pointing to the last entry in
 57860   ** the b-tree if possible. If the cursor is left pointing to the last
 57861   ** entry in the table, and the next row inserted has an integer key
 57862   ** larger than the largest existing key, it is possible to insert the
 57863   ** row without seeking the cursor. This can be a big performance boost.
 57864   */
 57865   pCur->info.nSize = 0;
 57866   if( rc==SQLITE_OK && pPage->nOverflow ){
 57867     pCur->validNKey = 0;
 57868     rc = balance(pCur);
 57870     /* Must make sure nOverflow is reset to zero even if the balance()
 57871     ** fails. Internal data structure corruption will result otherwise. 
 57872     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
 57873     ** from trying to save the current position of the cursor.  */
 57874     pCur->apPage[pCur->iPage]->nOverflow = 0;
 57875     pCur->eState = CURSOR_INVALID;
 57877   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
 57879 end_insert:
 57880   return rc;
 57883 /*
 57884 ** Delete the entry that the cursor is pointing to.  The cursor
 57885 ** is left pointing at a arbitrary location.
 57886 */
 57887 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
 57888   Btree *p = pCur->pBtree;
 57889   BtShared *pBt = p->pBt;              
 57890   int rc;                              /* Return code */
 57891   MemPage *pPage;                      /* Page to delete cell from */
 57892   unsigned char *pCell;                /* Pointer to cell to delete */
 57893   int iCellIdx;                        /* Index of cell to delete */
 57894   int iCellDepth;                      /* Depth of node containing pCell */ 
 57896   assert( cursorHoldsMutex(pCur) );
 57897   assert( pBt->inTransaction==TRANS_WRITE );
 57898   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 57899   assert( pCur->wrFlag );
 57900   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
 57901   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
 57903   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
 57904    || NEVER(pCur->eState!=CURSOR_VALID)
 57905   ){
 57906     return SQLITE_ERROR;  /* Something has gone awry. */
 57909   iCellDepth = pCur->iPage;
 57910   iCellIdx = pCur->aiIdx[iCellDepth];
 57911   pPage = pCur->apPage[iCellDepth];
 57912   pCell = findCell(pPage, iCellIdx);
 57914   /* If the page containing the entry to delete is not a leaf page, move
 57915   ** the cursor to the largest entry in the tree that is smaller than
 57916   ** the entry being deleted. This cell will replace the cell being deleted
 57917   ** from the internal node. The 'previous' entry is used for this instead
 57918   ** of the 'next' entry, as the previous entry is always a part of the
 57919   ** sub-tree headed by the child page of the cell being deleted. This makes
 57920   ** balancing the tree following the delete operation easier.  */
 57921   if( !pPage->leaf ){
 57922     int notUsed = 0;
 57923     rc = sqlite3BtreePrevious(pCur, &notUsed);
 57924     if( rc ) return rc;
 57927   /* Save the positions of any other cursors open on this table before
 57928   ** making any modifications. Make the page containing the entry to be 
 57929   ** deleted writable. Then free any overflow pages associated with the 
 57930   ** entry and finally remove the cell itself from within the page.  
 57931   */
 57932   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
 57933   if( rc ) return rc;
 57935   /* If this is a delete operation to remove a row from a table b-tree,
 57936   ** invalidate any incrblob cursors open on the row being deleted.  */
 57937   if( pCur->pKeyInfo==0 ){
 57938     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
 57941   rc = sqlite3PagerWrite(pPage->pDbPage);
 57942   if( rc ) return rc;
 57943   rc = clearCell(pPage, pCell);
 57944   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
 57945   if( rc ) return rc;
 57947   /* If the cell deleted was not located on a leaf page, then the cursor
 57948   ** is currently pointing to the largest entry in the sub-tree headed
 57949   ** by the child-page of the cell that was just deleted from an internal
 57950   ** node. The cell from the leaf node needs to be moved to the internal
 57951   ** node to replace the deleted cell.  */
 57952   if( !pPage->leaf ){
 57953     MemPage *pLeaf = pCur->apPage[pCur->iPage];
 57954     int nCell;
 57955     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
 57956     unsigned char *pTmp;
 57958     pCell = findCell(pLeaf, pLeaf->nCell-1);
 57959     nCell = cellSizePtr(pLeaf, pCell);
 57960     assert( MX_CELL_SIZE(pBt) >= nCell );
 57962     allocateTempSpace(pBt);
 57963     pTmp = pBt->pTmpSpace;
 57965     rc = sqlite3PagerWrite(pLeaf->pDbPage);
 57966     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
 57967     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
 57968     if( rc ) return rc;
 57971   /* Balance the tree. If the entry deleted was located on a leaf page,
 57972   ** then the cursor still points to that page. In this case the first
 57973   ** call to balance() repairs the tree, and the if(...) condition is
 57974   ** never true.
 57975   **
 57976   ** Otherwise, if the entry deleted was on an internal node page, then
 57977   ** pCur is pointing to the leaf page from which a cell was removed to
 57978   ** replace the cell deleted from the internal node. This is slightly
 57979   ** tricky as the leaf node may be underfull, and the internal node may
 57980   ** be either under or overfull. In this case run the balancing algorithm
 57981   ** on the leaf node first. If the balance proceeds far enough up the
 57982   ** tree that we can be sure that any problem in the internal node has
 57983   ** been corrected, so be it. Otherwise, after balancing the leaf node,
 57984   ** walk the cursor up the tree to the internal node and balance it as 
 57985   ** well.  */
 57986   rc = balance(pCur);
 57987   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
 57988     while( pCur->iPage>iCellDepth ){
 57989       releasePage(pCur->apPage[pCur->iPage--]);
 57991     rc = balance(pCur);
 57994   if( rc==SQLITE_OK ){
 57995     moveToRoot(pCur);
 57997   return rc;
 58000 /*
 58001 ** Create a new BTree table.  Write into *piTable the page
 58002 ** number for the root page of the new table.
 58003 **
 58004 ** The type of type is determined by the flags parameter.  Only the
 58005 ** following values of flags are currently in use.  Other values for
 58006 ** flags might not work:
 58007 **
 58008 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
 58009 **     BTREE_ZERODATA                  Used for SQL indices
 58010 */
 58011 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
 58012   BtShared *pBt = p->pBt;
 58013   MemPage *pRoot;
 58014   Pgno pgnoRoot;
 58015   int rc;
 58016   int ptfFlags;          /* Page-type flage for the root page of new table */
 58018   assert( sqlite3BtreeHoldsMutex(p) );
 58019   assert( pBt->inTransaction==TRANS_WRITE );
 58020   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
 58022 #ifdef SQLITE_OMIT_AUTOVACUUM
 58023   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 58024   if( rc ){
 58025     return rc;
 58027 #else
 58028   if( pBt->autoVacuum ){
 58029     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
 58030     MemPage *pPageMove; /* The page to move to. */
 58032     /* Creating a new table may probably require moving an existing database
 58033     ** to make room for the new tables root page. In case this page turns
 58034     ** out to be an overflow page, delete all overflow page-map caches
 58035     ** held by open cursors.
 58036     */
 58037     invalidateAllOverflowCache(pBt);
 58039     /* Read the value of meta[3] from the database to determine where the
 58040     ** root page of the new table should go. meta[3] is the largest root-page
 58041     ** created so far, so the new root-page is (meta[3]+1).
 58042     */
 58043     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
 58044     pgnoRoot++;
 58046     /* The new root-page may not be allocated on a pointer-map page, or the
 58047     ** PENDING_BYTE page.
 58048     */
 58049     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
 58050         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
 58051       pgnoRoot++;
 58053     assert( pgnoRoot>=3 );
 58055     /* Allocate a page. The page that currently resides at pgnoRoot will
 58056     ** be moved to the allocated page (unless the allocated page happens
 58057     ** to reside at pgnoRoot).
 58058     */
 58059     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
 58060     if( rc!=SQLITE_OK ){
 58061       return rc;
 58064     if( pgnoMove!=pgnoRoot ){
 58065       /* pgnoRoot is the page that will be used for the root-page of
 58066       ** the new table (assuming an error did not occur). But we were
 58067       ** allocated pgnoMove. If required (i.e. if it was not allocated
 58068       ** by extending the file), the current page at position pgnoMove
 58069       ** is already journaled.
 58070       */
 58071       u8 eType = 0;
 58072       Pgno iPtrPage = 0;
 58074       /* Save the positions of any open cursors. This is required in
 58075       ** case they are holding a reference to an xFetch reference
 58076       ** corresponding to page pgnoRoot.  */
 58077       rc = saveAllCursors(pBt, 0, 0);
 58078       releasePage(pPageMove);
 58079       if( rc!=SQLITE_OK ){
 58080         return rc;
 58083       /* Move the page currently at pgnoRoot to pgnoMove. */
 58084       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 58085       if( rc!=SQLITE_OK ){
 58086         return rc;
 58088       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
 58089       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
 58090         rc = SQLITE_CORRUPT_BKPT;
 58092       if( rc!=SQLITE_OK ){
 58093         releasePage(pRoot);
 58094         return rc;
 58096       assert( eType!=PTRMAP_ROOTPAGE );
 58097       assert( eType!=PTRMAP_FREEPAGE );
 58098       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
 58099       releasePage(pRoot);
 58101       /* Obtain the page at pgnoRoot */
 58102       if( rc!=SQLITE_OK ){
 58103         return rc;
 58105       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
 58106       if( rc!=SQLITE_OK ){
 58107         return rc;
 58109       rc = sqlite3PagerWrite(pRoot->pDbPage);
 58110       if( rc!=SQLITE_OK ){
 58111         releasePage(pRoot);
 58112         return rc;
 58114     }else{
 58115       pRoot = pPageMove;
 58118     /* Update the pointer-map and meta-data with the new root-page number. */
 58119     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
 58120     if( rc ){
 58121       releasePage(pRoot);
 58122       return rc;
 58125     /* When the new root page was allocated, page 1 was made writable in
 58126     ** order either to increase the database filesize, or to decrement the
 58127     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
 58128     */
 58129     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
 58130     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
 58131     if( NEVER(rc) ){
 58132       releasePage(pRoot);
 58133       return rc;
 58136   }else{
 58137     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
 58138     if( rc ) return rc;
 58140 #endif
 58141   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
 58142   if( createTabFlags & BTREE_INTKEY ){
 58143     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
 58144   }else{
 58145     ptfFlags = PTF_ZERODATA | PTF_LEAF;
 58147   zeroPage(pRoot, ptfFlags);
 58148   sqlite3PagerUnref(pRoot->pDbPage);
 58149   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
 58150   *piTable = (int)pgnoRoot;
 58151   return SQLITE_OK;
 58153 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
 58154   int rc;
 58155   sqlite3BtreeEnter(p);
 58156   rc = btreeCreateTable(p, piTable, flags);
 58157   sqlite3BtreeLeave(p);
 58158   return rc;
 58161 /*
 58162 ** Erase the given database page and all its children.  Return
 58163 ** the page to the freelist.
 58164 */
 58165 static int clearDatabasePage(
 58166   BtShared *pBt,           /* The BTree that contains the table */
 58167   Pgno pgno,               /* Page number to clear */
 58168   int freePageFlag,        /* Deallocate page if true */
 58169   int *pnChange            /* Add number of Cells freed to this counter */
 58170 ){
 58171   MemPage *pPage;
 58172   int rc;
 58173   unsigned char *pCell;
 58174   int i;
 58175   int hdr;
 58177   assert( sqlite3_mutex_held(pBt->mutex) );
 58178   if( pgno>btreePagecount(pBt) ){
 58179     return SQLITE_CORRUPT_BKPT;
 58182   rc = getAndInitPage(pBt, pgno, &pPage, 0);
 58183   if( rc ) return rc;
 58184   hdr = pPage->hdrOffset;
 58185   for(i=0; i<pPage->nCell; i++){
 58186     pCell = findCell(pPage, i);
 58187     if( !pPage->leaf ){
 58188       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
 58189       if( rc ) goto cleardatabasepage_out;
 58191     rc = clearCell(pPage, pCell);
 58192     if( rc ) goto cleardatabasepage_out;
 58194   if( !pPage->leaf ){
 58195     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
 58196     if( rc ) goto cleardatabasepage_out;
 58197   }else if( pnChange ){
 58198     assert( pPage->intKey );
 58199     *pnChange += pPage->nCell;
 58201   if( freePageFlag ){
 58202     freePage(pPage, &rc);
 58203   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
 58204     zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
 58207 cleardatabasepage_out:
 58208   releasePage(pPage);
 58209   return rc;
 58212 /*
 58213 ** Delete all information from a single table in the database.  iTable is
 58214 ** the page number of the root of the table.  After this routine returns,
 58215 ** the root page is empty, but still exists.
 58216 **
 58217 ** This routine will fail with SQLITE_LOCKED if there are any open
 58218 ** read cursors on the table.  Open write cursors are moved to the
 58219 ** root of the table.
 58220 **
 58221 ** If pnChange is not NULL, then table iTable must be an intkey table. The
 58222 ** integer value pointed to by pnChange is incremented by the number of
 58223 ** entries in the table.
 58224 */
 58225 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
 58226   int rc;
 58227   BtShared *pBt = p->pBt;
 58228   sqlite3BtreeEnter(p);
 58229   assert( p->inTrans==TRANS_WRITE );
 58231   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
 58233   if( SQLITE_OK==rc ){
 58234     /* Invalidate all incrblob cursors open on table iTable (assuming iTable
 58235     ** is the root of a table b-tree - if it is not, the following call is
 58236     ** a no-op).  */
 58237     invalidateIncrblobCursors(p, 0, 1);
 58238     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
 58240   sqlite3BtreeLeave(p);
 58241   return rc;
 58244 /*
 58245 ** Erase all information in a table and add the root of the table to
 58246 ** the freelist.  Except, the root of the principle table (the one on
 58247 ** page 1) is never added to the freelist.
 58248 **
 58249 ** This routine will fail with SQLITE_LOCKED if there are any open
 58250 ** cursors on the table.
 58251 **
 58252 ** If AUTOVACUUM is enabled and the page at iTable is not the last
 58253 ** root page in the database file, then the last root page 
 58254 ** in the database file is moved into the slot formerly occupied by
 58255 ** iTable and that last slot formerly occupied by the last root page
 58256 ** is added to the freelist instead of iTable.  In this say, all
 58257 ** root pages are kept at the beginning of the database file, which
 58258 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
 58259 ** page number that used to be the last root page in the file before
 58260 ** the move.  If no page gets moved, *piMoved is set to 0.
 58261 ** The last root page is recorded in meta[3] and the value of
 58262 ** meta[3] is updated by this procedure.
 58263 */
 58264 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
 58265   int rc;
 58266   MemPage *pPage = 0;
 58267   BtShared *pBt = p->pBt;
 58269   assert( sqlite3BtreeHoldsMutex(p) );
 58270   assert( p->inTrans==TRANS_WRITE );
 58272   /* It is illegal to drop a table if any cursors are open on the
 58273   ** database. This is because in auto-vacuum mode the backend may
 58274   ** need to move another root-page to fill a gap left by the deleted
 58275   ** root page. If an open cursor was using this page a problem would 
 58276   ** occur.
 58277   **
 58278   ** This error is caught long before control reaches this point.
 58279   */
 58280   if( NEVER(pBt->pCursor) ){
 58281     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
 58282     return SQLITE_LOCKED_SHAREDCACHE;
 58285   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
 58286   if( rc ) return rc;
 58287   rc = sqlite3BtreeClearTable(p, iTable, 0);
 58288   if( rc ){
 58289     releasePage(pPage);
 58290     return rc;
 58293   *piMoved = 0;
 58295   if( iTable>1 ){
 58296 #ifdef SQLITE_OMIT_AUTOVACUUM
 58297     freePage(pPage, &rc);
 58298     releasePage(pPage);
 58299 #else
 58300     if( pBt->autoVacuum ){
 58301       Pgno maxRootPgno;
 58302       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
 58304       if( iTable==maxRootPgno ){
 58305         /* If the table being dropped is the table with the largest root-page
 58306         ** number in the database, put the root page on the free list. 
 58307         */
 58308         freePage(pPage, &rc);
 58309         releasePage(pPage);
 58310         if( rc!=SQLITE_OK ){
 58311           return rc;
 58313       }else{
 58314         /* The table being dropped does not have the largest root-page
 58315         ** number in the database. So move the page that does into the 
 58316         ** gap left by the deleted root-page.
 58317         */
 58318         MemPage *pMove;
 58319         releasePage(pPage);
 58320         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 58321         if( rc!=SQLITE_OK ){
 58322           return rc;
 58324         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
 58325         releasePage(pMove);
 58326         if( rc!=SQLITE_OK ){
 58327           return rc;
 58329         pMove = 0;
 58330         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
 58331         freePage(pMove, &rc);
 58332         releasePage(pMove);
 58333         if( rc!=SQLITE_OK ){
 58334           return rc;
 58336         *piMoved = maxRootPgno;
 58339       /* Set the new 'max-root-page' value in the database header. This
 58340       ** is the old value less one, less one more if that happens to
 58341       ** be a root-page number, less one again if that is the
 58342       ** PENDING_BYTE_PAGE.
 58343       */
 58344       maxRootPgno--;
 58345       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
 58346              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
 58347         maxRootPgno--;
 58349       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
 58351       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
 58352     }else{
 58353       freePage(pPage, &rc);
 58354       releasePage(pPage);
 58356 #endif
 58357   }else{
 58358     /* If sqlite3BtreeDropTable was called on page 1.
 58359     ** This really never should happen except in a corrupt
 58360     ** database. 
 58361     */
 58362     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
 58363     releasePage(pPage);
 58365   return rc;  
 58367 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
 58368   int rc;
 58369   sqlite3BtreeEnter(p);
 58370   rc = btreeDropTable(p, iTable, piMoved);
 58371   sqlite3BtreeLeave(p);
 58372   return rc;
 58376 /*
 58377 ** This function may only be called if the b-tree connection already
 58378 ** has a read or write transaction open on the database.
 58379 **
 58380 ** Read the meta-information out of a database file.  Meta[0]
 58381 ** is the number of free pages currently in the database.  Meta[1]
 58382 ** through meta[15] are available for use by higher layers.  Meta[0]
 58383 ** is read-only, the others are read/write.
 58384 ** 
 58385 ** The schema layer numbers meta values differently.  At the schema
 58386 ** layer (and the SetCookie and ReadCookie opcodes) the number of
 58387 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
 58388 */
 58389 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
 58390   BtShared *pBt = p->pBt;
 58392   sqlite3BtreeEnter(p);
 58393   assert( p->inTrans>TRANS_NONE );
 58394   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
 58395   assert( pBt->pPage1 );
 58396   assert( idx>=0 && idx<=15 );
 58398   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
 58400   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
 58401   ** database, mark the database as read-only.  */
 58402 #ifdef SQLITE_OMIT_AUTOVACUUM
 58403   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
 58404     pBt->btsFlags |= BTS_READ_ONLY;
 58406 #endif
 58408   sqlite3BtreeLeave(p);
 58411 /*
 58412 ** Write meta-information back into the database.  Meta[0] is
 58413 ** read-only and may not be written.
 58414 */
 58415 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
 58416   BtShared *pBt = p->pBt;
 58417   unsigned char *pP1;
 58418   int rc;
 58419   assert( idx>=1 && idx<=15 );
 58420   sqlite3BtreeEnter(p);
 58421   assert( p->inTrans==TRANS_WRITE );
 58422   assert( pBt->pPage1!=0 );
 58423   pP1 = pBt->pPage1->aData;
 58424   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 58425   if( rc==SQLITE_OK ){
 58426     put4byte(&pP1[36 + idx*4], iMeta);
 58427 #ifndef SQLITE_OMIT_AUTOVACUUM
 58428     if( idx==BTREE_INCR_VACUUM ){
 58429       assert( pBt->autoVacuum || iMeta==0 );
 58430       assert( iMeta==0 || iMeta==1 );
 58431       pBt->incrVacuum = (u8)iMeta;
 58433 #endif
 58435   sqlite3BtreeLeave(p);
 58436   return rc;
 58439 #ifndef SQLITE_OMIT_BTREECOUNT
 58440 /*
 58441 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
 58442 ** number of entries in the b-tree and write the result to *pnEntry.
 58443 **
 58444 ** SQLITE_OK is returned if the operation is successfully executed. 
 58445 ** Otherwise, if an error is encountered (i.e. an IO error or database
 58446 ** corruption) an SQLite error code is returned.
 58447 */
 58448 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
 58449   i64 nEntry = 0;                      /* Value to return in *pnEntry */
 58450   int rc;                              /* Return code */
 58452   if( pCur->pgnoRoot==0 ){
 58453     *pnEntry = 0;
 58454     return SQLITE_OK;
 58456   rc = moveToRoot(pCur);
 58458   /* Unless an error occurs, the following loop runs one iteration for each
 58459   ** page in the B-Tree structure (not including overflow pages). 
 58460   */
 58461   while( rc==SQLITE_OK ){
 58462     int iIdx;                          /* Index of child node in parent */
 58463     MemPage *pPage;                    /* Current page of the b-tree */
 58465     /* If this is a leaf page or the tree is not an int-key tree, then 
 58466     ** this page contains countable entries. Increment the entry counter
 58467     ** accordingly.
 58468     */
 58469     pPage = pCur->apPage[pCur->iPage];
 58470     if( pPage->leaf || !pPage->intKey ){
 58471       nEntry += pPage->nCell;
 58474     /* pPage is a leaf node. This loop navigates the cursor so that it 
 58475     ** points to the first interior cell that it points to the parent of
 58476     ** the next page in the tree that has not yet been visited. The
 58477     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
 58478     ** of the page, or to the number of cells in the page if the next page
 58479     ** to visit is the right-child of its parent.
 58480     **
 58481     ** If all pages in the tree have been visited, return SQLITE_OK to the
 58482     ** caller.
 58483     */
 58484     if( pPage->leaf ){
 58485       do {
 58486         if( pCur->iPage==0 ){
 58487           /* All pages of the b-tree have been visited. Return successfully. */
 58488           *pnEntry = nEntry;
 58489           return SQLITE_OK;
 58491         moveToParent(pCur);
 58492       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
 58494       pCur->aiIdx[pCur->iPage]++;
 58495       pPage = pCur->apPage[pCur->iPage];
 58498     /* Descend to the child node of the cell that the cursor currently 
 58499     ** points at. This is the right-child if (iIdx==pPage->nCell).
 58500     */
 58501     iIdx = pCur->aiIdx[pCur->iPage];
 58502     if( iIdx==pPage->nCell ){
 58503       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
 58504     }else{
 58505       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
 58509   /* An error has occurred. Return an error code. */
 58510   return rc;
 58512 #endif
 58514 /*
 58515 ** Return the pager associated with a BTree.  This routine is used for
 58516 ** testing and debugging only.
 58517 */
 58518 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
 58519   return p->pBt->pPager;
 58522 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 58523 /*
 58524 ** Append a message to the error message string.
 58525 */
 58526 static void checkAppendMsg(
 58527   IntegrityCk *pCheck,
 58528   char *zMsg1,
 58529   const char *zFormat,
 58530   ...
 58531 ){
 58532   va_list ap;
 58533   if( !pCheck->mxErr ) return;
 58534   pCheck->mxErr--;
 58535   pCheck->nErr++;
 58536   va_start(ap, zFormat);
 58537   if( pCheck->errMsg.nChar ){
 58538     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
 58540   if( zMsg1 ){
 58541     sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
 58543   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
 58544   va_end(ap);
 58545   if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
 58546     pCheck->mallocFailed = 1;
 58549 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 58551 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 58553 /*
 58554 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
 58555 ** corresponds to page iPg is already set.
 58556 */
 58557 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 58558   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 58559   return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
 58562 /*
 58563 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
 58564 */
 58565 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
 58566   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
 58567   pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
 58571 /*
 58572 ** Add 1 to the reference count for page iPage.  If this is the second
 58573 ** reference to the page, add an error message to pCheck->zErrMsg.
 58574 ** Return 1 if there are 2 ore more references to the page and 0 if
 58575 ** if this is the first reference to the page.
 58576 **
 58577 ** Also check that the page number is in bounds.
 58578 */
 58579 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
 58580   if( iPage==0 ) return 1;
 58581   if( iPage>pCheck->nPage ){
 58582     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
 58583     return 1;
 58585   if( getPageReferenced(pCheck, iPage) ){
 58586     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
 58587     return 1;
 58589   setPageReferenced(pCheck, iPage);
 58590   return 0;
 58593 #ifndef SQLITE_OMIT_AUTOVACUUM
 58594 /*
 58595 ** Check that the entry in the pointer-map for page iChild maps to 
 58596 ** page iParent, pointer type ptrType. If not, append an error message
 58597 ** to pCheck.
 58598 */
 58599 static void checkPtrmap(
 58600   IntegrityCk *pCheck,   /* Integrity check context */
 58601   Pgno iChild,           /* Child page number */
 58602   u8 eType,              /* Expected pointer map type */
 58603   Pgno iParent,          /* Expected pointer map parent page number */
 58604   char *zContext         /* Context description (used for error msg) */
 58605 ){
 58606   int rc;
 58607   u8 ePtrmapType;
 58608   Pgno iPtrmapParent;
 58610   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
 58611   if( rc!=SQLITE_OK ){
 58612     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
 58613     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
 58614     return;
 58617   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
 58618     checkAppendMsg(pCheck, zContext, 
 58619       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
 58620       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
 58623 #endif
 58625 /*
 58626 ** Check the integrity of the freelist or of an overflow page list.
 58627 ** Verify that the number of pages on the list is N.
 58628 */
 58629 static void checkList(
 58630   IntegrityCk *pCheck,  /* Integrity checking context */
 58631   int isFreeList,       /* True for a freelist.  False for overflow page list */
 58632   int iPage,            /* Page number for first page in the list */
 58633   int N,                /* Expected number of pages in the list */
 58634   char *zContext        /* Context for error messages */
 58635 ){
 58636   int i;
 58637   int expected = N;
 58638   int iFirst = iPage;
 58639   while( N-- > 0 && pCheck->mxErr ){
 58640     DbPage *pOvflPage;
 58641     unsigned char *pOvflData;
 58642     if( iPage<1 ){
 58643       checkAppendMsg(pCheck, zContext,
 58644          "%d of %d pages missing from overflow list starting at %d",
 58645           N+1, expected, iFirst);
 58646       break;
 58648     if( checkRef(pCheck, iPage, zContext) ) break;
 58649     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
 58650       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
 58651       break;
 58653     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
 58654     if( isFreeList ){
 58655       int n = get4byte(&pOvflData[4]);
 58656 #ifndef SQLITE_OMIT_AUTOVACUUM
 58657       if( pCheck->pBt->autoVacuum ){
 58658         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
 58660 #endif
 58661       if( n>(int)pCheck->pBt->usableSize/4-2 ){
 58662         checkAppendMsg(pCheck, zContext,
 58663            "freelist leaf count too big on page %d", iPage);
 58664         N--;
 58665       }else{
 58666         for(i=0; i<n; i++){
 58667           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
 58668 #ifndef SQLITE_OMIT_AUTOVACUUM
 58669           if( pCheck->pBt->autoVacuum ){
 58670             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
 58672 #endif
 58673           checkRef(pCheck, iFreePage, zContext);
 58675         N -= n;
 58678 #ifndef SQLITE_OMIT_AUTOVACUUM
 58679     else{
 58680       /* If this database supports auto-vacuum and iPage is not the last
 58681       ** page in this overflow list, check that the pointer-map entry for
 58682       ** the following page matches iPage.
 58683       */
 58684       if( pCheck->pBt->autoVacuum && N>0 ){
 58685         i = get4byte(pOvflData);
 58686         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
 58689 #endif
 58690     iPage = get4byte(pOvflData);
 58691     sqlite3PagerUnref(pOvflPage);
 58694 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 58696 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 58697 /*
 58698 ** Do various sanity checks on a single page of a tree.  Return
 58699 ** the tree depth.  Root pages return 0.  Parents of root pages
 58700 ** return 1, and so forth.
 58701 ** 
 58702 ** These checks are done:
 58703 **
 58704 **      1.  Make sure that cells and freeblocks do not overlap
 58705 **          but combine to completely cover the page.
 58706 **  NO  2.  Make sure cell keys are in order.
 58707 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
 58708 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
 58709 **      5.  Check the integrity of overflow pages.
 58710 **      6.  Recursively call checkTreePage on all children.
 58711 **      7.  Verify that the depth of all children is the same.
 58712 **      8.  Make sure this page is at least 33% full or else it is
 58713 **          the root of the tree.
 58714 */
 58715 static int checkTreePage(
 58716   IntegrityCk *pCheck,  /* Context for the sanity check */
 58717   int iPage,            /* Page number of the page to check */
 58718   char *zParentContext, /* Parent context */
 58719   i64 *pnParentMinKey, 
 58720   i64 *pnParentMaxKey
 58721 ){
 58722   MemPage *pPage;
 58723   int i, rc, depth, d2, pgno, cnt;
 58724   int hdr, cellStart;
 58725   int nCell;
 58726   u8 *data;
 58727   BtShared *pBt;
 58728   int usableSize;
 58729   char zContext[100];
 58730   char *hit = 0;
 58731   i64 nMinKey = 0;
 58732   i64 nMaxKey = 0;
 58734   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
 58736   /* Check that the page exists
 58737   */
 58738   pBt = pCheck->pBt;
 58739   usableSize = pBt->usableSize;
 58740   if( iPage==0 ) return 0;
 58741   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
 58742   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
 58743     checkAppendMsg(pCheck, zContext,
 58744        "unable to get the page. error code=%d", rc);
 58745     return 0;
 58748   /* Clear MemPage.isInit to make sure the corruption detection code in
 58749   ** btreeInitPage() is executed.  */
 58750   pPage->isInit = 0;
 58751   if( (rc = btreeInitPage(pPage))!=0 ){
 58752     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
 58753     checkAppendMsg(pCheck, zContext, 
 58754                    "btreeInitPage() returns error code %d", rc);
 58755     releasePage(pPage);
 58756     return 0;
 58759   /* Check out all the cells.
 58760   */
 58761   depth = 0;
 58762   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
 58763     u8 *pCell;
 58764     u32 sz;
 58765     CellInfo info;
 58767     /* Check payload overflow pages
 58768     */
 58769     sqlite3_snprintf(sizeof(zContext), zContext,
 58770              "On tree page %d cell %d: ", iPage, i);
 58771     pCell = findCell(pPage,i);
 58772     btreeParseCellPtr(pPage, pCell, &info);
 58773     sz = info.nData;
 58774     if( !pPage->intKey ) sz += (int)info.nKey;
 58775     /* For intKey pages, check that the keys are in order.
 58776     */
 58777     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
 58778     else{
 58779       if( info.nKey <= nMaxKey ){
 58780         checkAppendMsg(pCheck, zContext, 
 58781             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
 58783       nMaxKey = info.nKey;
 58785     assert( sz==info.nPayload );
 58786     if( (sz>info.nLocal) 
 58787      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
 58788     ){
 58789       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
 58790       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
 58791 #ifndef SQLITE_OMIT_AUTOVACUUM
 58792       if( pBt->autoVacuum ){
 58793         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
 58795 #endif
 58796       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
 58799     /* Check sanity of left child page.
 58800     */
 58801     if( !pPage->leaf ){
 58802       pgno = get4byte(pCell);
 58803 #ifndef SQLITE_OMIT_AUTOVACUUM
 58804       if( pBt->autoVacuum ){
 58805         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 58807 #endif
 58808       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
 58809       if( i>0 && d2!=depth ){
 58810         checkAppendMsg(pCheck, zContext, "Child page depth differs");
 58812       depth = d2;
 58816   if( !pPage->leaf ){
 58817     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
 58818     sqlite3_snprintf(sizeof(zContext), zContext, 
 58819                      "On page %d at right child: ", iPage);
 58820 #ifndef SQLITE_OMIT_AUTOVACUUM
 58821     if( pBt->autoVacuum ){
 58822       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
 58824 #endif
 58825     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
 58828   /* For intKey leaf pages, check that the min/max keys are in order
 58829   ** with any left/parent/right pages.
 58830   */
 58831   if( pPage->leaf && pPage->intKey ){
 58832     /* if we are a left child page */
 58833     if( pnParentMinKey ){
 58834       /* if we are the left most child page */
 58835       if( !pnParentMaxKey ){
 58836         if( nMaxKey > *pnParentMinKey ){
 58837           checkAppendMsg(pCheck, zContext, 
 58838               "Rowid %lld out of order (max larger than parent min of %lld)",
 58839               nMaxKey, *pnParentMinKey);
 58841       }else{
 58842         if( nMinKey <= *pnParentMinKey ){
 58843           checkAppendMsg(pCheck, zContext, 
 58844               "Rowid %lld out of order (min less than parent min of %lld)",
 58845               nMinKey, *pnParentMinKey);
 58847         if( nMaxKey > *pnParentMaxKey ){
 58848           checkAppendMsg(pCheck, zContext, 
 58849               "Rowid %lld out of order (max larger than parent max of %lld)",
 58850               nMaxKey, *pnParentMaxKey);
 58852         *pnParentMinKey = nMaxKey;
 58854     /* else if we're a right child page */
 58855     } else if( pnParentMaxKey ){
 58856       if( nMinKey <= *pnParentMaxKey ){
 58857         checkAppendMsg(pCheck, zContext, 
 58858             "Rowid %lld out of order (min less than parent max of %lld)",
 58859             nMinKey, *pnParentMaxKey);
 58864   /* Check for complete coverage of the page
 58865   */
 58866   data = pPage->aData;
 58867   hdr = pPage->hdrOffset;
 58868   hit = sqlite3PageMalloc( pBt->pageSize );
 58869   if( hit==0 ){
 58870     pCheck->mallocFailed = 1;
 58871   }else{
 58872     int contentOffset = get2byteNotZero(&data[hdr+5]);
 58873     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
 58874     memset(hit+contentOffset, 0, usableSize-contentOffset);
 58875     memset(hit, 1, contentOffset);
 58876     nCell = get2byte(&data[hdr+3]);
 58877     cellStart = hdr + 12 - 4*pPage->leaf;
 58878     for(i=0; i<nCell; i++){
 58879       int pc = get2byte(&data[cellStart+i*2]);
 58880       u32 size = 65536;
 58881       int j;
 58882       if( pc<=usableSize-4 ){
 58883         size = cellSizePtr(pPage, &data[pc]);
 58885       if( (int)(pc+size-1)>=usableSize ){
 58886         checkAppendMsg(pCheck, 0, 
 58887             "Corruption detected in cell %d on page %d",i,iPage);
 58888       }else{
 58889         for(j=pc+size-1; j>=pc; j--) hit[j]++;
 58892     i = get2byte(&data[hdr+1]);
 58893     while( i>0 ){
 58894       int size, j;
 58895       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
 58896       size = get2byte(&data[i+2]);
 58897       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
 58898       for(j=i+size-1; j>=i; j--) hit[j]++;
 58899       j = get2byte(&data[i]);
 58900       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
 58901       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
 58902       i = j;
 58904     for(i=cnt=0; i<usableSize; i++){
 58905       if( hit[i]==0 ){
 58906         cnt++;
 58907       }else if( hit[i]>1 ){
 58908         checkAppendMsg(pCheck, 0,
 58909           "Multiple uses for byte %d of page %d", i, iPage);
 58910         break;
 58913     if( cnt!=data[hdr+7] ){
 58914       checkAppendMsg(pCheck, 0, 
 58915           "Fragmentation of %d bytes reported as %d on page %d",
 58916           cnt, data[hdr+7], iPage);
 58919   sqlite3PageFree(hit);
 58920   releasePage(pPage);
 58921   return depth+1;
 58923 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 58925 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 58926 /*
 58927 ** This routine does a complete check of the given BTree file.  aRoot[] is
 58928 ** an array of pages numbers were each page number is the root page of
 58929 ** a table.  nRoot is the number of entries in aRoot.
 58930 **
 58931 ** A read-only or read-write transaction must be opened before calling
 58932 ** this function.
 58933 **
 58934 ** Write the number of error seen in *pnErr.  Except for some memory
 58935 ** allocation errors,  an error message held in memory obtained from
 58936 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
 58937 ** returned.  If a memory allocation error occurs, NULL is returned.
 58938 */
 58939 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
 58940   Btree *p,     /* The btree to be checked */
 58941   int *aRoot,   /* An array of root pages numbers for individual trees */
 58942   int nRoot,    /* Number of entries in aRoot[] */
 58943   int mxErr,    /* Stop reporting errors after this many */
 58944   int *pnErr    /* Write number of errors seen to this variable */
 58945 ){
 58946   Pgno i;
 58947   int nRef;
 58948   IntegrityCk sCheck;
 58949   BtShared *pBt = p->pBt;
 58950   char zErr[100];
 58952   sqlite3BtreeEnter(p);
 58953   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
 58954   nRef = sqlite3PagerRefcount(pBt->pPager);
 58955   sCheck.pBt = pBt;
 58956   sCheck.pPager = pBt->pPager;
 58957   sCheck.nPage = btreePagecount(sCheck.pBt);
 58958   sCheck.mxErr = mxErr;
 58959   sCheck.nErr = 0;
 58960   sCheck.mallocFailed = 0;
 58961   *pnErr = 0;
 58962   if( sCheck.nPage==0 ){
 58963     sqlite3BtreeLeave(p);
 58964     return 0;
 58967   sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
 58968   if( !sCheck.aPgRef ){
 58969     *pnErr = 1;
 58970     sqlite3BtreeLeave(p);
 58971     return 0;
 58973   i = PENDING_BYTE_PAGE(pBt);
 58974   if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
 58975   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
 58976   sCheck.errMsg.useMalloc = 2;
 58978   /* Check the integrity of the freelist
 58979   */
 58980   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 58981             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
 58983   /* Check all the tables.
 58984   */
 58985   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
 58986     if( aRoot[i]==0 ) continue;
 58987 #ifndef SQLITE_OMIT_AUTOVACUUM
 58988     if( pBt->autoVacuum && aRoot[i]>1 ){
 58989       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
 58991 #endif
 58992     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
 58995   /* Make sure every page in the file is referenced
 58996   */
 58997   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
 58998 #ifdef SQLITE_OMIT_AUTOVACUUM
 58999     if( getPageReferenced(&sCheck, i)==0 ){
 59000       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 59002 #else
 59003     /* If the database supports auto-vacuum, make sure no tables contain
 59004     ** references to pointer-map pages.
 59005     */
 59006     if( getPageReferenced(&sCheck, i)==0 && 
 59007        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
 59008       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
 59010     if( getPageReferenced(&sCheck, i)!=0 && 
 59011        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
 59012       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
 59014 #endif
 59017   /* Make sure this analysis did not leave any unref() pages.
 59018   ** This is an internal consistency check; an integrity check
 59019   ** of the integrity check.
 59020   */
 59021   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
 59022     checkAppendMsg(&sCheck, 0, 
 59023       "Outstanding page count goes from %d to %d during this analysis",
 59024       nRef, sqlite3PagerRefcount(pBt->pPager)
 59025     );
 59028   /* Clean  up and report errors.
 59029   */
 59030   sqlite3BtreeLeave(p);
 59031   sqlite3_free(sCheck.aPgRef);
 59032   if( sCheck.mallocFailed ){
 59033     sqlite3StrAccumReset(&sCheck.errMsg);
 59034     *pnErr = sCheck.nErr+1;
 59035     return 0;
 59037   *pnErr = sCheck.nErr;
 59038   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
 59039   return sqlite3StrAccumFinish(&sCheck.errMsg);
 59041 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 59043 /*
 59044 ** Return the full pathname of the underlying database file.  Return
 59045 ** an empty string if the database is in-memory or a TEMP database.
 59046 **
 59047 ** The pager filename is invariant as long as the pager is
 59048 ** open so it is safe to access without the BtShared mutex.
 59049 */
 59050 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
 59051   assert( p->pBt->pPager!=0 );
 59052   return sqlite3PagerFilename(p->pBt->pPager, 1);
 59055 /*
 59056 ** Return the pathname of the journal file for this database. The return
 59057 ** value of this routine is the same regardless of whether the journal file
 59058 ** has been created or not.
 59059 **
 59060 ** The pager journal filename is invariant as long as the pager is
 59061 ** open so it is safe to access without the BtShared mutex.
 59062 */
 59063 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
 59064   assert( p->pBt->pPager!=0 );
 59065   return sqlite3PagerJournalname(p->pBt->pPager);
 59068 /*
 59069 ** Return non-zero if a transaction is active.
 59070 */
 59071 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
 59072   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
 59073   return (p && (p->inTrans==TRANS_WRITE));
 59076 #ifndef SQLITE_OMIT_WAL
 59077 /*
 59078 ** Run a checkpoint on the Btree passed as the first argument.
 59079 **
 59080 ** Return SQLITE_LOCKED if this or any other connection has an open 
 59081 ** transaction on the shared-cache the argument Btree is connected to.
 59082 **
 59083 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 59084 */
 59085 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
 59086   int rc = SQLITE_OK;
 59087   if( p ){
 59088     BtShared *pBt = p->pBt;
 59089     sqlite3BtreeEnter(p);
 59090     if( pBt->inTransaction!=TRANS_NONE ){
 59091       rc = SQLITE_LOCKED;
 59092     }else{
 59093       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
 59095     sqlite3BtreeLeave(p);
 59097   return rc;
 59099 #endif
 59101 /*
 59102 ** Return non-zero if a read (or write) transaction is active.
 59103 */
 59104 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
 59105   assert( p );
 59106   assert( sqlite3_mutex_held(p->db->mutex) );
 59107   return p->inTrans!=TRANS_NONE;
 59110 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
 59111   assert( p );
 59112   assert( sqlite3_mutex_held(p->db->mutex) );
 59113   return p->nBackup!=0;
 59116 /*
 59117 ** This function returns a pointer to a blob of memory associated with
 59118 ** a single shared-btree. The memory is used by client code for its own
 59119 ** purposes (for example, to store a high-level schema associated with 
 59120 ** the shared-btree). The btree layer manages reference counting issues.
 59121 **
 59122 ** The first time this is called on a shared-btree, nBytes bytes of memory
 59123 ** are allocated, zeroed, and returned to the caller. For each subsequent 
 59124 ** call the nBytes parameter is ignored and a pointer to the same blob
 59125 ** of memory returned. 
 59126 **
 59127 ** If the nBytes parameter is 0 and the blob of memory has not yet been
 59128 ** allocated, a null pointer is returned. If the blob has already been
 59129 ** allocated, it is returned as normal.
 59130 **
 59131 ** Just before the shared-btree is closed, the function passed as the 
 59132 ** xFree argument when the memory allocation was made is invoked on the 
 59133 ** blob of allocated memory. The xFree function should not call sqlite3_free()
 59134 ** on the memory, the btree layer does that.
 59135 */
 59136 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
 59137   BtShared *pBt = p->pBt;
 59138   sqlite3BtreeEnter(p);
 59139   if( !pBt->pSchema && nBytes ){
 59140     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
 59141     pBt->xFreeSchema = xFree;
 59143   sqlite3BtreeLeave(p);
 59144   return pBt->pSchema;
 59147 /*
 59148 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
 59149 ** btree as the argument handle holds an exclusive lock on the 
 59150 ** sqlite_master table. Otherwise SQLITE_OK.
 59151 */
 59152 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
 59153   int rc;
 59154   assert( sqlite3_mutex_held(p->db->mutex) );
 59155   sqlite3BtreeEnter(p);
 59156   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
 59157   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
 59158   sqlite3BtreeLeave(p);
 59159   return rc;
 59163 #ifndef SQLITE_OMIT_SHARED_CACHE
 59164 /*
 59165 ** Obtain a lock on the table whose root page is iTab.  The
 59166 ** lock is a write lock if isWritelock is true or a read lock
 59167 ** if it is false.
 59168 */
 59169 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
 59170   int rc = SQLITE_OK;
 59171   assert( p->inTrans!=TRANS_NONE );
 59172   if( p->sharable ){
 59173     u8 lockType = READ_LOCK + isWriteLock;
 59174     assert( READ_LOCK+1==WRITE_LOCK );
 59175     assert( isWriteLock==0 || isWriteLock==1 );
 59177     sqlite3BtreeEnter(p);
 59178     rc = querySharedCacheTableLock(p, iTab, lockType);
 59179     if( rc==SQLITE_OK ){
 59180       rc = setSharedCacheTableLock(p, iTab, lockType);
 59182     sqlite3BtreeLeave(p);
 59184   return rc;
 59186 #endif
 59188 #ifndef SQLITE_OMIT_INCRBLOB
 59189 /*
 59190 ** Argument pCsr must be a cursor opened for writing on an 
 59191 ** INTKEY table currently pointing at a valid table entry. 
 59192 ** This function modifies the data stored as part of that entry.
 59193 **
 59194 ** Only the data content may only be modified, it is not possible to 
 59195 ** change the length of the data stored. If this function is called with
 59196 ** parameters that attempt to write past the end of the existing data,
 59197 ** no modifications are made and SQLITE_CORRUPT is returned.
 59198 */
 59199 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
 59200   int rc;
 59201   assert( cursorHoldsMutex(pCsr) );
 59202   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
 59203   assert( pCsr->isIncrblobHandle );
 59205   rc = restoreCursorPosition(pCsr);
 59206   if( rc!=SQLITE_OK ){
 59207     return rc;
 59209   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
 59210   if( pCsr->eState!=CURSOR_VALID ){
 59211     return SQLITE_ABORT;
 59214   /* Save the positions of all other cursors open on this table. This is
 59215   ** required in case any of them are holding references to an xFetch
 59216   ** version of the b-tree page modified by the accessPayload call below.
 59217   **
 59218   ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
 59219   ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
 59220   ** saveAllCursors can only return SQLITE_OK.
 59221   */
 59222   VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
 59223   assert( rc==SQLITE_OK );
 59225   /* Check some assumptions: 
 59226   **   (a) the cursor is open for writing,
 59227   **   (b) there is a read/write transaction open,
 59228   **   (c) the connection holds a write-lock on the table (if required),
 59229   **   (d) there are no conflicting read-locks, and
 59230   **   (e) the cursor points at a valid row of an intKey table.
 59231   */
 59232   if( !pCsr->wrFlag ){
 59233     return SQLITE_READONLY;
 59235   assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
 59236               && pCsr->pBt->inTransaction==TRANS_WRITE );
 59237   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
 59238   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
 59239   assert( pCsr->apPage[pCsr->iPage]->intKey );
 59241   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
 59244 /* 
 59245 ** Set a flag on this cursor to cache the locations of pages from the 
 59246 ** overflow list for the current row. This is used by cursors opened
 59247 ** for incremental blob IO only.
 59248 **
 59249 ** This function sets a flag only. The actual page location cache
 59250 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
 59251 ** accessPayload() (the worker function for sqlite3BtreeData() and
 59252 ** sqlite3BtreePutData()).
 59253 */
 59254 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
 59255   assert( cursorHoldsMutex(pCur) );
 59256   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
 59257   invalidateOverflowCache(pCur);
 59258   pCur->isIncrblobHandle = 1;
 59260 #endif
 59262 /*
 59263 ** Set both the "read version" (single byte at byte offset 18) and 
 59264 ** "write version" (single byte at byte offset 19) fields in the database
 59265 ** header to iVersion.
 59266 */
 59267 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
 59268   BtShared *pBt = pBtree->pBt;
 59269   int rc;                         /* Return code */
 59271   assert( iVersion==1 || iVersion==2 );
 59273   /* If setting the version fields to 1, do not automatically open the
 59274   ** WAL connection, even if the version fields are currently set to 2.
 59275   */
 59276   pBt->btsFlags &= ~BTS_NO_WAL;
 59277   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
 59279   rc = sqlite3BtreeBeginTrans(pBtree, 0);
 59280   if( rc==SQLITE_OK ){
 59281     u8 *aData = pBt->pPage1->aData;
 59282     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
 59283       rc = sqlite3BtreeBeginTrans(pBtree, 2);
 59284       if( rc==SQLITE_OK ){
 59285         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 59286         if( rc==SQLITE_OK ){
 59287           aData[18] = (u8)iVersion;
 59288           aData[19] = (u8)iVersion;
 59294   pBt->btsFlags &= ~BTS_NO_WAL;
 59295   return rc;
 59298 /*
 59299 ** set the mask of hint flags for cursor pCsr. Currently the only valid
 59300 ** values are 0 and BTREE_BULKLOAD.
 59301 */
 59302 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
 59303   assert( mask==BTREE_BULKLOAD || mask==0 );
 59304   pCsr->hints = mask;
 59307 /************** End of btree.c ***********************************************/
 59308 /************** Begin file backup.c ******************************************/
 59309 /*
 59310 ** 2009 January 28
 59311 **
 59312 ** The author disclaims copyright to this source code.  In place of
 59313 ** a legal notice, here is a blessing:
 59314 **
 59315 **    May you do good and not evil.
 59316 **    May you find forgiveness for yourself and forgive others.
 59317 **    May you share freely, never taking more than you give.
 59318 **
 59319 *************************************************************************
 59320 ** This file contains the implementation of the sqlite3_backup_XXX() 
 59321 ** API functions and the related features.
 59322 */
 59324 /*
 59325 ** Structure allocated for each backup operation.
 59326 */
 59327 struct sqlite3_backup {
 59328   sqlite3* pDestDb;        /* Destination database handle */
 59329   Btree *pDest;            /* Destination b-tree file */
 59330   u32 iDestSchema;         /* Original schema cookie in destination */
 59331   int bDestLocked;         /* True once a write-transaction is open on pDest */
 59333   Pgno iNext;              /* Page number of the next source page to copy */
 59334   sqlite3* pSrcDb;         /* Source database handle */
 59335   Btree *pSrc;             /* Source b-tree file */
 59337   int rc;                  /* Backup process error code */
 59339   /* These two variables are set by every call to backup_step(). They are
 59340   ** read by calls to backup_remaining() and backup_pagecount().
 59341   */
 59342   Pgno nRemaining;         /* Number of pages left to copy */
 59343   Pgno nPagecount;         /* Total number of pages to copy */
 59345   int isAttached;          /* True once backup has been registered with pager */
 59346   sqlite3_backup *pNext;   /* Next backup associated with source pager */
 59347 };
 59349 /*
 59350 ** THREAD SAFETY NOTES:
 59351 **
 59352 **   Once it has been created using backup_init(), a single sqlite3_backup
 59353 **   structure may be accessed via two groups of thread-safe entry points:
 59354 **
 59355 **     * Via the sqlite3_backup_XXX() API function backup_step() and 
 59356 **       backup_finish(). Both these functions obtain the source database
 59357 **       handle mutex and the mutex associated with the source BtShared 
 59358 **       structure, in that order.
 59359 **
 59360 **     * Via the BackupUpdate() and BackupRestart() functions, which are
 59361 **       invoked by the pager layer to report various state changes in
 59362 **       the page cache associated with the source database. The mutex
 59363 **       associated with the source database BtShared structure will always 
 59364 **       be held when either of these functions are invoked.
 59365 **
 59366 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
 59367 **   backup_pagecount() are not thread-safe functions. If they are called
 59368 **   while some other thread is calling backup_step() or backup_finish(),
 59369 **   the values returned may be invalid. There is no way for a call to
 59370 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
 59371 **   or backup_pagecount().
 59372 **
 59373 **   Depending on the SQLite configuration, the database handles and/or
 59374 **   the Btree objects may have their own mutexes that require locking.
 59375 **   Non-sharable Btrees (in-memory databases for example), do not have
 59376 **   associated mutexes.
 59377 */
 59379 /*
 59380 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
 59381 ** in connection handle pDb. If such a database cannot be found, return
 59382 ** a NULL pointer and write an error message to pErrorDb.
 59383 **
 59384 ** If the "temp" database is requested, it may need to be opened by this 
 59385 ** function. If an error occurs while doing so, return 0 and write an 
 59386 ** error message to pErrorDb.
 59387 */
 59388 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
 59389   int i = sqlite3FindDbName(pDb, zDb);
 59391   if( i==1 ){
 59392     Parse *pParse;
 59393     int rc = 0;
 59394     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
 59395     if( pParse==0 ){
 59396       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
 59397       rc = SQLITE_NOMEM;
 59398     }else{
 59399       pParse->db = pDb;
 59400       if( sqlite3OpenTempDatabase(pParse) ){
 59401         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
 59402         rc = SQLITE_ERROR;
 59404       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
 59405       sqlite3ParserReset(pParse);
 59406       sqlite3StackFree(pErrorDb, pParse);
 59408     if( rc ){
 59409       return 0;
 59413   if( i<0 ){
 59414     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
 59415     return 0;
 59418   return pDb->aDb[i].pBt;
 59421 /*
 59422 ** Attempt to set the page size of the destination to match the page size
 59423 ** of the source.
 59424 */
 59425 static int setDestPgsz(sqlite3_backup *p){
 59426   int rc;
 59427   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
 59428   return rc;
 59431 /*
 59432 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
 59433 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
 59434 ** a pointer to the new sqlite3_backup object.
 59435 **
 59436 ** If an error occurs, NULL is returned and an error code and error message
 59437 ** stored in database handle pDestDb.
 59438 */
 59439 SQLITE_API sqlite3_backup *sqlite3_backup_init(
 59440   sqlite3* pDestDb,                     /* Database to write to */
 59441   const char *zDestDb,                  /* Name of database within pDestDb */
 59442   sqlite3* pSrcDb,                      /* Database connection to read from */
 59443   const char *zSrcDb                    /* Name of database within pSrcDb */
 59444 ){
 59445   sqlite3_backup *p;                    /* Value to return */
 59447   /* Lock the source database handle. The destination database
 59448   ** handle is not locked in this routine, but it is locked in
 59449   ** sqlite3_backup_step(). The user is required to ensure that no
 59450   ** other thread accesses the destination handle for the duration
 59451   ** of the backup operation.  Any attempt to use the destination
 59452   ** database connection while a backup is in progress may cause
 59453   ** a malfunction or a deadlock.
 59454   */
 59455   sqlite3_mutex_enter(pSrcDb->mutex);
 59456   sqlite3_mutex_enter(pDestDb->mutex);
 59458   if( pSrcDb==pDestDb ){
 59459     sqlite3Error(
 59460         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
 59461     );
 59462     p = 0;
 59463   }else {
 59464     /* Allocate space for a new sqlite3_backup object...
 59465     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 59466     ** call to sqlite3_backup_init() and is destroyed by a call to
 59467     ** sqlite3_backup_finish(). */
 59468     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
 59469     if( !p ){
 59470       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
 59474   /* If the allocation succeeded, populate the new object. */
 59475   if( p ){
 59476     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
 59477     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
 59478     p->pDestDb = pDestDb;
 59479     p->pSrcDb = pSrcDb;
 59480     p->iNext = 1;
 59481     p->isAttached = 0;
 59483     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
 59484       /* One (or both) of the named databases did not exist or an OOM
 59485       ** error was hit.  The error has already been written into the
 59486       ** pDestDb handle.  All that is left to do here is free the
 59487       ** sqlite3_backup structure.
 59488       */
 59489       sqlite3_free(p);
 59490       p = 0;
 59493   if( p ){
 59494     p->pSrc->nBackup++;
 59497   sqlite3_mutex_leave(pDestDb->mutex);
 59498   sqlite3_mutex_leave(pSrcDb->mutex);
 59499   return p;
 59502 /*
 59503 ** Argument rc is an SQLite error code. Return true if this error is 
 59504 ** considered fatal if encountered during a backup operation. All errors
 59505 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
 59506 */
 59507 static int isFatalError(int rc){
 59508   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
 59511 /*
 59512 ** Parameter zSrcData points to a buffer containing the data for 
 59513 ** page iSrcPg from the source database. Copy this data into the 
 59514 ** destination database.
 59515 */
 59516 static int backupOnePage(
 59517   sqlite3_backup *p,              /* Backup handle */
 59518   Pgno iSrcPg,                    /* Source database page to backup */
 59519   const u8 *zSrcData,             /* Source database page data */
 59520   int bUpdate                     /* True for an update, false otherwise */
 59521 ){
 59522   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
 59523   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
 59524   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
 59525   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
 59526   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
 59527 #ifdef SQLITE_HAS_CODEC
 59528   /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
 59529   ** guaranteed that the shared-mutex is held by this thread, handle
 59530   ** p->pSrc may not actually be the owner.  */
 59531   int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
 59532   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
 59533 #endif
 59534   int rc = SQLITE_OK;
 59535   i64 iOff;
 59537   assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
 59538   assert( p->bDestLocked );
 59539   assert( !isFatalError(p->rc) );
 59540   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
 59541   assert( zSrcData );
 59543   /* Catch the case where the destination is an in-memory database and the
 59544   ** page sizes of the source and destination differ. 
 59545   */
 59546   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
 59547     rc = SQLITE_READONLY;
 59550 #ifdef SQLITE_HAS_CODEC
 59551   /* Backup is not possible if the page size of the destination is changing
 59552   ** and a codec is in use.
 59553   */
 59554   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
 59555     rc = SQLITE_READONLY;
 59558   /* Backup is not possible if the number of bytes of reserve space differ
 59559   ** between source and destination.  If there is a difference, try to
 59560   ** fix the destination to agree with the source.  If that is not possible,
 59561   ** then the backup cannot proceed.
 59562   */
 59563   if( nSrcReserve!=nDestReserve ){
 59564     u32 newPgsz = nSrcPgsz;
 59565     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
 59566     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
 59568 #endif
 59570   /* This loop runs once for each destination page spanned by the source 
 59571   ** page. For each iteration, variable iOff is set to the byte offset
 59572   ** of the destination page.
 59573   */
 59574   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
 59575     DbPage *pDestPg = 0;
 59576     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
 59577     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
 59578     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
 59579      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
 59580     ){
 59581       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
 59582       u8 *zDestData = sqlite3PagerGetData(pDestPg);
 59583       u8 *zOut = &zDestData[iOff%nDestPgsz];
 59585       /* Copy the data from the source page into the destination page.
 59586       ** Then clear the Btree layer MemPage.isInit flag. Both this module
 59587       ** and the pager code use this trick (clearing the first byte
 59588       ** of the page 'extra' space to invalidate the Btree layers
 59589       ** cached parse of the page). MemPage.isInit is marked 
 59590       ** "MUST BE FIRST" for this purpose.
 59591       */
 59592       memcpy(zOut, zIn, nCopy);
 59593       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
 59594       if( iOff==0 && bUpdate==0 ){
 59595         sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
 59598     sqlite3PagerUnref(pDestPg);
 59601   return rc;
 59604 /*
 59605 ** If pFile is currently larger than iSize bytes, then truncate it to
 59606 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
 59607 ** this function is a no-op.
 59608 **
 59609 ** Return SQLITE_OK if everything is successful, or an SQLite error 
 59610 ** code if an error occurs.
 59611 */
 59612 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
 59613   i64 iCurrent;
 59614   int rc = sqlite3OsFileSize(pFile, &iCurrent);
 59615   if( rc==SQLITE_OK && iCurrent>iSize ){
 59616     rc = sqlite3OsTruncate(pFile, iSize);
 59618   return rc;
 59621 /*
 59622 ** Register this backup object with the associated source pager for
 59623 ** callbacks when pages are changed or the cache invalidated.
 59624 */
 59625 static void attachBackupObject(sqlite3_backup *p){
 59626   sqlite3_backup **pp;
 59627   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
 59628   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 59629   p->pNext = *pp;
 59630   *pp = p;
 59631   p->isAttached = 1;
 59634 /*
 59635 ** Copy nPage pages from the source b-tree to the destination.
 59636 */
 59637 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
 59638   int rc;
 59639   int destMode;       /* Destination journal mode */
 59640   int pgszSrc = 0;    /* Source page size */
 59641   int pgszDest = 0;   /* Destination page size */
 59643   sqlite3_mutex_enter(p->pSrcDb->mutex);
 59644   sqlite3BtreeEnter(p->pSrc);
 59645   if( p->pDestDb ){
 59646     sqlite3_mutex_enter(p->pDestDb->mutex);
 59649   rc = p->rc;
 59650   if( !isFatalError(rc) ){
 59651     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
 59652     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
 59653     int ii;                            /* Iterator variable */
 59654     int nSrcPage = -1;                 /* Size of source db in pages */
 59655     int bCloseTrans = 0;               /* True if src db requires unlocking */
 59657     /* If the source pager is currently in a write-transaction, return
 59658     ** SQLITE_BUSY immediately.
 59659     */
 59660     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
 59661       rc = SQLITE_BUSY;
 59662     }else{
 59663       rc = SQLITE_OK;
 59666     /* Lock the destination database, if it is not locked already. */
 59667     if( SQLITE_OK==rc && p->bDestLocked==0
 59668      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
 59669     ){
 59670       p->bDestLocked = 1;
 59671       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
 59674     /* If there is no open read-transaction on the source database, open
 59675     ** one now. If a transaction is opened here, then it will be closed
 59676     ** before this function exits.
 59677     */
 59678     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
 59679       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
 59680       bCloseTrans = 1;
 59683     /* Do not allow backup if the destination database is in WAL mode
 59684     ** and the page sizes are different between source and destination */
 59685     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
 59686     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
 59687     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
 59688     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
 59689       rc = SQLITE_READONLY;
 59692     /* Now that there is a read-lock on the source database, query the
 59693     ** source pager for the number of pages in the database.
 59694     */
 59695     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
 59696     assert( nSrcPage>=0 );
 59697     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
 59698       const Pgno iSrcPg = p->iNext;                 /* Source page number */
 59699       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
 59700         DbPage *pSrcPg;                             /* Source page object */
 59701         rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
 59702                                  PAGER_GET_READONLY);
 59703         if( rc==SQLITE_OK ){
 59704           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
 59705           sqlite3PagerUnref(pSrcPg);
 59708       p->iNext++;
 59710     if( rc==SQLITE_OK ){
 59711       p->nPagecount = nSrcPage;
 59712       p->nRemaining = nSrcPage+1-p->iNext;
 59713       if( p->iNext>(Pgno)nSrcPage ){
 59714         rc = SQLITE_DONE;
 59715       }else if( !p->isAttached ){
 59716         attachBackupObject(p);
 59720     /* Update the schema version field in the destination database. This
 59721     ** is to make sure that the schema-version really does change in
 59722     ** the case where the source and destination databases have the
 59723     ** same schema version.
 59724     */
 59725     if( rc==SQLITE_DONE ){
 59726       if( nSrcPage==0 ){
 59727         rc = sqlite3BtreeNewDb(p->pDest);
 59728         nSrcPage = 1;
 59730       if( rc==SQLITE_OK || rc==SQLITE_DONE ){
 59731         rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
 59733       if( rc==SQLITE_OK ){
 59734         if( p->pDestDb ){
 59735           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
 59737         if( destMode==PAGER_JOURNALMODE_WAL ){
 59738           rc = sqlite3BtreeSetVersion(p->pDest, 2);
 59741       if( rc==SQLITE_OK ){
 59742         int nDestTruncate;
 59743         /* Set nDestTruncate to the final number of pages in the destination
 59744         ** database. The complication here is that the destination page
 59745         ** size may be different to the source page size. 
 59746         **
 59747         ** If the source page size is smaller than the destination page size, 
 59748         ** round up. In this case the call to sqlite3OsTruncate() below will
 59749         ** fix the size of the file. However it is important to call
 59750         ** sqlite3PagerTruncateImage() here so that any pages in the 
 59751         ** destination file that lie beyond the nDestTruncate page mark are
 59752         ** journalled by PagerCommitPhaseOne() before they are destroyed
 59753         ** by the file truncation.
 59754         */
 59755         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
 59756         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
 59757         if( pgszSrc<pgszDest ){
 59758           int ratio = pgszDest/pgszSrc;
 59759           nDestTruncate = (nSrcPage+ratio-1)/ratio;
 59760           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
 59761             nDestTruncate--;
 59763         }else{
 59764           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
 59766         assert( nDestTruncate>0 );
 59768         if( pgszSrc<pgszDest ){
 59769           /* If the source page-size is smaller than the destination page-size,
 59770           ** two extra things may need to happen:
 59771           **
 59772           **   * The destination may need to be truncated, and
 59773           **
 59774           **   * Data stored on the pages immediately following the 
 59775           **     pending-byte page in the source database may need to be
 59776           **     copied into the destination database.
 59777           */
 59778           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
 59779           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
 59780           Pgno iPg;
 59781           int nDstPage;
 59782           i64 iOff;
 59783           i64 iEnd;
 59785           assert( pFile );
 59786           assert( nDestTruncate==0 
 59787               || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
 59788                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
 59789              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
 59790           ));
 59792           /* This block ensures that all data required to recreate the original
 59793           ** database has been stored in the journal for pDestPager and the
 59794           ** journal synced to disk. So at this point we may safely modify
 59795           ** the database file in any way, knowing that if a power failure
 59796           ** occurs, the original database will be reconstructed from the 
 59797           ** journal file.  */
 59798           sqlite3PagerPagecount(pDestPager, &nDstPage);
 59799           for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
 59800             if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
 59801               DbPage *pPg;
 59802               rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
 59803               if( rc==SQLITE_OK ){
 59804                 rc = sqlite3PagerWrite(pPg);
 59805                 sqlite3PagerUnref(pPg);
 59809           if( rc==SQLITE_OK ){
 59810             rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
 59813           /* Write the extra pages and truncate the database file as required */
 59814           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
 59815           for(
 59816             iOff=PENDING_BYTE+pgszSrc; 
 59817             rc==SQLITE_OK && iOff<iEnd; 
 59818             iOff+=pgszSrc
 59819           ){
 59820             PgHdr *pSrcPg = 0;
 59821             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
 59822             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 59823             if( rc==SQLITE_OK ){
 59824               u8 *zData = sqlite3PagerGetData(pSrcPg);
 59825               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
 59827             sqlite3PagerUnref(pSrcPg);
 59829           if( rc==SQLITE_OK ){
 59830             rc = backupTruncateFile(pFile, iSize);
 59833           /* Sync the database file to disk. */
 59834           if( rc==SQLITE_OK ){
 59835             rc = sqlite3PagerSync(pDestPager, 0);
 59837         }else{
 59838           sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
 59839           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
 59842         /* Finish committing the transaction to the destination database. */
 59843         if( SQLITE_OK==rc
 59844          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
 59845         ){
 59846           rc = SQLITE_DONE;
 59851     /* If bCloseTrans is true, then this function opened a read transaction
 59852     ** on the source database. Close the read transaction here. There is
 59853     ** no need to check the return values of the btree methods here, as
 59854     ** "committing" a read-only transaction cannot fail.
 59855     */
 59856     if( bCloseTrans ){
 59857       TESTONLY( int rc2 );
 59858       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
 59859       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
 59860       assert( rc2==SQLITE_OK );
 59863     if( rc==SQLITE_IOERR_NOMEM ){
 59864       rc = SQLITE_NOMEM;
 59866     p->rc = rc;
 59868   if( p->pDestDb ){
 59869     sqlite3_mutex_leave(p->pDestDb->mutex);
 59871   sqlite3BtreeLeave(p->pSrc);
 59872   sqlite3_mutex_leave(p->pSrcDb->mutex);
 59873   return rc;
 59876 /*
 59877 ** Release all resources associated with an sqlite3_backup* handle.
 59878 */
 59879 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
 59880   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
 59881   sqlite3 *pSrcDb;                     /* Source database connection */
 59882   int rc;                              /* Value to return */
 59884   /* Enter the mutexes */
 59885   if( p==0 ) return SQLITE_OK;
 59886   pSrcDb = p->pSrcDb;
 59887   sqlite3_mutex_enter(pSrcDb->mutex);
 59888   sqlite3BtreeEnter(p->pSrc);
 59889   if( p->pDestDb ){
 59890     sqlite3_mutex_enter(p->pDestDb->mutex);
 59893   /* Detach this backup from the source pager. */
 59894   if( p->pDestDb ){
 59895     p->pSrc->nBackup--;
 59897   if( p->isAttached ){
 59898     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
 59899     while( *pp!=p ){
 59900       pp = &(*pp)->pNext;
 59902     *pp = p->pNext;
 59905   /* If a transaction is still open on the Btree, roll it back. */
 59906   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
 59908   /* Set the error code of the destination database handle. */
 59909   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
 59910   if( p->pDestDb ){
 59911     sqlite3Error(p->pDestDb, rc, 0);
 59913     /* Exit the mutexes and free the backup context structure. */
 59914     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
 59916   sqlite3BtreeLeave(p->pSrc);
 59917   if( p->pDestDb ){
 59918     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 59919     ** call to sqlite3_backup_init() and is destroyed by a call to
 59920     ** sqlite3_backup_finish(). */
 59921     sqlite3_free(p);
 59923   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
 59924   return rc;
 59927 /*
 59928 ** Return the number of pages still to be backed up as of the most recent
 59929 ** call to sqlite3_backup_step().
 59930 */
 59931 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
 59932   return p->nRemaining;
 59935 /*
 59936 ** Return the total number of pages in the source database as of the most 
 59937 ** recent call to sqlite3_backup_step().
 59938 */
 59939 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
 59940   return p->nPagecount;
 59943 /*
 59944 ** This function is called after the contents of page iPage of the
 59945 ** source database have been modified. If page iPage has already been 
 59946 ** copied into the destination database, then the data written to the
 59947 ** destination is now invalidated. The destination copy of iPage needs
 59948 ** to be updated with the new data before the backup operation is
 59949 ** complete.
 59950 **
 59951 ** It is assumed that the mutex associated with the BtShared object
 59952 ** corresponding to the source database is held when this function is
 59953 ** called.
 59954 */
 59955 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
 59956   sqlite3_backup *p;                   /* Iterator variable */
 59957   for(p=pBackup; p; p=p->pNext){
 59958     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 59959     if( !isFatalError(p->rc) && iPage<p->iNext ){
 59960       /* The backup process p has already copied page iPage. But now it
 59961       ** has been modified by a transaction on the source pager. Copy
 59962       ** the new data into the backup.
 59963       */
 59964       int rc;
 59965       assert( p->pDestDb );
 59966       sqlite3_mutex_enter(p->pDestDb->mutex);
 59967       rc = backupOnePage(p, iPage, aData, 1);
 59968       sqlite3_mutex_leave(p->pDestDb->mutex);
 59969       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
 59970       if( rc!=SQLITE_OK ){
 59971         p->rc = rc;
 59977 /*
 59978 ** Restart the backup process. This is called when the pager layer
 59979 ** detects that the database has been modified by an external database
 59980 ** connection. In this case there is no way of knowing which of the
 59981 ** pages that have been copied into the destination database are still 
 59982 ** valid and which are not, so the entire process needs to be restarted.
 59983 **
 59984 ** It is assumed that the mutex associated with the BtShared object
 59985 ** corresponding to the source database is held when this function is
 59986 ** called.
 59987 */
 59988 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
 59989   sqlite3_backup *p;                   /* Iterator variable */
 59990   for(p=pBackup; p; p=p->pNext){
 59991     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 59992     p->iNext = 1;
 59996 #ifndef SQLITE_OMIT_VACUUM
 59997 /*
 59998 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
 59999 ** must be active for both files.
 60000 **
 60001 ** The size of file pTo may be reduced by this operation. If anything 
 60002 ** goes wrong, the transaction on pTo is rolled back. If successful, the 
 60003 ** transaction is committed before returning.
 60004 */
 60005 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
 60006   int rc;
 60007   sqlite3_file *pFd;              /* File descriptor for database pTo */
 60008   sqlite3_backup b;
 60009   sqlite3BtreeEnter(pTo);
 60010   sqlite3BtreeEnter(pFrom);
 60012   assert( sqlite3BtreeIsInTrans(pTo) );
 60013   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
 60014   if( pFd->pMethods ){
 60015     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
 60016     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
 60017     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 60018     if( rc ) goto copy_finished;
 60021   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
 60022   ** to 0. This is used by the implementations of sqlite3_backup_step()
 60023   ** and sqlite3_backup_finish() to detect that they are being called
 60024   ** from this function, not directly by the user.
 60025   */
 60026   memset(&b, 0, sizeof(b));
 60027   b.pSrcDb = pFrom->db;
 60028   b.pSrc = pFrom;
 60029   b.pDest = pTo;
 60030   b.iNext = 1;
 60032   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
 60033   ** file. By passing this as the number of pages to copy to
 60034   ** sqlite3_backup_step(), we can guarantee that the copy finishes 
 60035   ** within a single call (unless an error occurs). The assert() statement
 60036   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
 60037   ** or an error code.
 60038   */
 60039   sqlite3_backup_step(&b, 0x7FFFFFFF);
 60040   assert( b.rc!=SQLITE_OK );
 60041   rc = sqlite3_backup_finish(&b);
 60042   if( rc==SQLITE_OK ){
 60043     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
 60044   }else{
 60045     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
 60048   assert( sqlite3BtreeIsInTrans(pTo)==0 );
 60049 copy_finished:
 60050   sqlite3BtreeLeave(pFrom);
 60051   sqlite3BtreeLeave(pTo);
 60052   return rc;
 60054 #endif /* SQLITE_OMIT_VACUUM */
 60056 /************** End of backup.c **********************************************/
 60057 /************** Begin file vdbemem.c *****************************************/
 60058 /*
 60059 ** 2004 May 26
 60060 **
 60061 ** The author disclaims copyright to this source code.  In place of
 60062 ** a legal notice, here is a blessing:
 60063 **
 60064 **    May you do good and not evil.
 60065 **    May you find forgiveness for yourself and forgive others.
 60066 **    May you share freely, never taking more than you give.
 60067 **
 60068 *************************************************************************
 60069 **
 60070 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
 60071 ** stores a single value in the VDBE.  Mem is an opaque structure visible
 60072 ** only within the VDBE.  Interface routines refer to a Mem using the
 60073 ** name sqlite_value
 60074 */
 60076 #ifdef SQLITE_DEBUG
 60077 /*
 60078 ** Check invariants on a Mem object.
 60079 **
 60080 ** This routine is intended for use inside of assert() statements, like
 60081 ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
 60082 */
 60083 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
 60084   /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
 60085   ** function for Mem.z 
 60086   */
 60087   assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
 60088   assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
 60090   /* If p holds a string or blob, the Mem.z must point to exactly
 60091   ** one of the following:
 60092   **
 60093   **   (1) Memory in Mem.zMalloc and managed by the Mem object
 60094   **   (2) Memory to be freed using Mem.xDel
 60095   **   (3) An ephermal string or blob
 60096   **   (4) A static string or blob
 60097   */
 60098   if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
 60099     assert( 
 60100       ((p->z==p->zMalloc)? 1 : 0) +
 60101       ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
 60102       ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
 60103       ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
 60104     );
 60107   return 1;
 60109 #endif
 60112 /*
 60113 ** If pMem is an object with a valid string representation, this routine
 60114 ** ensures the internal encoding for the string representation is
 60115 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
 60116 **
 60117 ** If pMem is not a string object, or the encoding of the string
 60118 ** representation is already stored using the requested encoding, then this
 60119 ** routine is a no-op.
 60120 **
 60121 ** SQLITE_OK is returned if the conversion is successful (or not required).
 60122 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
 60123 ** between formats.
 60124 */
 60125 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
 60126 #ifndef SQLITE_OMIT_UTF16
 60127   int rc;
 60128 #endif
 60129   assert( (pMem->flags&MEM_RowSet)==0 );
 60130   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
 60131            || desiredEnc==SQLITE_UTF16BE );
 60132   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
 60133     return SQLITE_OK;
 60135   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60136 #ifdef SQLITE_OMIT_UTF16
 60137   return SQLITE_ERROR;
 60138 #else
 60140   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
 60141   ** then the encoding of the value may not have changed.
 60142   */
 60143   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
 60144   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
 60145   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
 60146   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
 60147   return rc;
 60148 #endif
 60151 /*
 60152 ** Make sure pMem->z points to a writable allocation of at least 
 60153 ** min(n,32) bytes.
 60154 **
 60155 ** If the bPreserve argument is true, then copy of the content of
 60156 ** pMem->z into the new allocation.  pMem must be either a string or
 60157 ** blob if bPreserve is true.  If bPreserve is false, any prior content
 60158 ** in pMem->z is discarded.
 60159 */
 60160 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
 60161   assert( sqlite3VdbeCheckMemInvariants(pMem) );
 60162   assert( (pMem->flags&MEM_RowSet)==0 );
 60164   /* If the bPreserve flag is set to true, then the memory cell must already
 60165   ** contain a valid string or blob value.  */
 60166   assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
 60167   testcase( bPreserve && pMem->z==0 );
 60169   if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
 60170     if( n<32 ) n = 32;
 60171     if( bPreserve && pMem->z==pMem->zMalloc ){
 60172       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 60173       bPreserve = 0;
 60174     }else{
 60175       sqlite3DbFree(pMem->db, pMem->zMalloc);
 60176       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
 60178     if( pMem->zMalloc==0 ){
 60179       VdbeMemRelease(pMem);
 60180       pMem->z = 0;
 60181       pMem->flags = MEM_Null;  
 60182       return SQLITE_NOMEM;
 60186   if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
 60187     memcpy(pMem->zMalloc, pMem->z, pMem->n);
 60189   if( (pMem->flags&MEM_Dyn)!=0 ){
 60190     assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
 60191     pMem->xDel((void *)(pMem->z));
 60194   pMem->z = pMem->zMalloc;
 60195   pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
 60196   pMem->xDel = 0;
 60197   return SQLITE_OK;
 60200 /*
 60201 ** Make the given Mem object MEM_Dyn.  In other words, make it so
 60202 ** that any TEXT or BLOB content is stored in memory obtained from
 60203 ** malloc().  In this way, we know that the memory is safe to be
 60204 ** overwritten or altered.
 60205 **
 60206 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
 60207 */
 60208 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
 60209   int f;
 60210   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60211   assert( (pMem->flags&MEM_RowSet)==0 );
 60212   ExpandBlob(pMem);
 60213   f = pMem->flags;
 60214   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
 60215     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
 60216       return SQLITE_NOMEM;
 60218     pMem->z[pMem->n] = 0;
 60219     pMem->z[pMem->n+1] = 0;
 60220     pMem->flags |= MEM_Term;
 60221 #ifdef SQLITE_DEBUG
 60222     pMem->pScopyFrom = 0;
 60223 #endif
 60226   return SQLITE_OK;
 60229 /*
 60230 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
 60231 ** blob stored in dynamically allocated space.
 60232 */
 60233 #ifndef SQLITE_OMIT_INCRBLOB
 60234 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
 60235   if( pMem->flags & MEM_Zero ){
 60236     int nByte;
 60237     assert( pMem->flags&MEM_Blob );
 60238     assert( (pMem->flags&MEM_RowSet)==0 );
 60239     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60241     /* Set nByte to the number of bytes required to store the expanded blob. */
 60242     nByte = pMem->n + pMem->u.nZero;
 60243     if( nByte<=0 ){
 60244       nByte = 1;
 60246     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
 60247       return SQLITE_NOMEM;
 60250     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
 60251     pMem->n += pMem->u.nZero;
 60252     pMem->flags &= ~(MEM_Zero|MEM_Term);
 60254   return SQLITE_OK;
 60256 #endif
 60259 /*
 60260 ** Make sure the given Mem is \u0000 terminated.
 60261 */
 60262 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
 60263   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60264   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
 60265     return SQLITE_OK;   /* Nothing to do */
 60267   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
 60268     return SQLITE_NOMEM;
 60270   pMem->z[pMem->n] = 0;
 60271   pMem->z[pMem->n+1] = 0;
 60272   pMem->flags |= MEM_Term;
 60273   return SQLITE_OK;
 60276 /*
 60277 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
 60278 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
 60279 ** is a no-op.
 60280 **
 60281 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
 60282 **
 60283 ** A MEM_Null value will never be passed to this function. This function is
 60284 ** used for converting values to text for returning to the user (i.e. via
 60285 ** sqlite3_value_text()), or for ensuring that values to be used as btree
 60286 ** keys are strings. In the former case a NULL pointer is returned the
 60287 ** user and the later is an internal programming error.
 60288 */
 60289 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
 60290   int rc = SQLITE_OK;
 60291   int fg = pMem->flags;
 60292   const int nByte = 32;
 60294   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60295   assert( !(fg&MEM_Zero) );
 60296   assert( !(fg&(MEM_Str|MEM_Blob)) );
 60297   assert( fg&(MEM_Int|MEM_Real) );
 60298   assert( (pMem->flags&MEM_RowSet)==0 );
 60299   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60302   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 60303     return SQLITE_NOMEM;
 60306   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
 60307   ** string representation of the value. Then, if the required encoding
 60308   ** is UTF-16le or UTF-16be do a translation.
 60309   ** 
 60310   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
 60311   */
 60312   if( fg & MEM_Int ){
 60313     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
 60314   }else{
 60315     assert( fg & MEM_Real );
 60316     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
 60318   pMem->n = sqlite3Strlen30(pMem->z);
 60319   pMem->enc = SQLITE_UTF8;
 60320   pMem->flags |= MEM_Str|MEM_Term;
 60321   sqlite3VdbeChangeEncoding(pMem, enc);
 60322   return rc;
 60325 /*
 60326 ** Memory cell pMem contains the context of an aggregate function.
 60327 ** This routine calls the finalize method for that function.  The
 60328 ** result of the aggregate is stored back into pMem.
 60329 **
 60330 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
 60331 ** otherwise.
 60332 */
 60333 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
 60334   int rc = SQLITE_OK;
 60335   if( ALWAYS(pFunc && pFunc->xFinalize) ){
 60336     sqlite3_context ctx;
 60337     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
 60338     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60339     memset(&ctx, 0, sizeof(ctx));
 60340     ctx.s.flags = MEM_Null;
 60341     ctx.s.db = pMem->db;
 60342     ctx.pMem = pMem;
 60343     ctx.pFunc = pFunc;
 60344     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
 60345     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
 60346     sqlite3DbFree(pMem->db, pMem->zMalloc);
 60347     memcpy(pMem, &ctx.s, sizeof(ctx.s));
 60348     rc = ctx.isError;
 60350   return rc;
 60353 /*
 60354 ** If the memory cell contains a string value that must be freed by
 60355 ** invoking an external callback, free it now. Calling this function
 60356 ** does not free any Mem.zMalloc buffer.
 60357 */
 60358 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
 60359   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
 60360   if( p->flags&MEM_Agg ){
 60361     sqlite3VdbeMemFinalize(p, p->u.pDef);
 60362     assert( (p->flags & MEM_Agg)==0 );
 60363     sqlite3VdbeMemRelease(p);
 60364   }else if( p->flags&MEM_Dyn ){
 60365     assert( (p->flags&MEM_RowSet)==0 );
 60366     assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
 60367     p->xDel((void *)p->z);
 60368     p->xDel = 0;
 60369   }else if( p->flags&MEM_RowSet ){
 60370     sqlite3RowSetClear(p->u.pRowSet);
 60371   }else if( p->flags&MEM_Frame ){
 60372     sqlite3VdbeMemSetNull(p);
 60376 /*
 60377 ** Release any memory held by the Mem. This may leave the Mem in an
 60378 ** inconsistent state, for example with (Mem.z==0) and
 60379 ** (Mem.flags==MEM_Str).
 60380 */
 60381 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
 60382   assert( sqlite3VdbeCheckMemInvariants(p) );
 60383   VdbeMemRelease(p);
 60384   if( p->zMalloc ){
 60385     sqlite3DbFree(p->db, p->zMalloc);
 60386     p->zMalloc = 0;
 60388   p->z = 0;
 60389   assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
 60392 /*
 60393 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
 60394 ** If the double is out of range of a 64-bit signed integer then
 60395 ** return the closest available 64-bit signed integer.
 60396 */
 60397 static i64 doubleToInt64(double r){
 60398 #ifdef SQLITE_OMIT_FLOATING_POINT
 60399   /* When floating-point is omitted, double and int64 are the same thing */
 60400   return r;
 60401 #else
 60402   /*
 60403   ** Many compilers we encounter do not define constants for the
 60404   ** minimum and maximum 64-bit integers, or they define them
 60405   ** inconsistently.  And many do not understand the "LL" notation.
 60406   ** So we define our own static constants here using nothing
 60407   ** larger than a 32-bit integer constant.
 60408   */
 60409   static const i64 maxInt = LARGEST_INT64;
 60410   static const i64 minInt = SMALLEST_INT64;
 60412   if( r<=(double)minInt ){
 60413     return minInt;
 60414   }else if( r>=(double)maxInt ){
 60415     return maxInt;
 60416   }else{
 60417     return (i64)r;
 60419 #endif
 60422 /*
 60423 ** Return some kind of integer value which is the best we can do
 60424 ** at representing the value that *pMem describes as an integer.
 60425 ** If pMem is an integer, then the value is exact.  If pMem is
 60426 ** a floating-point then the value returned is the integer part.
 60427 ** If pMem is a string or blob, then we make an attempt to convert
 60428 ** it into a integer and return that.  If pMem represents an
 60429 ** an SQL-NULL value, return 0.
 60430 **
 60431 ** If pMem represents a string value, its encoding might be changed.
 60432 */
 60433 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
 60434   int flags;
 60435   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60436   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60437   flags = pMem->flags;
 60438   if( flags & MEM_Int ){
 60439     return pMem->u.i;
 60440   }else if( flags & MEM_Real ){
 60441     return doubleToInt64(pMem->r);
 60442   }else if( flags & (MEM_Str|MEM_Blob) ){
 60443     i64 value = 0;
 60444     assert( pMem->z || pMem->n==0 );
 60445     testcase( pMem->z==0 );
 60446     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
 60447     return value;
 60448   }else{
 60449     return 0;
 60453 /*
 60454 ** Return the best representation of pMem that we can get into a
 60455 ** double.  If pMem is already a double or an integer, return its
 60456 ** value.  If it is a string or blob, try to convert it to a double.
 60457 ** If it is a NULL, return 0.0.
 60458 */
 60459 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
 60460   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60461   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60462   if( pMem->flags & MEM_Real ){
 60463     return pMem->r;
 60464   }else if( pMem->flags & MEM_Int ){
 60465     return (double)pMem->u.i;
 60466   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
 60467     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 60468     double val = (double)0;
 60469     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
 60470     return val;
 60471   }else{
 60472     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 60473     return (double)0;
 60477 /*
 60478 ** The MEM structure is already a MEM_Real.  Try to also make it a
 60479 ** MEM_Int if we can.
 60480 */
 60481 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
 60482   assert( pMem->flags & MEM_Real );
 60483   assert( (pMem->flags & MEM_RowSet)==0 );
 60484   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60485   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60487   pMem->u.i = doubleToInt64(pMem->r);
 60489   /* Only mark the value as an integer if
 60490   **
 60491   **    (1) the round-trip conversion real->int->real is a no-op, and
 60492   **    (2) The integer is neither the largest nor the smallest
 60493   **        possible integer (ticket #3922)
 60494   **
 60495   ** The second and third terms in the following conditional enforces
 60496   ** the second condition under the assumption that addition overflow causes
 60497   ** values to wrap around.
 60498   */
 60499   if( pMem->r==(double)pMem->u.i
 60500    && pMem->u.i>SMALLEST_INT64
 60501    && pMem->u.i<LARGEST_INT64
 60502   ){
 60503     pMem->flags |= MEM_Int;
 60507 /*
 60508 ** Convert pMem to type integer.  Invalidate any prior representations.
 60509 */
 60510 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
 60511   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60512   assert( (pMem->flags & MEM_RowSet)==0 );
 60513   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60515   pMem->u.i = sqlite3VdbeIntValue(pMem);
 60516   MemSetTypeFlag(pMem, MEM_Int);
 60517   return SQLITE_OK;
 60520 /*
 60521 ** Convert pMem so that it is of type MEM_Real.
 60522 ** Invalidate any prior representations.
 60523 */
 60524 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
 60525   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60526   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 60528   pMem->r = sqlite3VdbeRealValue(pMem);
 60529   MemSetTypeFlag(pMem, MEM_Real);
 60530   return SQLITE_OK;
 60533 /*
 60534 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
 60535 ** Invalidate any prior representations.
 60536 **
 60537 ** Every effort is made to force the conversion, even if the input
 60538 ** is a string that does not look completely like a number.  Convert
 60539 ** as much of the string as we can and ignore the rest.
 60540 */
 60541 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
 60542   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
 60543     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
 60544     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60545     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
 60546       MemSetTypeFlag(pMem, MEM_Int);
 60547     }else{
 60548       pMem->r = sqlite3VdbeRealValue(pMem);
 60549       MemSetTypeFlag(pMem, MEM_Real);
 60550       sqlite3VdbeIntegerAffinity(pMem);
 60553   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
 60554   pMem->flags &= ~(MEM_Str|MEM_Blob);
 60555   return SQLITE_OK;
 60558 /*
 60559 ** Delete any previous value and set the value stored in *pMem to NULL.
 60560 */
 60561 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
 60562   if( pMem->flags & MEM_Frame ){
 60563     VdbeFrame *pFrame = pMem->u.pFrame;
 60564     pFrame->pParent = pFrame->v->pDelFrame;
 60565     pFrame->v->pDelFrame = pFrame;
 60567   if( pMem->flags & MEM_RowSet ){
 60568     sqlite3RowSetClear(pMem->u.pRowSet);
 60570   MemSetTypeFlag(pMem, MEM_Null);
 60572 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
 60573   sqlite3VdbeMemSetNull((Mem*)p); 
 60576 /*
 60577 ** Delete any previous value and set the value to be a BLOB of length
 60578 ** n containing all zeros.
 60579 */
 60580 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
 60581   sqlite3VdbeMemRelease(pMem);
 60582   pMem->flags = MEM_Blob|MEM_Zero;
 60583   pMem->n = 0;
 60584   if( n<0 ) n = 0;
 60585   pMem->u.nZero = n;
 60586   pMem->enc = SQLITE_UTF8;
 60588 #ifdef SQLITE_OMIT_INCRBLOB
 60589   sqlite3VdbeMemGrow(pMem, n, 0);
 60590   if( pMem->z ){
 60591     pMem->n = n;
 60592     memset(pMem->z, 0, n);
 60594 #endif
 60597 /*
 60598 ** Delete any previous value and set the value stored in *pMem to val,
 60599 ** manifest type INTEGER.
 60600 */
 60601 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
 60602   sqlite3VdbeMemRelease(pMem);
 60603   pMem->u.i = val;
 60604   pMem->flags = MEM_Int;
 60607 #ifndef SQLITE_OMIT_FLOATING_POINT
 60608 /*
 60609 ** Delete any previous value and set the value stored in *pMem to val,
 60610 ** manifest type REAL.
 60611 */
 60612 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
 60613   if( sqlite3IsNaN(val) ){
 60614     sqlite3VdbeMemSetNull(pMem);
 60615   }else{
 60616     sqlite3VdbeMemRelease(pMem);
 60617     pMem->r = val;
 60618     pMem->flags = MEM_Real;
 60621 #endif
 60623 /*
 60624 ** Delete any previous value and set the value of pMem to be an
 60625 ** empty boolean index.
 60626 */
 60627 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
 60628   sqlite3 *db = pMem->db;
 60629   assert( db!=0 );
 60630   assert( (pMem->flags & MEM_RowSet)==0 );
 60631   sqlite3VdbeMemRelease(pMem);
 60632   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
 60633   if( db->mallocFailed ){
 60634     pMem->flags = MEM_Null;
 60635   }else{
 60636     assert( pMem->zMalloc );
 60637     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
 60638                                        sqlite3DbMallocSize(db, pMem->zMalloc));
 60639     assert( pMem->u.pRowSet!=0 );
 60640     pMem->flags = MEM_RowSet;
 60644 /*
 60645 ** Return true if the Mem object contains a TEXT or BLOB that is
 60646 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
 60647 */
 60648 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
 60649   assert( p->db!=0 );
 60650   if( p->flags & (MEM_Str|MEM_Blob) ){
 60651     int n = p->n;
 60652     if( p->flags & MEM_Zero ){
 60653       n += p->u.nZero;
 60655     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
 60657   return 0; 
 60660 #ifdef SQLITE_DEBUG
 60661 /*
 60662 ** This routine prepares a memory cell for modication by breaking
 60663 ** its link to a shallow copy and by marking any current shallow
 60664 ** copies of this cell as invalid.
 60665 **
 60666 ** This is used for testing and debugging only - to make sure shallow
 60667 ** copies are not misused.
 60668 */
 60669 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
 60670   int i;
 60671   Mem *pX;
 60672   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
 60673     if( pX->pScopyFrom==pMem ){
 60674       pX->flags |= MEM_Undefined;
 60675       pX->pScopyFrom = 0;
 60678   pMem->pScopyFrom = 0;
 60680 #endif /* SQLITE_DEBUG */
 60682 /*
 60683 ** Size of struct Mem not including the Mem.zMalloc member.
 60684 */
 60685 #define MEMCELLSIZE offsetof(Mem,zMalloc)
 60687 /*
 60688 ** Make an shallow copy of pFrom into pTo.  Prior contents of
 60689 ** pTo are freed.  The pFrom->z field is not duplicated.  If
 60690 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
 60691 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
 60692 */
 60693 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
 60694   assert( (pFrom->flags & MEM_RowSet)==0 );
 60695   VdbeMemRelease(pTo);
 60696   memcpy(pTo, pFrom, MEMCELLSIZE);
 60697   pTo->xDel = 0;
 60698   if( (pFrom->flags&MEM_Static)==0 ){
 60699     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
 60700     assert( srcType==MEM_Ephem || srcType==MEM_Static );
 60701     pTo->flags |= srcType;
 60705 /*
 60706 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
 60707 ** freed before the copy is made.
 60708 */
 60709 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
 60710   int rc = SQLITE_OK;
 60712   assert( (pFrom->flags & MEM_RowSet)==0 );
 60713   VdbeMemRelease(pTo);
 60714   memcpy(pTo, pFrom, MEMCELLSIZE);
 60715   pTo->flags &= ~MEM_Dyn;
 60716   pTo->xDel = 0;
 60718   if( pTo->flags&(MEM_Str|MEM_Blob) ){
 60719     if( 0==(pFrom->flags&MEM_Static) ){
 60720       pTo->flags |= MEM_Ephem;
 60721       rc = sqlite3VdbeMemMakeWriteable(pTo);
 60725   return rc;
 60728 /*
 60729 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
 60730 ** freed. If pFrom contains ephemeral data, a copy is made.
 60731 **
 60732 ** pFrom contains an SQL NULL when this routine returns.
 60733 */
 60734 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
 60735   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
 60736   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
 60737   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
 60739   sqlite3VdbeMemRelease(pTo);
 60740   memcpy(pTo, pFrom, sizeof(Mem));
 60741   pFrom->flags = MEM_Null;
 60742   pFrom->xDel = 0;
 60743   pFrom->zMalloc = 0;
 60746 /*
 60747 ** Change the value of a Mem to be a string or a BLOB.
 60748 **
 60749 ** The memory management strategy depends on the value of the xDel
 60750 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
 60751 ** string is copied into a (possibly existing) buffer managed by the 
 60752 ** Mem structure. Otherwise, any existing buffer is freed and the
 60753 ** pointer copied.
 60754 **
 60755 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
 60756 ** size limit) then no memory allocation occurs.  If the string can be
 60757 ** stored without allocating memory, then it is.  If a memory allocation
 60758 ** is required to store the string, then value of pMem is unchanged.  In
 60759 ** either case, SQLITE_TOOBIG is returned.
 60760 */
 60761 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
 60762   Mem *pMem,          /* Memory cell to set to string value */
 60763   const char *z,      /* String pointer */
 60764   int n,              /* Bytes in string, or negative */
 60765   u8 enc,             /* Encoding of z.  0 for BLOBs */
 60766   void (*xDel)(void*) /* Destructor function */
 60767 ){
 60768   int nByte = n;      /* New value for pMem->n */
 60769   int iLimit;         /* Maximum allowed string or blob size */
 60770   u16 flags = 0;      /* New value for pMem->flags */
 60772   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 60773   assert( (pMem->flags & MEM_RowSet)==0 );
 60775   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
 60776   if( !z ){
 60777     sqlite3VdbeMemSetNull(pMem);
 60778     return SQLITE_OK;
 60781   if( pMem->db ){
 60782     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
 60783   }else{
 60784     iLimit = SQLITE_MAX_LENGTH;
 60786   flags = (enc==0?MEM_Blob:MEM_Str);
 60787   if( nByte<0 ){
 60788     assert( enc!=0 );
 60789     if( enc==SQLITE_UTF8 ){
 60790       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
 60791     }else{
 60792       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
 60794     flags |= MEM_Term;
 60797   /* The following block sets the new values of Mem.z and Mem.xDel. It
 60798   ** also sets a flag in local variable "flags" to indicate the memory
 60799   ** management (one of MEM_Dyn or MEM_Static).
 60800   */
 60801   if( xDel==SQLITE_TRANSIENT ){
 60802     int nAlloc = nByte;
 60803     if( flags&MEM_Term ){
 60804       nAlloc += (enc==SQLITE_UTF8?1:2);
 60806     if( nByte>iLimit ){
 60807       return SQLITE_TOOBIG;
 60809     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
 60810       return SQLITE_NOMEM;
 60812     memcpy(pMem->z, z, nAlloc);
 60813   }else if( xDel==SQLITE_DYNAMIC ){
 60814     sqlite3VdbeMemRelease(pMem);
 60815     pMem->zMalloc = pMem->z = (char *)z;
 60816     pMem->xDel = 0;
 60817   }else{
 60818     sqlite3VdbeMemRelease(pMem);
 60819     pMem->z = (char *)z;
 60820     pMem->xDel = xDel;
 60821     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
 60824   pMem->n = nByte;
 60825   pMem->flags = flags;
 60826   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
 60828 #ifndef SQLITE_OMIT_UTF16
 60829   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
 60830     return SQLITE_NOMEM;
 60832 #endif
 60834   if( nByte>iLimit ){
 60835     return SQLITE_TOOBIG;
 60838   return SQLITE_OK;
 60841 /*
 60842 ** Move data out of a btree key or data field and into a Mem structure.
 60843 ** The data or key is taken from the entry that pCur is currently pointing
 60844 ** to.  offset and amt determine what portion of the data or key to retrieve.
 60845 ** key is true to get the key or false to get data.  The result is written
 60846 ** into the pMem element.
 60847 **
 60848 ** The pMem structure is assumed to be uninitialized.  Any prior content
 60849 ** is overwritten without being freed.
 60850 **
 60851 ** If this routine fails for any reason (malloc returns NULL or unable
 60852 ** to read from the disk) then the pMem is left in an inconsistent state.
 60853 */
 60854 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
 60855   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
 60856   u32 offset,       /* Offset from the start of data to return bytes from. */
 60857   u32 amt,          /* Number of bytes to return. */
 60858   int key,          /* If true, retrieve from the btree key, not data. */
 60859   Mem *pMem         /* OUT: Return data in this Mem structure. */
 60860 ){
 60861   char *zData;        /* Data from the btree layer */
 60862   u32 available = 0;  /* Number of bytes available on the local btree page */
 60863   int rc = SQLITE_OK; /* Return code */
 60865   assert( sqlite3BtreeCursorIsValid(pCur) );
 60867   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
 60868   ** that both the BtShared and database handle mutexes are held. */
 60869   assert( (pMem->flags & MEM_RowSet)==0 );
 60870   if( key ){
 60871     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
 60872   }else{
 60873     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
 60875   assert( zData!=0 );
 60877   if( offset+amt<=available ){
 60878     sqlite3VdbeMemRelease(pMem);
 60879     pMem->z = &zData[offset];
 60880     pMem->flags = MEM_Blob|MEM_Ephem;
 60881     pMem->n = (int)amt;
 60882   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
 60883     if( key ){
 60884       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
 60885     }else{
 60886       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
 60888     if( rc==SQLITE_OK ){
 60889       pMem->z[amt] = 0;
 60890       pMem->z[amt+1] = 0;
 60891       pMem->flags = MEM_Blob|MEM_Term;
 60892       pMem->n = (int)amt;
 60893     }else{
 60894       sqlite3VdbeMemRelease(pMem);
 60898   return rc;
 60901 /* This function is only available internally, it is not part of the
 60902 ** external API. It works in a similar way to sqlite3_value_text(),
 60903 ** except the data returned is in the encoding specified by the second
 60904 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
 60905 ** SQLITE_UTF8.
 60906 **
 60907 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
 60908 ** If that is the case, then the result must be aligned on an even byte
 60909 ** boundary.
 60910 */
 60911 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
 60912   if( !pVal ) return 0;
 60914   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
 60915   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 60916   assert( (pVal->flags & MEM_RowSet)==0 );
 60918   if( pVal->flags&MEM_Null ){
 60919     return 0;
 60921   assert( (MEM_Blob>>3) == MEM_Str );
 60922   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
 60923   ExpandBlob(pVal);
 60924   if( pVal->flags&MEM_Str ){
 60925     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
 60926     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
 60927       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
 60928       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
 60929         return 0;
 60932     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
 60933   }else{
 60934     assert( (pVal->flags&MEM_Blob)==0 );
 60935     sqlite3VdbeMemStringify(pVal, enc);
 60936     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
 60938   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
 60939               || pVal->db->mallocFailed );
 60940   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
 60941     return pVal->z;
 60942   }else{
 60943     return 0;
 60947 /*
 60948 ** Create a new sqlite3_value object.
 60949 */
 60950 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
 60951   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
 60952   if( p ){
 60953     p->flags = MEM_Null;
 60954     p->db = db;
 60956   return p;
 60959 /*
 60960 ** Context object passed by sqlite3Stat4ProbeSetValue() through to 
 60961 ** valueNew(). See comments above valueNew() for details.
 60962 */
 60963 struct ValueNewStat4Ctx {
 60964   Parse *pParse;
 60965   Index *pIdx;
 60966   UnpackedRecord **ppRec;
 60967   int iVal;
 60968 };
 60970 /*
 60971 ** Allocate and return a pointer to a new sqlite3_value object. If
 60972 ** the second argument to this function is NULL, the object is allocated
 60973 ** by calling sqlite3ValueNew().
 60974 **
 60975 ** Otherwise, if the second argument is non-zero, then this function is 
 60976 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
 60977 ** already been allocated, allocate the UnpackedRecord structure that 
 60978 ** that function will return to its caller here. Then return a pointer 
 60979 ** an sqlite3_value within the UnpackedRecord.a[] array.
 60980 */
 60981 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
 60982 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 60983   if( p ){
 60984     UnpackedRecord *pRec = p->ppRec[0];
 60986     if( pRec==0 ){
 60987       Index *pIdx = p->pIdx;      /* Index being probed */
 60988       int nByte;                  /* Bytes of space to allocate */
 60989       int i;                      /* Counter variable */
 60990       int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
 60992       nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
 60993       pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
 60994       if( pRec ){
 60995         pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
 60996         if( pRec->pKeyInfo ){
 60997           assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
 60998           assert( pRec->pKeyInfo->enc==ENC(db) );
 60999           pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
 61000           for(i=0; i<nCol; i++){
 61001             pRec->aMem[i].flags = MEM_Null;
 61002             pRec->aMem[i].db = db;
 61004         }else{
 61005           sqlite3DbFree(db, pRec);
 61006           pRec = 0;
 61009       if( pRec==0 ) return 0;
 61010       p->ppRec[0] = pRec;
 61013     pRec->nField = p->iVal+1;
 61014     return &pRec->aMem[p->iVal];
 61016 #else
 61017   UNUSED_PARAMETER(p);
 61018 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
 61019   return sqlite3ValueNew(db);
 61022 /*
 61023 ** Extract a value from the supplied expression in the manner described
 61024 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
 61025 ** using valueNew().
 61026 **
 61027 ** If pCtx is NULL and an error occurs after the sqlite3_value object
 61028 ** has been allocated, it is freed before returning. Or, if pCtx is not
 61029 ** NULL, it is assumed that the caller will free any allocated object
 61030 ** in all cases.
 61031 */
 61032 static int valueFromExpr(
 61033   sqlite3 *db,                    /* The database connection */
 61034   Expr *pExpr,                    /* The expression to evaluate */
 61035   u8 enc,                         /* Encoding to use */
 61036   u8 affinity,                    /* Affinity to use */
 61037   sqlite3_value **ppVal,          /* Write the new value here */
 61038   struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
 61039 ){
 61040   int op;
 61041   char *zVal = 0;
 61042   sqlite3_value *pVal = 0;
 61043   int negInt = 1;
 61044   const char *zNeg = "";
 61045   int rc = SQLITE_OK;
 61047   if( !pExpr ){
 61048     *ppVal = 0;
 61049     return SQLITE_OK;
 61051   op = pExpr->op;
 61052   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
 61054   /* Handle negative integers in a single step.  This is needed in the
 61055   ** case when the value is -9223372036854775808.
 61056   */
 61057   if( op==TK_UMINUS
 61058    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
 61059     pExpr = pExpr->pLeft;
 61060     op = pExpr->op;
 61061     negInt = -1;
 61062     zNeg = "-";
 61065   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
 61066     pVal = valueNew(db, pCtx);
 61067     if( pVal==0 ) goto no_mem;
 61068     if( ExprHasProperty(pExpr, EP_IntValue) ){
 61069       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
 61070     }else{
 61071       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
 61072       if( zVal==0 ) goto no_mem;
 61073       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 61075     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
 61076       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
 61077     }else{
 61078       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
 61080     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
 61081     if( enc!=SQLITE_UTF8 ){
 61082       rc = sqlite3VdbeChangeEncoding(pVal, enc);
 61084   }else if( op==TK_UMINUS ) {
 61085     /* This branch happens for multiple negative signs.  Ex: -(-5) */
 61086     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
 61087      && pVal!=0
 61088     ){
 61089       sqlite3VdbeMemNumerify(pVal);
 61090       if( pVal->u.i==SMALLEST_INT64 ){
 61091         pVal->flags &= ~MEM_Int;
 61092         pVal->flags |= MEM_Real;
 61093         pVal->r = (double)SMALLEST_INT64;
 61094       }else{
 61095         pVal->u.i = -pVal->u.i;
 61097       pVal->r = -pVal->r;
 61098       sqlite3ValueApplyAffinity(pVal, affinity, enc);
 61100   }else if( op==TK_NULL ){
 61101     pVal = valueNew(db, pCtx);
 61102     if( pVal==0 ) goto no_mem;
 61104 #ifndef SQLITE_OMIT_BLOB_LITERAL
 61105   else if( op==TK_BLOB ){
 61106     int nVal;
 61107     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 61108     assert( pExpr->u.zToken[1]=='\'' );
 61109     pVal = valueNew(db, pCtx);
 61110     if( !pVal ) goto no_mem;
 61111     zVal = &pExpr->u.zToken[2];
 61112     nVal = sqlite3Strlen30(zVal)-1;
 61113     assert( zVal[nVal]=='\'' );
 61114     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
 61115                          0, SQLITE_DYNAMIC);
 61117 #endif
 61119   *ppVal = pVal;
 61120   return rc;
 61122 no_mem:
 61123   db->mallocFailed = 1;
 61124   sqlite3DbFree(db, zVal);
 61125   assert( *ppVal==0 );
 61126 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 61127   if( pCtx==0 ) sqlite3ValueFree(pVal);
 61128 #else
 61129   assert( pCtx==0 ); sqlite3ValueFree(pVal);
 61130 #endif
 61131   return SQLITE_NOMEM;
 61134 /*
 61135 ** Create a new sqlite3_value object, containing the value of pExpr.
 61136 **
 61137 ** This only works for very simple expressions that consist of one constant
 61138 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
 61139 ** be converted directly into a value, then the value is allocated and
 61140 ** a pointer written to *ppVal. The caller is responsible for deallocating
 61141 ** the value by passing it to sqlite3ValueFree() later on. If the expression
 61142 ** cannot be converted to a value, then *ppVal is set to NULL.
 61143 */
 61144 SQLITE_PRIVATE int sqlite3ValueFromExpr(
 61145   sqlite3 *db,              /* The database connection */
 61146   Expr *pExpr,              /* The expression to evaluate */
 61147   u8 enc,                   /* Encoding to use */
 61148   u8 affinity,              /* Affinity to use */
 61149   sqlite3_value **ppVal     /* Write the new value here */
 61150 ){
 61151   return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
 61154 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 61155 /*
 61156 ** The implementation of the sqlite_record() function. This function accepts
 61157 ** a single argument of any type. The return value is a formatted database 
 61158 ** record (a blob) containing the argument value.
 61159 **
 61160 ** This is used to convert the value stored in the 'sample' column of the
 61161 ** sqlite_stat3 table to the record format SQLite uses internally.
 61162 */
 61163 static void recordFunc(
 61164   sqlite3_context *context,
 61165   int argc,
 61166   sqlite3_value **argv
 61167 ){
 61168   const int file_format = 1;
 61169   int iSerial;                    /* Serial type */
 61170   int nSerial;                    /* Bytes of space for iSerial as varint */
 61171   int nVal;                       /* Bytes of space required for argv[0] */
 61172   int nRet;
 61173   sqlite3 *db;
 61174   u8 *aRet;
 61176   UNUSED_PARAMETER( argc );
 61177   iSerial = sqlite3VdbeSerialType(argv[0], file_format);
 61178   nSerial = sqlite3VarintLen(iSerial);
 61179   nVal = sqlite3VdbeSerialTypeLen(iSerial);
 61180   db = sqlite3_context_db_handle(context);
 61182   nRet = 1 + nSerial + nVal;
 61183   aRet = sqlite3DbMallocRaw(db, nRet);
 61184   if( aRet==0 ){
 61185     sqlite3_result_error_nomem(context);
 61186   }else{
 61187     aRet[0] = nSerial+1;
 61188     sqlite3PutVarint(&aRet[1], iSerial);
 61189     sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
 61190     sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
 61191     sqlite3DbFree(db, aRet);
 61195 /*
 61196 ** Register built-in functions used to help read ANALYZE data.
 61197 */
 61198 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
 61199   static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
 61200     FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
 61201   };
 61202   int i;
 61203   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 61204   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
 61205   for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
 61206     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 61210 /*
 61211 ** This function is used to allocate and populate UnpackedRecord 
 61212 ** structures intended to be compared against sample index keys stored 
 61213 ** in the sqlite_stat4 table.
 61214 **
 61215 ** A single call to this function attempts to populates field iVal (leftmost 
 61216 ** is 0 etc.) of the unpacked record with a value extracted from expression
 61217 ** pExpr. Extraction of values is possible if:
 61218 **
 61219 **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
 61220 **
 61221 **  * The expression is a bound variable, and this is a reprepare, or
 61222 **
 61223 **  * The sqlite3ValueFromExpr() function is able to extract a value 
 61224 **    from the expression (i.e. the expression is a literal value).
 61225 **
 61226 ** If a value can be extracted, the affinity passed as the 5th argument
 61227 ** is applied to it before it is copied into the UnpackedRecord. Output
 61228 ** parameter *pbOk is set to true if a value is extracted, or false 
 61229 ** otherwise.
 61230 **
 61231 ** When this function is called, *ppRec must either point to an object
 61232 ** allocated by an earlier call to this function, or must be NULL. If it
 61233 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
 61234 ** is allocated (and *ppRec set to point to it) before returning.
 61235 **
 61236 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
 61237 ** error if a value cannot be extracted from pExpr. If an error does
 61238 ** occur, an SQLite error code is returned.
 61239 */
 61240 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
 61241   Parse *pParse,                  /* Parse context */
 61242   Index *pIdx,                    /* Index being probed */
 61243   UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
 61244   Expr *pExpr,                    /* The expression to extract a value from */
 61245   u8 affinity,                    /* Affinity to use */
 61246   int iVal,                       /* Array element to populate */
 61247   int *pbOk                       /* OUT: True if value was extracted */
 61248 ){
 61249   int rc = SQLITE_OK;
 61250   sqlite3_value *pVal = 0;
 61251   sqlite3 *db = pParse->db;
 61254   struct ValueNewStat4Ctx alloc;
 61255   alloc.pParse = pParse;
 61256   alloc.pIdx = pIdx;
 61257   alloc.ppRec = ppRec;
 61258   alloc.iVal = iVal;
 61260   /* Skip over any TK_COLLATE nodes */
 61261   pExpr = sqlite3ExprSkipCollate(pExpr);
 61263   if( !pExpr ){
 61264     pVal = valueNew(db, &alloc);
 61265     if( pVal ){
 61266       sqlite3VdbeMemSetNull((Mem*)pVal);
 61268   }else if( pExpr->op==TK_VARIABLE
 61269         || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
 61270   ){
 61271     Vdbe *v;
 61272     int iBindVar = pExpr->iColumn;
 61273     sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
 61274     if( (v = pParse->pReprepare)!=0 ){
 61275       pVal = valueNew(db, &alloc);
 61276       if( pVal ){
 61277         rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
 61278         if( rc==SQLITE_OK ){
 61279           sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
 61281         pVal->db = pParse->db;
 61284   }else{
 61285     rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
 61287   *pbOk = (pVal!=0);
 61289   assert( pVal==0 || pVal->db==db );
 61290   return rc;
 61293 /*
 61294 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
 61295 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
 61296 ** the object.
 61297 */
 61298 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
 61299   if( pRec ){
 61300     int i;
 61301     int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
 61302     Mem *aMem = pRec->aMem;
 61303     sqlite3 *db = aMem[0].db;
 61304     for(i=0; i<nCol; i++){
 61305       sqlite3DbFree(db, aMem[i].zMalloc);
 61307     sqlite3KeyInfoUnref(pRec->pKeyInfo);
 61308     sqlite3DbFree(db, pRec);
 61311 #endif /* ifdef SQLITE_ENABLE_STAT4 */
 61313 /*
 61314 ** Change the string value of an sqlite3_value object
 61315 */
 61316 SQLITE_PRIVATE void sqlite3ValueSetStr(
 61317   sqlite3_value *v,     /* Value to be set */
 61318   int n,                /* Length of string z */
 61319   const void *z,        /* Text of the new string */
 61320   u8 enc,               /* Encoding to use */
 61321   void (*xDel)(void*)   /* Destructor for the string */
 61322 ){
 61323   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
 61326 /*
 61327 ** Free an sqlite3_value object
 61328 */
 61329 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
 61330   if( !v ) return;
 61331   sqlite3VdbeMemRelease((Mem *)v);
 61332   sqlite3DbFree(((Mem*)v)->db, v);
 61335 /*
 61336 ** Return the number of bytes in the sqlite3_value object assuming
 61337 ** that it uses the encoding "enc"
 61338 */
 61339 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
 61340   Mem *p = (Mem*)pVal;
 61341   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
 61342     if( p->flags & MEM_Zero ){
 61343       return p->n + p->u.nZero;
 61344     }else{
 61345       return p->n;
 61348   return 0;
 61351 /************** End of vdbemem.c *********************************************/
 61352 /************** Begin file vdbeaux.c *****************************************/
 61353 /*
 61354 ** 2003 September 6
 61355 **
 61356 ** The author disclaims copyright to this source code.  In place of
 61357 ** a legal notice, here is a blessing:
 61358 **
 61359 **    May you do good and not evil.
 61360 **    May you find forgiveness for yourself and forgive others.
 61361 **    May you share freely, never taking more than you give.
 61362 **
 61363 *************************************************************************
 61364 ** This file contains code used for creating, destroying, and populating
 61365 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
 61366 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
 61367 ** But that file was getting too big so this subroutines were split out.
 61368 */
 61370 /*
 61371 ** Create a new virtual database engine.
 61372 */
 61373 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
 61374   sqlite3 *db = pParse->db;
 61375   Vdbe *p;
 61376   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
 61377   if( p==0 ) return 0;
 61378   p->db = db;
 61379   if( db->pVdbe ){
 61380     db->pVdbe->pPrev = p;
 61382   p->pNext = db->pVdbe;
 61383   p->pPrev = 0;
 61384   db->pVdbe = p;
 61385   p->magic = VDBE_MAGIC_INIT;
 61386   p->pParse = pParse;
 61387   assert( pParse->aLabel==0 );
 61388   assert( pParse->nLabel==0 );
 61389   assert( pParse->nOpAlloc==0 );
 61390   return p;
 61393 /*
 61394 ** Remember the SQL string for a prepared statement.
 61395 */
 61396 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
 61397   assert( isPrepareV2==1 || isPrepareV2==0 );
 61398   if( p==0 ) return;
 61399 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
 61400   if( !isPrepareV2 ) return;
 61401 #endif
 61402   assert( p->zSql==0 );
 61403   p->zSql = sqlite3DbStrNDup(p->db, z, n);
 61404   p->isPrepareV2 = (u8)isPrepareV2;
 61407 /*
 61408 ** Return the SQL associated with a prepared statement
 61409 */
 61410 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
 61411   Vdbe *p = (Vdbe *)pStmt;
 61412   return (p && p->isPrepareV2) ? p->zSql : 0;
 61415 /*
 61416 ** Swap all content between two VDBE structures.
 61417 */
 61418 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
 61419   Vdbe tmp, *pTmp;
 61420   char *zTmp;
 61421   tmp = *pA;
 61422   *pA = *pB;
 61423   *pB = tmp;
 61424   pTmp = pA->pNext;
 61425   pA->pNext = pB->pNext;
 61426   pB->pNext = pTmp;
 61427   pTmp = pA->pPrev;
 61428   pA->pPrev = pB->pPrev;
 61429   pB->pPrev = pTmp;
 61430   zTmp = pA->zSql;
 61431   pA->zSql = pB->zSql;
 61432   pB->zSql = zTmp;
 61433   pB->isPrepareV2 = pA->isPrepareV2;
 61436 /*
 61437 ** Resize the Vdbe.aOp array so that it is at least one op larger than 
 61438 ** it was.
 61439 **
 61440 ** If an out-of-memory error occurs while resizing the array, return
 61441 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
 61442 ** unchanged (this is so that any opcodes already allocated can be 
 61443 ** correctly deallocated along with the rest of the Vdbe).
 61444 */
 61445 static int growOpArray(Vdbe *v){
 61446   VdbeOp *pNew;
 61447   Parse *p = v->pParse;
 61448   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
 61449   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
 61450   if( pNew ){
 61451     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
 61452     v->aOp = pNew;
 61454   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
 61457 #ifdef SQLITE_DEBUG
 61458 /* This routine is just a convenient place to set a breakpoint that will
 61459 ** fire after each opcode is inserted and displayed using
 61460 ** "PRAGMA vdbe_addoptrace=on".
 61461 */
 61462 static void test_addop_breakpoint(void){
 61463   static int n = 0;
 61464   n++;
 61466 #endif
 61468 /*
 61469 ** Add a new instruction to the list of instructions current in the
 61470 ** VDBE.  Return the address of the new instruction.
 61471 **
 61472 ** Parameters:
 61473 **
 61474 **    p               Pointer to the VDBE
 61475 **
 61476 **    op              The opcode for this instruction
 61477 **
 61478 **    p1, p2, p3      Operands
 61479 **
 61480 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
 61481 ** the sqlite3VdbeChangeP4() function to change the value of the P4
 61482 ** operand.
 61483 */
 61484 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
 61485   int i;
 61486   VdbeOp *pOp;
 61488   i = p->nOp;
 61489   assert( p->magic==VDBE_MAGIC_INIT );
 61490   assert( op>0 && op<0xff );
 61491   if( p->pParse->nOpAlloc<=i ){
 61492     if( growOpArray(p) ){
 61493       return 1;
 61496   p->nOp++;
 61497   pOp = &p->aOp[i];
 61498   pOp->opcode = (u8)op;
 61499   pOp->p5 = 0;
 61500   pOp->p1 = p1;
 61501   pOp->p2 = p2;
 61502   pOp->p3 = p3;
 61503   pOp->p4.p = 0;
 61504   pOp->p4type = P4_NOTUSED;
 61505 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 61506   pOp->zComment = 0;
 61507 #endif
 61508 #ifdef SQLITE_DEBUG
 61509   if( p->db->flags & SQLITE_VdbeAddopTrace ){
 61510     int jj, kk;
 61511     Parse *pParse = p->pParse;
 61512     for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
 61513       struct yColCache *x = pParse->aColCache + jj;
 61514       if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
 61515       printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
 61516       kk++;
 61518     if( kk ) printf("\n");
 61519     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 61520     test_addop_breakpoint();
 61522 #endif
 61523 #ifdef VDBE_PROFILE
 61524   pOp->cycles = 0;
 61525   pOp->cnt = 0;
 61526 #endif
 61527 #ifdef SQLITE_VDBE_COVERAGE
 61528   pOp->iSrcLine = 0;
 61529 #endif
 61530   return i;
 61532 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
 61533   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
 61535 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
 61536   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
 61538 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
 61539   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
 61543 /*
 61544 ** Add an opcode that includes the p4 value as a pointer.
 61545 */
 61546 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
 61547   Vdbe *p,            /* Add the opcode to this VM */
 61548   int op,             /* The new opcode */
 61549   int p1,             /* The P1 operand */
 61550   int p2,             /* The P2 operand */
 61551   int p3,             /* The P3 operand */
 61552   const char *zP4,    /* The P4 operand */
 61553   int p4type          /* P4 operand type */
 61554 ){
 61555   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 61556   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
 61557   return addr;
 61560 /*
 61561 ** Add an OP_ParseSchema opcode.  This routine is broken out from
 61562 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
 61563 ** as having been used.
 61564 **
 61565 ** The zWhere string must have been obtained from sqlite3_malloc().
 61566 ** This routine will take ownership of the allocated memory.
 61567 */
 61568 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
 61569   int j;
 61570   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
 61571   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
 61572   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
 61575 /*
 61576 ** Add an opcode that includes the p4 value as an integer.
 61577 */
 61578 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
 61579   Vdbe *p,            /* Add the opcode to this VM */
 61580   int op,             /* The new opcode */
 61581   int p1,             /* The P1 operand */
 61582   int p2,             /* The P2 operand */
 61583   int p3,             /* The P3 operand */
 61584   int p4              /* The P4 operand as an integer */
 61585 ){
 61586   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
 61587   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
 61588   return addr;
 61591 /*
 61592 ** Create a new symbolic label for an instruction that has yet to be
 61593 ** coded.  The symbolic label is really just a negative number.  The
 61594 ** label can be used as the P2 value of an operation.  Later, when
 61595 ** the label is resolved to a specific address, the VDBE will scan
 61596 ** through its operation list and change all values of P2 which match
 61597 ** the label into the resolved address.
 61598 **
 61599 ** The VDBE knows that a P2 value is a label because labels are
 61600 ** always negative and P2 values are suppose to be non-negative.
 61601 ** Hence, a negative P2 value is a label that has yet to be resolved.
 61602 **
 61603 ** Zero is returned if a malloc() fails.
 61604 */
 61605 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
 61606   Parse *p = v->pParse;
 61607   int i = p->nLabel++;
 61608   assert( v->magic==VDBE_MAGIC_INIT );
 61609   if( (i & (i-1))==0 ){
 61610     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
 61611                                        (i*2+1)*sizeof(p->aLabel[0]));
 61613   if( p->aLabel ){
 61614     p->aLabel[i] = -1;
 61616   return -1-i;
 61619 /*
 61620 ** Resolve label "x" to be the address of the next instruction to
 61621 ** be inserted.  The parameter "x" must have been obtained from
 61622 ** a prior call to sqlite3VdbeMakeLabel().
 61623 */
 61624 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
 61625   Parse *p = v->pParse;
 61626   int j = -1-x;
 61627   assert( v->magic==VDBE_MAGIC_INIT );
 61628   assert( j<p->nLabel );
 61629   if( j>=0 && p->aLabel ){
 61630     p->aLabel[j] = v->nOp;
 61632   p->iFixedOp = v->nOp - 1;
 61635 /*
 61636 ** Mark the VDBE as one that can only be run one time.
 61637 */
 61638 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
 61639   p->runOnlyOnce = 1;
 61642 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
 61644 /*
 61645 ** The following type and function are used to iterate through all opcodes
 61646 ** in a Vdbe main program and each of the sub-programs (triggers) it may 
 61647 ** invoke directly or indirectly. It should be used as follows:
 61648 **
 61649 **   Op *pOp;
 61650 **   VdbeOpIter sIter;
 61651 **
 61652 **   memset(&sIter, 0, sizeof(sIter));
 61653 **   sIter.v = v;                            // v is of type Vdbe* 
 61654 **   while( (pOp = opIterNext(&sIter)) ){
 61655 **     // Do something with pOp
 61656 **   }
 61657 **   sqlite3DbFree(v->db, sIter.apSub);
 61658 ** 
 61659 */
 61660 typedef struct VdbeOpIter VdbeOpIter;
 61661 struct VdbeOpIter {
 61662   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
 61663   SubProgram **apSub;        /* Array of subprograms */
 61664   int nSub;                  /* Number of entries in apSub */
 61665   int iAddr;                 /* Address of next instruction to return */
 61666   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
 61667 };
 61668 static Op *opIterNext(VdbeOpIter *p){
 61669   Vdbe *v = p->v;
 61670   Op *pRet = 0;
 61671   Op *aOp;
 61672   int nOp;
 61674   if( p->iSub<=p->nSub ){
 61676     if( p->iSub==0 ){
 61677       aOp = v->aOp;
 61678       nOp = v->nOp;
 61679     }else{
 61680       aOp = p->apSub[p->iSub-1]->aOp;
 61681       nOp = p->apSub[p->iSub-1]->nOp;
 61683     assert( p->iAddr<nOp );
 61685     pRet = &aOp[p->iAddr];
 61686     p->iAddr++;
 61687     if( p->iAddr==nOp ){
 61688       p->iSub++;
 61689       p->iAddr = 0;
 61692     if( pRet->p4type==P4_SUBPROGRAM ){
 61693       int nByte = (p->nSub+1)*sizeof(SubProgram*);
 61694       int j;
 61695       for(j=0; j<p->nSub; j++){
 61696         if( p->apSub[j]==pRet->p4.pProgram ) break;
 61698       if( j==p->nSub ){
 61699         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
 61700         if( !p->apSub ){
 61701           pRet = 0;
 61702         }else{
 61703           p->apSub[p->nSub++] = pRet->p4.pProgram;
 61709   return pRet;
 61712 /*
 61713 ** Check if the program stored in the VM associated with pParse may
 61714 ** throw an ABORT exception (causing the statement, but not entire transaction
 61715 ** to be rolled back). This condition is true if the main program or any
 61716 ** sub-programs contains any of the following:
 61717 **
 61718 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 61719 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
 61720 **   *  OP_Destroy
 61721 **   *  OP_VUpdate
 61722 **   *  OP_VRename
 61723 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
 61724 **
 61725 ** Then check that the value of Parse.mayAbort is true if an
 61726 ** ABORT may be thrown, or false otherwise. Return true if it does
 61727 ** match, or false otherwise. This function is intended to be used as
 61728 ** part of an assert statement in the compiler. Similar to:
 61729 **
 61730 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
 61731 */
 61732 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
 61733   int hasAbort = 0;
 61734   Op *pOp;
 61735   VdbeOpIter sIter;
 61736   memset(&sIter, 0, sizeof(sIter));
 61737   sIter.v = v;
 61739   while( (pOp = opIterNext(&sIter))!=0 ){
 61740     int opcode = pOp->opcode;
 61741     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
 61742 #ifndef SQLITE_OMIT_FOREIGN_KEY
 61743      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
 61744 #endif
 61745      || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
 61746       && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
 61747     ){
 61748       hasAbort = 1;
 61749       break;
 61752   sqlite3DbFree(v->db, sIter.apSub);
 61754   /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
 61755   ** If malloc failed, then the while() loop above may not have iterated
 61756   ** through all opcodes and hasAbort may be set incorrectly. Return
 61757   ** true for this case to prevent the assert() in the callers frame
 61758   ** from failing.  */
 61759   return ( v->db->mallocFailed || hasAbort==mayAbort );
 61761 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
 61763 /*
 61764 ** Loop through the program looking for P2 values that are negative
 61765 ** on jump instructions.  Each such value is a label.  Resolve the
 61766 ** label by setting the P2 value to its correct non-zero value.
 61767 **
 61768 ** This routine is called once after all opcodes have been inserted.
 61769 **
 61770 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
 61771 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
 61772 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
 61773 **
 61774 ** The Op.opflags field is set on all opcodes.
 61775 */
 61776 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
 61777   int i;
 61778   int nMaxArgs = *pMaxFuncArgs;
 61779   Op *pOp;
 61780   Parse *pParse = p->pParse;
 61781   int *aLabel = pParse->aLabel;
 61782   p->readOnly = 1;
 61783   p->bIsReader = 0;
 61784   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
 61785     u8 opcode = pOp->opcode;
 61787     /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
 61788     ** cases from this switch! */
 61789     switch( opcode ){
 61790       case OP_Function:
 61791       case OP_AggStep: {
 61792         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
 61793         break;
 61795       case OP_Transaction: {
 61796         if( pOp->p2!=0 ) p->readOnly = 0;
 61797         /* fall thru */
 61799       case OP_AutoCommit:
 61800       case OP_Savepoint: {
 61801         p->bIsReader = 1;
 61802         break;
 61804 #ifndef SQLITE_OMIT_WAL
 61805       case OP_Checkpoint:
 61806 #endif
 61807       case OP_Vacuum:
 61808       case OP_JournalMode: {
 61809         p->readOnly = 0;
 61810         p->bIsReader = 1;
 61811         break;
 61813 #ifndef SQLITE_OMIT_VIRTUALTABLE
 61814       case OP_VUpdate: {
 61815         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
 61816         break;
 61818       case OP_VFilter: {
 61819         int n;
 61820         assert( p->nOp - i >= 3 );
 61821         assert( pOp[-1].opcode==OP_Integer );
 61822         n = pOp[-1].p1;
 61823         if( n>nMaxArgs ) nMaxArgs = n;
 61824         break;
 61826 #endif
 61827       case OP_Next:
 61828       case OP_NextIfOpen:
 61829       case OP_SorterNext: {
 61830         pOp->p4.xAdvance = sqlite3BtreeNext;
 61831         pOp->p4type = P4_ADVANCE;
 61832         break;
 61834       case OP_Prev:
 61835       case OP_PrevIfOpen: {
 61836         pOp->p4.xAdvance = sqlite3BtreePrevious;
 61837         pOp->p4type = P4_ADVANCE;
 61838         break;
 61842     pOp->opflags = sqlite3OpcodeProperty[opcode];
 61843     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
 61844       assert( -1-pOp->p2<pParse->nLabel );
 61845       pOp->p2 = aLabel[-1-pOp->p2];
 61848   sqlite3DbFree(p->db, pParse->aLabel);
 61849   pParse->aLabel = 0;
 61850   pParse->nLabel = 0;
 61851   *pMaxFuncArgs = nMaxArgs;
 61852   assert( p->bIsReader!=0 || p->btreeMask==0 );
 61855 /*
 61856 ** Return the address of the next instruction to be inserted.
 61857 */
 61858 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
 61859   assert( p->magic==VDBE_MAGIC_INIT );
 61860   return p->nOp;
 61863 /*
 61864 ** This function returns a pointer to the array of opcodes associated with
 61865 ** the Vdbe passed as the first argument. It is the callers responsibility
 61866 ** to arrange for the returned array to be eventually freed using the 
 61867 ** vdbeFreeOpArray() function.
 61868 **
 61869 ** Before returning, *pnOp is set to the number of entries in the returned
 61870 ** array. Also, *pnMaxArg is set to the larger of its current value and 
 61871 ** the number of entries in the Vdbe.apArg[] array required to execute the 
 61872 ** returned program.
 61873 */
 61874 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
 61875   VdbeOp *aOp = p->aOp;
 61876   assert( aOp && !p->db->mallocFailed );
 61878   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
 61879   assert( p->btreeMask==0 );
 61881   resolveP2Values(p, pnMaxArg);
 61882   *pnOp = p->nOp;
 61883   p->aOp = 0;
 61884   return aOp;
 61887 /*
 61888 ** Add a whole list of operations to the operation stack.  Return the
 61889 ** address of the first operation added.
 61890 */
 61891 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
 61892   int addr;
 61893   assert( p->magic==VDBE_MAGIC_INIT );
 61894   if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
 61895     return 0;
 61897   addr = p->nOp;
 61898   if( ALWAYS(nOp>0) ){
 61899     int i;
 61900     VdbeOpList const *pIn = aOp;
 61901     for(i=0; i<nOp; i++, pIn++){
 61902       int p2 = pIn->p2;
 61903       VdbeOp *pOut = &p->aOp[i+addr];
 61904       pOut->opcode = pIn->opcode;
 61905       pOut->p1 = pIn->p1;
 61906       if( p2<0 ){
 61907         assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
 61908         pOut->p2 = addr + ADDR(p2);
 61909       }else{
 61910         pOut->p2 = p2;
 61912       pOut->p3 = pIn->p3;
 61913       pOut->p4type = P4_NOTUSED;
 61914       pOut->p4.p = 0;
 61915       pOut->p5 = 0;
 61916 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 61917       pOut->zComment = 0;
 61918 #endif
 61919 #ifdef SQLITE_VDBE_COVERAGE
 61920       pOut->iSrcLine = iLineno+i;
 61921 #else
 61922       (void)iLineno;
 61923 #endif
 61924 #ifdef SQLITE_DEBUG
 61925       if( p->db->flags & SQLITE_VdbeAddopTrace ){
 61926         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
 61928 #endif
 61930     p->nOp += nOp;
 61932   return addr;
 61935 /*
 61936 ** Change the value of the P1 operand for a specific instruction.
 61937 ** This routine is useful when a large program is loaded from a
 61938 ** static array using sqlite3VdbeAddOpList but we want to make a
 61939 ** few minor changes to the program.
 61940 */
 61941 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
 61942   assert( p!=0 );
 61943   if( ((u32)p->nOp)>addr ){
 61944     p->aOp[addr].p1 = val;
 61948 /*
 61949 ** Change the value of the P2 operand for a specific instruction.
 61950 ** This routine is useful for setting a jump destination.
 61951 */
 61952 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
 61953   assert( p!=0 );
 61954   if( ((u32)p->nOp)>addr ){
 61955     p->aOp[addr].p2 = val;
 61959 /*
 61960 ** Change the value of the P3 operand for a specific instruction.
 61961 */
 61962 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
 61963   assert( p!=0 );
 61964   if( ((u32)p->nOp)>addr ){
 61965     p->aOp[addr].p3 = val;
 61969 /*
 61970 ** Change the value of the P5 operand for the most recently
 61971 ** added operation.
 61972 */
 61973 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
 61974   assert( p!=0 );
 61975   if( p->aOp ){
 61976     assert( p->nOp>0 );
 61977     p->aOp[p->nOp-1].p5 = val;
 61981 /*
 61982 ** Change the P2 operand of instruction addr so that it points to
 61983 ** the address of the next instruction to be coded.
 61984 */
 61985 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
 61986   sqlite3VdbeChangeP2(p, addr, p->nOp);
 61987   p->pParse->iFixedOp = p->nOp - 1;
 61991 /*
 61992 ** If the input FuncDef structure is ephemeral, then free it.  If
 61993 ** the FuncDef is not ephermal, then do nothing.
 61994 */
 61995 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
 61996   if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
 61997     sqlite3DbFree(db, pDef);
 62001 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
 62003 /*
 62004 ** Delete a P4 value if necessary.
 62005 */
 62006 static void freeP4(sqlite3 *db, int p4type, void *p4){
 62007   if( p4 ){
 62008     assert( db );
 62009     switch( p4type ){
 62010       case P4_REAL:
 62011       case P4_INT64:
 62012       case P4_DYNAMIC:
 62013       case P4_INTARRAY: {
 62014         sqlite3DbFree(db, p4);
 62015         break;
 62017       case P4_KEYINFO: {
 62018         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
 62019         break;
 62021       case P4_MPRINTF: {
 62022         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
 62023         break;
 62025       case P4_FUNCDEF: {
 62026         freeEphemeralFunction(db, (FuncDef*)p4);
 62027         break;
 62029       case P4_MEM: {
 62030         if( db->pnBytesFreed==0 ){
 62031           sqlite3ValueFree((sqlite3_value*)p4);
 62032         }else{
 62033           Mem *p = (Mem*)p4;
 62034           sqlite3DbFree(db, p->zMalloc);
 62035           sqlite3DbFree(db, p);
 62037         break;
 62039       case P4_VTAB : {
 62040         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
 62041         break;
 62047 /*
 62048 ** Free the space allocated for aOp and any p4 values allocated for the
 62049 ** opcodes contained within. If aOp is not NULL it is assumed to contain 
 62050 ** nOp entries. 
 62051 */
 62052 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
 62053   if( aOp ){
 62054     Op *pOp;
 62055     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
 62056       freeP4(db, pOp->p4type, pOp->p4.p);
 62057 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62058       sqlite3DbFree(db, pOp->zComment);
 62059 #endif     
 62062   sqlite3DbFree(db, aOp);
 62065 /*
 62066 ** Link the SubProgram object passed as the second argument into the linked
 62067 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
 62068 ** objects when the VM is no longer required.
 62069 */
 62070 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
 62071   p->pNext = pVdbe->pProgram;
 62072   pVdbe->pProgram = p;
 62075 /*
 62076 ** Change the opcode at addr into OP_Noop
 62077 */
 62078 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
 62079   if( p->aOp ){
 62080     VdbeOp *pOp = &p->aOp[addr];
 62081     sqlite3 *db = p->db;
 62082     freeP4(db, pOp->p4type, pOp->p4.p);
 62083     memset(pOp, 0, sizeof(pOp[0]));
 62084     pOp->opcode = OP_Noop;
 62085     if( addr==p->nOp-1 ) p->nOp--;
 62089 /*
 62090 ** Remove the last opcode inserted
 62091 */
 62092 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
 62093   if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
 62094     sqlite3VdbeChangeToNoop(p, p->nOp-1);
 62095     return 1;
 62096   }else{
 62097     return 0;
 62101 /*
 62102 ** Change the value of the P4 operand for a specific instruction.
 62103 ** This routine is useful when a large program is loaded from a
 62104 ** static array using sqlite3VdbeAddOpList but we want to make a
 62105 ** few minor changes to the program.
 62106 **
 62107 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
 62108 ** the string is made into memory obtained from sqlite3_malloc().
 62109 ** A value of n==0 means copy bytes of zP4 up to and including the
 62110 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
 62111 ** 
 62112 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
 62113 ** to a string or structure that is guaranteed to exist for the lifetime of
 62114 ** the Vdbe. In these cases we can just copy the pointer.
 62115 **
 62116 ** If addr<0 then change P4 on the most recently inserted instruction.
 62117 */
 62118 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
 62119   Op *pOp;
 62120   sqlite3 *db;
 62121   assert( p!=0 );
 62122   db = p->db;
 62123   assert( p->magic==VDBE_MAGIC_INIT );
 62124   if( p->aOp==0 || db->mallocFailed ){
 62125     if( n!=P4_VTAB ){
 62126       freeP4(db, n, (void*)*(char**)&zP4);
 62128     return;
 62130   assert( p->nOp>0 );
 62131   assert( addr<p->nOp );
 62132   if( addr<0 ){
 62133     addr = p->nOp - 1;
 62135   pOp = &p->aOp[addr];
 62136   assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
 62137   freeP4(db, pOp->p4type, pOp->p4.p);
 62138   pOp->p4.p = 0;
 62139   if( n==P4_INT32 ){
 62140     /* Note: this cast is safe, because the origin data point was an int
 62141     ** that was cast to a (const char *). */
 62142     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
 62143     pOp->p4type = P4_INT32;
 62144   }else if( zP4==0 ){
 62145     pOp->p4.p = 0;
 62146     pOp->p4type = P4_NOTUSED;
 62147   }else if( n==P4_KEYINFO ){
 62148     pOp->p4.p = (void*)zP4;
 62149     pOp->p4type = P4_KEYINFO;
 62150   }else if( n==P4_VTAB ){
 62151     pOp->p4.p = (void*)zP4;
 62152     pOp->p4type = P4_VTAB;
 62153     sqlite3VtabLock((VTable *)zP4);
 62154     assert( ((VTable *)zP4)->db==p->db );
 62155   }else if( n<0 ){
 62156     pOp->p4.p = (void*)zP4;
 62157     pOp->p4type = (signed char)n;
 62158   }else{
 62159     if( n==0 ) n = sqlite3Strlen30(zP4);
 62160     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
 62161     pOp->p4type = P4_DYNAMIC;
 62165 /*
 62166 ** Set the P4 on the most recently added opcode to the KeyInfo for the
 62167 ** index given.
 62168 */
 62169 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
 62170   Vdbe *v = pParse->pVdbe;
 62171   assert( v!=0 );
 62172   assert( pIdx!=0 );
 62173   sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
 62174                       P4_KEYINFO);
 62177 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62178 /*
 62179 ** Change the comment on the most recently coded instruction.  Or
 62180 ** insert a No-op and add the comment to that new instruction.  This
 62181 ** makes the code easier to read during debugging.  None of this happens
 62182 ** in a production build.
 62183 */
 62184 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
 62185   assert( p->nOp>0 || p->aOp==0 );
 62186   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
 62187   if( p->nOp ){
 62188     assert( p->aOp );
 62189     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
 62190     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
 62193 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
 62194   va_list ap;
 62195   if( p ){
 62196     va_start(ap, zFormat);
 62197     vdbeVComment(p, zFormat, ap);
 62198     va_end(ap);
 62201 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
 62202   va_list ap;
 62203   if( p ){
 62204     sqlite3VdbeAddOp0(p, OP_Noop);
 62205     va_start(ap, zFormat);
 62206     vdbeVComment(p, zFormat, ap);
 62207     va_end(ap);
 62210 #endif  /* NDEBUG */
 62212 #ifdef SQLITE_VDBE_COVERAGE
 62213 /*
 62214 ** Set the value if the iSrcLine field for the previously coded instruction.
 62215 */
 62216 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
 62217   sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
 62219 #endif /* SQLITE_VDBE_COVERAGE */
 62221 /*
 62222 ** Return the opcode for a given address.  If the address is -1, then
 62223 ** return the most recently inserted opcode.
 62224 **
 62225 ** If a memory allocation error has occurred prior to the calling of this
 62226 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
 62227 ** is readable but not writable, though it is cast to a writable value.
 62228 ** The return of a dummy opcode allows the call to continue functioning
 62229 ** after a OOM fault without having to check to see if the return from 
 62230 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
 62231 ** dummy will never be written to.  This is verified by code inspection and
 62232 ** by running with Valgrind.
 62233 */
 62234 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
 62235   /* C89 specifies that the constant "dummy" will be initialized to all
 62236   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
 62237   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
 62238   assert( p->magic==VDBE_MAGIC_INIT );
 62239   if( addr<0 ){
 62240     addr = p->nOp - 1;
 62242   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
 62243   if( p->db->mallocFailed ){
 62244     return (VdbeOp*)&dummy;
 62245   }else{
 62246     return &p->aOp[addr];
 62250 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
 62251 /*
 62252 ** Return an integer value for one of the parameters to the opcode pOp
 62253 ** determined by character c.
 62254 */
 62255 static int translateP(char c, const Op *pOp){
 62256   if( c=='1' ) return pOp->p1;
 62257   if( c=='2' ) return pOp->p2;
 62258   if( c=='3' ) return pOp->p3;
 62259   if( c=='4' ) return pOp->p4.i;
 62260   return pOp->p5;
 62263 /*
 62264 ** Compute a string for the "comment" field of a VDBE opcode listing.
 62265 **
 62266 ** The Synopsis: field in comments in the vdbe.c source file gets converted
 62267 ** to an extra string that is appended to the sqlite3OpcodeName().  In the
 62268 ** absence of other comments, this synopsis becomes the comment on the opcode.
 62269 ** Some translation occurs:
 62270 **
 62271 **       "PX"      ->  "r[X]"
 62272 **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
 62273 **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
 62274 **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
 62275 */
 62276 static int displayComment(
 62277   const Op *pOp,     /* The opcode to be commented */
 62278   const char *zP4,   /* Previously obtained value for P4 */
 62279   char *zTemp,       /* Write result here */
 62280   int nTemp          /* Space available in zTemp[] */
 62281 ){
 62282   const char *zOpName;
 62283   const char *zSynopsis;
 62284   int nOpName;
 62285   int ii, jj;
 62286   zOpName = sqlite3OpcodeName(pOp->opcode);
 62287   nOpName = sqlite3Strlen30(zOpName);
 62288   if( zOpName[nOpName+1] ){
 62289     int seenCom = 0;
 62290     char c;
 62291     zSynopsis = zOpName += nOpName + 1;
 62292     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
 62293       if( c=='P' ){
 62294         c = zSynopsis[++ii];
 62295         if( c=='4' ){
 62296           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
 62297         }else if( c=='X' ){
 62298           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
 62299           seenCom = 1;
 62300         }else{
 62301           int v1 = translateP(c, pOp);
 62302           int v2;
 62303           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
 62304           if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
 62305             ii += 3;
 62306             jj += sqlite3Strlen30(zTemp+jj);
 62307             v2 = translateP(zSynopsis[ii], pOp);
 62308             if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
 62309               ii += 2;
 62310               v2++;
 62312             if( v2>1 ){
 62313               sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
 62315           }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
 62316             ii += 4;
 62319         jj += sqlite3Strlen30(zTemp+jj);
 62320       }else{
 62321         zTemp[jj++] = c;
 62324     if( !seenCom && jj<nTemp-5 && pOp->zComment ){
 62325       sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
 62326       jj += sqlite3Strlen30(zTemp+jj);
 62328     if( jj<nTemp ) zTemp[jj] = 0;
 62329   }else if( pOp->zComment ){
 62330     sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
 62331     jj = sqlite3Strlen30(zTemp);
 62332   }else{
 62333     zTemp[0] = 0;
 62334     jj = 0;
 62336   return jj;
 62338 #endif /* SQLITE_DEBUG */
 62341 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
 62342      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 62343 /*
 62344 ** Compute a string that describes the P4 parameter for an opcode.
 62345 ** Use zTemp for any required temporary buffer space.
 62346 */
 62347 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
 62348   char *zP4 = zTemp;
 62349   assert( nTemp>=20 );
 62350   switch( pOp->p4type ){
 62351     case P4_KEYINFO: {
 62352       int i, j;
 62353       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
 62354       assert( pKeyInfo->aSortOrder!=0 );
 62355       sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
 62356       i = sqlite3Strlen30(zTemp);
 62357       for(j=0; j<pKeyInfo->nField; j++){
 62358         CollSeq *pColl = pKeyInfo->aColl[j];
 62359         const char *zColl = pColl ? pColl->zName : "nil";
 62360         int n = sqlite3Strlen30(zColl);
 62361         if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
 62362           zColl = "B";
 62363           n = 1;
 62365         if( i+n>nTemp-6 ){
 62366           memcpy(&zTemp[i],",...",4);
 62367           break;
 62369         zTemp[i++] = ',';
 62370         if( pKeyInfo->aSortOrder[j] ){
 62371           zTemp[i++] = '-';
 62373         memcpy(&zTemp[i], zColl, n+1);
 62374         i += n;
 62376       zTemp[i++] = ')';
 62377       zTemp[i] = 0;
 62378       assert( i<nTemp );
 62379       break;
 62381     case P4_COLLSEQ: {
 62382       CollSeq *pColl = pOp->p4.pColl;
 62383       sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
 62384       break;
 62386     case P4_FUNCDEF: {
 62387       FuncDef *pDef = pOp->p4.pFunc;
 62388       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
 62389       break;
 62391     case P4_INT64: {
 62392       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
 62393       break;
 62395     case P4_INT32: {
 62396       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
 62397       break;
 62399     case P4_REAL: {
 62400       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
 62401       break;
 62403     case P4_MEM: {
 62404       Mem *pMem = pOp->p4.pMem;
 62405       if( pMem->flags & MEM_Str ){
 62406         zP4 = pMem->z;
 62407       }else if( pMem->flags & MEM_Int ){
 62408         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
 62409       }else if( pMem->flags & MEM_Real ){
 62410         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
 62411       }else if( pMem->flags & MEM_Null ){
 62412         sqlite3_snprintf(nTemp, zTemp, "NULL");
 62413       }else{
 62414         assert( pMem->flags & MEM_Blob );
 62415         zP4 = "(blob)";
 62417       break;
 62419 #ifndef SQLITE_OMIT_VIRTUALTABLE
 62420     case P4_VTAB: {
 62421       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
 62422       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
 62423       break;
 62425 #endif
 62426     case P4_INTARRAY: {
 62427       sqlite3_snprintf(nTemp, zTemp, "intarray");
 62428       break;
 62430     case P4_SUBPROGRAM: {
 62431       sqlite3_snprintf(nTemp, zTemp, "program");
 62432       break;
 62434     case P4_ADVANCE: {
 62435       zTemp[0] = 0;
 62436       break;
 62438     default: {
 62439       zP4 = pOp->p4.z;
 62440       if( zP4==0 ){
 62441         zP4 = zTemp;
 62442         zTemp[0] = 0;
 62446   assert( zP4!=0 );
 62447   return zP4;
 62449 #endif
 62451 /*
 62452 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
 62453 **
 62454 ** The prepared statements need to know in advance the complete set of
 62455 ** attached databases that will be use.  A mask of these databases
 62456 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
 62457 ** p->btreeMask of databases that will require a lock.
 62458 */
 62459 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
 62460   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
 62461   assert( i<(int)sizeof(p->btreeMask)*8 );
 62462   p->btreeMask |= ((yDbMask)1)<<i;
 62463   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
 62464     p->lockMask |= ((yDbMask)1)<<i;
 62468 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 62469 /*
 62470 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
 62471 ** this routine obtains the mutex associated with each BtShared structure
 62472 ** that may be accessed by the VM passed as an argument. In doing so it also
 62473 ** sets the BtShared.db member of each of the BtShared structures, ensuring
 62474 ** that the correct busy-handler callback is invoked if required.
 62475 **
 62476 ** If SQLite is not threadsafe but does support shared-cache mode, then
 62477 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
 62478 ** of all of BtShared structures accessible via the database handle 
 62479 ** associated with the VM.
 62480 **
 62481 ** If SQLite is not threadsafe and does not support shared-cache mode, this
 62482 ** function is a no-op.
 62483 **
 62484 ** The p->btreeMask field is a bitmask of all btrees that the prepared 
 62485 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
 62486 ** corresponding to btrees that use shared cache.  Then the runtime of
 62487 ** this routine is N*N.  But as N is rarely more than 1, this should not
 62488 ** be a problem.
 62489 */
 62490 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
 62491   int i;
 62492   yDbMask mask;
 62493   sqlite3 *db;
 62494   Db *aDb;
 62495   int nDb;
 62496   if( p->lockMask==0 ) return;  /* The common case */
 62497   db = p->db;
 62498   aDb = db->aDb;
 62499   nDb = db->nDb;
 62500   for(i=0, mask=1; i<nDb; i++, mask += mask){
 62501     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 62502       sqlite3BtreeEnter(aDb[i].pBt);
 62506 #endif
 62508 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
 62509 /*
 62510 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
 62511 */
 62512 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
 62513   int i;
 62514   yDbMask mask;
 62515   sqlite3 *db;
 62516   Db *aDb;
 62517   int nDb;
 62518   if( p->lockMask==0 ) return;  /* The common case */
 62519   db = p->db;
 62520   aDb = db->aDb;
 62521   nDb = db->nDb;
 62522   for(i=0, mask=1; i<nDb; i++, mask += mask){
 62523     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
 62524       sqlite3BtreeLeave(aDb[i].pBt);
 62528 #endif
 62530 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 62531 /*
 62532 ** Print a single opcode.  This routine is used for debugging only.
 62533 */
 62534 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
 62535   char *zP4;
 62536   char zPtr[50];
 62537   char zCom[100];
 62538   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
 62539   if( pOut==0 ) pOut = stdout;
 62540   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
 62541 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62542   displayComment(pOp, zP4, zCom, sizeof(zCom));
 62543 #else
 62544   zCom[0] = 0;
 62545 #endif
 62546   /* NB:  The sqlite3OpcodeName() function is implemented by code created
 62547   ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
 62548   ** information from the vdbe.c source text */
 62549   fprintf(pOut, zFormat1, pc, 
 62550       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
 62551       zCom
 62552   );
 62553   fflush(pOut);
 62555 #endif
 62557 /*
 62558 ** Release an array of N Mem elements
 62559 */
 62560 static void releaseMemArray(Mem *p, int N){
 62561   if( p && N ){
 62562     Mem *pEnd;
 62563     sqlite3 *db = p->db;
 62564     u8 malloc_failed = db->mallocFailed;
 62565     if( db->pnBytesFreed ){
 62566       for(pEnd=&p[N]; p<pEnd; p++){
 62567         sqlite3DbFree(db, p->zMalloc);
 62569       return;
 62571     for(pEnd=&p[N]; p<pEnd; p++){
 62572       assert( (&p[1])==pEnd || p[0].db==p[1].db );
 62573       assert( sqlite3VdbeCheckMemInvariants(p) );
 62575       /* This block is really an inlined version of sqlite3VdbeMemRelease()
 62576       ** that takes advantage of the fact that the memory cell value is 
 62577       ** being set to NULL after releasing any dynamic resources.
 62578       **
 62579       ** The justification for duplicating code is that according to 
 62580       ** callgrind, this causes a certain test case to hit the CPU 4.7 
 62581       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
 62582       ** sqlite3MemRelease() were called from here. With -O2, this jumps
 62583       ** to 6.6 percent. The test case is inserting 1000 rows into a table 
 62584       ** with no indexes using a single prepared INSERT statement, bind() 
 62585       ** and reset(). Inserts are grouped into a transaction.
 62586       */
 62587       testcase( p->flags & MEM_Agg );
 62588       testcase( p->flags & MEM_Dyn );
 62589       testcase( p->flags & MEM_Frame );
 62590       testcase( p->flags & MEM_RowSet );
 62591       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
 62592         sqlite3VdbeMemRelease(p);
 62593       }else if( p->zMalloc ){
 62594         sqlite3DbFree(db, p->zMalloc);
 62595         p->zMalloc = 0;
 62598       p->flags = MEM_Undefined;
 62600     db->mallocFailed = malloc_failed;
 62604 /*
 62605 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
 62606 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
 62607 */
 62608 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
 62609   int i;
 62610   Mem *aMem = VdbeFrameMem(p);
 62611   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
 62612   for(i=0; i<p->nChildCsr; i++){
 62613     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
 62615   releaseMemArray(aMem, p->nChildMem);
 62616   sqlite3DbFree(p->v->db, p);
 62619 #ifndef SQLITE_OMIT_EXPLAIN
 62620 /*
 62621 ** Give a listing of the program in the virtual machine.
 62622 **
 62623 ** The interface is the same as sqlite3VdbeExec().  But instead of
 62624 ** running the code, it invokes the callback once for each instruction.
 62625 ** This feature is used to implement "EXPLAIN".
 62626 **
 62627 ** When p->explain==1, each instruction is listed.  When
 62628 ** p->explain==2, only OP_Explain instructions are listed and these
 62629 ** are shown in a different format.  p->explain==2 is used to implement
 62630 ** EXPLAIN QUERY PLAN.
 62631 **
 62632 ** When p->explain==1, first the main program is listed, then each of
 62633 ** the trigger subprograms are listed one by one.
 62634 */
 62635 SQLITE_PRIVATE int sqlite3VdbeList(
 62636   Vdbe *p                   /* The VDBE */
 62637 ){
 62638   int nRow;                            /* Stop when row count reaches this */
 62639   int nSub = 0;                        /* Number of sub-vdbes seen so far */
 62640   SubProgram **apSub = 0;              /* Array of sub-vdbes */
 62641   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
 62642   sqlite3 *db = p->db;                 /* The database connection */
 62643   int i;                               /* Loop counter */
 62644   int rc = SQLITE_OK;                  /* Return code */
 62645   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
 62647   assert( p->explain );
 62648   assert( p->magic==VDBE_MAGIC_RUN );
 62649   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
 62651   /* Even though this opcode does not use dynamic strings for
 62652   ** the result, result columns may become dynamic if the user calls
 62653   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
 62654   */
 62655   releaseMemArray(pMem, 8);
 62656   p->pResultSet = 0;
 62658   if( p->rc==SQLITE_NOMEM ){
 62659     /* This happens if a malloc() inside a call to sqlite3_column_text() or
 62660     ** sqlite3_column_text16() failed.  */
 62661     db->mallocFailed = 1;
 62662     return SQLITE_ERROR;
 62665   /* When the number of output rows reaches nRow, that means the
 62666   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
 62667   ** nRow is the sum of the number of rows in the main program, plus
 62668   ** the sum of the number of rows in all trigger subprograms encountered
 62669   ** so far.  The nRow value will increase as new trigger subprograms are
 62670   ** encountered, but p->pc will eventually catch up to nRow.
 62671   */
 62672   nRow = p->nOp;
 62673   if( p->explain==1 ){
 62674     /* The first 8 memory cells are used for the result set.  So we will
 62675     ** commandeer the 9th cell to use as storage for an array of pointers
 62676     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
 62677     ** cells.  */
 62678     assert( p->nMem>9 );
 62679     pSub = &p->aMem[9];
 62680     if( pSub->flags&MEM_Blob ){
 62681       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
 62682       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
 62683       nSub = pSub->n/sizeof(Vdbe*);
 62684       apSub = (SubProgram **)pSub->z;
 62686     for(i=0; i<nSub; i++){
 62687       nRow += apSub[i]->nOp;
 62691   do{
 62692     i = p->pc++;
 62693   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
 62694   if( i>=nRow ){
 62695     p->rc = SQLITE_OK;
 62696     rc = SQLITE_DONE;
 62697   }else if( db->u1.isInterrupted ){
 62698     p->rc = SQLITE_INTERRUPT;
 62699     rc = SQLITE_ERROR;
 62700     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
 62701   }else{
 62702     char *zP4;
 62703     Op *pOp;
 62704     if( i<p->nOp ){
 62705       /* The output line number is small enough that we are still in the
 62706       ** main program. */
 62707       pOp = &p->aOp[i];
 62708     }else{
 62709       /* We are currently listing subprograms.  Figure out which one and
 62710       ** pick up the appropriate opcode. */
 62711       int j;
 62712       i -= p->nOp;
 62713       for(j=0; i>=apSub[j]->nOp; j++){
 62714         i -= apSub[j]->nOp;
 62716       pOp = &apSub[j]->aOp[i];
 62718     if( p->explain==1 ){
 62719       pMem->flags = MEM_Int;
 62720       pMem->u.i = i;                                /* Program counter */
 62721       pMem++;
 62723       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
 62724       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
 62725       assert( pMem->z!=0 );
 62726       pMem->n = sqlite3Strlen30(pMem->z);
 62727       pMem->enc = SQLITE_UTF8;
 62728       pMem++;
 62730       /* When an OP_Program opcode is encounter (the only opcode that has
 62731       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
 62732       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
 62733       ** has not already been seen.
 62734       */
 62735       if( pOp->p4type==P4_SUBPROGRAM ){
 62736         int nByte = (nSub+1)*sizeof(SubProgram*);
 62737         int j;
 62738         for(j=0; j<nSub; j++){
 62739           if( apSub[j]==pOp->p4.pProgram ) break;
 62741         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
 62742           apSub = (SubProgram **)pSub->z;
 62743           apSub[nSub++] = pOp->p4.pProgram;
 62744           pSub->flags |= MEM_Blob;
 62745           pSub->n = nSub*sizeof(SubProgram*);
 62750     pMem->flags = MEM_Int;
 62751     pMem->u.i = pOp->p1;                          /* P1 */
 62752     pMem++;
 62754     pMem->flags = MEM_Int;
 62755     pMem->u.i = pOp->p2;                          /* P2 */
 62756     pMem++;
 62758     pMem->flags = MEM_Int;
 62759     pMem->u.i = pOp->p3;                          /* P3 */
 62760     pMem++;
 62762     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
 62763       assert( p->db->mallocFailed );
 62764       return SQLITE_ERROR;
 62766     pMem->flags = MEM_Str|MEM_Term;
 62767     zP4 = displayP4(pOp, pMem->z, 32);
 62768     if( zP4!=pMem->z ){
 62769       sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
 62770     }else{
 62771       assert( pMem->z!=0 );
 62772       pMem->n = sqlite3Strlen30(pMem->z);
 62773       pMem->enc = SQLITE_UTF8;
 62775     pMem++;
 62777     if( p->explain==1 ){
 62778       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
 62779         assert( p->db->mallocFailed );
 62780         return SQLITE_ERROR;
 62782       pMem->flags = MEM_Str|MEM_Term;
 62783       pMem->n = 2;
 62784       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
 62785       pMem->enc = SQLITE_UTF8;
 62786       pMem++;
 62788 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 62789       if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
 62790         assert( p->db->mallocFailed );
 62791         return SQLITE_ERROR;
 62793       pMem->flags = MEM_Str|MEM_Term;
 62794       pMem->n = displayComment(pOp, zP4, pMem->z, 500);
 62795       pMem->enc = SQLITE_UTF8;
 62796 #else
 62797       pMem->flags = MEM_Null;                       /* Comment */
 62798 #endif
 62801     p->nResColumn = 8 - 4*(p->explain-1);
 62802     p->pResultSet = &p->aMem[1];
 62803     p->rc = SQLITE_OK;
 62804     rc = SQLITE_ROW;
 62806   return rc;
 62808 #endif /* SQLITE_OMIT_EXPLAIN */
 62810 #ifdef SQLITE_DEBUG
 62811 /*
 62812 ** Print the SQL that was used to generate a VDBE program.
 62813 */
 62814 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
 62815   const char *z = 0;
 62816   if( p->zSql ){
 62817     z = p->zSql;
 62818   }else if( p->nOp>=1 ){
 62819     const VdbeOp *pOp = &p->aOp[0];
 62820     if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
 62821       z = pOp->p4.z;
 62822       while( sqlite3Isspace(*z) ) z++;
 62825   if( z ) printf("SQL: [%s]\n", z);
 62827 #endif
 62829 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
 62830 /*
 62831 ** Print an IOTRACE message showing SQL content.
 62832 */
 62833 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
 62834   int nOp = p->nOp;
 62835   VdbeOp *pOp;
 62836   if( sqlite3IoTrace==0 ) return;
 62837   if( nOp<1 ) return;
 62838   pOp = &p->aOp[0];
 62839   if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
 62840     int i, j;
 62841     char z[1000];
 62842     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
 62843     for(i=0; sqlite3Isspace(z[i]); i++){}
 62844     for(j=0; z[i]; i++){
 62845       if( sqlite3Isspace(z[i]) ){
 62846         if( z[i-1]!=' ' ){
 62847           z[j++] = ' ';
 62849       }else{
 62850         z[j++] = z[i];
 62853     z[j] = 0;
 62854     sqlite3IoTrace("SQL %s\n", z);
 62857 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 62859 /*
 62860 ** Allocate space from a fixed size buffer and return a pointer to
 62861 ** that space.  If insufficient space is available, return NULL.
 62862 **
 62863 ** The pBuf parameter is the initial value of a pointer which will
 62864 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
 62865 ** NULL, it means that memory space has already been allocated and that
 62866 ** this routine should not allocate any new memory.  When pBuf is not
 62867 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
 62868 ** is NULL.
 62869 **
 62870 ** nByte is the number of bytes of space needed.
 62871 **
 62872 ** *ppFrom points to available space and pEnd points to the end of the
 62873 ** available space.  When space is allocated, *ppFrom is advanced past
 62874 ** the end of the allocated space.
 62875 **
 62876 ** *pnByte is a counter of the number of bytes of space that have failed
 62877 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
 62878 ** request, then increment *pnByte by the amount of the request.
 62879 */
 62880 static void *allocSpace(
 62881   void *pBuf,          /* Where return pointer will be stored */
 62882   int nByte,           /* Number of bytes to allocate */
 62883   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
 62884   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
 62885   int *pnByte          /* If allocation cannot be made, increment *pnByte */
 62886 ){
 62887   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
 62888   if( pBuf ) return pBuf;
 62889   nByte = ROUND8(nByte);
 62890   if( &(*ppFrom)[nByte] <= pEnd ){
 62891     pBuf = (void*)*ppFrom;
 62892     *ppFrom += nByte;
 62893   }else{
 62894     *pnByte += nByte;
 62896   return pBuf;
 62899 /*
 62900 ** Rewind the VDBE back to the beginning in preparation for
 62901 ** running it.
 62902 */
 62903 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
 62904 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
 62905   int i;
 62906 #endif
 62907   assert( p!=0 );
 62908   assert( p->magic==VDBE_MAGIC_INIT );
 62910   /* There should be at least one opcode.
 62911   */
 62912   assert( p->nOp>0 );
 62914   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
 62915   p->magic = VDBE_MAGIC_RUN;
 62917 #ifdef SQLITE_DEBUG
 62918   for(i=1; i<p->nMem; i++){
 62919     assert( p->aMem[i].db==p->db );
 62921 #endif
 62922   p->pc = -1;
 62923   p->rc = SQLITE_OK;
 62924   p->errorAction = OE_Abort;
 62925   p->magic = VDBE_MAGIC_RUN;
 62926   p->nChange = 0;
 62927   p->cacheCtr = 1;
 62928   p->minWriteFileFormat = 255;
 62929   p->iStatement = 0;
 62930   p->nFkConstraint = 0;
 62931 #ifdef VDBE_PROFILE
 62932   for(i=0; i<p->nOp; i++){
 62933     p->aOp[i].cnt = 0;
 62934     p->aOp[i].cycles = 0;
 62936 #endif
 62939 /*
 62940 ** Prepare a virtual machine for execution for the first time after
 62941 ** creating the virtual machine.  This involves things such
 62942 ** as allocating stack space and initializing the program counter.
 62943 ** After the VDBE has be prepped, it can be executed by one or more
 62944 ** calls to sqlite3VdbeExec().  
 62945 **
 62946 ** This function may be called exact once on a each virtual machine.
 62947 ** After this routine is called the VM has been "packaged" and is ready
 62948 ** to run.  After this routine is called, futher calls to 
 62949 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
 62950 ** the Vdbe from the Parse object that helped generate it so that the
 62951 ** the Vdbe becomes an independent entity and the Parse object can be
 62952 ** destroyed.
 62953 **
 62954 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
 62955 ** to its initial state after it has been run.
 62956 */
 62957 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
 62958   Vdbe *p,                       /* The VDBE */
 62959   Parse *pParse                  /* Parsing context */
 62960 ){
 62961   sqlite3 *db;                   /* The database connection */
 62962   int nVar;                      /* Number of parameters */
 62963   int nMem;                      /* Number of VM memory registers */
 62964   int nCursor;                   /* Number of cursors required */
 62965   int nArg;                      /* Number of arguments in subprograms */
 62966   int nOnce;                     /* Number of OP_Once instructions */
 62967   int n;                         /* Loop counter */
 62968   u8 *zCsr;                      /* Memory available for allocation */
 62969   u8 *zEnd;                      /* First byte past allocated memory */
 62970   int nByte;                     /* How much extra memory is needed */
 62972   assert( p!=0 );
 62973   assert( p->nOp>0 );
 62974   assert( pParse!=0 );
 62975   assert( p->magic==VDBE_MAGIC_INIT );
 62976   assert( pParse==p->pParse );
 62977   db = p->db;
 62978   assert( db->mallocFailed==0 );
 62979   nVar = pParse->nVar;
 62980   nMem = pParse->nMem;
 62981   nCursor = pParse->nTab;
 62982   nArg = pParse->nMaxArg;
 62983   nOnce = pParse->nOnce;
 62984   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
 62986   /* For each cursor required, also allocate a memory cell. Memory
 62987   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
 62988   ** the vdbe program. Instead they are used to allocate space for
 62989   ** VdbeCursor/BtCursor structures. The blob of memory associated with 
 62990   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
 62991   ** stores the blob of memory associated with cursor 1, etc.
 62992   **
 62993   ** See also: allocateCursor().
 62994   */
 62995   nMem += nCursor;
 62997   /* Allocate space for memory registers, SQL variables, VDBE cursors and 
 62998   ** an array to marshal SQL function arguments in.
 62999   */
 63000   zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
 63001   zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
 63003   resolveP2Values(p, &nArg);
 63004   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
 63005   if( pParse->explain && nMem<10 ){
 63006     nMem = 10;
 63008   memset(zCsr, 0, zEnd-zCsr);
 63009   zCsr += (zCsr - (u8*)0)&7;
 63010   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
 63011   p->expired = 0;
 63013   /* Memory for registers, parameters, cursor, etc, is allocated in two
 63014   ** passes.  On the first pass, we try to reuse unused space at the 
 63015   ** end of the opcode array.  If we are unable to satisfy all memory
 63016   ** requirements by reusing the opcode array tail, then the second
 63017   ** pass will fill in the rest using a fresh allocation.  
 63018   **
 63019   ** This two-pass approach that reuses as much memory as possible from
 63020   ** the leftover space at the end of the opcode array can significantly
 63021   ** reduce the amount of memory held by a prepared statement.
 63022   */
 63023   do {
 63024     nByte = 0;
 63025     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
 63026     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
 63027     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
 63028     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
 63029     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
 63030                           &zCsr, zEnd, &nByte);
 63031     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
 63032     if( nByte ){
 63033       p->pFree = sqlite3DbMallocZero(db, nByte);
 63035     zCsr = p->pFree;
 63036     zEnd = &zCsr[nByte];
 63037   }while( nByte && !db->mallocFailed );
 63039   p->nCursor = nCursor;
 63040   p->nOnceFlag = nOnce;
 63041   if( p->aVar ){
 63042     p->nVar = (ynVar)nVar;
 63043     for(n=0; n<nVar; n++){
 63044       p->aVar[n].flags = MEM_Null;
 63045       p->aVar[n].db = db;
 63048   if( p->azVar ){
 63049     p->nzVar = pParse->nzVar;
 63050     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
 63051     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
 63053   if( p->aMem ){
 63054     p->aMem--;                      /* aMem[] goes from 1..nMem */
 63055     p->nMem = nMem;                 /*       not from 0..nMem-1 */
 63056     for(n=1; n<=nMem; n++){
 63057       p->aMem[n].flags = MEM_Undefined;
 63058       p->aMem[n].db = db;
 63061   p->explain = pParse->explain;
 63062   sqlite3VdbeRewind(p);
 63065 /*
 63066 ** Close a VDBE cursor and release all the resources that cursor 
 63067 ** happens to hold.
 63068 */
 63069 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
 63070   if( pCx==0 ){
 63071     return;
 63073   sqlite3VdbeSorterClose(p->db, pCx);
 63074   if( pCx->pBt ){
 63075     sqlite3BtreeClose(pCx->pBt);
 63076     /* The pCx->pCursor will be close automatically, if it exists, by
 63077     ** the call above. */
 63078   }else if( pCx->pCursor ){
 63079     sqlite3BtreeCloseCursor(pCx->pCursor);
 63081 #ifndef SQLITE_OMIT_VIRTUALTABLE
 63082   if( pCx->pVtabCursor ){
 63083     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
 63084     const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
 63085     p->inVtabMethod = 1;
 63086     pModule->xClose(pVtabCursor);
 63087     p->inVtabMethod = 0;
 63089 #endif
 63092 /*
 63093 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
 63094 ** is used, for example, when a trigger sub-program is halted to restore
 63095 ** control to the main program.
 63096 */
 63097 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
 63098   Vdbe *v = pFrame->v;
 63099   v->aOnceFlag = pFrame->aOnceFlag;
 63100   v->nOnceFlag = pFrame->nOnceFlag;
 63101   v->aOp = pFrame->aOp;
 63102   v->nOp = pFrame->nOp;
 63103   v->aMem = pFrame->aMem;
 63104   v->nMem = pFrame->nMem;
 63105   v->apCsr = pFrame->apCsr;
 63106   v->nCursor = pFrame->nCursor;
 63107   v->db->lastRowid = pFrame->lastRowid;
 63108   v->nChange = pFrame->nChange;
 63109   return pFrame->pc;
 63112 /*
 63113 ** Close all cursors.
 63114 **
 63115 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
 63116 ** cell array. This is necessary as the memory cell array may contain
 63117 ** pointers to VdbeFrame objects, which may in turn contain pointers to
 63118 ** open cursors.
 63119 */
 63120 static void closeAllCursors(Vdbe *p){
 63121   if( p->pFrame ){
 63122     VdbeFrame *pFrame;
 63123     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 63124     sqlite3VdbeFrameRestore(pFrame);
 63126   p->pFrame = 0;
 63127   p->nFrame = 0;
 63129   if( p->apCsr ){
 63130     int i;
 63131     for(i=0; i<p->nCursor; i++){
 63132       VdbeCursor *pC = p->apCsr[i];
 63133       if( pC ){
 63134         sqlite3VdbeFreeCursor(p, pC);
 63135         p->apCsr[i] = 0;
 63139   if( p->aMem ){
 63140     releaseMemArray(&p->aMem[1], p->nMem);
 63142   while( p->pDelFrame ){
 63143     VdbeFrame *pDel = p->pDelFrame;
 63144     p->pDelFrame = pDel->pParent;
 63145     sqlite3VdbeFrameDelete(pDel);
 63148   /* Delete any auxdata allocations made by the VM */
 63149   sqlite3VdbeDeleteAuxData(p, -1, 0);
 63150   assert( p->pAuxData==0 );
 63153 /*
 63154 ** Clean up the VM after execution.
 63155 **
 63156 ** This routine will automatically close any cursors, lists, and/or
 63157 ** sorters that were left open.  It also deletes the values of
 63158 ** variables in the aVar[] array.
 63159 */
 63160 static void Cleanup(Vdbe *p){
 63161   sqlite3 *db = p->db;
 63163 #ifdef SQLITE_DEBUG
 63164   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
 63165   ** Vdbe.aMem[] arrays have already been cleaned up.  */
 63166   int i;
 63167   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
 63168   if( p->aMem ){
 63169     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
 63171 #endif
 63173   sqlite3DbFree(db, p->zErrMsg);
 63174   p->zErrMsg = 0;
 63175   p->pResultSet = 0;
 63178 /*
 63179 ** Set the number of result columns that will be returned by this SQL
 63180 ** statement. This is now set at compile time, rather than during
 63181 ** execution of the vdbe program so that sqlite3_column_count() can
 63182 ** be called on an SQL statement before sqlite3_step().
 63183 */
 63184 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
 63185   Mem *pColName;
 63186   int n;
 63187   sqlite3 *db = p->db;
 63189   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 63190   sqlite3DbFree(db, p->aColName);
 63191   n = nResColumn*COLNAME_N;
 63192   p->nResColumn = (u16)nResColumn;
 63193   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
 63194   if( p->aColName==0 ) return;
 63195   while( n-- > 0 ){
 63196     pColName->flags = MEM_Null;
 63197     pColName->db = p->db;
 63198     pColName++;
 63202 /*
 63203 ** Set the name of the idx'th column to be returned by the SQL statement.
 63204 ** zName must be a pointer to a nul terminated string.
 63205 **
 63206 ** This call must be made after a call to sqlite3VdbeSetNumCols().
 63207 **
 63208 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
 63209 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
 63210 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
 63211 */
 63212 SQLITE_PRIVATE int sqlite3VdbeSetColName(
 63213   Vdbe *p,                         /* Vdbe being configured */
 63214   int idx,                         /* Index of column zName applies to */
 63215   int var,                         /* One of the COLNAME_* constants */
 63216   const char *zName,               /* Pointer to buffer containing name */
 63217   void (*xDel)(void*)              /* Memory management strategy for zName */
 63218 ){
 63219   int rc;
 63220   Mem *pColName;
 63221   assert( idx<p->nResColumn );
 63222   assert( var<COLNAME_N );
 63223   if( p->db->mallocFailed ){
 63224     assert( !zName || xDel!=SQLITE_DYNAMIC );
 63225     return SQLITE_NOMEM;
 63227   assert( p->aColName!=0 );
 63228   pColName = &(p->aColName[idx+var*p->nResColumn]);
 63229   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
 63230   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
 63231   return rc;
 63234 /*
 63235 ** A read or write transaction may or may not be active on database handle
 63236 ** db. If a transaction is active, commit it. If there is a
 63237 ** write-transaction spanning more than one database file, this routine
 63238 ** takes care of the master journal trickery.
 63239 */
 63240 static int vdbeCommit(sqlite3 *db, Vdbe *p){
 63241   int i;
 63242   int nTrans = 0;  /* Number of databases with an active write-transaction */
 63243   int rc = SQLITE_OK;
 63244   int needXcommit = 0;
 63246 #ifdef SQLITE_OMIT_VIRTUALTABLE
 63247   /* With this option, sqlite3VtabSync() is defined to be simply 
 63248   ** SQLITE_OK so p is not used. 
 63249   */
 63250   UNUSED_PARAMETER(p);
 63251 #endif
 63253   /* Before doing anything else, call the xSync() callback for any
 63254   ** virtual module tables written in this transaction. This has to
 63255   ** be done before determining whether a master journal file is 
 63256   ** required, as an xSync() callback may add an attached database
 63257   ** to the transaction.
 63258   */
 63259   rc = sqlite3VtabSync(db, p);
 63261   /* This loop determines (a) if the commit hook should be invoked and
 63262   ** (b) how many database files have open write transactions, not 
 63263   ** including the temp database. (b) is important because if more than 
 63264   ** one database file has an open write transaction, a master journal
 63265   ** file is required for an atomic commit.
 63266   */ 
 63267   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 63268     Btree *pBt = db->aDb[i].pBt;
 63269     if( sqlite3BtreeIsInTrans(pBt) ){
 63270       needXcommit = 1;
 63271       if( i!=1 ) nTrans++;
 63272       sqlite3BtreeEnter(pBt);
 63273       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
 63274       sqlite3BtreeLeave(pBt);
 63277   if( rc!=SQLITE_OK ){
 63278     return rc;
 63281   /* If there are any write-transactions at all, invoke the commit hook */
 63282   if( needXcommit && db->xCommitCallback ){
 63283     rc = db->xCommitCallback(db->pCommitArg);
 63284     if( rc ){
 63285       return SQLITE_CONSTRAINT_COMMITHOOK;
 63289   /* The simple case - no more than one database file (not counting the
 63290   ** TEMP database) has a transaction active.   There is no need for the
 63291   ** master-journal.
 63292   **
 63293   ** If the return value of sqlite3BtreeGetFilename() is a zero length
 63294   ** string, it means the main database is :memory: or a temp file.  In 
 63295   ** that case we do not support atomic multi-file commits, so use the 
 63296   ** simple case then too.
 63297   */
 63298   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
 63299    || nTrans<=1
 63300   ){
 63301     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 63302       Btree *pBt = db->aDb[i].pBt;
 63303       if( pBt ){
 63304         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
 63308     /* Do the commit only if all databases successfully complete phase 1. 
 63309     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
 63310     ** IO error while deleting or truncating a journal file. It is unlikely,
 63311     ** but could happen. In this case abandon processing and return the error.
 63312     */
 63313     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 63314       Btree *pBt = db->aDb[i].pBt;
 63315       if( pBt ){
 63316         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
 63319     if( rc==SQLITE_OK ){
 63320       sqlite3VtabCommit(db);
 63324   /* The complex case - There is a multi-file write-transaction active.
 63325   ** This requires a master journal file to ensure the transaction is
 63326   ** committed atomicly.
 63327   */
 63328 #ifndef SQLITE_OMIT_DISKIO
 63329   else{
 63330     sqlite3_vfs *pVfs = db->pVfs;
 63331     int needSync = 0;
 63332     char *zMaster = 0;   /* File-name for the master journal */
 63333     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
 63334     sqlite3_file *pMaster = 0;
 63335     i64 offset = 0;
 63336     int res;
 63337     int retryCount = 0;
 63338     int nMainFile;
 63340     /* Select a master journal file name */
 63341     nMainFile = sqlite3Strlen30(zMainFile);
 63342     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
 63343     if( zMaster==0 ) return SQLITE_NOMEM;
 63344     do {
 63345       u32 iRandom;
 63346       if( retryCount ){
 63347         if( retryCount>100 ){
 63348           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
 63349           sqlite3OsDelete(pVfs, zMaster, 0);
 63350           break;
 63351         }else if( retryCount==1 ){
 63352           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
 63355       retryCount++;
 63356       sqlite3_randomness(sizeof(iRandom), &iRandom);
 63357       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
 63358                                (iRandom>>8)&0xffffff, iRandom&0xff);
 63359       /* The antipenultimate character of the master journal name must
 63360       ** be "9" to avoid name collisions when using 8+3 filenames. */
 63361       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
 63362       sqlite3FileSuffix3(zMainFile, zMaster);
 63363       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
 63364     }while( rc==SQLITE_OK && res );
 63365     if( rc==SQLITE_OK ){
 63366       /* Open the master journal. */
 63367       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
 63368           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
 63369           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
 63370       );
 63372     if( rc!=SQLITE_OK ){
 63373       sqlite3DbFree(db, zMaster);
 63374       return rc;
 63377     /* Write the name of each database file in the transaction into the new
 63378     ** master journal file. If an error occurs at this point close
 63379     ** and delete the master journal file. All the individual journal files
 63380     ** still have 'null' as the master journal pointer, so they will roll
 63381     ** back independently if a failure occurs.
 63382     */
 63383     for(i=0; i<db->nDb; i++){
 63384       Btree *pBt = db->aDb[i].pBt;
 63385       if( sqlite3BtreeIsInTrans(pBt) ){
 63386         char const *zFile = sqlite3BtreeGetJournalname(pBt);
 63387         if( zFile==0 ){
 63388           continue;  /* Ignore TEMP and :memory: databases */
 63390         assert( zFile[0]!=0 );
 63391         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
 63392           needSync = 1;
 63394         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
 63395         offset += sqlite3Strlen30(zFile)+1;
 63396         if( rc!=SQLITE_OK ){
 63397           sqlite3OsCloseFree(pMaster);
 63398           sqlite3OsDelete(pVfs, zMaster, 0);
 63399           sqlite3DbFree(db, zMaster);
 63400           return rc;
 63405     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
 63406     ** flag is set this is not required.
 63407     */
 63408     if( needSync 
 63409      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
 63410      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
 63411     ){
 63412       sqlite3OsCloseFree(pMaster);
 63413       sqlite3OsDelete(pVfs, zMaster, 0);
 63414       sqlite3DbFree(db, zMaster);
 63415       return rc;
 63418     /* Sync all the db files involved in the transaction. The same call
 63419     ** sets the master journal pointer in each individual journal. If
 63420     ** an error occurs here, do not delete the master journal file.
 63421     **
 63422     ** If the error occurs during the first call to
 63423     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
 63424     ** master journal file will be orphaned. But we cannot delete it,
 63425     ** in case the master journal file name was written into the journal
 63426     ** file before the failure occurred.
 63427     */
 63428     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
 63429       Btree *pBt = db->aDb[i].pBt;
 63430       if( pBt ){
 63431         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
 63434     sqlite3OsCloseFree(pMaster);
 63435     assert( rc!=SQLITE_BUSY );
 63436     if( rc!=SQLITE_OK ){
 63437       sqlite3DbFree(db, zMaster);
 63438       return rc;
 63441     /* Delete the master journal file. This commits the transaction. After
 63442     ** doing this the directory is synced again before any individual
 63443     ** transaction files are deleted.
 63444     */
 63445     rc = sqlite3OsDelete(pVfs, zMaster, 1);
 63446     sqlite3DbFree(db, zMaster);
 63447     zMaster = 0;
 63448     if( rc ){
 63449       return rc;
 63452     /* All files and directories have already been synced, so the following
 63453     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
 63454     ** deleting or truncating journals. If something goes wrong while
 63455     ** this is happening we don't really care. The integrity of the
 63456     ** transaction is already guaranteed, but some stray 'cold' journals
 63457     ** may be lying around. Returning an error code won't help matters.
 63458     */
 63459     disable_simulated_io_errors();
 63460     sqlite3BeginBenignMalloc();
 63461     for(i=0; i<db->nDb; i++){ 
 63462       Btree *pBt = db->aDb[i].pBt;
 63463       if( pBt ){
 63464         sqlite3BtreeCommitPhaseTwo(pBt, 1);
 63467     sqlite3EndBenignMalloc();
 63468     enable_simulated_io_errors();
 63470     sqlite3VtabCommit(db);
 63472 #endif
 63474   return rc;
 63477 /* 
 63478 ** This routine checks that the sqlite3.nVdbeActive count variable
 63479 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
 63480 ** currently active. An assertion fails if the two counts do not match.
 63481 ** This is an internal self-check only - it is not an essential processing
 63482 ** step.
 63483 **
 63484 ** This is a no-op if NDEBUG is defined.
 63485 */
 63486 #ifndef NDEBUG
 63487 static void checkActiveVdbeCnt(sqlite3 *db){
 63488   Vdbe *p;
 63489   int cnt = 0;
 63490   int nWrite = 0;
 63491   int nRead = 0;
 63492   p = db->pVdbe;
 63493   while( p ){
 63494     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
 63495       cnt++;
 63496       if( p->readOnly==0 ) nWrite++;
 63497       if( p->bIsReader ) nRead++;
 63499     p = p->pNext;
 63501   assert( cnt==db->nVdbeActive );
 63502   assert( nWrite==db->nVdbeWrite );
 63503   assert( nRead==db->nVdbeRead );
 63505 #else
 63506 #define checkActiveVdbeCnt(x)
 63507 #endif
 63509 /*
 63510 ** If the Vdbe passed as the first argument opened a statement-transaction,
 63511 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
 63512 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
 63513 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
 63514 ** statement transaction is committed.
 63515 **
 63516 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
 63517 ** Otherwise SQLITE_OK.
 63518 */
 63519 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
 63520   sqlite3 *const db = p->db;
 63521   int rc = SQLITE_OK;
 63523   /* If p->iStatement is greater than zero, then this Vdbe opened a 
 63524   ** statement transaction that should be closed here. The only exception
 63525   ** is that an IO error may have occurred, causing an emergency rollback.
 63526   ** In this case (db->nStatement==0), and there is nothing to do.
 63527   */
 63528   if( db->nStatement && p->iStatement ){
 63529     int i;
 63530     const int iSavepoint = p->iStatement-1;
 63532     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
 63533     assert( db->nStatement>0 );
 63534     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
 63536     for(i=0; i<db->nDb; i++){ 
 63537       int rc2 = SQLITE_OK;
 63538       Btree *pBt = db->aDb[i].pBt;
 63539       if( pBt ){
 63540         if( eOp==SAVEPOINT_ROLLBACK ){
 63541           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
 63543         if( rc2==SQLITE_OK ){
 63544           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
 63546         if( rc==SQLITE_OK ){
 63547           rc = rc2;
 63551     db->nStatement--;
 63552     p->iStatement = 0;
 63554     if( rc==SQLITE_OK ){
 63555       if( eOp==SAVEPOINT_ROLLBACK ){
 63556         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
 63558       if( rc==SQLITE_OK ){
 63559         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
 63563     /* If the statement transaction is being rolled back, also restore the 
 63564     ** database handles deferred constraint counter to the value it had when 
 63565     ** the statement transaction was opened.  */
 63566     if( eOp==SAVEPOINT_ROLLBACK ){
 63567       db->nDeferredCons = p->nStmtDefCons;
 63568       db->nDeferredImmCons = p->nStmtDefImmCons;
 63571   return rc;
 63574 /*
 63575 ** This function is called when a transaction opened by the database 
 63576 ** handle associated with the VM passed as an argument is about to be 
 63577 ** committed. If there are outstanding deferred foreign key constraint
 63578 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 63579 **
 63580 ** If there are outstanding FK violations and this function returns 
 63581 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
 63582 ** and write an error message to it. Then return SQLITE_ERROR.
 63583 */
 63584 #ifndef SQLITE_OMIT_FOREIGN_KEY
 63585 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
 63586   sqlite3 *db = p->db;
 63587   if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) 
 63588    || (!deferred && p->nFkConstraint>0) 
 63589   ){
 63590     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
 63591     p->errorAction = OE_Abort;
 63592     sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
 63593     return SQLITE_ERROR;
 63595   return SQLITE_OK;
 63597 #endif
 63599 /*
 63600 ** This routine is called the when a VDBE tries to halt.  If the VDBE
 63601 ** has made changes and is in autocommit mode, then commit those
 63602 ** changes.  If a rollback is needed, then do the rollback.
 63603 **
 63604 ** This routine is the only way to move the state of a VM from
 63605 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
 63606 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
 63607 **
 63608 ** Return an error code.  If the commit could not complete because of
 63609 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
 63610 ** means the close did not happen and needs to be repeated.
 63611 */
 63612 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
 63613   int rc;                         /* Used to store transient return codes */
 63614   sqlite3 *db = p->db;
 63616   /* This function contains the logic that determines if a statement or
 63617   ** transaction will be committed or rolled back as a result of the
 63618   ** execution of this virtual machine. 
 63619   **
 63620   ** If any of the following errors occur:
 63621   **
 63622   **     SQLITE_NOMEM
 63623   **     SQLITE_IOERR
 63624   **     SQLITE_FULL
 63625   **     SQLITE_INTERRUPT
 63626   **
 63627   ** Then the internal cache might have been left in an inconsistent
 63628   ** state.  We need to rollback the statement transaction, if there is
 63629   ** one, or the complete transaction if there is no statement transaction.
 63630   */
 63632   if( p->db->mallocFailed ){
 63633     p->rc = SQLITE_NOMEM;
 63635   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
 63636   closeAllCursors(p);
 63637   if( p->magic!=VDBE_MAGIC_RUN ){
 63638     return SQLITE_OK;
 63640   checkActiveVdbeCnt(db);
 63642   /* No commit or rollback needed if the program never started or if the
 63643   ** SQL statement does not read or write a database file.  */
 63644   if( p->pc>=0 && p->bIsReader ){
 63645     int mrc;   /* Primary error code from p->rc */
 63646     int eStatementOp = 0;
 63647     int isSpecialError;            /* Set to true if a 'special' error */
 63649     /* Lock all btrees used by the statement */
 63650     sqlite3VdbeEnter(p);
 63652     /* Check for one of the special errors */
 63653     mrc = p->rc & 0xff;
 63654     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
 63655     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
 63656                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
 63657     if( isSpecialError ){
 63658       /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
 63659       ** no rollback is necessary. Otherwise, at least a savepoint 
 63660       ** transaction must be rolled back to restore the database to a 
 63661       ** consistent state.
 63662       **
 63663       ** Even if the statement is read-only, it is important to perform
 63664       ** a statement or transaction rollback operation. If the error 
 63665       ** occurred while writing to the journal, sub-journal or database
 63666       ** file as part of an effort to free up cache space (see function
 63667       ** pagerStress() in pager.c), the rollback is required to restore 
 63668       ** the pager to a consistent state.
 63669       */
 63670       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
 63671         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
 63672           eStatementOp = SAVEPOINT_ROLLBACK;
 63673         }else{
 63674           /* We are forced to roll back the active transaction. Before doing
 63675           ** so, abort any other statements this handle currently has active.
 63676           */
 63677           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 63678           sqlite3CloseSavepoints(db);
 63679           db->autoCommit = 1;
 63684     /* Check for immediate foreign key violations. */
 63685     if( p->rc==SQLITE_OK ){
 63686       sqlite3VdbeCheckFk(p, 0);
 63689     /* If the auto-commit flag is set and this is the only active writer 
 63690     ** VM, then we do either a commit or rollback of the current transaction. 
 63691     **
 63692     ** Note: This block also runs if one of the special errors handled 
 63693     ** above has occurred. 
 63694     */
 63695     if( !sqlite3VtabInSync(db) 
 63696      && db->autoCommit 
 63697      && db->nVdbeWrite==(p->readOnly==0) 
 63698     ){
 63699       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
 63700         rc = sqlite3VdbeCheckFk(p, 1);
 63701         if( rc!=SQLITE_OK ){
 63702           if( NEVER(p->readOnly) ){
 63703             sqlite3VdbeLeave(p);
 63704             return SQLITE_ERROR;
 63706           rc = SQLITE_CONSTRAINT_FOREIGNKEY;
 63707         }else{ 
 63708           /* The auto-commit flag is true, the vdbe program was successful 
 63709           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
 63710           ** key constraints to hold up the transaction. This means a commit 
 63711           ** is required. */
 63712           rc = vdbeCommit(db, p);
 63714         if( rc==SQLITE_BUSY && p->readOnly ){
 63715           sqlite3VdbeLeave(p);
 63716           return SQLITE_BUSY;
 63717         }else if( rc!=SQLITE_OK ){
 63718           p->rc = rc;
 63719           sqlite3RollbackAll(db, SQLITE_OK);
 63720         }else{
 63721           db->nDeferredCons = 0;
 63722           db->nDeferredImmCons = 0;
 63723           db->flags &= ~SQLITE_DeferFKs;
 63724           sqlite3CommitInternalChanges(db);
 63726       }else{
 63727         sqlite3RollbackAll(db, SQLITE_OK);
 63729       db->nStatement = 0;
 63730     }else if( eStatementOp==0 ){
 63731       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
 63732         eStatementOp = SAVEPOINT_RELEASE;
 63733       }else if( p->errorAction==OE_Abort ){
 63734         eStatementOp = SAVEPOINT_ROLLBACK;
 63735       }else{
 63736         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 63737         sqlite3CloseSavepoints(db);
 63738         db->autoCommit = 1;
 63742     /* If eStatementOp is non-zero, then a statement transaction needs to
 63743     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
 63744     ** do so. If this operation returns an error, and the current statement
 63745     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
 63746     ** current statement error code.
 63747     */
 63748     if( eStatementOp ){
 63749       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
 63750       if( rc ){
 63751         if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
 63752           p->rc = rc;
 63753           sqlite3DbFree(db, p->zErrMsg);
 63754           p->zErrMsg = 0;
 63756         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 63757         sqlite3CloseSavepoints(db);
 63758         db->autoCommit = 1;
 63762     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
 63763     ** has been rolled back, update the database connection change-counter. 
 63764     */
 63765     if( p->changeCntOn ){
 63766       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
 63767         sqlite3VdbeSetChanges(db, p->nChange);
 63768       }else{
 63769         sqlite3VdbeSetChanges(db, 0);
 63771       p->nChange = 0;
 63774     /* Release the locks */
 63775     sqlite3VdbeLeave(p);
 63778   /* We have successfully halted and closed the VM.  Record this fact. */
 63779   if( p->pc>=0 ){
 63780     db->nVdbeActive--;
 63781     if( !p->readOnly ) db->nVdbeWrite--;
 63782     if( p->bIsReader ) db->nVdbeRead--;
 63783     assert( db->nVdbeActive>=db->nVdbeRead );
 63784     assert( db->nVdbeRead>=db->nVdbeWrite );
 63785     assert( db->nVdbeWrite>=0 );
 63787   p->magic = VDBE_MAGIC_HALT;
 63788   checkActiveVdbeCnt(db);
 63789   if( p->db->mallocFailed ){
 63790     p->rc = SQLITE_NOMEM;
 63793   /* If the auto-commit flag is set to true, then any locks that were held
 63794   ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
 63795   ** to invoke any required unlock-notify callbacks.
 63796   */
 63797   if( db->autoCommit ){
 63798     sqlite3ConnectionUnlocked(db);
 63801   assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
 63802   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
 63806 /*
 63807 ** Each VDBE holds the result of the most recent sqlite3_step() call
 63808 ** in p->rc.  This routine sets that result back to SQLITE_OK.
 63809 */
 63810 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
 63811   p->rc = SQLITE_OK;
 63814 /*
 63815 ** Copy the error code and error message belonging to the VDBE passed
 63816 ** as the first argument to its database handle (so that they will be 
 63817 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
 63818 **
 63819 ** This function does not clear the VDBE error code or message, just
 63820 ** copies them to the database handle.
 63821 */
 63822 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
 63823   sqlite3 *db = p->db;
 63824   int rc = p->rc;
 63825   if( p->zErrMsg ){
 63826     u8 mallocFailed = db->mallocFailed;
 63827     sqlite3BeginBenignMalloc();
 63828     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
 63829     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
 63830     sqlite3EndBenignMalloc();
 63831     db->mallocFailed = mallocFailed;
 63832     db->errCode = rc;
 63833   }else{
 63834     sqlite3Error(db, rc, 0);
 63836   return rc;
 63839 #ifdef SQLITE_ENABLE_SQLLOG
 63840 /*
 63841 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 
 63842 ** invoke it.
 63843 */
 63844 static void vdbeInvokeSqllog(Vdbe *v){
 63845   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
 63846     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
 63847     assert( v->db->init.busy==0 );
 63848     if( zExpanded ){
 63849       sqlite3GlobalConfig.xSqllog(
 63850           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
 63851       );
 63852       sqlite3DbFree(v->db, zExpanded);
 63856 #else
 63857 # define vdbeInvokeSqllog(x)
 63858 #endif
 63860 /*
 63861 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
 63862 ** Write any error messages into *pzErrMsg.  Return the result code.
 63863 **
 63864 ** After this routine is run, the VDBE should be ready to be executed
 63865 ** again.
 63866 **
 63867 ** To look at it another way, this routine resets the state of the
 63868 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
 63869 ** VDBE_MAGIC_INIT.
 63870 */
 63871 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
 63872   sqlite3 *db;
 63873   db = p->db;
 63875   /* If the VM did not run to completion or if it encountered an
 63876   ** error, then it might not have been halted properly.  So halt
 63877   ** it now.
 63878   */
 63879   sqlite3VdbeHalt(p);
 63881   /* If the VDBE has be run even partially, then transfer the error code
 63882   ** and error message from the VDBE into the main database structure.  But
 63883   ** if the VDBE has just been set to run but has not actually executed any
 63884   ** instructions yet, leave the main database error information unchanged.
 63885   */
 63886   if( p->pc>=0 ){
 63887     vdbeInvokeSqllog(p);
 63888     sqlite3VdbeTransferError(p);
 63889     sqlite3DbFree(db, p->zErrMsg);
 63890     p->zErrMsg = 0;
 63891     if( p->runOnlyOnce ) p->expired = 1;
 63892   }else if( p->rc && p->expired ){
 63893     /* The expired flag was set on the VDBE before the first call
 63894     ** to sqlite3_step(). For consistency (since sqlite3_step() was
 63895     ** called), set the database error in this case as well.
 63896     */
 63897     sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
 63898     sqlite3DbFree(db, p->zErrMsg);
 63899     p->zErrMsg = 0;
 63902   /* Reclaim all memory used by the VDBE
 63903   */
 63904   Cleanup(p);
 63906   /* Save profiling information from this VDBE run.
 63907   */
 63908 #ifdef VDBE_PROFILE
 63910     FILE *out = fopen("vdbe_profile.out", "a");
 63911     if( out ){
 63912       int i;
 63913       fprintf(out, "---- ");
 63914       for(i=0; i<p->nOp; i++){
 63915         fprintf(out, "%02x", p->aOp[i].opcode);
 63917       fprintf(out, "\n");
 63918       if( p->zSql ){
 63919         char c, pc = 0;
 63920         fprintf(out, "-- ");
 63921         for(i=0; (c = p->zSql[i])!=0; i++){
 63922           if( pc=='\n' ) fprintf(out, "-- ");
 63923           putc(c, out);
 63924           pc = c;
 63926         if( pc!='\n' ) fprintf(out, "\n");
 63928       for(i=0; i<p->nOp; i++){
 63929         char zHdr[100];
 63930         sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
 63931            p->aOp[i].cnt,
 63932            p->aOp[i].cycles,
 63933            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
 63934         );
 63935         fprintf(out, "%s", zHdr);
 63936         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
 63938       fclose(out);
 63941 #endif
 63942   p->iCurrentTime = 0;
 63943   p->magic = VDBE_MAGIC_INIT;
 63944   return p->rc & db->errMask;
 63947 /*
 63948 ** Clean up and delete a VDBE after execution.  Return an integer which is
 63949 ** the result code.  Write any error message text into *pzErrMsg.
 63950 */
 63951 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
 63952   int rc = SQLITE_OK;
 63953   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
 63954     rc = sqlite3VdbeReset(p);
 63955     assert( (rc & p->db->errMask)==rc );
 63957   sqlite3VdbeDelete(p);
 63958   return rc;
 63961 /*
 63962 ** If parameter iOp is less than zero, then invoke the destructor for
 63963 ** all auxiliary data pointers currently cached by the VM passed as
 63964 ** the first argument.
 63965 **
 63966 ** Or, if iOp is greater than or equal to zero, then the destructor is
 63967 ** only invoked for those auxiliary data pointers created by the user 
 63968 ** function invoked by the OP_Function opcode at instruction iOp of 
 63969 ** VM pVdbe, and only then if:
 63970 **
 63971 **    * the associated function parameter is the 32nd or later (counting
 63972 **      from left to right), or
 63973 **
 63974 **    * the corresponding bit in argument mask is clear (where the first
 63975 **      function parameter corrsponds to bit 0 etc.).
 63976 */
 63977 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
 63978   AuxData **pp = &pVdbe->pAuxData;
 63979   while( *pp ){
 63980     AuxData *pAux = *pp;
 63981     if( (iOp<0)
 63982      || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
 63983     ){
 63984       testcase( pAux->iArg==31 );
 63985       if( pAux->xDelete ){
 63986         pAux->xDelete(pAux->pAux);
 63988       *pp = pAux->pNext;
 63989       sqlite3DbFree(pVdbe->db, pAux);
 63990     }else{
 63991       pp= &pAux->pNext;
 63996 /*
 63997 ** Free all memory associated with the Vdbe passed as the second argument,
 63998 ** except for object itself, which is preserved.
 63999 **
 64000 ** The difference between this function and sqlite3VdbeDelete() is that
 64001 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
 64002 ** the database connection and frees the object itself.
 64003 */
 64004 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
 64005   SubProgram *pSub, *pNext;
 64006   int i;
 64007   assert( p->db==0 || p->db==db );
 64008   releaseMemArray(p->aVar, p->nVar);
 64009   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
 64010   for(pSub=p->pProgram; pSub; pSub=pNext){
 64011     pNext = pSub->pNext;
 64012     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
 64013     sqlite3DbFree(db, pSub);
 64015   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
 64016   vdbeFreeOpArray(db, p->aOp, p->nOp);
 64017   sqlite3DbFree(db, p->aColName);
 64018   sqlite3DbFree(db, p->zSql);
 64019   sqlite3DbFree(db, p->pFree);
 64020 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 64021   sqlite3DbFree(db, p->zExplain);
 64022   sqlite3DbFree(db, p->pExplain);
 64023 #endif
 64026 /*
 64027 ** Delete an entire VDBE.
 64028 */
 64029 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
 64030   sqlite3 *db;
 64032   if( NEVER(p==0) ) return;
 64033   db = p->db;
 64034   assert( sqlite3_mutex_held(db->mutex) );
 64035   sqlite3VdbeClearObject(db, p);
 64036   if( p->pPrev ){
 64037     p->pPrev->pNext = p->pNext;
 64038   }else{
 64039     assert( db->pVdbe==p );
 64040     db->pVdbe = p->pNext;
 64042   if( p->pNext ){
 64043     p->pNext->pPrev = p->pPrev;
 64045   p->magic = VDBE_MAGIC_DEAD;
 64046   p->db = 0;
 64047   sqlite3DbFree(db, p);
 64050 /*
 64051 ** Make sure the cursor p is ready to read or write the row to which it
 64052 ** was last positioned.  Return an error code if an OOM fault or I/O error
 64053 ** prevents us from positioning the cursor to its correct position.
 64054 **
 64055 ** If a MoveTo operation is pending on the given cursor, then do that
 64056 ** MoveTo now.  If no move is pending, check to see if the row has been
 64057 ** deleted out from under the cursor and if it has, mark the row as
 64058 ** a NULL row.
 64059 **
 64060 ** If the cursor is already pointing to the correct row and that row has
 64061 ** not been deleted out from under the cursor, then this routine is a no-op.
 64062 */
 64063 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
 64064   if( p->deferredMoveto ){
 64065     int res, rc;
 64066 #ifdef SQLITE_TEST
 64067     extern int sqlite3_search_count;
 64068 #endif
 64069     assert( p->isTable );
 64070     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
 64071     if( rc ) return rc;
 64072     p->lastRowid = p->movetoTarget;
 64073     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
 64074     p->rowidIsValid = 1;
 64075 #ifdef SQLITE_TEST
 64076     sqlite3_search_count++;
 64077 #endif
 64078     p->deferredMoveto = 0;
 64079     p->cacheStatus = CACHE_STALE;
 64080   }else if( p->pCursor ){
 64081     int hasMoved;
 64082     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
 64083     if( rc ) return rc;
 64084     if( hasMoved ){
 64085       p->cacheStatus = CACHE_STALE;
 64086       p->nullRow = 1;
 64089   return SQLITE_OK;
 64092 /*
 64093 ** The following functions:
 64094 **
 64095 ** sqlite3VdbeSerialType()
 64096 ** sqlite3VdbeSerialTypeLen()
 64097 ** sqlite3VdbeSerialLen()
 64098 ** sqlite3VdbeSerialPut()
 64099 ** sqlite3VdbeSerialGet()
 64100 **
 64101 ** encapsulate the code that serializes values for storage in SQLite
 64102 ** data and index records. Each serialized value consists of a
 64103 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
 64104 ** integer, stored as a varint.
 64105 **
 64106 ** In an SQLite index record, the serial type is stored directly before
 64107 ** the blob of data that it corresponds to. In a table record, all serial
 64108 ** types are stored at the start of the record, and the blobs of data at
 64109 ** the end. Hence these functions allow the caller to handle the
 64110 ** serial-type and data blob separately.
 64111 **
 64112 ** The following table describes the various storage classes for data:
 64113 **
 64114 **   serial type        bytes of data      type
 64115 **   --------------     ---------------    ---------------
 64116 **      0                     0            NULL
 64117 **      1                     1            signed integer
 64118 **      2                     2            signed integer
 64119 **      3                     3            signed integer
 64120 **      4                     4            signed integer
 64121 **      5                     6            signed integer
 64122 **      6                     8            signed integer
 64123 **      7                     8            IEEE float
 64124 **      8                     0            Integer constant 0
 64125 **      9                     0            Integer constant 1
 64126 **     10,11                               reserved for expansion
 64127 **    N>=12 and even       (N-12)/2        BLOB
 64128 **    N>=13 and odd        (N-13)/2        text
 64129 **
 64130 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
 64131 ** of SQLite will not understand those serial types.
 64132 */
 64134 /*
 64135 ** Return the serial-type for the value stored in pMem.
 64136 */
 64137 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
 64138   int flags = pMem->flags;
 64139   int n;
 64141   if( flags&MEM_Null ){
 64142     return 0;
 64144   if( flags&MEM_Int ){
 64145     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
 64146 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
 64147     i64 i = pMem->u.i;
 64148     u64 u;
 64149     if( i<0 ){
 64150       if( i<(-MAX_6BYTE) ) return 6;
 64151       /* Previous test prevents:  u = -(-9223372036854775808) */
 64152       u = -i;
 64153     }else{
 64154       u = i;
 64156     if( u<=127 ){
 64157       return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
 64159     if( u<=32767 ) return 2;
 64160     if( u<=8388607 ) return 3;
 64161     if( u<=2147483647 ) return 4;
 64162     if( u<=MAX_6BYTE ) return 5;
 64163     return 6;
 64165   if( flags&MEM_Real ){
 64166     return 7;
 64168   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
 64169   n = pMem->n;
 64170   if( flags & MEM_Zero ){
 64171     n += pMem->u.nZero;
 64173   assert( n>=0 );
 64174   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
 64177 /*
 64178 ** Return the length of the data corresponding to the supplied serial-type.
 64179 */
 64180 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
 64181   if( serial_type>=12 ){
 64182     return (serial_type-12)/2;
 64183   }else{
 64184     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
 64185     return aSize[serial_type];
 64189 /*
 64190 ** If we are on an architecture with mixed-endian floating 
 64191 ** points (ex: ARM7) then swap the lower 4 bytes with the 
 64192 ** upper 4 bytes.  Return the result.
 64193 **
 64194 ** For most architectures, this is a no-op.
 64195 **
 64196 ** (later):  It is reported to me that the mixed-endian problem
 64197 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
 64198 ** that early versions of GCC stored the two words of a 64-bit
 64199 ** float in the wrong order.  And that error has been propagated
 64200 ** ever since.  The blame is not necessarily with GCC, though.
 64201 ** GCC might have just copying the problem from a prior compiler.
 64202 ** I am also told that newer versions of GCC that follow a different
 64203 ** ABI get the byte order right.
 64204 **
 64205 ** Developers using SQLite on an ARM7 should compile and run their
 64206 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
 64207 ** enabled, some asserts below will ensure that the byte order of
 64208 ** floating point values is correct.
 64209 **
 64210 ** (2007-08-30)  Frank van Vugt has studied this problem closely
 64211 ** and has send his findings to the SQLite developers.  Frank
 64212 ** writes that some Linux kernels offer floating point hardware
 64213 ** emulation that uses only 32-bit mantissas instead of a full 
 64214 ** 48-bits as required by the IEEE standard.  (This is the
 64215 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
 64216 ** byte swapping becomes very complicated.  To avoid problems,
 64217 ** the necessary byte swapping is carried out using a 64-bit integer
 64218 ** rather than a 64-bit float.  Frank assures us that the code here
 64219 ** works for him.  We, the developers, have no way to independently
 64220 ** verify this, but Frank seems to know what he is talking about
 64221 ** so we trust him.
 64222 */
 64223 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 64224 static u64 floatSwap(u64 in){
 64225   union {
 64226     u64 r;
 64227     u32 i[2];
 64228   } u;
 64229   u32 t;
 64231   u.r = in;
 64232   t = u.i[0];
 64233   u.i[0] = u.i[1];
 64234   u.i[1] = t;
 64235   return u.r;
 64237 # define swapMixedEndianFloat(X)  X = floatSwap(X)
 64238 #else
 64239 # define swapMixedEndianFloat(X)
 64240 #endif
 64242 /*
 64243 ** Write the serialized data blob for the value stored in pMem into 
 64244 ** buf. It is assumed that the caller has allocated sufficient space.
 64245 ** Return the number of bytes written.
 64246 **
 64247 ** nBuf is the amount of space left in buf[].  The caller is responsible
 64248 ** for allocating enough space to buf[] to hold the entire field, exclusive
 64249 ** of the pMem->u.nZero bytes for a MEM_Zero value.
 64250 **
 64251 ** Return the number of bytes actually written into buf[].  The number
 64252 ** of bytes in the zero-filled tail is included in the return value only
 64253 ** if those bytes were zeroed in buf[].
 64254 */ 
 64255 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
 64256   u32 len;
 64258   /* Integer and Real */
 64259   if( serial_type<=7 && serial_type>0 ){
 64260     u64 v;
 64261     u32 i;
 64262     if( serial_type==7 ){
 64263       assert( sizeof(v)==sizeof(pMem->r) );
 64264       memcpy(&v, &pMem->r, sizeof(v));
 64265       swapMixedEndianFloat(v);
 64266     }else{
 64267       v = pMem->u.i;
 64269     len = i = sqlite3VdbeSerialTypeLen(serial_type);
 64270     while( i-- ){
 64271       buf[i] = (u8)(v&0xFF);
 64272       v >>= 8;
 64274     return len;
 64277   /* String or blob */
 64278   if( serial_type>=12 ){
 64279     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
 64280              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
 64281     len = pMem->n;
 64282     memcpy(buf, pMem->z, len);
 64283     return len;
 64286   /* NULL or constants 0 or 1 */
 64287   return 0;
 64290 /* Input "x" is a sequence of unsigned characters that represent a
 64291 ** big-endian integer.  Return the equivalent native integer
 64292 */
 64293 #define ONE_BYTE_INT(x)    ((i8)(x)[0])
 64294 #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
 64295 #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
 64296 #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
 64298 /*
 64299 ** Deserialize the data blob pointed to by buf as serial type serial_type
 64300 ** and store the result in pMem.  Return the number of bytes read.
 64301 */ 
 64302 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
 64303   const unsigned char *buf,     /* Buffer to deserialize from */
 64304   u32 serial_type,              /* Serial type to deserialize */
 64305   Mem *pMem                     /* Memory cell to write value into */
 64306 ){
 64307   u64 x;
 64308   u32 y;
 64309   switch( serial_type ){
 64310     case 10:   /* Reserved for future use */
 64311     case 11:   /* Reserved for future use */
 64312     case 0: {  /* NULL */
 64313       pMem->flags = MEM_Null;
 64314       break;
 64316     case 1: { /* 1-byte signed integer */
 64317       pMem->u.i = ONE_BYTE_INT(buf);
 64318       pMem->flags = MEM_Int;
 64319       testcase( pMem->u.i<0 );
 64320       return 1;
 64322     case 2: { /* 2-byte signed integer */
 64323       pMem->u.i = TWO_BYTE_INT(buf);
 64324       pMem->flags = MEM_Int;
 64325       testcase( pMem->u.i<0 );
 64326       return 2;
 64328     case 3: { /* 3-byte signed integer */
 64329       pMem->u.i = THREE_BYTE_INT(buf);
 64330       pMem->flags = MEM_Int;
 64331       testcase( pMem->u.i<0 );
 64332       return 3;
 64334     case 4: { /* 4-byte signed integer */
 64335       y = FOUR_BYTE_UINT(buf);
 64336       pMem->u.i = (i64)*(int*)&y;
 64337       pMem->flags = MEM_Int;
 64338       testcase( pMem->u.i<0 );
 64339       return 4;
 64341     case 5: { /* 6-byte signed integer */
 64342       pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
 64343       pMem->flags = MEM_Int;
 64344       testcase( pMem->u.i<0 );
 64345       return 6;
 64347     case 6:   /* 8-byte signed integer */
 64348     case 7: { /* IEEE floating point */
 64349 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
 64350       /* Verify that integers and floating point values use the same
 64351       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
 64352       ** defined that 64-bit floating point values really are mixed
 64353       ** endian.
 64354       */
 64355       static const u64 t1 = ((u64)0x3ff00000)<<32;
 64356       static const double r1 = 1.0;
 64357       u64 t2 = t1;
 64358       swapMixedEndianFloat(t2);
 64359       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
 64360 #endif
 64361       x = FOUR_BYTE_UINT(buf);
 64362       y = FOUR_BYTE_UINT(buf+4);
 64363       x = (x<<32) | y;
 64364       if( serial_type==6 ){
 64365         pMem->u.i = *(i64*)&x;
 64366         pMem->flags = MEM_Int;
 64367         testcase( pMem->u.i<0 );
 64368       }else{
 64369         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
 64370         swapMixedEndianFloat(x);
 64371         memcpy(&pMem->r, &x, sizeof(x));
 64372         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
 64374       return 8;
 64376     case 8:    /* Integer 0 */
 64377     case 9: {  /* Integer 1 */
 64378       pMem->u.i = serial_type-8;
 64379       pMem->flags = MEM_Int;
 64380       return 0;
 64382     default: {
 64383       static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
 64384       u32 len = (serial_type-12)/2;
 64385       pMem->z = (char *)buf;
 64386       pMem->n = len;
 64387       pMem->xDel = 0;
 64388       pMem->flags = aFlag[serial_type&1];
 64389       return len;
 64392   return 0;
 64395 /*
 64396 ** This routine is used to allocate sufficient space for an UnpackedRecord
 64397 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
 64398 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
 64399 **
 64400 ** The space is either allocated using sqlite3DbMallocRaw() or from within
 64401 ** the unaligned buffer passed via the second and third arguments (presumably
 64402 ** stack space). If the former, then *ppFree is set to a pointer that should
 64403 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
 64404 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
 64405 ** before returning.
 64406 **
 64407 ** If an OOM error occurs, NULL is returned.
 64408 */
 64409 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
 64410   KeyInfo *pKeyInfo,              /* Description of the record */
 64411   char *pSpace,                   /* Unaligned space available */
 64412   int szSpace,                    /* Size of pSpace[] in bytes */
 64413   char **ppFree                   /* OUT: Caller should free this pointer */
 64414 ){
 64415   UnpackedRecord *p;              /* Unpacked record to return */
 64416   int nOff;                       /* Increment pSpace by nOff to align it */
 64417   int nByte;                      /* Number of bytes required for *p */
 64419   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
 64420   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
 64421   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
 64422   */
 64423   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
 64424   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
 64425   if( nByte>szSpace+nOff ){
 64426     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
 64427     *ppFree = (char *)p;
 64428     if( !p ) return 0;
 64429   }else{
 64430     p = (UnpackedRecord*)&pSpace[nOff];
 64431     *ppFree = 0;
 64434   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
 64435   assert( pKeyInfo->aSortOrder!=0 );
 64436   p->pKeyInfo = pKeyInfo;
 64437   p->nField = pKeyInfo->nField + 1;
 64438   return p;
 64441 /*
 64442 ** Given the nKey-byte encoding of a record in pKey[], populate the 
 64443 ** UnpackedRecord structure indicated by the fourth argument with the
 64444 ** contents of the decoded record.
 64445 */ 
 64446 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
 64447   KeyInfo *pKeyInfo,     /* Information about the record format */
 64448   int nKey,              /* Size of the binary record */
 64449   const void *pKey,      /* The binary record */
 64450   UnpackedRecord *p      /* Populate this structure before returning. */
 64451 ){
 64452   const unsigned char *aKey = (const unsigned char *)pKey;
 64453   int d; 
 64454   u32 idx;                        /* Offset in aKey[] to read from */
 64455   u16 u;                          /* Unsigned loop counter */
 64456   u32 szHdr;
 64457   Mem *pMem = p->aMem;
 64459   p->default_rc = 0;
 64460   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 64461   idx = getVarint32(aKey, szHdr);
 64462   d = szHdr;
 64463   u = 0;
 64464   while( idx<szHdr && u<p->nField && d<=nKey ){
 64465     u32 serial_type;
 64467     idx += getVarint32(&aKey[idx], serial_type);
 64468     pMem->enc = pKeyInfo->enc;
 64469     pMem->db = pKeyInfo->db;
 64470     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
 64471     pMem->zMalloc = 0;
 64472     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 64473     pMem++;
 64474     u++;
 64476   assert( u<=pKeyInfo->nField + 1 );
 64477   p->nField = u;
 64480 #if SQLITE_DEBUG
 64481 /*
 64482 ** This function compares two index or table record keys in the same way
 64483 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
 64484 ** this function deserializes and compares values using the
 64485 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
 64486 ** in assert() statements to ensure that the optimized code in
 64487 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
 64488 */
 64489 static int vdbeRecordCompareDebug(
 64490   int nKey1, const void *pKey1, /* Left key */
 64491   const UnpackedRecord *pPKey2  /* Right key */
 64492 ){
 64493   u32 d1;            /* Offset into aKey[] of next data element */
 64494   u32 idx1;          /* Offset into aKey[] of next header element */
 64495   u32 szHdr1;        /* Number of bytes in header */
 64496   int i = 0;
 64497   int rc = 0;
 64498   const unsigned char *aKey1 = (const unsigned char *)pKey1;
 64499   KeyInfo *pKeyInfo;
 64500   Mem mem1;
 64502   pKeyInfo = pPKey2->pKeyInfo;
 64503   mem1.enc = pKeyInfo->enc;
 64504   mem1.db = pKeyInfo->db;
 64505   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
 64506   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
 64508   /* Compilers may complain that mem1.u.i is potentially uninitialized.
 64509   ** We could initialize it, as shown here, to silence those complaints.
 64510   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
 64511   ** the unnecessary initialization has a measurable negative performance
 64512   ** impact, since this routine is a very high runner.  And so, we choose
 64513   ** to ignore the compiler warnings and leave this variable uninitialized.
 64514   */
 64515   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
 64517   idx1 = getVarint32(aKey1, szHdr1);
 64518   d1 = szHdr1;
 64519   assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
 64520   assert( pKeyInfo->aSortOrder!=0 );
 64521   assert( pKeyInfo->nField>0 );
 64522   assert( idx1<=szHdr1 || CORRUPT_DB );
 64523   do{
 64524     u32 serial_type1;
 64526     /* Read the serial types for the next element in each key. */
 64527     idx1 += getVarint32( aKey1+idx1, serial_type1 );
 64529     /* Verify that there is enough key space remaining to avoid
 64530     ** a buffer overread.  The "d1+serial_type1+2" subexpression will
 64531     ** always be greater than or equal to the amount of required key space.
 64532     ** Use that approximation to avoid the more expensive call to
 64533     ** sqlite3VdbeSerialTypeLen() in the common case.
 64534     */
 64535     if( d1+serial_type1+2>(u32)nKey1
 64536      && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 
 64537     ){
 64538       break;
 64541     /* Extract the values to be compared.
 64542     */
 64543     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
 64545     /* Do the comparison
 64546     */
 64547     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
 64548     if( rc!=0 ){
 64549       assert( mem1.zMalloc==0 );  /* See comment below */
 64550       if( pKeyInfo->aSortOrder[i] ){
 64551         rc = -rc;  /* Invert the result for DESC sort order. */
 64553       return rc;
 64555     i++;
 64556   }while( idx1<szHdr1 && i<pPKey2->nField );
 64558   /* No memory allocation is ever used on mem1.  Prove this using
 64559   ** the following assert().  If the assert() fails, it indicates a
 64560   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
 64561   */
 64562   assert( mem1.zMalloc==0 );
 64564   /* rc==0 here means that one of the keys ran out of fields and
 64565   ** all the fields up to that point were equal. Return the the default_rc
 64566   ** value.  */
 64567   return pPKey2->default_rc;
 64569 #endif
 64571 /*
 64572 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
 64573 ** using the collation sequence pColl. As usual, return a negative , zero
 64574 ** or positive value if *pMem1 is less than, equal to or greater than 
 64575 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
 64576 */
 64577 static int vdbeCompareMemString(
 64578   const Mem *pMem1,
 64579   const Mem *pMem2,
 64580   const CollSeq *pColl
 64581 ){
 64582   if( pMem1->enc==pColl->enc ){
 64583     /* The strings are already in the correct encoding.  Call the
 64584      ** comparison function directly */
 64585     return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
 64586   }else{
 64587     int rc;
 64588     const void *v1, *v2;
 64589     int n1, n2;
 64590     Mem c1;
 64591     Mem c2;
 64592     memset(&c1, 0, sizeof(c1));
 64593     memset(&c2, 0, sizeof(c2));
 64594     sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
 64595     sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
 64596     v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
 64597     n1 = v1==0 ? 0 : c1.n;
 64598     v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
 64599     n2 = v2==0 ? 0 : c2.n;
 64600     rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
 64601     sqlite3VdbeMemRelease(&c1);
 64602     sqlite3VdbeMemRelease(&c2);
 64603     return rc;
 64607 /*
 64608 ** Compare the values contained by the two memory cells, returning
 64609 ** negative, zero or positive if pMem1 is less than, equal to, or greater
 64610 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
 64611 ** and reals) sorted numerically, followed by text ordered by the collating
 64612 ** sequence pColl and finally blob's ordered by memcmp().
 64613 **
 64614 ** Two NULL values are considered equal by this function.
 64615 */
 64616 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
 64617   int rc;
 64618   int f1, f2;
 64619   int combined_flags;
 64621   f1 = pMem1->flags;
 64622   f2 = pMem2->flags;
 64623   combined_flags = f1|f2;
 64624   assert( (combined_flags & MEM_RowSet)==0 );
 64626   /* If one value is NULL, it is less than the other. If both values
 64627   ** are NULL, return 0.
 64628   */
 64629   if( combined_flags&MEM_Null ){
 64630     return (f2&MEM_Null) - (f1&MEM_Null);
 64633   /* If one value is a number and the other is not, the number is less.
 64634   ** If both are numbers, compare as reals if one is a real, or as integers
 64635   ** if both values are integers.
 64636   */
 64637   if( combined_flags&(MEM_Int|MEM_Real) ){
 64638     double r1, r2;
 64639     if( (f1 & f2 & MEM_Int)!=0 ){
 64640       if( pMem1->u.i < pMem2->u.i ) return -1;
 64641       if( pMem1->u.i > pMem2->u.i ) return 1;
 64642       return 0;
 64644     if( (f1&MEM_Real)!=0 ){
 64645       r1 = pMem1->r;
 64646     }else if( (f1&MEM_Int)!=0 ){
 64647       r1 = (double)pMem1->u.i;
 64648     }else{
 64649       return 1;
 64651     if( (f2&MEM_Real)!=0 ){
 64652       r2 = pMem2->r;
 64653     }else if( (f2&MEM_Int)!=0 ){
 64654       r2 = (double)pMem2->u.i;
 64655     }else{
 64656       return -1;
 64658     if( r1<r2 ) return -1;
 64659     if( r1>r2 ) return 1;
 64660     return 0;
 64663   /* If one value is a string and the other is a blob, the string is less.
 64664   ** If both are strings, compare using the collating functions.
 64665   */
 64666   if( combined_flags&MEM_Str ){
 64667     if( (f1 & MEM_Str)==0 ){
 64668       return 1;
 64670     if( (f2 & MEM_Str)==0 ){
 64671       return -1;
 64674     assert( pMem1->enc==pMem2->enc );
 64675     assert( pMem1->enc==SQLITE_UTF8 || 
 64676             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
 64678     /* The collation sequence must be defined at this point, even if
 64679     ** the user deletes the collation sequence after the vdbe program is
 64680     ** compiled (this was not always the case).
 64681     */
 64682     assert( !pColl || pColl->xCmp );
 64684     if( pColl ){
 64685       return vdbeCompareMemString(pMem1, pMem2, pColl);
 64687     /* If a NULL pointer was passed as the collate function, fall through
 64688     ** to the blob case and use memcmp().  */
 64691   /* Both values must be blobs.  Compare using memcmp().  */
 64692   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
 64693   if( rc==0 ){
 64694     rc = pMem1->n - pMem2->n;
 64696   return rc;
 64700 /*
 64701 ** The first argument passed to this function is a serial-type that
 64702 ** corresponds to an integer - all values between 1 and 9 inclusive 
 64703 ** except 7. The second points to a buffer containing an integer value
 64704 ** serialized according to serial_type. This function deserializes
 64705 ** and returns the value.
 64706 */
 64707 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
 64708   u32 y;
 64709   assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
 64710   switch( serial_type ){
 64711     case 0:
 64712     case 1:
 64713       testcase( aKey[0]&0x80 );
 64714       return ONE_BYTE_INT(aKey);
 64715     case 2:
 64716       testcase( aKey[0]&0x80 );
 64717       return TWO_BYTE_INT(aKey);
 64718     case 3:
 64719       testcase( aKey[0]&0x80 );
 64720       return THREE_BYTE_INT(aKey);
 64721     case 4: {
 64722       testcase( aKey[0]&0x80 );
 64723       y = FOUR_BYTE_UINT(aKey);
 64724       return (i64)*(int*)&y;
 64726     case 5: {
 64727       testcase( aKey[0]&0x80 );
 64728       return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
 64730     case 6: {
 64731       u64 x = FOUR_BYTE_UINT(aKey);
 64732       testcase( aKey[0]&0x80 );
 64733       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
 64734       return (i64)*(i64*)&x;
 64738   return (serial_type - 8);
 64741 /*
 64742 ** This function compares the two table rows or index records
 64743 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
 64744 ** or positive integer if key1 is less than, equal to or 
 64745 ** greater than key2.  The {nKey1, pKey1} key must be a blob
 64746 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
 64747 ** key must be a parsed key such as obtained from
 64748 ** sqlite3VdbeParseRecord.
 64749 **
 64750 ** If argument bSkip is non-zero, it is assumed that the caller has already
 64751 ** determined that the first fields of the keys are equal.
 64752 **
 64753 ** Key1 and Key2 do not have to contain the same number of fields. If all 
 64754 ** fields that appear in both keys are equal, then pPKey2->default_rc is 
 64755 ** returned.
 64756 */
 64757 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
 64758   int nKey1, const void *pKey1,   /* Left key */
 64759   const UnpackedRecord *pPKey2,   /* Right key */
 64760   int bSkip                       /* If true, skip the first field */
 64761 ){
 64762   u32 d1;                         /* Offset into aKey[] of next data element */
 64763   int i;                          /* Index of next field to compare */
 64764   u32 szHdr1;                     /* Size of record header in bytes */
 64765   u32 idx1;                       /* Offset of first type in header */
 64766   int rc = 0;                     /* Return value */
 64767   Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
 64768   KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
 64769   const unsigned char *aKey1 = (const unsigned char *)pKey1;
 64770   Mem mem1;
 64772   /* If bSkip is true, then the caller has already determined that the first
 64773   ** two elements in the keys are equal. Fix the various stack variables so
 64774   ** that this routine begins comparing at the second field. */
 64775   if( bSkip ){
 64776     u32 s1;
 64777     idx1 = 1 + getVarint32(&aKey1[1], s1);
 64778     szHdr1 = aKey1[0];
 64779     d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
 64780     i = 1;
 64781     pRhs++;
 64782   }else{
 64783     idx1 = getVarint32(aKey1, szHdr1);
 64784     d1 = szHdr1;
 64785     if( d1>(unsigned)nKey1 ) return 1;  /* Corruption */
 64786     i = 0;
 64789   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
 64790   assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
 64791        || CORRUPT_DB );
 64792   assert( pPKey2->pKeyInfo->aSortOrder!=0 );
 64793   assert( pPKey2->pKeyInfo->nField>0 );
 64794   assert( idx1<=szHdr1 || CORRUPT_DB );
 64795   do{
 64796     u32 serial_type;
 64798     /* RHS is an integer */
 64799     if( pRhs->flags & MEM_Int ){
 64800       serial_type = aKey1[idx1];
 64801       testcase( serial_type==12 );
 64802       if( serial_type>=12 ){
 64803         rc = +1;
 64804       }else if( serial_type==0 ){
 64805         rc = -1;
 64806       }else if( serial_type==7 ){
 64807         double rhs = (double)pRhs->u.i;
 64808         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
 64809         if( mem1.r<rhs ){
 64810           rc = -1;
 64811         }else if( mem1.r>rhs ){
 64812           rc = +1;
 64814       }else{
 64815         i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
 64816         i64 rhs = pRhs->u.i;
 64817         if( lhs<rhs ){
 64818           rc = -1;
 64819         }else if( lhs>rhs ){
 64820           rc = +1;
 64825     /* RHS is real */
 64826     else if( pRhs->flags & MEM_Real ){
 64827       serial_type = aKey1[idx1];
 64828       if( serial_type>=12 ){
 64829         rc = +1;
 64830       }else if( serial_type==0 ){
 64831         rc = -1;
 64832       }else{
 64833         double rhs = pRhs->r;
 64834         double lhs;
 64835         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
 64836         if( serial_type==7 ){
 64837           lhs = mem1.r;
 64838         }else{
 64839           lhs = (double)mem1.u.i;
 64841         if( lhs<rhs ){
 64842           rc = -1;
 64843         }else if( lhs>rhs ){
 64844           rc = +1;
 64849     /* RHS is a string */
 64850     else if( pRhs->flags & MEM_Str ){
 64851       getVarint32(&aKey1[idx1], serial_type);
 64852       testcase( serial_type==12 );
 64853       if( serial_type<12 ){
 64854         rc = -1;
 64855       }else if( !(serial_type & 0x01) ){
 64856         rc = +1;
 64857       }else{
 64858         mem1.n = (serial_type - 12) / 2;
 64859         testcase( (d1+mem1.n)==(unsigned)nKey1 );
 64860         testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
 64861         if( (d1+mem1.n) > (unsigned)nKey1 ){
 64862           rc = 1;                /* Corruption */
 64863         }else if( pKeyInfo->aColl[i] ){
 64864           mem1.enc = pKeyInfo->enc;
 64865           mem1.db = pKeyInfo->db;
 64866           mem1.flags = MEM_Str;
 64867           mem1.z = (char*)&aKey1[d1];
 64868           rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
 64869         }else{
 64870           int nCmp = MIN(mem1.n, pRhs->n);
 64871           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
 64872           if( rc==0 ) rc = mem1.n - pRhs->n; 
 64877     /* RHS is a blob */
 64878     else if( pRhs->flags & MEM_Blob ){
 64879       getVarint32(&aKey1[idx1], serial_type);
 64880       testcase( serial_type==12 );
 64881       if( serial_type<12 || (serial_type & 0x01) ){
 64882         rc = -1;
 64883       }else{
 64884         int nStr = (serial_type - 12) / 2;
 64885         testcase( (d1+nStr)==(unsigned)nKey1 );
 64886         testcase( (d1+nStr+1)==(unsigned)nKey1 );
 64887         if( (d1+nStr) > (unsigned)nKey1 ){
 64888           rc = 1;                /* Corruption */
 64889         }else{
 64890           int nCmp = MIN(nStr, pRhs->n);
 64891           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
 64892           if( rc==0 ) rc = nStr - pRhs->n;
 64897     /* RHS is null */
 64898     else{
 64899       serial_type = aKey1[idx1];
 64900       rc = (serial_type!=0);
 64903     if( rc!=0 ){
 64904       if( pKeyInfo->aSortOrder[i] ){
 64905         rc = -rc;
 64907       assert( CORRUPT_DB
 64908           || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
 64909           || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
 64910           || pKeyInfo->db->mallocFailed
 64911       );
 64912       assert( mem1.zMalloc==0 );  /* See comment below */
 64913       return rc;
 64916     i++;
 64917     pRhs++;
 64918     d1 += sqlite3VdbeSerialTypeLen(serial_type);
 64919     idx1 += sqlite3VarintLen(serial_type);
 64920   }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
 64922   /* No memory allocation is ever used on mem1.  Prove this using
 64923   ** the following assert().  If the assert() fails, it indicates a
 64924   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
 64925   assert( mem1.zMalloc==0 );
 64927   /* rc==0 here means that one or both of the keys ran out of fields and
 64928   ** all the fields up to that point were equal. Return the the default_rc
 64929   ** value.  */
 64930   assert( CORRUPT_DB 
 64931        || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2) 
 64932   );
 64933   return pPKey2->default_rc;
 64936 /*
 64937 ** This function is an optimized version of sqlite3VdbeRecordCompare() 
 64938 ** that (a) the first field of pPKey2 is an integer, and (b) the 
 64939 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
 64940 ** byte (i.e. is less than 128).
 64941 */
 64942 static int vdbeRecordCompareInt(
 64943   int nKey1, const void *pKey1, /* Left key */
 64944   const UnpackedRecord *pPKey2, /* Right key */
 64945   int bSkip                     /* Ignored */
 64946 ){
 64947   const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
 64948   int serial_type = ((const u8*)pKey1)[1];
 64949   int res;
 64950   u32 y;
 64951   u64 x;
 64952   i64 v = pPKey2->aMem[0].u.i;
 64953   i64 lhs;
 64954   UNUSED_PARAMETER(bSkip);
 64956   assert( bSkip==0 );
 64957   switch( serial_type ){
 64958     case 1: { /* 1-byte signed integer */
 64959       lhs = ONE_BYTE_INT(aKey);
 64960       testcase( lhs<0 );
 64961       break;
 64963     case 2: { /* 2-byte signed integer */
 64964       lhs = TWO_BYTE_INT(aKey);
 64965       testcase( lhs<0 );
 64966       break;
 64968     case 3: { /* 3-byte signed integer */
 64969       lhs = THREE_BYTE_INT(aKey);
 64970       testcase( lhs<0 );
 64971       break;
 64973     case 4: { /* 4-byte signed integer */
 64974       y = FOUR_BYTE_UINT(aKey);
 64975       lhs = (i64)*(int*)&y;
 64976       testcase( lhs<0 );
 64977       break;
 64979     case 5: { /* 6-byte signed integer */
 64980       lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
 64981       testcase( lhs<0 );
 64982       break;
 64984     case 6: { /* 8-byte signed integer */
 64985       x = FOUR_BYTE_UINT(aKey);
 64986       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
 64987       lhs = *(i64*)&x;
 64988       testcase( lhs<0 );
 64989       break;
 64991     case 8: 
 64992       lhs = 0;
 64993       break;
 64994     case 9:
 64995       lhs = 1;
 64996       break;
 64998     /* This case could be removed without changing the results of running
 64999     ** this code. Including it causes gcc to generate a faster switch 
 65000     ** statement (since the range of switch targets now starts at zero and
 65001     ** is contiguous) but does not cause any duplicate code to be generated
 65002     ** (as gcc is clever enough to combine the two like cases). Other 
 65003     ** compilers might be similar.  */ 
 65004     case 0: case 7:
 65005       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
 65007     default:
 65008       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
 65011   if( v>lhs ){
 65012     res = pPKey2->r1;
 65013   }else if( v<lhs ){
 65014     res = pPKey2->r2;
 65015   }else if( pPKey2->nField>1 ){
 65016     /* The first fields of the two keys are equal. Compare the trailing 
 65017     ** fields.  */
 65018     res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
 65019   }else{
 65020     /* The first fields of the two keys are equal and there are no trailing
 65021     ** fields. Return pPKey2->default_rc in this case. */
 65022     res = pPKey2->default_rc;
 65025   assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
 65026        || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
 65027        || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
 65028        || CORRUPT_DB
 65029   );
 65030   return res;
 65033 /*
 65034 ** This function is an optimized version of sqlite3VdbeRecordCompare() 
 65035 ** that (a) the first field of pPKey2 is a string, that (b) the first field
 65036 ** uses the collation sequence BINARY and (c) that the size-of-header varint 
 65037 ** at the start of (pKey1/nKey1) fits in a single byte.
 65038 */
 65039 static int vdbeRecordCompareString(
 65040   int nKey1, const void *pKey1, /* Left key */
 65041   const UnpackedRecord *pPKey2, /* Right key */
 65042   int bSkip
 65043 ){
 65044   const u8 *aKey1 = (const u8*)pKey1;
 65045   int serial_type;
 65046   int res;
 65047   UNUSED_PARAMETER(bSkip);
 65049   assert( bSkip==0 );
 65050   getVarint32(&aKey1[1], serial_type);
 65052   if( serial_type<12 ){
 65053     res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
 65054   }else if( !(serial_type & 0x01) ){ 
 65055     res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
 65056   }else{
 65057     int nCmp;
 65058     int nStr;
 65059     int szHdr = aKey1[0];
 65061     nStr = (serial_type-12) / 2;
 65062     if( (szHdr + nStr) > nKey1 ) return 0;    /* Corruption */
 65063     nCmp = MIN( pPKey2->aMem[0].n, nStr );
 65064     res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
 65066     if( res==0 ){
 65067       res = nStr - pPKey2->aMem[0].n;
 65068       if( res==0 ){
 65069         if( pPKey2->nField>1 ){
 65070           res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
 65071         }else{
 65072           res = pPKey2->default_rc;
 65074       }else if( res>0 ){
 65075         res = pPKey2->r2;
 65076       }else{
 65077         res = pPKey2->r1;
 65079     }else if( res>0 ){
 65080       res = pPKey2->r2;
 65081     }else{
 65082       res = pPKey2->r1;
 65086   assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
 65087        || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
 65088        || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
 65089        || CORRUPT_DB
 65090   );
 65091   return res;
 65094 /*
 65095 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
 65096 ** suitable for comparing serialized records to the unpacked record passed
 65097 ** as the only argument.
 65098 */
 65099 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
 65100   /* varintRecordCompareInt() and varintRecordCompareString() both assume
 65101   ** that the size-of-header varint that occurs at the start of each record
 65102   ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
 65103   ** also assumes that it is safe to overread a buffer by at least the 
 65104   ** maximum possible legal header size plus 8 bytes. Because there is
 65105   ** guaranteed to be at least 74 (but not 136) bytes of padding following each
 65106   ** buffer passed to varintRecordCompareInt() this makes it convenient to
 65107   ** limit the size of the header to 64 bytes in cases where the first field
 65108   ** is an integer.
 65109   **
 65110   ** The easiest way to enforce this limit is to consider only records with
 65111   ** 13 fields or less. If the first field is an integer, the maximum legal
 65112   ** header size is (12*5 + 1 + 1) bytes.  */
 65113   if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
 65114     int flags = p->aMem[0].flags;
 65115     if( p->pKeyInfo->aSortOrder[0] ){
 65116       p->r1 = 1;
 65117       p->r2 = -1;
 65118     }else{
 65119       p->r1 = -1;
 65120       p->r2 = 1;
 65122     if( (flags & MEM_Int) ){
 65123       return vdbeRecordCompareInt;
 65125     testcase( flags & MEM_Real );
 65126     testcase( flags & MEM_Null );
 65127     testcase( flags & MEM_Blob );
 65128     if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
 65129       assert( flags & MEM_Str );
 65130       return vdbeRecordCompareString;
 65134   return sqlite3VdbeRecordCompare;
 65137 /*
 65138 ** pCur points at an index entry created using the OP_MakeRecord opcode.
 65139 ** Read the rowid (the last field in the record) and store it in *rowid.
 65140 ** Return SQLITE_OK if everything works, or an error code otherwise.
 65141 **
 65142 ** pCur might be pointing to text obtained from a corrupt database file.
 65143 ** So the content cannot be trusted.  Do appropriate checks on the content.
 65144 */
 65145 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
 65146   i64 nCellKey = 0;
 65147   int rc;
 65148   u32 szHdr;        /* Size of the header */
 65149   u32 typeRowid;    /* Serial type of the rowid */
 65150   u32 lenRowid;     /* Size of the rowid */
 65151   Mem m, v;
 65153   UNUSED_PARAMETER(db);
 65155   /* Get the size of the index entry.  Only indices entries of less
 65156   ** than 2GiB are support - anything large must be database corruption.
 65157   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
 65158   ** this code can safely assume that nCellKey is 32-bits  
 65159   */
 65160   assert( sqlite3BtreeCursorIsValid(pCur) );
 65161   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 65162   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
 65163   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
 65165   /* Read in the complete content of the index entry */
 65166   memset(&m, 0, sizeof(m));
 65167   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
 65168   if( rc ){
 65169     return rc;
 65172   /* The index entry must begin with a header size */
 65173   (void)getVarint32((u8*)m.z, szHdr);
 65174   testcase( szHdr==3 );
 65175   testcase( szHdr==m.n );
 65176   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
 65177     goto idx_rowid_corruption;
 65180   /* The last field of the index should be an integer - the ROWID.
 65181   ** Verify that the last entry really is an integer. */
 65182   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
 65183   testcase( typeRowid==1 );
 65184   testcase( typeRowid==2 );
 65185   testcase( typeRowid==3 );
 65186   testcase( typeRowid==4 );
 65187   testcase( typeRowid==5 );
 65188   testcase( typeRowid==6 );
 65189   testcase( typeRowid==8 );
 65190   testcase( typeRowid==9 );
 65191   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
 65192     goto idx_rowid_corruption;
 65194   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
 65195   testcase( (u32)m.n==szHdr+lenRowid );
 65196   if( unlikely((u32)m.n<szHdr+lenRowid) ){
 65197     goto idx_rowid_corruption;
 65200   /* Fetch the integer off the end of the index record */
 65201   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
 65202   *rowid = v.u.i;
 65203   sqlite3VdbeMemRelease(&m);
 65204   return SQLITE_OK;
 65206   /* Jump here if database corruption is detected after m has been
 65207   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
 65208 idx_rowid_corruption:
 65209   testcase( m.zMalloc!=0 );
 65210   sqlite3VdbeMemRelease(&m);
 65211   return SQLITE_CORRUPT_BKPT;
 65214 /*
 65215 ** Compare the key of the index entry that cursor pC is pointing to against
 65216 ** the key string in pUnpacked.  Write into *pRes a number
 65217 ** that is negative, zero, or positive if pC is less than, equal to,
 65218 ** or greater than pUnpacked.  Return SQLITE_OK on success.
 65219 **
 65220 ** pUnpacked is either created without a rowid or is truncated so that it
 65221 ** omits the rowid at the end.  The rowid at the end of the index entry
 65222 ** is ignored as well.  Hence, this routine only compares the prefixes 
 65223 ** of the keys prior to the final rowid, not the entire key.
 65224 */
 65225 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
 65226   VdbeCursor *pC,                  /* The cursor to compare against */
 65227   const UnpackedRecord *pUnpacked, /* Unpacked version of key */
 65228   int *res                         /* Write the comparison result here */
 65229 ){
 65230   i64 nCellKey = 0;
 65231   int rc;
 65232   BtCursor *pCur = pC->pCursor;
 65233   Mem m;
 65235   assert( sqlite3BtreeCursorIsValid(pCur) );
 65236   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
 65237   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
 65238   /* nCellKey will always be between 0 and 0xffffffff because of the way
 65239   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
 65240   if( nCellKey<=0 || nCellKey>0x7fffffff ){
 65241     *res = 0;
 65242     return SQLITE_CORRUPT_BKPT;
 65244   memset(&m, 0, sizeof(m));
 65245   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
 65246   if( rc ){
 65247     return rc;
 65249   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
 65250   sqlite3VdbeMemRelease(&m);
 65251   return SQLITE_OK;
 65254 /*
 65255 ** This routine sets the value to be returned by subsequent calls to
 65256 ** sqlite3_changes() on the database handle 'db'. 
 65257 */
 65258 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
 65259   assert( sqlite3_mutex_held(db->mutex) );
 65260   db->nChange = nChange;
 65261   db->nTotalChange += nChange;
 65264 /*
 65265 ** Set a flag in the vdbe to update the change counter when it is finalised
 65266 ** or reset.
 65267 */
 65268 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
 65269   v->changeCntOn = 1;
 65272 /*
 65273 ** Mark every prepared statement associated with a database connection
 65274 ** as expired.
 65275 **
 65276 ** An expired statement means that recompilation of the statement is
 65277 ** recommend.  Statements expire when things happen that make their
 65278 ** programs obsolete.  Removing user-defined functions or collating
 65279 ** sequences, or changing an authorization function are the types of
 65280 ** things that make prepared statements obsolete.
 65281 */
 65282 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
 65283   Vdbe *p;
 65284   for(p = db->pVdbe; p; p=p->pNext){
 65285     p->expired = 1;
 65289 /*
 65290 ** Return the database associated with the Vdbe.
 65291 */
 65292 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
 65293   return v->db;
 65296 /*
 65297 ** Return a pointer to an sqlite3_value structure containing the value bound
 65298 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
 65299 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
 65300 ** constants) to the value before returning it.
 65301 **
 65302 ** The returned value must be freed by the caller using sqlite3ValueFree().
 65303 */
 65304 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
 65305   assert( iVar>0 );
 65306   if( v ){
 65307     Mem *pMem = &v->aVar[iVar-1];
 65308     if( 0==(pMem->flags & MEM_Null) ){
 65309       sqlite3_value *pRet = sqlite3ValueNew(v->db);
 65310       if( pRet ){
 65311         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
 65312         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
 65314       return pRet;
 65317   return 0;
 65320 /*
 65321 ** Configure SQL variable iVar so that binding a new value to it signals
 65322 ** to sqlite3_reoptimize() that re-preparing the statement may result
 65323 ** in a better query plan.
 65324 */
 65325 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
 65326   assert( iVar>0 );
 65327   if( iVar>32 ){
 65328     v->expmask = 0xffffffff;
 65329   }else{
 65330     v->expmask |= ((u32)1 << (iVar-1));
 65334 #ifndef SQLITE_OMIT_VIRTUALTABLE
 65335 /*
 65336 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
 65337 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
 65338 ** in memory obtained from sqlite3DbMalloc).
 65339 */
 65340 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
 65341   sqlite3 *db = p->db;
 65342   sqlite3DbFree(db, p->zErrMsg);
 65343   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
 65344   sqlite3_free(pVtab->zErrMsg);
 65345   pVtab->zErrMsg = 0;
 65347 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 65349 /************** End of vdbeaux.c *********************************************/
 65350 /************** Begin file vdbeapi.c *****************************************/
 65351 /*
 65352 ** 2004 May 26
 65353 **
 65354 ** The author disclaims copyright to this source code.  In place of
 65355 ** a legal notice, here is a blessing:
 65356 **
 65357 **    May you do good and not evil.
 65358 **    May you find forgiveness for yourself and forgive others.
 65359 **    May you share freely, never taking more than you give.
 65360 **
 65361 *************************************************************************
 65362 **
 65363 ** This file contains code use to implement APIs that are part of the
 65364 ** VDBE.
 65365 */
 65367 #ifndef SQLITE_OMIT_DEPRECATED
 65368 /*
 65369 ** Return TRUE (non-zero) of the statement supplied as an argument needs
 65370 ** to be recompiled.  A statement needs to be recompiled whenever the
 65371 ** execution environment changes in a way that would alter the program
 65372 ** that sqlite3_prepare() generates.  For example, if new functions or
 65373 ** collating sequences are registered or if an authorizer function is
 65374 ** added or changed.
 65375 */
 65376 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
 65377   Vdbe *p = (Vdbe*)pStmt;
 65378   return p==0 || p->expired;
 65380 #endif
 65382 /*
 65383 ** Check on a Vdbe to make sure it has not been finalized.  Log
 65384 ** an error and return true if it has been finalized (or is otherwise
 65385 ** invalid).  Return false if it is ok.
 65386 */
 65387 static int vdbeSafety(Vdbe *p){
 65388   if( p->db==0 ){
 65389     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
 65390     return 1;
 65391   }else{
 65392     return 0;
 65395 static int vdbeSafetyNotNull(Vdbe *p){
 65396   if( p==0 ){
 65397     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
 65398     return 1;
 65399   }else{
 65400     return vdbeSafety(p);
 65404 /*
 65405 ** The following routine destroys a virtual machine that is created by
 65406 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
 65407 ** success/failure code that describes the result of executing the virtual
 65408 ** machine.
 65409 **
 65410 ** This routine sets the error code and string returned by
 65411 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 65412 */
 65413 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
 65414   int rc;
 65415   if( pStmt==0 ){
 65416     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
 65417     ** pointer is a harmless no-op. */
 65418     rc = SQLITE_OK;
 65419   }else{
 65420     Vdbe *v = (Vdbe*)pStmt;
 65421     sqlite3 *db = v->db;
 65422     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
 65423     sqlite3_mutex_enter(db->mutex);
 65424     rc = sqlite3VdbeFinalize(v);
 65425     rc = sqlite3ApiExit(db, rc);
 65426     sqlite3LeaveMutexAndCloseZombie(db);
 65428   return rc;
 65431 /*
 65432 ** Terminate the current execution of an SQL statement and reset it
 65433 ** back to its starting state so that it can be reused. A success code from
 65434 ** the prior execution is returned.
 65435 **
 65436 ** This routine sets the error code and string returned by
 65437 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
 65438 */
 65439 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
 65440   int rc;
 65441   if( pStmt==0 ){
 65442     rc = SQLITE_OK;
 65443   }else{
 65444     Vdbe *v = (Vdbe*)pStmt;
 65445     sqlite3_mutex_enter(v->db->mutex);
 65446     rc = sqlite3VdbeReset(v);
 65447     sqlite3VdbeRewind(v);
 65448     assert( (rc & (v->db->errMask))==rc );
 65449     rc = sqlite3ApiExit(v->db, rc);
 65450     sqlite3_mutex_leave(v->db->mutex);
 65452   return rc;
 65455 /*
 65456 ** Set all the parameters in the compiled SQL statement to NULL.
 65457 */
 65458 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
 65459   int i;
 65460   int rc = SQLITE_OK;
 65461   Vdbe *p = (Vdbe*)pStmt;
 65462 #if SQLITE_THREADSAFE
 65463   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
 65464 #endif
 65465   sqlite3_mutex_enter(mutex);
 65466   for(i=0; i<p->nVar; i++){
 65467     sqlite3VdbeMemRelease(&p->aVar[i]);
 65468     p->aVar[i].flags = MEM_Null;
 65470   if( p->isPrepareV2 && p->expmask ){
 65471     p->expired = 1;
 65473   sqlite3_mutex_leave(mutex);
 65474   return rc;
 65478 /**************************** sqlite3_value_  *******************************
 65479 ** The following routines extract information from a Mem or sqlite3_value
 65480 ** structure.
 65481 */
 65482 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
 65483   Mem *p = (Mem*)pVal;
 65484   if( p->flags & (MEM_Blob|MEM_Str) ){
 65485     sqlite3VdbeMemExpandBlob(p);
 65486     p->flags |= MEM_Blob;
 65487     return p->n ? p->z : 0;
 65488   }else{
 65489     return sqlite3_value_text(pVal);
 65492 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
 65493   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
 65495 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
 65496   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
 65498 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
 65499   return sqlite3VdbeRealValue((Mem*)pVal);
 65501 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
 65502   return (int)sqlite3VdbeIntValue((Mem*)pVal);
 65504 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
 65505   return sqlite3VdbeIntValue((Mem*)pVal);
 65507 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
 65508   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
 65510 #ifndef SQLITE_OMIT_UTF16
 65511 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
 65512   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
 65514 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
 65515   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
 65517 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
 65518   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
 65520 #endif /* SQLITE_OMIT_UTF16 */
 65521 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
 65522   static const u8 aType[] = {
 65523      SQLITE_BLOB,     /* 0x00 */
 65524      SQLITE_NULL,     /* 0x01 */
 65525      SQLITE_TEXT,     /* 0x02 */
 65526      SQLITE_NULL,     /* 0x03 */
 65527      SQLITE_INTEGER,  /* 0x04 */
 65528      SQLITE_NULL,     /* 0x05 */
 65529      SQLITE_INTEGER,  /* 0x06 */
 65530      SQLITE_NULL,     /* 0x07 */
 65531      SQLITE_FLOAT,    /* 0x08 */
 65532      SQLITE_NULL,     /* 0x09 */
 65533      SQLITE_FLOAT,    /* 0x0a */
 65534      SQLITE_NULL,     /* 0x0b */
 65535      SQLITE_INTEGER,  /* 0x0c */
 65536      SQLITE_NULL,     /* 0x0d */
 65537      SQLITE_INTEGER,  /* 0x0e */
 65538      SQLITE_NULL,     /* 0x0f */
 65539      SQLITE_BLOB,     /* 0x10 */
 65540      SQLITE_NULL,     /* 0x11 */
 65541      SQLITE_TEXT,     /* 0x12 */
 65542      SQLITE_NULL,     /* 0x13 */
 65543      SQLITE_INTEGER,  /* 0x14 */
 65544      SQLITE_NULL,     /* 0x15 */
 65545      SQLITE_INTEGER,  /* 0x16 */
 65546      SQLITE_NULL,     /* 0x17 */
 65547      SQLITE_FLOAT,    /* 0x18 */
 65548      SQLITE_NULL,     /* 0x19 */
 65549      SQLITE_FLOAT,    /* 0x1a */
 65550      SQLITE_NULL,     /* 0x1b */
 65551      SQLITE_INTEGER,  /* 0x1c */
 65552      SQLITE_NULL,     /* 0x1d */
 65553      SQLITE_INTEGER,  /* 0x1e */
 65554      SQLITE_NULL,     /* 0x1f */
 65555   };
 65556   return aType[pVal->flags&MEM_AffMask];
 65559 /**************************** sqlite3_result_  *******************************
 65560 ** The following routines are used by user-defined functions to specify
 65561 ** the function result.
 65562 **
 65563 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
 65564 ** result as a string or blob but if the string or blob is too large, it
 65565 ** then sets the error code to SQLITE_TOOBIG
 65566 */
 65567 static void setResultStrOrError(
 65568   sqlite3_context *pCtx,  /* Function context */
 65569   const char *z,          /* String pointer */
 65570   int n,                  /* Bytes in string, or negative */
 65571   u8 enc,                 /* Encoding of z.  0 for BLOBs */
 65572   void (*xDel)(void*)     /* Destructor function */
 65573 ){
 65574   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
 65575     sqlite3_result_error_toobig(pCtx);
 65578 SQLITE_API void sqlite3_result_blob(
 65579   sqlite3_context *pCtx, 
 65580   const void *z, 
 65581   int n, 
 65582   void (*xDel)(void *)
 65583 ){
 65584   assert( n>=0 );
 65585   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65586   setResultStrOrError(pCtx, z, n, 0, xDel);
 65588 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
 65589   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65590   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
 65592 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
 65593   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65594   pCtx->isError = SQLITE_ERROR;
 65595   pCtx->fErrorOrAux = 1;
 65596   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 65598 #ifndef SQLITE_OMIT_UTF16
 65599 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
 65600   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65601   pCtx->isError = SQLITE_ERROR;
 65602   pCtx->fErrorOrAux = 1;
 65603   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 65605 #endif
 65606 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
 65607   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65608   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
 65610 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
 65611   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65612   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
 65614 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
 65615   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65616   sqlite3VdbeMemSetNull(&pCtx->s);
 65618 SQLITE_API void sqlite3_result_text(
 65619   sqlite3_context *pCtx, 
 65620   const char *z, 
 65621   int n,
 65622   void (*xDel)(void *)
 65623 ){
 65624   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65625   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
 65627 #ifndef SQLITE_OMIT_UTF16
 65628 SQLITE_API void sqlite3_result_text16(
 65629   sqlite3_context *pCtx, 
 65630   const void *z, 
 65631   int n, 
 65632   void (*xDel)(void *)
 65633 ){
 65634   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65635   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
 65637 SQLITE_API void sqlite3_result_text16be(
 65638   sqlite3_context *pCtx, 
 65639   const void *z, 
 65640   int n, 
 65641   void (*xDel)(void *)
 65642 ){
 65643   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65644   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
 65646 SQLITE_API void sqlite3_result_text16le(
 65647   sqlite3_context *pCtx, 
 65648   const void *z, 
 65649   int n, 
 65650   void (*xDel)(void *)
 65651 ){
 65652   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65653   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
 65655 #endif /* SQLITE_OMIT_UTF16 */
 65656 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
 65657   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65658   sqlite3VdbeMemCopy(&pCtx->s, pValue);
 65660 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
 65661   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65662   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
 65664 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
 65665   pCtx->isError = errCode;
 65666   pCtx->fErrorOrAux = 1;
 65667   if( pCtx->s.flags & MEM_Null ){
 65668     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
 65669                          SQLITE_UTF8, SQLITE_STATIC);
 65673 /* Force an SQLITE_TOOBIG error. */
 65674 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
 65675   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65676   pCtx->isError = SQLITE_TOOBIG;
 65677   pCtx->fErrorOrAux = 1;
 65678   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
 65679                        SQLITE_UTF8, SQLITE_STATIC);
 65682 /* An SQLITE_NOMEM error. */
 65683 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
 65684   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65685   sqlite3VdbeMemSetNull(&pCtx->s);
 65686   pCtx->isError = SQLITE_NOMEM;
 65687   pCtx->fErrorOrAux = 1;
 65688   pCtx->s.db->mallocFailed = 1;
 65691 /*
 65692 ** This function is called after a transaction has been committed. It 
 65693 ** invokes callbacks registered with sqlite3_wal_hook() as required.
 65694 */
 65695 static int doWalCallbacks(sqlite3 *db){
 65696   int rc = SQLITE_OK;
 65697 #ifndef SQLITE_OMIT_WAL
 65698   int i;
 65699   for(i=0; i<db->nDb; i++){
 65700     Btree *pBt = db->aDb[i].pBt;
 65701     if( pBt ){
 65702       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
 65703       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
 65704         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
 65708 #endif
 65709   return rc;
 65712 /*
 65713 ** Execute the statement pStmt, either until a row of data is ready, the
 65714 ** statement is completely executed or an error occurs.
 65715 **
 65716 ** This routine implements the bulk of the logic behind the sqlite_step()
 65717 ** API.  The only thing omitted is the automatic recompile if a 
 65718 ** schema change has occurred.  That detail is handled by the
 65719 ** outer sqlite3_step() wrapper procedure.
 65720 */
 65721 static int sqlite3Step(Vdbe *p){
 65722   sqlite3 *db;
 65723   int rc;
 65725   assert(p);
 65726   if( p->magic!=VDBE_MAGIC_RUN ){
 65727     /* We used to require that sqlite3_reset() be called before retrying
 65728     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
 65729     ** with version 3.7.0, we changed this so that sqlite3_reset() would
 65730     ** be called automatically instead of throwing the SQLITE_MISUSE error.
 65731     ** This "automatic-reset" change is not technically an incompatibility, 
 65732     ** since any application that receives an SQLITE_MISUSE is broken by
 65733     ** definition.
 65734     **
 65735     ** Nevertheless, some published applications that were originally written
 65736     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
 65737     ** returns, and those were broken by the automatic-reset change.  As a
 65738     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
 65739     ** legacy behavior of returning SQLITE_MISUSE for cases where the 
 65740     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
 65741     ** or SQLITE_BUSY error.
 65742     */
 65743 #ifdef SQLITE_OMIT_AUTORESET
 65744     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
 65745       sqlite3_reset((sqlite3_stmt*)p);
 65746     }else{
 65747       return SQLITE_MISUSE_BKPT;
 65749 #else
 65750     sqlite3_reset((sqlite3_stmt*)p);
 65751 #endif
 65754   /* Check that malloc() has not failed. If it has, return early. */
 65755   db = p->db;
 65756   if( db->mallocFailed ){
 65757     p->rc = SQLITE_NOMEM;
 65758     return SQLITE_NOMEM;
 65761   if( p->pc<=0 && p->expired ){
 65762     p->rc = SQLITE_SCHEMA;
 65763     rc = SQLITE_ERROR;
 65764     goto end_of_step;
 65766   if( p->pc<0 ){
 65767     /* If there are no other statements currently running, then
 65768     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
 65769     ** from interrupting a statement that has not yet started.
 65770     */
 65771     if( db->nVdbeActive==0 ){
 65772       db->u1.isInterrupted = 0;
 65775     assert( db->nVdbeWrite>0 || db->autoCommit==0 
 65776         || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
 65777     );
 65779 #ifndef SQLITE_OMIT_TRACE
 65780     if( db->xProfile && !db->init.busy ){
 65781       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 65783 #endif
 65785     db->nVdbeActive++;
 65786     if( p->readOnly==0 ) db->nVdbeWrite++;
 65787     if( p->bIsReader ) db->nVdbeRead++;
 65788     p->pc = 0;
 65790 #ifndef SQLITE_OMIT_EXPLAIN
 65791   if( p->explain ){
 65792     rc = sqlite3VdbeList(p);
 65793   }else
 65794 #endif /* SQLITE_OMIT_EXPLAIN */
 65796     db->nVdbeExec++;
 65797     rc = sqlite3VdbeExec(p);
 65798     db->nVdbeExec--;
 65801 #ifndef SQLITE_OMIT_TRACE
 65802   /* Invoke the profile callback if there is one
 65803   */
 65804   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
 65805     sqlite3_int64 iNow;
 65806     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
 65807     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
 65809 #endif
 65811   if( rc==SQLITE_DONE ){
 65812     assert( p->rc==SQLITE_OK );
 65813     p->rc = doWalCallbacks(db);
 65814     if( p->rc!=SQLITE_OK ){
 65815       rc = SQLITE_ERROR;
 65819   db->errCode = rc;
 65820   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
 65821     p->rc = SQLITE_NOMEM;
 65823 end_of_step:
 65824   /* At this point local variable rc holds the value that should be 
 65825   ** returned if this statement was compiled using the legacy 
 65826   ** sqlite3_prepare() interface. According to the docs, this can only
 65827   ** be one of the values in the first assert() below. Variable p->rc 
 65828   ** contains the value that would be returned if sqlite3_finalize() 
 65829   ** were called on statement p.
 65830   */
 65831   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
 65832        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
 65833   );
 65834   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
 65835   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
 65836     /* If this statement was prepared using sqlite3_prepare_v2(), and an
 65837     ** error has occurred, then return the error code in p->rc to the
 65838     ** caller. Set the error code in the database handle to the same value.
 65839     */ 
 65840     rc = sqlite3VdbeTransferError(p);
 65842   return (rc&db->errMask);
 65845 /*
 65846 ** This is the top-level implementation of sqlite3_step().  Call
 65847 ** sqlite3Step() to do most of the work.  If a schema error occurs,
 65848 ** call sqlite3Reprepare() and try again.
 65849 */
 65850 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
 65851   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
 65852   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
 65853   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
 65854   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
 65855   sqlite3 *db;             /* The database connection */
 65857   if( vdbeSafetyNotNull(v) ){
 65858     return SQLITE_MISUSE_BKPT;
 65860   db = v->db;
 65861   sqlite3_mutex_enter(db->mutex);
 65862   v->doingRerun = 0;
 65863   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
 65864          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
 65865          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
 65866     sqlite3_reset(pStmt);
 65867     v->doingRerun = 1;
 65868     assert( v->expired==0 );
 65870   if( rc2!=SQLITE_OK ){
 65871     /* This case occurs after failing to recompile an sql statement. 
 65872     ** The error message from the SQL compiler has already been loaded 
 65873     ** into the database handle. This block copies the error message 
 65874     ** from the database handle into the statement and sets the statement
 65875     ** program counter to 0 to ensure that when the statement is 
 65876     ** finalized or reset the parser error message is available via
 65877     ** sqlite3_errmsg() and sqlite3_errcode().
 65878     */
 65879     const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
 65880     assert( zErr!=0 || db->mallocFailed );
 65881     sqlite3DbFree(db, v->zErrMsg);
 65882     if( !db->mallocFailed ){
 65883       v->zErrMsg = sqlite3DbStrDup(db, zErr);
 65884       v->rc = rc2;
 65885     } else {
 65886       v->zErrMsg = 0;
 65887       v->rc = rc = SQLITE_NOMEM;
 65890   rc = sqlite3ApiExit(db, rc);
 65891   sqlite3_mutex_leave(db->mutex);
 65892   return rc;
 65896 /*
 65897 ** Extract the user data from a sqlite3_context structure and return a
 65898 ** pointer to it.
 65899 */
 65900 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
 65901   assert( p && p->pFunc );
 65902   return p->pFunc->pUserData;
 65905 /*
 65906 ** Extract the user data from a sqlite3_context structure and return a
 65907 ** pointer to it.
 65908 **
 65909 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
 65910 ** returns a copy of the pointer to the database connection (the 1st
 65911 ** parameter) of the sqlite3_create_function() and
 65912 ** sqlite3_create_function16() routines that originally registered the
 65913 ** application defined function.
 65914 */
 65915 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
 65916   assert( p && p->pFunc );
 65917   return p->s.db;
 65920 /*
 65921 ** Return the current time for a statement
 65922 */
 65923 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
 65924   Vdbe *v = p->pVdbe;
 65925   int rc;
 65926   if( v->iCurrentTime==0 ){
 65927     rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
 65928     if( rc ) v->iCurrentTime = 0;
 65930   return v->iCurrentTime;
 65933 /*
 65934 ** The following is the implementation of an SQL function that always
 65935 ** fails with an error message stating that the function is used in the
 65936 ** wrong context.  The sqlite3_overload_function() API might construct
 65937 ** SQL function that use this routine so that the functions will exist
 65938 ** for name resolution but are actually overloaded by the xFindFunction
 65939 ** method of virtual tables.
 65940 */
 65941 SQLITE_PRIVATE void sqlite3InvalidFunction(
 65942   sqlite3_context *context,  /* The function calling context */
 65943   int NotUsed,               /* Number of arguments to the function */
 65944   sqlite3_value **NotUsed2   /* Value of each argument */
 65945 ){
 65946   const char *zName = context->pFunc->zName;
 65947   char *zErr;
 65948   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 65949   zErr = sqlite3_mprintf(
 65950       "unable to use function %s in the requested context", zName);
 65951   sqlite3_result_error(context, zErr, -1);
 65952   sqlite3_free(zErr);
 65955 /*
 65956 ** Allocate or return the aggregate context for a user function.  A new
 65957 ** context is allocated on the first call.  Subsequent calls return the
 65958 ** same context that was returned on prior calls.
 65959 */
 65960 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
 65961   Mem *pMem;
 65962   assert( p && p->pFunc && p->pFunc->xStep );
 65963   assert( sqlite3_mutex_held(p->s.db->mutex) );
 65964   pMem = p->pMem;
 65965   testcase( nByte<0 );
 65966   if( (pMem->flags & MEM_Agg)==0 ){
 65967     if( nByte<=0 ){
 65968       sqlite3VdbeMemReleaseExternal(pMem);
 65969       pMem->flags = MEM_Null;
 65970       pMem->z = 0;
 65971     }else{
 65972       sqlite3VdbeMemGrow(pMem, nByte, 0);
 65973       pMem->flags = MEM_Agg;
 65974       pMem->u.pDef = p->pFunc;
 65975       if( pMem->z ){
 65976         memset(pMem->z, 0, nByte);
 65980   return (void*)pMem->z;
 65983 /*
 65984 ** Return the auxilary data pointer, if any, for the iArg'th argument to
 65985 ** the user-function defined by pCtx.
 65986 */
 65987 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
 65988   AuxData *pAuxData;
 65990   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 65991   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
 65992     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
 65995   return (pAuxData ? pAuxData->pAux : 0);
 65998 /*
 65999 ** Set the auxilary data pointer and delete function, for the iArg'th
 66000 ** argument to the user-function defined by pCtx. Any previous value is
 66001 ** deleted by calling the delete function specified when it was set.
 66002 */
 66003 SQLITE_API void sqlite3_set_auxdata(
 66004   sqlite3_context *pCtx, 
 66005   int iArg, 
 66006   void *pAux, 
 66007   void (*xDelete)(void*)
 66008 ){
 66009   AuxData *pAuxData;
 66010   Vdbe *pVdbe = pCtx->pVdbe;
 66012   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
 66013   if( iArg<0 ) goto failed;
 66015   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
 66016     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
 66018   if( pAuxData==0 ){
 66019     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
 66020     if( !pAuxData ) goto failed;
 66021     pAuxData->iOp = pCtx->iOp;
 66022     pAuxData->iArg = iArg;
 66023     pAuxData->pNext = pVdbe->pAuxData;
 66024     pVdbe->pAuxData = pAuxData;
 66025     if( pCtx->fErrorOrAux==0 ){
 66026       pCtx->isError = 0;
 66027       pCtx->fErrorOrAux = 1;
 66029   }else if( pAuxData->xDelete ){
 66030     pAuxData->xDelete(pAuxData->pAux);
 66033   pAuxData->pAux = pAux;
 66034   pAuxData->xDelete = xDelete;
 66035   return;
 66037 failed:
 66038   if( xDelete ){
 66039     xDelete(pAux);
 66043 #ifndef SQLITE_OMIT_DEPRECATED
 66044 /*
 66045 ** Return the number of times the Step function of a aggregate has been 
 66046 ** called.
 66047 **
 66048 ** This function is deprecated.  Do not use it for new code.  It is
 66049 ** provide only to avoid breaking legacy code.  New aggregate function
 66050 ** implementations should keep their own counts within their aggregate
 66051 ** context.
 66052 */
 66053 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
 66054   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
 66055   return p->pMem->n;
 66057 #endif
 66059 /*
 66060 ** Return the number of columns in the result set for the statement pStmt.
 66061 */
 66062 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
 66063   Vdbe *pVm = (Vdbe *)pStmt;
 66064   return pVm ? pVm->nResColumn : 0;
 66067 /*
 66068 ** Return the number of values available from the current row of the
 66069 ** currently executing statement pStmt.
 66070 */
 66071 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
 66072   Vdbe *pVm = (Vdbe *)pStmt;
 66073   if( pVm==0 || pVm->pResultSet==0 ) return 0;
 66074   return pVm->nResColumn;
 66077 /*
 66078 ** Return a pointer to static memory containing an SQL NULL value.
 66079 */
 66080 static const Mem *columnNullValue(void){
 66081   /* Even though the Mem structure contains an element
 66082   ** of type i64, on certain architectures (x86) with certain compiler
 66083   ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
 66084   ** instead of an 8-byte one. This all works fine, except that when
 66085   ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
 66086   ** that a Mem structure is located on an 8-byte boundary. To prevent
 66087   ** these assert()s from failing, when building with SQLITE_DEBUG defined
 66088   ** using gcc, we force nullMem to be 8-byte aligned using the magical
 66089   ** __attribute__((aligned(8))) macro.  */
 66090   static const Mem nullMem 
 66091 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
 66092     __attribute__((aligned(8))) 
 66093 #endif
 66094     = {0, "", (double)0, {0}, 0, MEM_Null, 0,
 66095 #ifdef SQLITE_DEBUG
 66096        0, 0,  /* pScopyFrom, pFiller */
 66097 #endif
 66098        0, 0 };
 66099   return &nullMem;
 66102 /*
 66103 ** Check to see if column iCol of the given statement is valid.  If
 66104 ** it is, return a pointer to the Mem for the value of that column.
 66105 ** If iCol is not valid, return a pointer to a Mem which has a value
 66106 ** of NULL.
 66107 */
 66108 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
 66109   Vdbe *pVm;
 66110   Mem *pOut;
 66112   pVm = (Vdbe *)pStmt;
 66113   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 66114     sqlite3_mutex_enter(pVm->db->mutex);
 66115     pOut = &pVm->pResultSet[i];
 66116   }else{
 66117     if( pVm && ALWAYS(pVm->db) ){
 66118       sqlite3_mutex_enter(pVm->db->mutex);
 66119       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 66121     pOut = (Mem*)columnNullValue();
 66123   return pOut;
 66126 /*
 66127 ** This function is called after invoking an sqlite3_value_XXX function on a 
 66128 ** column value (i.e. a value returned by evaluating an SQL expression in the
 66129 ** select list of a SELECT statement) that may cause a malloc() failure. If 
 66130 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
 66131 ** code of statement pStmt set to SQLITE_NOMEM.
 66132 **
 66133 ** Specifically, this is called from within:
 66134 **
 66135 **     sqlite3_column_int()
 66136 **     sqlite3_column_int64()
 66137 **     sqlite3_column_text()
 66138 **     sqlite3_column_text16()
 66139 **     sqlite3_column_real()
 66140 **     sqlite3_column_bytes()
 66141 **     sqlite3_column_bytes16()
 66142 **     sqiite3_column_blob()
 66143 */
 66144 static void columnMallocFailure(sqlite3_stmt *pStmt)
 66146   /* If malloc() failed during an encoding conversion within an
 66147   ** sqlite3_column_XXX API, then set the return code of the statement to
 66148   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
 66149   ** and _finalize() will return NOMEM.
 66150   */
 66151   Vdbe *p = (Vdbe *)pStmt;
 66152   if( p ){
 66153     p->rc = sqlite3ApiExit(p->db, p->rc);
 66154     sqlite3_mutex_leave(p->db->mutex);
 66158 /**************************** sqlite3_column_  *******************************
 66159 ** The following routines are used to access elements of the current row
 66160 ** in the result set.
 66161 */
 66162 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
 66163   const void *val;
 66164   val = sqlite3_value_blob( columnMem(pStmt,i) );
 66165   /* Even though there is no encoding conversion, value_blob() might
 66166   ** need to call malloc() to expand the result of a zeroblob() 
 66167   ** expression. 
 66168   */
 66169   columnMallocFailure(pStmt);
 66170   return val;
 66172 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
 66173   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
 66174   columnMallocFailure(pStmt);
 66175   return val;
 66177 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
 66178   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
 66179   columnMallocFailure(pStmt);
 66180   return val;
 66182 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
 66183   double val = sqlite3_value_double( columnMem(pStmt,i) );
 66184   columnMallocFailure(pStmt);
 66185   return val;
 66187 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
 66188   int val = sqlite3_value_int( columnMem(pStmt,i) );
 66189   columnMallocFailure(pStmt);
 66190   return val;
 66192 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
 66193   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
 66194   columnMallocFailure(pStmt);
 66195   return val;
 66197 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
 66198   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
 66199   columnMallocFailure(pStmt);
 66200   return val;
 66202 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
 66203   Mem *pOut = columnMem(pStmt, i);
 66204   if( pOut->flags&MEM_Static ){
 66205     pOut->flags &= ~MEM_Static;
 66206     pOut->flags |= MEM_Ephem;
 66208   columnMallocFailure(pStmt);
 66209   return (sqlite3_value *)pOut;
 66211 #ifndef SQLITE_OMIT_UTF16
 66212 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
 66213   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
 66214   columnMallocFailure(pStmt);
 66215   return val;
 66217 #endif /* SQLITE_OMIT_UTF16 */
 66218 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
 66219   int iType = sqlite3_value_type( columnMem(pStmt,i) );
 66220   columnMallocFailure(pStmt);
 66221   return iType;
 66224 /*
 66225 ** Convert the N-th element of pStmt->pColName[] into a string using
 66226 ** xFunc() then return that string.  If N is out of range, return 0.
 66227 **
 66228 ** There are up to 5 names for each column.  useType determines which
 66229 ** name is returned.  Here are the names:
 66230 **
 66231 **    0      The column name as it should be displayed for output
 66232 **    1      The datatype name for the column
 66233 **    2      The name of the database that the column derives from
 66234 **    3      The name of the table that the column derives from
 66235 **    4      The name of the table column that the result column derives from
 66236 **
 66237 ** If the result is not a simple column reference (if it is an expression
 66238 ** or a constant) then useTypes 2, 3, and 4 return NULL.
 66239 */
 66240 static const void *columnName(
 66241   sqlite3_stmt *pStmt,
 66242   int N,
 66243   const void *(*xFunc)(Mem*),
 66244   int useType
 66245 ){
 66246   const void *ret = 0;
 66247   Vdbe *p = (Vdbe *)pStmt;
 66248   int n;
 66249   sqlite3 *db = p->db;
 66251   assert( db!=0 );
 66252   n = sqlite3_column_count(pStmt);
 66253   if( N<n && N>=0 ){
 66254     N += useType*n;
 66255     sqlite3_mutex_enter(db->mutex);
 66256     assert( db->mallocFailed==0 );
 66257     ret = xFunc(&p->aColName[N]);
 66258      /* A malloc may have failed inside of the xFunc() call. If this
 66259     ** is the case, clear the mallocFailed flag and return NULL.
 66260     */
 66261     if( db->mallocFailed ){
 66262       db->mallocFailed = 0;
 66263       ret = 0;
 66265     sqlite3_mutex_leave(db->mutex);
 66267   return ret;
 66270 /*
 66271 ** Return the name of the Nth column of the result set returned by SQL
 66272 ** statement pStmt.
 66273 */
 66274 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
 66275   return columnName(
 66276       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
 66278 #ifndef SQLITE_OMIT_UTF16
 66279 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
 66280   return columnName(
 66281       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
 66283 #endif
 66285 /*
 66286 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
 66287 ** not define OMIT_DECLTYPE.
 66288 */
 66289 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
 66290 # error "Must not define both SQLITE_OMIT_DECLTYPE \
 66291          and SQLITE_ENABLE_COLUMN_METADATA"
 66292 #endif
 66294 #ifndef SQLITE_OMIT_DECLTYPE
 66295 /*
 66296 ** Return the column declaration type (if applicable) of the 'i'th column
 66297 ** of the result set of SQL statement pStmt.
 66298 */
 66299 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
 66300   return columnName(
 66301       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
 66303 #ifndef SQLITE_OMIT_UTF16
 66304 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
 66305   return columnName(
 66306       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
 66308 #endif /* SQLITE_OMIT_UTF16 */
 66309 #endif /* SQLITE_OMIT_DECLTYPE */
 66311 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 66312 /*
 66313 ** Return the name of the database from which a result column derives.
 66314 ** NULL is returned if the result column is an expression or constant or
 66315 ** anything else which is not an unabiguous reference to a database column.
 66316 */
 66317 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
 66318   return columnName(
 66319       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
 66321 #ifndef SQLITE_OMIT_UTF16
 66322 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
 66323   return columnName(
 66324       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
 66326 #endif /* SQLITE_OMIT_UTF16 */
 66328 /*
 66329 ** Return the name of the table from which a result column derives.
 66330 ** NULL is returned if the result column is an expression or constant or
 66331 ** anything else which is not an unabiguous reference to a database column.
 66332 */
 66333 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
 66334   return columnName(
 66335       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
 66337 #ifndef SQLITE_OMIT_UTF16
 66338 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
 66339   return columnName(
 66340       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
 66342 #endif /* SQLITE_OMIT_UTF16 */
 66344 /*
 66345 ** Return the name of the table column from which a result column derives.
 66346 ** NULL is returned if the result column is an expression or constant or
 66347 ** anything else which is not an unabiguous reference to a database column.
 66348 */
 66349 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
 66350   return columnName(
 66351       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
 66353 #ifndef SQLITE_OMIT_UTF16
 66354 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
 66355   return columnName(
 66356       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
 66358 #endif /* SQLITE_OMIT_UTF16 */
 66359 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
 66362 /******************************* sqlite3_bind_  ***************************
 66363 ** 
 66364 ** Routines used to attach values to wildcards in a compiled SQL statement.
 66365 */
 66366 /*
 66367 ** Unbind the value bound to variable i in virtual machine p. This is the 
 66368 ** the same as binding a NULL value to the column. If the "i" parameter is
 66369 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
 66370 **
 66371 ** A successful evaluation of this routine acquires the mutex on p.
 66372 ** the mutex is released if any kind of error occurs.
 66373 **
 66374 ** The error code stored in database p->db is overwritten with the return
 66375 ** value in any case.
 66376 */
 66377 static int vdbeUnbind(Vdbe *p, int i){
 66378   Mem *pVar;
 66379   if( vdbeSafetyNotNull(p) ){
 66380     return SQLITE_MISUSE_BKPT;
 66382   sqlite3_mutex_enter(p->db->mutex);
 66383   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
 66384     sqlite3Error(p->db, SQLITE_MISUSE, 0);
 66385     sqlite3_mutex_leave(p->db->mutex);
 66386     sqlite3_log(SQLITE_MISUSE, 
 66387         "bind on a busy prepared statement: [%s]", p->zSql);
 66388     return SQLITE_MISUSE_BKPT;
 66390   if( i<1 || i>p->nVar ){
 66391     sqlite3Error(p->db, SQLITE_RANGE, 0);
 66392     sqlite3_mutex_leave(p->db->mutex);
 66393     return SQLITE_RANGE;
 66395   i--;
 66396   pVar = &p->aVar[i];
 66397   sqlite3VdbeMemRelease(pVar);
 66398   pVar->flags = MEM_Null;
 66399   sqlite3Error(p->db, SQLITE_OK, 0);
 66401   /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
 66402   ** binding a new value to this variable invalidates the current query plan.
 66403   **
 66404   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
 66405   ** parameter in the WHERE clause might influence the choice of query plan
 66406   ** for a statement, then the statement will be automatically recompiled,
 66407   ** as if there had been a schema change, on the first sqlite3_step() call
 66408   ** following any change to the bindings of that parameter.
 66409   */
 66410   if( p->isPrepareV2 &&
 66411      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
 66412   ){
 66413     p->expired = 1;
 66415   return SQLITE_OK;
 66418 /*
 66419 ** Bind a text or BLOB value.
 66420 */
 66421 static int bindText(
 66422   sqlite3_stmt *pStmt,   /* The statement to bind against */
 66423   int i,                 /* Index of the parameter to bind */
 66424   const void *zData,     /* Pointer to the data to be bound */
 66425   int nData,             /* Number of bytes of data to be bound */
 66426   void (*xDel)(void*),   /* Destructor for the data */
 66427   u8 encoding            /* Encoding for the data */
 66428 ){
 66429   Vdbe *p = (Vdbe *)pStmt;
 66430   Mem *pVar;
 66431   int rc;
 66433   rc = vdbeUnbind(p, i);
 66434   if( rc==SQLITE_OK ){
 66435     if( zData!=0 ){
 66436       pVar = &p->aVar[i-1];
 66437       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
 66438       if( rc==SQLITE_OK && encoding!=0 ){
 66439         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
 66441       sqlite3Error(p->db, rc, 0);
 66442       rc = sqlite3ApiExit(p->db, rc);
 66444     sqlite3_mutex_leave(p->db->mutex);
 66445   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
 66446     xDel((void*)zData);
 66448   return rc;
 66452 /*
 66453 ** Bind a blob value to an SQL statement variable.
 66454 */
 66455 SQLITE_API int sqlite3_bind_blob(
 66456   sqlite3_stmt *pStmt, 
 66457   int i, 
 66458   const void *zData, 
 66459   int nData, 
 66460   void (*xDel)(void*)
 66461 ){
 66462   return bindText(pStmt, i, zData, nData, xDel, 0);
 66464 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
 66465   int rc;
 66466   Vdbe *p = (Vdbe *)pStmt;
 66467   rc = vdbeUnbind(p, i);
 66468   if( rc==SQLITE_OK ){
 66469     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
 66470     sqlite3_mutex_leave(p->db->mutex);
 66472   return rc;
 66474 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
 66475   return sqlite3_bind_int64(p, i, (i64)iValue);
 66477 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
 66478   int rc;
 66479   Vdbe *p = (Vdbe *)pStmt;
 66480   rc = vdbeUnbind(p, i);
 66481   if( rc==SQLITE_OK ){
 66482     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
 66483     sqlite3_mutex_leave(p->db->mutex);
 66485   return rc;
 66487 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
 66488   int rc;
 66489   Vdbe *p = (Vdbe*)pStmt;
 66490   rc = vdbeUnbind(p, i);
 66491   if( rc==SQLITE_OK ){
 66492     sqlite3_mutex_leave(p->db->mutex);
 66494   return rc;
 66496 SQLITE_API int sqlite3_bind_text( 
 66497   sqlite3_stmt *pStmt, 
 66498   int i, 
 66499   const char *zData, 
 66500   int nData, 
 66501   void (*xDel)(void*)
 66502 ){
 66503   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
 66505 #ifndef SQLITE_OMIT_UTF16
 66506 SQLITE_API int sqlite3_bind_text16(
 66507   sqlite3_stmt *pStmt, 
 66508   int i, 
 66509   const void *zData, 
 66510   int nData, 
 66511   void (*xDel)(void*)
 66512 ){
 66513   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
 66515 #endif /* SQLITE_OMIT_UTF16 */
 66516 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
 66517   int rc;
 66518   switch( sqlite3_value_type((sqlite3_value*)pValue) ){
 66519     case SQLITE_INTEGER: {
 66520       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
 66521       break;
 66523     case SQLITE_FLOAT: {
 66524       rc = sqlite3_bind_double(pStmt, i, pValue->r);
 66525       break;
 66527     case SQLITE_BLOB: {
 66528       if( pValue->flags & MEM_Zero ){
 66529         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
 66530       }else{
 66531         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
 66533       break;
 66535     case SQLITE_TEXT: {
 66536       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
 66537                               pValue->enc);
 66538       break;
 66540     default: {
 66541       rc = sqlite3_bind_null(pStmt, i);
 66542       break;
 66545   return rc;
 66547 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
 66548   int rc;
 66549   Vdbe *p = (Vdbe *)pStmt;
 66550   rc = vdbeUnbind(p, i);
 66551   if( rc==SQLITE_OK ){
 66552     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
 66553     sqlite3_mutex_leave(p->db->mutex);
 66555   return rc;
 66558 /*
 66559 ** Return the number of wildcards that can be potentially bound to.
 66560 ** This routine is added to support DBD::SQLite.  
 66561 */
 66562 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
 66563   Vdbe *p = (Vdbe*)pStmt;
 66564   return p ? p->nVar : 0;
 66567 /*
 66568 ** Return the name of a wildcard parameter.  Return NULL if the index
 66569 ** is out of range or if the wildcard is unnamed.
 66570 **
 66571 ** The result is always UTF-8.
 66572 */
 66573 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
 66574   Vdbe *p = (Vdbe*)pStmt;
 66575   if( p==0 || i<1 || i>p->nzVar ){
 66576     return 0;
 66578   return p->azVar[i-1];
 66581 /*
 66582 ** Given a wildcard parameter name, return the index of the variable
 66583 ** with that name.  If there is no variable with the given name,
 66584 ** return 0.
 66585 */
 66586 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
 66587   int i;
 66588   if( p==0 ){
 66589     return 0;
 66591   if( zName ){
 66592     for(i=0; i<p->nzVar; i++){
 66593       const char *z = p->azVar[i];
 66594       if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
 66595         return i+1;
 66599   return 0;
 66601 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
 66602   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
 66605 /*
 66606 ** Transfer all bindings from the first statement over to the second.
 66607 */
 66608 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 66609   Vdbe *pFrom = (Vdbe*)pFromStmt;
 66610   Vdbe *pTo = (Vdbe*)pToStmt;
 66611   int i;
 66612   assert( pTo->db==pFrom->db );
 66613   assert( pTo->nVar==pFrom->nVar );
 66614   sqlite3_mutex_enter(pTo->db->mutex);
 66615   for(i=0; i<pFrom->nVar; i++){
 66616     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
 66618   sqlite3_mutex_leave(pTo->db->mutex);
 66619   return SQLITE_OK;
 66622 #ifndef SQLITE_OMIT_DEPRECATED
 66623 /*
 66624 ** Deprecated external interface.  Internal/core SQLite code
 66625 ** should call sqlite3TransferBindings.
 66626 **
 66627 ** Is is misuse to call this routine with statements from different
 66628 ** database connections.  But as this is a deprecated interface, we
 66629 ** will not bother to check for that condition.
 66630 **
 66631 ** If the two statements contain a different number of bindings, then
 66632 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
 66633 ** SQLITE_OK is returned.
 66634 */
 66635 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
 66636   Vdbe *pFrom = (Vdbe*)pFromStmt;
 66637   Vdbe *pTo = (Vdbe*)pToStmt;
 66638   if( pFrom->nVar!=pTo->nVar ){
 66639     return SQLITE_ERROR;
 66641   if( pTo->isPrepareV2 && pTo->expmask ){
 66642     pTo->expired = 1;
 66644   if( pFrom->isPrepareV2 && pFrom->expmask ){
 66645     pFrom->expired = 1;
 66647   return sqlite3TransferBindings(pFromStmt, pToStmt);
 66649 #endif
 66651 /*
 66652 ** Return the sqlite3* database handle to which the prepared statement given
 66653 ** in the argument belongs.  This is the same database handle that was
 66654 ** the first argument to the sqlite3_prepare() that was used to create
 66655 ** the statement in the first place.
 66656 */
 66657 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
 66658   return pStmt ? ((Vdbe*)pStmt)->db : 0;
 66661 /*
 66662 ** Return true if the prepared statement is guaranteed to not modify the
 66663 ** database.
 66664 */
 66665 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
 66666   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
 66669 /*
 66670 ** Return true if the prepared statement is in need of being reset.
 66671 */
 66672 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
 66673   Vdbe *v = (Vdbe*)pStmt;
 66674   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
 66677 /*
 66678 ** Return a pointer to the next prepared statement after pStmt associated
 66679 ** with database connection pDb.  If pStmt is NULL, return the first
 66680 ** prepared statement for the database connection.  Return NULL if there
 66681 ** are no more.
 66682 */
 66683 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
 66684   sqlite3_stmt *pNext;
 66685   sqlite3_mutex_enter(pDb->mutex);
 66686   if( pStmt==0 ){
 66687     pNext = (sqlite3_stmt*)pDb->pVdbe;
 66688   }else{
 66689     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
 66691   sqlite3_mutex_leave(pDb->mutex);
 66692   return pNext;
 66695 /*
 66696 ** Return the value of a status counter for a prepared statement
 66697 */
 66698 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
 66699   Vdbe *pVdbe = (Vdbe*)pStmt;
 66700   u32 v = pVdbe->aCounter[op];
 66701   if( resetFlag ) pVdbe->aCounter[op] = 0;
 66702   return (int)v;
 66705 /************** End of vdbeapi.c *********************************************/
 66706 /************** Begin file vdbetrace.c ***************************************/
 66707 /*
 66708 ** 2009 November 25
 66709 **
 66710 ** The author disclaims copyright to this source code.  In place of
 66711 ** a legal notice, here is a blessing:
 66712 **
 66713 **    May you do good and not evil.
 66714 **    May you find forgiveness for yourself and forgive others.
 66715 **    May you share freely, never taking more than you give.
 66716 **
 66717 *************************************************************************
 66718 **
 66719 ** This file contains code used to insert the values of host parameters
 66720 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
 66721 **
 66722 ** The Vdbe parse-tree explainer is also found here.
 66723 */
 66725 #ifndef SQLITE_OMIT_TRACE
 66727 /*
 66728 ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
 66729 ** bytes in this text up to but excluding the first character in
 66730 ** a host parameter.  If the text contains no host parameters, return
 66731 ** the total number of bytes in the text.
 66732 */
 66733 static int findNextHostParameter(const char *zSql, int *pnToken){
 66734   int tokenType;
 66735   int nTotal = 0;
 66736   int n;
 66738   *pnToken = 0;
 66739   while( zSql[0] ){
 66740     n = sqlite3GetToken((u8*)zSql, &tokenType);
 66741     assert( n>0 && tokenType!=TK_ILLEGAL );
 66742     if( tokenType==TK_VARIABLE ){
 66743       *pnToken = n;
 66744       break;
 66746     nTotal += n;
 66747     zSql += n;
 66749   return nTotal;
 66752 /*
 66753 ** This function returns a pointer to a nul-terminated string in memory
 66754 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
 66755 ** string contains a copy of zRawSql but with host parameters expanded to 
 66756 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1, 
 66757 ** then the returned string holds a copy of zRawSql with "-- " prepended
 66758 ** to each line of text.
 66759 **
 66760 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
 66761 ** then long strings and blobs are truncated to that many bytes.  This
 66762 ** can be used to prevent unreasonably large trace strings when dealing
 66763 ** with large (multi-megabyte) strings and blobs.
 66764 **
 66765 ** The calling function is responsible for making sure the memory returned
 66766 ** is eventually freed.
 66767 **
 66768 ** ALGORITHM:  Scan the input string looking for host parameters in any of
 66769 ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
 66770 ** string literals, quoted identifier names, and comments.  For text forms,
 66771 ** the host parameter index is found by scanning the perpared
 66772 ** statement for the corresponding OP_Variable opcode.  Once the host
 66773 ** parameter index is known, locate the value in p->aVar[].  Then render
 66774 ** the value as a literal in place of the host parameter name.
 66775 */
 66776 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
 66777   Vdbe *p,                 /* The prepared statement being evaluated */
 66778   const char *zRawSql      /* Raw text of the SQL statement */
 66779 ){
 66780   sqlite3 *db;             /* The database connection */
 66781   int idx = 0;             /* Index of a host parameter */
 66782   int nextIndex = 1;       /* Index of next ? host parameter */
 66783   int n;                   /* Length of a token prefix */
 66784   int nToken;              /* Length of the parameter token */
 66785   int i;                   /* Loop counter */
 66786   Mem *pVar;               /* Value of a host parameter */
 66787   StrAccum out;            /* Accumulate the output here */
 66788   char zBase[100];         /* Initial working space */
 66790   db = p->db;
 66791   sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
 66792                       db->aLimit[SQLITE_LIMIT_LENGTH]);
 66793   out.db = db;
 66794   if( db->nVdbeExec>1 ){
 66795     while( *zRawSql ){
 66796       const char *zStart = zRawSql;
 66797       while( *(zRawSql++)!='\n' && *zRawSql );
 66798       sqlite3StrAccumAppend(&out, "-- ", 3);
 66799       assert( (zRawSql - zStart) > 0 );
 66800       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
 66802   }else{
 66803     while( zRawSql[0] ){
 66804       n = findNextHostParameter(zRawSql, &nToken);
 66805       assert( n>0 );
 66806       sqlite3StrAccumAppend(&out, zRawSql, n);
 66807       zRawSql += n;
 66808       assert( zRawSql[0] || nToken==0 );
 66809       if( nToken==0 ) break;
 66810       if( zRawSql[0]=='?' ){
 66811         if( nToken>1 ){
 66812           assert( sqlite3Isdigit(zRawSql[1]) );
 66813           sqlite3GetInt32(&zRawSql[1], &idx);
 66814         }else{
 66815           idx = nextIndex;
 66817       }else{
 66818         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
 66819         testcase( zRawSql[0]==':' );
 66820         testcase( zRawSql[0]=='$' );
 66821         testcase( zRawSql[0]=='@' );
 66822         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
 66823         assert( idx>0 );
 66825       zRawSql += nToken;
 66826       nextIndex = idx + 1;
 66827       assert( idx>0 && idx<=p->nVar );
 66828       pVar = &p->aVar[idx-1];
 66829       if( pVar->flags & MEM_Null ){
 66830         sqlite3StrAccumAppend(&out, "NULL", 4);
 66831       }else if( pVar->flags & MEM_Int ){
 66832         sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
 66833       }else if( pVar->flags & MEM_Real ){
 66834         sqlite3XPrintf(&out, 0, "%!.15g", pVar->r);
 66835       }else if( pVar->flags & MEM_Str ){
 66836         int nOut;  /* Number of bytes of the string text to include in output */
 66837 #ifndef SQLITE_OMIT_UTF16
 66838         u8 enc = ENC(db);
 66839         Mem utf8;
 66840         if( enc!=SQLITE_UTF8 ){
 66841           memset(&utf8, 0, sizeof(utf8));
 66842           utf8.db = db;
 66843           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
 66844           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
 66845           pVar = &utf8;
 66847 #endif
 66848         nOut = pVar->n;
 66849 #ifdef SQLITE_TRACE_SIZE_LIMIT
 66850         if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
 66851           nOut = SQLITE_TRACE_SIZE_LIMIT;
 66852           while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
 66854 #endif    
 66855         sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
 66856 #ifdef SQLITE_TRACE_SIZE_LIMIT
 66857         if( nOut<pVar->n ){
 66858           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
 66860 #endif
 66861 #ifndef SQLITE_OMIT_UTF16
 66862         if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
 66863 #endif
 66864       }else if( pVar->flags & MEM_Zero ){
 66865         sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
 66866       }else{
 66867         int nOut;  /* Number of bytes of the blob to include in output */
 66868         assert( pVar->flags & MEM_Blob );
 66869         sqlite3StrAccumAppend(&out, "x'", 2);
 66870         nOut = pVar->n;
 66871 #ifdef SQLITE_TRACE_SIZE_LIMIT
 66872         if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
 66873 #endif
 66874         for(i=0; i<nOut; i++){
 66875           sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
 66877         sqlite3StrAccumAppend(&out, "'", 1);
 66878 #ifdef SQLITE_TRACE_SIZE_LIMIT
 66879         if( nOut<pVar->n ){
 66880           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
 66882 #endif
 66886   return sqlite3StrAccumFinish(&out);
 66889 #endif /* #ifndef SQLITE_OMIT_TRACE */
 66891 /*****************************************************************************
 66892 ** The following code implements the data-structure explaining logic
 66893 ** for the Vdbe.
 66894 */
 66896 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 66898 /*
 66899 ** Allocate a new Explain object
 66900 */
 66901 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
 66902   if( pVdbe ){
 66903     Explain *p;
 66904     sqlite3BeginBenignMalloc();
 66905     p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
 66906     if( p ){
 66907       p->pVdbe = pVdbe;
 66908       sqlite3_free(pVdbe->pExplain);
 66909       pVdbe->pExplain = p;
 66910       sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
 66911                           SQLITE_MAX_LENGTH);
 66912       p->str.useMalloc = 2;
 66913     }else{
 66914       sqlite3EndBenignMalloc();
 66919 /*
 66920 ** Return true if the Explain ends with a new-line.
 66921 */
 66922 static int endsWithNL(Explain *p){
 66923   return p && p->str.zText && p->str.nChar
 66924            && p->str.zText[p->str.nChar-1]=='\n';
 66927 /*
 66928 ** Append text to the indentation
 66929 */
 66930 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
 66931   Explain *p;
 66932   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 66933     va_list ap;
 66934     if( p->nIndent && endsWithNL(p) ){
 66935       int n = p->nIndent;
 66936       if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
 66937       sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
 66939     va_start(ap, zFormat);
 66940     sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap);
 66941     va_end(ap);
 66945 /*
 66946 ** Append a '\n' if there is not already one.
 66947 */
 66948 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
 66949   Explain *p;
 66950   if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
 66951     sqlite3StrAccumAppend(&p->str, "\n", 1);
 66955 /*
 66956 ** Push a new indentation level.  Subsequent lines will be indented
 66957 ** so that they begin at the current cursor position.
 66958 */
 66959 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
 66960   Explain *p;
 66961   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
 66962     if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
 66963       const char *z = p->str.zText;
 66964       int i = p->str.nChar-1;
 66965       int x;
 66966       while( i>=0 && z[i]!='\n' ){ i--; }
 66967       x = (p->str.nChar - 1) - i;
 66968       if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
 66969         x = p->aIndent[p->nIndent-1];
 66971       p->aIndent[p->nIndent] = x;
 66973     p->nIndent++;
 66977 /*
 66978 ** Pop the indentation stack by one level.
 66979 */
 66980 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
 66981   if( p && p->pExplain ) p->pExplain->nIndent--;
 66984 /*
 66985 ** Free the indentation structure
 66986 */
 66987 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
 66988   if( pVdbe && pVdbe->pExplain ){
 66989     sqlite3_free(pVdbe->zExplain);
 66990     sqlite3ExplainNL(pVdbe);
 66991     pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
 66992     sqlite3_free(pVdbe->pExplain);
 66993     pVdbe->pExplain = 0;
 66994     sqlite3EndBenignMalloc();
 66998 /*
 66999 ** Return the explanation of a virtual machine.
 67000 */
 67001 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
 67002   return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
 67004 #endif /* defined(SQLITE_DEBUG) */
 67006 /************** End of vdbetrace.c *******************************************/
 67007 /************** Begin file vdbe.c ********************************************/
 67008 /*
 67009 ** 2001 September 15
 67010 **
 67011 ** The author disclaims copyright to this source code.  In place of
 67012 ** a legal notice, here is a blessing:
 67013 **
 67014 **    May you do good and not evil.
 67015 **    May you find forgiveness for yourself and forgive others.
 67016 **    May you share freely, never taking more than you give.
 67017 **
 67018 *************************************************************************
 67019 ** The code in this file implements the function that runs the
 67020 ** bytecode of a prepared statement.
 67021 **
 67022 ** Various scripts scan this source file in order to generate HTML
 67023 ** documentation, headers files, or other derived files.  The formatting
 67024 ** of the code in this file is, therefore, important.  See other comments
 67025 ** in this file for details.  If in doubt, do not deviate from existing
 67026 ** commenting and indentation practices when changing or adding code.
 67027 */
 67029 /*
 67030 ** Invoke this macro on memory cells just prior to changing the
 67031 ** value of the cell.  This macro verifies that shallow copies are
 67032 ** not misused.  A shallow copy of a string or blob just copies a
 67033 ** pointer to the string or blob, not the content.  If the original
 67034 ** is changed while the copy is still in use, the string or blob might
 67035 ** be changed out from under the copy.  This macro verifies that nothing
 67036 ** like that ever happens.
 67037 */
 67038 #ifdef SQLITE_DEBUG
 67039 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
 67040 #else
 67041 # define memAboutToChange(P,M)
 67042 #endif
 67044 /*
 67045 ** The following global variable is incremented every time a cursor
 67046 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
 67047 ** procedures use this information to make sure that indices are
 67048 ** working correctly.  This variable has no function other than to
 67049 ** help verify the correct operation of the library.
 67050 */
 67051 #ifdef SQLITE_TEST
 67052 SQLITE_API int sqlite3_search_count = 0;
 67053 #endif
 67055 /*
 67056 ** When this global variable is positive, it gets decremented once before
 67057 ** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
 67058 ** field of the sqlite3 structure is set in order to simulate an interrupt.
 67059 **
 67060 ** This facility is used for testing purposes only.  It does not function
 67061 ** in an ordinary build.
 67062 */
 67063 #ifdef SQLITE_TEST
 67064 SQLITE_API int sqlite3_interrupt_count = 0;
 67065 #endif
 67067 /*
 67068 ** The next global variable is incremented each type the OP_Sort opcode
 67069 ** is executed.  The test procedures use this information to make sure that
 67070 ** sorting is occurring or not occurring at appropriate times.   This variable
 67071 ** has no function other than to help verify the correct operation of the
 67072 ** library.
 67073 */
 67074 #ifdef SQLITE_TEST
 67075 SQLITE_API int sqlite3_sort_count = 0;
 67076 #endif
 67078 /*
 67079 ** The next global variable records the size of the largest MEM_Blob
 67080 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
 67081 ** use this information to make sure that the zero-blob functionality
 67082 ** is working correctly.   This variable has no function other than to
 67083 ** help verify the correct operation of the library.
 67084 */
 67085 #ifdef SQLITE_TEST
 67086 SQLITE_API int sqlite3_max_blobsize = 0;
 67087 static void updateMaxBlobsize(Mem *p){
 67088   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
 67089     sqlite3_max_blobsize = p->n;
 67092 #endif
 67094 /*
 67095 ** The next global variable is incremented each time the OP_Found opcode
 67096 ** is executed. This is used to test whether or not the foreign key
 67097 ** operation implemented using OP_FkIsZero is working. This variable
 67098 ** has no function other than to help verify the correct operation of the
 67099 ** library.
 67100 */
 67101 #ifdef SQLITE_TEST
 67102 SQLITE_API int sqlite3_found_count = 0;
 67103 #endif
 67105 /*
 67106 ** Test a register to see if it exceeds the current maximum blob size.
 67107 ** If it does, record the new maximum blob size.
 67108 */
 67109 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
 67110 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
 67111 #else
 67112 # define UPDATE_MAX_BLOBSIZE(P)
 67113 #endif
 67115 /*
 67116 ** Invoke the VDBE coverage callback, if that callback is defined.  This
 67117 ** feature is used for test suite validation only and does not appear an
 67118 ** production builds.
 67119 **
 67120 ** M is an integer, 2 or 3, that indices how many different ways the
 67121 ** branch can go.  It is usually 2.  "I" is the direction the branch
 67122 ** goes.  0 means falls through.  1 means branch is taken.  2 means the
 67123 ** second alternative branch is taken.
 67124 */
 67125 #if !defined(SQLITE_VDBE_COVERAGE)
 67126 # define VdbeBranchTaken(I,M)
 67127 #else
 67128 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
 67129   static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
 67130     if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
 67131       M = iSrcLine;
 67132       /* Assert the truth of VdbeCoverageAlwaysTaken() and 
 67133       ** VdbeCoverageNeverTaken() */
 67134       assert( (M & I)==I );
 67135     }else{
 67136       if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
 67137       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
 67138                                       iSrcLine,I,M);
 67141 #endif
 67143 /*
 67144 ** Convert the given register into a string if it isn't one
 67145 ** already. Return non-zero if a malloc() fails.
 67146 */
 67147 #define Stringify(P, enc) \
 67148    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
 67149      { goto no_mem; }
 67151 /*
 67152 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
 67153 ** a pointer to a dynamically allocated string where some other entity
 67154 ** is responsible for deallocating that string.  Because the register
 67155 ** does not control the string, it might be deleted without the register
 67156 ** knowing it.
 67157 **
 67158 ** This routine converts an ephemeral string into a dynamically allocated
 67159 ** string that the register itself controls.  In other words, it
 67160 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
 67161 */
 67162 #define Deephemeralize(P) \
 67163    if( ((P)->flags&MEM_Ephem)!=0 \
 67164        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
 67166 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
 67167 #define isSorter(x) ((x)->pSorter!=0)
 67169 /*
 67170 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
 67171 ** if we run out of memory.
 67172 */
 67173 static VdbeCursor *allocateCursor(
 67174   Vdbe *p,              /* The virtual machine */
 67175   int iCur,             /* Index of the new VdbeCursor */
 67176   int nField,           /* Number of fields in the table or index */
 67177   int iDb,              /* Database the cursor belongs to, or -1 */
 67178   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
 67179 ){
 67180   /* Find the memory cell that will be used to store the blob of memory
 67181   ** required for this VdbeCursor structure. It is convenient to use a 
 67182   ** vdbe memory cell to manage the memory allocation required for a
 67183   ** VdbeCursor structure for the following reasons:
 67184   **
 67185   **   * Sometimes cursor numbers are used for a couple of different
 67186   **     purposes in a vdbe program. The different uses might require
 67187   **     different sized allocations. Memory cells provide growable
 67188   **     allocations.
 67189   **
 67190   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
 67191   **     be freed lazily via the sqlite3_release_memory() API. This
 67192   **     minimizes the number of malloc calls made by the system.
 67193   **
 67194   ** Memory cells for cursors are allocated at the top of the address
 67195   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
 67196   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
 67197   */
 67198   Mem *pMem = &p->aMem[p->nMem-iCur];
 67200   int nByte;
 67201   VdbeCursor *pCx = 0;
 67202   nByte = 
 67203       ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 
 67204       (isBtreeCursor?sqlite3BtreeCursorSize():0);
 67206   assert( iCur<p->nCursor );
 67207   if( p->apCsr[iCur] ){
 67208     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
 67209     p->apCsr[iCur] = 0;
 67211   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
 67212     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
 67213     memset(pCx, 0, sizeof(VdbeCursor));
 67214     pCx->iDb = iDb;
 67215     pCx->nField = nField;
 67216     if( isBtreeCursor ){
 67217       pCx->pCursor = (BtCursor*)
 67218           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
 67219       sqlite3BtreeCursorZero(pCx->pCursor);
 67222   return pCx;
 67225 /*
 67226 ** Try to convert a value into a numeric representation if we can
 67227 ** do so without loss of information.  In other words, if the string
 67228 ** looks like a number, convert it into a number.  If it does not
 67229 ** look like a number, leave it alone.
 67230 */
 67231 static void applyNumericAffinity(Mem *pRec){
 67232   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
 67233     double rValue;
 67234     i64 iValue;
 67235     u8 enc = pRec->enc;
 67236     if( (pRec->flags&MEM_Str)==0 ) return;
 67237     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
 67238     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
 67239       pRec->u.i = iValue;
 67240       pRec->flags |= MEM_Int;
 67241     }else{
 67242       pRec->r = rValue;
 67243       pRec->flags |= MEM_Real;
 67248 /*
 67249 ** Processing is determine by the affinity parameter:
 67250 **
 67251 ** SQLITE_AFF_INTEGER:
 67252 ** SQLITE_AFF_REAL:
 67253 ** SQLITE_AFF_NUMERIC:
 67254 **    Try to convert pRec to an integer representation or a 
 67255 **    floating-point representation if an integer representation
 67256 **    is not possible.  Note that the integer representation is
 67257 **    always preferred, even if the affinity is REAL, because
 67258 **    an integer representation is more space efficient on disk.
 67259 **
 67260 ** SQLITE_AFF_TEXT:
 67261 **    Convert pRec to a text representation.
 67262 **
 67263 ** SQLITE_AFF_NONE:
 67264 **    No-op.  pRec is unchanged.
 67265 */
 67266 static void applyAffinity(
 67267   Mem *pRec,          /* The value to apply affinity to */
 67268   char affinity,      /* The affinity to be applied */
 67269   u8 enc              /* Use this text encoding */
 67270 ){
 67271   if( affinity==SQLITE_AFF_TEXT ){
 67272     /* Only attempt the conversion to TEXT if there is an integer or real
 67273     ** representation (blob and NULL do not get converted) but no string
 67274     ** representation.
 67275     */
 67276     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
 67277       sqlite3VdbeMemStringify(pRec, enc);
 67279     pRec->flags &= ~(MEM_Real|MEM_Int);
 67280   }else if( affinity!=SQLITE_AFF_NONE ){
 67281     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
 67282              || affinity==SQLITE_AFF_NUMERIC );
 67283     applyNumericAffinity(pRec);
 67284     if( pRec->flags & MEM_Real ){
 67285       sqlite3VdbeIntegerAffinity(pRec);
 67290 /*
 67291 ** Try to convert the type of a function argument or a result column
 67292 ** into a numeric representation.  Use either INTEGER or REAL whichever
 67293 ** is appropriate.  But only do the conversion if it is possible without
 67294 ** loss of information and return the revised type of the argument.
 67295 */
 67296 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
 67297   int eType = sqlite3_value_type(pVal);
 67298   if( eType==SQLITE_TEXT ){
 67299     Mem *pMem = (Mem*)pVal;
 67300     applyNumericAffinity(pMem);
 67301     eType = sqlite3_value_type(pVal);
 67303   return eType;
 67306 /*
 67307 ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
 67308 ** not the internal Mem* type.
 67309 */
 67310 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
 67311   sqlite3_value *pVal, 
 67312   u8 affinity, 
 67313   u8 enc
 67314 ){
 67315   applyAffinity((Mem *)pVal, affinity, enc);
 67318 #ifdef SQLITE_DEBUG
 67319 /*
 67320 ** Write a nice string representation of the contents of cell pMem
 67321 ** into buffer zBuf, length nBuf.
 67322 */
 67323 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
 67324   char *zCsr = zBuf;
 67325   int f = pMem->flags;
 67327   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
 67329   if( f&MEM_Blob ){
 67330     int i;
 67331     char c;
 67332     if( f & MEM_Dyn ){
 67333       c = 'z';
 67334       assert( (f & (MEM_Static|MEM_Ephem))==0 );
 67335     }else if( f & MEM_Static ){
 67336       c = 't';
 67337       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 67338     }else if( f & MEM_Ephem ){
 67339       c = 'e';
 67340       assert( (f & (MEM_Static|MEM_Dyn))==0 );
 67341     }else{
 67342       c = 's';
 67345     sqlite3_snprintf(100, zCsr, "%c", c);
 67346     zCsr += sqlite3Strlen30(zCsr);
 67347     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
 67348     zCsr += sqlite3Strlen30(zCsr);
 67349     for(i=0; i<16 && i<pMem->n; i++){
 67350       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
 67351       zCsr += sqlite3Strlen30(zCsr);
 67353     for(i=0; i<16 && i<pMem->n; i++){
 67354       char z = pMem->z[i];
 67355       if( z<32 || z>126 ) *zCsr++ = '.';
 67356       else *zCsr++ = z;
 67359     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
 67360     zCsr += sqlite3Strlen30(zCsr);
 67361     if( f & MEM_Zero ){
 67362       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
 67363       zCsr += sqlite3Strlen30(zCsr);
 67365     *zCsr = '\0';
 67366   }else if( f & MEM_Str ){
 67367     int j, k;
 67368     zBuf[0] = ' ';
 67369     if( f & MEM_Dyn ){
 67370       zBuf[1] = 'z';
 67371       assert( (f & (MEM_Static|MEM_Ephem))==0 );
 67372     }else if( f & MEM_Static ){
 67373       zBuf[1] = 't';
 67374       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
 67375     }else if( f & MEM_Ephem ){
 67376       zBuf[1] = 'e';
 67377       assert( (f & (MEM_Static|MEM_Dyn))==0 );
 67378     }else{
 67379       zBuf[1] = 's';
 67381     k = 2;
 67382     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
 67383     k += sqlite3Strlen30(&zBuf[k]);
 67384     zBuf[k++] = '[';
 67385     for(j=0; j<15 && j<pMem->n; j++){
 67386       u8 c = pMem->z[j];
 67387       if( c>=0x20 && c<0x7f ){
 67388         zBuf[k++] = c;
 67389       }else{
 67390         zBuf[k++] = '.';
 67393     zBuf[k++] = ']';
 67394     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
 67395     k += sqlite3Strlen30(&zBuf[k]);
 67396     zBuf[k++] = 0;
 67399 #endif
 67401 #ifdef SQLITE_DEBUG
 67402 /*
 67403 ** Print the value of a register for tracing purposes:
 67404 */
 67405 static void memTracePrint(Mem *p){
 67406   if( p->flags & MEM_Undefined ){
 67407     printf(" undefined");
 67408   }else if( p->flags & MEM_Null ){
 67409     printf(" NULL");
 67410   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
 67411     printf(" si:%lld", p->u.i);
 67412   }else if( p->flags & MEM_Int ){
 67413     printf(" i:%lld", p->u.i);
 67414 #ifndef SQLITE_OMIT_FLOATING_POINT
 67415   }else if( p->flags & MEM_Real ){
 67416     printf(" r:%g", p->r);
 67417 #endif
 67418   }else if( p->flags & MEM_RowSet ){
 67419     printf(" (rowset)");
 67420   }else{
 67421     char zBuf[200];
 67422     sqlite3VdbeMemPrettyPrint(p, zBuf);
 67423     printf(" %s", zBuf);
 67426 static void registerTrace(int iReg, Mem *p){
 67427   printf("REG[%d] = ", iReg);
 67428   memTracePrint(p);
 67429   printf("\n");
 67431 #endif
 67433 #ifdef SQLITE_DEBUG
 67434 #  define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
 67435 #else
 67436 #  define REGISTER_TRACE(R,M)
 67437 #endif
 67440 #ifdef VDBE_PROFILE
 67442 /* 
 67443 ** hwtime.h contains inline assembler code for implementing 
 67444 ** high-performance timing routines.
 67445 */
 67446 /************** Include hwtime.h in the middle of vdbe.c *********************/
 67447 /************** Begin file hwtime.h ******************************************/
 67448 /*
 67449 ** 2008 May 27
 67450 **
 67451 ** The author disclaims copyright to this source code.  In place of
 67452 ** a legal notice, here is a blessing:
 67453 **
 67454 **    May you do good and not evil.
 67455 **    May you find forgiveness for yourself and forgive others.
 67456 **    May you share freely, never taking more than you give.
 67457 **
 67458 ******************************************************************************
 67459 **
 67460 ** This file contains inline asm code for retrieving "high-performance"
 67461 ** counters for x86 class CPUs.
 67462 */
 67463 #ifndef _HWTIME_H_
 67464 #define _HWTIME_H_
 67466 /*
 67467 ** The following routine only works on pentium-class (or newer) processors.
 67468 ** It uses the RDTSC opcode to read the cycle count value out of the
 67469 ** processor and returns that value.  This can be used for high-res
 67470 ** profiling.
 67471 */
 67472 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
 67473       (defined(i386) || defined(__i386__) || defined(_M_IX86))
 67475   #if defined(__GNUC__)
 67477   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 67478      unsigned int lo, hi;
 67479      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 67480      return (sqlite_uint64)hi << 32 | lo;
 67483   #elif defined(_MSC_VER)
 67485   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 67486      __asm {
 67487         rdtsc
 67488         ret       ; return value at EDX:EAX
 67492   #endif
 67494 #elif (defined(__GNUC__) && defined(__x86_64__))
 67496   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 67497       unsigned long val;
 67498       __asm__ __volatile__ ("rdtsc" : "=A" (val));
 67499       return val;
 67502 #elif (defined(__GNUC__) && defined(__ppc__))
 67504   __inline__ sqlite_uint64 sqlite3Hwtime(void){
 67505       unsigned long long retval;
 67506       unsigned long junk;
 67507       __asm__ __volatile__ ("\n\
 67508           1:      mftbu   %1\n\
 67509                   mftb    %L0\n\
 67510                   mftbu   %0\n\
 67511                   cmpw    %0,%1\n\
 67512                   bne     1b"
 67513                   : "=r" (retval), "=r" (junk));
 67514       return retval;
 67517 #else
 67519   #error Need implementation of sqlite3Hwtime() for your platform.
 67521   /*
 67522   ** To compile without implementing sqlite3Hwtime() for your platform,
 67523   ** you can remove the above #error and use the following
 67524   ** stub function.  You will lose timing support for many
 67525   ** of the debugging and testing utilities, but it should at
 67526   ** least compile and run.
 67527   */
 67528 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 67530 #endif
 67532 #endif /* !defined(_HWTIME_H_) */
 67534 /************** End of hwtime.h **********************************************/
 67535 /************** Continuing where we left off in vdbe.c ***********************/
 67537 #endif
 67539 #ifndef NDEBUG
 67540 /*
 67541 ** This function is only called from within an assert() expression. It
 67542 ** checks that the sqlite3.nTransaction variable is correctly set to
 67543 ** the number of non-transaction savepoints currently in the 
 67544 ** linked list starting at sqlite3.pSavepoint.
 67545 ** 
 67546 ** Usage:
 67547 **
 67548 **     assert( checkSavepointCount(db) );
 67549 */
 67550 static int checkSavepointCount(sqlite3 *db){
 67551   int n = 0;
 67552   Savepoint *p;
 67553   for(p=db->pSavepoint; p; p=p->pNext) n++;
 67554   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
 67555   return 1;
 67557 #endif
 67560 /*
 67561 ** Execute as much of a VDBE program as we can.
 67562 ** This is the core of sqlite3_step().  
 67563 */
 67564 SQLITE_PRIVATE int sqlite3VdbeExec(
 67565   Vdbe *p                    /* The VDBE */
 67566 ){
 67567   int pc=0;                  /* The program counter */
 67568   Op *aOp = p->aOp;          /* Copy of p->aOp */
 67569   Op *pOp;                   /* Current operation */
 67570   int rc = SQLITE_OK;        /* Value to return */
 67571   sqlite3 *db = p->db;       /* The database */
 67572   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 67573   u8 encoding = ENC(db);     /* The database encoding */
 67574   int iCompare = 0;          /* Result of last OP_Compare operation */
 67575   unsigned nVmStep = 0;      /* Number of virtual machine steps */
 67576 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 67577   unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
 67578 #endif
 67579   Mem *aMem = p->aMem;       /* Copy of p->aMem */
 67580   Mem *pIn1 = 0;             /* 1st input operand */
 67581   Mem *pIn2 = 0;             /* 2nd input operand */
 67582   Mem *pIn3 = 0;             /* 3rd input operand */
 67583   Mem *pOut = 0;             /* Output operand */
 67584   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
 67585   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
 67586 #ifdef VDBE_PROFILE
 67587   u64 start;                 /* CPU clock count at start of opcode */
 67588 #endif
 67589   /*** INSERT STACK UNION HERE ***/
 67591   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 67592   sqlite3VdbeEnter(p);
 67593   if( p->rc==SQLITE_NOMEM ){
 67594     /* This happens if a malloc() inside a call to sqlite3_column_text() or
 67595     ** sqlite3_column_text16() failed.  */
 67596     goto no_mem;
 67598   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
 67599   assert( p->bIsReader || p->readOnly!=0 );
 67600   p->rc = SQLITE_OK;
 67601   p->iCurrentTime = 0;
 67602   assert( p->explain==0 );
 67603   p->pResultSet = 0;
 67604   db->busyHandler.nBusy = 0;
 67605   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 67606   sqlite3VdbeIOTraceSql(p);
 67607 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 67608   if( db->xProgress ){
 67609     assert( 0 < db->nProgressOps );
 67610     nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
 67611     if( nProgressLimit==0 ){
 67612       nProgressLimit = db->nProgressOps;
 67613     }else{
 67614       nProgressLimit %= (unsigned)db->nProgressOps;
 67617 #endif
 67618 #ifdef SQLITE_DEBUG
 67619   sqlite3BeginBenignMalloc();
 67620   if( p->pc==0
 67621    && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
 67622   ){
 67623     int i;
 67624     int once = 1;
 67625     sqlite3VdbePrintSql(p);
 67626     if( p->db->flags & SQLITE_VdbeListing ){
 67627       printf("VDBE Program Listing:\n");
 67628       for(i=0; i<p->nOp; i++){
 67629         sqlite3VdbePrintOp(stdout, i, &aOp[i]);
 67632     if( p->db->flags & SQLITE_VdbeEQP ){
 67633       for(i=0; i<p->nOp; i++){
 67634         if( aOp[i].opcode==OP_Explain ){
 67635           if( once ) printf("VDBE Query Plan:\n");
 67636           printf("%s\n", aOp[i].p4.z);
 67637           once = 0;
 67641     if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
 67643   sqlite3EndBenignMalloc();
 67644 #endif
 67645   for(pc=p->pc; rc==SQLITE_OK; pc++){
 67646     assert( pc>=0 && pc<p->nOp );
 67647     if( db->mallocFailed ) goto no_mem;
 67648 #ifdef VDBE_PROFILE
 67649     start = sqlite3Hwtime();
 67650 #endif
 67651     nVmStep++;
 67652     pOp = &aOp[pc];
 67654     /* Only allow tracing if SQLITE_DEBUG is defined.
 67655     */
 67656 #ifdef SQLITE_DEBUG
 67657     if( db->flags & SQLITE_VdbeTrace ){
 67658       sqlite3VdbePrintOp(stdout, pc, pOp);
 67660 #endif
 67663     /* Check to see if we need to simulate an interrupt.  This only happens
 67664     ** if we have a special test build.
 67665     */
 67666 #ifdef SQLITE_TEST
 67667     if( sqlite3_interrupt_count>0 ){
 67668       sqlite3_interrupt_count--;
 67669       if( sqlite3_interrupt_count==0 ){
 67670         sqlite3_interrupt(db);
 67673 #endif
 67675     /* On any opcode with the "out2-prerelease" tag, free any
 67676     ** external allocations out of mem[p2] and set mem[p2] to be
 67677     ** an undefined integer.  Opcodes will either fill in the integer
 67678     ** value or convert mem[p2] to a different type.
 67679     */
 67680     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
 67681     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
 67682       assert( pOp->p2>0 );
 67683       assert( pOp->p2<=(p->nMem-p->nCursor) );
 67684       pOut = &aMem[pOp->p2];
 67685       memAboutToChange(p, pOut);
 67686       VdbeMemRelease(pOut);
 67687       pOut->flags = MEM_Int;
 67690     /* Sanity checking on other operands */
 67691 #ifdef SQLITE_DEBUG
 67692     if( (pOp->opflags & OPFLG_IN1)!=0 ){
 67693       assert( pOp->p1>0 );
 67694       assert( pOp->p1<=(p->nMem-p->nCursor) );
 67695       assert( memIsValid(&aMem[pOp->p1]) );
 67696       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
 67697       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
 67699     if( (pOp->opflags & OPFLG_IN2)!=0 ){
 67700       assert( pOp->p2>0 );
 67701       assert( pOp->p2<=(p->nMem-p->nCursor) );
 67702       assert( memIsValid(&aMem[pOp->p2]) );
 67703       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
 67704       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
 67706     if( (pOp->opflags & OPFLG_IN3)!=0 ){
 67707       assert( pOp->p3>0 );
 67708       assert( pOp->p3<=(p->nMem-p->nCursor) );
 67709       assert( memIsValid(&aMem[pOp->p3]) );
 67710       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
 67711       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
 67713     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
 67714       assert( pOp->p2>0 );
 67715       assert( pOp->p2<=(p->nMem-p->nCursor) );
 67716       memAboutToChange(p, &aMem[pOp->p2]);
 67718     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
 67719       assert( pOp->p3>0 );
 67720       assert( pOp->p3<=(p->nMem-p->nCursor) );
 67721       memAboutToChange(p, &aMem[pOp->p3]);
 67723 #endif
 67725     switch( pOp->opcode ){
 67727 /*****************************************************************************
 67728 ** What follows is a massive switch statement where each case implements a
 67729 ** separate instruction in the virtual machine.  If we follow the usual
 67730 ** indentation conventions, each case should be indented by 6 spaces.  But
 67731 ** that is a lot of wasted space on the left margin.  So the code within
 67732 ** the switch statement will break with convention and be flush-left. Another
 67733 ** big comment (similar to this one) will mark the point in the code where
 67734 ** we transition back to normal indentation.
 67735 **
 67736 ** The formatting of each case is important.  The makefile for SQLite
 67737 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
 67738 ** file looking for lines that begin with "case OP_".  The opcodes.h files
 67739 ** will be filled with #defines that give unique integer values to each
 67740 ** opcode and the opcodes.c file is filled with an array of strings where
 67741 ** each string is the symbolic name for the corresponding opcode.  If the
 67742 ** case statement is followed by a comment of the form "/# same as ... #/"
 67743 ** that comment is used to determine the particular value of the opcode.
 67744 **
 67745 ** Other keywords in the comment that follows each case are used to
 67746 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
 67747 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
 67748 ** the mkopcodeh.awk script for additional information.
 67749 **
 67750 ** Documentation about VDBE opcodes is generated by scanning this file
 67751 ** for lines of that contain "Opcode:".  That line and all subsequent
 67752 ** comment lines are used in the generation of the opcode.html documentation
 67753 ** file.
 67754 **
 67755 ** SUMMARY:
 67756 **
 67757 **     Formatting is important to scripts that scan this file.
 67758 **     Do not deviate from the formatting style currently in use.
 67759 **
 67760 *****************************************************************************/
 67762 /* Opcode:  Goto * P2 * * *
 67763 **
 67764 ** An unconditional jump to address P2.
 67765 ** The next instruction executed will be 
 67766 ** the one at index P2 from the beginning of
 67767 ** the program.
 67768 **
 67769 ** The P1 parameter is not actually used by this opcode.  However, it
 67770 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
 67771 ** that this Goto is the bottom of a loop and that the lines from P2 down
 67772 ** to the current line should be indented for EXPLAIN output.
 67773 */
 67774 case OP_Goto: {             /* jump */
 67775   pc = pOp->p2 - 1;
 67777   /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
 67778   ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
 67779   ** completion.  Check to see if sqlite3_interrupt() has been called
 67780   ** or if the progress callback needs to be invoked. 
 67781   **
 67782   ** This code uses unstructured "goto" statements and does not look clean.
 67783   ** But that is not due to sloppy coding habits. The code is written this
 67784   ** way for performance, to avoid having to run the interrupt and progress
 67785   ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
 67786   ** faster according to "valgrind --tool=cachegrind" */
 67787 check_for_interrupt:
 67788   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
 67789 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 67790   /* Call the progress callback if it is configured and the required number
 67791   ** of VDBE ops have been executed (either since this invocation of
 67792   ** sqlite3VdbeExec() or since last time the progress callback was called).
 67793   ** If the progress callback returns non-zero, exit the virtual machine with
 67794   ** a return code SQLITE_ABORT.
 67795   */
 67796   if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
 67797     assert( db->nProgressOps!=0 );
 67798     nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
 67799     if( db->xProgress(db->pProgressArg) ){
 67800       rc = SQLITE_INTERRUPT;
 67801       goto vdbe_error_halt;
 67804 #endif
 67806   break;
 67809 /* Opcode:  Gosub P1 P2 * * *
 67810 **
 67811 ** Write the current address onto register P1
 67812 ** and then jump to address P2.
 67813 */
 67814 case OP_Gosub: {            /* jump */
 67815   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
 67816   pIn1 = &aMem[pOp->p1];
 67817   assert( VdbeMemDynamic(pIn1)==0 );
 67818   memAboutToChange(p, pIn1);
 67819   pIn1->flags = MEM_Int;
 67820   pIn1->u.i = pc;
 67821   REGISTER_TRACE(pOp->p1, pIn1);
 67822   pc = pOp->p2 - 1;
 67823   break;
 67826 /* Opcode:  Return P1 * * * *
 67827 **
 67828 ** Jump to the next instruction after the address in register P1.  After
 67829 ** the jump, register P1 becomes undefined.
 67830 */
 67831 case OP_Return: {           /* in1 */
 67832   pIn1 = &aMem[pOp->p1];
 67833   assert( pIn1->flags==MEM_Int );
 67834   pc = (int)pIn1->u.i;
 67835   pIn1->flags = MEM_Undefined;
 67836   break;
 67839 /* Opcode: InitCoroutine P1 P2 P3 * *
 67840 **
 67841 ** Set up register P1 so that it will OP_Yield to the co-routine
 67842 ** located at address P3.
 67843 **
 67844 ** If P2!=0 then the co-routine implementation immediately follows
 67845 ** this opcode.  So jump over the co-routine implementation to
 67846 ** address P2.
 67847 */
 67848 case OP_InitCoroutine: {     /* jump */
 67849   assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
 67850   assert( pOp->p2>=0 && pOp->p2<p->nOp );
 67851   assert( pOp->p3>=0 && pOp->p3<p->nOp );
 67852   pOut = &aMem[pOp->p1];
 67853   assert( !VdbeMemDynamic(pOut) );
 67854   pOut->u.i = pOp->p3 - 1;
 67855   pOut->flags = MEM_Int;
 67856   if( pOp->p2 ) pc = pOp->p2 - 1;
 67857   break;
 67860 /* Opcode:  EndCoroutine P1 * * * *
 67861 **
 67862 ** The instruction at the address in register P1 is an OP_Yield.
 67863 ** Jump to the P2 parameter of that OP_Yield.
 67864 ** After the jump, register P1 becomes undefined.
 67865 */
 67866 case OP_EndCoroutine: {           /* in1 */
 67867   VdbeOp *pCaller;
 67868   pIn1 = &aMem[pOp->p1];
 67869   assert( pIn1->flags==MEM_Int );
 67870   assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
 67871   pCaller = &aOp[pIn1->u.i];
 67872   assert( pCaller->opcode==OP_Yield );
 67873   assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
 67874   pc = pCaller->p2 - 1;
 67875   pIn1->flags = MEM_Undefined;
 67876   break;
 67879 /* Opcode:  Yield P1 P2 * * *
 67880 **
 67881 ** Swap the program counter with the value in register P1.
 67882 **
 67883 ** If the co-routine ends with OP_Yield or OP_Return then continue
 67884 ** to the next instruction.  But if the co-routine ends with
 67885 ** OP_EndCoroutine, jump immediately to P2.
 67886 */
 67887 case OP_Yield: {            /* in1, jump */
 67888   int pcDest;
 67889   pIn1 = &aMem[pOp->p1];
 67890   assert( VdbeMemDynamic(pIn1)==0 );
 67891   pIn1->flags = MEM_Int;
 67892   pcDest = (int)pIn1->u.i;
 67893   pIn1->u.i = pc;
 67894   REGISTER_TRACE(pOp->p1, pIn1);
 67895   pc = pcDest;
 67896   break;
 67899 /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
 67900 ** Synopsis:  if r[P3]=null halt
 67901 **
 67902 ** Check the value in register P3.  If it is NULL then Halt using
 67903 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
 67904 ** value in register P3 is not NULL, then this routine is a no-op.
 67905 ** The P5 parameter should be 1.
 67906 */
 67907 case OP_HaltIfNull: {      /* in3 */
 67908   pIn3 = &aMem[pOp->p3];
 67909   if( (pIn3->flags & MEM_Null)==0 ) break;
 67910   /* Fall through into OP_Halt */
 67913 /* Opcode:  Halt P1 P2 * P4 P5
 67914 **
 67915 ** Exit immediately.  All open cursors, etc are closed
 67916 ** automatically.
 67917 **
 67918 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
 67919 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
 67920 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
 67921 ** whether or not to rollback the current transaction.  Do not rollback
 67922 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
 67923 ** then back out all changes that have occurred during this execution of the
 67924 ** VDBE, but do not rollback the transaction. 
 67925 **
 67926 ** If P4 is not null then it is an error message string.
 67927 **
 67928 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
 67929 **
 67930 **    0:  (no change)
 67931 **    1:  NOT NULL contraint failed: P4
 67932 **    2:  UNIQUE constraint failed: P4
 67933 **    3:  CHECK constraint failed: P4
 67934 **    4:  FOREIGN KEY constraint failed: P4
 67935 **
 67936 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
 67937 ** omitted.
 67938 **
 67939 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
 67940 ** every program.  So a jump past the last instruction of the program
 67941 ** is the same as executing Halt.
 67942 */
 67943 case OP_Halt: {
 67944   const char *zType;
 67945   const char *zLogFmt;
 67947   if( pOp->p1==SQLITE_OK && p->pFrame ){
 67948     /* Halt the sub-program. Return control to the parent frame. */
 67949     VdbeFrame *pFrame = p->pFrame;
 67950     p->pFrame = pFrame->pParent;
 67951     p->nFrame--;
 67952     sqlite3VdbeSetChanges(db, p->nChange);
 67953     pc = sqlite3VdbeFrameRestore(pFrame);
 67954     lastRowid = db->lastRowid;
 67955     if( pOp->p2==OE_Ignore ){
 67956       /* Instruction pc is the OP_Program that invoked the sub-program 
 67957       ** currently being halted. If the p2 instruction of this OP_Halt
 67958       ** instruction is set to OE_Ignore, then the sub-program is throwing
 67959       ** an IGNORE exception. In this case jump to the address specified
 67960       ** as the p2 of the calling OP_Program.  */
 67961       pc = p->aOp[pc].p2-1;
 67963     aOp = p->aOp;
 67964     aMem = p->aMem;
 67965     break;
 67967   p->rc = pOp->p1;
 67968   p->errorAction = (u8)pOp->p2;
 67969   p->pc = pc;
 67970   if( p->rc ){
 67971     if( pOp->p5 ){
 67972       static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
 67973                                              "FOREIGN KEY" };
 67974       assert( pOp->p5>=1 && pOp->p5<=4 );
 67975       testcase( pOp->p5==1 );
 67976       testcase( pOp->p5==2 );
 67977       testcase( pOp->p5==3 );
 67978       testcase( pOp->p5==4 );
 67979       zType = azType[pOp->p5-1];
 67980     }else{
 67981       zType = 0;
 67983     assert( zType!=0 || pOp->p4.z!=0 );
 67984     zLogFmt = "abort at %d in [%s]: %s";
 67985     if( zType && pOp->p4.z ){
 67986       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", 
 67987                        zType, pOp->p4.z);
 67988     }else if( pOp->p4.z ){
 67989       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
 67990     }else{
 67991       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
 67993     sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
 67995   rc = sqlite3VdbeHalt(p);
 67996   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
 67997   if( rc==SQLITE_BUSY ){
 67998     p->rc = rc = SQLITE_BUSY;
 67999   }else{
 68000     assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
 68001     assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
 68002     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
 68004   goto vdbe_return;
 68007 /* Opcode: Integer P1 P2 * * *
 68008 ** Synopsis: r[P2]=P1
 68009 **
 68010 ** The 32-bit integer value P1 is written into register P2.
 68011 */
 68012 case OP_Integer: {         /* out2-prerelease */
 68013   pOut->u.i = pOp->p1;
 68014   break;
 68017 /* Opcode: Int64 * P2 * P4 *
 68018 ** Synopsis: r[P2]=P4
 68019 **
 68020 ** P4 is a pointer to a 64-bit integer value.
 68021 ** Write that value into register P2.
 68022 */
 68023 case OP_Int64: {           /* out2-prerelease */
 68024   assert( pOp->p4.pI64!=0 );
 68025   pOut->u.i = *pOp->p4.pI64;
 68026   break;
 68029 #ifndef SQLITE_OMIT_FLOATING_POINT
 68030 /* Opcode: Real * P2 * P4 *
 68031 ** Synopsis: r[P2]=P4
 68032 **
 68033 ** P4 is a pointer to a 64-bit floating point value.
 68034 ** Write that value into register P2.
 68035 */
 68036 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
 68037   pOut->flags = MEM_Real;
 68038   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
 68039   pOut->r = *pOp->p4.pReal;
 68040   break;
 68042 #endif
 68044 /* Opcode: String8 * P2 * P4 *
 68045 ** Synopsis: r[P2]='P4'
 68046 **
 68047 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
 68048 ** into an OP_String before it is executed for the first time.  During
 68049 ** this transformation, the length of string P4 is computed and stored
 68050 ** as the P1 parameter.
 68051 */
 68052 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
 68053   assert( pOp->p4.z!=0 );
 68054   pOp->opcode = OP_String;
 68055   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
 68057 #ifndef SQLITE_OMIT_UTF16
 68058   if( encoding!=SQLITE_UTF8 ){
 68059     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
 68060     if( rc==SQLITE_TOOBIG ) goto too_big;
 68061     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
 68062     assert( pOut->zMalloc==pOut->z );
 68063     assert( VdbeMemDynamic(pOut)==0 );
 68064     pOut->zMalloc = 0;
 68065     pOut->flags |= MEM_Static;
 68066     if( pOp->p4type==P4_DYNAMIC ){
 68067       sqlite3DbFree(db, pOp->p4.z);
 68069     pOp->p4type = P4_DYNAMIC;
 68070     pOp->p4.z = pOut->z;
 68071     pOp->p1 = pOut->n;
 68073 #endif
 68074   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 68075     goto too_big;
 68077   /* Fall through to the next case, OP_String */
 68080 /* Opcode: String P1 P2 * P4 *
 68081 ** Synopsis: r[P2]='P4' (len=P1)
 68082 **
 68083 ** The string value P4 of length P1 (bytes) is stored in register P2.
 68084 */
 68085 case OP_String: {          /* out2-prerelease */
 68086   assert( pOp->p4.z!=0 );
 68087   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 68088   pOut->z = pOp->p4.z;
 68089   pOut->n = pOp->p1;
 68090   pOut->enc = encoding;
 68091   UPDATE_MAX_BLOBSIZE(pOut);
 68092   break;
 68095 /* Opcode: Null P1 P2 P3 * *
 68096 ** Synopsis:  r[P2..P3]=NULL
 68097 **
 68098 ** Write a NULL into registers P2.  If P3 greater than P2, then also write
 68099 ** NULL into register P3 and every register in between P2 and P3.  If P3
 68100 ** is less than P2 (typically P3 is zero) then only register P2 is
 68101 ** set to NULL.
 68102 **
 68103 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
 68104 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
 68105 ** OP_Ne or OP_Eq.
 68106 */
 68107 case OP_Null: {           /* out2-prerelease */
 68108   int cnt;
 68109   u16 nullFlag;
 68110   cnt = pOp->p3-pOp->p2;
 68111   assert( pOp->p3<=(p->nMem-p->nCursor) );
 68112   pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
 68113   while( cnt>0 ){
 68114     pOut++;
 68115     memAboutToChange(p, pOut);
 68116     VdbeMemRelease(pOut);
 68117     pOut->flags = nullFlag;
 68118     cnt--;
 68120   break;
 68123 /* Opcode: SoftNull P1 * * * *
 68124 ** Synopsis:  r[P1]=NULL
 68125 **
 68126 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
 68127 ** instruction, but do not free any string or blob memory associated with
 68128 ** the register, so that if the value was a string or blob that was
 68129 ** previously copied using OP_SCopy, the copies will continue to be valid.
 68130 */
 68131 case OP_SoftNull: {
 68132   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
 68133   pOut = &aMem[pOp->p1];
 68134   pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
 68135   break;
 68138 /* Opcode: Blob P1 P2 * P4 *
 68139 ** Synopsis: r[P2]=P4 (len=P1)
 68140 **
 68141 ** P4 points to a blob of data P1 bytes long.  Store this
 68142 ** blob in register P2.
 68143 */
 68144 case OP_Blob: {                /* out2-prerelease */
 68145   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
 68146   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
 68147   pOut->enc = encoding;
 68148   UPDATE_MAX_BLOBSIZE(pOut);
 68149   break;
 68152 /* Opcode: Variable P1 P2 * P4 *
 68153 ** Synopsis: r[P2]=parameter(P1,P4)
 68154 **
 68155 ** Transfer the values of bound parameter P1 into register P2
 68156 **
 68157 ** If the parameter is named, then its name appears in P4.
 68158 ** The P4 value is used by sqlite3_bind_parameter_name().
 68159 */
 68160 case OP_Variable: {            /* out2-prerelease */
 68161   Mem *pVar;       /* Value being transferred */
 68163   assert( pOp->p1>0 && pOp->p1<=p->nVar );
 68164   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
 68165   pVar = &p->aVar[pOp->p1 - 1];
 68166   if( sqlite3VdbeMemTooBig(pVar) ){
 68167     goto too_big;
 68169   sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
 68170   UPDATE_MAX_BLOBSIZE(pOut);
 68171   break;
 68174 /* Opcode: Move P1 P2 P3 * *
 68175 ** Synopsis:  r[P2@P3]=r[P1@P3]
 68176 **
 68177 ** Move the values in register P1..P1+P3 over into
 68178 ** registers P2..P2+P3.  Registers P1..P1+P3 are
 68179 ** left holding a NULL.  It is an error for register ranges
 68180 ** P1..P1+P3 and P2..P2+P3 to overlap.
 68181 */
 68182 case OP_Move: {
 68183   char *zMalloc;   /* Holding variable for allocated memory */
 68184   int n;           /* Number of registers left to copy */
 68185   int p1;          /* Register to copy from */
 68186   int p2;          /* Register to copy to */
 68188   n = pOp->p3;
 68189   p1 = pOp->p1;
 68190   p2 = pOp->p2;
 68191   assert( n>=0 && p1>0 && p2>0 );
 68192   assert( p1+n<=p2 || p2+n<=p1 );
 68194   pIn1 = &aMem[p1];
 68195   pOut = &aMem[p2];
 68196   do{
 68197     assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
 68198     assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
 68199     assert( memIsValid(pIn1) );
 68200     memAboutToChange(p, pOut);
 68201     VdbeMemRelease(pOut);
 68202     zMalloc = pOut->zMalloc;
 68203     memcpy(pOut, pIn1, sizeof(Mem));
 68204 #ifdef SQLITE_DEBUG
 68205     if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
 68206       pOut->pScopyFrom += p1 - pOp->p2;
 68208 #endif
 68209     pIn1->flags = MEM_Undefined;
 68210     pIn1->xDel = 0;
 68211     pIn1->zMalloc = zMalloc;
 68212     REGISTER_TRACE(p2++, pOut);
 68213     pIn1++;
 68214     pOut++;
 68215   }while( n-- );
 68216   break;
 68219 /* Opcode: Copy P1 P2 P3 * *
 68220 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
 68221 **
 68222 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
 68223 **
 68224 ** This instruction makes a deep copy of the value.  A duplicate
 68225 ** is made of any string or blob constant.  See also OP_SCopy.
 68226 */
 68227 case OP_Copy: {
 68228   int n;
 68230   n = pOp->p3;
 68231   pIn1 = &aMem[pOp->p1];
 68232   pOut = &aMem[pOp->p2];
 68233   assert( pOut!=pIn1 );
 68234   while( 1 ){
 68235     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 68236     Deephemeralize(pOut);
 68237 #ifdef SQLITE_DEBUG
 68238     pOut->pScopyFrom = 0;
 68239 #endif
 68240     REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
 68241     if( (n--)==0 ) break;
 68242     pOut++;
 68243     pIn1++;
 68245   break;
 68248 /* Opcode: SCopy P1 P2 * * *
 68249 ** Synopsis: r[P2]=r[P1]
 68250 **
 68251 ** Make a shallow copy of register P1 into register P2.
 68252 **
 68253 ** This instruction makes a shallow copy of the value.  If the value
 68254 ** is a string or blob, then the copy is only a pointer to the
 68255 ** original and hence if the original changes so will the copy.
 68256 ** Worse, if the original is deallocated, the copy becomes invalid.
 68257 ** Thus the program must guarantee that the original will not change
 68258 ** during the lifetime of the copy.  Use OP_Copy to make a complete
 68259 ** copy.
 68260 */
 68261 case OP_SCopy: {            /* out2 */
 68262   pIn1 = &aMem[pOp->p1];
 68263   pOut = &aMem[pOp->p2];
 68264   assert( pOut!=pIn1 );
 68265   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
 68266 #ifdef SQLITE_DEBUG
 68267   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
 68268 #endif
 68269   break;
 68272 /* Opcode: ResultRow P1 P2 * * *
 68273 ** Synopsis:  output=r[P1@P2]
 68274 **
 68275 ** The registers P1 through P1+P2-1 contain a single row of
 68276 ** results. This opcode causes the sqlite3_step() call to terminate
 68277 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
 68278 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
 68279 ** the result row.
 68280 */
 68281 case OP_ResultRow: {
 68282   Mem *pMem;
 68283   int i;
 68284   assert( p->nResColumn==pOp->p2 );
 68285   assert( pOp->p1>0 );
 68286   assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
 68288 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 68289   /* Run the progress counter just before returning.
 68290   */
 68291   if( db->xProgress!=0
 68292    && nVmStep>=nProgressLimit
 68293    && db->xProgress(db->pProgressArg)!=0
 68294   ){
 68295     rc = SQLITE_INTERRUPT;
 68296     goto vdbe_error_halt;
 68298 #endif
 68300   /* If this statement has violated immediate foreign key constraints, do
 68301   ** not return the number of rows modified. And do not RELEASE the statement
 68302   ** transaction. It needs to be rolled back.  */
 68303   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
 68304     assert( db->flags&SQLITE_CountRows );
 68305     assert( p->usesStmtJournal );
 68306     break;
 68309   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 
 68310   ** DML statements invoke this opcode to return the number of rows 
 68311   ** modified to the user. This is the only way that a VM that
 68312   ** opens a statement transaction may invoke this opcode.
 68313   **
 68314   ** In case this is such a statement, close any statement transaction
 68315   ** opened by this VM before returning control to the user. This is to
 68316   ** ensure that statement-transactions are always nested, not overlapping.
 68317   ** If the open statement-transaction is not closed here, then the user
 68318   ** may step another VM that opens its own statement transaction. This
 68319   ** may lead to overlapping statement transactions.
 68320   **
 68321   ** The statement transaction is never a top-level transaction.  Hence
 68322   ** the RELEASE call below can never fail.
 68323   */
 68324   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
 68325   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
 68326   if( NEVER(rc!=SQLITE_OK) ){
 68327     break;
 68330   /* Invalidate all ephemeral cursor row caches */
 68331   p->cacheCtr = (p->cacheCtr + 2)|1;
 68333   /* Make sure the results of the current row are \000 terminated
 68334   ** and have an assigned type.  The results are de-ephemeralized as
 68335   ** a side effect.
 68336   */
 68337   pMem = p->pResultSet = &aMem[pOp->p1];
 68338   for(i=0; i<pOp->p2; i++){
 68339     assert( memIsValid(&pMem[i]) );
 68340     Deephemeralize(&pMem[i]);
 68341     assert( (pMem[i].flags & MEM_Ephem)==0
 68342             || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
 68343     sqlite3VdbeMemNulTerminate(&pMem[i]);
 68344     REGISTER_TRACE(pOp->p1+i, &pMem[i]);
 68346   if( db->mallocFailed ) goto no_mem;
 68348   /* Return SQLITE_ROW
 68349   */
 68350   p->pc = pc + 1;
 68351   rc = SQLITE_ROW;
 68352   goto vdbe_return;
 68355 /* Opcode: Concat P1 P2 P3 * *
 68356 ** Synopsis: r[P3]=r[P2]+r[P1]
 68357 **
 68358 ** Add the text in register P1 onto the end of the text in
 68359 ** register P2 and store the result in register P3.
 68360 ** If either the P1 or P2 text are NULL then store NULL in P3.
 68361 **
 68362 **   P3 = P2 || P1
 68363 **
 68364 ** It is illegal for P1 and P3 to be the same register. Sometimes,
 68365 ** if P3 is the same register as P2, the implementation is able
 68366 ** to avoid a memcpy().
 68367 */
 68368 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
 68369   i64 nByte;
 68371   pIn1 = &aMem[pOp->p1];
 68372   pIn2 = &aMem[pOp->p2];
 68373   pOut = &aMem[pOp->p3];
 68374   assert( pIn1!=pOut );
 68375   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 68376     sqlite3VdbeMemSetNull(pOut);
 68377     break;
 68379   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
 68380   Stringify(pIn1, encoding);
 68381   Stringify(pIn2, encoding);
 68382   nByte = pIn1->n + pIn2->n;
 68383   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 68384     goto too_big;
 68386   if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
 68387     goto no_mem;
 68389   MemSetTypeFlag(pOut, MEM_Str);
 68390   if( pOut!=pIn2 ){
 68391     memcpy(pOut->z, pIn2->z, pIn2->n);
 68393   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
 68394   pOut->z[nByte]=0;
 68395   pOut->z[nByte+1] = 0;
 68396   pOut->flags |= MEM_Term;
 68397   pOut->n = (int)nByte;
 68398   pOut->enc = encoding;
 68399   UPDATE_MAX_BLOBSIZE(pOut);
 68400   break;
 68403 /* Opcode: Add P1 P2 P3 * *
 68404 ** Synopsis:  r[P3]=r[P1]+r[P2]
 68405 **
 68406 ** Add the value in register P1 to the value in register P2
 68407 ** and store the result in register P3.
 68408 ** If either input is NULL, the result is NULL.
 68409 */
 68410 /* Opcode: Multiply P1 P2 P3 * *
 68411 ** Synopsis:  r[P3]=r[P1]*r[P2]
 68412 **
 68413 **
 68414 ** Multiply the value in register P1 by the value in register P2
 68415 ** and store the result in register P3.
 68416 ** If either input is NULL, the result is NULL.
 68417 */
 68418 /* Opcode: Subtract P1 P2 P3 * *
 68419 ** Synopsis:  r[P3]=r[P2]-r[P1]
 68420 **
 68421 ** Subtract the value in register P1 from the value in register P2
 68422 ** and store the result in register P3.
 68423 ** If either input is NULL, the result is NULL.
 68424 */
 68425 /* Opcode: Divide P1 P2 P3 * *
 68426 ** Synopsis:  r[P3]=r[P2]/r[P1]
 68427 **
 68428 ** Divide the value in register P1 by the value in register P2
 68429 ** and store the result in register P3 (P3=P2/P1). If the value in 
 68430 ** register P1 is zero, then the result is NULL. If either input is 
 68431 ** NULL, the result is NULL.
 68432 */
 68433 /* Opcode: Remainder P1 P2 P3 * *
 68434 ** Synopsis:  r[P3]=r[P2]%r[P1]
 68435 **
 68436 ** Compute the remainder after integer register P2 is divided by 
 68437 ** register P1 and store the result in register P3. 
 68438 ** If the value in register P1 is zero the result is NULL.
 68439 ** If either operand is NULL, the result is NULL.
 68440 */
 68441 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
 68442 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
 68443 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
 68444 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
 68445 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
 68446   char bIntint;   /* Started out as two integer operands */
 68447   int flags;      /* Combined MEM_* flags from both inputs */
 68448   i64 iA;         /* Integer value of left operand */
 68449   i64 iB;         /* Integer value of right operand */
 68450   double rA;      /* Real value of left operand */
 68451   double rB;      /* Real value of right operand */
 68453   pIn1 = &aMem[pOp->p1];
 68454   applyNumericAffinity(pIn1);
 68455   pIn2 = &aMem[pOp->p2];
 68456   applyNumericAffinity(pIn2);
 68457   pOut = &aMem[pOp->p3];
 68458   flags = pIn1->flags | pIn2->flags;
 68459   if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
 68460   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
 68461     iA = pIn1->u.i;
 68462     iB = pIn2->u.i;
 68463     bIntint = 1;
 68464     switch( pOp->opcode ){
 68465       case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
 68466       case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
 68467       case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
 68468       case OP_Divide: {
 68469         if( iA==0 ) goto arithmetic_result_is_null;
 68470         if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
 68471         iB /= iA;
 68472         break;
 68474       default: {
 68475         if( iA==0 ) goto arithmetic_result_is_null;
 68476         if( iA==-1 ) iA = 1;
 68477         iB %= iA;
 68478         break;
 68481     pOut->u.i = iB;
 68482     MemSetTypeFlag(pOut, MEM_Int);
 68483   }else{
 68484     bIntint = 0;
 68485 fp_math:
 68486     rA = sqlite3VdbeRealValue(pIn1);
 68487     rB = sqlite3VdbeRealValue(pIn2);
 68488     switch( pOp->opcode ){
 68489       case OP_Add:         rB += rA;       break;
 68490       case OP_Subtract:    rB -= rA;       break;
 68491       case OP_Multiply:    rB *= rA;       break;
 68492       case OP_Divide: {
 68493         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 68494         if( rA==(double)0 ) goto arithmetic_result_is_null;
 68495         rB /= rA;
 68496         break;
 68498       default: {
 68499         iA = (i64)rA;
 68500         iB = (i64)rB;
 68501         if( iA==0 ) goto arithmetic_result_is_null;
 68502         if( iA==-1 ) iA = 1;
 68503         rB = (double)(iB % iA);
 68504         break;
 68507 #ifdef SQLITE_OMIT_FLOATING_POINT
 68508     pOut->u.i = rB;
 68509     MemSetTypeFlag(pOut, MEM_Int);
 68510 #else
 68511     if( sqlite3IsNaN(rB) ){
 68512       goto arithmetic_result_is_null;
 68514     pOut->r = rB;
 68515     MemSetTypeFlag(pOut, MEM_Real);
 68516     if( (flags & MEM_Real)==0 && !bIntint ){
 68517       sqlite3VdbeIntegerAffinity(pOut);
 68519 #endif
 68521   break;
 68523 arithmetic_result_is_null:
 68524   sqlite3VdbeMemSetNull(pOut);
 68525   break;
 68528 /* Opcode: CollSeq P1 * * P4
 68529 **
 68530 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
 68531 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
 68532 ** be returned. This is used by the built-in min(), max() and nullif()
 68533 ** functions.
 68534 **
 68535 ** If P1 is not zero, then it is a register that a subsequent min() or
 68536 ** max() aggregate will set to 1 if the current row is not the minimum or
 68537 ** maximum.  The P1 register is initialized to 0 by this instruction.
 68538 **
 68539 ** The interface used by the implementation of the aforementioned functions
 68540 ** to retrieve the collation sequence set by this opcode is not available
 68541 ** publicly, only to user functions defined in func.c.
 68542 */
 68543 case OP_CollSeq: {
 68544   assert( pOp->p4type==P4_COLLSEQ );
 68545   if( pOp->p1 ){
 68546     sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
 68548   break;
 68551 /* Opcode: Function P1 P2 P3 P4 P5
 68552 ** Synopsis: r[P3]=func(r[P2@P5])
 68553 **
 68554 ** Invoke a user function (P4 is a pointer to a Function structure that
 68555 ** defines the function) with P5 arguments taken from register P2 and
 68556 ** successors.  The result of the function is stored in register P3.
 68557 ** Register P3 must not be one of the function inputs.
 68558 **
 68559 ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
 68560 ** function was determined to be constant at compile time. If the first
 68561 ** argument was constant then bit 0 of P1 is set. This is used to determine
 68562 ** whether meta data associated with a user function argument using the
 68563 ** sqlite3_set_auxdata() API may be safely retained until the next
 68564 ** invocation of this opcode.
 68565 **
 68566 ** See also: AggStep and AggFinal
 68567 */
 68568 case OP_Function: {
 68569   int i;
 68570   Mem *pArg;
 68571   sqlite3_context ctx;
 68572   sqlite3_value **apVal;
 68573   int n;
 68575   n = pOp->p5;
 68576   apVal = p->apArg;
 68577   assert( apVal || n==0 );
 68578   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 68579   pOut = &aMem[pOp->p3];
 68580   memAboutToChange(p, pOut);
 68582   assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
 68583   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
 68584   pArg = &aMem[pOp->p2];
 68585   for(i=0; i<n; i++, pArg++){
 68586     assert( memIsValid(pArg) );
 68587     apVal[i] = pArg;
 68588     Deephemeralize(pArg);
 68589     REGISTER_TRACE(pOp->p2+i, pArg);
 68592   assert( pOp->p4type==P4_FUNCDEF );
 68593   ctx.pFunc = pOp->p4.pFunc;
 68594   ctx.iOp = pc;
 68595   ctx.pVdbe = p;
 68597   /* The output cell may already have a buffer allocated. Move
 68598   ** the pointer to ctx.s so in case the user-function can use
 68599   ** the already allocated buffer instead of allocating a new one.
 68600   */
 68601   memcpy(&ctx.s, pOut, sizeof(Mem));
 68602   pOut->flags = MEM_Null;
 68603   pOut->xDel = 0;
 68604   pOut->zMalloc = 0;
 68605   MemSetTypeFlag(&ctx.s, MEM_Null);
 68607   ctx.fErrorOrAux = 0;
 68608   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
 68609     assert( pOp>aOp );
 68610     assert( pOp[-1].p4type==P4_COLLSEQ );
 68611     assert( pOp[-1].opcode==OP_CollSeq );
 68612     ctx.pColl = pOp[-1].p4.pColl;
 68614   db->lastRowid = lastRowid;
 68615   (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
 68616   lastRowid = db->lastRowid;
 68618   if( db->mallocFailed ){
 68619     /* Even though a malloc() has failed, the implementation of the
 68620     ** user function may have called an sqlite3_result_XXX() function
 68621     ** to return a value. The following call releases any resources
 68622     ** associated with such a value.
 68623     */
 68624     sqlite3VdbeMemRelease(&ctx.s);
 68625     goto no_mem;
 68628   /* If the function returned an error, throw an exception */
 68629   if( ctx.fErrorOrAux ){
 68630     if( ctx.isError ){
 68631       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
 68632       rc = ctx.isError;
 68634     sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
 68637   /* Copy the result of the function into register P3 */
 68638   sqlite3VdbeChangeEncoding(&ctx.s, encoding);
 68639   assert( pOut->flags==MEM_Null );
 68640   memcpy(pOut, &ctx.s, sizeof(Mem));
 68641   if( sqlite3VdbeMemTooBig(pOut) ){
 68642     goto too_big;
 68645 #if 0
 68646   /* The app-defined function has done something that as caused this
 68647   ** statement to expire.  (Perhaps the function called sqlite3_exec()
 68648   ** with a CREATE TABLE statement.)
 68649   */
 68650   if( p->expired ) rc = SQLITE_ABORT;
 68651 #endif
 68653   REGISTER_TRACE(pOp->p3, pOut);
 68654   UPDATE_MAX_BLOBSIZE(pOut);
 68655   break;
 68658 /* Opcode: BitAnd P1 P2 P3 * *
 68659 ** Synopsis:  r[P3]=r[P1]&r[P2]
 68660 **
 68661 ** Take the bit-wise AND of the values in register P1 and P2 and
 68662 ** store the result in register P3.
 68663 ** If either input is NULL, the result is NULL.
 68664 */
 68665 /* Opcode: BitOr P1 P2 P3 * *
 68666 ** Synopsis:  r[P3]=r[P1]|r[P2]
 68667 **
 68668 ** Take the bit-wise OR of the values in register P1 and P2 and
 68669 ** store the result in register P3.
 68670 ** If either input is NULL, the result is NULL.
 68671 */
 68672 /* Opcode: ShiftLeft P1 P2 P3 * *
 68673 ** Synopsis:  r[P3]=r[P2]<<r[P1]
 68674 **
 68675 ** Shift the integer value in register P2 to the left by the
 68676 ** number of bits specified by the integer in register P1.
 68677 ** Store the result in register P3.
 68678 ** If either input is NULL, the result is NULL.
 68679 */
 68680 /* Opcode: ShiftRight P1 P2 P3 * *
 68681 ** Synopsis:  r[P3]=r[P2]>>r[P1]
 68682 **
 68683 ** Shift the integer value in register P2 to the right by the
 68684 ** number of bits specified by the integer in register P1.
 68685 ** Store the result in register P3.
 68686 ** If either input is NULL, the result is NULL.
 68687 */
 68688 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
 68689 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
 68690 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
 68691 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
 68692   i64 iA;
 68693   u64 uA;
 68694   i64 iB;
 68695   u8 op;
 68697   pIn1 = &aMem[pOp->p1];
 68698   pIn2 = &aMem[pOp->p2];
 68699   pOut = &aMem[pOp->p3];
 68700   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 68701     sqlite3VdbeMemSetNull(pOut);
 68702     break;
 68704   iA = sqlite3VdbeIntValue(pIn2);
 68705   iB = sqlite3VdbeIntValue(pIn1);
 68706   op = pOp->opcode;
 68707   if( op==OP_BitAnd ){
 68708     iA &= iB;
 68709   }else if( op==OP_BitOr ){
 68710     iA |= iB;
 68711   }else if( iB!=0 ){
 68712     assert( op==OP_ShiftRight || op==OP_ShiftLeft );
 68714     /* If shifting by a negative amount, shift in the other direction */
 68715     if( iB<0 ){
 68716       assert( OP_ShiftRight==OP_ShiftLeft+1 );
 68717       op = 2*OP_ShiftLeft + 1 - op;
 68718       iB = iB>(-64) ? -iB : 64;
 68721     if( iB>=64 ){
 68722       iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
 68723     }else{
 68724       memcpy(&uA, &iA, sizeof(uA));
 68725       if( op==OP_ShiftLeft ){
 68726         uA <<= iB;
 68727       }else{
 68728         uA >>= iB;
 68729         /* Sign-extend on a right shift of a negative number */
 68730         if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
 68732       memcpy(&iA, &uA, sizeof(iA));
 68735   pOut->u.i = iA;
 68736   MemSetTypeFlag(pOut, MEM_Int);
 68737   break;
 68740 /* Opcode: AddImm  P1 P2 * * *
 68741 ** Synopsis:  r[P1]=r[P1]+P2
 68742 ** 
 68743 ** Add the constant P2 to the value in register P1.
 68744 ** The result is always an integer.
 68745 **
 68746 ** To force any register to be an integer, just add 0.
 68747 */
 68748 case OP_AddImm: {            /* in1 */
 68749   pIn1 = &aMem[pOp->p1];
 68750   memAboutToChange(p, pIn1);
 68751   sqlite3VdbeMemIntegerify(pIn1);
 68752   pIn1->u.i += pOp->p2;
 68753   break;
 68756 /* Opcode: MustBeInt P1 P2 * * *
 68757 ** 
 68758 ** Force the value in register P1 to be an integer.  If the value
 68759 ** in P1 is not an integer and cannot be converted into an integer
 68760 ** without data loss, then jump immediately to P2, or if P2==0
 68761 ** raise an SQLITE_MISMATCH exception.
 68762 */
 68763 case OP_MustBeInt: {            /* jump, in1 */
 68764   pIn1 = &aMem[pOp->p1];
 68765   if( (pIn1->flags & MEM_Int)==0 ){
 68766     applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
 68767     VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
 68768     if( (pIn1->flags & MEM_Int)==0 ){
 68769       if( pOp->p2==0 ){
 68770         rc = SQLITE_MISMATCH;
 68771         goto abort_due_to_error;
 68772       }else{
 68773         pc = pOp->p2 - 1;
 68774         break;
 68778   MemSetTypeFlag(pIn1, MEM_Int);
 68779   break;
 68782 #ifndef SQLITE_OMIT_FLOATING_POINT
 68783 /* Opcode: RealAffinity P1 * * * *
 68784 **
 68785 ** If register P1 holds an integer convert it to a real value.
 68786 **
 68787 ** This opcode is used when extracting information from a column that
 68788 ** has REAL affinity.  Such column values may still be stored as
 68789 ** integers, for space efficiency, but after extraction we want them
 68790 ** to have only a real value.
 68791 */
 68792 case OP_RealAffinity: {                  /* in1 */
 68793   pIn1 = &aMem[pOp->p1];
 68794   if( pIn1->flags & MEM_Int ){
 68795     sqlite3VdbeMemRealify(pIn1);
 68797   break;
 68799 #endif
 68801 #ifndef SQLITE_OMIT_CAST
 68802 /* Opcode: ToText P1 * * * *
 68803 **
 68804 ** Force the value in register P1 to be text.
 68805 ** If the value is numeric, convert it to a string using the
 68806 ** equivalent of sprintf().  Blob values are unchanged and
 68807 ** are afterwards simply interpreted as text.
 68808 **
 68809 ** A NULL value is not changed by this routine.  It remains NULL.
 68810 */
 68811 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
 68812   pIn1 = &aMem[pOp->p1];
 68813   memAboutToChange(p, pIn1);
 68814   if( pIn1->flags & MEM_Null ) break;
 68815   assert( MEM_Str==(MEM_Blob>>3) );
 68816   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
 68817   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 68818   rc = ExpandBlob(pIn1);
 68819   assert( pIn1->flags & MEM_Str || db->mallocFailed );
 68820   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
 68821   UPDATE_MAX_BLOBSIZE(pIn1);
 68822   break;
 68825 /* Opcode: ToBlob P1 * * * *
 68826 **
 68827 ** Force the value in register P1 to be a BLOB.
 68828 ** If the value is numeric, convert it to a string first.
 68829 ** Strings are simply reinterpreted as blobs with no change
 68830 ** to the underlying data.
 68831 **
 68832 ** A NULL value is not changed by this routine.  It remains NULL.
 68833 */
 68834 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
 68835   pIn1 = &aMem[pOp->p1];
 68836   if( pIn1->flags & MEM_Null ) break;
 68837   if( (pIn1->flags & MEM_Blob)==0 ){
 68838     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
 68839     assert( pIn1->flags & MEM_Str || db->mallocFailed );
 68840     MemSetTypeFlag(pIn1, MEM_Blob);
 68841   }else{
 68842     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
 68844   UPDATE_MAX_BLOBSIZE(pIn1);
 68845   break;
 68848 /* Opcode: ToNumeric P1 * * * *
 68849 **
 68850 ** Force the value in register P1 to be numeric (either an
 68851 ** integer or a floating-point number.)
 68852 ** If the value is text or blob, try to convert it to an using the
 68853 ** equivalent of atoi() or atof() and store 0 if no such conversion 
 68854 ** is possible.
 68855 **
 68856 ** A NULL value is not changed by this routine.  It remains NULL.
 68857 */
 68858 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
 68859   pIn1 = &aMem[pOp->p1];
 68860   sqlite3VdbeMemNumerify(pIn1);
 68861   break;
 68863 #endif /* SQLITE_OMIT_CAST */
 68865 /* Opcode: ToInt P1 * * * *
 68866 **
 68867 ** Force the value in register P1 to be an integer.  If
 68868 ** The value is currently a real number, drop its fractional part.
 68869 ** If the value is text or blob, try to convert it to an integer using the
 68870 ** equivalent of atoi() and store 0 if no such conversion is possible.
 68871 **
 68872 ** A NULL value is not changed by this routine.  It remains NULL.
 68873 */
 68874 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
 68875   pIn1 = &aMem[pOp->p1];
 68876   if( (pIn1->flags & MEM_Null)==0 ){
 68877     sqlite3VdbeMemIntegerify(pIn1);
 68879   break;
 68882 #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
 68883 /* Opcode: ToReal P1 * * * *
 68884 **
 68885 ** Force the value in register P1 to be a floating point number.
 68886 ** If The value is currently an integer, convert it.
 68887 ** If the value is text or blob, try to convert it to an integer using the
 68888 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
 68889 **
 68890 ** A NULL value is not changed by this routine.  It remains NULL.
 68891 */
 68892 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
 68893   pIn1 = &aMem[pOp->p1];
 68894   memAboutToChange(p, pIn1);
 68895   if( (pIn1->flags & MEM_Null)==0 ){
 68896     sqlite3VdbeMemRealify(pIn1);
 68898   break;
 68900 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
 68902 /* Opcode: Lt P1 P2 P3 P4 P5
 68903 ** Synopsis: if r[P1]<r[P3] goto P2
 68904 **
 68905 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
 68906 ** jump to address P2.  
 68907 **
 68908 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
 68909 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
 68910 ** bit is clear then fall through if either operand is NULL.
 68911 **
 68912 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
 68913 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
 68914 ** to coerce both inputs according to this affinity before the
 68915 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
 68916 ** affinity is used. Note that the affinity conversions are stored
 68917 ** back into the input registers P1 and P3.  So this opcode can cause
 68918 ** persistent changes to registers P1 and P3.
 68919 **
 68920 ** Once any conversions have taken place, and neither value is NULL, 
 68921 ** the values are compared. If both values are blobs then memcmp() is
 68922 ** used to determine the results of the comparison.  If both values
 68923 ** are text, then the appropriate collating function specified in
 68924 ** P4 is  used to do the comparison.  If P4 is not specified then
 68925 ** memcmp() is used to compare text string.  If both values are
 68926 ** numeric, then a numeric comparison is used. If the two values
 68927 ** are of different types, then numbers are considered less than
 68928 ** strings and strings are considered less than blobs.
 68929 **
 68930 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
 68931 ** store a boolean result (either 0, or 1, or NULL) in register P2.
 68932 **
 68933 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
 68934 ** equal to one another, provided that they do not have their MEM_Cleared
 68935 ** bit set.
 68936 */
 68937 /* Opcode: Ne P1 P2 P3 P4 P5
 68938 ** Synopsis: if r[P1]!=r[P3] goto P2
 68939 **
 68940 ** This works just like the Lt opcode except that the jump is taken if
 68941 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
 68942 ** additional information.
 68943 **
 68944 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 68945 ** true or false and is never NULL.  If both operands are NULL then the result
 68946 ** of comparison is false.  If either operand is NULL then the result is true.
 68947 ** If neither operand is NULL the result is the same as it would be if
 68948 ** the SQLITE_NULLEQ flag were omitted from P5.
 68949 */
 68950 /* Opcode: Eq P1 P2 P3 P4 P5
 68951 ** Synopsis: if r[P1]==r[P3] goto P2
 68952 **
 68953 ** This works just like the Lt opcode except that the jump is taken if
 68954 ** the operands in registers P1 and P3 are equal.
 68955 ** See the Lt opcode for additional information.
 68956 **
 68957 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
 68958 ** true or false and is never NULL.  If both operands are NULL then the result
 68959 ** of comparison is true.  If either operand is NULL then the result is false.
 68960 ** If neither operand is NULL the result is the same as it would be if
 68961 ** the SQLITE_NULLEQ flag were omitted from P5.
 68962 */
 68963 /* Opcode: Le P1 P2 P3 P4 P5
 68964 ** Synopsis: if r[P1]<=r[P3] goto P2
 68965 **
 68966 ** This works just like the Lt opcode except that the jump is taken if
 68967 ** the content of register P3 is less than or equal to the content of
 68968 ** register P1.  See the Lt opcode for additional information.
 68969 */
 68970 /* Opcode: Gt P1 P2 P3 P4 P5
 68971 ** Synopsis: if r[P1]>r[P3] goto P2
 68972 **
 68973 ** This works just like the Lt opcode except that the jump is taken if
 68974 ** the content of register P3 is greater than the content of
 68975 ** register P1.  See the Lt opcode for additional information.
 68976 */
 68977 /* Opcode: Ge P1 P2 P3 P4 P5
 68978 ** Synopsis: if r[P1]>=r[P3] goto P2
 68979 **
 68980 ** This works just like the Lt opcode except that the jump is taken if
 68981 ** the content of register P3 is greater than or equal to the content of
 68982 ** register P1.  See the Lt opcode for additional information.
 68983 */
 68984 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
 68985 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
 68986 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
 68987 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
 68988 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
 68989 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
 68990   int res;            /* Result of the comparison of pIn1 against pIn3 */
 68991   char affinity;      /* Affinity to use for comparison */
 68992   u16 flags1;         /* Copy of initial value of pIn1->flags */
 68993   u16 flags3;         /* Copy of initial value of pIn3->flags */
 68995   pIn1 = &aMem[pOp->p1];
 68996   pIn3 = &aMem[pOp->p3];
 68997   flags1 = pIn1->flags;
 68998   flags3 = pIn3->flags;
 68999   if( (flags1 | flags3)&MEM_Null ){
 69000     /* One or both operands are NULL */
 69001     if( pOp->p5 & SQLITE_NULLEQ ){
 69002       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
 69003       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
 69004       ** or not both operands are null.
 69005       */
 69006       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
 69007       assert( (flags1 & MEM_Cleared)==0 );
 69008       assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
 69009       if( (flags1&MEM_Null)!=0
 69010        && (flags3&MEM_Null)!=0
 69011        && (flags3&MEM_Cleared)==0
 69012       ){
 69013         res = 0;  /* Results are equal */
 69014       }else{
 69015         res = 1;  /* Results are not equal */
 69017     }else{
 69018       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
 69019       ** then the result is always NULL.
 69020       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
 69021       */
 69022       if( pOp->p5 & SQLITE_STOREP2 ){
 69023         pOut = &aMem[pOp->p2];
 69024         MemSetTypeFlag(pOut, MEM_Null);
 69025         REGISTER_TRACE(pOp->p2, pOut);
 69026       }else{
 69027         VdbeBranchTaken(2,3);
 69028         if( pOp->p5 & SQLITE_JUMPIFNULL ){
 69029           pc = pOp->p2-1;
 69032       break;
 69034   }else{
 69035     /* Neither operand is NULL.  Do a comparison. */
 69036     affinity = pOp->p5 & SQLITE_AFF_MASK;
 69037     if( affinity ){
 69038       applyAffinity(pIn1, affinity, encoding);
 69039       applyAffinity(pIn3, affinity, encoding);
 69040       if( db->mallocFailed ) goto no_mem;
 69043     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
 69044     ExpandBlob(pIn1);
 69045     ExpandBlob(pIn3);
 69046     res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
 69048   switch( pOp->opcode ){
 69049     case OP_Eq:    res = res==0;     break;
 69050     case OP_Ne:    res = res!=0;     break;
 69051     case OP_Lt:    res = res<0;      break;
 69052     case OP_Le:    res = res<=0;     break;
 69053     case OP_Gt:    res = res>0;      break;
 69054     default:       res = res>=0;     break;
 69057   if( pOp->p5 & SQLITE_STOREP2 ){
 69058     pOut = &aMem[pOp->p2];
 69059     memAboutToChange(p, pOut);
 69060     MemSetTypeFlag(pOut, MEM_Int);
 69061     pOut->u.i = res;
 69062     REGISTER_TRACE(pOp->p2, pOut);
 69063   }else{
 69064     VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
 69065     if( res ){
 69066       pc = pOp->p2-1;
 69069   /* Undo any changes made by applyAffinity() to the input registers. */
 69070   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
 69071   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
 69072   break;
 69075 /* Opcode: Permutation * * * P4 *
 69076 **
 69077 ** Set the permutation used by the OP_Compare operator to be the array
 69078 ** of integers in P4.
 69079 **
 69080 ** The permutation is only valid until the next OP_Compare that has
 69081 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
 69082 ** occur immediately prior to the OP_Compare.
 69083 */
 69084 case OP_Permutation: {
 69085   assert( pOp->p4type==P4_INTARRAY );
 69086   assert( pOp->p4.ai );
 69087   aPermute = pOp->p4.ai;
 69088   break;
 69091 /* Opcode: Compare P1 P2 P3 P4 P5
 69092 **
 69093 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
 69094 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
 69095 ** the comparison for use by the next OP_Jump instruct.
 69096 **
 69097 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
 69098 ** determined by the most recent OP_Permutation operator.  If the
 69099 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
 69100 ** order.
 69101 **
 69102 ** P4 is a KeyInfo structure that defines collating sequences and sort
 69103 ** orders for the comparison.  The permutation applies to registers
 69104 ** only.  The KeyInfo elements are used sequentially.
 69105 **
 69106 ** The comparison is a sort comparison, so NULLs compare equal,
 69107 ** NULLs are less than numbers, numbers are less than strings,
 69108 ** and strings are less than blobs.
 69109 */
 69110 case OP_Compare: {
 69111   int n;
 69112   int i;
 69113   int p1;
 69114   int p2;
 69115   const KeyInfo *pKeyInfo;
 69116   int idx;
 69117   CollSeq *pColl;    /* Collating sequence to use on this term */
 69118   int bRev;          /* True for DESCENDING sort order */
 69120   if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
 69121   n = pOp->p3;
 69122   pKeyInfo = pOp->p4.pKeyInfo;
 69123   assert( n>0 );
 69124   assert( pKeyInfo!=0 );
 69125   p1 = pOp->p1;
 69126   p2 = pOp->p2;
 69127 #if SQLITE_DEBUG
 69128   if( aPermute ){
 69129     int k, mx = 0;
 69130     for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
 69131     assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
 69132     assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
 69133   }else{
 69134     assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
 69135     assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
 69137 #endif /* SQLITE_DEBUG */
 69138   for(i=0; i<n; i++){
 69139     idx = aPermute ? aPermute[i] : i;
 69140     assert( memIsValid(&aMem[p1+idx]) );
 69141     assert( memIsValid(&aMem[p2+idx]) );
 69142     REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
 69143     REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
 69144     assert( i<pKeyInfo->nField );
 69145     pColl = pKeyInfo->aColl[i];
 69146     bRev = pKeyInfo->aSortOrder[i];
 69147     iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
 69148     if( iCompare ){
 69149       if( bRev ) iCompare = -iCompare;
 69150       break;
 69153   aPermute = 0;
 69154   break;
 69157 /* Opcode: Jump P1 P2 P3 * *
 69158 **
 69159 ** Jump to the instruction at address P1, P2, or P3 depending on whether
 69160 ** in the most recent OP_Compare instruction the P1 vector was less than
 69161 ** equal to, or greater than the P2 vector, respectively.
 69162 */
 69163 case OP_Jump: {             /* jump */
 69164   if( iCompare<0 ){
 69165     pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
 69166   }else if( iCompare==0 ){
 69167     pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
 69168   }else{
 69169     pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
 69171   break;
 69174 /* Opcode: And P1 P2 P3 * *
 69175 ** Synopsis: r[P3]=(r[P1] && r[P2])
 69176 **
 69177 ** Take the logical AND of the values in registers P1 and P2 and
 69178 ** write the result into register P3.
 69179 **
 69180 ** If either P1 or P2 is 0 (false) then the result is 0 even if
 69181 ** the other input is NULL.  A NULL and true or two NULLs give
 69182 ** a NULL output.
 69183 */
 69184 /* Opcode: Or P1 P2 P3 * *
 69185 ** Synopsis: r[P3]=(r[P1] || r[P2])
 69186 **
 69187 ** Take the logical OR of the values in register P1 and P2 and
 69188 ** store the answer in register P3.
 69189 **
 69190 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
 69191 ** even if the other input is NULL.  A NULL and false or two NULLs
 69192 ** give a NULL output.
 69193 */
 69194 case OP_And:              /* same as TK_AND, in1, in2, out3 */
 69195 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
 69196   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 69197   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
 69199   pIn1 = &aMem[pOp->p1];
 69200   if( pIn1->flags & MEM_Null ){
 69201     v1 = 2;
 69202   }else{
 69203     v1 = sqlite3VdbeIntValue(pIn1)!=0;
 69205   pIn2 = &aMem[pOp->p2];
 69206   if( pIn2->flags & MEM_Null ){
 69207     v2 = 2;
 69208   }else{
 69209     v2 = sqlite3VdbeIntValue(pIn2)!=0;
 69211   if( pOp->opcode==OP_And ){
 69212     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
 69213     v1 = and_logic[v1*3+v2];
 69214   }else{
 69215     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
 69216     v1 = or_logic[v1*3+v2];
 69218   pOut = &aMem[pOp->p3];
 69219   if( v1==2 ){
 69220     MemSetTypeFlag(pOut, MEM_Null);
 69221   }else{
 69222     pOut->u.i = v1;
 69223     MemSetTypeFlag(pOut, MEM_Int);
 69225   break;
 69228 /* Opcode: Not P1 P2 * * *
 69229 ** Synopsis: r[P2]= !r[P1]
 69230 **
 69231 ** Interpret the value in register P1 as a boolean value.  Store the
 69232 ** boolean complement in register P2.  If the value in register P1 is 
 69233 ** NULL, then a NULL is stored in P2.
 69234 */
 69235 case OP_Not: {                /* same as TK_NOT, in1, out2 */
 69236   pIn1 = &aMem[pOp->p1];
 69237   pOut = &aMem[pOp->p2];
 69238   if( pIn1->flags & MEM_Null ){
 69239     sqlite3VdbeMemSetNull(pOut);
 69240   }else{
 69241     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
 69243   break;
 69246 /* Opcode: BitNot P1 P2 * * *
 69247 ** Synopsis: r[P1]= ~r[P1]
 69248 **
 69249 ** Interpret the content of register P1 as an integer.  Store the
 69250 ** ones-complement of the P1 value into register P2.  If P1 holds
 69251 ** a NULL then store a NULL in P2.
 69252 */
 69253 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
 69254   pIn1 = &aMem[pOp->p1];
 69255   pOut = &aMem[pOp->p2];
 69256   if( pIn1->flags & MEM_Null ){
 69257     sqlite3VdbeMemSetNull(pOut);
 69258   }else{
 69259     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
 69261   break;
 69264 /* Opcode: Once P1 P2 * * *
 69265 **
 69266 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
 69267 ** set the flag and fall through to the next instruction.  In other words,
 69268 ** this opcode causes all following opcodes up through P2 (but not including
 69269 ** P2) to run just once and to be skipped on subsequent times through the loop.
 69270 */
 69271 case OP_Once: {             /* jump */
 69272   assert( pOp->p1<p->nOnceFlag );
 69273   VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
 69274   if( p->aOnceFlag[pOp->p1] ){
 69275     pc = pOp->p2-1;
 69276   }else{
 69277     p->aOnceFlag[pOp->p1] = 1;
 69279   break;
 69282 /* Opcode: If P1 P2 P3 * *
 69283 **
 69284 ** Jump to P2 if the value in register P1 is true.  The value
 69285 ** is considered true if it is numeric and non-zero.  If the value
 69286 ** in P1 is NULL then take the jump if P3 is non-zero.
 69287 */
 69288 /* Opcode: IfNot P1 P2 P3 * *
 69289 **
 69290 ** Jump to P2 if the value in register P1 is False.  The value
 69291 ** is considered false if it has a numeric value of zero.  If the value
 69292 ** in P1 is NULL then take the jump if P3 is zero.
 69293 */
 69294 case OP_If:                 /* jump, in1 */
 69295 case OP_IfNot: {            /* jump, in1 */
 69296   int c;
 69297   pIn1 = &aMem[pOp->p1];
 69298   if( pIn1->flags & MEM_Null ){
 69299     c = pOp->p3;
 69300   }else{
 69301 #ifdef SQLITE_OMIT_FLOATING_POINT
 69302     c = sqlite3VdbeIntValue(pIn1)!=0;
 69303 #else
 69304     c = sqlite3VdbeRealValue(pIn1)!=0.0;
 69305 #endif
 69306     if( pOp->opcode==OP_IfNot ) c = !c;
 69308   VdbeBranchTaken(c!=0, 2);
 69309   if( c ){
 69310     pc = pOp->p2-1;
 69312   break;
 69315 /* Opcode: IsNull P1 P2 * * *
 69316 ** Synopsis:  if r[P1]==NULL goto P2
 69317 **
 69318 ** Jump to P2 if the value in register P1 is NULL.
 69319 */
 69320 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
 69321   pIn1 = &aMem[pOp->p1];
 69322   VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
 69323   if( (pIn1->flags & MEM_Null)!=0 ){
 69324     pc = pOp->p2 - 1;
 69326   break;
 69329 /* Opcode: NotNull P1 P2 * * *
 69330 ** Synopsis: if r[P1]!=NULL goto P2
 69331 **
 69332 ** Jump to P2 if the value in register P1 is not NULL.  
 69333 */
 69334 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
 69335   pIn1 = &aMem[pOp->p1];
 69336   VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
 69337   if( (pIn1->flags & MEM_Null)==0 ){
 69338     pc = pOp->p2 - 1;
 69340   break;
 69343 /* Opcode: Column P1 P2 P3 P4 P5
 69344 ** Synopsis:  r[P3]=PX
 69345 **
 69346 ** Interpret the data that cursor P1 points to as a structure built using
 69347 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
 69348 ** information about the format of the data.)  Extract the P2-th column
 69349 ** from this record.  If there are less that (P2+1) 
 69350 ** values in the record, extract a NULL.
 69351 **
 69352 ** The value extracted is stored in register P3.
 69353 **
 69354 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
 69355 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
 69356 ** the result.
 69357 **
 69358 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
 69359 ** then the cache of the cursor is reset prior to extracting the column.
 69360 ** The first OP_Column against a pseudo-table after the value of the content
 69361 ** register has changed should have this bit set.
 69362 **
 69363 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
 69364 ** the result is guaranteed to only be used as the argument of a length()
 69365 ** or typeof() function, respectively.  The loading of large blobs can be
 69366 ** skipped for length() and all content loading can be skipped for typeof().
 69367 */
 69368 case OP_Column: {
 69369   i64 payloadSize64; /* Number of bytes in the record */
 69370   int p2;            /* column number to retrieve */
 69371   VdbeCursor *pC;    /* The VDBE cursor */
 69372   BtCursor *pCrsr;   /* The BTree cursor */
 69373   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
 69374   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
 69375   int len;           /* The length of the serialized data for the column */
 69376   int i;             /* Loop counter */
 69377   Mem *pDest;        /* Where to write the extracted value */
 69378   Mem sMem;          /* For storing the record being decoded */
 69379   const u8 *zData;   /* Part of the record being decoded */
 69380   const u8 *zHdr;    /* Next unparsed byte of the header */
 69381   const u8 *zEndHdr; /* Pointer to first byte after the header */
 69382   u32 offset;        /* Offset into the data */
 69383   u32 szField;       /* Number of bytes in the content of a field */
 69384   u32 avail;         /* Number of bytes of available data */
 69385   u32 t;             /* A type code from the record header */
 69386   Mem *pReg;         /* PseudoTable input register */
 69388   p2 = pOp->p2;
 69389   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 69390   pDest = &aMem[pOp->p3];
 69391   memAboutToChange(p, pDest);
 69392   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 69393   pC = p->apCsr[pOp->p1];
 69394   assert( pC!=0 );
 69395   assert( p2<pC->nField );
 69396   aType = pC->aType;
 69397   aOffset = aType + pC->nField;
 69398 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69399   assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
 69400 #endif
 69401   pCrsr = pC->pCursor;
 69402   assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
 69403   assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
 69405   /* If the cursor cache is stale, bring it up-to-date */
 69406   rc = sqlite3VdbeCursorMoveto(pC);
 69407   if( rc ) goto abort_due_to_error;
 69408   if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
 69409     if( pC->nullRow ){
 69410       if( pCrsr==0 ){
 69411         assert( pC->pseudoTableReg>0 );
 69412         pReg = &aMem[pC->pseudoTableReg];
 69413         assert( pReg->flags & MEM_Blob );
 69414         assert( memIsValid(pReg) );
 69415         pC->payloadSize = pC->szRow = avail = pReg->n;
 69416         pC->aRow = (u8*)pReg->z;
 69417       }else{
 69418         MemSetTypeFlag(pDest, MEM_Null);
 69419         goto op_column_out;
 69421     }else{
 69422       assert( pCrsr );
 69423       if( pC->isTable==0 ){
 69424         assert( sqlite3BtreeCursorIsValid(pCrsr) );
 69425         VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
 69426         assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
 69427         /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
 69428         ** payload size, so it is impossible for payloadSize64 to be
 69429         ** larger than 32 bits. */
 69430         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
 69431         pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
 69432         pC->payloadSize = (u32)payloadSize64;
 69433       }else{
 69434         assert( sqlite3BtreeCursorIsValid(pCrsr) );
 69435         VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
 69436         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
 69437         pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
 69439       assert( avail<=65536 );  /* Maximum page size is 64KiB */
 69440       if( pC->payloadSize <= (u32)avail ){
 69441         pC->szRow = pC->payloadSize;
 69442       }else{
 69443         pC->szRow = avail;
 69445       if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 69446         goto too_big;
 69449     pC->cacheStatus = p->cacheCtr;
 69450     pC->iHdrOffset = getVarint32(pC->aRow, offset);
 69451     pC->nHdrParsed = 0;
 69452     aOffset[0] = offset;
 69453     if( avail<offset ){
 69454       /* pC->aRow does not have to hold the entire row, but it does at least
 69455       ** need to cover the header of the record.  If pC->aRow does not contain
 69456       ** the complete header, then set it to zero, forcing the header to be
 69457       ** dynamically allocated. */
 69458       pC->aRow = 0;
 69459       pC->szRow = 0;
 69462     /* Make sure a corrupt database has not given us an oversize header.
 69463     ** Do this now to avoid an oversize memory allocation.
 69464     **
 69465     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
 69466     ** types use so much data space that there can only be 4096 and 32 of
 69467     ** them, respectively.  So the maximum header length results from a
 69468     ** 3-byte type for each of the maximum of 32768 columns plus three
 69469     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
 69470     */
 69471     if( offset > 98307 || offset > pC->payloadSize ){
 69472       rc = SQLITE_CORRUPT_BKPT;
 69473       goto op_column_error;
 69477   /* Make sure at least the first p2+1 entries of the header have been
 69478   ** parsed and valid information is in aOffset[] and aType[].
 69479   */
 69480   if( pC->nHdrParsed<=p2 ){
 69481     /* If there is more header available for parsing in the record, try
 69482     ** to extract additional fields up through the p2+1-th field 
 69483     */
 69484     if( pC->iHdrOffset<aOffset[0] ){
 69485       /* Make sure zData points to enough of the record to cover the header. */
 69486       if( pC->aRow==0 ){
 69487         memset(&sMem, 0, sizeof(sMem));
 69488         rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], 
 69489                                      !pC->isTable, &sMem);
 69490         if( rc!=SQLITE_OK ){
 69491           goto op_column_error;
 69493         zData = (u8*)sMem.z;
 69494       }else{
 69495         zData = pC->aRow;
 69498       /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
 69499       i = pC->nHdrParsed;
 69500       offset = aOffset[i];
 69501       zHdr = zData + pC->iHdrOffset;
 69502       zEndHdr = zData + aOffset[0];
 69503       assert( i<=p2 && zHdr<zEndHdr );
 69504       do{
 69505         if( zHdr[0]<0x80 ){
 69506           t = zHdr[0];
 69507           zHdr++;
 69508         }else{
 69509           zHdr += sqlite3GetVarint32(zHdr, &t);
 69511         aType[i] = t;
 69512         szField = sqlite3VdbeSerialTypeLen(t);
 69513         offset += szField;
 69514         if( offset<szField ){  /* True if offset overflows */
 69515           zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
 69516           break;
 69518         i++;
 69519         aOffset[i] = offset;
 69520       }while( i<=p2 && zHdr<zEndHdr );
 69521       pC->nHdrParsed = i;
 69522       pC->iHdrOffset = (u32)(zHdr - zData);
 69523       if( pC->aRow==0 ){
 69524         sqlite3VdbeMemRelease(&sMem);
 69525         sMem.flags = MEM_Null;
 69528       /* If we have read more header data than was contained in the header,
 69529       ** or if the end of the last field appears to be past the end of the
 69530       ** record, or if the end of the last field appears to be before the end
 69531       ** of the record (when all fields present), then we must be dealing 
 69532       ** with a corrupt database.
 69533       */
 69534       if( (zHdr > zEndHdr)
 69535        || (offset > pC->payloadSize)
 69536        || (zHdr==zEndHdr && offset!=pC->payloadSize)
 69537       ){
 69538         rc = SQLITE_CORRUPT_BKPT;
 69539         goto op_column_error;
 69543     /* If after trying to extra new entries from the header, nHdrParsed is
 69544     ** still not up to p2, that means that the record has fewer than p2
 69545     ** columns.  So the result will be either the default value or a NULL.
 69546     */
 69547     if( pC->nHdrParsed<=p2 ){
 69548       if( pOp->p4type==P4_MEM ){
 69549         sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
 69550       }else{
 69551         MemSetTypeFlag(pDest, MEM_Null);
 69553       goto op_column_out;
 69557   /* Extract the content for the p2+1-th column.  Control can only
 69558   ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
 69559   ** all valid.
 69560   */
 69561   assert( p2<pC->nHdrParsed );
 69562   assert( rc==SQLITE_OK );
 69563   assert( sqlite3VdbeCheckMemInvariants(pDest) );
 69564   if( pC->szRow>=aOffset[p2+1] ){
 69565     /* This is the common case where the desired content fits on the original
 69566     ** page - where the content is not on an overflow page */
 69567     VdbeMemRelease(pDest);
 69568     sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
 69569   }else{
 69570     /* This branch happens only when content is on overflow pages */
 69571     t = aType[p2];
 69572     if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
 69573           && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
 69574      || (len = sqlite3VdbeSerialTypeLen(t))==0
 69575     ){
 69576       /* Content is irrelevant for the typeof() function and for
 69577       ** the length(X) function if X is a blob.  So we might as well use
 69578       ** bogus content rather than reading content from disk.  NULL works
 69579       ** for text and blob and whatever is in the payloadSize64 variable
 69580       ** will work for everything else.  Content is also irrelevant if
 69581       ** the content length is 0. */
 69582       zData = t<=13 ? (u8*)&payloadSize64 : 0;
 69583       sMem.zMalloc = 0;
 69584     }else{
 69585       memset(&sMem, 0, sizeof(sMem));
 69586       sqlite3VdbeMemMove(&sMem, pDest);
 69587       rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
 69588                                    &sMem);
 69589       if( rc!=SQLITE_OK ){
 69590         goto op_column_error;
 69592       zData = (u8*)sMem.z;
 69594     sqlite3VdbeSerialGet(zData, t, pDest);
 69595     /* If we dynamically allocated space to hold the data (in the
 69596     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
 69597     ** dynamically allocated space over to the pDest structure.
 69598     ** This prevents a memory copy. */
 69599     if( sMem.zMalloc ){
 69600       assert( sMem.z==sMem.zMalloc );
 69601       assert( VdbeMemDynamic(pDest)==0 );
 69602       assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
 69603       pDest->flags &= ~(MEM_Ephem|MEM_Static);
 69604       pDest->flags |= MEM_Term;
 69605       pDest->z = sMem.z;
 69606       pDest->zMalloc = sMem.zMalloc;
 69609   pDest->enc = encoding;
 69611 op_column_out:
 69612   Deephemeralize(pDest);
 69613 op_column_error:
 69614   UPDATE_MAX_BLOBSIZE(pDest);
 69615   REGISTER_TRACE(pOp->p3, pDest);
 69616   break;
 69619 /* Opcode: Affinity P1 P2 * P4 *
 69620 ** Synopsis: affinity(r[P1@P2])
 69621 **
 69622 ** Apply affinities to a range of P2 registers starting with P1.
 69623 **
 69624 ** P4 is a string that is P2 characters long. The nth character of the
 69625 ** string indicates the column affinity that should be used for the nth
 69626 ** memory cell in the range.
 69627 */
 69628 case OP_Affinity: {
 69629   const char *zAffinity;   /* The affinity to be applied */
 69630   char cAff;               /* A single character of affinity */
 69632   zAffinity = pOp->p4.z;
 69633   assert( zAffinity!=0 );
 69634   assert( zAffinity[pOp->p2]==0 );
 69635   pIn1 = &aMem[pOp->p1];
 69636   while( (cAff = *(zAffinity++))!=0 ){
 69637     assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
 69638     assert( memIsValid(pIn1) );
 69639     applyAffinity(pIn1, cAff, encoding);
 69640     pIn1++;
 69642   break;
 69645 /* Opcode: MakeRecord P1 P2 P3 P4 *
 69646 ** Synopsis: r[P3]=mkrec(r[P1@P2])
 69647 **
 69648 ** Convert P2 registers beginning with P1 into the [record format]
 69649 ** use as a data record in a database table or as a key
 69650 ** in an index.  The OP_Column opcode can decode the record later.
 69651 **
 69652 ** P4 may be a string that is P2 characters long.  The nth character of the
 69653 ** string indicates the column affinity that should be used for the nth
 69654 ** field of the index key.
 69655 **
 69656 ** The mapping from character to affinity is given by the SQLITE_AFF_
 69657 ** macros defined in sqliteInt.h.
 69658 **
 69659 ** If P4 is NULL then all index fields have the affinity NONE.
 69660 */
 69661 case OP_MakeRecord: {
 69662   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
 69663   Mem *pRec;             /* The new record */
 69664   u64 nData;             /* Number of bytes of data space */
 69665   int nHdr;              /* Number of bytes of header space */
 69666   i64 nByte;             /* Data space required for this record */
 69667   int nZero;             /* Number of zero bytes at the end of the record */
 69668   int nVarint;           /* Number of bytes in a varint */
 69669   u32 serial_type;       /* Type field */
 69670   Mem *pData0;           /* First field to be combined into the record */
 69671   Mem *pLast;            /* Last field of the record */
 69672   int nField;            /* Number of fields in the record */
 69673   char *zAffinity;       /* The affinity string for the record */
 69674   int file_format;       /* File format to use for encoding */
 69675   int i;                 /* Space used in zNewRecord[] header */
 69676   int j;                 /* Space used in zNewRecord[] content */
 69677   int len;               /* Length of a field */
 69679   /* Assuming the record contains N fields, the record format looks
 69680   ** like this:
 69681   **
 69682   ** ------------------------------------------------------------------------
 69683   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
 69684   ** ------------------------------------------------------------------------
 69685   **
 69686   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
 69687   ** and so froth.
 69688   **
 69689   ** Each type field is a varint representing the serial type of the 
 69690   ** corresponding data element (see sqlite3VdbeSerialType()). The
 69691   ** hdr-size field is also a varint which is the offset from the beginning
 69692   ** of the record to data0.
 69693   */
 69694   nData = 0;         /* Number of bytes of data space */
 69695   nHdr = 0;          /* Number of bytes of header space */
 69696   nZero = 0;         /* Number of zero bytes at the end of the record */
 69697   nField = pOp->p1;
 69698   zAffinity = pOp->p4.z;
 69699   assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
 69700   pData0 = &aMem[nField];
 69701   nField = pOp->p2;
 69702   pLast = &pData0[nField-1];
 69703   file_format = p->minWriteFileFormat;
 69705   /* Identify the output register */
 69706   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
 69707   pOut = &aMem[pOp->p3];
 69708   memAboutToChange(p, pOut);
 69710   /* Apply the requested affinity to all inputs
 69711   */
 69712   assert( pData0<=pLast );
 69713   if( zAffinity ){
 69714     pRec = pData0;
 69715     do{
 69716       applyAffinity(pRec++, *(zAffinity++), encoding);
 69717       assert( zAffinity[0]==0 || pRec<=pLast );
 69718     }while( zAffinity[0] );
 69721   /* Loop through the elements that will make up the record to figure
 69722   ** out how much space is required for the new record.
 69723   */
 69724   pRec = pLast;
 69725   do{
 69726     assert( memIsValid(pRec) );
 69727     serial_type = sqlite3VdbeSerialType(pRec, file_format);
 69728     len = sqlite3VdbeSerialTypeLen(serial_type);
 69729     if( pRec->flags & MEM_Zero ){
 69730       if( nData ){
 69731         sqlite3VdbeMemExpandBlob(pRec);
 69732       }else{
 69733         nZero += pRec->u.nZero;
 69734         len -= pRec->u.nZero;
 69737     nData += len;
 69738     testcase( serial_type==127 );
 69739     testcase( serial_type==128 );
 69740     nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
 69741   }while( (--pRec)>=pData0 );
 69743   /* Add the initial header varint and total the size */
 69744   testcase( nHdr==126 );
 69745   testcase( nHdr==127 );
 69746   if( nHdr<=126 ){
 69747     /* The common case */
 69748     nHdr += 1;
 69749   }else{
 69750     /* Rare case of a really large header */
 69751     nVarint = sqlite3VarintLen(nHdr);
 69752     nHdr += nVarint;
 69753     if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
 69755   nByte = nHdr+nData;
 69756   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 69757     goto too_big;
 69760   /* Make sure the output register has a buffer large enough to store 
 69761   ** the new record. The output register (pOp->p3) is not allowed to
 69762   ** be one of the input registers (because the following call to
 69763   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
 69764   */
 69765   if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
 69766     goto no_mem;
 69768   zNewRecord = (u8 *)pOut->z;
 69770   /* Write the record */
 69771   i = putVarint32(zNewRecord, nHdr);
 69772   j = nHdr;
 69773   assert( pData0<=pLast );
 69774   pRec = pData0;
 69775   do{
 69776     serial_type = sqlite3VdbeSerialType(pRec, file_format);
 69777     i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
 69778     j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
 69779   }while( (++pRec)<=pLast );
 69780   assert( i==nHdr );
 69781   assert( j==nByte );
 69783   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 69784   pOut->n = (int)nByte;
 69785   pOut->flags = MEM_Blob;
 69786   pOut->xDel = 0;
 69787   if( nZero ){
 69788     pOut->u.nZero = nZero;
 69789     pOut->flags |= MEM_Zero;
 69791   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
 69792   REGISTER_TRACE(pOp->p3, pOut);
 69793   UPDATE_MAX_BLOBSIZE(pOut);
 69794   break;
 69797 /* Opcode: Count P1 P2 * * *
 69798 ** Synopsis: r[P2]=count()
 69799 **
 69800 ** Store the number of entries (an integer value) in the table or index 
 69801 ** opened by cursor P1 in register P2
 69802 */
 69803 #ifndef SQLITE_OMIT_BTREECOUNT
 69804 case OP_Count: {         /* out2-prerelease */
 69805   i64 nEntry;
 69806   BtCursor *pCrsr;
 69808   pCrsr = p->apCsr[pOp->p1]->pCursor;
 69809   assert( pCrsr );
 69810   nEntry = 0;  /* Not needed.  Only used to silence a warning. */
 69811   rc = sqlite3BtreeCount(pCrsr, &nEntry);
 69812   pOut->u.i = nEntry;
 69813   break;
 69815 #endif
 69817 /* Opcode: Savepoint P1 * * P4 *
 69818 **
 69819 ** Open, release or rollback the savepoint named by parameter P4, depending
 69820 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
 69821 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
 69822 */
 69823 case OP_Savepoint: {
 69824   int p1;                         /* Value of P1 operand */
 69825   char *zName;                    /* Name of savepoint */
 69826   int nName;
 69827   Savepoint *pNew;
 69828   Savepoint *pSavepoint;
 69829   Savepoint *pTmp;
 69830   int iSavepoint;
 69831   int ii;
 69833   p1 = pOp->p1;
 69834   zName = pOp->p4.z;
 69836   /* Assert that the p1 parameter is valid. Also that if there is no open
 69837   ** transaction, then there cannot be any savepoints. 
 69838   */
 69839   assert( db->pSavepoint==0 || db->autoCommit==0 );
 69840   assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
 69841   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
 69842   assert( checkSavepointCount(db) );
 69843   assert( p->bIsReader );
 69845   if( p1==SAVEPOINT_BEGIN ){
 69846     if( db->nVdbeWrite>0 ){
 69847       /* A new savepoint cannot be created if there are active write 
 69848       ** statements (i.e. open read/write incremental blob handles).
 69849       */
 69850       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
 69851         "SQL statements in progress");
 69852       rc = SQLITE_BUSY;
 69853     }else{
 69854       nName = sqlite3Strlen30(zName);
 69856 #ifndef SQLITE_OMIT_VIRTUALTABLE
 69857       /* This call is Ok even if this savepoint is actually a transaction
 69858       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
 69859       ** If this is a transaction savepoint being opened, it is guaranteed
 69860       ** that the db->aVTrans[] array is empty.  */
 69861       assert( db->autoCommit==0 || db->nVTrans==0 );
 69862       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
 69863                                 db->nStatement+db->nSavepoint);
 69864       if( rc!=SQLITE_OK ) goto abort_due_to_error;
 69865 #endif
 69867       /* Create a new savepoint structure. */
 69868       pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
 69869       if( pNew ){
 69870         pNew->zName = (char *)&pNew[1];
 69871         memcpy(pNew->zName, zName, nName+1);
 69873         /* If there is no open transaction, then mark this as a special
 69874         ** "transaction savepoint". */
 69875         if( db->autoCommit ){
 69876           db->autoCommit = 0;
 69877           db->isTransactionSavepoint = 1;
 69878         }else{
 69879           db->nSavepoint++;
 69882         /* Link the new savepoint into the database handle's list. */
 69883         pNew->pNext = db->pSavepoint;
 69884         db->pSavepoint = pNew;
 69885         pNew->nDeferredCons = db->nDeferredCons;
 69886         pNew->nDeferredImmCons = db->nDeferredImmCons;
 69889   }else{
 69890     iSavepoint = 0;
 69892     /* Find the named savepoint. If there is no such savepoint, then an
 69893     ** an error is returned to the user.  */
 69894     for(
 69895       pSavepoint = db->pSavepoint; 
 69896       pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
 69897       pSavepoint = pSavepoint->pNext
 69898     ){
 69899       iSavepoint++;
 69901     if( !pSavepoint ){
 69902       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
 69903       rc = SQLITE_ERROR;
 69904     }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
 69905       /* It is not possible to release (commit) a savepoint if there are 
 69906       ** active write statements.
 69907       */
 69908       sqlite3SetString(&p->zErrMsg, db, 
 69909         "cannot release savepoint - SQL statements in progress"
 69910       );
 69911       rc = SQLITE_BUSY;
 69912     }else{
 69914       /* Determine whether or not this is a transaction savepoint. If so,
 69915       ** and this is a RELEASE command, then the current transaction 
 69916       ** is committed. 
 69917       */
 69918       int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
 69919       if( isTransaction && p1==SAVEPOINT_RELEASE ){
 69920         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 69921           goto vdbe_return;
 69923         db->autoCommit = 1;
 69924         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 69925           p->pc = pc;
 69926           db->autoCommit = 0;
 69927           p->rc = rc = SQLITE_BUSY;
 69928           goto vdbe_return;
 69930         db->isTransactionSavepoint = 0;
 69931         rc = p->rc;
 69932       }else{
 69933         iSavepoint = db->nSavepoint - iSavepoint - 1;
 69934         if( p1==SAVEPOINT_ROLLBACK ){
 69935           for(ii=0; ii<db->nDb; ii++){
 69936             sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
 69939         for(ii=0; ii<db->nDb; ii++){
 69940           rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
 69941           if( rc!=SQLITE_OK ){
 69942             goto abort_due_to_error;
 69945         if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
 69946           sqlite3ExpirePreparedStatements(db);
 69947           sqlite3ResetAllSchemasOfConnection(db);
 69948           db->flags = (db->flags | SQLITE_InternChanges);
 69952       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 
 69953       ** savepoints nested inside of the savepoint being operated on. */
 69954       while( db->pSavepoint!=pSavepoint ){
 69955         pTmp = db->pSavepoint;
 69956         db->pSavepoint = pTmp->pNext;
 69957         sqlite3DbFree(db, pTmp);
 69958         db->nSavepoint--;
 69961       /* If it is a RELEASE, then destroy the savepoint being operated on 
 69962       ** too. If it is a ROLLBACK TO, then set the number of deferred 
 69963       ** constraint violations present in the database to the value stored
 69964       ** when the savepoint was created.  */
 69965       if( p1==SAVEPOINT_RELEASE ){
 69966         assert( pSavepoint==db->pSavepoint );
 69967         db->pSavepoint = pSavepoint->pNext;
 69968         sqlite3DbFree(db, pSavepoint);
 69969         if( !isTransaction ){
 69970           db->nSavepoint--;
 69972       }else{
 69973         db->nDeferredCons = pSavepoint->nDeferredCons;
 69974         db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
 69977       if( !isTransaction ){
 69978         rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
 69979         if( rc!=SQLITE_OK ) goto abort_due_to_error;
 69984   break;
 69987 /* Opcode: AutoCommit P1 P2 * * *
 69988 **
 69989 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
 69990 ** back any currently active btree transactions. If there are any active
 69991 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
 69992 ** there are active writing VMs or active VMs that use shared cache.
 69993 **
 69994 ** This instruction causes the VM to halt.
 69995 */
 69996 case OP_AutoCommit: {
 69997   int desiredAutoCommit;
 69998   int iRollback;
 69999   int turnOnAC;
 70001   desiredAutoCommit = pOp->p1;
 70002   iRollback = pOp->p2;
 70003   turnOnAC = desiredAutoCommit && !db->autoCommit;
 70004   assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
 70005   assert( desiredAutoCommit==1 || iRollback==0 );
 70006   assert( db->nVdbeActive>0 );  /* At least this one VM is active */
 70007   assert( p->bIsReader );
 70009 #if 0
 70010   if( turnOnAC && iRollback && db->nVdbeActive>1 ){
 70011     /* If this instruction implements a ROLLBACK and other VMs are
 70012     ** still running, and a transaction is active, return an error indicating
 70013     ** that the other VMs must complete first. 
 70014     */
 70015     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
 70016         "SQL statements in progress");
 70017     rc = SQLITE_BUSY;
 70018   }else
 70019 #endif
 70020   if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
 70021     /* If this instruction implements a COMMIT and other VMs are writing
 70022     ** return an error indicating that the other VMs must complete first. 
 70023     */
 70024     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
 70025         "SQL statements in progress");
 70026     rc = SQLITE_BUSY;
 70027   }else if( desiredAutoCommit!=db->autoCommit ){
 70028     if( iRollback ){
 70029       assert( desiredAutoCommit==1 );
 70030       sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
 70031       db->autoCommit = 1;
 70032     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
 70033       goto vdbe_return;
 70034     }else{
 70035       db->autoCommit = (u8)desiredAutoCommit;
 70036       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
 70037         p->pc = pc;
 70038         db->autoCommit = (u8)(1-desiredAutoCommit);
 70039         p->rc = rc = SQLITE_BUSY;
 70040         goto vdbe_return;
 70043     assert( db->nStatement==0 );
 70044     sqlite3CloseSavepoints(db);
 70045     if( p->rc==SQLITE_OK ){
 70046       rc = SQLITE_DONE;
 70047     }else{
 70048       rc = SQLITE_ERROR;
 70050     goto vdbe_return;
 70051   }else{
 70052     sqlite3SetString(&p->zErrMsg, db,
 70053         (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
 70054         (iRollback)?"cannot rollback - no transaction is active":
 70055                    "cannot commit - no transaction is active"));
 70057     rc = SQLITE_ERROR;
 70059   break;
 70062 /* Opcode: Transaction P1 P2 P3 P4 P5
 70063 **
 70064 ** Begin a transaction on database P1 if a transaction is not already
 70065 ** active.
 70066 ** If P2 is non-zero, then a write-transaction is started, or if a 
 70067 ** read-transaction is already active, it is upgraded to a write-transaction.
 70068 ** If P2 is zero, then a read-transaction is started.
 70069 **
 70070 ** P1 is the index of the database file on which the transaction is
 70071 ** started.  Index 0 is the main database file and index 1 is the
 70072 ** file used for temporary tables.  Indices of 2 or more are used for
 70073 ** attached databases.
 70074 **
 70075 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
 70076 ** true (this flag is set if the Vdbe may modify more than one row and may
 70077 ** throw an ABORT exception), a statement transaction may also be opened.
 70078 ** More specifically, a statement transaction is opened iff the database
 70079 ** connection is currently not in autocommit mode, or if there are other
 70080 ** active statements. A statement transaction allows the changes made by this
 70081 ** VDBE to be rolled back after an error without having to roll back the
 70082 ** entire transaction. If no error is encountered, the statement transaction
 70083 ** will automatically commit when the VDBE halts.
 70084 **
 70085 ** If P5!=0 then this opcode also checks the schema cookie against P3
 70086 ** and the schema generation counter against P4.
 70087 ** The cookie changes its value whenever the database schema changes.
 70088 ** This operation is used to detect when that the cookie has changed
 70089 ** and that the current process needs to reread the schema.  If the schema
 70090 ** cookie in P3 differs from the schema cookie in the database header or
 70091 ** if the schema generation counter in P4 differs from the current
 70092 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
 70093 ** halts.  The sqlite3_step() wrapper function might then reprepare the
 70094 ** statement and rerun it from the beginning.
 70095 */
 70096 case OP_Transaction: {
 70097   Btree *pBt;
 70098   int iMeta;
 70099   int iGen;
 70101   assert( p->bIsReader );
 70102   assert( p->readOnly==0 || pOp->p2==0 );
 70103   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 70104   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 70105   if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
 70106     rc = SQLITE_READONLY;
 70107     goto abort_due_to_error;
 70109   pBt = db->aDb[pOp->p1].pBt;
 70111   if( pBt ){
 70112     rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
 70113     if( rc==SQLITE_BUSY ){
 70114       p->pc = pc;
 70115       p->rc = rc = SQLITE_BUSY;
 70116       goto vdbe_return;
 70118     if( rc!=SQLITE_OK ){
 70119       goto abort_due_to_error;
 70122     if( pOp->p2 && p->usesStmtJournal 
 70123      && (db->autoCommit==0 || db->nVdbeRead>1) 
 70124     ){
 70125       assert( sqlite3BtreeIsInTrans(pBt) );
 70126       if( p->iStatement==0 ){
 70127         assert( db->nStatement>=0 && db->nSavepoint>=0 );
 70128         db->nStatement++; 
 70129         p->iStatement = db->nSavepoint + db->nStatement;
 70132       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
 70133       if( rc==SQLITE_OK ){
 70134         rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
 70137       /* Store the current value of the database handles deferred constraint
 70138       ** counter. If the statement transaction needs to be rolled back,
 70139       ** the value of this counter needs to be restored too.  */
 70140       p->nStmtDefCons = db->nDeferredCons;
 70141       p->nStmtDefImmCons = db->nDeferredImmCons;
 70144     /* Gather the schema version number for checking */
 70145     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
 70146     iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 70147   }else{
 70148     iGen = iMeta = 0;
 70150   assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
 70151   if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
 70152     sqlite3DbFree(db, p->zErrMsg);
 70153     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
 70154     /* If the schema-cookie from the database file matches the cookie 
 70155     ** stored with the in-memory representation of the schema, do
 70156     ** not reload the schema from the database file.
 70157     **
 70158     ** If virtual-tables are in use, this is not just an optimization.
 70159     ** Often, v-tables store their data in other SQLite tables, which
 70160     ** are queried from within xNext() and other v-table methods using
 70161     ** prepared queries. If such a query is out-of-date, we do not want to
 70162     ** discard the database schema, as the user code implementing the
 70163     ** v-table would have to be ready for the sqlite3_vtab structure itself
 70164     ** to be invalidated whenever sqlite3_step() is called from within 
 70165     ** a v-table method.
 70166     */
 70167     if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
 70168       sqlite3ResetOneSchema(db, pOp->p1);
 70170     p->expired = 1;
 70171     rc = SQLITE_SCHEMA;
 70173   break;
 70176 /* Opcode: ReadCookie P1 P2 P3 * *
 70177 **
 70178 ** Read cookie number P3 from database P1 and write it into register P2.
 70179 ** P3==1 is the schema version.  P3==2 is the database format.
 70180 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
 70181 ** the main database file and P1==1 is the database file used to store
 70182 ** temporary tables.
 70183 **
 70184 ** There must be a read-lock on the database (either a transaction
 70185 ** must be started or there must be an open cursor) before
 70186 ** executing this instruction.
 70187 */
 70188 case OP_ReadCookie: {               /* out2-prerelease */
 70189   int iMeta;
 70190   int iDb;
 70191   int iCookie;
 70193   assert( p->bIsReader );
 70194   iDb = pOp->p1;
 70195   iCookie = pOp->p3;
 70196   assert( pOp->p3<SQLITE_N_BTREE_META );
 70197   assert( iDb>=0 && iDb<db->nDb );
 70198   assert( db->aDb[iDb].pBt!=0 );
 70199   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
 70201   sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
 70202   pOut->u.i = iMeta;
 70203   break;
 70206 /* Opcode: SetCookie P1 P2 P3 * *
 70207 **
 70208 ** Write the content of register P3 (interpreted as an integer)
 70209 ** into cookie number P2 of database P1.  P2==1 is the schema version.  
 70210 ** P2==2 is the database format. P2==3 is the recommended pager cache 
 70211 ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
 70212 ** database file used to store temporary tables.
 70213 **
 70214 ** A transaction must be started before executing this opcode.
 70215 */
 70216 case OP_SetCookie: {       /* in3 */
 70217   Db *pDb;
 70218   assert( pOp->p2<SQLITE_N_BTREE_META );
 70219   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 70220   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 70221   assert( p->readOnly==0 );
 70222   pDb = &db->aDb[pOp->p1];
 70223   assert( pDb->pBt!=0 );
 70224   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 70225   pIn3 = &aMem[pOp->p3];
 70226   sqlite3VdbeMemIntegerify(pIn3);
 70227   /* See note about index shifting on OP_ReadCookie */
 70228   rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
 70229   if( pOp->p2==BTREE_SCHEMA_VERSION ){
 70230     /* When the schema cookie changes, record the new cookie internally */
 70231     pDb->pSchema->schema_cookie = (int)pIn3->u.i;
 70232     db->flags |= SQLITE_InternChanges;
 70233   }else if( pOp->p2==BTREE_FILE_FORMAT ){
 70234     /* Record changes in the file format */
 70235     pDb->pSchema->file_format = (u8)pIn3->u.i;
 70237   if( pOp->p1==1 ){
 70238     /* Invalidate all prepared statements whenever the TEMP database
 70239     ** schema is changed.  Ticket #1644 */
 70240     sqlite3ExpirePreparedStatements(db);
 70241     p->expired = 0;
 70243   break;
 70246 /* Opcode: OpenRead P1 P2 P3 P4 P5
 70247 ** Synopsis: root=P2 iDb=P3
 70248 **
 70249 ** Open a read-only cursor for the database table whose root page is
 70250 ** P2 in a database file.  The database file is determined by P3. 
 70251 ** P3==0 means the main database, P3==1 means the database used for 
 70252 ** temporary tables, and P3>1 means used the corresponding attached
 70253 ** database.  Give the new cursor an identifier of P1.  The P1
 70254 ** values need not be contiguous but all P1 values should be small integers.
 70255 ** It is an error for P1 to be negative.
 70256 **
 70257 ** If P5!=0 then use the content of register P2 as the root page, not
 70258 ** the value of P2 itself.
 70259 **
 70260 ** There will be a read lock on the database whenever there is an
 70261 ** open cursor.  If the database was unlocked prior to this instruction
 70262 ** then a read lock is acquired as part of this instruction.  A read
 70263 ** lock allows other processes to read the database but prohibits
 70264 ** any other process from modifying the database.  The read lock is
 70265 ** released when all cursors are closed.  If this instruction attempts
 70266 ** to get a read lock but fails, the script terminates with an
 70267 ** SQLITE_BUSY error code.
 70268 **
 70269 ** The P4 value may be either an integer (P4_INT32) or a pointer to
 70270 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 70271 ** structure, then said structure defines the content and collating 
 70272 ** sequence of the index being opened. Otherwise, if P4 is an integer 
 70273 ** value, it is set to the number of columns in the table.
 70274 **
 70275 ** See also OpenWrite.
 70276 */
 70277 /* Opcode: OpenWrite P1 P2 P3 P4 P5
 70278 ** Synopsis: root=P2 iDb=P3
 70279 **
 70280 ** Open a read/write cursor named P1 on the table or index whose root
 70281 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
 70282 ** root page.
 70283 **
 70284 ** The P4 value may be either an integer (P4_INT32) or a pointer to
 70285 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
 70286 ** structure, then said structure defines the content and collating 
 70287 ** sequence of the index being opened. Otherwise, if P4 is an integer 
 70288 ** value, it is set to the number of columns in the table, or to the
 70289 ** largest index of any column of the table that is actually used.
 70290 **
 70291 ** This instruction works just like OpenRead except that it opens the cursor
 70292 ** in read/write mode.  For a given table, there can be one or more read-only
 70293 ** cursors or a single read/write cursor but not both.
 70294 **
 70295 ** See also OpenRead.
 70296 */
 70297 case OP_OpenRead:
 70298 case OP_OpenWrite: {
 70299   int nField;
 70300   KeyInfo *pKeyInfo;
 70301   int p2;
 70302   int iDb;
 70303   int wrFlag;
 70304   Btree *pX;
 70305   VdbeCursor *pCur;
 70306   Db *pDb;
 70308   assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
 70309   assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
 70310   assert( p->bIsReader );
 70311   assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
 70313   if( p->expired ){
 70314     rc = SQLITE_ABORT;
 70315     break;
 70318   nField = 0;
 70319   pKeyInfo = 0;
 70320   p2 = pOp->p2;
 70321   iDb = pOp->p3;
 70322   assert( iDb>=0 && iDb<db->nDb );
 70323   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
 70324   pDb = &db->aDb[iDb];
 70325   pX = pDb->pBt;
 70326   assert( pX!=0 );
 70327   if( pOp->opcode==OP_OpenWrite ){
 70328     wrFlag = 1;
 70329     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 70330     if( pDb->pSchema->file_format < p->minWriteFileFormat ){
 70331       p->minWriteFileFormat = pDb->pSchema->file_format;
 70333   }else{
 70334     wrFlag = 0;
 70336   if( pOp->p5 & OPFLAG_P2ISREG ){
 70337     assert( p2>0 );
 70338     assert( p2<=(p->nMem-p->nCursor) );
 70339     pIn2 = &aMem[p2];
 70340     assert( memIsValid(pIn2) );
 70341     assert( (pIn2->flags & MEM_Int)!=0 );
 70342     sqlite3VdbeMemIntegerify(pIn2);
 70343     p2 = (int)pIn2->u.i;
 70344     /* The p2 value always comes from a prior OP_CreateTable opcode and
 70345     ** that opcode will always set the p2 value to 2 or more or else fail.
 70346     ** If there were a failure, the prepared statement would have halted
 70347     ** before reaching this instruction. */
 70348     if( NEVER(p2<2) ) {
 70349       rc = SQLITE_CORRUPT_BKPT;
 70350       goto abort_due_to_error;
 70353   if( pOp->p4type==P4_KEYINFO ){
 70354     pKeyInfo = pOp->p4.pKeyInfo;
 70355     assert( pKeyInfo->enc==ENC(db) );
 70356     assert( pKeyInfo->db==db );
 70357     nField = pKeyInfo->nField+pKeyInfo->nXField;
 70358   }else if( pOp->p4type==P4_INT32 ){
 70359     nField = pOp->p4.i;
 70361   assert( pOp->p1>=0 );
 70362   assert( nField>=0 );
 70363   testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
 70364   pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
 70365   if( pCur==0 ) goto no_mem;
 70366   pCur->nullRow = 1;
 70367   pCur->isOrdered = 1;
 70368   rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
 70369   pCur->pKeyInfo = pKeyInfo;
 70370   assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
 70371   sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
 70373   /* Since it performs no memory allocation or IO, the only value that
 70374   ** sqlite3BtreeCursor() may return is SQLITE_OK. */
 70375   assert( rc==SQLITE_OK );
 70377   /* Set the VdbeCursor.isTable variable. Previous versions of
 70378   ** SQLite used to check if the root-page flags were sane at this point
 70379   ** and report database corruption if they were not, but this check has
 70380   ** since moved into the btree layer.  */  
 70381   pCur->isTable = pOp->p4type!=P4_KEYINFO;
 70382   break;
 70385 /* Opcode: OpenEphemeral P1 P2 * P4 P5
 70386 ** Synopsis: nColumn=P2
 70387 **
 70388 ** Open a new cursor P1 to a transient table.
 70389 ** The cursor is always opened read/write even if 
 70390 ** the main database is read-only.  The ephemeral
 70391 ** table is deleted automatically when the cursor is closed.
 70392 **
 70393 ** P2 is the number of columns in the ephemeral table.
 70394 ** The cursor points to a BTree table if P4==0 and to a BTree index
 70395 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
 70396 ** that defines the format of keys in the index.
 70397 **
 70398 ** The P5 parameter can be a mask of the BTREE_* flags defined
 70399 ** in btree.h.  These flags control aspects of the operation of
 70400 ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
 70401 ** added automatically.
 70402 */
 70403 /* Opcode: OpenAutoindex P1 P2 * P4 *
 70404 ** Synopsis: nColumn=P2
 70405 **
 70406 ** This opcode works the same as OP_OpenEphemeral.  It has a
 70407 ** different name to distinguish its use.  Tables created using
 70408 ** by this opcode will be used for automatically created transient
 70409 ** indices in joins.
 70410 */
 70411 case OP_OpenAutoindex: 
 70412 case OP_OpenEphemeral: {
 70413   VdbeCursor *pCx;
 70414   KeyInfo *pKeyInfo;
 70416   static const int vfsFlags = 
 70417       SQLITE_OPEN_READWRITE |
 70418       SQLITE_OPEN_CREATE |
 70419       SQLITE_OPEN_EXCLUSIVE |
 70420       SQLITE_OPEN_DELETEONCLOSE |
 70421       SQLITE_OPEN_TRANSIENT_DB;
 70422   assert( pOp->p1>=0 );
 70423   assert( pOp->p2>=0 );
 70424   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 70425   if( pCx==0 ) goto no_mem;
 70426   pCx->nullRow = 1;
 70427   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
 70428                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
 70429   if( rc==SQLITE_OK ){
 70430     rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
 70432   if( rc==SQLITE_OK ){
 70433     /* If a transient index is required, create it by calling
 70434     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
 70435     ** opening it. If a transient table is required, just use the
 70436     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
 70437     */
 70438     if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
 70439       int pgno;
 70440       assert( pOp->p4type==P4_KEYINFO );
 70441       rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 
 70442       if( rc==SQLITE_OK ){
 70443         assert( pgno==MASTER_ROOT+1 );
 70444         assert( pKeyInfo->db==db );
 70445         assert( pKeyInfo->enc==ENC(db) );
 70446         pCx->pKeyInfo = pKeyInfo;
 70447         rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
 70449       pCx->isTable = 0;
 70450     }else{
 70451       rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
 70452       pCx->isTable = 1;
 70455   pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 70456   break;
 70459 /* Opcode: SorterOpen P1 P2 * P4 *
 70460 **
 70461 ** This opcode works like OP_OpenEphemeral except that it opens
 70462 ** a transient index that is specifically designed to sort large
 70463 ** tables using an external merge-sort algorithm.
 70464 */
 70465 case OP_SorterOpen: {
 70466   VdbeCursor *pCx;
 70468   assert( pOp->p1>=0 );
 70469   assert( pOp->p2>=0 );
 70470   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
 70471   if( pCx==0 ) goto no_mem;
 70472   pCx->pKeyInfo = pOp->p4.pKeyInfo;
 70473   assert( pCx->pKeyInfo->db==db );
 70474   assert( pCx->pKeyInfo->enc==ENC(db) );
 70475   rc = sqlite3VdbeSorterInit(db, pCx);
 70476   break;
 70479 /* Opcode: OpenPseudo P1 P2 P3 * *
 70480 ** Synopsis: P3 columns in r[P2]
 70481 **
 70482 ** Open a new cursor that points to a fake table that contains a single
 70483 ** row of data.  The content of that one row is the content of memory
 70484 ** register P2.  In other words, cursor P1 becomes an alias for the 
 70485 ** MEM_Blob content contained in register P2.
 70486 **
 70487 ** A pseudo-table created by this opcode is used to hold a single
 70488 ** row output from the sorter so that the row can be decomposed into
 70489 ** individual columns using the OP_Column opcode.  The OP_Column opcode
 70490 ** is the only cursor opcode that works with a pseudo-table.
 70491 **
 70492 ** P3 is the number of fields in the records that will be stored by
 70493 ** the pseudo-table.
 70494 */
 70495 case OP_OpenPseudo: {
 70496   VdbeCursor *pCx;
 70498   assert( pOp->p1>=0 );
 70499   assert( pOp->p3>=0 );
 70500   pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
 70501   if( pCx==0 ) goto no_mem;
 70502   pCx->nullRow = 1;
 70503   pCx->pseudoTableReg = pOp->p2;
 70504   pCx->isTable = 1;
 70505   assert( pOp->p5==0 );
 70506   break;
 70509 /* Opcode: Close P1 * * * *
 70510 **
 70511 ** Close a cursor previously opened as P1.  If P1 is not
 70512 ** currently open, this instruction is a no-op.
 70513 */
 70514 case OP_Close: {
 70515   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70516   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
 70517   p->apCsr[pOp->p1] = 0;
 70518   break;
 70521 /* Opcode: SeekGe P1 P2 P3 P4 *
 70522 ** Synopsis: key=r[P3@P4]
 70523 **
 70524 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70525 ** use the value in register P3 as the key.  If cursor P1 refers 
 70526 ** to an SQL index, then P3 is the first in an array of P4 registers 
 70527 ** that are used as an unpacked index key. 
 70528 **
 70529 ** Reposition cursor P1 so that  it points to the smallest entry that 
 70530 ** is greater than or equal to the key value. If there are no records 
 70531 ** greater than or equal to the key and P2 is not zero, then jump to P2.
 70532 **
 70533 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
 70534 */
 70535 /* Opcode: SeekGt P1 P2 P3 P4 *
 70536 ** Synopsis: key=r[P3@P4]
 70537 **
 70538 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70539 ** use the value in register P3 as a key. If cursor P1 refers 
 70540 ** to an SQL index, then P3 is the first in an array of P4 registers 
 70541 ** that are used as an unpacked index key. 
 70542 **
 70543 ** Reposition cursor P1 so that  it points to the smallest entry that 
 70544 ** is greater than the key value. If there are no records greater than 
 70545 ** the key and P2 is not zero, then jump to P2.
 70546 **
 70547 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
 70548 */
 70549 /* Opcode: SeekLt P1 P2 P3 P4 * 
 70550 ** Synopsis: key=r[P3@P4]
 70551 **
 70552 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70553 ** use the value in register P3 as a key. If cursor P1 refers 
 70554 ** to an SQL index, then P3 is the first in an array of P4 registers 
 70555 ** that are used as an unpacked index key. 
 70556 **
 70557 ** Reposition cursor P1 so that  it points to the largest entry that 
 70558 ** is less than the key value. If there are no records less than 
 70559 ** the key and P2 is not zero, then jump to P2.
 70560 **
 70561 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
 70562 */
 70563 /* Opcode: SeekLe P1 P2 P3 P4 *
 70564 ** Synopsis: key=r[P3@P4]
 70565 **
 70566 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
 70567 ** use the value in register P3 as a key. If cursor P1 refers 
 70568 ** to an SQL index, then P3 is the first in an array of P4 registers 
 70569 ** that are used as an unpacked index key. 
 70570 **
 70571 ** Reposition cursor P1 so that it points to the largest entry that 
 70572 ** is less than or equal to the key value. If there are no records 
 70573 ** less than or equal to the key and P2 is not zero, then jump to P2.
 70574 **
 70575 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
 70576 */
 70577 case OP_SeekLT:         /* jump, in3 */
 70578 case OP_SeekLE:         /* jump, in3 */
 70579 case OP_SeekGE:         /* jump, in3 */
 70580 case OP_SeekGT: {       /* jump, in3 */
 70581   int res;
 70582   int oc;
 70583   VdbeCursor *pC;
 70584   UnpackedRecord r;
 70585   int nField;
 70586   i64 iKey;      /* The rowid we are to seek to */
 70588   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70589   assert( pOp->p2!=0 );
 70590   pC = p->apCsr[pOp->p1];
 70591   assert( pC!=0 );
 70592   assert( pC->pseudoTableReg==0 );
 70593   assert( OP_SeekLE == OP_SeekLT+1 );
 70594   assert( OP_SeekGE == OP_SeekLT+2 );
 70595   assert( OP_SeekGT == OP_SeekLT+3 );
 70596   assert( pC->isOrdered );
 70597   assert( pC->pCursor!=0 );
 70598   oc = pOp->opcode;
 70599   pC->nullRow = 0;
 70600   if( pC->isTable ){
 70601     /* The input value in P3 might be of any type: integer, real, string,
 70602     ** blob, or NULL.  But it needs to be an integer before we can do
 70603     ** the seek, so covert it. */
 70604     pIn3 = &aMem[pOp->p3];
 70605     applyNumericAffinity(pIn3);
 70606     iKey = sqlite3VdbeIntValue(pIn3);
 70607     pC->rowidIsValid = 0;
 70609     /* If the P3 value could not be converted into an integer without
 70610     ** loss of information, then special processing is required... */
 70611     if( (pIn3->flags & MEM_Int)==0 ){
 70612       if( (pIn3->flags & MEM_Real)==0 ){
 70613         /* If the P3 value cannot be converted into any kind of a number,
 70614         ** then the seek is not possible, so jump to P2 */
 70615         pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
 70616         break;
 70619       /* If the approximation iKey is larger than the actual real search
 70620       ** term, substitute >= for > and < for <=. e.g. if the search term
 70621       ** is 4.9 and the integer approximation 5:
 70622       **
 70623       **        (x >  4.9)    ->     (x >= 5)
 70624       **        (x <= 4.9)    ->     (x <  5)
 70625       */
 70626       if( pIn3->r<(double)iKey ){
 70627         assert( OP_SeekGE==(OP_SeekGT-1) );
 70628         assert( OP_SeekLT==(OP_SeekLE-1) );
 70629         assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
 70630         if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
 70633       /* If the approximation iKey is smaller than the actual real search
 70634       ** term, substitute <= for < and > for >=.  */
 70635       else if( pIn3->r>(double)iKey ){
 70636         assert( OP_SeekLE==(OP_SeekLT+1) );
 70637         assert( OP_SeekGT==(OP_SeekGE+1) );
 70638         assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
 70639         if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
 70642     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
 70643     if( rc!=SQLITE_OK ){
 70644       goto abort_due_to_error;
 70646     if( res==0 ){
 70647       pC->rowidIsValid = 1;
 70648       pC->lastRowid = iKey;
 70650   }else{
 70651     nField = pOp->p4.i;
 70652     assert( pOp->p4type==P4_INT32 );
 70653     assert( nField>0 );
 70654     r.pKeyInfo = pC->pKeyInfo;
 70655     r.nField = (u16)nField;
 70657     /* The next line of code computes as follows, only faster:
 70658     **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
 70659     **     r.default_rc = -1;
 70660     **   }else{
 70661     **     r.default_rc = +1;
 70662     **   }
 70663     */
 70664     r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
 70665     assert( oc!=OP_SeekGT || r.default_rc==-1 );
 70666     assert( oc!=OP_SeekLE || r.default_rc==-1 );
 70667     assert( oc!=OP_SeekGE || r.default_rc==+1 );
 70668     assert( oc!=OP_SeekLT || r.default_rc==+1 );
 70670     r.aMem = &aMem[pOp->p3];
 70671 #ifdef SQLITE_DEBUG
 70672     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 70673 #endif
 70674     ExpandBlob(r.aMem);
 70675     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
 70676     if( rc!=SQLITE_OK ){
 70677       goto abort_due_to_error;
 70679     pC->rowidIsValid = 0;
 70681   pC->deferredMoveto = 0;
 70682   pC->cacheStatus = CACHE_STALE;
 70683 #ifdef SQLITE_TEST
 70684   sqlite3_search_count++;
 70685 #endif
 70686   if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
 70687     if( res<0 || (res==0 && oc==OP_SeekGT) ){
 70688       res = 0;
 70689       rc = sqlite3BtreeNext(pC->pCursor, &res);
 70690       if( rc!=SQLITE_OK ) goto abort_due_to_error;
 70691       pC->rowidIsValid = 0;
 70692     }else{
 70693       res = 0;
 70695   }else{
 70696     assert( oc==OP_SeekLT || oc==OP_SeekLE );
 70697     if( res>0 || (res==0 && oc==OP_SeekLT) ){
 70698       res = 0;
 70699       rc = sqlite3BtreePrevious(pC->pCursor, &res);
 70700       if( rc!=SQLITE_OK ) goto abort_due_to_error;
 70701       pC->rowidIsValid = 0;
 70702     }else{
 70703       /* res might be negative because the table is empty.  Check to
 70704       ** see if this is the case.
 70705       */
 70706       res = sqlite3BtreeEof(pC->pCursor);
 70709   assert( pOp->p2>0 );
 70710   VdbeBranchTaken(res!=0,2);
 70711   if( res ){
 70712     pc = pOp->p2 - 1;
 70714   break;
 70717 /* Opcode: Seek P1 P2 * * *
 70718 ** Synopsis:  intkey=r[P2]
 70719 **
 70720 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
 70721 ** for P1 to move so that it points to the rowid given by P2.
 70722 **
 70723 ** This is actually a deferred seek.  Nothing actually happens until
 70724 ** the cursor is used to read a record.  That way, if no reads
 70725 ** occur, no unnecessary I/O happens.
 70726 */
 70727 case OP_Seek: {    /* in2 */
 70728   VdbeCursor *pC;
 70730   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70731   pC = p->apCsr[pOp->p1];
 70732   assert( pC!=0 );
 70733   assert( pC->pCursor!=0 );
 70734   assert( pC->isTable );
 70735   pC->nullRow = 0;
 70736   pIn2 = &aMem[pOp->p2];
 70737   pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
 70738   pC->rowidIsValid = 0;
 70739   pC->deferredMoveto = 1;
 70740   break;
 70744 /* Opcode: Found P1 P2 P3 P4 *
 70745 ** Synopsis: key=r[P3@P4]
 70746 **
 70747 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 70748 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 70749 ** record.
 70750 **
 70751 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 70752 ** is a prefix of any entry in P1 then a jump is made to P2 and
 70753 ** P1 is left pointing at the matching entry.
 70754 **
 70755 ** See also: NotFound, NoConflict, NotExists. SeekGe
 70756 */
 70757 /* Opcode: NotFound P1 P2 P3 P4 *
 70758 ** Synopsis: key=r[P3@P4]
 70759 **
 70760 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 70761 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 70762 ** record.
 70763 ** 
 70764 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 70765 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
 70766 ** does contain an entry whose prefix matches the P3/P4 record then control
 70767 ** falls through to the next instruction and P1 is left pointing at the
 70768 ** matching entry.
 70769 **
 70770 ** See also: Found, NotExists, NoConflict
 70771 */
 70772 /* Opcode: NoConflict P1 P2 P3 P4 *
 70773 ** Synopsis: key=r[P3@P4]
 70774 **
 70775 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
 70776 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
 70777 ** record.
 70778 ** 
 70779 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
 70780 ** contains any NULL value, jump immediately to P2.  If all terms of the
 70781 ** record are not-NULL then a check is done to determine if any row in the
 70782 ** P1 index btree has a matching key prefix.  If there are no matches, jump
 70783 ** immediately to P2.  If there is a match, fall through and leave the P1
 70784 ** cursor pointing to the matching row.
 70785 **
 70786 ** This opcode is similar to OP_NotFound with the exceptions that the
 70787 ** branch is always taken if any part of the search key input is NULL.
 70788 **
 70789 ** See also: NotFound, Found, NotExists
 70790 */
 70791 case OP_NoConflict:     /* jump, in3 */
 70792 case OP_NotFound:       /* jump, in3 */
 70793 case OP_Found: {        /* jump, in3 */
 70794   int alreadyExists;
 70795   int ii;
 70796   VdbeCursor *pC;
 70797   int res;
 70798   char *pFree;
 70799   UnpackedRecord *pIdxKey;
 70800   UnpackedRecord r;
 70801   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
 70803 #ifdef SQLITE_TEST
 70804   if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
 70805 #endif
 70807   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70808   assert( pOp->p4type==P4_INT32 );
 70809   pC = p->apCsr[pOp->p1];
 70810   assert( pC!=0 );
 70811   pIn3 = &aMem[pOp->p3];
 70812   assert( pC->pCursor!=0 );
 70813   assert( pC->isTable==0 );
 70814   pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
 70815   if( pOp->p4.i>0 ){
 70816     r.pKeyInfo = pC->pKeyInfo;
 70817     r.nField = (u16)pOp->p4.i;
 70818     r.aMem = pIn3;
 70819     for(ii=0; ii<r.nField; ii++){
 70820       assert( memIsValid(&r.aMem[ii]) );
 70821       ExpandBlob(&r.aMem[ii]);
 70822 #ifdef SQLITE_DEBUG
 70823       if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
 70824 #endif
 70826     pIdxKey = &r;
 70827   }else{
 70828     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
 70829         pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
 70830     ); 
 70831     if( pIdxKey==0 ) goto no_mem;
 70832     assert( pIn3->flags & MEM_Blob );
 70833     assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
 70834     sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 70836   pIdxKey->default_rc = 0;
 70837   if( pOp->opcode==OP_NoConflict ){
 70838     /* For the OP_NoConflict opcode, take the jump if any of the
 70839     ** input fields are NULL, since any key with a NULL will not
 70840     ** conflict */
 70841     for(ii=0; ii<r.nField; ii++){
 70842       if( r.aMem[ii].flags & MEM_Null ){
 70843         pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
 70844         break;
 70848   rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
 70849   if( pOp->p4.i==0 ){
 70850     sqlite3DbFree(db, pFree);
 70852   if( rc!=SQLITE_OK ){
 70853     break;
 70855   pC->seekResult = res;
 70856   alreadyExists = (res==0);
 70857   pC->nullRow = 1-alreadyExists;
 70858   pC->deferredMoveto = 0;
 70859   pC->cacheStatus = CACHE_STALE;
 70860   if( pOp->opcode==OP_Found ){
 70861     VdbeBranchTaken(alreadyExists!=0,2);
 70862     if( alreadyExists ) pc = pOp->p2 - 1;
 70863   }else{
 70864     VdbeBranchTaken(alreadyExists==0,2);
 70865     if( !alreadyExists ) pc = pOp->p2 - 1;
 70867   break;
 70870 /* Opcode: NotExists P1 P2 P3 * *
 70871 ** Synopsis: intkey=r[P3]
 70872 **
 70873 ** P1 is the index of a cursor open on an SQL table btree (with integer
 70874 ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
 70875 ** rowid P3 then jump immediately to P2.  If P1 does contain a record
 70876 ** with rowid P3 then leave the cursor pointing at that record and fall
 70877 ** through to the next instruction.
 70878 **
 70879 ** The OP_NotFound opcode performs the same operation on index btrees
 70880 ** (with arbitrary multi-value keys).
 70881 **
 70882 ** See also: Found, NotFound, NoConflict
 70883 */
 70884 case OP_NotExists: {        /* jump, in3 */
 70885   VdbeCursor *pC;
 70886   BtCursor *pCrsr;
 70887   int res;
 70888   u64 iKey;
 70890   pIn3 = &aMem[pOp->p3];
 70891   assert( pIn3->flags & MEM_Int );
 70892   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70893   pC = p->apCsr[pOp->p1];
 70894   assert( pC!=0 );
 70895   assert( pC->isTable );
 70896   assert( pC->pseudoTableReg==0 );
 70897   pCrsr = pC->pCursor;
 70898   assert( pCrsr!=0 );
 70899   res = 0;
 70900   iKey = pIn3->u.i;
 70901   rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
 70902   pC->lastRowid = pIn3->u.i;
 70903   pC->rowidIsValid = res==0 ?1:0;
 70904   pC->nullRow = 0;
 70905   pC->cacheStatus = CACHE_STALE;
 70906   pC->deferredMoveto = 0;
 70907   VdbeBranchTaken(res!=0,2);
 70908   if( res!=0 ){
 70909     pc = pOp->p2 - 1;
 70910     assert( pC->rowidIsValid==0 );
 70912   pC->seekResult = res;
 70913   break;
 70916 /* Opcode: Sequence P1 P2 * * *
 70917 ** Synopsis: r[P2]=rowid
 70918 **
 70919 ** Find the next available sequence number for cursor P1.
 70920 ** Write the sequence number into register P2.
 70921 ** The sequence number on the cursor is incremented after this
 70922 ** instruction.  
 70923 */
 70924 case OP_Sequence: {           /* out2-prerelease */
 70925   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70926   assert( p->apCsr[pOp->p1]!=0 );
 70927   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
 70928   break;
 70932 /* Opcode: NewRowid P1 P2 P3 * *
 70933 ** Synopsis: r[P2]=rowid
 70934 **
 70935 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
 70936 ** The record number is not previously used as a key in the database
 70937 ** table that cursor P1 points to.  The new record number is written
 70938 ** written to register P2.
 70939 **
 70940 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
 70941 ** the largest previously generated record number. No new record numbers are
 70942 ** allowed to be less than this value. When this value reaches its maximum, 
 70943 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
 70944 ** generated record number. This P3 mechanism is used to help implement the
 70945 ** AUTOINCREMENT feature.
 70946 */
 70947 case OP_NewRowid: {           /* out2-prerelease */
 70948   i64 v;                 /* The new rowid */
 70949   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
 70950   int res;               /* Result of an sqlite3BtreeLast() */
 70951   int cnt;               /* Counter to limit the number of searches */
 70952   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
 70953   VdbeFrame *pFrame;     /* Root frame of VDBE */
 70955   v = 0;
 70956   res = 0;
 70957   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 70958   pC = p->apCsr[pOp->p1];
 70959   assert( pC!=0 );
 70960   if( NEVER(pC->pCursor==0) ){
 70961     /* The zero initialization above is all that is needed */
 70962   }else{
 70963     /* The next rowid or record number (different terms for the same
 70964     ** thing) is obtained in a two-step algorithm.
 70965     **
 70966     ** First we attempt to find the largest existing rowid and add one
 70967     ** to that.  But if the largest existing rowid is already the maximum
 70968     ** positive integer, we have to fall through to the second
 70969     ** probabilistic algorithm
 70970     **
 70971     ** The second algorithm is to select a rowid at random and see if
 70972     ** it already exists in the table.  If it does not exist, we have
 70973     ** succeeded.  If the random rowid does exist, we select a new one
 70974     ** and try again, up to 100 times.
 70975     */
 70976     assert( pC->isTable );
 70978 #ifdef SQLITE_32BIT_ROWID
 70979 #   define MAX_ROWID 0x7fffffff
 70980 #else
 70981     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
 70982     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 70983     ** to provide the constant while making all compilers happy.
 70984     */
 70985 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
 70986 #endif
 70988     if( !pC->useRandomRowid ){
 70989       rc = sqlite3BtreeLast(pC->pCursor, &res);
 70990       if( rc!=SQLITE_OK ){
 70991         goto abort_due_to_error;
 70993       if( res ){
 70994         v = 1;   /* IMP: R-61914-48074 */
 70995       }else{
 70996         assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
 70997         rc = sqlite3BtreeKeySize(pC->pCursor, &v);
 70998         assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
 70999         if( v>=MAX_ROWID ){
 71000           pC->useRandomRowid = 1;
 71001         }else{
 71002           v++;   /* IMP: R-29538-34987 */
 71007 #ifndef SQLITE_OMIT_AUTOINCREMENT
 71008     if( pOp->p3 ){
 71009       /* Assert that P3 is a valid memory cell. */
 71010       assert( pOp->p3>0 );
 71011       if( p->pFrame ){
 71012         for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 71013         /* Assert that P3 is a valid memory cell. */
 71014         assert( pOp->p3<=pFrame->nMem );
 71015         pMem = &pFrame->aMem[pOp->p3];
 71016       }else{
 71017         /* Assert that P3 is a valid memory cell. */
 71018         assert( pOp->p3<=(p->nMem-p->nCursor) );
 71019         pMem = &aMem[pOp->p3];
 71020         memAboutToChange(p, pMem);
 71022       assert( memIsValid(pMem) );
 71024       REGISTER_TRACE(pOp->p3, pMem);
 71025       sqlite3VdbeMemIntegerify(pMem);
 71026       assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
 71027       if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
 71028         rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
 71029         goto abort_due_to_error;
 71031       if( v<pMem->u.i+1 ){
 71032         v = pMem->u.i + 1;
 71034       pMem->u.i = v;
 71036 #endif
 71037     if( pC->useRandomRowid ){
 71038       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
 71039       ** largest possible integer (9223372036854775807) then the database
 71040       ** engine starts picking positive candidate ROWIDs at random until
 71041       ** it finds one that is not previously used. */
 71042       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
 71043                              ** an AUTOINCREMENT table. */
 71044       /* on the first attempt, simply do one more than previous */
 71045       v = lastRowid;
 71046       v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 71047       v++; /* ensure non-zero */
 71048       cnt = 0;
 71049       while(   ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
 71050                                                  0, &res))==SQLITE_OK)
 71051             && (res==0)
 71052             && (++cnt<100)){
 71053         /* collision - try another random rowid */
 71054         sqlite3_randomness(sizeof(v), &v);
 71055         if( cnt<5 ){
 71056           /* try "small" random rowids for the initial attempts */
 71057           v &= 0xffffff;
 71058         }else{
 71059           v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
 71061         v++; /* ensure non-zero */
 71063       if( rc==SQLITE_OK && res==0 ){
 71064         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
 71065         goto abort_due_to_error;
 71067       assert( v>0 );  /* EV: R-40812-03570 */
 71069     pC->rowidIsValid = 0;
 71070     pC->deferredMoveto = 0;
 71071     pC->cacheStatus = CACHE_STALE;
 71073   pOut->u.i = v;
 71074   break;
 71077 /* Opcode: Insert P1 P2 P3 P4 P5
 71078 ** Synopsis: intkey=r[P3] data=r[P2]
 71079 **
 71080 ** Write an entry into the table of cursor P1.  A new entry is
 71081 ** created if it doesn't already exist or the data for an existing
 71082 ** entry is overwritten.  The data is the value MEM_Blob stored in register
 71083 ** number P2. The key is stored in register P3. The key must
 71084 ** be a MEM_Int.
 71085 **
 71086 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
 71087 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
 71088 ** then rowid is stored for subsequent return by the
 71089 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
 71090 **
 71091 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
 71092 ** the last seek operation (OP_NotExists) was a success, then this
 71093 ** operation will not attempt to find the appropriate row before doing
 71094 ** the insert but will instead overwrite the row that the cursor is
 71095 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
 71096 ** has already positioned the cursor correctly.  This is an optimization
 71097 ** that boosts performance by avoiding redundant seeks.
 71098 **
 71099 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
 71100 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
 71101 ** is part of an INSERT operation.  The difference is only important to
 71102 ** the update hook.
 71103 **
 71104 ** Parameter P4 may point to a string containing the table-name, or
 71105 ** may be NULL. If it is not NULL, then the update-hook 
 71106 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
 71107 **
 71108 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
 71109 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
 71110 ** and register P2 becomes ephemeral.  If the cursor is changed, the
 71111 ** value of register P2 will then change.  Make sure this does not
 71112 ** cause any problems.)
 71113 **
 71114 ** This instruction only works on tables.  The equivalent instruction
 71115 ** for indices is OP_IdxInsert.
 71116 */
 71117 /* Opcode: InsertInt P1 P2 P3 P4 P5
 71118 ** Synopsis:  intkey=P3 data=r[P2]
 71119 **
 71120 ** This works exactly like OP_Insert except that the key is the
 71121 ** integer value P3, not the value of the integer stored in register P3.
 71122 */
 71123 case OP_Insert: 
 71124 case OP_InsertInt: {
 71125   Mem *pData;       /* MEM cell holding data for the record to be inserted */
 71126   Mem *pKey;        /* MEM cell holding key  for the record */
 71127   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
 71128   VdbeCursor *pC;   /* Cursor to table into which insert is written */
 71129   int nZero;        /* Number of zero-bytes to append */
 71130   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
 71131   const char *zDb;  /* database name - used by the update hook */
 71132   const char *zTbl; /* Table name - used by the opdate hook */
 71133   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
 71135   pData = &aMem[pOp->p2];
 71136   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71137   assert( memIsValid(pData) );
 71138   pC = p->apCsr[pOp->p1];
 71139   assert( pC!=0 );
 71140   assert( pC->pCursor!=0 );
 71141   assert( pC->pseudoTableReg==0 );
 71142   assert( pC->isTable );
 71143   REGISTER_TRACE(pOp->p2, pData);
 71145   if( pOp->opcode==OP_Insert ){
 71146     pKey = &aMem[pOp->p3];
 71147     assert( pKey->flags & MEM_Int );
 71148     assert( memIsValid(pKey) );
 71149     REGISTER_TRACE(pOp->p3, pKey);
 71150     iKey = pKey->u.i;
 71151   }else{
 71152     assert( pOp->opcode==OP_InsertInt );
 71153     iKey = pOp->p3;
 71156   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 71157   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
 71158   if( pData->flags & MEM_Null ){
 71159     pData->z = 0;
 71160     pData->n = 0;
 71161   }else{
 71162     assert( pData->flags & (MEM_Blob|MEM_Str) );
 71164   seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
 71165   if( pData->flags & MEM_Zero ){
 71166     nZero = pData->u.nZero;
 71167   }else{
 71168     nZero = 0;
 71170   rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
 71171                           pData->z, pData->n, nZero,
 71172                           (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
 71173   );
 71174   pC->rowidIsValid = 0;
 71175   pC->deferredMoveto = 0;
 71176   pC->cacheStatus = CACHE_STALE;
 71178   /* Invoke the update-hook if required. */
 71179   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
 71180     zDb = db->aDb[pC->iDb].zName;
 71181     zTbl = pOp->p4.z;
 71182     op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
 71183     assert( pC->isTable );
 71184     db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
 71185     assert( pC->iDb>=0 );
 71187   break;
 71190 /* Opcode: Delete P1 P2 * P4 *
 71191 **
 71192 ** Delete the record at which the P1 cursor is currently pointing.
 71193 **
 71194 ** The cursor will be left pointing at either the next or the previous
 71195 ** record in the table. If it is left pointing at the next record, then
 71196 ** the next Next instruction will be a no-op.  Hence it is OK to delete
 71197 ** a record from within an Next loop.
 71198 **
 71199 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
 71200 ** incremented (otherwise not).
 71201 **
 71202 ** P1 must not be pseudo-table.  It has to be a real table with
 71203 ** multiple rows.
 71204 **
 71205 ** If P4 is not NULL, then it is the name of the table that P1 is
 71206 ** pointing to.  The update hook will be invoked, if it exists.
 71207 ** If P4 is not NULL then the P1 cursor must have been positioned
 71208 ** using OP_NotFound prior to invoking this opcode.
 71209 */
 71210 case OP_Delete: {
 71211   i64 iKey;
 71212   VdbeCursor *pC;
 71214   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71215   pC = p->apCsr[pOp->p1];
 71216   assert( pC!=0 );
 71217   assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
 71218   iKey = pC->lastRowid;      /* Only used for the update hook */
 71220   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
 71221   ** OP_Column on the same table without any intervening operations that
 71222   ** might move or invalidate the cursor.  Hence cursor pC is always pointing
 71223   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
 71224   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
 71225   ** to guard against future changes to the code generator.
 71226   **/
 71227   assert( pC->deferredMoveto==0 );
 71228   rc = sqlite3VdbeCursorMoveto(pC);
 71229   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 71231   rc = sqlite3BtreeDelete(pC->pCursor);
 71232   pC->cacheStatus = CACHE_STALE;
 71234   /* Invoke the update-hook if required. */
 71235   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
 71236     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
 71237                         db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
 71238     assert( pC->iDb>=0 );
 71240   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
 71241   break;
 71243 /* Opcode: ResetCount * * * * *
 71244 **
 71245 ** The value of the change counter is copied to the database handle
 71246 ** change counter (returned by subsequent calls to sqlite3_changes()).
 71247 ** Then the VMs internal change counter resets to 0.
 71248 ** This is used by trigger programs.
 71249 */
 71250 case OP_ResetCount: {
 71251   sqlite3VdbeSetChanges(db, p->nChange);
 71252   p->nChange = 0;
 71253   break;
 71256 /* Opcode: SorterCompare P1 P2 P3 P4
 71257 ** Synopsis:  if key(P1)!=rtrim(r[P3],P4) goto P2
 71258 **
 71259 ** P1 is a sorter cursor. This instruction compares a prefix of the
 71260 ** the record blob in register P3 against a prefix of the entry that 
 71261 ** the sorter cursor currently points to.  The final P4 fields of both
 71262 ** the P3 and sorter record are ignored.
 71263 **
 71264 ** If either P3 or the sorter contains a NULL in one of their significant
 71265 ** fields (not counting the P4 fields at the end which are ignored) then
 71266 ** the comparison is assumed to be equal.
 71267 **
 71268 ** Fall through to next instruction if the two records compare equal to
 71269 ** each other.  Jump to P2 if they are different.
 71270 */
 71271 case OP_SorterCompare: {
 71272   VdbeCursor *pC;
 71273   int res;
 71274   int nIgnore;
 71276   pC = p->apCsr[pOp->p1];
 71277   assert( isSorter(pC) );
 71278   assert( pOp->p4type==P4_INT32 );
 71279   pIn3 = &aMem[pOp->p3];
 71280   nIgnore = pOp->p4.i;
 71281   rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
 71282   VdbeBranchTaken(res!=0,2);
 71283   if( res ){
 71284     pc = pOp->p2-1;
 71286   break;
 71287 };
 71289 /* Opcode: SorterData P1 P2 * * *
 71290 ** Synopsis: r[P2]=data
 71291 **
 71292 ** Write into register P2 the current sorter data for sorter cursor P1.
 71293 */
 71294 case OP_SorterData: {
 71295   VdbeCursor *pC;
 71297   pOut = &aMem[pOp->p2];
 71298   pC = p->apCsr[pOp->p1];
 71299   assert( isSorter(pC) );
 71300   rc = sqlite3VdbeSorterRowkey(pC, pOut);
 71301   break;
 71304 /* Opcode: RowData P1 P2 * * *
 71305 ** Synopsis: r[P2]=data
 71306 **
 71307 ** Write into register P2 the complete row data for cursor P1.
 71308 ** There is no interpretation of the data.  
 71309 ** It is just copied onto the P2 register exactly as 
 71310 ** it is found in the database file.
 71311 **
 71312 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 71313 ** of a real table, not a pseudo-table.
 71314 */
 71315 /* Opcode: RowKey P1 P2 * * *
 71316 ** Synopsis: r[P2]=key
 71317 **
 71318 ** Write into register P2 the complete row key for cursor P1.
 71319 ** There is no interpretation of the data.  
 71320 ** The key is copied onto the P2 register exactly as 
 71321 ** it is found in the database file.
 71322 **
 71323 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
 71324 ** of a real table, not a pseudo-table.
 71325 */
 71326 case OP_RowKey:
 71327 case OP_RowData: {
 71328   VdbeCursor *pC;
 71329   BtCursor *pCrsr;
 71330   u32 n;
 71331   i64 n64;
 71333   pOut = &aMem[pOp->p2];
 71334   memAboutToChange(p, pOut);
 71336   /* Note that RowKey and RowData are really exactly the same instruction */
 71337   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71338   pC = p->apCsr[pOp->p1];
 71339   assert( isSorter(pC)==0 );
 71340   assert( pC->isTable || pOp->opcode!=OP_RowData );
 71341   assert( pC->isTable==0 || pOp->opcode==OP_RowData );
 71342   assert( pC!=0 );
 71343   assert( pC->nullRow==0 );
 71344   assert( pC->pseudoTableReg==0 );
 71345   assert( pC->pCursor!=0 );
 71346   pCrsr = pC->pCursor;
 71347   assert( sqlite3BtreeCursorIsValid(pCrsr) );
 71349   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 71350   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 71351   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
 71352   ** a no-op and can never fail.  But we leave it in place as a safety.
 71353   */
 71354   assert( pC->deferredMoveto==0 );
 71355   rc = sqlite3VdbeCursorMoveto(pC);
 71356   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
 71358   if( pC->isTable==0 ){
 71359     assert( !pC->isTable );
 71360     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
 71361     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
 71362     if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 71363       goto too_big;
 71365     n = (u32)n64;
 71366   }else{
 71367     VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
 71368     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
 71369     if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
 71370       goto too_big;
 71373   if( sqlite3VdbeMemGrow(pOut, n, 0) ){
 71374     goto no_mem;
 71376   pOut->n = n;
 71377   MemSetTypeFlag(pOut, MEM_Blob);
 71378   if( pC->isTable==0 ){
 71379     rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
 71380   }else{
 71381     rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
 71383   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
 71384   UPDATE_MAX_BLOBSIZE(pOut);
 71385   REGISTER_TRACE(pOp->p2, pOut);
 71386   break;
 71389 /* Opcode: Rowid P1 P2 * * *
 71390 ** Synopsis: r[P2]=rowid
 71391 **
 71392 ** Store in register P2 an integer which is the key of the table entry that
 71393 ** P1 is currently point to.
 71394 **
 71395 ** P1 can be either an ordinary table or a virtual table.  There used to
 71396 ** be a separate OP_VRowid opcode for use with virtual tables, but this
 71397 ** one opcode now works for both table types.
 71398 */
 71399 case OP_Rowid: {                 /* out2-prerelease */
 71400   VdbeCursor *pC;
 71401   i64 v;
 71402   sqlite3_vtab *pVtab;
 71403   const sqlite3_module *pModule;
 71405   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71406   pC = p->apCsr[pOp->p1];
 71407   assert( pC!=0 );
 71408   assert( pC->pseudoTableReg==0 || pC->nullRow );
 71409   if( pC->nullRow ){
 71410     pOut->flags = MEM_Null;
 71411     break;
 71412   }else if( pC->deferredMoveto ){
 71413     v = pC->movetoTarget;
 71414 #ifndef SQLITE_OMIT_VIRTUALTABLE
 71415   }else if( pC->pVtabCursor ){
 71416     pVtab = pC->pVtabCursor->pVtab;
 71417     pModule = pVtab->pModule;
 71418     assert( pModule->xRowid );
 71419     rc = pModule->xRowid(pC->pVtabCursor, &v);
 71420     sqlite3VtabImportErrmsg(p, pVtab);
 71421 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 71422   }else{
 71423     assert( pC->pCursor!=0 );
 71424     rc = sqlite3VdbeCursorMoveto(pC);
 71425     if( rc ) goto abort_due_to_error;
 71426     if( pC->rowidIsValid ){
 71427       v = pC->lastRowid;
 71428     }else{
 71429       rc = sqlite3BtreeKeySize(pC->pCursor, &v);
 71430       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
 71433   pOut->u.i = v;
 71434   break;
 71437 /* Opcode: NullRow P1 * * * *
 71438 **
 71439 ** Move the cursor P1 to a null row.  Any OP_Column operations
 71440 ** that occur while the cursor is on the null row will always
 71441 ** write a NULL.
 71442 */
 71443 case OP_NullRow: {
 71444   VdbeCursor *pC;
 71446   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71447   pC = p->apCsr[pOp->p1];
 71448   assert( pC!=0 );
 71449   pC->nullRow = 1;
 71450   pC->rowidIsValid = 0;
 71451   pC->cacheStatus = CACHE_STALE;
 71452   if( pC->pCursor ){
 71453     sqlite3BtreeClearCursor(pC->pCursor);
 71455   break;
 71458 /* Opcode: Last P1 P2 * * *
 71459 **
 71460 ** The next use of the Rowid or Column or Next instruction for P1 
 71461 ** will refer to the last entry in the database table or index.
 71462 ** If the table or index is empty and P2>0, then jump immediately to P2.
 71463 ** If P2 is 0 or if the table or index is not empty, fall through
 71464 ** to the following instruction.
 71465 */
 71466 case OP_Last: {        /* jump */
 71467   VdbeCursor *pC;
 71468   BtCursor *pCrsr;
 71469   int res;
 71471   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71472   pC = p->apCsr[pOp->p1];
 71473   assert( pC!=0 );
 71474   pCrsr = pC->pCursor;
 71475   res = 0;
 71476   assert( pCrsr!=0 );
 71477   rc = sqlite3BtreeLast(pCrsr, &res);
 71478   pC->nullRow = (u8)res;
 71479   pC->deferredMoveto = 0;
 71480   pC->rowidIsValid = 0;
 71481   pC->cacheStatus = CACHE_STALE;
 71482   if( pOp->p2>0 ){
 71483     VdbeBranchTaken(res!=0,2);
 71484     if( res ) pc = pOp->p2 - 1;
 71486   break;
 71490 /* Opcode: Sort P1 P2 * * *
 71491 **
 71492 ** This opcode does exactly the same thing as OP_Rewind except that
 71493 ** it increments an undocumented global variable used for testing.
 71494 **
 71495 ** Sorting is accomplished by writing records into a sorting index,
 71496 ** then rewinding that index and playing it back from beginning to
 71497 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
 71498 ** rewinding so that the global variable will be incremented and
 71499 ** regression tests can determine whether or not the optimizer is
 71500 ** correctly optimizing out sorts.
 71501 */
 71502 case OP_SorterSort:    /* jump */
 71503 case OP_Sort: {        /* jump */
 71504 #ifdef SQLITE_TEST
 71505   sqlite3_sort_count++;
 71506   sqlite3_search_count--;
 71507 #endif
 71508   p->aCounter[SQLITE_STMTSTATUS_SORT]++;
 71509   /* Fall through into OP_Rewind */
 71511 /* Opcode: Rewind P1 P2 * * *
 71512 **
 71513 ** The next use of the Rowid or Column or Next instruction for P1 
 71514 ** will refer to the first entry in the database table or index.
 71515 ** If the table or index is empty and P2>0, then jump immediately to P2.
 71516 ** If P2 is 0 or if the table or index is not empty, fall through
 71517 ** to the following instruction.
 71518 */
 71519 case OP_Rewind: {        /* jump */
 71520   VdbeCursor *pC;
 71521   BtCursor *pCrsr;
 71522   int res;
 71524   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71525   pC = p->apCsr[pOp->p1];
 71526   assert( pC!=0 );
 71527   assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
 71528   res = 1;
 71529   if( isSorter(pC) ){
 71530     rc = sqlite3VdbeSorterRewind(db, pC, &res);
 71531   }else{
 71532     pCrsr = pC->pCursor;
 71533     assert( pCrsr );
 71534     rc = sqlite3BtreeFirst(pCrsr, &res);
 71535     pC->deferredMoveto = 0;
 71536     pC->cacheStatus = CACHE_STALE;
 71537     pC->rowidIsValid = 0;
 71539   pC->nullRow = (u8)res;
 71540   assert( pOp->p2>0 && pOp->p2<p->nOp );
 71541   VdbeBranchTaken(res!=0,2);
 71542   if( res ){
 71543     pc = pOp->p2 - 1;
 71545   break;
 71548 /* Opcode: Next P1 P2 P3 P4 P5
 71549 **
 71550 ** Advance cursor P1 so that it points to the next key/data pair in its
 71551 ** table or index.  If there are no more key/value pairs then fall through
 71552 ** to the following instruction.  But if the cursor advance was successful,
 71553 ** jump immediately to P2.
 71554 **
 71555 ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
 71556 ** been opened prior to this opcode or the program will segfault.
 71557 **
 71558 ** The P3 value is a hint to the btree implementation. If P3==1, that
 71559 ** means P1 is an SQL index and that this instruction could have been
 71560 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
 71561 ** always either 0 or 1.
 71562 **
 71563 ** P4 is always of type P4_ADVANCE. The function pointer points to
 71564 ** sqlite3BtreeNext().
 71565 **
 71566 ** If P5 is positive and the jump is taken, then event counter
 71567 ** number P5-1 in the prepared statement is incremented.
 71568 **
 71569 ** See also: Prev, NextIfOpen
 71570 */
 71571 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
 71572 **
 71573 ** This opcode works just like OP_Next except that if cursor P1 is not
 71574 ** open it behaves a no-op.
 71575 */
 71576 /* Opcode: Prev P1 P2 P3 P4 P5
 71577 **
 71578 ** Back up cursor P1 so that it points to the previous key/data pair in its
 71579 ** table or index.  If there is no previous key/value pairs then fall through
 71580 ** to the following instruction.  But if the cursor backup was successful,
 71581 ** jump immediately to P2.
 71582 **
 71583 ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
 71584 ** not open then the behavior is undefined.
 71585 **
 71586 ** The P3 value is a hint to the btree implementation. If P3==1, that
 71587 ** means P1 is an SQL index and that this instruction could have been
 71588 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
 71589 ** always either 0 or 1.
 71590 **
 71591 ** P4 is always of type P4_ADVANCE. The function pointer points to
 71592 ** sqlite3BtreePrevious().
 71593 **
 71594 ** If P5 is positive and the jump is taken, then event counter
 71595 ** number P5-1 in the prepared statement is incremented.
 71596 */
 71597 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
 71598 **
 71599 ** This opcode works just like OP_Prev except that if cursor P1 is not
 71600 ** open it behaves a no-op.
 71601 */
 71602 case OP_SorterNext: {  /* jump */
 71603   VdbeCursor *pC;
 71604   int res;
 71606   pC = p->apCsr[pOp->p1];
 71607   assert( isSorter(pC) );
 71608   rc = sqlite3VdbeSorterNext(db, pC, &res);
 71609   goto next_tail;
 71610 case OP_PrevIfOpen:    /* jump */
 71611 case OP_NextIfOpen:    /* jump */
 71612   if( p->apCsr[pOp->p1]==0 ) break;
 71613   /* Fall through */
 71614 case OP_Prev:          /* jump */
 71615 case OP_Next:          /* jump */
 71616   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71617   assert( pOp->p5<ArraySize(p->aCounter) );
 71618   pC = p->apCsr[pOp->p1];
 71619   res = pOp->p3;
 71620   assert( pC!=0 );
 71621   assert( pC->deferredMoveto==0 );
 71622   assert( pC->pCursor );
 71623   assert( res==0 || (res==1 && pC->isTable==0) );
 71624   testcase( res==1 );
 71625   assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
 71626   assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
 71627   assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
 71628   assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
 71629   rc = pOp->p4.xAdvance(pC->pCursor, &res);
 71630 next_tail:
 71631   pC->cacheStatus = CACHE_STALE;
 71632   VdbeBranchTaken(res==0,2);
 71633   if( res==0 ){
 71634     pC->nullRow = 0;
 71635     pc = pOp->p2 - 1;
 71636     p->aCounter[pOp->p5]++;
 71637 #ifdef SQLITE_TEST
 71638     sqlite3_search_count++;
 71639 #endif
 71640   }else{
 71641     pC->nullRow = 1;
 71643   pC->rowidIsValid = 0;
 71644   goto check_for_interrupt;
 71647 /* Opcode: IdxInsert P1 P2 P3 * P5
 71648 ** Synopsis: key=r[P2]
 71649 **
 71650 ** Register P2 holds an SQL index key made using the
 71651 ** MakeRecord instructions.  This opcode writes that key
 71652 ** into the index P1.  Data for the entry is nil.
 71653 **
 71654 ** P3 is a flag that provides a hint to the b-tree layer that this
 71655 ** insert is likely to be an append.
 71656 **
 71657 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
 71658 ** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
 71659 ** then the change counter is unchanged.
 71660 **
 71661 ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
 71662 ** just done a seek to the spot where the new entry is to be inserted.
 71663 ** This flag avoids doing an extra seek.
 71664 **
 71665 ** This instruction only works for indices.  The equivalent instruction
 71666 ** for tables is OP_Insert.
 71667 */
 71668 case OP_SorterInsert:       /* in2 */
 71669 case OP_IdxInsert: {        /* in2 */
 71670   VdbeCursor *pC;
 71671   BtCursor *pCrsr;
 71672   int nKey;
 71673   const char *zKey;
 71675   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71676   pC = p->apCsr[pOp->p1];
 71677   assert( pC!=0 );
 71678   assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
 71679   pIn2 = &aMem[pOp->p2];
 71680   assert( pIn2->flags & MEM_Blob );
 71681   pCrsr = pC->pCursor;
 71682   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
 71683   assert( pCrsr!=0 );
 71684   assert( pC->isTable==0 );
 71685   rc = ExpandBlob(pIn2);
 71686   if( rc==SQLITE_OK ){
 71687     if( isSorter(pC) ){
 71688       rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
 71689     }else{
 71690       nKey = pIn2->n;
 71691       zKey = pIn2->z;
 71692       rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
 71693           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
 71694           );
 71695       assert( pC->deferredMoveto==0 );
 71696       pC->cacheStatus = CACHE_STALE;
 71699   break;
 71702 /* Opcode: IdxDelete P1 P2 P3 * *
 71703 ** Synopsis: key=r[P2@P3]
 71704 **
 71705 ** The content of P3 registers starting at register P2 form
 71706 ** an unpacked index key. This opcode removes that entry from the 
 71707 ** index opened by cursor P1.
 71708 */
 71709 case OP_IdxDelete: {
 71710   VdbeCursor *pC;
 71711   BtCursor *pCrsr;
 71712   int res;
 71713   UnpackedRecord r;
 71715   assert( pOp->p3>0 );
 71716   assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
 71717   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71718   pC = p->apCsr[pOp->p1];
 71719   assert( pC!=0 );
 71720   pCrsr = pC->pCursor;
 71721   assert( pCrsr!=0 );
 71722   assert( pOp->p5==0 );
 71723   r.pKeyInfo = pC->pKeyInfo;
 71724   r.nField = (u16)pOp->p3;
 71725   r.default_rc = 0;
 71726   r.aMem = &aMem[pOp->p2];
 71727 #ifdef SQLITE_DEBUG
 71728   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 71729 #endif
 71730   rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
 71731   if( rc==SQLITE_OK && res==0 ){
 71732     rc = sqlite3BtreeDelete(pCrsr);
 71734   assert( pC->deferredMoveto==0 );
 71735   pC->cacheStatus = CACHE_STALE;
 71736   break;
 71739 /* Opcode: IdxRowid P1 P2 * * *
 71740 ** Synopsis: r[P2]=rowid
 71741 **
 71742 ** Write into register P2 an integer which is the last entry in the record at
 71743 ** the end of the index key pointed to by cursor P1.  This integer should be
 71744 ** the rowid of the table entry to which this index entry points.
 71745 **
 71746 ** See also: Rowid, MakeRecord.
 71747 */
 71748 case OP_IdxRowid: {              /* out2-prerelease */
 71749   BtCursor *pCrsr;
 71750   VdbeCursor *pC;
 71751   i64 rowid;
 71753   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71754   pC = p->apCsr[pOp->p1];
 71755   assert( pC!=0 );
 71756   pCrsr = pC->pCursor;
 71757   assert( pCrsr!=0 );
 71758   pOut->flags = MEM_Null;
 71759   rc = sqlite3VdbeCursorMoveto(pC);
 71760   if( NEVER(rc) ) goto abort_due_to_error;
 71761   assert( pC->deferredMoveto==0 );
 71762   assert( pC->isTable==0 );
 71763   if( !pC->nullRow ){
 71764     rowid = 0;  /* Not needed.  Only used to silence a warning. */
 71765     rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
 71766     if( rc!=SQLITE_OK ){
 71767       goto abort_due_to_error;
 71769     pOut->u.i = rowid;
 71770     pOut->flags = MEM_Int;
 71772   break;
 71775 /* Opcode: IdxGE P1 P2 P3 P4 P5
 71776 ** Synopsis: key=r[P3@P4]
 71777 **
 71778 ** The P4 register values beginning with P3 form an unpacked index 
 71779 ** key that omits the PRIMARY KEY.  Compare this key value against the index 
 71780 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 
 71781 ** fields at the end.
 71782 **
 71783 ** If the P1 index entry is greater than or equal to the key value
 71784 ** then jump to P2.  Otherwise fall through to the next instruction.
 71785 */
 71786 /* Opcode: IdxGT P1 P2 P3 P4 P5
 71787 ** Synopsis: key=r[P3@P4]
 71788 **
 71789 ** The P4 register values beginning with P3 form an unpacked index 
 71790 ** key that omits the PRIMARY KEY.  Compare this key value against the index 
 71791 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID 
 71792 ** fields at the end.
 71793 **
 71794 ** If the P1 index entry is greater than the key value
 71795 ** then jump to P2.  Otherwise fall through to the next instruction.
 71796 */
 71797 /* Opcode: IdxLT P1 P2 P3 P4 P5
 71798 ** Synopsis: key=r[P3@P4]
 71799 **
 71800 ** The P4 register values beginning with P3 form an unpacked index 
 71801 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
 71802 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
 71803 ** ROWID on the P1 index.
 71804 **
 71805 ** If the P1 index entry is less than the key value then jump to P2.
 71806 ** Otherwise fall through to the next instruction.
 71807 */
 71808 /* Opcode: IdxLE P1 P2 P3 P4 P5
 71809 ** Synopsis: key=r[P3@P4]
 71810 **
 71811 ** The P4 register values beginning with P3 form an unpacked index 
 71812 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
 71813 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
 71814 ** ROWID on the P1 index.
 71815 **
 71816 ** If the P1 index entry is less than or equal to the key value then jump
 71817 ** to P2. Otherwise fall through to the next instruction.
 71818 */
 71819 case OP_IdxLE:          /* jump */
 71820 case OP_IdxGT:          /* jump */
 71821 case OP_IdxLT:          /* jump */
 71822 case OP_IdxGE:  {       /* jump */
 71823   VdbeCursor *pC;
 71824   int res;
 71825   UnpackedRecord r;
 71827   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 71828   pC = p->apCsr[pOp->p1];
 71829   assert( pC!=0 );
 71830   assert( pC->isOrdered );
 71831   assert( pC->pCursor!=0);
 71832   assert( pC->deferredMoveto==0 );
 71833   assert( pOp->p5==0 || pOp->p5==1 );
 71834   assert( pOp->p4type==P4_INT32 );
 71835   r.pKeyInfo = pC->pKeyInfo;
 71836   r.nField = (u16)pOp->p4.i;
 71837   if( pOp->opcode<OP_IdxLT ){
 71838     assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
 71839     r.default_rc = -1;
 71840   }else{
 71841     assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
 71842     r.default_rc = 0;
 71844   r.aMem = &aMem[pOp->p3];
 71845 #ifdef SQLITE_DEBUG
 71846   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 71847 #endif
 71848   res = 0;  /* Not needed.  Only used to silence a warning. */
 71849   rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
 71850   assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
 71851   if( (pOp->opcode&1)==(OP_IdxLT&1) ){
 71852     assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
 71853     res = -res;
 71854   }else{
 71855     assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
 71856     res++;
 71858   VdbeBranchTaken(res>0,2);
 71859   if( res>0 ){
 71860     pc = pOp->p2 - 1 ;
 71862   break;
 71865 /* Opcode: Destroy P1 P2 P3 * *
 71866 **
 71867 ** Delete an entire database table or index whose root page in the database
 71868 ** file is given by P1.
 71869 **
 71870 ** The table being destroyed is in the main database file if P3==0.  If
 71871 ** P3==1 then the table to be clear is in the auxiliary database file
 71872 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 71873 **
 71874 ** If AUTOVACUUM is enabled then it is possible that another root page
 71875 ** might be moved into the newly deleted root page in order to keep all
 71876 ** root pages contiguous at the beginning of the database.  The former
 71877 ** value of the root page that moved - its value before the move occurred -
 71878 ** is stored in register P2.  If no page 
 71879 ** movement was required (because the table being dropped was already 
 71880 ** the last one in the database) then a zero is stored in register P2.
 71881 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
 71882 **
 71883 ** See also: Clear
 71884 */
 71885 case OP_Destroy: {     /* out2-prerelease */
 71886   int iMoved;
 71887   int iCnt;
 71888   Vdbe *pVdbe;
 71889   int iDb;
 71891   assert( p->readOnly==0 );
 71892 #ifndef SQLITE_OMIT_VIRTUALTABLE
 71893   iCnt = 0;
 71894   for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
 71895     if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader 
 71896      && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 
 71897     ){
 71898       iCnt++;
 71901 #else
 71902   iCnt = db->nVdbeRead;
 71903 #endif
 71904   pOut->flags = MEM_Null;
 71905   if( iCnt>1 ){
 71906     rc = SQLITE_LOCKED;
 71907     p->errorAction = OE_Abort;
 71908   }else{
 71909     iDb = pOp->p3;
 71910     assert( iCnt==1 );
 71911     assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
 71912     iMoved = 0;  /* Not needed.  Only to silence a warning. */
 71913     rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
 71914     pOut->flags = MEM_Int;
 71915     pOut->u.i = iMoved;
 71916 #ifndef SQLITE_OMIT_AUTOVACUUM
 71917     if( rc==SQLITE_OK && iMoved!=0 ){
 71918       sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
 71919       /* All OP_Destroy operations occur on the same btree */
 71920       assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
 71921       resetSchemaOnFault = iDb+1;
 71923 #endif
 71925   break;
 71928 /* Opcode: Clear P1 P2 P3
 71929 **
 71930 ** Delete all contents of the database table or index whose root page
 71931 ** in the database file is given by P1.  But, unlike Destroy, do not
 71932 ** remove the table or index from the database file.
 71933 **
 71934 ** The table being clear is in the main database file if P2==0.  If
 71935 ** P2==1 then the table to be clear is in the auxiliary database file
 71936 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 71937 **
 71938 ** If the P3 value is non-zero, then the table referred to must be an
 71939 ** intkey table (an SQL table, not an index). In this case the row change 
 71940 ** count is incremented by the number of rows in the table being cleared. 
 71941 ** If P3 is greater than zero, then the value stored in register P3 is
 71942 ** also incremented by the number of rows in the table being cleared.
 71943 **
 71944 ** See also: Destroy
 71945 */
 71946 case OP_Clear: {
 71947   int nChange;
 71949   nChange = 0;
 71950   assert( p->readOnly==0 );
 71951   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 71952   rc = sqlite3BtreeClearTable(
 71953       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
 71954   );
 71955   if( pOp->p3 ){
 71956     p->nChange += nChange;
 71957     if( pOp->p3>0 ){
 71958       assert( memIsValid(&aMem[pOp->p3]) );
 71959       memAboutToChange(p, &aMem[pOp->p3]);
 71960       aMem[pOp->p3].u.i += nChange;
 71963   break;
 71966 /* Opcode: CreateTable P1 P2 * * *
 71967 ** Synopsis: r[P2]=root iDb=P1
 71968 **
 71969 ** Allocate a new table in the main database file if P1==0 or in the
 71970 ** auxiliary database file if P1==1 or in an attached database if
 71971 ** P1>1.  Write the root page number of the new table into
 71972 ** register P2
 71973 **
 71974 ** The difference between a table and an index is this:  A table must
 71975 ** have a 4-byte integer key and can have arbitrary data.  An index
 71976 ** has an arbitrary key but no data.
 71977 **
 71978 ** See also: CreateIndex
 71979 */
 71980 /* Opcode: CreateIndex P1 P2 * * *
 71981 ** Synopsis: r[P2]=root iDb=P1
 71982 **
 71983 ** Allocate a new index in the main database file if P1==0 or in the
 71984 ** auxiliary database file if P1==1 or in an attached database if
 71985 ** P1>1.  Write the root page number of the new table into
 71986 ** register P2.
 71987 **
 71988 ** See documentation on OP_CreateTable for additional information.
 71989 */
 71990 case OP_CreateIndex:            /* out2-prerelease */
 71991 case OP_CreateTable: {          /* out2-prerelease */
 71992   int pgno;
 71993   int flags;
 71994   Db *pDb;
 71996   pgno = 0;
 71997   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 71998   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 71999   assert( p->readOnly==0 );
 72000   pDb = &db->aDb[pOp->p1];
 72001   assert( pDb->pBt!=0 );
 72002   if( pOp->opcode==OP_CreateTable ){
 72003     /* flags = BTREE_INTKEY; */
 72004     flags = BTREE_INTKEY;
 72005   }else{
 72006     flags = BTREE_BLOBKEY;
 72008   rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
 72009   pOut->u.i = pgno;
 72010   break;
 72013 /* Opcode: ParseSchema P1 * * P4 *
 72014 **
 72015 ** Read and parse all entries from the SQLITE_MASTER table of database P1
 72016 ** that match the WHERE clause P4. 
 72017 **
 72018 ** This opcode invokes the parser to create a new virtual machine,
 72019 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
 72020 */
 72021 case OP_ParseSchema: {
 72022   int iDb;
 72023   const char *zMaster;
 72024   char *zSql;
 72025   InitData initData;
 72027   /* Any prepared statement that invokes this opcode will hold mutexes
 72028   ** on every btree.  This is a prerequisite for invoking 
 72029   ** sqlite3InitCallback().
 72030   */
 72031 #ifdef SQLITE_DEBUG
 72032   for(iDb=0; iDb<db->nDb; iDb++){
 72033     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
 72035 #endif
 72037   iDb = pOp->p1;
 72038   assert( iDb>=0 && iDb<db->nDb );
 72039   assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
 72040   /* Used to be a conditional */ {
 72041     zMaster = SCHEMA_TABLE(iDb);
 72042     initData.db = db;
 72043     initData.iDb = pOp->p1;
 72044     initData.pzErrMsg = &p->zErrMsg;
 72045     zSql = sqlite3MPrintf(db,
 72046        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 72047        db->aDb[iDb].zName, zMaster, pOp->p4.z);
 72048     if( zSql==0 ){
 72049       rc = SQLITE_NOMEM;
 72050     }else{
 72051       assert( db->init.busy==0 );
 72052       db->init.busy = 1;
 72053       initData.rc = SQLITE_OK;
 72054       assert( !db->mallocFailed );
 72055       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
 72056       if( rc==SQLITE_OK ) rc = initData.rc;
 72057       sqlite3DbFree(db, zSql);
 72058       db->init.busy = 0;
 72061   if( rc ) sqlite3ResetAllSchemasOfConnection(db);
 72062   if( rc==SQLITE_NOMEM ){
 72063     goto no_mem;
 72065   break;  
 72068 #if !defined(SQLITE_OMIT_ANALYZE)
 72069 /* Opcode: LoadAnalysis P1 * * * *
 72070 **
 72071 ** Read the sqlite_stat1 table for database P1 and load the content
 72072 ** of that table into the internal index hash table.  This will cause
 72073 ** the analysis to be used when preparing all subsequent queries.
 72074 */
 72075 case OP_LoadAnalysis: {
 72076   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 72077   rc = sqlite3AnalysisLoad(db, pOp->p1);
 72078   break;  
 72080 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
 72082 /* Opcode: DropTable P1 * * P4 *
 72083 **
 72084 ** Remove the internal (in-memory) data structures that describe
 72085 ** the table named P4 in database P1.  This is called after a table
 72086 ** is dropped in order to keep the internal representation of the
 72087 ** schema consistent with what is on disk.
 72088 */
 72089 case OP_DropTable: {
 72090   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
 72091   break;
 72094 /* Opcode: DropIndex P1 * * P4 *
 72095 **
 72096 ** Remove the internal (in-memory) data structures that describe
 72097 ** the index named P4 in database P1.  This is called after an index
 72098 ** is dropped in order to keep the internal representation of the
 72099 ** schema consistent with what is on disk.
 72100 */
 72101 case OP_DropIndex: {
 72102   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
 72103   break;
 72106 /* Opcode: DropTrigger P1 * * P4 *
 72107 **
 72108 ** Remove the internal (in-memory) data structures that describe
 72109 ** the trigger named P4 in database P1.  This is called after a trigger
 72110 ** is dropped in order to keep the internal representation of the
 72111 ** schema consistent with what is on disk.
 72112 */
 72113 case OP_DropTrigger: {
 72114   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
 72115   break;
 72119 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 72120 /* Opcode: IntegrityCk P1 P2 P3 * P5
 72121 **
 72122 ** Do an analysis of the currently open database.  Store in
 72123 ** register P1 the text of an error message describing any problems.
 72124 ** If no problems are found, store a NULL in register P1.
 72125 **
 72126 ** The register P3 contains the maximum number of allowed errors.
 72127 ** At most reg(P3) errors will be reported.
 72128 ** In other words, the analysis stops as soon as reg(P1) errors are 
 72129 ** seen.  Reg(P1) is updated with the number of errors remaining.
 72130 **
 72131 ** The root page numbers of all tables in the database are integer
 72132 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
 72133 ** total.
 72134 **
 72135 ** If P5 is not zero, the check is done on the auxiliary database
 72136 ** file, not the main database file.
 72137 **
 72138 ** This opcode is used to implement the integrity_check pragma.
 72139 */
 72140 case OP_IntegrityCk: {
 72141   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
 72142   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
 72143   int j;          /* Loop counter */
 72144   int nErr;       /* Number of errors reported */
 72145   char *z;        /* Text of the error report */
 72146   Mem *pnErr;     /* Register keeping track of errors remaining */
 72148   assert( p->bIsReader );
 72149   nRoot = pOp->p2;
 72150   assert( nRoot>0 );
 72151   aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
 72152   if( aRoot==0 ) goto no_mem;
 72153   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 72154   pnErr = &aMem[pOp->p3];
 72155   assert( (pnErr->flags & MEM_Int)!=0 );
 72156   assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 72157   pIn1 = &aMem[pOp->p1];
 72158   for(j=0; j<nRoot; j++){
 72159     aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
 72161   aRoot[j] = 0;
 72162   assert( pOp->p5<db->nDb );
 72163   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 72164   z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
 72165                                  (int)pnErr->u.i, &nErr);
 72166   sqlite3DbFree(db, aRoot);
 72167   pnErr->u.i -= nErr;
 72168   sqlite3VdbeMemSetNull(pIn1);
 72169   if( nErr==0 ){
 72170     assert( z==0 );
 72171   }else if( z==0 ){
 72172     goto no_mem;
 72173   }else{
 72174     sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
 72176   UPDATE_MAX_BLOBSIZE(pIn1);
 72177   sqlite3VdbeChangeEncoding(pIn1, encoding);
 72178   break;
 72180 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 72182 /* Opcode: RowSetAdd P1 P2 * * *
 72183 ** Synopsis:  rowset(P1)=r[P2]
 72184 **
 72185 ** Insert the integer value held by register P2 into a boolean index
 72186 ** held in register P1.
 72187 **
 72188 ** An assertion fails if P2 is not an integer.
 72189 */
 72190 case OP_RowSetAdd: {       /* in1, in2 */
 72191   pIn1 = &aMem[pOp->p1];
 72192   pIn2 = &aMem[pOp->p2];
 72193   assert( (pIn2->flags & MEM_Int)!=0 );
 72194   if( (pIn1->flags & MEM_RowSet)==0 ){
 72195     sqlite3VdbeMemSetRowSet(pIn1);
 72196     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 72198   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
 72199   break;
 72202 /* Opcode: RowSetRead P1 P2 P3 * *
 72203 ** Synopsis:  r[P3]=rowset(P1)
 72204 **
 72205 ** Extract the smallest value from boolean index P1 and put that value into
 72206 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
 72207 ** unchanged and jump to instruction P2.
 72208 */
 72209 case OP_RowSetRead: {       /* jump, in1, out3 */
 72210   i64 val;
 72212   pIn1 = &aMem[pOp->p1];
 72213   if( (pIn1->flags & MEM_RowSet)==0 
 72214    || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
 72215   ){
 72216     /* The boolean index is empty */
 72217     sqlite3VdbeMemSetNull(pIn1);
 72218     pc = pOp->p2 - 1;
 72219     VdbeBranchTaken(1,2);
 72220   }else{
 72221     /* A value was pulled from the index */
 72222     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
 72223     VdbeBranchTaken(0,2);
 72225   goto check_for_interrupt;
 72228 /* Opcode: RowSetTest P1 P2 P3 P4
 72229 ** Synopsis: if r[P3] in rowset(P1) goto P2
 72230 **
 72231 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
 72232 ** contains a RowSet object and that RowSet object contains
 72233 ** the value held in P3, jump to register P2. Otherwise, insert the
 72234 ** integer in P3 into the RowSet and continue on to the
 72235 ** next opcode.
 72236 **
 72237 ** The RowSet object is optimized for the case where successive sets
 72238 ** of integers, where each set contains no duplicates. Each set
 72239 ** of values is identified by a unique P4 value. The first set
 72240 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
 72241 ** non-negative.  For non-negative values of P4 only the lower 4
 72242 ** bits are significant.
 72243 **
 72244 ** This allows optimizations: (a) when P4==0 there is no need to test
 72245 ** the rowset object for P3, as it is guaranteed not to contain it,
 72246 ** (b) when P4==-1 there is no need to insert the value, as it will
 72247 ** never be tested for, and (c) when a value that is part of set X is
 72248 ** inserted, there is no need to search to see if the same value was
 72249 ** previously inserted as part of set X (only if it was previously
 72250 ** inserted as part of some other set).
 72251 */
 72252 case OP_RowSetTest: {                     /* jump, in1, in3 */
 72253   int iSet;
 72254   int exists;
 72256   pIn1 = &aMem[pOp->p1];
 72257   pIn3 = &aMem[pOp->p3];
 72258   iSet = pOp->p4.i;
 72259   assert( pIn3->flags&MEM_Int );
 72261   /* If there is anything other than a rowset object in memory cell P1,
 72262   ** delete it now and initialize P1 with an empty rowset
 72263   */
 72264   if( (pIn1->flags & MEM_RowSet)==0 ){
 72265     sqlite3VdbeMemSetRowSet(pIn1);
 72266     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
 72269   assert( pOp->p4type==P4_INT32 );
 72270   assert( iSet==-1 || iSet>=0 );
 72271   if( iSet ){
 72272     exists = sqlite3RowSetTest(pIn1->u.pRowSet, 
 72273                                (u8)(iSet>=0 ? iSet & 0xf : 0xff),
 72274                                pIn3->u.i);
 72275     VdbeBranchTaken(exists!=0,2);
 72276     if( exists ){
 72277       pc = pOp->p2 - 1;
 72278       break;
 72281   if( iSet>=0 ){
 72282     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
 72284   break;
 72288 #ifndef SQLITE_OMIT_TRIGGER
 72290 /* Opcode: Program P1 P2 P3 P4 P5
 72291 **
 72292 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
 72293 **
 72294 ** P1 contains the address of the memory cell that contains the first memory 
 72295 ** cell in an array of values used as arguments to the sub-program. P2 
 72296 ** contains the address to jump to if the sub-program throws an IGNORE 
 72297 ** exception using the RAISE() function. Register P3 contains the address 
 72298 ** of a memory cell in this (the parent) VM that is used to allocate the 
 72299 ** memory required by the sub-vdbe at runtime.
 72300 **
 72301 ** P4 is a pointer to the VM containing the trigger program.
 72302 **
 72303 ** If P5 is non-zero, then recursive program invocation is enabled.
 72304 */
 72305 case OP_Program: {        /* jump */
 72306   int nMem;               /* Number of memory registers for sub-program */
 72307   int nByte;              /* Bytes of runtime space required for sub-program */
 72308   Mem *pRt;               /* Register to allocate runtime space */
 72309   Mem *pMem;              /* Used to iterate through memory cells */
 72310   Mem *pEnd;              /* Last memory cell in new array */
 72311   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
 72312   SubProgram *pProgram;   /* Sub-program to execute */
 72313   void *t;                /* Token identifying trigger */
 72315   pProgram = pOp->p4.pProgram;
 72316   pRt = &aMem[pOp->p3];
 72317   assert( pProgram->nOp>0 );
 72319   /* If the p5 flag is clear, then recursive invocation of triggers is 
 72320   ** disabled for backwards compatibility (p5 is set if this sub-program
 72321   ** is really a trigger, not a foreign key action, and the flag set
 72322   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
 72323   ** 
 72324   ** It is recursive invocation of triggers, at the SQL level, that is 
 72325   ** disabled. In some cases a single trigger may generate more than one 
 72326   ** SubProgram (if the trigger may be executed with more than one different 
 72327   ** ON CONFLICT algorithm). SubProgram structures associated with a
 72328   ** single trigger all have the same value for the SubProgram.token 
 72329   ** variable.  */
 72330   if( pOp->p5 ){
 72331     t = pProgram->token;
 72332     for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
 72333     if( pFrame ) break;
 72336   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
 72337     rc = SQLITE_ERROR;
 72338     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
 72339     break;
 72342   /* Register pRt is used to store the memory required to save the state
 72343   ** of the current program, and the memory required at runtime to execute
 72344   ** the trigger program. If this trigger has been fired before, then pRt 
 72345   ** is already allocated. Otherwise, it must be initialized.  */
 72346   if( (pRt->flags&MEM_Frame)==0 ){
 72347     /* SubProgram.nMem is set to the number of memory cells used by the 
 72348     ** program stored in SubProgram.aOp. As well as these, one memory
 72349     ** cell is required for each cursor used by the program. Set local
 72350     ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
 72351     */
 72352     nMem = pProgram->nMem + pProgram->nCsr;
 72353     nByte = ROUND8(sizeof(VdbeFrame))
 72354               + nMem * sizeof(Mem)
 72355               + pProgram->nCsr * sizeof(VdbeCursor *)
 72356               + pProgram->nOnce * sizeof(u8);
 72357     pFrame = sqlite3DbMallocZero(db, nByte);
 72358     if( !pFrame ){
 72359       goto no_mem;
 72361     sqlite3VdbeMemRelease(pRt);
 72362     pRt->flags = MEM_Frame;
 72363     pRt->u.pFrame = pFrame;
 72365     pFrame->v = p;
 72366     pFrame->nChildMem = nMem;
 72367     pFrame->nChildCsr = pProgram->nCsr;
 72368     pFrame->pc = pc;
 72369     pFrame->aMem = p->aMem;
 72370     pFrame->nMem = p->nMem;
 72371     pFrame->apCsr = p->apCsr;
 72372     pFrame->nCursor = p->nCursor;
 72373     pFrame->aOp = p->aOp;
 72374     pFrame->nOp = p->nOp;
 72375     pFrame->token = pProgram->token;
 72376     pFrame->aOnceFlag = p->aOnceFlag;
 72377     pFrame->nOnceFlag = p->nOnceFlag;
 72379     pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
 72380     for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
 72381       pMem->flags = MEM_Undefined;
 72382       pMem->db = db;
 72384   }else{
 72385     pFrame = pRt->u.pFrame;
 72386     assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
 72387     assert( pProgram->nCsr==pFrame->nChildCsr );
 72388     assert( pc==pFrame->pc );
 72391   p->nFrame++;
 72392   pFrame->pParent = p->pFrame;
 72393   pFrame->lastRowid = lastRowid;
 72394   pFrame->nChange = p->nChange;
 72395   p->nChange = 0;
 72396   p->pFrame = pFrame;
 72397   p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
 72398   p->nMem = pFrame->nChildMem;
 72399   p->nCursor = (u16)pFrame->nChildCsr;
 72400   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
 72401   p->aOp = aOp = pProgram->aOp;
 72402   p->nOp = pProgram->nOp;
 72403   p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
 72404   p->nOnceFlag = pProgram->nOnce;
 72405   pc = -1;
 72406   memset(p->aOnceFlag, 0, p->nOnceFlag);
 72408   break;
 72411 /* Opcode: Param P1 P2 * * *
 72412 **
 72413 ** This opcode is only ever present in sub-programs called via the 
 72414 ** OP_Program instruction. Copy a value currently stored in a memory 
 72415 ** cell of the calling (parent) frame to cell P2 in the current frames 
 72416 ** address space. This is used by trigger programs to access the new.* 
 72417 ** and old.* values.
 72418 **
 72419 ** The address of the cell in the parent frame is determined by adding
 72420 ** the value of the P1 argument to the value of the P1 argument to the
 72421 ** calling OP_Program instruction.
 72422 */
 72423 case OP_Param: {           /* out2-prerelease */
 72424   VdbeFrame *pFrame;
 72425   Mem *pIn;
 72426   pFrame = p->pFrame;
 72427   pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];   
 72428   sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
 72429   break;
 72432 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
 72434 #ifndef SQLITE_OMIT_FOREIGN_KEY
 72435 /* Opcode: FkCounter P1 P2 * * *
 72436 ** Synopsis: fkctr[P1]+=P2
 72437 **
 72438 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
 72439 ** If P1 is non-zero, the database constraint counter is incremented 
 72440 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
 72441 ** statement counter is incremented (immediate foreign key constraints).
 72442 */
 72443 case OP_FkCounter: {
 72444   if( db->flags & SQLITE_DeferFKs ){
 72445     db->nDeferredImmCons += pOp->p2;
 72446   }else if( pOp->p1 ){
 72447     db->nDeferredCons += pOp->p2;
 72448   }else{
 72449     p->nFkConstraint += pOp->p2;
 72451   break;
 72454 /* Opcode: FkIfZero P1 P2 * * *
 72455 ** Synopsis: if fkctr[P1]==0 goto P2
 72456 **
 72457 ** This opcode tests if a foreign key constraint-counter is currently zero.
 72458 ** If so, jump to instruction P2. Otherwise, fall through to the next 
 72459 ** instruction.
 72460 **
 72461 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
 72462 ** is zero (the one that counts deferred constraint violations). If P1 is
 72463 ** zero, the jump is taken if the statement constraint-counter is zero
 72464 ** (immediate foreign key constraint violations).
 72465 */
 72466 case OP_FkIfZero: {         /* jump */
 72467   if( pOp->p1 ){
 72468     VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
 72469     if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
 72470   }else{
 72471     VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
 72472     if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
 72474   break;
 72476 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
 72478 #ifndef SQLITE_OMIT_AUTOINCREMENT
 72479 /* Opcode: MemMax P1 P2 * * *
 72480 ** Synopsis: r[P1]=max(r[P1],r[P2])
 72481 **
 72482 ** P1 is a register in the root frame of this VM (the root frame is
 72483 ** different from the current frame if this instruction is being executed
 72484 ** within a sub-program). Set the value of register P1 to the maximum of 
 72485 ** its current value and the value in register P2.
 72486 **
 72487 ** This instruction throws an error if the memory cell is not initially
 72488 ** an integer.
 72489 */
 72490 case OP_MemMax: {        /* in2 */
 72491   VdbeFrame *pFrame;
 72492   if( p->pFrame ){
 72493     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 72494     pIn1 = &pFrame->aMem[pOp->p1];
 72495   }else{
 72496     pIn1 = &aMem[pOp->p1];
 72498   assert( memIsValid(pIn1) );
 72499   sqlite3VdbeMemIntegerify(pIn1);
 72500   pIn2 = &aMem[pOp->p2];
 72501   sqlite3VdbeMemIntegerify(pIn2);
 72502   if( pIn1->u.i<pIn2->u.i){
 72503     pIn1->u.i = pIn2->u.i;
 72505   break;
 72507 #endif /* SQLITE_OMIT_AUTOINCREMENT */
 72509 /* Opcode: IfPos P1 P2 * * *
 72510 ** Synopsis: if r[P1]>0 goto P2
 72511 **
 72512 ** If the value of register P1 is 1 or greater, jump to P2.
 72513 **
 72514 ** It is illegal to use this instruction on a register that does
 72515 ** not contain an integer.  An assertion fault will result if you try.
 72516 */
 72517 case OP_IfPos: {        /* jump, in1 */
 72518   pIn1 = &aMem[pOp->p1];
 72519   assert( pIn1->flags&MEM_Int );
 72520   VdbeBranchTaken( pIn1->u.i>0, 2);
 72521   if( pIn1->u.i>0 ){
 72522      pc = pOp->p2 - 1;
 72524   break;
 72527 /* Opcode: IfNeg P1 P2 * * *
 72528 ** Synopsis: if r[P1]<0 goto P2
 72529 **
 72530 ** If the value of register P1 is less than zero, jump to P2. 
 72531 **
 72532 ** It is illegal to use this instruction on a register that does
 72533 ** not contain an integer.  An assertion fault will result if you try.
 72534 */
 72535 case OP_IfNeg: {        /* jump, in1 */
 72536   pIn1 = &aMem[pOp->p1];
 72537   assert( pIn1->flags&MEM_Int );
 72538   VdbeBranchTaken(pIn1->u.i<0, 2);
 72539   if( pIn1->u.i<0 ){
 72540      pc = pOp->p2 - 1;
 72542   break;
 72545 /* Opcode: IfZero P1 P2 P3 * *
 72546 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
 72547 **
 72548 ** The register P1 must contain an integer.  Add literal P3 to the
 72549 ** value in register P1.  If the result is exactly 0, jump to P2. 
 72550 **
 72551 ** It is illegal to use this instruction on a register that does
 72552 ** not contain an integer.  An assertion fault will result if you try.
 72553 */
 72554 case OP_IfZero: {        /* jump, in1 */
 72555   pIn1 = &aMem[pOp->p1];
 72556   assert( pIn1->flags&MEM_Int );
 72557   pIn1->u.i += pOp->p3;
 72558   VdbeBranchTaken(pIn1->u.i==0, 2);
 72559   if( pIn1->u.i==0 ){
 72560      pc = pOp->p2 - 1;
 72562   break;
 72565 /* Opcode: AggStep * P2 P3 P4 P5
 72566 ** Synopsis: accum=r[P3] step(r[P2@P5])
 72567 **
 72568 ** Execute the step function for an aggregate.  The
 72569 ** function has P5 arguments.   P4 is a pointer to the FuncDef
 72570 ** structure that specifies the function.  Use register
 72571 ** P3 as the accumulator.
 72572 **
 72573 ** The P5 arguments are taken from register P2 and its
 72574 ** successors.
 72575 */
 72576 case OP_AggStep: {
 72577   int n;
 72578   int i;
 72579   Mem *pMem;
 72580   Mem *pRec;
 72581   sqlite3_context ctx;
 72582   sqlite3_value **apVal;
 72584   n = pOp->p5;
 72585   assert( n>=0 );
 72586   pRec = &aMem[pOp->p2];
 72587   apVal = p->apArg;
 72588   assert( apVal || n==0 );
 72589   for(i=0; i<n; i++, pRec++){
 72590     assert( memIsValid(pRec) );
 72591     apVal[i] = pRec;
 72592     memAboutToChange(p, pRec);
 72594   ctx.pFunc = pOp->p4.pFunc;
 72595   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 72596   ctx.pMem = pMem = &aMem[pOp->p3];
 72597   pMem->n++;
 72598   ctx.s.flags = MEM_Null;
 72599   ctx.s.z = 0;
 72600   ctx.s.zMalloc = 0;
 72601   ctx.s.xDel = 0;
 72602   ctx.s.db = db;
 72603   ctx.isError = 0;
 72604   ctx.pColl = 0;
 72605   ctx.skipFlag = 0;
 72606   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
 72607     assert( pOp>p->aOp );
 72608     assert( pOp[-1].p4type==P4_COLLSEQ );
 72609     assert( pOp[-1].opcode==OP_CollSeq );
 72610     ctx.pColl = pOp[-1].p4.pColl;
 72612   (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
 72613   if( ctx.isError ){
 72614     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
 72615     rc = ctx.isError;
 72617   if( ctx.skipFlag ){
 72618     assert( pOp[-1].opcode==OP_CollSeq );
 72619     i = pOp[-1].p1;
 72620     if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
 72623   sqlite3VdbeMemRelease(&ctx.s);
 72625   break;
 72628 /* Opcode: AggFinal P1 P2 * P4 *
 72629 ** Synopsis: accum=r[P1] N=P2
 72630 **
 72631 ** Execute the finalizer function for an aggregate.  P1 is
 72632 ** the memory location that is the accumulator for the aggregate.
 72633 **
 72634 ** P2 is the number of arguments that the step function takes and
 72635 ** P4 is a pointer to the FuncDef for this function.  The P2
 72636 ** argument is not used by this opcode.  It is only there to disambiguate
 72637 ** functions that can take varying numbers of arguments.  The
 72638 ** P4 argument is only needed for the degenerate case where
 72639 ** the step function was not previously called.
 72640 */
 72641 case OP_AggFinal: {
 72642   Mem *pMem;
 72643   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
 72644   pMem = &aMem[pOp->p1];
 72645   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
 72646   rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
 72647   if( rc ){
 72648     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
 72650   sqlite3VdbeChangeEncoding(pMem, encoding);
 72651   UPDATE_MAX_BLOBSIZE(pMem);
 72652   if( sqlite3VdbeMemTooBig(pMem) ){
 72653     goto too_big;
 72655   break;
 72658 #ifndef SQLITE_OMIT_WAL
 72659 /* Opcode: Checkpoint P1 P2 P3 * *
 72660 **
 72661 ** Checkpoint database P1. This is a no-op if P1 is not currently in
 72662 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
 72663 ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
 72664 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
 72665 ** WAL after the checkpoint into mem[P3+1] and the number of pages
 72666 ** in the WAL that have been checkpointed after the checkpoint
 72667 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
 72668 ** mem[P3+2] are initialized to -1.
 72669 */
 72670 case OP_Checkpoint: {
 72671   int i;                          /* Loop counter */
 72672   int aRes[3];                    /* Results */
 72673   Mem *pMem;                      /* Write results here */
 72675   assert( p->readOnly==0 );
 72676   aRes[0] = 0;
 72677   aRes[1] = aRes[2] = -1;
 72678   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
 72679        || pOp->p2==SQLITE_CHECKPOINT_FULL
 72680        || pOp->p2==SQLITE_CHECKPOINT_RESTART
 72681   );
 72682   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
 72683   if( rc==SQLITE_BUSY ){
 72684     rc = SQLITE_OK;
 72685     aRes[0] = 1;
 72687   for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
 72688     sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
 72690   break;
 72691 };  
 72692 #endif
 72694 #ifndef SQLITE_OMIT_PRAGMA
 72695 /* Opcode: JournalMode P1 P2 P3 * *
 72696 **
 72697 ** Change the journal mode of database P1 to P3. P3 must be one of the
 72698 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
 72699 ** modes (delete, truncate, persist, off and memory), this is a simple
 72700 ** operation. No IO is required.
 72701 **
 72702 ** If changing into or out of WAL mode the procedure is more complicated.
 72703 **
 72704 ** Write a string containing the final journal-mode to register P2.
 72705 */
 72706 case OP_JournalMode: {    /* out2-prerelease */
 72707   Btree *pBt;                     /* Btree to change journal mode of */
 72708   Pager *pPager;                  /* Pager associated with pBt */
 72709   int eNew;                       /* New journal mode */
 72710   int eOld;                       /* The old journal mode */
 72711 #ifndef SQLITE_OMIT_WAL
 72712   const char *zFilename;          /* Name of database file for pPager */
 72713 #endif
 72715   eNew = pOp->p3;
 72716   assert( eNew==PAGER_JOURNALMODE_DELETE 
 72717        || eNew==PAGER_JOURNALMODE_TRUNCATE 
 72718        || eNew==PAGER_JOURNALMODE_PERSIST 
 72719        || eNew==PAGER_JOURNALMODE_OFF
 72720        || eNew==PAGER_JOURNALMODE_MEMORY
 72721        || eNew==PAGER_JOURNALMODE_WAL
 72722        || eNew==PAGER_JOURNALMODE_QUERY
 72723   );
 72724   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 72725   assert( p->readOnly==0 );
 72727   pBt = db->aDb[pOp->p1].pBt;
 72728   pPager = sqlite3BtreePager(pBt);
 72729   eOld = sqlite3PagerGetJournalMode(pPager);
 72730   if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
 72731   if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
 72733 #ifndef SQLITE_OMIT_WAL
 72734   zFilename = sqlite3PagerFilename(pPager, 1);
 72736   /* Do not allow a transition to journal_mode=WAL for a database
 72737   ** in temporary storage or if the VFS does not support shared memory 
 72738   */
 72739   if( eNew==PAGER_JOURNALMODE_WAL
 72740    && (sqlite3Strlen30(zFilename)==0           /* Temp file */
 72741        || !sqlite3PagerWalSupported(pPager))   /* No shared-memory support */
 72742   ){
 72743     eNew = eOld;
 72746   if( (eNew!=eOld)
 72747    && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
 72748   ){
 72749     if( !db->autoCommit || db->nVdbeRead>1 ){
 72750       rc = SQLITE_ERROR;
 72751       sqlite3SetString(&p->zErrMsg, db, 
 72752           "cannot change %s wal mode from within a transaction",
 72753           (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 72754       );
 72755       break;
 72756     }else{
 72758       if( eOld==PAGER_JOURNALMODE_WAL ){
 72759         /* If leaving WAL mode, close the log file. If successful, the call
 72760         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log 
 72761         ** file. An EXCLUSIVE lock may still be held on the database file 
 72762         ** after a successful return. 
 72763         */
 72764         rc = sqlite3PagerCloseWal(pPager);
 72765         if( rc==SQLITE_OK ){
 72766           sqlite3PagerSetJournalMode(pPager, eNew);
 72768       }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
 72769         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 72770         ** as an intermediate */
 72771         sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
 72774       /* Open a transaction on the database file. Regardless of the journal
 72775       ** mode, this transaction always uses a rollback journal.
 72776       */
 72777       assert( sqlite3BtreeIsInTrans(pBt)==0 );
 72778       if( rc==SQLITE_OK ){
 72779         rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 72783 #endif /* ifndef SQLITE_OMIT_WAL */
 72785   if( rc ){
 72786     eNew = eOld;
 72788   eNew = sqlite3PagerSetJournalMode(pPager, eNew);
 72790   pOut = &aMem[pOp->p2];
 72791   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 72792   pOut->z = (char *)sqlite3JournalModename(eNew);
 72793   pOut->n = sqlite3Strlen30(pOut->z);
 72794   pOut->enc = SQLITE_UTF8;
 72795   sqlite3VdbeChangeEncoding(pOut, encoding);
 72796   break;
 72797 };
 72798 #endif /* SQLITE_OMIT_PRAGMA */
 72800 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 72801 /* Opcode: Vacuum * * * * *
 72802 **
 72803 ** Vacuum the entire database.  This opcode will cause other virtual
 72804 ** machines to be created and run.  It may not be called from within
 72805 ** a transaction.
 72806 */
 72807 case OP_Vacuum: {
 72808   assert( p->readOnly==0 );
 72809   rc = sqlite3RunVacuum(&p->zErrMsg, db);
 72810   break;
 72812 #endif
 72814 #if !defined(SQLITE_OMIT_AUTOVACUUM)
 72815 /* Opcode: IncrVacuum P1 P2 * * *
 72816 **
 72817 ** Perform a single step of the incremental vacuum procedure on
 72818 ** the P1 database. If the vacuum has finished, jump to instruction
 72819 ** P2. Otherwise, fall through to the next instruction.
 72820 */
 72821 case OP_IncrVacuum: {        /* jump */
 72822   Btree *pBt;
 72824   assert( pOp->p1>=0 && pOp->p1<db->nDb );
 72825   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 72826   assert( p->readOnly==0 );
 72827   pBt = db->aDb[pOp->p1].pBt;
 72828   rc = sqlite3BtreeIncrVacuum(pBt);
 72829   VdbeBranchTaken(rc==SQLITE_DONE,2);
 72830   if( rc==SQLITE_DONE ){
 72831     pc = pOp->p2 - 1;
 72832     rc = SQLITE_OK;
 72834   break;
 72836 #endif
 72838 /* Opcode: Expire P1 * * * *
 72839 **
 72840 ** Cause precompiled statements to become expired. An expired statement
 72841 ** fails with an error code of SQLITE_SCHEMA if it is ever executed 
 72842 ** (via sqlite3_step()).
 72843 ** 
 72844 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
 72845 ** then only the currently executing statement is affected. 
 72846 */
 72847 case OP_Expire: {
 72848   if( !pOp->p1 ){
 72849     sqlite3ExpirePreparedStatements(db);
 72850   }else{
 72851     p->expired = 1;
 72853   break;
 72856 #ifndef SQLITE_OMIT_SHARED_CACHE
 72857 /* Opcode: TableLock P1 P2 P3 P4 *
 72858 ** Synopsis: iDb=P1 root=P2 write=P3
 72859 **
 72860 ** Obtain a lock on a particular table. This instruction is only used when
 72861 ** the shared-cache feature is enabled. 
 72862 **
 72863 ** P1 is the index of the database in sqlite3.aDb[] of the database
 72864 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
 72865 ** a write lock if P3==1.
 72866 **
 72867 ** P2 contains the root-page of the table to lock.
 72868 **
 72869 ** P4 contains a pointer to the name of the table being locked. This is only
 72870 ** used to generate an error message if the lock cannot be obtained.
 72871 */
 72872 case OP_TableLock: {
 72873   u8 isWriteLock = (u8)pOp->p3;
 72874   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
 72875     int p1 = pOp->p1; 
 72876     assert( p1>=0 && p1<db->nDb );
 72877     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
 72878     assert( isWriteLock==0 || isWriteLock==1 );
 72879     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
 72880     if( (rc&0xFF)==SQLITE_LOCKED ){
 72881       const char *z = pOp->p4.z;
 72882       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
 72885   break;
 72887 #endif /* SQLITE_OMIT_SHARED_CACHE */
 72889 #ifndef SQLITE_OMIT_VIRTUALTABLE
 72890 /* Opcode: VBegin * * * P4 *
 72891 **
 72892 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
 72893 ** xBegin method for that table.
 72894 **
 72895 ** Also, whether or not P4 is set, check that this is not being called from
 72896 ** within a callback to a virtual table xSync() method. If it is, the error
 72897 ** code will be set to SQLITE_LOCKED.
 72898 */
 72899 case OP_VBegin: {
 72900   VTable *pVTab;
 72901   pVTab = pOp->p4.pVtab;
 72902   rc = sqlite3VtabBegin(db, pVTab);
 72903   if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
 72904   break;
 72906 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 72908 #ifndef SQLITE_OMIT_VIRTUALTABLE
 72909 /* Opcode: VCreate P1 * * P4 *
 72910 **
 72911 ** P4 is the name of a virtual table in database P1. Call the xCreate method
 72912 ** for that table.
 72913 */
 72914 case OP_VCreate: {
 72915   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
 72916   break;
 72918 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 72920 #ifndef SQLITE_OMIT_VIRTUALTABLE
 72921 /* Opcode: VDestroy P1 * * P4 *
 72922 **
 72923 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
 72924 ** of that table.
 72925 */
 72926 case OP_VDestroy: {
 72927   p->inVtabMethod = 2;
 72928   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
 72929   p->inVtabMethod = 0;
 72930   break;
 72932 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 72934 #ifndef SQLITE_OMIT_VIRTUALTABLE
 72935 /* Opcode: VOpen P1 * * P4 *
 72936 **
 72937 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 72938 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
 72939 ** table and stores that cursor in P1.
 72940 */
 72941 case OP_VOpen: {
 72942   VdbeCursor *pCur;
 72943   sqlite3_vtab_cursor *pVtabCursor;
 72944   sqlite3_vtab *pVtab;
 72945   sqlite3_module *pModule;
 72947   assert( p->bIsReader );
 72948   pCur = 0;
 72949   pVtabCursor = 0;
 72950   pVtab = pOp->p4.pVtab->pVtab;
 72951   pModule = (sqlite3_module *)pVtab->pModule;
 72952   assert(pVtab && pModule);
 72953   rc = pModule->xOpen(pVtab, &pVtabCursor);
 72954   sqlite3VtabImportErrmsg(p, pVtab);
 72955   if( SQLITE_OK==rc ){
 72956     /* Initialize sqlite3_vtab_cursor base class */
 72957     pVtabCursor->pVtab = pVtab;
 72959     /* Initialize vdbe cursor object */
 72960     pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 72961     if( pCur ){
 72962       pCur->pVtabCursor = pVtabCursor;
 72963     }else{
 72964       db->mallocFailed = 1;
 72965       pModule->xClose(pVtabCursor);
 72968   break;
 72970 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 72972 #ifndef SQLITE_OMIT_VIRTUALTABLE
 72973 /* Opcode: VFilter P1 P2 P3 P4 *
 72974 ** Synopsis: iPlan=r[P3] zPlan='P4'
 72975 **
 72976 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
 72977 ** the filtered result set is empty.
 72978 **
 72979 ** P4 is either NULL or a string that was generated by the xBestIndex
 72980 ** method of the module.  The interpretation of the P4 string is left
 72981 ** to the module implementation.
 72982 **
 72983 ** This opcode invokes the xFilter method on the virtual table specified
 72984 ** by P1.  The integer query plan parameter to xFilter is stored in register
 72985 ** P3. Register P3+1 stores the argc parameter to be passed to the
 72986 ** xFilter method. Registers P3+2..P3+1+argc are the argc
 72987 ** additional parameters which are passed to
 72988 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
 72989 **
 72990 ** A jump is made to P2 if the result set after filtering would be empty.
 72991 */
 72992 case OP_VFilter: {   /* jump */
 72993   int nArg;
 72994   int iQuery;
 72995   const sqlite3_module *pModule;
 72996   Mem *pQuery;
 72997   Mem *pArgc;
 72998   sqlite3_vtab_cursor *pVtabCursor;
 72999   sqlite3_vtab *pVtab;
 73000   VdbeCursor *pCur;
 73001   int res;
 73002   int i;
 73003   Mem **apArg;
 73005   pQuery = &aMem[pOp->p3];
 73006   pArgc = &pQuery[1];
 73007   pCur = p->apCsr[pOp->p1];
 73008   assert( memIsValid(pQuery) );
 73009   REGISTER_TRACE(pOp->p3, pQuery);
 73010   assert( pCur->pVtabCursor );
 73011   pVtabCursor = pCur->pVtabCursor;
 73012   pVtab = pVtabCursor->pVtab;
 73013   pModule = pVtab->pModule;
 73015   /* Grab the index number and argc parameters */
 73016   assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
 73017   nArg = (int)pArgc->u.i;
 73018   iQuery = (int)pQuery->u.i;
 73020   /* Invoke the xFilter method */
 73022     res = 0;
 73023     apArg = p->apArg;
 73024     for(i = 0; i<nArg; i++){
 73025       apArg[i] = &pArgc[i+1];
 73028     p->inVtabMethod = 1;
 73029     rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
 73030     p->inVtabMethod = 0;
 73031     sqlite3VtabImportErrmsg(p, pVtab);
 73032     if( rc==SQLITE_OK ){
 73033       res = pModule->xEof(pVtabCursor);
 73035     VdbeBranchTaken(res!=0,2);
 73036     if( res ){
 73037       pc = pOp->p2 - 1;
 73040   pCur->nullRow = 0;
 73042   break;
 73044 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 73046 #ifndef SQLITE_OMIT_VIRTUALTABLE
 73047 /* Opcode: VColumn P1 P2 P3 * *
 73048 ** Synopsis: r[P3]=vcolumn(P2)
 73049 **
 73050 ** Store the value of the P2-th column of
 73051 ** the row of the virtual-table that the 
 73052 ** P1 cursor is pointing to into register P3.
 73053 */
 73054 case OP_VColumn: {
 73055   sqlite3_vtab *pVtab;
 73056   const sqlite3_module *pModule;
 73057   Mem *pDest;
 73058   sqlite3_context sContext;
 73060   VdbeCursor *pCur = p->apCsr[pOp->p1];
 73061   assert( pCur->pVtabCursor );
 73062   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
 73063   pDest = &aMem[pOp->p3];
 73064   memAboutToChange(p, pDest);
 73065   if( pCur->nullRow ){
 73066     sqlite3VdbeMemSetNull(pDest);
 73067     break;
 73069   pVtab = pCur->pVtabCursor->pVtab;
 73070   pModule = pVtab->pModule;
 73071   assert( pModule->xColumn );
 73072   memset(&sContext, 0, sizeof(sContext));
 73074   /* The output cell may already have a buffer allocated. Move
 73075   ** the current contents to sContext.s so in case the user-function 
 73076   ** can use the already allocated buffer instead of allocating a 
 73077   ** new one.
 73078   */
 73079   sqlite3VdbeMemMove(&sContext.s, pDest);
 73080   MemSetTypeFlag(&sContext.s, MEM_Null);
 73082   rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
 73083   sqlite3VtabImportErrmsg(p, pVtab);
 73084   if( sContext.isError ){
 73085     rc = sContext.isError;
 73088   /* Copy the result of the function to the P3 register. We
 73089   ** do this regardless of whether or not an error occurred to ensure any
 73090   ** dynamic allocation in sContext.s (a Mem struct) is  released.
 73091   */
 73092   sqlite3VdbeChangeEncoding(&sContext.s, encoding);
 73093   sqlite3VdbeMemMove(pDest, &sContext.s);
 73094   REGISTER_TRACE(pOp->p3, pDest);
 73095   UPDATE_MAX_BLOBSIZE(pDest);
 73097   if( sqlite3VdbeMemTooBig(pDest) ){
 73098     goto too_big;
 73100   break;
 73102 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 73104 #ifndef SQLITE_OMIT_VIRTUALTABLE
 73105 /* Opcode: VNext P1 P2 * * *
 73106 **
 73107 ** Advance virtual table P1 to the next row in its result set and
 73108 ** jump to instruction P2.  Or, if the virtual table has reached
 73109 ** the end of its result set, then fall through to the next instruction.
 73110 */
 73111 case OP_VNext: {   /* jump */
 73112   sqlite3_vtab *pVtab;
 73113   const sqlite3_module *pModule;
 73114   int res;
 73115   VdbeCursor *pCur;
 73117   res = 0;
 73118   pCur = p->apCsr[pOp->p1];
 73119   assert( pCur->pVtabCursor );
 73120   if( pCur->nullRow ){
 73121     break;
 73123   pVtab = pCur->pVtabCursor->pVtab;
 73124   pModule = pVtab->pModule;
 73125   assert( pModule->xNext );
 73127   /* Invoke the xNext() method of the module. There is no way for the
 73128   ** underlying implementation to return an error if one occurs during
 73129   ** xNext(). Instead, if an error occurs, true is returned (indicating that 
 73130   ** data is available) and the error code returned when xColumn or
 73131   ** some other method is next invoked on the save virtual table cursor.
 73132   */
 73133   p->inVtabMethod = 1;
 73134   rc = pModule->xNext(pCur->pVtabCursor);
 73135   p->inVtabMethod = 0;
 73136   sqlite3VtabImportErrmsg(p, pVtab);
 73137   if( rc==SQLITE_OK ){
 73138     res = pModule->xEof(pCur->pVtabCursor);
 73140   VdbeBranchTaken(!res,2);
 73141   if( !res ){
 73142     /* If there is data, jump to P2 */
 73143     pc = pOp->p2 - 1;
 73145   goto check_for_interrupt;
 73147 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 73149 #ifndef SQLITE_OMIT_VIRTUALTABLE
 73150 /* Opcode: VRename P1 * * P4 *
 73151 **
 73152 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 73153 ** This opcode invokes the corresponding xRename method. The value
 73154 ** in register P1 is passed as the zName argument to the xRename method.
 73155 */
 73156 case OP_VRename: {
 73157   sqlite3_vtab *pVtab;
 73158   Mem *pName;
 73160   pVtab = pOp->p4.pVtab->pVtab;
 73161   pName = &aMem[pOp->p1];
 73162   assert( pVtab->pModule->xRename );
 73163   assert( memIsValid(pName) );
 73164   assert( p->readOnly==0 );
 73165   REGISTER_TRACE(pOp->p1, pName);
 73166   assert( pName->flags & MEM_Str );
 73167   testcase( pName->enc==SQLITE_UTF8 );
 73168   testcase( pName->enc==SQLITE_UTF16BE );
 73169   testcase( pName->enc==SQLITE_UTF16LE );
 73170   rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
 73171   if( rc==SQLITE_OK ){
 73172     rc = pVtab->pModule->xRename(pVtab, pName->z);
 73173     sqlite3VtabImportErrmsg(p, pVtab);
 73174     p->expired = 0;
 73176   break;
 73178 #endif
 73180 #ifndef SQLITE_OMIT_VIRTUALTABLE
 73181 /* Opcode: VUpdate P1 P2 P3 P4 P5
 73182 ** Synopsis: data=r[P3@P2]
 73183 **
 73184 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 73185 ** This opcode invokes the corresponding xUpdate method. P2 values
 73186 ** are contiguous memory cells starting at P3 to pass to the xUpdate 
 73187 ** invocation. The value in register (P3+P2-1) corresponds to the 
 73188 ** p2th element of the argv array passed to xUpdate.
 73189 **
 73190 ** The xUpdate method will do a DELETE or an INSERT or both.
 73191 ** The argv[0] element (which corresponds to memory cell P3)
 73192 ** is the rowid of a row to delete.  If argv[0] is NULL then no 
 73193 ** deletion occurs.  The argv[1] element is the rowid of the new 
 73194 ** row.  This can be NULL to have the virtual table select the new 
 73195 ** rowid for itself.  The subsequent elements in the array are 
 73196 ** the values of columns in the new row.
 73197 **
 73198 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
 73199 ** a row to delete.
 73200 **
 73201 ** P1 is a boolean flag. If it is set to true and the xUpdate call
 73202 ** is successful, then the value returned by sqlite3_last_insert_rowid() 
 73203 ** is set to the value of the rowid for the row just inserted.
 73204 **
 73205 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
 73206 ** apply in the case of a constraint failure on an insert or update.
 73207 */
 73208 case OP_VUpdate: {
 73209   sqlite3_vtab *pVtab;
 73210   sqlite3_module *pModule;
 73211   int nArg;
 73212   int i;
 73213   sqlite_int64 rowid;
 73214   Mem **apArg;
 73215   Mem *pX;
 73217   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
 73218        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
 73219   );
 73220   assert( p->readOnly==0 );
 73221   pVtab = pOp->p4.pVtab->pVtab;
 73222   pModule = (sqlite3_module *)pVtab->pModule;
 73223   nArg = pOp->p2;
 73224   assert( pOp->p4type==P4_VTAB );
 73225   if( ALWAYS(pModule->xUpdate) ){
 73226     u8 vtabOnConflict = db->vtabOnConflict;
 73227     apArg = p->apArg;
 73228     pX = &aMem[pOp->p3];
 73229     for(i=0; i<nArg; i++){
 73230       assert( memIsValid(pX) );
 73231       memAboutToChange(p, pX);
 73232       apArg[i] = pX;
 73233       pX++;
 73235     db->vtabOnConflict = pOp->p5;
 73236     rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
 73237     db->vtabOnConflict = vtabOnConflict;
 73238     sqlite3VtabImportErrmsg(p, pVtab);
 73239     if( rc==SQLITE_OK && pOp->p1 ){
 73240       assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
 73241       db->lastRowid = lastRowid = rowid;
 73243     if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
 73244       if( pOp->p5==OE_Ignore ){
 73245         rc = SQLITE_OK;
 73246       }else{
 73247         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
 73249     }else{
 73250       p->nChange++;
 73253   break;
 73255 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 73257 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 73258 /* Opcode: Pagecount P1 P2 * * *
 73259 **
 73260 ** Write the current number of pages in database P1 to memory cell P2.
 73261 */
 73262 case OP_Pagecount: {            /* out2-prerelease */
 73263   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
 73264   break;
 73266 #endif
 73269 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
 73270 /* Opcode: MaxPgcnt P1 P2 P3 * *
 73271 **
 73272 ** Try to set the maximum page count for database P1 to the value in P3.
 73273 ** Do not let the maximum page count fall below the current page count and
 73274 ** do not change the maximum page count value if P3==0.
 73275 **
 73276 ** Store the maximum page count after the change in register P2.
 73277 */
 73278 case OP_MaxPgcnt: {            /* out2-prerelease */
 73279   unsigned int newMax;
 73280   Btree *pBt;
 73282   pBt = db->aDb[pOp->p1].pBt;
 73283   newMax = 0;
 73284   if( pOp->p3 ){
 73285     newMax = sqlite3BtreeLastPage(pBt);
 73286     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
 73288   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
 73289   break;
 73291 #endif
 73294 /* Opcode: Init * P2 * P4 *
 73295 ** Synopsis:  Start at P2
 73296 **
 73297 ** Programs contain a single instance of this opcode as the very first
 73298 ** opcode.
 73299 **
 73300 ** If tracing is enabled (by the sqlite3_trace()) interface, then
 73301 ** the UTF-8 string contained in P4 is emitted on the trace callback.
 73302 ** Or if P4 is blank, use the string returned by sqlite3_sql().
 73303 **
 73304 ** If P2 is not zero, jump to instruction P2.
 73305 */
 73306 case OP_Init: {          /* jump */
 73307   char *zTrace;
 73308   char *z;
 73310   if( pOp->p2 ){
 73311     pc = pOp->p2 - 1;
 73313 #ifndef SQLITE_OMIT_TRACE
 73314   if( db->xTrace
 73315    && !p->doingRerun
 73316    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 73317   ){
 73318     z = sqlite3VdbeExpandSql(p, zTrace);
 73319     db->xTrace(db->pTraceArg, z);
 73320     sqlite3DbFree(db, z);
 73322 #ifdef SQLITE_USE_FCNTL_TRACE
 73323   zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 73324   if( zTrace ){
 73325     int i;
 73326     for(i=0; i<db->nDb; i++){
 73327       if( MASKBIT(i) & p->btreeMask)==0 ) continue;
 73328       sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
 73331 #endif /* SQLITE_USE_FCNTL_TRACE */
 73332 #ifdef SQLITE_DEBUG
 73333   if( (db->flags & SQLITE_SqlTrace)!=0
 73334    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
 73335   ){
 73336     sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
 73338 #endif /* SQLITE_DEBUG */
 73339 #endif /* SQLITE_OMIT_TRACE */
 73340   break;
 73344 /* Opcode: Noop * * * * *
 73345 **
 73346 ** Do nothing.  This instruction is often useful as a jump
 73347 ** destination.
 73348 */
 73349 /*
 73350 ** The magic Explain opcode are only inserted when explain==2 (which
 73351 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
 73352 ** This opcode records information from the optimizer.  It is the
 73353 ** the same as a no-op.  This opcodesnever appears in a real VM program.
 73354 */
 73355 default: {          /* This is really OP_Noop and OP_Explain */
 73356   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
 73357   break;
 73360 /*****************************************************************************
 73361 ** The cases of the switch statement above this line should all be indented
 73362 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
 73363 ** readability.  From this point on down, the normal indentation rules are
 73364 ** restored.
 73365 *****************************************************************************/
 73368 #ifdef VDBE_PROFILE
 73370       u64 elapsed = sqlite3Hwtime() - start;
 73371       pOp->cycles += elapsed;
 73372       pOp->cnt++;
 73374 #endif
 73376     /* The following code adds nothing to the actual functionality
 73377     ** of the program.  It is only here for testing and debugging.
 73378     ** On the other hand, it does burn CPU cycles every time through
 73379     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
 73380     */
 73381 #ifndef NDEBUG
 73382     assert( pc>=-1 && pc<p->nOp );
 73384 #ifdef SQLITE_DEBUG
 73385     if( db->flags & SQLITE_VdbeTrace ){
 73386       if( rc!=0 ) printf("rc=%d\n",rc);
 73387       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
 73388         registerTrace(pOp->p2, &aMem[pOp->p2]);
 73390       if( pOp->opflags & OPFLG_OUT3 ){
 73391         registerTrace(pOp->p3, &aMem[pOp->p3]);
 73394 #endif  /* SQLITE_DEBUG */
 73395 #endif  /* NDEBUG */
 73396   }  /* The end of the for(;;) loop the loops through opcodes */
 73398   /* If we reach this point, it means that execution is finished with
 73399   ** an error of some kind.
 73400   */
 73401 vdbe_error_halt:
 73402   assert( rc );
 73403   p->rc = rc;
 73404   testcase( sqlite3GlobalConfig.xLog!=0 );
 73405   sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
 73406                    pc, p->zSql, p->zErrMsg);
 73407   sqlite3VdbeHalt(p);
 73408   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
 73409   rc = SQLITE_ERROR;
 73410   if( resetSchemaOnFault>0 ){
 73411     sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
 73414   /* This is the only way out of this procedure.  We have to
 73415   ** release the mutexes on btrees that were acquired at the
 73416   ** top. */
 73417 vdbe_return:
 73418   db->lastRowid = lastRowid;
 73419   testcase( nVmStep>0 );
 73420   p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
 73421   sqlite3VdbeLeave(p);
 73422   return rc;
 73424   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
 73425   ** is encountered.
 73426   */
 73427 too_big:
 73428   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
 73429   rc = SQLITE_TOOBIG;
 73430   goto vdbe_error_halt;
 73432   /* Jump to here if a malloc() fails.
 73433   */
 73434 no_mem:
 73435   db->mallocFailed = 1;
 73436   sqlite3SetString(&p->zErrMsg, db, "out of memory");
 73437   rc = SQLITE_NOMEM;
 73438   goto vdbe_error_halt;
 73440   /* Jump to here for any other kind of fatal error.  The "rc" variable
 73441   ** should hold the error number.
 73442   */
 73443 abort_due_to_error:
 73444   assert( p->zErrMsg==0 );
 73445   if( db->mallocFailed ) rc = SQLITE_NOMEM;
 73446   if( rc!=SQLITE_IOERR_NOMEM ){
 73447     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 73449   goto vdbe_error_halt;
 73451   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
 73452   ** flag.
 73453   */
 73454 abort_due_to_interrupt:
 73455   assert( db->u1.isInterrupted );
 73456   rc = SQLITE_INTERRUPT;
 73457   p->rc = rc;
 73458   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
 73459   goto vdbe_error_halt;
 73463 /************** End of vdbe.c ************************************************/
 73464 /************** Begin file vdbeblob.c ****************************************/
 73465 /*
 73466 ** 2007 May 1
 73467 **
 73468 ** The author disclaims copyright to this source code.  In place of
 73469 ** a legal notice, here is a blessing:
 73470 **
 73471 **    May you do good and not evil.
 73472 **    May you find forgiveness for yourself and forgive others.
 73473 **    May you share freely, never taking more than you give.
 73474 **
 73475 *************************************************************************
 73476 **
 73477 ** This file contains code used to implement incremental BLOB I/O.
 73478 */
 73481 #ifndef SQLITE_OMIT_INCRBLOB
 73483 /*
 73484 ** Valid sqlite3_blob* handles point to Incrblob structures.
 73485 */
 73486 typedef struct Incrblob Incrblob;
 73487 struct Incrblob {
 73488   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
 73489   int nByte;              /* Size of open blob, in bytes */
 73490   int iOffset;            /* Byte offset of blob in cursor data */
 73491   int iCol;               /* Table column this handle is open on */
 73492   BtCursor *pCsr;         /* Cursor pointing at blob row */
 73493   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
 73494   sqlite3 *db;            /* The associated database */
 73495 };
 73498 /*
 73499 ** This function is used by both blob_open() and blob_reopen(). It seeks
 73500 ** the b-tree cursor associated with blob handle p to point to row iRow.
 73501 ** If successful, SQLITE_OK is returned and subsequent calls to
 73502 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
 73503 **
 73504 ** If an error occurs, or if the specified row does not exist or does not
 73505 ** contain a value of type TEXT or BLOB in the column nominated when the
 73506 ** blob handle was opened, then an error code is returned and *pzErr may
 73507 ** be set to point to a buffer containing an error message. It is the
 73508 ** responsibility of the caller to free the error message buffer using
 73509 ** sqlite3DbFree().
 73510 **
 73511 ** If an error does occur, then the b-tree cursor is closed. All subsequent
 73512 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will 
 73513 ** immediately return SQLITE_ABORT.
 73514 */
 73515 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
 73516   int rc;                         /* Error code */
 73517   char *zErr = 0;                 /* Error message */
 73518   Vdbe *v = (Vdbe *)p->pStmt;
 73520   /* Set the value of the SQL statements only variable to integer iRow. 
 73521   ** This is done directly instead of using sqlite3_bind_int64() to avoid 
 73522   ** triggering asserts related to mutexes.
 73523   */
 73524   assert( v->aVar[0].flags&MEM_Int );
 73525   v->aVar[0].u.i = iRow;
 73527   rc = sqlite3_step(p->pStmt);
 73528   if( rc==SQLITE_ROW ){
 73529     VdbeCursor *pC = v->apCsr[0];
 73530     u32 type = pC->aType[p->iCol];
 73531     if( type<12 ){
 73532       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
 73533           type==0?"null": type==7?"real": "integer"
 73534       );
 73535       rc = SQLITE_ERROR;
 73536       sqlite3_finalize(p->pStmt);
 73537       p->pStmt = 0;
 73538     }else{
 73539       p->iOffset = pC->aType[p->iCol + pC->nField];
 73540       p->nByte = sqlite3VdbeSerialTypeLen(type);
 73541       p->pCsr =  pC->pCursor;
 73542       sqlite3BtreeEnterCursor(p->pCsr);
 73543       sqlite3BtreeCacheOverflow(p->pCsr);
 73544       sqlite3BtreeLeaveCursor(p->pCsr);
 73548   if( rc==SQLITE_ROW ){
 73549     rc = SQLITE_OK;
 73550   }else if( p->pStmt ){
 73551     rc = sqlite3_finalize(p->pStmt);
 73552     p->pStmt = 0;
 73553     if( rc==SQLITE_OK ){
 73554       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
 73555       rc = SQLITE_ERROR;
 73556     }else{
 73557       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
 73561   assert( rc!=SQLITE_OK || zErr==0 );
 73562   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
 73564   *pzErr = zErr;
 73565   return rc;
 73568 /*
 73569 ** Open a blob handle.
 73570 */
 73571 SQLITE_API int sqlite3_blob_open(
 73572   sqlite3* db,            /* The database connection */
 73573   const char *zDb,        /* The attached database containing the blob */
 73574   const char *zTable,     /* The table containing the blob */
 73575   const char *zColumn,    /* The column containing the blob */
 73576   sqlite_int64 iRow,      /* The row containing the glob */
 73577   int flags,              /* True -> read/write access, false -> read-only */
 73578   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
 73579 ){
 73580   int nAttempt = 0;
 73581   int iCol;               /* Index of zColumn in row-record */
 73583   /* This VDBE program seeks a btree cursor to the identified 
 73584   ** db/table/row entry. The reason for using a vdbe program instead
 73585   ** of writing code to use the b-tree layer directly is that the
 73586   ** vdbe program will take advantage of the various transaction,
 73587   ** locking and error handling infrastructure built into the vdbe.
 73588   **
 73589   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
 73590   ** Code external to the Vdbe then "borrows" the b-tree cursor and
 73591   ** uses it to implement the blob_read(), blob_write() and 
 73592   ** blob_bytes() functions.
 73593   **
 73594   ** The sqlite3_blob_close() function finalizes the vdbe program,
 73595   ** which closes the b-tree cursor and (possibly) commits the 
 73596   ** transaction.
 73597   */
 73598   static const int iLn = VDBE_OFFSET_LINENO(4);
 73599   static const VdbeOpList openBlob[] = {
 73600     /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
 73601     {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
 73602     /* One of the following two instructions is replaced by an OP_Noop. */
 73603     {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
 73604     {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
 73605     {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
 73606     {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
 73607     {OP_Column, 0, 0, 1},          /* 6  */
 73608     {OP_ResultRow, 1, 0, 0},       /* 7  */
 73609     {OP_Goto, 0, 4, 0},            /* 8  */
 73610     {OP_Close, 0, 0, 0},           /* 9  */
 73611     {OP_Halt, 0, 0, 0},            /* 10 */
 73612   };
 73614   int rc = SQLITE_OK;
 73615   char *zErr = 0;
 73616   Table *pTab;
 73617   Parse *pParse = 0;
 73618   Incrblob *pBlob = 0;
 73620   flags = !!flags;                /* flags = (flags ? 1 : 0); */
 73621   *ppBlob = 0;
 73623   sqlite3_mutex_enter(db->mutex);
 73625   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
 73626   if( !pBlob ) goto blob_open_out;
 73627   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
 73628   if( !pParse ) goto blob_open_out;
 73630   do {
 73631     memset(pParse, 0, sizeof(Parse));
 73632     pParse->db = db;
 73633     sqlite3DbFree(db, zErr);
 73634     zErr = 0;
 73636     sqlite3BtreeEnterAll(db);
 73637     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
 73638     if( pTab && IsVirtual(pTab) ){
 73639       pTab = 0;
 73640       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
 73642     if( pTab && !HasRowid(pTab) ){
 73643       pTab = 0;
 73644       sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
 73646 #ifndef SQLITE_OMIT_VIEW
 73647     if( pTab && pTab->pSelect ){
 73648       pTab = 0;
 73649       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
 73651 #endif
 73652     if( !pTab ){
 73653       if( pParse->zErrMsg ){
 73654         sqlite3DbFree(db, zErr);
 73655         zErr = pParse->zErrMsg;
 73656         pParse->zErrMsg = 0;
 73658       rc = SQLITE_ERROR;
 73659       sqlite3BtreeLeaveAll(db);
 73660       goto blob_open_out;
 73663     /* Now search pTab for the exact column. */
 73664     for(iCol=0; iCol<pTab->nCol; iCol++) {
 73665       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
 73666         break;
 73669     if( iCol==pTab->nCol ){
 73670       sqlite3DbFree(db, zErr);
 73671       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
 73672       rc = SQLITE_ERROR;
 73673       sqlite3BtreeLeaveAll(db);
 73674       goto blob_open_out;
 73677     /* If the value is being opened for writing, check that the
 73678     ** column is not indexed, and that it is not part of a foreign key. 
 73679     ** It is against the rules to open a column to which either of these
 73680     ** descriptions applies for writing.  */
 73681     if( flags ){
 73682       const char *zFault = 0;
 73683       Index *pIdx;
 73684 #ifndef SQLITE_OMIT_FOREIGN_KEY
 73685       if( db->flags&SQLITE_ForeignKeys ){
 73686         /* Check that the column is not part of an FK child key definition. It
 73687         ** is not necessary to check if it is part of a parent key, as parent
 73688         ** key columns must be indexed. The check below will pick up this 
 73689         ** case.  */
 73690         FKey *pFKey;
 73691         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 73692           int j;
 73693           for(j=0; j<pFKey->nCol; j++){
 73694             if( pFKey->aCol[j].iFrom==iCol ){
 73695               zFault = "foreign key";
 73700 #endif
 73701       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 73702         int j;
 73703         for(j=0; j<pIdx->nKeyCol; j++){
 73704           if( pIdx->aiColumn[j]==iCol ){
 73705             zFault = "indexed";
 73709       if( zFault ){
 73710         sqlite3DbFree(db, zErr);
 73711         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
 73712         rc = SQLITE_ERROR;
 73713         sqlite3BtreeLeaveAll(db);
 73714         goto blob_open_out;
 73718     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
 73719     assert( pBlob->pStmt || db->mallocFailed );
 73720     if( pBlob->pStmt ){
 73721       Vdbe *v = (Vdbe *)pBlob->pStmt;
 73722       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 73725       sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
 73726                            pTab->pSchema->schema_cookie,
 73727                            pTab->pSchema->iGeneration);
 73728       sqlite3VdbeChangeP5(v, 1);     
 73729       sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
 73731       /* Make sure a mutex is held on the table to be accessed */
 73732       sqlite3VdbeUsesBtree(v, iDb); 
 73734       /* Configure the OP_TableLock instruction */
 73735 #ifdef SQLITE_OMIT_SHARED_CACHE
 73736       sqlite3VdbeChangeToNoop(v, 1);
 73737 #else
 73738       sqlite3VdbeChangeP1(v, 1, iDb);
 73739       sqlite3VdbeChangeP2(v, 1, pTab->tnum);
 73740       sqlite3VdbeChangeP3(v, 1, flags);
 73741       sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
 73742 #endif
 73744       /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
 73745       ** parameter of the other to pTab->tnum.  */
 73746       sqlite3VdbeChangeToNoop(v, 3 - flags);
 73747       sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
 73748       sqlite3VdbeChangeP3(v, 2 + flags, iDb);
 73750       /* Configure the number of columns. Configure the cursor to
 73751       ** think that the table has one more column than it really
 73752       ** does. An OP_Column to retrieve this imaginary column will
 73753       ** always return an SQL NULL. This is useful because it means
 73754       ** we can invoke OP_Column to fill in the vdbe cursors type 
 73755       ** and offset cache without causing any IO.
 73756       */
 73757       sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
 73758       sqlite3VdbeChangeP2(v, 6, pTab->nCol);
 73759       if( !db->mallocFailed ){
 73760         pParse->nVar = 1;
 73761         pParse->nMem = 1;
 73762         pParse->nTab = 1;
 73763         sqlite3VdbeMakeReady(v, pParse);
 73767     pBlob->flags = flags;
 73768     pBlob->iCol = iCol;
 73769     pBlob->db = db;
 73770     sqlite3BtreeLeaveAll(db);
 73771     if( db->mallocFailed ){
 73772       goto blob_open_out;
 73774     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
 73775     rc = blobSeekToRow(pBlob, iRow, &zErr);
 73776   } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
 73778 blob_open_out:
 73779   if( rc==SQLITE_OK && db->mallocFailed==0 ){
 73780     *ppBlob = (sqlite3_blob *)pBlob;
 73781   }else{
 73782     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
 73783     sqlite3DbFree(db, pBlob);
 73785   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 73786   sqlite3DbFree(db, zErr);
 73787   sqlite3ParserReset(pParse);
 73788   sqlite3StackFree(db, pParse);
 73789   rc = sqlite3ApiExit(db, rc);
 73790   sqlite3_mutex_leave(db->mutex);
 73791   return rc;
 73794 /*
 73795 ** Close a blob handle that was previously created using
 73796 ** sqlite3_blob_open().
 73797 */
 73798 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
 73799   Incrblob *p = (Incrblob *)pBlob;
 73800   int rc;
 73801   sqlite3 *db;
 73803   if( p ){
 73804     db = p->db;
 73805     sqlite3_mutex_enter(db->mutex);
 73806     rc = sqlite3_finalize(p->pStmt);
 73807     sqlite3DbFree(db, p);
 73808     sqlite3_mutex_leave(db->mutex);
 73809   }else{
 73810     rc = SQLITE_OK;
 73812   return rc;
 73815 /*
 73816 ** Perform a read or write operation on a blob
 73817 */
 73818 static int blobReadWrite(
 73819   sqlite3_blob *pBlob, 
 73820   void *z, 
 73821   int n, 
 73822   int iOffset, 
 73823   int (*xCall)(BtCursor*, u32, u32, void*)
 73824 ){
 73825   int rc;
 73826   Incrblob *p = (Incrblob *)pBlob;
 73827   Vdbe *v;
 73828   sqlite3 *db;
 73830   if( p==0 ) return SQLITE_MISUSE_BKPT;
 73831   db = p->db;
 73832   sqlite3_mutex_enter(db->mutex);
 73833   v = (Vdbe*)p->pStmt;
 73835   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
 73836     /* Request is out of range. Return a transient error. */
 73837     rc = SQLITE_ERROR;
 73838     sqlite3Error(db, SQLITE_ERROR, 0);
 73839   }else if( v==0 ){
 73840     /* If there is no statement handle, then the blob-handle has
 73841     ** already been invalidated. Return SQLITE_ABORT in this case.
 73842     */
 73843     rc = SQLITE_ABORT;
 73844   }else{
 73845     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
 73846     ** returned, clean-up the statement handle.
 73847     */
 73848     assert( db == v->db );
 73849     sqlite3BtreeEnterCursor(p->pCsr);
 73850     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
 73851     sqlite3BtreeLeaveCursor(p->pCsr);
 73852     if( rc==SQLITE_ABORT ){
 73853       sqlite3VdbeFinalize(v);
 73854       p->pStmt = 0;
 73855     }else{
 73856       db->errCode = rc;
 73857       v->rc = rc;
 73860   rc = sqlite3ApiExit(db, rc);
 73861   sqlite3_mutex_leave(db->mutex);
 73862   return rc;
 73865 /*
 73866 ** Read data from a blob handle.
 73867 */
 73868 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
 73869   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
 73872 /*
 73873 ** Write data to a blob handle.
 73874 */
 73875 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
 73876   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
 73879 /*
 73880 ** Query a blob handle for the size of the data.
 73881 **
 73882 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
 73883 ** so no mutex is required for access.
 73884 */
 73885 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
 73886   Incrblob *p = (Incrblob *)pBlob;
 73887   return (p && p->pStmt) ? p->nByte : 0;
 73890 /*
 73891 ** Move an existing blob handle to point to a different row of the same
 73892 ** database table.
 73893 **
 73894 ** If an error occurs, or if the specified row does not exist or does not
 73895 ** contain a blob or text value, then an error code is returned and the
 73896 ** database handle error code and message set. If this happens, then all 
 73897 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
 73898 ** immediately return SQLITE_ABORT.
 73899 */
 73900 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
 73901   int rc;
 73902   Incrblob *p = (Incrblob *)pBlob;
 73903   sqlite3 *db;
 73905   if( p==0 ) return SQLITE_MISUSE_BKPT;
 73906   db = p->db;
 73907   sqlite3_mutex_enter(db->mutex);
 73909   if( p->pStmt==0 ){
 73910     /* If there is no statement handle, then the blob-handle has
 73911     ** already been invalidated. Return SQLITE_ABORT in this case.
 73912     */
 73913     rc = SQLITE_ABORT;
 73914   }else{
 73915     char *zErr;
 73916     rc = blobSeekToRow(p, iRow, &zErr);
 73917     if( rc!=SQLITE_OK ){
 73918       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
 73919       sqlite3DbFree(db, zErr);
 73921     assert( rc!=SQLITE_SCHEMA );
 73924   rc = sqlite3ApiExit(db, rc);
 73925   assert( rc==SQLITE_OK || p->pStmt==0 );
 73926   sqlite3_mutex_leave(db->mutex);
 73927   return rc;
 73930 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
 73932 /************** End of vdbeblob.c ********************************************/
 73933 /************** Begin file vdbesort.c ****************************************/
 73934 /*
 73935 ** 2011 July 9
 73936 **
 73937 ** The author disclaims copyright to this source code.  In place of
 73938 ** a legal notice, here is a blessing:
 73939 **
 73940 **    May you do good and not evil.
 73941 **    May you find forgiveness for yourself and forgive others.
 73942 **    May you share freely, never taking more than you give.
 73943 **
 73944 *************************************************************************
 73945 ** This file contains code for the VdbeSorter object, used in concert with
 73946 ** a VdbeCursor to sort large numbers of keys (as may be required, for
 73947 ** example, by CREATE INDEX statements on tables too large to fit in main
 73948 ** memory).
 73949 */
 73953 typedef struct VdbeSorterIter VdbeSorterIter;
 73954 typedef struct SorterRecord SorterRecord;
 73955 typedef struct FileWriter FileWriter;
 73957 /*
 73958 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
 73959 **
 73960 ** As keys are added to the sorter, they are written to disk in a series
 73961 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
 73962 ** the same as the cache-size allowed for temporary databases. In order
 73963 ** to allow the caller to extract keys from the sorter in sorted order,
 73964 ** all PMAs currently stored on disk must be merged together. This comment
 73965 ** describes the data structure used to do so. The structure supports 
 73966 ** merging any number of arrays in a single pass with no redundant comparison 
 73967 ** operations.
 73968 **
 73969 ** The aIter[] array contains an iterator for each of the PMAs being merged.
 73970 ** An aIter[] iterator either points to a valid key or else is at EOF. For 
 73971 ** the purposes of the paragraphs below, we assume that the array is actually 
 73972 ** N elements in size, where N is the smallest power of 2 greater to or equal 
 73973 ** to the number of iterators being merged. The extra aIter[] elements are 
 73974 ** treated as if they are empty (always at EOF).
 73975 **
 73976 ** The aTree[] array is also N elements in size. The value of N is stored in
 73977 ** the VdbeSorter.nTree variable.
 73978 **
 73979 ** The final (N/2) elements of aTree[] contain the results of comparing
 73980 ** pairs of iterator keys together. Element i contains the result of 
 73981 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
 73982 ** aTree element is set to the index of it. 
 73983 **
 73984 ** For the purposes of this comparison, EOF is considered greater than any
 73985 ** other key value. If the keys are equal (only possible with two EOF
 73986 ** values), it doesn't matter which index is stored.
 73987 **
 73988 ** The (N/4) elements of aTree[] that precede the final (N/2) described 
 73989 ** above contains the index of the smallest of each block of 4 iterators.
 73990 ** And so on. So that aTree[1] contains the index of the iterator that 
 73991 ** currently points to the smallest key value. aTree[0] is unused.
 73992 **
 73993 ** Example:
 73994 **
 73995 **     aIter[0] -> Banana
 73996 **     aIter[1] -> Feijoa
 73997 **     aIter[2] -> Elderberry
 73998 **     aIter[3] -> Currant
 73999 **     aIter[4] -> Grapefruit
 74000 **     aIter[5] -> Apple
 74001 **     aIter[6] -> Durian
 74002 **     aIter[7] -> EOF
 74003 **
 74004 **     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
 74005 **
 74006 ** The current element is "Apple" (the value of the key indicated by 
 74007 ** iterator 5). When the Next() operation is invoked, iterator 5 will
 74008 ** be advanced to the next key in its segment. Say the next key is
 74009 ** "Eggplant":
 74010 **
 74011 **     aIter[5] -> Eggplant
 74012 **
 74013 ** The contents of aTree[] are updated first by comparing the new iterator
 74014 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
 74015 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
 74016 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
 74017 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
 74018 ** so the value written into element 1 of the array is 0. As follows:
 74019 **
 74020 **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
 74021 **
 74022 ** In other words, each time we advance to the next sorter element, log2(N)
 74023 ** key comparison operations are required, where N is the number of segments
 74024 ** being merged (rounded up to the next power of 2).
 74025 */
 74026 struct VdbeSorter {
 74027   i64 iWriteOff;                  /* Current write offset within file pTemp1 */
 74028   i64 iReadOff;                   /* Current read offset within file pTemp1 */
 74029   int nInMemory;                  /* Current size of pRecord list as PMA */
 74030   int nTree;                      /* Used size of aTree/aIter (power of 2) */
 74031   int nPMA;                       /* Number of PMAs stored in pTemp1 */
 74032   int mnPmaSize;                  /* Minimum PMA size, in bytes */
 74033   int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
 74034   VdbeSorterIter *aIter;          /* Array of iterators to merge */
 74035   int *aTree;                     /* Current state of incremental merge */
 74036   sqlite3_file *pTemp1;           /* PMA file 1 */
 74037   SorterRecord *pRecord;          /* Head of in-memory record list */
 74038   UnpackedRecord *pUnpacked;      /* Used to unpack keys */
 74039 };
 74041 /*
 74042 ** The following type is an iterator for a PMA. It caches the current key in 
 74043 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
 74044 */
 74045 struct VdbeSorterIter {
 74046   i64 iReadOff;                   /* Current read offset */
 74047   i64 iEof;                       /* 1 byte past EOF for this iterator */
 74048   int nAlloc;                     /* Bytes of space at aAlloc */
 74049   int nKey;                       /* Number of bytes in key */
 74050   sqlite3_file *pFile;            /* File iterator is reading from */
 74051   u8 *aAlloc;                     /* Allocated space */
 74052   u8 *aKey;                       /* Pointer to current key */
 74053   u8 *aBuffer;                    /* Current read buffer */
 74054   int nBuffer;                    /* Size of read buffer in bytes */
 74055 };
 74057 /*
 74058 ** An instance of this structure is used to organize the stream of records
 74059 ** being written to files by the merge-sort code into aligned, page-sized
 74060 ** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
 74061 ** faster on many operating systems.
 74062 */
 74063 struct FileWriter {
 74064   int eFWErr;                     /* Non-zero if in an error state */
 74065   u8 *aBuffer;                    /* Pointer to write buffer */
 74066   int nBuffer;                    /* Size of write buffer in bytes */
 74067   int iBufStart;                  /* First byte of buffer to write */
 74068   int iBufEnd;                    /* Last byte of buffer to write */
 74069   i64 iWriteOff;                  /* Offset of start of buffer in file */
 74070   sqlite3_file *pFile;            /* File to write to */
 74071 };
 74073 /*
 74074 ** A structure to store a single record. All in-memory records are connected
 74075 ** together into a linked list headed at VdbeSorter.pRecord using the 
 74076 ** SorterRecord.pNext pointer.
 74077 */
 74078 struct SorterRecord {
 74079   void *pVal;
 74080   int nVal;
 74081   SorterRecord *pNext;
 74082 };
 74084 /* Minimum allowable value for the VdbeSorter.nWorking variable */
 74085 #define SORTER_MIN_WORKING 10
 74087 /* Maximum number of segments to merge in a single pass. */
 74088 #define SORTER_MAX_MERGE_COUNT 16
 74090 /*
 74091 ** Free all memory belonging to the VdbeSorterIter object passed as the second
 74092 ** argument. All structure fields are set to zero before returning.
 74093 */
 74094 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
 74095   sqlite3DbFree(db, pIter->aAlloc);
 74096   sqlite3DbFree(db, pIter->aBuffer);
 74097   memset(pIter, 0, sizeof(VdbeSorterIter));
 74100 /*
 74101 ** Read nByte bytes of data from the stream of data iterated by object p.
 74102 ** If successful, set *ppOut to point to a buffer containing the data
 74103 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
 74104 ** error code.
 74105 **
 74106 ** The buffer indicated by *ppOut may only be considered valid until the
 74107 ** next call to this function.
 74108 */
 74109 static int vdbeSorterIterRead(
 74110   sqlite3 *db,                    /* Database handle (for malloc) */
 74111   VdbeSorterIter *p,              /* Iterator */
 74112   int nByte,                      /* Bytes of data to read */
 74113   u8 **ppOut                      /* OUT: Pointer to buffer containing data */
 74114 ){
 74115   int iBuf;                       /* Offset within buffer to read from */
 74116   int nAvail;                     /* Bytes of data available in buffer */
 74117   assert( p->aBuffer );
 74119   /* If there is no more data to be read from the buffer, read the next 
 74120   ** p->nBuffer bytes of data from the file into it. Or, if there are less
 74121   ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
 74122   iBuf = p->iReadOff % p->nBuffer;
 74123   if( iBuf==0 ){
 74124     int nRead;                    /* Bytes to read from disk */
 74125     int rc;                       /* sqlite3OsRead() return code */
 74127     /* Determine how many bytes of data to read. */
 74128     if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
 74129       nRead = p->nBuffer;
 74130     }else{
 74131       nRead = (int)(p->iEof - p->iReadOff);
 74133     assert( nRead>0 );
 74135     /* Read data from the file. Return early if an error occurs. */
 74136     rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
 74137     assert( rc!=SQLITE_IOERR_SHORT_READ );
 74138     if( rc!=SQLITE_OK ) return rc;
 74140   nAvail = p->nBuffer - iBuf; 
 74142   if( nByte<=nAvail ){
 74143     /* The requested data is available in the in-memory buffer. In this
 74144     ** case there is no need to make a copy of the data, just return a 
 74145     ** pointer into the buffer to the caller.  */
 74146     *ppOut = &p->aBuffer[iBuf];
 74147     p->iReadOff += nByte;
 74148   }else{
 74149     /* The requested data is not all available in the in-memory buffer.
 74150     ** In this case, allocate space at p->aAlloc[] to copy the requested
 74151     ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
 74152     int nRem;                     /* Bytes remaining to copy */
 74154     /* Extend the p->aAlloc[] allocation if required. */
 74155     if( p->nAlloc<nByte ){
 74156       int nNew = p->nAlloc*2;
 74157       while( nByte>nNew ) nNew = nNew*2;
 74158       p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
 74159       if( !p->aAlloc ) return SQLITE_NOMEM;
 74160       p->nAlloc = nNew;
 74163     /* Copy as much data as is available in the buffer into the start of
 74164     ** p->aAlloc[].  */
 74165     memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
 74166     p->iReadOff += nAvail;
 74167     nRem = nByte - nAvail;
 74169     /* The following loop copies up to p->nBuffer bytes per iteration into
 74170     ** the p->aAlloc[] buffer.  */
 74171     while( nRem>0 ){
 74172       int rc;                     /* vdbeSorterIterRead() return code */
 74173       int nCopy;                  /* Number of bytes to copy */
 74174       u8 *aNext;                  /* Pointer to buffer to copy data from */
 74176       nCopy = nRem;
 74177       if( nRem>p->nBuffer ) nCopy = p->nBuffer;
 74178       rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
 74179       if( rc!=SQLITE_OK ) return rc;
 74180       assert( aNext!=p->aAlloc );
 74181       memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
 74182       nRem -= nCopy;
 74185     *ppOut = p->aAlloc;
 74188   return SQLITE_OK;
 74191 /*
 74192 ** Read a varint from the stream of data accessed by p. Set *pnOut to
 74193 ** the value read.
 74194 */
 74195 static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
 74196   int iBuf;
 74198   iBuf = p->iReadOff % p->nBuffer;
 74199   if( iBuf && (p->nBuffer-iBuf)>=9 ){
 74200     p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
 74201   }else{
 74202     u8 aVarint[16], *a;
 74203     int i = 0, rc;
 74204     do{
 74205       rc = vdbeSorterIterRead(db, p, 1, &a);
 74206       if( rc ) return rc;
 74207       aVarint[(i++)&0xf] = a[0];
 74208     }while( (a[0]&0x80)!=0 );
 74209     sqlite3GetVarint(aVarint, pnOut);
 74212   return SQLITE_OK;
 74216 /*
 74217 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
 74218 ** no error occurs, or an SQLite error code if one does.
 74219 */
 74220 static int vdbeSorterIterNext(
 74221   sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
 74222   VdbeSorterIter *pIter           /* Iterator to advance */
 74223 ){
 74224   int rc;                         /* Return Code */
 74225   u64 nRec = 0;                   /* Size of record in bytes */
 74227   if( pIter->iReadOff>=pIter->iEof ){
 74228     /* This is an EOF condition */
 74229     vdbeSorterIterZero(db, pIter);
 74230     return SQLITE_OK;
 74233   rc = vdbeSorterIterVarint(db, pIter, &nRec);
 74234   if( rc==SQLITE_OK ){
 74235     pIter->nKey = (int)nRec;
 74236     rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
 74239   return rc;
 74242 /*
 74243 ** Initialize iterator pIter to scan through the PMA stored in file pFile
 74244 ** starting at offset iStart and ending at offset iEof-1. This function 
 74245 ** leaves the iterator pointing to the first key in the PMA (or EOF if the 
 74246 ** PMA is empty).
 74247 */
 74248 static int vdbeSorterIterInit(
 74249   sqlite3 *db,                    /* Database handle */
 74250   const VdbeSorter *pSorter,      /* Sorter object */
 74251   i64 iStart,                     /* Start offset in pFile */
 74252   VdbeSorterIter *pIter,          /* Iterator to populate */
 74253   i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
 74254 ){
 74255   int rc = SQLITE_OK;
 74256   int nBuf;
 74258   nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 74260   assert( pSorter->iWriteOff>iStart );
 74261   assert( pIter->aAlloc==0 );
 74262   assert( pIter->aBuffer==0 );
 74263   pIter->pFile = pSorter->pTemp1;
 74264   pIter->iReadOff = iStart;
 74265   pIter->nAlloc = 128;
 74266   pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
 74267   pIter->nBuffer = nBuf;
 74268   pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 74270   if( !pIter->aBuffer ){
 74271     rc = SQLITE_NOMEM;
 74272   }else{
 74273     int iBuf;
 74275     iBuf = iStart % nBuf;
 74276     if( iBuf ){
 74277       int nRead = nBuf - iBuf;
 74278       if( (iStart + nRead) > pSorter->iWriteOff ){
 74279         nRead = (int)(pSorter->iWriteOff - iStart);
 74281       rc = sqlite3OsRead(
 74282           pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
 74283       );
 74284       assert( rc!=SQLITE_IOERR_SHORT_READ );
 74287     if( rc==SQLITE_OK ){
 74288       u64 nByte;                       /* Size of PMA in bytes */
 74289       pIter->iEof = pSorter->iWriteOff;
 74290       rc = vdbeSorterIterVarint(db, pIter, &nByte);
 74291       pIter->iEof = pIter->iReadOff + nByte;
 74292       *pnByte += nByte;
 74296   if( rc==SQLITE_OK ){
 74297     rc = vdbeSorterIterNext(db, pIter);
 74299   return rc;
 74303 /*
 74304 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
 74305 ** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
 74306 ** used by the comparison. If an error occurs, return an SQLite error code.
 74307 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
 74308 ** value, depending on whether key1 is smaller, equal to or larger than key2.
 74309 **
 74310 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
 74311 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
 74312 ** is true and key1 contains even a single NULL value, it is considered to
 74313 ** be less than key2. Even if key2 also contains NULL values.
 74314 **
 74315 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
 74316 ** has been allocated and contains an unpacked record that is used as key2.
 74317 */
 74318 static void vdbeSorterCompare(
 74319   const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
 74320   int nIgnore,                    /* Ignore the last nIgnore fields */
 74321   const void *pKey1, int nKey1,   /* Left side of comparison */
 74322   const void *pKey2, int nKey2,   /* Right side of comparison */
 74323   int *pRes                       /* OUT: Result of comparison */
 74324 ){
 74325   KeyInfo *pKeyInfo = pCsr->pKeyInfo;
 74326   VdbeSorter *pSorter = pCsr->pSorter;
 74327   UnpackedRecord *r2 = pSorter->pUnpacked;
 74328   int i;
 74330   if( pKey2 ){
 74331     sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
 74334   if( nIgnore ){
 74335     r2->nField = pKeyInfo->nField - nIgnore;
 74336     assert( r2->nField>0 );
 74337     for(i=0; i<r2->nField; i++){
 74338       if( r2->aMem[i].flags & MEM_Null ){
 74339         *pRes = -1;
 74340         return;
 74343     assert( r2->default_rc==0 );
 74346   *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
 74349 /*
 74350 ** This function is called to compare two iterator keys when merging 
 74351 ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
 74352 ** value to recalculate.
 74353 */
 74354 static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
 74355   VdbeSorter *pSorter = pCsr->pSorter;
 74356   int i1;
 74357   int i2;
 74358   int iRes;
 74359   VdbeSorterIter *p1;
 74360   VdbeSorterIter *p2;
 74362   assert( iOut<pSorter->nTree && iOut>0 );
 74364   if( iOut>=(pSorter->nTree/2) ){
 74365     i1 = (iOut - pSorter->nTree/2) * 2;
 74366     i2 = i1 + 1;
 74367   }else{
 74368     i1 = pSorter->aTree[iOut*2];
 74369     i2 = pSorter->aTree[iOut*2+1];
 74372   p1 = &pSorter->aIter[i1];
 74373   p2 = &pSorter->aIter[i2];
 74375   if( p1->pFile==0 ){
 74376     iRes = i2;
 74377   }else if( p2->pFile==0 ){
 74378     iRes = i1;
 74379   }else{
 74380     int res;
 74381     assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
 74382     vdbeSorterCompare(
 74383         pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
 74384     );
 74385     if( res<=0 ){
 74386       iRes = i1;
 74387     }else{
 74388       iRes = i2;
 74392   pSorter->aTree[iOut] = iRes;
 74393   return SQLITE_OK;
 74396 /*
 74397 ** Initialize the temporary index cursor just opened as a sorter cursor.
 74398 */
 74399 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
 74400   int pgsz;                       /* Page size of main database */
 74401   int mxCache;                    /* Cache size */
 74402   VdbeSorter *pSorter;            /* The new sorter */
 74403   char *d;                        /* Dummy */
 74405   assert( pCsr->pKeyInfo && pCsr->pBt==0 );
 74406   pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
 74407   if( pSorter==0 ){
 74408     return SQLITE_NOMEM;
 74411   pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
 74412   if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
 74413   assert( pSorter->pUnpacked==(UnpackedRecord *)d );
 74415   if( !sqlite3TempInMemory(db) ){
 74416     pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 74417     pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
 74418     mxCache = db->aDb[0].pSchema->cache_size;
 74419     if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
 74420     pSorter->mxPmaSize = mxCache * pgsz;
 74423   return SQLITE_OK;
 74426 /*
 74427 ** Free the list of sorted records starting at pRecord.
 74428 */
 74429 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
 74430   SorterRecord *p;
 74431   SorterRecord *pNext;
 74432   for(p=pRecord; p; p=pNext){
 74433     pNext = p->pNext;
 74434     sqlite3DbFree(db, p);
 74438 /*
 74439 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
 74440 */
 74441 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
 74442   VdbeSorter *pSorter = pCsr->pSorter;
 74443   if( pSorter ){
 74444     if( pSorter->aIter ){
 74445       int i;
 74446       for(i=0; i<pSorter->nTree; i++){
 74447         vdbeSorterIterZero(db, &pSorter->aIter[i]);
 74449       sqlite3DbFree(db, pSorter->aIter);
 74451     if( pSorter->pTemp1 ){
 74452       sqlite3OsCloseFree(pSorter->pTemp1);
 74454     vdbeSorterRecordFree(db, pSorter->pRecord);
 74455     sqlite3DbFree(db, pSorter->pUnpacked);
 74456     sqlite3DbFree(db, pSorter);
 74457     pCsr->pSorter = 0;
 74461 /*
 74462 ** Allocate space for a file-handle and open a temporary file. If successful,
 74463 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
 74464 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
 74465 */
 74466 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
 74467   int dummy;
 74468   return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
 74469       SQLITE_OPEN_TEMP_JOURNAL |
 74470       SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
 74471       SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
 74472   );
 74475 /*
 74476 ** Merge the two sorted lists p1 and p2 into a single list.
 74477 ** Set *ppOut to the head of the new list.
 74478 */
 74479 static void vdbeSorterMerge(
 74480   const VdbeCursor *pCsr,         /* For pKeyInfo */
 74481   SorterRecord *p1,               /* First list to merge */
 74482   SorterRecord *p2,               /* Second list to merge */
 74483   SorterRecord **ppOut            /* OUT: Head of merged list */
 74484 ){
 74485   SorterRecord *pFinal = 0;
 74486   SorterRecord **pp = &pFinal;
 74487   void *pVal2 = p2 ? p2->pVal : 0;
 74489   while( p1 && p2 ){
 74490     int res;
 74491     vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
 74492     if( res<=0 ){
 74493       *pp = p1;
 74494       pp = &p1->pNext;
 74495       p1 = p1->pNext;
 74496       pVal2 = 0;
 74497     }else{
 74498       *pp = p2;
 74499        pp = &p2->pNext;
 74500       p2 = p2->pNext;
 74501       if( p2==0 ) break;
 74502       pVal2 = p2->pVal;
 74505   *pp = p1 ? p1 : p2;
 74506   *ppOut = pFinal;
 74509 /*
 74510 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
 74511 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
 74512 ** occurs.
 74513 */
 74514 static int vdbeSorterSort(const VdbeCursor *pCsr){
 74515   int i;
 74516   SorterRecord **aSlot;
 74517   SorterRecord *p;
 74518   VdbeSorter *pSorter = pCsr->pSorter;
 74520   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
 74521   if( !aSlot ){
 74522     return SQLITE_NOMEM;
 74525   p = pSorter->pRecord;
 74526   while( p ){
 74527     SorterRecord *pNext = p->pNext;
 74528     p->pNext = 0;
 74529     for(i=0; aSlot[i]; i++){
 74530       vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 74531       aSlot[i] = 0;
 74533     aSlot[i] = p;
 74534     p = pNext;
 74537   p = 0;
 74538   for(i=0; i<64; i++){
 74539     vdbeSorterMerge(pCsr, p, aSlot[i], &p);
 74541   pSorter->pRecord = p;
 74543   sqlite3_free(aSlot);
 74544   return SQLITE_OK;
 74547 /*
 74548 ** Initialize a file-writer object.
 74549 */
 74550 static void fileWriterInit(
 74551   sqlite3 *db,                    /* Database (for malloc) */
 74552   sqlite3_file *pFile,            /* File to write to */
 74553   FileWriter *p,                  /* Object to populate */
 74554   i64 iStart                      /* Offset of pFile to begin writing at */
 74555 ){
 74556   int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
 74558   memset(p, 0, sizeof(FileWriter));
 74559   p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
 74560   if( !p->aBuffer ){
 74561     p->eFWErr = SQLITE_NOMEM;
 74562   }else{
 74563     p->iBufEnd = p->iBufStart = (iStart % nBuf);
 74564     p->iWriteOff = iStart - p->iBufStart;
 74565     p->nBuffer = nBuf;
 74566     p->pFile = pFile;
 74570 /*
 74571 ** Write nData bytes of data to the file-write object. Return SQLITE_OK
 74572 ** if successful, or an SQLite error code if an error occurs.
 74573 */
 74574 static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
 74575   int nRem = nData;
 74576   while( nRem>0 && p->eFWErr==0 ){
 74577     int nCopy = nRem;
 74578     if( nCopy>(p->nBuffer - p->iBufEnd) ){
 74579       nCopy = p->nBuffer - p->iBufEnd;
 74582     memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
 74583     p->iBufEnd += nCopy;
 74584     if( p->iBufEnd==p->nBuffer ){
 74585       p->eFWErr = sqlite3OsWrite(p->pFile, 
 74586           &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 74587           p->iWriteOff + p->iBufStart
 74588       );
 74589       p->iBufStart = p->iBufEnd = 0;
 74590       p->iWriteOff += p->nBuffer;
 74592     assert( p->iBufEnd<p->nBuffer );
 74594     nRem -= nCopy;
 74598 /*
 74599 ** Flush any buffered data to disk and clean up the file-writer object.
 74600 ** The results of using the file-writer after this call are undefined.
 74601 ** Return SQLITE_OK if flushing the buffered data succeeds or is not 
 74602 ** required. Otherwise, return an SQLite error code.
 74603 **
 74604 ** Before returning, set *piEof to the offset immediately following the
 74605 ** last byte written to the file.
 74606 */
 74607 static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
 74608   int rc;
 74609   if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
 74610     p->eFWErr = sqlite3OsWrite(p->pFile, 
 74611         &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, 
 74612         p->iWriteOff + p->iBufStart
 74613     );
 74615   *piEof = (p->iWriteOff + p->iBufEnd);
 74616   sqlite3DbFree(db, p->aBuffer);
 74617   rc = p->eFWErr;
 74618   memset(p, 0, sizeof(FileWriter));
 74619   return rc;
 74622 /*
 74623 ** Write value iVal encoded as a varint to the file-write object. Return 
 74624 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
 74625 */
 74626 static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
 74627   int nByte; 
 74628   u8 aByte[10];
 74629   nByte = sqlite3PutVarint(aByte, iVal);
 74630   fileWriterWrite(p, aByte, nByte);
 74633 /*
 74634 ** Write the current contents of the in-memory linked-list to a PMA. Return
 74635 ** SQLITE_OK if successful, or an SQLite error code otherwise.
 74636 **
 74637 ** The format of a PMA is:
 74638 **
 74639 **     * A varint. This varint contains the total number of bytes of content
 74640 **       in the PMA (not including the varint itself).
 74641 **
 74642 **     * One or more records packed end-to-end in order of ascending keys. 
 74643 **       Each record consists of a varint followed by a blob of data (the 
 74644 **       key). The varint is the number of bytes in the blob of data.
 74645 */
 74646 static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
 74647   int rc = SQLITE_OK;             /* Return code */
 74648   VdbeSorter *pSorter = pCsr->pSorter;
 74649   FileWriter writer;
 74651   memset(&writer, 0, sizeof(FileWriter));
 74653   if( pSorter->nInMemory==0 ){
 74654     assert( pSorter->pRecord==0 );
 74655     return rc;
 74658   rc = vdbeSorterSort(pCsr);
 74660   /* If the first temporary PMA file has not been opened, open it now. */
 74661   if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
 74662     rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
 74663     assert( rc!=SQLITE_OK || pSorter->pTemp1 );
 74664     assert( pSorter->iWriteOff==0 );
 74665     assert( pSorter->nPMA==0 );
 74668   if( rc==SQLITE_OK ){
 74669     SorterRecord *p;
 74670     SorterRecord *pNext = 0;
 74672     fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
 74673     pSorter->nPMA++;
 74674     fileWriterWriteVarint(&writer, pSorter->nInMemory);
 74675     for(p=pSorter->pRecord; p; p=pNext){
 74676       pNext = p->pNext;
 74677       fileWriterWriteVarint(&writer, p->nVal);
 74678       fileWriterWrite(&writer, p->pVal, p->nVal);
 74679       sqlite3DbFree(db, p);
 74681     pSorter->pRecord = p;
 74682     rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
 74685   return rc;
 74688 /*
 74689 ** Add a record to the sorter.
 74690 */
 74691 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
 74692   sqlite3 *db,                    /* Database handle */
 74693   const VdbeCursor *pCsr,               /* Sorter cursor */
 74694   Mem *pVal                       /* Memory cell containing record */
 74695 ){
 74696   VdbeSorter *pSorter = pCsr->pSorter;
 74697   int rc = SQLITE_OK;             /* Return Code */
 74698   SorterRecord *pNew;             /* New list element */
 74700   assert( pSorter );
 74701   pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
 74703   pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
 74704   if( pNew==0 ){
 74705     rc = SQLITE_NOMEM;
 74706   }else{
 74707     pNew->pVal = (void *)&pNew[1];
 74708     memcpy(pNew->pVal, pVal->z, pVal->n);
 74709     pNew->nVal = pVal->n;
 74710     pNew->pNext = pSorter->pRecord;
 74711     pSorter->pRecord = pNew;
 74714   /* See if the contents of the sorter should now be written out. They
 74715   ** are written out when either of the following are true:
 74716   **
 74717   **   * The total memory allocated for the in-memory list is greater 
 74718   **     than (page-size * cache-size), or
 74719   **
 74720   **   * The total memory allocated for the in-memory list is greater 
 74721   **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
 74722   */
 74723   if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
 74724         (pSorter->nInMemory>pSorter->mxPmaSize)
 74725      || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
 74726   )){
 74727 #ifdef SQLITE_DEBUG
 74728     i64 nExpect = pSorter->iWriteOff
 74729                 + sqlite3VarintLen(pSorter->nInMemory)
 74730                 + pSorter->nInMemory;
 74731 #endif
 74732     rc = vdbeSorterListToPMA(db, pCsr);
 74733     pSorter->nInMemory = 0;
 74734     assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
 74737   return rc;
 74740 /*
 74741 ** Helper function for sqlite3VdbeSorterRewind(). 
 74742 */
 74743 static int vdbeSorterInitMerge(
 74744   sqlite3 *db,                    /* Database handle */
 74745   const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
 74746   i64 *pnByte                     /* Sum of bytes in all opened PMAs */
 74747 ){
 74748   VdbeSorter *pSorter = pCsr->pSorter;
 74749   int rc = SQLITE_OK;             /* Return code */
 74750   int i;                          /* Used to iterator through aIter[] */
 74751   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
 74753   /* Initialize the iterators. */
 74754   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
 74755     VdbeSorterIter *pIter = &pSorter->aIter[i];
 74756     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
 74757     pSorter->iReadOff = pIter->iEof;
 74758     assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
 74759     if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
 74762   /* Initialize the aTree[] array. */
 74763   for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
 74764     rc = vdbeSorterDoCompare(pCsr, i);
 74767   *pnByte = nByte;
 74768   return rc;
 74771 /*
 74772 ** Once the sorter has been populated, this function is called to prepare
 74773 ** for iterating through its contents in sorted order.
 74774 */
 74775 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 74776   VdbeSorter *pSorter = pCsr->pSorter;
 74777   int rc;                         /* Return code */
 74778   sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
 74779   i64 iWrite2 = 0;                /* Write offset for pTemp2 */
 74780   int nIter;                      /* Number of iterators used */
 74781   int nByte;                      /* Bytes of space required for aIter/aTree */
 74782   int N = 2;                      /* Power of 2 >= nIter */
 74784   assert( pSorter );
 74786   /* If no data has been written to disk, then do not do so now. Instead,
 74787   ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
 74788   ** from the in-memory list.  */
 74789   if( pSorter->nPMA==0 ){
 74790     *pbEof = !pSorter->pRecord;
 74791     assert( pSorter->aTree==0 );
 74792     return vdbeSorterSort(pCsr);
 74795   /* Write the current in-memory list to a PMA. */
 74796   rc = vdbeSorterListToPMA(db, pCsr);
 74797   if( rc!=SQLITE_OK ) return rc;
 74799   /* Allocate space for aIter[] and aTree[]. */
 74800   nIter = pSorter->nPMA;
 74801   if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
 74802   assert( nIter>0 );
 74803   while( N<nIter ) N += N;
 74804   nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
 74805   pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
 74806   if( !pSorter->aIter ) return SQLITE_NOMEM;
 74807   pSorter->aTree = (int *)&pSorter->aIter[N];
 74808   pSorter->nTree = N;
 74810   do {
 74811     int iNew;                     /* Index of new, merged, PMA */
 74813     for(iNew=0; 
 74814         rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
 74815         iNew++
 74816     ){
 74817       int rc2;                    /* Return code from fileWriterFinish() */
 74818       FileWriter writer;          /* Object used to write to disk */
 74819       i64 nWrite;                 /* Number of bytes in new PMA */
 74821       memset(&writer, 0, sizeof(FileWriter));
 74823       /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
 74824       ** initialize an iterator for each of them and break out of the loop.
 74825       ** These iterators will be incrementally merged as the VDBE layer calls
 74826       ** sqlite3VdbeSorterNext().
 74827       **
 74828       ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
 74829       ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
 74830       ** are merged into a single PMA that is written to file pTemp2.
 74831       */
 74832       rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
 74833       assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
 74834       if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 74835         break;
 74838       /* Open the second temp file, if it is not already open. */
 74839       if( pTemp2==0 ){
 74840         assert( iWrite2==0 );
 74841         rc = vdbeSorterOpenTempFile(db, &pTemp2);
 74844       if( rc==SQLITE_OK ){
 74845         int bEof = 0;
 74846         fileWriterInit(db, pTemp2, &writer, iWrite2);
 74847         fileWriterWriteVarint(&writer, nWrite);
 74848         while( rc==SQLITE_OK && bEof==0 ){
 74849           VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 74850           assert( pIter->pFile );
 74852           fileWriterWriteVarint(&writer, pIter->nKey);
 74853           fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
 74854           rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
 74856         rc2 = fileWriterFinish(db, &writer, &iWrite2);
 74857         if( rc==SQLITE_OK ) rc = rc2;
 74861     if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
 74862       break;
 74863     }else{
 74864       sqlite3_file *pTmp = pSorter->pTemp1;
 74865       pSorter->nPMA = iNew;
 74866       pSorter->pTemp1 = pTemp2;
 74867       pTemp2 = pTmp;
 74868       pSorter->iWriteOff = iWrite2;
 74869       pSorter->iReadOff = 0;
 74870       iWrite2 = 0;
 74872   }while( rc==SQLITE_OK );
 74874   if( pTemp2 ){
 74875     sqlite3OsCloseFree(pTemp2);
 74877   *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 74878   return rc;
 74881 /*
 74882 ** Advance to the next element in the sorter.
 74883 */
 74884 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
 74885   VdbeSorter *pSorter = pCsr->pSorter;
 74886   int rc;                         /* Return code */
 74888   if( pSorter->aTree ){
 74889     int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
 74890     int i;                        /* Index of aTree[] to recalculate */
 74892     rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
 74893     for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
 74894       rc = vdbeSorterDoCompare(pCsr, i);
 74897     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
 74898   }else{
 74899     SorterRecord *pFree = pSorter->pRecord;
 74900     pSorter->pRecord = pFree->pNext;
 74901     pFree->pNext = 0;
 74902     vdbeSorterRecordFree(db, pFree);
 74903     *pbEof = !pSorter->pRecord;
 74904     rc = SQLITE_OK;
 74906   return rc;
 74909 /*
 74910 ** Return a pointer to a buffer owned by the sorter that contains the 
 74911 ** current key.
 74912 */
 74913 static void *vdbeSorterRowkey(
 74914   const VdbeSorter *pSorter,      /* Sorter object */
 74915   int *pnKey                      /* OUT: Size of current key in bytes */
 74916 ){
 74917   void *pKey;
 74918   if( pSorter->aTree ){
 74919     VdbeSorterIter *pIter;
 74920     pIter = &pSorter->aIter[ pSorter->aTree[1] ];
 74921     *pnKey = pIter->nKey;
 74922     pKey = pIter->aKey;
 74923   }else{
 74924     *pnKey = pSorter->pRecord->nVal;
 74925     pKey = pSorter->pRecord->pVal;
 74927   return pKey;
 74930 /*
 74931 ** Copy the current sorter key into the memory cell pOut.
 74932 */
 74933 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
 74934   VdbeSorter *pSorter = pCsr->pSorter;
 74935   void *pKey; int nKey;           /* Sorter key to copy into pOut */
 74937   pKey = vdbeSorterRowkey(pSorter, &nKey);
 74938   if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
 74939     return SQLITE_NOMEM;
 74941   pOut->n = nKey;
 74942   MemSetTypeFlag(pOut, MEM_Blob);
 74943   memcpy(pOut->z, pKey, nKey);
 74945   return SQLITE_OK;
 74948 /*
 74949 ** Compare the key in memory cell pVal with the key that the sorter cursor
 74950 ** passed as the first argument currently points to. For the purposes of
 74951 ** the comparison, ignore the rowid field at the end of each record.
 74952 **
 74953 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
 74954 ** Otherwise, set *pRes to a negative, zero or positive value if the
 74955 ** key in pVal is smaller than, equal to or larger than the current sorter
 74956 ** key.
 74957 */
 74958 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
 74959   const VdbeCursor *pCsr,         /* Sorter cursor */
 74960   Mem *pVal,                      /* Value to compare to current sorter key */
 74961   int nIgnore,                    /* Ignore this many fields at the end */
 74962   int *pRes                       /* OUT: Result of comparison */
 74963 ){
 74964   VdbeSorter *pSorter = pCsr->pSorter;
 74965   void *pKey; int nKey;           /* Sorter key to compare pVal with */
 74967   pKey = vdbeSorterRowkey(pSorter, &nKey);
 74968   vdbeSorterCompare(pCsr, nIgnore, pVal->z, pVal->n, pKey, nKey, pRes);
 74969   return SQLITE_OK;
 74972 /************** End of vdbesort.c ********************************************/
 74973 /************** Begin file journal.c *****************************************/
 74974 /*
 74975 ** 2007 August 22
 74976 **
 74977 ** The author disclaims copyright to this source code.  In place of
 74978 ** a legal notice, here is a blessing:
 74979 **
 74980 **    May you do good and not evil.
 74981 **    May you find forgiveness for yourself and forgive others.
 74982 **    May you share freely, never taking more than you give.
 74983 **
 74984 *************************************************************************
 74985 **
 74986 ** This file implements a special kind of sqlite3_file object used
 74987 ** by SQLite to create journal files if the atomic-write optimization
 74988 ** is enabled.
 74989 **
 74990 ** The distinctive characteristic of this sqlite3_file is that the
 74991 ** actual on disk file is created lazily. When the file is created,
 74992 ** the caller specifies a buffer size for an in-memory buffer to
 74993 ** be used to service read() and write() requests. The actual file
 74994 ** on disk is not created or populated until either:
 74995 **
 74996 **   1) The in-memory representation grows too large for the allocated 
 74997 **      buffer, or
 74998 **   2) The sqlite3JournalCreate() function is called.
 74999 */
 75000 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 75003 /*
 75004 ** A JournalFile object is a subclass of sqlite3_file used by
 75005 ** as an open file handle for journal files.
 75006 */
 75007 struct JournalFile {
 75008   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
 75009   int nBuf;                       /* Size of zBuf[] in bytes */
 75010   char *zBuf;                     /* Space to buffer journal writes */
 75011   int iSize;                      /* Amount of zBuf[] currently used */
 75012   int flags;                      /* xOpen flags */
 75013   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
 75014   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
 75015   const char *zJournal;           /* Name of the journal file */
 75016 };
 75017 typedef struct JournalFile JournalFile;
 75019 /*
 75020 ** If it does not already exists, create and populate the on-disk file 
 75021 ** for JournalFile p.
 75022 */
 75023 static int createFile(JournalFile *p){
 75024   int rc = SQLITE_OK;
 75025   if( !p->pReal ){
 75026     sqlite3_file *pReal = (sqlite3_file *)&p[1];
 75027     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
 75028     if( rc==SQLITE_OK ){
 75029       p->pReal = pReal;
 75030       if( p->iSize>0 ){
 75031         assert(p->iSize<=p->nBuf);
 75032         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
 75034       if( rc!=SQLITE_OK ){
 75035         /* If an error occurred while writing to the file, close it before
 75036         ** returning. This way, SQLite uses the in-memory journal data to 
 75037         ** roll back changes made to the internal page-cache before this
 75038         ** function was called.  */
 75039         sqlite3OsClose(pReal);
 75040         p->pReal = 0;
 75044   return rc;
 75047 /*
 75048 ** Close the file.
 75049 */
 75050 static int jrnlClose(sqlite3_file *pJfd){
 75051   JournalFile *p = (JournalFile *)pJfd;
 75052   if( p->pReal ){
 75053     sqlite3OsClose(p->pReal);
 75055   sqlite3_free(p->zBuf);
 75056   return SQLITE_OK;
 75059 /*
 75060 ** Read data from the file.
 75061 */
 75062 static int jrnlRead(
 75063   sqlite3_file *pJfd,    /* The journal file from which to read */
 75064   void *zBuf,            /* Put the results here */
 75065   int iAmt,              /* Number of bytes to read */
 75066   sqlite_int64 iOfst     /* Begin reading at this offset */
 75067 ){
 75068   int rc = SQLITE_OK;
 75069   JournalFile *p = (JournalFile *)pJfd;
 75070   if( p->pReal ){
 75071     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
 75072   }else if( (iAmt+iOfst)>p->iSize ){
 75073     rc = SQLITE_IOERR_SHORT_READ;
 75074   }else{
 75075     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
 75077   return rc;
 75080 /*
 75081 ** Write data to the file.
 75082 */
 75083 static int jrnlWrite(
 75084   sqlite3_file *pJfd,    /* The journal file into which to write */
 75085   const void *zBuf,      /* Take data to be written from here */
 75086   int iAmt,              /* Number of bytes to write */
 75087   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 75088 ){
 75089   int rc = SQLITE_OK;
 75090   JournalFile *p = (JournalFile *)pJfd;
 75091   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
 75092     rc = createFile(p);
 75094   if( rc==SQLITE_OK ){
 75095     if( p->pReal ){
 75096       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
 75097     }else{
 75098       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
 75099       if( p->iSize<(iOfst+iAmt) ){
 75100         p->iSize = (iOfst+iAmt);
 75104   return rc;
 75107 /*
 75108 ** Truncate the file.
 75109 */
 75110 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 75111   int rc = SQLITE_OK;
 75112   JournalFile *p = (JournalFile *)pJfd;
 75113   if( p->pReal ){
 75114     rc = sqlite3OsTruncate(p->pReal, size);
 75115   }else if( size<p->iSize ){
 75116     p->iSize = size;
 75118   return rc;
 75121 /*
 75122 ** Sync the file.
 75123 */
 75124 static int jrnlSync(sqlite3_file *pJfd, int flags){
 75125   int rc;
 75126   JournalFile *p = (JournalFile *)pJfd;
 75127   if( p->pReal ){
 75128     rc = sqlite3OsSync(p->pReal, flags);
 75129   }else{
 75130     rc = SQLITE_OK;
 75132   return rc;
 75135 /*
 75136 ** Query the size of the file in bytes.
 75137 */
 75138 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 75139   int rc = SQLITE_OK;
 75140   JournalFile *p = (JournalFile *)pJfd;
 75141   if( p->pReal ){
 75142     rc = sqlite3OsFileSize(p->pReal, pSize);
 75143   }else{
 75144     *pSize = (sqlite_int64) p->iSize;
 75146   return rc;
 75149 /*
 75150 ** Table of methods for JournalFile sqlite3_file object.
 75151 */
 75152 static struct sqlite3_io_methods JournalFileMethods = {
 75153   1,             /* iVersion */
 75154   jrnlClose,     /* xClose */
 75155   jrnlRead,      /* xRead */
 75156   jrnlWrite,     /* xWrite */
 75157   jrnlTruncate,  /* xTruncate */
 75158   jrnlSync,      /* xSync */
 75159   jrnlFileSize,  /* xFileSize */
 75160   0,             /* xLock */
 75161   0,             /* xUnlock */
 75162   0,             /* xCheckReservedLock */
 75163   0,             /* xFileControl */
 75164   0,             /* xSectorSize */
 75165   0,             /* xDeviceCharacteristics */
 75166   0,             /* xShmMap */
 75167   0,             /* xShmLock */
 75168   0,             /* xShmBarrier */
 75169   0              /* xShmUnmap */
 75170 };
 75172 /* 
 75173 ** Open a journal file.
 75174 */
 75175 SQLITE_PRIVATE int sqlite3JournalOpen(
 75176   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
 75177   const char *zName,         /* Name of the journal file */
 75178   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
 75179   int flags,                 /* Opening flags */
 75180   int nBuf                   /* Bytes buffered before opening the file */
 75181 ){
 75182   JournalFile *p = (JournalFile *)pJfd;
 75183   memset(p, 0, sqlite3JournalSize(pVfs));
 75184   if( nBuf>0 ){
 75185     p->zBuf = sqlite3MallocZero(nBuf);
 75186     if( !p->zBuf ){
 75187       return SQLITE_NOMEM;
 75189   }else{
 75190     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
 75192   p->pMethod = &JournalFileMethods;
 75193   p->nBuf = nBuf;
 75194   p->flags = flags;
 75195   p->zJournal = zName;
 75196   p->pVfs = pVfs;
 75197   return SQLITE_OK;
 75200 /*
 75201 ** If the argument p points to a JournalFile structure, and the underlying
 75202 ** file has not yet been created, create it now.
 75203 */
 75204 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
 75205   if( p->pMethods!=&JournalFileMethods ){
 75206     return SQLITE_OK;
 75208   return createFile((JournalFile *)p);
 75211 /*
 75212 ** The file-handle passed as the only argument is guaranteed to be an open
 75213 ** file. It may or may not be of class JournalFile. If the file is a
 75214 ** JournalFile, and the underlying file on disk has not yet been opened,
 75215 ** return 0. Otherwise, return 1.
 75216 */
 75217 SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
 75218   return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
 75221 /* 
 75222 ** Return the number of bytes required to store a JournalFile that uses vfs
 75223 ** pVfs to create the underlying on-disk files.
 75224 */
 75225 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
 75226   return (pVfs->szOsFile+sizeof(JournalFile));
 75228 #endif
 75230 /************** End of journal.c *********************************************/
 75231 /************** Begin file memjournal.c **************************************/
 75232 /*
 75233 ** 2008 October 7
 75234 **
 75235 ** The author disclaims copyright to this source code.  In place of
 75236 ** a legal notice, here is a blessing:
 75237 **
 75238 **    May you do good and not evil.
 75239 **    May you find forgiveness for yourself and forgive others.
 75240 **    May you share freely, never taking more than you give.
 75241 **
 75242 *************************************************************************
 75243 **
 75244 ** This file contains code use to implement an in-memory rollback journal.
 75245 ** The in-memory rollback journal is used to journal transactions for
 75246 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
 75247 */
 75249 /* Forward references to internal structures */
 75250 typedef struct MemJournal MemJournal;
 75251 typedef struct FilePoint FilePoint;
 75252 typedef struct FileChunk FileChunk;
 75254 /* Space to hold the rollback journal is allocated in increments of
 75255 ** this many bytes.
 75256 **
 75257 ** The size chosen is a little less than a power of two.  That way,
 75258 ** the FileChunk object will have a size that almost exactly fills
 75259 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
 75260 ** memory allocators.
 75261 */
 75262 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
 75264 /*
 75265 ** The rollback journal is composed of a linked list of these structures.
 75266 */
 75267 struct FileChunk {
 75268   FileChunk *pNext;               /* Next chunk in the journal */
 75269   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
 75270 };
 75272 /*
 75273 ** An instance of this object serves as a cursor into the rollback journal.
 75274 ** The cursor can be either for reading or writing.
 75275 */
 75276 struct FilePoint {
 75277   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
 75278   FileChunk *pChunk;              /* Specific chunk into which cursor points */
 75279 };
 75281 /*
 75282 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
 75283 ** is an instance of this class.
 75284 */
 75285 struct MemJournal {
 75286   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
 75287   FileChunk *pFirst;              /* Head of in-memory chunk-list */
 75288   FilePoint endpoint;             /* Pointer to the end of the file */
 75289   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
 75290 };
 75292 /*
 75293 ** Read data from the in-memory journal file.  This is the implementation
 75294 ** of the sqlite3_vfs.xRead method.
 75295 */
 75296 static int memjrnlRead(
 75297   sqlite3_file *pJfd,    /* The journal file from which to read */
 75298   void *zBuf,            /* Put the results here */
 75299   int iAmt,              /* Number of bytes to read */
 75300   sqlite_int64 iOfst     /* Begin reading at this offset */
 75301 ){
 75302   MemJournal *p = (MemJournal *)pJfd;
 75303   u8 *zOut = zBuf;
 75304   int nRead = iAmt;
 75305   int iChunkOffset;
 75306   FileChunk *pChunk;
 75308   /* SQLite never tries to read past the end of a rollback journal file */
 75309   assert( iOfst+iAmt<=p->endpoint.iOffset );
 75311   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
 75312     sqlite3_int64 iOff = 0;
 75313     for(pChunk=p->pFirst; 
 75314         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
 75315         pChunk=pChunk->pNext
 75316     ){
 75317       iOff += JOURNAL_CHUNKSIZE;
 75319   }else{
 75320     pChunk = p->readpoint.pChunk;
 75323   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
 75324   do {
 75325     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
 75326     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
 75327     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
 75328     zOut += nCopy;
 75329     nRead -= iSpace;
 75330     iChunkOffset = 0;
 75331   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
 75332   p->readpoint.iOffset = iOfst+iAmt;
 75333   p->readpoint.pChunk = pChunk;
 75335   return SQLITE_OK;
 75338 /*
 75339 ** Write data to the file.
 75340 */
 75341 static int memjrnlWrite(
 75342   sqlite3_file *pJfd,    /* The journal file into which to write */
 75343   const void *zBuf,      /* Take data to be written from here */
 75344   int iAmt,              /* Number of bytes to write */
 75345   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
 75346 ){
 75347   MemJournal *p = (MemJournal *)pJfd;
 75348   int nWrite = iAmt;
 75349   u8 *zWrite = (u8 *)zBuf;
 75351   /* An in-memory journal file should only ever be appended to. Random
 75352   ** access writes are not required by sqlite.
 75353   */
 75354   assert( iOfst==p->endpoint.iOffset );
 75355   UNUSED_PARAMETER(iOfst);
 75357   while( nWrite>0 ){
 75358     FileChunk *pChunk = p->endpoint.pChunk;
 75359     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
 75360     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
 75362     if( iChunkOffset==0 ){
 75363       /* New chunk is required to extend the file. */
 75364       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
 75365       if( !pNew ){
 75366         return SQLITE_IOERR_NOMEM;
 75368       pNew->pNext = 0;
 75369       if( pChunk ){
 75370         assert( p->pFirst );
 75371         pChunk->pNext = pNew;
 75372       }else{
 75373         assert( !p->pFirst );
 75374         p->pFirst = pNew;
 75376       p->endpoint.pChunk = pNew;
 75379     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
 75380     zWrite += iSpace;
 75381     nWrite -= iSpace;
 75382     p->endpoint.iOffset += iSpace;
 75385   return SQLITE_OK;
 75388 /*
 75389 ** Truncate the file.
 75390 */
 75391 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
 75392   MemJournal *p = (MemJournal *)pJfd;
 75393   FileChunk *pChunk;
 75394   assert(size==0);
 75395   UNUSED_PARAMETER(size);
 75396   pChunk = p->pFirst;
 75397   while( pChunk ){
 75398     FileChunk *pTmp = pChunk;
 75399     pChunk = pChunk->pNext;
 75400     sqlite3_free(pTmp);
 75402   sqlite3MemJournalOpen(pJfd);
 75403   return SQLITE_OK;
 75406 /*
 75407 ** Close the file.
 75408 */
 75409 static int memjrnlClose(sqlite3_file *pJfd){
 75410   memjrnlTruncate(pJfd, 0);
 75411   return SQLITE_OK;
 75415 /*
 75416 ** Sync the file.
 75417 **
 75418 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
 75419 ** is never called in a working implementation.  This implementation
 75420 ** exists purely as a contingency, in case some malfunction in some other
 75421 ** part of SQLite causes Sync to be called by mistake.
 75422 */
 75423 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
 75424   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 75425   return SQLITE_OK;
 75428 /*
 75429 ** Query the size of the file in bytes.
 75430 */
 75431 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
 75432   MemJournal *p = (MemJournal *)pJfd;
 75433   *pSize = (sqlite_int64) p->endpoint.iOffset;
 75434   return SQLITE_OK;
 75437 /*
 75438 ** Table of methods for MemJournal sqlite3_file object.
 75439 */
 75440 static const struct sqlite3_io_methods MemJournalMethods = {
 75441   1,                /* iVersion */
 75442   memjrnlClose,     /* xClose */
 75443   memjrnlRead,      /* xRead */
 75444   memjrnlWrite,     /* xWrite */
 75445   memjrnlTruncate,  /* xTruncate */
 75446   memjrnlSync,      /* xSync */
 75447   memjrnlFileSize,  /* xFileSize */
 75448   0,                /* xLock */
 75449   0,                /* xUnlock */
 75450   0,                /* xCheckReservedLock */
 75451   0,                /* xFileControl */
 75452   0,                /* xSectorSize */
 75453   0,                /* xDeviceCharacteristics */
 75454   0,                /* xShmMap */
 75455   0,                /* xShmLock */
 75456   0,                /* xShmBarrier */
 75457   0,                /* xShmUnmap */
 75458   0,                /* xFetch */
 75459   0                 /* xUnfetch */
 75460 };
 75462 /* 
 75463 ** Open a journal file.
 75464 */
 75465 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
 75466   MemJournal *p = (MemJournal *)pJfd;
 75467   assert( EIGHT_BYTE_ALIGNMENT(p) );
 75468   memset(p, 0, sqlite3MemJournalSize());
 75469   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
 75472 /*
 75473 ** Return true if the file-handle passed as an argument is 
 75474 ** an in-memory journal 
 75475 */
 75476 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
 75477   return pJfd->pMethods==&MemJournalMethods;
 75480 /* 
 75481 ** Return the number of bytes required to store a MemJournal file descriptor.
 75482 */
 75483 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
 75484   return sizeof(MemJournal);
 75487 /************** End of memjournal.c ******************************************/
 75488 /************** Begin file walker.c ******************************************/
 75489 /*
 75490 ** 2008 August 16
 75491 **
 75492 ** The author disclaims copyright to this source code.  In place of
 75493 ** a legal notice, here is a blessing:
 75494 **
 75495 **    May you do good and not evil.
 75496 **    May you find forgiveness for yourself and forgive others.
 75497 **    May you share freely, never taking more than you give.
 75498 **
 75499 *************************************************************************
 75500 ** This file contains routines used for walking the parser tree for
 75501 ** an SQL statement.
 75502 */
 75503 /* #include <stdlib.h> */
 75504 /* #include <string.h> */
 75507 /*
 75508 ** Walk an expression tree.  Invoke the callback once for each node
 75509 ** of the expression, while decending.  (In other words, the callback
 75510 ** is invoked before visiting children.)
 75511 **
 75512 ** The return value from the callback should be one of the WRC_*
 75513 ** constants to specify how to proceed with the walk.
 75514 **
 75515 **    WRC_Continue      Continue descending down the tree.
 75516 **
 75517 **    WRC_Prune         Do not descend into child nodes.  But allow
 75518 **                      the walk to continue with sibling nodes.
 75519 **
 75520 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
 75521 **                      return the top-level walk call.
 75522 **
 75523 ** The return value from this routine is WRC_Abort to abandon the tree walk
 75524 ** and WRC_Continue to continue.
 75525 */
 75526 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
 75527   int rc;
 75528   if( pExpr==0 ) return WRC_Continue;
 75529   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
 75530   testcase( ExprHasProperty(pExpr, EP_Reduced) );
 75531   rc = pWalker->xExprCallback(pWalker, pExpr);
 75532   if( rc==WRC_Continue
 75533               && !ExprHasProperty(pExpr,EP_TokenOnly) ){
 75534     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
 75535     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
 75536     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 75537       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
 75538     }else{
 75539       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
 75542   return rc & WRC_Abort;
 75545 /*
 75546 ** Call sqlite3WalkExpr() for every expression in list p or until
 75547 ** an abort request is seen.
 75548 */
 75549 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
 75550   int i;
 75551   struct ExprList_item *pItem;
 75552   if( p ){
 75553     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
 75554       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
 75557   return WRC_Continue;
 75560 /*
 75561 ** Walk all expressions associated with SELECT statement p.  Do
 75562 ** not invoke the SELECT callback on p, but do (of course) invoke
 75563 ** any expr callbacks and SELECT callbacks that come from subqueries.
 75564 ** Return WRC_Abort or WRC_Continue.
 75565 */
 75566 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
 75567   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
 75568   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
 75569   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
 75570   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
 75571   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
 75572   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
 75573   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
 75574   return WRC_Continue;
 75577 /*
 75578 ** Walk the parse trees associated with all subqueries in the
 75579 ** FROM clause of SELECT statement p.  Do not invoke the select
 75580 ** callback on p, but do invoke it on each FROM clause subquery
 75581 ** and on any subqueries further down in the tree.  Return 
 75582 ** WRC_Abort or WRC_Continue;
 75583 */
 75584 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
 75585   SrcList *pSrc;
 75586   int i;
 75587   struct SrcList_item *pItem;
 75589   pSrc = p->pSrc;
 75590   if( ALWAYS(pSrc) ){
 75591     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 75592       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
 75593         return WRC_Abort;
 75597   return WRC_Continue;
 75600 /*
 75601 ** Call sqlite3WalkExpr() for every expression in Select statement p.
 75602 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
 75603 ** on the compound select chain, p->pPrior. 
 75604 **
 75605 ** If it is not NULL, the xSelectCallback() callback is invoked before
 75606 ** the walk of the expressions and FROM clause. The xSelectCallback2()
 75607 ** method, if it is not NULL, is invoked following the walk of the 
 75608 ** expressions and FROM clause.
 75609 **
 75610 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
 75611 ** there is an abort request.
 75612 **
 75613 ** If the Walker does not have an xSelectCallback() then this routine
 75614 ** is a no-op returning WRC_Continue.
 75615 */
 75616 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
 75617   int rc;
 75618   if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
 75619     return WRC_Continue;
 75621   rc = WRC_Continue;
 75622   pWalker->walkerDepth++;
 75623   while( p ){
 75624     if( pWalker->xSelectCallback ){
 75625        rc = pWalker->xSelectCallback(pWalker, p);
 75626        if( rc ) break;
 75628     if( sqlite3WalkSelectExpr(pWalker, p)
 75629      || sqlite3WalkSelectFrom(pWalker, p)
 75630     ){
 75631       pWalker->walkerDepth--;
 75632       return WRC_Abort;
 75634     if( pWalker->xSelectCallback2 ){
 75635       pWalker->xSelectCallback2(pWalker, p);
 75637     p = p->pPrior;
 75639   pWalker->walkerDepth--;
 75640   return rc & WRC_Abort;
 75643 /************** End of walker.c **********************************************/
 75644 /************** Begin file resolve.c *****************************************/
 75645 /*
 75646 ** 2008 August 18
 75647 **
 75648 ** The author disclaims copyright to this source code.  In place of
 75649 ** a legal notice, here is a blessing:
 75650 **
 75651 **    May you do good and not evil.
 75652 **    May you find forgiveness for yourself and forgive others.
 75653 **    May you share freely, never taking more than you give.
 75654 **
 75655 *************************************************************************
 75656 **
 75657 ** This file contains routines used for walking the parser tree and
 75658 ** resolve all identifiers by associating them with a particular
 75659 ** table and column.
 75660 */
 75661 /* #include <stdlib.h> */
 75662 /* #include <string.h> */
 75664 /*
 75665 ** Walk the expression tree pExpr and increase the aggregate function
 75666 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
 75667 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
 75668 ** outer query into an inner subquery.
 75669 **
 75670 ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
 75671 ** is a helper function - a callback for the tree walker.
 75672 */
 75673 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
 75674   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
 75675   return WRC_Continue;
 75677 static void incrAggFunctionDepth(Expr *pExpr, int N){
 75678   if( N>0 ){
 75679     Walker w;
 75680     memset(&w, 0, sizeof(w));
 75681     w.xExprCallback = incrAggDepth;
 75682     w.u.i = N;
 75683     sqlite3WalkExpr(&w, pExpr);
 75687 /*
 75688 ** Turn the pExpr expression into an alias for the iCol-th column of the
 75689 ** result set in pEList.
 75690 **
 75691 ** If the result set column is a simple column reference, then this routine
 75692 ** makes an exact copy.  But for any other kind of expression, this
 75693 ** routine make a copy of the result set column as the argument to the
 75694 ** TK_AS operator.  The TK_AS operator causes the expression to be
 75695 ** evaluated just once and then reused for each alias.
 75696 **
 75697 ** The reason for suppressing the TK_AS term when the expression is a simple
 75698 ** column reference is so that the column reference will be recognized as
 75699 ** usable by indices within the WHERE clause processing logic. 
 75700 **
 75701 ** The TK_AS operator is inhibited if zType[0]=='G'.  This means
 75702 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
 75703 **
 75704 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
 75705 **
 75706 ** Is equivalent to:
 75707 **
 75708 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
 75709 **
 75710 ** The result of random()%5 in the GROUP BY clause is probably different
 75711 ** from the result in the result-set.  On the other hand Standard SQL does
 75712 ** not allow the GROUP BY clause to contain references to result-set columns.
 75713 ** So this should never come up in well-formed queries.
 75714 **
 75715 ** If the reference is followed by a COLLATE operator, then make sure
 75716 ** the COLLATE operator is preserved.  For example:
 75717 **
 75718 **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
 75719 **
 75720 ** Should be transformed into:
 75721 **
 75722 **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
 75723 **
 75724 ** The nSubquery parameter specifies how many levels of subquery the
 75725 ** alias is removed from the original expression.  The usually value is
 75726 ** zero but it might be more if the alias is contained within a subquery
 75727 ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
 75728 ** structures must be increased by the nSubquery amount.
 75729 */
 75730 static void resolveAlias(
 75731   Parse *pParse,         /* Parsing context */
 75732   ExprList *pEList,      /* A result set */
 75733   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
 75734   Expr *pExpr,           /* Transform this into an alias to the result set */
 75735   const char *zType,     /* "GROUP" or "ORDER" or "" */
 75736   int nSubquery          /* Number of subqueries that the label is moving */
 75737 ){
 75738   Expr *pOrig;           /* The iCol-th column of the result set */
 75739   Expr *pDup;            /* Copy of pOrig */
 75740   sqlite3 *db;           /* The database connection */
 75742   assert( iCol>=0 && iCol<pEList->nExpr );
 75743   pOrig = pEList->a[iCol].pExpr;
 75744   assert( pOrig!=0 );
 75745   assert( pOrig->flags & EP_Resolved );
 75746   db = pParse->db;
 75747   pDup = sqlite3ExprDup(db, pOrig, 0);
 75748   if( pDup==0 ) return;
 75749   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
 75750     incrAggFunctionDepth(pDup, nSubquery);
 75751     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
 75752     if( pDup==0 ) return;
 75753     ExprSetProperty(pDup, EP_Skip);
 75754     if( pEList->a[iCol].u.x.iAlias==0 ){
 75755       pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
 75757     pDup->iTable = pEList->a[iCol].u.x.iAlias;
 75759   if( pExpr->op==TK_COLLATE ){
 75760     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
 75763   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
 75764   ** prevents ExprDelete() from deleting the Expr structure itself,
 75765   ** allowing it to be repopulated by the memcpy() on the following line.
 75766   ** The pExpr->u.zToken might point into memory that will be freed by the
 75767   ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
 75768   ** make a copy of the token before doing the sqlite3DbFree().
 75769   */
 75770   ExprSetProperty(pExpr, EP_Static);
 75771   sqlite3ExprDelete(db, pExpr);
 75772   memcpy(pExpr, pDup, sizeof(*pExpr));
 75773   if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
 75774     assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
 75775     pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
 75776     pExpr->flags |= EP_MemToken;
 75778   sqlite3DbFree(db, pDup);
 75782 /*
 75783 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
 75784 **
 75785 ** Return FALSE if the USING clause is NULL or if it does not contain
 75786 ** zCol.
 75787 */
 75788 static int nameInUsingClause(IdList *pUsing, const char *zCol){
 75789   if( pUsing ){
 75790     int k;
 75791     for(k=0; k<pUsing->nId; k++){
 75792       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
 75795   return 0;
 75798 /*
 75799 ** Subqueries stores the original database, table and column names for their
 75800 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
 75801 ** Check to see if the zSpan given to this routine matches the zDb, zTab,
 75802 ** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
 75803 ** match anything.
 75804 */
 75805 SQLITE_PRIVATE int sqlite3MatchSpanName(
 75806   const char *zSpan,
 75807   const char *zCol,
 75808   const char *zTab,
 75809   const char *zDb
 75810 ){
 75811   int n;
 75812   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
 75813   if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
 75814     return 0;
 75816   zSpan += n+1;
 75817   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
 75818   if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
 75819     return 0;
 75821   zSpan += n+1;
 75822   if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
 75823     return 0;
 75825   return 1;
 75828 /*
 75829 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
 75830 ** that name in the set of source tables in pSrcList and make the pExpr 
 75831 ** expression node refer back to that source column.  The following changes
 75832 ** are made to pExpr:
 75833 **
 75834 **    pExpr->iDb           Set the index in db->aDb[] of the database X
 75835 **                         (even if X is implied).
 75836 **    pExpr->iTable        Set to the cursor number for the table obtained
 75837 **                         from pSrcList.
 75838 **    pExpr->pTab          Points to the Table structure of X.Y (even if
 75839 **                         X and/or Y are implied.)
 75840 **    pExpr->iColumn       Set to the column number within the table.
 75841 **    pExpr->op            Set to TK_COLUMN.
 75842 **    pExpr->pLeft         Any expression this points to is deleted
 75843 **    pExpr->pRight        Any expression this points to is deleted.
 75844 **
 75845 ** The zDb variable is the name of the database (the "X").  This value may be
 75846 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
 75847 ** can be used.  The zTable variable is the name of the table (the "Y").  This
 75848 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
 75849 ** means that the form of the name is Z and that columns from any table
 75850 ** can be used.
 75851 **
 75852 ** If the name cannot be resolved unambiguously, leave an error message
 75853 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
 75854 */
 75855 static int lookupName(
 75856   Parse *pParse,       /* The parsing context */
 75857   const char *zDb,     /* Name of the database containing table, or NULL */
 75858   const char *zTab,    /* Name of table containing column, or NULL */
 75859   const char *zCol,    /* Name of the column. */
 75860   NameContext *pNC,    /* The name context used to resolve the name */
 75861   Expr *pExpr          /* Make this EXPR node point to the selected column */
 75862 ){
 75863   int i, j;                         /* Loop counters */
 75864   int cnt = 0;                      /* Number of matching column names */
 75865   int cntTab = 0;                   /* Number of matching table names */
 75866   int nSubquery = 0;                /* How many levels of subquery */
 75867   sqlite3 *db = pParse->db;         /* The database connection */
 75868   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
 75869   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
 75870   NameContext *pTopNC = pNC;        /* First namecontext in the list */
 75871   Schema *pSchema = 0;              /* Schema of the expression */
 75872   int isTrigger = 0;                /* True if resolved to a trigger column */
 75873   Table *pTab = 0;                  /* Table hold the row */
 75874   Column *pCol;                     /* A column of pTab */
 75876   assert( pNC );     /* the name context cannot be NULL. */
 75877   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
 75878   assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 75880   /* Initialize the node to no-match */
 75881   pExpr->iTable = -1;
 75882   pExpr->pTab = 0;
 75883   ExprSetVVAProperty(pExpr, EP_NoReduce);
 75885   /* Translate the schema name in zDb into a pointer to the corresponding
 75886   ** schema.  If not found, pSchema will remain NULL and nothing will match
 75887   ** resulting in an appropriate error message toward the end of this routine
 75888   */
 75889   if( zDb ){
 75890     testcase( pNC->ncFlags & NC_PartIdx );
 75891     testcase( pNC->ncFlags & NC_IsCheck );
 75892     if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
 75893       /* Silently ignore database qualifiers inside CHECK constraints and partial
 75894       ** indices.  Do not raise errors because that might break legacy and
 75895       ** because it does not hurt anything to just ignore the database name. */
 75896       zDb = 0;
 75897     }else{
 75898       for(i=0; i<db->nDb; i++){
 75899         assert( db->aDb[i].zName );
 75900         if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
 75901           pSchema = db->aDb[i].pSchema;
 75902           break;
 75908   /* Start at the inner-most context and move outward until a match is found */
 75909   while( pNC && cnt==0 ){
 75910     ExprList *pEList;
 75911     SrcList *pSrcList = pNC->pSrcList;
 75913     if( pSrcList ){
 75914       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
 75915         pTab = pItem->pTab;
 75916         assert( pTab!=0 && pTab->zName!=0 );
 75917         assert( pTab->nCol>0 );
 75918         if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
 75919           int hit = 0;
 75920           pEList = pItem->pSelect->pEList;
 75921           for(j=0; j<pEList->nExpr; j++){
 75922             if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
 75923               cnt++;
 75924               cntTab = 2;
 75925               pMatch = pItem;
 75926               pExpr->iColumn = j;
 75927               hit = 1;
 75930           if( hit || zTab==0 ) continue;
 75932         if( zDb && pTab->pSchema!=pSchema ){
 75933           continue;
 75935         if( zTab ){
 75936           const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
 75937           assert( zTabName!=0 );
 75938           if( sqlite3StrICmp(zTabName, zTab)!=0 ){
 75939             continue;
 75942         if( 0==(cntTab++) ){
 75943           pMatch = pItem;
 75945         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
 75946           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 75947             /* If there has been exactly one prior match and this match
 75948             ** is for the right-hand table of a NATURAL JOIN or is in a 
 75949             ** USING clause, then skip this match.
 75950             */
 75951             if( cnt==1 ){
 75952               if( pItem->jointype & JT_NATURAL ) continue;
 75953               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
 75955             cnt++;
 75956             pMatch = pItem;
 75957             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
 75958             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
 75959             break;
 75963       if( pMatch ){
 75964         pExpr->iTable = pMatch->iCursor;
 75965         pExpr->pTab = pMatch->pTab;
 75966         pSchema = pExpr->pTab->pSchema;
 75968     } /* if( pSrcList ) */
 75970 #ifndef SQLITE_OMIT_TRIGGER
 75971     /* If we have not already resolved the name, then maybe 
 75972     ** it is a new.* or old.* trigger argument reference
 75973     */
 75974     if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
 75975       int op = pParse->eTriggerOp;
 75976       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
 75977       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
 75978         pExpr->iTable = 1;
 75979         pTab = pParse->pTriggerTab;
 75980       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
 75981         pExpr->iTable = 0;
 75982         pTab = pParse->pTriggerTab;
 75983       }else{
 75984         pTab = 0;
 75987       if( pTab ){ 
 75988         int iCol;
 75989         pSchema = pTab->pSchema;
 75990         cntTab++;
 75991         for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
 75992           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
 75993             if( iCol==pTab->iPKey ){
 75994               iCol = -1;
 75996             break;
 75999         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
 76000           /* IMP: R-24309-18625 */
 76001           /* IMP: R-44911-55124 */
 76002           iCol = -1;
 76004         if( iCol<pTab->nCol ){
 76005           cnt++;
 76006           if( iCol<0 ){
 76007             pExpr->affinity = SQLITE_AFF_INTEGER;
 76008           }else if( pExpr->iTable==0 ){
 76009             testcase( iCol==31 );
 76010             testcase( iCol==32 );
 76011             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 76012           }else{
 76013             testcase( iCol==31 );
 76014             testcase( iCol==32 );
 76015             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
 76017           pExpr->iColumn = (i16)iCol;
 76018           pExpr->pTab = pTab;
 76019           isTrigger = 1;
 76023 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 76025     /*
 76026     ** Perhaps the name is a reference to the ROWID
 76027     */
 76028     if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
 76029      && HasRowid(pMatch->pTab) ){
 76030       cnt = 1;
 76031       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
 76032       pExpr->affinity = SQLITE_AFF_INTEGER;
 76035     /*
 76036     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
 76037     ** might refer to an result-set alias.  This happens, for example, when
 76038     ** we are resolving names in the WHERE clause of the following command:
 76039     **
 76040     **     SELECT a+b AS x FROM table WHERE x<10;
 76041     **
 76042     ** In cases like this, replace pExpr with a copy of the expression that
 76043     ** forms the result set entry ("a+b" in the example) and return immediately.
 76044     ** Note that the expression in the result set should have already been
 76045     ** resolved by the time the WHERE clause is resolved.
 76046     **
 76047     ** The ability to use an output result-set column in the WHERE, GROUP BY,
 76048     ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
 76049     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
 76050     ** is supported for backwards compatibility only.  TO DO: Issue a warning
 76051     ** on sqlite3_log() whenever the capability is used.
 76052     */
 76053     if( (pEList = pNC->pEList)!=0
 76054      && zTab==0
 76055      && cnt==0
 76056     ){
 76057       for(j=0; j<pEList->nExpr; j++){
 76058         char *zAs = pEList->a[j].zName;
 76059         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 76060           Expr *pOrig;
 76061           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 76062           assert( pExpr->x.pList==0 );
 76063           assert( pExpr->x.pSelect==0 );
 76064           pOrig = pEList->a[j].pExpr;
 76065           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
 76066             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
 76067             return WRC_Abort;
 76069           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
 76070           cnt = 1;
 76071           pMatch = 0;
 76072           assert( zTab==0 && zDb==0 );
 76073           goto lookupname_end;
 76078     /* Advance to the next name context.  The loop will exit when either
 76079     ** we have a match (cnt>0) or when we run out of name contexts.
 76080     */
 76081     if( cnt==0 ){
 76082       pNC = pNC->pNext;
 76083       nSubquery++;
 76087   /*
 76088   ** If X and Y are NULL (in other words if only the column name Z is
 76089   ** supplied) and the value of Z is enclosed in double-quotes, then
 76090   ** Z is a string literal if it doesn't match any column names.  In that
 76091   ** case, we need to return right away and not make any changes to
 76092   ** pExpr.
 76093   **
 76094   ** Because no reference was made to outer contexts, the pNC->nRef
 76095   ** fields are not changed in any context.
 76096   */
 76097   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
 76098     pExpr->op = TK_STRING;
 76099     pExpr->pTab = 0;
 76100     return WRC_Prune;
 76103   /*
 76104   ** cnt==0 means there was not match.  cnt>1 means there were two or
 76105   ** more matches.  Either way, we have an error.
 76106   */
 76107   if( cnt!=1 ){
 76108     const char *zErr;
 76109     zErr = cnt==0 ? "no such column" : "ambiguous column name";
 76110     if( zDb ){
 76111       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
 76112     }else if( zTab ){
 76113       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
 76114     }else{
 76115       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
 76117     pParse->checkSchema = 1;
 76118     pTopNC->nErr++;
 76121   /* If a column from a table in pSrcList is referenced, then record
 76122   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
 76123   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
 76124   ** column number is greater than the number of bits in the bitmask
 76125   ** then set the high-order bit of the bitmask.
 76126   */
 76127   if( pExpr->iColumn>=0 && pMatch!=0 ){
 76128     int n = pExpr->iColumn;
 76129     testcase( n==BMS-1 );
 76130     if( n>=BMS ){
 76131       n = BMS-1;
 76133     assert( pMatch->iCursor==pExpr->iTable );
 76134     pMatch->colUsed |= ((Bitmask)1)<<n;
 76137   /* Clean up and return
 76138   */
 76139   sqlite3ExprDelete(db, pExpr->pLeft);
 76140   pExpr->pLeft = 0;
 76141   sqlite3ExprDelete(db, pExpr->pRight);
 76142   pExpr->pRight = 0;
 76143   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
 76144 lookupname_end:
 76145   if( cnt==1 ){
 76146     assert( pNC!=0 );
 76147     if( pExpr->op!=TK_AS ){
 76148       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 76150     /* Increment the nRef value on all name contexts from TopNC up to
 76151     ** the point where the name matched. */
 76152     for(;;){
 76153       assert( pTopNC!=0 );
 76154       pTopNC->nRef++;
 76155       if( pTopNC==pNC ) break;
 76156       pTopNC = pTopNC->pNext;
 76158     return WRC_Prune;
 76159   } else {
 76160     return WRC_Abort;
 76164 /*
 76165 ** Allocate and return a pointer to an expression to load the column iCol
 76166 ** from datasource iSrc in SrcList pSrc.
 76167 */
 76168 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
 76169   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
 76170   if( p ){
 76171     struct SrcList_item *pItem = &pSrc->a[iSrc];
 76172     p->pTab = pItem->pTab;
 76173     p->iTable = pItem->iCursor;
 76174     if( p->pTab->iPKey==iCol ){
 76175       p->iColumn = -1;
 76176     }else{
 76177       p->iColumn = (ynVar)iCol;
 76178       testcase( iCol==BMS );
 76179       testcase( iCol==BMS-1 );
 76180       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
 76182     ExprSetProperty(p, EP_Resolved);
 76184   return p;
 76187 /*
 76188 ** Report an error that an expression is not valid for a partial index WHERE
 76189 ** clause.
 76190 */
 76191 static void notValidPartIdxWhere(
 76192   Parse *pParse,       /* Leave error message here */
 76193   NameContext *pNC,    /* The name context */
 76194   const char *zMsg     /* Type of error */
 76195 ){
 76196   if( (pNC->ncFlags & NC_PartIdx)!=0 ){
 76197     sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
 76198                     zMsg);
 76202 #ifndef SQLITE_OMIT_CHECK
 76203 /*
 76204 ** Report an error that an expression is not valid for a CHECK constraint.
 76205 */
 76206 static void notValidCheckConstraint(
 76207   Parse *pParse,       /* Leave error message here */
 76208   NameContext *pNC,    /* The name context */
 76209   const char *zMsg     /* Type of error */
 76210 ){
 76211   if( (pNC->ncFlags & NC_IsCheck)!=0 ){
 76212     sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
 76215 #else
 76216 # define notValidCheckConstraint(P,N,M)
 76217 #endif
 76219 /*
 76220 ** Expression p should encode a floating point value between 1.0 and 0.0.
 76221 ** Return 1024 times this value.  Or return -1 if p is not a floating point
 76222 ** value between 1.0 and 0.0.
 76223 */
 76224 static int exprProbability(Expr *p){
 76225   double r = -1.0;
 76226   if( p->op!=TK_FLOAT ) return -1;
 76227   sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
 76228   assert( r>=0.0 );
 76229   if( r>1.0 ) return -1;
 76230   return (int)(r*1000.0);
 76233 /*
 76234 ** This routine is callback for sqlite3WalkExpr().
 76235 **
 76236 ** Resolve symbolic names into TK_COLUMN operators for the current
 76237 ** node in the expression tree.  Return 0 to continue the search down
 76238 ** the tree or 2 to abort the tree walk.
 76239 **
 76240 ** This routine also does error checking and name resolution for
 76241 ** function names.  The operator for aggregate functions is changed
 76242 ** to TK_AGG_FUNCTION.
 76243 */
 76244 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
 76245   NameContext *pNC;
 76246   Parse *pParse;
 76248   pNC = pWalker->u.pNC;
 76249   assert( pNC!=0 );
 76250   pParse = pNC->pParse;
 76251   assert( pParse==pWalker->pParse );
 76253   if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
 76254   ExprSetProperty(pExpr, EP_Resolved);
 76255 #ifndef NDEBUG
 76256   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
 76257     SrcList *pSrcList = pNC->pSrcList;
 76258     int i;
 76259     for(i=0; i<pNC->pSrcList->nSrc; i++){
 76260       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
 76263 #endif
 76264   switch( pExpr->op ){
 76266 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 76267     /* The special operator TK_ROW means use the rowid for the first
 76268     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
 76269     ** clause processing on UPDATE and DELETE statements.
 76270     */
 76271     case TK_ROW: {
 76272       SrcList *pSrcList = pNC->pSrcList;
 76273       struct SrcList_item *pItem;
 76274       assert( pSrcList && pSrcList->nSrc==1 );
 76275       pItem = pSrcList->a; 
 76276       pExpr->op = TK_COLUMN;
 76277       pExpr->pTab = pItem->pTab;
 76278       pExpr->iTable = pItem->iCursor;
 76279       pExpr->iColumn = -1;
 76280       pExpr->affinity = SQLITE_AFF_INTEGER;
 76281       break;
 76283 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
 76285     /* A lone identifier is the name of a column.
 76286     */
 76287     case TK_ID: {
 76288       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
 76291     /* A table name and column name:     ID.ID
 76292     ** Or a database, table and column:  ID.ID.ID
 76293     */
 76294     case TK_DOT: {
 76295       const char *zColumn;
 76296       const char *zTable;
 76297       const char *zDb;
 76298       Expr *pRight;
 76300       /* if( pSrcList==0 ) break; */
 76301       pRight = pExpr->pRight;
 76302       if( pRight->op==TK_ID ){
 76303         zDb = 0;
 76304         zTable = pExpr->pLeft->u.zToken;
 76305         zColumn = pRight->u.zToken;
 76306       }else{
 76307         assert( pRight->op==TK_DOT );
 76308         zDb = pExpr->pLeft->u.zToken;
 76309         zTable = pRight->pLeft->u.zToken;
 76310         zColumn = pRight->pRight->u.zToken;
 76312       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
 76315     /* Resolve function names
 76316     */
 76317     case TK_FUNCTION: {
 76318       ExprList *pList = pExpr->x.pList;    /* The argument list */
 76319       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
 76320       int no_such_func = 0;       /* True if no such function exists */
 76321       int wrong_num_args = 0;     /* True if wrong number of arguments */
 76322       int is_agg = 0;             /* True if is an aggregate function */
 76323       int auth;                   /* Authorization to use the function */
 76324       int nId;                    /* Number of characters in function name */
 76325       const char *zId;            /* The function name. */
 76326       FuncDef *pDef;              /* Information about the function */
 76327       u8 enc = ENC(pParse->db);   /* The database encoding */
 76329       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 76330       notValidPartIdxWhere(pParse, pNC, "functions");
 76331       zId = pExpr->u.zToken;
 76332       nId = sqlite3Strlen30(zId);
 76333       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
 76334       if( pDef==0 ){
 76335         pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
 76336         if( pDef==0 ){
 76337           no_such_func = 1;
 76338         }else{
 76339           wrong_num_args = 1;
 76341       }else{
 76342         is_agg = pDef->xFunc==0;
 76343         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 76344           ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
 76345           if( n==2 ){
 76346             pExpr->iTable = exprProbability(pList->a[1].pExpr);
 76347             if( pExpr->iTable<0 ){
 76348               sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a "
 76349                                       "constant between 0.0 and 1.0");
 76350               pNC->nErr++;
 76352           }else{
 76353             /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
 76354             ** likelihood(X, 0.0625).
 76355             ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
 76356             ** likelihood(X,0.0625). */
 76357             pExpr->iTable = 62;  /* TUNING:  Default 2nd arg to unlikely() is 0.0625 */
 76361 #ifndef SQLITE_OMIT_AUTHORIZATION
 76362       if( pDef ){
 76363         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
 76364         if( auth!=SQLITE_OK ){
 76365           if( auth==SQLITE_DENY ){
 76366             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
 76367                                     pDef->zName);
 76368             pNC->nErr++;
 76370           pExpr->op = TK_NULL;
 76371           return WRC_Prune;
 76373         if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
 76375 #endif
 76376       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
 76377         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
 76378         pNC->nErr++;
 76379         is_agg = 0;
 76380       }else if( no_such_func && pParse->db->init.busy==0 ){
 76381         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
 76382         pNC->nErr++;
 76383       }else if( wrong_num_args ){
 76384         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
 76385              nId, zId);
 76386         pNC->nErr++;
 76388       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
 76389       sqlite3WalkExprList(pWalker, pList);
 76390       if( is_agg ){
 76391         NameContext *pNC2 = pNC;
 76392         pExpr->op = TK_AGG_FUNCTION;
 76393         pExpr->op2 = 0;
 76394         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
 76395           pExpr->op2++;
 76396           pNC2 = pNC2->pNext;
 76398         if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
 76399         pNC->ncFlags |= NC_AllowAgg;
 76401       /* FIX ME:  Compute pExpr->affinity based on the expected return
 76402       ** type of the function 
 76403       */
 76404       return WRC_Prune;
 76406 #ifndef SQLITE_OMIT_SUBQUERY
 76407     case TK_SELECT:
 76408     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
 76409 #endif
 76410     case TK_IN: {
 76411       testcase( pExpr->op==TK_IN );
 76412       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 76413         int nRef = pNC->nRef;
 76414         notValidCheckConstraint(pParse, pNC, "subqueries");
 76415         notValidPartIdxWhere(pParse, pNC, "subqueries");
 76416         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
 76417         assert( pNC->nRef>=nRef );
 76418         if( nRef!=pNC->nRef ){
 76419           ExprSetProperty(pExpr, EP_VarSelect);
 76422       break;
 76424     case TK_VARIABLE: {
 76425       notValidCheckConstraint(pParse, pNC, "parameters");
 76426       notValidPartIdxWhere(pParse, pNC, "parameters");
 76427       break;
 76430   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
 76433 /*
 76434 ** pEList is a list of expressions which are really the result set of the
 76435 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
 76436 ** This routine checks to see if pE is a simple identifier which corresponds
 76437 ** to the AS-name of one of the terms of the expression list.  If it is,
 76438 ** this routine return an integer between 1 and N where N is the number of
 76439 ** elements in pEList, corresponding to the matching entry.  If there is
 76440 ** no match, or if pE is not a simple identifier, then this routine
 76441 ** return 0.
 76442 **
 76443 ** pEList has been resolved.  pE has not.
 76444 */
 76445 static int resolveAsName(
 76446   Parse *pParse,     /* Parsing context for error messages */
 76447   ExprList *pEList,  /* List of expressions to scan */
 76448   Expr *pE           /* Expression we are trying to match */
 76449 ){
 76450   int i;             /* Loop counter */
 76452   UNUSED_PARAMETER(pParse);
 76454   if( pE->op==TK_ID ){
 76455     char *zCol = pE->u.zToken;
 76456     for(i=0; i<pEList->nExpr; i++){
 76457       char *zAs = pEList->a[i].zName;
 76458       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
 76459         return i+1;
 76463   return 0;
 76466 /*
 76467 ** pE is a pointer to an expression which is a single term in the
 76468 ** ORDER BY of a compound SELECT.  The expression has not been
 76469 ** name resolved.
 76470 **
 76471 ** At the point this routine is called, we already know that the
 76472 ** ORDER BY term is not an integer index into the result set.  That
 76473 ** case is handled by the calling routine.
 76474 **
 76475 ** Attempt to match pE against result set columns in the left-most
 76476 ** SELECT statement.  Return the index i of the matching column,
 76477 ** as an indication to the caller that it should sort by the i-th column.
 76478 ** The left-most column is 1.  In other words, the value returned is the
 76479 ** same integer value that would be used in the SQL statement to indicate
 76480 ** the column.
 76481 **
 76482 ** If there is no match, return 0.  Return -1 if an error occurs.
 76483 */
 76484 static int resolveOrderByTermToExprList(
 76485   Parse *pParse,     /* Parsing context for error messages */
 76486   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
 76487   Expr *pE           /* The specific ORDER BY term */
 76488 ){
 76489   int i;             /* Loop counter */
 76490   ExprList *pEList;  /* The columns of the result set */
 76491   NameContext nc;    /* Name context for resolving pE */
 76492   sqlite3 *db;       /* Database connection */
 76493   int rc;            /* Return code from subprocedures */
 76494   u8 savedSuppErr;   /* Saved value of db->suppressErr */
 76496   assert( sqlite3ExprIsInteger(pE, &i)==0 );
 76497   pEList = pSelect->pEList;
 76499   /* Resolve all names in the ORDER BY term expression
 76500   */
 76501   memset(&nc, 0, sizeof(nc));
 76502   nc.pParse = pParse;
 76503   nc.pSrcList = pSelect->pSrc;
 76504   nc.pEList = pEList;
 76505   nc.ncFlags = NC_AllowAgg;
 76506   nc.nErr = 0;
 76507   db = pParse->db;
 76508   savedSuppErr = db->suppressErr;
 76509   db->suppressErr = 1;
 76510   rc = sqlite3ResolveExprNames(&nc, pE);
 76511   db->suppressErr = savedSuppErr;
 76512   if( rc ) return 0;
 76514   /* Try to match the ORDER BY expression against an expression
 76515   ** in the result set.  Return an 1-based index of the matching
 76516   ** result-set entry.
 76517   */
 76518   for(i=0; i<pEList->nExpr; i++){
 76519     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
 76520       return i+1;
 76524   /* If no match, return 0. */
 76525   return 0;
 76528 /*
 76529 ** Generate an ORDER BY or GROUP BY term out-of-range error.
 76530 */
 76531 static void resolveOutOfRangeError(
 76532   Parse *pParse,         /* The error context into which to write the error */
 76533   const char *zType,     /* "ORDER" or "GROUP" */
 76534   int i,                 /* The index (1-based) of the term out of range */
 76535   int mx                 /* Largest permissible value of i */
 76536 ){
 76537   sqlite3ErrorMsg(pParse, 
 76538     "%r %s BY term out of range - should be "
 76539     "between 1 and %d", i, zType, mx);
 76542 /*
 76543 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
 76544 ** each term of the ORDER BY clause is a constant integer between 1
 76545 ** and N where N is the number of columns in the compound SELECT.
 76546 **
 76547 ** ORDER BY terms that are already an integer between 1 and N are
 76548 ** unmodified.  ORDER BY terms that are integers outside the range of
 76549 ** 1 through N generate an error.  ORDER BY terms that are expressions
 76550 ** are matched against result set expressions of compound SELECT
 76551 ** beginning with the left-most SELECT and working toward the right.
 76552 ** At the first match, the ORDER BY expression is transformed into
 76553 ** the integer column number.
 76554 **
 76555 ** Return the number of errors seen.
 76556 */
 76557 static int resolveCompoundOrderBy(
 76558   Parse *pParse,        /* Parsing context.  Leave error messages here */
 76559   Select *pSelect       /* The SELECT statement containing the ORDER BY */
 76560 ){
 76561   int i;
 76562   ExprList *pOrderBy;
 76563   ExprList *pEList;
 76564   sqlite3 *db;
 76565   int moreToDo = 1;
 76567   pOrderBy = pSelect->pOrderBy;
 76568   if( pOrderBy==0 ) return 0;
 76569   db = pParse->db;
 76570 #if SQLITE_MAX_COLUMN
 76571   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 76572     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
 76573     return 1;
 76575 #endif
 76576   for(i=0; i<pOrderBy->nExpr; i++){
 76577     pOrderBy->a[i].done = 0;
 76579   pSelect->pNext = 0;
 76580   while( pSelect->pPrior ){
 76581     pSelect->pPrior->pNext = pSelect;
 76582     pSelect = pSelect->pPrior;
 76584   while( pSelect && moreToDo ){
 76585     struct ExprList_item *pItem;
 76586     moreToDo = 0;
 76587     pEList = pSelect->pEList;
 76588     assert( pEList!=0 );
 76589     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 76590       int iCol = -1;
 76591       Expr *pE, *pDup;
 76592       if( pItem->done ) continue;
 76593       pE = sqlite3ExprSkipCollate(pItem->pExpr);
 76594       if( sqlite3ExprIsInteger(pE, &iCol) ){
 76595         if( iCol<=0 || iCol>pEList->nExpr ){
 76596           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
 76597           return 1;
 76599       }else{
 76600         iCol = resolveAsName(pParse, pEList, pE);
 76601         if( iCol==0 ){
 76602           pDup = sqlite3ExprDup(db, pE, 0);
 76603           if( !db->mallocFailed ){
 76604             assert(pDup);
 76605             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
 76607           sqlite3ExprDelete(db, pDup);
 76610       if( iCol>0 ){
 76611         /* Convert the ORDER BY term into an integer column number iCol,
 76612         ** taking care to preserve the COLLATE clause if it exists */
 76613         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 76614         if( pNew==0 ) return 1;
 76615         pNew->flags |= EP_IntValue;
 76616         pNew->u.iValue = iCol;
 76617         if( pItem->pExpr==pE ){
 76618           pItem->pExpr = pNew;
 76619         }else{
 76620           assert( pItem->pExpr->op==TK_COLLATE );
 76621           assert( pItem->pExpr->pLeft==pE );
 76622           pItem->pExpr->pLeft = pNew;
 76624         sqlite3ExprDelete(db, pE);
 76625         pItem->u.x.iOrderByCol = (u16)iCol;
 76626         pItem->done = 1;
 76627       }else{
 76628         moreToDo = 1;
 76631     pSelect = pSelect->pNext;
 76633   for(i=0; i<pOrderBy->nExpr; i++){
 76634     if( pOrderBy->a[i].done==0 ){
 76635       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
 76636             "column in the result set", i+1);
 76637       return 1;
 76640   return 0;
 76643 /*
 76644 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
 76645 ** the SELECT statement pSelect.  If any term is reference to a
 76646 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
 76647 ** field) then convert that term into a copy of the corresponding result set
 76648 ** column.
 76649 **
 76650 ** If any errors are detected, add an error message to pParse and
 76651 ** return non-zero.  Return zero if no errors are seen.
 76652 */
 76653 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
 76654   Parse *pParse,        /* Parsing context.  Leave error messages here */
 76655   Select *pSelect,      /* The SELECT statement containing the clause */
 76656   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
 76657   const char *zType     /* "ORDER" or "GROUP" */
 76658 ){
 76659   int i;
 76660   sqlite3 *db = pParse->db;
 76661   ExprList *pEList;
 76662   struct ExprList_item *pItem;
 76664   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
 76665 #if SQLITE_MAX_COLUMN
 76666   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 76667     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
 76668     return 1;
 76670 #endif
 76671   pEList = pSelect->pEList;
 76672   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
 76673   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 76674     if( pItem->u.x.iOrderByCol ){
 76675       if( pItem->u.x.iOrderByCol>pEList->nExpr ){
 76676         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
 76677         return 1;
 76679       resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
 76682   return 0;
 76685 /*
 76686 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
 76687 ** The Name context of the SELECT statement is pNC.  zType is either
 76688 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
 76689 **
 76690 ** This routine resolves each term of the clause into an expression.
 76691 ** If the order-by term is an integer I between 1 and N (where N is the
 76692 ** number of columns in the result set of the SELECT) then the expression
 76693 ** in the resolution is a copy of the I-th result-set expression.  If
 76694 ** the order-by term is an identifier that corresponds to the AS-name of
 76695 ** a result-set expression, then the term resolves to a copy of the
 76696 ** result-set expression.  Otherwise, the expression is resolved in
 76697 ** the usual way - using sqlite3ResolveExprNames().
 76698 **
 76699 ** This routine returns the number of errors.  If errors occur, then
 76700 ** an appropriate error message might be left in pParse.  (OOM errors
 76701 ** excepted.)
 76702 */
 76703 static int resolveOrderGroupBy(
 76704   NameContext *pNC,     /* The name context of the SELECT statement */
 76705   Select *pSelect,      /* The SELECT statement holding pOrderBy */
 76706   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
 76707   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
 76708 ){
 76709   int i, j;                      /* Loop counters */
 76710   int iCol;                      /* Column number */
 76711   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
 76712   Parse *pParse;                 /* Parsing context */
 76713   int nResult;                   /* Number of terms in the result set */
 76715   if( pOrderBy==0 ) return 0;
 76716   nResult = pSelect->pEList->nExpr;
 76717   pParse = pNC->pParse;
 76718   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
 76719     Expr *pE = pItem->pExpr;
 76720     Expr *pE2 = sqlite3ExprSkipCollate(pE);
 76721     if( zType[0]!='G' ){
 76722       iCol = resolveAsName(pParse, pSelect->pEList, pE2);
 76723       if( iCol>0 ){
 76724         /* If an AS-name match is found, mark this ORDER BY column as being
 76725         ** a copy of the iCol-th result-set column.  The subsequent call to
 76726         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
 76727         ** copy of the iCol-th result-set expression. */
 76728         pItem->u.x.iOrderByCol = (u16)iCol;
 76729         continue;
 76732     if( sqlite3ExprIsInteger(pE2, &iCol) ){
 76733       /* The ORDER BY term is an integer constant.  Again, set the column
 76734       ** number so that sqlite3ResolveOrderGroupBy() will convert the
 76735       ** order-by term to a copy of the result-set expression */
 76736       if( iCol<1 || iCol>0xffff ){
 76737         resolveOutOfRangeError(pParse, zType, i+1, nResult);
 76738         return 1;
 76740       pItem->u.x.iOrderByCol = (u16)iCol;
 76741       continue;
 76744     /* Otherwise, treat the ORDER BY term as an ordinary expression */
 76745     pItem->u.x.iOrderByCol = 0;
 76746     if( sqlite3ResolveExprNames(pNC, pE) ){
 76747       return 1;
 76749     for(j=0; j<pSelect->pEList->nExpr; j++){
 76750       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
 76751         pItem->u.x.iOrderByCol = j+1;
 76755   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
 76758 /*
 76759 ** Resolve names in the SELECT statement p and all of its descendents.
 76760 */
 76761 static int resolveSelectStep(Walker *pWalker, Select *p){
 76762   NameContext *pOuterNC;  /* Context that contains this SELECT */
 76763   NameContext sNC;        /* Name context of this SELECT */
 76764   int isCompound;         /* True if p is a compound select */
 76765   int nCompound;          /* Number of compound terms processed so far */
 76766   Parse *pParse;          /* Parsing context */
 76767   ExprList *pEList;       /* Result set expression list */
 76768   int i;                  /* Loop counter */
 76769   ExprList *pGroupBy;     /* The GROUP BY clause */
 76770   Select *pLeftmost;      /* Left-most of SELECT of a compound */
 76771   sqlite3 *db;            /* Database connection */
 76774   assert( p!=0 );
 76775   if( p->selFlags & SF_Resolved ){
 76776     return WRC_Prune;
 76778   pOuterNC = pWalker->u.pNC;
 76779   pParse = pWalker->pParse;
 76780   db = pParse->db;
 76782   /* Normally sqlite3SelectExpand() will be called first and will have
 76783   ** already expanded this SELECT.  However, if this is a subquery within
 76784   ** an expression, sqlite3ResolveExprNames() will be called without a
 76785   ** prior call to sqlite3SelectExpand().  When that happens, let
 76786   ** sqlite3SelectPrep() do all of the processing for this SELECT.
 76787   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
 76788   ** this routine in the correct order.
 76789   */
 76790   if( (p->selFlags & SF_Expanded)==0 ){
 76791     sqlite3SelectPrep(pParse, p, pOuterNC);
 76792     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
 76795   isCompound = p->pPrior!=0;
 76796   nCompound = 0;
 76797   pLeftmost = p;
 76798   while( p ){
 76799     assert( (p->selFlags & SF_Expanded)!=0 );
 76800     assert( (p->selFlags & SF_Resolved)==0 );
 76801     p->selFlags |= SF_Resolved;
 76803     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
 76804     ** are not allowed to refer to any names, so pass an empty NameContext.
 76805     */
 76806     memset(&sNC, 0, sizeof(sNC));
 76807     sNC.pParse = pParse;
 76808     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 76809         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
 76810       return WRC_Abort;
 76813     /* Recursively resolve names in all subqueries
 76814     */
 76815     for(i=0; i<p->pSrc->nSrc; i++){
 76816       struct SrcList_item *pItem = &p->pSrc->a[i];
 76817       if( pItem->pSelect ){
 76818         NameContext *pNC;         /* Used to iterate name contexts */
 76819         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
 76820         const char *zSavedContext = pParse->zAuthContext;
 76822         /* Count the total number of references to pOuterNC and all of its
 76823         ** parent contexts. After resolving references to expressions in
 76824         ** pItem->pSelect, check if this value has changed. If so, then
 76825         ** SELECT statement pItem->pSelect must be correlated. Set the
 76826         ** pItem->isCorrelated flag if this is the case. */
 76827         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
 76829         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
 76830         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
 76831         pParse->zAuthContext = zSavedContext;
 76832         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
 76834         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
 76835         assert( pItem->isCorrelated==0 && nRef<=0 );
 76836         pItem->isCorrelated = (nRef!=0);
 76840     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
 76841     ** resolve the result-set expression list.
 76842     */
 76843     sNC.ncFlags = NC_AllowAgg;
 76844     sNC.pSrcList = p->pSrc;
 76845     sNC.pNext = pOuterNC;
 76847     /* Resolve names in the result set. */
 76848     pEList = p->pEList;
 76849     assert( pEList!=0 );
 76850     for(i=0; i<pEList->nExpr; i++){
 76851       Expr *pX = pEList->a[i].pExpr;
 76852       if( sqlite3ResolveExprNames(&sNC, pX) ){
 76853         return WRC_Abort;
 76857     /* If there are no aggregate functions in the result-set, and no GROUP BY 
 76858     ** expression, do not allow aggregates in any of the other expressions.
 76859     */
 76860     assert( (p->selFlags & SF_Aggregate)==0 );
 76861     pGroupBy = p->pGroupBy;
 76862     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
 76863       p->selFlags |= SF_Aggregate;
 76864     }else{
 76865       sNC.ncFlags &= ~NC_AllowAgg;
 76868     /* If a HAVING clause is present, then there must be a GROUP BY clause.
 76869     */
 76870     if( p->pHaving && !pGroupBy ){
 76871       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
 76872       return WRC_Abort;
 76875     /* Add the output column list to the name-context before parsing the
 76876     ** other expressions in the SELECT statement. This is so that
 76877     ** expressions in the WHERE clause (etc.) can refer to expressions by
 76878     ** aliases in the result set.
 76879     **
 76880     ** Minor point: If this is the case, then the expression will be
 76881     ** re-evaluated for each reference to it.
 76882     */
 76883     sNC.pEList = p->pEList;
 76884     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
 76885     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
 76887     /* The ORDER BY and GROUP BY clauses may not refer to terms in
 76888     ** outer queries 
 76889     */
 76890     sNC.pNext = 0;
 76891     sNC.ncFlags |= NC_AllowAgg;
 76893     /* Process the ORDER BY clause for singleton SELECT statements.
 76894     ** The ORDER BY clause for compounds SELECT statements is handled
 76895     ** below, after all of the result-sets for all of the elements of
 76896     ** the compound have been resolved.
 76897     */
 76898     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
 76899       return WRC_Abort;
 76901     if( db->mallocFailed ){
 76902       return WRC_Abort;
 76905     /* Resolve the GROUP BY clause.  At the same time, make sure 
 76906     ** the GROUP BY clause does not contain aggregate functions.
 76907     */
 76908     if( pGroupBy ){
 76909       struct ExprList_item *pItem;
 76911       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
 76912         return WRC_Abort;
 76914       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
 76915         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
 76916           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
 76917               "the GROUP BY clause");
 76918           return WRC_Abort;
 76923     /* Advance to the next term of the compound
 76924     */
 76925     p = p->pPrior;
 76926     nCompound++;
 76929   /* Resolve the ORDER BY on a compound SELECT after all terms of
 76930   ** the compound have been resolved.
 76931   */
 76932   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
 76933     return WRC_Abort;
 76936   return WRC_Prune;
 76939 /*
 76940 ** This routine walks an expression tree and resolves references to
 76941 ** table columns and result-set columns.  At the same time, do error
 76942 ** checking on function usage and set a flag if any aggregate functions
 76943 ** are seen.
 76944 **
 76945 ** To resolve table columns references we look for nodes (or subtrees) of the 
 76946 ** form X.Y.Z or Y.Z or just Z where
 76947 **
 76948 **      X:   The name of a database.  Ex:  "main" or "temp" or
 76949 **           the symbolic name assigned to an ATTACH-ed database.
 76950 **
 76951 **      Y:   The name of a table in a FROM clause.  Or in a trigger
 76952 **           one of the special names "old" or "new".
 76953 **
 76954 **      Z:   The name of a column in table Y.
 76955 **
 76956 ** The node at the root of the subtree is modified as follows:
 76957 **
 76958 **    Expr.op        Changed to TK_COLUMN
 76959 **    Expr.pTab      Points to the Table object for X.Y
 76960 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
 76961 **    Expr.iTable    The VDBE cursor number for X.Y
 76962 **
 76963 **
 76964 ** To resolve result-set references, look for expression nodes of the
 76965 ** form Z (with no X and Y prefix) where the Z matches the right-hand
 76966 ** size of an AS clause in the result-set of a SELECT.  The Z expression
 76967 ** is replaced by a copy of the left-hand side of the result-set expression.
 76968 ** Table-name and function resolution occurs on the substituted expression
 76969 ** tree.  For example, in:
 76970 **
 76971 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
 76972 **
 76973 ** The "x" term of the order by is replaced by "a+b" to render:
 76974 **
 76975 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
 76976 **
 76977 ** Function calls are checked to make sure that the function is 
 76978 ** defined and that the correct number of arguments are specified.
 76979 ** If the function is an aggregate function, then the NC_HasAgg flag is
 76980 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
 76981 ** If an expression contains aggregate functions then the EP_Agg
 76982 ** property on the expression is set.
 76983 **
 76984 ** An error message is left in pParse if anything is amiss.  The number
 76985 ** if errors is returned.
 76986 */
 76987 SQLITE_PRIVATE int sqlite3ResolveExprNames( 
 76988   NameContext *pNC,       /* Namespace to resolve expressions in. */
 76989   Expr *pExpr             /* The expression to be analyzed. */
 76990 ){
 76991   u8 savedHasAgg;
 76992   Walker w;
 76994   if( pExpr==0 ) return 0;
 76995 #if SQLITE_MAX_EXPR_DEPTH>0
 76997     Parse *pParse = pNC->pParse;
 76998     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
 76999       return 1;
 77001     pParse->nHeight += pExpr->nHeight;
 77003 #endif
 77004   savedHasAgg = pNC->ncFlags & NC_HasAgg;
 77005   pNC->ncFlags &= ~NC_HasAgg;
 77006   memset(&w, 0, sizeof(w));
 77007   w.xExprCallback = resolveExprStep;
 77008   w.xSelectCallback = resolveSelectStep;
 77009   w.pParse = pNC->pParse;
 77010   w.u.pNC = pNC;
 77011   sqlite3WalkExpr(&w, pExpr);
 77012 #if SQLITE_MAX_EXPR_DEPTH>0
 77013   pNC->pParse->nHeight -= pExpr->nHeight;
 77014 #endif
 77015   if( pNC->nErr>0 || w.pParse->nErr>0 ){
 77016     ExprSetProperty(pExpr, EP_Error);
 77018   if( pNC->ncFlags & NC_HasAgg ){
 77019     ExprSetProperty(pExpr, EP_Agg);
 77020   }else if( savedHasAgg ){
 77021     pNC->ncFlags |= NC_HasAgg;
 77023   return ExprHasProperty(pExpr, EP_Error);
 77027 /*
 77028 ** Resolve all names in all expressions of a SELECT and in all
 77029 ** decendents of the SELECT, including compounds off of p->pPrior,
 77030 ** subqueries in expressions, and subqueries used as FROM clause
 77031 ** terms.
 77032 **
 77033 ** See sqlite3ResolveExprNames() for a description of the kinds of
 77034 ** transformations that occur.
 77035 **
 77036 ** All SELECT statements should have been expanded using
 77037 ** sqlite3SelectExpand() prior to invoking this routine.
 77038 */
 77039 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
 77040   Parse *pParse,         /* The parser context */
 77041   Select *p,             /* The SELECT statement being coded. */
 77042   NameContext *pOuterNC  /* Name context for parent SELECT statement */
 77043 ){
 77044   Walker w;
 77046   assert( p!=0 );
 77047   memset(&w, 0, sizeof(w));
 77048   w.xExprCallback = resolveExprStep;
 77049   w.xSelectCallback = resolveSelectStep;
 77050   w.pParse = pParse;
 77051   w.u.pNC = pOuterNC;
 77052   sqlite3WalkSelect(&w, p);
 77055 /*
 77056 ** Resolve names in expressions that can only reference a single table:
 77057 **
 77058 **    *   CHECK constraints
 77059 **    *   WHERE clauses on partial indices
 77060 **
 77061 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
 77062 ** is set to -1 and the Expr.iColumn value is set to the column number.
 77063 **
 77064 ** Any errors cause an error message to be set in pParse.
 77065 */
 77066 SQLITE_PRIVATE void sqlite3ResolveSelfReference(
 77067   Parse *pParse,      /* Parsing context */
 77068   Table *pTab,        /* The table being referenced */
 77069   int type,           /* NC_IsCheck or NC_PartIdx */
 77070   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
 77071   ExprList *pList     /* Expression list to resolve.  May be NUL. */
 77072 ){
 77073   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
 77074   NameContext sNC;                /* Name context for pParse->pNewTable */
 77075   int i;                          /* Loop counter */
 77077   assert( type==NC_IsCheck || type==NC_PartIdx );
 77078   memset(&sNC, 0, sizeof(sNC));
 77079   memset(&sSrc, 0, sizeof(sSrc));
 77080   sSrc.nSrc = 1;
 77081   sSrc.a[0].zName = pTab->zName;
 77082   sSrc.a[0].pTab = pTab;
 77083   sSrc.a[0].iCursor = -1;
 77084   sNC.pParse = pParse;
 77085   sNC.pSrcList = &sSrc;
 77086   sNC.ncFlags = type;
 77087   if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
 77088   if( pList ){
 77089     for(i=0; i<pList->nExpr; i++){
 77090       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 77091         return;
 77097 /************** End of resolve.c *********************************************/
 77098 /************** Begin file expr.c ********************************************/
 77099 /*
 77100 ** 2001 September 15
 77101 **
 77102 ** The author disclaims copyright to this source code.  In place of
 77103 ** a legal notice, here is a blessing:
 77104 **
 77105 **    May you do good and not evil.
 77106 **    May you find forgiveness for yourself and forgive others.
 77107 **    May you share freely, never taking more than you give.
 77108 **
 77109 *************************************************************************
 77110 ** This file contains routines used for analyzing expressions and
 77111 ** for generating VDBE code that evaluates expressions in SQLite.
 77112 */
 77114 /*
 77115 ** Return the 'affinity' of the expression pExpr if any.
 77116 **
 77117 ** If pExpr is a column, a reference to a column via an 'AS' alias,
 77118 ** or a sub-select with a column as the return value, then the 
 77119 ** affinity of that column is returned. Otherwise, 0x00 is returned,
 77120 ** indicating no affinity for the expression.
 77121 **
 77122 ** i.e. the WHERE clause expresssions in the following statements all
 77123 ** have an affinity:
 77124 **
 77125 ** CREATE TABLE t1(a);
 77126 ** SELECT * FROM t1 WHERE a;
 77127 ** SELECT a AS b FROM t1 WHERE b;
 77128 ** SELECT * FROM t1 WHERE (select a from t1);
 77129 */
 77130 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
 77131   int op;
 77132   pExpr = sqlite3ExprSkipCollate(pExpr);
 77133   op = pExpr->op;
 77134   if( op==TK_SELECT ){
 77135     assert( pExpr->flags&EP_xIsSelect );
 77136     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
 77138 #ifndef SQLITE_OMIT_CAST
 77139   if( op==TK_CAST ){
 77140     assert( !ExprHasProperty(pExpr, EP_IntValue) );
 77141     return sqlite3AffinityType(pExpr->u.zToken, 0);
 77143 #endif
 77144   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
 77145    && pExpr->pTab!=0
 77146   ){
 77147     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
 77148     ** a TK_COLUMN but was previously evaluated and cached in a register */
 77149     int j = pExpr->iColumn;
 77150     if( j<0 ) return SQLITE_AFF_INTEGER;
 77151     assert( pExpr->pTab && j<pExpr->pTab->nCol );
 77152     return pExpr->pTab->aCol[j].affinity;
 77154   return pExpr->affinity;
 77157 /*
 77158 ** Set the collating sequence for expression pExpr to be the collating
 77159 ** sequence named by pToken.   Return a pointer to a new Expr node that
 77160 ** implements the COLLATE operator.
 77161 **
 77162 ** If a memory allocation error occurs, that fact is recorded in pParse->db
 77163 ** and the pExpr parameter is returned unchanged.
 77164 */
 77165 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
 77166   if( pCollName->n>0 ){
 77167     Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
 77168     if( pNew ){
 77169       pNew->pLeft = pExpr;
 77170       pNew->flags |= EP_Collate|EP_Skip;
 77171       pExpr = pNew;
 77174   return pExpr;
 77176 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
 77177   Token s;
 77178   assert( zC!=0 );
 77179   s.z = zC;
 77180   s.n = sqlite3Strlen30(s.z);
 77181   return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
 77184 /*
 77185 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
 77186 ** or likelihood() function at the root of an expression.
 77187 */
 77188 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
 77189   while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
 77190     if( ExprHasProperty(pExpr, EP_Unlikely) ){
 77191       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 77192       assert( pExpr->x.pList->nExpr>0 );
 77193       assert( pExpr->op==TK_FUNCTION );
 77194       pExpr = pExpr->x.pList->a[0].pExpr;
 77195     }else{
 77196       assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
 77197       pExpr = pExpr->pLeft;
 77200   return pExpr;
 77203 /*
 77204 ** Return the collation sequence for the expression pExpr. If
 77205 ** there is no defined collating sequence, return NULL.
 77206 **
 77207 ** The collating sequence might be determined by a COLLATE operator
 77208 ** or by the presence of a column with a defined collating sequence.
 77209 ** COLLATE operators take first precedence.  Left operands take
 77210 ** precedence over right operands.
 77211 */
 77212 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 77213   sqlite3 *db = pParse->db;
 77214   CollSeq *pColl = 0;
 77215   Expr *p = pExpr;
 77216   while( p ){
 77217     int op = p->op;
 77218     if( op==TK_CAST || op==TK_UPLUS ){
 77219       p = p->pLeft;
 77220       continue;
 77222     if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
 77223       pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
 77224       break;
 77226     if( p->pTab!=0
 77227      && (op==TK_AGG_COLUMN || op==TK_COLUMN
 77228           || op==TK_REGISTER || op==TK_TRIGGER)
 77229     ){
 77230       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
 77231       ** a TK_COLUMN but was previously evaluated and cached in a register */
 77232       int j = p->iColumn;
 77233       if( j>=0 ){
 77234         const char *zColl = p->pTab->aCol[j].zColl;
 77235         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 77237       break;
 77239     if( p->flags & EP_Collate ){
 77240       if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
 77241         p = p->pLeft;
 77242       }else{
 77243         p = p->pRight;
 77245     }else{
 77246       break;
 77249   if( sqlite3CheckCollSeq(pParse, pColl) ){ 
 77250     pColl = 0;
 77252   return pColl;
 77255 /*
 77256 ** pExpr is an operand of a comparison operator.  aff2 is the
 77257 ** type affinity of the other operand.  This routine returns the
 77258 ** type affinity that should be used for the comparison operator.
 77259 */
 77260 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
 77261   char aff1 = sqlite3ExprAffinity(pExpr);
 77262   if( aff1 && aff2 ){
 77263     /* Both sides of the comparison are columns. If one has numeric
 77264     ** affinity, use that. Otherwise use no affinity.
 77265     */
 77266     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
 77267       return SQLITE_AFF_NUMERIC;
 77268     }else{
 77269       return SQLITE_AFF_NONE;
 77271   }else if( !aff1 && !aff2 ){
 77272     /* Neither side of the comparison is a column.  Compare the
 77273     ** results directly.
 77274     */
 77275     return SQLITE_AFF_NONE;
 77276   }else{
 77277     /* One side is a column, the other is not. Use the columns affinity. */
 77278     assert( aff1==0 || aff2==0 );
 77279     return (aff1 + aff2);
 77283 /*
 77284 ** pExpr is a comparison operator.  Return the type affinity that should
 77285 ** be applied to both operands prior to doing the comparison.
 77286 */
 77287 static char comparisonAffinity(Expr *pExpr){
 77288   char aff;
 77289   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
 77290           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
 77291           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
 77292   assert( pExpr->pLeft );
 77293   aff = sqlite3ExprAffinity(pExpr->pLeft);
 77294   if( pExpr->pRight ){
 77295     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
 77296   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 77297     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
 77298   }else if( !aff ){
 77299     aff = SQLITE_AFF_NONE;
 77301   return aff;
 77304 /*
 77305 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
 77306 ** idx_affinity is the affinity of an indexed column. Return true
 77307 ** if the index with affinity idx_affinity may be used to implement
 77308 ** the comparison in pExpr.
 77309 */
 77310 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
 77311   char aff = comparisonAffinity(pExpr);
 77312   switch( aff ){
 77313     case SQLITE_AFF_NONE:
 77314       return 1;
 77315     case SQLITE_AFF_TEXT:
 77316       return idx_affinity==SQLITE_AFF_TEXT;
 77317     default:
 77318       return sqlite3IsNumericAffinity(idx_affinity);
 77322 /*
 77323 ** Return the P5 value that should be used for a binary comparison
 77324 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
 77325 */
 77326 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
 77327   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
 77328   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
 77329   return aff;
 77332 /*
 77333 ** Return a pointer to the collation sequence that should be used by
 77334 ** a binary comparison operator comparing pLeft and pRight.
 77335 **
 77336 ** If the left hand expression has a collating sequence type, then it is
 77337 ** used. Otherwise the collation sequence for the right hand expression
 77338 ** is used, or the default (BINARY) if neither expression has a collating
 77339 ** type.
 77340 **
 77341 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
 77342 ** it is not considered.
 77343 */
 77344 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
 77345   Parse *pParse, 
 77346   Expr *pLeft, 
 77347   Expr *pRight
 77348 ){
 77349   CollSeq *pColl;
 77350   assert( pLeft );
 77351   if( pLeft->flags & EP_Collate ){
 77352     pColl = sqlite3ExprCollSeq(pParse, pLeft);
 77353   }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
 77354     pColl = sqlite3ExprCollSeq(pParse, pRight);
 77355   }else{
 77356     pColl = sqlite3ExprCollSeq(pParse, pLeft);
 77357     if( !pColl ){
 77358       pColl = sqlite3ExprCollSeq(pParse, pRight);
 77361   return pColl;
 77364 /*
 77365 ** Generate code for a comparison operator.
 77366 */
 77367 static int codeCompare(
 77368   Parse *pParse,    /* The parsing (and code generating) context */
 77369   Expr *pLeft,      /* The left operand */
 77370   Expr *pRight,     /* The right operand */
 77371   int opcode,       /* The comparison opcode */
 77372   int in1, int in2, /* Register holding operands */
 77373   int dest,         /* Jump here if true.  */
 77374   int jumpIfNull    /* If true, jump if either operand is NULL */
 77375 ){
 77376   int p5;
 77377   int addr;
 77378   CollSeq *p4;
 77380   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
 77381   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
 77382   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
 77383                            (void*)p4, P4_COLLSEQ);
 77384   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
 77385   return addr;
 77388 #if SQLITE_MAX_EXPR_DEPTH>0
 77389 /*
 77390 ** Check that argument nHeight is less than or equal to the maximum
 77391 ** expression depth allowed. If it is not, leave an error message in
 77392 ** pParse.
 77393 */
 77394 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
 77395   int rc = SQLITE_OK;
 77396   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
 77397   if( nHeight>mxHeight ){
 77398     sqlite3ErrorMsg(pParse, 
 77399        "Expression tree is too large (maximum depth %d)", mxHeight
 77400     );
 77401     rc = SQLITE_ERROR;
 77403   return rc;
 77406 /* The following three functions, heightOfExpr(), heightOfExprList()
 77407 ** and heightOfSelect(), are used to determine the maximum height
 77408 ** of any expression tree referenced by the structure passed as the
 77409 ** first argument.
 77410 **
 77411 ** If this maximum height is greater than the current value pointed
 77412 ** to by pnHeight, the second parameter, then set *pnHeight to that
 77413 ** value.
 77414 */
 77415 static void heightOfExpr(Expr *p, int *pnHeight){
 77416   if( p ){
 77417     if( p->nHeight>*pnHeight ){
 77418       *pnHeight = p->nHeight;
 77422 static void heightOfExprList(ExprList *p, int *pnHeight){
 77423   if( p ){
 77424     int i;
 77425     for(i=0; i<p->nExpr; i++){
 77426       heightOfExpr(p->a[i].pExpr, pnHeight);
 77430 static void heightOfSelect(Select *p, int *pnHeight){
 77431   if( p ){
 77432     heightOfExpr(p->pWhere, pnHeight);
 77433     heightOfExpr(p->pHaving, pnHeight);
 77434     heightOfExpr(p->pLimit, pnHeight);
 77435     heightOfExpr(p->pOffset, pnHeight);
 77436     heightOfExprList(p->pEList, pnHeight);
 77437     heightOfExprList(p->pGroupBy, pnHeight);
 77438     heightOfExprList(p->pOrderBy, pnHeight);
 77439     heightOfSelect(p->pPrior, pnHeight);
 77443 /*
 77444 ** Set the Expr.nHeight variable in the structure passed as an 
 77445 ** argument. An expression with no children, Expr.pList or 
 77446 ** Expr.pSelect member has a height of 1. Any other expression
 77447 ** has a height equal to the maximum height of any other 
 77448 ** referenced Expr plus one.
 77449 */
 77450 static void exprSetHeight(Expr *p){
 77451   int nHeight = 0;
 77452   heightOfExpr(p->pLeft, &nHeight);
 77453   heightOfExpr(p->pRight, &nHeight);
 77454   if( ExprHasProperty(p, EP_xIsSelect) ){
 77455     heightOfSelect(p->x.pSelect, &nHeight);
 77456   }else{
 77457     heightOfExprList(p->x.pList, &nHeight);
 77459   p->nHeight = nHeight + 1;
 77462 /*
 77463 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
 77464 ** the height is greater than the maximum allowed expression depth,
 77465 ** leave an error in pParse.
 77466 */
 77467 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
 77468   exprSetHeight(p);
 77469   sqlite3ExprCheckHeight(pParse, p->nHeight);
 77472 /*
 77473 ** Return the maximum height of any expression tree referenced
 77474 ** by the select statement passed as an argument.
 77475 */
 77476 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
 77477   int nHeight = 0;
 77478   heightOfSelect(p, &nHeight);
 77479   return nHeight;
 77481 #else
 77482   #define exprSetHeight(y)
 77483 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
 77485 /*
 77486 ** This routine is the core allocator for Expr nodes.
 77487 **
 77488 ** Construct a new expression node and return a pointer to it.  Memory
 77489 ** for this node and for the pToken argument is a single allocation
 77490 ** obtained from sqlite3DbMalloc().  The calling function
 77491 ** is responsible for making sure the node eventually gets freed.
 77492 **
 77493 ** If dequote is true, then the token (if it exists) is dequoted.
 77494 ** If dequote is false, no dequoting is performance.  The deQuote
 77495 ** parameter is ignored if pToken is NULL or if the token does not
 77496 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
 77497 ** then the EP_DblQuoted flag is set on the expression node.
 77498 **
 77499 ** Special case:  If op==TK_INTEGER and pToken points to a string that
 77500 ** can be translated into a 32-bit integer, then the token is not
 77501 ** stored in u.zToken.  Instead, the integer values is written
 77502 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
 77503 ** is allocated to hold the integer text and the dequote flag is ignored.
 77504 */
 77505 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
 77506   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 77507   int op,                 /* Expression opcode */
 77508   const Token *pToken,    /* Token argument.  Might be NULL */
 77509   int dequote             /* True to dequote */
 77510 ){
 77511   Expr *pNew;
 77512   int nExtra = 0;
 77513   int iValue = 0;
 77515   if( pToken ){
 77516     if( op!=TK_INTEGER || pToken->z==0
 77517           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
 77518       nExtra = pToken->n+1;
 77519       assert( iValue>=0 );
 77522   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
 77523   if( pNew ){
 77524     pNew->op = (u8)op;
 77525     pNew->iAgg = -1;
 77526     if( pToken ){
 77527       if( nExtra==0 ){
 77528         pNew->flags |= EP_IntValue;
 77529         pNew->u.iValue = iValue;
 77530       }else{
 77531         int c;
 77532         pNew->u.zToken = (char*)&pNew[1];
 77533         assert( pToken->z!=0 || pToken->n==0 );
 77534         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
 77535         pNew->u.zToken[pToken->n] = 0;
 77536         if( dequote && nExtra>=3 
 77537              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
 77538           sqlite3Dequote(pNew->u.zToken);
 77539           if( c=='"' ) pNew->flags |= EP_DblQuoted;
 77543 #if SQLITE_MAX_EXPR_DEPTH>0
 77544     pNew->nHeight = 1;
 77545 #endif  
 77547   return pNew;
 77550 /*
 77551 ** Allocate a new expression node from a zero-terminated token that has
 77552 ** already been dequoted.
 77553 */
 77554 SQLITE_PRIVATE Expr *sqlite3Expr(
 77555   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
 77556   int op,                 /* Expression opcode */
 77557   const char *zToken      /* Token argument.  Might be NULL */
 77558 ){
 77559   Token x;
 77560   x.z = zToken;
 77561   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
 77562   return sqlite3ExprAlloc(db, op, &x, 0);
 77565 /*
 77566 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
 77567 **
 77568 ** If pRoot==NULL that means that a memory allocation error has occurred.
 77569 ** In that case, delete the subtrees pLeft and pRight.
 77570 */
 77571 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
 77572   sqlite3 *db,
 77573   Expr *pRoot,
 77574   Expr *pLeft,
 77575   Expr *pRight
 77576 ){
 77577   if( pRoot==0 ){
 77578     assert( db->mallocFailed );
 77579     sqlite3ExprDelete(db, pLeft);
 77580     sqlite3ExprDelete(db, pRight);
 77581   }else{
 77582     if( pRight ){
 77583       pRoot->pRight = pRight;
 77584       pRoot->flags |= EP_Collate & pRight->flags;
 77586     if( pLeft ){
 77587       pRoot->pLeft = pLeft;
 77588       pRoot->flags |= EP_Collate & pLeft->flags;
 77590     exprSetHeight(pRoot);
 77594 /*
 77595 ** Allocate a Expr node which joins as many as two subtrees.
 77596 **
 77597 ** One or both of the subtrees can be NULL.  Return a pointer to the new
 77598 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
 77599 ** free the subtrees and return NULL.
 77600 */
 77601 SQLITE_PRIVATE Expr *sqlite3PExpr(
 77602   Parse *pParse,          /* Parsing context */
 77603   int op,                 /* Expression opcode */
 77604   Expr *pLeft,            /* Left operand */
 77605   Expr *pRight,           /* Right operand */
 77606   const Token *pToken     /* Argument token */
 77607 ){
 77608   Expr *p;
 77609   if( op==TK_AND && pLeft && pRight ){
 77610     /* Take advantage of short-circuit false optimization for AND */
 77611     p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
 77612   }else{
 77613     p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
 77614     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
 77616   if( p ) {
 77617     sqlite3ExprCheckHeight(pParse, p->nHeight);
 77619   return p;
 77622 /*
 77623 ** If the expression is always either TRUE or FALSE (respectively),
 77624 ** then return 1.  If one cannot determine the truth value of the
 77625 ** expression at compile-time return 0.
 77626 **
 77627 ** This is an optimization.  If is OK to return 0 here even if
 77628 ** the expression really is always false or false (a false negative).
 77629 ** But it is a bug to return 1 if the expression might have different
 77630 ** boolean values in different circumstances (a false positive.)
 77631 **
 77632 ** Note that if the expression is part of conditional for a
 77633 ** LEFT JOIN, then we cannot determine at compile-time whether or not
 77634 ** is it true or false, so always return 0.
 77635 */
 77636 static int exprAlwaysTrue(Expr *p){
 77637   int v = 0;
 77638   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
 77639   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
 77640   return v!=0;
 77642 static int exprAlwaysFalse(Expr *p){
 77643   int v = 0;
 77644   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
 77645   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
 77646   return v==0;
 77649 /*
 77650 ** Join two expressions using an AND operator.  If either expression is
 77651 ** NULL, then just return the other expression.
 77652 **
 77653 ** If one side or the other of the AND is known to be false, then instead
 77654 ** of returning an AND expression, just return a constant expression with
 77655 ** a value of false.
 77656 */
 77657 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
 77658   if( pLeft==0 ){
 77659     return pRight;
 77660   }else if( pRight==0 ){
 77661     return pLeft;
 77662   }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
 77663     sqlite3ExprDelete(db, pLeft);
 77664     sqlite3ExprDelete(db, pRight);
 77665     return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
 77666   }else{
 77667     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
 77668     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
 77669     return pNew;
 77673 /*
 77674 ** Construct a new expression node for a function with multiple
 77675 ** arguments.
 77676 */
 77677 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
 77678   Expr *pNew;
 77679   sqlite3 *db = pParse->db;
 77680   assert( pToken );
 77681   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
 77682   if( pNew==0 ){
 77683     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
 77684     return 0;
 77686   pNew->x.pList = pList;
 77687   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 77688   sqlite3ExprSetHeight(pParse, pNew);
 77689   return pNew;
 77692 /*
 77693 ** Assign a variable number to an expression that encodes a wildcard
 77694 ** in the original SQL statement.  
 77695 **
 77696 ** Wildcards consisting of a single "?" are assigned the next sequential
 77697 ** variable number.
 77698 **
 77699 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
 77700 ** sure "nnn" is not too be to avoid a denial of service attack when
 77701 ** the SQL statement comes from an external source.
 77702 **
 77703 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
 77704 ** as the previous instance of the same wildcard.  Or if this is the first
 77705 ** instance of the wildcard, the next sequenial variable number is
 77706 ** assigned.
 77707 */
 77708 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
 77709   sqlite3 *db = pParse->db;
 77710   const char *z;
 77712   if( pExpr==0 ) return;
 77713   assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
 77714   z = pExpr->u.zToken;
 77715   assert( z!=0 );
 77716   assert( z[0]!=0 );
 77717   if( z[1]==0 ){
 77718     /* Wildcard of the form "?".  Assign the next variable number */
 77719     assert( z[0]=='?' );
 77720     pExpr->iColumn = (ynVar)(++pParse->nVar);
 77721   }else{
 77722     ynVar x = 0;
 77723     u32 n = sqlite3Strlen30(z);
 77724     if( z[0]=='?' ){
 77725       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
 77726       ** use it as the variable number */
 77727       i64 i;
 77728       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
 77729       pExpr->iColumn = x = (ynVar)i;
 77730       testcase( i==0 );
 77731       testcase( i==1 );
 77732       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
 77733       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
 77734       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 77735         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
 77736             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
 77737         x = 0;
 77739       if( i>pParse->nVar ){
 77740         pParse->nVar = (int)i;
 77742     }else{
 77743       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
 77744       ** number as the prior appearance of the same name, or if the name
 77745       ** has never appeared before, reuse the same variable number
 77746       */
 77747       ynVar i;
 77748       for(i=0; i<pParse->nzVar; i++){
 77749         if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
 77750           pExpr->iColumn = x = (ynVar)i+1;
 77751           break;
 77754       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
 77756     if( x>0 ){
 77757       if( x>pParse->nzVar ){
 77758         char **a;
 77759         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
 77760         if( a==0 ) return;  /* Error reported through db->mallocFailed */
 77761         pParse->azVar = a;
 77762         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
 77763         pParse->nzVar = x;
 77765       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
 77766         sqlite3DbFree(db, pParse->azVar[x-1]);
 77767         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
 77771   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
 77772     sqlite3ErrorMsg(pParse, "too many SQL variables");
 77776 /*
 77777 ** Recursively delete an expression tree.
 77778 */
 77779 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 77780   if( p==0 ) return;
 77781   /* Sanity check: Assert that the IntValue is non-negative if it exists */
 77782   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 77783   if( !ExprHasProperty(p, EP_TokenOnly) ){
 77784     /* The Expr.x union is never used at the same time as Expr.pRight */
 77785     assert( p->x.pList==0 || p->pRight==0 );
 77786     sqlite3ExprDelete(db, p->pLeft);
 77787     sqlite3ExprDelete(db, p->pRight);
 77788     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
 77789     if( ExprHasProperty(p, EP_xIsSelect) ){
 77790       sqlite3SelectDelete(db, p->x.pSelect);
 77791     }else{
 77792       sqlite3ExprListDelete(db, p->x.pList);
 77795   if( !ExprHasProperty(p, EP_Static) ){
 77796     sqlite3DbFree(db, p);
 77800 /*
 77801 ** Return the number of bytes allocated for the expression structure 
 77802 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
 77803 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
 77804 */
 77805 static int exprStructSize(Expr *p){
 77806   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
 77807   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
 77808   return EXPR_FULLSIZE;
 77811 /*
 77812 ** The dupedExpr*Size() routines each return the number of bytes required
 77813 ** to store a copy of an expression or expression tree.  They differ in
 77814 ** how much of the tree is measured.
 77815 **
 77816 **     dupedExprStructSize()     Size of only the Expr structure 
 77817 **     dupedExprNodeSize()       Size of Expr + space for token
 77818 **     dupedExprSize()           Expr + token + subtree components
 77819 **
 77820 ***************************************************************************
 77821 **
 77822 ** The dupedExprStructSize() function returns two values OR-ed together:  
 77823 ** (1) the space required for a copy of the Expr structure only and 
 77824 ** (2) the EP_xxx flags that indicate what the structure size should be.
 77825 ** The return values is always one of:
 77826 **
 77827 **      EXPR_FULLSIZE
 77828 **      EXPR_REDUCEDSIZE   | EP_Reduced
 77829 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
 77830 **
 77831 ** The size of the structure can be found by masking the return value
 77832 ** of this routine with 0xfff.  The flags can be found by masking the
 77833 ** return value with EP_Reduced|EP_TokenOnly.
 77834 **
 77835 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
 77836 ** (unreduced) Expr objects as they or originally constructed by the parser.
 77837 ** During expression analysis, extra information is computed and moved into
 77838 ** later parts of teh Expr object and that extra information might get chopped
 77839 ** off if the expression is reduced.  Note also that it does not work to
 77840 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
 77841 ** to reduce a pristine expression tree from the parser.  The implementation
 77842 ** of dupedExprStructSize() contain multiple assert() statements that attempt
 77843 ** to enforce this constraint.
 77844 */
 77845 static int dupedExprStructSize(Expr *p, int flags){
 77846   int nSize;
 77847   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
 77848   assert( EXPR_FULLSIZE<=0xfff );
 77849   assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
 77850   if( 0==(flags&EXPRDUP_REDUCE) ){
 77851     nSize = EXPR_FULLSIZE;
 77852   }else{
 77853     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
 77854     assert( !ExprHasProperty(p, EP_FromJoin) ); 
 77855     assert( !ExprHasProperty(p, EP_MemToken) );
 77856     assert( !ExprHasProperty(p, EP_NoReduce) );
 77857     if( p->pLeft || p->x.pList ){
 77858       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
 77859     }else{
 77860       assert( p->pRight==0 );
 77861       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
 77864   return nSize;
 77867 /*
 77868 ** This function returns the space in bytes required to store the copy 
 77869 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
 77870 ** string is defined.)
 77871 */
 77872 static int dupedExprNodeSize(Expr *p, int flags){
 77873   int nByte = dupedExprStructSize(p, flags) & 0xfff;
 77874   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 77875     nByte += sqlite3Strlen30(p->u.zToken)+1;
 77877   return ROUND8(nByte);
 77880 /*
 77881 ** Return the number of bytes required to create a duplicate of the 
 77882 ** expression passed as the first argument. The second argument is a
 77883 ** mask containing EXPRDUP_XXX flags.
 77884 **
 77885 ** The value returned includes space to create a copy of the Expr struct
 77886 ** itself and the buffer referred to by Expr.u.zToken, if any.
 77887 **
 77888 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
 77889 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
 77890 ** and Expr.pRight variables (but not for any structures pointed to or 
 77891 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
 77892 */
 77893 static int dupedExprSize(Expr *p, int flags){
 77894   int nByte = 0;
 77895   if( p ){
 77896     nByte = dupedExprNodeSize(p, flags);
 77897     if( flags&EXPRDUP_REDUCE ){
 77898       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
 77901   return nByte;
 77904 /*
 77905 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
 77906 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
 77907 ** to store the copy of expression p, the copies of p->u.zToken
 77908 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
 77909 ** if any. Before returning, *pzBuffer is set to the first byte passed the
 77910 ** portion of the buffer copied into by this function.
 77911 */
 77912 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
 77913   Expr *pNew = 0;                      /* Value to return */
 77914   if( p ){
 77915     const int isReduced = (flags&EXPRDUP_REDUCE);
 77916     u8 *zAlloc;
 77917     u32 staticFlag = 0;
 77919     assert( pzBuffer==0 || isReduced );
 77921     /* Figure out where to write the new Expr structure. */
 77922     if( pzBuffer ){
 77923       zAlloc = *pzBuffer;
 77924       staticFlag = EP_Static;
 77925     }else{
 77926       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
 77928     pNew = (Expr *)zAlloc;
 77930     if( pNew ){
 77931       /* Set nNewSize to the size allocated for the structure pointed to
 77932       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
 77933       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
 77934       ** by the copy of the p->u.zToken string (if any).
 77935       */
 77936       const unsigned nStructSize = dupedExprStructSize(p, flags);
 77937       const int nNewSize = nStructSize & 0xfff;
 77938       int nToken;
 77939       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
 77940         nToken = sqlite3Strlen30(p->u.zToken) + 1;
 77941       }else{
 77942         nToken = 0;
 77944       if( isReduced ){
 77945         assert( ExprHasProperty(p, EP_Reduced)==0 );
 77946         memcpy(zAlloc, p, nNewSize);
 77947       }else{
 77948         int nSize = exprStructSize(p);
 77949         memcpy(zAlloc, p, nSize);
 77950         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
 77953       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
 77954       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
 77955       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
 77956       pNew->flags |= staticFlag;
 77958       /* Copy the p->u.zToken string, if any. */
 77959       if( nToken ){
 77960         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
 77961         memcpy(zToken, p->u.zToken, nToken);
 77964       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
 77965         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
 77966         if( ExprHasProperty(p, EP_xIsSelect) ){
 77967           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
 77968         }else{
 77969           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
 77973       /* Fill in pNew->pLeft and pNew->pRight. */
 77974       if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
 77975         zAlloc += dupedExprNodeSize(p, flags);
 77976         if( ExprHasProperty(pNew, EP_Reduced) ){
 77977           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
 77978           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
 77980         if( pzBuffer ){
 77981           *pzBuffer = zAlloc;
 77983       }else{
 77984         if( !ExprHasProperty(p, EP_TokenOnly) ){
 77985           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
 77986           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
 77992   return pNew;
 77995 /*
 77996 ** Create and return a deep copy of the object passed as the second 
 77997 ** argument. If an OOM condition is encountered, NULL is returned
 77998 ** and the db->mallocFailed flag set.
 77999 */
 78000 #ifndef SQLITE_OMIT_CTE
 78001 static With *withDup(sqlite3 *db, With *p){
 78002   With *pRet = 0;
 78003   if( p ){
 78004     int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
 78005     pRet = sqlite3DbMallocZero(db, nByte);
 78006     if( pRet ){
 78007       int i;
 78008       pRet->nCte = p->nCte;
 78009       for(i=0; i<p->nCte; i++){
 78010         pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
 78011         pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
 78012         pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
 78016   return pRet;
 78018 #else
 78019 # define withDup(x,y) 0
 78020 #endif
 78022 /*
 78023 ** The following group of routines make deep copies of expressions,
 78024 ** expression lists, ID lists, and select statements.  The copies can
 78025 ** be deleted (by being passed to their respective ...Delete() routines)
 78026 ** without effecting the originals.
 78027 **
 78028 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
 78029 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
 78030 ** by subsequent calls to sqlite*ListAppend() routines.
 78031 **
 78032 ** Any tables that the SrcList might point to are not duplicated.
 78033 **
 78034 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
 78035 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
 78036 ** truncated version of the usual Expr structure that will be stored as
 78037 ** part of the in-memory representation of the database schema.
 78038 */
 78039 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
 78040   return exprDup(db, p, flags, 0);
 78042 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
 78043   ExprList *pNew;
 78044   struct ExprList_item *pItem, *pOldItem;
 78045   int i;
 78046   if( p==0 ) return 0;
 78047   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 78048   if( pNew==0 ) return 0;
 78049   pNew->iECursor = 0;
 78050   pNew->nExpr = i = p->nExpr;
 78051   if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
 78052   pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
 78053   if( pItem==0 ){
 78054     sqlite3DbFree(db, pNew);
 78055     return 0;
 78057   pOldItem = p->a;
 78058   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
 78059     Expr *pOldExpr = pOldItem->pExpr;
 78060     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
 78061     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 78062     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
 78063     pItem->sortOrder = pOldItem->sortOrder;
 78064     pItem->done = 0;
 78065     pItem->bSpanIsTab = pOldItem->bSpanIsTab;
 78066     pItem->u = pOldItem->u;
 78068   return pNew;
 78071 /*
 78072 ** If cursors, triggers, views and subqueries are all omitted from
 78073 ** the build, then none of the following routines, except for 
 78074 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
 78075 ** called with a NULL argument.
 78076 */
 78077 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
 78078  || !defined(SQLITE_OMIT_SUBQUERY)
 78079 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
 78080   SrcList *pNew;
 78081   int i;
 78082   int nByte;
 78083   if( p==0 ) return 0;
 78084   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
 78085   pNew = sqlite3DbMallocRaw(db, nByte );
 78086   if( pNew==0 ) return 0;
 78087   pNew->nSrc = pNew->nAlloc = p->nSrc;
 78088   for(i=0; i<p->nSrc; i++){
 78089     struct SrcList_item *pNewItem = &pNew->a[i];
 78090     struct SrcList_item *pOldItem = &p->a[i];
 78091     Table *pTab;
 78092     pNewItem->pSchema = pOldItem->pSchema;
 78093     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
 78094     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 78095     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
 78096     pNewItem->jointype = pOldItem->jointype;
 78097     pNewItem->iCursor = pOldItem->iCursor;
 78098     pNewItem->addrFillSub = pOldItem->addrFillSub;
 78099     pNewItem->regReturn = pOldItem->regReturn;
 78100     pNewItem->isCorrelated = pOldItem->isCorrelated;
 78101     pNewItem->viaCoroutine = pOldItem->viaCoroutine;
 78102     pNewItem->isRecursive = pOldItem->isRecursive;
 78103     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
 78104     pNewItem->notIndexed = pOldItem->notIndexed;
 78105     pNewItem->pIndex = pOldItem->pIndex;
 78106     pTab = pNewItem->pTab = pOldItem->pTab;
 78107     if( pTab ){
 78108       pTab->nRef++;
 78110     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
 78111     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
 78112     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
 78113     pNewItem->colUsed = pOldItem->colUsed;
 78115   return pNew;
 78117 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
 78118   IdList *pNew;
 78119   int i;
 78120   if( p==0 ) return 0;
 78121   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
 78122   if( pNew==0 ) return 0;
 78123   pNew->nId = p->nId;
 78124   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
 78125   if( pNew->a==0 ){
 78126     sqlite3DbFree(db, pNew);
 78127     return 0;
 78129   /* Note that because the size of the allocation for p->a[] is not
 78130   ** necessarily a power of two, sqlite3IdListAppend() may not be called
 78131   ** on the duplicate created by this function. */
 78132   for(i=0; i<p->nId; i++){
 78133     struct IdList_item *pNewItem = &pNew->a[i];
 78134     struct IdList_item *pOldItem = &p->a[i];
 78135     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
 78136     pNewItem->idx = pOldItem->idx;
 78138   return pNew;
 78140 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 78141   Select *pNew, *pPrior;
 78142   if( p==0 ) return 0;
 78143   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
 78144   if( pNew==0 ) return 0;
 78145   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
 78146   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
 78147   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
 78148   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 78149   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 78150   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 78151   pNew->op = p->op;
 78152   pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 78153   if( pPrior ) pPrior->pNext = pNew;
 78154   pNew->pNext = 0;
 78155   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 78156   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 78157   pNew->iLimit = 0;
 78158   pNew->iOffset = 0;
 78159   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 78160   pNew->addrOpenEphm[0] = -1;
 78161   pNew->addrOpenEphm[1] = -1;
 78162   pNew->addrOpenEphm[2] = -1;
 78163   pNew->nSelectRow = p->nSelectRow;
 78164   pNew->pWith = withDup(db, p->pWith);
 78165   return pNew;
 78167 #else
 78168 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
 78169   assert( p==0 );
 78170   return 0;
 78172 #endif
 78175 /*
 78176 ** Add a new element to the end of an expression list.  If pList is
 78177 ** initially NULL, then create a new expression list.
 78178 **
 78179 ** If a memory allocation error occurs, the entire list is freed and
 78180 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
 78181 ** that the new entry was successfully appended.
 78182 */
 78183 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
 78184   Parse *pParse,          /* Parsing context */
 78185   ExprList *pList,        /* List to which to append. Might be NULL */
 78186   Expr *pExpr             /* Expression to be appended. Might be NULL */
 78187 ){
 78188   sqlite3 *db = pParse->db;
 78189   if( pList==0 ){
 78190     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
 78191     if( pList==0 ){
 78192       goto no_mem;
 78194     pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
 78195     if( pList->a==0 ) goto no_mem;
 78196   }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
 78197     struct ExprList_item *a;
 78198     assert( pList->nExpr>0 );
 78199     a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
 78200     if( a==0 ){
 78201       goto no_mem;
 78203     pList->a = a;
 78205   assert( pList->a!=0 );
 78206   if( 1 ){
 78207     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
 78208     memset(pItem, 0, sizeof(*pItem));
 78209     pItem->pExpr = pExpr;
 78211   return pList;
 78213 no_mem:     
 78214   /* Avoid leaking memory if malloc has failed. */
 78215   sqlite3ExprDelete(db, pExpr);
 78216   sqlite3ExprListDelete(db, pList);
 78217   return 0;
 78220 /*
 78221 ** Set the ExprList.a[].zName element of the most recently added item
 78222 ** on the expression list.
 78223 **
 78224 ** pList might be NULL following an OOM error.  But pName should never be
 78225 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 78226 ** is set.
 78227 */
 78228 SQLITE_PRIVATE void sqlite3ExprListSetName(
 78229   Parse *pParse,          /* Parsing context */
 78230   ExprList *pList,        /* List to which to add the span. */
 78231   Token *pName,           /* Name to be added */
 78232   int dequote             /* True to cause the name to be dequoted */
 78233 ){
 78234   assert( pList!=0 || pParse->db->mallocFailed!=0 );
 78235   if( pList ){
 78236     struct ExprList_item *pItem;
 78237     assert( pList->nExpr>0 );
 78238     pItem = &pList->a[pList->nExpr-1];
 78239     assert( pItem->zName==0 );
 78240     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
 78241     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
 78245 /*
 78246 ** Set the ExprList.a[].zSpan element of the most recently added item
 78247 ** on the expression list.
 78248 **
 78249 ** pList might be NULL following an OOM error.  But pSpan should never be
 78250 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
 78251 ** is set.
 78252 */
 78253 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
 78254   Parse *pParse,          /* Parsing context */
 78255   ExprList *pList,        /* List to which to add the span. */
 78256   ExprSpan *pSpan         /* The span to be added */
 78257 ){
 78258   sqlite3 *db = pParse->db;
 78259   assert( pList!=0 || db->mallocFailed!=0 );
 78260   if( pList ){
 78261     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
 78262     assert( pList->nExpr>0 );
 78263     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
 78264     sqlite3DbFree(db, pItem->zSpan);
 78265     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 78266                                     (int)(pSpan->zEnd - pSpan->zStart));
 78270 /*
 78271 ** If the expression list pEList contains more than iLimit elements,
 78272 ** leave an error message in pParse.
 78273 */
 78274 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
 78275   Parse *pParse,
 78276   ExprList *pEList,
 78277   const char *zObject
 78278 ){
 78279   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
 78280   testcase( pEList && pEList->nExpr==mx );
 78281   testcase( pEList && pEList->nExpr==mx+1 );
 78282   if( pEList && pEList->nExpr>mx ){
 78283     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
 78287 /*
 78288 ** Delete an entire expression list.
 78289 */
 78290 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
 78291   int i;
 78292   struct ExprList_item *pItem;
 78293   if( pList==0 ) return;
 78294   assert( pList->a!=0 || pList->nExpr==0 );
 78295   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 78296     sqlite3ExprDelete(db, pItem->pExpr);
 78297     sqlite3DbFree(db, pItem->zName);
 78298     sqlite3DbFree(db, pItem->zSpan);
 78300   sqlite3DbFree(db, pList->a);
 78301   sqlite3DbFree(db, pList);
 78304 /*
 78305 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
 78306 ** to an integer.  These routines are checking an expression to see
 78307 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
 78308 ** not constant.
 78309 **
 78310 ** These callback routines are used to implement the following:
 78311 **
 78312 **     sqlite3ExprIsConstant()
 78313 **     sqlite3ExprIsConstantNotJoin()
 78314 **     sqlite3ExprIsConstantOrFunction()
 78315 **
 78316 */
 78317 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
 78319   /* If pWalker->u.i is 3 then any term of the expression that comes from
 78320   ** the ON or USING clauses of a join disqualifies the expression
 78321   ** from being considered constant. */
 78322   if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
 78323     pWalker->u.i = 0;
 78324     return WRC_Abort;
 78327   switch( pExpr->op ){
 78328     /* Consider functions to be constant if all their arguments are constant
 78329     ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST
 78330     ** flag. */
 78331     case TK_FUNCTION:
 78332       if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
 78333         return WRC_Continue;
 78335       /* Fall through */
 78336     case TK_ID:
 78337     case TK_COLUMN:
 78338     case TK_AGG_FUNCTION:
 78339     case TK_AGG_COLUMN:
 78340       testcase( pExpr->op==TK_ID );
 78341       testcase( pExpr->op==TK_COLUMN );
 78342       testcase( pExpr->op==TK_AGG_FUNCTION );
 78343       testcase( pExpr->op==TK_AGG_COLUMN );
 78344       pWalker->u.i = 0;
 78345       return WRC_Abort;
 78346     default:
 78347       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
 78348       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
 78349       return WRC_Continue;
 78352 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
 78353   UNUSED_PARAMETER(NotUsed);
 78354   pWalker->u.i = 0;
 78355   return WRC_Abort;
 78357 static int exprIsConst(Expr *p, int initFlag){
 78358   Walker w;
 78359   memset(&w, 0, sizeof(w));
 78360   w.u.i = initFlag;
 78361   w.xExprCallback = exprNodeIsConstant;
 78362   w.xSelectCallback = selectNodeIsConstant;
 78363   sqlite3WalkExpr(&w, p);
 78364   return w.u.i;
 78367 /*
 78368 ** Walk an expression tree.  Return 1 if the expression is constant
 78369 ** and 0 if it involves variables or function calls.
 78370 **
 78371 ** For the purposes of this function, a double-quoted string (ex: "abc")
 78372 ** is considered a variable but a single-quoted string (ex: 'abc') is
 78373 ** a constant.
 78374 */
 78375 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
 78376   return exprIsConst(p, 1);
 78379 /*
 78380 ** Walk an expression tree.  Return 1 if the expression is constant
 78381 ** that does no originate from the ON or USING clauses of a join.
 78382 ** Return 0 if it involves variables or function calls or terms from
 78383 ** an ON or USING clause.
 78384 */
 78385 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
 78386   return exprIsConst(p, 3);
 78389 /*
 78390 ** Walk an expression tree.  Return 1 if the expression is constant
 78391 ** or a function call with constant arguments.  Return and 0 if there
 78392 ** are any variables.
 78393 **
 78394 ** For the purposes of this function, a double-quoted string (ex: "abc")
 78395 ** is considered a variable but a single-quoted string (ex: 'abc') is
 78396 ** a constant.
 78397 */
 78398 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
 78399   return exprIsConst(p, 2);
 78402 /*
 78403 ** If the expression p codes a constant integer that is small enough
 78404 ** to fit in a 32-bit integer, return 1 and put the value of the integer
 78405 ** in *pValue.  If the expression is not an integer or if it is too big
 78406 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
 78407 */
 78408 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
 78409   int rc = 0;
 78411   /* If an expression is an integer literal that fits in a signed 32-bit
 78412   ** integer, then the EP_IntValue flag will have already been set */
 78413   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
 78414            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
 78416   if( p->flags & EP_IntValue ){
 78417     *pValue = p->u.iValue;
 78418     return 1;
 78420   switch( p->op ){
 78421     case TK_UPLUS: {
 78422       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
 78423       break;
 78425     case TK_UMINUS: {
 78426       int v;
 78427       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
 78428         assert( v!=(-2147483647-1) );
 78429         *pValue = -v;
 78430         rc = 1;
 78432       break;
 78434     default: break;
 78436   return rc;
 78439 /*
 78440 ** Return FALSE if there is no chance that the expression can be NULL.
 78441 **
 78442 ** If the expression might be NULL or if the expression is too complex
 78443 ** to tell return TRUE.  
 78444 **
 78445 ** This routine is used as an optimization, to skip OP_IsNull opcodes
 78446 ** when we know that a value cannot be NULL.  Hence, a false positive
 78447 ** (returning TRUE when in fact the expression can never be NULL) might
 78448 ** be a small performance hit but is otherwise harmless.  On the other
 78449 ** hand, a false negative (returning FALSE when the result could be NULL)
 78450 ** will likely result in an incorrect answer.  So when in doubt, return
 78451 ** TRUE.
 78452 */
 78453 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
 78454   u8 op;
 78455   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 78456   op = p->op;
 78457   if( op==TK_REGISTER ) op = p->op2;
 78458   switch( op ){
 78459     case TK_INTEGER:
 78460     case TK_STRING:
 78461     case TK_FLOAT:
 78462     case TK_BLOB:
 78463       return 0;
 78464     default:
 78465       return 1;
 78469 /*
 78470 ** Return TRUE if the given expression is a constant which would be
 78471 ** unchanged by OP_Affinity with the affinity given in the second
 78472 ** argument.
 78473 **
 78474 ** This routine is used to determine if the OP_Affinity operation
 78475 ** can be omitted.  When in doubt return FALSE.  A false negative
 78476 ** is harmless.  A false positive, however, can result in the wrong
 78477 ** answer.
 78478 */
 78479 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
 78480   u8 op;
 78481   if( aff==SQLITE_AFF_NONE ) return 1;
 78482   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
 78483   op = p->op;
 78484   if( op==TK_REGISTER ) op = p->op2;
 78485   switch( op ){
 78486     case TK_INTEGER: {
 78487       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
 78489     case TK_FLOAT: {
 78490       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
 78492     case TK_STRING: {
 78493       return aff==SQLITE_AFF_TEXT;
 78495     case TK_BLOB: {
 78496       return 1;
 78498     case TK_COLUMN: {
 78499       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
 78500       return p->iColumn<0
 78501           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
 78503     default: {
 78504       return 0;
 78509 /*
 78510 ** Return TRUE if the given string is a row-id column name.
 78511 */
 78512 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
 78513   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
 78514   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
 78515   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
 78516   return 0;
 78519 /*
 78520 ** Return true if we are able to the IN operator optimization on a
 78521 ** query of the form
 78522 **
 78523 **       x IN (SELECT ...)
 78524 **
 78525 ** Where the SELECT... clause is as specified by the parameter to this
 78526 ** routine.
 78527 **
 78528 ** The Select object passed in has already been preprocessed and no
 78529 ** errors have been found.
 78530 */
 78531 #ifndef SQLITE_OMIT_SUBQUERY
 78532 static int isCandidateForInOpt(Select *p){
 78533   SrcList *pSrc;
 78534   ExprList *pEList;
 78535   Table *pTab;
 78536   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
 78537   if( p->pPrior ) return 0;              /* Not a compound SELECT */
 78538   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 78539     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 78540     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 78541     return 0; /* No DISTINCT keyword and no aggregate functions */
 78543   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
 78544   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
 78545   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
 78546   if( p->pWhere ) return 0;              /* Has no WHERE clause */
 78547   pSrc = p->pSrc;
 78548   assert( pSrc!=0 );
 78549   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 78550   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 78551   pTab = pSrc->a[0].pTab;
 78552   if( NEVER(pTab==0) ) return 0;
 78553   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
 78554   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
 78555   pEList = p->pEList;
 78556   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
 78557   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
 78558   return 1;
 78560 #endif /* SQLITE_OMIT_SUBQUERY */
 78562 /*
 78563 ** Code an OP_Once instruction and allocate space for its flag. Return the 
 78564 ** address of the new instruction.
 78565 */
 78566 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
 78567   Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
 78568   return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
 78571 /*
 78572 ** This function is used by the implementation of the IN (...) operator.
 78573 ** The pX parameter is the expression on the RHS of the IN operator, which
 78574 ** might be either a list of expressions or a subquery.
 78575 **
 78576 ** The job of this routine is to find or create a b-tree object that can
 78577 ** be used either to test for membership in the RHS set or to iterate through
 78578 ** all members of the RHS set, skipping duplicates.
 78579 **
 78580 ** A cursor is opened on the b-tree object that the RHS of the IN operator
 78581 ** and pX->iTable is set to the index of that cursor.
 78582 **
 78583 ** The returned value of this function indicates the b-tree type, as follows:
 78584 **
 78585 **   IN_INDEX_ROWID      - The cursor was opened on a database table.
 78586 **   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
 78587 **   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
 78588 **   IN_INDEX_EPH        - The cursor was opened on a specially created and
 78589 **                         populated epheremal table.
 78590 **
 78591 ** An existing b-tree might be used if the RHS expression pX is a simple
 78592 ** subquery such as:
 78593 **
 78594 **     SELECT <column> FROM <table>
 78595 **
 78596 ** If the RHS of the IN operator is a list or a more complex subquery, then
 78597 ** an ephemeral table might need to be generated from the RHS and then
 78598 ** pX->iTable made to point to the ephermeral table instead of an
 78599 ** existing table.  
 78600 **
 78601 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
 78602 ** through the set members, skipping any duplicates. In this case an
 78603 ** epheremal table must be used unless the selected <column> is guaranteed
 78604 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
 78605 ** has a UNIQUE constraint or UNIQUE index.
 78606 **
 78607 ** If the prNotFound parameter is not 0, then the b-tree will be used 
 78608 ** for fast set membership tests. In this case an epheremal table must 
 78609 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
 78610 ** be found with <column> as its left-most column.
 78611 **
 78612 ** When the b-tree is being used for membership tests, the calling function
 78613 ** needs to know whether or not the structure contains an SQL NULL 
 78614 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
 78615 ** If there is any chance that the (...) might contain a NULL value at
 78616 ** runtime, then a register is allocated and the register number written
 78617 ** to *prNotFound. If there is no chance that the (...) contains a
 78618 ** NULL value, then *prNotFound is left unchanged.
 78619 **
 78620 ** If a register is allocated and its location stored in *prNotFound, then
 78621 ** its initial value is NULL.  If the (...) does not remain constant
 78622 ** for the duration of the query (i.e. the SELECT within the (...)
 78623 ** is a correlated subquery) then the value of the allocated register is
 78624 ** reset to NULL each time the subquery is rerun. This allows the
 78625 ** caller to use vdbe code equivalent to the following:
 78626 **
 78627 **   if( register==NULL ){
 78628 **     has_null = <test if data structure contains null>
 78629 **     register = 1
 78630 **   }
 78631 **
 78632 ** in order to avoid running the <test if data structure contains null>
 78633 ** test more often than is necessary.
 78634 */
 78635 #ifndef SQLITE_OMIT_SUBQUERY
 78636 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
 78637   Select *p;                            /* SELECT to the right of IN operator */
 78638   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
 78639   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
 78640   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
 78641   Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
 78643   assert( pX->op==TK_IN );
 78645   /* Check to see if an existing table or index can be used to
 78646   ** satisfy the query.  This is preferable to generating a new 
 78647   ** ephemeral table.
 78648   */
 78649   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
 78650   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
 78651     sqlite3 *db = pParse->db;              /* Database connection */
 78652     Table *pTab;                           /* Table <table>. */
 78653     Expr *pExpr;                           /* Expression <column> */
 78654     i16 iCol;                              /* Index of column <column> */
 78655     i16 iDb;                               /* Database idx for pTab */
 78657     assert( p );                        /* Because of isCandidateForInOpt(p) */
 78658     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
 78659     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
 78660     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
 78661     pTab = p->pSrc->a[0].pTab;
 78662     pExpr = p->pEList->a[0].pExpr;
 78663     iCol = (i16)pExpr->iColumn;
 78665     /* Code an OP_Transaction and OP_TableLock for <table>. */
 78666     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 78667     sqlite3CodeVerifySchema(pParse, iDb);
 78668     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 78670     /* This function is only called from two places. In both cases the vdbe
 78671     ** has already been allocated. So assume sqlite3GetVdbe() is always
 78672     ** successful here.
 78673     */
 78674     assert(v);
 78675     if( iCol<0 ){
 78676       int iAddr = sqlite3CodeOnce(pParse);
 78677       VdbeCoverage(v);
 78679       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 78680       eType = IN_INDEX_ROWID;
 78682       sqlite3VdbeJumpHere(v, iAddr);
 78683     }else{
 78684       Index *pIdx;                         /* Iterator variable */
 78686       /* The collation sequence used by the comparison. If an index is to
 78687       ** be used in place of a temp-table, it must be ordered according
 78688       ** to this collation sequence.  */
 78689       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
 78691       /* Check that the affinity that will be used to perform the 
 78692       ** comparison is the same as the affinity of the column. If
 78693       ** it is not, it is not possible to use any index.
 78694       */
 78695       int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 78697       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
 78698         if( (pIdx->aiColumn[0]==iCol)
 78699          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
 78700          && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
 78701         ){
 78702           int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 78703           sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
 78704           sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 78705           VdbeComment((v, "%s", pIdx->zName));
 78706           assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
 78707           eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
 78709           if( prNotFound && !pTab->aCol[iCol].notNull ){
 78710             *prNotFound = ++pParse->nMem;
 78711             sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 78713           sqlite3VdbeJumpHere(v, iAddr);
 78719   if( eType==0 ){
 78720     /* Could not found an existing table or index to use as the RHS b-tree.
 78721     ** We will have to generate an ephemeral table to do the job.
 78722     */
 78723     u32 savedNQueryLoop = pParse->nQueryLoop;
 78724     int rMayHaveNull = 0;
 78725     eType = IN_INDEX_EPH;
 78726     if( prNotFound ){
 78727       *prNotFound = rMayHaveNull = ++pParse->nMem;
 78728       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
 78729     }else{
 78730       testcase( pParse->nQueryLoop>0 );
 78731       pParse->nQueryLoop = 0;
 78732       if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
 78733         eType = IN_INDEX_ROWID;
 78736     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
 78737     pParse->nQueryLoop = savedNQueryLoop;
 78738   }else{
 78739     pX->iTable = iTab;
 78741   return eType;
 78743 #endif
 78745 /*
 78746 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
 78747 ** or IN operators.  Examples:
 78748 **
 78749 **     (SELECT a FROM b)          -- subquery
 78750 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
 78751 **     x IN (4,5,11)              -- IN operator with list on right-hand side
 78752 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
 78753 **
 78754 ** The pExpr parameter describes the expression that contains the IN
 78755 ** operator or subquery.
 78756 **
 78757 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
 78758 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
 78759 ** to some integer key column of a table B-Tree. In this case, use an
 78760 ** intkey B-Tree to store the set of IN(...) values instead of the usual
 78761 ** (slower) variable length keys B-Tree.
 78762 **
 78763 ** If rMayHaveNull is non-zero, that means that the operation is an IN
 78764 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
 78765 ** Furthermore, the IN is in a WHERE clause and that we really want
 78766 ** to iterate over the RHS of the IN operator in order to quickly locate
 78767 ** all corresponding LHS elements.  All this routine does is initialize
 78768 ** the register given by rMayHaveNull to NULL.  Calling routines will take
 78769 ** care of changing this register value to non-NULL if the RHS is NULL-free.
 78770 **
 78771 ** If rMayHaveNull is zero, that means that the subquery is being used
 78772 ** for membership testing only.  There is no need to initialize any
 78773 ** registers to indicate the presence or absence of NULLs on the RHS.
 78774 **
 78775 ** For a SELECT or EXISTS operator, return the register that holds the
 78776 ** result.  For IN operators or if an error occurs, the return value is 0.
 78777 */
 78778 #ifndef SQLITE_OMIT_SUBQUERY
 78779 SQLITE_PRIVATE int sqlite3CodeSubselect(
 78780   Parse *pParse,          /* Parsing context */
 78781   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
 78782   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
 78783   int isRowid             /* If true, LHS of IN operator is a rowid */
 78784 ){
 78785   int testAddr = -1;                      /* One-time test address */
 78786   int rReg = 0;                           /* Register storing resulting */
 78787   Vdbe *v = sqlite3GetVdbe(pParse);
 78788   if( NEVER(v==0) ) return 0;
 78789   sqlite3ExprCachePush(pParse);
 78791   /* This code must be run in its entirety every time it is encountered
 78792   ** if any of the following is true:
 78793   **
 78794   **    *  The right-hand side is a correlated subquery
 78795   **    *  The right-hand side is an expression list containing variables
 78796   **    *  We are inside a trigger
 78797   **
 78798   ** If all of the above are false, then we can run this code just once
 78799   ** save the results, and reuse the same result on subsequent invocations.
 78800   */
 78801   if( !ExprHasProperty(pExpr, EP_VarSelect) ){
 78802     testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 78805 #ifndef SQLITE_OMIT_EXPLAIN
 78806   if( pParse->explain==2 ){
 78807     char *zMsg = sqlite3MPrintf(
 78808         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
 78809         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
 78810     );
 78811     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 78813 #endif
 78815   switch( pExpr->op ){
 78816     case TK_IN: {
 78817       char affinity;              /* Affinity of the LHS of the IN */
 78818       int addr;                   /* Address of OP_OpenEphemeral instruction */
 78819       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
 78820       KeyInfo *pKeyInfo = 0;      /* Key information */
 78822       if( rMayHaveNull ){
 78823         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
 78826       affinity = sqlite3ExprAffinity(pLeft);
 78828       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
 78829       ** expression it is handled the same way.  An ephemeral table is 
 78830       ** filled with single-field index keys representing the results
 78831       ** from the SELECT or the <exprlist>.
 78832       **
 78833       ** If the 'x' expression is a column value, or the SELECT...
 78834       ** statement returns a column value, then the affinity of that
 78835       ** column is used to build the index keys. If both 'x' and the
 78836       ** SELECT... statement are columns, then numeric affinity is used
 78837       ** if either column has NUMERIC or INTEGER affinity. If neither
 78838       ** 'x' nor the SELECT... statement are columns, then numeric affinity
 78839       ** is used.
 78840       */
 78841       pExpr->iTable = pParse->nTab++;
 78842       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
 78843       pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
 78845       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 78846         /* Case 1:     expr IN (SELECT ...)
 78847         **
 78848         ** Generate code to write the results of the select into the temporary
 78849         ** table allocated and opened above.
 78850         */
 78851         SelectDest dest;
 78852         ExprList *pEList;
 78854         assert( !isRowid );
 78855         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
 78856         dest.affSdst = (u8)affinity;
 78857         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
 78858         pExpr->x.pSelect->iLimit = 0;
 78859         testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
 78860         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
 78861           sqlite3KeyInfoUnref(pKeyInfo);
 78862           return 0;
 78864         pEList = pExpr->x.pSelect->pEList;
 78865         assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
 78866         assert( pEList!=0 );
 78867         assert( pEList->nExpr>0 );
 78868         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 78869         pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
 78870                                                          pEList->a[0].pExpr);
 78871       }else if( ALWAYS(pExpr->x.pList!=0) ){
 78872         /* Case 2:     expr IN (exprlist)
 78873         **
 78874         ** For each expression, build an index key from the evaluation and
 78875         ** store it in the temporary table. If <expr> is a column, then use
 78876         ** that columns affinity when building index keys. If <expr> is not
 78877         ** a column, use numeric affinity.
 78878         */
 78879         int i;
 78880         ExprList *pList = pExpr->x.pList;
 78881         struct ExprList_item *pItem;
 78882         int r1, r2, r3;
 78884         if( !affinity ){
 78885           affinity = SQLITE_AFF_NONE;
 78887         if( pKeyInfo ){
 78888           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
 78889           pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
 78892         /* Loop through each expression in <exprlist>. */
 78893         r1 = sqlite3GetTempReg(pParse);
 78894         r2 = sqlite3GetTempReg(pParse);
 78895         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
 78896         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
 78897           Expr *pE2 = pItem->pExpr;
 78898           int iValToIns;
 78900           /* If the expression is not constant then we will need to
 78901           ** disable the test that was generated above that makes sure
 78902           ** this code only executes once.  Because for a non-constant
 78903           ** expression we need to rerun this code each time.
 78904           */
 78905           if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
 78906             sqlite3VdbeChangeToNoop(v, testAddr);
 78907             testAddr = -1;
 78910           /* Evaluate the expression and insert it into the temp table */
 78911           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
 78912             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
 78913           }else{
 78914             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
 78915             if( isRowid ){
 78916               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
 78917                                 sqlite3VdbeCurrentAddr(v)+2);
 78918               VdbeCoverage(v);
 78919               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
 78920             }else{
 78921               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
 78922               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
 78923               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
 78927         sqlite3ReleaseTempReg(pParse, r1);
 78928         sqlite3ReleaseTempReg(pParse, r2);
 78930       if( pKeyInfo ){
 78931         sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
 78933       break;
 78936     case TK_EXISTS:
 78937     case TK_SELECT:
 78938     default: {
 78939       /* If this has to be a scalar SELECT.  Generate code to put the
 78940       ** value of this select in a memory cell and record the number
 78941       ** of the memory cell in iColumn.  If this is an EXISTS, write
 78942       ** an integer 0 (not exists) or 1 (exists) into a memory cell
 78943       ** and record that memory cell in iColumn.
 78944       */
 78945       Select *pSel;                         /* SELECT statement to encode */
 78946       SelectDest dest;                      /* How to deal with SELECt result */
 78948       testcase( pExpr->op==TK_EXISTS );
 78949       testcase( pExpr->op==TK_SELECT );
 78950       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 78952       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 78953       pSel = pExpr->x.pSelect;
 78954       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
 78955       if( pExpr->op==TK_SELECT ){
 78956         dest.eDest = SRT_Mem;
 78957         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
 78958         VdbeComment((v, "Init subquery result"));
 78959       }else{
 78960         dest.eDest = SRT_Exists;
 78961         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 78962         VdbeComment((v, "Init EXISTS result"));
 78964       sqlite3ExprDelete(pParse->db, pSel->pLimit);
 78965       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
 78966                                   &sqlite3IntTokens[1]);
 78967       pSel->iLimit = 0;
 78968       if( sqlite3Select(pParse, pSel, &dest) ){
 78969         return 0;
 78971       rReg = dest.iSDParm;
 78972       ExprSetVVAProperty(pExpr, EP_NoReduce);
 78973       break;
 78977   if( testAddr>=0 ){
 78978     sqlite3VdbeJumpHere(v, testAddr);
 78980   sqlite3ExprCachePop(pParse, 1);
 78982   return rReg;
 78984 #endif /* SQLITE_OMIT_SUBQUERY */
 78986 #ifndef SQLITE_OMIT_SUBQUERY
 78987 /*
 78988 ** Generate code for an IN expression.
 78989 **
 78990 **      x IN (SELECT ...)
 78991 **      x IN (value, value, ...)
 78992 **
 78993 ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
 78994 ** is an array of zero or more values.  The expression is true if the LHS is
 78995 ** contained within the RHS.  The value of the expression is unknown (NULL)
 78996 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
 78997 ** RHS contains one or more NULL values.
 78998 **
 78999 ** This routine generates code will jump to destIfFalse if the LHS is not 
 79000 ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
 79001 ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
 79002 ** within the RHS then fall through.
 79003 */
 79004 static void sqlite3ExprCodeIN(
 79005   Parse *pParse,        /* Parsing and code generating context */
 79006   Expr *pExpr,          /* The IN expression */
 79007   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
 79008   int destIfNull        /* Jump here if the results are unknown due to NULLs */
 79009 ){
 79010   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
 79011   char affinity;        /* Comparison affinity to use */
 79012   int eType;            /* Type of the RHS */
 79013   int r1;               /* Temporary use register */
 79014   Vdbe *v;              /* Statement under construction */
 79016   /* Compute the RHS.   After this step, the table with cursor
 79017   ** pExpr->iTable will contains the values that make up the RHS.
 79018   */
 79019   v = pParse->pVdbe;
 79020   assert( v!=0 );       /* OOM detected prior to this routine */
 79021   VdbeNoopComment((v, "begin IN expr"));
 79022   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
 79024   /* Figure out the affinity to use to create a key from the results
 79025   ** of the expression. affinityStr stores a static string suitable for
 79026   ** P4 of OP_MakeRecord.
 79027   */
 79028   affinity = comparisonAffinity(pExpr);
 79030   /* Code the LHS, the <expr> from "<expr> IN (...)".
 79031   */
 79032   sqlite3ExprCachePush(pParse);
 79033   r1 = sqlite3GetTempReg(pParse);
 79034   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
 79036   /* If the LHS is NULL, then the result is either false or NULL depending
 79037   ** on whether the RHS is empty or not, respectively.
 79038   */
 79039   if( destIfNull==destIfFalse ){
 79040     /* Shortcut for the common case where the false and NULL outcomes are
 79041     ** the same. */
 79042     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
 79043   }else{
 79044     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
 79045     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
 79046     VdbeCoverage(v);
 79047     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 79048     sqlite3VdbeJumpHere(v, addr1);
 79051   if( eType==IN_INDEX_ROWID ){
 79052     /* In this case, the RHS is the ROWID of table b-tree
 79053     */
 79054     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
 79055     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
 79056     VdbeCoverage(v);
 79057   }else{
 79058     /* In this case, the RHS is an index b-tree.
 79059     */
 79060     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
 79062     /* If the set membership test fails, then the result of the 
 79063     ** "x IN (...)" expression must be either 0 or NULL. If the set
 79064     ** contains no NULL values, then the result is 0. If the set 
 79065     ** contains one or more NULL values, then the result of the
 79066     ** expression is also NULL.
 79067     */
 79068     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
 79069       /* This branch runs if it is known at compile time that the RHS
 79070       ** cannot contain NULL values. This happens as the result
 79071       ** of a "NOT NULL" constraint in the database schema.
 79072       **
 79073       ** Also run this branch if NULL is equivalent to FALSE
 79074       ** for this particular IN operator.
 79075       */
 79076       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
 79077       VdbeCoverage(v);
 79078     }else{
 79079       /* In this branch, the RHS of the IN might contain a NULL and
 79080       ** the presence of a NULL on the RHS makes a difference in the
 79081       ** outcome.
 79082       */
 79083       int j1, j2;
 79085       /* First check to see if the LHS is contained in the RHS.  If so,
 79086       ** then the presence of NULLs in the RHS does not matter, so jump
 79087       ** over all of the code that follows.
 79088       */
 79089       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
 79090       VdbeCoverage(v);
 79092       /* Here we begin generating code that runs if the LHS is not
 79093       ** contained within the RHS.  Generate additional code that
 79094       ** tests the RHS for NULLs.  If the RHS contains a NULL then
 79095       ** jump to destIfNull.  If there are no NULLs in the RHS then
 79096       ** jump to destIfFalse.
 79097       */
 79098       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
 79099       sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
 79100       j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
 79101       VdbeCoverage(v);
 79102       sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
 79103       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
 79104       sqlite3VdbeJumpHere(v, j2);
 79105       sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
 79106       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
 79108       /* The OP_Found at the top of this branch jumps here when true, 
 79109       ** causing the overall IN expression evaluation to fall through.
 79110       */
 79111       sqlite3VdbeJumpHere(v, j1);
 79114   sqlite3ReleaseTempReg(pParse, r1);
 79115   sqlite3ExprCachePop(pParse, 1);
 79116   VdbeComment((v, "end IN expr"));
 79118 #endif /* SQLITE_OMIT_SUBQUERY */
 79120 /*
 79121 ** Duplicate an 8-byte value
 79122 */
 79123 static char *dup8bytes(Vdbe *v, const char *in){
 79124   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
 79125   if( out ){
 79126     memcpy(out, in, 8);
 79128   return out;
 79131 #ifndef SQLITE_OMIT_FLOATING_POINT
 79132 /*
 79133 ** Generate an instruction that will put the floating point
 79134 ** value described by z[0..n-1] into register iMem.
 79135 **
 79136 ** The z[] string will probably not be zero-terminated.  But the 
 79137 ** z[n] character is guaranteed to be something that does not look
 79138 ** like the continuation of the number.
 79139 */
 79140 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
 79141   if( ALWAYS(z!=0) ){
 79142     double value;
 79143     char *zV;
 79144     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 79145     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
 79146     if( negateFlag ) value = -value;
 79147     zV = dup8bytes(v, (char*)&value);
 79148     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
 79151 #endif
 79154 /*
 79155 ** Generate an instruction that will put the integer describe by
 79156 ** text z[0..n-1] into register iMem.
 79157 **
 79158 ** Expr.u.zToken is always UTF8 and zero-terminated.
 79159 */
 79160 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
 79161   Vdbe *v = pParse->pVdbe;
 79162   if( pExpr->flags & EP_IntValue ){
 79163     int i = pExpr->u.iValue;
 79164     assert( i>=0 );
 79165     if( negFlag ) i = -i;
 79166     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
 79167   }else{
 79168     int c;
 79169     i64 value;
 79170     const char *z = pExpr->u.zToken;
 79171     assert( z!=0 );
 79172     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 79173     if( c==0 || (c==2 && negFlag) ){
 79174       char *zV;
 79175       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
 79176       zV = dup8bytes(v, (char*)&value);
 79177       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
 79178     }else{
 79179 #ifdef SQLITE_OMIT_FLOATING_POINT
 79180       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 79181 #else
 79182       codeReal(v, z, negFlag, iMem);
 79183 #endif
 79188 /*
 79189 ** Clear a cache entry.
 79190 */
 79191 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
 79192   if( p->tempReg ){
 79193     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 79194       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
 79196     p->tempReg = 0;
 79201 /*
 79202 ** Record in the column cache that a particular column from a
 79203 ** particular table is stored in a particular register.
 79204 */
 79205 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
 79206   int i;
 79207   int minLru;
 79208   int idxLru;
 79209   struct yColCache *p;
 79211   assert( iReg>0 );  /* Register numbers are always positive */
 79212   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
 79214   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
 79215   ** for testing only - to verify that SQLite always gets the same answer
 79216   ** with and without the column cache.
 79217   */
 79218   if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
 79220   /* First replace any existing entry.
 79221   **
 79222   ** Actually, the way the column cache is currently used, we are guaranteed
 79223   ** that the object will never already be in cache.  Verify this guarantee.
 79224   */
 79225 #ifndef NDEBUG
 79226   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79227     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
 79229 #endif
 79231   /* Find an empty slot and replace it */
 79232   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79233     if( p->iReg==0 ){
 79234       p->iLevel = pParse->iCacheLevel;
 79235       p->iTable = iTab;
 79236       p->iColumn = iCol;
 79237       p->iReg = iReg;
 79238       p->tempReg = 0;
 79239       p->lru = pParse->iCacheCnt++;
 79240       return;
 79244   /* Replace the last recently used */
 79245   minLru = 0x7fffffff;
 79246   idxLru = -1;
 79247   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79248     if( p->lru<minLru ){
 79249       idxLru = i;
 79250       minLru = p->lru;
 79253   if( ALWAYS(idxLru>=0) ){
 79254     p = &pParse->aColCache[idxLru];
 79255     p->iLevel = pParse->iCacheLevel;
 79256     p->iTable = iTab;
 79257     p->iColumn = iCol;
 79258     p->iReg = iReg;
 79259     p->tempReg = 0;
 79260     p->lru = pParse->iCacheCnt++;
 79261     return;
 79265 /*
 79266 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
 79267 ** Purge the range of registers from the column cache.
 79268 */
 79269 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
 79270   int i;
 79271   int iLast = iReg + nReg - 1;
 79272   struct yColCache *p;
 79273   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79274     int r = p->iReg;
 79275     if( r>=iReg && r<=iLast ){
 79276       cacheEntryClear(pParse, p);
 79277       p->iReg = 0;
 79282 /*
 79283 ** Remember the current column cache context.  Any new entries added
 79284 ** added to the column cache after this call are removed when the
 79285 ** corresponding pop occurs.
 79286 */
 79287 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
 79288   pParse->iCacheLevel++;
 79289 #ifdef SQLITE_DEBUG
 79290   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 79291     printf("PUSH to %d\n", pParse->iCacheLevel);
 79293 #endif
 79296 /*
 79297 ** Remove from the column cache any entries that were added since the
 79298 ** the previous N Push operations.  In other words, restore the cache
 79299 ** to the state it was in N Pushes ago.
 79300 */
 79301 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
 79302   int i;
 79303   struct yColCache *p;
 79304   assert( N>0 );
 79305   assert( pParse->iCacheLevel>=N );
 79306   pParse->iCacheLevel -= N;
 79307 #ifdef SQLITE_DEBUG
 79308   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 79309     printf("POP  to %d\n", pParse->iCacheLevel);
 79311 #endif
 79312   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79313     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
 79314       cacheEntryClear(pParse, p);
 79315       p->iReg = 0;
 79320 /*
 79321 ** When a cached column is reused, make sure that its register is
 79322 ** no longer available as a temp register.  ticket #3879:  that same
 79323 ** register might be in the cache in multiple places, so be sure to
 79324 ** get them all.
 79325 */
 79326 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
 79327   int i;
 79328   struct yColCache *p;
 79329   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79330     if( p->iReg==iReg ){
 79331       p->tempReg = 0;
 79336 /*
 79337 ** Generate code to extract the value of the iCol-th column of a table.
 79338 */
 79339 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
 79340   Vdbe *v,        /* The VDBE under construction */
 79341   Table *pTab,    /* The table containing the value */
 79342   int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
 79343   int iCol,       /* Index of the column to extract */
 79344   int regOut      /* Extract the value into this register */
 79345 ){
 79346   if( iCol<0 || iCol==pTab->iPKey ){
 79347     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
 79348   }else{
 79349     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
 79350     int x = iCol;
 79351     if( !HasRowid(pTab) ){
 79352       x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
 79354     sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
 79356   if( iCol>=0 ){
 79357     sqlite3ColumnDefault(v, pTab, iCol, regOut);
 79361 /*
 79362 ** Generate code that will extract the iColumn-th column from
 79363 ** table pTab and store the column value in a register.  An effort
 79364 ** is made to store the column value in register iReg, but this is
 79365 ** not guaranteed.  The location of the column value is returned.
 79366 **
 79367 ** There must be an open cursor to pTab in iTable when this routine
 79368 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
 79369 */
 79370 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
 79371   Parse *pParse,   /* Parsing and code generating context */
 79372   Table *pTab,     /* Description of the table we are reading from */
 79373   int iColumn,     /* Index of the table column */
 79374   int iTable,      /* The cursor pointing to the table */
 79375   int iReg,        /* Store results here */
 79376   u8 p5            /* P5 value for OP_Column */
 79377 ){
 79378   Vdbe *v = pParse->pVdbe;
 79379   int i;
 79380   struct yColCache *p;
 79382   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79383     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
 79384       p->lru = pParse->iCacheCnt++;
 79385       sqlite3ExprCachePinRegister(pParse, p->iReg);
 79386       return p->iReg;
 79389   assert( v!=0 );
 79390   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
 79391   if( p5 ){
 79392     sqlite3VdbeChangeP5(v, p5);
 79393   }else{   
 79394     sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
 79396   return iReg;
 79399 /*
 79400 ** Clear all column cache entries.
 79401 */
 79402 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
 79403   int i;
 79404   struct yColCache *p;
 79406 #if SQLITE_DEBUG
 79407   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
 79408     printf("CLEAR\n");
 79410 #endif
 79411   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79412     if( p->iReg ){
 79413       cacheEntryClear(pParse, p);
 79414       p->iReg = 0;
 79419 /*
 79420 ** Record the fact that an affinity change has occurred on iCount
 79421 ** registers starting with iStart.
 79422 */
 79423 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
 79424   sqlite3ExprCacheRemove(pParse, iStart, iCount);
 79427 /*
 79428 ** Generate code to move content from registers iFrom...iFrom+nReg-1
 79429 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
 79430 */
 79431 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
 79432   int i;
 79433   struct yColCache *p;
 79434   assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
 79435   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
 79436   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79437     int x = p->iReg;
 79438     if( x>=iFrom && x<iFrom+nReg ){
 79439       p->iReg += iTo-iFrom;
 79444 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 79445 /*
 79446 ** Return true if any register in the range iFrom..iTo (inclusive)
 79447 ** is used as part of the column cache.
 79448 **
 79449 ** This routine is used within assert() and testcase() macros only
 79450 ** and does not appear in a normal build.
 79451 */
 79452 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
 79453   int i;
 79454   struct yColCache *p;
 79455   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 79456     int r = p->iReg;
 79457     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
 79459   return 0;
 79461 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
 79463 /*
 79464 ** Convert an expression node to a TK_REGISTER
 79465 */
 79466 static void exprToRegister(Expr *p, int iReg){
 79467   p->op2 = p->op;
 79468   p->op = TK_REGISTER;
 79469   p->iTable = iReg;
 79470   ExprClearProperty(p, EP_Skip);
 79473 /*
 79474 ** Generate code into the current Vdbe to evaluate the given
 79475 ** expression.  Attempt to store the results in register "target".
 79476 ** Return the register where results are stored.
 79477 **
 79478 ** With this routine, there is no guarantee that results will
 79479 ** be stored in target.  The result might be stored in some other
 79480 ** register if it is convenient to do so.  The calling function
 79481 ** must check the return code and move the results to the desired
 79482 ** register.
 79483 */
 79484 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
 79485   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
 79486   int op;                   /* The opcode being coded */
 79487   int inReg = target;       /* Results stored in register inReg */
 79488   int regFree1 = 0;         /* If non-zero free this temporary register */
 79489   int regFree2 = 0;         /* If non-zero free this temporary register */
 79490   int r1, r2, r3, r4;       /* Various register numbers */
 79491   sqlite3 *db = pParse->db; /* The database connection */
 79492   Expr tempX;               /* Temporary expression node */
 79494   assert( target>0 && target<=pParse->nMem );
 79495   if( v==0 ){
 79496     assert( pParse->db->mallocFailed );
 79497     return 0;
 79500   if( pExpr==0 ){
 79501     op = TK_NULL;
 79502   }else{
 79503     op = pExpr->op;
 79505   switch( op ){
 79506     case TK_AGG_COLUMN: {
 79507       AggInfo *pAggInfo = pExpr->pAggInfo;
 79508       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
 79509       if( !pAggInfo->directMode ){
 79510         assert( pCol->iMem>0 );
 79511         inReg = pCol->iMem;
 79512         break;
 79513       }else if( pAggInfo->useSortingIdx ){
 79514         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
 79515                               pCol->iSorterColumn, target);
 79516         break;
 79518       /* Otherwise, fall thru into the TK_COLUMN case */
 79520     case TK_COLUMN: {
 79521       int iTab = pExpr->iTable;
 79522       if( iTab<0 ){
 79523         if( pParse->ckBase>0 ){
 79524           /* Generating CHECK constraints or inserting into partial index */
 79525           inReg = pExpr->iColumn + pParse->ckBase;
 79526           break;
 79527         }else{
 79528           /* Deleting from a partial index */
 79529           iTab = pParse->iPartIdxTab;
 79532       inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
 79533                                pExpr->iColumn, iTab, target,
 79534                                pExpr->op2);
 79535       break;
 79537     case TK_INTEGER: {
 79538       codeInteger(pParse, pExpr, 0, target);
 79539       break;
 79541 #ifndef SQLITE_OMIT_FLOATING_POINT
 79542     case TK_FLOAT: {
 79543       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79544       codeReal(v, pExpr->u.zToken, 0, target);
 79545       break;
 79547 #endif
 79548     case TK_STRING: {
 79549       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79550       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
 79551       break;
 79553     case TK_NULL: {
 79554       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 79555       break;
 79557 #ifndef SQLITE_OMIT_BLOB_LITERAL
 79558     case TK_BLOB: {
 79559       int n;
 79560       const char *z;
 79561       char *zBlob;
 79562       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79563       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 79564       assert( pExpr->u.zToken[1]=='\'' );
 79565       z = &pExpr->u.zToken[2];
 79566       n = sqlite3Strlen30(z) - 1;
 79567       assert( z[n]=='\'' );
 79568       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
 79569       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
 79570       break;
 79572 #endif
 79573     case TK_VARIABLE: {
 79574       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79575       assert( pExpr->u.zToken!=0 );
 79576       assert( pExpr->u.zToken[0]!=0 );
 79577       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 79578       if( pExpr->u.zToken[1]!=0 ){
 79579         assert( pExpr->u.zToken[0]=='?' 
 79580              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
 79581         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
 79583       break;
 79585     case TK_REGISTER: {
 79586       inReg = pExpr->iTable;
 79587       break;
 79589     case TK_AS: {
 79590       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 79591       break;
 79593 #ifndef SQLITE_OMIT_CAST
 79594     case TK_CAST: {
 79595       /* Expressions of the form:   CAST(pLeft AS token) */
 79596       int aff, to_op;
 79597       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 79598       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79599       aff = sqlite3AffinityType(pExpr->u.zToken, 0);
 79600       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
 79601       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
 79602       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
 79603       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
 79604       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
 79605       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
 79606       testcase( to_op==OP_ToText );
 79607       testcase( to_op==OP_ToBlob );
 79608       testcase( to_op==OP_ToNumeric );
 79609       testcase( to_op==OP_ToInt );
 79610       testcase( to_op==OP_ToReal );
 79611       if( inReg!=target ){
 79612         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
 79613         inReg = target;
 79615       sqlite3VdbeAddOp1(v, to_op, inReg);
 79616       testcase( usedAsColumnCache(pParse, inReg, inReg) );
 79617       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
 79618       break;
 79620 #endif /* SQLITE_OMIT_CAST */
 79621     case TK_LT:
 79622     case TK_LE:
 79623     case TK_GT:
 79624     case TK_GE:
 79625     case TK_NE:
 79626     case TK_EQ: {
 79627       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79628       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 79629       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 79630                   r1, r2, inReg, SQLITE_STOREP2);
 79631       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 79632       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
 79633       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
 79634       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
 79635       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
 79636       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 79637       testcase( regFree1==0 );
 79638       testcase( regFree2==0 );
 79639       break;
 79641     case TK_IS:
 79642     case TK_ISNOT: {
 79643       testcase( op==TK_IS );
 79644       testcase( op==TK_ISNOT );
 79645       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79646       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 79647       op = (op==TK_IS) ? TK_EQ : TK_NE;
 79648       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 79649                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
 79650       VdbeCoverageIf(v, op==TK_EQ);
 79651       VdbeCoverageIf(v, op==TK_NE);
 79652       testcase( regFree1==0 );
 79653       testcase( regFree2==0 );
 79654       break;
 79656     case TK_AND:
 79657     case TK_OR:
 79658     case TK_PLUS:
 79659     case TK_STAR:
 79660     case TK_MINUS:
 79661     case TK_REM:
 79662     case TK_BITAND:
 79663     case TK_BITOR:
 79664     case TK_SLASH:
 79665     case TK_LSHIFT:
 79666     case TK_RSHIFT: 
 79667     case TK_CONCAT: {
 79668       assert( TK_AND==OP_And );            testcase( op==TK_AND );
 79669       assert( TK_OR==OP_Or );              testcase( op==TK_OR );
 79670       assert( TK_PLUS==OP_Add );           testcase( op==TK_PLUS );
 79671       assert( TK_MINUS==OP_Subtract );     testcase( op==TK_MINUS );
 79672       assert( TK_REM==OP_Remainder );      testcase( op==TK_REM );
 79673       assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
 79674       assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
 79675       assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
 79676       assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
 79677       assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
 79678       assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
 79679       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79680       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 79681       sqlite3VdbeAddOp3(v, op, r2, r1, target);
 79682       testcase( regFree1==0 );
 79683       testcase( regFree2==0 );
 79684       break;
 79686     case TK_UMINUS: {
 79687       Expr *pLeft = pExpr->pLeft;
 79688       assert( pLeft );
 79689       if( pLeft->op==TK_INTEGER ){
 79690         codeInteger(pParse, pLeft, 1, target);
 79691 #ifndef SQLITE_OMIT_FLOATING_POINT
 79692       }else if( pLeft->op==TK_FLOAT ){
 79693         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79694         codeReal(v, pLeft->u.zToken, 1, target);
 79695 #endif
 79696       }else{
 79697         tempX.op = TK_INTEGER;
 79698         tempX.flags = EP_IntValue|EP_TokenOnly;
 79699         tempX.u.iValue = 0;
 79700         r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
 79701         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
 79702         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
 79703         testcase( regFree2==0 );
 79705       inReg = target;
 79706       break;
 79708     case TK_BITNOT:
 79709     case TK_NOT: {
 79710       assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
 79711       assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
 79712       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79713       testcase( regFree1==0 );
 79714       inReg = target;
 79715       sqlite3VdbeAddOp2(v, op, r1, inReg);
 79716       break;
 79718     case TK_ISNULL:
 79719     case TK_NOTNULL: {
 79720       int addr;
 79721       assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
 79722       assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 79723       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 79724       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 79725       testcase( regFree1==0 );
 79726       addr = sqlite3VdbeAddOp1(v, op, r1);
 79727       VdbeCoverageIf(v, op==TK_ISNULL);
 79728       VdbeCoverageIf(v, op==TK_NOTNULL);
 79729       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
 79730       sqlite3VdbeJumpHere(v, addr);
 79731       break;
 79733     case TK_AGG_FUNCTION: {
 79734       AggInfo *pInfo = pExpr->pAggInfo;
 79735       if( pInfo==0 ){
 79736         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79737         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
 79738       }else{
 79739         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
 79741       break;
 79743     case TK_FUNCTION: {
 79744       ExprList *pFarg;       /* List of function arguments */
 79745       int nFarg;             /* Number of function arguments */
 79746       FuncDef *pDef;         /* The function definition object */
 79747       int nId;               /* Length of the function name in bytes */
 79748       const char *zId;       /* The function name */
 79749       u32 constMask = 0;     /* Mask of function arguments that are constant */
 79750       int i;                 /* Loop counter */
 79751       u8 enc = ENC(db);      /* The text encoding used by this database */
 79752       CollSeq *pColl = 0;    /* A collating sequence */
 79754       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 79755       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 79756         pFarg = 0;
 79757       }else{
 79758         pFarg = pExpr->x.pList;
 79760       nFarg = pFarg ? pFarg->nExpr : 0;
 79761       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 79762       zId = pExpr->u.zToken;
 79763       nId = sqlite3Strlen30(zId);
 79764       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
 79765       if( pDef==0 ){
 79766         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
 79767         break;
 79770       /* Attempt a direct implementation of the built-in COALESCE() and
 79771       ** IFNULL() functions.  This avoids unnecessary evalation of
 79772       ** arguments past the first non-NULL argument.
 79773       */
 79774       if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
 79775         int endCoalesce = sqlite3VdbeMakeLabel(v);
 79776         assert( nFarg>=2 );
 79777         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 79778         for(i=1; i<nFarg; i++){
 79779           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
 79780           VdbeCoverage(v);
 79781           sqlite3ExprCacheRemove(pParse, target, 1);
 79782           sqlite3ExprCachePush(pParse);
 79783           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
 79784           sqlite3ExprCachePop(pParse, 1);
 79786         sqlite3VdbeResolveLabel(v, endCoalesce);
 79787         break;
 79790       /* The UNLIKELY() function is a no-op.  The result is the value
 79791       ** of the first argument.
 79792       */
 79793       if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
 79794         assert( nFarg>=1 );
 79795         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
 79796         break;
 79799       for(i=0; i<nFarg; i++){
 79800         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
 79801           testcase( i==31 );
 79802           constMask |= MASKBIT32(i);
 79804         if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
 79805           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
 79808       if( pFarg ){
 79809         if( constMask ){
 79810           r1 = pParse->nMem+1;
 79811           pParse->nMem += nFarg;
 79812         }else{
 79813           r1 = sqlite3GetTempRange(pParse, nFarg);
 79816         /* For length() and typeof() functions with a column argument,
 79817         ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
 79818         ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
 79819         ** loading.
 79820         */
 79821         if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
 79822           u8 exprOp;
 79823           assert( nFarg==1 );
 79824           assert( pFarg->a[0].pExpr!=0 );
 79825           exprOp = pFarg->a[0].pExpr->op;
 79826           if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
 79827             assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
 79828             assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
 79829             testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
 79830             pFarg->a[0].pExpr->op2 = 
 79831                   pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
 79835         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
 79836         sqlite3ExprCodeExprList(pParse, pFarg, r1, 
 79837                                 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
 79838         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
 79839       }else{
 79840         r1 = 0;
 79842 #ifndef SQLITE_OMIT_VIRTUALTABLE
 79843       /* Possibly overload the function if the first argument is
 79844       ** a virtual table column.
 79845       **
 79846       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
 79847       ** second argument, not the first, as the argument to test to
 79848       ** see if it is a column in a virtual table.  This is done because
 79849       ** the left operand of infix functions (the operand we want to
 79850       ** control overloading) ends up as the second argument to the
 79851       ** function.  The expression "A glob B" is equivalent to 
 79852       ** "glob(B,A).  We want to use the A in "A glob B" to test
 79853       ** for function overloading.  But we use the B term in "glob(B,A)".
 79854       */
 79855       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
 79856         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
 79857       }else if( nFarg>0 ){
 79858         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
 79860 #endif
 79861       if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
 79862         if( !pColl ) pColl = db->pDfltColl; 
 79863         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
 79865       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
 79866                         (char*)pDef, P4_FUNCDEF);
 79867       sqlite3VdbeChangeP5(v, (u8)nFarg);
 79868       if( nFarg && constMask==0 ){
 79869         sqlite3ReleaseTempRange(pParse, r1, nFarg);
 79871       break;
 79873 #ifndef SQLITE_OMIT_SUBQUERY
 79874     case TK_EXISTS:
 79875     case TK_SELECT: {
 79876       testcase( op==TK_EXISTS );
 79877       testcase( op==TK_SELECT );
 79878       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
 79879       break;
 79881     case TK_IN: {
 79882       int destIfFalse = sqlite3VdbeMakeLabel(v);
 79883       int destIfNull = sqlite3VdbeMakeLabel(v);
 79884       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 79885       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 79886       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
 79887       sqlite3VdbeResolveLabel(v, destIfFalse);
 79888       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
 79889       sqlite3VdbeResolveLabel(v, destIfNull);
 79890       break;
 79892 #endif /* SQLITE_OMIT_SUBQUERY */
 79895     /*
 79896     **    x BETWEEN y AND z
 79897     **
 79898     ** This is equivalent to
 79899     **
 79900     **    x>=y AND x<=z
 79901     **
 79902     ** X is stored in pExpr->pLeft.
 79903     ** Y is stored in pExpr->pList->a[0].pExpr.
 79904     ** Z is stored in pExpr->pList->a[1].pExpr.
 79905     */
 79906     case TK_BETWEEN: {
 79907       Expr *pLeft = pExpr->pLeft;
 79908       struct ExprList_item *pLItem = pExpr->x.pList->a;
 79909       Expr *pRight = pLItem->pExpr;
 79911       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
 79912       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 79913       testcase( regFree1==0 );
 79914       testcase( regFree2==0 );
 79915       r3 = sqlite3GetTempReg(pParse);
 79916       r4 = sqlite3GetTempReg(pParse);
 79917       codeCompare(pParse, pLeft, pRight, OP_Ge,
 79918                   r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
 79919       pLItem++;
 79920       pRight = pLItem->pExpr;
 79921       sqlite3ReleaseTempReg(pParse, regFree2);
 79922       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
 79923       testcase( regFree2==0 );
 79924       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
 79925       VdbeCoverage(v);
 79926       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
 79927       sqlite3ReleaseTempReg(pParse, r3);
 79928       sqlite3ReleaseTempReg(pParse, r4);
 79929       break;
 79931     case TK_COLLATE: 
 79932     case TK_UPLUS: {
 79933       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
 79934       break;
 79937     case TK_TRIGGER: {
 79938       /* If the opcode is TK_TRIGGER, then the expression is a reference
 79939       ** to a column in the new.* or old.* pseudo-tables available to
 79940       ** trigger programs. In this case Expr.iTable is set to 1 for the
 79941       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 79942       ** is set to the column of the pseudo-table to read, or to -1 to
 79943       ** read the rowid field.
 79944       **
 79945       ** The expression is implemented using an OP_Param opcode. The p1
 79946       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
 79947       ** to reference another column of the old.* pseudo-table, where 
 79948       ** i is the index of the column. For a new.rowid reference, p1 is
 79949       ** set to (n+1), where n is the number of columns in each pseudo-table.
 79950       ** For a reference to any other column in the new.* pseudo-table, p1
 79951       ** is set to (n+2+i), where n and i are as defined previously. For
 79952       ** example, if the table on which triggers are being fired is
 79953       ** declared as:
 79954       **
 79955       **   CREATE TABLE t1(a, b);
 79956       **
 79957       ** Then p1 is interpreted as follows:
 79958       **
 79959       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
 79960       **   p1==1   ->    old.a         p1==4   ->    new.a
 79961       **   p1==2   ->    old.b         p1==5   ->    new.b       
 79962       */
 79963       Table *pTab = pExpr->pTab;
 79964       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
 79966       assert( pExpr->iTable==0 || pExpr->iTable==1 );
 79967       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
 79968       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
 79969       assert( p1>=0 && p1<(pTab->nCol*2+2) );
 79971       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
 79972       VdbeComment((v, "%s.%s -> $%d",
 79973         (pExpr->iTable ? "new" : "old"),
 79974         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
 79975         target
 79976       ));
 79978 #ifndef SQLITE_OMIT_FLOATING_POINT
 79979       /* If the column has REAL affinity, it may currently be stored as an
 79980       ** integer. Use OP_RealAffinity to make sure it is really real.  */
 79981       if( pExpr->iColumn>=0 
 79982        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
 79983       ){
 79984         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
 79986 #endif
 79987       break;
 79991     /*
 79992     ** Form A:
 79993     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 79994     **
 79995     ** Form B:
 79996     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
 79997     **
 79998     ** Form A is can be transformed into the equivalent form B as follows:
 79999     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
 80000     **        WHEN x=eN THEN rN ELSE y END
 80001     **
 80002     ** X (if it exists) is in pExpr->pLeft.
 80003     ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
 80004     ** odd.  The Y is also optional.  If the number of elements in x.pList
 80005     ** is even, then Y is omitted and the "otherwise" result is NULL.
 80006     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
 80007     **
 80008     ** The result of the expression is the Ri for the first matching Ei,
 80009     ** or if there is no matching Ei, the ELSE term Y, or if there is
 80010     ** no ELSE term, NULL.
 80011     */
 80012     default: assert( op==TK_CASE ); {
 80013       int endLabel;                     /* GOTO label for end of CASE stmt */
 80014       int nextCase;                     /* GOTO label for next WHEN clause */
 80015       int nExpr;                        /* 2x number of WHEN terms */
 80016       int i;                            /* Loop counter */
 80017       ExprList *pEList;                 /* List of WHEN terms */
 80018       struct ExprList_item *aListelem;  /* Array of WHEN terms */
 80019       Expr opCompare;                   /* The X==Ei expression */
 80020       Expr *pX;                         /* The X expression */
 80021       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
 80022       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
 80024       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
 80025       assert(pExpr->x.pList->nExpr > 0);
 80026       pEList = pExpr->x.pList;
 80027       aListelem = pEList->a;
 80028       nExpr = pEList->nExpr;
 80029       endLabel = sqlite3VdbeMakeLabel(v);
 80030       if( (pX = pExpr->pLeft)!=0 ){
 80031         tempX = *pX;
 80032         testcase( pX->op==TK_COLUMN );
 80033         exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
 80034         testcase( regFree1==0 );
 80035         opCompare.op = TK_EQ;
 80036         opCompare.pLeft = &tempX;
 80037         pTest = &opCompare;
 80038         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
 80039         ** The value in regFree1 might get SCopy-ed into the file result.
 80040         ** So make sure that the regFree1 register is not reused for other
 80041         ** purposes and possibly overwritten.  */
 80042         regFree1 = 0;
 80044       for(i=0; i<nExpr-1; i=i+2){
 80045         sqlite3ExprCachePush(pParse);
 80046         if( pX ){
 80047           assert( pTest!=0 );
 80048           opCompare.pRight = aListelem[i].pExpr;
 80049         }else{
 80050           pTest = aListelem[i].pExpr;
 80052         nextCase = sqlite3VdbeMakeLabel(v);
 80053         testcase( pTest->op==TK_COLUMN );
 80054         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
 80055         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
 80056         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
 80057         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
 80058         sqlite3ExprCachePop(pParse, 1);
 80059         sqlite3VdbeResolveLabel(v, nextCase);
 80061       if( (nExpr&1)!=0 ){
 80062         sqlite3ExprCachePush(pParse);
 80063         sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
 80064         sqlite3ExprCachePop(pParse, 1);
 80065       }else{
 80066         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
 80068       assert( db->mallocFailed || pParse->nErr>0 
 80069            || pParse->iCacheLevel==iCacheLevel );
 80070       sqlite3VdbeResolveLabel(v, endLabel);
 80071       break;
 80073 #ifndef SQLITE_OMIT_TRIGGER
 80074     case TK_RAISE: {
 80075       assert( pExpr->affinity==OE_Rollback 
 80076            || pExpr->affinity==OE_Abort
 80077            || pExpr->affinity==OE_Fail
 80078            || pExpr->affinity==OE_Ignore
 80079       );
 80080       if( !pParse->pTriggerTab ){
 80081         sqlite3ErrorMsg(pParse,
 80082                        "RAISE() may only be used within a trigger-program");
 80083         return 0;
 80085       if( pExpr->affinity==OE_Abort ){
 80086         sqlite3MayAbort(pParse);
 80088       assert( !ExprHasProperty(pExpr, EP_IntValue) );
 80089       if( pExpr->affinity==OE_Ignore ){
 80090         sqlite3VdbeAddOp4(
 80091             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
 80092         VdbeCoverage(v);
 80093       }else{
 80094         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
 80095                               pExpr->affinity, pExpr->u.zToken, 0, 0);
 80098       break;
 80100 #endif
 80102   sqlite3ReleaseTempReg(pParse, regFree1);
 80103   sqlite3ReleaseTempReg(pParse, regFree2);
 80104   return inReg;
 80107 /*
 80108 ** Factor out the code of the given expression to initialization time.
 80109 */
 80110 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
 80111   Parse *pParse,    /* Parsing context */
 80112   Expr *pExpr,      /* The expression to code when the VDBE initializes */
 80113   int regDest,      /* Store the value in this register */
 80114   u8 reusable       /* True if this expression is reusable */
 80115 ){
 80116   ExprList *p;
 80117   assert( ConstFactorOk(pParse) );
 80118   p = pParse->pConstExpr;
 80119   pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
 80120   p = sqlite3ExprListAppend(pParse, p, pExpr);
 80121   if( p ){
 80122      struct ExprList_item *pItem = &p->a[p->nExpr-1];
 80123      pItem->u.iConstExprReg = regDest;
 80124      pItem->reusable = reusable;
 80126   pParse->pConstExpr = p;
 80129 /*
 80130 ** Generate code to evaluate an expression and store the results
 80131 ** into a register.  Return the register number where the results
 80132 ** are stored.
 80133 **
 80134 ** If the register is a temporary register that can be deallocated,
 80135 ** then write its number into *pReg.  If the result register is not
 80136 ** a temporary, then set *pReg to zero.
 80137 **
 80138 ** If pExpr is a constant, then this routine might generate this
 80139 ** code to fill the register in the initialization section of the
 80140 ** VDBE program, in order to factor it out of the evaluation loop.
 80141 */
 80142 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
 80143   int r2;
 80144   pExpr = sqlite3ExprSkipCollate(pExpr);
 80145   if( ConstFactorOk(pParse)
 80146    && pExpr->op!=TK_REGISTER
 80147    && sqlite3ExprIsConstantNotJoin(pExpr)
 80148   ){
 80149     ExprList *p = pParse->pConstExpr;
 80150     int i;
 80151     *pReg  = 0;
 80152     if( p ){
 80153       struct ExprList_item *pItem;
 80154       for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
 80155         if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
 80156           return pItem->u.iConstExprReg;
 80160     r2 = ++pParse->nMem;
 80161     sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
 80162   }else{
 80163     int r1 = sqlite3GetTempReg(pParse);
 80164     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
 80165     if( r2==r1 ){
 80166       *pReg = r1;
 80167     }else{
 80168       sqlite3ReleaseTempReg(pParse, r1);
 80169       *pReg = 0;
 80172   return r2;
 80175 /*
 80176 ** Generate code that will evaluate expression pExpr and store the
 80177 ** results in register target.  The results are guaranteed to appear
 80178 ** in register target.
 80179 */
 80180 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
 80181   int inReg;
 80183   assert( target>0 && target<=pParse->nMem );
 80184   if( pExpr && pExpr->op==TK_REGISTER ){
 80185     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
 80186   }else{
 80187     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
 80188     assert( pParse->pVdbe || pParse->db->mallocFailed );
 80189     if( inReg!=target && pParse->pVdbe ){
 80190       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
 80195 /*
 80196 ** Generate code that will evaluate expression pExpr and store the
 80197 ** results in register target.  The results are guaranteed to appear
 80198 ** in register target.  If the expression is constant, then this routine
 80199 ** might choose to code the expression at initialization time.
 80200 */
 80201 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
 80202   if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
 80203     sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
 80204   }else{
 80205     sqlite3ExprCode(pParse, pExpr, target);
 80209 /*
 80210 ** Generate code that evalutes the given expression and puts the result
 80211 ** in register target.
 80212 **
 80213 ** Also make a copy of the expression results into another "cache" register
 80214 ** and modify the expression so that the next time it is evaluated,
 80215 ** the result is a copy of the cache register.
 80216 **
 80217 ** This routine is used for expressions that are used multiple 
 80218 ** times.  They are evaluated once and the results of the expression
 80219 ** are reused.
 80220 */
 80221 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
 80222   Vdbe *v = pParse->pVdbe;
 80223   int iMem;
 80225   assert( target>0 );
 80226   assert( pExpr->op!=TK_REGISTER );
 80227   sqlite3ExprCode(pParse, pExpr, target);
 80228   iMem = ++pParse->nMem;
 80229   sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
 80230   exprToRegister(pExpr, iMem);
 80233 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 80234 /*
 80235 ** Generate a human-readable explanation of an expression tree.
 80236 */
 80237 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
 80238   int op;                   /* The opcode being coded */
 80239   const char *zBinOp = 0;   /* Binary operator */
 80240   const char *zUniOp = 0;   /* Unary operator */
 80241   if( pExpr==0 ){
 80242     op = TK_NULL;
 80243   }else{
 80244     op = pExpr->op;
 80246   switch( op ){
 80247     case TK_AGG_COLUMN: {
 80248       sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
 80249             pExpr->iTable, pExpr->iColumn);
 80250       break;
 80252     case TK_COLUMN: {
 80253       if( pExpr->iTable<0 ){
 80254         /* This only happens when coding check constraints */
 80255         sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
 80256       }else{
 80257         sqlite3ExplainPrintf(pOut, "{%d:%d}",
 80258                              pExpr->iTable, pExpr->iColumn);
 80260       break;
 80262     case TK_INTEGER: {
 80263       if( pExpr->flags & EP_IntValue ){
 80264         sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
 80265       }else{
 80266         sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
 80268       break;
 80270 #ifndef SQLITE_OMIT_FLOATING_POINT
 80271     case TK_FLOAT: {
 80272       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 80273       break;
 80275 #endif
 80276     case TK_STRING: {
 80277       sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
 80278       break;
 80280     case TK_NULL: {
 80281       sqlite3ExplainPrintf(pOut,"NULL");
 80282       break;
 80284 #ifndef SQLITE_OMIT_BLOB_LITERAL
 80285     case TK_BLOB: {
 80286       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
 80287       break;
 80289 #endif
 80290     case TK_VARIABLE: {
 80291       sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
 80292                            pExpr->u.zToken, pExpr->iColumn);
 80293       break;
 80295     case TK_REGISTER: {
 80296       sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
 80297       break;
 80299     case TK_AS: {
 80300       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80301       break;
 80303 #ifndef SQLITE_OMIT_CAST
 80304     case TK_CAST: {
 80305       /* Expressions of the form:   CAST(pLeft AS token) */
 80306       const char *zAff = "unk";
 80307       switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
 80308         case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
 80309         case SQLITE_AFF_NONE:    zAff = "NONE";     break;
 80310         case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
 80311         case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
 80312         case SQLITE_AFF_REAL:    zAff = "REAL";     break;
 80314       sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
 80315       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80316       sqlite3ExplainPrintf(pOut, ")");
 80317       break;
 80319 #endif /* SQLITE_OMIT_CAST */
 80320     case TK_LT:      zBinOp = "LT";     break;
 80321     case TK_LE:      zBinOp = "LE";     break;
 80322     case TK_GT:      zBinOp = "GT";     break;
 80323     case TK_GE:      zBinOp = "GE";     break;
 80324     case TK_NE:      zBinOp = "NE";     break;
 80325     case TK_EQ:      zBinOp = "EQ";     break;
 80326     case TK_IS:      zBinOp = "IS";     break;
 80327     case TK_ISNOT:   zBinOp = "ISNOT";  break;
 80328     case TK_AND:     zBinOp = "AND";    break;
 80329     case TK_OR:      zBinOp = "OR";     break;
 80330     case TK_PLUS:    zBinOp = "ADD";    break;
 80331     case TK_STAR:    zBinOp = "MUL";    break;
 80332     case TK_MINUS:   zBinOp = "SUB";    break;
 80333     case TK_REM:     zBinOp = "REM";    break;
 80334     case TK_BITAND:  zBinOp = "BITAND"; break;
 80335     case TK_BITOR:   zBinOp = "BITOR";  break;
 80336     case TK_SLASH:   zBinOp = "DIV";    break;
 80337     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
 80338     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
 80339     case TK_CONCAT:  zBinOp = "CONCAT"; break;
 80341     case TK_UMINUS:  zUniOp = "UMINUS"; break;
 80342     case TK_UPLUS:   zUniOp = "UPLUS";  break;
 80343     case TK_BITNOT:  zUniOp = "BITNOT"; break;
 80344     case TK_NOT:     zUniOp = "NOT";    break;
 80345     case TK_ISNULL:  zUniOp = "ISNULL"; break;
 80346     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
 80348     case TK_COLLATE: {
 80349       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80350       sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
 80351       break;
 80354     case TK_AGG_FUNCTION:
 80355     case TK_FUNCTION: {
 80356       ExprList *pFarg;       /* List of function arguments */
 80357       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
 80358         pFarg = 0;
 80359       }else{
 80360         pFarg = pExpr->x.pList;
 80362       if( op==TK_AGG_FUNCTION ){
 80363         sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
 80364                              pExpr->op2, pExpr->u.zToken);
 80365       }else{
 80366         sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
 80368       if( pFarg ){
 80369         sqlite3ExplainExprList(pOut, pFarg);
 80371       sqlite3ExplainPrintf(pOut, ")");
 80372       break;
 80374 #ifndef SQLITE_OMIT_SUBQUERY
 80375     case TK_EXISTS: {
 80376       sqlite3ExplainPrintf(pOut, "EXISTS(");
 80377       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 80378       sqlite3ExplainPrintf(pOut,")");
 80379       break;
 80381     case TK_SELECT: {
 80382       sqlite3ExplainPrintf(pOut, "(");
 80383       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 80384       sqlite3ExplainPrintf(pOut, ")");
 80385       break;
 80387     case TK_IN: {
 80388       sqlite3ExplainPrintf(pOut, "IN(");
 80389       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80390       sqlite3ExplainPrintf(pOut, ",");
 80391       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 80392         sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
 80393       }else{
 80394         sqlite3ExplainExprList(pOut, pExpr->x.pList);
 80396       sqlite3ExplainPrintf(pOut, ")");
 80397       break;
 80399 #endif /* SQLITE_OMIT_SUBQUERY */
 80401     /*
 80402     **    x BETWEEN y AND z
 80403     **
 80404     ** This is equivalent to
 80405     **
 80406     **    x>=y AND x<=z
 80407     **
 80408     ** X is stored in pExpr->pLeft.
 80409     ** Y is stored in pExpr->pList->a[0].pExpr.
 80410     ** Z is stored in pExpr->pList->a[1].pExpr.
 80411     */
 80412     case TK_BETWEEN: {
 80413       Expr *pX = pExpr->pLeft;
 80414       Expr *pY = pExpr->x.pList->a[0].pExpr;
 80415       Expr *pZ = pExpr->x.pList->a[1].pExpr;
 80416       sqlite3ExplainPrintf(pOut, "BETWEEN(");
 80417       sqlite3ExplainExpr(pOut, pX);
 80418       sqlite3ExplainPrintf(pOut, ",");
 80419       sqlite3ExplainExpr(pOut, pY);
 80420       sqlite3ExplainPrintf(pOut, ",");
 80421       sqlite3ExplainExpr(pOut, pZ);
 80422       sqlite3ExplainPrintf(pOut, ")");
 80423       break;
 80425     case TK_TRIGGER: {
 80426       /* If the opcode is TK_TRIGGER, then the expression is a reference
 80427       ** to a column in the new.* or old.* pseudo-tables available to
 80428       ** trigger programs. In this case Expr.iTable is set to 1 for the
 80429       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
 80430       ** is set to the column of the pseudo-table to read, or to -1 to
 80431       ** read the rowid field.
 80432       */
 80433       sqlite3ExplainPrintf(pOut, "%s(%d)", 
 80434           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
 80435       break;
 80437     case TK_CASE: {
 80438       sqlite3ExplainPrintf(pOut, "CASE(");
 80439       sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80440       sqlite3ExplainPrintf(pOut, ",");
 80441       sqlite3ExplainExprList(pOut, pExpr->x.pList);
 80442       break;
 80444 #ifndef SQLITE_OMIT_TRIGGER
 80445     case TK_RAISE: {
 80446       const char *zType = "unk";
 80447       switch( pExpr->affinity ){
 80448         case OE_Rollback:   zType = "rollback";  break;
 80449         case OE_Abort:      zType = "abort";     break;
 80450         case OE_Fail:       zType = "fail";      break;
 80451         case OE_Ignore:     zType = "ignore";    break;
 80453       sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
 80454       break;
 80456 #endif
 80458   if( zBinOp ){
 80459     sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
 80460     sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80461     sqlite3ExplainPrintf(pOut,",");
 80462     sqlite3ExplainExpr(pOut, pExpr->pRight);
 80463     sqlite3ExplainPrintf(pOut,")");
 80464   }else if( zUniOp ){
 80465     sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
 80466     sqlite3ExplainExpr(pOut, pExpr->pLeft);
 80467     sqlite3ExplainPrintf(pOut,")");
 80470 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 80472 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 80473 /*
 80474 ** Generate a human-readable explanation of an expression list.
 80475 */
 80476 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
 80477   int i;
 80478   if( pList==0 || pList->nExpr==0 ){
 80479     sqlite3ExplainPrintf(pOut, "(empty-list)");
 80480     return;
 80481   }else if( pList->nExpr==1 ){
 80482     sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
 80483   }else{
 80484     sqlite3ExplainPush(pOut);
 80485     for(i=0; i<pList->nExpr; i++){
 80486       sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
 80487       sqlite3ExplainPush(pOut);
 80488       sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
 80489       sqlite3ExplainPop(pOut);
 80490       if( pList->a[i].zName ){
 80491         sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
 80493       if( pList->a[i].bSpanIsTab ){
 80494         sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
 80496       if( i<pList->nExpr-1 ){
 80497         sqlite3ExplainNL(pOut);
 80500     sqlite3ExplainPop(pOut);
 80503 #endif /* SQLITE_DEBUG */
 80505 /*
 80506 ** Generate code that pushes the value of every element of the given
 80507 ** expression list into a sequence of registers beginning at target.
 80508 **
 80509 ** Return the number of elements evaluated.
 80510 **
 80511 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
 80512 ** filled using OP_SCopy.  OP_Copy must be used instead.
 80513 **
 80514 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
 80515 ** factored out into initialization code.
 80516 */
 80517 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
 80518   Parse *pParse,     /* Parsing context */
 80519   ExprList *pList,   /* The expression list to be coded */
 80520   int target,        /* Where to write results */
 80521   u8 flags           /* SQLITE_ECEL_* flags */
 80522 ){
 80523   struct ExprList_item *pItem;
 80524   int i, n;
 80525   u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
 80526   assert( pList!=0 );
 80527   assert( target>0 );
 80528   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
 80529   n = pList->nExpr;
 80530   if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
 80531   for(pItem=pList->a, i=0; i<n; i++, pItem++){
 80532     Expr *pExpr = pItem->pExpr;
 80533     if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
 80534       sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
 80535     }else{
 80536       int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
 80537       if( inReg!=target+i ){
 80538         VdbeOp *pOp;
 80539         Vdbe *v = pParse->pVdbe;
 80540         if( copyOp==OP_Copy
 80541          && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
 80542          && pOp->p1+pOp->p3+1==inReg
 80543          && pOp->p2+pOp->p3+1==target+i
 80544         ){
 80545           pOp->p3++;
 80546         }else{
 80547           sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
 80552   return n;
 80555 /*
 80556 ** Generate code for a BETWEEN operator.
 80557 **
 80558 **    x BETWEEN y AND z
 80559 **
 80560 ** The above is equivalent to 
 80561 **
 80562 **    x>=y AND x<=z
 80563 **
 80564 ** Code it as such, taking care to do the common subexpression
 80565 ** elementation of x.
 80566 */
 80567 static void exprCodeBetween(
 80568   Parse *pParse,    /* Parsing and code generating context */
 80569   Expr *pExpr,      /* The BETWEEN expression */
 80570   int dest,         /* Jump here if the jump is taken */
 80571   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
 80572   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
 80573 ){
 80574   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
 80575   Expr compLeft;    /* The  x>=y  term */
 80576   Expr compRight;   /* The  x<=z  term */
 80577   Expr exprX;       /* The  x  subexpression */
 80578   int regFree1 = 0; /* Temporary use register */
 80580   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 80581   exprX = *pExpr->pLeft;
 80582   exprAnd.op = TK_AND;
 80583   exprAnd.pLeft = &compLeft;
 80584   exprAnd.pRight = &compRight;
 80585   compLeft.op = TK_GE;
 80586   compLeft.pLeft = &exprX;
 80587   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
 80588   compRight.op = TK_LE;
 80589   compRight.pLeft = &exprX;
 80590   compRight.pRight = pExpr->x.pList->a[1].pExpr;
 80591   exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
 80592   if( jumpIfTrue ){
 80593     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
 80594   }else{
 80595     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
 80597   sqlite3ReleaseTempReg(pParse, regFree1);
 80599   /* Ensure adequate test coverage */
 80600   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
 80601   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
 80602   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
 80603   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
 80604   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
 80605   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
 80606   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
 80607   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
 80610 /*
 80611 ** Generate code for a boolean expression such that a jump is made
 80612 ** to the label "dest" if the expression is true but execution
 80613 ** continues straight thru if the expression is false.
 80614 **
 80615 ** If the expression evaluates to NULL (neither true nor false), then
 80616 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
 80617 **
 80618 ** This code depends on the fact that certain token values (ex: TK_EQ)
 80619 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
 80620 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
 80621 ** the make process cause these values to align.  Assert()s in the code
 80622 ** below verify that the numbers are aligned correctly.
 80623 */
 80624 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 80625   Vdbe *v = pParse->pVdbe;
 80626   int op = 0;
 80627   int regFree1 = 0;
 80628   int regFree2 = 0;
 80629   int r1, r2;
 80631   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 80632   if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
 80633   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
 80634   op = pExpr->op;
 80635   switch( op ){
 80636     case TK_AND: {
 80637       int d2 = sqlite3VdbeMakeLabel(v);
 80638       testcase( jumpIfNull==0 );
 80639       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
 80640       sqlite3ExprCachePush(pParse);
 80641       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 80642       sqlite3VdbeResolveLabel(v, d2);
 80643       sqlite3ExprCachePop(pParse, 1);
 80644       break;
 80646     case TK_OR: {
 80647       testcase( jumpIfNull==0 );
 80648       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 80649       sqlite3ExprCachePush(pParse);
 80650       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
 80651       sqlite3ExprCachePop(pParse, 1);
 80652       break;
 80654     case TK_NOT: {
 80655       testcase( jumpIfNull==0 );
 80656       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 80657       break;
 80659     case TK_LT:
 80660     case TK_LE:
 80661     case TK_GT:
 80662     case TK_GE:
 80663     case TK_NE:
 80664     case TK_EQ: {
 80665       testcase( jumpIfNull==0 );
 80666       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80667       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80668       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80669                   r1, r2, dest, jumpIfNull);
 80670       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 80671       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
 80672       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
 80673       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
 80674       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
 80675       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 80676       testcase( regFree1==0 );
 80677       testcase( regFree2==0 );
 80678       break;
 80680     case TK_IS:
 80681     case TK_ISNOT: {
 80682       testcase( op==TK_IS );
 80683       testcase( op==TK_ISNOT );
 80684       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80685       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80686       op = (op==TK_IS) ? TK_EQ : TK_NE;
 80687       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80688                   r1, r2, dest, SQLITE_NULLEQ);
 80689       VdbeCoverageIf(v, op==TK_EQ);
 80690       VdbeCoverageIf(v, op==TK_NE);
 80691       testcase( regFree1==0 );
 80692       testcase( regFree2==0 );
 80693       break;
 80695     case TK_ISNULL:
 80696     case TK_NOTNULL: {
 80697       assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
 80698       assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
 80699       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80700       sqlite3VdbeAddOp2(v, op, r1, dest);
 80701       VdbeCoverageIf(v, op==TK_ISNULL);
 80702       VdbeCoverageIf(v, op==TK_NOTNULL);
 80703       testcase( regFree1==0 );
 80704       break;
 80706     case TK_BETWEEN: {
 80707       testcase( jumpIfNull==0 );
 80708       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
 80709       break;
 80711 #ifndef SQLITE_OMIT_SUBQUERY
 80712     case TK_IN: {
 80713       int destIfFalse = sqlite3VdbeMakeLabel(v);
 80714       int destIfNull = jumpIfNull ? dest : destIfFalse;
 80715       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 80716       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 80717       sqlite3VdbeResolveLabel(v, destIfFalse);
 80718       break;
 80720 #endif
 80721     default: {
 80722       if( exprAlwaysTrue(pExpr) ){
 80723         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 80724       }else if( exprAlwaysFalse(pExpr) ){
 80725         /* No-op */
 80726       }else{
 80727         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 80728         sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
 80729         VdbeCoverage(v);
 80730         testcase( regFree1==0 );
 80731         testcase( jumpIfNull==0 );
 80733       break;
 80736   sqlite3ReleaseTempReg(pParse, regFree1);
 80737   sqlite3ReleaseTempReg(pParse, regFree2);  
 80740 /*
 80741 ** Generate code for a boolean expression such that a jump is made
 80742 ** to the label "dest" if the expression is false but execution
 80743 ** continues straight thru if the expression is true.
 80744 **
 80745 ** If the expression evaluates to NULL (neither true nor false) then
 80746 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
 80747 ** is 0.
 80748 */
 80749 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
 80750   Vdbe *v = pParse->pVdbe;
 80751   int op = 0;
 80752   int regFree1 = 0;
 80753   int regFree2 = 0;
 80754   int r1, r2;
 80756   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
 80757   if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
 80758   if( pExpr==0 )    return;
 80760   /* The value of pExpr->op and op are related as follows:
 80761   **
 80762   **       pExpr->op            op
 80763   **       ---------          ----------
 80764   **       TK_ISNULL          OP_NotNull
 80765   **       TK_NOTNULL         OP_IsNull
 80766   **       TK_NE              OP_Eq
 80767   **       TK_EQ              OP_Ne
 80768   **       TK_GT              OP_Le
 80769   **       TK_LE              OP_Gt
 80770   **       TK_GE              OP_Lt
 80771   **       TK_LT              OP_Ge
 80772   **
 80773   ** For other values of pExpr->op, op is undefined and unused.
 80774   ** The value of TK_ and OP_ constants are arranged such that we
 80775   ** can compute the mapping above using the following expression.
 80776   ** Assert()s verify that the computation is correct.
 80777   */
 80778   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
 80780   /* Verify correct alignment of TK_ and OP_ constants
 80781   */
 80782   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
 80783   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
 80784   assert( pExpr->op!=TK_NE || op==OP_Eq );
 80785   assert( pExpr->op!=TK_EQ || op==OP_Ne );
 80786   assert( pExpr->op!=TK_LT || op==OP_Ge );
 80787   assert( pExpr->op!=TK_LE || op==OP_Gt );
 80788   assert( pExpr->op!=TK_GT || op==OP_Le );
 80789   assert( pExpr->op!=TK_GE || op==OP_Lt );
 80791   switch( pExpr->op ){
 80792     case TK_AND: {
 80793       testcase( jumpIfNull==0 );
 80794       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
 80795       sqlite3ExprCachePush(pParse);
 80796       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 80797       sqlite3ExprCachePop(pParse, 1);
 80798       break;
 80800     case TK_OR: {
 80801       int d2 = sqlite3VdbeMakeLabel(v);
 80802       testcase( jumpIfNull==0 );
 80803       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
 80804       sqlite3ExprCachePush(pParse);
 80805       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
 80806       sqlite3VdbeResolveLabel(v, d2);
 80807       sqlite3ExprCachePop(pParse, 1);
 80808       break;
 80810     case TK_NOT: {
 80811       testcase( jumpIfNull==0 );
 80812       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
 80813       break;
 80815     case TK_LT:
 80816     case TK_LE:
 80817     case TK_GT:
 80818     case TK_GE:
 80819     case TK_NE:
 80820     case TK_EQ: {
 80821       testcase( jumpIfNull==0 );
 80822       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80823       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80824       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80825                   r1, r2, dest, jumpIfNull);
 80826       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
 80827       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
 80828       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
 80829       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
 80830       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
 80831       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
 80832       testcase( regFree1==0 );
 80833       testcase( regFree2==0 );
 80834       break;
 80836     case TK_IS:
 80837     case TK_ISNOT: {
 80838       testcase( pExpr->op==TK_IS );
 80839       testcase( pExpr->op==TK_ISNOT );
 80840       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80841       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
 80842       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
 80843       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
 80844                   r1, r2, dest, SQLITE_NULLEQ);
 80845       VdbeCoverageIf(v, op==TK_EQ);
 80846       VdbeCoverageIf(v, op==TK_NE);
 80847       testcase( regFree1==0 );
 80848       testcase( regFree2==0 );
 80849       break;
 80851     case TK_ISNULL:
 80852     case TK_NOTNULL: {
 80853       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 80854       sqlite3VdbeAddOp2(v, op, r1, dest);
 80855       testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
 80856       testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
 80857       testcase( regFree1==0 );
 80858       break;
 80860     case TK_BETWEEN: {
 80861       testcase( jumpIfNull==0 );
 80862       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
 80863       break;
 80865 #ifndef SQLITE_OMIT_SUBQUERY
 80866     case TK_IN: {
 80867       if( jumpIfNull ){
 80868         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 80869       }else{
 80870         int destIfNull = sqlite3VdbeMakeLabel(v);
 80871         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 80872         sqlite3VdbeResolveLabel(v, destIfNull);
 80874       break;
 80876 #endif
 80877     default: {
 80878       if( exprAlwaysFalse(pExpr) ){
 80879         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 80880       }else if( exprAlwaysTrue(pExpr) ){
 80881         /* no-op */
 80882       }else{
 80883         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 80884         sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
 80885         VdbeCoverage(v);
 80886         testcase( regFree1==0 );
 80887         testcase( jumpIfNull==0 );
 80889       break;
 80892   sqlite3ReleaseTempReg(pParse, regFree1);
 80893   sqlite3ReleaseTempReg(pParse, regFree2);
 80896 /*
 80897 ** Do a deep comparison of two expression trees.  Return 0 if the two
 80898 ** expressions are completely identical.  Return 1 if they differ only
 80899 ** by a COLLATE operator at the top level.  Return 2 if there are differences
 80900 ** other than the top-level COLLATE operator.
 80901 **
 80902 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
 80903 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
 80904 **
 80905 ** The pA side might be using TK_REGISTER.  If that is the case and pB is
 80906 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
 80907 **
 80908 ** Sometimes this routine will return 2 even if the two expressions
 80909 ** really are equivalent.  If we cannot prove that the expressions are
 80910 ** identical, we return 2 just to be safe.  So if this routine
 80911 ** returns 2, then you do not really know for certain if the two
 80912 ** expressions are the same.  But if you get a 0 or 1 return, then you
 80913 ** can be sure the expressions are the same.  In the places where
 80914 ** this routine is used, it does not hurt to get an extra 2 - that
 80915 ** just might result in some slightly slower code.  But returning
 80916 ** an incorrect 0 or 1 could lead to a malfunction.
 80917 */
 80918 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
 80919   u32 combinedFlags;
 80920   if( pA==0 || pB==0 ){
 80921     return pB==pA ? 0 : 2;
 80923   combinedFlags = pA->flags | pB->flags;
 80924   if( combinedFlags & EP_IntValue ){
 80925     if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
 80926       return 0;
 80928     return 2;
 80930   if( pA->op!=pB->op ){
 80931     if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
 80932       return 1;
 80934     if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
 80935       return 1;
 80937     return 2;
 80939   if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
 80940     if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
 80941       return pA->op==TK_COLLATE ? 1 : 2;
 80944   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
 80945   if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
 80946     if( combinedFlags & EP_xIsSelect ) return 2;
 80947     if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
 80948     if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
 80949     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
 80950     if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
 80951       if( pA->iColumn!=pB->iColumn ) return 2;
 80952       if( pA->iTable!=pB->iTable 
 80953        && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
 80956   return 0;
 80959 /*
 80960 ** Compare two ExprList objects.  Return 0 if they are identical and 
 80961 ** non-zero if they differ in any way.
 80962 **
 80963 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
 80964 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
 80965 **
 80966 ** This routine might return non-zero for equivalent ExprLists.  The
 80967 ** only consequence will be disabled optimizations.  But this routine
 80968 ** must never return 0 if the two ExprList objects are different, or
 80969 ** a malfunction will result.
 80970 **
 80971 ** Two NULL pointers are considered to be the same.  But a NULL pointer
 80972 ** always differs from a non-NULL pointer.
 80973 */
 80974 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
 80975   int i;
 80976   if( pA==0 && pB==0 ) return 0;
 80977   if( pA==0 || pB==0 ) return 1;
 80978   if( pA->nExpr!=pB->nExpr ) return 1;
 80979   for(i=0; i<pA->nExpr; i++){
 80980     Expr *pExprA = pA->a[i].pExpr;
 80981     Expr *pExprB = pB->a[i].pExpr;
 80982     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
 80983     if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
 80985   return 0;
 80988 /*
 80989 ** Return true if we can prove the pE2 will always be true if pE1 is
 80990 ** true.  Return false if we cannot complete the proof or if pE2 might
 80991 ** be false.  Examples:
 80992 **
 80993 **     pE1: x==5       pE2: x==5             Result: true
 80994 **     pE1: x>0        pE2: x==5             Result: false
 80995 **     pE1: x=21       pE2: x=21 OR y=43     Result: true
 80996 **     pE1: x!=123     pE2: x IS NOT NULL    Result: true
 80997 **     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
 80998 **     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
 80999 **     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
 81000 **
 81001 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
 81002 ** Expr.iTable<0 then assume a table number given by iTab.
 81003 **
 81004 ** When in doubt, return false.  Returning true might give a performance
 81005 ** improvement.  Returning false might cause a performance reduction, but
 81006 ** it will always give the correct answer and is hence always safe.
 81007 */
 81008 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
 81009   if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
 81010     return 1;
 81012   if( pE2->op==TK_OR
 81013    && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
 81014              || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
 81015   ){
 81016     return 1;
 81018   if( pE2->op==TK_NOTNULL
 81019    && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
 81020    && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
 81021   ){
 81022     return 1;
 81024   return 0;
 81027 /*
 81028 ** An instance of the following structure is used by the tree walker
 81029 ** to count references to table columns in the arguments of an 
 81030 ** aggregate function, in order to implement the
 81031 ** sqlite3FunctionThisSrc() routine.
 81032 */
 81033 struct SrcCount {
 81034   SrcList *pSrc;   /* One particular FROM clause in a nested query */
 81035   int nThis;       /* Number of references to columns in pSrcList */
 81036   int nOther;      /* Number of references to columns in other FROM clauses */
 81037 };
 81039 /*
 81040 ** Count the number of references to columns.
 81041 */
 81042 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
 81043   /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
 81044   ** is always called before sqlite3ExprAnalyzeAggregates() and so the
 81045   ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
 81046   ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
 81047   ** NEVER() will need to be removed. */
 81048   if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
 81049     int i;
 81050     struct SrcCount *p = pWalker->u.pSrcCount;
 81051     SrcList *pSrc = p->pSrc;
 81052     for(i=0; i<pSrc->nSrc; i++){
 81053       if( pExpr->iTable==pSrc->a[i].iCursor ) break;
 81055     if( i<pSrc->nSrc ){
 81056       p->nThis++;
 81057     }else{
 81058       p->nOther++;
 81061   return WRC_Continue;
 81064 /*
 81065 ** Determine if any of the arguments to the pExpr Function reference
 81066 ** pSrcList.  Return true if they do.  Also return true if the function
 81067 ** has no arguments or has only constant arguments.  Return false if pExpr
 81068 ** references columns but not columns of tables found in pSrcList.
 81069 */
 81070 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
 81071   Walker w;
 81072   struct SrcCount cnt;
 81073   assert( pExpr->op==TK_AGG_FUNCTION );
 81074   memset(&w, 0, sizeof(w));
 81075   w.xExprCallback = exprSrcCount;
 81076   w.u.pSrcCount = &cnt;
 81077   cnt.pSrc = pSrcList;
 81078   cnt.nThis = 0;
 81079   cnt.nOther = 0;
 81080   sqlite3WalkExprList(&w, pExpr->x.pList);
 81081   return cnt.nThis>0 || cnt.nOther==0;
 81084 /*
 81085 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
 81086 ** the new element.  Return a negative number if malloc fails.
 81087 */
 81088 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
 81089   int i;
 81090   pInfo->aCol = sqlite3ArrayAllocate(
 81091        db,
 81092        pInfo->aCol,
 81093        sizeof(pInfo->aCol[0]),
 81094        &pInfo->nColumn,
 81095        &i
 81096   );
 81097   return i;
 81100 /*
 81101 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
 81102 ** the new element.  Return a negative number if malloc fails.
 81103 */
 81104 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
 81105   int i;
 81106   pInfo->aFunc = sqlite3ArrayAllocate(
 81107        db, 
 81108        pInfo->aFunc,
 81109        sizeof(pInfo->aFunc[0]),
 81110        &pInfo->nFunc,
 81111        &i
 81112   );
 81113   return i;
 81116 /*
 81117 ** This is the xExprCallback for a tree walker.  It is used to
 81118 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
 81119 ** for additional information.
 81120 */
 81121 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
 81122   int i;
 81123   NameContext *pNC = pWalker->u.pNC;
 81124   Parse *pParse = pNC->pParse;
 81125   SrcList *pSrcList = pNC->pSrcList;
 81126   AggInfo *pAggInfo = pNC->pAggInfo;
 81128   switch( pExpr->op ){
 81129     case TK_AGG_COLUMN:
 81130     case TK_COLUMN: {
 81131       testcase( pExpr->op==TK_AGG_COLUMN );
 81132       testcase( pExpr->op==TK_COLUMN );
 81133       /* Check to see if the column is in one of the tables in the FROM
 81134       ** clause of the aggregate query */
 81135       if( ALWAYS(pSrcList!=0) ){
 81136         struct SrcList_item *pItem = pSrcList->a;
 81137         for(i=0; i<pSrcList->nSrc; i++, pItem++){
 81138           struct AggInfo_col *pCol;
 81139           assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 81140           if( pExpr->iTable==pItem->iCursor ){
 81141             /* If we reach this point, it means that pExpr refers to a table
 81142             ** that is in the FROM clause of the aggregate query.  
 81143             **
 81144             ** Make an entry for the column in pAggInfo->aCol[] if there
 81145             ** is not an entry there already.
 81146             */
 81147             int k;
 81148             pCol = pAggInfo->aCol;
 81149             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
 81150               if( pCol->iTable==pExpr->iTable &&
 81151                   pCol->iColumn==pExpr->iColumn ){
 81152                 break;
 81155             if( (k>=pAggInfo->nColumn)
 81156              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
 81157             ){
 81158               pCol = &pAggInfo->aCol[k];
 81159               pCol->pTab = pExpr->pTab;
 81160               pCol->iTable = pExpr->iTable;
 81161               pCol->iColumn = pExpr->iColumn;
 81162               pCol->iMem = ++pParse->nMem;
 81163               pCol->iSorterColumn = -1;
 81164               pCol->pExpr = pExpr;
 81165               if( pAggInfo->pGroupBy ){
 81166                 int j, n;
 81167                 ExprList *pGB = pAggInfo->pGroupBy;
 81168                 struct ExprList_item *pTerm = pGB->a;
 81169                 n = pGB->nExpr;
 81170                 for(j=0; j<n; j++, pTerm++){
 81171                   Expr *pE = pTerm->pExpr;
 81172                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
 81173                       pE->iColumn==pExpr->iColumn ){
 81174                     pCol->iSorterColumn = j;
 81175                     break;
 81179               if( pCol->iSorterColumn<0 ){
 81180                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
 81183             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
 81184             ** because it was there before or because we just created it).
 81185             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
 81186             ** pAggInfo->aCol[] entry.
 81187             */
 81188             ExprSetVVAProperty(pExpr, EP_NoReduce);
 81189             pExpr->pAggInfo = pAggInfo;
 81190             pExpr->op = TK_AGG_COLUMN;
 81191             pExpr->iAgg = (i16)k;
 81192             break;
 81193           } /* endif pExpr->iTable==pItem->iCursor */
 81194         } /* end loop over pSrcList */
 81196       return WRC_Prune;
 81198     case TK_AGG_FUNCTION: {
 81199       if( (pNC->ncFlags & NC_InAggFunc)==0
 81200        && pWalker->walkerDepth==pExpr->op2
 81201       ){
 81202         /* Check to see if pExpr is a duplicate of another aggregate 
 81203         ** function that is already in the pAggInfo structure
 81204         */
 81205         struct AggInfo_func *pItem = pAggInfo->aFunc;
 81206         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
 81207           if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
 81208             break;
 81211         if( i>=pAggInfo->nFunc ){
 81212           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
 81213           */
 81214           u8 enc = ENC(pParse->db);
 81215           i = addAggInfoFunc(pParse->db, pAggInfo);
 81216           if( i>=0 ){
 81217             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 81218             pItem = &pAggInfo->aFunc[i];
 81219             pItem->pExpr = pExpr;
 81220             pItem->iMem = ++pParse->nMem;
 81221             assert( !ExprHasProperty(pExpr, EP_IntValue) );
 81222             pItem->pFunc = sqlite3FindFunction(pParse->db,
 81223                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
 81224                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
 81225             if( pExpr->flags & EP_Distinct ){
 81226               pItem->iDistinct = pParse->nTab++;
 81227             }else{
 81228               pItem->iDistinct = -1;
 81232         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
 81233         */
 81234         assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
 81235         ExprSetVVAProperty(pExpr, EP_NoReduce);
 81236         pExpr->iAgg = (i16)i;
 81237         pExpr->pAggInfo = pAggInfo;
 81238         return WRC_Prune;
 81239       }else{
 81240         return WRC_Continue;
 81244   return WRC_Continue;
 81246 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
 81247   UNUSED_PARAMETER(pWalker);
 81248   UNUSED_PARAMETER(pSelect);
 81249   return WRC_Continue;
 81252 /*
 81253 ** Analyze the pExpr expression looking for aggregate functions and
 81254 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
 81255 ** points to.  Additional entries are made on the AggInfo object as
 81256 ** necessary.
 81257 **
 81258 ** This routine should only be called after the expression has been
 81259 ** analyzed by sqlite3ResolveExprNames().
 81260 */
 81261 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
 81262   Walker w;
 81263   memset(&w, 0, sizeof(w));
 81264   w.xExprCallback = analyzeAggregate;
 81265   w.xSelectCallback = analyzeAggregatesInSelect;
 81266   w.u.pNC = pNC;
 81267   assert( pNC->pSrcList!=0 );
 81268   sqlite3WalkExpr(&w, pExpr);
 81271 /*
 81272 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
 81273 ** expression list.  Return the number of errors.
 81274 **
 81275 ** If an error is found, the analysis is cut short.
 81276 */
 81277 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
 81278   struct ExprList_item *pItem;
 81279   int i;
 81280   if( pList ){
 81281     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
 81282       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
 81287 /*
 81288 ** Allocate a single new register for use to hold some intermediate result.
 81289 */
 81290 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
 81291   if( pParse->nTempReg==0 ){
 81292     return ++pParse->nMem;
 81294   return pParse->aTempReg[--pParse->nTempReg];
 81297 /*
 81298 ** Deallocate a register, making available for reuse for some other
 81299 ** purpose.
 81300 **
 81301 ** If a register is currently being used by the column cache, then
 81302 ** the dallocation is deferred until the column cache line that uses
 81303 ** the register becomes stale.
 81304 */
 81305 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
 81306   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
 81307     int i;
 81308     struct yColCache *p;
 81309     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
 81310       if( p->iReg==iReg ){
 81311         p->tempReg = 1;
 81312         return;
 81315     pParse->aTempReg[pParse->nTempReg++] = iReg;
 81319 /*
 81320 ** Allocate or deallocate a block of nReg consecutive registers
 81321 */
 81322 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
 81323   int i, n;
 81324   i = pParse->iRangeReg;
 81325   n = pParse->nRangeReg;
 81326   if( nReg<=n ){
 81327     assert( !usedAsColumnCache(pParse, i, i+n-1) );
 81328     pParse->iRangeReg += nReg;
 81329     pParse->nRangeReg -= nReg;
 81330   }else{
 81331     i = pParse->nMem+1;
 81332     pParse->nMem += nReg;
 81334   return i;
 81336 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
 81337   sqlite3ExprCacheRemove(pParse, iReg, nReg);
 81338   if( nReg>pParse->nRangeReg ){
 81339     pParse->nRangeReg = nReg;
 81340     pParse->iRangeReg = iReg;
 81344 /*
 81345 ** Mark all temporary registers as being unavailable for reuse.
 81346 */
 81347 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
 81348   pParse->nTempReg = 0;
 81349   pParse->nRangeReg = 0;
 81352 /************** End of expr.c ************************************************/
 81353 /************** Begin file alter.c *******************************************/
 81354 /*
 81355 ** 2005 February 15
 81356 **
 81357 ** The author disclaims copyright to this source code.  In place of
 81358 ** a legal notice, here is a blessing:
 81359 **
 81360 **    May you do good and not evil.
 81361 **    May you find forgiveness for yourself and forgive others.
 81362 **    May you share freely, never taking more than you give.
 81363 **
 81364 *************************************************************************
 81365 ** This file contains C code routines that used to generate VDBE code
 81366 ** that implements the ALTER TABLE command.
 81367 */
 81369 /*
 81370 ** The code in this file only exists if we are not omitting the
 81371 ** ALTER TABLE logic from the build.
 81372 */
 81373 #ifndef SQLITE_OMIT_ALTERTABLE
 81376 /*
 81377 ** This function is used by SQL generated to implement the 
 81378 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
 81379 ** CREATE INDEX command. The second is a table name. The table name in 
 81380 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
 81381 ** argument and the result returned. Examples:
 81382 **
 81383 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
 81384 **     -> 'CREATE TABLE def(a, b, c)'
 81385 **
 81386 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
 81387 **     -> 'CREATE INDEX i ON def(a, b, c)'
 81388 */
 81389 static void renameTableFunc(
 81390   sqlite3_context *context,
 81391   int NotUsed,
 81392   sqlite3_value **argv
 81393 ){
 81394   unsigned char const *zSql = sqlite3_value_text(argv[0]);
 81395   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 81397   int token;
 81398   Token tname;
 81399   unsigned char const *zCsr = zSql;
 81400   int len = 0;
 81401   char *zRet;
 81403   sqlite3 *db = sqlite3_context_db_handle(context);
 81405   UNUSED_PARAMETER(NotUsed);
 81407   /* The principle used to locate the table name in the CREATE TABLE 
 81408   ** statement is that the table name is the first non-space token that
 81409   ** is immediately followed by a TK_LP or TK_USING token.
 81410   */
 81411   if( zSql ){
 81412     do {
 81413       if( !*zCsr ){
 81414         /* Ran out of input before finding an opening bracket. Return NULL. */
 81415         return;
 81418       /* Store the token that zCsr points to in tname. */
 81419       tname.z = (char*)zCsr;
 81420       tname.n = len;
 81422       /* Advance zCsr to the next token. Store that token type in 'token',
 81423       ** and its length in 'len' (to be used next iteration of this loop).
 81424       */
 81425       do {
 81426         zCsr += len;
 81427         len = sqlite3GetToken(zCsr, &token);
 81428       } while( token==TK_SPACE );
 81429       assert( len>0 );
 81430     } while( token!=TK_LP && token!=TK_USING );
 81432     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
 81433        zSql, zTableName, tname.z+tname.n);
 81434     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 81438 /*
 81439 ** This C function implements an SQL user function that is used by SQL code
 81440 ** generated by the ALTER TABLE ... RENAME command to modify the definition
 81441 ** of any foreign key constraints that use the table being renamed as the 
 81442 ** parent table. It is passed three arguments:
 81443 **
 81444 **   1) The complete text of the CREATE TABLE statement being modified,
 81445 **   2) The old name of the table being renamed, and
 81446 **   3) The new name of the table being renamed.
 81447 **
 81448 ** It returns the new CREATE TABLE statement. For example:
 81449 **
 81450 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
 81451 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
 81452 */
 81453 #ifndef SQLITE_OMIT_FOREIGN_KEY
 81454 static void renameParentFunc(
 81455   sqlite3_context *context,
 81456   int NotUsed,
 81457   sqlite3_value **argv
 81458 ){
 81459   sqlite3 *db = sqlite3_context_db_handle(context);
 81460   char *zOutput = 0;
 81461   char *zResult;
 81462   unsigned char const *zInput = sqlite3_value_text(argv[0]);
 81463   unsigned char const *zOld = sqlite3_value_text(argv[1]);
 81464   unsigned char const *zNew = sqlite3_value_text(argv[2]);
 81466   unsigned const char *z;         /* Pointer to token */
 81467   int n;                          /* Length of token z */
 81468   int token;                      /* Type of token */
 81470   UNUSED_PARAMETER(NotUsed);
 81471   for(z=zInput; *z; z=z+n){
 81472     n = sqlite3GetToken(z, &token);
 81473     if( token==TK_REFERENCES ){
 81474       char *zParent;
 81475       do {
 81476         z += n;
 81477         n = sqlite3GetToken(z, &token);
 81478       }while( token==TK_SPACE );
 81480       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
 81481       if( zParent==0 ) break;
 81482       sqlite3Dequote(zParent);
 81483       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
 81484         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
 81485             (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
 81486         );
 81487         sqlite3DbFree(db, zOutput);
 81488         zOutput = zOut;
 81489         zInput = &z[n];
 81491       sqlite3DbFree(db, zParent);
 81495   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
 81496   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
 81497   sqlite3DbFree(db, zOutput);
 81499 #endif
 81501 #ifndef SQLITE_OMIT_TRIGGER
 81502 /* This function is used by SQL generated to implement the
 81503 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
 81504 ** statement. The second is a table name. The table name in the CREATE 
 81505 ** TRIGGER statement is replaced with the third argument and the result 
 81506 ** returned. This is analagous to renameTableFunc() above, except for CREATE
 81507 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
 81508 */
 81509 static void renameTriggerFunc(
 81510   sqlite3_context *context,
 81511   int NotUsed,
 81512   sqlite3_value **argv
 81513 ){
 81514   unsigned char const *zSql = sqlite3_value_text(argv[0]);
 81515   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
 81517   int token;
 81518   Token tname;
 81519   int dist = 3;
 81520   unsigned char const *zCsr = zSql;
 81521   int len = 0;
 81522   char *zRet;
 81523   sqlite3 *db = sqlite3_context_db_handle(context);
 81525   UNUSED_PARAMETER(NotUsed);
 81527   /* The principle used to locate the table name in the CREATE TRIGGER 
 81528   ** statement is that the table name is the first token that is immediatedly
 81529   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
 81530   ** of TK_WHEN, TK_BEGIN or TK_FOR.
 81531   */
 81532   if( zSql ){
 81533     do {
 81535       if( !*zCsr ){
 81536         /* Ran out of input before finding the table name. Return NULL. */
 81537         return;
 81540       /* Store the token that zCsr points to in tname. */
 81541       tname.z = (char*)zCsr;
 81542       tname.n = len;
 81544       /* Advance zCsr to the next token. Store that token type in 'token',
 81545       ** and its length in 'len' (to be used next iteration of this loop).
 81546       */
 81547       do {
 81548         zCsr += len;
 81549         len = sqlite3GetToken(zCsr, &token);
 81550       }while( token==TK_SPACE );
 81551       assert( len>0 );
 81553       /* Variable 'dist' stores the number of tokens read since the most
 81554       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
 81555       ** token is read and 'dist' equals 2, the condition stated above
 81556       ** to be met.
 81557       **
 81558       ** Note that ON cannot be a database, table or column name, so
 81559       ** there is no need to worry about syntax like 
 81560       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
 81561       */
 81562       dist++;
 81563       if( token==TK_DOT || token==TK_ON ){
 81564         dist = 0;
 81566     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
 81568     /* Variable tname now contains the token that is the old table-name
 81569     ** in the CREATE TRIGGER statement.
 81570     */
 81571     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
 81572        zSql, zTableName, tname.z+tname.n);
 81573     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
 81576 #endif   /* !SQLITE_OMIT_TRIGGER */
 81578 /*
 81579 ** Register built-in functions used to help implement ALTER TABLE
 81580 */
 81581 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
 81582   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
 81583     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
 81584 #ifndef SQLITE_OMIT_TRIGGER
 81585     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
 81586 #endif
 81587 #ifndef SQLITE_OMIT_FOREIGN_KEY
 81588     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
 81589 #endif
 81590   };
 81591   int i;
 81592   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 81593   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
 81595   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
 81596     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 81600 /*
 81601 ** This function is used to create the text of expressions of the form:
 81602 **
 81603 **   name=<constant1> OR name=<constant2> OR ...
 81604 **
 81605 ** If argument zWhere is NULL, then a pointer string containing the text 
 81606 ** "name=<constant>" is returned, where <constant> is the quoted version
 81607 ** of the string passed as argument zConstant. The returned buffer is
 81608 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
 81609 ** caller to ensure that it is eventually freed.
 81610 **
 81611 ** If argument zWhere is not NULL, then the string returned is 
 81612 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
 81613 ** In this case zWhere is passed to sqlite3DbFree() before returning.
 81614 ** 
 81615 */
 81616 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
 81617   char *zNew;
 81618   if( !zWhere ){
 81619     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
 81620   }else{
 81621     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
 81622     sqlite3DbFree(db, zWhere);
 81624   return zNew;
 81627 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 81628 /*
 81629 ** Generate the text of a WHERE expression which can be used to select all
 81630 ** tables that have foreign key constraints that refer to table pTab (i.e.
 81631 ** constraints for which pTab is the parent table) from the sqlite_master
 81632 ** table.
 81633 */
 81634 static char *whereForeignKeys(Parse *pParse, Table *pTab){
 81635   FKey *p;
 81636   char *zWhere = 0;
 81637   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 81638     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
 81640   return zWhere;
 81642 #endif
 81644 /*
 81645 ** Generate the text of a WHERE expression which can be used to select all
 81646 ** temporary triggers on table pTab from the sqlite_temp_master table. If
 81647 ** table pTab has no temporary triggers, or is itself stored in the 
 81648 ** temporary database, NULL is returned.
 81649 */
 81650 static char *whereTempTriggers(Parse *pParse, Table *pTab){
 81651   Trigger *pTrig;
 81652   char *zWhere = 0;
 81653   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
 81655   /* If the table is not located in the temp-db (in which case NULL is 
 81656   ** returned, loop through the tables list of triggers. For each trigger
 81657   ** that is not part of the temp-db schema, add a clause to the WHERE 
 81658   ** expression being built up in zWhere.
 81659   */
 81660   if( pTab->pSchema!=pTempSchema ){
 81661     sqlite3 *db = pParse->db;
 81662     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 81663       if( pTrig->pSchema==pTempSchema ){
 81664         zWhere = whereOrName(db, zWhere, pTrig->zName);
 81668   if( zWhere ){
 81669     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
 81670     sqlite3DbFree(pParse->db, zWhere);
 81671     zWhere = zNew;
 81673   return zWhere;
 81676 /*
 81677 ** Generate code to drop and reload the internal representation of table
 81678 ** pTab from the database, including triggers and temporary triggers.
 81679 ** Argument zName is the name of the table in the database schema at
 81680 ** the time the generated code is executed. This can be different from
 81681 ** pTab->zName if this function is being called to code part of an 
 81682 ** "ALTER TABLE RENAME TO" statement.
 81683 */
 81684 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
 81685   Vdbe *v;
 81686   char *zWhere;
 81687   int iDb;                   /* Index of database containing pTab */
 81688 #ifndef SQLITE_OMIT_TRIGGER
 81689   Trigger *pTrig;
 81690 #endif
 81692   v = sqlite3GetVdbe(pParse);
 81693   if( NEVER(v==0) ) return;
 81694   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 81695   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 81696   assert( iDb>=0 );
 81698 #ifndef SQLITE_OMIT_TRIGGER
 81699   /* Drop any table triggers from the internal schema. */
 81700   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
 81701     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 81702     assert( iTrigDb==iDb || iTrigDb==1 );
 81703     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
 81705 #endif
 81707   /* Drop the table and index from the internal schema.  */
 81708   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 81710   /* Reload the table, index and permanent trigger schemas. */
 81711   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
 81712   if( !zWhere ) return;
 81713   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
 81715 #ifndef SQLITE_OMIT_TRIGGER
 81716   /* Now, if the table is not stored in the temp database, reload any temp 
 81717   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
 81718   */
 81719   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 81720     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
 81722 #endif
 81725 /*
 81726 ** Parameter zName is the name of a table that is about to be altered
 81727 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
 81728 ** If the table is a system table, this function leaves an error message
 81729 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
 81730 **
 81731 ** Or, if zName is not a system table, zero is returned.
 81732 */
 81733 static int isSystemTable(Parse *pParse, const char *zName){
 81734   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 81735     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
 81736     return 1;
 81738   return 0;
 81741 /*
 81742 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
 81743 ** command. 
 81744 */
 81745 SQLITE_PRIVATE void sqlite3AlterRenameTable(
 81746   Parse *pParse,            /* Parser context. */
 81747   SrcList *pSrc,            /* The table to rename. */
 81748   Token *pName              /* The new table name. */
 81749 ){
 81750   int iDb;                  /* Database that contains the table */
 81751   char *zDb;                /* Name of database iDb */
 81752   Table *pTab;              /* Table being renamed */
 81753   char *zName = 0;          /* NULL-terminated version of pName */ 
 81754   sqlite3 *db = pParse->db; /* Database connection */
 81755   int nTabName;             /* Number of UTF-8 characters in zTabName */
 81756   const char *zTabName;     /* Original name of the table */
 81757   Vdbe *v;
 81758 #ifndef SQLITE_OMIT_TRIGGER
 81759   char *zWhere = 0;         /* Where clause to locate temp triggers */
 81760 #endif
 81761   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
 81762   int savedDbFlags;         /* Saved value of db->flags */
 81764   savedDbFlags = db->flags;  
 81765   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
 81766   assert( pSrc->nSrc==1 );
 81767   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 81769   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 81770   if( !pTab ) goto exit_rename_table;
 81771   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 81772   zDb = db->aDb[iDb].zName;
 81773   db->flags |= SQLITE_PreferBuiltin;
 81775   /* Get a NULL terminated version of the new table name. */
 81776   zName = sqlite3NameFromToken(db, pName);
 81777   if( !zName ) goto exit_rename_table;
 81779   /* Check that a table or index named 'zName' does not already exist
 81780   ** in database iDb. If so, this is an error.
 81781   */
 81782   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
 81783     sqlite3ErrorMsg(pParse, 
 81784         "there is already another table or index with this name: %s", zName);
 81785     goto exit_rename_table;
 81788   /* Make sure it is not a system table being altered, or a reserved name
 81789   ** that the table is being renamed to.
 81790   */
 81791   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 81792     goto exit_rename_table;
 81794   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
 81795     exit_rename_table;
 81798 #ifndef SQLITE_OMIT_VIEW
 81799   if( pTab->pSelect ){
 81800     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
 81801     goto exit_rename_table;
 81803 #endif
 81805 #ifndef SQLITE_OMIT_AUTHORIZATION
 81806   /* Invoke the authorization callback. */
 81807   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 81808     goto exit_rename_table;
 81810 #endif
 81812 #ifndef SQLITE_OMIT_VIRTUALTABLE
 81813   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 81814     goto exit_rename_table;
 81816   if( IsVirtual(pTab) ){
 81817     pVTab = sqlite3GetVTable(db, pTab);
 81818     if( pVTab->pVtab->pModule->xRename==0 ){
 81819       pVTab = 0;
 81822 #endif
 81824   /* Begin a transaction for database iDb. 
 81825   ** Then modify the schema cookie (since the ALTER TABLE modifies the
 81826   ** schema). Open a statement transaction if the table is a virtual
 81827   ** table.
 81828   */
 81829   v = sqlite3GetVdbe(pParse);
 81830   if( v==0 ){
 81831     goto exit_rename_table;
 81833   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
 81834   sqlite3ChangeCookie(pParse, iDb);
 81836   /* If this is a virtual table, invoke the xRename() function if
 81837   ** one is defined. The xRename() callback will modify the names
 81838   ** of any resources used by the v-table implementation (including other
 81839   ** SQLite tables) that are identified by the name of the virtual table.
 81840   */
 81841 #ifndef SQLITE_OMIT_VIRTUALTABLE
 81842   if( pVTab ){
 81843     int i = ++pParse->nMem;
 81844     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
 81845     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
 81846     sqlite3MayAbort(pParse);
 81848 #endif
 81850   /* figure out how many UTF-8 characters are in zName */
 81851   zTabName = pTab->zName;
 81852   nTabName = sqlite3Utf8CharLen(zTabName, -1);
 81854 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 81855   if( db->flags&SQLITE_ForeignKeys ){
 81856     /* If foreign-key support is enabled, rewrite the CREATE TABLE 
 81857     ** statements corresponding to all child tables of foreign key constraints
 81858     ** for which the renamed table is the parent table.  */
 81859     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
 81860       sqlite3NestedParse(pParse, 
 81861           "UPDATE \"%w\".%s SET "
 81862               "sql = sqlite_rename_parent(sql, %Q, %Q) "
 81863               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
 81864       sqlite3DbFree(db, zWhere);
 81867 #endif
 81869   /* Modify the sqlite_master table to use the new table name. */
 81870   sqlite3NestedParse(pParse,
 81871       "UPDATE %Q.%s SET "
 81872 #ifdef SQLITE_OMIT_TRIGGER
 81873           "sql = sqlite_rename_table(sql, %Q), "
 81874 #else
 81875           "sql = CASE "
 81876             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
 81877             "ELSE sqlite_rename_table(sql, %Q) END, "
 81878 #endif
 81879           "tbl_name = %Q, "
 81880           "name = CASE "
 81881             "WHEN type='table' THEN %Q "
 81882             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
 81883              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
 81884             "ELSE name END "
 81885       "WHERE tbl_name=%Q COLLATE nocase AND "
 81886           "(type='table' OR type='index' OR type='trigger');", 
 81887       zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
 81888 #ifndef SQLITE_OMIT_TRIGGER
 81889       zName,
 81890 #endif
 81891       zName, nTabName, zTabName
 81892   );
 81894 #ifndef SQLITE_OMIT_AUTOINCREMENT
 81895   /* If the sqlite_sequence table exists in this database, then update 
 81896   ** it with the new table name.
 81897   */
 81898   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
 81899     sqlite3NestedParse(pParse,
 81900         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
 81901         zDb, zName, pTab->zName);
 81903 #endif
 81905 #ifndef SQLITE_OMIT_TRIGGER
 81906   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
 81907   ** table. Don't do this if the table being ALTERed is itself located in
 81908   ** the temp database.
 81909   */
 81910   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 81911     sqlite3NestedParse(pParse, 
 81912         "UPDATE sqlite_temp_master SET "
 81913             "sql = sqlite_rename_trigger(sql, %Q), "
 81914             "tbl_name = %Q "
 81915             "WHERE %s;", zName, zName, zWhere);
 81916     sqlite3DbFree(db, zWhere);
 81918 #endif
 81920 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 81921   if( db->flags&SQLITE_ForeignKeys ){
 81922     FKey *p;
 81923     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 81924       Table *pFrom = p->pFrom;
 81925       if( pFrom!=pTab ){
 81926         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
 81930 #endif
 81932   /* Drop and reload the internal table schema. */
 81933   reloadTableSchema(pParse, pTab, zName);
 81935 exit_rename_table:
 81936   sqlite3SrcListDelete(db, pSrc);
 81937   sqlite3DbFree(db, zName);
 81938   db->flags = savedDbFlags;
 81942 /*
 81943 ** Generate code to make sure the file format number is at least minFormat.
 81944 ** The generated code will increase the file format number if necessary.
 81945 */
 81946 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
 81947   Vdbe *v;
 81948   v = sqlite3GetVdbe(pParse);
 81949   /* The VDBE should have been allocated before this routine is called.
 81950   ** If that allocation failed, we would have quit before reaching this
 81951   ** point */
 81952   if( ALWAYS(v) ){
 81953     int r1 = sqlite3GetTempReg(pParse);
 81954     int r2 = sqlite3GetTempReg(pParse);
 81955     int j1;
 81956     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
 81957     sqlite3VdbeUsesBtree(v, iDb);
 81958     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
 81959     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
 81960     sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
 81961     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
 81962     sqlite3VdbeJumpHere(v, j1);
 81963     sqlite3ReleaseTempReg(pParse, r1);
 81964     sqlite3ReleaseTempReg(pParse, r2);
 81968 /*
 81969 ** This function is called after an "ALTER TABLE ... ADD" statement
 81970 ** has been parsed. Argument pColDef contains the text of the new
 81971 ** column definition.
 81972 **
 81973 ** The Table structure pParse->pNewTable was extended to include
 81974 ** the new column during parsing.
 81975 */
 81976 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
 81977   Table *pNew;              /* Copy of pParse->pNewTable */
 81978   Table *pTab;              /* Table being altered */
 81979   int iDb;                  /* Database number */
 81980   const char *zDb;          /* Database name */
 81981   const char *zTab;         /* Table name */
 81982   char *zCol;               /* Null-terminated column definition */
 81983   Column *pCol;             /* The new column */
 81984   Expr *pDflt;              /* Default value for the new column */
 81985   sqlite3 *db;              /* The database connection; */
 81987   db = pParse->db;
 81988   if( pParse->nErr || db->mallocFailed ) return;
 81989   pNew = pParse->pNewTable;
 81990   assert( pNew );
 81992   assert( sqlite3BtreeHoldsAllMutexes(db) );
 81993   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
 81994   zDb = db->aDb[iDb].zName;
 81995   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
 81996   pCol = &pNew->aCol[pNew->nCol-1];
 81997   pDflt = pCol->pDflt;
 81998   pTab = sqlite3FindTable(db, zTab, zDb);
 81999   assert( pTab );
 82001 #ifndef SQLITE_OMIT_AUTHORIZATION
 82002   /* Invoke the authorization callback. */
 82003   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
 82004     return;
 82006 #endif
 82008   /* If the default value for the new column was specified with a 
 82009   ** literal NULL, then set pDflt to 0. This simplifies checking
 82010   ** for an SQL NULL default below.
 82011   */
 82012   if( pDflt && pDflt->op==TK_NULL ){
 82013     pDflt = 0;
 82016   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
 82017   ** If there is a NOT NULL constraint, then the default value for the
 82018   ** column must not be NULL.
 82019   */
 82020   if( pCol->colFlags & COLFLAG_PRIMKEY ){
 82021     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
 82022     return;
 82024   if( pNew->pIndex ){
 82025     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
 82026     return;
 82028   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
 82029     sqlite3ErrorMsg(pParse, 
 82030         "Cannot add a REFERENCES column with non-NULL default value");
 82031     return;
 82033   if( pCol->notNull && !pDflt ){
 82034     sqlite3ErrorMsg(pParse, 
 82035         "Cannot add a NOT NULL column with default value NULL");
 82036     return;
 82039   /* Ensure the default expression is something that sqlite3ValueFromExpr()
 82040   ** can handle (i.e. not CURRENT_TIME etc.)
 82041   */
 82042   if( pDflt ){
 82043     sqlite3_value *pVal = 0;
 82044     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
 82045       db->mallocFailed = 1;
 82046       return;
 82048     if( !pVal ){
 82049       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
 82050       return;
 82052     sqlite3ValueFree(pVal);
 82055   /* Modify the CREATE TABLE statement. */
 82056   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
 82057   if( zCol ){
 82058     char *zEnd = &zCol[pColDef->n-1];
 82059     int savedDbFlags = db->flags;
 82060     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
 82061       *zEnd-- = '\0';
 82063     db->flags |= SQLITE_PreferBuiltin;
 82064     sqlite3NestedParse(pParse, 
 82065         "UPDATE \"%w\".%s SET "
 82066           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
 82067         "WHERE type = 'table' AND name = %Q", 
 82068       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
 82069       zTab
 82070     );
 82071     sqlite3DbFree(db, zCol);
 82072     db->flags = savedDbFlags;
 82075   /* If the default value of the new column is NULL, then set the file
 82076   ** format to 2. If the default value of the new column is not NULL,
 82077   ** the file format becomes 3.
 82078   */
 82079   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
 82081   /* Reload the schema of the modified table. */
 82082   reloadTableSchema(pParse, pTab, pTab->zName);
 82085 /*
 82086 ** This function is called by the parser after the table-name in
 82087 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
 82088 ** pSrc is the full-name of the table being altered.
 82089 **
 82090 ** This routine makes a (partial) copy of the Table structure
 82091 ** for the table being altered and sets Parse.pNewTable to point
 82092 ** to it. Routines called by the parser as the column definition
 82093 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
 82094 ** the copy. The copy of the Table structure is deleted by tokenize.c 
 82095 ** after parsing is finished.
 82096 **
 82097 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
 82098 ** coding the "ALTER TABLE ... ADD" statement.
 82099 */
 82100 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
 82101   Table *pNew;
 82102   Table *pTab;
 82103   Vdbe *v;
 82104   int iDb;
 82105   int i;
 82106   int nAlloc;
 82107   sqlite3 *db = pParse->db;
 82109   /* Look up the table being altered. */
 82110   assert( pParse->pNewTable==0 );
 82111   assert( sqlite3BtreeHoldsAllMutexes(db) );
 82112   if( db->mallocFailed ) goto exit_begin_add_column;
 82113   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
 82114   if( !pTab ) goto exit_begin_add_column;
 82116 #ifndef SQLITE_OMIT_VIRTUALTABLE
 82117   if( IsVirtual(pTab) ){
 82118     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
 82119     goto exit_begin_add_column;
 82121 #endif
 82123   /* Make sure this is not an attempt to ALTER a view. */
 82124   if( pTab->pSelect ){
 82125     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
 82126     goto exit_begin_add_column;
 82128   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 82129     goto exit_begin_add_column;
 82132   assert( pTab->addColOffset>0 );
 82133   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 82135   /* Put a copy of the Table struct in Parse.pNewTable for the
 82136   ** sqlite3AddColumn() function and friends to modify.  But modify
 82137   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
 82138   ** prefix, we insure that the name will not collide with an existing
 82139   ** table because user table are not allowed to have the "sqlite_"
 82140   ** prefix on their name.
 82141   */
 82142   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
 82143   if( !pNew ) goto exit_begin_add_column;
 82144   pParse->pNewTable = pNew;
 82145   pNew->nRef = 1;
 82146   pNew->nCol = pTab->nCol;
 82147   assert( pNew->nCol>0 );
 82148   nAlloc = (((pNew->nCol-1)/8)*8)+8;
 82149   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
 82150   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
 82151   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
 82152   if( !pNew->aCol || !pNew->zName ){
 82153     db->mallocFailed = 1;
 82154     goto exit_begin_add_column;
 82156   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
 82157   for(i=0; i<pNew->nCol; i++){
 82158     Column *pCol = &pNew->aCol[i];
 82159     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
 82160     pCol->zColl = 0;
 82161     pCol->zType = 0;
 82162     pCol->pDflt = 0;
 82163     pCol->zDflt = 0;
 82165   pNew->pSchema = db->aDb[iDb].pSchema;
 82166   pNew->addColOffset = pTab->addColOffset;
 82167   pNew->nRef = 1;
 82169   /* Begin a transaction and increment the schema cookie.  */
 82170   sqlite3BeginWriteOperation(pParse, 0, iDb);
 82171   v = sqlite3GetVdbe(pParse);
 82172   if( !v ) goto exit_begin_add_column;
 82173   sqlite3ChangeCookie(pParse, iDb);
 82175 exit_begin_add_column:
 82176   sqlite3SrcListDelete(db, pSrc);
 82177   return;
 82179 #endif  /* SQLITE_ALTER_TABLE */
 82181 /************** End of alter.c ***********************************************/
 82182 /************** Begin file analyze.c *****************************************/
 82183 /*
 82184 ** 2005-07-08
 82185 **
 82186 ** The author disclaims copyright to this source code.  In place of
 82187 ** a legal notice, here is a blessing:
 82188 **
 82189 **    May you do good and not evil.
 82190 **    May you find forgiveness for yourself and forgive others.
 82191 **    May you share freely, never taking more than you give.
 82192 **
 82193 *************************************************************************
 82194 ** This file contains code associated with the ANALYZE command.
 82195 **
 82196 ** The ANALYZE command gather statistics about the content of tables
 82197 ** and indices.  These statistics are made available to the query planner
 82198 ** to help it make better decisions about how to perform queries.
 82199 **
 82200 ** The following system tables are or have been supported:
 82201 **
 82202 **    CREATE TABLE sqlite_stat1(tbl, idx, stat);
 82203 **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
 82204 **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 82205 **    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
 82206 **
 82207 ** Additional tables might be added in future releases of SQLite.
 82208 ** The sqlite_stat2 table is not created or used unless the SQLite version
 82209 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
 82210 ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
 82211 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
 82212 ** created and used by SQLite versions 3.7.9 and later and with
 82213 ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
 82214 ** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
 82215 ** version of sqlite_stat3 and is only available when compiled with
 82216 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
 82217 ** not possible to enable both STAT3 and STAT4 at the same time.  If they
 82218 ** are both enabled, then STAT4 takes precedence.
 82219 **
 82220 ** For most applications, sqlite_stat1 provides all the statisics required
 82221 ** for the query planner to make good choices.
 82222 **
 82223 ** Format of sqlite_stat1:
 82224 **
 82225 ** There is normally one row per index, with the index identified by the
 82226 ** name in the idx column.  The tbl column is the name of the table to
 82227 ** which the index belongs.  In each such row, the stat column will be
 82228 ** a string consisting of a list of integers.  The first integer in this
 82229 ** list is the number of rows in the index.  (This is the same as the
 82230 ** number of rows in the table, except for partial indices.)  The second
 82231 ** integer is the average number of rows in the index that have the same
 82232 ** value in the first column of the index.  The third integer is the average
 82233 ** number of rows in the index that have the same value for the first two
 82234 ** columns.  The N-th integer (for N>1) is the average number of rows in 
 82235 ** the index which have the same value for the first N-1 columns.  For
 82236 ** a K-column index, there will be K+1 integers in the stat column.  If
 82237 ** the index is unique, then the last integer will be 1.
 82238 **
 82239 ** The list of integers in the stat column can optionally be followed
 82240 ** by the keyword "unordered".  The "unordered" keyword, if it is present,
 82241 ** must be separated from the last integer by a single space.  If the
 82242 ** "unordered" keyword is present, then the query planner assumes that
 82243 ** the index is unordered and will not use the index for a range query.
 82244 ** 
 82245 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
 82246 ** column contains a single integer which is the (estimated) number of
 82247 ** rows in the table identified by sqlite_stat1.tbl.
 82248 **
 82249 ** Format of sqlite_stat2:
 82250 **
 82251 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
 82252 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
 82253 ** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
 82254 ** about the distribution of keys within an index.  The index is identified by
 82255 ** the "idx" column and the "tbl" column is the name of the table to which
 82256 ** the index belongs.  There are usually 10 rows in the sqlite_stat2
 82257 ** table for each index.
 82258 **
 82259 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
 82260 ** inclusive are samples of the left-most key value in the index taken at
 82261 ** evenly spaced points along the index.  Let the number of samples be S
 82262 ** (10 in the standard build) and let C be the number of rows in the index.
 82263 ** Then the sampled rows are given by:
 82264 **
 82265 **     rownumber = (i*C*2 + C)/(S*2)
 82266 **
 82267 ** For i between 0 and S-1.  Conceptually, the index space is divided into
 82268 ** S uniform buckets and the samples are the middle row from each bucket.
 82269 **
 82270 ** The format for sqlite_stat2 is recorded here for legacy reference.  This
 82271 ** version of SQLite does not support sqlite_stat2.  It neither reads nor
 82272 ** writes the sqlite_stat2 table.  This version of SQLite only supports
 82273 ** sqlite_stat3.
 82274 **
 82275 ** Format for sqlite_stat3:
 82276 **
 82277 ** The sqlite_stat3 format is a subset of sqlite_stat4.  Hence, the
 82278 ** sqlite_stat4 format will be described first.  Further information
 82279 ** about sqlite_stat3 follows the sqlite_stat4 description.
 82280 **
 82281 ** Format for sqlite_stat4:
 82282 **
 82283 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
 82284 ** to aid the query planner in choosing good indices based on the values
 82285 ** that indexed columns are compared against in the WHERE clauses of
 82286 ** queries.
 82287 **
 82288 ** The sqlite_stat4 table contains multiple entries for each index.
 82289 ** The idx column names the index and the tbl column is the table of the
 82290 ** index.  If the idx and tbl columns are the same, then the sample is
 82291 ** of the INTEGER PRIMARY KEY.  The sample column is a blob which is the
 82292 ** binary encoding of a key from the index.  The nEq column is a
 82293 ** list of integers.  The first integer is the approximate number
 82294 ** of entries in the index whose left-most column exactly matches
 82295 ** the left-most column of the sample.  The second integer in nEq
 82296 ** is the approximate number of entries in the index where the
 82297 ** first two columns match the first two columns of the sample.
 82298 ** And so forth.  nLt is another list of integers that show the approximate
 82299 ** number of entries that are strictly less than the sample.  The first
 82300 ** integer in nLt contains the number of entries in the index where the
 82301 ** left-most column is less than the left-most column of the sample.
 82302 ** The K-th integer in the nLt entry is the number of index entries 
 82303 ** where the first K columns are less than the first K columns of the
 82304 ** sample.  The nDLt column is like nLt except that it contains the 
 82305 ** number of distinct entries in the index that are less than the
 82306 ** sample.
 82307 **
 82308 ** There can be an arbitrary number of sqlite_stat4 entries per index.
 82309 ** The ANALYZE command will typically generate sqlite_stat4 tables
 82310 ** that contain between 10 and 40 samples which are distributed across
 82311 ** the key space, though not uniformly, and which include samples with
 82312 ** large nEq values.
 82313 **
 82314 ** Format for sqlite_stat3 redux:
 82315 **
 82316 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
 82317 ** looks at the left-most column of the index.  The sqlite_stat3.sample
 82318 ** column contains the actual value of the left-most column instead
 82319 ** of a blob encoding of the complete index key as is found in
 82320 ** sqlite_stat4.sample.  The nEq, nLt, and nDLt entries of sqlite_stat3
 82321 ** all contain just a single integer which is the same as the first
 82322 ** integer in the equivalent columns in sqlite_stat4.
 82323 */
 82324 #ifndef SQLITE_OMIT_ANALYZE
 82326 #if defined(SQLITE_ENABLE_STAT4)
 82327 # define IsStat4     1
 82328 # define IsStat3     0
 82329 #elif defined(SQLITE_ENABLE_STAT3)
 82330 # define IsStat4     0
 82331 # define IsStat3     1
 82332 #else
 82333 # define IsStat4     0
 82334 # define IsStat3     0
 82335 # undef SQLITE_STAT4_SAMPLES
 82336 # define SQLITE_STAT4_SAMPLES 1
 82337 #endif
 82338 #define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
 82340 /*
 82341 ** This routine generates code that opens the sqlite_statN tables.
 82342 ** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
 82343 ** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
 82344 ** appropriate compile-time options are provided.
 82345 **
 82346 ** If the sqlite_statN tables do not previously exist, it is created.
 82347 **
 82348 ** Argument zWhere may be a pointer to a buffer containing a table name,
 82349 ** or it may be a NULL pointer. If it is not NULL, then all entries in
 82350 ** the sqlite_statN tables associated with the named table are deleted.
 82351 ** If zWhere==0, then code is generated to delete all stat table entries.
 82352 */
 82353 static void openStatTable(
 82354   Parse *pParse,          /* Parsing context */
 82355   int iDb,                /* The database we are looking in */
 82356   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
 82357   const char *zWhere,     /* Delete entries for this table or index */
 82358   const char *zWhereType  /* Either "tbl" or "idx" */
 82359 ){
 82360   static const struct {
 82361     const char *zName;
 82362     const char *zCols;
 82363   } aTable[] = {
 82364     { "sqlite_stat1", "tbl,idx,stat" },
 82365 #if defined(SQLITE_ENABLE_STAT4)
 82366     { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
 82367     { "sqlite_stat3", 0 },
 82368 #elif defined(SQLITE_ENABLE_STAT3)
 82369     { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 82370     { "sqlite_stat4", 0 },
 82371 #else
 82372     { "sqlite_stat3", 0 },
 82373     { "sqlite_stat4", 0 },
 82374 #endif
 82375   };
 82376   int i;
 82377   sqlite3 *db = pParse->db;
 82378   Db *pDb;
 82379   Vdbe *v = sqlite3GetVdbe(pParse);
 82380   int aRoot[ArraySize(aTable)];
 82381   u8 aCreateTbl[ArraySize(aTable)];
 82383   if( v==0 ) return;
 82384   assert( sqlite3BtreeHoldsAllMutexes(db) );
 82385   assert( sqlite3VdbeDb(v)==db );
 82386   pDb = &db->aDb[iDb];
 82388   /* Create new statistic tables if they do not exist, or clear them
 82389   ** if they do already exist.
 82390   */
 82391   for(i=0; i<ArraySize(aTable); i++){
 82392     const char *zTab = aTable[i].zName;
 82393     Table *pStat;
 82394     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
 82395       if( aTable[i].zCols ){
 82396         /* The sqlite_statN table does not exist. Create it. Note that a 
 82397         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
 82398         ** of the new table in register pParse->regRoot. This is important 
 82399         ** because the OpenWrite opcode below will be needing it. */
 82400         sqlite3NestedParse(pParse,
 82401             "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
 82402         );
 82403         aRoot[i] = pParse->regRoot;
 82404         aCreateTbl[i] = OPFLAG_P2ISREG;
 82406     }else{
 82407       /* The table already exists. If zWhere is not NULL, delete all entries 
 82408       ** associated with the table zWhere. If zWhere is NULL, delete the
 82409       ** entire contents of the table. */
 82410       aRoot[i] = pStat->tnum;
 82411       aCreateTbl[i] = 0;
 82412       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 82413       if( zWhere ){
 82414         sqlite3NestedParse(pParse,
 82415            "DELETE FROM %Q.%s WHERE %s=%Q",
 82416            pDb->zName, zTab, zWhereType, zWhere
 82417         );
 82418       }else{
 82419         /* The sqlite_stat[134] table already exists.  Delete all rows. */
 82420         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 82425   /* Open the sqlite_stat[134] tables for writing. */
 82426   for(i=0; aTable[i].zCols; i++){
 82427     assert( i<ArraySize(aTable) );
 82428     sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
 82429     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
 82433 /*
 82434 ** Recommended number of samples for sqlite_stat4
 82435 */
 82436 #ifndef SQLITE_STAT4_SAMPLES
 82437 # define SQLITE_STAT4_SAMPLES 24
 82438 #endif
 82440 /*
 82441 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
 82442 ** share an instance of the following structure to hold their state
 82443 ** information.
 82444 */
 82445 typedef struct Stat4Accum Stat4Accum;
 82446 typedef struct Stat4Sample Stat4Sample;
 82447 struct Stat4Sample {
 82448   tRowcnt *anEq;                  /* sqlite_stat4.nEq */
 82449   tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
 82450 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82451   tRowcnt *anLt;                  /* sqlite_stat4.nLt */
 82452   union {
 82453     i64 iRowid;                     /* Rowid in main table of the key */
 82454     u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
 82455   } u;
 82456   u32 nRowid;                     /* Sizeof aRowid[] */
 82457   u8 isPSample;                   /* True if a periodic sample */
 82458   int iCol;                       /* If !isPSample, the reason for inclusion */
 82459   u32 iHash;                      /* Tiebreaker hash */
 82460 #endif
 82461 };                                                    
 82462 struct Stat4Accum {
 82463   tRowcnt nRow;             /* Number of rows in the entire table */
 82464   tRowcnt nPSample;         /* How often to do a periodic sample */
 82465   int nCol;                 /* Number of columns in index + rowid */
 82466   int mxSample;             /* Maximum number of samples to accumulate */
 82467   Stat4Sample current;      /* Current row as a Stat4Sample */
 82468   u32 iPrn;                 /* Pseudo-random number used for sampling */
 82469   Stat4Sample *aBest;       /* Array of nCol best samples */
 82470   int iMin;                 /* Index in a[] of entry with minimum score */
 82471   int nSample;              /* Current number of samples */
 82472   int iGet;                 /* Index of current sample accessed by stat_get() */
 82473   Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
 82474   sqlite3 *db;              /* Database connection, for malloc() */
 82475 };
 82477 /* Reclaim memory used by a Stat4Sample
 82478 */
 82479 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82480 static void sampleClear(sqlite3 *db, Stat4Sample *p){
 82481   assert( db!=0 );
 82482   if( p->nRowid ){
 82483     sqlite3DbFree(db, p->u.aRowid);
 82484     p->nRowid = 0;
 82487 #endif
 82489 /* Initialize the BLOB value of a ROWID
 82490 */
 82491 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82492 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
 82493   assert( db!=0 );
 82494   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
 82495   p->u.aRowid = sqlite3DbMallocRaw(db, n);
 82496   if( p->u.aRowid ){
 82497     p->nRowid = n;
 82498     memcpy(p->u.aRowid, pData, n);
 82499   }else{
 82500     p->nRowid = 0;
 82503 #endif
 82505 /* Initialize the INTEGER value of a ROWID.
 82506 */
 82507 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82508 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
 82509   assert( db!=0 );
 82510   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
 82511   p->nRowid = 0;
 82512   p->u.iRowid = iRowid;
 82514 #endif
 82517 /*
 82518 ** Copy the contents of object (*pFrom) into (*pTo).
 82519 */
 82520 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82521 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
 82522   pTo->isPSample = pFrom->isPSample;
 82523   pTo->iCol = pFrom->iCol;
 82524   pTo->iHash = pFrom->iHash;
 82525   memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
 82526   memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
 82527   memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
 82528   if( pFrom->nRowid ){
 82529     sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
 82530   }else{
 82531     sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
 82534 #endif
 82536 /*
 82537 ** Reclaim all memory of a Stat4Accum structure.
 82538 */
 82539 static void stat4Destructor(void *pOld){
 82540   Stat4Accum *p = (Stat4Accum*)pOld;
 82541 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82542   int i;
 82543   for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
 82544   for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
 82545   sampleClear(p->db, &p->current);
 82546 #endif
 82547   sqlite3DbFree(p->db, p);
 82550 /*
 82551 ** Implementation of the stat_init(N,C) SQL function. The two parameters
 82552 ** are the number of rows in the table or index (C) and the number of columns
 82553 ** in the index (N).  The second argument (C) is only used for STAT3 and STAT4.
 82554 **
 82555 ** This routine allocates the Stat4Accum object in heap memory. The return 
 82556 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e. 
 82557 ** the size of the blob is sizeof(void*) bytes). 
 82558 */
 82559 static void statInit(
 82560   sqlite3_context *context,
 82561   int argc,
 82562   sqlite3_value **argv
 82563 ){
 82564   Stat4Accum *p;
 82565   int nCol;                       /* Number of columns in index being sampled */
 82566   int nColUp;                     /* nCol rounded up for alignment */
 82567   int n;                          /* Bytes of space to allocate */
 82568   sqlite3 *db;                    /* Database connection */
 82569 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82570   int mxSample = SQLITE_STAT4_SAMPLES;
 82571 #endif
 82573   /* Decode the three function arguments */
 82574   UNUSED_PARAMETER(argc);
 82575   nCol = sqlite3_value_int(argv[0]);
 82576   assert( nCol>1 );               /* >1 because it includes the rowid column */
 82577   nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
 82579   /* Allocate the space required for the Stat4Accum object */
 82580   n = sizeof(*p) 
 82581     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
 82582     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
 82583 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82584     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
 82585     + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
 82586     + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
 82587 #endif
 82589   db = sqlite3_context_db_handle(context);
 82590   p = sqlite3DbMallocZero(db, n);
 82591   if( p==0 ){
 82592     sqlite3_result_error_nomem(context);
 82593     return;
 82596   p->db = db;
 82597   p->nRow = 0;
 82598   p->nCol = nCol;
 82599   p->current.anDLt = (tRowcnt*)&p[1];
 82600   p->current.anEq = &p->current.anDLt[nColUp];
 82602 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82604     u8 *pSpace;                     /* Allocated space not yet assigned */
 82605     int i;                          /* Used to iterate through p->aSample[] */
 82607     p->iGet = -1;
 82608     p->mxSample = mxSample;
 82609     p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
 82610     p->current.anLt = &p->current.anEq[nColUp];
 82611     p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
 82613     /* Set up the Stat4Accum.a[] and aBest[] arrays */
 82614     p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
 82615     p->aBest = &p->a[mxSample];
 82616     pSpace = (u8*)(&p->a[mxSample+nCol]);
 82617     for(i=0; i<(mxSample+nCol); i++){
 82618       p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
 82619       p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
 82620       p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
 82622     assert( (pSpace - (u8*)p)==n );
 82624     for(i=0; i<nCol; i++){
 82625       p->aBest[i].iCol = i;
 82628 #endif
 82630   /* Return a pointer to the allocated object to the caller */
 82631   sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
 82633 static const FuncDef statInitFuncdef = {
 82634   1+IsStat34,      /* nArg */
 82635   SQLITE_UTF8,     /* funcFlags */
 82636   0,               /* pUserData */
 82637   0,               /* pNext */
 82638   statInit,        /* xFunc */
 82639   0,               /* xStep */
 82640   0,               /* xFinalize */
 82641   "stat_init",     /* zName */
 82642   0,               /* pHash */
 82643   0                /* pDestructor */
 82644 };
 82646 #ifdef SQLITE_ENABLE_STAT4
 82647 /*
 82648 ** pNew and pOld are both candidate non-periodic samples selected for 
 82649 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and 
 82650 ** considering only any trailing columns and the sample hash value, this
 82651 ** function returns true if sample pNew is to be preferred over pOld.
 82652 ** In other words, if we assume that the cardinalities of the selected
 82653 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
 82654 **
 82655 ** This function assumes that for each argument sample, the contents of
 82656 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. 
 82657 */
 82658 static int sampleIsBetterPost(
 82659   Stat4Accum *pAccum, 
 82660   Stat4Sample *pNew, 
 82661   Stat4Sample *pOld
 82662 ){
 82663   int nCol = pAccum->nCol;
 82664   int i;
 82665   assert( pNew->iCol==pOld->iCol );
 82666   for(i=pNew->iCol+1; i<nCol; i++){
 82667     if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
 82668     if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
 82670   if( pNew->iHash>pOld->iHash ) return 1;
 82671   return 0;
 82673 #endif
 82675 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82676 /*
 82677 ** Return true if pNew is to be preferred over pOld.
 82678 **
 82679 ** This function assumes that for each argument sample, the contents of
 82680 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. 
 82681 */
 82682 static int sampleIsBetter(
 82683   Stat4Accum *pAccum, 
 82684   Stat4Sample *pNew, 
 82685   Stat4Sample *pOld
 82686 ){
 82687   tRowcnt nEqNew = pNew->anEq[pNew->iCol];
 82688   tRowcnt nEqOld = pOld->anEq[pOld->iCol];
 82690   assert( pOld->isPSample==0 && pNew->isPSample==0 );
 82691   assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
 82693   if( (nEqNew>nEqOld) ) return 1;
 82694 #ifdef SQLITE_ENABLE_STAT4
 82695   if( nEqNew==nEqOld ){
 82696     if( pNew->iCol<pOld->iCol ) return 1;
 82697     return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
 82699   return 0;
 82700 #else
 82701   return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
 82702 #endif
 82705 /*
 82706 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
 82707 ** remove the least desirable sample from p->a[] to make room.
 82708 */
 82709 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
 82710   Stat4Sample *pSample = 0;
 82711   int i;
 82713   assert( IsStat4 || nEqZero==0 );
 82715 #ifdef SQLITE_ENABLE_STAT4
 82716   if( pNew->isPSample==0 ){
 82717     Stat4Sample *pUpgrade = 0;
 82718     assert( pNew->anEq[pNew->iCol]>0 );
 82720     /* This sample is being added because the prefix that ends in column 
 82721     ** iCol occurs many times in the table. However, if we have already
 82722     ** added a sample that shares this prefix, there is no need to add
 82723     ** this one. Instead, upgrade the priority of the highest priority
 82724     ** existing sample that shares this prefix.  */
 82725     for(i=p->nSample-1; i>=0; i--){
 82726       Stat4Sample *pOld = &p->a[i];
 82727       if( pOld->anEq[pNew->iCol]==0 ){
 82728         if( pOld->isPSample ) return;
 82729         assert( pOld->iCol>pNew->iCol );
 82730         assert( sampleIsBetter(p, pNew, pOld) );
 82731         if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
 82732           pUpgrade = pOld;
 82736     if( pUpgrade ){
 82737       pUpgrade->iCol = pNew->iCol;
 82738       pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
 82739       goto find_new_min;
 82742 #endif
 82744   /* If necessary, remove sample iMin to make room for the new sample. */
 82745   if( p->nSample>=p->mxSample ){
 82746     Stat4Sample *pMin = &p->a[p->iMin];
 82747     tRowcnt *anEq = pMin->anEq;
 82748     tRowcnt *anLt = pMin->anLt;
 82749     tRowcnt *anDLt = pMin->anDLt;
 82750     sampleClear(p->db, pMin);
 82751     memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
 82752     pSample = &p->a[p->nSample-1];
 82753     pSample->nRowid = 0;
 82754     pSample->anEq = anEq;
 82755     pSample->anDLt = anDLt;
 82756     pSample->anLt = anLt;
 82757     p->nSample = p->mxSample-1;
 82760   /* The "rows less-than" for the rowid column must be greater than that
 82761   ** for the last sample in the p->a[] array. Otherwise, the samples would
 82762   ** be out of order. */
 82763 #ifdef SQLITE_ENABLE_STAT4
 82764   assert( p->nSample==0 
 82765        || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
 82766 #endif
 82768   /* Insert the new sample */
 82769   pSample = &p->a[p->nSample];
 82770   sampleCopy(p, pSample, pNew);
 82771   p->nSample++;
 82773   /* Zero the first nEqZero entries in the anEq[] array. */
 82774   memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
 82776 #ifdef SQLITE_ENABLE_STAT4
 82777  find_new_min:
 82778 #endif
 82779   if( p->nSample>=p->mxSample ){
 82780     int iMin = -1;
 82781     for(i=0; i<p->mxSample; i++){
 82782       if( p->a[i].isPSample ) continue;
 82783       if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
 82784         iMin = i;
 82787     assert( iMin>=0 );
 82788     p->iMin = iMin;
 82791 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 82793 /*
 82794 ** Field iChng of the index being scanned has changed. So at this point
 82795 ** p->current contains a sample that reflects the previous row of the
 82796 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
 82797 ** correct at this point.
 82798 */
 82799 static void samplePushPrevious(Stat4Accum *p, int iChng){
 82800 #ifdef SQLITE_ENABLE_STAT4
 82801   int i;
 82803   /* Check if any samples from the aBest[] array should be pushed
 82804   ** into IndexSample.a[] at this point.  */
 82805   for(i=(p->nCol-2); i>=iChng; i--){
 82806     Stat4Sample *pBest = &p->aBest[i];
 82807     pBest->anEq[i] = p->current.anEq[i];
 82808     if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
 82809       sampleInsert(p, pBest, i);
 82813   /* Update the anEq[] fields of any samples already collected. */
 82814   for(i=p->nSample-1; i>=0; i--){
 82815     int j;
 82816     for(j=iChng; j<p->nCol; j++){
 82817       if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
 82820 #endif
 82822 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
 82823   if( iChng==0 ){
 82824     tRowcnt nLt = p->current.anLt[0];
 82825     tRowcnt nEq = p->current.anEq[0];
 82827     /* Check if this is to be a periodic sample. If so, add it. */
 82828     if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
 82829       p->current.isPSample = 1;
 82830       sampleInsert(p, &p->current, 0);
 82831       p->current.isPSample = 0;
 82832     }else 
 82834     /* Or if it is a non-periodic sample. Add it in this case too. */
 82835     if( p->nSample<p->mxSample 
 82836      || sampleIsBetter(p, &p->current, &p->a[p->iMin]) 
 82837     ){
 82838       sampleInsert(p, &p->current, 0);
 82841 #endif
 82843 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
 82844   UNUSED_PARAMETER( p );
 82845   UNUSED_PARAMETER( iChng );
 82846 #endif
 82849 /*
 82850 ** Implementation of the stat_push SQL function:  stat_push(P,C,R)
 82851 ** Arguments:
 82852 **
 82853 **    P     Pointer to the Stat4Accum object created by stat_init()
 82854 **    C     Index of left-most column to differ from previous row
 82855 **    R     Rowid for the current row.  Might be a key record for
 82856 **          WITHOUT ROWID tables.
 82857 **
 82858 ** The SQL function always returns NULL.
 82859 **
 82860 ** The R parameter is only used for STAT3 and STAT4
 82861 */
 82862 static void statPush(
 82863   sqlite3_context *context,
 82864   int argc,
 82865   sqlite3_value **argv
 82866 ){
 82867   int i;
 82869   /* The three function arguments */
 82870   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
 82871   int iChng = sqlite3_value_int(argv[1]);
 82873   UNUSED_PARAMETER( argc );
 82874   UNUSED_PARAMETER( context );
 82875   assert( p->nCol>1 );        /* Includes rowid field */
 82876   assert( iChng<p->nCol );
 82878   if( p->nRow==0 ){
 82879     /* This is the first call to this function. Do initialization. */
 82880     for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
 82881   }else{
 82882     /* Second and subsequent calls get processed here */
 82883     samplePushPrevious(p, iChng);
 82885     /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
 82886     ** to the current row of the index. */
 82887     for(i=0; i<iChng; i++){
 82888       p->current.anEq[i]++;
 82890     for(i=iChng; i<p->nCol; i++){
 82891       p->current.anDLt[i]++;
 82892 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82893       p->current.anLt[i] += p->current.anEq[i];
 82894 #endif
 82895       p->current.anEq[i] = 1;
 82898   p->nRow++;
 82899 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82900   if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
 82901     sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
 82902   }else{
 82903     sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
 82904                                        sqlite3_value_blob(argv[2]));
 82906   p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
 82907 #endif
 82909 #ifdef SQLITE_ENABLE_STAT4
 82911     tRowcnt nLt = p->current.anLt[p->nCol-1];
 82913     /* Check if this is to be a periodic sample. If so, add it. */
 82914     if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
 82915       p->current.isPSample = 1;
 82916       p->current.iCol = 0;
 82917       sampleInsert(p, &p->current, p->nCol-1);
 82918       p->current.isPSample = 0;
 82921     /* Update the aBest[] array. */
 82922     for(i=0; i<(p->nCol-1); i++){
 82923       p->current.iCol = i;
 82924       if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
 82925         sampleCopy(p, &p->aBest[i], &p->current);
 82929 #endif
 82931 static const FuncDef statPushFuncdef = {
 82932   2+IsStat34,      /* nArg */
 82933   SQLITE_UTF8,     /* funcFlags */
 82934   0,               /* pUserData */
 82935   0,               /* pNext */
 82936   statPush,        /* xFunc */
 82937   0,               /* xStep */
 82938   0,               /* xFinalize */
 82939   "stat_push",     /* zName */
 82940   0,               /* pHash */
 82941   0                /* pDestructor */
 82942 };
 82944 #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
 82945 #define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
 82946 #define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
 82947 #define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
 82948 #define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
 82950 /*
 82951 ** Implementation of the stat_get(P,J) SQL function.  This routine is
 82952 ** used to query the results.  Content is returned for parameter J
 82953 ** which is one of the STAT_GET_xxxx values defined above.
 82954 **
 82955 ** If neither STAT3 nor STAT4 are enabled, then J is always
 82956 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
 82957 ** a one-parameter function, stat_get(P), that always returns the
 82958 ** stat1 table entry information.
 82959 */
 82960 static void statGet(
 82961   sqlite3_context *context,
 82962   int argc,
 82963   sqlite3_value **argv
 82964 ){
 82965   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
 82966 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 82967   /* STAT3 and STAT4 have a parameter on this routine. */
 82968   int eCall = sqlite3_value_int(argv[1]);
 82969   assert( argc==2 );
 82970   assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ 
 82971        || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
 82972        || eCall==STAT_GET_NDLT 
 82973   );
 82974   if( eCall==STAT_GET_STAT1 )
 82975 #else
 82976   assert( argc==1 );
 82977 #endif
 82979     /* Return the value to store in the "stat" column of the sqlite_stat1
 82980     ** table for this index.
 82981     **
 82982     ** The value is a string composed of a list of integers describing 
 82983     ** the index. The first integer in the list is the total number of 
 82984     ** entries in the index. There is one additional integer in the list 
 82985     ** for each indexed column. This additional integer is an estimate of
 82986     ** the number of rows matched by a stabbing query on the index using
 82987     ** a key with the corresponding number of fields. In other words,
 82988     ** if the index is on columns (a,b) and the sqlite_stat1 value is 
 82989     ** "100 10 2", then SQLite estimates that:
 82990     **
 82991     **   * the index contains 100 rows,
 82992     **   * "WHERE a=?" matches 10 rows, and
 82993     **   * "WHERE a=? AND b=?" matches 2 rows.
 82994     **
 82995     ** If D is the count of distinct values and K is the total number of 
 82996     ** rows, then each estimate is computed as:
 82997     **
 82998     **        I = (K+D-1)/D
 82999     */
 83000     char *z;
 83001     int i;
 83003     char *zRet = sqlite3MallocZero(p->nCol * 25);
 83004     if( zRet==0 ){
 83005       sqlite3_result_error_nomem(context);
 83006       return;
 83009     sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
 83010     z = zRet + sqlite3Strlen30(zRet);
 83011     for(i=0; i<(p->nCol-1); i++){
 83012       u64 nDistinct = p->current.anDLt[i] + 1;
 83013       u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
 83014       sqlite3_snprintf(24, z, " %llu", iVal);
 83015       z += sqlite3Strlen30(z);
 83016       assert( p->current.anEq[i] );
 83018     assert( z[0]=='\0' && z>zRet );
 83020     sqlite3_result_text(context, zRet, -1, sqlite3_free);
 83022 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83023   else if( eCall==STAT_GET_ROWID ){
 83024     if( p->iGet<0 ){
 83025       samplePushPrevious(p, 0);
 83026       p->iGet = 0;
 83028     if( p->iGet<p->nSample ){
 83029       Stat4Sample *pS = p->a + p->iGet;
 83030       if( pS->nRowid==0 ){
 83031         sqlite3_result_int64(context, pS->u.iRowid);
 83032       }else{
 83033         sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
 83034                             SQLITE_TRANSIENT);
 83037   }else{
 83038     tRowcnt *aCnt = 0;
 83040     assert( p->iGet<p->nSample );
 83041     switch( eCall ){
 83042       case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
 83043       case STAT_GET_NLT:  aCnt = p->a[p->iGet].anLt; break;
 83044       default: {
 83045         aCnt = p->a[p->iGet].anDLt; 
 83046         p->iGet++;
 83047         break;
 83051     if( IsStat3 ){
 83052       sqlite3_result_int64(context, (i64)aCnt[0]);
 83053     }else{
 83054       char *zRet = sqlite3MallocZero(p->nCol * 25);
 83055       if( zRet==0 ){
 83056         sqlite3_result_error_nomem(context);
 83057       }else{
 83058         int i;
 83059         char *z = zRet;
 83060         for(i=0; i<p->nCol; i++){
 83061           sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
 83062           z += sqlite3Strlen30(z);
 83064         assert( z[0]=='\0' && z>zRet );
 83065         z[-1] = '\0';
 83066         sqlite3_result_text(context, zRet, -1, sqlite3_free);
 83070 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 83071 #ifndef SQLITE_DEBUG
 83072   UNUSED_PARAMETER( argc );
 83073 #endif
 83075 static const FuncDef statGetFuncdef = {
 83076   1+IsStat34,      /* nArg */
 83077   SQLITE_UTF8,     /* funcFlags */
 83078   0,               /* pUserData */
 83079   0,               /* pNext */
 83080   statGet,         /* xFunc */
 83081   0,               /* xStep */
 83082   0,               /* xFinalize */
 83083   "stat_get",      /* zName */
 83084   0,               /* pHash */
 83085   0                /* pDestructor */
 83086 };
 83088 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
 83089   assert( regOut!=regStat4 && regOut!=regStat4+1 );
 83090 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83091   sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
 83092 #elif SQLITE_DEBUG
 83093   assert( iParam==STAT_GET_STAT1 );
 83094 #else
 83095   UNUSED_PARAMETER( iParam );
 83096 #endif
 83097   sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
 83098   sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
 83099   sqlite3VdbeChangeP5(v, 1 + IsStat34);
 83102 /*
 83103 ** Generate code to do an analysis of all indices associated with
 83104 ** a single table.
 83105 */
 83106 static void analyzeOneTable(
 83107   Parse *pParse,   /* Parser context */
 83108   Table *pTab,     /* Table whose indices are to be analyzed */
 83109   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
 83110   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
 83111   int iMem,        /* Available memory locations begin here */
 83112   int iTab         /* Next available cursor */
 83113 ){
 83114   sqlite3 *db = pParse->db;    /* Database handle */
 83115   Index *pIdx;                 /* An index to being analyzed */
 83116   int iIdxCur;                 /* Cursor open on index being analyzed */
 83117   int iTabCur;                 /* Table cursor */
 83118   Vdbe *v;                     /* The virtual machine being built up */
 83119   int i;                       /* Loop counter */
 83120   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
 83121   int iDb;                     /* Index of database containing pTab */
 83122   u8 needTableCnt = 1;         /* True to count the table */
 83123   int regNewRowid = iMem++;    /* Rowid for the inserted record */
 83124   int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
 83125   int regChng = iMem++;        /* Index of changed index field */
 83126 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83127   int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
 83128 #endif
 83129   int regTemp = iMem++;        /* Temporary use register */
 83130   int regTabname = iMem++;     /* Register containing table name */
 83131   int regIdxname = iMem++;     /* Register containing index name */
 83132   int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
 83133   int regPrev = iMem;          /* MUST BE LAST (see below) */
 83135   pParse->nMem = MAX(pParse->nMem, iMem);
 83136   v = sqlite3GetVdbe(pParse);
 83137   if( v==0 || NEVER(pTab==0) ){
 83138     return;
 83140   if( pTab->tnum==0 ){
 83141     /* Do not gather statistics on views or virtual tables */
 83142     return;
 83144   if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
 83145     /* Do not gather statistics on system tables */
 83146     return;
 83148   assert( sqlite3BtreeHoldsAllMutexes(db) );
 83149   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 83150   assert( iDb>=0 );
 83151   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 83152 #ifndef SQLITE_OMIT_AUTHORIZATION
 83153   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 83154       db->aDb[iDb].zName ) ){
 83155     return;
 83157 #endif
 83159   /* Establish a read-lock on the table at the shared-cache level. 
 83160   ** Open a read-only cursor on the table. Also allocate a cursor number
 83161   ** to use for scanning indexes (iIdxCur). No index cursor is opened at
 83162   ** this time though.  */
 83163   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 83164   iTabCur = iTab++;
 83165   iIdxCur = iTab++;
 83166   pParse->nTab = MAX(pParse->nTab, iTab);
 83167   sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
 83168   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 83170   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 83171     int nCol;                     /* Number of columns indexed by pIdx */
 83172     int *aGotoChng;               /* Array of jump instruction addresses */
 83173     int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
 83174     int addrGotoChng0;            /* Address of "Goto addr_chng_0" */
 83175     int addrNextRow;              /* Address of "next_row:" */
 83176     const char *zIdxName;         /* Name of the index */
 83178     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 83179     if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
 83180     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
 83181     nCol = pIdx->nKeyCol;
 83182     aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
 83183     if( aGotoChng==0 ) continue;
 83185     /* Populate the register containing the index name. */
 83186     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
 83187       zIdxName = pTab->zName;
 83188     }else{
 83189       zIdxName = pIdx->zName;
 83191     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
 83193     /*
 83194     ** Pseudo-code for loop that calls stat_push():
 83195     **
 83196     **   Rewind csr
 83197     **   if eof(csr) goto end_of_scan;
 83198     **   regChng = 0
 83199     **   goto chng_addr_0;
 83200     **
 83201     **  next_row:
 83202     **   regChng = 0
 83203     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
 83204     **   regChng = 1
 83205     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
 83206     **   ...
 83207     **   regChng = N
 83208     **   goto chng_addr_N
 83209     **
 83210     **  chng_addr_0:
 83211     **   regPrev(0) = idx(0)
 83212     **  chng_addr_1:
 83213     **   regPrev(1) = idx(1)
 83214     **  ...
 83215     **
 83216     **  chng_addr_N:
 83217     **   regRowid = idx(rowid)
 83218     **   stat_push(P, regChng, regRowid)
 83219     **   Next csr
 83220     **   if !eof(csr) goto next_row;
 83221     **
 83222     **  end_of_scan:
 83223     */
 83225     /* Make sure there are enough memory cells allocated to accommodate 
 83226     ** the regPrev array and a trailing rowid (the rowid slot is required
 83227     ** when building a record to insert into the sample column of 
 83228     ** the sqlite_stat4 table.  */
 83229     pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
 83231     /* Open a read-only cursor on the index being analyzed. */
 83232     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
 83233     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
 83234     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 83235     VdbeComment((v, "%s", pIdx->zName));
 83237     /* Invoke the stat_init() function. The arguments are:
 83238     ** 
 83239     **    (1) the number of columns in the index including the rowid,
 83240     **    (2) the number of rows in the index,
 83241     **
 83242     ** The second argument is only used for STAT3 and STAT4
 83243     */
 83244 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83245     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
 83246 #endif
 83247     sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
 83248     sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
 83249     sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
 83250     sqlite3VdbeChangeP5(v, 1+IsStat34);
 83252     /* Implementation of the following:
 83253     **
 83254     **   Rewind csr
 83255     **   if eof(csr) goto end_of_scan;
 83256     **   regChng = 0
 83257     **   goto next_push_0;
 83258     **
 83259     */
 83260     addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
 83261     VdbeCoverage(v);
 83262     sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
 83263     addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
 83265     /*
 83266     **  next_row:
 83267     **   regChng = 0
 83268     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
 83269     **   regChng = 1
 83270     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
 83271     **   ...
 83272     **   regChng = N
 83273     **   goto chng_addr_N
 83274     */
 83275     addrNextRow = sqlite3VdbeCurrentAddr(v);
 83276     for(i=0; i<nCol; i++){
 83277       char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
 83278       sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
 83279       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
 83280       aGotoChng[i] = 
 83281       sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
 83282       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 83283       VdbeCoverage(v);
 83285     sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
 83286     aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
 83288     /*
 83289     **  chng_addr_0:
 83290     **   regPrev(0) = idx(0)
 83291     **  chng_addr_1:
 83292     **   regPrev(1) = idx(1)
 83293     **  ...
 83294     */
 83295     sqlite3VdbeJumpHere(v, addrGotoChng0);
 83296     for(i=0; i<nCol; i++){
 83297       sqlite3VdbeJumpHere(v, aGotoChng[i]);
 83298       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
 83301     /*
 83302     **  chng_addr_N:
 83303     **   regRowid = idx(rowid)            // STAT34 only
 83304     **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
 83305     **   Next csr
 83306     **   if !eof(csr) goto next_row;
 83307     */
 83308     sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
 83309 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83310     assert( regRowid==(regStat4+2) );
 83311     if( HasRowid(pTab) ){
 83312       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
 83313     }else{
 83314       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
 83315       int j, k, regKey;
 83316       regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
 83317       for(j=0; j<pPk->nKeyCol; j++){
 83318         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
 83319         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
 83320         VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
 83322       sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
 83323       sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
 83325 #endif
 83326     assert( regChng==(regStat4+1) );
 83327     sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
 83328     sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
 83329     sqlite3VdbeChangeP5(v, 2+IsStat34);
 83330     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
 83332     /* Add the entry to the stat1 table. */
 83333     callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
 83334     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
 83335     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 83336     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
 83337     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 83339     /* Add the entries to the stat3 or stat4 table. */
 83340 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83342       int regEq = regStat1;
 83343       int regLt = regStat1+1;
 83344       int regDLt = regStat1+2;
 83345       int regSample = regStat1+3;
 83346       int regCol = regStat1+4;
 83347       int regSampleRowid = regCol + nCol;
 83348       int addrNext;
 83349       int addrIsNull;
 83350       u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
 83352       pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
 83354       addrNext = sqlite3VdbeCurrentAddr(v);
 83355       callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
 83356       addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
 83357       VdbeCoverage(v);
 83358       callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
 83359       callStatGet(v, regStat4, STAT_GET_NLT, regLt);
 83360       callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
 83361       sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
 83362       /* We know that the regSampleRowid row exists because it was read by
 83363       ** the previous loop.  Thus the not-found jump of seekOp will never
 83364       ** be taken */
 83365       VdbeCoverageNeverTaken(v);
 83366 #ifdef SQLITE_ENABLE_STAT3
 83367       sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, 
 83368                                       pIdx->aiColumn[0], regSample);
 83369 #else
 83370       for(i=0; i<nCol; i++){
 83371         i16 iCol = pIdx->aiColumn[i];
 83372         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
 83374       sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
 83375 #endif
 83376       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
 83377       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
 83378       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
 83379       sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
 83380       sqlite3VdbeJumpHere(v, addrIsNull);
 83382 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 83384     /* End of analysis */
 83385     sqlite3VdbeJumpHere(v, addrRewind);
 83386     sqlite3DbFree(db, aGotoChng);
 83390   /* Create a single sqlite_stat1 entry containing NULL as the index
 83391   ** name and the row count as the content.
 83392   */
 83393   if( pOnlyIdx==0 && needTableCnt ){
 83394     VdbeComment((v, "%s", pTab->zName));
 83395     sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
 83396     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
 83397     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 83398     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
 83399     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
 83400     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
 83401     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 83402     sqlite3VdbeJumpHere(v, jZeroRows);
 83407 /*
 83408 ** Generate code that will cause the most recent index analysis to
 83409 ** be loaded into internal hash tables where is can be used.
 83410 */
 83411 static void loadAnalysis(Parse *pParse, int iDb){
 83412   Vdbe *v = sqlite3GetVdbe(pParse);
 83413   if( v ){
 83414     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
 83418 /*
 83419 ** Generate code that will do an analysis of an entire database
 83420 */
 83421 static void analyzeDatabase(Parse *pParse, int iDb){
 83422   sqlite3 *db = pParse->db;
 83423   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
 83424   HashElem *k;
 83425   int iStatCur;
 83426   int iMem;
 83427   int iTab;
 83429   sqlite3BeginWriteOperation(pParse, 0, iDb);
 83430   iStatCur = pParse->nTab;
 83431   pParse->nTab += 3;
 83432   openStatTable(pParse, iDb, iStatCur, 0, 0);
 83433   iMem = pParse->nMem+1;
 83434   iTab = pParse->nTab;
 83435   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 83436   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
 83437     Table *pTab = (Table*)sqliteHashData(k);
 83438     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
 83440   loadAnalysis(pParse, iDb);
 83443 /*
 83444 ** Generate code that will do an analysis of a single table in
 83445 ** a database.  If pOnlyIdx is not NULL then it is a single index
 83446 ** in pTab that should be analyzed.
 83447 */
 83448 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
 83449   int iDb;
 83450   int iStatCur;
 83452   assert( pTab!=0 );
 83453   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 83454   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 83455   sqlite3BeginWriteOperation(pParse, 0, iDb);
 83456   iStatCur = pParse->nTab;
 83457   pParse->nTab += 3;
 83458   if( pOnlyIdx ){
 83459     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
 83460   }else{
 83461     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
 83463   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
 83464   loadAnalysis(pParse, iDb);
 83467 /*
 83468 ** Generate code for the ANALYZE command.  The parser calls this routine
 83469 ** when it recognizes an ANALYZE command.
 83470 **
 83471 **        ANALYZE                            -- 1
 83472 **        ANALYZE  <database>                -- 2
 83473 **        ANALYZE  ?<database>.?<tablename>  -- 3
 83474 **
 83475 ** Form 1 causes all indices in all attached databases to be analyzed.
 83476 ** Form 2 analyzes all indices the single database named.
 83477 ** Form 3 analyzes all indices associated with the named table.
 83478 */
 83479 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
 83480   sqlite3 *db = pParse->db;
 83481   int iDb;
 83482   int i;
 83483   char *z, *zDb;
 83484   Table *pTab;
 83485   Index *pIdx;
 83486   Token *pTableName;
 83488   /* Read the database schema. If an error occurs, leave an error message
 83489   ** and code in pParse and return NULL. */
 83490   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 83491   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 83492     return;
 83495   assert( pName2!=0 || pName1==0 );
 83496   if( pName1==0 ){
 83497     /* Form 1:  Analyze everything */
 83498     for(i=0; i<db->nDb; i++){
 83499       if( i==1 ) continue;  /* Do not analyze the TEMP database */
 83500       analyzeDatabase(pParse, i);
 83502   }else if( pName2->n==0 ){
 83503     /* Form 2:  Analyze the database or table named */
 83504     iDb = sqlite3FindDb(db, pName1);
 83505     if( iDb>=0 ){
 83506       analyzeDatabase(pParse, iDb);
 83507     }else{
 83508       z = sqlite3NameFromToken(db, pName1);
 83509       if( z ){
 83510         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
 83511           analyzeTable(pParse, pIdx->pTable, pIdx);
 83512         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
 83513           analyzeTable(pParse, pTab, 0);
 83515         sqlite3DbFree(db, z);
 83518   }else{
 83519     /* Form 3: Analyze the fully qualified table name */
 83520     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 83521     if( iDb>=0 ){
 83522       zDb = db->aDb[iDb].zName;
 83523       z = sqlite3NameFromToken(db, pTableName);
 83524       if( z ){
 83525         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
 83526           analyzeTable(pParse, pIdx->pTable, pIdx);
 83527         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
 83528           analyzeTable(pParse, pTab, 0);
 83530         sqlite3DbFree(db, z);
 83536 /*
 83537 ** Used to pass information from the analyzer reader through to the
 83538 ** callback routine.
 83539 */
 83540 typedef struct analysisInfo analysisInfo;
 83541 struct analysisInfo {
 83542   sqlite3 *db;
 83543   const char *zDatabase;
 83544 };
 83546 /*
 83547 ** The first argument points to a nul-terminated string containing a
 83548 ** list of space separated integers. Read the first nOut of these into
 83549 ** the array aOut[].
 83550 */
 83551 static void decodeIntArray(
 83552   char *zIntArray,       /* String containing int array to decode */
 83553   int nOut,              /* Number of slots in aOut[] */
 83554   tRowcnt *aOut,         /* Store integers here */
 83555   Index *pIndex          /* Handle extra flags for this index, if not NULL */
 83556 ){
 83557   char *z = zIntArray;
 83558   int c;
 83559   int i;
 83560   tRowcnt v;
 83562 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83563   if( z==0 ) z = "";
 83564 #else
 83565   if( NEVER(z==0) ) z = "";
 83566 #endif
 83567   for(i=0; *z && i<nOut; i++){
 83568     v = 0;
 83569     while( (c=z[0])>='0' && c<='9' ){
 83570       v = v*10 + c - '0';
 83571       z++;
 83573     aOut[i] = v;
 83574     if( *z==' ' ) z++;
 83576 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
 83577   assert( pIndex!=0 );
 83578 #else
 83579   if( pIndex )
 83580 #endif
 83582     if( strcmp(z, "unordered")==0 ){
 83583       pIndex->bUnordered = 1;
 83584     }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
 83585       int v32 = 0;
 83586       sqlite3GetInt32(z+3, &v32);
 83587       pIndex->szIdxRow = sqlite3LogEst(v32);
 83592 /*
 83593 ** This callback is invoked once for each index when reading the
 83594 ** sqlite_stat1 table.  
 83595 **
 83596 **     argv[0] = name of the table
 83597 **     argv[1] = name of the index (might be NULL)
 83598 **     argv[2] = results of analysis - on integer for each column
 83599 **
 83600 ** Entries for which argv[1]==NULL simply record the number of rows in
 83601 ** the table.
 83602 */
 83603 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
 83604   analysisInfo *pInfo = (analysisInfo*)pData;
 83605   Index *pIndex;
 83606   Table *pTable;
 83607   const char *z;
 83609   assert( argc==3 );
 83610   UNUSED_PARAMETER2(NotUsed, argc);
 83612   if( argv==0 || argv[0]==0 || argv[2]==0 ){
 83613     return 0;
 83615   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
 83616   if( pTable==0 ){
 83617     return 0;
 83619   if( argv[1]==0 ){
 83620     pIndex = 0;
 83621   }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
 83622     pIndex = sqlite3PrimaryKeyIndex(pTable);
 83623   }else{
 83624     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
 83626   z = argv[2];
 83628   if( pIndex ){
 83629     decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
 83630     if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
 83631   }else{
 83632     Index fakeIdx;
 83633     fakeIdx.szIdxRow = pTable->szTabRow;
 83634     decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
 83635     pTable->szTabRow = fakeIdx.szIdxRow;
 83638   return 0;
 83641 /*
 83642 ** If the Index.aSample variable is not NULL, delete the aSample[] array
 83643 ** and its contents.
 83644 */
 83645 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
 83646 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83647   if( pIdx->aSample ){
 83648     int j;
 83649     for(j=0; j<pIdx->nSample; j++){
 83650       IndexSample *p = &pIdx->aSample[j];
 83651       sqlite3DbFree(db, p->p);
 83653     sqlite3DbFree(db, pIdx->aSample);
 83655   if( db && db->pnBytesFreed==0 ){
 83656     pIdx->nSample = 0;
 83657     pIdx->aSample = 0;
 83659 #else
 83660   UNUSED_PARAMETER(db);
 83661   UNUSED_PARAMETER(pIdx);
 83662 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 83665 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83666 /*
 83667 ** Populate the pIdx->aAvgEq[] array based on the samples currently
 83668 ** stored in pIdx->aSample[]. 
 83669 */
 83670 static void initAvgEq(Index *pIdx){
 83671   if( pIdx ){
 83672     IndexSample *aSample = pIdx->aSample;
 83673     IndexSample *pFinal = &aSample[pIdx->nSample-1];
 83674     int iCol;
 83675     for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
 83676       int i;                    /* Used to iterate through samples */
 83677       tRowcnt sumEq = 0;        /* Sum of the nEq values */
 83678       tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
 83679       tRowcnt avgEq = 0;
 83680       tRowcnt nDLt = pFinal->anDLt[iCol];
 83682       /* Set nSum to the number of distinct (iCol+1) field prefixes that
 83683       ** occur in the stat4 table for this index before pFinal. Set
 83684       ** sumEq to the sum of the nEq values for column iCol for the same
 83685       ** set (adding the value only once where there exist dupicate 
 83686       ** prefixes).  */
 83687       for(i=0; i<(pIdx->nSample-1); i++){
 83688         if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
 83689           sumEq += aSample[i].anEq[iCol];
 83690           nSum++;
 83693       if( nDLt>nSum ){
 83694         avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
 83696       if( avgEq==0 ) avgEq = 1;
 83697       pIdx->aAvgEq[iCol] = avgEq;
 83698       if( pIdx->nSampleCol==1 ) break;
 83703 /*
 83704 ** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
 83705 ** is supplied instead, find the PRIMARY KEY index for that table.
 83706 */
 83707 static Index *findIndexOrPrimaryKey(
 83708   sqlite3 *db,
 83709   const char *zName,
 83710   const char *zDb
 83711 ){
 83712   Index *pIdx = sqlite3FindIndex(db, zName, zDb);
 83713   if( pIdx==0 ){
 83714     Table *pTab = sqlite3FindTable(db, zName, zDb);
 83715     if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
 83717   return pIdx;
 83720 /*
 83721 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table 
 83722 ** into the relevant Index.aSample[] arrays.
 83723 **
 83724 ** Arguments zSql1 and zSql2 must point to SQL statements that return
 83725 ** data equivalent to the following (statements are different for stat3,
 83726 ** see the caller of this function for details):
 83727 **
 83728 **    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
 83729 **    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
 83730 **
 83731 ** where %Q is replaced with the database name before the SQL is executed.
 83732 */
 83733 static int loadStatTbl(
 83734   sqlite3 *db,                  /* Database handle */
 83735   int bStat3,                   /* Assume single column records only */
 83736   const char *zSql1,            /* SQL statement 1 (see above) */
 83737   const char *zSql2,            /* SQL statement 2 (see above) */
 83738   const char *zDb               /* Database name (e.g. "main") */
 83739 ){
 83740   int rc;                       /* Result codes from subroutines */
 83741   sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
 83742   char *zSql;                   /* Text of the SQL statement */
 83743   Index *pPrevIdx = 0;          /* Previous index in the loop */
 83744   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
 83746   assert( db->lookaside.bEnabled==0 );
 83747   zSql = sqlite3MPrintf(db, zSql1, zDb);
 83748   if( !zSql ){
 83749     return SQLITE_NOMEM;
 83751   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 83752   sqlite3DbFree(db, zSql);
 83753   if( rc ) return rc;
 83755   while( sqlite3_step(pStmt)==SQLITE_ROW ){
 83756     int nIdxCol = 1;              /* Number of columns in stat4 records */
 83757     int nAvgCol = 1;              /* Number of entries in Index.aAvgEq */
 83759     char *zIndex;   /* Index name */
 83760     Index *pIdx;    /* Pointer to the index object */
 83761     int nSample;    /* Number of samples */
 83762     int nByte;      /* Bytes of space required */
 83763     int i;          /* Bytes of space required */
 83764     tRowcnt *pSpace;
 83766     zIndex = (char *)sqlite3_column_text(pStmt, 0);
 83767     if( zIndex==0 ) continue;
 83768     nSample = sqlite3_column_int(pStmt, 1);
 83769     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
 83770     assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
 83771     /* Index.nSample is non-zero at this point if data has already been
 83772     ** loaded from the stat4 table. In this case ignore stat3 data.  */
 83773     if( pIdx==0 || pIdx->nSample ) continue;
 83774     if( bStat3==0 ){
 83775       nIdxCol = pIdx->nKeyCol+1;
 83776       nAvgCol = pIdx->nKeyCol;
 83778     pIdx->nSampleCol = nIdxCol;
 83779     nByte = sizeof(IndexSample) * nSample;
 83780     nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
 83781     nByte += nAvgCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
 83783     pIdx->aSample = sqlite3DbMallocZero(db, nByte);
 83784     if( pIdx->aSample==0 ){
 83785       sqlite3_finalize(pStmt);
 83786       return SQLITE_NOMEM;
 83788     pSpace = (tRowcnt*)&pIdx->aSample[nSample];
 83789     pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
 83790     for(i=0; i<nSample; i++){
 83791       pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
 83792       pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
 83793       pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
 83795     assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
 83797   rc = sqlite3_finalize(pStmt);
 83798   if( rc ) return rc;
 83800   zSql = sqlite3MPrintf(db, zSql2, zDb);
 83801   if( !zSql ){
 83802     return SQLITE_NOMEM;
 83804   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 83805   sqlite3DbFree(db, zSql);
 83806   if( rc ) return rc;
 83808   while( sqlite3_step(pStmt)==SQLITE_ROW ){
 83809     char *zIndex;                 /* Index name */
 83810     Index *pIdx;                  /* Pointer to the index object */
 83811     int nCol = 1;                 /* Number of columns in index */
 83813     zIndex = (char *)sqlite3_column_text(pStmt, 0);
 83814     if( zIndex==0 ) continue;
 83815     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
 83816     if( pIdx==0 ) continue;
 83817     /* This next condition is true if data has already been loaded from 
 83818     ** the sqlite_stat4 table. In this case ignore stat3 data.  */
 83819     nCol = pIdx->nSampleCol;
 83820     if( bStat3 && nCol>1 ) continue;
 83821     if( pIdx!=pPrevIdx ){
 83822       initAvgEq(pPrevIdx);
 83823       pPrevIdx = pIdx;
 83825     pSample = &pIdx->aSample[pIdx->nSample];
 83826     decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
 83827     decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
 83828     decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
 83830     /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
 83831     ** This is in case the sample record is corrupted. In that case, the
 83832     ** sqlite3VdbeRecordCompare() may read up to two varints past the
 83833     ** end of the allocated buffer before it realizes it is dealing with
 83834     ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
 83835     ** a buffer overread.  */
 83836     pSample->n = sqlite3_column_bytes(pStmt, 4);
 83837     pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
 83838     if( pSample->p==0 ){
 83839       sqlite3_finalize(pStmt);
 83840       return SQLITE_NOMEM;
 83842     memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
 83843     pIdx->nSample++;
 83845   rc = sqlite3_finalize(pStmt);
 83846   if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
 83847   return rc;
 83850 /*
 83851 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into 
 83852 ** the Index.aSample[] arrays of all indices.
 83853 */
 83854 static int loadStat4(sqlite3 *db, const char *zDb){
 83855   int rc = SQLITE_OK;             /* Result codes from subroutines */
 83857   assert( db->lookaside.bEnabled==0 );
 83858   if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
 83859     rc = loadStatTbl(db, 0,
 83860       "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", 
 83861       "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
 83862       zDb
 83863     );
 83866   if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
 83867     rc = loadStatTbl(db, 1,
 83868       "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", 
 83869       "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
 83870       zDb
 83871     );
 83874   return rc;
 83876 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 83878 /*
 83879 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
 83880 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
 83881 ** arrays. The contents of sqlite_stat3/4 are used to populate the
 83882 ** Index.aSample[] arrays.
 83883 **
 83884 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
 83885 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined 
 83886 ** during compilation and the sqlite_stat3/4 table is present, no data is 
 83887 ** read from it.
 83888 **
 83889 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the 
 83890 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
 83891 ** returned. However, in this case, data is read from the sqlite_stat1
 83892 ** table (if it is present) before returning.
 83893 **
 83894 ** If an OOM error occurs, this function always sets db->mallocFailed.
 83895 ** This means if the caller does not care about other errors, the return
 83896 ** code may be ignored.
 83897 */
 83898 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
 83899   analysisInfo sInfo;
 83900   HashElem *i;
 83901   char *zSql;
 83902   int rc;
 83904   assert( iDb>=0 && iDb<db->nDb );
 83905   assert( db->aDb[iDb].pBt!=0 );
 83907   /* Clear any prior statistics */
 83908   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 83909   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
 83910     Index *pIdx = sqliteHashData(i);
 83911     sqlite3DefaultRowEst(pIdx);
 83912 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83913     sqlite3DeleteIndexSamples(db, pIdx);
 83914     pIdx->aSample = 0;
 83915 #endif
 83918   /* Check to make sure the sqlite_stat1 table exists */
 83919   sInfo.db = db;
 83920   sInfo.zDatabase = db->aDb[iDb].zName;
 83921   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
 83922     return SQLITE_ERROR;
 83925   /* Load new statistics out of the sqlite_stat1 table */
 83926   zSql = sqlite3MPrintf(db, 
 83927       "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
 83928   if( zSql==0 ){
 83929     rc = SQLITE_NOMEM;
 83930   }else{
 83931     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
 83932     sqlite3DbFree(db, zSql);
 83936   /* Load the statistics from the sqlite_stat4 table. */
 83937 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 83938   if( rc==SQLITE_OK ){
 83939     int lookasideEnabled = db->lookaside.bEnabled;
 83940     db->lookaside.bEnabled = 0;
 83941     rc = loadStat4(db, sInfo.zDatabase);
 83942     db->lookaside.bEnabled = lookasideEnabled;
 83944 #endif
 83946   if( rc==SQLITE_NOMEM ){
 83947     db->mallocFailed = 1;
 83949   return rc;
 83953 #endif /* SQLITE_OMIT_ANALYZE */
 83955 /************** End of analyze.c *********************************************/
 83956 /************** Begin file attach.c ******************************************/
 83957 /*
 83958 ** 2003 April 6
 83959 **
 83960 ** The author disclaims copyright to this source code.  In place of
 83961 ** a legal notice, here is a blessing:
 83962 **
 83963 **    May you do good and not evil.
 83964 **    May you find forgiveness for yourself and forgive others.
 83965 **    May you share freely, never taking more than you give.
 83966 **
 83967 *************************************************************************
 83968 ** This file contains code used to implement the ATTACH and DETACH commands.
 83969 */
 83971 #ifndef SQLITE_OMIT_ATTACH
 83972 /*
 83973 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
 83974 ** is slightly different from resolving a normal SQL expression, because simple
 83975 ** identifiers are treated as strings, not possible column names or aliases.
 83976 **
 83977 ** i.e. if the parser sees:
 83978 **
 83979 **     ATTACH DATABASE abc AS def
 83980 **
 83981 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
 83982 ** looking for columns of the same name.
 83983 **
 83984 ** This only applies to the root node of pExpr, so the statement:
 83985 **
 83986 **     ATTACH DATABASE abc||def AS 'db2'
 83987 **
 83988 ** will fail because neither abc or def can be resolved.
 83989 */
 83990 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
 83992   int rc = SQLITE_OK;
 83993   if( pExpr ){
 83994     if( pExpr->op!=TK_ID ){
 83995       rc = sqlite3ResolveExprNames(pName, pExpr);
 83996     }else{
 83997       pExpr->op = TK_STRING;
 84000   return rc;
 84003 /*
 84004 ** An SQL user-function registered to do the work of an ATTACH statement. The
 84005 ** three arguments to the function come directly from an attach statement:
 84006 **
 84007 **     ATTACH DATABASE x AS y KEY z
 84008 **
 84009 **     SELECT sqlite_attach(x, y, z)
 84010 **
 84011 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
 84012 ** third argument.
 84013 */
 84014 static void attachFunc(
 84015   sqlite3_context *context,
 84016   int NotUsed,
 84017   sqlite3_value **argv
 84018 ){
 84019   int i;
 84020   int rc = 0;
 84021   sqlite3 *db = sqlite3_context_db_handle(context);
 84022   const char *zName;
 84023   const char *zFile;
 84024   char *zPath = 0;
 84025   char *zErr = 0;
 84026   unsigned int flags;
 84027   Db *aNew;
 84028   char *zErrDyn = 0;
 84029   sqlite3_vfs *pVfs;
 84031   UNUSED_PARAMETER(NotUsed);
 84033   zFile = (const char *)sqlite3_value_text(argv[0]);
 84034   zName = (const char *)sqlite3_value_text(argv[1]);
 84035   if( zFile==0 ) zFile = "";
 84036   if( zName==0 ) zName = "";
 84038   /* Check for the following errors:
 84039   **
 84040   **     * Too many attached databases,
 84041   **     * Transaction currently open
 84042   **     * Specified database name already being used.
 84043   */
 84044   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
 84045     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
 84046       db->aLimit[SQLITE_LIMIT_ATTACHED]
 84047     );
 84048     goto attach_error;
 84050   if( !db->autoCommit ){
 84051     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
 84052     goto attach_error;
 84054   for(i=0; i<db->nDb; i++){
 84055     char *z = db->aDb[i].zName;
 84056     assert( z && zName );
 84057     if( sqlite3StrICmp(z, zName)==0 ){
 84058       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
 84059       goto attach_error;
 84063   /* Allocate the new entry in the db->aDb[] array and initialize the schema
 84064   ** hash tables.
 84065   */
 84066   if( db->aDb==db->aDbStatic ){
 84067     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
 84068     if( aNew==0 ) return;
 84069     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
 84070   }else{
 84071     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
 84072     if( aNew==0 ) return;
 84074   db->aDb = aNew;
 84075   aNew = &db->aDb[db->nDb];
 84076   memset(aNew, 0, sizeof(*aNew));
 84078   /* Open the database file. If the btree is successfully opened, use
 84079   ** it to obtain the database schema. At this point the schema may
 84080   ** or may not be initialized.
 84081   */
 84082   flags = db->openFlags;
 84083   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
 84084   if( rc!=SQLITE_OK ){
 84085     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 84086     sqlite3_result_error(context, zErr, -1);
 84087     sqlite3_free(zErr);
 84088     return;
 84090   assert( pVfs );
 84091   flags |= SQLITE_OPEN_MAIN_DB;
 84092   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
 84093   sqlite3_free( zPath );
 84094   db->nDb++;
 84095   if( rc==SQLITE_CONSTRAINT ){
 84096     rc = SQLITE_ERROR;
 84097     zErrDyn = sqlite3MPrintf(db, "database is already attached");
 84098   }else if( rc==SQLITE_OK ){
 84099     Pager *pPager;
 84100     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
 84101     if( !aNew->pSchema ){
 84102       rc = SQLITE_NOMEM;
 84103     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
 84104       zErrDyn = sqlite3MPrintf(db, 
 84105         "attached databases must use the same text encoding as main database");
 84106       rc = SQLITE_ERROR;
 84108     pPager = sqlite3BtreePager(aNew->pBt);
 84109     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
 84110     sqlite3BtreeSecureDelete(aNew->pBt,
 84111                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
 84112 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 84113     sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
 84114 #endif
 84116   aNew->safety_level = 3;
 84117   aNew->zName = sqlite3DbStrDup(db, zName);
 84118   if( rc==SQLITE_OK && aNew->zName==0 ){
 84119     rc = SQLITE_NOMEM;
 84123 #ifdef SQLITE_HAS_CODEC
 84124   if( rc==SQLITE_OK ){
 84125     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
 84126     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
 84127     int nKey;
 84128     char *zKey;
 84129     int t = sqlite3_value_type(argv[2]);
 84130     switch( t ){
 84131       case SQLITE_INTEGER:
 84132       case SQLITE_FLOAT:
 84133         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
 84134         rc = SQLITE_ERROR;
 84135         break;
 84137       case SQLITE_TEXT:
 84138       case SQLITE_BLOB:
 84139         nKey = sqlite3_value_bytes(argv[2]);
 84140         zKey = (char *)sqlite3_value_blob(argv[2]);
 84141         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 84142         break;
 84144       case SQLITE_NULL:
 84145         /* No key specified.  Use the key from the main database */
 84146         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 84147         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
 84148           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 84150         break;
 84153 #endif
 84155   /* If the file was opened successfully, read the schema for the new database.
 84156   ** If this fails, or if opening the file failed, then close the file and 
 84157   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
 84158   ** we found it.
 84159   */
 84160   if( rc==SQLITE_OK ){
 84161     sqlite3BtreeEnterAll(db);
 84162     rc = sqlite3Init(db, &zErrDyn);
 84163     sqlite3BtreeLeaveAll(db);
 84165   if( rc ){
 84166     int iDb = db->nDb - 1;
 84167     assert( iDb>=2 );
 84168     if( db->aDb[iDb].pBt ){
 84169       sqlite3BtreeClose(db->aDb[iDb].pBt);
 84170       db->aDb[iDb].pBt = 0;
 84171       db->aDb[iDb].pSchema = 0;
 84173     sqlite3ResetAllSchemasOfConnection(db);
 84174     db->nDb = iDb;
 84175     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 84176       db->mallocFailed = 1;
 84177       sqlite3DbFree(db, zErrDyn);
 84178       zErrDyn = sqlite3MPrintf(db, "out of memory");
 84179     }else if( zErrDyn==0 ){
 84180       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
 84182     goto attach_error;
 84185   return;
 84187 attach_error:
 84188   /* Return an error if we get here */
 84189   if( zErrDyn ){
 84190     sqlite3_result_error(context, zErrDyn, -1);
 84191     sqlite3DbFree(db, zErrDyn);
 84193   if( rc ) sqlite3_result_error_code(context, rc);
 84196 /*
 84197 ** An SQL user-function registered to do the work of an DETACH statement. The
 84198 ** three arguments to the function come directly from a detach statement:
 84199 **
 84200 **     DETACH DATABASE x
 84201 **
 84202 **     SELECT sqlite_detach(x)
 84203 */
 84204 static void detachFunc(
 84205   sqlite3_context *context,
 84206   int NotUsed,
 84207   sqlite3_value **argv
 84208 ){
 84209   const char *zName = (const char *)sqlite3_value_text(argv[0]);
 84210   sqlite3 *db = sqlite3_context_db_handle(context);
 84211   int i;
 84212   Db *pDb = 0;
 84213   char zErr[128];
 84215   UNUSED_PARAMETER(NotUsed);
 84217   if( zName==0 ) zName = "";
 84218   for(i=0; i<db->nDb; i++){
 84219     pDb = &db->aDb[i];
 84220     if( pDb->pBt==0 ) continue;
 84221     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
 84224   if( i>=db->nDb ){
 84225     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
 84226     goto detach_error;
 84228   if( i<2 ){
 84229     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
 84230     goto detach_error;
 84232   if( !db->autoCommit ){
 84233     sqlite3_snprintf(sizeof(zErr), zErr,
 84234                      "cannot DETACH database within transaction");
 84235     goto detach_error;
 84237   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
 84238     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 84239     goto detach_error;
 84242   sqlite3BtreeClose(pDb->pBt);
 84243   pDb->pBt = 0;
 84244   pDb->pSchema = 0;
 84245   sqlite3ResetAllSchemasOfConnection(db);
 84246   return;
 84248 detach_error:
 84249   sqlite3_result_error(context, zErr, -1);
 84252 /*
 84253 ** This procedure generates VDBE code for a single invocation of either the
 84254 ** sqlite_detach() or sqlite_attach() SQL user functions.
 84255 */
 84256 static void codeAttach(
 84257   Parse *pParse,       /* The parser context */
 84258   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
 84259   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
 84260   Expr *pAuthArg,      /* Expression to pass to authorization callback */
 84261   Expr *pFilename,     /* Name of database file */
 84262   Expr *pDbname,       /* Name of the database to use internally */
 84263   Expr *pKey           /* Database key for encryption extension */
 84264 ){
 84265   int rc;
 84266   NameContext sName;
 84267   Vdbe *v;
 84268   sqlite3* db = pParse->db;
 84269   int regArgs;
 84271   memset(&sName, 0, sizeof(NameContext));
 84272   sName.pParse = pParse;
 84274   if( 
 84275       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
 84276       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
 84277       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
 84278   ){
 84279     pParse->nErr++;
 84280     goto attach_end;
 84283 #ifndef SQLITE_OMIT_AUTHORIZATION
 84284   if( pAuthArg ){
 84285     char *zAuthArg;
 84286     if( pAuthArg->op==TK_STRING ){
 84287       zAuthArg = pAuthArg->u.zToken;
 84288     }else{
 84289       zAuthArg = 0;
 84291     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
 84292     if(rc!=SQLITE_OK ){
 84293       goto attach_end;
 84296 #endif /* SQLITE_OMIT_AUTHORIZATION */
 84299   v = sqlite3GetVdbe(pParse);
 84300   regArgs = sqlite3GetTempRange(pParse, 4);
 84301   sqlite3ExprCode(pParse, pFilename, regArgs);
 84302   sqlite3ExprCode(pParse, pDbname, regArgs+1);
 84303   sqlite3ExprCode(pParse, pKey, regArgs+2);
 84305   assert( v || db->mallocFailed );
 84306   if( v ){
 84307     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
 84308     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
 84309     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
 84310     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
 84312     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
 84313     ** statement only). For DETACH, set it to false (expire all existing
 84314     ** statements).
 84315     */
 84316     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
 84319 attach_end:
 84320   sqlite3ExprDelete(db, pFilename);
 84321   sqlite3ExprDelete(db, pDbname);
 84322   sqlite3ExprDelete(db, pKey);
 84325 /*
 84326 ** Called by the parser to compile a DETACH statement.
 84327 **
 84328 **     DETACH pDbname
 84329 */
 84330 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
 84331   static const FuncDef detach_func = {
 84332     1,                /* nArg */
 84333     SQLITE_UTF8,      /* funcFlags */
 84334     0,                /* pUserData */
 84335     0,                /* pNext */
 84336     detachFunc,       /* xFunc */
 84337     0,                /* xStep */
 84338     0,                /* xFinalize */
 84339     "sqlite_detach",  /* zName */
 84340     0,                /* pHash */
 84341     0                 /* pDestructor */
 84342   };
 84343   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
 84346 /*
 84347 ** Called by the parser to compile an ATTACH statement.
 84348 **
 84349 **     ATTACH p AS pDbname KEY pKey
 84350 */
 84351 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
 84352   static const FuncDef attach_func = {
 84353     3,                /* nArg */
 84354     SQLITE_UTF8,      /* funcFlags */
 84355     0,                /* pUserData */
 84356     0,                /* pNext */
 84357     attachFunc,       /* xFunc */
 84358     0,                /* xStep */
 84359     0,                /* xFinalize */
 84360     "sqlite_attach",  /* zName */
 84361     0,                /* pHash */
 84362     0                 /* pDestructor */
 84363   };
 84364   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
 84366 #endif /* SQLITE_OMIT_ATTACH */
 84368 /*
 84369 ** Initialize a DbFixer structure.  This routine must be called prior
 84370 ** to passing the structure to one of the sqliteFixAAAA() routines below.
 84371 */
 84372 SQLITE_PRIVATE void sqlite3FixInit(
 84373   DbFixer *pFix,      /* The fixer to be initialized */
 84374   Parse *pParse,      /* Error messages will be written here */
 84375   int iDb,            /* This is the database that must be used */
 84376   const char *zType,  /* "view", "trigger", or "index" */
 84377   const Token *pName  /* Name of the view, trigger, or index */
 84378 ){
 84379   sqlite3 *db;
 84381   db = pParse->db;
 84382   assert( db->nDb>iDb );
 84383   pFix->pParse = pParse;
 84384   pFix->zDb = db->aDb[iDb].zName;
 84385   pFix->pSchema = db->aDb[iDb].pSchema;
 84386   pFix->zType = zType;
 84387   pFix->pName = pName;
 84388   pFix->bVarOnly = (iDb==1);
 84391 /*
 84392 ** The following set of routines walk through the parse tree and assign
 84393 ** a specific database to all table references where the database name
 84394 ** was left unspecified in the original SQL statement.  The pFix structure
 84395 ** must have been initialized by a prior call to sqlite3FixInit().
 84396 **
 84397 ** These routines are used to make sure that an index, trigger, or
 84398 ** view in one database does not refer to objects in a different database.
 84399 ** (Exception: indices, triggers, and views in the TEMP database are
 84400 ** allowed to refer to anything.)  If a reference is explicitly made
 84401 ** to an object in a different database, an error message is added to
 84402 ** pParse->zErrMsg and these routines return non-zero.  If everything
 84403 ** checks out, these routines return 0.
 84404 */
 84405 SQLITE_PRIVATE int sqlite3FixSrcList(
 84406   DbFixer *pFix,       /* Context of the fixation */
 84407   SrcList *pList       /* The Source list to check and modify */
 84408 ){
 84409   int i;
 84410   const char *zDb;
 84411   struct SrcList_item *pItem;
 84413   if( NEVER(pList==0) ) return 0;
 84414   zDb = pFix->zDb;
 84415   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 84416     if( pFix->bVarOnly==0 ){
 84417       if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
 84418         sqlite3ErrorMsg(pFix->pParse,
 84419             "%s %T cannot reference objects in database %s",
 84420             pFix->zType, pFix->pName, pItem->zDatabase);
 84421         return 1;
 84423       sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
 84424       pItem->zDatabase = 0;
 84425       pItem->pSchema = pFix->pSchema;
 84427 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 84428     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
 84429     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
 84430 #endif
 84432   return 0;
 84434 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
 84435 SQLITE_PRIVATE int sqlite3FixSelect(
 84436   DbFixer *pFix,       /* Context of the fixation */
 84437   Select *pSelect      /* The SELECT statement to be fixed to one database */
 84438 ){
 84439   while( pSelect ){
 84440     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
 84441       return 1;
 84443     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
 84444       return 1;
 84446     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
 84447       return 1;
 84449     if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
 84450       return 1;
 84452     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
 84453       return 1;
 84455     if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
 84456       return 1;
 84458     if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
 84459       return 1;
 84461     if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
 84462       return 1;
 84464     pSelect = pSelect->pPrior;
 84466   return 0;
 84468 SQLITE_PRIVATE int sqlite3FixExpr(
 84469   DbFixer *pFix,     /* Context of the fixation */
 84470   Expr *pExpr        /* The expression to be fixed to one database */
 84471 ){
 84472   while( pExpr ){
 84473     if( pExpr->op==TK_VARIABLE ){
 84474       if( pFix->pParse->db->init.busy ){
 84475         pExpr->op = TK_NULL;
 84476       }else{
 84477         sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
 84478         return 1;
 84481     if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
 84482     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 84483       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
 84484     }else{
 84485       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
 84487     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
 84488       return 1;
 84490     pExpr = pExpr->pLeft;
 84492   return 0;
 84494 SQLITE_PRIVATE int sqlite3FixExprList(
 84495   DbFixer *pFix,     /* Context of the fixation */
 84496   ExprList *pList    /* The expression to be fixed to one database */
 84497 ){
 84498   int i;
 84499   struct ExprList_item *pItem;
 84500   if( pList==0 ) return 0;
 84501   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
 84502     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
 84503       return 1;
 84506   return 0;
 84508 #endif
 84510 #ifndef SQLITE_OMIT_TRIGGER
 84511 SQLITE_PRIVATE int sqlite3FixTriggerStep(
 84512   DbFixer *pFix,     /* Context of the fixation */
 84513   TriggerStep *pStep /* The trigger step be fixed to one database */
 84514 ){
 84515   while( pStep ){
 84516     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
 84517       return 1;
 84519     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
 84520       return 1;
 84522     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
 84523       return 1;
 84525     pStep = pStep->pNext;
 84527   return 0;
 84529 #endif
 84531 /************** End of attach.c **********************************************/
 84532 /************** Begin file auth.c ********************************************/
 84533 /*
 84534 ** 2003 January 11
 84535 **
 84536 ** The author disclaims copyright to this source code.  In place of
 84537 ** a legal notice, here is a blessing:
 84538 **
 84539 **    May you do good and not evil.
 84540 **    May you find forgiveness for yourself and forgive others.
 84541 **    May you share freely, never taking more than you give.
 84542 **
 84543 *************************************************************************
 84544 ** This file contains code used to implement the sqlite3_set_authorizer()
 84545 ** API.  This facility is an optional feature of the library.  Embedded
 84546 ** systems that do not need this facility may omit it by recompiling
 84547 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
 84548 */
 84550 /*
 84551 ** All of the code in this file may be omitted by defining a single
 84552 ** macro.
 84553 */
 84554 #ifndef SQLITE_OMIT_AUTHORIZATION
 84556 /*
 84557 ** Set or clear the access authorization function.
 84558 **
 84559 ** The access authorization function is be called during the compilation
 84560 ** phase to verify that the user has read and/or write access permission on
 84561 ** various fields of the database.  The first argument to the auth function
 84562 ** is a copy of the 3rd argument to this routine.  The second argument
 84563 ** to the auth function is one of these constants:
 84564 **
 84565 **       SQLITE_CREATE_INDEX
 84566 **       SQLITE_CREATE_TABLE
 84567 **       SQLITE_CREATE_TEMP_INDEX
 84568 **       SQLITE_CREATE_TEMP_TABLE
 84569 **       SQLITE_CREATE_TEMP_TRIGGER
 84570 **       SQLITE_CREATE_TEMP_VIEW
 84571 **       SQLITE_CREATE_TRIGGER
 84572 **       SQLITE_CREATE_VIEW
 84573 **       SQLITE_DELETE
 84574 **       SQLITE_DROP_INDEX
 84575 **       SQLITE_DROP_TABLE
 84576 **       SQLITE_DROP_TEMP_INDEX
 84577 **       SQLITE_DROP_TEMP_TABLE
 84578 **       SQLITE_DROP_TEMP_TRIGGER
 84579 **       SQLITE_DROP_TEMP_VIEW
 84580 **       SQLITE_DROP_TRIGGER
 84581 **       SQLITE_DROP_VIEW
 84582 **       SQLITE_INSERT
 84583 **       SQLITE_PRAGMA
 84584 **       SQLITE_READ
 84585 **       SQLITE_SELECT
 84586 **       SQLITE_TRANSACTION
 84587 **       SQLITE_UPDATE
 84588 **
 84589 ** The third and fourth arguments to the auth function are the name of
 84590 ** the table and the column that are being accessed.  The auth function
 84591 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
 84592 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
 84593 ** means that the SQL statement will never-run - the sqlite3_exec() call
 84594 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
 84595 ** should run but attempts to read the specified column will return NULL
 84596 ** and attempts to write the column will be ignored.
 84597 **
 84598 ** Setting the auth function to NULL disables this hook.  The default
 84599 ** setting of the auth function is NULL.
 84600 */
 84601 SQLITE_API int sqlite3_set_authorizer(
 84602   sqlite3 *db,
 84603   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
 84604   void *pArg
 84605 ){
 84606   sqlite3_mutex_enter(db->mutex);
 84607   db->xAuth = xAuth;
 84608   db->pAuthArg = pArg;
 84609   sqlite3ExpirePreparedStatements(db);
 84610   sqlite3_mutex_leave(db->mutex);
 84611   return SQLITE_OK;
 84614 /*
 84615 ** Write an error message into pParse->zErrMsg that explains that the
 84616 ** user-supplied authorization function returned an illegal value.
 84617 */
 84618 static void sqliteAuthBadReturnCode(Parse *pParse){
 84619   sqlite3ErrorMsg(pParse, "authorizer malfunction");
 84620   pParse->rc = SQLITE_ERROR;
 84623 /*
 84624 ** Invoke the authorization callback for permission to read column zCol from
 84625 ** table zTab in database zDb. This function assumes that an authorization
 84626 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
 84627 **
 84628 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
 84629 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
 84630 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
 84631 */
 84632 SQLITE_PRIVATE int sqlite3AuthReadCol(
 84633   Parse *pParse,                  /* The parser context */
 84634   const char *zTab,               /* Table name */
 84635   const char *zCol,               /* Column name */
 84636   int iDb                         /* Index of containing database. */
 84637 ){
 84638   sqlite3 *db = pParse->db;       /* Database handle */
 84639   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
 84640   int rc;                         /* Auth callback return code */
 84642   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
 84643   if( rc==SQLITE_DENY ){
 84644     if( db->nDb>2 || iDb!=0 ){
 84645       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
 84646     }else{
 84647       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
 84649     pParse->rc = SQLITE_AUTH;
 84650   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
 84651     sqliteAuthBadReturnCode(pParse);
 84653   return rc;
 84656 /*
 84657 ** The pExpr should be a TK_COLUMN expression.  The table referred to
 84658 ** is in pTabList or else it is the NEW or OLD table of a trigger.  
 84659 ** Check to see if it is OK to read this particular column.
 84660 **
 84661 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
 84662 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
 84663 ** then generate an error.
 84664 */
 84665 SQLITE_PRIVATE void sqlite3AuthRead(
 84666   Parse *pParse,        /* The parser context */
 84667   Expr *pExpr,          /* The expression to check authorization on */
 84668   Schema *pSchema,      /* The schema of the expression */
 84669   SrcList *pTabList     /* All table that pExpr might refer to */
 84670 ){
 84671   sqlite3 *db = pParse->db;
 84672   Table *pTab = 0;      /* The table being read */
 84673   const char *zCol;     /* Name of the column of the table */
 84674   int iSrc;             /* Index in pTabList->a[] of table being read */
 84675   int iDb;              /* The index of the database the expression refers to */
 84676   int iCol;             /* Index of column in table */
 84678   if( db->xAuth==0 ) return;
 84679   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
 84680   if( iDb<0 ){
 84681     /* An attempt to read a column out of a subquery or other
 84682     ** temporary table. */
 84683     return;
 84686   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
 84687   if( pExpr->op==TK_TRIGGER ){
 84688     pTab = pParse->pTriggerTab;
 84689   }else{
 84690     assert( pTabList );
 84691     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
 84692       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
 84693         pTab = pTabList->a[iSrc].pTab;
 84694         break;
 84698   iCol = pExpr->iColumn;
 84699   if( NEVER(pTab==0) ) return;
 84701   if( iCol>=0 ){
 84702     assert( iCol<pTab->nCol );
 84703     zCol = pTab->aCol[iCol].zName;
 84704   }else if( pTab->iPKey>=0 ){
 84705     assert( pTab->iPKey<pTab->nCol );
 84706     zCol = pTab->aCol[pTab->iPKey].zName;
 84707   }else{
 84708     zCol = "ROWID";
 84710   assert( iDb>=0 && iDb<db->nDb );
 84711   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
 84712     pExpr->op = TK_NULL;
 84716 /*
 84717 ** Do an authorization check using the code and arguments given.  Return
 84718 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
 84719 ** is returned, then the error count and error message in pParse are
 84720 ** modified appropriately.
 84721 */
 84722 SQLITE_PRIVATE int sqlite3AuthCheck(
 84723   Parse *pParse,
 84724   int code,
 84725   const char *zArg1,
 84726   const char *zArg2,
 84727   const char *zArg3
 84728 ){
 84729   sqlite3 *db = pParse->db;
 84730   int rc;
 84732   /* Don't do any authorization checks if the database is initialising
 84733   ** or if the parser is being invoked from within sqlite3_declare_vtab.
 84734   */
 84735   if( db->init.busy || IN_DECLARE_VTAB ){
 84736     return SQLITE_OK;
 84739   if( db->xAuth==0 ){
 84740     return SQLITE_OK;
 84742   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
 84743   if( rc==SQLITE_DENY ){
 84744     sqlite3ErrorMsg(pParse, "not authorized");
 84745     pParse->rc = SQLITE_AUTH;
 84746   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
 84747     rc = SQLITE_DENY;
 84748     sqliteAuthBadReturnCode(pParse);
 84750   return rc;
 84753 /*
 84754 ** Push an authorization context.  After this routine is called, the
 84755 ** zArg3 argument to authorization callbacks will be zContext until
 84756 ** popped.  Or if pParse==0, this routine is a no-op.
 84757 */
 84758 SQLITE_PRIVATE void sqlite3AuthContextPush(
 84759   Parse *pParse,
 84760   AuthContext *pContext, 
 84761   const char *zContext
 84762 ){
 84763   assert( pParse );
 84764   pContext->pParse = pParse;
 84765   pContext->zAuthContext = pParse->zAuthContext;
 84766   pParse->zAuthContext = zContext;
 84769 /*
 84770 ** Pop an authorization context that was previously pushed
 84771 ** by sqlite3AuthContextPush
 84772 */
 84773 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
 84774   if( pContext->pParse ){
 84775     pContext->pParse->zAuthContext = pContext->zAuthContext;
 84776     pContext->pParse = 0;
 84780 #endif /* SQLITE_OMIT_AUTHORIZATION */
 84782 /************** End of auth.c ************************************************/
 84783 /************** Begin file build.c *******************************************/
 84784 /*
 84785 ** 2001 September 15
 84786 **
 84787 ** The author disclaims copyright to this source code.  In place of
 84788 ** a legal notice, here is a blessing:
 84789 **
 84790 **    May you do good and not evil.
 84791 **    May you find forgiveness for yourself and forgive others.
 84792 **    May you share freely, never taking more than you give.
 84793 **
 84794 *************************************************************************
 84795 ** This file contains C code routines that are called by the SQLite parser
 84796 ** when syntax rules are reduced.  The routines in this file handle the
 84797 ** following kinds of SQL syntax:
 84798 **
 84799 **     CREATE TABLE
 84800 **     DROP TABLE
 84801 **     CREATE INDEX
 84802 **     DROP INDEX
 84803 **     creating ID lists
 84804 **     BEGIN TRANSACTION
 84805 **     COMMIT
 84806 **     ROLLBACK
 84807 */
 84809 /*
 84810 ** This routine is called when a new SQL statement is beginning to
 84811 ** be parsed.  Initialize the pParse structure as needed.
 84812 */
 84813 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
 84814   pParse->explain = (u8)explainFlag;
 84815   pParse->nVar = 0;
 84818 #ifndef SQLITE_OMIT_SHARED_CACHE
 84819 /*
 84820 ** The TableLock structure is only used by the sqlite3TableLock() and
 84821 ** codeTableLocks() functions.
 84822 */
 84823 struct TableLock {
 84824   int iDb;             /* The database containing the table to be locked */
 84825   int iTab;            /* The root page of the table to be locked */
 84826   u8 isWriteLock;      /* True for write lock.  False for a read lock */
 84827   const char *zName;   /* Name of the table */
 84828 };
 84830 /*
 84831 ** Record the fact that we want to lock a table at run-time.  
 84832 **
 84833 ** The table to be locked has root page iTab and is found in database iDb.
 84834 ** A read or a write lock can be taken depending on isWritelock.
 84835 **
 84836 ** This routine just records the fact that the lock is desired.  The
 84837 ** code to make the lock occur is generated by a later call to
 84838 ** codeTableLocks() which occurs during sqlite3FinishCoding().
 84839 */
 84840 SQLITE_PRIVATE void sqlite3TableLock(
 84841   Parse *pParse,     /* Parsing context */
 84842   int iDb,           /* Index of the database containing the table to lock */
 84843   int iTab,          /* Root page number of the table to be locked */
 84844   u8 isWriteLock,    /* True for a write lock */
 84845   const char *zName  /* Name of the table to be locked */
 84846 ){
 84847   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 84848   int i;
 84849   int nBytes;
 84850   TableLock *p;
 84851   assert( iDb>=0 );
 84853   for(i=0; i<pToplevel->nTableLock; i++){
 84854     p = &pToplevel->aTableLock[i];
 84855     if( p->iDb==iDb && p->iTab==iTab ){
 84856       p->isWriteLock = (p->isWriteLock || isWriteLock);
 84857       return;
 84861   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
 84862   pToplevel->aTableLock =
 84863       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
 84864   if( pToplevel->aTableLock ){
 84865     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
 84866     p->iDb = iDb;
 84867     p->iTab = iTab;
 84868     p->isWriteLock = isWriteLock;
 84869     p->zName = zName;
 84870   }else{
 84871     pToplevel->nTableLock = 0;
 84872     pToplevel->db->mallocFailed = 1;
 84876 /*
 84877 ** Code an OP_TableLock instruction for each table locked by the
 84878 ** statement (configured by calls to sqlite3TableLock()).
 84879 */
 84880 static void codeTableLocks(Parse *pParse){
 84881   int i;
 84882   Vdbe *pVdbe; 
 84884   pVdbe = sqlite3GetVdbe(pParse);
 84885   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
 84887   for(i=0; i<pParse->nTableLock; i++){
 84888     TableLock *p = &pParse->aTableLock[i];
 84889     int p1 = p->iDb;
 84890     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
 84891                       p->zName, P4_STATIC);
 84894 #else
 84895   #define codeTableLocks(x)
 84896 #endif
 84898 /*
 84899 ** This routine is called after a single SQL statement has been
 84900 ** parsed and a VDBE program to execute that statement has been
 84901 ** prepared.  This routine puts the finishing touches on the
 84902 ** VDBE program and resets the pParse structure for the next
 84903 ** parse.
 84904 **
 84905 ** Note that if an error occurred, it might be the case that
 84906 ** no VDBE code was generated.
 84907 */
 84908 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
 84909   sqlite3 *db;
 84910   Vdbe *v;
 84912   assert( pParse->pToplevel==0 );
 84913   db = pParse->db;
 84914   if( db->mallocFailed ) return;
 84915   if( pParse->nested ) return;
 84916   if( pParse->nErr ) return;
 84918   /* Begin by generating some termination code at the end of the
 84919   ** vdbe program
 84920   */
 84921   v = sqlite3GetVdbe(pParse);
 84922   assert( !pParse->isMultiWrite 
 84923        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
 84924   if( v ){
 84925     while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
 84926     sqlite3VdbeAddOp0(v, OP_Halt);
 84928     /* The cookie mask contains one bit for each database file open.
 84929     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
 84930     ** set for each database that is used.  Generate code to start a
 84931     ** transaction on each used database and to verify the schema cookie
 84932     ** on each used database.
 84933     */
 84934     if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
 84935       yDbMask mask;
 84936       int iDb, i;
 84937       assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
 84938       sqlite3VdbeJumpHere(v, 0);
 84939       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
 84940         if( (mask & pParse->cookieMask)==0 ) continue;
 84941         sqlite3VdbeUsesBtree(v, iDb);
 84942         sqlite3VdbeAddOp4Int(v,
 84943           OP_Transaction,                    /* Opcode */
 84944           iDb,                               /* P1 */
 84945           (mask & pParse->writeMask)!=0,     /* P2 */
 84946           pParse->cookieValue[iDb],          /* P3 */
 84947           db->aDb[iDb].pSchema->iGeneration  /* P4 */
 84948         );
 84949         if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
 84951 #ifndef SQLITE_OMIT_VIRTUALTABLE
 84952       for(i=0; i<pParse->nVtabLock; i++){
 84953         char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
 84954         sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
 84956       pParse->nVtabLock = 0;
 84957 #endif
 84959       /* Once all the cookies have been verified and transactions opened, 
 84960       ** obtain the required table-locks. This is a no-op unless the 
 84961       ** shared-cache feature is enabled.
 84962       */
 84963       codeTableLocks(pParse);
 84965       /* Initialize any AUTOINCREMENT data structures required.
 84966       */
 84967       sqlite3AutoincrementBegin(pParse);
 84969       /* Code constant expressions that where factored out of inner loops */
 84970       if( pParse->pConstExpr ){
 84971         ExprList *pEL = pParse->pConstExpr;
 84972         pParse->okConstFactor = 0;
 84973         for(i=0; i<pEL->nExpr; i++){
 84974           sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
 84978       /* Finally, jump back to the beginning of the executable code. */
 84979       sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
 84984   /* Get the VDBE program ready for execution
 84985   */
 84986   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
 84987     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
 84988     /* A minimum of one cursor is required if autoincrement is used
 84989     *  See ticket [a696379c1f08866] */
 84990     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
 84991     sqlite3VdbeMakeReady(v, pParse);
 84992     pParse->rc = SQLITE_DONE;
 84993     pParse->colNamesSet = 0;
 84994   }else{
 84995     pParse->rc = SQLITE_ERROR;
 84997   pParse->nTab = 0;
 84998   pParse->nMem = 0;
 84999   pParse->nSet = 0;
 85000   pParse->nVar = 0;
 85001   pParse->cookieMask = 0;
 85004 /*
 85005 ** Run the parser and code generator recursively in order to generate
 85006 ** code for the SQL statement given onto the end of the pParse context
 85007 ** currently under construction.  When the parser is run recursively
 85008 ** this way, the final OP_Halt is not appended and other initialization
 85009 ** and finalization steps are omitted because those are handling by the
 85010 ** outermost parser.
 85011 **
 85012 ** Not everything is nestable.  This facility is designed to permit
 85013 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
 85014 ** care if you decide to try to use this routine for some other purposes.
 85015 */
 85016 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
 85017   va_list ap;
 85018   char *zSql;
 85019   char *zErrMsg = 0;
 85020   sqlite3 *db = pParse->db;
 85021 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
 85022   char saveBuf[SAVE_SZ];
 85024   if( pParse->nErr ) return;
 85025   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
 85026   va_start(ap, zFormat);
 85027   zSql = sqlite3VMPrintf(db, zFormat, ap);
 85028   va_end(ap);
 85029   if( zSql==0 ){
 85030     return;   /* A malloc must have failed */
 85032   pParse->nested++;
 85033   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
 85034   memset(&pParse->nVar, 0, SAVE_SZ);
 85035   sqlite3RunParser(pParse, zSql, &zErrMsg);
 85036   sqlite3DbFree(db, zErrMsg);
 85037   sqlite3DbFree(db, zSql);
 85038   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
 85039   pParse->nested--;
 85042 /*
 85043 ** Locate the in-memory structure that describes a particular database
 85044 ** table given the name of that table and (optionally) the name of the
 85045 ** database containing the table.  Return NULL if not found.
 85046 **
 85047 ** If zDatabase is 0, all databases are searched for the table and the
 85048 ** first matching table is returned.  (No checking for duplicate table
 85049 ** names is done.)  The search order is TEMP first, then MAIN, then any
 85050 ** auxiliary databases added using the ATTACH command.
 85051 **
 85052 ** See also sqlite3LocateTable().
 85053 */
 85054 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
 85055   Table *p = 0;
 85056   int i;
 85057   int nName;
 85058   assert( zName!=0 );
 85059   nName = sqlite3Strlen30(zName);
 85060   /* All mutexes are required for schema access.  Make sure we hold them. */
 85061   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 85062   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 85063     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 85064     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
 85065     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 85066     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
 85067     if( p ) break;
 85069   return p;
 85072 /*
 85073 ** Locate the in-memory structure that describes a particular database
 85074 ** table given the name of that table and (optionally) the name of the
 85075 ** database containing the table.  Return NULL if not found.  Also leave an
 85076 ** error message in pParse->zErrMsg.
 85077 **
 85078 ** The difference between this routine and sqlite3FindTable() is that this
 85079 ** routine leaves an error message in pParse->zErrMsg where
 85080 ** sqlite3FindTable() does not.
 85081 */
 85082 SQLITE_PRIVATE Table *sqlite3LocateTable(
 85083   Parse *pParse,         /* context in which to report errors */
 85084   int isView,            /* True if looking for a VIEW rather than a TABLE */
 85085   const char *zName,     /* Name of the table we are looking for */
 85086   const char *zDbase     /* Name of the database.  Might be NULL */
 85087 ){
 85088   Table *p;
 85090   /* Read the database schema. If an error occurs, leave an error message
 85091   ** and code in pParse and return NULL. */
 85092   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 85093     return 0;
 85096   p = sqlite3FindTable(pParse->db, zName, zDbase);
 85097   if( p==0 ){
 85098     const char *zMsg = isView ? "no such view" : "no such table";
 85099     if( zDbase ){
 85100       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
 85101     }else{
 85102       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
 85104     pParse->checkSchema = 1;
 85106   return p;
 85109 /*
 85110 ** Locate the table identified by *p.
 85111 **
 85112 ** This is a wrapper around sqlite3LocateTable(). The difference between
 85113 ** sqlite3LocateTable() and this function is that this function restricts
 85114 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
 85115 ** non-NULL if it is part of a view or trigger program definition. See
 85116 ** sqlite3FixSrcList() for details.
 85117 */
 85118 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
 85119   Parse *pParse, 
 85120   int isView, 
 85121   struct SrcList_item *p
 85122 ){
 85123   const char *zDb;
 85124   assert( p->pSchema==0 || p->zDatabase==0 );
 85125   if( p->pSchema ){
 85126     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
 85127     zDb = pParse->db->aDb[iDb].zName;
 85128   }else{
 85129     zDb = p->zDatabase;
 85131   return sqlite3LocateTable(pParse, isView, p->zName, zDb);
 85134 /*
 85135 ** Locate the in-memory structure that describes 
 85136 ** a particular index given the name of that index
 85137 ** and the name of the database that contains the index.
 85138 ** Return NULL if not found.
 85139 **
 85140 ** If zDatabase is 0, all databases are searched for the
 85141 ** table and the first matching index is returned.  (No checking
 85142 ** for duplicate index names is done.)  The search order is
 85143 ** TEMP first, then MAIN, then any auxiliary databases added
 85144 ** using the ATTACH command.
 85145 */
 85146 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
 85147   Index *p = 0;
 85148   int i;
 85149   int nName = sqlite3Strlen30(zName);
 85150   /* All mutexes are required for schema access.  Make sure we hold them. */
 85151   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 85152   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 85153     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 85154     Schema *pSchema = db->aDb[j].pSchema;
 85155     assert( pSchema );
 85156     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
 85157     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 85158     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
 85159     if( p ) break;
 85161   return p;
 85164 /*
 85165 ** Reclaim the memory used by an index
 85166 */
 85167 static void freeIndex(sqlite3 *db, Index *p){
 85168 #ifndef SQLITE_OMIT_ANALYZE
 85169   sqlite3DeleteIndexSamples(db, p);
 85170 #endif
 85171   if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo);
 85172   sqlite3ExprDelete(db, p->pPartIdxWhere);
 85173   sqlite3DbFree(db, p->zColAff);
 85174   if( p->isResized ) sqlite3DbFree(db, p->azColl);
 85175   sqlite3DbFree(db, p);
 85178 /*
 85179 ** For the index called zIdxName which is found in the database iDb,
 85180 ** unlike that index from its Table then remove the index from
 85181 ** the index hash table and free all memory structures associated
 85182 ** with the index.
 85183 */
 85184 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
 85185   Index *pIndex;
 85186   int len;
 85187   Hash *pHash;
 85189   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 85190   pHash = &db->aDb[iDb].pSchema->idxHash;
 85191   len = sqlite3Strlen30(zIdxName);
 85192   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
 85193   if( ALWAYS(pIndex) ){
 85194     if( pIndex->pTable->pIndex==pIndex ){
 85195       pIndex->pTable->pIndex = pIndex->pNext;
 85196     }else{
 85197       Index *p;
 85198       /* Justification of ALWAYS();  The index must be on the list of
 85199       ** indices. */
 85200       p = pIndex->pTable->pIndex;
 85201       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
 85202       if( ALWAYS(p && p->pNext==pIndex) ){
 85203         p->pNext = pIndex->pNext;
 85206     freeIndex(db, pIndex);
 85208   db->flags |= SQLITE_InternChanges;
 85211 /*
 85212 ** Look through the list of open database files in db->aDb[] and if
 85213 ** any have been closed, remove them from the list.  Reallocate the
 85214 ** db->aDb[] structure to a smaller size, if possible.
 85215 **
 85216 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
 85217 ** are never candidates for being collapsed.
 85218 */
 85219 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
 85220   int i, j;
 85221   for(i=j=2; i<db->nDb; i++){
 85222     struct Db *pDb = &db->aDb[i];
 85223     if( pDb->pBt==0 ){
 85224       sqlite3DbFree(db, pDb->zName);
 85225       pDb->zName = 0;
 85226       continue;
 85228     if( j<i ){
 85229       db->aDb[j] = db->aDb[i];
 85231     j++;
 85233   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
 85234   db->nDb = j;
 85235   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
 85236     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
 85237     sqlite3DbFree(db, db->aDb);
 85238     db->aDb = db->aDbStatic;
 85242 /*
 85243 ** Reset the schema for the database at index iDb.  Also reset the
 85244 ** TEMP schema.
 85245 */
 85246 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
 85247   Db *pDb;
 85248   assert( iDb<db->nDb );
 85250   /* Case 1:  Reset the single schema identified by iDb */
 85251   pDb = &db->aDb[iDb];
 85252   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 85253   assert( pDb->pSchema!=0 );
 85254   sqlite3SchemaClear(pDb->pSchema);
 85256   /* If any database other than TEMP is reset, then also reset TEMP
 85257   ** since TEMP might be holding triggers that reference tables in the
 85258   ** other database.
 85259   */
 85260   if( iDb!=1 ){
 85261     pDb = &db->aDb[1];
 85262     assert( pDb->pSchema!=0 );
 85263     sqlite3SchemaClear(pDb->pSchema);
 85265   return;
 85268 /*
 85269 ** Erase all schema information from all attached databases (including
 85270 ** "main" and "temp") for a single database connection.
 85271 */
 85272 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
 85273   int i;
 85274   sqlite3BtreeEnterAll(db);
 85275   for(i=0; i<db->nDb; i++){
 85276     Db *pDb = &db->aDb[i];
 85277     if( pDb->pSchema ){
 85278       sqlite3SchemaClear(pDb->pSchema);
 85281   db->flags &= ~SQLITE_InternChanges;
 85282   sqlite3VtabUnlockList(db);
 85283   sqlite3BtreeLeaveAll(db);
 85284   sqlite3CollapseDatabaseArray(db);
 85287 /*
 85288 ** This routine is called when a commit occurs.
 85289 */
 85290 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
 85291   db->flags &= ~SQLITE_InternChanges;
 85294 /*
 85295 ** Delete memory allocated for the column names of a table or view (the
 85296 ** Table.aCol[] array).
 85297 */
 85298 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
 85299   int i;
 85300   Column *pCol;
 85301   assert( pTable!=0 );
 85302   if( (pCol = pTable->aCol)!=0 ){
 85303     for(i=0; i<pTable->nCol; i++, pCol++){
 85304       sqlite3DbFree(db, pCol->zName);
 85305       sqlite3ExprDelete(db, pCol->pDflt);
 85306       sqlite3DbFree(db, pCol->zDflt);
 85307       sqlite3DbFree(db, pCol->zType);
 85308       sqlite3DbFree(db, pCol->zColl);
 85310     sqlite3DbFree(db, pTable->aCol);
 85314 /*
 85315 ** Remove the memory data structures associated with the given
 85316 ** Table.  No changes are made to disk by this routine.
 85317 **
 85318 ** This routine just deletes the data structure.  It does not unlink
 85319 ** the table data structure from the hash table.  But it does destroy
 85320 ** memory structures of the indices and foreign keys associated with 
 85321 ** the table.
 85322 **
 85323 ** The db parameter is optional.  It is needed if the Table object 
 85324 ** contains lookaside memory.  (Table objects in the schema do not use
 85325 ** lookaside memory, but some ephemeral Table objects do.)  Or the
 85326 ** db parameter can be used with db->pnBytesFreed to measure the memory
 85327 ** used by the Table object.
 85328 */
 85329 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
 85330   Index *pIndex, *pNext;
 85331   TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
 85333   assert( !pTable || pTable->nRef>0 );
 85335   /* Do not delete the table until the reference count reaches zero. */
 85336   if( !pTable ) return;
 85337   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
 85339   /* Record the number of outstanding lookaside allocations in schema Tables
 85340   ** prior to doing any free() operations.  Since schema Tables do not use
 85341   ** lookaside, this number should not change. */
 85342   TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
 85343                          db->lookaside.nOut : 0 );
 85345   /* Delete all indices associated with this table. */
 85346   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
 85347     pNext = pIndex->pNext;
 85348     assert( pIndex->pSchema==pTable->pSchema );
 85349     if( !db || db->pnBytesFreed==0 ){
 85350       char *zName = pIndex->zName; 
 85351       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 85352          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 85353       );
 85354       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 85355       assert( pOld==pIndex || pOld==0 );
 85357     freeIndex(db, pIndex);
 85360   /* Delete any foreign keys attached to this table. */
 85361   sqlite3FkDelete(db, pTable);
 85363   /* Delete the Table structure itself.
 85364   */
 85365   sqliteDeleteColumnNames(db, pTable);
 85366   sqlite3DbFree(db, pTable->zName);
 85367   sqlite3DbFree(db, pTable->zColAff);
 85368   sqlite3SelectDelete(db, pTable->pSelect);
 85369 #ifndef SQLITE_OMIT_CHECK
 85370   sqlite3ExprListDelete(db, pTable->pCheck);
 85371 #endif
 85372 #ifndef SQLITE_OMIT_VIRTUALTABLE
 85373   sqlite3VtabClear(db, pTable);
 85374 #endif
 85375   sqlite3DbFree(db, pTable);
 85377   /* Verify that no lookaside memory was used by schema tables */
 85378   assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
 85381 /*
 85382 ** Unlink the given table from the hash tables and the delete the
 85383 ** table structure with all its indices and foreign keys.
 85384 */
 85385 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
 85386   Table *p;
 85387   Db *pDb;
 85389   assert( db!=0 );
 85390   assert( iDb>=0 && iDb<db->nDb );
 85391   assert( zTabName );
 85392   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 85393   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
 85394   pDb = &db->aDb[iDb];
 85395   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
 85396                         sqlite3Strlen30(zTabName),0);
 85397   sqlite3DeleteTable(db, p);
 85398   db->flags |= SQLITE_InternChanges;
 85401 /*
 85402 ** Given a token, return a string that consists of the text of that
 85403 ** token.  Space to hold the returned string
 85404 ** is obtained from sqliteMalloc() and must be freed by the calling
 85405 ** function.
 85406 **
 85407 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
 85408 ** surround the body of the token are removed.
 85409 **
 85410 ** Tokens are often just pointers into the original SQL text and so
 85411 ** are not \000 terminated and are not persistent.  The returned string
 85412 ** is \000 terminated and is persistent.
 85413 */
 85414 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
 85415   char *zName;
 85416   if( pName ){
 85417     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
 85418     sqlite3Dequote(zName);
 85419   }else{
 85420     zName = 0;
 85422   return zName;
 85425 /*
 85426 ** Open the sqlite_master table stored in database number iDb for
 85427 ** writing. The table is opened using cursor 0.
 85428 */
 85429 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
 85430   Vdbe *v = sqlite3GetVdbe(p);
 85431   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
 85432   sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
 85433   if( p->nTab==0 ){
 85434     p->nTab = 1;
 85438 /*
 85439 ** Parameter zName points to a nul-terminated buffer containing the name
 85440 ** of a database ("main", "temp" or the name of an attached db). This
 85441 ** function returns the index of the named database in db->aDb[], or
 85442 ** -1 if the named db cannot be found.
 85443 */
 85444 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
 85445   int i = -1;         /* Database number */
 85446   if( zName ){
 85447     Db *pDb;
 85448     int n = sqlite3Strlen30(zName);
 85449     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
 85450       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
 85451           0==sqlite3StrICmp(pDb->zName, zName) ){
 85452         break;
 85456   return i;
 85459 /*
 85460 ** The token *pName contains the name of a database (either "main" or
 85461 ** "temp" or the name of an attached db). This routine returns the
 85462 ** index of the named database in db->aDb[], or -1 if the named db 
 85463 ** does not exist.
 85464 */
 85465 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
 85466   int i;                               /* Database number */
 85467   char *zName;                         /* Name we are searching for */
 85468   zName = sqlite3NameFromToken(db, pName);
 85469   i = sqlite3FindDbName(db, zName);
 85470   sqlite3DbFree(db, zName);
 85471   return i;
 85474 /* The table or view or trigger name is passed to this routine via tokens
 85475 ** pName1 and pName2. If the table name was fully qualified, for example:
 85476 **
 85477 ** CREATE TABLE xxx.yyy (...);
 85478 ** 
 85479 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 85480 ** the table name is not fully qualified, i.e.:
 85481 **
 85482 ** CREATE TABLE yyy(...);
 85483 **
 85484 ** Then pName1 is set to "yyy" and pName2 is "".
 85485 **
 85486 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
 85487 ** pName2) that stores the unqualified table name.  The index of the
 85488 ** database "xxx" is returned.
 85489 */
 85490 SQLITE_PRIVATE int sqlite3TwoPartName(
 85491   Parse *pParse,      /* Parsing and code generating context */
 85492   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
 85493   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
 85494   Token **pUnqual     /* Write the unqualified object name here */
 85495 ){
 85496   int iDb;                    /* Database holding the object */
 85497   sqlite3 *db = pParse->db;
 85499   if( ALWAYS(pName2!=0) && pName2->n>0 ){
 85500     if( db->init.busy ) {
 85501       sqlite3ErrorMsg(pParse, "corrupt database");
 85502       pParse->nErr++;
 85503       return -1;
 85505     *pUnqual = pName2;
 85506     iDb = sqlite3FindDb(db, pName1);
 85507     if( iDb<0 ){
 85508       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
 85509       pParse->nErr++;
 85510       return -1;
 85512   }else{
 85513     assert( db->init.iDb==0 || db->init.busy );
 85514     iDb = db->init.iDb;
 85515     *pUnqual = pName1;
 85517   return iDb;
 85520 /*
 85521 ** This routine is used to check if the UTF-8 string zName is a legal
 85522 ** unqualified name for a new schema object (table, index, view or
 85523 ** trigger). All names are legal except those that begin with the string
 85524 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
 85525 ** is reserved for internal use.
 85526 */
 85527 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
 85528   if( !pParse->db->init.busy && pParse->nested==0 
 85529           && (pParse->db->flags & SQLITE_WriteSchema)==0
 85530           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
 85531     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
 85532     return SQLITE_ERROR;
 85534   return SQLITE_OK;
 85537 /*
 85538 ** Return the PRIMARY KEY index of a table
 85539 */
 85540 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
 85541   Index *p;
 85542   for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
 85543   return p;
 85546 /*
 85547 ** Return the column of index pIdx that corresponds to table
 85548 ** column iCol.  Return -1 if not found.
 85549 */
 85550 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
 85551   int i;
 85552   for(i=0; i<pIdx->nColumn; i++){
 85553     if( iCol==pIdx->aiColumn[i] ) return i;
 85555   return -1;
 85558 /*
 85559 ** Begin constructing a new table representation in memory.  This is
 85560 ** the first of several action routines that get called in response
 85561 ** to a CREATE TABLE statement.  In particular, this routine is called
 85562 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
 85563 ** flag is true if the table should be stored in the auxiliary database
 85564 ** file instead of in the main database file.  This is normally the case
 85565 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
 85566 ** CREATE and TABLE.
 85567 **
 85568 ** The new table record is initialized and put in pParse->pNewTable.
 85569 ** As more of the CREATE TABLE statement is parsed, additional action
 85570 ** routines will be called to add more information to this record.
 85571 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
 85572 ** is called to complete the construction of the new table record.
 85573 */
 85574 SQLITE_PRIVATE void sqlite3StartTable(
 85575   Parse *pParse,   /* Parser context */
 85576   Token *pName1,   /* First part of the name of the table or view */
 85577   Token *pName2,   /* Second part of the name of the table or view */
 85578   int isTemp,      /* True if this is a TEMP table */
 85579   int isView,      /* True if this is a VIEW */
 85580   int isVirtual,   /* True if this is a VIRTUAL table */
 85581   int noErr        /* Do nothing if table already exists */
 85582 ){
 85583   Table *pTable;
 85584   char *zName = 0; /* The name of the new table */
 85585   sqlite3 *db = pParse->db;
 85586   Vdbe *v;
 85587   int iDb;         /* Database number to create the table in */
 85588   Token *pName;    /* Unqualified name of the table to create */
 85590   /* The table or view name to create is passed to this routine via tokens
 85591   ** pName1 and pName2. If the table name was fully qualified, for example:
 85592   **
 85593   ** CREATE TABLE xxx.yyy (...);
 85594   ** 
 85595   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
 85596   ** the table name is not fully qualified, i.e.:
 85597   **
 85598   ** CREATE TABLE yyy(...);
 85599   **
 85600   ** Then pName1 is set to "yyy" and pName2 is "".
 85601   **
 85602   ** The call below sets the pName pointer to point at the token (pName1 or
 85603   ** pName2) that stores the unqualified table name. The variable iDb is
 85604   ** set to the index of the database that the table or view is to be
 85605   ** created in.
 85606   */
 85607   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 85608   if( iDb<0 ) return;
 85609   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
 85610     /* If creating a temp table, the name may not be qualified. Unless 
 85611     ** the database name is "temp" anyway.  */
 85612     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
 85613     return;
 85615   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
 85617   pParse->sNameToken = *pName;
 85618   zName = sqlite3NameFromToken(db, pName);
 85619   if( zName==0 ) return;
 85620   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 85621     goto begin_table_error;
 85623   if( db->init.iDb==1 ) isTemp = 1;
 85624 #ifndef SQLITE_OMIT_AUTHORIZATION
 85625   assert( (isTemp & 1)==isTemp );
 85627     int code;
 85628     char *zDb = db->aDb[iDb].zName;
 85629     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
 85630       goto begin_table_error;
 85632     if( isView ){
 85633       if( !OMIT_TEMPDB && isTemp ){
 85634         code = SQLITE_CREATE_TEMP_VIEW;
 85635       }else{
 85636         code = SQLITE_CREATE_VIEW;
 85638     }else{
 85639       if( !OMIT_TEMPDB && isTemp ){
 85640         code = SQLITE_CREATE_TEMP_TABLE;
 85641       }else{
 85642         code = SQLITE_CREATE_TABLE;
 85645     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
 85646       goto begin_table_error;
 85649 #endif
 85651   /* Make sure the new table name does not collide with an existing
 85652   ** index or table name in the same database.  Issue an error message if
 85653   ** it does. The exception is if the statement being parsed was passed
 85654   ** to an sqlite3_declare_vtab() call. In that case only the column names
 85655   ** and types will be used, so there is no need to test for namespace
 85656   ** collisions.
 85657   */
 85658   if( !IN_DECLARE_VTAB ){
 85659     char *zDb = db->aDb[iDb].zName;
 85660     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 85661       goto begin_table_error;
 85663     pTable = sqlite3FindTable(db, zName, zDb);
 85664     if( pTable ){
 85665       if( !noErr ){
 85666         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
 85667       }else{
 85668         assert( !db->init.busy );
 85669         sqlite3CodeVerifySchema(pParse, iDb);
 85671       goto begin_table_error;
 85673     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
 85674       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
 85675       goto begin_table_error;
 85679   pTable = sqlite3DbMallocZero(db, sizeof(Table));
 85680   if( pTable==0 ){
 85681     db->mallocFailed = 1;
 85682     pParse->rc = SQLITE_NOMEM;
 85683     pParse->nErr++;
 85684     goto begin_table_error;
 85686   pTable->zName = zName;
 85687   pTable->iPKey = -1;
 85688   pTable->pSchema = db->aDb[iDb].pSchema;
 85689   pTable->nRef = 1;
 85690   pTable->nRowEst = 1048576;
 85691   assert( pParse->pNewTable==0 );
 85692   pParse->pNewTable = pTable;
 85694   /* If this is the magic sqlite_sequence table used by autoincrement,
 85695   ** then record a pointer to this table in the main database structure
 85696   ** so that INSERT can find the table easily.
 85697   */
 85698 #ifndef SQLITE_OMIT_AUTOINCREMENT
 85699   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
 85700     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 85701     pTable->pSchema->pSeqTab = pTable;
 85703 #endif
 85705   /* Begin generating the code that will insert the table record into
 85706   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
 85707   ** and allocate the record number for the table entry now.  Before any
 85708   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
 85709   ** indices to be created and the table record must come before the 
 85710   ** indices.  Hence, the record number for the table must be allocated
 85711   ** now.
 85712   */
 85713   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
 85714     int j1;
 85715     int fileFormat;
 85716     int reg1, reg2, reg3;
 85717     sqlite3BeginWriteOperation(pParse, 0, iDb);
 85719 #ifndef SQLITE_OMIT_VIRTUALTABLE
 85720     if( isVirtual ){
 85721       sqlite3VdbeAddOp0(v, OP_VBegin);
 85723 #endif
 85725     /* If the file format and encoding in the database have not been set, 
 85726     ** set them now.
 85727     */
 85728     reg1 = pParse->regRowid = ++pParse->nMem;
 85729     reg2 = pParse->regRoot = ++pParse->nMem;
 85730     reg3 = ++pParse->nMem;
 85731     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
 85732     sqlite3VdbeUsesBtree(v, iDb);
 85733     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
 85734     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
 85735                   1 : SQLITE_MAX_FILE_FORMAT;
 85736     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
 85737     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
 85738     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
 85739     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
 85740     sqlite3VdbeJumpHere(v, j1);
 85742     /* This just creates a place-holder record in the sqlite_master table.
 85743     ** The record created does not contain anything yet.  It will be replaced
 85744     ** by the real entry in code generated at sqlite3EndTable().
 85745     **
 85746     ** The rowid for the new entry is left in register pParse->regRowid.
 85747     ** The root page number of the new table is left in reg pParse->regRoot.
 85748     ** The rowid and root page number values are needed by the code that
 85749     ** sqlite3EndTable will generate.
 85750     */
 85751 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 85752     if( isView || isVirtual ){
 85753       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
 85754     }else
 85755 #endif
 85757       pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
 85759     sqlite3OpenMasterTable(pParse, iDb);
 85760     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
 85761     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
 85762     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
 85763     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 85764     sqlite3VdbeAddOp0(v, OP_Close);
 85767   /* Normal (non-error) return. */
 85768   return;
 85770   /* If an error occurs, we jump here */
 85771 begin_table_error:
 85772   sqlite3DbFree(db, zName);
 85773   return;
 85776 /*
 85777 ** This macro is used to compare two strings in a case-insensitive manner.
 85778 ** It is slightly faster than calling sqlite3StrICmp() directly, but
 85779 ** produces larger code.
 85780 **
 85781 ** WARNING: This macro is not compatible with the strcmp() family. It
 85782 ** returns true if the two strings are equal, otherwise false.
 85783 */
 85784 #define STRICMP(x, y) (\
 85785 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
 85786 sqlite3UpperToLower[*(unsigned char *)(y)]     \
 85787 && sqlite3StrICmp((x)+1,(y)+1)==0 )
 85789 /*
 85790 ** Add a new column to the table currently being constructed.
 85791 **
 85792 ** The parser calls this routine once for each column declaration
 85793 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
 85794 ** first to get things going.  Then this routine is called for each
 85795 ** column.
 85796 */
 85797 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
 85798   Table *p;
 85799   int i;
 85800   char *z;
 85801   Column *pCol;
 85802   sqlite3 *db = pParse->db;
 85803   if( (p = pParse->pNewTable)==0 ) return;
 85804 #if SQLITE_MAX_COLUMN
 85805   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 85806     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
 85807     return;
 85809 #endif
 85810   z = sqlite3NameFromToken(db, pName);
 85811   if( z==0 ) return;
 85812   for(i=0; i<p->nCol; i++){
 85813     if( STRICMP(z, p->aCol[i].zName) ){
 85814       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
 85815       sqlite3DbFree(db, z);
 85816       return;
 85819   if( (p->nCol & 0x7)==0 ){
 85820     Column *aNew;
 85821     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
 85822     if( aNew==0 ){
 85823       sqlite3DbFree(db, z);
 85824       return;
 85826     p->aCol = aNew;
 85828   pCol = &p->aCol[p->nCol];
 85829   memset(pCol, 0, sizeof(p->aCol[0]));
 85830   pCol->zName = z;
 85832   /* If there is no type specified, columns have the default affinity
 85833   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
 85834   ** be called next to set pCol->affinity correctly.
 85835   */
 85836   pCol->affinity = SQLITE_AFF_NONE;
 85837   pCol->szEst = 1;
 85838   p->nCol++;
 85841 /*
 85842 ** This routine is called by the parser while in the middle of
 85843 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
 85844 ** been seen on a column.  This routine sets the notNull flag on
 85845 ** the column currently under construction.
 85846 */
 85847 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
 85848   Table *p;
 85849   p = pParse->pNewTable;
 85850   if( p==0 || NEVER(p->nCol<1) ) return;
 85851   p->aCol[p->nCol-1].notNull = (u8)onError;
 85854 /*
 85855 ** Scan the column type name zType (length nType) and return the
 85856 ** associated affinity type.
 85857 **
 85858 ** This routine does a case-independent search of zType for the 
 85859 ** substrings in the following table. If one of the substrings is
 85860 ** found, the corresponding affinity is returned. If zType contains
 85861 ** more than one of the substrings, entries toward the top of 
 85862 ** the table take priority. For example, if zType is 'BLOBINT', 
 85863 ** SQLITE_AFF_INTEGER is returned.
 85864 **
 85865 ** Substring     | Affinity
 85866 ** --------------------------------
 85867 ** 'INT'         | SQLITE_AFF_INTEGER
 85868 ** 'CHAR'        | SQLITE_AFF_TEXT
 85869 ** 'CLOB'        | SQLITE_AFF_TEXT
 85870 ** 'TEXT'        | SQLITE_AFF_TEXT
 85871 ** 'BLOB'        | SQLITE_AFF_NONE
 85872 ** 'REAL'        | SQLITE_AFF_REAL
 85873 ** 'FLOA'        | SQLITE_AFF_REAL
 85874 ** 'DOUB'        | SQLITE_AFF_REAL
 85875 **
 85876 ** If none of the substrings in the above table are found,
 85877 ** SQLITE_AFF_NUMERIC is returned.
 85878 */
 85879 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
 85880   u32 h = 0;
 85881   char aff = SQLITE_AFF_NUMERIC;
 85882   const char *zChar = 0;
 85884   if( zIn==0 ) return aff;
 85885   while( zIn[0] ){
 85886     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
 85887     zIn++;
 85888     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
 85889       aff = SQLITE_AFF_TEXT;
 85890       zChar = zIn;
 85891     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
 85892       aff = SQLITE_AFF_TEXT;
 85893     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
 85894       aff = SQLITE_AFF_TEXT;
 85895     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
 85896         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
 85897       aff = SQLITE_AFF_NONE;
 85898       if( zIn[0]=='(' ) zChar = zIn;
 85899 #ifndef SQLITE_OMIT_FLOATING_POINT
 85900     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
 85901         && aff==SQLITE_AFF_NUMERIC ){
 85902       aff = SQLITE_AFF_REAL;
 85903     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
 85904         && aff==SQLITE_AFF_NUMERIC ){
 85905       aff = SQLITE_AFF_REAL;
 85906     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
 85907         && aff==SQLITE_AFF_NUMERIC ){
 85908       aff = SQLITE_AFF_REAL;
 85909 #endif
 85910     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
 85911       aff = SQLITE_AFF_INTEGER;
 85912       break;
 85916   /* If pszEst is not NULL, store an estimate of the field size.  The
 85917   ** estimate is scaled so that the size of an integer is 1.  */
 85918   if( pszEst ){
 85919     *pszEst = 1;   /* default size is approx 4 bytes */
 85920     if( aff<=SQLITE_AFF_NONE ){
 85921       if( zChar ){
 85922         while( zChar[0] ){
 85923           if( sqlite3Isdigit(zChar[0]) ){
 85924             int v = 0;
 85925             sqlite3GetInt32(zChar, &v);
 85926             v = v/4 + 1;
 85927             if( v>255 ) v = 255;
 85928             *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
 85929             break;
 85931           zChar++;
 85933       }else{
 85934         *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
 85938   return aff;
 85941 /*
 85942 ** This routine is called by the parser while in the middle of
 85943 ** parsing a CREATE TABLE statement.  The pFirst token is the first
 85944 ** token in the sequence of tokens that describe the type of the
 85945 ** column currently under construction.   pLast is the last token
 85946 ** in the sequence.  Use this information to construct a string
 85947 ** that contains the typename of the column and store that string
 85948 ** in zType.
 85949 */ 
 85950 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
 85951   Table *p;
 85952   Column *pCol;
 85954   p = pParse->pNewTable;
 85955   if( p==0 || NEVER(p->nCol<1) ) return;
 85956   pCol = &p->aCol[p->nCol-1];
 85957   assert( pCol->zType==0 );
 85958   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
 85959   pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
 85962 /*
 85963 ** The expression is the default value for the most recently added column
 85964 ** of the table currently under construction.
 85965 **
 85966 ** Default value expressions must be constant.  Raise an exception if this
 85967 ** is not the case.
 85968 **
 85969 ** This routine is called by the parser while in the middle of
 85970 ** parsing a CREATE TABLE statement.
 85971 */
 85972 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
 85973   Table *p;
 85974   Column *pCol;
 85975   sqlite3 *db = pParse->db;
 85976   p = pParse->pNewTable;
 85977   if( p!=0 ){
 85978     pCol = &(p->aCol[p->nCol-1]);
 85979     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
 85980       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
 85981           pCol->zName);
 85982     }else{
 85983       /* A copy of pExpr is used instead of the original, as pExpr contains
 85984       ** tokens that point to volatile memory. The 'span' of the expression
 85985       ** is required by pragma table_info.
 85986       */
 85987       sqlite3ExprDelete(db, pCol->pDflt);
 85988       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
 85989       sqlite3DbFree(db, pCol->zDflt);
 85990       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
 85991                                      (int)(pSpan->zEnd - pSpan->zStart));
 85994   sqlite3ExprDelete(db, pSpan->pExpr);
 85997 /*
 85998 ** Designate the PRIMARY KEY for the table.  pList is a list of names 
 85999 ** of columns that form the primary key.  If pList is NULL, then the
 86000 ** most recently added column of the table is the primary key.
 86001 **
 86002 ** A table can have at most one primary key.  If the table already has
 86003 ** a primary key (and this is the second primary key) then create an
 86004 ** error.
 86005 **
 86006 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
 86007 ** then we will try to use that column as the rowid.  Set the Table.iPKey
 86008 ** field of the table under construction to be the index of the
 86009 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
 86010 ** no INTEGER PRIMARY KEY.
 86011 **
 86012 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
 86013 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
 86014 */
 86015 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
 86016   Parse *pParse,    /* Parsing context */
 86017   ExprList *pList,  /* List of field names to be indexed */
 86018   int onError,      /* What to do with a uniqueness conflict */
 86019   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
 86020   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
 86021 ){
 86022   Table *pTab = pParse->pNewTable;
 86023   char *zType = 0;
 86024   int iCol = -1, i;
 86025   int nTerm;
 86026   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
 86027   if( pTab->tabFlags & TF_HasPrimaryKey ){
 86028     sqlite3ErrorMsg(pParse, 
 86029       "table \"%s\" has more than one primary key", pTab->zName);
 86030     goto primary_key_exit;
 86032   pTab->tabFlags |= TF_HasPrimaryKey;
 86033   if( pList==0 ){
 86034     iCol = pTab->nCol - 1;
 86035     pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 86036     zType = pTab->aCol[iCol].zType;
 86037     nTerm = 1;
 86038   }else{
 86039     nTerm = pList->nExpr;
 86040     for(i=0; i<nTerm; i++){
 86041       for(iCol=0; iCol<pTab->nCol; iCol++){
 86042         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
 86043           pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
 86044           zType = pTab->aCol[iCol].zType;
 86045           break;
 86050   if( nTerm==1
 86051    && zType && sqlite3StrICmp(zType, "INTEGER")==0
 86052    && sortOrder==SQLITE_SO_ASC
 86053   ){
 86054     pTab->iPKey = iCol;
 86055     pTab->keyConf = (u8)onError;
 86056     assert( autoInc==0 || autoInc==1 );
 86057     pTab->tabFlags |= autoInc*TF_Autoincrement;
 86058     if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
 86059   }else if( autoInc ){
 86060 #ifndef SQLITE_OMIT_AUTOINCREMENT
 86061     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
 86062        "INTEGER PRIMARY KEY");
 86063 #endif
 86064   }else{
 86065     Vdbe *v = pParse->pVdbe;
 86066     Index *p;
 86067     if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
 86068     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
 86069                            0, sortOrder, 0);
 86070     if( p ){
 86071       p->autoIndex = 2;
 86072       if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
 86074     pList = 0;
 86077 primary_key_exit:
 86078   sqlite3ExprListDelete(pParse->db, pList);
 86079   return;
 86082 /*
 86083 ** Add a new CHECK constraint to the table currently under construction.
 86084 */
 86085 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
 86086   Parse *pParse,    /* Parsing context */
 86087   Expr *pCheckExpr  /* The check expression */
 86088 ){
 86089 #ifndef SQLITE_OMIT_CHECK
 86090   Table *pTab = pParse->pNewTable;
 86091   if( pTab && !IN_DECLARE_VTAB ){
 86092     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
 86093     if( pParse->constraintName.n ){
 86094       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
 86096   }else
 86097 #endif
 86099     sqlite3ExprDelete(pParse->db, pCheckExpr);
 86103 /*
 86104 ** Set the collation function of the most recently parsed table column
 86105 ** to the CollSeq given.
 86106 */
 86107 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
 86108   Table *p;
 86109   int i;
 86110   char *zColl;              /* Dequoted name of collation sequence */
 86111   sqlite3 *db;
 86113   if( (p = pParse->pNewTable)==0 ) return;
 86114   i = p->nCol-1;
 86115   db = pParse->db;
 86116   zColl = sqlite3NameFromToken(db, pToken);
 86117   if( !zColl ) return;
 86119   if( sqlite3LocateCollSeq(pParse, zColl) ){
 86120     Index *pIdx;
 86121     sqlite3DbFree(db, p->aCol[i].zColl);
 86122     p->aCol[i].zColl = zColl;
 86124     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
 86125     ** then an index may have been created on this column before the
 86126     ** collation type was added. Correct this if it is the case.
 86127     */
 86128     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
 86129       assert( pIdx->nKeyCol==1 );
 86130       if( pIdx->aiColumn[0]==i ){
 86131         pIdx->azColl[0] = p->aCol[i].zColl;
 86134   }else{
 86135     sqlite3DbFree(db, zColl);
 86139 /*
 86140 ** This function returns the collation sequence for database native text
 86141 ** encoding identified by the string zName, length nName.
 86142 **
 86143 ** If the requested collation sequence is not available, or not available
 86144 ** in the database native encoding, the collation factory is invoked to
 86145 ** request it. If the collation factory does not supply such a sequence,
 86146 ** and the sequence is available in another text encoding, then that is
 86147 ** returned instead.
 86148 **
 86149 ** If no versions of the requested collations sequence are available, or
 86150 ** another error occurs, NULL is returned and an error message written into
 86151 ** pParse.
 86152 **
 86153 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
 86154 ** invokes the collation factory if the named collation cannot be found
 86155 ** and generates an error message.
 86156 **
 86157 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
 86158 */
 86159 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
 86160   sqlite3 *db = pParse->db;
 86161   u8 enc = ENC(db);
 86162   u8 initbusy = db->init.busy;
 86163   CollSeq *pColl;
 86165   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
 86166   if( !initbusy && (!pColl || !pColl->xCmp) ){
 86167     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
 86170   return pColl;
 86174 /*
 86175 ** Generate code that will increment the schema cookie.
 86176 **
 86177 ** The schema cookie is used to determine when the schema for the
 86178 ** database changes.  After each schema change, the cookie value
 86179 ** changes.  When a process first reads the schema it records the
 86180 ** cookie.  Thereafter, whenever it goes to access the database,
 86181 ** it checks the cookie to make sure the schema has not changed
 86182 ** since it was last read.
 86183 **
 86184 ** This plan is not completely bullet-proof.  It is possible for
 86185 ** the schema to change multiple times and for the cookie to be
 86186 ** set back to prior value.  But schema changes are infrequent
 86187 ** and the probability of hitting the same cookie value is only
 86188 ** 1 chance in 2^32.  So we're safe enough.
 86189 */
 86190 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 86191   int r1 = sqlite3GetTempReg(pParse);
 86192   sqlite3 *db = pParse->db;
 86193   Vdbe *v = pParse->pVdbe;
 86194   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86195   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
 86196   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
 86197   sqlite3ReleaseTempReg(pParse, r1);
 86200 /*
 86201 ** Measure the number of characters needed to output the given
 86202 ** identifier.  The number returned includes any quotes used
 86203 ** but does not include the null terminator.
 86204 **
 86205 ** The estimate is conservative.  It might be larger that what is
 86206 ** really needed.
 86207 */
 86208 static int identLength(const char *z){
 86209   int n;
 86210   for(n=0; *z; n++, z++){
 86211     if( *z=='"' ){ n++; }
 86213   return n + 2;
 86216 /*
 86217 ** The first parameter is a pointer to an output buffer. The second 
 86218 ** parameter is a pointer to an integer that contains the offset at
 86219 ** which to write into the output buffer. This function copies the
 86220 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
 86221 ** to the specified offset in the buffer and updates *pIdx to refer
 86222 ** to the first byte after the last byte written before returning.
 86223 ** 
 86224 ** If the string zSignedIdent consists entirely of alpha-numeric
 86225 ** characters, does not begin with a digit and is not an SQL keyword,
 86226 ** then it is copied to the output buffer exactly as it is. Otherwise,
 86227 ** it is quoted using double-quotes.
 86228 */
 86229 static void identPut(char *z, int *pIdx, char *zSignedIdent){
 86230   unsigned char *zIdent = (unsigned char*)zSignedIdent;
 86231   int i, j, needQuote;
 86232   i = *pIdx;
 86234   for(j=0; zIdent[j]; j++){
 86235     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
 86237   needQuote = sqlite3Isdigit(zIdent[0])
 86238             || sqlite3KeywordCode(zIdent, j)!=TK_ID
 86239             || zIdent[j]!=0
 86240             || j==0;
 86242   if( needQuote ) z[i++] = '"';
 86243   for(j=0; zIdent[j]; j++){
 86244     z[i++] = zIdent[j];
 86245     if( zIdent[j]=='"' ) z[i++] = '"';
 86247   if( needQuote ) z[i++] = '"';
 86248   z[i] = 0;
 86249   *pIdx = i;
 86252 /*
 86253 ** Generate a CREATE TABLE statement appropriate for the given
 86254 ** table.  Memory to hold the text of the statement is obtained
 86255 ** from sqliteMalloc() and must be freed by the calling function.
 86256 */
 86257 static char *createTableStmt(sqlite3 *db, Table *p){
 86258   int i, k, n;
 86259   char *zStmt;
 86260   char *zSep, *zSep2, *zEnd;
 86261   Column *pCol;
 86262   n = 0;
 86263   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
 86264     n += identLength(pCol->zName) + 5;
 86266   n += identLength(p->zName);
 86267   if( n<50 ){ 
 86268     zSep = "";
 86269     zSep2 = ",";
 86270     zEnd = ")";
 86271   }else{
 86272     zSep = "\n  ";
 86273     zSep2 = ",\n  ";
 86274     zEnd = "\n)";
 86276   n += 35 + 6*p->nCol;
 86277   zStmt = sqlite3DbMallocRaw(0, n);
 86278   if( zStmt==0 ){
 86279     db->mallocFailed = 1;
 86280     return 0;
 86282   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
 86283   k = sqlite3Strlen30(zStmt);
 86284   identPut(zStmt, &k, p->zName);
 86285   zStmt[k++] = '(';
 86286   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
 86287     static const char * const azType[] = {
 86288         /* SQLITE_AFF_TEXT    */ " TEXT",
 86289         /* SQLITE_AFF_NONE    */ "",
 86290         /* SQLITE_AFF_NUMERIC */ " NUM",
 86291         /* SQLITE_AFF_INTEGER */ " INT",
 86292         /* SQLITE_AFF_REAL    */ " REAL"
 86293     };
 86294     int len;
 86295     const char *zType;
 86297     sqlite3_snprintf(n-k, &zStmt[k], zSep);
 86298     k += sqlite3Strlen30(&zStmt[k]);
 86299     zSep = zSep2;
 86300     identPut(zStmt, &k, pCol->zName);
 86301     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
 86302     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
 86303     testcase( pCol->affinity==SQLITE_AFF_TEXT );
 86304     testcase( pCol->affinity==SQLITE_AFF_NONE );
 86305     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
 86306     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
 86307     testcase( pCol->affinity==SQLITE_AFF_REAL );
 86309     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
 86310     len = sqlite3Strlen30(zType);
 86311     assert( pCol->affinity==SQLITE_AFF_NONE 
 86312             || pCol->affinity==sqlite3AffinityType(zType, 0) );
 86313     memcpy(&zStmt[k], zType, len);
 86314     k += len;
 86315     assert( k<=n );
 86317   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
 86318   return zStmt;
 86321 /*
 86322 ** Resize an Index object to hold N columns total.  Return SQLITE_OK
 86323 ** on success and SQLITE_NOMEM on an OOM error.
 86324 */
 86325 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
 86326   char *zExtra;
 86327   int nByte;
 86328   if( pIdx->nColumn>=N ) return SQLITE_OK;
 86329   assert( pIdx->isResized==0 );
 86330   nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
 86331   zExtra = sqlite3DbMallocZero(db, nByte);
 86332   if( zExtra==0 ) return SQLITE_NOMEM;
 86333   memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
 86334   pIdx->azColl = (char**)zExtra;
 86335   zExtra += sizeof(char*)*N;
 86336   memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
 86337   pIdx->aiColumn = (i16*)zExtra;
 86338   zExtra += sizeof(i16)*N;
 86339   memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
 86340   pIdx->aSortOrder = (u8*)zExtra;
 86341   pIdx->nColumn = N;
 86342   pIdx->isResized = 1;
 86343   return SQLITE_OK;
 86346 /*
 86347 ** Estimate the total row width for a table.
 86348 */
 86349 static void estimateTableWidth(Table *pTab){
 86350   unsigned wTable = 0;
 86351   const Column *pTabCol;
 86352   int i;
 86353   for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
 86354     wTable += pTabCol->szEst;
 86356   if( pTab->iPKey<0 ) wTable++;
 86357   pTab->szTabRow = sqlite3LogEst(wTable*4);
 86360 /*
 86361 ** Estimate the average size of a row for an index.
 86362 */
 86363 static void estimateIndexWidth(Index *pIdx){
 86364   unsigned wIndex = 0;
 86365   int i;
 86366   const Column *aCol = pIdx->pTable->aCol;
 86367   for(i=0; i<pIdx->nColumn; i++){
 86368     i16 x = pIdx->aiColumn[i];
 86369     assert( x<pIdx->pTable->nCol );
 86370     wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
 86372   pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
 86375 /* Return true if value x is found any of the first nCol entries of aiCol[]
 86376 */
 86377 static int hasColumn(const i16 *aiCol, int nCol, int x){
 86378   while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
 86379   return 0;
 86382 /*
 86383 ** This routine runs at the end of parsing a CREATE TABLE statement that
 86384 ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
 86385 ** internal schema data structures and the generated VDBE code so that they
 86386 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
 86387 ** Changes include:
 86388 **
 86389 **     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
 86390 **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
 86391 **          data storage is a covering index btree.
 86392 **     (2)  Bypass the creation of the sqlite_master table entry
 86393 **          for the PRIMARY KEY as the the primary key index is now
 86394 **          identified by the sqlite_master table entry of the table itself.
 86395 **     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
 86396 **          schema to the rootpage from the main table.
 86397 **     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
 86398 **     (5)  Add all table columns to the PRIMARY KEY Index object
 86399 **          so that the PRIMARY KEY is a covering index.  The surplus
 86400 **          columns are part of KeyInfo.nXField and are not used for
 86401 **          sorting or lookup or uniqueness checks.
 86402 **     (6)  Replace the rowid tail on all automatically generated UNIQUE
 86403 **          indices with the PRIMARY KEY columns.
 86404 */
 86405 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
 86406   Index *pIdx;
 86407   Index *pPk;
 86408   int nPk;
 86409   int i, j;
 86410   sqlite3 *db = pParse->db;
 86411   Vdbe *v = pParse->pVdbe;
 86413   /* Convert the OP_CreateTable opcode that would normally create the
 86414   ** root-page for the table into a OP_CreateIndex opcode.  The index
 86415   ** created will become the PRIMARY KEY index.
 86416   */
 86417   if( pParse->addrCrTab ){
 86418     assert( v );
 86419     sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
 86422   /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
 86423   ** table entry.
 86424   */
 86425   if( pParse->addrSkipPK ){
 86426     assert( v );
 86427     sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
 86430   /* Locate the PRIMARY KEY index.  Or, if this table was originally
 86431   ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. 
 86432   */
 86433   if( pTab->iPKey>=0 ){
 86434     ExprList *pList;
 86435     pList = sqlite3ExprListAppend(pParse, 0, 0);
 86436     if( pList==0 ) return;
 86437     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
 86438                                         pTab->aCol[pTab->iPKey].zName);
 86439     pList->a[0].sortOrder = pParse->iPkSortOrder;
 86440     assert( pParse->pNewTable==pTab );
 86441     pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
 86442     if( pPk==0 ) return;
 86443     pPk->autoIndex = 2;
 86444     pTab->iPKey = -1;
 86445   }else{
 86446     pPk = sqlite3PrimaryKeyIndex(pTab);
 86448   pPk->isCovering = 1;
 86449   assert( pPk!=0 );
 86450   nPk = pPk->nKeyCol;
 86452   /* Make sure every column of the PRIMARY KEY is NOT NULL */
 86453   for(i=0; i<nPk; i++){
 86454     pTab->aCol[pPk->aiColumn[i]].notNull = 1;
 86456   pPk->uniqNotNull = 1;
 86458   /* The root page of the PRIMARY KEY is the table root page */
 86459   pPk->tnum = pTab->tnum;
 86461   /* Update the in-memory representation of all UNIQUE indices by converting
 86462   ** the final rowid column into one or more columns of the PRIMARY KEY.
 86463   */
 86464   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 86465     int n;
 86466     if( pIdx->autoIndex==2 ) continue;
 86467     for(i=n=0; i<nPk; i++){
 86468       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
 86470     if( n==0 ){
 86471       /* This index is a superset of the primary key */
 86472       pIdx->nColumn = pIdx->nKeyCol;
 86473       continue;
 86475     if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
 86476     for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
 86477       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
 86478         pIdx->aiColumn[j] = pPk->aiColumn[i];
 86479         pIdx->azColl[j] = pPk->azColl[i];
 86480         j++;
 86483     assert( pIdx->nColumn>=pIdx->nKeyCol+n );
 86484     assert( pIdx->nColumn>=j );
 86487   /* Add all table columns to the PRIMARY KEY index
 86488   */
 86489   if( nPk<pTab->nCol ){
 86490     if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
 86491     for(i=0, j=nPk; i<pTab->nCol; i++){
 86492       if( !hasColumn(pPk->aiColumn, j, i) ){
 86493         assert( j<pPk->nColumn );
 86494         pPk->aiColumn[j] = i;
 86495         pPk->azColl[j] = "BINARY";
 86496         j++;
 86499     assert( pPk->nColumn==j );
 86500     assert( pTab->nCol==j );
 86501   }else{
 86502     pPk->nColumn = pTab->nCol;
 86506 /*
 86507 ** This routine is called to report the final ")" that terminates
 86508 ** a CREATE TABLE statement.
 86509 **
 86510 ** The table structure that other action routines have been building
 86511 ** is added to the internal hash tables, assuming no errors have
 86512 ** occurred.
 86513 **
 86514 ** An entry for the table is made in the master table on disk, unless
 86515 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
 86516 ** it means we are reading the sqlite_master table because we just
 86517 ** connected to the database or because the sqlite_master table has
 86518 ** recently changed, so the entry for this table already exists in
 86519 ** the sqlite_master table.  We do not want to create it again.
 86520 **
 86521 ** If the pSelect argument is not NULL, it means that this routine
 86522 ** was called to create a table generated from a 
 86523 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
 86524 ** the new table will match the result set of the SELECT.
 86525 */
 86526 SQLITE_PRIVATE void sqlite3EndTable(
 86527   Parse *pParse,          /* Parse context */
 86528   Token *pCons,           /* The ',' token after the last column defn. */
 86529   Token *pEnd,            /* The ')' before options in the CREATE TABLE */
 86530   u8 tabOpts,             /* Extra table options. Usually 0. */
 86531   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
 86532 ){
 86533   Table *p;                 /* The new table */
 86534   sqlite3 *db = pParse->db; /* The database connection */
 86535   int iDb;                  /* Database in which the table lives */
 86536   Index *pIdx;              /* An implied index of the table */
 86538   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
 86539     return;
 86541   p = pParse->pNewTable;
 86542   if( p==0 ) return;
 86544   assert( !db->init.busy || !pSelect );
 86546   /* If the db->init.busy is 1 it means we are reading the SQL off the
 86547   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
 86548   ** So do not write to the disk again.  Extract the root page number
 86549   ** for the table from the db->init.newTnum field.  (The page number
 86550   ** should have been put there by the sqliteOpenCb routine.)
 86551   */
 86552   if( db->init.busy ){
 86553     p->tnum = db->init.newTnum;
 86556   /* Special processing for WITHOUT ROWID Tables */
 86557   if( tabOpts & TF_WithoutRowid ){
 86558     if( (p->tabFlags & TF_Autoincrement) ){
 86559       sqlite3ErrorMsg(pParse,
 86560           "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
 86561       return;
 86563     if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
 86564       sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
 86565     }else{
 86566       p->tabFlags |= TF_WithoutRowid;
 86567       convertToWithoutRowidTable(pParse, p);
 86571   iDb = sqlite3SchemaToIndex(db, p->pSchema);
 86573 #ifndef SQLITE_OMIT_CHECK
 86574   /* Resolve names in all CHECK constraint expressions.
 86575   */
 86576   if( p->pCheck ){
 86577     sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
 86579 #endif /* !defined(SQLITE_OMIT_CHECK) */
 86581   /* Estimate the average row size for the table and for all implied indices */
 86582   estimateTableWidth(p);
 86583   for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
 86584     estimateIndexWidth(pIdx);
 86587   /* If not initializing, then create a record for the new table
 86588   ** in the SQLITE_MASTER table of the database.
 86589   **
 86590   ** If this is a TEMPORARY table, write the entry into the auxiliary
 86591   ** file instead of into the main database file.
 86592   */
 86593   if( !db->init.busy ){
 86594     int n;
 86595     Vdbe *v;
 86596     char *zType;    /* "view" or "table" */
 86597     char *zType2;   /* "VIEW" or "TABLE" */
 86598     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
 86600     v = sqlite3GetVdbe(pParse);
 86601     if( NEVER(v==0) ) return;
 86603     sqlite3VdbeAddOp1(v, OP_Close, 0);
 86605     /* 
 86606     ** Initialize zType for the new view or table.
 86607     */
 86608     if( p->pSelect==0 ){
 86609       /* A regular table */
 86610       zType = "table";
 86611       zType2 = "TABLE";
 86612 #ifndef SQLITE_OMIT_VIEW
 86613     }else{
 86614       /* A view */
 86615       zType = "view";
 86616       zType2 = "VIEW";
 86617 #endif
 86620     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
 86621     ** statement to populate the new table. The root-page number for the
 86622     ** new table is in register pParse->regRoot.
 86623     **
 86624     ** Once the SELECT has been coded by sqlite3Select(), it is in a
 86625     ** suitable state to query for the column names and types to be used
 86626     ** by the new table.
 86627     **
 86628     ** A shared-cache write-lock is not required to write to the new table,
 86629     ** as a schema-lock must have already been obtained to create it. Since
 86630     ** a schema-lock excludes all other database users, the write-lock would
 86631     ** be redundant.
 86632     */
 86633     if( pSelect ){
 86634       SelectDest dest;
 86635       Table *pSelTab;
 86637       assert(pParse->nTab==1);
 86638       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
 86639       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
 86640       pParse->nTab = 2;
 86641       sqlite3SelectDestInit(&dest, SRT_Table, 1);
 86642       sqlite3Select(pParse, pSelect, &dest);
 86643       sqlite3VdbeAddOp1(v, OP_Close, 1);
 86644       if( pParse->nErr==0 ){
 86645         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
 86646         if( pSelTab==0 ) return;
 86647         assert( p->aCol==0 );
 86648         p->nCol = pSelTab->nCol;
 86649         p->aCol = pSelTab->aCol;
 86650         pSelTab->nCol = 0;
 86651         pSelTab->aCol = 0;
 86652         sqlite3DeleteTable(db, pSelTab);
 86656     /* Compute the complete text of the CREATE statement */
 86657     if( pSelect ){
 86658       zStmt = createTableStmt(db, p);
 86659     }else{
 86660       Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
 86661       n = (int)(pEnd2->z - pParse->sNameToken.z);
 86662       if( pEnd2->z[0]!=';' ) n += pEnd2->n;
 86663       zStmt = sqlite3MPrintf(db, 
 86664           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
 86665       );
 86668     /* A slot for the record has already been allocated in the 
 86669     ** SQLITE_MASTER table.  We just need to update that slot with all
 86670     ** the information we've collected.
 86671     */
 86672     sqlite3NestedParse(pParse,
 86673       "UPDATE %Q.%s "
 86674          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
 86675        "WHERE rowid=#%d",
 86676       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 86677       zType,
 86678       p->zName,
 86679       p->zName,
 86680       pParse->regRoot,
 86681       zStmt,
 86682       pParse->regRowid
 86683     );
 86684     sqlite3DbFree(db, zStmt);
 86685     sqlite3ChangeCookie(pParse, iDb);
 86687 #ifndef SQLITE_OMIT_AUTOINCREMENT
 86688     /* Check to see if we need to create an sqlite_sequence table for
 86689     ** keeping track of autoincrement keys.
 86690     */
 86691     if( p->tabFlags & TF_Autoincrement ){
 86692       Db *pDb = &db->aDb[iDb];
 86693       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86694       if( pDb->pSchema->pSeqTab==0 ){
 86695         sqlite3NestedParse(pParse,
 86696           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 86697           pDb->zName
 86698         );
 86701 #endif
 86703     /* Reparse everything to update our internal data structures */
 86704     sqlite3VdbeAddParseSchemaOp(v, iDb,
 86705            sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
 86709   /* Add the table to the in-memory representation of the database.
 86710   */
 86711   if( db->init.busy ){
 86712     Table *pOld;
 86713     Schema *pSchema = p->pSchema;
 86714     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86715     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
 86716                              sqlite3Strlen30(p->zName),p);
 86717     if( pOld ){
 86718       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
 86719       db->mallocFailed = 1;
 86720       return;
 86722     pParse->pNewTable = 0;
 86723     db->flags |= SQLITE_InternChanges;
 86725 #ifndef SQLITE_OMIT_ALTERTABLE
 86726     if( !p->pSelect ){
 86727       const char *zName = (const char *)pParse->sNameToken.z;
 86728       int nName;
 86729       assert( !pSelect && pCons && pEnd );
 86730       if( pCons->z==0 ){
 86731         pCons = pEnd;
 86733       nName = (int)((const char *)pCons->z - zName);
 86734       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
 86736 #endif
 86740 #ifndef SQLITE_OMIT_VIEW
 86741 /*
 86742 ** The parser calls this routine in order to create a new VIEW
 86743 */
 86744 SQLITE_PRIVATE void sqlite3CreateView(
 86745   Parse *pParse,     /* The parsing context */
 86746   Token *pBegin,     /* The CREATE token that begins the statement */
 86747   Token *pName1,     /* The token that holds the name of the view */
 86748   Token *pName2,     /* The token that holds the name of the view */
 86749   Select *pSelect,   /* A SELECT statement that will become the new view */
 86750   int isTemp,        /* TRUE for a TEMPORARY view */
 86751   int noErr          /* Suppress error messages if VIEW already exists */
 86752 ){
 86753   Table *p;
 86754   int n;
 86755   const char *z;
 86756   Token sEnd;
 86757   DbFixer sFix;
 86758   Token *pName = 0;
 86759   int iDb;
 86760   sqlite3 *db = pParse->db;
 86762   if( pParse->nVar>0 ){
 86763     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
 86764     sqlite3SelectDelete(db, pSelect);
 86765     return;
 86767   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
 86768   p = pParse->pNewTable;
 86769   if( p==0 || pParse->nErr ){
 86770     sqlite3SelectDelete(db, pSelect);
 86771     return;
 86773   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 86774   iDb = sqlite3SchemaToIndex(db, p->pSchema);
 86775   sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
 86776   if( sqlite3FixSelect(&sFix, pSelect) ){
 86777     sqlite3SelectDelete(db, pSelect);
 86778     return;
 86781   /* Make a copy of the entire SELECT statement that defines the view.
 86782   ** This will force all the Expr.token.z values to be dynamically
 86783   ** allocated rather than point to the input string - which means that
 86784   ** they will persist after the current sqlite3_exec() call returns.
 86785   */
 86786   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 86787   sqlite3SelectDelete(db, pSelect);
 86788   if( db->mallocFailed ){
 86789     return;
 86791   if( !db->init.busy ){
 86792     sqlite3ViewGetColumnNames(pParse, p);
 86795   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
 86796   ** the end.
 86797   */
 86798   sEnd = pParse->sLastToken;
 86799   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
 86800     sEnd.z += sEnd.n;
 86802   sEnd.n = 0;
 86803   n = (int)(sEnd.z - pBegin->z);
 86804   z = pBegin->z;
 86805   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
 86806   sEnd.z = &z[n-1];
 86807   sEnd.n = 1;
 86809   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
 86810   sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
 86811   return;
 86813 #endif /* SQLITE_OMIT_VIEW */
 86815 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
 86816 /*
 86817 ** The Table structure pTable is really a VIEW.  Fill in the names of
 86818 ** the columns of the view in the pTable structure.  Return the number
 86819 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
 86820 */
 86821 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 86822   Table *pSelTab;   /* A fake table from which we get the result set */
 86823   Select *pSel;     /* Copy of the SELECT that implements the view */
 86824   int nErr = 0;     /* Number of errors encountered */
 86825   int n;            /* Temporarily holds the number of cursors assigned */
 86826   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
 86827   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 86829   assert( pTable );
 86831 #ifndef SQLITE_OMIT_VIRTUALTABLE
 86832   if( sqlite3VtabCallConnect(pParse, pTable) ){
 86833     return SQLITE_ERROR;
 86835   if( IsVirtual(pTable) ) return 0;
 86836 #endif
 86838 #ifndef SQLITE_OMIT_VIEW
 86839   /* A positive nCol means the columns names for this view are
 86840   ** already known.
 86841   */
 86842   if( pTable->nCol>0 ) return 0;
 86844   /* A negative nCol is a special marker meaning that we are currently
 86845   ** trying to compute the column names.  If we enter this routine with
 86846   ** a negative nCol, it means two or more views form a loop, like this:
 86847   **
 86848   **     CREATE VIEW one AS SELECT * FROM two;
 86849   **     CREATE VIEW two AS SELECT * FROM one;
 86850   **
 86851   ** Actually, the error above is now caught prior to reaching this point.
 86852   ** But the following test is still important as it does come up
 86853   ** in the following:
 86854   ** 
 86855   **     CREATE TABLE main.ex1(a);
 86856   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
 86857   **     SELECT * FROM temp.ex1;
 86858   */
 86859   if( pTable->nCol<0 ){
 86860     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
 86861     return 1;
 86863   assert( pTable->nCol>=0 );
 86865   /* If we get this far, it means we need to compute the table names.
 86866   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
 86867   ** "*" elements in the results set of the view and will assign cursors
 86868   ** to the elements of the FROM clause.  But we do not want these changes
 86869   ** to be permanent.  So the computation is done on a copy of the SELECT
 86870   ** statement that defines the view.
 86871   */
 86872   assert( pTable->pSelect );
 86873   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
 86874   if( pSel ){
 86875     u8 enableLookaside = db->lookaside.bEnabled;
 86876     n = pParse->nTab;
 86877     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
 86878     pTable->nCol = -1;
 86879     db->lookaside.bEnabled = 0;
 86880 #ifndef SQLITE_OMIT_AUTHORIZATION
 86881     xAuth = db->xAuth;
 86882     db->xAuth = 0;
 86883     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 86884     db->xAuth = xAuth;
 86885 #else
 86886     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 86887 #endif
 86888     db->lookaside.bEnabled = enableLookaside;
 86889     pParse->nTab = n;
 86890     if( pSelTab ){
 86891       assert( pTable->aCol==0 );
 86892       pTable->nCol = pSelTab->nCol;
 86893       pTable->aCol = pSelTab->aCol;
 86894       pSelTab->nCol = 0;
 86895       pSelTab->aCol = 0;
 86896       sqlite3DeleteTable(db, pSelTab);
 86897       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
 86898       pTable->pSchema->flags |= DB_UnresetViews;
 86899     }else{
 86900       pTable->nCol = 0;
 86901       nErr++;
 86903     sqlite3SelectDelete(db, pSel);
 86904   } else {
 86905     nErr++;
 86907 #endif /* SQLITE_OMIT_VIEW */
 86908   return nErr;  
 86910 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
 86912 #ifndef SQLITE_OMIT_VIEW
 86913 /*
 86914 ** Clear the column names from every VIEW in database idx.
 86915 */
 86916 static void sqliteViewResetAll(sqlite3 *db, int idx){
 86917   HashElem *i;
 86918   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
 86919   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
 86920   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
 86921     Table *pTab = sqliteHashData(i);
 86922     if( pTab->pSelect ){
 86923       sqliteDeleteColumnNames(db, pTab);
 86924       pTab->aCol = 0;
 86925       pTab->nCol = 0;
 86928   DbClearProperty(db, idx, DB_UnresetViews);
 86930 #else
 86931 # define sqliteViewResetAll(A,B)
 86932 #endif /* SQLITE_OMIT_VIEW */
 86934 /*
 86935 ** This function is called by the VDBE to adjust the internal schema
 86936 ** used by SQLite when the btree layer moves a table root page. The
 86937 ** root-page of a table or index in database iDb has changed from iFrom
 86938 ** to iTo.
 86939 **
 86940 ** Ticket #1728:  The symbol table might still contain information
 86941 ** on tables and/or indices that are the process of being deleted.
 86942 ** If you are unlucky, one of those deleted indices or tables might
 86943 ** have the same rootpage number as the real table or index that is
 86944 ** being moved.  So we cannot stop searching after the first match 
 86945 ** because the first match might be for one of the deleted indices
 86946 ** or tables and not the table/index that is actually being moved.
 86947 ** We must continue looping until all tables and indices with
 86948 ** rootpage==iFrom have been converted to have a rootpage of iTo
 86949 ** in order to be certain that we got the right one.
 86950 */
 86951 #ifndef SQLITE_OMIT_AUTOVACUUM
 86952 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
 86953   HashElem *pElem;
 86954   Hash *pHash;
 86955   Db *pDb;
 86957   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86958   pDb = &db->aDb[iDb];
 86959   pHash = &pDb->pSchema->tblHash;
 86960   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 86961     Table *pTab = sqliteHashData(pElem);
 86962     if( pTab->tnum==iFrom ){
 86963       pTab->tnum = iTo;
 86966   pHash = &pDb->pSchema->idxHash;
 86967   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 86968     Index *pIdx = sqliteHashData(pElem);
 86969     if( pIdx->tnum==iFrom ){
 86970       pIdx->tnum = iTo;
 86974 #endif
 86976 /*
 86977 ** Write code to erase the table with root-page iTable from database iDb.
 86978 ** Also write code to modify the sqlite_master table and internal schema
 86979 ** if a root-page of another table is moved by the btree-layer whilst
 86980 ** erasing iTable (this can happen with an auto-vacuum database).
 86981 */ 
 86982 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
 86983   Vdbe *v = sqlite3GetVdbe(pParse);
 86984   int r1 = sqlite3GetTempReg(pParse);
 86985   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
 86986   sqlite3MayAbort(pParse);
 86987 #ifndef SQLITE_OMIT_AUTOVACUUM
 86988   /* OP_Destroy stores an in integer r1. If this integer
 86989   ** is non-zero, then it is the root page number of a table moved to
 86990   ** location iTable. The following code modifies the sqlite_master table to
 86991   ** reflect this.
 86992   **
 86993   ** The "#NNN" in the SQL is a special constant that means whatever value
 86994   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
 86995   ** token for additional information.
 86996   */
 86997   sqlite3NestedParse(pParse, 
 86998      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
 86999      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
 87000 #endif
 87001   sqlite3ReleaseTempReg(pParse, r1);
 87004 /*
 87005 ** Write VDBE code to erase table pTab and all associated indices on disk.
 87006 ** Code to update the sqlite_master tables and internal schema definitions
 87007 ** in case a root-page belonging to another table is moved by the btree layer
 87008 ** is also added (this can happen with an auto-vacuum database).
 87009 */
 87010 static void destroyTable(Parse *pParse, Table *pTab){
 87011 #ifdef SQLITE_OMIT_AUTOVACUUM
 87012   Index *pIdx;
 87013   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 87014   destroyRootPage(pParse, pTab->tnum, iDb);
 87015   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 87016     destroyRootPage(pParse, pIdx->tnum, iDb);
 87018 #else
 87019   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
 87020   ** is not defined), then it is important to call OP_Destroy on the
 87021   ** table and index root-pages in order, starting with the numerically 
 87022   ** largest root-page number. This guarantees that none of the root-pages
 87023   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
 87024   ** following were coded:
 87025   **
 87026   ** OP_Destroy 4 0
 87027   ** ...
 87028   ** OP_Destroy 5 0
 87029   **
 87030   ** and root page 5 happened to be the largest root-page number in the
 87031   ** database, then root page 5 would be moved to page 4 by the 
 87032   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
 87033   ** a free-list page.
 87034   */
 87035   int iTab = pTab->tnum;
 87036   int iDestroyed = 0;
 87038   while( 1 ){
 87039     Index *pIdx;
 87040     int iLargest = 0;
 87042     if( iDestroyed==0 || iTab<iDestroyed ){
 87043       iLargest = iTab;
 87045     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 87046       int iIdx = pIdx->tnum;
 87047       assert( pIdx->pSchema==pTab->pSchema );
 87048       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
 87049         iLargest = iIdx;
 87052     if( iLargest==0 ){
 87053       return;
 87054     }else{
 87055       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 87056       assert( iDb>=0 && iDb<pParse->db->nDb );
 87057       destroyRootPage(pParse, iLargest, iDb);
 87058       iDestroyed = iLargest;
 87061 #endif
 87064 /*
 87065 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
 87066 ** after a DROP INDEX or DROP TABLE command.
 87067 */
 87068 static void sqlite3ClearStatTables(
 87069   Parse *pParse,         /* The parsing context */
 87070   int iDb,               /* The database number */
 87071   const char *zType,     /* "idx" or "tbl" */
 87072   const char *zName      /* Name of index or table */
 87073 ){
 87074   int i;
 87075   const char *zDbName = pParse->db->aDb[iDb].zName;
 87076   for(i=1; i<=4; i++){
 87077     char zTab[24];
 87078     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
 87079     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
 87080       sqlite3NestedParse(pParse,
 87081         "DELETE FROM %Q.%s WHERE %s=%Q",
 87082         zDbName, zTab, zType, zName
 87083       );
 87088 /*
 87089 ** Generate code to drop a table.
 87090 */
 87091 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
 87092   Vdbe *v;
 87093   sqlite3 *db = pParse->db;
 87094   Trigger *pTrigger;
 87095   Db *pDb = &db->aDb[iDb];
 87097   v = sqlite3GetVdbe(pParse);
 87098   assert( v!=0 );
 87099   sqlite3BeginWriteOperation(pParse, 1, iDb);
 87101 #ifndef SQLITE_OMIT_VIRTUALTABLE
 87102   if( IsVirtual(pTab) ){
 87103     sqlite3VdbeAddOp0(v, OP_VBegin);
 87105 #endif
 87107   /* Drop all triggers associated with the table being dropped. Code
 87108   ** is generated to remove entries from sqlite_master and/or
 87109   ** sqlite_temp_master if required.
 87110   */
 87111   pTrigger = sqlite3TriggerList(pParse, pTab);
 87112   while( pTrigger ){
 87113     assert( pTrigger->pSchema==pTab->pSchema || 
 87114         pTrigger->pSchema==db->aDb[1].pSchema );
 87115     sqlite3DropTriggerPtr(pParse, pTrigger);
 87116     pTrigger = pTrigger->pNext;
 87119 #ifndef SQLITE_OMIT_AUTOINCREMENT
 87120   /* Remove any entries of the sqlite_sequence table associated with
 87121   ** the table being dropped. This is done before the table is dropped
 87122   ** at the btree level, in case the sqlite_sequence table needs to
 87123   ** move as a result of the drop (can happen in auto-vacuum mode).
 87124   */
 87125   if( pTab->tabFlags & TF_Autoincrement ){
 87126     sqlite3NestedParse(pParse,
 87127       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
 87128       pDb->zName, pTab->zName
 87129     );
 87131 #endif
 87133   /* Drop all SQLITE_MASTER table and index entries that refer to the
 87134   ** table. The program name loops through the master table and deletes
 87135   ** every row that refers to a table of the same name as the one being
 87136   ** dropped. Triggers are handled separately because a trigger can be
 87137   ** created in the temp database that refers to a table in another
 87138   ** database.
 87139   */
 87140   sqlite3NestedParse(pParse, 
 87141       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
 87142       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
 87143   if( !isView && !IsVirtual(pTab) ){
 87144     destroyTable(pParse, pTab);
 87147   /* Remove the table entry from SQLite's internal schema and modify
 87148   ** the schema cookie.
 87149   */
 87150   if( IsVirtual(pTab) ){
 87151     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
 87153   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
 87154   sqlite3ChangeCookie(pParse, iDb);
 87155   sqliteViewResetAll(db, iDb);
 87158 /*
 87159 ** This routine is called to do the work of a DROP TABLE statement.
 87160 ** pName is the name of the table to be dropped.
 87161 */
 87162 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
 87163   Table *pTab;
 87164   Vdbe *v;
 87165   sqlite3 *db = pParse->db;
 87166   int iDb;
 87168   if( db->mallocFailed ){
 87169     goto exit_drop_table;
 87171   assert( pParse->nErr==0 );
 87172   assert( pName->nSrc==1 );
 87173   if( noErr ) db->suppressErr++;
 87174   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
 87175   if( noErr ) db->suppressErr--;
 87177   if( pTab==0 ){
 87178     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 87179     goto exit_drop_table;
 87181   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 87182   assert( iDb>=0 && iDb<db->nDb );
 87184   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
 87185   ** it is initialized.
 87186   */
 87187   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
 87188     goto exit_drop_table;
 87190 #ifndef SQLITE_OMIT_AUTHORIZATION
 87192     int code;
 87193     const char *zTab = SCHEMA_TABLE(iDb);
 87194     const char *zDb = db->aDb[iDb].zName;
 87195     const char *zArg2 = 0;
 87196     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
 87197       goto exit_drop_table;
 87199     if( isView ){
 87200       if( !OMIT_TEMPDB && iDb==1 ){
 87201         code = SQLITE_DROP_TEMP_VIEW;
 87202       }else{
 87203         code = SQLITE_DROP_VIEW;
 87205 #ifndef SQLITE_OMIT_VIRTUALTABLE
 87206     }else if( IsVirtual(pTab) ){
 87207       code = SQLITE_DROP_VTABLE;
 87208       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
 87209 #endif
 87210     }else{
 87211       if( !OMIT_TEMPDB && iDb==1 ){
 87212         code = SQLITE_DROP_TEMP_TABLE;
 87213       }else{
 87214         code = SQLITE_DROP_TABLE;
 87217     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
 87218       goto exit_drop_table;
 87220     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
 87221       goto exit_drop_table;
 87224 #endif
 87225   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 87226     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
 87227     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
 87228     goto exit_drop_table;
 87231 #ifndef SQLITE_OMIT_VIEW
 87232   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
 87233   ** on a table.
 87234   */
 87235   if( isView && pTab->pSelect==0 ){
 87236     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
 87237     goto exit_drop_table;
 87239   if( !isView && pTab->pSelect ){
 87240     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
 87241     goto exit_drop_table;
 87243 #endif
 87245   /* Generate code to remove the table from the master table
 87246   ** on disk.
 87247   */
 87248   v = sqlite3GetVdbe(pParse);
 87249   if( v ){
 87250     sqlite3BeginWriteOperation(pParse, 1, iDb);
 87251     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 87252     sqlite3FkDropTable(pParse, pName, pTab);
 87253     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 87256 exit_drop_table:
 87257   sqlite3SrcListDelete(db, pName);
 87260 /*
 87261 ** This routine is called to create a new foreign key on the table
 87262 ** currently under construction.  pFromCol determines which columns
 87263 ** in the current table point to the foreign key.  If pFromCol==0 then
 87264 ** connect the key to the last column inserted.  pTo is the name of
 87265 ** the table referred to (a.k.a the "parent" table).  pToCol is a list
 87266 ** of tables in the parent pTo table.  flags contains all
 87267 ** information about the conflict resolution algorithms specified
 87268 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
 87269 **
 87270 ** An FKey structure is created and added to the table currently
 87271 ** under construction in the pParse->pNewTable field.
 87272 **
 87273 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
 87274 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
 87275 */
 87276 SQLITE_PRIVATE void sqlite3CreateForeignKey(
 87277   Parse *pParse,       /* Parsing context */
 87278   ExprList *pFromCol,  /* Columns in this table that point to other table */
 87279   Token *pTo,          /* Name of the other table */
 87280   ExprList *pToCol,    /* Columns in the other table */
 87281   int flags            /* Conflict resolution algorithms. */
 87282 ){
 87283   sqlite3 *db = pParse->db;
 87284 #ifndef SQLITE_OMIT_FOREIGN_KEY
 87285   FKey *pFKey = 0;
 87286   FKey *pNextTo;
 87287   Table *p = pParse->pNewTable;
 87288   int nByte;
 87289   int i;
 87290   int nCol;
 87291   char *z;
 87293   assert( pTo!=0 );
 87294   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
 87295   if( pFromCol==0 ){
 87296     int iCol = p->nCol-1;
 87297     if( NEVER(iCol<0) ) goto fk_end;
 87298     if( pToCol && pToCol->nExpr!=1 ){
 87299       sqlite3ErrorMsg(pParse, "foreign key on %s"
 87300          " should reference only one column of table %T",
 87301          p->aCol[iCol].zName, pTo);
 87302       goto fk_end;
 87304     nCol = 1;
 87305   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
 87306     sqlite3ErrorMsg(pParse,
 87307         "number of columns in foreign key does not match the number of "
 87308         "columns in the referenced table");
 87309     goto fk_end;
 87310   }else{
 87311     nCol = pFromCol->nExpr;
 87313   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
 87314   if( pToCol ){
 87315     for(i=0; i<pToCol->nExpr; i++){
 87316       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
 87319   pFKey = sqlite3DbMallocZero(db, nByte );
 87320   if( pFKey==0 ){
 87321     goto fk_end;
 87323   pFKey->pFrom = p;
 87324   pFKey->pNextFrom = p->pFKey;
 87325   z = (char*)&pFKey->aCol[nCol];
 87326   pFKey->zTo = z;
 87327   memcpy(z, pTo->z, pTo->n);
 87328   z[pTo->n] = 0;
 87329   sqlite3Dequote(z);
 87330   z += pTo->n+1;
 87331   pFKey->nCol = nCol;
 87332   if( pFromCol==0 ){
 87333     pFKey->aCol[0].iFrom = p->nCol-1;
 87334   }else{
 87335     for(i=0; i<nCol; i++){
 87336       int j;
 87337       for(j=0; j<p->nCol; j++){
 87338         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
 87339           pFKey->aCol[i].iFrom = j;
 87340           break;
 87343       if( j>=p->nCol ){
 87344         sqlite3ErrorMsg(pParse, 
 87345           "unknown column \"%s\" in foreign key definition", 
 87346           pFromCol->a[i].zName);
 87347         goto fk_end;
 87351   if( pToCol ){
 87352     for(i=0; i<nCol; i++){
 87353       int n = sqlite3Strlen30(pToCol->a[i].zName);
 87354       pFKey->aCol[i].zCol = z;
 87355       memcpy(z, pToCol->a[i].zName, n);
 87356       z[n] = 0;
 87357       z += n+1;
 87360   pFKey->isDeferred = 0;
 87361   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
 87362   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
 87364   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 87365   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
 87366       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
 87367   );
 87368   if( pNextTo==pFKey ){
 87369     db->mallocFailed = 1;
 87370     goto fk_end;
 87372   if( pNextTo ){
 87373     assert( pNextTo->pPrevTo==0 );
 87374     pFKey->pNextTo = pNextTo;
 87375     pNextTo->pPrevTo = pFKey;
 87378   /* Link the foreign key to the table as the last step.
 87379   */
 87380   p->pFKey = pFKey;
 87381   pFKey = 0;
 87383 fk_end:
 87384   sqlite3DbFree(db, pFKey);
 87385 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 87386   sqlite3ExprListDelete(db, pFromCol);
 87387   sqlite3ExprListDelete(db, pToCol);
 87390 /*
 87391 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
 87392 ** clause is seen as part of a foreign key definition.  The isDeferred
 87393 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
 87394 ** The behavior of the most recently created foreign key is adjusted
 87395 ** accordingly.
 87396 */
 87397 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
 87398 #ifndef SQLITE_OMIT_FOREIGN_KEY
 87399   Table *pTab;
 87400   FKey *pFKey;
 87401   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
 87402   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
 87403   pFKey->isDeferred = (u8)isDeferred;
 87404 #endif
 87407 /*
 87408 ** Generate code that will erase and refill index *pIdx.  This is
 87409 ** used to initialize a newly created index or to recompute the
 87410 ** content of an index in response to a REINDEX command.
 87411 **
 87412 ** if memRootPage is not negative, it means that the index is newly
 87413 ** created.  The register specified by memRootPage contains the
 87414 ** root page number of the index.  If memRootPage is negative, then
 87415 ** the index already exists and must be cleared before being refilled and
 87416 ** the root page number of the index is taken from pIndex->tnum.
 87417 */
 87418 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
 87419   Table *pTab = pIndex->pTable;  /* The table that is indexed */
 87420   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
 87421   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
 87422   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
 87423   int addr1;                     /* Address of top of loop */
 87424   int addr2;                     /* Address to jump to for next iteration */
 87425   int tnum;                      /* Root page of index */
 87426   int iPartIdxLabel;             /* Jump to this label to skip a row */
 87427   Vdbe *v;                       /* Generate code into this virtual machine */
 87428   KeyInfo *pKey;                 /* KeyInfo for index */
 87429   int regRecord;                 /* Register holding assemblied index record */
 87430   sqlite3 *db = pParse->db;      /* The database connection */
 87431   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 87433 #ifndef SQLITE_OMIT_AUTHORIZATION
 87434   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
 87435       db->aDb[iDb].zName ) ){
 87436     return;
 87438 #endif
 87440   /* Require a write-lock on the table to perform this operation */
 87441   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
 87443   v = sqlite3GetVdbe(pParse);
 87444   if( v==0 ) return;
 87445   if( memRootPage>=0 ){
 87446     tnum = memRootPage;
 87447   }else{
 87448     tnum = pIndex->tnum;
 87450   pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
 87452   /* Open the sorter cursor if we are to use one. */
 87453   iSorter = pParse->nTab++;
 87454   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
 87455                     sqlite3KeyInfoRef(pKey), P4_KEYINFO);
 87457   /* Open the table. Loop through all rows of the table, inserting index
 87458   ** records into the sorter. */
 87459   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
 87460   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
 87461   regRecord = sqlite3GetTempReg(pParse);
 87463   sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
 87464   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
 87465   sqlite3VdbeResolveLabel(v, iPartIdxLabel);
 87466   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
 87467   sqlite3VdbeJumpHere(v, addr1);
 87468   if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
 87469   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
 87470                     (char *)pKey, P4_KEYINFO);
 87471   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
 87473   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
 87474   assert( pKey!=0 || db->mallocFailed || pParse->nErr );
 87475   if( pIndex->onError!=OE_None && pKey!=0 ){
 87476     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
 87477     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
 87478     addr2 = sqlite3VdbeCurrentAddr(v);
 87479     sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
 87480                          pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
 87481     sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
 87482   }else{
 87483     addr2 = sqlite3VdbeCurrentAddr(v);
 87485   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
 87486   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
 87487   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 87488   sqlite3ReleaseTempReg(pParse, regRecord);
 87489   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
 87490   sqlite3VdbeJumpHere(v, addr1);
 87492   sqlite3VdbeAddOp1(v, OP_Close, iTab);
 87493   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 87494   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
 87497 /*
 87498 ** Allocate heap space to hold an Index object with nCol columns.
 87499 **
 87500 ** Increase the allocation size to provide an extra nExtra bytes
 87501 ** of 8-byte aligned space after the Index object and return a
 87502 ** pointer to this extra space in *ppExtra.
 87503 */
 87504 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
 87505   sqlite3 *db,         /* Database connection */
 87506   i16 nCol,            /* Total number of columns in the index */
 87507   int nExtra,          /* Number of bytes of extra space to alloc */
 87508   char **ppExtra       /* Pointer to the "extra" space */
 87509 ){
 87510   Index *p;            /* Allocated index object */
 87511   int nByte;           /* Bytes of space for Index object + arrays */
 87513   nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
 87514           ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
 87515           ROUND8(sizeof(tRowcnt)*(nCol+1) +    /* Index.aiRowEst   */
 87516                  sizeof(i16)*nCol +            /* Index.aiColumn   */
 87517                  sizeof(u8)*nCol);             /* Index.aSortOrder */
 87518   p = sqlite3DbMallocZero(db, nByte + nExtra);
 87519   if( p ){
 87520     char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
 87521     p->azColl = (char**)pExtra;      pExtra += ROUND8(sizeof(char*)*nCol);
 87522     p->aiRowEst = (tRowcnt*)pExtra;  pExtra += sizeof(tRowcnt)*(nCol+1);
 87523     p->aiColumn = (i16*)pExtra;      pExtra += sizeof(i16)*nCol;
 87524     p->aSortOrder = (u8*)pExtra;
 87525     p->nColumn = nCol;
 87526     p->nKeyCol = nCol - 1;
 87527     *ppExtra = ((char*)p) + nByte;
 87529   return p;
 87532 /*
 87533 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
 87534 ** and pTblList is the name of the table that is to be indexed.  Both will 
 87535 ** be NULL for a primary key or an index that is created to satisfy a
 87536 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
 87537 ** as the table to be indexed.  pParse->pNewTable is a table that is
 87538 ** currently being constructed by a CREATE TABLE statement.
 87539 **
 87540 ** pList is a list of columns to be indexed.  pList will be NULL if this
 87541 ** is a primary key or unique-constraint on the most recent column added
 87542 ** to the table currently under construction.  
 87543 **
 87544 ** If the index is created successfully, return a pointer to the new Index
 87545 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
 87546 ** as the tables primary key (Index.autoIndex==2).
 87547 */
 87548 SQLITE_PRIVATE Index *sqlite3CreateIndex(
 87549   Parse *pParse,     /* All information about this parse */
 87550   Token *pName1,     /* First part of index name. May be NULL */
 87551   Token *pName2,     /* Second part of index name. May be NULL */
 87552   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
 87553   ExprList *pList,   /* A list of columns to be indexed */
 87554   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
 87555   Token *pStart,     /* The CREATE token that begins this statement */
 87556   Expr *pPIWhere,    /* WHERE clause for partial indices */
 87557   int sortOrder,     /* Sort order of primary key when pList==NULL */
 87558   int ifNotExist     /* Omit error if index already exists */
 87559 ){
 87560   Index *pRet = 0;     /* Pointer to return */
 87561   Table *pTab = 0;     /* Table to be indexed */
 87562   Index *pIndex = 0;   /* The index to be created */
 87563   char *zName = 0;     /* Name of the index */
 87564   int nName;           /* Number of characters in zName */
 87565   int i, j;
 87566   DbFixer sFix;        /* For assigning database names to pTable */
 87567   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
 87568   sqlite3 *db = pParse->db;
 87569   Db *pDb;             /* The specific table containing the indexed database */
 87570   int iDb;             /* Index of the database that is being written */
 87571   Token *pName = 0;    /* Unqualified name of the index to create */
 87572   struct ExprList_item *pListItem; /* For looping over pList */
 87573   const Column *pTabCol;           /* A column in the table */
 87574   int nExtra = 0;                  /* Space allocated for zExtra[] */
 87575   int nExtraCol;                   /* Number of extra columns needed */
 87576   char *zExtra = 0;                /* Extra space after the Index object */
 87577   Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
 87579   assert( pParse->nErr==0 );      /* Never called with prior errors */
 87580   if( db->mallocFailed || IN_DECLARE_VTAB ){
 87581     goto exit_create_index;
 87583   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 87584     goto exit_create_index;
 87587   /*
 87588   ** Find the table that is to be indexed.  Return early if not found.
 87589   */
 87590   if( pTblName!=0 ){
 87592     /* Use the two-part index name to determine the database 
 87593     ** to search for the table. 'Fix' the table name to this db
 87594     ** before looking up the table.
 87595     */
 87596     assert( pName1 && pName2 );
 87597     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 87598     if( iDb<0 ) goto exit_create_index;
 87599     assert( pName && pName->z );
 87601 #ifndef SQLITE_OMIT_TEMPDB
 87602     /* If the index name was unqualified, check if the table
 87603     ** is a temp table. If so, set the database to 1. Do not do this
 87604     ** if initialising a database schema.
 87605     */
 87606     if( !db->init.busy ){
 87607       pTab = sqlite3SrcListLookup(pParse, pTblName);
 87608       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
 87609         iDb = 1;
 87612 #endif
 87614     sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
 87615     if( sqlite3FixSrcList(&sFix, pTblName) ){
 87616       /* Because the parser constructs pTblName from a single identifier,
 87617       ** sqlite3FixSrcList can never fail. */
 87618       assert(0);
 87620     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
 87621     assert( db->mallocFailed==0 || pTab==0 );
 87622     if( pTab==0 ) goto exit_create_index;
 87623     if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
 87624       sqlite3ErrorMsg(pParse, 
 87625            "cannot create a TEMP index on non-TEMP table \"%s\"",
 87626            pTab->zName);
 87627       goto exit_create_index;
 87629     if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
 87630   }else{
 87631     assert( pName==0 );
 87632     assert( pStart==0 );
 87633     pTab = pParse->pNewTable;
 87634     if( !pTab ) goto exit_create_index;
 87635     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 87637   pDb = &db->aDb[iDb];
 87639   assert( pTab!=0 );
 87640   assert( pParse->nErr==0 );
 87641   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
 87642        && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
 87643     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
 87644     goto exit_create_index;
 87646 #ifndef SQLITE_OMIT_VIEW
 87647   if( pTab->pSelect ){
 87648     sqlite3ErrorMsg(pParse, "views may not be indexed");
 87649     goto exit_create_index;
 87651 #endif
 87652 #ifndef SQLITE_OMIT_VIRTUALTABLE
 87653   if( IsVirtual(pTab) ){
 87654     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
 87655     goto exit_create_index;
 87657 #endif
 87659   /*
 87660   ** Find the name of the index.  Make sure there is not already another
 87661   ** index or table with the same name.  
 87662   **
 87663   ** Exception:  If we are reading the names of permanent indices from the
 87664   ** sqlite_master table (because some other process changed the schema) and
 87665   ** one of the index names collides with the name of a temporary table or
 87666   ** index, then we will continue to process this index.
 87667   **
 87668   ** If pName==0 it means that we are
 87669   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
 87670   ** own name.
 87671   */
 87672   if( pName ){
 87673     zName = sqlite3NameFromToken(db, pName);
 87674     if( zName==0 ) goto exit_create_index;
 87675     assert( pName->z!=0 );
 87676     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 87677       goto exit_create_index;
 87679     if( !db->init.busy ){
 87680       if( sqlite3FindTable(db, zName, 0)!=0 ){
 87681         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 87682         goto exit_create_index;
 87685     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
 87686       if( !ifNotExist ){
 87687         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
 87688       }else{
 87689         assert( !db->init.busy );
 87690         sqlite3CodeVerifySchema(pParse, iDb);
 87692       goto exit_create_index;
 87694   }else{
 87695     int n;
 87696     Index *pLoop;
 87697     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
 87698     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
 87699     if( zName==0 ){
 87700       goto exit_create_index;
 87704   /* Check for authorization to create an index.
 87705   */
 87706 #ifndef SQLITE_OMIT_AUTHORIZATION
 87708     const char *zDb = pDb->zName;
 87709     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
 87710       goto exit_create_index;
 87712     i = SQLITE_CREATE_INDEX;
 87713     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
 87714     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
 87715       goto exit_create_index;
 87718 #endif
 87720   /* If pList==0, it means this routine was called to make a primary
 87721   ** key out of the last column added to the table under construction.
 87722   ** So create a fake list to simulate this.
 87723   */
 87724   if( pList==0 ){
 87725     pList = sqlite3ExprListAppend(pParse, 0, 0);
 87726     if( pList==0 ) goto exit_create_index;
 87727     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
 87728                                         pTab->aCol[pTab->nCol-1].zName);
 87729     pList->a[0].sortOrder = (u8)sortOrder;
 87732   /* Figure out how many bytes of space are required to store explicitly
 87733   ** specified collation sequence names.
 87734   */
 87735   for(i=0; i<pList->nExpr; i++){
 87736     Expr *pExpr = pList->a[i].pExpr;
 87737     if( pExpr ){
 87738       assert( pExpr->op==TK_COLLATE );
 87739       nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
 87743   /* 
 87744   ** Allocate the index structure. 
 87745   */
 87746   nName = sqlite3Strlen30(zName);
 87747   nExtraCol = pPk ? pPk->nKeyCol : 1;
 87748   pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
 87749                                       nName + nExtra + 1, &zExtra);
 87750   if( db->mallocFailed ){
 87751     goto exit_create_index;
 87753   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
 87754   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
 87755   pIndex->zName = zExtra;
 87756   zExtra += nName + 1;
 87757   memcpy(pIndex->zName, zName, nName+1);
 87758   pIndex->pTable = pTab;
 87759   pIndex->onError = (u8)onError;
 87760   pIndex->uniqNotNull = onError!=OE_None;
 87761   pIndex->autoIndex = (u8)(pName==0);
 87762   pIndex->pSchema = db->aDb[iDb].pSchema;
 87763   pIndex->nKeyCol = pList->nExpr;
 87764   if( pPIWhere ){
 87765     sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
 87766     pIndex->pPartIdxWhere = pPIWhere;
 87767     pPIWhere = 0;
 87769   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 87771   /* Check to see if we should honor DESC requests on index columns
 87772   */
 87773   if( pDb->pSchema->file_format>=4 ){
 87774     sortOrderMask = -1;   /* Honor DESC */
 87775   }else{
 87776     sortOrderMask = 0;    /* Ignore DESC */
 87779   /* Scan the names of the columns of the table to be indexed and
 87780   ** load the column indices into the Index structure.  Report an error
 87781   ** if any column is not found.
 87782   **
 87783   ** TODO:  Add a test to make sure that the same column is not named
 87784   ** more than once within the same index.  Only the first instance of
 87785   ** the column will ever be used by the optimizer.  Note that using the
 87786   ** same column more than once cannot be an error because that would 
 87787   ** break backwards compatibility - it needs to be a warning.
 87788   */
 87789   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
 87790     const char *zColName = pListItem->zName;
 87791     int requestedSortOrder;
 87792     char *zColl;                   /* Collation sequence name */
 87794     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
 87795       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
 87797     if( j>=pTab->nCol ){
 87798       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
 87799         pTab->zName, zColName);
 87800       pParse->checkSchema = 1;
 87801       goto exit_create_index;
 87803     assert( pTab->nCol<=0x7fff && j<=0x7fff );
 87804     pIndex->aiColumn[i] = (i16)j;
 87805     if( pListItem->pExpr ){
 87806       int nColl;
 87807       assert( pListItem->pExpr->op==TK_COLLATE );
 87808       zColl = pListItem->pExpr->u.zToken;
 87809       nColl = sqlite3Strlen30(zColl) + 1;
 87810       assert( nExtra>=nColl );
 87811       memcpy(zExtra, zColl, nColl);
 87812       zColl = zExtra;
 87813       zExtra += nColl;
 87814       nExtra -= nColl;
 87815     }else{
 87816       zColl = pTab->aCol[j].zColl;
 87817       if( !zColl ) zColl = "BINARY";
 87819     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
 87820       goto exit_create_index;
 87822     pIndex->azColl[i] = zColl;
 87823     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
 87824     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
 87825     if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
 87827   if( pPk ){
 87828     for(j=0; j<pPk->nKeyCol; j++){
 87829       int x = pPk->aiColumn[j];
 87830       if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
 87831         pIndex->nColumn--; 
 87832       }else{
 87833         pIndex->aiColumn[i] = x;
 87834         pIndex->azColl[i] = pPk->azColl[j];
 87835         pIndex->aSortOrder[i] = pPk->aSortOrder[j];
 87836         i++;
 87839     assert( i==pIndex->nColumn );
 87840   }else{
 87841     pIndex->aiColumn[i] = -1;
 87842     pIndex->azColl[i] = "BINARY";
 87844   sqlite3DefaultRowEst(pIndex);
 87845   if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
 87847   if( pTab==pParse->pNewTable ){
 87848     /* This routine has been called to create an automatic index as a
 87849     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
 87850     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
 87851     ** i.e. one of:
 87852     **
 87853     ** CREATE TABLE t(x PRIMARY KEY, y);
 87854     ** CREATE TABLE t(x, y, UNIQUE(x, y));
 87855     **
 87856     ** Either way, check to see if the table already has such an index. If
 87857     ** so, don't bother creating this one. This only applies to
 87858     ** automatically created indices. Users can do as they wish with
 87859     ** explicit indices.
 87860     **
 87861     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
 87862     ** (and thus suppressing the second one) even if they have different
 87863     ** sort orders.
 87864     **
 87865     ** If there are different collating sequences or if the columns of
 87866     ** the constraint occur in different orders, then the constraints are
 87867     ** considered distinct and both result in separate indices.
 87868     */
 87869     Index *pIdx;
 87870     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 87871       int k;
 87872       assert( pIdx->onError!=OE_None );
 87873       assert( pIdx->autoIndex );
 87874       assert( pIndex->onError!=OE_None );
 87876       if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
 87877       for(k=0; k<pIdx->nKeyCol; k++){
 87878         const char *z1;
 87879         const char *z2;
 87880         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
 87881         z1 = pIdx->azColl[k];
 87882         z2 = pIndex->azColl[k];
 87883         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
 87885       if( k==pIdx->nKeyCol ){
 87886         if( pIdx->onError!=pIndex->onError ){
 87887           /* This constraint creates the same index as a previous
 87888           ** constraint specified somewhere in the CREATE TABLE statement.
 87889           ** However the ON CONFLICT clauses are different. If both this 
 87890           ** constraint and the previous equivalent constraint have explicit
 87891           ** ON CONFLICT clauses this is an error. Otherwise, use the
 87892           ** explicitly specified behavior for the index.
 87893           */
 87894           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
 87895             sqlite3ErrorMsg(pParse, 
 87896                 "conflicting ON CONFLICT clauses specified", 0);
 87898           if( pIdx->onError==OE_Default ){
 87899             pIdx->onError = pIndex->onError;
 87902         goto exit_create_index;
 87907   /* Link the new Index structure to its table and to the other
 87908   ** in-memory database structures. 
 87909   */
 87910   if( db->init.busy ){
 87911     Index *p;
 87912     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 87913     p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
 87914                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
 87915                           pIndex);
 87916     if( p ){
 87917       assert( p==pIndex );  /* Malloc must have failed */
 87918       db->mallocFailed = 1;
 87919       goto exit_create_index;
 87921     db->flags |= SQLITE_InternChanges;
 87922     if( pTblName!=0 ){
 87923       pIndex->tnum = db->init.newTnum;
 87927   /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
 87928   ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
 87929   ** emit code to allocate the index rootpage on disk and make an entry for
 87930   ** the index in the sqlite_master table and populate the index with
 87931   ** content.  But, do not do this if we are simply reading the sqlite_master
 87932   ** table to parse the schema, or if this index is the PRIMARY KEY index
 87933   ** of a WITHOUT ROWID table.
 87934   **
 87935   ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
 87936   ** or UNIQUE index in a CREATE TABLE statement.  Since the table
 87937   ** has just been created, it contains no data and the index initialization
 87938   ** step can be skipped.
 87939   */
 87940   else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
 87941     Vdbe *v;
 87942     char *zStmt;
 87943     int iMem = ++pParse->nMem;
 87945     v = sqlite3GetVdbe(pParse);
 87946     if( v==0 ) goto exit_create_index;
 87949     /* Create the rootpage for the index
 87950     */
 87951     sqlite3BeginWriteOperation(pParse, 1, iDb);
 87952     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
 87954     /* Gather the complete text of the CREATE INDEX statement into
 87955     ** the zStmt variable
 87956     */
 87957     if( pStart ){
 87958       int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
 87959       if( pName->z[n-1]==';' ) n--;
 87960       /* A named index with an explicit CREATE INDEX statement */
 87961       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
 87962         onError==OE_None ? "" : " UNIQUE", n, pName->z);
 87963     }else{
 87964       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
 87965       /* zStmt = sqlite3MPrintf(""); */
 87966       zStmt = 0;
 87969     /* Add an entry in sqlite_master for this index
 87970     */
 87971     sqlite3NestedParse(pParse, 
 87972         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
 87973         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 87974         pIndex->zName,
 87975         pTab->zName,
 87976         iMem,
 87977         zStmt
 87978     );
 87979     sqlite3DbFree(db, zStmt);
 87981     /* Fill the index with data and reparse the schema. Code an OP_Expire
 87982     ** to invalidate all pre-compiled statements.
 87983     */
 87984     if( pTblName ){
 87985       sqlite3RefillIndex(pParse, pIndex, iMem);
 87986       sqlite3ChangeCookie(pParse, iDb);
 87987       sqlite3VdbeAddParseSchemaOp(v, iDb,
 87988          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
 87989       sqlite3VdbeAddOp1(v, OP_Expire, 0);
 87993   /* When adding an index to the list of indices for a table, make
 87994   ** sure all indices labeled OE_Replace come after all those labeled
 87995   ** OE_Ignore.  This is necessary for the correct constraint check
 87996   ** processing (in sqlite3GenerateConstraintChecks()) as part of
 87997   ** UPDATE and INSERT statements.  
 87998   */
 87999   if( db->init.busy || pTblName==0 ){
 88000     if( onError!=OE_Replace || pTab->pIndex==0
 88001          || pTab->pIndex->onError==OE_Replace){
 88002       pIndex->pNext = pTab->pIndex;
 88003       pTab->pIndex = pIndex;
 88004     }else{
 88005       Index *pOther = pTab->pIndex;
 88006       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
 88007         pOther = pOther->pNext;
 88009       pIndex->pNext = pOther->pNext;
 88010       pOther->pNext = pIndex;
 88012     pRet = pIndex;
 88013     pIndex = 0;
 88016   /* Clean up before exiting */
 88017 exit_create_index:
 88018   if( pIndex ) freeIndex(db, pIndex);
 88019   sqlite3ExprDelete(db, pPIWhere);
 88020   sqlite3ExprListDelete(db, pList);
 88021   sqlite3SrcListDelete(db, pTblName);
 88022   sqlite3DbFree(db, zName);
 88023   return pRet;
 88026 /*
 88027 ** Fill the Index.aiRowEst[] array with default information - information
 88028 ** to be used when we have not run the ANALYZE command.
 88029 **
 88030 ** aiRowEst[0] is suppose to contain the number of elements in the index.
 88031 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
 88032 ** number of rows in the table that match any particular value of the
 88033 ** first column of the index.  aiRowEst[2] is an estimate of the number
 88034 ** of rows that match any particular combiniation of the first 2 columns
 88035 ** of the index.  And so forth.  It must always be the case that
 88037 **           aiRowEst[N]<=aiRowEst[N-1]
 88038 **           aiRowEst[N]>=1
 88039 **
 88040 ** Apart from that, we have little to go on besides intuition as to
 88041 ** how aiRowEst[] should be initialized.  The numbers generated here
 88042 ** are based on typical values found in actual indices.
 88043 */
 88044 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
 88045   tRowcnt *a = pIdx->aiRowEst;
 88046   int i;
 88047   tRowcnt n;
 88048   assert( a!=0 );
 88049   a[0] = pIdx->pTable->nRowEst;
 88050   if( a[0]<10 ) a[0] = 10;
 88051   n = 10;
 88052   for(i=1; i<=pIdx->nKeyCol; i++){
 88053     a[i] = n;
 88054     if( n>5 ) n--;
 88056   if( pIdx->onError!=OE_None ){
 88057     a[pIdx->nKeyCol] = 1;
 88061 /*
 88062 ** This routine will drop an existing named index.  This routine
 88063 ** implements the DROP INDEX statement.
 88064 */
 88065 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
 88066   Index *pIndex;
 88067   Vdbe *v;
 88068   sqlite3 *db = pParse->db;
 88069   int iDb;
 88071   assert( pParse->nErr==0 );   /* Never called with prior errors */
 88072   if( db->mallocFailed ){
 88073     goto exit_drop_index;
 88075   assert( pName->nSrc==1 );
 88076   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 88077     goto exit_drop_index;
 88079   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
 88080   if( pIndex==0 ){
 88081     if( !ifExists ){
 88082       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
 88083     }else{
 88084       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 88086     pParse->checkSchema = 1;
 88087     goto exit_drop_index;
 88089   if( pIndex->autoIndex ){
 88090     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
 88091       "or PRIMARY KEY constraint cannot be dropped", 0);
 88092     goto exit_drop_index;
 88094   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 88095 #ifndef SQLITE_OMIT_AUTHORIZATION
 88097     int code = SQLITE_DROP_INDEX;
 88098     Table *pTab = pIndex->pTable;
 88099     const char *zDb = db->aDb[iDb].zName;
 88100     const char *zTab = SCHEMA_TABLE(iDb);
 88101     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 88102       goto exit_drop_index;
 88104     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
 88105     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
 88106       goto exit_drop_index;
 88109 #endif
 88111   /* Generate code to remove the index and from the master table */
 88112   v = sqlite3GetVdbe(pParse);
 88113   if( v ){
 88114     sqlite3BeginWriteOperation(pParse, 1, iDb);
 88115     sqlite3NestedParse(pParse,
 88116        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
 88117        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
 88118     );
 88119     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
 88120     sqlite3ChangeCookie(pParse, iDb);
 88121     destroyRootPage(pParse, pIndex->tnum, iDb);
 88122     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
 88125 exit_drop_index:
 88126   sqlite3SrcListDelete(db, pName);
 88129 /*
 88130 ** pArray is a pointer to an array of objects. Each object in the
 88131 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
 88132 ** to extend the array so that there is space for a new object at the end.
 88133 **
 88134 ** When this function is called, *pnEntry contains the current size of
 88135 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
 88136 ** in total).
 88137 **
 88138 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
 88139 ** space allocated for the new object is zeroed, *pnEntry updated to
 88140 ** reflect the new size of the array and a pointer to the new allocation
 88141 ** returned. *pIdx is set to the index of the new array entry in this case.
 88142 **
 88143 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
 88144 ** unchanged and a copy of pArray returned.
 88145 */
 88146 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
 88147   sqlite3 *db,      /* Connection to notify of malloc failures */
 88148   void *pArray,     /* Array of objects.  Might be reallocated */
 88149   int szEntry,      /* Size of each object in the array */
 88150   int *pnEntry,     /* Number of objects currently in use */
 88151   int *pIdx         /* Write the index of a new slot here */
 88152 ){
 88153   char *z;
 88154   int n = *pnEntry;
 88155   if( (n & (n-1))==0 ){
 88156     int sz = (n==0) ? 1 : 2*n;
 88157     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
 88158     if( pNew==0 ){
 88159       *pIdx = -1;
 88160       return pArray;
 88162     pArray = pNew;
 88164   z = (char*)pArray;
 88165   memset(&z[n * szEntry], 0, szEntry);
 88166   *pIdx = n;
 88167   ++*pnEntry;
 88168   return pArray;
 88171 /*
 88172 ** Append a new element to the given IdList.  Create a new IdList if
 88173 ** need be.
 88174 **
 88175 ** A new IdList is returned, or NULL if malloc() fails.
 88176 */
 88177 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
 88178   int i;
 88179   if( pList==0 ){
 88180     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
 88181     if( pList==0 ) return 0;
 88183   pList->a = sqlite3ArrayAllocate(
 88184       db,
 88185       pList->a,
 88186       sizeof(pList->a[0]),
 88187       &pList->nId,
 88188       &i
 88189   );
 88190   if( i<0 ){
 88191     sqlite3IdListDelete(db, pList);
 88192     return 0;
 88194   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
 88195   return pList;
 88198 /*
 88199 ** Delete an IdList.
 88200 */
 88201 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
 88202   int i;
 88203   if( pList==0 ) return;
 88204   for(i=0; i<pList->nId; i++){
 88205     sqlite3DbFree(db, pList->a[i].zName);
 88207   sqlite3DbFree(db, pList->a);
 88208   sqlite3DbFree(db, pList);
 88211 /*
 88212 ** Return the index in pList of the identifier named zId.  Return -1
 88213 ** if not found.
 88214 */
 88215 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
 88216   int i;
 88217   if( pList==0 ) return -1;
 88218   for(i=0; i<pList->nId; i++){
 88219     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
 88221   return -1;
 88224 /*
 88225 ** Expand the space allocated for the given SrcList object by
 88226 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
 88227 ** New slots are zeroed.
 88228 **
 88229 ** For example, suppose a SrcList initially contains two entries: A,B.
 88230 ** To append 3 new entries onto the end, do this:
 88231 **
 88232 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
 88233 **
 88234 ** After the call above it would contain:  A, B, nil, nil, nil.
 88235 ** If the iStart argument had been 1 instead of 2, then the result
 88236 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
 88237 ** the iStart value would be 0.  The result then would
 88238 ** be: nil, nil, nil, A, B.
 88239 **
 88240 ** If a memory allocation fails the SrcList is unchanged.  The
 88241 ** db->mallocFailed flag will be set to true.
 88242 */
 88243 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
 88244   sqlite3 *db,       /* Database connection to notify of OOM errors */
 88245   SrcList *pSrc,     /* The SrcList to be enlarged */
 88246   int nExtra,        /* Number of new slots to add to pSrc->a[] */
 88247   int iStart         /* Index in pSrc->a[] of first new slot */
 88248 ){
 88249   int i;
 88251   /* Sanity checking on calling parameters */
 88252   assert( iStart>=0 );
 88253   assert( nExtra>=1 );
 88254   assert( pSrc!=0 );
 88255   assert( iStart<=pSrc->nSrc );
 88257   /* Allocate additional space if needed */
 88258   if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
 88259     SrcList *pNew;
 88260     int nAlloc = pSrc->nSrc+nExtra;
 88261     int nGot;
 88262     pNew = sqlite3DbRealloc(db, pSrc,
 88263                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
 88264     if( pNew==0 ){
 88265       assert( db->mallocFailed );
 88266       return pSrc;
 88268     pSrc = pNew;
 88269     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
 88270     pSrc->nAlloc = nGot;
 88273   /* Move existing slots that come after the newly inserted slots
 88274   ** out of the way */
 88275   for(i=pSrc->nSrc-1; i>=iStart; i--){
 88276     pSrc->a[i+nExtra] = pSrc->a[i];
 88278   pSrc->nSrc += nExtra;
 88280   /* Zero the newly allocated slots */
 88281   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
 88282   for(i=iStart; i<iStart+nExtra; i++){
 88283     pSrc->a[i].iCursor = -1;
 88286   /* Return a pointer to the enlarged SrcList */
 88287   return pSrc;
 88291 /*
 88292 ** Append a new table name to the given SrcList.  Create a new SrcList if
 88293 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
 88294 **
 88295 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
 88296 ** SrcList might be the same as the SrcList that was input or it might be
 88297 ** a new one.  If an OOM error does occurs, then the prior value of pList
 88298 ** that is input to this routine is automatically freed.
 88299 **
 88300 ** If pDatabase is not null, it means that the table has an optional
 88301 ** database name prefix.  Like this:  "database.table".  The pDatabase
 88302 ** points to the table name and the pTable points to the database name.
 88303 ** The SrcList.a[].zName field is filled with the table name which might
 88304 ** come from pTable (if pDatabase is NULL) or from pDatabase.  
 88305 ** SrcList.a[].zDatabase is filled with the database name from pTable,
 88306 ** or with NULL if no database is specified.
 88307 **
 88308 ** In other words, if call like this:
 88309 **
 88310 **         sqlite3SrcListAppend(D,A,B,0);
 88311 **
 88312 ** Then B is a table name and the database name is unspecified.  If called
 88313 ** like this:
 88314 **
 88315 **         sqlite3SrcListAppend(D,A,B,C);
 88316 **
 88317 ** Then C is the table name and B is the database name.  If C is defined
 88318 ** then so is B.  In other words, we never have a case where:
 88319 **
 88320 **         sqlite3SrcListAppend(D,A,0,C);
 88321 **
 88322 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
 88323 ** before being added to the SrcList.
 88324 */
 88325 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
 88326   sqlite3 *db,        /* Connection to notify of malloc failures */
 88327   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
 88328   Token *pTable,      /* Table to append */
 88329   Token *pDatabase    /* Database of the table */
 88330 ){
 88331   struct SrcList_item *pItem;
 88332   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
 88333   if( pList==0 ){
 88334     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
 88335     if( pList==0 ) return 0;
 88336     pList->nAlloc = 1;
 88338   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
 88339   if( db->mallocFailed ){
 88340     sqlite3SrcListDelete(db, pList);
 88341     return 0;
 88343   pItem = &pList->a[pList->nSrc-1];
 88344   if( pDatabase && pDatabase->z==0 ){
 88345     pDatabase = 0;
 88347   if( pDatabase ){
 88348     Token *pTemp = pDatabase;
 88349     pDatabase = pTable;
 88350     pTable = pTemp;
 88352   pItem->zName = sqlite3NameFromToken(db, pTable);
 88353   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
 88354   return pList;
 88357 /*
 88358 ** Assign VdbeCursor index numbers to all tables in a SrcList
 88359 */
 88360 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
 88361   int i;
 88362   struct SrcList_item *pItem;
 88363   assert(pList || pParse->db->mallocFailed );
 88364   if( pList ){
 88365     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
 88366       if( pItem->iCursor>=0 ) break;
 88367       pItem->iCursor = pParse->nTab++;
 88368       if( pItem->pSelect ){
 88369         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
 88375 /*
 88376 ** Delete an entire SrcList including all its substructure.
 88377 */
 88378 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
 88379   int i;
 88380   struct SrcList_item *pItem;
 88381   if( pList==0 ) return;
 88382   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
 88383     sqlite3DbFree(db, pItem->zDatabase);
 88384     sqlite3DbFree(db, pItem->zName);
 88385     sqlite3DbFree(db, pItem->zAlias);
 88386     sqlite3DbFree(db, pItem->zIndex);
 88387     sqlite3DeleteTable(db, pItem->pTab);
 88388     sqlite3SelectDelete(db, pItem->pSelect);
 88389     sqlite3ExprDelete(db, pItem->pOn);
 88390     sqlite3IdListDelete(db, pItem->pUsing);
 88392   sqlite3DbFree(db, pList);
 88395 /*
 88396 ** This routine is called by the parser to add a new term to the
 88397 ** end of a growing FROM clause.  The "p" parameter is the part of
 88398 ** the FROM clause that has already been constructed.  "p" is NULL
 88399 ** if this is the first term of the FROM clause.  pTable and pDatabase
 88400 ** are the name of the table and database named in the FROM clause term.
 88401 ** pDatabase is NULL if the database name qualifier is missing - the
 88402 ** usual case.  If the term has a alias, then pAlias points to the
 88403 ** alias token.  If the term is a subquery, then pSubquery is the
 88404 ** SELECT statement that the subquery encodes.  The pTable and
 88405 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
 88406 ** parameters are the content of the ON and USING clauses.
 88407 **
 88408 ** Return a new SrcList which encodes is the FROM with the new
 88409 ** term added.
 88410 */
 88411 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
 88412   Parse *pParse,          /* Parsing context */
 88413   SrcList *p,             /* The left part of the FROM clause already seen */
 88414   Token *pTable,          /* Name of the table to add to the FROM clause */
 88415   Token *pDatabase,       /* Name of the database containing pTable */
 88416   Token *pAlias,          /* The right-hand side of the AS subexpression */
 88417   Select *pSubquery,      /* A subquery used in place of a table name */
 88418   Expr *pOn,              /* The ON clause of a join */
 88419   IdList *pUsing          /* The USING clause of a join */
 88420 ){
 88421   struct SrcList_item *pItem;
 88422   sqlite3 *db = pParse->db;
 88423   if( !p && (pOn || pUsing) ){
 88424     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
 88425       (pOn ? "ON" : "USING")
 88426     );
 88427     goto append_from_error;
 88429   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
 88430   if( p==0 || NEVER(p->nSrc==0) ){
 88431     goto append_from_error;
 88433   pItem = &p->a[p->nSrc-1];
 88434   assert( pAlias!=0 );
 88435   if( pAlias->n ){
 88436     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
 88438   pItem->pSelect = pSubquery;
 88439   pItem->pOn = pOn;
 88440   pItem->pUsing = pUsing;
 88441   return p;
 88443  append_from_error:
 88444   assert( p==0 );
 88445   sqlite3ExprDelete(db, pOn);
 88446   sqlite3IdListDelete(db, pUsing);
 88447   sqlite3SelectDelete(db, pSubquery);
 88448   return 0;
 88451 /*
 88452 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
 88453 ** element of the source-list passed as the second argument.
 88454 */
 88455 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
 88456   assert( pIndexedBy!=0 );
 88457   if( p && ALWAYS(p->nSrc>0) ){
 88458     struct SrcList_item *pItem = &p->a[p->nSrc-1];
 88459     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
 88460     if( pIndexedBy->n==1 && !pIndexedBy->z ){
 88461       /* A "NOT INDEXED" clause was supplied. See parse.y 
 88462       ** construct "indexed_opt" for details. */
 88463       pItem->notIndexed = 1;
 88464     }else{
 88465       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
 88470 /*
 88471 ** When building up a FROM clause in the parser, the join operator
 88472 ** is initially attached to the left operand.  But the code generator
 88473 ** expects the join operator to be on the right operand.  This routine
 88474 ** Shifts all join operators from left to right for an entire FROM
 88475 ** clause.
 88476 **
 88477 ** Example: Suppose the join is like this:
 88478 **
 88479 **           A natural cross join B
 88480 **
 88481 ** The operator is "natural cross join".  The A and B operands are stored
 88482 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
 88483 ** operator with A.  This routine shifts that operator over to B.
 88484 */
 88485 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
 88486   if( p ){
 88487     int i;
 88488     assert( p->a || p->nSrc==0 );
 88489     for(i=p->nSrc-1; i>0; i--){
 88490       p->a[i].jointype = p->a[i-1].jointype;
 88492     p->a[0].jointype = 0;
 88496 /*
 88497 ** Begin a transaction
 88498 */
 88499 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
 88500   sqlite3 *db;
 88501   Vdbe *v;
 88502   int i;
 88504   assert( pParse!=0 );
 88505   db = pParse->db;
 88506   assert( db!=0 );
 88507 /*  if( db->aDb[0].pBt==0 ) return; */
 88508   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
 88509     return;
 88511   v = sqlite3GetVdbe(pParse);
 88512   if( !v ) return;
 88513   if( type!=TK_DEFERRED ){
 88514     for(i=0; i<db->nDb; i++){
 88515       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
 88516       sqlite3VdbeUsesBtree(v, i);
 88519   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
 88522 /*
 88523 ** Commit a transaction
 88524 */
 88525 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
 88526   Vdbe *v;
 88528   assert( pParse!=0 );
 88529   assert( pParse->db!=0 );
 88530   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
 88531     return;
 88533   v = sqlite3GetVdbe(pParse);
 88534   if( v ){
 88535     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
 88539 /*
 88540 ** Rollback a transaction
 88541 */
 88542 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
 88543   Vdbe *v;
 88545   assert( pParse!=0 );
 88546   assert( pParse->db!=0 );
 88547   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
 88548     return;
 88550   v = sqlite3GetVdbe(pParse);
 88551   if( v ){
 88552     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
 88556 /*
 88557 ** This function is called by the parser when it parses a command to create,
 88558 ** release or rollback an SQL savepoint. 
 88559 */
 88560 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
 88561   char *zName = sqlite3NameFromToken(pParse->db, pName);
 88562   if( zName ){
 88563     Vdbe *v = sqlite3GetVdbe(pParse);
 88564 #ifndef SQLITE_OMIT_AUTHORIZATION
 88565     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
 88566     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
 88567 #endif
 88568     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
 88569       sqlite3DbFree(pParse->db, zName);
 88570       return;
 88572     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
 88576 /*
 88577 ** Make sure the TEMP database is open and available for use.  Return
 88578 ** the number of errors.  Leave any error messages in the pParse structure.
 88579 */
 88580 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
 88581   sqlite3 *db = pParse->db;
 88582   if( db->aDb[1].pBt==0 && !pParse->explain ){
 88583     int rc;
 88584     Btree *pBt;
 88585     static const int flags = 
 88586           SQLITE_OPEN_READWRITE |
 88587           SQLITE_OPEN_CREATE |
 88588           SQLITE_OPEN_EXCLUSIVE |
 88589           SQLITE_OPEN_DELETEONCLOSE |
 88590           SQLITE_OPEN_TEMP_DB;
 88592     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
 88593     if( rc!=SQLITE_OK ){
 88594       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
 88595         "file for storing temporary tables");
 88596       pParse->rc = rc;
 88597       return 1;
 88599     db->aDb[1].pBt = pBt;
 88600     assert( db->aDb[1].pSchema );
 88601     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
 88602       db->mallocFailed = 1;
 88603       return 1;
 88606   return 0;
 88609 /*
 88610 ** Record the fact that the schema cookie will need to be verified
 88611 ** for database iDb.  The code to actually verify the schema cookie
 88612 ** will occur at the end of the top-level VDBE and will be generated
 88613 ** later, by sqlite3FinishCoding().
 88614 */
 88615 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
 88616   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 88617   sqlite3 *db = pToplevel->db;
 88618   yDbMask mask;
 88620   assert( iDb>=0 && iDb<db->nDb );
 88621   assert( db->aDb[iDb].pBt!=0 || iDb==1 );
 88622   assert( iDb<SQLITE_MAX_ATTACHED+2 );
 88623   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 88624   mask = ((yDbMask)1)<<iDb;
 88625   if( (pToplevel->cookieMask & mask)==0 ){
 88626     pToplevel->cookieMask |= mask;
 88627     pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
 88628     if( !OMIT_TEMPDB && iDb==1 ){
 88629       sqlite3OpenTempDatabase(pToplevel);
 88634 /*
 88635 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
 88636 ** attached database. Otherwise, invoke it for the database named zDb only.
 88637 */
 88638 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
 88639   sqlite3 *db = pParse->db;
 88640   int i;
 88641   for(i=0; i<db->nDb; i++){
 88642     Db *pDb = &db->aDb[i];
 88643     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
 88644       sqlite3CodeVerifySchema(pParse, i);
 88649 /*
 88650 ** Generate VDBE code that prepares for doing an operation that
 88651 ** might change the database.
 88652 **
 88653 ** This routine starts a new transaction if we are not already within
 88654 ** a transaction.  If we are already within a transaction, then a checkpoint
 88655 ** is set if the setStatement parameter is true.  A checkpoint should
 88656 ** be set for operations that might fail (due to a constraint) part of
 88657 ** the way through and which will need to undo some writes without having to
 88658 ** rollback the whole transaction.  For operations where all constraints
 88659 ** can be checked before any changes are made to the database, it is never
 88660 ** necessary to undo a write and the checkpoint should not be set.
 88661 */
 88662 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
 88663   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 88664   sqlite3CodeVerifySchema(pParse, iDb);
 88665   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
 88666   pToplevel->isMultiWrite |= setStatement;
 88669 /*
 88670 ** Indicate that the statement currently under construction might write
 88671 ** more than one entry (example: deleting one row then inserting another,
 88672 ** inserting multiple rows in a table, or inserting a row and index entries.)
 88673 ** If an abort occurs after some of these writes have completed, then it will
 88674 ** be necessary to undo the completed writes.
 88675 */
 88676 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
 88677   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 88678   pToplevel->isMultiWrite = 1;
 88681 /* 
 88682 ** The code generator calls this routine if is discovers that it is
 88683 ** possible to abort a statement prior to completion.  In order to 
 88684 ** perform this abort without corrupting the database, we need to make
 88685 ** sure that the statement is protected by a statement transaction.
 88686 **
 88687 ** Technically, we only need to set the mayAbort flag if the
 88688 ** isMultiWrite flag was previously set.  There is a time dependency
 88689 ** such that the abort must occur after the multiwrite.  This makes
 88690 ** some statements involving the REPLACE conflict resolution algorithm
 88691 ** go a little faster.  But taking advantage of this time dependency
 88692 ** makes it more difficult to prove that the code is correct (in 
 88693 ** particular, it prevents us from writing an effective
 88694 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
 88695 ** to take the safe route and skip the optimization.
 88696 */
 88697 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
 88698   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 88699   pToplevel->mayAbort = 1;
 88702 /*
 88703 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
 88704 ** error. The onError parameter determines which (if any) of the statement
 88705 ** and/or current transaction is rolled back.
 88706 */
 88707 SQLITE_PRIVATE void sqlite3HaltConstraint(
 88708   Parse *pParse,    /* Parsing context */
 88709   int errCode,      /* extended error code */
 88710   int onError,      /* Constraint type */
 88711   char *p4,         /* Error message */
 88712   i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
 88713   u8 p5Errmsg       /* P5_ErrMsg type */
 88714 ){
 88715   Vdbe *v = sqlite3GetVdbe(pParse);
 88716   assert( (errCode&0xff)==SQLITE_CONSTRAINT );
 88717   if( onError==OE_Abort ){
 88718     sqlite3MayAbort(pParse);
 88720   sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
 88721   if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
 88724 /*
 88725 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
 88726 */
 88727 SQLITE_PRIVATE void sqlite3UniqueConstraint(
 88728   Parse *pParse,    /* Parsing context */
 88729   int onError,      /* Constraint type */
 88730   Index *pIdx       /* The index that triggers the constraint */
 88731 ){
 88732   char *zErr;
 88733   int j;
 88734   StrAccum errMsg;
 88735   Table *pTab = pIdx->pTable;
 88737   sqlite3StrAccumInit(&errMsg, 0, 0, 200);
 88738   errMsg.db = pParse->db;
 88739   for(j=0; j<pIdx->nKeyCol; j++){
 88740     char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
 88741     if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
 88742     sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
 88743     sqlite3StrAccumAppend(&errMsg, ".", 1);
 88744     sqlite3StrAccumAppendAll(&errMsg, zCol);
 88746   zErr = sqlite3StrAccumFinish(&errMsg);
 88747   sqlite3HaltConstraint(pParse, 
 88748     (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
 88749     onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
 88753 /*
 88754 ** Code an OP_Halt due to non-unique rowid.
 88755 */
 88756 SQLITE_PRIVATE void sqlite3RowidConstraint(
 88757   Parse *pParse,    /* Parsing context */
 88758   int onError,      /* Conflict resolution algorithm */
 88759   Table *pTab       /* The table with the non-unique rowid */ 
 88760 ){
 88761   char *zMsg;
 88762   int rc;
 88763   if( pTab->iPKey>=0 ){
 88764     zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
 88765                           pTab->aCol[pTab->iPKey].zName);
 88766     rc = SQLITE_CONSTRAINT_PRIMARYKEY;
 88767   }else{
 88768     zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
 88769     rc = SQLITE_CONSTRAINT_ROWID;
 88771   sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
 88772                         P5_ConstraintUnique);
 88775 /*
 88776 ** Check to see if pIndex uses the collating sequence pColl.  Return
 88777 ** true if it does and false if it does not.
 88778 */
 88779 #ifndef SQLITE_OMIT_REINDEX
 88780 static int collationMatch(const char *zColl, Index *pIndex){
 88781   int i;
 88782   assert( zColl!=0 );
 88783   for(i=0; i<pIndex->nColumn; i++){
 88784     const char *z = pIndex->azColl[i];
 88785     assert( z!=0 || pIndex->aiColumn[i]<0 );
 88786     if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
 88787       return 1;
 88790   return 0;
 88792 #endif
 88794 /*
 88795 ** Recompute all indices of pTab that use the collating sequence pColl.
 88796 ** If pColl==0 then recompute all indices of pTab.
 88797 */
 88798 #ifndef SQLITE_OMIT_REINDEX
 88799 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
 88800   Index *pIndex;              /* An index associated with pTab */
 88802   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 88803     if( zColl==0 || collationMatch(zColl, pIndex) ){
 88804       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 88805       sqlite3BeginWriteOperation(pParse, 0, iDb);
 88806       sqlite3RefillIndex(pParse, pIndex, -1);
 88810 #endif
 88812 /*
 88813 ** Recompute all indices of all tables in all databases where the
 88814 ** indices use the collating sequence pColl.  If pColl==0 then recompute
 88815 ** all indices everywhere.
 88816 */
 88817 #ifndef SQLITE_OMIT_REINDEX
 88818 static void reindexDatabases(Parse *pParse, char const *zColl){
 88819   Db *pDb;                    /* A single database */
 88820   int iDb;                    /* The database index number */
 88821   sqlite3 *db = pParse->db;   /* The database connection */
 88822   HashElem *k;                /* For looping over tables in pDb */
 88823   Table *pTab;                /* A table in the database */
 88825   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
 88826   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
 88827     assert( pDb!=0 );
 88828     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
 88829       pTab = (Table*)sqliteHashData(k);
 88830       reindexTable(pParse, pTab, zColl);
 88834 #endif
 88836 /*
 88837 ** Generate code for the REINDEX command.
 88838 **
 88839 **        REINDEX                            -- 1
 88840 **        REINDEX  <collation>               -- 2
 88841 **        REINDEX  ?<database>.?<tablename>  -- 3
 88842 **        REINDEX  ?<database>.?<indexname>  -- 4
 88843 **
 88844 ** Form 1 causes all indices in all attached databases to be rebuilt.
 88845 ** Form 2 rebuilds all indices in all databases that use the named
 88846 ** collating function.  Forms 3 and 4 rebuild the named index or all
 88847 ** indices associated with the named table.
 88848 */
 88849 #ifndef SQLITE_OMIT_REINDEX
 88850 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
 88851   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
 88852   char *z;                    /* Name of a table or index */
 88853   const char *zDb;            /* Name of the database */
 88854   Table *pTab;                /* A table in the database */
 88855   Index *pIndex;              /* An index associated with pTab */
 88856   int iDb;                    /* The database index number */
 88857   sqlite3 *db = pParse->db;   /* The database connection */
 88858   Token *pObjName;            /* Name of the table or index to be reindexed */
 88860   /* Read the database schema. If an error occurs, leave an error message
 88861   ** and code in pParse and return NULL. */
 88862   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 88863     return;
 88866   if( pName1==0 ){
 88867     reindexDatabases(pParse, 0);
 88868     return;
 88869   }else if( NEVER(pName2==0) || pName2->z==0 ){
 88870     char *zColl;
 88871     assert( pName1->z );
 88872     zColl = sqlite3NameFromToken(pParse->db, pName1);
 88873     if( !zColl ) return;
 88874     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
 88875     if( pColl ){
 88876       reindexDatabases(pParse, zColl);
 88877       sqlite3DbFree(db, zColl);
 88878       return;
 88880     sqlite3DbFree(db, zColl);
 88882   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
 88883   if( iDb<0 ) return;
 88884   z = sqlite3NameFromToken(db, pObjName);
 88885   if( z==0 ) return;
 88886   zDb = db->aDb[iDb].zName;
 88887   pTab = sqlite3FindTable(db, z, zDb);
 88888   if( pTab ){
 88889     reindexTable(pParse, pTab, 0);
 88890     sqlite3DbFree(db, z);
 88891     return;
 88893   pIndex = sqlite3FindIndex(db, z, zDb);
 88894   sqlite3DbFree(db, z);
 88895   if( pIndex ){
 88896     sqlite3BeginWriteOperation(pParse, 0, iDb);
 88897     sqlite3RefillIndex(pParse, pIndex, -1);
 88898     return;
 88900   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
 88902 #endif
 88904 /*
 88905 ** Return a KeyInfo structure that is appropriate for the given Index.
 88906 **
 88907 ** The KeyInfo structure for an index is cached in the Index object.
 88908 ** So there might be multiple references to the returned pointer.  The
 88909 ** caller should not try to modify the KeyInfo object.
 88910 **
 88911 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
 88912 ** when it has finished using it.
 88913 */
 88914 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
 88915   if( pParse->nErr ) return 0;
 88916 #ifndef SQLITE_OMIT_SHARED_CACHE
 88917   if( pIdx->pKeyInfo && pIdx->pKeyInfo->db!=pParse->db ){
 88918     sqlite3KeyInfoUnref(pIdx->pKeyInfo);
 88919     pIdx->pKeyInfo = 0;
 88921 #endif
 88922   if( pIdx->pKeyInfo==0 ){
 88923     int i;
 88924     int nCol = pIdx->nColumn;
 88925     int nKey = pIdx->nKeyCol;
 88926     KeyInfo *pKey;
 88927     if( pIdx->uniqNotNull ){
 88928       pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
 88929     }else{
 88930       pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
 88932     if( pKey ){
 88933       assert( sqlite3KeyInfoIsWriteable(pKey) );
 88934       for(i=0; i<nCol; i++){
 88935         char *zColl = pIdx->azColl[i];
 88936         assert( zColl!=0 );
 88937         pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
 88938                           sqlite3LocateCollSeq(pParse, zColl);
 88939         pKey->aSortOrder[i] = pIdx->aSortOrder[i];
 88941       if( pParse->nErr ){
 88942         sqlite3KeyInfoUnref(pKey);
 88943       }else{
 88944         pIdx->pKeyInfo = pKey;
 88948   return sqlite3KeyInfoRef(pIdx->pKeyInfo);
 88951 #ifndef SQLITE_OMIT_CTE
 88952 /* 
 88953 ** This routine is invoked once per CTE by the parser while parsing a 
 88954 ** WITH clause. 
 88955 */
 88956 SQLITE_PRIVATE With *sqlite3WithAdd(
 88957   Parse *pParse,          /* Parsing context */
 88958   With *pWith,            /* Existing WITH clause, or NULL */
 88959   Token *pName,           /* Name of the common-table */
 88960   ExprList *pArglist,     /* Optional column name list for the table */
 88961   Select *pQuery          /* Query used to initialize the table */
 88962 ){
 88963   sqlite3 *db = pParse->db;
 88964   With *pNew;
 88965   char *zName;
 88967   /* Check that the CTE name is unique within this WITH clause. If
 88968   ** not, store an error in the Parse structure. */
 88969   zName = sqlite3NameFromToken(pParse->db, pName);
 88970   if( zName && pWith ){
 88971     int i;
 88972     for(i=0; i<pWith->nCte; i++){
 88973       if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
 88974         sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
 88979   if( pWith ){
 88980     int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
 88981     pNew = sqlite3DbRealloc(db, pWith, nByte);
 88982   }else{
 88983     pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
 88985   assert( zName!=0 || pNew==0 );
 88986   assert( db->mallocFailed==0 || pNew==0 );
 88988   if( pNew==0 ){
 88989     sqlite3ExprListDelete(db, pArglist);
 88990     sqlite3SelectDelete(db, pQuery);
 88991     sqlite3DbFree(db, zName);
 88992     pNew = pWith;
 88993   }else{
 88994     pNew->a[pNew->nCte].pSelect = pQuery;
 88995     pNew->a[pNew->nCte].pCols = pArglist;
 88996     pNew->a[pNew->nCte].zName = zName;
 88997     pNew->a[pNew->nCte].zErr = 0;
 88998     pNew->nCte++;
 89001   return pNew;
 89004 /*
 89005 ** Free the contents of the With object passed as the second argument.
 89006 */
 89007 SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
 89008   if( pWith ){
 89009     int i;
 89010     for(i=0; i<pWith->nCte; i++){
 89011       struct Cte *pCte = &pWith->a[i];
 89012       sqlite3ExprListDelete(db, pCte->pCols);
 89013       sqlite3SelectDelete(db, pCte->pSelect);
 89014       sqlite3DbFree(db, pCte->zName);
 89016     sqlite3DbFree(db, pWith);
 89019 #endif /* !defined(SQLITE_OMIT_CTE) */
 89021 /************** End of build.c ***********************************************/
 89022 /************** Begin file callback.c ****************************************/
 89023 /*
 89024 ** 2005 May 23 
 89025 **
 89026 ** The author disclaims copyright to this source code.  In place of
 89027 ** a legal notice, here is a blessing:
 89028 **
 89029 **    May you do good and not evil.
 89030 **    May you find forgiveness for yourself and forgive others.
 89031 **    May you share freely, never taking more than you give.
 89032 **
 89033 *************************************************************************
 89034 **
 89035 ** This file contains functions used to access the internal hash tables
 89036 ** of user defined functions and collation sequences.
 89037 */
 89040 /*
 89041 ** Invoke the 'collation needed' callback to request a collation sequence
 89042 ** in the encoding enc of name zName, length nName.
 89043 */
 89044 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
 89045   assert( !db->xCollNeeded || !db->xCollNeeded16 );
 89046   if( db->xCollNeeded ){
 89047     char *zExternal = sqlite3DbStrDup(db, zName);
 89048     if( !zExternal ) return;
 89049     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
 89050     sqlite3DbFree(db, zExternal);
 89052 #ifndef SQLITE_OMIT_UTF16
 89053   if( db->xCollNeeded16 ){
 89054     char const *zExternal;
 89055     sqlite3_value *pTmp = sqlite3ValueNew(db);
 89056     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
 89057     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
 89058     if( zExternal ){
 89059       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
 89061     sqlite3ValueFree(pTmp);
 89063 #endif
 89066 /*
 89067 ** This routine is called if the collation factory fails to deliver a
 89068 ** collation function in the best encoding but there may be other versions
 89069 ** of this collation function (for other text encodings) available. Use one
 89070 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
 89071 ** possible.
 89072 */
 89073 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
 89074   CollSeq *pColl2;
 89075   char *z = pColl->zName;
 89076   int i;
 89077   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
 89078   for(i=0; i<3; i++){
 89079     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
 89080     if( pColl2->xCmp!=0 ){
 89081       memcpy(pColl, pColl2, sizeof(CollSeq));
 89082       pColl->xDel = 0;         /* Do not copy the destructor */
 89083       return SQLITE_OK;
 89086   return SQLITE_ERROR;
 89089 /*
 89090 ** This function is responsible for invoking the collation factory callback
 89091 ** or substituting a collation sequence of a different encoding when the
 89092 ** requested collation sequence is not available in the desired encoding.
 89093 ** 
 89094 ** If it is not NULL, then pColl must point to the database native encoding 
 89095 ** collation sequence with name zName, length nName.
 89096 **
 89097 ** The return value is either the collation sequence to be used in database
 89098 ** db for collation type name zName, length nName, or NULL, if no collation
 89099 ** sequence can be found.  If no collation is found, leave an error message.
 89100 **
 89101 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
 89102 */
 89103 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
 89104   Parse *pParse,        /* Parsing context */
 89105   u8 enc,               /* The desired encoding for the collating sequence */
 89106   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
 89107   const char *zName     /* Collating sequence name */
 89108 ){
 89109   CollSeq *p;
 89110   sqlite3 *db = pParse->db;
 89112   p = pColl;
 89113   if( !p ){
 89114     p = sqlite3FindCollSeq(db, enc, zName, 0);
 89116   if( !p || !p->xCmp ){
 89117     /* No collation sequence of this type for this encoding is registered.
 89118     ** Call the collation factory to see if it can supply us with one.
 89119     */
 89120     callCollNeeded(db, enc, zName);
 89121     p = sqlite3FindCollSeq(db, enc, zName, 0);
 89123   if( p && !p->xCmp && synthCollSeq(db, p) ){
 89124     p = 0;
 89126   assert( !p || p->xCmp );
 89127   if( p==0 ){
 89128     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
 89130   return p;
 89133 /*
 89134 ** This routine is called on a collation sequence before it is used to
 89135 ** check that it is defined. An undefined collation sequence exists when
 89136 ** a database is loaded that contains references to collation sequences
 89137 ** that have not been defined by sqlite3_create_collation() etc.
 89138 **
 89139 ** If required, this routine calls the 'collation needed' callback to
 89140 ** request a definition of the collating sequence. If this doesn't work, 
 89141 ** an equivalent collating sequence that uses a text encoding different
 89142 ** from the main database is substituted, if one is available.
 89143 */
 89144 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
 89145   if( pColl ){
 89146     const char *zName = pColl->zName;
 89147     sqlite3 *db = pParse->db;
 89148     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
 89149     if( !p ){
 89150       return SQLITE_ERROR;
 89152     assert( p==pColl );
 89154   return SQLITE_OK;
 89159 /*
 89160 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
 89161 ** specified by zName and nName is not found and parameter 'create' is
 89162 ** true, then create a new entry. Otherwise return NULL.
 89163 **
 89164 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
 89165 ** array of three CollSeq structures. The first is the collation sequence
 89166 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
 89167 **
 89168 ** Stored immediately after the three collation sequences is a copy of
 89169 ** the collation sequence name. A pointer to this string is stored in
 89170 ** each collation sequence structure.
 89171 */
 89172 static CollSeq *findCollSeqEntry(
 89173   sqlite3 *db,          /* Database connection */
 89174   const char *zName,    /* Name of the collating sequence */
 89175   int create            /* Create a new entry if true */
 89176 ){
 89177   CollSeq *pColl;
 89178   int nName = sqlite3Strlen30(zName);
 89179   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 89181   if( 0==pColl && create ){
 89182     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
 89183     if( pColl ){
 89184       CollSeq *pDel = 0;
 89185       pColl[0].zName = (char*)&pColl[3];
 89186       pColl[0].enc = SQLITE_UTF8;
 89187       pColl[1].zName = (char*)&pColl[3];
 89188       pColl[1].enc = SQLITE_UTF16LE;
 89189       pColl[2].zName = (char*)&pColl[3];
 89190       pColl[2].enc = SQLITE_UTF16BE;
 89191       memcpy(pColl[0].zName, zName, nName);
 89192       pColl[0].zName[nName] = 0;
 89193       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
 89195       /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
 89196       ** return the pColl pointer to be deleted (because it wasn't added
 89197       ** to the hash table).
 89198       */
 89199       assert( pDel==0 || pDel==pColl );
 89200       if( pDel!=0 ){
 89201         db->mallocFailed = 1;
 89202         sqlite3DbFree(db, pDel);
 89203         pColl = 0;
 89207   return pColl;
 89210 /*
 89211 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
 89212 ** Return the CollSeq* pointer for the collation sequence named zName
 89213 ** for the encoding 'enc' from the database 'db'.
 89214 **
 89215 ** If the entry specified is not found and 'create' is true, then create a
 89216 ** new entry.  Otherwise return NULL.
 89217 **
 89218 ** A separate function sqlite3LocateCollSeq() is a wrapper around
 89219 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
 89220 ** if necessary and generates an error message if the collating sequence
 89221 ** cannot be found.
 89222 **
 89223 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
 89224 */
 89225 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
 89226   sqlite3 *db,
 89227   u8 enc,
 89228   const char *zName,
 89229   int create
 89230 ){
 89231   CollSeq *pColl;
 89232   if( zName ){
 89233     pColl = findCollSeqEntry(db, zName, create);
 89234   }else{
 89235     pColl = db->pDfltColl;
 89237   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
 89238   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
 89239   if( pColl ) pColl += enc-1;
 89240   return pColl;
 89243 /* During the search for the best function definition, this procedure
 89244 ** is called to test how well the function passed as the first argument
 89245 ** matches the request for a function with nArg arguments in a system
 89246 ** that uses encoding enc. The value returned indicates how well the
 89247 ** request is matched. A higher value indicates a better match.
 89248 **
 89249 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
 89250 ** is also -1.  In other words, we are searching for a function that
 89251 ** takes a variable number of arguments.
 89252 **
 89253 ** If nArg is -2 that means that we are searching for any function 
 89254 ** regardless of the number of arguments it uses, so return a positive
 89255 ** match score for any
 89256 **
 89257 ** The returned value is always between 0 and 6, as follows:
 89258 **
 89259 ** 0: Not a match.
 89260 ** 1: UTF8/16 conversion required and function takes any number of arguments.
 89261 ** 2: UTF16 byte order change required and function takes any number of args.
 89262 ** 3: encoding matches and function takes any number of arguments
 89263 ** 4: UTF8/16 conversion required - argument count matches exactly
 89264 ** 5: UTF16 byte order conversion required - argument count matches exactly
 89265 ** 6: Perfect match:  encoding and argument count match exactly.
 89266 **
 89267 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
 89268 ** a perfect match and any function with both xStep and xFunc NULL is
 89269 ** a non-match.
 89270 */
 89271 #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
 89272 static int matchQuality(
 89273   FuncDef *p,     /* The function we are evaluating for match quality */
 89274   int nArg,       /* Desired number of arguments.  (-1)==any */
 89275   u8 enc          /* Desired text encoding */
 89276 ){
 89277   int match;
 89279   /* nArg of -2 is a special case */
 89280   if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
 89282   /* Wrong number of arguments means "no match" */
 89283   if( p->nArg!=nArg && p->nArg>=0 ) return 0;
 89285   /* Give a better score to a function with a specific number of arguments
 89286   ** than to function that accepts any number of arguments. */
 89287   if( p->nArg==nArg ){
 89288     match = 4;
 89289   }else{
 89290     match = 1;
 89293   /* Bonus points if the text encoding matches */
 89294   if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
 89295     match += 2;  /* Exact encoding match */
 89296   }else if( (enc & p->funcFlags & 2)!=0 ){
 89297     match += 1;  /* Both are UTF16, but with different byte orders */
 89300   return match;
 89303 /*
 89304 ** Search a FuncDefHash for a function with the given name.  Return
 89305 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
 89306 */
 89307 static FuncDef *functionSearch(
 89308   FuncDefHash *pHash,  /* Hash table to search */
 89309   int h,               /* Hash of the name */
 89310   const char *zFunc,   /* Name of function */
 89311   int nFunc            /* Number of bytes in zFunc */
 89312 ){
 89313   FuncDef *p;
 89314   for(p=pHash->a[h]; p; p=p->pHash){
 89315     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
 89316       return p;
 89319   return 0;
 89322 /*
 89323 ** Insert a new FuncDef into a FuncDefHash hash table.
 89324 */
 89325 SQLITE_PRIVATE void sqlite3FuncDefInsert(
 89326   FuncDefHash *pHash,  /* The hash table into which to insert */
 89327   FuncDef *pDef        /* The function definition to insert */
 89328 ){
 89329   FuncDef *pOther;
 89330   int nName = sqlite3Strlen30(pDef->zName);
 89331   u8 c1 = (u8)pDef->zName[0];
 89332   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
 89333   pOther = functionSearch(pHash, h, pDef->zName, nName);
 89334   if( pOther ){
 89335     assert( pOther!=pDef && pOther->pNext!=pDef );
 89336     pDef->pNext = pOther->pNext;
 89337     pOther->pNext = pDef;
 89338   }else{
 89339     pDef->pNext = 0;
 89340     pDef->pHash = pHash->a[h];
 89341     pHash->a[h] = pDef;
 89347 /*
 89348 ** Locate a user function given a name, a number of arguments and a flag
 89349 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
 89350 ** pointer to the FuncDef structure that defines that function, or return
 89351 ** NULL if the function does not exist.
 89352 **
 89353 ** If the createFlag argument is true, then a new (blank) FuncDef
 89354 ** structure is created and liked into the "db" structure if a
 89355 ** no matching function previously existed.
 89356 **
 89357 ** If nArg is -2, then the first valid function found is returned.  A
 89358 ** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
 89359 ** case is used to see if zName is a valid function name for some number
 89360 ** of arguments.  If nArg is -2, then createFlag must be 0.
 89361 **
 89362 ** If createFlag is false, then a function with the required name and
 89363 ** number of arguments may be returned even if the eTextRep flag does not
 89364 ** match that requested.
 89365 */
 89366 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
 89367   sqlite3 *db,       /* An open database */
 89368   const char *zName, /* Name of the function.  Not null-terminated */
 89369   int nName,         /* Number of characters in the name */
 89370   int nArg,          /* Number of arguments.  -1 means any number */
 89371   u8 enc,            /* Preferred text encoding */
 89372   u8 createFlag      /* Create new entry if true and does not otherwise exist */
 89373 ){
 89374   FuncDef *p;         /* Iterator variable */
 89375   FuncDef *pBest = 0; /* Best match found so far */
 89376   int bestScore = 0;  /* Score of best match */
 89377   int h;              /* Hash value */
 89379   assert( nArg>=(-2) );
 89380   assert( nArg>=(-1) || createFlag==0 );
 89381   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
 89383   /* First search for a match amongst the application-defined functions.
 89384   */
 89385   p = functionSearch(&db->aFunc, h, zName, nName);
 89386   while( p ){
 89387     int score = matchQuality(p, nArg, enc);
 89388     if( score>bestScore ){
 89389       pBest = p;
 89390       bestScore = score;
 89392     p = p->pNext;
 89395   /* If no match is found, search the built-in functions.
 89396   **
 89397   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
 89398   ** functions even if a prior app-defined function was found.  And give
 89399   ** priority to built-in functions.
 89400   **
 89401   ** Except, if createFlag is true, that means that we are trying to
 89402   ** install a new function.  Whatever FuncDef structure is returned it will
 89403   ** have fields overwritten with new information appropriate for the
 89404   ** new function.  But the FuncDefs for built-in functions are read-only.
 89405   ** So we must not search for built-ins when creating a new function.
 89406   */ 
 89407   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
 89408     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 89409     bestScore = 0;
 89410     p = functionSearch(pHash, h, zName, nName);
 89411     while( p ){
 89412       int score = matchQuality(p, nArg, enc);
 89413       if( score>bestScore ){
 89414         pBest = p;
 89415         bestScore = score;
 89417       p = p->pNext;
 89421   /* If the createFlag parameter is true and the search did not reveal an
 89422   ** exact match for the name, number of arguments and encoding, then add a
 89423   ** new entry to the hash table and return it.
 89424   */
 89425   if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
 89426       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
 89427     pBest->zName = (char *)&pBest[1];
 89428     pBest->nArg = (u16)nArg;
 89429     pBest->funcFlags = enc;
 89430     memcpy(pBest->zName, zName, nName);
 89431     pBest->zName[nName] = 0;
 89432     sqlite3FuncDefInsert(&db->aFunc, pBest);
 89435   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
 89436     return pBest;
 89438   return 0;
 89441 /*
 89442 ** Free all resources held by the schema structure. The void* argument points
 89443 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
 89444 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
 89445 ** of the schema hash tables).
 89446 **
 89447 ** The Schema.cache_size variable is not cleared.
 89448 */
 89449 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
 89450   Hash temp1;
 89451   Hash temp2;
 89452   HashElem *pElem;
 89453   Schema *pSchema = (Schema *)p;
 89455   temp1 = pSchema->tblHash;
 89456   temp2 = pSchema->trigHash;
 89457   sqlite3HashInit(&pSchema->trigHash);
 89458   sqlite3HashClear(&pSchema->idxHash);
 89459   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
 89460     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
 89462   sqlite3HashClear(&temp2);
 89463   sqlite3HashInit(&pSchema->tblHash);
 89464   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
 89465     Table *pTab = sqliteHashData(pElem);
 89466     sqlite3DeleteTable(0, pTab);
 89468   sqlite3HashClear(&temp1);
 89469   sqlite3HashClear(&pSchema->fkeyHash);
 89470   pSchema->pSeqTab = 0;
 89471   if( pSchema->flags & DB_SchemaLoaded ){
 89472     pSchema->iGeneration++;
 89473     pSchema->flags &= ~DB_SchemaLoaded;
 89477 /*
 89478 ** Find and return the schema associated with a BTree.  Create
 89479 ** a new one if necessary.
 89480 */
 89481 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
 89482   Schema * p;
 89483   if( pBt ){
 89484     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
 89485   }else{
 89486     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
 89488   if( !p ){
 89489     db->mallocFailed = 1;
 89490   }else if ( 0==p->file_format ){
 89491     sqlite3HashInit(&p->tblHash);
 89492     sqlite3HashInit(&p->idxHash);
 89493     sqlite3HashInit(&p->trigHash);
 89494     sqlite3HashInit(&p->fkeyHash);
 89495     p->enc = SQLITE_UTF8;
 89497   return p;
 89500 /************** End of callback.c ********************************************/
 89501 /************** Begin file delete.c ******************************************/
 89502 /*
 89503 ** 2001 September 15
 89504 **
 89505 ** The author disclaims copyright to this source code.  In place of
 89506 ** a legal notice, here is a blessing:
 89507 **
 89508 **    May you do good and not evil.
 89509 **    May you find forgiveness for yourself and forgive others.
 89510 **    May you share freely, never taking more than you give.
 89511 **
 89512 *************************************************************************
 89513 ** This file contains C code routines that are called by the parser
 89514 ** in order to generate code for DELETE FROM statements.
 89515 */
 89517 /*
 89518 ** While a SrcList can in general represent multiple tables and subqueries
 89519 ** (as in the FROM clause of a SELECT statement) in this case it contains
 89520 ** the name of a single table, as one might find in an INSERT, DELETE,
 89521 ** or UPDATE statement.  Look up that table in the symbol table and
 89522 ** return a pointer.  Set an error message and return NULL if the table 
 89523 ** name is not found or if any other error occurs.
 89524 **
 89525 ** The following fields are initialized appropriate in pSrc:
 89526 **
 89527 **    pSrc->a[0].pTab       Pointer to the Table object
 89528 **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
 89529 **
 89530 */
 89531 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
 89532   struct SrcList_item *pItem = pSrc->a;
 89533   Table *pTab;
 89534   assert( pItem && pSrc->nSrc==1 );
 89535   pTab = sqlite3LocateTableItem(pParse, 0, pItem);
 89536   sqlite3DeleteTable(pParse->db, pItem->pTab);
 89537   pItem->pTab = pTab;
 89538   if( pTab ){
 89539     pTab->nRef++;
 89541   if( sqlite3IndexedByLookup(pParse, pItem) ){
 89542     pTab = 0;
 89544   return pTab;
 89547 /*
 89548 ** Check to make sure the given table is writable.  If it is not
 89549 ** writable, generate an error message and return 1.  If it is
 89550 ** writable return 0;
 89551 */
 89552 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
 89553   /* A table is not writable under the following circumstances:
 89554   **
 89555   **   1) It is a virtual table and no implementation of the xUpdate method
 89556   **      has been provided, or
 89557   **   2) It is a system table (i.e. sqlite_master), this call is not
 89558   **      part of a nested parse and writable_schema pragma has not 
 89559   **      been specified.
 89560   **
 89561   ** In either case leave an error message in pParse and return non-zero.
 89562   */
 89563   if( ( IsVirtual(pTab) 
 89564      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
 89565    || ( (pTab->tabFlags & TF_Readonly)!=0
 89566      && (pParse->db->flags & SQLITE_WriteSchema)==0
 89567      && pParse->nested==0 )
 89568   ){
 89569     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
 89570     return 1;
 89573 #ifndef SQLITE_OMIT_VIEW
 89574   if( !viewOk && pTab->pSelect ){
 89575     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
 89576     return 1;
 89578 #endif
 89579   return 0;
 89583 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 89584 /*
 89585 ** Evaluate a view and store its result in an ephemeral table.  The
 89586 ** pWhere argument is an optional WHERE clause that restricts the
 89587 ** set of rows in the view that are to be added to the ephemeral table.
 89588 */
 89589 SQLITE_PRIVATE void sqlite3MaterializeView(
 89590   Parse *pParse,       /* Parsing context */
 89591   Table *pView,        /* View definition */
 89592   Expr *pWhere,        /* Optional WHERE clause to be added */
 89593   int iCur             /* Cursor number for ephemerial table */
 89594 ){
 89595   SelectDest dest;
 89596   Select *pSel;
 89597   SrcList *pFrom;
 89598   sqlite3 *db = pParse->db;
 89599   int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
 89600   pWhere = sqlite3ExprDup(db, pWhere, 0);
 89601   pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
 89602   if( pFrom ){
 89603     assert( pFrom->nSrc==1 );
 89604     pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
 89605     pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
 89606     assert( pFrom->a[0].pOn==0 );
 89607     assert( pFrom->a[0].pUsing==0 );
 89609   pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
 89610   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
 89611   sqlite3Select(pParse, pSel, &dest);
 89612   sqlite3SelectDelete(db, pSel);
 89614 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
 89616 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 89617 /*
 89618 ** Generate an expression tree to implement the WHERE, ORDER BY,
 89619 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
 89620 **
 89621 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
 89622 **                            \__________________________/
 89623 **                               pLimitWhere (pInClause)
 89624 */
 89625 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
 89626   Parse *pParse,               /* The parser context */
 89627   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
 89628   Expr *pWhere,                /* The WHERE clause.  May be null */
 89629   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
 89630   Expr *pLimit,                /* The LIMIT clause.  May be null */
 89631   Expr *pOffset,               /* The OFFSET clause.  May be null */
 89632   char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
 89633 ){
 89634   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
 89635   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
 89636   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
 89637   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
 89638   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
 89639   Select *pSelect = NULL;      /* Complete SELECT tree */
 89641   /* Check that there isn't an ORDER BY without a LIMIT clause.
 89642   */
 89643   if( pOrderBy && (pLimit == 0) ) {
 89644     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
 89645     goto limit_where_cleanup_2;
 89648   /* We only need to generate a select expression if there
 89649   ** is a limit/offset term to enforce.
 89650   */
 89651   if( pLimit == 0 ) {
 89652     /* if pLimit is null, pOffset will always be null as well. */
 89653     assert( pOffset == 0 );
 89654     return pWhere;
 89657   /* Generate a select expression tree to enforce the limit/offset 
 89658   ** term for the DELETE or UPDATE statement.  For example:
 89659   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 89660   ** becomes:
 89661   **   DELETE FROM table_a WHERE rowid IN ( 
 89662   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
 89663   **   );
 89664   */
 89666   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 89667   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
 89668   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
 89669   if( pEList == 0 ) goto limit_where_cleanup_2;
 89671   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
 89672   ** and the SELECT subtree. */
 89673   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
 89674   if( pSelectSrc == 0 ) {
 89675     sqlite3ExprListDelete(pParse->db, pEList);
 89676     goto limit_where_cleanup_2;
 89679   /* generate the SELECT expression tree. */
 89680   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
 89681                              pOrderBy,0,pLimit,pOffset);
 89682   if( pSelect == 0 ) return 0;
 89684   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
 89685   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
 89686   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
 89687   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
 89688   if( pInClause == 0 ) goto limit_where_cleanup_1;
 89690   pInClause->x.pSelect = pSelect;
 89691   pInClause->flags |= EP_xIsSelect;
 89692   sqlite3ExprSetHeight(pParse, pInClause);
 89693   return pInClause;
 89695   /* something went wrong. clean up anything allocated. */
 89696 limit_where_cleanup_1:
 89697   sqlite3SelectDelete(pParse->db, pSelect);
 89698   return 0;
 89700 limit_where_cleanup_2:
 89701   sqlite3ExprDelete(pParse->db, pWhere);
 89702   sqlite3ExprListDelete(pParse->db, pOrderBy);
 89703   sqlite3ExprDelete(pParse->db, pLimit);
 89704   sqlite3ExprDelete(pParse->db, pOffset);
 89705   return 0;
 89707 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
 89708        /*      && !defined(SQLITE_OMIT_SUBQUERY) */
 89710 /*
 89711 ** Generate code for a DELETE FROM statement.
 89712 **
 89713 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
 89714 **                 \________/       \________________/
 89715 **                  pTabList              pWhere
 89716 */
 89717 SQLITE_PRIVATE void sqlite3DeleteFrom(
 89718   Parse *pParse,         /* The parser context */
 89719   SrcList *pTabList,     /* The table from which we should delete things */
 89720   Expr *pWhere           /* The WHERE clause.  May be null */
 89721 ){
 89722   Vdbe *v;               /* The virtual database engine */
 89723   Table *pTab;           /* The table from which records will be deleted */
 89724   const char *zDb;       /* Name of database holding pTab */
 89725   int i;                 /* Loop counter */
 89726   WhereInfo *pWInfo;     /* Information about the WHERE clause */
 89727   Index *pIdx;           /* For looping over indices of the table */
 89728   int iTabCur;           /* Cursor number for the table */
 89729   int iDataCur;          /* VDBE cursor for the canonical data source */
 89730   int iIdxCur;           /* Cursor number of the first index */
 89731   int nIdx;              /* Number of indices */
 89732   sqlite3 *db;           /* Main database structure */
 89733   AuthContext sContext;  /* Authorization context */
 89734   NameContext sNC;       /* Name context to resolve expressions in */
 89735   int iDb;               /* Database number */
 89736   int memCnt = -1;       /* Memory cell used for change counting */
 89737   int rcauth;            /* Value returned by authorization callback */
 89738   int okOnePass;         /* True for one-pass algorithm without the FIFO */
 89739   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
 89740   u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
 89741   Index *pPk;            /* The PRIMARY KEY index on the table */
 89742   int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
 89743   i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
 89744   int iKey;              /* Memory cell holding key of row to be deleted */
 89745   i16 nKey;              /* Number of memory cells in the row key */
 89746   int iEphCur = 0;       /* Ephemeral table holding all primary key values */
 89747   int iRowSet = 0;       /* Register for rowset of rows to delete */
 89748   int addrBypass = 0;    /* Address of jump over the delete logic */
 89749   int addrLoop = 0;      /* Top of the delete loop */
 89750   int addrDelete = 0;    /* Jump directly to the delete logic */
 89751   int addrEphOpen = 0;   /* Instruction to open the Ephermeral table */
 89753 #ifndef SQLITE_OMIT_TRIGGER
 89754   int isView;                  /* True if attempting to delete from a view */
 89755   Trigger *pTrigger;           /* List of table triggers, if required */
 89756 #endif
 89758   memset(&sContext, 0, sizeof(sContext));
 89759   db = pParse->db;
 89760   if( pParse->nErr || db->mallocFailed ){
 89761     goto delete_from_cleanup;
 89763   assert( pTabList->nSrc==1 );
 89765   /* Locate the table which we want to delete.  This table has to be
 89766   ** put in an SrcList structure because some of the subroutines we
 89767   ** will be calling are designed to work with multiple tables and expect
 89768   ** an SrcList* parameter instead of just a Table* parameter.
 89769   */
 89770   pTab = sqlite3SrcListLookup(pParse, pTabList);
 89771   if( pTab==0 )  goto delete_from_cleanup;
 89773   /* Figure out if we have any triggers and if the table being
 89774   ** deleted from is a view
 89775   */
 89776 #ifndef SQLITE_OMIT_TRIGGER
 89777   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 89778   isView = pTab->pSelect!=0;
 89779 #else
 89780 # define pTrigger 0
 89781 # define isView 0
 89782 #endif
 89783 #ifdef SQLITE_OMIT_VIEW
 89784 # undef isView
 89785 # define isView 0
 89786 #endif
 89788   /* If pTab is really a view, make sure it has been initialized.
 89789   */
 89790   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 89791     goto delete_from_cleanup;
 89794   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
 89795     goto delete_from_cleanup;
 89797   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 89798   assert( iDb<db->nDb );
 89799   zDb = db->aDb[iDb].zName;
 89800   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
 89801   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
 89802   if( rcauth==SQLITE_DENY ){
 89803     goto delete_from_cleanup;
 89805   assert(!isView || pTrigger);
 89807   /* Assign cursor numbers to the table and all its indices.
 89808   */
 89809   assert( pTabList->nSrc==1 );
 89810   iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
 89811   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
 89812     pParse->nTab++;
 89815   /* Start the view context
 89816   */
 89817   if( isView ){
 89818     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 89821   /* Begin generating code.
 89822   */
 89823   v = sqlite3GetVdbe(pParse);
 89824   if( v==0 ){
 89825     goto delete_from_cleanup;
 89827   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 89828   sqlite3BeginWriteOperation(pParse, 1, iDb);
 89830   /* If we are trying to delete from a view, realize that view into
 89831   ** a ephemeral table.
 89832   */
 89833 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 89834   if( isView ){
 89835     sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
 89836     iDataCur = iIdxCur = iTabCur;
 89838 #endif
 89840   /* Resolve the column names in the WHERE clause.
 89841   */
 89842   memset(&sNC, 0, sizeof(sNC));
 89843   sNC.pParse = pParse;
 89844   sNC.pSrcList = pTabList;
 89845   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
 89846     goto delete_from_cleanup;
 89849   /* Initialize the counter of the number of rows deleted, if
 89850   ** we are counting rows.
 89851   */
 89852   if( db->flags & SQLITE_CountRows ){
 89853     memCnt = ++pParse->nMem;
 89854     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
 89857 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 89858   /* Special case: A DELETE without a WHERE clause deletes everything.
 89859   ** It is easier just to erase the whole table. Prior to version 3.6.5,
 89860   ** this optimization caused the row change count (the value returned by 
 89861   ** API function sqlite3_count_changes) to be set incorrectly.  */
 89862   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
 89863    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
 89864   ){
 89865     assert( !isView );
 89866     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
 89867     if( HasRowid(pTab) ){
 89868       sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
 89869                         pTab->zName, P4_STATIC);
 89871     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 89872       assert( pIdx->pSchema==pTab->pSchema );
 89873       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
 89875   }else
 89876 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
 89878     if( HasRowid(pTab) ){
 89879       /* For a rowid table, initialize the RowSet to an empty set */
 89880       pPk = 0;
 89881       nPk = 1;
 89882       iRowSet = ++pParse->nMem;
 89883       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
 89884     }else{
 89885       /* For a WITHOUT ROWID table, create an ephermeral table used to
 89886       ** hold all primary keys for rows to be deleted. */
 89887       pPk = sqlite3PrimaryKeyIndex(pTab);
 89888       assert( pPk!=0 );
 89889       nPk = pPk->nKeyCol;
 89890       iPk = pParse->nMem+1;
 89891       pParse->nMem += nPk;
 89892       iEphCur = pParse->nTab++;
 89893       addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
 89894       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
 89897     /* Construct a query to find the rowid or primary key for every row
 89898     ** to be deleted, based on the WHERE clause.
 89899     */
 89900     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
 89901                                WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
 89902                                iTabCur+1);
 89903     if( pWInfo==0 ) goto delete_from_cleanup;
 89904     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
 89906     /* Keep track of the number of rows to be deleted */
 89907     if( db->flags & SQLITE_CountRows ){
 89908       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
 89911     /* Extract the rowid or primary key for the current row */
 89912     if( pPk ){
 89913       for(i=0; i<nPk; i++){
 89914         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
 89915                                         pPk->aiColumn[i], iPk+i);
 89917       iKey = iPk;
 89918     }else{
 89919       iKey = pParse->nMem + 1;
 89920       iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
 89921       if( iKey>pParse->nMem ) pParse->nMem = iKey;
 89924     if( okOnePass ){
 89925       /* For ONEPASS, no need to store the rowid/primary-key.  There is only
 89926       ** one, so just keep it in its register(s) and fall through to the
 89927       ** delete code.
 89928       */
 89929       nKey = nPk; /* OP_Found will use an unpacked key */
 89930       aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
 89931       if( aToOpen==0 ){
 89932         sqlite3WhereEnd(pWInfo);
 89933         goto delete_from_cleanup;
 89935       memset(aToOpen, 1, nIdx+1);
 89936       aToOpen[nIdx+1] = 0;
 89937       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
 89938       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
 89939       if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
 89940       addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
 89941     }else if( pPk ){
 89942       /* Construct a composite key for the row to be deleted and remember it */
 89943       iKey = ++pParse->nMem;
 89944       nKey = 0;   /* Zero tells OP_Found to use a composite key */
 89945       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
 89946                         sqlite3IndexAffinityStr(v, pPk), nPk);
 89947       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
 89948     }else{
 89949       /* Get the rowid of the row to be deleted and remember it in the RowSet */
 89950       nKey = 1;  /* OP_Seek always uses a single rowid */
 89951       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
 89954     /* End of the WHERE loop */
 89955     sqlite3WhereEnd(pWInfo);
 89956     if( okOnePass ){
 89957       /* Bypass the delete logic below if the WHERE loop found zero rows */
 89958       addrBypass = sqlite3VdbeMakeLabel(v);
 89959       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
 89960       sqlite3VdbeJumpHere(v, addrDelete);
 89963     /* Unless this is a view, open cursors for the table we are 
 89964     ** deleting from and all its indices. If this is a view, then the
 89965     ** only effect this statement has is to fire the INSTEAD OF 
 89966     ** triggers.
 89967     */
 89968     if( !isView ){
 89969       sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
 89970                                  &iDataCur, &iIdxCur);
 89971       assert( pPk || iDataCur==iTabCur );
 89972       assert( pPk || iIdxCur==iDataCur+1 );
 89975     /* Set up a loop over the rowids/primary-keys that were found in the
 89976     ** where-clause loop above.
 89977     */
 89978     if( okOnePass ){
 89979       /* Just one row.  Hence the top-of-loop is a no-op */
 89980       assert( nKey==nPk ); /* OP_Found will use an unpacked key */
 89981       if( aToOpen[iDataCur-iTabCur] ){
 89982         assert( pPk!=0 );
 89983         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
 89984         VdbeCoverage(v);
 89986     }else if( pPk ){
 89987       addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
 89988       sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
 89989       assert( nKey==0 );  /* OP_Found will use a composite key */
 89990     }else{
 89991       addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
 89992       VdbeCoverage(v);
 89993       assert( nKey==1 );
 89996     /* Delete the row */
 89997 #ifndef SQLITE_OMIT_VIRTUALTABLE
 89998     if( IsVirtual(pTab) ){
 89999       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 90000       sqlite3VtabMakeWritable(pParse, pTab);
 90001       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
 90002       sqlite3VdbeChangeP5(v, OE_Abort);
 90003       sqlite3MayAbort(pParse);
 90004     }else
 90005 #endif
 90007       int count = (pParse->nested==0);    /* True to count changes */
 90008       sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
 90009                                iKey, nKey, count, OE_Default, okOnePass);
 90012     /* End of the loop over all rowids/primary-keys. */
 90013     if( okOnePass ){
 90014       sqlite3VdbeResolveLabel(v, addrBypass);
 90015     }else if( pPk ){
 90016       sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
 90017       sqlite3VdbeJumpHere(v, addrLoop);
 90018     }else{
 90019       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
 90020       sqlite3VdbeJumpHere(v, addrLoop);
 90023     /* Close the cursors open on the table and its indexes. */
 90024     if( !isView && !IsVirtual(pTab) ){
 90025       if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
 90026       for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 90027         sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
 90030   } /* End non-truncate path */
 90032   /* Update the sqlite_sequence table by storing the content of the
 90033   ** maximum rowid counter values recorded while inserting into
 90034   ** autoincrement tables.
 90035   */
 90036   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 90037     sqlite3AutoincrementEnd(pParse);
 90040   /* Return the number of rows that were deleted. If this routine is 
 90041   ** generating code because of a call to sqlite3NestedParse(), do not
 90042   ** invoke the callback function.
 90043   */
 90044   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 90045     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
 90046     sqlite3VdbeSetNumCols(v, 1);
 90047     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
 90050 delete_from_cleanup:
 90051   sqlite3AuthContextPop(&sContext);
 90052   sqlite3SrcListDelete(db, pTabList);
 90053   sqlite3ExprDelete(db, pWhere);
 90054   sqlite3DbFree(db, aToOpen);
 90055   return;
 90057 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 90058 ** thely may interfere with compilation of other functions in this file
 90059 ** (or in another file, if this file becomes part of the amalgamation).  */
 90060 #ifdef isView
 90061  #undef isView
 90062 #endif
 90063 #ifdef pTrigger
 90064  #undef pTrigger
 90065 #endif
 90067 /*
 90068 ** This routine generates VDBE code that causes a single row of a
 90069 ** single table to be deleted.  Both the original table entry and
 90070 ** all indices are removed.
 90071 **
 90072 ** Preconditions:
 90073 **
 90074 **   1.  iDataCur is an open cursor on the btree that is the canonical data
 90075 **       store for the table.  (This will be either the table itself,
 90076 **       in the case of a rowid table, or the PRIMARY KEY index in the case
 90077 **       of a WITHOUT ROWID table.)
 90078 **
 90079 **   2.  Read/write cursors for all indices of pTab must be open as
 90080 **       cursor number iIdxCur+i for the i-th index.
 90081 **
 90082 **   3.  The primary key for the row to be deleted must be stored in a
 90083 **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
 90084 **       that a search record formed from OP_MakeRecord is contained in the
 90085 **       single memory location iPk.
 90086 */
 90087 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
 90088   Parse *pParse,     /* Parsing context */
 90089   Table *pTab,       /* Table containing the row to be deleted */
 90090   Trigger *pTrigger, /* List of triggers to (potentially) fire */
 90091   int iDataCur,      /* Cursor from which column data is extracted */
 90092   int iIdxCur,       /* First index cursor */
 90093   int iPk,           /* First memory cell containing the PRIMARY KEY */
 90094   i16 nPk,           /* Number of PRIMARY KEY memory cells */
 90095   u8 count,          /* If non-zero, increment the row change counter */
 90096   u8 onconf,         /* Default ON CONFLICT policy for triggers */
 90097   u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
 90098 ){
 90099   Vdbe *v = pParse->pVdbe;        /* Vdbe */
 90100   int iOld = 0;                   /* First register in OLD.* array */
 90101   int iLabel;                     /* Label resolved to end of generated code */
 90102   u8 opSeek;                      /* Seek opcode */
 90104   /* Vdbe is guaranteed to have been allocated by this stage. */
 90105   assert( v );
 90106   VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
 90107                          iDataCur, iIdxCur, iPk, (int)nPk));
 90109   /* Seek cursor iCur to the row to delete. If this row no longer exists 
 90110   ** (this can happen if a trigger program has already deleted it), do
 90111   ** not attempt to delete it or fire any DELETE triggers.  */
 90112   iLabel = sqlite3VdbeMakeLabel(v);
 90113   opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
 90114   if( !bNoSeek ){
 90115     sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
 90116     VdbeCoverageIf(v, opSeek==OP_NotExists);
 90117     VdbeCoverageIf(v, opSeek==OP_NotFound);
 90120   /* If there are any triggers to fire, allocate a range of registers to
 90121   ** use for the old.* references in the triggers.  */
 90122   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
 90123     u32 mask;                     /* Mask of OLD.* columns in use */
 90124     int iCol;                     /* Iterator used while populating OLD.* */
 90125     int addrStart;                /* Start of BEFORE trigger programs */
 90127     /* TODO: Could use temporary registers here. Also could attempt to
 90128     ** avoid copying the contents of the rowid register.  */
 90129     mask = sqlite3TriggerColmask(
 90130         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
 90131     );
 90132     mask |= sqlite3FkOldmask(pParse, pTab);
 90133     iOld = pParse->nMem+1;
 90134     pParse->nMem += (1 + pTab->nCol);
 90136     /* Populate the OLD.* pseudo-table register array. These values will be 
 90137     ** used by any BEFORE and AFTER triggers that exist.  */
 90138     sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
 90139     for(iCol=0; iCol<pTab->nCol; iCol++){
 90140       testcase( mask!=0xffffffff && iCol==31 );
 90141       testcase( mask!=0xffffffff && iCol==32 );
 90142       if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
 90143         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
 90147     /* Invoke BEFORE DELETE trigger programs. */
 90148     addrStart = sqlite3VdbeCurrentAddr(v);
 90149     sqlite3CodeRowTrigger(pParse, pTrigger, 
 90150         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
 90151     );
 90153     /* If any BEFORE triggers were coded, then seek the cursor to the 
 90154     ** row to be deleted again. It may be that the BEFORE triggers moved
 90155     ** the cursor or of already deleted the row that the cursor was
 90156     ** pointing to.
 90157     */
 90158     if( addrStart<sqlite3VdbeCurrentAddr(v) ){
 90159       sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
 90160       VdbeCoverageIf(v, opSeek==OP_NotExists);
 90161       VdbeCoverageIf(v, opSeek==OP_NotFound);
 90164     /* Do FK processing. This call checks that any FK constraints that
 90165     ** refer to this table (i.e. constraints attached to other tables) 
 90166     ** are not violated by deleting this row.  */
 90167     sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
 90170   /* Delete the index and table entries. Skip this step if pTab is really
 90171   ** a view (in which case the only effect of the DELETE statement is to
 90172   ** fire the INSTEAD OF triggers).  */ 
 90173   if( pTab->pSelect==0 ){
 90174     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
 90175     sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
 90176     if( count ){
 90177       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 90181   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 90182   ** handle rows (possibly in other tables) that refer via a foreign key
 90183   ** to the row just deleted. */ 
 90184   sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
 90186   /* Invoke AFTER DELETE trigger programs. */
 90187   sqlite3CodeRowTrigger(pParse, pTrigger, 
 90188       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
 90189   );
 90191   /* Jump here if the row had already been deleted before any BEFORE
 90192   ** trigger programs were invoked. Or if a trigger program throws a 
 90193   ** RAISE(IGNORE) exception.  */
 90194   sqlite3VdbeResolveLabel(v, iLabel);
 90195   VdbeModuleComment((v, "END: GenRowDel()"));
 90198 /*
 90199 ** This routine generates VDBE code that causes the deletion of all
 90200 ** index entries associated with a single row of a single table, pTab
 90201 **
 90202 ** Preconditions:
 90203 **
 90204 **   1.  A read/write cursor "iDataCur" must be open on the canonical storage
 90205 **       btree for the table pTab.  (This will be either the table itself
 90206 **       for rowid tables or to the primary key index for WITHOUT ROWID
 90207 **       tables.)
 90208 **
 90209 **   2.  Read/write cursors for all indices of pTab must be open as
 90210 **       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
 90211 **       index is the 0-th index.)
 90212 **
 90213 **   3.  The "iDataCur" cursor must be already be positioned on the row
 90214 **       that is to be deleted.
 90215 */
 90216 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
 90217   Parse *pParse,     /* Parsing and code generating context */
 90218   Table *pTab,       /* Table containing the row to be deleted */
 90219   int iDataCur,      /* Cursor of table holding data. */
 90220   int iIdxCur,       /* First index cursor */
 90221   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
 90222 ){
 90223   int i;             /* Index loop counter */
 90224   int r1 = -1;       /* Register holding an index key */
 90225   int iPartIdxLabel; /* Jump destination for skipping partial index entries */
 90226   Index *pIdx;       /* Current index */
 90227   Index *pPrior = 0; /* Prior index */
 90228   Vdbe *v;           /* The prepared statement under construction */
 90229   Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
 90231   v = pParse->pVdbe;
 90232   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
 90233   for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
 90234     assert( iIdxCur+i!=iDataCur || pPk==pIdx );
 90235     if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
 90236     if( pIdx==pPk ) continue;
 90237     VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
 90238     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
 90239                                  &iPartIdxLabel, pPrior, r1);
 90240     sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
 90241                       pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
 90242     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
 90243     pPrior = pIdx;
 90247 /*
 90248 ** Generate code that will assemble an index key and stores it in register
 90249 ** regOut.  The key with be for index pIdx which is an index on pTab.
 90250 ** iCur is the index of a cursor open on the pTab table and pointing to
 90251 ** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
 90252 ** iCur must be the cursor of the PRIMARY KEY index.
 90253 **
 90254 ** Return a register number which is the first in a block of
 90255 ** registers that holds the elements of the index key.  The
 90256 ** block of registers has already been deallocated by the time
 90257 ** this routine returns.
 90258 **
 90259 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
 90260 ** to that label if pIdx is a partial index that should be skipped.
 90261 ** A partial index should be skipped if its WHERE clause evaluates
 90262 ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
 90263 ** will be set to zero which is an empty label that is ignored by
 90264 ** sqlite3VdbeResolveLabel().
 90265 **
 90266 ** The pPrior and regPrior parameters are used to implement a cache to
 90267 ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
 90268 ** a pointer to a different index for which an index key has just been
 90269 ** computed into register regPrior.  If the current pIdx index is generating
 90270 ** its key into the same sequence of registers and if pPrior and pIdx share
 90271 ** a column in common, then the register corresponding to that column already
 90272 ** holds the correct value and the loading of that register is skipped.
 90273 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK 
 90274 ** on a table with multiple indices, and especially with the ROWID or
 90275 ** PRIMARY KEY columns of the index.
 90276 */
 90277 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
 90278   Parse *pParse,       /* Parsing context */
 90279   Index *pIdx,         /* The index for which to generate a key */
 90280   int iDataCur,        /* Cursor number from which to take column data */
 90281   int regOut,          /* Put the new key into this register if not 0 */
 90282   int prefixOnly,      /* Compute only a unique prefix of the key */
 90283   int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
 90284   Index *pPrior,       /* Previously generated index key */
 90285   int regPrior         /* Register holding previous generated key */
 90286 ){
 90287   Vdbe *v = pParse->pVdbe;
 90288   int j;
 90289   Table *pTab = pIdx->pTable;
 90290   int regBase;
 90291   int nCol;
 90293   if( piPartIdxLabel ){
 90294     if( pIdx->pPartIdxWhere ){
 90295       *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
 90296       pParse->iPartIdxTab = iDataCur;
 90297       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
 90298                          SQLITE_JUMPIFNULL);
 90299     }else{
 90300       *piPartIdxLabel = 0;
 90303   nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
 90304   regBase = sqlite3GetTempRange(pParse, nCol);
 90305   if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
 90306   for(j=0; j<nCol; j++){
 90307     if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
 90308     sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
 90309                                     regBase+j);
 90310     /* If the column affinity is REAL but the number is an integer, then it
 90311     ** might be stored in the table as an integer (using a compact
 90312     ** representation) then converted to REAL by an OP_RealAffinity opcode.
 90313     ** But we are getting ready to store this value back into an index, where
 90314     ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
 90315     ** opcode if it is present */
 90316     sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
 90318   if( regOut ){
 90319     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
 90321   sqlite3ReleaseTempRange(pParse, regBase, nCol);
 90322   return regBase;
 90325 /************** End of delete.c **********************************************/
 90326 /************** Begin file func.c ********************************************/
 90327 /*
 90328 ** 2002 February 23
 90329 **
 90330 ** The author disclaims copyright to this source code.  In place of
 90331 ** a legal notice, here is a blessing:
 90332 **
 90333 **    May you do good and not evil.
 90334 **    May you find forgiveness for yourself and forgive others.
 90335 **    May you share freely, never taking more than you give.
 90336 **
 90337 *************************************************************************
 90338 ** This file contains the C functions that implement various SQL
 90339 ** functions of SQLite.  
 90340 **
 90341 ** There is only one exported symbol in this file - the function
 90342 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 90343 ** All other code has file scope.
 90344 */
 90345 /* #include <stdlib.h> */
 90346 /* #include <assert.h> */
 90348 /*
 90349 ** Return the collating function associated with a function.
 90350 */
 90351 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
 90352   return context->pColl;
 90355 /*
 90356 ** Indicate that the accumulator load should be skipped on this
 90357 ** iteration of the aggregate loop.
 90358 */
 90359 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
 90360   context->skipFlag = 1;
 90363 /*
 90364 ** Implementation of the non-aggregate min() and max() functions
 90365 */
 90366 static void minmaxFunc(
 90367   sqlite3_context *context,
 90368   int argc,
 90369   sqlite3_value **argv
 90370 ){
 90371   int i;
 90372   int mask;    /* 0 for min() or 0xffffffff for max() */
 90373   int iBest;
 90374   CollSeq *pColl;
 90376   assert( argc>1 );
 90377   mask = sqlite3_user_data(context)==0 ? 0 : -1;
 90378   pColl = sqlite3GetFuncCollSeq(context);
 90379   assert( pColl );
 90380   assert( mask==-1 || mask==0 );
 90381   iBest = 0;
 90382   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 90383   for(i=1; i<argc; i++){
 90384     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
 90385     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
 90386       testcase( mask==0 );
 90387       iBest = i;
 90390   sqlite3_result_value(context, argv[iBest]);
 90393 /*
 90394 ** Return the type of the argument.
 90395 */
 90396 static void typeofFunc(
 90397   sqlite3_context *context,
 90398   int NotUsed,
 90399   sqlite3_value **argv
 90400 ){
 90401   const char *z = 0;
 90402   UNUSED_PARAMETER(NotUsed);
 90403   switch( sqlite3_value_type(argv[0]) ){
 90404     case SQLITE_INTEGER: z = "integer"; break;
 90405     case SQLITE_TEXT:    z = "text";    break;
 90406     case SQLITE_FLOAT:   z = "real";    break;
 90407     case SQLITE_BLOB:    z = "blob";    break;
 90408     default:             z = "null";    break;
 90410   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
 90414 /*
 90415 ** Implementation of the length() function
 90416 */
 90417 static void lengthFunc(
 90418   sqlite3_context *context,
 90419   int argc,
 90420   sqlite3_value **argv
 90421 ){
 90422   int len;
 90424   assert( argc==1 );
 90425   UNUSED_PARAMETER(argc);
 90426   switch( sqlite3_value_type(argv[0]) ){
 90427     case SQLITE_BLOB:
 90428     case SQLITE_INTEGER:
 90429     case SQLITE_FLOAT: {
 90430       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
 90431       break;
 90433     case SQLITE_TEXT: {
 90434       const unsigned char *z = sqlite3_value_text(argv[0]);
 90435       if( z==0 ) return;
 90436       len = 0;
 90437       while( *z ){
 90438         len++;
 90439         SQLITE_SKIP_UTF8(z);
 90441       sqlite3_result_int(context, len);
 90442       break;
 90444     default: {
 90445       sqlite3_result_null(context);
 90446       break;
 90451 /*
 90452 ** Implementation of the abs() function.
 90453 **
 90454 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
 90455 ** the numeric argument X. 
 90456 */
 90457 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 90458   assert( argc==1 );
 90459   UNUSED_PARAMETER(argc);
 90460   switch( sqlite3_value_type(argv[0]) ){
 90461     case SQLITE_INTEGER: {
 90462       i64 iVal = sqlite3_value_int64(argv[0]);
 90463       if( iVal<0 ){
 90464         if( iVal==SMALLEST_INT64 ){
 90465           /* IMP: R-31676-45509 If X is the integer -9223372036854775808
 90466           ** then abs(X) throws an integer overflow error since there is no
 90467           ** equivalent positive 64-bit two complement value. */
 90468           sqlite3_result_error(context, "integer overflow", -1);
 90469           return;
 90471         iVal = -iVal;
 90473       sqlite3_result_int64(context, iVal);
 90474       break;
 90476     case SQLITE_NULL: {
 90477       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
 90478       sqlite3_result_null(context);
 90479       break;
 90481     default: {
 90482       /* Because sqlite3_value_double() returns 0.0 if the argument is not
 90483       ** something that can be converted into a number, we have:
 90484       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
 90485       ** cannot be converted to a numeric value. 
 90486       */
 90487       double rVal = sqlite3_value_double(argv[0]);
 90488       if( rVal<0 ) rVal = -rVal;
 90489       sqlite3_result_double(context, rVal);
 90490       break;
 90495 /*
 90496 ** Implementation of the instr() function.
 90497 **
 90498 ** instr(haystack,needle) finds the first occurrence of needle
 90499 ** in haystack and returns the number of previous characters plus 1,
 90500 ** or 0 if needle does not occur within haystack.
 90501 **
 90502 ** If both haystack and needle are BLOBs, then the result is one more than
 90503 ** the number of bytes in haystack prior to the first occurrence of needle,
 90504 ** or 0 if needle never occurs in haystack.
 90505 */
 90506 static void instrFunc(
 90507   sqlite3_context *context,
 90508   int argc,
 90509   sqlite3_value **argv
 90510 ){
 90511   const unsigned char *zHaystack;
 90512   const unsigned char *zNeedle;
 90513   int nHaystack;
 90514   int nNeedle;
 90515   int typeHaystack, typeNeedle;
 90516   int N = 1;
 90517   int isText;
 90519   UNUSED_PARAMETER(argc);
 90520   typeHaystack = sqlite3_value_type(argv[0]);
 90521   typeNeedle = sqlite3_value_type(argv[1]);
 90522   if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
 90523   nHaystack = sqlite3_value_bytes(argv[0]);
 90524   nNeedle = sqlite3_value_bytes(argv[1]);
 90525   if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
 90526     zHaystack = sqlite3_value_blob(argv[0]);
 90527     zNeedle = sqlite3_value_blob(argv[1]);
 90528     isText = 0;
 90529   }else{
 90530     zHaystack = sqlite3_value_text(argv[0]);
 90531     zNeedle = sqlite3_value_text(argv[1]);
 90532     isText = 1;
 90534   while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
 90535     N++;
 90536     do{
 90537       nHaystack--;
 90538       zHaystack++;
 90539     }while( isText && (zHaystack[0]&0xc0)==0x80 );
 90541   if( nNeedle>nHaystack ) N = 0;
 90542   sqlite3_result_int(context, N);
 90545 /*
 90546 ** Implementation of the printf() function.
 90547 */
 90548 static void printfFunc(
 90549   sqlite3_context *context,
 90550   int argc,
 90551   sqlite3_value **argv
 90552 ){
 90553   PrintfArguments x;
 90554   StrAccum str;
 90555   const char *zFormat;
 90556   int n;
 90558   if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
 90559     x.nArg = argc-1;
 90560     x.nUsed = 0;
 90561     x.apArg = argv+1;
 90562     sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
 90563     str.db = sqlite3_context_db_handle(context);
 90564     sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
 90565     n = str.nChar;
 90566     sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
 90567                         SQLITE_DYNAMIC);
 90571 /*
 90572 ** Implementation of the substr() function.
 90573 **
 90574 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
 90575 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
 90576 ** of x.  If x is text, then we actually count UTF-8 characters.
 90577 ** If x is a blob, then we count bytes.
 90578 **
 90579 ** If p1 is negative, then we begin abs(p1) from the end of x[].
 90580 **
 90581 ** If p2 is negative, return the p2 characters preceding p1.
 90582 */
 90583 static void substrFunc(
 90584   sqlite3_context *context,
 90585   int argc,
 90586   sqlite3_value **argv
 90587 ){
 90588   const unsigned char *z;
 90589   const unsigned char *z2;
 90590   int len;
 90591   int p0type;
 90592   i64 p1, p2;
 90593   int negP2 = 0;
 90595   assert( argc==3 || argc==2 );
 90596   if( sqlite3_value_type(argv[1])==SQLITE_NULL
 90597    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
 90598   ){
 90599     return;
 90601   p0type = sqlite3_value_type(argv[0]);
 90602   p1 = sqlite3_value_int(argv[1]);
 90603   if( p0type==SQLITE_BLOB ){
 90604     len = sqlite3_value_bytes(argv[0]);
 90605     z = sqlite3_value_blob(argv[0]);
 90606     if( z==0 ) return;
 90607     assert( len==sqlite3_value_bytes(argv[0]) );
 90608   }else{
 90609     z = sqlite3_value_text(argv[0]);
 90610     if( z==0 ) return;
 90611     len = 0;
 90612     if( p1<0 ){
 90613       for(z2=z; *z2; len++){
 90614         SQLITE_SKIP_UTF8(z2);
 90618   if( argc==3 ){
 90619     p2 = sqlite3_value_int(argv[2]);
 90620     if( p2<0 ){
 90621       p2 = -p2;
 90622       negP2 = 1;
 90624   }else{
 90625     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
 90627   if( p1<0 ){
 90628     p1 += len;
 90629     if( p1<0 ){
 90630       p2 += p1;
 90631       if( p2<0 ) p2 = 0;
 90632       p1 = 0;
 90634   }else if( p1>0 ){
 90635     p1--;
 90636   }else if( p2>0 ){
 90637     p2--;
 90639   if( negP2 ){
 90640     p1 -= p2;
 90641     if( p1<0 ){
 90642       p2 += p1;
 90643       p1 = 0;
 90646   assert( p1>=0 && p2>=0 );
 90647   if( p0type!=SQLITE_BLOB ){
 90648     while( *z && p1 ){
 90649       SQLITE_SKIP_UTF8(z);
 90650       p1--;
 90652     for(z2=z; *z2 && p2; p2--){
 90653       SQLITE_SKIP_UTF8(z2);
 90655     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
 90656   }else{
 90657     if( p1+p2>len ){
 90658       p2 = len-p1;
 90659       if( p2<0 ) p2 = 0;
 90661     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
 90665 /*
 90666 ** Implementation of the round() function
 90667 */
 90668 #ifndef SQLITE_OMIT_FLOATING_POINT
 90669 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 90670   int n = 0;
 90671   double r;
 90672   char *zBuf;
 90673   assert( argc==1 || argc==2 );
 90674   if( argc==2 ){
 90675     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
 90676     n = sqlite3_value_int(argv[1]);
 90677     if( n>30 ) n = 30;
 90678     if( n<0 ) n = 0;
 90680   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 90681   r = sqlite3_value_double(argv[0]);
 90682   /* If Y==0 and X will fit in a 64-bit int,
 90683   ** handle the rounding directly,
 90684   ** otherwise use printf.
 90685   */
 90686   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
 90687     r = (double)((sqlite_int64)(r+0.5));
 90688   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
 90689     r = -(double)((sqlite_int64)((-r)+0.5));
 90690   }else{
 90691     zBuf = sqlite3_mprintf("%.*f",n,r);
 90692     if( zBuf==0 ){
 90693       sqlite3_result_error_nomem(context);
 90694       return;
 90696     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
 90697     sqlite3_free(zBuf);
 90699   sqlite3_result_double(context, r);
 90701 #endif
 90703 /*
 90704 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
 90705 ** allocation fails, call sqlite3_result_error_nomem() to notify
 90706 ** the database handle that malloc() has failed and return NULL.
 90707 ** If nByte is larger than the maximum string or blob length, then
 90708 ** raise an SQLITE_TOOBIG exception and return NULL.
 90709 */
 90710 static void *contextMalloc(sqlite3_context *context, i64 nByte){
 90711   char *z;
 90712   sqlite3 *db = sqlite3_context_db_handle(context);
 90713   assert( nByte>0 );
 90714   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
 90715   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 90716   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 90717     sqlite3_result_error_toobig(context);
 90718     z = 0;
 90719   }else{
 90720     z = sqlite3Malloc((int)nByte);
 90721     if( !z ){
 90722       sqlite3_result_error_nomem(context);
 90725   return z;
 90728 /*
 90729 ** Implementation of the upper() and lower() SQL functions.
 90730 */
 90731 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 90732   char *z1;
 90733   const char *z2;
 90734   int i, n;
 90735   UNUSED_PARAMETER(argc);
 90736   z2 = (char*)sqlite3_value_text(argv[0]);
 90737   n = sqlite3_value_bytes(argv[0]);
 90738   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 90739   assert( z2==(char*)sqlite3_value_text(argv[0]) );
 90740   if( z2 ){
 90741     z1 = contextMalloc(context, ((i64)n)+1);
 90742     if( z1 ){
 90743       for(i=0; i<n; i++){
 90744         z1[i] = (char)sqlite3Toupper(z2[i]);
 90746       sqlite3_result_text(context, z1, n, sqlite3_free);
 90750 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 90751   char *z1;
 90752   const char *z2;
 90753   int i, n;
 90754   UNUSED_PARAMETER(argc);
 90755   z2 = (char*)sqlite3_value_text(argv[0]);
 90756   n = sqlite3_value_bytes(argv[0]);
 90757   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
 90758   assert( z2==(char*)sqlite3_value_text(argv[0]) );
 90759   if( z2 ){
 90760     z1 = contextMalloc(context, ((i64)n)+1);
 90761     if( z1 ){
 90762       for(i=0; i<n; i++){
 90763         z1[i] = sqlite3Tolower(z2[i]);
 90765       sqlite3_result_text(context, z1, n, sqlite3_free);
 90770 /*
 90771 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
 90772 ** as VDBE code so that unused argument values do not have to be computed.
 90773 ** However, we still need some kind of function implementation for this
 90774 ** routines in the function table.  The noopFunc macro provides this.
 90775 ** noopFunc will never be called so it doesn't matter what the implementation
 90776 ** is.  We might as well use the "version()" function as a substitute.
 90777 */
 90778 #define noopFunc versionFunc   /* Substitute function - never called */
 90780 /*
 90781 ** Implementation of random().  Return a random integer.  
 90782 */
 90783 static void randomFunc(
 90784   sqlite3_context *context,
 90785   int NotUsed,
 90786   sqlite3_value **NotUsed2
 90787 ){
 90788   sqlite_int64 r;
 90789   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 90790   sqlite3_randomness(sizeof(r), &r);
 90791   if( r<0 ){
 90792     /* We need to prevent a random number of 0x8000000000000000 
 90793     ** (or -9223372036854775808) since when you do abs() of that
 90794     ** number of you get the same value back again.  To do this
 90795     ** in a way that is testable, mask the sign bit off of negative
 90796     ** values, resulting in a positive value.  Then take the 
 90797     ** 2s complement of that positive value.  The end result can
 90798     ** therefore be no less than -9223372036854775807.
 90799     */
 90800     r = -(r & LARGEST_INT64);
 90802   sqlite3_result_int64(context, r);
 90805 /*
 90806 ** Implementation of randomblob(N).  Return a random blob
 90807 ** that is N bytes long.
 90808 */
 90809 static void randomBlob(
 90810   sqlite3_context *context,
 90811   int argc,
 90812   sqlite3_value **argv
 90813 ){
 90814   int n;
 90815   unsigned char *p;
 90816   assert( argc==1 );
 90817   UNUSED_PARAMETER(argc);
 90818   n = sqlite3_value_int(argv[0]);
 90819   if( n<1 ){
 90820     n = 1;
 90822   p = contextMalloc(context, n);
 90823   if( p ){
 90824     sqlite3_randomness(n, p);
 90825     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
 90829 /*
 90830 ** Implementation of the last_insert_rowid() SQL function.  The return
 90831 ** value is the same as the sqlite3_last_insert_rowid() API function.
 90832 */
 90833 static void last_insert_rowid(
 90834   sqlite3_context *context, 
 90835   int NotUsed, 
 90836   sqlite3_value **NotUsed2
 90837 ){
 90838   sqlite3 *db = sqlite3_context_db_handle(context);
 90839   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 90840   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
 90841   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
 90842   ** function. */
 90843   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
 90846 /*
 90847 ** Implementation of the changes() SQL function.
 90848 **
 90849 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
 90850 ** around the sqlite3_changes() C/C++ function and hence follows the same
 90851 ** rules for counting changes.
 90852 */
 90853 static void changes(
 90854   sqlite3_context *context,
 90855   int NotUsed,
 90856   sqlite3_value **NotUsed2
 90857 ){
 90858   sqlite3 *db = sqlite3_context_db_handle(context);
 90859   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 90860   sqlite3_result_int(context, sqlite3_changes(db));
 90863 /*
 90864 ** Implementation of the total_changes() SQL function.  The return value is
 90865 ** the same as the sqlite3_total_changes() API function.
 90866 */
 90867 static void total_changes(
 90868   sqlite3_context *context,
 90869   int NotUsed,
 90870   sqlite3_value **NotUsed2
 90871 ){
 90872   sqlite3 *db = sqlite3_context_db_handle(context);
 90873   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 90874   /* IMP: R-52756-41993 This function is a wrapper around the
 90875   ** sqlite3_total_changes() C/C++ interface. */
 90876   sqlite3_result_int(context, sqlite3_total_changes(db));
 90879 /*
 90880 ** A structure defining how to do GLOB-style comparisons.
 90881 */
 90882 struct compareInfo {
 90883   u8 matchAll;
 90884   u8 matchOne;
 90885   u8 matchSet;
 90886   u8 noCase;
 90887 };
 90889 /*
 90890 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
 90891 ** character is exactly one byte in size.  Also, all characters are
 90892 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
 90893 ** whereas only characters less than 0x80 do in ASCII.
 90894 */
 90895 #if defined(SQLITE_EBCDIC)
 90896 # define sqlite3Utf8Read(A)    (*((*A)++))
 90897 # define GlobUpperToLower(A)   A = sqlite3UpperToLower[A]
 90898 #else
 90899 # define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
 90900 #endif
 90902 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
 90903 /* The correct SQL-92 behavior is for the LIKE operator to ignore
 90904 ** case.  Thus  'a' LIKE 'A' would be true. */
 90905 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
 90906 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
 90907 ** is case sensitive causing 'a' LIKE 'A' to be false */
 90908 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
 90910 /*
 90911 ** Compare two UTF-8 strings for equality where the first string can
 90912 ** potentially be a "glob" expression.  Return true (1) if they
 90913 ** are the same and false (0) if they are different.
 90914 **
 90915 ** Globbing rules:
 90916 **
 90917 **      '*'       Matches any sequence of zero or more characters.
 90918 **
 90919 **      '?'       Matches exactly one character.
 90920 **
 90921 **     [...]      Matches one character from the enclosed list of
 90922 **                characters.
 90923 **
 90924 **     [^...]     Matches one character not in the enclosed list.
 90925 **
 90926 ** With the [...] and [^...] matching, a ']' character can be included
 90927 ** in the list by making it the first character after '[' or '^'.  A
 90928 ** range of characters can be specified using '-'.  Example:
 90929 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
 90930 ** it the last character in the list.
 90931 **
 90932 ** This routine is usually quick, but can be N**2 in the worst case.
 90933 **
 90934 ** Hints: to match '*' or '?', put them in "[]".  Like this:
 90935 **
 90936 **         abc[*]xyz        Matches "abc*xyz" only
 90937 */
 90938 static int patternCompare(
 90939   const u8 *zPattern,              /* The glob pattern */
 90940   const u8 *zString,               /* The string to compare against the glob */
 90941   const struct compareInfo *pInfo, /* Information about how to do the compare */
 90942   u32 esc                          /* The escape character */
 90943 ){
 90944   u32 c, c2;
 90945   int invert;
 90946   int seen;
 90947   u8 matchOne = pInfo->matchOne;
 90948   u8 matchAll = pInfo->matchAll;
 90949   u8 matchSet = pInfo->matchSet;
 90950   u8 noCase = pInfo->noCase; 
 90951   int prevEscape = 0;     /* True if the previous character was 'escape' */
 90953   while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
 90954     if( c==matchAll && !prevEscape ){
 90955       while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
 90956                || c == matchOne ){
 90957         if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
 90958           return 0;
 90961       if( c==0 ){
 90962         return 1;
 90963       }else if( c==esc ){
 90964         c = sqlite3Utf8Read(&zPattern);
 90965         if( c==0 ){
 90966           return 0;
 90968       }else if( c==matchSet ){
 90969         assert( esc==0 );         /* This is GLOB, not LIKE */
 90970         assert( matchSet<0x80 );  /* '[' is a single-byte character */
 90971         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
 90972           SQLITE_SKIP_UTF8(zString);
 90974         return *zString!=0;
 90976       while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
 90977         if( noCase ){
 90978           GlobUpperToLower(c2);
 90979           GlobUpperToLower(c);
 90980           while( c2 != 0 && c2 != c ){
 90981             c2 = sqlite3Utf8Read(&zString);
 90982             GlobUpperToLower(c2);
 90984         }else{
 90985           while( c2 != 0 && c2 != c ){
 90986             c2 = sqlite3Utf8Read(&zString);
 90989         if( c2==0 ) return 0;
 90990         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
 90992       return 0;
 90993     }else if( c==matchOne && !prevEscape ){
 90994       if( sqlite3Utf8Read(&zString)==0 ){
 90995         return 0;
 90997     }else if( c==matchSet ){
 90998       u32 prior_c = 0;
 90999       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
 91000       seen = 0;
 91001       invert = 0;
 91002       c = sqlite3Utf8Read(&zString);
 91003       if( c==0 ) return 0;
 91004       c2 = sqlite3Utf8Read(&zPattern);
 91005       if( c2=='^' ){
 91006         invert = 1;
 91007         c2 = sqlite3Utf8Read(&zPattern);
 91009       if( c2==']' ){
 91010         if( c==']' ) seen = 1;
 91011         c2 = sqlite3Utf8Read(&zPattern);
 91013       while( c2 && c2!=']' ){
 91014         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
 91015           c2 = sqlite3Utf8Read(&zPattern);
 91016           if( c>=prior_c && c<=c2 ) seen = 1;
 91017           prior_c = 0;
 91018         }else{
 91019           if( c==c2 ){
 91020             seen = 1;
 91022           prior_c = c2;
 91024         c2 = sqlite3Utf8Read(&zPattern);
 91026       if( c2==0 || (seen ^ invert)==0 ){
 91027         return 0;
 91029     }else if( esc==c && !prevEscape ){
 91030       prevEscape = 1;
 91031     }else{
 91032       c2 = sqlite3Utf8Read(&zString);
 91033       if( noCase ){
 91034         GlobUpperToLower(c);
 91035         GlobUpperToLower(c2);
 91037       if( c!=c2 ){
 91038         return 0;
 91040       prevEscape = 0;
 91043   return *zString==0;
 91046 /*
 91047 ** The sqlite3_strglob() interface.
 91048 */
 91049 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
 91050   return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
 91053 /*
 91054 ** Count the number of times that the LIKE operator (or GLOB which is
 91055 ** just a variation of LIKE) gets called.  This is used for testing
 91056 ** only.
 91057 */
 91058 #ifdef SQLITE_TEST
 91059 SQLITE_API int sqlite3_like_count = 0;
 91060 #endif
 91063 /*
 91064 ** Implementation of the like() SQL function.  This function implements
 91065 ** the build-in LIKE operator.  The first argument to the function is the
 91066 ** pattern and the second argument is the string.  So, the SQL statements:
 91067 **
 91068 **       A LIKE B
 91069 **
 91070 ** is implemented as like(B,A).
 91071 **
 91072 ** This same function (with a different compareInfo structure) computes
 91073 ** the GLOB operator.
 91074 */
 91075 static void likeFunc(
 91076   sqlite3_context *context, 
 91077   int argc, 
 91078   sqlite3_value **argv
 91079 ){
 91080   const unsigned char *zA, *zB;
 91081   u32 escape = 0;
 91082   int nPat;
 91083   sqlite3 *db = sqlite3_context_db_handle(context);
 91085   zB = sqlite3_value_text(argv[0]);
 91086   zA = sqlite3_value_text(argv[1]);
 91088   /* Limit the length of the LIKE or GLOB pattern to avoid problems
 91089   ** of deep recursion and N*N behavior in patternCompare().
 91090   */
 91091   nPat = sqlite3_value_bytes(argv[0]);
 91092   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
 91093   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
 91094   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
 91095     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
 91096     return;
 91098   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
 91100   if( argc==3 ){
 91101     /* The escape character string must consist of a single UTF-8 character.
 91102     ** Otherwise, return an error.
 91103     */
 91104     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
 91105     if( zEsc==0 ) return;
 91106     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
 91107       sqlite3_result_error(context, 
 91108           "ESCAPE expression must be a single character", -1);
 91109       return;
 91111     escape = sqlite3Utf8Read(&zEsc);
 91113   if( zA && zB ){
 91114     struct compareInfo *pInfo = sqlite3_user_data(context);
 91115 #ifdef SQLITE_TEST
 91116     sqlite3_like_count++;
 91117 #endif
 91119     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
 91123 /*
 91124 ** Implementation of the NULLIF(x,y) function.  The result is the first
 91125 ** argument if the arguments are different.  The result is NULL if the
 91126 ** arguments are equal to each other.
 91127 */
 91128 static void nullifFunc(
 91129   sqlite3_context *context,
 91130   int NotUsed,
 91131   sqlite3_value **argv
 91132 ){
 91133   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 91134   UNUSED_PARAMETER(NotUsed);
 91135   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
 91136     sqlite3_result_value(context, argv[0]);
 91140 /*
 91141 ** Implementation of the sqlite_version() function.  The result is the version
 91142 ** of the SQLite library that is running.
 91143 */
 91144 static void versionFunc(
 91145   sqlite3_context *context,
 91146   int NotUsed,
 91147   sqlite3_value **NotUsed2
 91148 ){
 91149   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 91150   /* IMP: R-48699-48617 This function is an SQL wrapper around the
 91151   ** sqlite3_libversion() C-interface. */
 91152   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
 91155 /*
 91156 ** Implementation of the sqlite_source_id() function. The result is a string
 91157 ** that identifies the particular version of the source code used to build
 91158 ** SQLite.
 91159 */
 91160 static void sourceidFunc(
 91161   sqlite3_context *context,
 91162   int NotUsed,
 91163   sqlite3_value **NotUsed2
 91164 ){
 91165   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 91166   /* IMP: R-24470-31136 This function is an SQL wrapper around the
 91167   ** sqlite3_sourceid() C interface. */
 91168   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
 91171 /*
 91172 ** Implementation of the sqlite_log() function.  This is a wrapper around
 91173 ** sqlite3_log().  The return value is NULL.  The function exists purely for
 91174 ** its side-effects.
 91175 */
 91176 static void errlogFunc(
 91177   sqlite3_context *context,
 91178   int argc,
 91179   sqlite3_value **argv
 91180 ){
 91181   UNUSED_PARAMETER(argc);
 91182   UNUSED_PARAMETER(context);
 91183   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
 91186 /*
 91187 ** Implementation of the sqlite_compileoption_used() function.
 91188 ** The result is an integer that identifies if the compiler option
 91189 ** was used to build SQLite.
 91190 */
 91191 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 91192 static void compileoptionusedFunc(
 91193   sqlite3_context *context,
 91194   int argc,
 91195   sqlite3_value **argv
 91196 ){
 91197   const char *zOptName;
 91198   assert( argc==1 );
 91199   UNUSED_PARAMETER(argc);
 91200   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
 91201   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
 91202   ** function.
 91203   */
 91204   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
 91205     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
 91208 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 91210 /*
 91211 ** Implementation of the sqlite_compileoption_get() function. 
 91212 ** The result is a string that identifies the compiler options 
 91213 ** used to build SQLite.
 91214 */
 91215 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 91216 static void compileoptiongetFunc(
 91217   sqlite3_context *context,
 91218   int argc,
 91219   sqlite3_value **argv
 91220 ){
 91221   int n;
 91222   assert( argc==1 );
 91223   UNUSED_PARAMETER(argc);
 91224   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
 91225   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
 91226   */
 91227   n = sqlite3_value_int(argv[0]);
 91228   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
 91230 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 91232 /* Array for converting from half-bytes (nybbles) into ASCII hex
 91233 ** digits. */
 91234 static const char hexdigits[] = {
 91235   '0', '1', '2', '3', '4', '5', '6', '7',
 91236   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
 91237 };
 91239 /*
 91240 ** Implementation of the QUOTE() function.  This function takes a single
 91241 ** argument.  If the argument is numeric, the return value is the same as
 91242 ** the argument.  If the argument is NULL, the return value is the string
 91243 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
 91244 ** single-quote escapes.
 91245 */
 91246 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 91247   assert( argc==1 );
 91248   UNUSED_PARAMETER(argc);
 91249   switch( sqlite3_value_type(argv[0]) ){
 91250     case SQLITE_FLOAT: {
 91251       double r1, r2;
 91252       char zBuf[50];
 91253       r1 = sqlite3_value_double(argv[0]);
 91254       sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
 91255       sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
 91256       if( r1!=r2 ){
 91257         sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
 91259       sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 91260       break;
 91262     case SQLITE_INTEGER: {
 91263       sqlite3_result_value(context, argv[0]);
 91264       break;
 91266     case SQLITE_BLOB: {
 91267       char *zText = 0;
 91268       char const *zBlob = sqlite3_value_blob(argv[0]);
 91269       int nBlob = sqlite3_value_bytes(argv[0]);
 91270       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
 91271       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
 91272       if( zText ){
 91273         int i;
 91274         for(i=0; i<nBlob; i++){
 91275           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
 91276           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
 91278         zText[(nBlob*2)+2] = '\'';
 91279         zText[(nBlob*2)+3] = '\0';
 91280         zText[0] = 'X';
 91281         zText[1] = '\'';
 91282         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
 91283         sqlite3_free(zText);
 91285       break;
 91287     case SQLITE_TEXT: {
 91288       int i,j;
 91289       u64 n;
 91290       const unsigned char *zArg = sqlite3_value_text(argv[0]);
 91291       char *z;
 91293       if( zArg==0 ) return;
 91294       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
 91295       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
 91296       if( z ){
 91297         z[0] = '\'';
 91298         for(i=0, j=1; zArg[i]; i++){
 91299           z[j++] = zArg[i];
 91300           if( zArg[i]=='\'' ){
 91301             z[j++] = '\'';
 91304         z[j++] = '\'';
 91305         z[j] = 0;
 91306         sqlite3_result_text(context, z, j, sqlite3_free);
 91308       break;
 91310     default: {
 91311       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
 91312       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
 91313       break;
 91318 /*
 91319 ** The unicode() function.  Return the integer unicode code-point value
 91320 ** for the first character of the input string. 
 91321 */
 91322 static void unicodeFunc(
 91323   sqlite3_context *context,
 91324   int argc,
 91325   sqlite3_value **argv
 91326 ){
 91327   const unsigned char *z = sqlite3_value_text(argv[0]);
 91328   (void)argc;
 91329   if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
 91332 /*
 91333 ** The char() function takes zero or more arguments, each of which is
 91334 ** an integer.  It constructs a string where each character of the string
 91335 ** is the unicode character for the corresponding integer argument.
 91336 */
 91337 static void charFunc(
 91338   sqlite3_context *context,
 91339   int argc,
 91340   sqlite3_value **argv
 91341 ){
 91342   unsigned char *z, *zOut;
 91343   int i;
 91344   zOut = z = sqlite3_malloc( argc*4+1 );
 91345   if( z==0 ){
 91346     sqlite3_result_error_nomem(context);
 91347     return;
 91349   for(i=0; i<argc; i++){
 91350     sqlite3_int64 x;
 91351     unsigned c;
 91352     x = sqlite3_value_int64(argv[i]);
 91353     if( x<0 || x>0x10ffff ) x = 0xfffd;
 91354     c = (unsigned)(x & 0x1fffff);
 91355     if( c<0x00080 ){
 91356       *zOut++ = (u8)(c&0xFF);
 91357     }else if( c<0x00800 ){
 91358       *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
 91359       *zOut++ = 0x80 + (u8)(c & 0x3F);
 91360     }else if( c<0x10000 ){
 91361       *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
 91362       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
 91363       *zOut++ = 0x80 + (u8)(c & 0x3F);
 91364     }else{
 91365       *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
 91366       *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
 91367       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
 91368       *zOut++ = 0x80 + (u8)(c & 0x3F);
 91369     }                                                    \
 91371   sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
 91374 /*
 91375 ** The hex() function.  Interpret the argument as a blob.  Return
 91376 ** a hexadecimal rendering as text.
 91377 */
 91378 static void hexFunc(
 91379   sqlite3_context *context,
 91380   int argc,
 91381   sqlite3_value **argv
 91382 ){
 91383   int i, n;
 91384   const unsigned char *pBlob;
 91385   char *zHex, *z;
 91386   assert( argc==1 );
 91387   UNUSED_PARAMETER(argc);
 91388   pBlob = sqlite3_value_blob(argv[0]);
 91389   n = sqlite3_value_bytes(argv[0]);
 91390   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
 91391   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
 91392   if( zHex ){
 91393     for(i=0; i<n; i++, pBlob++){
 91394       unsigned char c = *pBlob;
 91395       *(z++) = hexdigits[(c>>4)&0xf];
 91396       *(z++) = hexdigits[c&0xf];
 91398     *z = 0;
 91399     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
 91403 /*
 91404 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
 91405 */
 91406 static void zeroblobFunc(
 91407   sqlite3_context *context,
 91408   int argc,
 91409   sqlite3_value **argv
 91410 ){
 91411   i64 n;
 91412   sqlite3 *db = sqlite3_context_db_handle(context);
 91413   assert( argc==1 );
 91414   UNUSED_PARAMETER(argc);
 91415   n = sqlite3_value_int64(argv[0]);
 91416   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
 91417   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
 91418   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 91419     sqlite3_result_error_toobig(context);
 91420   }else{
 91421     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
 91425 /*
 91426 ** The replace() function.  Three arguments are all strings: call
 91427 ** them A, B, and C. The result is also a string which is derived
 91428 ** from A by replacing every occurrence of B with C.  The match
 91429 ** must be exact.  Collating sequences are not used.
 91430 */
 91431 static void replaceFunc(
 91432   sqlite3_context *context,
 91433   int argc,
 91434   sqlite3_value **argv
 91435 ){
 91436   const unsigned char *zStr;        /* The input string A */
 91437   const unsigned char *zPattern;    /* The pattern string B */
 91438   const unsigned char *zRep;        /* The replacement string C */
 91439   unsigned char *zOut;              /* The output */
 91440   int nStr;                /* Size of zStr */
 91441   int nPattern;            /* Size of zPattern */
 91442   int nRep;                /* Size of zRep */
 91443   i64 nOut;                /* Maximum size of zOut */
 91444   int loopLimit;           /* Last zStr[] that might match zPattern[] */
 91445   int i, j;                /* Loop counters */
 91447   assert( argc==3 );
 91448   UNUSED_PARAMETER(argc);
 91449   zStr = sqlite3_value_text(argv[0]);
 91450   if( zStr==0 ) return;
 91451   nStr = sqlite3_value_bytes(argv[0]);
 91452   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
 91453   zPattern = sqlite3_value_text(argv[1]);
 91454   if( zPattern==0 ){
 91455     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
 91456             || sqlite3_context_db_handle(context)->mallocFailed );
 91457     return;
 91459   if( zPattern[0]==0 ){
 91460     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
 91461     sqlite3_result_value(context, argv[0]);
 91462     return;
 91464   nPattern = sqlite3_value_bytes(argv[1]);
 91465   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
 91466   zRep = sqlite3_value_text(argv[2]);
 91467   if( zRep==0 ) return;
 91468   nRep = sqlite3_value_bytes(argv[2]);
 91469   assert( zRep==sqlite3_value_text(argv[2]) );
 91470   nOut = nStr + 1;
 91471   assert( nOut<SQLITE_MAX_LENGTH );
 91472   zOut = contextMalloc(context, (i64)nOut);
 91473   if( zOut==0 ){
 91474     return;
 91476   loopLimit = nStr - nPattern;  
 91477   for(i=j=0; i<=loopLimit; i++){
 91478     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
 91479       zOut[j++] = zStr[i];
 91480     }else{
 91481       u8 *zOld;
 91482       sqlite3 *db = sqlite3_context_db_handle(context);
 91483       nOut += nRep - nPattern;
 91484       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
 91485       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
 91486       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
 91487         sqlite3_result_error_toobig(context);
 91488         sqlite3_free(zOut);
 91489         return;
 91491       zOld = zOut;
 91492       zOut = sqlite3_realloc(zOut, (int)nOut);
 91493       if( zOut==0 ){
 91494         sqlite3_result_error_nomem(context);
 91495         sqlite3_free(zOld);
 91496         return;
 91498       memcpy(&zOut[j], zRep, nRep);
 91499       j += nRep;
 91500       i += nPattern-1;
 91503   assert( j+nStr-i+1==nOut );
 91504   memcpy(&zOut[j], &zStr[i], nStr-i);
 91505   j += nStr - i;
 91506   assert( j<=nOut );
 91507   zOut[j] = 0;
 91508   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
 91511 /*
 91512 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
 91513 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
 91514 */
 91515 static void trimFunc(
 91516   sqlite3_context *context,
 91517   int argc,
 91518   sqlite3_value **argv
 91519 ){
 91520   const unsigned char *zIn;         /* Input string */
 91521   const unsigned char *zCharSet;    /* Set of characters to trim */
 91522   int nIn;                          /* Number of bytes in input */
 91523   int flags;                        /* 1: trimleft  2: trimright  3: trim */
 91524   int i;                            /* Loop counter */
 91525   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
 91526   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
 91527   int nChar;                        /* Number of characters in zCharSet */
 91529   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 91530     return;
 91532   zIn = sqlite3_value_text(argv[0]);
 91533   if( zIn==0 ) return;
 91534   nIn = sqlite3_value_bytes(argv[0]);
 91535   assert( zIn==sqlite3_value_text(argv[0]) );
 91536   if( argc==1 ){
 91537     static const unsigned char lenOne[] = { 1 };
 91538     static unsigned char * const azOne[] = { (u8*)" " };
 91539     nChar = 1;
 91540     aLen = (u8*)lenOne;
 91541     azChar = (unsigned char **)azOne;
 91542     zCharSet = 0;
 91543   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
 91544     return;
 91545   }else{
 91546     const unsigned char *z;
 91547     for(z=zCharSet, nChar=0; *z; nChar++){
 91548       SQLITE_SKIP_UTF8(z);
 91550     if( nChar>0 ){
 91551       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
 91552       if( azChar==0 ){
 91553         return;
 91555       aLen = (unsigned char*)&azChar[nChar];
 91556       for(z=zCharSet, nChar=0; *z; nChar++){
 91557         azChar[nChar] = (unsigned char *)z;
 91558         SQLITE_SKIP_UTF8(z);
 91559         aLen[nChar] = (u8)(z - azChar[nChar]);
 91563   if( nChar>0 ){
 91564     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
 91565     if( flags & 1 ){
 91566       while( nIn>0 ){
 91567         int len = 0;
 91568         for(i=0; i<nChar; i++){
 91569           len = aLen[i];
 91570           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
 91572         if( i>=nChar ) break;
 91573         zIn += len;
 91574         nIn -= len;
 91577     if( flags & 2 ){
 91578       while( nIn>0 ){
 91579         int len = 0;
 91580         for(i=0; i<nChar; i++){
 91581           len = aLen[i];
 91582           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
 91584         if( i>=nChar ) break;
 91585         nIn -= len;
 91588     if( zCharSet ){
 91589       sqlite3_free(azChar);
 91592   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
 91596 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
 91597 ** is only available if the SQLITE_SOUNDEX compile-time option is used
 91598 ** when SQLite is built.
 91599 */
 91600 #ifdef SQLITE_SOUNDEX
 91601 /*
 91602 ** Compute the soundex encoding of a word.
 91603 **
 91604 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
 91605 ** soundex encoding of the string X. 
 91606 */
 91607 static void soundexFunc(
 91608   sqlite3_context *context,
 91609   int argc,
 91610   sqlite3_value **argv
 91611 ){
 91612   char zResult[8];
 91613   const u8 *zIn;
 91614   int i, j;
 91615   static const unsigned char iCode[] = {
 91616     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 91617     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 91618     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 91619     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 91620     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 91621     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 91622     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
 91623     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
 91624   };
 91625   assert( argc==1 );
 91626   zIn = (u8*)sqlite3_value_text(argv[0]);
 91627   if( zIn==0 ) zIn = (u8*)"";
 91628   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
 91629   if( zIn[i] ){
 91630     u8 prevcode = iCode[zIn[i]&0x7f];
 91631     zResult[0] = sqlite3Toupper(zIn[i]);
 91632     for(j=1; j<4 && zIn[i]; i++){
 91633       int code = iCode[zIn[i]&0x7f];
 91634       if( code>0 ){
 91635         if( code!=prevcode ){
 91636           prevcode = code;
 91637           zResult[j++] = code + '0';
 91639       }else{
 91640         prevcode = 0;
 91643     while( j<4 ){
 91644       zResult[j++] = '0';
 91646     zResult[j] = 0;
 91647     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
 91648   }else{
 91649     /* IMP: R-64894-50321 The string "?000" is returned if the argument
 91650     ** is NULL or contains no ASCII alphabetic characters. */
 91651     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
 91654 #endif /* SQLITE_SOUNDEX */
 91656 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 91657 /*
 91658 ** A function that loads a shared-library extension then returns NULL.
 91659 */
 91660 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
 91661   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
 91662   const char *zProc;
 91663   sqlite3 *db = sqlite3_context_db_handle(context);
 91664   char *zErrMsg = 0;
 91666   if( argc==2 ){
 91667     zProc = (const char *)sqlite3_value_text(argv[1]);
 91668   }else{
 91669     zProc = 0;
 91671   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
 91672     sqlite3_result_error(context, zErrMsg, -1);
 91673     sqlite3_free(zErrMsg);
 91676 #endif
 91679 /*
 91680 ** An instance of the following structure holds the context of a
 91681 ** sum() or avg() aggregate computation.
 91682 */
 91683 typedef struct SumCtx SumCtx;
 91684 struct SumCtx {
 91685   double rSum;      /* Floating point sum */
 91686   i64 iSum;         /* Integer sum */   
 91687   i64 cnt;          /* Number of elements summed */
 91688   u8 overflow;      /* True if integer overflow seen */
 91689   u8 approx;        /* True if non-integer value was input to the sum */
 91690 };
 91692 /*
 91693 ** Routines used to compute the sum, average, and total.
 91694 **
 91695 ** The SUM() function follows the (broken) SQL standard which means
 91696 ** that it returns NULL if it sums over no inputs.  TOTAL returns
 91697 ** 0.0 in that case.  In addition, TOTAL always returns a float where
 91698 ** SUM might return an integer if it never encounters a floating point
 91699 ** value.  TOTAL never fails, but SUM might through an exception if
 91700 ** it overflows an integer.
 91701 */
 91702 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 91703   SumCtx *p;
 91704   int type;
 91705   assert( argc==1 );
 91706   UNUSED_PARAMETER(argc);
 91707   p = sqlite3_aggregate_context(context, sizeof(*p));
 91708   type = sqlite3_value_numeric_type(argv[0]);
 91709   if( p && type!=SQLITE_NULL ){
 91710     p->cnt++;
 91711     if( type==SQLITE_INTEGER ){
 91712       i64 v = sqlite3_value_int64(argv[0]);
 91713       p->rSum += v;
 91714       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
 91715         p->overflow = 1;
 91717     }else{
 91718       p->rSum += sqlite3_value_double(argv[0]);
 91719       p->approx = 1;
 91723 static void sumFinalize(sqlite3_context *context){
 91724   SumCtx *p;
 91725   p = sqlite3_aggregate_context(context, 0);
 91726   if( p && p->cnt>0 ){
 91727     if( p->overflow ){
 91728       sqlite3_result_error(context,"integer overflow",-1);
 91729     }else if( p->approx ){
 91730       sqlite3_result_double(context, p->rSum);
 91731     }else{
 91732       sqlite3_result_int64(context, p->iSum);
 91736 static void avgFinalize(sqlite3_context *context){
 91737   SumCtx *p;
 91738   p = sqlite3_aggregate_context(context, 0);
 91739   if( p && p->cnt>0 ){
 91740     sqlite3_result_double(context, p->rSum/(double)p->cnt);
 91743 static void totalFinalize(sqlite3_context *context){
 91744   SumCtx *p;
 91745   p = sqlite3_aggregate_context(context, 0);
 91746   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
 91747   sqlite3_result_double(context, p ? p->rSum : (double)0);
 91750 /*
 91751 ** The following structure keeps track of state information for the
 91752 ** count() aggregate function.
 91753 */
 91754 typedef struct CountCtx CountCtx;
 91755 struct CountCtx {
 91756   i64 n;
 91757 };
 91759 /*
 91760 ** Routines to implement the count() aggregate function.
 91761 */
 91762 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 91763   CountCtx *p;
 91764   p = sqlite3_aggregate_context(context, sizeof(*p));
 91765   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
 91766     p->n++;
 91769 #ifndef SQLITE_OMIT_DEPRECATED
 91770   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
 91771   ** sure it still operates correctly, verify that its count agrees with our 
 91772   ** internal count when using count(*) and when the total count can be
 91773   ** expressed as a 32-bit integer. */
 91774   assert( argc==1 || p==0 || p->n>0x7fffffff
 91775           || p->n==sqlite3_aggregate_count(context) );
 91776 #endif
 91778 static void countFinalize(sqlite3_context *context){
 91779   CountCtx *p;
 91780   p = sqlite3_aggregate_context(context, 0);
 91781   sqlite3_result_int64(context, p ? p->n : 0);
 91784 /*
 91785 ** Routines to implement min() and max() aggregate functions.
 91786 */
 91787 static void minmaxStep(
 91788   sqlite3_context *context, 
 91789   int NotUsed, 
 91790   sqlite3_value **argv
 91791 ){
 91792   Mem *pArg  = (Mem *)argv[0];
 91793   Mem *pBest;
 91794   UNUSED_PARAMETER(NotUsed);
 91796   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
 91797   if( !pBest ) return;
 91799   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
 91800     if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
 91801   }else if( pBest->flags ){
 91802     int max;
 91803     int cmp;
 91804     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 91805     /* This step function is used for both the min() and max() aggregates,
 91806     ** the only difference between the two being that the sense of the
 91807     ** comparison is inverted. For the max() aggregate, the
 91808     ** sqlite3_user_data() function returns (void *)-1. For min() it
 91809     ** returns (void *)db, where db is the sqlite3* database pointer.
 91810     ** Therefore the next statement sets variable 'max' to 1 for the max()
 91811     ** aggregate, or 0 for min().
 91812     */
 91813     max = sqlite3_user_data(context)!=0;
 91814     cmp = sqlite3MemCompare(pBest, pArg, pColl);
 91815     if( (max && cmp<0) || (!max && cmp>0) ){
 91816       sqlite3VdbeMemCopy(pBest, pArg);
 91817     }else{
 91818       sqlite3SkipAccumulatorLoad(context);
 91820   }else{
 91821     sqlite3VdbeMemCopy(pBest, pArg);
 91824 static void minMaxFinalize(sqlite3_context *context){
 91825   sqlite3_value *pRes;
 91826   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
 91827   if( pRes ){
 91828     if( pRes->flags ){
 91829       sqlite3_result_value(context, pRes);
 91831     sqlite3VdbeMemRelease(pRes);
 91835 /*
 91836 ** group_concat(EXPR, ?SEPARATOR?)
 91837 */
 91838 static void groupConcatStep(
 91839   sqlite3_context *context,
 91840   int argc,
 91841   sqlite3_value **argv
 91842 ){
 91843   const char *zVal;
 91844   StrAccum *pAccum;
 91845   const char *zSep;
 91846   int nVal, nSep;
 91847   assert( argc==1 || argc==2 );
 91848   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
 91849   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
 91851   if( pAccum ){
 91852     sqlite3 *db = sqlite3_context_db_handle(context);
 91853     int firstTerm = pAccum->useMalloc==0;
 91854     pAccum->useMalloc = 2;
 91855     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
 91856     if( !firstTerm ){
 91857       if( argc==2 ){
 91858         zSep = (char*)sqlite3_value_text(argv[1]);
 91859         nSep = sqlite3_value_bytes(argv[1]);
 91860       }else{
 91861         zSep = ",";
 91862         nSep = 1;
 91864       if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
 91866     zVal = (char*)sqlite3_value_text(argv[0]);
 91867     nVal = sqlite3_value_bytes(argv[0]);
 91868     if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
 91871 static void groupConcatFinalize(sqlite3_context *context){
 91872   StrAccum *pAccum;
 91873   pAccum = sqlite3_aggregate_context(context, 0);
 91874   if( pAccum ){
 91875     if( pAccum->accError==STRACCUM_TOOBIG ){
 91876       sqlite3_result_error_toobig(context);
 91877     }else if( pAccum->accError==STRACCUM_NOMEM ){
 91878       sqlite3_result_error_nomem(context);
 91879     }else{    
 91880       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
 91881                           sqlite3_free);
 91886 /*
 91887 ** This routine does per-connection function registration.  Most
 91888 ** of the built-in functions above are part of the global function set.
 91889 ** This routine only deals with those that are not global.
 91890 */
 91891 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
 91892   int rc = sqlite3_overload_function(db, "MATCH", 2);
 91893   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
 91894   if( rc==SQLITE_NOMEM ){
 91895     db->mallocFailed = 1;
 91899 /*
 91900 ** Set the LIKEOPT flag on the 2-argument function with the given name.
 91901 */
 91902 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
 91903   FuncDef *pDef;
 91904   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
 91905                              2, SQLITE_UTF8, 0);
 91906   if( ALWAYS(pDef) ){
 91907     pDef->funcFlags |= flagVal;
 91911 /*
 91912 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
 91913 ** parameter determines whether or not the LIKE operator is case
 91914 ** sensitive.  GLOB is always case sensitive.
 91915 */
 91916 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
 91917   struct compareInfo *pInfo;
 91918   if( caseSensitive ){
 91919     pInfo = (struct compareInfo*)&likeInfoAlt;
 91920   }else{
 91921     pInfo = (struct compareInfo*)&likeInfoNorm;
 91923   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 91924   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
 91925   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
 91926       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
 91927   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
 91928   setLikeOptFlag(db, "like", 
 91929       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
 91932 /*
 91933 ** pExpr points to an expression which implements a function.  If
 91934 ** it is appropriate to apply the LIKE optimization to that function
 91935 ** then set aWc[0] through aWc[2] to the wildcard characters and
 91936 ** return TRUE.  If the function is not a LIKE-style function then
 91937 ** return FALSE.
 91938 */
 91939 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
 91940   FuncDef *pDef;
 91941   if( pExpr->op!=TK_FUNCTION 
 91942    || !pExpr->x.pList 
 91943    || pExpr->x.pList->nExpr!=2
 91944   ){
 91945     return 0;
 91947   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
 91948   pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
 91949                              sqlite3Strlen30(pExpr->u.zToken),
 91950                              2, SQLITE_UTF8, 0);
 91951   if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
 91952     return 0;
 91955   /* The memcpy() statement assumes that the wildcard characters are
 91956   ** the first three statements in the compareInfo structure.  The
 91957   ** asserts() that follow verify that assumption
 91958   */
 91959   memcpy(aWc, pDef->pUserData, 3);
 91960   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
 91961   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
 91962   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
 91963   *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
 91964   return 1;
 91967 /*
 91968 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
 91969 ** to the global function hash table.  This occurs at start-time (as
 91970 ** a consequence of calling sqlite3_initialize()).
 91971 **
 91972 ** After this routine runs
 91973 */
 91974 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
 91975   /*
 91976   ** The following array holds FuncDef structures for all of the functions
 91977   ** defined in this file.
 91978   **
 91979   ** The array cannot be constant since changes are made to the
 91980   ** FuncDef.pHash elements at start-time.  The elements of this array
 91981   ** are read-only after initialization is complete.
 91982   */
 91983   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
 91984     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
 91985     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
 91986     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
 91987     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
 91988     FUNCTION(trim,               1, 3, 0, trimFunc         ),
 91989     FUNCTION(trim,               2, 3, 0, trimFunc         ),
 91990     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
 91991     FUNCTION(min,                0, 0, 1, 0                ),
 91992     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
 91993     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
 91994     FUNCTION(max,                0, 1, 1, 0                ),
 91995     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
 91996     FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
 91997     FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
 91998     FUNCTION(instr,              2, 0, 0, instrFunc        ),
 91999     FUNCTION(substr,             2, 0, 0, substrFunc       ),
 92000     FUNCTION(substr,             3, 0, 0, substrFunc       ),
 92001     FUNCTION(printf,            -1, 0, 0, printfFunc       ),
 92002     FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
 92003     FUNCTION(char,              -1, 0, 0, charFunc         ),
 92004     FUNCTION(abs,                1, 0, 0, absFunc          ),
 92005 #ifndef SQLITE_OMIT_FLOATING_POINT
 92006     FUNCTION(round,              1, 0, 0, roundFunc        ),
 92007     FUNCTION(round,              2, 0, 0, roundFunc        ),
 92008 #endif
 92009     FUNCTION(upper,              1, 0, 0, upperFunc        ),
 92010     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
 92011     FUNCTION(coalesce,           1, 0, 0, 0                ),
 92012     FUNCTION(coalesce,           0, 0, 0, 0                ),
 92013     FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
 92014     FUNCTION(hex,                1, 0, 0, hexFunc          ),
 92015     FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
 92016     FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
 92017     FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
 92018     VFUNCTION(random,            0, 0, 0, randomFunc       ),
 92019     VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
 92020     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
 92021     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
 92022     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
 92023     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
 92024 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 92025     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
 92026     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
 92027 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 92028     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
 92029     VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
 92030     VFUNCTION(changes,           0, 0, 0, changes          ),
 92031     VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
 92032     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
 92033     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
 92034   #ifdef SQLITE_SOUNDEX
 92035     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
 92036   #endif
 92037   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 92038     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
 92039     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
 92040   #endif
 92041     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
 92042     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
 92043     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
 92044  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
 92045     {0,SQLITE_UTF8|SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
 92046     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
 92047     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
 92048     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
 92050     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 92051   #ifdef SQLITE_CASE_SENSITIVE_LIKE
 92052     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 92053     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
 92054   #else
 92055     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
 92056     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
 92057   #endif
 92058   };
 92060   int i;
 92061   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 92062   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
 92064   for(i=0; i<ArraySize(aBuiltinFunc); i++){
 92065     sqlite3FuncDefInsert(pHash, &aFunc[i]);
 92067   sqlite3RegisterDateTimeFunctions();
 92068 #ifndef SQLITE_OMIT_ALTERTABLE
 92069   sqlite3AlterFunctions();
 92070 #endif
 92071 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
 92072   sqlite3AnalyzeFunctions();
 92073 #endif
 92076 /************** End of func.c ************************************************/
 92077 /************** Begin file fkey.c ********************************************/
 92078 /*
 92079 **
 92080 ** The author disclaims copyright to this source code.  In place of
 92081 ** a legal notice, here is a blessing:
 92082 **
 92083 **    May you do good and not evil.
 92084 **    May you find forgiveness for yourself and forgive others.
 92085 **    May you share freely, never taking more than you give.
 92086 **
 92087 *************************************************************************
 92088 ** This file contains code used by the compiler to add foreign key
 92089 ** support to compiled SQL statements.
 92090 */
 92092 #ifndef SQLITE_OMIT_FOREIGN_KEY
 92093 #ifndef SQLITE_OMIT_TRIGGER
 92095 /*
 92096 ** Deferred and Immediate FKs
 92097 ** --------------------------
 92098 **
 92099 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
 92100 ** If an immediate foreign key constraint is violated,
 92101 ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
 92102 ** statement transaction rolled back. If a 
 92103 ** deferred foreign key constraint is violated, no action is taken 
 92104 ** immediately. However if the application attempts to commit the 
 92105 ** transaction before fixing the constraint violation, the attempt fails.
 92106 **
 92107 ** Deferred constraints are implemented using a simple counter associated
 92108 ** with the database handle. The counter is set to zero each time a 
 92109 ** database transaction is opened. Each time a statement is executed 
 92110 ** that causes a foreign key violation, the counter is incremented. Each
 92111 ** time a statement is executed that removes an existing violation from
 92112 ** the database, the counter is decremented. When the transaction is
 92113 ** committed, the commit fails if the current value of the counter is
 92114 ** greater than zero. This scheme has two big drawbacks:
 92115 **
 92116 **   * When a commit fails due to a deferred foreign key constraint, 
 92117 **     there is no way to tell which foreign constraint is not satisfied,
 92118 **     or which row it is not satisfied for.
 92119 **
 92120 **   * If the database contains foreign key violations when the 
 92121 **     transaction is opened, this may cause the mechanism to malfunction.
 92122 **
 92123 ** Despite these problems, this approach is adopted as it seems simpler
 92124 ** than the alternatives.
 92125 **
 92126 ** INSERT operations:
 92127 **
 92128 **   I.1) For each FK for which the table is the child table, search
 92129 **        the parent table for a match. If none is found increment the
 92130 **        constraint counter.
 92131 **
 92132 **   I.2) For each FK for which the table is the parent table, 
 92133 **        search the child table for rows that correspond to the new
 92134 **        row in the parent table. Decrement the counter for each row
 92135 **        found (as the constraint is now satisfied).
 92136 **
 92137 ** DELETE operations:
 92138 **
 92139 **   D.1) For each FK for which the table is the child table, 
 92140 **        search the parent table for a row that corresponds to the 
 92141 **        deleted row in the child table. If such a row is not found, 
 92142 **        decrement the counter.
 92143 **
 92144 **   D.2) For each FK for which the table is the parent table, search 
 92145 **        the child table for rows that correspond to the deleted row 
 92146 **        in the parent table. For each found increment the counter.
 92147 **
 92148 ** UPDATE operations:
 92149 **
 92150 **   An UPDATE command requires that all 4 steps above are taken, but only
 92151 **   for FK constraints for which the affected columns are actually 
 92152 **   modified (values must be compared at runtime).
 92153 **
 92154 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
 92155 ** This simplifies the implementation a bit.
 92156 **
 92157 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
 92158 ** resolution is considered to delete rows before the new row is inserted.
 92159 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
 92160 ** is thrown, even if the FK constraint would be satisfied after the new 
 92161 ** row is inserted.
 92162 **
 92163 ** Immediate constraints are usually handled similarly. The only difference 
 92164 ** is that the counter used is stored as part of each individual statement
 92165 ** object (struct Vdbe). If, after the statement has run, its immediate
 92166 ** constraint counter is greater than zero,
 92167 ** it returns SQLITE_CONSTRAINT_FOREIGNKEY
 92168 ** and the statement transaction is rolled back. An exception is an INSERT
 92169 ** statement that inserts a single row only (no triggers). In this case,
 92170 ** instead of using a counter, an exception is thrown immediately if the
 92171 ** INSERT violates a foreign key constraint. This is necessary as such
 92172 ** an INSERT does not open a statement transaction.
 92173 **
 92174 ** TODO: How should dropping a table be handled? How should renaming a 
 92175 ** table be handled?
 92176 **
 92177 **
 92178 ** Query API Notes
 92179 ** ---------------
 92180 **
 92181 ** Before coding an UPDATE or DELETE row operation, the code-generator
 92182 ** for those two operations needs to know whether or not the operation
 92183 ** requires any FK processing and, if so, which columns of the original
 92184 ** row are required by the FK processing VDBE code (i.e. if FKs were
 92185 ** implemented using triggers, which of the old.* columns would be 
 92186 ** accessed). No information is required by the code-generator before
 92187 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
 92188 ** generation code to query for this information are:
 92189 **
 92190 **   sqlite3FkRequired() - Test to see if FK processing is required.
 92191 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
 92192 **
 92193 **
 92194 ** Externally accessible module functions
 92195 ** --------------------------------------
 92196 **
 92197 **   sqlite3FkCheck()    - Check for foreign key violations.
 92198 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
 92199 **   sqlite3FkDelete()   - Delete an FKey structure.
 92200 */
 92202 /*
 92203 ** VDBE Calling Convention
 92204 ** -----------------------
 92205 **
 92206 ** Example:
 92207 **
 92208 **   For the following INSERT statement:
 92209 **
 92210 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
 92211 **     INSERT INTO t1 VALUES(1, 2, 3.1);
 92212 **
 92213 **   Register (x):        2    (type integer)
 92214 **   Register (x+1):      1    (type integer)
 92215 **   Register (x+2):      NULL (type NULL)
 92216 **   Register (x+3):      3.1  (type real)
 92217 */
 92219 /*
 92220 ** A foreign key constraint requires that the key columns in the parent
 92221 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
 92222 ** Given that pParent is the parent table for foreign key constraint pFKey, 
 92223 ** search the schema for a unique index on the parent key columns. 
 92224 **
 92225 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
 92226 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
 92227 ** is set to point to the unique index. 
 92228 ** 
 92229 ** If the parent key consists of a single column (the foreign key constraint
 92230 ** is not a composite foreign key), output variable *paiCol is set to NULL.
 92231 ** Otherwise, it is set to point to an allocated array of size N, where
 92232 ** N is the number of columns in the parent key. The first element of the
 92233 ** array is the index of the child table column that is mapped by the FK
 92234 ** constraint to the parent table column stored in the left-most column
 92235 ** of index *ppIdx. The second element of the array is the index of the
 92236 ** child table column that corresponds to the second left-most column of
 92237 ** *ppIdx, and so on.
 92238 **
 92239 ** If the required index cannot be found, either because:
 92240 **
 92241 **   1) The named parent key columns do not exist, or
 92242 **
 92243 **   2) The named parent key columns do exist, but are not subject to a
 92244 **      UNIQUE or PRIMARY KEY constraint, or
 92245 **
 92246 **   3) No parent key columns were provided explicitly as part of the
 92247 **      foreign key definition, and the parent table does not have a
 92248 **      PRIMARY KEY, or
 92249 **
 92250 **   4) No parent key columns were provided explicitly as part of the
 92251 **      foreign key definition, and the PRIMARY KEY of the parent table 
 92252 **      consists of a a different number of columns to the child key in 
 92253 **      the child table.
 92254 **
 92255 ** then non-zero is returned, and a "foreign key mismatch" error loaded
 92256 ** into pParse. If an OOM error occurs, non-zero is returned and the
 92257 ** pParse->db->mallocFailed flag is set.
 92258 */
 92259 SQLITE_PRIVATE int sqlite3FkLocateIndex(
 92260   Parse *pParse,                  /* Parse context to store any error in */
 92261   Table *pParent,                 /* Parent table of FK constraint pFKey */
 92262   FKey *pFKey,                    /* Foreign key to find index for */
 92263   Index **ppIdx,                  /* OUT: Unique index on parent table */
 92264   int **paiCol                    /* OUT: Map of index columns in pFKey */
 92265 ){
 92266   Index *pIdx = 0;                    /* Value to return via *ppIdx */
 92267   int *aiCol = 0;                     /* Value to return via *paiCol */
 92268   int nCol = pFKey->nCol;             /* Number of columns in parent key */
 92269   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
 92271   /* The caller is responsible for zeroing output parameters. */
 92272   assert( ppIdx && *ppIdx==0 );
 92273   assert( !paiCol || *paiCol==0 );
 92274   assert( pParse );
 92276   /* If this is a non-composite (single column) foreign key, check if it 
 92277   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
 92278   ** and *paiCol set to zero and return early. 
 92279   **
 92280   ** Otherwise, for a composite foreign key (more than one column), allocate
 92281   ** space for the aiCol array (returned via output parameter *paiCol).
 92282   ** Non-composite foreign keys do not require the aiCol array.
 92283   */
 92284   if( nCol==1 ){
 92285     /* The FK maps to the IPK if any of the following are true:
 92286     **
 92287     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
 92288     **      mapped to the primary key of table pParent, or
 92289     **   2) The FK is explicitly mapped to a column declared as INTEGER
 92290     **      PRIMARY KEY.
 92291     */
 92292     if( pParent->iPKey>=0 ){
 92293       if( !zKey ) return 0;
 92294       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
 92296   }else if( paiCol ){
 92297     assert( nCol>1 );
 92298     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
 92299     if( !aiCol ) return 1;
 92300     *paiCol = aiCol;
 92303   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
 92304     if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){ 
 92305       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
 92306       ** of columns. If each indexed column corresponds to a foreign key
 92307       ** column of pFKey, then this index is a winner.  */
 92309       if( zKey==0 ){
 92310         /* If zKey is NULL, then this foreign key is implicitly mapped to 
 92311         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
 92312         ** identified by the test (Index.autoIndex==2).  */
 92313         if( pIdx->autoIndex==2 ){
 92314           if( aiCol ){
 92315             int i;
 92316             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
 92318           break;
 92320       }else{
 92321         /* If zKey is non-NULL, then this foreign key was declared to
 92322         ** map to an explicit list of columns in table pParent. Check if this
 92323         ** index matches those columns. Also, check that the index uses
 92324         ** the default collation sequences for each column. */
 92325         int i, j;
 92326         for(i=0; i<nCol; i++){
 92327           i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
 92328           char *zDfltColl;                  /* Def. collation for column */
 92329           char *zIdxCol;                    /* Name of indexed column */
 92331           /* If the index uses a collation sequence that is different from
 92332           ** the default collation sequence for the column, this index is
 92333           ** unusable. Bail out early in this case.  */
 92334           zDfltColl = pParent->aCol[iCol].zColl;
 92335           if( !zDfltColl ){
 92336             zDfltColl = "BINARY";
 92338           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
 92340           zIdxCol = pParent->aCol[iCol].zName;
 92341           for(j=0; j<nCol; j++){
 92342             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
 92343               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
 92344               break;
 92347           if( j==nCol ) break;
 92349         if( i==nCol ) break;      /* pIdx is usable */
 92354   if( !pIdx ){
 92355     if( !pParse->disableTriggers ){
 92356       sqlite3ErrorMsg(pParse,
 92357            "foreign key mismatch - \"%w\" referencing \"%w\"",
 92358            pFKey->pFrom->zName, pFKey->zTo);
 92360     sqlite3DbFree(pParse->db, aiCol);
 92361     return 1;
 92364   *ppIdx = pIdx;
 92365   return 0;
 92368 /*
 92369 ** This function is called when a row is inserted into or deleted from the 
 92370 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
 92371 ** on the child table of pFKey, this function is invoked twice for each row
 92372 ** affected - once to "delete" the old row, and then again to "insert" the
 92373 ** new row.
 92374 **
 92375 ** Each time it is called, this function generates VDBE code to locate the
 92376 ** row in the parent table that corresponds to the row being inserted into 
 92377 ** or deleted from the child table. If the parent row can be found, no 
 92378 ** special action is taken. Otherwise, if the parent row can *not* be
 92379 ** found in the parent table:
 92380 **
 92381 **   Operation | FK type   | Action taken
 92382 **   --------------------------------------------------------------------------
 92383 **   INSERT      immediate   Increment the "immediate constraint counter".
 92384 **
 92385 **   DELETE      immediate   Decrement the "immediate constraint counter".
 92386 **
 92387 **   INSERT      deferred    Increment the "deferred constraint counter".
 92388 **
 92389 **   DELETE      deferred    Decrement the "deferred constraint counter".
 92390 **
 92391 ** These operations are identified in the comment at the top of this file 
 92392 ** (fkey.c) as "I.1" and "D.1".
 92393 */
 92394 static void fkLookupParent(
 92395   Parse *pParse,        /* Parse context */
 92396   int iDb,              /* Index of database housing pTab */
 92397   Table *pTab,          /* Parent table of FK pFKey */
 92398   Index *pIdx,          /* Unique index on parent key columns in pTab */
 92399   FKey *pFKey,          /* Foreign key constraint */
 92400   int *aiCol,           /* Map from parent key columns to child table columns */
 92401   int regData,          /* Address of array containing child table row */
 92402   int nIncr,            /* Increment constraint counter by this */
 92403   int isIgnore          /* If true, pretend pTab contains all NULL values */
 92404 ){
 92405   int i;                                    /* Iterator variable */
 92406   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
 92407   int iCur = pParse->nTab - 1;              /* Cursor number to use */
 92408   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
 92410   /* If nIncr is less than zero, then check at runtime if there are any
 92411   ** outstanding constraints to resolve. If there are not, there is no need
 92412   ** to check if deleting this row resolves any outstanding violations.
 92413   **
 92414   ** Check if any of the key columns in the child table row are NULL. If 
 92415   ** any are, then the constraint is considered satisfied. No need to 
 92416   ** search for a matching row in the parent table.  */
 92417   if( nIncr<0 ){
 92418     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
 92419     VdbeCoverage(v);
 92421   for(i=0; i<pFKey->nCol; i++){
 92422     int iReg = aiCol[i] + regData + 1;
 92423     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
 92426   if( isIgnore==0 ){
 92427     if( pIdx==0 ){
 92428       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
 92429       ** column of the parent table (table pTab).  */
 92430       int iMustBeInt;               /* Address of MustBeInt instruction */
 92431       int regTemp = sqlite3GetTempReg(pParse);
 92433       /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
 92434       ** apply the affinity of the parent key). If this fails, then there
 92435       ** is no matching parent key. Before using MustBeInt, make a copy of
 92436       ** the value. Otherwise, the value inserted into the child key column
 92437       ** will have INTEGER affinity applied to it, which may not be correct.  */
 92438       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
 92439       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
 92440       VdbeCoverage(v);
 92442       /* If the parent table is the same as the child table, and we are about
 92443       ** to increment the constraint-counter (i.e. this is an INSERT operation),
 92444       ** then check if the row being inserted matches itself. If so, do not
 92445       ** increment the constraint-counter.  */
 92446       if( pTab==pFKey->pFrom && nIncr==1 ){
 92447         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
 92448         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 92451       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
 92452       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
 92453       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 92454       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 92455       sqlite3VdbeJumpHere(v, iMustBeInt);
 92456       sqlite3ReleaseTempReg(pParse, regTemp);
 92457     }else{
 92458       int nCol = pFKey->nCol;
 92459       int regTemp = sqlite3GetTempRange(pParse, nCol);
 92460       int regRec = sqlite3GetTempReg(pParse);
 92462       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
 92463       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 92464       for(i=0; i<nCol; i++){
 92465         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
 92468       /* If the parent table is the same as the child table, and we are about
 92469       ** to increment the constraint-counter (i.e. this is an INSERT operation),
 92470       ** then check if the row being inserted matches itself. If so, do not
 92471       ** increment the constraint-counter. 
 92472       **
 92473       ** If any of the parent-key values are NULL, then the row cannot match 
 92474       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
 92475       ** of the parent-key values are NULL (at this point it is known that
 92476       ** none of the child key values are).
 92477       */
 92478       if( pTab==pFKey->pFrom && nIncr==1 ){
 92479         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
 92480         for(i=0; i<nCol; i++){
 92481           int iChild = aiCol[i]+1+regData;
 92482           int iParent = pIdx->aiColumn[i]+1+regData;
 92483           assert( aiCol[i]!=pTab->iPKey );
 92484           if( pIdx->aiColumn[i]==pTab->iPKey ){
 92485             /* The parent key is a composite key that includes the IPK column */
 92486             iParent = regData;
 92488           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
 92489           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 92491         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 92494       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
 92495                         sqlite3IndexAffinityStr(v,pIdx), nCol);
 92496       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
 92498       sqlite3ReleaseTempReg(pParse, regRec);
 92499       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
 92503   if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
 92504    && !pParse->pToplevel 
 92505    && !pParse->isMultiWrite 
 92506   ){
 92507     /* Special case: If this is an INSERT statement that will insert exactly
 92508     ** one row into the table, raise a constraint immediately instead of
 92509     ** incrementing a counter. This is necessary as the VM code is being
 92510     ** generated for will not open a statement transaction.  */
 92511     assert( nIncr==1 );
 92512     sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
 92513         OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
 92514   }else{
 92515     if( nIncr>0 && pFKey->isDeferred==0 ){
 92516       sqlite3ParseToplevel(pParse)->mayAbort = 1;
 92518     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 92521   sqlite3VdbeResolveLabel(v, iOk);
 92522   sqlite3VdbeAddOp1(v, OP_Close, iCur);
 92526 /*
 92527 ** Return an Expr object that refers to a memory register corresponding
 92528 ** to column iCol of table pTab.
 92529 **
 92530 ** regBase is the first of an array of register that contains the data
 92531 ** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
 92532 ** column.  regBase+2 holds the second column, and so forth.
 92533 */
 92534 static Expr *exprTableRegister(
 92535   Parse *pParse,     /* Parsing and code generating context */
 92536   Table *pTab,       /* The table whose content is at r[regBase]... */
 92537   int regBase,       /* Contents of table pTab */
 92538   i16 iCol           /* Which column of pTab is desired */
 92539 ){
 92540   Expr *pExpr;
 92541   Column *pCol;
 92542   const char *zColl;
 92543   sqlite3 *db = pParse->db;
 92545   pExpr = sqlite3Expr(db, TK_REGISTER, 0);
 92546   if( pExpr ){
 92547     if( iCol>=0 && iCol!=pTab->iPKey ){
 92548       pCol = &pTab->aCol[iCol];
 92549       pExpr->iTable = regBase + iCol + 1;
 92550       pExpr->affinity = pCol->affinity;
 92551       zColl = pCol->zColl;
 92552       if( zColl==0 ) zColl = db->pDfltColl->zName;
 92553       pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
 92554     }else{
 92555       pExpr->iTable = regBase;
 92556       pExpr->affinity = SQLITE_AFF_INTEGER;
 92559   return pExpr;
 92562 /*
 92563 ** Return an Expr object that refers to column iCol of table pTab which
 92564 ** has cursor iCur.
 92565 */
 92566 static Expr *exprTableColumn(
 92567   sqlite3 *db,      /* The database connection */
 92568   Table *pTab,      /* The table whose column is desired */
 92569   int iCursor,      /* The open cursor on the table */
 92570   i16 iCol          /* The column that is wanted */
 92571 ){
 92572   Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
 92573   if( pExpr ){
 92574     pExpr->pTab = pTab;
 92575     pExpr->iTable = iCursor;
 92576     pExpr->iColumn = iCol;
 92578   return pExpr;
 92581 /*
 92582 ** This function is called to generate code executed when a row is deleted
 92583 ** from the parent table of foreign key constraint pFKey and, if pFKey is 
 92584 ** deferred, when a row is inserted into the same table. When generating
 92585 ** code for an SQL UPDATE operation, this function may be called twice -
 92586 ** once to "delete" the old row and once to "insert" the new row.
 92587 **
 92588 ** The code generated by this function scans through the rows in the child
 92589 ** table that correspond to the parent table row being deleted or inserted.
 92590 ** For each child row found, one of the following actions is taken:
 92591 **
 92592 **   Operation | FK type   | Action taken
 92593 **   --------------------------------------------------------------------------
 92594 **   DELETE      immediate   Increment the "immediate constraint counter".
 92595 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 92596 **                           throw a "FOREIGN KEY constraint failed" exception.
 92597 **
 92598 **   INSERT      immediate   Decrement the "immediate constraint counter".
 92599 **
 92600 **   DELETE      deferred    Increment the "deferred constraint counter".
 92601 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
 92602 **                           throw a "FOREIGN KEY constraint failed" exception.
 92603 **
 92604 **   INSERT      deferred    Decrement the "deferred constraint counter".
 92605 **
 92606 ** These operations are identified in the comment at the top of this file 
 92607 ** (fkey.c) as "I.2" and "D.2".
 92608 */
 92609 static void fkScanChildren(
 92610   Parse *pParse,                  /* Parse context */
 92611   SrcList *pSrc,                  /* The child table to be scanned */
 92612   Table *pTab,                    /* The parent table */
 92613   Index *pIdx,                    /* Index on parent covering the foreign key */
 92614   FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
 92615   int *aiCol,                     /* Map from pIdx cols to child table cols */
 92616   int regData,                    /* Parent row data starts here */
 92617   int nIncr                       /* Amount to increment deferred counter by */
 92618 ){
 92619   sqlite3 *db = pParse->db;       /* Database handle */
 92620   int i;                          /* Iterator variable */
 92621   Expr *pWhere = 0;               /* WHERE clause to scan with */
 92622   NameContext sNameContext;       /* Context used to resolve WHERE clause */
 92623   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
 92624   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
 92625   Vdbe *v = sqlite3GetVdbe(pParse);
 92627   assert( pIdx==0 || pIdx->pTable==pTab );
 92628   assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
 92629   assert( pIdx!=0 || pFKey->nCol==1 );
 92630   assert( pIdx!=0 || HasRowid(pTab) );
 92632   if( nIncr<0 ){
 92633     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
 92634     VdbeCoverage(v);
 92637   /* Create an Expr object representing an SQL expression like:
 92638   **
 92639   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
 92640   **
 92641   ** The collation sequence used for the comparison should be that of
 92642   ** the parent key columns. The affinity of the parent key column should
 92643   ** be applied to each child key value before the comparison takes place.
 92644   */
 92645   for(i=0; i<pFKey->nCol; i++){
 92646     Expr *pLeft;                  /* Value from parent table row */
 92647     Expr *pRight;                 /* Column ref to child table */
 92648     Expr *pEq;                    /* Expression (pLeft = pRight) */
 92649     i16 iCol;                     /* Index of column in child table */ 
 92650     const char *zCol;             /* Name of column in child table */
 92652     iCol = pIdx ? pIdx->aiColumn[i] : -1;
 92653     pLeft = exprTableRegister(pParse, pTab, regData, iCol);
 92654     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 92655     assert( iCol>=0 );
 92656     zCol = pFKey->pFrom->aCol[iCol].zName;
 92657     pRight = sqlite3Expr(db, TK_ID, zCol);
 92658     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
 92659     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 92662   /* If the child table is the same as the parent table, then add terms
 92663   ** to the WHERE clause that prevent this entry from being scanned.
 92664   ** The added WHERE clause terms are like this:
 92665   **
 92666   **     $current_rowid!=rowid
 92667   **     NOT( $current_a==a AND $current_b==b AND ... )
 92668   **
 92669   ** The first form is used for rowid tables.  The second form is used
 92670   ** for WITHOUT ROWID tables.  In the second form, the primary key is
 92671   ** (a,b,...)
 92672   */
 92673   if( pTab==pFKey->pFrom && nIncr>0 ){
 92674     Expr *pNe;                    /* Expression (pLeft != pRight) */
 92675     Expr *pLeft;                  /* Value from parent table row */
 92676     Expr *pRight;                 /* Column ref to child table */
 92677     if( HasRowid(pTab) ){
 92678       pLeft = exprTableRegister(pParse, pTab, regData, -1);
 92679       pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
 92680       pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
 92681     }else{
 92682       Expr *pEq, *pAll = 0;
 92683       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
 92684       assert( pIdx!=0 );
 92685       for(i=0; i<pPk->nKeyCol; i++){
 92686         i16 iCol = pIdx->aiColumn[i];
 92687         pLeft = exprTableRegister(pParse, pTab, regData, iCol);
 92688         pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
 92689         pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
 92690         pAll = sqlite3ExprAnd(db, pAll, pEq);
 92692       pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
 92694     pWhere = sqlite3ExprAnd(db, pWhere, pNe);
 92697   /* Resolve the references in the WHERE clause. */
 92698   memset(&sNameContext, 0, sizeof(NameContext));
 92699   sNameContext.pSrcList = pSrc;
 92700   sNameContext.pParse = pParse;
 92701   sqlite3ResolveExprNames(&sNameContext, pWhere);
 92703   /* Create VDBE to loop through the entries in pSrc that match the WHERE
 92704   ** clause. If the constraint is not deferred, throw an exception for
 92705   ** each row found. Otherwise, for deferred constraints, increment the
 92706   ** deferred constraint counter by nIncr for each row selected.  */
 92707   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
 92708   if( nIncr>0 && pFKey->isDeferred==0 ){
 92709     sqlite3ParseToplevel(pParse)->mayAbort = 1;
 92711   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
 92712   if( pWInfo ){
 92713     sqlite3WhereEnd(pWInfo);
 92716   /* Clean up the WHERE clause constructed above. */
 92717   sqlite3ExprDelete(db, pWhere);
 92718   if( iFkIfZero ){
 92719     sqlite3VdbeJumpHere(v, iFkIfZero);
 92723 /*
 92724 ** This function returns a linked list of FKey objects (connected by
 92725 ** FKey.pNextTo) holding all children of table pTab.  For example,
 92726 ** given the following schema:
 92727 **
 92728 **   CREATE TABLE t1(a PRIMARY KEY);
 92729 **   CREATE TABLE t2(b REFERENCES t1(a);
 92730 **
 92731 ** Calling this function with table "t1" as an argument returns a pointer
 92732 ** to the FKey structure representing the foreign key constraint on table
 92733 ** "t2". Calling this function with "t2" as the argument would return a
 92734 ** NULL pointer (as there are no FK constraints for which t2 is the parent
 92735 ** table).
 92736 */
 92737 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
 92738   int nName = sqlite3Strlen30(pTab->zName);
 92739   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
 92742 /*
 92743 ** The second argument is a Trigger structure allocated by the 
 92744 ** fkActionTrigger() routine. This function deletes the Trigger structure
 92745 ** and all of its sub-components.
 92746 **
 92747 ** The Trigger structure or any of its sub-components may be allocated from
 92748 ** the lookaside buffer belonging to database handle dbMem.
 92749 */
 92750 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
 92751   if( p ){
 92752     TriggerStep *pStep = p->step_list;
 92753     sqlite3ExprDelete(dbMem, pStep->pWhere);
 92754     sqlite3ExprListDelete(dbMem, pStep->pExprList);
 92755     sqlite3SelectDelete(dbMem, pStep->pSelect);
 92756     sqlite3ExprDelete(dbMem, p->pWhen);
 92757     sqlite3DbFree(dbMem, p);
 92761 /*
 92762 ** This function is called to generate code that runs when table pTab is
 92763 ** being dropped from the database. The SrcList passed as the second argument
 92764 ** to this function contains a single entry guaranteed to resolve to
 92765 ** table pTab.
 92766 **
 92767 ** Normally, no code is required. However, if either
 92768 **
 92769 **   (a) The table is the parent table of a FK constraint, or
 92770 **   (b) The table is the child table of a deferred FK constraint and it is
 92771 **       determined at runtime that there are outstanding deferred FK 
 92772 **       constraint violations in the database,
 92773 **
 92774 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
 92775 ** the table from the database. Triggers are disabled while running this
 92776 ** DELETE, but foreign key actions are not.
 92777 */
 92778 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
 92779   sqlite3 *db = pParse->db;
 92780   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
 92781     int iSkip = 0;
 92782     Vdbe *v = sqlite3GetVdbe(pParse);
 92784     assert( v );                  /* VDBE has already been allocated */
 92785     if( sqlite3FkReferences(pTab)==0 ){
 92786       /* Search for a deferred foreign key constraint for which this table
 92787       ** is the child table. If one cannot be found, return without 
 92788       ** generating any VDBE code. If one can be found, then jump over
 92789       ** the entire DELETE if there are no outstanding deferred constraints
 92790       ** when this statement is run.  */
 92791       FKey *p;
 92792       for(p=pTab->pFKey; p; p=p->pNextFrom){
 92793         if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
 92795       if( !p ) return;
 92796       iSkip = sqlite3VdbeMakeLabel(v);
 92797       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
 92800     pParse->disableTriggers = 1;
 92801     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
 92802     pParse->disableTriggers = 0;
 92804     /* If the DELETE has generated immediate foreign key constraint 
 92805     ** violations, halt the VDBE and return an error at this point, before
 92806     ** any modifications to the schema are made. This is because statement
 92807     ** transactions are not able to rollback schema changes.  
 92808     **
 92809     ** If the SQLITE_DeferFKs flag is set, then this is not required, as
 92810     ** the statement transaction will not be rolled back even if FK
 92811     ** constraints are violated.
 92812     */
 92813     if( (db->flags & SQLITE_DeferFKs)==0 ){
 92814       sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
 92815       VdbeCoverage(v);
 92816       sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
 92817           OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
 92820     if( iSkip ){
 92821       sqlite3VdbeResolveLabel(v, iSkip);
 92827 /*
 92828 ** The second argument points to an FKey object representing a foreign key
 92829 ** for which pTab is the child table. An UPDATE statement against pTab
 92830 ** is currently being processed. For each column of the table that is 
 92831 ** actually updated, the corresponding element in the aChange[] array
 92832 ** is zero or greater (if a column is unmodified the corresponding element
 92833 ** is set to -1). If the rowid column is modified by the UPDATE statement
 92834 ** the bChngRowid argument is non-zero.
 92835 **
 92836 ** This function returns true if any of the columns that are part of the
 92837 ** child key for FK constraint *p are modified.
 92838 */
 92839 static int fkChildIsModified(
 92840   Table *pTab,                    /* Table being updated */
 92841   FKey *p,                        /* Foreign key for which pTab is the child */
 92842   int *aChange,                   /* Array indicating modified columns */
 92843   int bChngRowid                  /* True if rowid is modified by this update */
 92844 ){
 92845   int i;
 92846   for(i=0; i<p->nCol; i++){
 92847     int iChildKey = p->aCol[i].iFrom;
 92848     if( aChange[iChildKey]>=0 ) return 1;
 92849     if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
 92851   return 0;
 92854 /*
 92855 ** The second argument points to an FKey object representing a foreign key
 92856 ** for which pTab is the parent table. An UPDATE statement against pTab
 92857 ** is currently being processed. For each column of the table that is 
 92858 ** actually updated, the corresponding element in the aChange[] array
 92859 ** is zero or greater (if a column is unmodified the corresponding element
 92860 ** is set to -1). If the rowid column is modified by the UPDATE statement
 92861 ** the bChngRowid argument is non-zero.
 92862 **
 92863 ** This function returns true if any of the columns that are part of the
 92864 ** parent key for FK constraint *p are modified.
 92865 */
 92866 static int fkParentIsModified(
 92867   Table *pTab, 
 92868   FKey *p, 
 92869   int *aChange, 
 92870   int bChngRowid
 92871 ){
 92872   int i;
 92873   for(i=0; i<p->nCol; i++){
 92874     char *zKey = p->aCol[i].zCol;
 92875     int iKey;
 92876     for(iKey=0; iKey<pTab->nCol; iKey++){
 92877       if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
 92878         Column *pCol = &pTab->aCol[iKey];
 92879         if( zKey ){
 92880           if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
 92881         }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
 92882           return 1;
 92887   return 0;
 92890 /*
 92891 ** This function is called when inserting, deleting or updating a row of
 92892 ** table pTab to generate VDBE code to perform foreign key constraint 
 92893 ** processing for the operation.
 92894 **
 92895 ** For a DELETE operation, parameter regOld is passed the index of the
 92896 ** first register in an array of (pTab->nCol+1) registers containing the
 92897 ** rowid of the row being deleted, followed by each of the column values
 92898 ** of the row being deleted, from left to right. Parameter regNew is passed
 92899 ** zero in this case.
 92900 **
 92901 ** For an INSERT operation, regOld is passed zero and regNew is passed the
 92902 ** first register of an array of (pTab->nCol+1) registers containing the new
 92903 ** row data.
 92904 **
 92905 ** For an UPDATE operation, this function is called twice. Once before
 92906 ** the original record is deleted from the table using the calling convention
 92907 ** described for DELETE. Then again after the original record is deleted
 92908 ** but before the new record is inserted using the INSERT convention. 
 92909 */
 92910 SQLITE_PRIVATE void sqlite3FkCheck(
 92911   Parse *pParse,                  /* Parse context */
 92912   Table *pTab,                    /* Row is being deleted from this table */ 
 92913   int regOld,                     /* Previous row data is stored here */
 92914   int regNew,                     /* New row data is stored here */
 92915   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
 92916   int bChngRowid                  /* True if rowid is UPDATEd */
 92917 ){
 92918   sqlite3 *db = pParse->db;       /* Database handle */
 92919   FKey *pFKey;                    /* Used to iterate through FKs */
 92920   int iDb;                        /* Index of database containing pTab */
 92921   const char *zDb;                /* Name of database containing pTab */
 92922   int isIgnoreErrors = pParse->disableTriggers;
 92924   /* Exactly one of regOld and regNew should be non-zero. */
 92925   assert( (regOld==0)!=(regNew==0) );
 92927   /* If foreign-keys are disabled, this function is a no-op. */
 92928   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
 92930   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 92931   zDb = db->aDb[iDb].zName;
 92933   /* Loop through all the foreign key constraints for which pTab is the
 92934   ** child table (the table that the foreign key definition is part of).  */
 92935   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 92936     Table *pTo;                   /* Parent table of foreign key pFKey */
 92937     Index *pIdx = 0;              /* Index on key columns in pTo */
 92938     int *aiFree = 0;
 92939     int *aiCol;
 92940     int iCol;
 92941     int i;
 92942     int isIgnore = 0;
 92944     if( aChange 
 92945      && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
 92946      && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0 
 92947     ){
 92948       continue;
 92951     /* Find the parent table of this foreign key. Also find a unique index 
 92952     ** on the parent key columns in the parent table. If either of these 
 92953     ** schema items cannot be located, set an error in pParse and return 
 92954     ** early.  */
 92955     if( pParse->disableTriggers ){
 92956       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
 92957     }else{
 92958       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
 92960     if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 92961       assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
 92962       if( !isIgnoreErrors || db->mallocFailed ) return;
 92963       if( pTo==0 ){
 92964         /* If isIgnoreErrors is true, then a table is being dropped. In this
 92965         ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
 92966         ** before actually dropping it in order to check FK constraints.
 92967         ** If the parent table of an FK constraint on the current table is
 92968         ** missing, behave as if it is empty. i.e. decrement the relevant
 92969         ** FK counter for each row of the current table with non-NULL keys.
 92970         */
 92971         Vdbe *v = sqlite3GetVdbe(pParse);
 92972         int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
 92973         for(i=0; i<pFKey->nCol; i++){
 92974           int iReg = pFKey->aCol[i].iFrom + regOld + 1;
 92975           sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
 92977         sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
 92979       continue;
 92981     assert( pFKey->nCol==1 || (aiFree && pIdx) );
 92983     if( aiFree ){
 92984       aiCol = aiFree;
 92985     }else{
 92986       iCol = pFKey->aCol[0].iFrom;
 92987       aiCol = &iCol;
 92989     for(i=0; i<pFKey->nCol; i++){
 92990       if( aiCol[i]==pTab->iPKey ){
 92991         aiCol[i] = -1;
 92993 #ifndef SQLITE_OMIT_AUTHORIZATION
 92994       /* Request permission to read the parent key columns. If the 
 92995       ** authorization callback returns SQLITE_IGNORE, behave as if any
 92996       ** values read from the parent table are NULL. */
 92997       if( db->xAuth ){
 92998         int rcauth;
 92999         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
 93000         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
 93001         isIgnore = (rcauth==SQLITE_IGNORE);
 93003 #endif
 93006     /* Take a shared-cache advisory read-lock on the parent table. Allocate 
 93007     ** a cursor to use to search the unique index on the parent key columns 
 93008     ** in the parent table.  */
 93009     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
 93010     pParse->nTab++;
 93012     if( regOld!=0 ){
 93013       /* A row is being removed from the child table. Search for the parent.
 93014       ** If the parent does not exist, removing the child row resolves an 
 93015       ** outstanding foreign key constraint violation. */
 93016       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
 93018     if( regNew!=0 ){
 93019       /* A row is being added to the child table. If a parent row cannot
 93020       ** be found, adding the child row has violated the FK constraint. */ 
 93021       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
 93024     sqlite3DbFree(db, aiFree);
 93027   /* Loop through all the foreign key constraints that refer to this table.
 93028   ** (the "child" constraints) */
 93029   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 93030     Index *pIdx = 0;              /* Foreign key index for pFKey */
 93031     SrcList *pSrc;
 93032     int *aiCol = 0;
 93034     if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
 93035       continue;
 93038     if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs) 
 93039      && !pParse->pToplevel && !pParse->isMultiWrite 
 93040     ){
 93041       assert( regOld==0 && regNew!=0 );
 93042       /* Inserting a single row into a parent table cannot cause an immediate
 93043       ** foreign key violation. So do nothing in this case.  */
 93044       continue;
 93047     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
 93048       if( !isIgnoreErrors || db->mallocFailed ) return;
 93049       continue;
 93051     assert( aiCol || pFKey->nCol==1 );
 93053     /* Create a SrcList structure containing the child table.  We need the
 93054     ** child table as a SrcList for sqlite3WhereBegin() */
 93055     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 93056     if( pSrc ){
 93057       struct SrcList_item *pItem = pSrc->a;
 93058       pItem->pTab = pFKey->pFrom;
 93059       pItem->zName = pFKey->pFrom->zName;
 93060       pItem->pTab->nRef++;
 93061       pItem->iCursor = pParse->nTab++;
 93063       if( regNew!=0 ){
 93064         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
 93066       if( regOld!=0 ){
 93067         /* If there is a RESTRICT action configured for the current operation
 93068         ** on the parent table of this FK, then throw an exception 
 93069         ** immediately if the FK constraint is violated, even if this is a
 93070         ** deferred trigger. That's what RESTRICT means. To defer checking
 93071         ** the constraint, the FK should specify NO ACTION (represented
 93072         ** using OE_None). NO ACTION is the default.  */
 93073         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
 93075       pItem->zName = 0;
 93076       sqlite3SrcListDelete(db, pSrc);
 93078     sqlite3DbFree(db, aiCol);
 93082 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
 93084 /*
 93085 ** This function is called before generating code to update or delete a 
 93086 ** row contained in table pTab.
 93087 */
 93088 SQLITE_PRIVATE u32 sqlite3FkOldmask(
 93089   Parse *pParse,                  /* Parse context */
 93090   Table *pTab                     /* Table being modified */
 93091 ){
 93092   u32 mask = 0;
 93093   if( pParse->db->flags&SQLITE_ForeignKeys ){
 93094     FKey *p;
 93095     int i;
 93096     for(p=pTab->pFKey; p; p=p->pNextFrom){
 93097       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
 93099     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 93100       Index *pIdx = 0;
 93101       sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
 93102       if( pIdx ){
 93103         for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
 93107   return mask;
 93111 /*
 93112 ** This function is called before generating code to update or delete a 
 93113 ** row contained in table pTab. If the operation is a DELETE, then
 93114 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
 93115 ** to an array of size N, where N is the number of columns in table pTab.
 93116 ** If the i'th column is not modified by the UPDATE, then the corresponding 
 93117 ** entry in the aChange[] array is set to -1. If the column is modified,
 93118 ** the value is 0 or greater. Parameter chngRowid is set to true if the
 93119 ** UPDATE statement modifies the rowid fields of the table.
 93120 **
 93121 ** If any foreign key processing will be required, this function returns
 93122 ** true. If there is no foreign key related processing, this function 
 93123 ** returns false.
 93124 */
 93125 SQLITE_PRIVATE int sqlite3FkRequired(
 93126   Parse *pParse,                  /* Parse context */
 93127   Table *pTab,                    /* Table being modified */
 93128   int *aChange,                   /* Non-NULL for UPDATE operations */
 93129   int chngRowid                   /* True for UPDATE that affects rowid */
 93130 ){
 93131   if( pParse->db->flags&SQLITE_ForeignKeys ){
 93132     if( !aChange ){
 93133       /* A DELETE operation. Foreign key processing is required if the 
 93134       ** table in question is either the child or parent table for any 
 93135       ** foreign key constraint.  */
 93136       return (sqlite3FkReferences(pTab) || pTab->pFKey);
 93137     }else{
 93138       /* This is an UPDATE. Foreign key processing is only required if the
 93139       ** operation modifies one or more child or parent key columns. */
 93140       FKey *p;
 93142       /* Check if any child key columns are being modified. */
 93143       for(p=pTab->pFKey; p; p=p->pNextFrom){
 93144         if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
 93147       /* Check if any parent key columns are being modified. */
 93148       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
 93149         if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
 93153   return 0;
 93156 /*
 93157 ** This function is called when an UPDATE or DELETE operation is being 
 93158 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
 93159 ** If the current operation is an UPDATE, then the pChanges parameter is
 93160 ** passed a pointer to the list of columns being modified. If it is a
 93161 ** DELETE, pChanges is passed a NULL pointer.
 93162 **
 93163 ** It returns a pointer to a Trigger structure containing a trigger
 93164 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
 93165 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
 93166 ** returned (these actions require no special handling by the triggers
 93167 ** sub-system, code for them is created by fkScanChildren()).
 93168 **
 93169 ** For example, if pFKey is the foreign key and pTab is table "p" in 
 93170 ** the following schema:
 93171 **
 93172 **   CREATE TABLE p(pk PRIMARY KEY);
 93173 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
 93174 **
 93175 ** then the returned trigger structure is equivalent to:
 93176 **
 93177 **   CREATE TRIGGER ... DELETE ON p BEGIN
 93178 **     DELETE FROM c WHERE ck = old.pk;
 93179 **   END;
 93180 **
 93181 ** The returned pointer is cached as part of the foreign key object. It
 93182 ** is eventually freed along with the rest of the foreign key object by 
 93183 ** sqlite3FkDelete().
 93184 */
 93185 static Trigger *fkActionTrigger(
 93186   Parse *pParse,                  /* Parse context */
 93187   Table *pTab,                    /* Table being updated or deleted from */
 93188   FKey *pFKey,                    /* Foreign key to get action for */
 93189   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
 93190 ){
 93191   sqlite3 *db = pParse->db;       /* Database handle */
 93192   int action;                     /* One of OE_None, OE_Cascade etc. */
 93193   Trigger *pTrigger;              /* Trigger definition to return */
 93194   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
 93196   action = pFKey->aAction[iAction];
 93197   pTrigger = pFKey->apTrigger[iAction];
 93199   if( action!=OE_None && !pTrigger ){
 93200     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
 93201     char const *zFrom;            /* Name of child table */
 93202     int nFrom;                    /* Length in bytes of zFrom */
 93203     Index *pIdx = 0;              /* Parent key index for this FK */
 93204     int *aiCol = 0;               /* child table cols -> parent key cols */
 93205     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
 93206     Expr *pWhere = 0;             /* WHERE clause of trigger step */
 93207     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
 93208     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
 93209     int i;                        /* Iterator variable */
 93210     Expr *pWhen = 0;              /* WHEN clause for the trigger */
 93212     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
 93213     assert( aiCol || pFKey->nCol==1 );
 93215     for(i=0; i<pFKey->nCol; i++){
 93216       Token tOld = { "old", 3 };  /* Literal "old" token */
 93217       Token tNew = { "new", 3 };  /* Literal "new" token */
 93218       Token tFromCol;             /* Name of column in child table */
 93219       Token tToCol;               /* Name of column in parent table */
 93220       int iFromCol;               /* Idx of column in child table */
 93221       Expr *pEq;                  /* tFromCol = OLD.tToCol */
 93223       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
 93224       assert( iFromCol>=0 );
 93225       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
 93226       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
 93228       tToCol.n = sqlite3Strlen30(tToCol.z);
 93229       tFromCol.n = sqlite3Strlen30(tFromCol.z);
 93231       /* Create the expression "OLD.zToCol = zFromCol". It is important
 93232       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
 93233       ** that the affinity and collation sequence associated with the
 93234       ** parent table are used for the comparison. */
 93235       pEq = sqlite3PExpr(pParse, TK_EQ,
 93236           sqlite3PExpr(pParse, TK_DOT, 
 93237             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 93238             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 93239           , 0),
 93240           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
 93241       , 0);
 93242       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
 93244       /* For ON UPDATE, construct the next term of the WHEN clause.
 93245       ** The final WHEN clause will be like this:
 93246       **
 93247       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
 93248       */
 93249       if( pChanges ){
 93250         pEq = sqlite3PExpr(pParse, TK_IS,
 93251             sqlite3PExpr(pParse, TK_DOT, 
 93252               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
 93253               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 93254               0),
 93255             sqlite3PExpr(pParse, TK_DOT, 
 93256               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 93257               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
 93258               0),
 93259             0);
 93260         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
 93263       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
 93264         Expr *pNew;
 93265         if( action==OE_Cascade ){
 93266           pNew = sqlite3PExpr(pParse, TK_DOT, 
 93267             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
 93268             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
 93269           , 0);
 93270         }else if( action==OE_SetDflt ){
 93271           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
 93272           if( pDflt ){
 93273             pNew = sqlite3ExprDup(db, pDflt, 0);
 93274           }else{
 93275             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 93277         }else{
 93278           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
 93280         pList = sqlite3ExprListAppend(pParse, pList, pNew);
 93281         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
 93284     sqlite3DbFree(db, aiCol);
 93286     zFrom = pFKey->pFrom->zName;
 93287     nFrom = sqlite3Strlen30(zFrom);
 93289     if( action==OE_Restrict ){
 93290       Token tFrom;
 93291       Expr *pRaise; 
 93293       tFrom.z = zFrom;
 93294       tFrom.n = nFrom;
 93295       pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
 93296       if( pRaise ){
 93297         pRaise->affinity = OE_Abort;
 93299       pSelect = sqlite3SelectNew(pParse, 
 93300           sqlite3ExprListAppend(pParse, 0, pRaise),
 93301           sqlite3SrcListAppend(db, 0, &tFrom, 0),
 93302           pWhere,
 93303           0, 0, 0, 0, 0, 0
 93304       );
 93305       pWhere = 0;
 93308     /* Disable lookaside memory allocation */
 93309     enableLookaside = db->lookaside.bEnabled;
 93310     db->lookaside.bEnabled = 0;
 93312     pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
 93313         sizeof(Trigger) +         /* struct Trigger */
 93314         sizeof(TriggerStep) +     /* Single step in trigger program */
 93315         nFrom + 1                 /* Space for pStep->target.z */
 93316     );
 93317     if( pTrigger ){
 93318       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
 93319       pStep->target.z = (char *)&pStep[1];
 93320       pStep->target.n = nFrom;
 93321       memcpy((char *)pStep->target.z, zFrom, nFrom);
 93323       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 93324       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
 93325       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 93326       if( pWhen ){
 93327         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
 93328         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 93332     /* Re-enable the lookaside buffer, if it was disabled earlier. */
 93333     db->lookaside.bEnabled = enableLookaside;
 93335     sqlite3ExprDelete(db, pWhere);
 93336     sqlite3ExprDelete(db, pWhen);
 93337     sqlite3ExprListDelete(db, pList);
 93338     sqlite3SelectDelete(db, pSelect);
 93339     if( db->mallocFailed==1 ){
 93340       fkTriggerDelete(db, pTrigger);
 93341       return 0;
 93343     assert( pStep!=0 );
 93345     switch( action ){
 93346       case OE_Restrict:
 93347         pStep->op = TK_SELECT; 
 93348         break;
 93349       case OE_Cascade: 
 93350         if( !pChanges ){ 
 93351           pStep->op = TK_DELETE; 
 93352           break; 
 93354       default:
 93355         pStep->op = TK_UPDATE;
 93357     pStep->pTrig = pTrigger;
 93358     pTrigger->pSchema = pTab->pSchema;
 93359     pTrigger->pTabSchema = pTab->pSchema;
 93360     pFKey->apTrigger[iAction] = pTrigger;
 93361     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
 93364   return pTrigger;
 93367 /*
 93368 ** This function is called when deleting or updating a row to implement
 93369 ** any required CASCADE, SET NULL or SET DEFAULT actions.
 93370 */
 93371 SQLITE_PRIVATE void sqlite3FkActions(
 93372   Parse *pParse,                  /* Parse context */
 93373   Table *pTab,                    /* Table being updated or deleted from */
 93374   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
 93375   int regOld,                     /* Address of array containing old row */
 93376   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
 93377   int bChngRowid                  /* True if rowid is UPDATEd */
 93378 ){
 93379   /* If foreign-key support is enabled, iterate through all FKs that 
 93380   ** refer to table pTab. If there is an action associated with the FK 
 93381   ** for this operation (either update or delete), invoke the associated 
 93382   ** trigger sub-program.  */
 93383   if( pParse->db->flags&SQLITE_ForeignKeys ){
 93384     FKey *pFKey;                  /* Iterator variable */
 93385     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
 93386       if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
 93387         Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
 93388         if( pAct ){
 93389           sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
 93396 #endif /* ifndef SQLITE_OMIT_TRIGGER */
 93398 /*
 93399 ** Free all memory associated with foreign key definitions attached to
 93400 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
 93401 ** hash table.
 93402 */
 93403 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
 93404   FKey *pFKey;                    /* Iterator variable */
 93405   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
 93407   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
 93408   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
 93410     /* Remove the FK from the fkeyHash hash table. */
 93411     if( !db || db->pnBytesFreed==0 ){
 93412       if( pFKey->pPrevTo ){
 93413         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
 93414       }else{
 93415         void *p = (void *)pFKey->pNextTo;
 93416         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
 93417         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
 93419       if( pFKey->pNextTo ){
 93420         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
 93424     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
 93425     ** classified as either immediate or deferred.
 93426     */
 93427     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
 93429     /* Delete any triggers created to implement actions for this FK. */
 93430 #ifndef SQLITE_OMIT_TRIGGER
 93431     fkTriggerDelete(db, pFKey->apTrigger[0]);
 93432     fkTriggerDelete(db, pFKey->apTrigger[1]);
 93433 #endif
 93435     pNext = pFKey->pNextFrom;
 93436     sqlite3DbFree(db, pFKey);
 93439 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
 93441 /************** End of fkey.c ************************************************/
 93442 /************** Begin file insert.c ******************************************/
 93443 /*
 93444 ** 2001 September 15
 93445 **
 93446 ** The author disclaims copyright to this source code.  In place of
 93447 ** a legal notice, here is a blessing:
 93448 **
 93449 **    May you do good and not evil.
 93450 **    May you find forgiveness for yourself and forgive others.
 93451 **    May you share freely, never taking more than you give.
 93452 **
 93453 *************************************************************************
 93454 ** This file contains C code routines that are called by the parser
 93455 ** to handle INSERT statements in SQLite.
 93456 */
 93458 /*
 93459 ** Generate code that will 
 93460 **
 93461 **   (1) acquire a lock for table pTab then
 93462 **   (2) open pTab as cursor iCur.
 93463 **
 93464 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
 93465 ** for that table that is actually opened.
 93466 */
 93467 SQLITE_PRIVATE void sqlite3OpenTable(
 93468   Parse *pParse,  /* Generate code into this VDBE */
 93469   int iCur,       /* The cursor number of the table */
 93470   int iDb,        /* The database index in sqlite3.aDb[] */
 93471   Table *pTab,    /* The table to be opened */
 93472   int opcode      /* OP_OpenRead or OP_OpenWrite */
 93473 ){
 93474   Vdbe *v;
 93475   assert( !IsVirtual(pTab) );
 93476   v = sqlite3GetVdbe(pParse);
 93477   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
 93478   sqlite3TableLock(pParse, iDb, pTab->tnum, 
 93479                    (opcode==OP_OpenWrite)?1:0, pTab->zName);
 93480   if( HasRowid(pTab) ){
 93481     sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
 93482     VdbeComment((v, "%s", pTab->zName));
 93483   }else{
 93484     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
 93485     assert( pPk!=0 );
 93486     assert( pPk->tnum=pTab->tnum );
 93487     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
 93488     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
 93489     VdbeComment((v, "%s", pTab->zName));
 93493 /*
 93494 ** Return a pointer to the column affinity string associated with index
 93495 ** pIdx. A column affinity string has one character for each column in 
 93496 ** the table, according to the affinity of the column:
 93497 **
 93498 **  Character      Column affinity
 93499 **  ------------------------------
 93500 **  'a'            TEXT
 93501 **  'b'            NONE
 93502 **  'c'            NUMERIC
 93503 **  'd'            INTEGER
 93504 **  'e'            REAL
 93505 **
 93506 ** An extra 'd' is appended to the end of the string to cover the
 93507 ** rowid that appears as the last column in every index.
 93508 **
 93509 ** Memory for the buffer containing the column index affinity string
 93510 ** is managed along with the rest of the Index structure. It will be
 93511 ** released when sqlite3DeleteIndex() is called.
 93512 */
 93513 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
 93514   if( !pIdx->zColAff ){
 93515     /* The first time a column affinity string for a particular index is
 93516     ** required, it is allocated and populated here. It is then stored as
 93517     ** a member of the Index structure for subsequent use.
 93518     **
 93519     ** The column affinity string will eventually be deleted by
 93520     ** sqliteDeleteIndex() when the Index structure itself is cleaned
 93521     ** up.
 93522     */
 93523     int n;
 93524     Table *pTab = pIdx->pTable;
 93525     sqlite3 *db = sqlite3VdbeDb(v);
 93526     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
 93527     if( !pIdx->zColAff ){
 93528       db->mallocFailed = 1;
 93529       return 0;
 93531     for(n=0; n<pIdx->nColumn; n++){
 93532       i16 x = pIdx->aiColumn[n];
 93533       pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
 93535     pIdx->zColAff[n] = 0;
 93538   return pIdx->zColAff;
 93541 /*
 93542 ** Compute the affinity string for table pTab, if it has not already been
 93543 ** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
 93544 **
 93545 ** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
 93546 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
 93547 ** for register iReg and following.  Or if affinities exists and iReg==0,
 93548 ** then just set the P4 operand of the previous opcode (which should  be
 93549 ** an OP_MakeRecord) to the affinity string.
 93550 **
 93551 ** A column affinity string has one character per column:
 93552 **
 93553 **  Character      Column affinity
 93554 **  ------------------------------
 93555 **  'a'            TEXT
 93556 **  'b'            NONE
 93557 **  'c'            NUMERIC
 93558 **  'd'            INTEGER
 93559 **  'e'            REAL
 93560 */
 93561 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
 93562   int i;
 93563   char *zColAff = pTab->zColAff;
 93564   if( zColAff==0 ){
 93565     sqlite3 *db = sqlite3VdbeDb(v);
 93566     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
 93567     if( !zColAff ){
 93568       db->mallocFailed = 1;
 93569       return;
 93572     for(i=0; i<pTab->nCol; i++){
 93573       zColAff[i] = pTab->aCol[i].affinity;
 93575     do{
 93576       zColAff[i--] = 0;
 93577     }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
 93578     pTab->zColAff = zColAff;
 93580   i = sqlite3Strlen30(zColAff);
 93581   if( i ){
 93582     if( iReg ){
 93583       sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
 93584     }else{
 93585       sqlite3VdbeChangeP4(v, -1, zColAff, i);
 93590 /*
 93591 ** Return non-zero if the table pTab in database iDb or any of its indices
 93592 ** have been opened at any point in the VDBE program. This is used to see if 
 93593 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
 93594 ** run without using a temporary table for the results of the SELECT. 
 93595 */
 93596 static int readsTable(Parse *p, int iDb, Table *pTab){
 93597   Vdbe *v = sqlite3GetVdbe(p);
 93598   int i;
 93599   int iEnd = sqlite3VdbeCurrentAddr(v);
 93600 #ifndef SQLITE_OMIT_VIRTUALTABLE
 93601   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
 93602 #endif
 93604   for(i=1; i<iEnd; i++){
 93605     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
 93606     assert( pOp!=0 );
 93607     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
 93608       Index *pIndex;
 93609       int tnum = pOp->p2;
 93610       if( tnum==pTab->tnum ){
 93611         return 1;
 93613       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
 93614         if( tnum==pIndex->tnum ){
 93615           return 1;
 93619 #ifndef SQLITE_OMIT_VIRTUALTABLE
 93620     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
 93621       assert( pOp->p4.pVtab!=0 );
 93622       assert( pOp->p4type==P4_VTAB );
 93623       return 1;
 93625 #endif
 93627   return 0;
 93630 #ifndef SQLITE_OMIT_AUTOINCREMENT
 93631 /*
 93632 ** Locate or create an AutoincInfo structure associated with table pTab
 93633 ** which is in database iDb.  Return the register number for the register
 93634 ** that holds the maximum rowid.
 93635 **
 93636 ** There is at most one AutoincInfo structure per table even if the
 93637 ** same table is autoincremented multiple times due to inserts within
 93638 ** triggers.  A new AutoincInfo structure is created if this is the
 93639 ** first use of table pTab.  On 2nd and subsequent uses, the original
 93640 ** AutoincInfo structure is used.
 93641 **
 93642 ** Three memory locations are allocated:
 93643 **
 93644 **   (1)  Register to hold the name of the pTab table.
 93645 **   (2)  Register to hold the maximum ROWID of pTab.
 93646 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
 93647 **
 93648 ** The 2nd register is the one that is returned.  That is all the
 93649 ** insert routine needs to know about.
 93650 */
 93651 static int autoIncBegin(
 93652   Parse *pParse,      /* Parsing context */
 93653   int iDb,            /* Index of the database holding pTab */
 93654   Table *pTab         /* The table we are writing to */
 93655 ){
 93656   int memId = 0;      /* Register holding maximum rowid */
 93657   if( pTab->tabFlags & TF_Autoincrement ){
 93658     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 93659     AutoincInfo *pInfo;
 93661     pInfo = pToplevel->pAinc;
 93662     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
 93663     if( pInfo==0 ){
 93664       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
 93665       if( pInfo==0 ) return 0;
 93666       pInfo->pNext = pToplevel->pAinc;
 93667       pToplevel->pAinc = pInfo;
 93668       pInfo->pTab = pTab;
 93669       pInfo->iDb = iDb;
 93670       pToplevel->nMem++;                  /* Register to hold name of table */
 93671       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
 93672       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
 93674     memId = pInfo->regCtr;
 93676   return memId;
 93679 /*
 93680 ** This routine generates code that will initialize all of the
 93681 ** register used by the autoincrement tracker.  
 93682 */
 93683 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
 93684   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
 93685   sqlite3 *db = pParse->db;  /* The database connection */
 93686   Db *pDb;                   /* Database only autoinc table */
 93687   int memId;                 /* Register holding max rowid */
 93688   int addr;                  /* A VDBE address */
 93689   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
 93691   /* This routine is never called during trigger-generation.  It is
 93692   ** only called from the top-level */
 93693   assert( pParse->pTriggerTab==0 );
 93694   assert( pParse==sqlite3ParseToplevel(pParse) );
 93696   assert( v );   /* We failed long ago if this is not so */
 93697   for(p = pParse->pAinc; p; p = p->pNext){
 93698     pDb = &db->aDb[p->iDb];
 93699     memId = p->regCtr;
 93700     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 93701     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 93702     sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
 93703     addr = sqlite3VdbeCurrentAddr(v);
 93704     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
 93705     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
 93706     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
 93707     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
 93708     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
 93709     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
 93710     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
 93711     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
 93712     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
 93713     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
 93714     sqlite3VdbeAddOp0(v, OP_Close);
 93718 /*
 93719 ** Update the maximum rowid for an autoincrement calculation.
 93720 **
 93721 ** This routine should be called when the top of the stack holds a
 93722 ** new rowid that is about to be inserted.  If that new rowid is
 93723 ** larger than the maximum rowid in the memId memory cell, then the
 93724 ** memory cell is updated.  The stack is unchanged.
 93725 */
 93726 static void autoIncStep(Parse *pParse, int memId, int regRowid){
 93727   if( memId>0 ){
 93728     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
 93732 /*
 93733 ** This routine generates the code needed to write autoincrement
 93734 ** maximum rowid values back into the sqlite_sequence register.
 93735 ** Every statement that might do an INSERT into an autoincrement
 93736 ** table (either directly or through triggers) needs to call this
 93737 ** routine just before the "exit" code.
 93738 */
 93739 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
 93740   AutoincInfo *p;
 93741   Vdbe *v = pParse->pVdbe;
 93742   sqlite3 *db = pParse->db;
 93744   assert( v );
 93745   for(p = pParse->pAinc; p; p = p->pNext){
 93746     Db *pDb = &db->aDb[p->iDb];
 93747     int j1;
 93748     int iRec;
 93749     int memId = p->regCtr;
 93751     iRec = sqlite3GetTempReg(pParse);
 93752     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 93753     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 93754     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
 93755     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
 93756     sqlite3VdbeJumpHere(v, j1);
 93757     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
 93758     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
 93759     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 93760     sqlite3VdbeAddOp0(v, OP_Close);
 93761     sqlite3ReleaseTempReg(pParse, iRec);
 93764 #else
 93765 /*
 93766 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
 93767 ** above are all no-ops
 93768 */
 93769 # define autoIncBegin(A,B,C) (0)
 93770 # define autoIncStep(A,B,C)
 93771 #endif /* SQLITE_OMIT_AUTOINCREMENT */
 93774 /* Forward declaration */
 93775 static int xferOptimization(
 93776   Parse *pParse,        /* Parser context */
 93777   Table *pDest,         /* The table we are inserting into */
 93778   Select *pSelect,      /* A SELECT statement to use as the data source */
 93779   int onError,          /* How to handle constraint errors */
 93780   int iDbDest           /* The database of pDest */
 93781 );
 93783 /*
 93784 ** This routine is called to handle SQL of the following forms:
 93785 **
 93786 **    insert into TABLE (IDLIST) values(EXPRLIST)
 93787 **    insert into TABLE (IDLIST) select
 93788 **
 93789 ** The IDLIST following the table name is always optional.  If omitted,
 93790 ** then a list of all columns for the table is substituted.  The IDLIST
 93791 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
 93792 **
 93793 ** The pList parameter holds EXPRLIST in the first form of the INSERT
 93794 ** statement above, and pSelect is NULL.  For the second form, pList is
 93795 ** NULL and pSelect is a pointer to the select statement used to generate
 93796 ** data for the insert.
 93797 **
 93798 ** The code generated follows one of four templates.  For a simple
 93799 ** insert with data coming from a VALUES clause, the code executes
 93800 ** once straight down through.  Pseudo-code follows (we call this
 93801 ** the "1st template"):
 93802 **
 93803 **         open write cursor to <table> and its indices
 93804 **         put VALUES clause expressions into registers
 93805 **         write the resulting record into <table>
 93806 **         cleanup
 93807 **
 93808 ** The three remaining templates assume the statement is of the form
 93809 **
 93810 **   INSERT INTO <table> SELECT ...
 93811 **
 93812 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
 93813 ** in other words if the SELECT pulls all columns from a single table
 93814 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
 93815 ** if <table2> and <table1> are distinct tables but have identical
 93816 ** schemas, including all the same indices, then a special optimization
 93817 ** is invoked that copies raw records from <table2> over to <table1>.
 93818 ** See the xferOptimization() function for the implementation of this
 93819 ** template.  This is the 2nd template.
 93820 **
 93821 **         open a write cursor to <table>
 93822 **         open read cursor on <table2>
 93823 **         transfer all records in <table2> over to <table>
 93824 **         close cursors
 93825 **         foreach index on <table>
 93826 **           open a write cursor on the <table> index
 93827 **           open a read cursor on the corresponding <table2> index
 93828 **           transfer all records from the read to the write cursors
 93829 **           close cursors
 93830 **         end foreach
 93831 **
 93832 ** The 3rd template is for when the second template does not apply
 93833 ** and the SELECT clause does not read from <table> at any time.
 93834 ** The generated code follows this template:
 93835 **
 93836 **         X <- A
 93837 **         goto B
 93838 **      A: setup for the SELECT
 93839 **         loop over the rows in the SELECT
 93840 **           load values into registers R..R+n
 93841 **           yield X
 93842 **         end loop
 93843 **         cleanup after the SELECT
 93844 **         end-coroutine X
 93845 **      B: open write cursor to <table> and its indices
 93846 **      C: yield X, at EOF goto D
 93847 **         insert the select result into <table> from R..R+n
 93848 **         goto C
 93849 **      D: cleanup
 93850 **
 93851 ** The 4th template is used if the insert statement takes its
 93852 ** values from a SELECT but the data is being inserted into a table
 93853 ** that is also read as part of the SELECT.  In the third form,
 93854 ** we have to use a intermediate table to store the results of
 93855 ** the select.  The template is like this:
 93856 **
 93857 **         X <- A
 93858 **         goto B
 93859 **      A: setup for the SELECT
 93860 **         loop over the tables in the SELECT
 93861 **           load value into register R..R+n
 93862 **           yield X
 93863 **         end loop
 93864 **         cleanup after the SELECT
 93865 **         end co-routine R
 93866 **      B: open temp table
 93867 **      L: yield X, at EOF goto M
 93868 **         insert row from R..R+n into temp table
 93869 **         goto L
 93870 **      M: open write cursor to <table> and its indices
 93871 **         rewind temp table
 93872 **      C: loop over rows of intermediate table
 93873 **           transfer values form intermediate table into <table>
 93874 **         end loop
 93875 **      D: cleanup
 93876 */
 93877 SQLITE_PRIVATE void sqlite3Insert(
 93878   Parse *pParse,        /* Parser context */
 93879   SrcList *pTabList,    /* Name of table into which we are inserting */
 93880   Select *pSelect,      /* A SELECT statement to use as the data source */
 93881   IdList *pColumn,      /* Column names corresponding to IDLIST. */
 93882   int onError           /* How to handle constraint errors */
 93883 ){
 93884   sqlite3 *db;          /* The main database structure */
 93885   Table *pTab;          /* The table to insert into.  aka TABLE */
 93886   char *zTab;           /* Name of the table into which we are inserting */
 93887   const char *zDb;      /* Name of the database holding this table */
 93888   int i, j, idx;        /* Loop counters */
 93889   Vdbe *v;              /* Generate code into this virtual machine */
 93890   Index *pIdx;          /* For looping over indices of the table */
 93891   int nColumn;          /* Number of columns in the data */
 93892   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
 93893   int iDataCur = 0;     /* VDBE cursor that is the main data repository */
 93894   int iIdxCur = 0;      /* First index cursor */
 93895   int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
 93896   int endOfLoop;        /* Label for the end of the insertion loop */
 93897   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
 93898   int addrInsTop = 0;   /* Jump to label "D" */
 93899   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
 93900   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
 93901   int iDb;              /* Index of database holding TABLE */
 93902   Db *pDb;              /* The database containing table being inserted into */
 93903   u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
 93904   u8 appendFlag = 0;    /* True if the insert is likely to be an append */
 93905   u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
 93906   u8 bIdListInOrder = 1; /* True if IDLIST is in table order */
 93907   ExprList *pList = 0;  /* List of VALUES() to be inserted  */
 93909   /* Register allocations */
 93910   int regFromSelect = 0;/* Base register for data coming from SELECT */
 93911   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 93912   int regRowCount = 0;  /* Memory cell used for the row counter */
 93913   int regIns;           /* Block of regs holding rowid+data being inserted */
 93914   int regRowid;         /* registers holding insert rowid */
 93915   int regData;          /* register holding first column to insert */
 93916   int *aRegIdx = 0;     /* One register allocated to each index */
 93918 #ifndef SQLITE_OMIT_TRIGGER
 93919   int isView;                 /* True if attempting to insert into a view */
 93920   Trigger *pTrigger;          /* List of triggers on pTab, if required */
 93921   int tmask;                  /* Mask of trigger times */
 93922 #endif
 93924   db = pParse->db;
 93925   memset(&dest, 0, sizeof(dest));
 93926   if( pParse->nErr || db->mallocFailed ){
 93927     goto insert_cleanup;
 93930   /* If the Select object is really just a simple VALUES() list with a
 93931   ** single row values (the common case) then keep that one row of values
 93932   ** and go ahead and discard the Select object
 93933   */
 93934   if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
 93935     pList = pSelect->pEList;
 93936     pSelect->pEList = 0;
 93937     sqlite3SelectDelete(db, pSelect);
 93938     pSelect = 0;
 93941   /* Locate the table into which we will be inserting new information.
 93942   */
 93943   assert( pTabList->nSrc==1 );
 93944   zTab = pTabList->a[0].zName;
 93945   if( NEVER(zTab==0) ) goto insert_cleanup;
 93946   pTab = sqlite3SrcListLookup(pParse, pTabList);
 93947   if( pTab==0 ){
 93948     goto insert_cleanup;
 93950   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 93951   assert( iDb<db->nDb );
 93952   pDb = &db->aDb[iDb];
 93953   zDb = pDb->zName;
 93954   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
 93955     goto insert_cleanup;
 93957   withoutRowid = !HasRowid(pTab);
 93959   /* Figure out if we have any triggers and if the table being
 93960   ** inserted into is a view
 93961   */
 93962 #ifndef SQLITE_OMIT_TRIGGER
 93963   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
 93964   isView = pTab->pSelect!=0;
 93965 #else
 93966 # define pTrigger 0
 93967 # define tmask 0
 93968 # define isView 0
 93969 #endif
 93970 #ifdef SQLITE_OMIT_VIEW
 93971 # undef isView
 93972 # define isView 0
 93973 #endif
 93974   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
 93976   /* If pTab is really a view, make sure it has been initialized.
 93977   ** ViewGetColumnNames() is a no-op if pTab is not a view.
 93978   */
 93979   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 93980     goto insert_cleanup;
 93983   /* Cannot insert into a read-only table.
 93984   */
 93985   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
 93986     goto insert_cleanup;
 93989   /* Allocate a VDBE
 93990   */
 93991   v = sqlite3GetVdbe(pParse);
 93992   if( v==0 ) goto insert_cleanup;
 93993   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 93994   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
 93996 #ifndef SQLITE_OMIT_XFER_OPT
 93997   /* If the statement is of the form
 93998   **
 93999   **       INSERT INTO <table1> SELECT * FROM <table2>;
 94000   **
 94001   ** Then special optimizations can be applied that make the transfer
 94002   ** very fast and which reduce fragmentation of indices.
 94003   **
 94004   ** This is the 2nd template.
 94005   */
 94006   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
 94007     assert( !pTrigger );
 94008     assert( pList==0 );
 94009     goto insert_end;
 94011 #endif /* SQLITE_OMIT_XFER_OPT */
 94013   /* If this is an AUTOINCREMENT table, look up the sequence number in the
 94014   ** sqlite_sequence table and store it in memory cell regAutoinc.
 94015   */
 94016   regAutoinc = autoIncBegin(pParse, iDb, pTab);
 94018   /* Allocate registers for holding the rowid of the new row,
 94019   ** the content of the new row, and the assemblied row record.
 94020   */
 94021   regRowid = regIns = pParse->nMem+1;
 94022   pParse->nMem += pTab->nCol + 1;
 94023   if( IsVirtual(pTab) ){
 94024     regRowid++;
 94025     pParse->nMem++;
 94027   regData = regRowid+1;
 94029   /* If the INSERT statement included an IDLIST term, then make sure
 94030   ** all elements of the IDLIST really are columns of the table and 
 94031   ** remember the column indices.
 94032   **
 94033   ** If the table has an INTEGER PRIMARY KEY column and that column
 94034   ** is named in the IDLIST, then record in the ipkColumn variable
 94035   ** the index into IDLIST of the primary key column.  ipkColumn is
 94036   ** the index of the primary key as it appears in IDLIST, not as
 94037   ** is appears in the original table.  (The index of the INTEGER
 94038   ** PRIMARY KEY in the original table is pTab->iPKey.)
 94039   */
 94040   if( pColumn ){
 94041     for(i=0; i<pColumn->nId; i++){
 94042       pColumn->a[i].idx = -1;
 94044     for(i=0; i<pColumn->nId; i++){
 94045       for(j=0; j<pTab->nCol; j++){
 94046         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
 94047           pColumn->a[i].idx = j;
 94048           if( i!=j ) bIdListInOrder = 0;
 94049           if( j==pTab->iPKey ){
 94050             ipkColumn = i;  assert( !withoutRowid );
 94052           break;
 94055       if( j>=pTab->nCol ){
 94056         if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
 94057           ipkColumn = i;
 94058         }else{
 94059           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
 94060               pTabList, 0, pColumn->a[i].zName);
 94061           pParse->checkSchema = 1;
 94062           goto insert_cleanup;
 94068   /* Figure out how many columns of data are supplied.  If the data
 94069   ** is coming from a SELECT statement, then generate a co-routine that
 94070   ** produces a single row of the SELECT on each invocation.  The
 94071   ** co-routine is the common header to the 3rd and 4th templates.
 94072   */
 94073   if( pSelect ){
 94074     /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
 94075     int regYield;       /* Register holding co-routine entry-point */
 94076     int addrTop;        /* Top of the co-routine */
 94077     int rc;             /* Result code */
 94079     regYield = ++pParse->nMem;
 94080     addrTop = sqlite3VdbeCurrentAddr(v) + 1;
 94081     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
 94082     sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
 94083     dest.iSdst = bIdListInOrder ? regData : 0;
 94084     dest.nSdst = pTab->nCol;
 94085     rc = sqlite3Select(pParse, pSelect, &dest);
 94086     regFromSelect = dest.iSdst;
 94087     assert( pParse->nErr==0 || rc );
 94088     if( rc || db->mallocFailed ) goto insert_cleanup;
 94089     sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
 94090     sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
 94091     assert( pSelect->pEList );
 94092     nColumn = pSelect->pEList->nExpr;
 94094     /* Set useTempTable to TRUE if the result of the SELECT statement
 94095     ** should be written into a temporary table (template 4).  Set to
 94096     ** FALSE if each output row of the SELECT can be written directly into
 94097     ** the destination table (template 3).
 94098     **
 94099     ** A temp table must be used if the table being updated is also one
 94100     ** of the tables being read by the SELECT statement.  Also use a 
 94101     ** temp table in the case of row triggers.
 94102     */
 94103     if( pTrigger || readsTable(pParse, iDb, pTab) ){
 94104       useTempTable = 1;
 94107     if( useTempTable ){
 94108       /* Invoke the coroutine to extract information from the SELECT
 94109       ** and add it to a transient table srcTab.  The code generated
 94110       ** here is from the 4th template:
 94111       **
 94112       **      B: open temp table
 94113       **      L: yield X, goto M at EOF
 94114       **         insert row from R..R+n into temp table
 94115       **         goto L
 94116       **      M: ...
 94117       */
 94118       int regRec;          /* Register to hold packed record */
 94119       int regTempRowid;    /* Register to hold temp table ROWID */
 94120       int addrL;           /* Label "L" */
 94122       srcTab = pParse->nTab++;
 94123       regRec = sqlite3GetTempReg(pParse);
 94124       regTempRowid = sqlite3GetTempReg(pParse);
 94125       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
 94126       addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
 94127       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
 94128       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
 94129       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
 94130       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
 94131       sqlite3VdbeJumpHere(v, addrL);
 94132       sqlite3ReleaseTempReg(pParse, regRec);
 94133       sqlite3ReleaseTempReg(pParse, regTempRowid);
 94135   }else{
 94136     /* This is the case if the data for the INSERT is coming from a VALUES
 94137     ** clause
 94138     */
 94139     NameContext sNC;
 94140     memset(&sNC, 0, sizeof(sNC));
 94141     sNC.pParse = pParse;
 94142     srcTab = -1;
 94143     assert( useTempTable==0 );
 94144     nColumn = pList ? pList->nExpr : 0;
 94145     for(i=0; i<nColumn; i++){
 94146       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
 94147         goto insert_cleanup;
 94152   /* If there is no IDLIST term but the table has an integer primary
 94153   ** key, the set the ipkColumn variable to the integer primary key 
 94154   ** column index in the original table definition.
 94155   */
 94156   if( pColumn==0 && nColumn>0 ){
 94157     ipkColumn = pTab->iPKey;
 94160   /* Make sure the number of columns in the source data matches the number
 94161   ** of columns to be inserted into the table.
 94162   */
 94163   if( IsVirtual(pTab) ){
 94164     for(i=0; i<pTab->nCol; i++){
 94165       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
 94168   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
 94169     sqlite3ErrorMsg(pParse, 
 94170        "table %S has %d columns but %d values were supplied",
 94171        pTabList, 0, pTab->nCol-nHidden, nColumn);
 94172     goto insert_cleanup;
 94174   if( pColumn!=0 && nColumn!=pColumn->nId ){
 94175     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
 94176     goto insert_cleanup;
 94179   /* Initialize the count of rows to be inserted
 94180   */
 94181   if( db->flags & SQLITE_CountRows ){
 94182     regRowCount = ++pParse->nMem;
 94183     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 94186   /* If this is not a view, open the table and and all indices */
 94187   if( !isView ){
 94188     int nIdx;
 94189     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
 94190                                       &iDataCur, &iIdxCur);
 94191     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
 94192     if( aRegIdx==0 ){
 94193       goto insert_cleanup;
 94195     for(i=0; i<nIdx; i++){
 94196       aRegIdx[i] = ++pParse->nMem;
 94200   /* This is the top of the main insertion loop */
 94201   if( useTempTable ){
 94202     /* This block codes the top of loop only.  The complete loop is the
 94203     ** following pseudocode (template 4):
 94204     **
 94205     **         rewind temp table, if empty goto D
 94206     **      C: loop over rows of intermediate table
 94207     **           transfer values form intermediate table into <table>
 94208     **         end loop
 94209     **      D: ...
 94210     */
 94211     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
 94212     addrCont = sqlite3VdbeCurrentAddr(v);
 94213   }else if( pSelect ){
 94214     /* This block codes the top of loop only.  The complete loop is the
 94215     ** following pseudocode (template 3):
 94216     **
 94217     **      C: yield X, at EOF goto D
 94218     **         insert the select result into <table> from R..R+n
 94219     **         goto C
 94220     **      D: ...
 94221     */
 94222     addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
 94223     VdbeCoverage(v);
 94226   /* Run the BEFORE and INSTEAD OF triggers, if there are any
 94227   */
 94228   endOfLoop = sqlite3VdbeMakeLabel(v);
 94229   if( tmask & TRIGGER_BEFORE ){
 94230     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
 94232     /* build the NEW.* reference row.  Note that if there is an INTEGER
 94233     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
 94234     ** translated into a unique ID for the row.  But on a BEFORE trigger,
 94235     ** we do not know what the unique ID will be (because the insert has
 94236     ** not happened yet) so we substitute a rowid of -1
 94237     */
 94238     if( ipkColumn<0 ){
 94239       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 94240     }else{
 94241       int j1;
 94242       assert( !withoutRowid );
 94243       if( useTempTable ){
 94244         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
 94245       }else{
 94246         assert( pSelect==0 );  /* Otherwise useTempTable is true */
 94247         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
 94249       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
 94250       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
 94251       sqlite3VdbeJumpHere(v, j1);
 94252       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
 94255     /* Cannot have triggers on a virtual table. If it were possible,
 94256     ** this block would have to account for hidden column.
 94257     */
 94258     assert( !IsVirtual(pTab) );
 94260     /* Create the new column data
 94261     */
 94262     for(i=0; i<pTab->nCol; i++){
 94263       if( pColumn==0 ){
 94264         j = i;
 94265       }else{
 94266         for(j=0; j<pColumn->nId; j++){
 94267           if( pColumn->a[j].idx==i ) break;
 94270       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
 94271         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
 94272       }else if( useTempTable ){
 94273         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
 94274       }else{
 94275         assert( pSelect==0 ); /* Otherwise useTempTable is true */
 94276         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
 94280     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
 94281     ** do not attempt any conversions before assembling the record.
 94282     ** If this is a real table, attempt conversions as required by the
 94283     ** table column affinities.
 94284     */
 94285     if( !isView ){
 94286       sqlite3TableAffinity(v, pTab, regCols+1);
 94289     /* Fire BEFORE or INSTEAD OF triggers */
 94290     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
 94291         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
 94293     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
 94296   /* Compute the content of the next row to insert into a range of
 94297   ** registers beginning at regIns.
 94298   */
 94299   if( !isView ){
 94300     if( IsVirtual(pTab) ){
 94301       /* The row that the VUpdate opcode will delete: none */
 94302       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
 94304     if( ipkColumn>=0 ){
 94305       if( useTempTable ){
 94306         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
 94307       }else if( pSelect ){
 94308         sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
 94309       }else{
 94310         VdbeOp *pOp;
 94311         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
 94312         pOp = sqlite3VdbeGetOp(v, -1);
 94313         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
 94314           appendFlag = 1;
 94315           pOp->opcode = OP_NewRowid;
 94316           pOp->p1 = iDataCur;
 94317           pOp->p2 = regRowid;
 94318           pOp->p3 = regAutoinc;
 94321       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
 94322       ** to generate a unique primary key value.
 94323       */
 94324       if( !appendFlag ){
 94325         int j1;
 94326         if( !IsVirtual(pTab) ){
 94327           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
 94328           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
 94329           sqlite3VdbeJumpHere(v, j1);
 94330         }else{
 94331           j1 = sqlite3VdbeCurrentAddr(v);
 94332           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
 94334         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
 94336     }else if( IsVirtual(pTab) || withoutRowid ){
 94337       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
 94338     }else{
 94339       sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
 94340       appendFlag = 1;
 94342     autoIncStep(pParse, regAutoinc, regRowid);
 94344     /* Compute data for all columns of the new entry, beginning
 94345     ** with the first column.
 94346     */
 94347     nHidden = 0;
 94348     for(i=0; i<pTab->nCol; i++){
 94349       int iRegStore = regRowid+1+i;
 94350       if( i==pTab->iPKey ){
 94351         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
 94352         ** Whenever this column is read, the rowid will be substituted
 94353         ** in its place.  Hence, fill this column with a NULL to avoid
 94354         ** taking up data space with information that will never be used.
 94355         ** As there may be shallow copies of this value, make it a soft-NULL */
 94356         sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
 94357         continue;
 94359       if( pColumn==0 ){
 94360         if( IsHiddenColumn(&pTab->aCol[i]) ){
 94361           assert( IsVirtual(pTab) );
 94362           j = -1;
 94363           nHidden++;
 94364         }else{
 94365           j = i - nHidden;
 94367       }else{
 94368         for(j=0; j<pColumn->nId; j++){
 94369           if( pColumn->a[j].idx==i ) break;
 94372       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
 94373         sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
 94374       }else if( useTempTable ){
 94375         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
 94376       }else if( pSelect ){
 94377         if( regFromSelect!=regData ){
 94378           sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
 94380       }else{
 94381         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
 94385     /* Generate code to check constraints and generate index keys and
 94386     ** do the insertion.
 94387     */
 94388 #ifndef SQLITE_OMIT_VIRTUALTABLE
 94389     if( IsVirtual(pTab) ){
 94390       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 94391       sqlite3VtabMakeWritable(pParse, pTab);
 94392       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
 94393       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
 94394       sqlite3MayAbort(pParse);
 94395     }else
 94396 #endif
 94398       int isReplace;    /* Set to true if constraints may cause a replace */
 94399       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
 94400           regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
 94401       );
 94402       sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
 94403       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
 94404                                regIns, aRegIdx, 0, appendFlag, isReplace==0);
 94408   /* Update the count of rows that are inserted
 94409   */
 94410   if( (db->flags & SQLITE_CountRows)!=0 ){
 94411     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 94414   if( pTrigger ){
 94415     /* Code AFTER triggers */
 94416     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
 94417         pTab, regData-2-pTab->nCol, onError, endOfLoop);
 94420   /* The bottom of the main insertion loop, if the data source
 94421   ** is a SELECT statement.
 94422   */
 94423   sqlite3VdbeResolveLabel(v, endOfLoop);
 94424   if( useTempTable ){
 94425     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
 94426     sqlite3VdbeJumpHere(v, addrInsTop);
 94427     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
 94428   }else if( pSelect ){
 94429     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
 94430     sqlite3VdbeJumpHere(v, addrInsTop);
 94433   if( !IsVirtual(pTab) && !isView ){
 94434     /* Close all tables opened */
 94435     if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
 94436     for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
 94437       sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
 94441 insert_end:
 94442   /* Update the sqlite_sequence table by storing the content of the
 94443   ** maximum rowid counter values recorded while inserting into
 94444   ** autoincrement tables.
 94445   */
 94446   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 94447     sqlite3AutoincrementEnd(pParse);
 94450   /*
 94451   ** Return the number of rows inserted. If this routine is 
 94452   ** generating code because of a call to sqlite3NestedParse(), do not
 94453   ** invoke the callback function.
 94454   */
 94455   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
 94456     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
 94457     sqlite3VdbeSetNumCols(v, 1);
 94458     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
 94461 insert_cleanup:
 94462   sqlite3SrcListDelete(db, pTabList);
 94463   sqlite3ExprListDelete(db, pList);
 94464   sqlite3SelectDelete(db, pSelect);
 94465   sqlite3IdListDelete(db, pColumn);
 94466   sqlite3DbFree(db, aRegIdx);
 94469 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 94470 ** thely may interfere with compilation of other functions in this file
 94471 ** (or in another file, if this file becomes part of the amalgamation).  */
 94472 #ifdef isView
 94473  #undef isView
 94474 #endif
 94475 #ifdef pTrigger
 94476  #undef pTrigger
 94477 #endif
 94478 #ifdef tmask
 94479  #undef tmask
 94480 #endif
 94482 /*
 94483 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
 94484 ** on table pTab.
 94485 **
 94486 ** The regNewData parameter is the first register in a range that contains
 94487 ** the data to be inserted or the data after the update.  There will be
 94488 ** pTab->nCol+1 registers in this range.  The first register (the one
 94489 ** that regNewData points to) will contain the new rowid, or NULL in the
 94490 ** case of a WITHOUT ROWID table.  The second register in the range will
 94491 ** contain the content of the first table column.  The third register will
 94492 ** contain the content of the second table column.  And so forth.
 94493 **
 94494 ** The regOldData parameter is similar to regNewData except that it contains
 94495 ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
 94496 ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
 94497 ** checking regOldData for zero.
 94498 **
 94499 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
 94500 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
 94501 ** might be modified by the UPDATE.  If pkChng is false, then the key of
 94502 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
 94503 **
 94504 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
 94505 ** was explicitly specified as part of the INSERT statement.  If pkChng
 94506 ** is zero, it means that the either rowid is computed automatically or
 94507 ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
 94508 ** pkChng will only be true if the INSERT statement provides an integer
 94509 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
 94510 **
 94511 ** The code generated by this routine will store new index entries into
 94512 ** registers identified by aRegIdx[].  No index entry is created for
 94513 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
 94514 ** the same as the order of indices on the linked list of indices
 94515 ** at pTab->pIndex.
 94516 **
 94517 ** The caller must have already opened writeable cursors on the main
 94518 ** table and all applicable indices (that is to say, all indices for which
 94519 ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
 94520 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
 94521 ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
 94522 ** for the first index in the pTab->pIndex list.  Cursors for other indices
 94523 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
 94524 **
 94525 ** This routine also generates code to check constraints.  NOT NULL,
 94526 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
 94527 ** then the appropriate action is performed.  There are five possible
 94528 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
 94529 **
 94530 **  Constraint type  Action       What Happens
 94531 **  ---------------  ----------   ----------------------------------------
 94532 **  any              ROLLBACK     The current transaction is rolled back and
 94533 **                                sqlite3_step() returns immediately with a
 94534 **                                return code of SQLITE_CONSTRAINT.
 94535 **
 94536 **  any              ABORT        Back out changes from the current command
 94537 **                                only (do not do a complete rollback) then
 94538 **                                cause sqlite3_step() to return immediately
 94539 **                                with SQLITE_CONSTRAINT.
 94540 **
 94541 **  any              FAIL         Sqlite3_step() returns immediately with a
 94542 **                                return code of SQLITE_CONSTRAINT.  The
 94543 **                                transaction is not rolled back and any
 94544 **                                changes to prior rows are retained.
 94545 **
 94546 **  any              IGNORE       The attempt in insert or update the current
 94547 **                                row is skipped, without throwing an error.
 94548 **                                Processing continues with the next row.
 94549 **                                (There is an immediate jump to ignoreDest.)
 94550 **
 94551 **  NOT NULL         REPLACE      The NULL value is replace by the default
 94552 **                                value for that column.  If the default value
 94553 **                                is NULL, the action is the same as ABORT.
 94554 **
 94555 **  UNIQUE           REPLACE      The other row that conflicts with the row
 94556 **                                being inserted is removed.
 94557 **
 94558 **  CHECK            REPLACE      Illegal.  The results in an exception.
 94559 **
 94560 ** Which action to take is determined by the overrideError parameter.
 94561 ** Or if overrideError==OE_Default, then the pParse->onError parameter
 94562 ** is used.  Or if pParse->onError==OE_Default then the onError value
 94563 ** for the constraint is used.
 94564 */
 94565 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
 94566   Parse *pParse,       /* The parser context */
 94567   Table *pTab,         /* The table being inserted or updated */
 94568   int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
 94569   int iDataCur,        /* Canonical data cursor (main table or PK index) */
 94570   int iIdxCur,         /* First index cursor */
 94571   int regNewData,      /* First register in a range holding values to insert */
 94572   int regOldData,      /* Previous content.  0 for INSERTs */
 94573   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
 94574   u8 overrideError,    /* Override onError to this if not OE_Default */
 94575   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
 94576   int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
 94577 ){
 94578   Vdbe *v;             /* VDBE under constrution */
 94579   Index *pIdx;         /* Pointer to one of the indices */
 94580   Index *pPk = 0;      /* The PRIMARY KEY index */
 94581   sqlite3 *db;         /* Database connection */
 94582   int i;               /* loop counter */
 94583   int ix;              /* Index loop counter */
 94584   int nCol;            /* Number of columns */
 94585   int onError;         /* Conflict resolution strategy */
 94586   int j1;              /* Addresss of jump instruction */
 94587   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
 94588   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
 94589   int ipkTop = 0;      /* Top of the rowid change constraint check */
 94590   int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
 94591   u8 isUpdate;         /* True if this is an UPDATE operation */
 94592   u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
 94593   int regRowid = -1;   /* Register holding ROWID value */
 94595   isUpdate = regOldData!=0;
 94596   db = pParse->db;
 94597   v = sqlite3GetVdbe(pParse);
 94598   assert( v!=0 );
 94599   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 94600   nCol = pTab->nCol;
 94602   /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
 94603   ** normal rowid tables.  nPkField is the number of key fields in the 
 94604   ** pPk index or 1 for a rowid table.  In other words, nPkField is the
 94605   ** number of fields in the true primary key of the table. */
 94606   if( HasRowid(pTab) ){
 94607     pPk = 0;
 94608     nPkField = 1;
 94609   }else{
 94610     pPk = sqlite3PrimaryKeyIndex(pTab);
 94611     nPkField = pPk->nKeyCol;
 94614   /* Record that this module has started */
 94615   VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
 94616                      iDataCur, iIdxCur, regNewData, regOldData, pkChng));
 94618   /* Test all NOT NULL constraints.
 94619   */
 94620   for(i=0; i<nCol; i++){
 94621     if( i==pTab->iPKey ){
 94622       continue;
 94624     onError = pTab->aCol[i].notNull;
 94625     if( onError==OE_None ) continue;
 94626     if( overrideError!=OE_Default ){
 94627       onError = overrideError;
 94628     }else if( onError==OE_Default ){
 94629       onError = OE_Abort;
 94631     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
 94632       onError = OE_Abort;
 94634     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 94635         || onError==OE_Ignore || onError==OE_Replace );
 94636     switch( onError ){
 94637       case OE_Abort:
 94638         sqlite3MayAbort(pParse);
 94639         /* Fall through */
 94640       case OE_Rollback:
 94641       case OE_Fail: {
 94642         char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
 94643                                     pTab->aCol[i].zName);
 94644         sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
 94645                           regNewData+1+i, zMsg, P4_DYNAMIC);
 94646         sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
 94647         VdbeCoverage(v);
 94648         break;
 94650       case OE_Ignore: {
 94651         sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
 94652         VdbeCoverage(v);
 94653         break;
 94655       default: {
 94656         assert( onError==OE_Replace );
 94657         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
 94658         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
 94659         sqlite3VdbeJumpHere(v, j1);
 94660         break;
 94665   /* Test all CHECK constraints
 94666   */
 94667 #ifndef SQLITE_OMIT_CHECK
 94668   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
 94669     ExprList *pCheck = pTab->pCheck;
 94670     pParse->ckBase = regNewData+1;
 94671     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
 94672     for(i=0; i<pCheck->nExpr; i++){
 94673       int allOk = sqlite3VdbeMakeLabel(v);
 94674       sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
 94675       if( onError==OE_Ignore ){
 94676         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 94677       }else{
 94678         char *zName = pCheck->a[i].zName;
 94679         if( zName==0 ) zName = pTab->zName;
 94680         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
 94681         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
 94682                               onError, zName, P4_TRANSIENT,
 94683                               P5_ConstraintCheck);
 94685       sqlite3VdbeResolveLabel(v, allOk);
 94688 #endif /* !defined(SQLITE_OMIT_CHECK) */
 94690   /* If rowid is changing, make sure the new rowid does not previously
 94691   ** exist in the table.
 94692   */
 94693   if( pkChng && pPk==0 ){
 94694     int addrRowidOk = sqlite3VdbeMakeLabel(v);
 94696     /* Figure out what action to take in case of a rowid collision */
 94697     onError = pTab->keyConf;
 94698     if( overrideError!=OE_Default ){
 94699       onError = overrideError;
 94700     }else if( onError==OE_Default ){
 94701       onError = OE_Abort;
 94704     if( isUpdate ){
 94705       /* pkChng!=0 does not mean that the rowid has change, only that
 94706       ** it might have changed.  Skip the conflict logic below if the rowid
 94707       ** is unchanged. */
 94708       sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
 94709       sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 94710       VdbeCoverage(v);
 94713     /* If the response to a rowid conflict is REPLACE but the response
 94714     ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
 94715     ** to defer the running of the rowid conflict checking until after
 94716     ** the UNIQUE constraints have run.
 94717     */
 94718     if( onError==OE_Replace && overrideError!=OE_Replace ){
 94719       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 94720         if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
 94721           ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
 94722           break;
 94727     /* Check to see if the new rowid already exists in the table.  Skip
 94728     ** the following conflict logic if it does not. */
 94729     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
 94730     VdbeCoverage(v);
 94732     /* Generate code that deals with a rowid collision */
 94733     switch( onError ){
 94734       default: {
 94735         onError = OE_Abort;
 94736         /* Fall thru into the next case */
 94738       case OE_Rollback:
 94739       case OE_Abort:
 94740       case OE_Fail: {
 94741         sqlite3RowidConstraint(pParse, onError, pTab);
 94742         break;
 94744       case OE_Replace: {
 94745         /* If there are DELETE triggers on this table and the
 94746         ** recursive-triggers flag is set, call GenerateRowDelete() to
 94747         ** remove the conflicting row from the table. This will fire
 94748         ** the triggers and remove both the table and index b-tree entries.
 94749         **
 94750         ** Otherwise, if there are no triggers or the recursive-triggers
 94751         ** flag is not set, but the table has one or more indexes, call 
 94752         ** GenerateRowIndexDelete(). This removes the index b-tree entries 
 94753         ** only. The table b-tree entry will be replaced by the new entry 
 94754         ** when it is inserted.  
 94755         **
 94756         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
 94757         ** also invoke MultiWrite() to indicate that this VDBE may require
 94758         ** statement rollback (if the statement is aborted after the delete
 94759         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
 94760         ** but being more selective here allows statements like:
 94761         **
 94762         **   REPLACE INTO t(rowid) VALUES($newrowid)
 94763         **
 94764         ** to run without a statement journal if there are no indexes on the
 94765         ** table.
 94766         */
 94767         Trigger *pTrigger = 0;
 94768         if( db->flags&SQLITE_RecTriggers ){
 94769           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 94771         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
 94772           sqlite3MultiWrite(pParse);
 94773           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
 94774                                    regNewData, 1, 0, OE_Replace, 1);
 94775         }else if( pTab->pIndex ){
 94776           sqlite3MultiWrite(pParse);
 94777           sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
 94779         seenReplace = 1;
 94780         break;
 94782       case OE_Ignore: {
 94783         /*assert( seenReplace==0 );*/
 94784         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 94785         break;
 94788     sqlite3VdbeResolveLabel(v, addrRowidOk);
 94789     if( ipkTop ){
 94790       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
 94791       sqlite3VdbeJumpHere(v, ipkTop);
 94795   /* Test all UNIQUE constraints by creating entries for each UNIQUE
 94796   ** index and making sure that duplicate entries do not already exist.
 94797   ** Compute the revised record entries for indices as we go.
 94798   **
 94799   ** This loop also handles the case of the PRIMARY KEY index for a
 94800   ** WITHOUT ROWID table.
 94801   */
 94802   for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
 94803     int regIdx;          /* Range of registers hold conent for pIdx */
 94804     int regR;            /* Range of registers holding conflicting PK */
 94805     int iThisCur;        /* Cursor for this UNIQUE index */
 94806     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
 94808     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
 94809     if( bAffinityDone==0 ){
 94810       sqlite3TableAffinity(v, pTab, regNewData+1);
 94811       bAffinityDone = 1;
 94813     iThisCur = iIdxCur+ix;
 94814     addrUniqueOk = sqlite3VdbeMakeLabel(v);
 94816     /* Skip partial indices for which the WHERE clause is not true */
 94817     if( pIdx->pPartIdxWhere ){
 94818       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
 94819       pParse->ckBase = regNewData+1;
 94820       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
 94821                          SQLITE_JUMPIFNULL);
 94822       pParse->ckBase = 0;
 94825     /* Create a record for this index entry as it should appear after
 94826     ** the insert or update.  Store that record in the aRegIdx[ix] register
 94827     */
 94828     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
 94829     for(i=0; i<pIdx->nColumn; i++){
 94830       int iField = pIdx->aiColumn[i];
 94831       int x;
 94832       if( iField<0 || iField==pTab->iPKey ){
 94833         if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
 94834         x = regNewData;
 94835         regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
 94836       }else{
 94837         x = iField + regNewData + 1;
 94839       sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
 94840       VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
 94842     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
 94843     VdbeComment((v, "for %s", pIdx->zName));
 94844     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
 94846     /* In an UPDATE operation, if this index is the PRIMARY KEY index 
 94847     ** of a WITHOUT ROWID table and there has been no change the
 94848     ** primary key, then no collision is possible.  The collision detection
 94849     ** logic below can all be skipped. */
 94850     if( isUpdate && pPk==pIdx && pkChng==0 ){
 94851       sqlite3VdbeResolveLabel(v, addrUniqueOk);
 94852       continue;
 94855     /* Find out what action to take in case there is a uniqueness conflict */
 94856     onError = pIdx->onError;
 94857     if( onError==OE_None ){ 
 94858       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
 94859       sqlite3VdbeResolveLabel(v, addrUniqueOk);
 94860       continue;  /* pIdx is not a UNIQUE index */
 94862     if( overrideError!=OE_Default ){
 94863       onError = overrideError;
 94864     }else if( onError==OE_Default ){
 94865       onError = OE_Abort;
 94868     /* Check to see if the new index entry will be unique */
 94869     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
 94870                          regIdx, pIdx->nKeyCol); VdbeCoverage(v);
 94872     /* Generate code to handle collisions */
 94873     regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
 94874     if( isUpdate || onError==OE_Replace ){
 94875       if( HasRowid(pTab) ){
 94876         sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
 94877         /* Conflict only if the rowid of the existing index entry
 94878         ** is different from old-rowid */
 94879         if( isUpdate ){
 94880           sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
 94881           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 94882           VdbeCoverage(v);
 94884       }else{
 94885         int x;
 94886         /* Extract the PRIMARY KEY from the end of the index entry and
 94887         ** store it in registers regR..regR+nPk-1 */
 94888         if( pIdx!=pPk ){
 94889           for(i=0; i<pPk->nKeyCol; i++){
 94890             x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
 94891             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
 94892             VdbeComment((v, "%s.%s", pTab->zName,
 94893                          pTab->aCol[pPk->aiColumn[i]].zName));
 94896         if( isUpdate ){
 94897           /* If currently processing the PRIMARY KEY of a WITHOUT ROWID 
 94898           ** table, only conflict if the new PRIMARY KEY values are actually
 94899           ** different from the old.
 94900           **
 94901           ** For a UNIQUE index, only conflict if the PRIMARY KEY values
 94902           ** of the matched index row are different from the original PRIMARY
 94903           ** KEY values of this row before the update.  */
 94904           int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
 94905           int op = OP_Ne;
 94906           int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
 94908           for(i=0; i<pPk->nKeyCol; i++){
 94909             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
 94910             x = pPk->aiColumn[i];
 94911             if( i==(pPk->nKeyCol-1) ){
 94912               addrJump = addrUniqueOk;
 94913               op = OP_Eq;
 94915             sqlite3VdbeAddOp4(v, op, 
 94916                 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
 94917             );
 94918             sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 94919             VdbeCoverageIf(v, op==OP_Eq);
 94920             VdbeCoverageIf(v, op==OP_Ne);
 94926     /* Generate code that executes if the new index entry is not unique */
 94927     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
 94928         || onError==OE_Ignore || onError==OE_Replace );
 94929     switch( onError ){
 94930       case OE_Rollback:
 94931       case OE_Abort:
 94932       case OE_Fail: {
 94933         sqlite3UniqueConstraint(pParse, onError, pIdx);
 94934         break;
 94936       case OE_Ignore: {
 94937         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
 94938         break;
 94940       default: {
 94941         Trigger *pTrigger = 0;
 94942         assert( onError==OE_Replace );
 94943         sqlite3MultiWrite(pParse);
 94944         if( db->flags&SQLITE_RecTriggers ){
 94945           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
 94947         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
 94948                                  regR, nPkField, 0, OE_Replace, pIdx==pPk);
 94949         seenReplace = 1;
 94950         break;
 94953     sqlite3VdbeResolveLabel(v, addrUniqueOk);
 94954     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
 94955     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
 94957   if( ipkTop ){
 94958     sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
 94959     sqlite3VdbeJumpHere(v, ipkBottom);
 94962   *pbMayReplace = seenReplace;
 94963   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
 94966 /*
 94967 ** This routine generates code to finish the INSERT or UPDATE operation
 94968 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
 94969 ** A consecutive range of registers starting at regNewData contains the
 94970 ** rowid and the content to be inserted.
 94971 **
 94972 ** The arguments to this routine should be the same as the first six
 94973 ** arguments to sqlite3GenerateConstraintChecks.
 94974 */
 94975 SQLITE_PRIVATE void sqlite3CompleteInsertion(
 94976   Parse *pParse,      /* The parser context */
 94977   Table *pTab,        /* the table into which we are inserting */
 94978   int iDataCur,       /* Cursor of the canonical data source */
 94979   int iIdxCur,        /* First index cursor */
 94980   int regNewData,     /* Range of content */
 94981   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
 94982   int isUpdate,       /* True for UPDATE, False for INSERT */
 94983   int appendBias,     /* True if this is likely to be an append */
 94984   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
 94985 ){
 94986   Vdbe *v;            /* Prepared statements under construction */
 94987   Index *pIdx;        /* An index being inserted or updated */
 94988   u8 pik_flags;       /* flag values passed to the btree insert */
 94989   int regData;        /* Content registers (after the rowid) */
 94990   int regRec;         /* Register holding assemblied record for the table */
 94991   int i;              /* Loop counter */
 94992   u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
 94994   v = sqlite3GetVdbe(pParse);
 94995   assert( v!=0 );
 94996   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
 94997   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 94998     if( aRegIdx[i]==0 ) continue;
 94999     bAffinityDone = 1;
 95000     if( pIdx->pPartIdxWhere ){
 95001       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
 95002       VdbeCoverage(v);
 95004     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
 95005     pik_flags = 0;
 95006     if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
 95007     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
 95008       assert( pParse->nested==0 );
 95009       pik_flags |= OPFLAG_NCHANGE;
 95011     if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
 95013   if( !HasRowid(pTab) ) return;
 95014   regData = regNewData + 1;
 95015   regRec = sqlite3GetTempReg(pParse);
 95016   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
 95017   if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
 95018   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
 95019   if( pParse->nested ){
 95020     pik_flags = 0;
 95021   }else{
 95022     pik_flags = OPFLAG_NCHANGE;
 95023     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
 95025   if( appendBias ){
 95026     pik_flags |= OPFLAG_APPEND;
 95028   if( useSeekResult ){
 95029     pik_flags |= OPFLAG_USESEEKRESULT;
 95031   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
 95032   if( !pParse->nested ){
 95033     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 95035   sqlite3VdbeChangeP5(v, pik_flags);
 95038 /*
 95039 ** Allocate cursors for the pTab table and all its indices and generate
 95040 ** code to open and initialized those cursors.
 95041 **
 95042 ** The cursor for the object that contains the complete data (normally
 95043 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
 95044 ** ROWID table) is returned in *piDataCur.  The first index cursor is
 95045 ** returned in *piIdxCur.  The number of indices is returned.
 95046 **
 95047 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
 95048 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
 95049 ** If iBase is negative, then allocate the next available cursor.
 95050 **
 95051 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
 95052 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
 95053 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
 95054 ** pTab->pIndex list.
 95055 */
 95056 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
 95057   Parse *pParse,   /* Parsing context */
 95058   Table *pTab,     /* Table to be opened */
 95059   int op,          /* OP_OpenRead or OP_OpenWrite */
 95060   int iBase,       /* Use this for the table cursor, if there is one */
 95061   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
 95062   int *piDataCur,  /* Write the database source cursor number here */
 95063   int *piIdxCur    /* Write the first index cursor number here */
 95064 ){
 95065   int i;
 95066   int iDb;
 95067   int iDataCur;
 95068   Index *pIdx;
 95069   Vdbe *v;
 95071   assert( op==OP_OpenRead || op==OP_OpenWrite );
 95072   if( IsVirtual(pTab) ){
 95073     assert( aToOpen==0 );
 95074     *piDataCur = 0;
 95075     *piIdxCur = 1;
 95076     return 0;
 95078   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 95079   v = sqlite3GetVdbe(pParse);
 95080   assert( v!=0 );
 95081   if( iBase<0 ) iBase = pParse->nTab;
 95082   iDataCur = iBase++;
 95083   if( piDataCur ) *piDataCur = iDataCur;
 95084   if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
 95085     sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
 95086   }else{
 95087     sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
 95089   if( piIdxCur ) *piIdxCur = iBase;
 95090   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 95091     int iIdxCur = iBase++;
 95092     assert( pIdx->pSchema==pTab->pSchema );
 95093     if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
 95094       *piDataCur = iIdxCur;
 95096     if( aToOpen==0 || aToOpen[i+1] ){
 95097       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
 95098       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 95099       VdbeComment((v, "%s", pIdx->zName));
 95102   if( iBase>pParse->nTab ) pParse->nTab = iBase;
 95103   return i;
 95107 #ifdef SQLITE_TEST
 95108 /*
 95109 ** The following global variable is incremented whenever the
 95110 ** transfer optimization is used.  This is used for testing
 95111 ** purposes only - to make sure the transfer optimization really
 95112 ** is happening when it is suppose to.
 95113 */
 95114 SQLITE_API int sqlite3_xferopt_count;
 95115 #endif /* SQLITE_TEST */
 95118 #ifndef SQLITE_OMIT_XFER_OPT
 95119 /*
 95120 ** Check to collation names to see if they are compatible.
 95121 */
 95122 static int xferCompatibleCollation(const char *z1, const char *z2){
 95123   if( z1==0 ){
 95124     return z2==0;
 95126   if( z2==0 ){
 95127     return 0;
 95129   return sqlite3StrICmp(z1, z2)==0;
 95133 /*
 95134 ** Check to see if index pSrc is compatible as a source of data
 95135 ** for index pDest in an insert transfer optimization.  The rules
 95136 ** for a compatible index:
 95137 **
 95138 **    *   The index is over the same set of columns
 95139 **    *   The same DESC and ASC markings occurs on all columns
 95140 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
 95141 **    *   The same collating sequence on each column
 95142 **    *   The index has the exact same WHERE clause
 95143 */
 95144 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
 95145   int i;
 95146   assert( pDest && pSrc );
 95147   assert( pDest->pTable!=pSrc->pTable );
 95148   if( pDest->nKeyCol!=pSrc->nKeyCol ){
 95149     return 0;   /* Different number of columns */
 95151   if( pDest->onError!=pSrc->onError ){
 95152     return 0;   /* Different conflict resolution strategies */
 95154   for(i=0; i<pSrc->nKeyCol; i++){
 95155     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
 95156       return 0;   /* Different columns indexed */
 95158     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
 95159       return 0;   /* Different sort orders */
 95161     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
 95162       return 0;   /* Different collating sequences */
 95165   if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
 95166     return 0;     /* Different WHERE clauses */
 95169   /* If no test above fails then the indices must be compatible */
 95170   return 1;
 95173 /*
 95174 ** Attempt the transfer optimization on INSERTs of the form
 95175 **
 95176 **     INSERT INTO tab1 SELECT * FROM tab2;
 95177 **
 95178 ** The xfer optimization transfers raw records from tab2 over to tab1.  
 95179 ** Columns are not decoded and reassemblied, which greatly improves
 95180 ** performance.  Raw index records are transferred in the same way.
 95181 **
 95182 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
 95183 ** There are lots of rules for determining compatibility - see comments
 95184 ** embedded in the code for details.
 95185 **
 95186 ** This routine returns TRUE if the optimization is guaranteed to be used.
 95187 ** Sometimes the xfer optimization will only work if the destination table
 95188 ** is empty - a factor that can only be determined at run-time.  In that
 95189 ** case, this routine generates code for the xfer optimization but also
 95190 ** does a test to see if the destination table is empty and jumps over the
 95191 ** xfer optimization code if the test fails.  In that case, this routine
 95192 ** returns FALSE so that the caller will know to go ahead and generate
 95193 ** an unoptimized transfer.  This routine also returns FALSE if there
 95194 ** is no chance that the xfer optimization can be applied.
 95195 **
 95196 ** This optimization is particularly useful at making VACUUM run faster.
 95197 */
 95198 static int xferOptimization(
 95199   Parse *pParse,        /* Parser context */
 95200   Table *pDest,         /* The table we are inserting into */
 95201   Select *pSelect,      /* A SELECT statement to use as the data source */
 95202   int onError,          /* How to handle constraint errors */
 95203   int iDbDest           /* The database of pDest */
 95204 ){
 95205   ExprList *pEList;                /* The result set of the SELECT */
 95206   Table *pSrc;                     /* The table in the FROM clause of SELECT */
 95207   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
 95208   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
 95209   int i;                           /* Loop counter */
 95210   int iDbSrc;                      /* The database of pSrc */
 95211   int iSrc, iDest;                 /* Cursors from source and destination */
 95212   int addr1, addr2;                /* Loop addresses */
 95213   int emptyDestTest = 0;           /* Address of test for empty pDest */
 95214   int emptySrcTest = 0;            /* Address of test for empty pSrc */
 95215   Vdbe *v;                         /* The VDBE we are building */
 95216   int regAutoinc;                  /* Memory register used by AUTOINC */
 95217   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
 95218   int regData, regRowid;           /* Registers holding data and rowid */
 95220   if( pSelect==0 ){
 95221     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
 95223   if( pParse->pWith || pSelect->pWith ){
 95224     /* Do not attempt to process this query if there are an WITH clauses
 95225     ** attached to it. Proceeding may generate a false "no such table: xxx"
 95226     ** error if pSelect reads from a CTE named "xxx".  */
 95227     return 0;
 95229   if( sqlite3TriggerList(pParse, pDest) ){
 95230     return 0;   /* tab1 must not have triggers */
 95232 #ifndef SQLITE_OMIT_VIRTUALTABLE
 95233   if( pDest->tabFlags & TF_Virtual ){
 95234     return 0;   /* tab1 must not be a virtual table */
 95236 #endif
 95237   if( onError==OE_Default ){
 95238     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
 95239     if( onError==OE_Default ) onError = OE_Abort;
 95241   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
 95242   if( pSelect->pSrc->nSrc!=1 ){
 95243     return 0;   /* FROM clause must have exactly one term */
 95245   if( pSelect->pSrc->a[0].pSelect ){
 95246     return 0;   /* FROM clause cannot contain a subquery */
 95248   if( pSelect->pWhere ){
 95249     return 0;   /* SELECT may not have a WHERE clause */
 95251   if( pSelect->pOrderBy ){
 95252     return 0;   /* SELECT may not have an ORDER BY clause */
 95254   /* Do not need to test for a HAVING clause.  If HAVING is present but
 95255   ** there is no ORDER BY, we will get an error. */
 95256   if( pSelect->pGroupBy ){
 95257     return 0;   /* SELECT may not have a GROUP BY clause */
 95259   if( pSelect->pLimit ){
 95260     return 0;   /* SELECT may not have a LIMIT clause */
 95262   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
 95263   if( pSelect->pPrior ){
 95264     return 0;   /* SELECT may not be a compound query */
 95266   if( pSelect->selFlags & SF_Distinct ){
 95267     return 0;   /* SELECT may not be DISTINCT */
 95269   pEList = pSelect->pEList;
 95270   assert( pEList!=0 );
 95271   if( pEList->nExpr!=1 ){
 95272     return 0;   /* The result set must have exactly one column */
 95274   assert( pEList->a[0].pExpr );
 95275   if( pEList->a[0].pExpr->op!=TK_ALL ){
 95276     return 0;   /* The result set must be the special operator "*" */
 95279   /* At this point we have established that the statement is of the
 95280   ** correct syntactic form to participate in this optimization.  Now
 95281   ** we have to check the semantics.
 95282   */
 95283   pItem = pSelect->pSrc->a;
 95284   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
 95285   if( pSrc==0 ){
 95286     return 0;   /* FROM clause does not contain a real table */
 95288   if( pSrc==pDest ){
 95289     return 0;   /* tab1 and tab2 may not be the same table */
 95291   if( HasRowid(pDest)!=HasRowid(pSrc) ){
 95292     return 0;   /* source and destination must both be WITHOUT ROWID or not */
 95294 #ifndef SQLITE_OMIT_VIRTUALTABLE
 95295   if( pSrc->tabFlags & TF_Virtual ){
 95296     return 0;   /* tab2 must not be a virtual table */
 95298 #endif
 95299   if( pSrc->pSelect ){
 95300     return 0;   /* tab2 may not be a view */
 95302   if( pDest->nCol!=pSrc->nCol ){
 95303     return 0;   /* Number of columns must be the same in tab1 and tab2 */
 95305   if( pDest->iPKey!=pSrc->iPKey ){
 95306     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
 95308   for(i=0; i<pDest->nCol; i++){
 95309     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
 95310       return 0;    /* Affinity must be the same on all columns */
 95312     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
 95313       return 0;    /* Collating sequence must be the same on all columns */
 95315     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
 95316       return 0;    /* tab2 must be NOT NULL if tab1 is */
 95319   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 95320     if( pDestIdx->onError!=OE_None ){
 95321       destHasUniqueIdx = 1;
 95323     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
 95324       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 95326     if( pSrcIdx==0 ){
 95327       return 0;    /* pDestIdx has no corresponding index in pSrc */
 95330 #ifndef SQLITE_OMIT_CHECK
 95331   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
 95332     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
 95334 #endif
 95335 #ifndef SQLITE_OMIT_FOREIGN_KEY
 95336   /* Disallow the transfer optimization if the destination table constains
 95337   ** any foreign key constraints.  This is more restrictive than necessary.
 95338   ** But the main beneficiary of the transfer optimization is the VACUUM 
 95339   ** command, and the VACUUM command disables foreign key constraints.  So
 95340   ** the extra complication to make this rule less restrictive is probably
 95341   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
 95342   */
 95343   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
 95344     return 0;
 95346 #endif
 95347   if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
 95348     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
 95351   /* If we get this far, it means that the xfer optimization is at
 95352   ** least a possibility, though it might only work if the destination
 95353   ** table (tab1) is initially empty.
 95354   */
 95355 #ifdef SQLITE_TEST
 95356   sqlite3_xferopt_count++;
 95357 #endif
 95358   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
 95359   v = sqlite3GetVdbe(pParse);
 95360   sqlite3CodeVerifySchema(pParse, iDbSrc);
 95361   iSrc = pParse->nTab++;
 95362   iDest = pParse->nTab++;
 95363   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
 95364   regData = sqlite3GetTempReg(pParse);
 95365   regRowid = sqlite3GetTempReg(pParse);
 95366   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
 95367   assert( HasRowid(pDest) || destHasUniqueIdx );
 95368   if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
 95369    || destHasUniqueIdx                              /* (2) */
 95370    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
 95371   ){
 95372     /* In some circumstances, we are able to run the xfer optimization
 95373     ** only if the destination table is initially empty.  This code makes
 95374     ** that determination.  Conditions under which the destination must
 95375     ** be empty:
 95376     **
 95377     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
 95378     **     (If the destination is not initially empty, the rowid fields
 95379     **     of index entries might need to change.)
 95380     **
 95381     ** (2) The destination has a unique index.  (The xfer optimization 
 95382     **     is unable to test uniqueness.)
 95383     **
 95384     ** (3) onError is something other than OE_Abort and OE_Rollback.
 95385     */
 95386     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
 95387     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
 95388     sqlite3VdbeJumpHere(v, addr1);
 95390   if( HasRowid(pSrc) ){
 95391     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
 95392     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
 95393     if( pDest->iPKey>=0 ){
 95394       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 95395       addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
 95396       VdbeCoverage(v);
 95397       sqlite3RowidConstraint(pParse, onError, pDest);
 95398       sqlite3VdbeJumpHere(v, addr2);
 95399       autoIncStep(pParse, regAutoinc, regRowid);
 95400     }else if( pDest->pIndex==0 ){
 95401       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
 95402     }else{
 95403       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
 95404       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
 95406     sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
 95407     sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
 95408     sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
 95409     sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
 95410     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
 95411     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 95412     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 95413   }else{
 95414     sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
 95415     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
 95417   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
 95418     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
 95419       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
 95421     assert( pSrcIdx );
 95422     sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
 95423     sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
 95424     VdbeComment((v, "%s", pSrcIdx->zName));
 95425     sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
 95426     sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
 95427     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
 95428     VdbeComment((v, "%s", pDestIdx->zName));
 95429     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
 95430     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
 95431     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
 95432     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
 95433     sqlite3VdbeJumpHere(v, addr1);
 95434     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
 95435     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 95437   if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
 95438   sqlite3ReleaseTempReg(pParse, regRowid);
 95439   sqlite3ReleaseTempReg(pParse, regData);
 95440   if( emptyDestTest ){
 95441     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
 95442     sqlite3VdbeJumpHere(v, emptyDestTest);
 95443     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
 95444     return 0;
 95445   }else{
 95446     return 1;
 95449 #endif /* SQLITE_OMIT_XFER_OPT */
 95451 /************** End of insert.c **********************************************/
 95452 /************** Begin file legacy.c ******************************************/
 95453 /*
 95454 ** 2001 September 15
 95455 **
 95456 ** The author disclaims copyright to this source code.  In place of
 95457 ** a legal notice, here is a blessing:
 95458 **
 95459 **    May you do good and not evil.
 95460 **    May you find forgiveness for yourself and forgive others.
 95461 **    May you share freely, never taking more than you give.
 95462 **
 95463 *************************************************************************
 95464 ** Main file for the SQLite library.  The routines in this file
 95465 ** implement the programmer interface to the library.  Routines in
 95466 ** other files are for internal use by SQLite and should not be
 95467 ** accessed by users of the library.
 95468 */
 95471 /*
 95472 ** Execute SQL code.  Return one of the SQLITE_ success/failure
 95473 ** codes.  Also write an error message into memory obtained from
 95474 ** malloc() and make *pzErrMsg point to that message.
 95475 **
 95476 ** If the SQL is a query, then for each row in the query result
 95477 ** the xCallback() function is called.  pArg becomes the first
 95478 ** argument to xCallback().  If xCallback=NULL then no callback
 95479 ** is invoked, even for queries.
 95480 */
 95481 SQLITE_API int sqlite3_exec(
 95482   sqlite3 *db,                /* The database on which the SQL executes */
 95483   const char *zSql,           /* The SQL to be executed */
 95484   sqlite3_callback xCallback, /* Invoke this callback routine */
 95485   void *pArg,                 /* First argument to xCallback() */
 95486   char **pzErrMsg             /* Write error messages here */
 95487 ){
 95488   int rc = SQLITE_OK;         /* Return code */
 95489   const char *zLeftover;      /* Tail of unprocessed SQL */
 95490   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
 95491   char **azCols = 0;          /* Names of result columns */
 95492   int callbackIsInit;         /* True if callback data is initialized */
 95494   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
 95495   if( zSql==0 ) zSql = "";
 95497   sqlite3_mutex_enter(db->mutex);
 95498   sqlite3Error(db, SQLITE_OK, 0);
 95499   while( rc==SQLITE_OK && zSql[0] ){
 95500     int nCol;
 95501     char **azVals = 0;
 95503     pStmt = 0;
 95504     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
 95505     assert( rc==SQLITE_OK || pStmt==0 );
 95506     if( rc!=SQLITE_OK ){
 95507       continue;
 95509     if( !pStmt ){
 95510       /* this happens for a comment or white-space */
 95511       zSql = zLeftover;
 95512       continue;
 95515     callbackIsInit = 0;
 95516     nCol = sqlite3_column_count(pStmt);
 95518     while( 1 ){
 95519       int i;
 95520       rc = sqlite3_step(pStmt);
 95522       /* Invoke the callback function if required */
 95523       if( xCallback && (SQLITE_ROW==rc || 
 95524           (SQLITE_DONE==rc && !callbackIsInit
 95525                            && db->flags&SQLITE_NullCallback)) ){
 95526         if( !callbackIsInit ){
 95527           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
 95528           if( azCols==0 ){
 95529             goto exec_out;
 95531           for(i=0; i<nCol; i++){
 95532             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
 95533             /* sqlite3VdbeSetColName() installs column names as UTF8
 95534             ** strings so there is no way for sqlite3_column_name() to fail. */
 95535             assert( azCols[i]!=0 );
 95537           callbackIsInit = 1;
 95539         if( rc==SQLITE_ROW ){
 95540           azVals = &azCols[nCol];
 95541           for(i=0; i<nCol; i++){
 95542             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
 95543             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
 95544               db->mallocFailed = 1;
 95545               goto exec_out;
 95549         if( xCallback(pArg, nCol, azVals, azCols) ){
 95550           rc = SQLITE_ABORT;
 95551           sqlite3VdbeFinalize((Vdbe *)pStmt);
 95552           pStmt = 0;
 95553           sqlite3Error(db, SQLITE_ABORT, 0);
 95554           goto exec_out;
 95558       if( rc!=SQLITE_ROW ){
 95559         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
 95560         pStmt = 0;
 95561         zSql = zLeftover;
 95562         while( sqlite3Isspace(zSql[0]) ) zSql++;
 95563         break;
 95567     sqlite3DbFree(db, azCols);
 95568     azCols = 0;
 95571 exec_out:
 95572   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
 95573   sqlite3DbFree(db, azCols);
 95575   rc = sqlite3ApiExit(db, rc);
 95576   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
 95577     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
 95578     *pzErrMsg = sqlite3Malloc(nErrMsg);
 95579     if( *pzErrMsg ){
 95580       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
 95581     }else{
 95582       rc = SQLITE_NOMEM;
 95583       sqlite3Error(db, SQLITE_NOMEM, 0);
 95585   }else if( pzErrMsg ){
 95586     *pzErrMsg = 0;
 95589   assert( (rc&db->errMask)==rc );
 95590   sqlite3_mutex_leave(db->mutex);
 95591   return rc;
 95594 /************** End of legacy.c **********************************************/
 95595 /************** Begin file loadext.c *****************************************/
 95596 /*
 95597 ** 2006 June 7
 95598 **
 95599 ** The author disclaims copyright to this source code.  In place of
 95600 ** a legal notice, here is a blessing:
 95601 **
 95602 **    May you do good and not evil.
 95603 **    May you find forgiveness for yourself and forgive others.
 95604 **    May you share freely, never taking more than you give.
 95605 **
 95606 *************************************************************************
 95607 ** This file contains code used to dynamically load extensions into
 95608 ** the SQLite library.
 95609 */
 95611 #ifndef SQLITE_CORE
 95612   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
 95613 #endif
 95614 /************** Include sqlite3ext.h in the middle of loadext.c **************/
 95615 /************** Begin file sqlite3ext.h **************************************/
 95616 /*
 95617 ** 2006 June 7
 95618 **
 95619 ** The author disclaims copyright to this source code.  In place of
 95620 ** a legal notice, here is a blessing:
 95621 **
 95622 **    May you do good and not evil.
 95623 **    May you find forgiveness for yourself and forgive others.
 95624 **    May you share freely, never taking more than you give.
 95625 **
 95626 *************************************************************************
 95627 ** This header file defines the SQLite interface for use by
 95628 ** shared libraries that want to be imported as extensions into
 95629 ** an SQLite instance.  Shared libraries that intend to be loaded
 95630 ** as extensions by SQLite should #include this file instead of 
 95631 ** sqlite3.h.
 95632 */
 95633 #ifndef _SQLITE3EXT_H_
 95634 #define _SQLITE3EXT_H_
 95636 typedef struct sqlite3_api_routines sqlite3_api_routines;
 95638 /*
 95639 ** The following structure holds pointers to all of the SQLite API
 95640 ** routines.
 95641 **
 95642 ** WARNING:  In order to maintain backwards compatibility, add new
 95643 ** interfaces to the end of this structure only.  If you insert new
 95644 ** interfaces in the middle of this structure, then older different
 95645 ** versions of SQLite will not be able to load each others' shared
 95646 ** libraries!
 95647 */
 95648 struct sqlite3_api_routines {
 95649   void * (*aggregate_context)(sqlite3_context*,int nBytes);
 95650   int  (*aggregate_count)(sqlite3_context*);
 95651   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
 95652   int  (*bind_double)(sqlite3_stmt*,int,double);
 95653   int  (*bind_int)(sqlite3_stmt*,int,int);
 95654   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
 95655   int  (*bind_null)(sqlite3_stmt*,int);
 95656   int  (*bind_parameter_count)(sqlite3_stmt*);
 95657   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
 95658   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
 95659   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
 95660   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
 95661   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
 95662   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
 95663   int  (*busy_timeout)(sqlite3*,int ms);
 95664   int  (*changes)(sqlite3*);
 95665   int  (*close)(sqlite3*);
 95666   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
 95667                            int eTextRep,const char*));
 95668   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
 95669                              int eTextRep,const void*));
 95670   const void * (*column_blob)(sqlite3_stmt*,int iCol);
 95671   int  (*column_bytes)(sqlite3_stmt*,int iCol);
 95672   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
 95673   int  (*column_count)(sqlite3_stmt*pStmt);
 95674   const char * (*column_database_name)(sqlite3_stmt*,int);
 95675   const void * (*column_database_name16)(sqlite3_stmt*,int);
 95676   const char * (*column_decltype)(sqlite3_stmt*,int i);
 95677   const void * (*column_decltype16)(sqlite3_stmt*,int);
 95678   double  (*column_double)(sqlite3_stmt*,int iCol);
 95679   int  (*column_int)(sqlite3_stmt*,int iCol);
 95680   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
 95681   const char * (*column_name)(sqlite3_stmt*,int);
 95682   const void * (*column_name16)(sqlite3_stmt*,int);
 95683   const char * (*column_origin_name)(sqlite3_stmt*,int);
 95684   const void * (*column_origin_name16)(sqlite3_stmt*,int);
 95685   const char * (*column_table_name)(sqlite3_stmt*,int);
 95686   const void * (*column_table_name16)(sqlite3_stmt*,int);
 95687   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
 95688   const void * (*column_text16)(sqlite3_stmt*,int iCol);
 95689   int  (*column_type)(sqlite3_stmt*,int iCol);
 95690   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
 95691   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
 95692   int  (*complete)(const char*sql);
 95693   int  (*complete16)(const void*sql);
 95694   int  (*create_collation)(sqlite3*,const char*,int,void*,
 95695                            int(*)(void*,int,const void*,int,const void*));
 95696   int  (*create_collation16)(sqlite3*,const void*,int,void*,
 95697                              int(*)(void*,int,const void*,int,const void*));
 95698   int  (*create_function)(sqlite3*,const char*,int,int,void*,
 95699                           void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 95700                           void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 95701                           void (*xFinal)(sqlite3_context*));
 95702   int  (*create_function16)(sqlite3*,const void*,int,int,void*,
 95703                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 95704                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 95705                             void (*xFinal)(sqlite3_context*));
 95706   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
 95707   int  (*data_count)(sqlite3_stmt*pStmt);
 95708   sqlite3 * (*db_handle)(sqlite3_stmt*);
 95709   int (*declare_vtab)(sqlite3*,const char*);
 95710   int  (*enable_shared_cache)(int);
 95711   int  (*errcode)(sqlite3*db);
 95712   const char * (*errmsg)(sqlite3*);
 95713   const void * (*errmsg16)(sqlite3*);
 95714   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
 95715   int  (*expired)(sqlite3_stmt*);
 95716   int  (*finalize)(sqlite3_stmt*pStmt);
 95717   void  (*free)(void*);
 95718   void  (*free_table)(char**result);
 95719   int  (*get_autocommit)(sqlite3*);
 95720   void * (*get_auxdata)(sqlite3_context*,int);
 95721   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
 95722   int  (*global_recover)(void);
 95723   void  (*interruptx)(sqlite3*);
 95724   sqlite_int64  (*last_insert_rowid)(sqlite3*);
 95725   const char * (*libversion)(void);
 95726   int  (*libversion_number)(void);
 95727   void *(*malloc)(int);
 95728   char * (*mprintf)(const char*,...);
 95729   int  (*open)(const char*,sqlite3**);
 95730   int  (*open16)(const void*,sqlite3**);
 95731   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 95732   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 95733   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
 95734   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
 95735   void *(*realloc)(void*,int);
 95736   int  (*reset)(sqlite3_stmt*pStmt);
 95737   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
 95738   void  (*result_double)(sqlite3_context*,double);
 95739   void  (*result_error)(sqlite3_context*,const char*,int);
 95740   void  (*result_error16)(sqlite3_context*,const void*,int);
 95741   void  (*result_int)(sqlite3_context*,int);
 95742   void  (*result_int64)(sqlite3_context*,sqlite_int64);
 95743   void  (*result_null)(sqlite3_context*);
 95744   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
 95745   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
 95746   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
 95747   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
 95748   void  (*result_value)(sqlite3_context*,sqlite3_value*);
 95749   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
 95750   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
 95751                          const char*,const char*),void*);
 95752   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
 95753   char * (*snprintf)(int,char*,const char*,...);
 95754   int  (*step)(sqlite3_stmt*);
 95755   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
 95756                                 char const**,char const**,int*,int*,int*);
 95757   void  (*thread_cleanup)(void);
 95758   int  (*total_changes)(sqlite3*);
 95759   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
 95760   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
 95761   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
 95762                                          sqlite_int64),void*);
 95763   void * (*user_data)(sqlite3_context*);
 95764   const void * (*value_blob)(sqlite3_value*);
 95765   int  (*value_bytes)(sqlite3_value*);
 95766   int  (*value_bytes16)(sqlite3_value*);
 95767   double  (*value_double)(sqlite3_value*);
 95768   int  (*value_int)(sqlite3_value*);
 95769   sqlite_int64  (*value_int64)(sqlite3_value*);
 95770   int  (*value_numeric_type)(sqlite3_value*);
 95771   const unsigned char * (*value_text)(sqlite3_value*);
 95772   const void * (*value_text16)(sqlite3_value*);
 95773   const void * (*value_text16be)(sqlite3_value*);
 95774   const void * (*value_text16le)(sqlite3_value*);
 95775   int  (*value_type)(sqlite3_value*);
 95776   char *(*vmprintf)(const char*,va_list);
 95777   /* Added ??? */
 95778   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
 95779   /* Added by 3.3.13 */
 95780   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
 95781   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
 95782   int (*clear_bindings)(sqlite3_stmt*);
 95783   /* Added by 3.4.1 */
 95784   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
 95785                           void (*xDestroy)(void *));
 95786   /* Added by 3.5.0 */
 95787   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
 95788   int (*blob_bytes)(sqlite3_blob*);
 95789   int (*blob_close)(sqlite3_blob*);
 95790   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
 95791                    int,sqlite3_blob**);
 95792   int (*blob_read)(sqlite3_blob*,void*,int,int);
 95793   int (*blob_write)(sqlite3_blob*,const void*,int,int);
 95794   int (*create_collation_v2)(sqlite3*,const char*,int,void*,
 95795                              int(*)(void*,int,const void*,int,const void*),
 95796                              void(*)(void*));
 95797   int (*file_control)(sqlite3*,const char*,int,void*);
 95798   sqlite3_int64 (*memory_highwater)(int);
 95799   sqlite3_int64 (*memory_used)(void);
 95800   sqlite3_mutex *(*mutex_alloc)(int);
 95801   void (*mutex_enter)(sqlite3_mutex*);
 95802   void (*mutex_free)(sqlite3_mutex*);
 95803   void (*mutex_leave)(sqlite3_mutex*);
 95804   int (*mutex_try)(sqlite3_mutex*);
 95805   int (*open_v2)(const char*,sqlite3**,int,const char*);
 95806   int (*release_memory)(int);
 95807   void (*result_error_nomem)(sqlite3_context*);
 95808   void (*result_error_toobig)(sqlite3_context*);
 95809   int (*sleep)(int);
 95810   void (*soft_heap_limit)(int);
 95811   sqlite3_vfs *(*vfs_find)(const char*);
 95812   int (*vfs_register)(sqlite3_vfs*,int);
 95813   int (*vfs_unregister)(sqlite3_vfs*);
 95814   int (*xthreadsafe)(void);
 95815   void (*result_zeroblob)(sqlite3_context*,int);
 95816   void (*result_error_code)(sqlite3_context*,int);
 95817   int (*test_control)(int, ...);
 95818   void (*randomness)(int,void*);
 95819   sqlite3 *(*context_db_handle)(sqlite3_context*);
 95820   int (*extended_result_codes)(sqlite3*,int);
 95821   int (*limit)(sqlite3*,int,int);
 95822   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
 95823   const char *(*sql)(sqlite3_stmt*);
 95824   int (*status)(int,int*,int*,int);
 95825   int (*backup_finish)(sqlite3_backup*);
 95826   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
 95827   int (*backup_pagecount)(sqlite3_backup*);
 95828   int (*backup_remaining)(sqlite3_backup*);
 95829   int (*backup_step)(sqlite3_backup*,int);
 95830   const char *(*compileoption_get)(int);
 95831   int (*compileoption_used)(const char*);
 95832   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
 95833                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 95834                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 95835                             void (*xFinal)(sqlite3_context*),
 95836                             void(*xDestroy)(void*));
 95837   int (*db_config)(sqlite3*,int,...);
 95838   sqlite3_mutex *(*db_mutex)(sqlite3*);
 95839   int (*db_status)(sqlite3*,int,int*,int*,int);
 95840   int (*extended_errcode)(sqlite3*);
 95841   void (*log)(int,const char*,...);
 95842   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
 95843   const char *(*sourceid)(void);
 95844   int (*stmt_status)(sqlite3_stmt*,int,int);
 95845   int (*strnicmp)(const char*,const char*,int);
 95846   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
 95847   int (*wal_autocheckpoint)(sqlite3*,int);
 95848   int (*wal_checkpoint)(sqlite3*,const char*);
 95849   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
 95850   int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
 95851   int (*vtab_config)(sqlite3*,int op,...);
 95852   int (*vtab_on_conflict)(sqlite3*);
 95853   /* Version 3.7.16 and later */
 95854   int (*close_v2)(sqlite3*);
 95855   const char *(*db_filename)(sqlite3*,const char*);
 95856   int (*db_readonly)(sqlite3*,const char*);
 95857   int (*db_release_memory)(sqlite3*);
 95858   const char *(*errstr)(int);
 95859   int (*stmt_busy)(sqlite3_stmt*);
 95860   int (*stmt_readonly)(sqlite3_stmt*);
 95861   int (*stricmp)(const char*,const char*);
 95862   int (*uri_boolean)(const char*,const char*,int);
 95863   sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
 95864   const char *(*uri_parameter)(const char*,const char*);
 95865   char *(*vsnprintf)(int,char*,const char*,va_list);
 95866   int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
 95867 };
 95869 /*
 95870 ** The following macros redefine the API routines so that they are
 95871 ** redirected throught the global sqlite3_api structure.
 95872 **
 95873 ** This header file is also used by the loadext.c source file
 95874 ** (part of the main SQLite library - not an extension) so that
 95875 ** it can get access to the sqlite3_api_routines structure
 95876 ** definition.  But the main library does not want to redefine
 95877 ** the API.  So the redefinition macros are only valid if the
 95878 ** SQLITE_CORE macros is undefined.
 95879 */
 95880 #ifndef SQLITE_CORE
 95881 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
 95882 #ifndef SQLITE_OMIT_DEPRECATED
 95883 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
 95884 #endif
 95885 #define sqlite3_bind_blob              sqlite3_api->bind_blob
 95886 #define sqlite3_bind_double            sqlite3_api->bind_double
 95887 #define sqlite3_bind_int               sqlite3_api->bind_int
 95888 #define sqlite3_bind_int64             sqlite3_api->bind_int64
 95889 #define sqlite3_bind_null              sqlite3_api->bind_null
 95890 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
 95891 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
 95892 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
 95893 #define sqlite3_bind_text              sqlite3_api->bind_text
 95894 #define sqlite3_bind_text16            sqlite3_api->bind_text16
 95895 #define sqlite3_bind_value             sqlite3_api->bind_value
 95896 #define sqlite3_busy_handler           sqlite3_api->busy_handler
 95897 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
 95898 #define sqlite3_changes                sqlite3_api->changes
 95899 #define sqlite3_close                  sqlite3_api->close
 95900 #define sqlite3_collation_needed       sqlite3_api->collation_needed
 95901 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
 95902 #define sqlite3_column_blob            sqlite3_api->column_blob
 95903 #define sqlite3_column_bytes           sqlite3_api->column_bytes
 95904 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
 95905 #define sqlite3_column_count           sqlite3_api->column_count
 95906 #define sqlite3_column_database_name   sqlite3_api->column_database_name
 95907 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
 95908 #define sqlite3_column_decltype        sqlite3_api->column_decltype
 95909 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
 95910 #define sqlite3_column_double          sqlite3_api->column_double
 95911 #define sqlite3_column_int             sqlite3_api->column_int
 95912 #define sqlite3_column_int64           sqlite3_api->column_int64
 95913 #define sqlite3_column_name            sqlite3_api->column_name
 95914 #define sqlite3_column_name16          sqlite3_api->column_name16
 95915 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
 95916 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
 95917 #define sqlite3_column_table_name      sqlite3_api->column_table_name
 95918 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
 95919 #define sqlite3_column_text            sqlite3_api->column_text
 95920 #define sqlite3_column_text16          sqlite3_api->column_text16
 95921 #define sqlite3_column_type            sqlite3_api->column_type
 95922 #define sqlite3_column_value           sqlite3_api->column_value
 95923 #define sqlite3_commit_hook            sqlite3_api->commit_hook
 95924 #define sqlite3_complete               sqlite3_api->complete
 95925 #define sqlite3_complete16             sqlite3_api->complete16
 95926 #define sqlite3_create_collation       sqlite3_api->create_collation
 95927 #define sqlite3_create_collation16     sqlite3_api->create_collation16
 95928 #define sqlite3_create_function        sqlite3_api->create_function
 95929 #define sqlite3_create_function16      sqlite3_api->create_function16
 95930 #define sqlite3_create_module          sqlite3_api->create_module
 95931 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
 95932 #define sqlite3_data_count             sqlite3_api->data_count
 95933 #define sqlite3_db_handle              sqlite3_api->db_handle
 95934 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
 95935 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
 95936 #define sqlite3_errcode                sqlite3_api->errcode
 95937 #define sqlite3_errmsg                 sqlite3_api->errmsg
 95938 #define sqlite3_errmsg16               sqlite3_api->errmsg16
 95939 #define sqlite3_exec                   sqlite3_api->exec
 95940 #ifndef SQLITE_OMIT_DEPRECATED
 95941 #define sqlite3_expired                sqlite3_api->expired
 95942 #endif
 95943 #define sqlite3_finalize               sqlite3_api->finalize
 95944 #define sqlite3_free                   sqlite3_api->free
 95945 #define sqlite3_free_table             sqlite3_api->free_table
 95946 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
 95947 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
 95948 #define sqlite3_get_table              sqlite3_api->get_table
 95949 #ifndef SQLITE_OMIT_DEPRECATED
 95950 #define sqlite3_global_recover         sqlite3_api->global_recover
 95951 #endif
 95952 #define sqlite3_interrupt              sqlite3_api->interruptx
 95953 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
 95954 #define sqlite3_libversion             sqlite3_api->libversion
 95955 #define sqlite3_libversion_number      sqlite3_api->libversion_number
 95956 #define sqlite3_malloc                 sqlite3_api->malloc
 95957 #define sqlite3_mprintf                sqlite3_api->mprintf
 95958 #define sqlite3_open                   sqlite3_api->open
 95959 #define sqlite3_open16                 sqlite3_api->open16
 95960 #define sqlite3_prepare                sqlite3_api->prepare
 95961 #define sqlite3_prepare16              sqlite3_api->prepare16
 95962 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 95963 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 95964 #define sqlite3_profile                sqlite3_api->profile
 95965 #define sqlite3_progress_handler       sqlite3_api->progress_handler
 95966 #define sqlite3_realloc                sqlite3_api->realloc
 95967 #define sqlite3_reset                  sqlite3_api->reset
 95968 #define sqlite3_result_blob            sqlite3_api->result_blob
 95969 #define sqlite3_result_double          sqlite3_api->result_double
 95970 #define sqlite3_result_error           sqlite3_api->result_error
 95971 #define sqlite3_result_error16         sqlite3_api->result_error16
 95972 #define sqlite3_result_int             sqlite3_api->result_int
 95973 #define sqlite3_result_int64           sqlite3_api->result_int64
 95974 #define sqlite3_result_null            sqlite3_api->result_null
 95975 #define sqlite3_result_text            sqlite3_api->result_text
 95976 #define sqlite3_result_text16          sqlite3_api->result_text16
 95977 #define sqlite3_result_text16be        sqlite3_api->result_text16be
 95978 #define sqlite3_result_text16le        sqlite3_api->result_text16le
 95979 #define sqlite3_result_value           sqlite3_api->result_value
 95980 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
 95981 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
 95982 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
 95983 #define sqlite3_snprintf               sqlite3_api->snprintf
 95984 #define sqlite3_step                   sqlite3_api->step
 95985 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
 95986 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
 95987 #define sqlite3_total_changes          sqlite3_api->total_changes
 95988 #define sqlite3_trace                  sqlite3_api->trace
 95989 #ifndef SQLITE_OMIT_DEPRECATED
 95990 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
 95991 #endif
 95992 #define sqlite3_update_hook            sqlite3_api->update_hook
 95993 #define sqlite3_user_data              sqlite3_api->user_data
 95994 #define sqlite3_value_blob             sqlite3_api->value_blob
 95995 #define sqlite3_value_bytes            sqlite3_api->value_bytes
 95996 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
 95997 #define sqlite3_value_double           sqlite3_api->value_double
 95998 #define sqlite3_value_int              sqlite3_api->value_int
 95999 #define sqlite3_value_int64            sqlite3_api->value_int64
 96000 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
 96001 #define sqlite3_value_text             sqlite3_api->value_text
 96002 #define sqlite3_value_text16           sqlite3_api->value_text16
 96003 #define sqlite3_value_text16be         sqlite3_api->value_text16be
 96004 #define sqlite3_value_text16le         sqlite3_api->value_text16le
 96005 #define sqlite3_value_type             sqlite3_api->value_type
 96006 #define sqlite3_vmprintf               sqlite3_api->vmprintf
 96007 #define sqlite3_overload_function      sqlite3_api->overload_function
 96008 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 96009 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 96010 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
 96011 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
 96012 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
 96013 #define sqlite3_blob_close             sqlite3_api->blob_close
 96014 #define sqlite3_blob_open              sqlite3_api->blob_open
 96015 #define sqlite3_blob_read              sqlite3_api->blob_read
 96016 #define sqlite3_blob_write             sqlite3_api->blob_write
 96017 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
 96018 #define sqlite3_file_control           sqlite3_api->file_control
 96019 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
 96020 #define sqlite3_memory_used            sqlite3_api->memory_used
 96021 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
 96022 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
 96023 #define sqlite3_mutex_free             sqlite3_api->mutex_free
 96024 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
 96025 #define sqlite3_mutex_try              sqlite3_api->mutex_try
 96026 #define sqlite3_open_v2                sqlite3_api->open_v2
 96027 #define sqlite3_release_memory         sqlite3_api->release_memory
 96028 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
 96029 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
 96030 #define sqlite3_sleep                  sqlite3_api->sleep
 96031 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
 96032 #define sqlite3_vfs_find               sqlite3_api->vfs_find
 96033 #define sqlite3_vfs_register           sqlite3_api->vfs_register
 96034 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
 96035 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
 96036 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
 96037 #define sqlite3_result_error_code      sqlite3_api->result_error_code
 96038 #define sqlite3_test_control           sqlite3_api->test_control
 96039 #define sqlite3_randomness             sqlite3_api->randomness
 96040 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
 96041 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
 96042 #define sqlite3_limit                  sqlite3_api->limit
 96043 #define sqlite3_next_stmt              sqlite3_api->next_stmt
 96044 #define sqlite3_sql                    sqlite3_api->sql
 96045 #define sqlite3_status                 sqlite3_api->status
 96046 #define sqlite3_backup_finish          sqlite3_api->backup_finish
 96047 #define sqlite3_backup_init            sqlite3_api->backup_init
 96048 #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
 96049 #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
 96050 #define sqlite3_backup_step            sqlite3_api->backup_step
 96051 #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
 96052 #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
 96053 #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
 96054 #define sqlite3_db_config              sqlite3_api->db_config
 96055 #define sqlite3_db_mutex               sqlite3_api->db_mutex
 96056 #define sqlite3_db_status              sqlite3_api->db_status
 96057 #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
 96058 #define sqlite3_log                    sqlite3_api->log
 96059 #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
 96060 #define sqlite3_sourceid               sqlite3_api->sourceid
 96061 #define sqlite3_stmt_status            sqlite3_api->stmt_status
 96062 #define sqlite3_strnicmp               sqlite3_api->strnicmp
 96063 #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
 96064 #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
 96065 #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
 96066 #define sqlite3_wal_hook               sqlite3_api->wal_hook
 96067 #define sqlite3_blob_reopen            sqlite3_api->blob_reopen
 96068 #define sqlite3_vtab_config            sqlite3_api->vtab_config
 96069 #define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
 96070 /* Version 3.7.16 and later */
 96071 #define sqlite3_close_v2               sqlite3_api->close_v2
 96072 #define sqlite3_db_filename            sqlite3_api->db_filename
 96073 #define sqlite3_db_readonly            sqlite3_api->db_readonly
 96074 #define sqlite3_db_release_memory      sqlite3_api->db_release_memory
 96075 #define sqlite3_errstr                 sqlite3_api->errstr
 96076 #define sqlite3_stmt_busy              sqlite3_api->stmt_busy
 96077 #define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
 96078 #define sqlite3_stricmp                sqlite3_api->stricmp
 96079 #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
 96080 #define sqlite3_uri_int64              sqlite3_api->uri_int64
 96081 #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
 96082 #define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
 96083 #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
 96084 #endif /* SQLITE_CORE */
 96086 #ifndef SQLITE_CORE
 96087   /* This case when the file really is being compiled as a loadable 
 96088   ** extension */
 96089 # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
 96090 # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
 96091 # define SQLITE_EXTENSION_INIT3     \
 96092     extern const sqlite3_api_routines *sqlite3_api;
 96093 #else
 96094   /* This case when the file is being statically linked into the 
 96095   ** application */
 96096 # define SQLITE_EXTENSION_INIT1     /*no-op*/
 96097 # define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
 96098 # define SQLITE_EXTENSION_INIT3     /*no-op*/
 96099 #endif
 96101 #endif /* _SQLITE3EXT_H_ */
 96103 /************** End of sqlite3ext.h ******************************************/
 96104 /************** Continuing where we left off in loadext.c ********************/
 96105 /* #include <string.h> */
 96107 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 96109 /*
 96110 ** Some API routines are omitted when various features are
 96111 ** excluded from a build of SQLite.  Substitute a NULL pointer
 96112 ** for any missing APIs.
 96113 */
 96114 #ifndef SQLITE_ENABLE_COLUMN_METADATA
 96115 # define sqlite3_column_database_name   0
 96116 # define sqlite3_column_database_name16 0
 96117 # define sqlite3_column_table_name      0
 96118 # define sqlite3_column_table_name16    0
 96119 # define sqlite3_column_origin_name     0
 96120 # define sqlite3_column_origin_name16   0
 96121 # define sqlite3_table_column_metadata  0
 96122 #endif
 96124 #ifdef SQLITE_OMIT_AUTHORIZATION
 96125 # define sqlite3_set_authorizer         0
 96126 #endif
 96128 #ifdef SQLITE_OMIT_UTF16
 96129 # define sqlite3_bind_text16            0
 96130 # define sqlite3_collation_needed16     0
 96131 # define sqlite3_column_decltype16      0
 96132 # define sqlite3_column_name16          0
 96133 # define sqlite3_column_text16          0
 96134 # define sqlite3_complete16             0
 96135 # define sqlite3_create_collation16     0
 96136 # define sqlite3_create_function16      0
 96137 # define sqlite3_errmsg16               0
 96138 # define sqlite3_open16                 0
 96139 # define sqlite3_prepare16              0
 96140 # define sqlite3_prepare16_v2           0
 96141 # define sqlite3_result_error16         0
 96142 # define sqlite3_result_text16          0
 96143 # define sqlite3_result_text16be        0
 96144 # define sqlite3_result_text16le        0
 96145 # define sqlite3_value_text16           0
 96146 # define sqlite3_value_text16be         0
 96147 # define sqlite3_value_text16le         0
 96148 # define sqlite3_column_database_name16 0
 96149 # define sqlite3_column_table_name16    0
 96150 # define sqlite3_column_origin_name16   0
 96151 #endif
 96153 #ifdef SQLITE_OMIT_COMPLETE
 96154 # define sqlite3_complete 0
 96155 # define sqlite3_complete16 0
 96156 #endif
 96158 #ifdef SQLITE_OMIT_DECLTYPE
 96159 # define sqlite3_column_decltype16      0
 96160 # define sqlite3_column_decltype        0
 96161 #endif
 96163 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 96164 # define sqlite3_progress_handler 0
 96165 #endif
 96167 #ifdef SQLITE_OMIT_VIRTUALTABLE
 96168 # define sqlite3_create_module 0
 96169 # define sqlite3_create_module_v2 0
 96170 # define sqlite3_declare_vtab 0
 96171 # define sqlite3_vtab_config 0
 96172 # define sqlite3_vtab_on_conflict 0
 96173 #endif
 96175 #ifdef SQLITE_OMIT_SHARED_CACHE
 96176 # define sqlite3_enable_shared_cache 0
 96177 #endif
 96179 #ifdef SQLITE_OMIT_TRACE
 96180 # define sqlite3_profile       0
 96181 # define sqlite3_trace         0
 96182 #endif
 96184 #ifdef SQLITE_OMIT_GET_TABLE
 96185 # define sqlite3_free_table    0
 96186 # define sqlite3_get_table     0
 96187 #endif
 96189 #ifdef SQLITE_OMIT_INCRBLOB
 96190 #define sqlite3_bind_zeroblob  0
 96191 #define sqlite3_blob_bytes     0
 96192 #define sqlite3_blob_close     0
 96193 #define sqlite3_blob_open      0
 96194 #define sqlite3_blob_read      0
 96195 #define sqlite3_blob_write     0
 96196 #define sqlite3_blob_reopen    0
 96197 #endif
 96199 /*
 96200 ** The following structure contains pointers to all SQLite API routines.
 96201 ** A pointer to this structure is passed into extensions when they are
 96202 ** loaded so that the extension can make calls back into the SQLite
 96203 ** library.
 96204 **
 96205 ** When adding new APIs, add them to the bottom of this structure
 96206 ** in order to preserve backwards compatibility.
 96207 **
 96208 ** Extensions that use newer APIs should first call the
 96209 ** sqlite3_libversion_number() to make sure that the API they
 96210 ** intend to use is supported by the library.  Extensions should
 96211 ** also check to make sure that the pointer to the function is
 96212 ** not NULL before calling it.
 96213 */
 96214 static const sqlite3_api_routines sqlite3Apis = {
 96215   sqlite3_aggregate_context,
 96216 #ifndef SQLITE_OMIT_DEPRECATED
 96217   sqlite3_aggregate_count,
 96218 #else
 96219   0,
 96220 #endif
 96221   sqlite3_bind_blob,
 96222   sqlite3_bind_double,
 96223   sqlite3_bind_int,
 96224   sqlite3_bind_int64,
 96225   sqlite3_bind_null,
 96226   sqlite3_bind_parameter_count,
 96227   sqlite3_bind_parameter_index,
 96228   sqlite3_bind_parameter_name,
 96229   sqlite3_bind_text,
 96230   sqlite3_bind_text16,
 96231   sqlite3_bind_value,
 96232   sqlite3_busy_handler,
 96233   sqlite3_busy_timeout,
 96234   sqlite3_changes,
 96235   sqlite3_close,
 96236   sqlite3_collation_needed,
 96237   sqlite3_collation_needed16,
 96238   sqlite3_column_blob,
 96239   sqlite3_column_bytes,
 96240   sqlite3_column_bytes16,
 96241   sqlite3_column_count,
 96242   sqlite3_column_database_name,
 96243   sqlite3_column_database_name16,
 96244   sqlite3_column_decltype,
 96245   sqlite3_column_decltype16,
 96246   sqlite3_column_double,
 96247   sqlite3_column_int,
 96248   sqlite3_column_int64,
 96249   sqlite3_column_name,
 96250   sqlite3_column_name16,
 96251   sqlite3_column_origin_name,
 96252   sqlite3_column_origin_name16,
 96253   sqlite3_column_table_name,
 96254   sqlite3_column_table_name16,
 96255   sqlite3_column_text,
 96256   sqlite3_column_text16,
 96257   sqlite3_column_type,
 96258   sqlite3_column_value,
 96259   sqlite3_commit_hook,
 96260   sqlite3_complete,
 96261   sqlite3_complete16,
 96262   sqlite3_create_collation,
 96263   sqlite3_create_collation16,
 96264   sqlite3_create_function,
 96265   sqlite3_create_function16,
 96266   sqlite3_create_module,
 96267   sqlite3_data_count,
 96268   sqlite3_db_handle,
 96269   sqlite3_declare_vtab,
 96270   sqlite3_enable_shared_cache,
 96271   sqlite3_errcode,
 96272   sqlite3_errmsg,
 96273   sqlite3_errmsg16,
 96274   sqlite3_exec,
 96275 #ifndef SQLITE_OMIT_DEPRECATED
 96276   sqlite3_expired,
 96277 #else
 96278   0,
 96279 #endif
 96280   sqlite3_finalize,
 96281   sqlite3_free,
 96282   sqlite3_free_table,
 96283   sqlite3_get_autocommit,
 96284   sqlite3_get_auxdata,
 96285   sqlite3_get_table,
 96286   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
 96287   sqlite3_interrupt,
 96288   sqlite3_last_insert_rowid,
 96289   sqlite3_libversion,
 96290   sqlite3_libversion_number,
 96291   sqlite3_malloc,
 96292   sqlite3_mprintf,
 96293   sqlite3_open,
 96294   sqlite3_open16,
 96295   sqlite3_prepare,
 96296   sqlite3_prepare16,
 96297   sqlite3_profile,
 96298   sqlite3_progress_handler,
 96299   sqlite3_realloc,
 96300   sqlite3_reset,
 96301   sqlite3_result_blob,
 96302   sqlite3_result_double,
 96303   sqlite3_result_error,
 96304   sqlite3_result_error16,
 96305   sqlite3_result_int,
 96306   sqlite3_result_int64,
 96307   sqlite3_result_null,
 96308   sqlite3_result_text,
 96309   sqlite3_result_text16,
 96310   sqlite3_result_text16be,
 96311   sqlite3_result_text16le,
 96312   sqlite3_result_value,
 96313   sqlite3_rollback_hook,
 96314   sqlite3_set_authorizer,
 96315   sqlite3_set_auxdata,
 96316   sqlite3_snprintf,
 96317   sqlite3_step,
 96318   sqlite3_table_column_metadata,
 96319 #ifndef SQLITE_OMIT_DEPRECATED
 96320   sqlite3_thread_cleanup,
 96321 #else
 96322   0,
 96323 #endif
 96324   sqlite3_total_changes,
 96325   sqlite3_trace,
 96326 #ifndef SQLITE_OMIT_DEPRECATED
 96327   sqlite3_transfer_bindings,
 96328 #else
 96329   0,
 96330 #endif
 96331   sqlite3_update_hook,
 96332   sqlite3_user_data,
 96333   sqlite3_value_blob,
 96334   sqlite3_value_bytes,
 96335   sqlite3_value_bytes16,
 96336   sqlite3_value_double,
 96337   sqlite3_value_int,
 96338   sqlite3_value_int64,
 96339   sqlite3_value_numeric_type,
 96340   sqlite3_value_text,
 96341   sqlite3_value_text16,
 96342   sqlite3_value_text16be,
 96343   sqlite3_value_text16le,
 96344   sqlite3_value_type,
 96345   sqlite3_vmprintf,
 96346   /*
 96347   ** The original API set ends here.  All extensions can call any
 96348   ** of the APIs above provided that the pointer is not NULL.  But
 96349   ** before calling APIs that follow, extension should check the
 96350   ** sqlite3_libversion_number() to make sure they are dealing with
 96351   ** a library that is new enough to support that API.
 96352   *************************************************************************
 96353   */
 96354   sqlite3_overload_function,
 96356   /*
 96357   ** Added after 3.3.13
 96358   */
 96359   sqlite3_prepare_v2,
 96360   sqlite3_prepare16_v2,
 96361   sqlite3_clear_bindings,
 96363   /*
 96364   ** Added for 3.4.1
 96365   */
 96366   sqlite3_create_module_v2,
 96368   /*
 96369   ** Added for 3.5.0
 96370   */
 96371   sqlite3_bind_zeroblob,
 96372   sqlite3_blob_bytes,
 96373   sqlite3_blob_close,
 96374   sqlite3_blob_open,
 96375   sqlite3_blob_read,
 96376   sqlite3_blob_write,
 96377   sqlite3_create_collation_v2,
 96378   sqlite3_file_control,
 96379   sqlite3_memory_highwater,
 96380   sqlite3_memory_used,
 96381 #ifdef SQLITE_MUTEX_OMIT
 96382   0, 
 96383   0, 
 96384   0,
 96385   0,
 96386   0,
 96387 #else
 96388   sqlite3_mutex_alloc,
 96389   sqlite3_mutex_enter,
 96390   sqlite3_mutex_free,
 96391   sqlite3_mutex_leave,
 96392   sqlite3_mutex_try,
 96393 #endif
 96394   sqlite3_open_v2,
 96395   sqlite3_release_memory,
 96396   sqlite3_result_error_nomem,
 96397   sqlite3_result_error_toobig,
 96398   sqlite3_sleep,
 96399   sqlite3_soft_heap_limit,
 96400   sqlite3_vfs_find,
 96401   sqlite3_vfs_register,
 96402   sqlite3_vfs_unregister,
 96404   /*
 96405   ** Added for 3.5.8
 96406   */
 96407   sqlite3_threadsafe,
 96408   sqlite3_result_zeroblob,
 96409   sqlite3_result_error_code,
 96410   sqlite3_test_control,
 96411   sqlite3_randomness,
 96412   sqlite3_context_db_handle,
 96414   /*
 96415   ** Added for 3.6.0
 96416   */
 96417   sqlite3_extended_result_codes,
 96418   sqlite3_limit,
 96419   sqlite3_next_stmt,
 96420   sqlite3_sql,
 96421   sqlite3_status,
 96423   /*
 96424   ** Added for 3.7.4
 96425   */
 96426   sqlite3_backup_finish,
 96427   sqlite3_backup_init,
 96428   sqlite3_backup_pagecount,
 96429   sqlite3_backup_remaining,
 96430   sqlite3_backup_step,
 96431 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 96432   sqlite3_compileoption_get,
 96433   sqlite3_compileoption_used,
 96434 #else
 96435   0,
 96436   0,
 96437 #endif
 96438   sqlite3_create_function_v2,
 96439   sqlite3_db_config,
 96440   sqlite3_db_mutex,
 96441   sqlite3_db_status,
 96442   sqlite3_extended_errcode,
 96443   sqlite3_log,
 96444   sqlite3_soft_heap_limit64,
 96445   sqlite3_sourceid,
 96446   sqlite3_stmt_status,
 96447   sqlite3_strnicmp,
 96448 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 96449   sqlite3_unlock_notify,
 96450 #else
 96451   0,
 96452 #endif
 96453 #ifndef SQLITE_OMIT_WAL
 96454   sqlite3_wal_autocheckpoint,
 96455   sqlite3_wal_checkpoint,
 96456   sqlite3_wal_hook,
 96457 #else
 96458   0,
 96459   0,
 96460   0,
 96461 #endif
 96462   sqlite3_blob_reopen,
 96463   sqlite3_vtab_config,
 96464   sqlite3_vtab_on_conflict,
 96465   sqlite3_close_v2,
 96466   sqlite3_db_filename,
 96467   sqlite3_db_readonly,
 96468   sqlite3_db_release_memory,
 96469   sqlite3_errstr,
 96470   sqlite3_stmt_busy,
 96471   sqlite3_stmt_readonly,
 96472   sqlite3_stricmp,
 96473   sqlite3_uri_boolean,
 96474   sqlite3_uri_int64,
 96475   sqlite3_uri_parameter,
 96476   sqlite3_vsnprintf,
 96477   sqlite3_wal_checkpoint_v2
 96478 };
 96480 /*
 96481 ** Attempt to load an SQLite extension library contained in the file
 96482 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
 96483 ** default entry point name (sqlite3_extension_init) is used.  Use
 96484 ** of the default name is recommended.
 96485 **
 96486 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
 96487 **
 96488 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
 96489 ** error message text.  The calling function should free this memory
 96490 ** by calling sqlite3DbFree(db, ).
 96491 */
 96492 static int sqlite3LoadExtension(
 96493   sqlite3 *db,          /* Load the extension into this database connection */
 96494   const char *zFile,    /* Name of the shared library containing extension */
 96495   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 96496   char **pzErrMsg       /* Put error message here if not 0 */
 96497 ){
 96498   sqlite3_vfs *pVfs = db->pVfs;
 96499   void *handle;
 96500   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 96501   char *zErrmsg = 0;
 96502   const char *zEntry;
 96503   char *zAltEntry = 0;
 96504   void **aHandle;
 96505   int nMsg = 300 + sqlite3Strlen30(zFile);
 96506   int ii;
 96508   /* Shared library endings to try if zFile cannot be loaded as written */
 96509   static const char *azEndings[] = {
 96510 #if SQLITE_OS_WIN
 96511      "dll"   
 96512 #elif defined(__APPLE__)
 96513      "dylib"
 96514 #else
 96515      "so"
 96516 #endif
 96517   };
 96520   if( pzErrMsg ) *pzErrMsg = 0;
 96522   /* Ticket #1863.  To avoid a creating security problems for older
 96523   ** applications that relink against newer versions of SQLite, the
 96524   ** ability to run load_extension is turned off by default.  One
 96525   ** must call sqlite3_enable_load_extension() to turn on extension
 96526   ** loading.  Otherwise you get the following error.
 96527   */
 96528   if( (db->flags & SQLITE_LoadExtension)==0 ){
 96529     if( pzErrMsg ){
 96530       *pzErrMsg = sqlite3_mprintf("not authorized");
 96532     return SQLITE_ERROR;
 96535   zEntry = zProc ? zProc : "sqlite3_extension_init";
 96537   handle = sqlite3OsDlOpen(pVfs, zFile);
 96538 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
 96539   for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
 96540     char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
 96541     if( zAltFile==0 ) return SQLITE_NOMEM;
 96542     handle = sqlite3OsDlOpen(pVfs, zAltFile);
 96543     sqlite3_free(zAltFile);
 96545 #endif
 96546   if( handle==0 ){
 96547     if( pzErrMsg ){
 96548       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 96549       if( zErrmsg ){
 96550         sqlite3_snprintf(nMsg, zErrmsg, 
 96551             "unable to open shared library [%s]", zFile);
 96552         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 96555     return SQLITE_ERROR;
 96557   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 96558                    sqlite3OsDlSym(pVfs, handle, zEntry);
 96560   /* If no entry point was specified and the default legacy
 96561   ** entry point name "sqlite3_extension_init" was not found, then
 96562   ** construct an entry point name "sqlite3_X_init" where the X is
 96563   ** replaced by the lowercase value of every ASCII alphabetic 
 96564   ** character in the filename after the last "/" upto the first ".",
 96565   ** and eliding the first three characters if they are "lib".  
 96566   ** Examples:
 96567   **
 96568   **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
 96569   **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
 96570   */
 96571   if( xInit==0 && zProc==0 ){
 96572     int iFile, iEntry, c;
 96573     int ncFile = sqlite3Strlen30(zFile);
 96574     zAltEntry = sqlite3_malloc(ncFile+30);
 96575     if( zAltEntry==0 ){
 96576       sqlite3OsDlClose(pVfs, handle);
 96577       return SQLITE_NOMEM;
 96579     memcpy(zAltEntry, "sqlite3_", 8);
 96580     for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
 96581     iFile++;
 96582     if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
 96583     for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
 96584       if( sqlite3Isalpha(c) ){
 96585         zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
 96588     memcpy(zAltEntry+iEntry, "_init", 6);
 96589     zEntry = zAltEntry;
 96590     xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 96591                      sqlite3OsDlSym(pVfs, handle, zEntry);
 96593   if( xInit==0 ){
 96594     if( pzErrMsg ){
 96595       nMsg += sqlite3Strlen30(zEntry);
 96596       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
 96597       if( zErrmsg ){
 96598         sqlite3_snprintf(nMsg, zErrmsg,
 96599             "no entry point [%s] in shared library [%s]", zEntry, zFile);
 96600         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
 96603     sqlite3OsDlClose(pVfs, handle);
 96604     sqlite3_free(zAltEntry);
 96605     return SQLITE_ERROR;
 96607   sqlite3_free(zAltEntry);
 96608   if( xInit(db, &zErrmsg, &sqlite3Apis) ){
 96609     if( pzErrMsg ){
 96610       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
 96612     sqlite3_free(zErrmsg);
 96613     sqlite3OsDlClose(pVfs, handle);
 96614     return SQLITE_ERROR;
 96617   /* Append the new shared library handle to the db->aExtension array. */
 96618   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
 96619   if( aHandle==0 ){
 96620     return SQLITE_NOMEM;
 96622   if( db->nExtension>0 ){
 96623     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
 96625   sqlite3DbFree(db, db->aExtension);
 96626   db->aExtension = aHandle;
 96628   db->aExtension[db->nExtension++] = handle;
 96629   return SQLITE_OK;
 96631 SQLITE_API int sqlite3_load_extension(
 96632   sqlite3 *db,          /* Load the extension into this database connection */
 96633   const char *zFile,    /* Name of the shared library containing extension */
 96634   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
 96635   char **pzErrMsg       /* Put error message here if not 0 */
 96636 ){
 96637   int rc;
 96638   sqlite3_mutex_enter(db->mutex);
 96639   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
 96640   rc = sqlite3ApiExit(db, rc);
 96641   sqlite3_mutex_leave(db->mutex);
 96642   return rc;
 96645 /*
 96646 ** Call this routine when the database connection is closing in order
 96647 ** to clean up loaded extensions
 96648 */
 96649 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
 96650   int i;
 96651   assert( sqlite3_mutex_held(db->mutex) );
 96652   for(i=0; i<db->nExtension; i++){
 96653     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
 96655   sqlite3DbFree(db, db->aExtension);
 96658 /*
 96659 ** Enable or disable extension loading.  Extension loading is disabled by
 96660 ** default so as not to open security holes in older applications.
 96661 */
 96662 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
 96663   sqlite3_mutex_enter(db->mutex);
 96664   if( onoff ){
 96665     db->flags |= SQLITE_LoadExtension;
 96666   }else{
 96667     db->flags &= ~SQLITE_LoadExtension;
 96669   sqlite3_mutex_leave(db->mutex);
 96670   return SQLITE_OK;
 96673 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
 96675 /*
 96676 ** The auto-extension code added regardless of whether or not extension
 96677 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
 96678 ** code if regular extension loading is not available.  This is that
 96679 ** dummy pointer.
 96680 */
 96681 #ifdef SQLITE_OMIT_LOAD_EXTENSION
 96682 static const sqlite3_api_routines sqlite3Apis = { 0 };
 96683 #endif
 96686 /*
 96687 ** The following object holds the list of automatically loaded
 96688 ** extensions.
 96689 **
 96690 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
 96691 ** mutex must be held while accessing this list.
 96692 */
 96693 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
 96694 static SQLITE_WSD struct sqlite3AutoExtList {
 96695   int nExt;              /* Number of entries in aExt[] */          
 96696   void (**aExt)(void);   /* Pointers to the extension init functions */
 96697 } sqlite3Autoext = { 0, 0 };
 96699 /* The "wsdAutoext" macro will resolve to the autoextension
 96700 ** state vector.  If writable static data is unsupported on the target,
 96701 ** we have to locate the state vector at run-time.  In the more common
 96702 ** case where writable static data is supported, wsdStat can refer directly
 96703 ** to the "sqlite3Autoext" state vector declared above.
 96704 */
 96705 #ifdef SQLITE_OMIT_WSD
 96706 # define wsdAutoextInit \
 96707   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
 96708 # define wsdAutoext x[0]
 96709 #else
 96710 # define wsdAutoextInit
 96711 # define wsdAutoext sqlite3Autoext
 96712 #endif
 96715 /*
 96716 ** Register a statically linked extension that is automatically
 96717 ** loaded by every new database connection.
 96718 */
 96719 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
 96720   int rc = SQLITE_OK;
 96721 #ifndef SQLITE_OMIT_AUTOINIT
 96722   rc = sqlite3_initialize();
 96723   if( rc ){
 96724     return rc;
 96725   }else
 96726 #endif
 96728     int i;
 96729 #if SQLITE_THREADSAFE
 96730     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 96731 #endif
 96732     wsdAutoextInit;
 96733     sqlite3_mutex_enter(mutex);
 96734     for(i=0; i<wsdAutoext.nExt; i++){
 96735       if( wsdAutoext.aExt[i]==xInit ) break;
 96737     if( i==wsdAutoext.nExt ){
 96738       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
 96739       void (**aNew)(void);
 96740       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
 96741       if( aNew==0 ){
 96742         rc = SQLITE_NOMEM;
 96743       }else{
 96744         wsdAutoext.aExt = aNew;
 96745         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
 96746         wsdAutoext.nExt++;
 96749     sqlite3_mutex_leave(mutex);
 96750     assert( (rc&0xff)==rc );
 96751     return rc;
 96755 /*
 96756 ** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
 96757 ** set of routines that is invoked for each new database connection, if it
 96758 ** is currently on the list.  If xInit is not on the list, then this
 96759 ** routine is a no-op.
 96760 **
 96761 ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
 96762 ** was not on the list.
 96763 */
 96764 SQLITE_API int sqlite3_cancel_auto_extension(void (*xInit)(void)){
 96765 #if SQLITE_THREADSAFE
 96766   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 96767 #endif
 96768   int i;
 96769   int n = 0;
 96770   wsdAutoextInit;
 96771   sqlite3_mutex_enter(mutex);
 96772   for(i=wsdAutoext.nExt-1; i>=0; i--){
 96773     if( wsdAutoext.aExt[i]==xInit ){
 96774       wsdAutoext.nExt--;
 96775       wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
 96776       n++;
 96777       break;
 96780   sqlite3_mutex_leave(mutex);
 96781   return n;
 96784 /*
 96785 ** Reset the automatic extension loading mechanism.
 96786 */
 96787 SQLITE_API void sqlite3_reset_auto_extension(void){
 96788 #ifndef SQLITE_OMIT_AUTOINIT
 96789   if( sqlite3_initialize()==SQLITE_OK )
 96790 #endif
 96792 #if SQLITE_THREADSAFE
 96793     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 96794 #endif
 96795     wsdAutoextInit;
 96796     sqlite3_mutex_enter(mutex);
 96797     sqlite3_free(wsdAutoext.aExt);
 96798     wsdAutoext.aExt = 0;
 96799     wsdAutoext.nExt = 0;
 96800     sqlite3_mutex_leave(mutex);
 96804 /*
 96805 ** Load all automatic extensions.
 96806 **
 96807 ** If anything goes wrong, set an error in the database connection.
 96808 */
 96809 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
 96810   int i;
 96811   int go = 1;
 96812   int rc;
 96813   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 96815   wsdAutoextInit;
 96816   if( wsdAutoext.nExt==0 ){
 96817     /* Common case: early out without every having to acquire a mutex */
 96818     return;
 96820   for(i=0; go; i++){
 96821     char *zErrmsg;
 96822 #if SQLITE_THREADSAFE
 96823     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
 96824 #endif
 96825     sqlite3_mutex_enter(mutex);
 96826     if( i>=wsdAutoext.nExt ){
 96827       xInit = 0;
 96828       go = 0;
 96829     }else{
 96830       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
 96831               wsdAutoext.aExt[i];
 96833     sqlite3_mutex_leave(mutex);
 96834     zErrmsg = 0;
 96835     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
 96836       sqlite3Error(db, rc,
 96837             "automatic extension loading failed: %s", zErrmsg);
 96838       go = 0;
 96840     sqlite3_free(zErrmsg);
 96844 /************** End of loadext.c *********************************************/
 96845 /************** Begin file pragma.c ******************************************/
 96846 /*
 96847 ** 2003 April 6
 96848 **
 96849 ** The author disclaims copyright to this source code.  In place of
 96850 ** a legal notice, here is a blessing:
 96851 **
 96852 **    May you do good and not evil.
 96853 **    May you find forgiveness for yourself and forgive others.
 96854 **    May you share freely, never taking more than you give.
 96855 **
 96856 *************************************************************************
 96857 ** This file contains code used to implement the PRAGMA command.
 96858 */
 96860 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
 96861 #  if defined(__APPLE__)
 96862 #    define SQLITE_ENABLE_LOCKING_STYLE 1
 96863 #  else
 96864 #    define SQLITE_ENABLE_LOCKING_STYLE 0
 96865 #  endif
 96866 #endif
 96868 /***************************************************************************
 96869 ** The next block of code, including the PragTyp_XXXX macro definitions and
 96870 ** the aPragmaName[] object is composed of generated code. DO NOT EDIT.
 96871 **
 96872 ** To add new pragmas, edit the code in ../tool/mkpragmatab.tcl and rerun
 96873 ** that script.  Then copy/paste the output in place of the following:
 96874 */
 96875 #define PragTyp_HEADER_VALUE                   0
 96876 #define PragTyp_AUTO_VACUUM                    1
 96877 #define PragTyp_FLAG                           2
 96878 #define PragTyp_BUSY_TIMEOUT                   3
 96879 #define PragTyp_CACHE_SIZE                     4
 96880 #define PragTyp_CASE_SENSITIVE_LIKE            5
 96881 #define PragTyp_COLLATION_LIST                 6
 96882 #define PragTyp_COMPILE_OPTIONS                7
 96883 #define PragTyp_DATA_STORE_DIRECTORY           8
 96884 #define PragTyp_DATABASE_LIST                  9
 96885 #define PragTyp_DEFAULT_CACHE_SIZE            10
 96886 #define PragTyp_ENCODING                      11
 96887 #define PragTyp_FOREIGN_KEY_CHECK             12
 96888 #define PragTyp_FOREIGN_KEY_LIST              13
 96889 #define PragTyp_INCREMENTAL_VACUUM            14
 96890 #define PragTyp_INDEX_INFO                    15
 96891 #define PragTyp_INDEX_LIST                    16
 96892 #define PragTyp_INTEGRITY_CHECK               17
 96893 #define PragTyp_JOURNAL_MODE                  18
 96894 #define PragTyp_JOURNAL_SIZE_LIMIT            19
 96895 #define PragTyp_LOCK_PROXY_FILE               20
 96896 #define PragTyp_LOCKING_MODE                  21
 96897 #define PragTyp_PAGE_COUNT                    22
 96898 #define PragTyp_MMAP_SIZE                     23
 96899 #define PragTyp_PAGE_SIZE                     24
 96900 #define PragTyp_SECURE_DELETE                 25
 96901 #define PragTyp_SHRINK_MEMORY                 26
 96902 #define PragTyp_SOFT_HEAP_LIMIT               27
 96903 #define PragTyp_STATS                         28
 96904 #define PragTyp_SYNCHRONOUS                   29
 96905 #define PragTyp_TABLE_INFO                    30
 96906 #define PragTyp_TEMP_STORE                    31
 96907 #define PragTyp_TEMP_STORE_DIRECTORY          32
 96908 #define PragTyp_WAL_AUTOCHECKPOINT            33
 96909 #define PragTyp_WAL_CHECKPOINT                34
 96910 #define PragTyp_ACTIVATE_EXTENSIONS           35
 96911 #define PragTyp_HEXKEY                        36
 96912 #define PragTyp_KEY                           37
 96913 #define PragTyp_REKEY                         38
 96914 #define PragTyp_LOCK_STATUS                   39
 96915 #define PragTyp_PARSER_TRACE                  40
 96916 #define PragFlag_NeedSchema           0x01
 96917 static const struct sPragmaNames {
 96918   const char *const zName;  /* Name of pragma */
 96919   u8 ePragTyp;              /* PragTyp_XXX value */
 96920   u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
 96921   u32 iArg;                 /* Extra argument */
 96922 } aPragmaNames[] = {
 96923 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 96924   { /* zName:     */ "activate_extensions",
 96925     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
 96926     /* ePragFlag: */ 0,
 96927     /* iArg:      */ 0 },
 96928 #endif
 96929 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
 96930   { /* zName:     */ "application_id",
 96931     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
 96932     /* ePragFlag: */ 0,
 96933     /* iArg:      */ 0 },
 96934 #endif
 96935 #if !defined(SQLITE_OMIT_AUTOVACUUM)
 96936   { /* zName:     */ "auto_vacuum",
 96937     /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
 96938     /* ePragFlag: */ PragFlag_NeedSchema,
 96939     /* iArg:      */ 0 },
 96940 #endif
 96941 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 96942 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
 96943   { /* zName:     */ "automatic_index",
 96944     /* ePragTyp:  */ PragTyp_FLAG,
 96945     /* ePragFlag: */ 0,
 96946     /* iArg:      */ SQLITE_AutoIndex },
 96947 #endif
 96948 #endif
 96949   { /* zName:     */ "busy_timeout",
 96950     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
 96951     /* ePragFlag: */ 0,
 96952     /* iArg:      */ 0 },
 96953 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 96954   { /* zName:     */ "cache_size",
 96955     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
 96956     /* ePragFlag: */ PragFlag_NeedSchema,
 96957     /* iArg:      */ 0 },
 96958 #endif
 96959 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 96960   { /* zName:     */ "cache_spill",
 96961     /* ePragTyp:  */ PragTyp_FLAG,
 96962     /* ePragFlag: */ 0,
 96963     /* iArg:      */ SQLITE_CacheSpill },
 96964 #endif
 96965   { /* zName:     */ "case_sensitive_like",
 96966     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
 96967     /* ePragFlag: */ 0,
 96968     /* iArg:      */ 0 },
 96969 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 96970   { /* zName:     */ "checkpoint_fullfsync",
 96971     /* ePragTyp:  */ PragTyp_FLAG,
 96972     /* ePragFlag: */ 0,
 96973     /* iArg:      */ SQLITE_CkptFullFSync },
 96974 #endif
 96975 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
 96976   { /* zName:     */ "collation_list",
 96977     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
 96978     /* ePragFlag: */ 0,
 96979     /* iArg:      */ 0 },
 96980 #endif
 96981 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
 96982   { /* zName:     */ "compile_options",
 96983     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
 96984     /* ePragFlag: */ 0,
 96985     /* iArg:      */ 0 },
 96986 #endif
 96987 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 96988   { /* zName:     */ "count_changes",
 96989     /* ePragTyp:  */ PragTyp_FLAG,
 96990     /* ePragFlag: */ 0,
 96991     /* iArg:      */ SQLITE_CountRows },
 96992 #endif
 96993 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
 96994   { /* zName:     */ "data_store_directory",
 96995     /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
 96996     /* ePragFlag: */ 0,
 96997     /* iArg:      */ 0 },
 96998 #endif
 96999 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
 97000   { /* zName:     */ "database_list",
 97001     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
 97002     /* ePragFlag: */ PragFlag_NeedSchema,
 97003     /* iArg:      */ 0 },
 97004 #endif
 97005 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
 97006   { /* zName:     */ "default_cache_size",
 97007     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
 97008     /* ePragFlag: */ PragFlag_NeedSchema,
 97009     /* iArg:      */ 0 },
 97010 #endif
 97011 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97012 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 97013   { /* zName:     */ "defer_foreign_keys",
 97014     /* ePragTyp:  */ PragTyp_FLAG,
 97015     /* ePragFlag: */ 0,
 97016     /* iArg:      */ SQLITE_DeferFKs },
 97017 #endif
 97018 #endif
 97019 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97020   { /* zName:     */ "empty_result_callbacks",
 97021     /* ePragTyp:  */ PragTyp_FLAG,
 97022     /* ePragFlag: */ 0,
 97023     /* iArg:      */ SQLITE_NullCallback },
 97024 #endif
 97025 #if !defined(SQLITE_OMIT_UTF16)
 97026   { /* zName:     */ "encoding",
 97027     /* ePragTyp:  */ PragTyp_ENCODING,
 97028     /* ePragFlag: */ 0,
 97029     /* iArg:      */ 0 },
 97030 #endif
 97031 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 97032   { /* zName:     */ "foreign_key_check",
 97033     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
 97034     /* ePragFlag: */ PragFlag_NeedSchema,
 97035     /* iArg:      */ 0 },
 97036 #endif
 97037 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
 97038   { /* zName:     */ "foreign_key_list",
 97039     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
 97040     /* ePragFlag: */ PragFlag_NeedSchema,
 97041     /* iArg:      */ 0 },
 97042 #endif
 97043 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97044 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
 97045   { /* zName:     */ "foreign_keys",
 97046     /* ePragTyp:  */ PragTyp_FLAG,
 97047     /* ePragFlag: */ 0,
 97048     /* iArg:      */ SQLITE_ForeignKeys },
 97049 #endif
 97050 #endif
 97051 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
 97052   { /* zName:     */ "freelist_count",
 97053     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
 97054     /* ePragFlag: */ 0,
 97055     /* iArg:      */ 0 },
 97056 #endif
 97057 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97058   { /* zName:     */ "full_column_names",
 97059     /* ePragTyp:  */ PragTyp_FLAG,
 97060     /* ePragFlag: */ 0,
 97061     /* iArg:      */ SQLITE_FullColNames },
 97062   { /* zName:     */ "fullfsync",
 97063     /* ePragTyp:  */ PragTyp_FLAG,
 97064     /* ePragFlag: */ 0,
 97065     /* iArg:      */ SQLITE_FullFSync },
 97066 #endif
 97067 #if defined(SQLITE_HAS_CODEC)
 97068   { /* zName:     */ "hexkey",
 97069     /* ePragTyp:  */ PragTyp_HEXKEY,
 97070     /* ePragFlag: */ 0,
 97071     /* iArg:      */ 0 },
 97072   { /* zName:     */ "hexrekey",
 97073     /* ePragTyp:  */ PragTyp_HEXKEY,
 97074     /* ePragFlag: */ 0,
 97075     /* iArg:      */ 0 },
 97076 #endif
 97077 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97078 #if !defined(SQLITE_OMIT_CHECK)
 97079   { /* zName:     */ "ignore_check_constraints",
 97080     /* ePragTyp:  */ PragTyp_FLAG,
 97081     /* ePragFlag: */ 0,
 97082     /* iArg:      */ SQLITE_IgnoreChecks },
 97083 #endif
 97084 #endif
 97085 #if !defined(SQLITE_OMIT_AUTOVACUUM)
 97086   { /* zName:     */ "incremental_vacuum",
 97087     /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
 97088     /* ePragFlag: */ PragFlag_NeedSchema,
 97089     /* iArg:      */ 0 },
 97090 #endif
 97091 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
 97092   { /* zName:     */ "index_info",
 97093     /* ePragTyp:  */ PragTyp_INDEX_INFO,
 97094     /* ePragFlag: */ PragFlag_NeedSchema,
 97095     /* iArg:      */ 0 },
 97096   { /* zName:     */ "index_list",
 97097     /* ePragTyp:  */ PragTyp_INDEX_LIST,
 97098     /* ePragFlag: */ PragFlag_NeedSchema,
 97099     /* iArg:      */ 0 },
 97100 #endif
 97101 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
 97102   { /* zName:     */ "integrity_check",
 97103     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
 97104     /* ePragFlag: */ PragFlag_NeedSchema,
 97105     /* iArg:      */ 0 },
 97106 #endif
 97107 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97108   { /* zName:     */ "journal_mode",
 97109     /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
 97110     /* ePragFlag: */ PragFlag_NeedSchema,
 97111     /* iArg:      */ 0 },
 97112   { /* zName:     */ "journal_size_limit",
 97113     /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
 97114     /* ePragFlag: */ 0,
 97115     /* iArg:      */ 0 },
 97116 #endif
 97117 #if defined(SQLITE_HAS_CODEC)
 97118   { /* zName:     */ "key",
 97119     /* ePragTyp:  */ PragTyp_KEY,
 97120     /* ePragFlag: */ 0,
 97121     /* iArg:      */ 0 },
 97122 #endif
 97123 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97124   { /* zName:     */ "legacy_file_format",
 97125     /* ePragTyp:  */ PragTyp_FLAG,
 97126     /* ePragFlag: */ 0,
 97127     /* iArg:      */ SQLITE_LegacyFileFmt },
 97128 #endif
 97129 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
 97130   { /* zName:     */ "lock_proxy_file",
 97131     /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
 97132     /* ePragFlag: */ 0,
 97133     /* iArg:      */ 0 },
 97134 #endif
 97135 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 97136   { /* zName:     */ "lock_status",
 97137     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
 97138     /* ePragFlag: */ 0,
 97139     /* iArg:      */ 0 },
 97140 #endif
 97141 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97142   { /* zName:     */ "locking_mode",
 97143     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
 97144     /* ePragFlag: */ 0,
 97145     /* iArg:      */ 0 },
 97146   { /* zName:     */ "max_page_count",
 97147     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
 97148     /* ePragFlag: */ PragFlag_NeedSchema,
 97149     /* iArg:      */ 0 },
 97150   { /* zName:     */ "mmap_size",
 97151     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
 97152     /* ePragFlag: */ 0,
 97153     /* iArg:      */ 0 },
 97154   { /* zName:     */ "page_count",
 97155     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
 97156     /* ePragFlag: */ PragFlag_NeedSchema,
 97157     /* iArg:      */ 0 },
 97158   { /* zName:     */ "page_size",
 97159     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
 97160     /* ePragFlag: */ 0,
 97161     /* iArg:      */ 0 },
 97162 #endif
 97163 #if defined(SQLITE_DEBUG)
 97164   { /* zName:     */ "parser_trace",
 97165     /* ePragTyp:  */ PragTyp_PARSER_TRACE,
 97166     /* ePragFlag: */ 0,
 97167     /* iArg:      */ 0 },
 97168 #endif
 97169 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97170   { /* zName:     */ "query_only",
 97171     /* ePragTyp:  */ PragTyp_FLAG,
 97172     /* ePragFlag: */ 0,
 97173     /* iArg:      */ SQLITE_QueryOnly },
 97174 #endif
 97175 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
 97176   { /* zName:     */ "quick_check",
 97177     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
 97178     /* ePragFlag: */ PragFlag_NeedSchema,
 97179     /* iArg:      */ 0 },
 97180 #endif
 97181 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97182   { /* zName:     */ "read_uncommitted",
 97183     /* ePragTyp:  */ PragTyp_FLAG,
 97184     /* ePragFlag: */ 0,
 97185     /* iArg:      */ SQLITE_ReadUncommitted },
 97186   { /* zName:     */ "recursive_triggers",
 97187     /* ePragTyp:  */ PragTyp_FLAG,
 97188     /* ePragFlag: */ 0,
 97189     /* iArg:      */ SQLITE_RecTriggers },
 97190 #endif
 97191 #if defined(SQLITE_HAS_CODEC)
 97192   { /* zName:     */ "rekey",
 97193     /* ePragTyp:  */ PragTyp_REKEY,
 97194     /* ePragFlag: */ 0,
 97195     /* iArg:      */ 0 },
 97196 #endif
 97197 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97198   { /* zName:     */ "reverse_unordered_selects",
 97199     /* ePragTyp:  */ PragTyp_FLAG,
 97200     /* ePragFlag: */ 0,
 97201     /* iArg:      */ SQLITE_ReverseOrder },
 97202 #endif
 97203 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
 97204   { /* zName:     */ "schema_version",
 97205     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
 97206     /* ePragFlag: */ 0,
 97207     /* iArg:      */ 0 },
 97208 #endif
 97209 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97210   { /* zName:     */ "secure_delete",
 97211     /* ePragTyp:  */ PragTyp_SECURE_DELETE,
 97212     /* ePragFlag: */ 0,
 97213     /* iArg:      */ 0 },
 97214 #endif
 97215 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97216   { /* zName:     */ "short_column_names",
 97217     /* ePragTyp:  */ PragTyp_FLAG,
 97218     /* ePragFlag: */ 0,
 97219     /* iArg:      */ SQLITE_ShortColNames },
 97220 #endif
 97221   { /* zName:     */ "shrink_memory",
 97222     /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
 97223     /* ePragFlag: */ 0,
 97224     /* iArg:      */ 0 },
 97225   { /* zName:     */ "soft_heap_limit",
 97226     /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
 97227     /* ePragFlag: */ 0,
 97228     /* iArg:      */ 0 },
 97229 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97230 #if defined(SQLITE_DEBUG)
 97231   { /* zName:     */ "sql_trace",
 97232     /* ePragTyp:  */ PragTyp_FLAG,
 97233     /* ePragFlag: */ 0,
 97234     /* iArg:      */ SQLITE_SqlTrace },
 97235 #endif
 97236 #endif
 97237 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
 97238   { /* zName:     */ "stats",
 97239     /* ePragTyp:  */ PragTyp_STATS,
 97240     /* ePragFlag: */ PragFlag_NeedSchema,
 97241     /* iArg:      */ 0 },
 97242 #endif
 97243 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97244   { /* zName:     */ "synchronous",
 97245     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
 97246     /* ePragFlag: */ PragFlag_NeedSchema,
 97247     /* iArg:      */ 0 },
 97248 #endif
 97249 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
 97250   { /* zName:     */ "table_info",
 97251     /* ePragTyp:  */ PragTyp_TABLE_INFO,
 97252     /* ePragFlag: */ PragFlag_NeedSchema,
 97253     /* iArg:      */ 0 },
 97254 #endif
 97255 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97256   { /* zName:     */ "temp_store",
 97257     /* ePragTyp:  */ PragTyp_TEMP_STORE,
 97258     /* ePragFlag: */ 0,
 97259     /* iArg:      */ 0 },
 97260   { /* zName:     */ "temp_store_directory",
 97261     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
 97262     /* ePragFlag: */ 0,
 97263     /* iArg:      */ 0 },
 97264 #endif
 97265 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
 97266   { /* zName:     */ "user_version",
 97267     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
 97268     /* ePragFlag: */ 0,
 97269     /* iArg:      */ 0 },
 97270 #endif
 97271 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97272 #if defined(SQLITE_DEBUG)
 97273   { /* zName:     */ "vdbe_addoptrace",
 97274     /* ePragTyp:  */ PragTyp_FLAG,
 97275     /* ePragFlag: */ 0,
 97276     /* iArg:      */ SQLITE_VdbeAddopTrace },
 97277   { /* zName:     */ "vdbe_debug",
 97278     /* ePragTyp:  */ PragTyp_FLAG,
 97279     /* ePragFlag: */ 0,
 97280     /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
 97281   { /* zName:     */ "vdbe_eqp",
 97282     /* ePragTyp:  */ PragTyp_FLAG,
 97283     /* ePragFlag: */ 0,
 97284     /* iArg:      */ SQLITE_VdbeEQP },
 97285   { /* zName:     */ "vdbe_listing",
 97286     /* ePragTyp:  */ PragTyp_FLAG,
 97287     /* ePragFlag: */ 0,
 97288     /* iArg:      */ SQLITE_VdbeListing },
 97289   { /* zName:     */ "vdbe_trace",
 97290     /* ePragTyp:  */ PragTyp_FLAG,
 97291     /* ePragFlag: */ 0,
 97292     /* iArg:      */ SQLITE_VdbeTrace },
 97293 #endif
 97294 #endif
 97295 #if !defined(SQLITE_OMIT_WAL)
 97296   { /* zName:     */ "wal_autocheckpoint",
 97297     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
 97298     /* ePragFlag: */ 0,
 97299     /* iArg:      */ 0 },
 97300   { /* zName:     */ "wal_checkpoint",
 97301     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
 97302     /* ePragFlag: */ PragFlag_NeedSchema,
 97303     /* iArg:      */ 0 },
 97304 #endif
 97305 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
 97306   { /* zName:     */ "writable_schema",
 97307     /* ePragTyp:  */ PragTyp_FLAG,
 97308     /* ePragFlag: */ 0,
 97309     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
 97310 #endif
 97311 };
 97312 /* Number of pragmas: 56 on by default, 69 total. */
 97313 /* End of the automatically generated pragma table.
 97314 ***************************************************************************/
 97316 /*
 97317 ** Interpret the given string as a safety level.  Return 0 for OFF,
 97318 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
 97319 ** unrecognized string argument.  The FULL option is disallowed
 97320 ** if the omitFull parameter it 1.
 97321 **
 97322 ** Note that the values returned are one less that the values that
 97323 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
 97324 ** to support legacy SQL code.  The safety level used to be boolean
 97325 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
 97326 */
 97327 static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
 97328                              /* 123456789 123456789 */
 97329   static const char zText[] = "onoffalseyestruefull";
 97330   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
 97331   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
 97332   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
 97333   int i, n;
 97334   if( sqlite3Isdigit(*z) ){
 97335     return (u8)sqlite3Atoi(z);
 97337   n = sqlite3Strlen30(z);
 97338   for(i=0; i<ArraySize(iLength)-omitFull; i++){
 97339     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
 97340       return iValue[i];
 97343   return dflt;
 97346 /*
 97347 ** Interpret the given string as a boolean value.
 97348 */
 97349 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
 97350   return getSafetyLevel(z,1,dflt)!=0;
 97353 /* The sqlite3GetBoolean() function is used by other modules but the
 97354 ** remainder of this file is specific to PRAGMA processing.  So omit
 97355 ** the rest of the file if PRAGMAs are omitted from the build.
 97356 */
 97357 #if !defined(SQLITE_OMIT_PRAGMA)
 97359 /*
 97360 ** Interpret the given string as a locking mode value.
 97361 */
 97362 static int getLockingMode(const char *z){
 97363   if( z ){
 97364     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
 97365     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
 97367   return PAGER_LOCKINGMODE_QUERY;
 97370 #ifndef SQLITE_OMIT_AUTOVACUUM
 97371 /*
 97372 ** Interpret the given string as an auto-vacuum mode value.
 97373 **
 97374 ** The following strings, "none", "full" and "incremental" are 
 97375 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
 97376 */
 97377 static int getAutoVacuum(const char *z){
 97378   int i;
 97379   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
 97380   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
 97381   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
 97382   i = sqlite3Atoi(z);
 97383   return (u8)((i>=0&&i<=2)?i:0);
 97385 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
 97387 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97388 /*
 97389 ** Interpret the given string as a temp db location. Return 1 for file
 97390 ** backed temporary databases, 2 for the Red-Black tree in memory database
 97391 ** and 0 to use the compile-time default.
 97392 */
 97393 static int getTempStore(const char *z){
 97394   if( z[0]>='0' && z[0]<='2' ){
 97395     return z[0] - '0';
 97396   }else if( sqlite3StrICmp(z, "file")==0 ){
 97397     return 1;
 97398   }else if( sqlite3StrICmp(z, "memory")==0 ){
 97399     return 2;
 97400   }else{
 97401     return 0;
 97404 #endif /* SQLITE_PAGER_PRAGMAS */
 97406 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97407 /*
 97408 ** Invalidate temp storage, either when the temp storage is changed
 97409 ** from default, or when 'file' and the temp_store_directory has changed
 97410 */
 97411 static int invalidateTempStorage(Parse *pParse){
 97412   sqlite3 *db = pParse->db;
 97413   if( db->aDb[1].pBt!=0 ){
 97414     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
 97415       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
 97416         "from within a transaction");
 97417       return SQLITE_ERROR;
 97419     sqlite3BtreeClose(db->aDb[1].pBt);
 97420     db->aDb[1].pBt = 0;
 97421     sqlite3ResetAllSchemasOfConnection(db);
 97423   return SQLITE_OK;
 97425 #endif /* SQLITE_PAGER_PRAGMAS */
 97427 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97428 /*
 97429 ** If the TEMP database is open, close it and mark the database schema
 97430 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
 97431 ** or DEFAULT_TEMP_STORE pragmas.
 97432 */
 97433 static int changeTempStorage(Parse *pParse, const char *zStorageType){
 97434   int ts = getTempStore(zStorageType);
 97435   sqlite3 *db = pParse->db;
 97436   if( db->temp_store==ts ) return SQLITE_OK;
 97437   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
 97438     return SQLITE_ERROR;
 97440   db->temp_store = (u8)ts;
 97441   return SQLITE_OK;
 97443 #endif /* SQLITE_PAGER_PRAGMAS */
 97445 /*
 97446 ** Generate code to return a single integer value.
 97447 */
 97448 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
 97449   Vdbe *v = sqlite3GetVdbe(pParse);
 97450   int mem = ++pParse->nMem;
 97451   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
 97452   if( pI64 ){
 97453     memcpy(pI64, &value, sizeof(value));
 97455   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
 97456   sqlite3VdbeSetNumCols(v, 1);
 97457   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
 97458   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 97462 /*
 97463 ** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
 97464 ** set these values for all pagers.
 97465 */
 97466 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97467 static void setAllPagerFlags(sqlite3 *db){
 97468   if( db->autoCommit ){
 97469     Db *pDb = db->aDb;
 97470     int n = db->nDb;
 97471     assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
 97472     assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
 97473     assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
 97474     assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
 97475              ==  PAGER_FLAGS_MASK );
 97476     assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
 97477     while( (n--) > 0 ){
 97478       if( pDb->pBt ){
 97479         sqlite3BtreeSetPagerFlags(pDb->pBt,
 97480                  pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
 97482       pDb++;
 97486 #else
 97487 # define setAllPagerFlags(X)  /* no-op */
 97488 #endif
 97491 /*
 97492 ** Return a human-readable name for a constraint resolution action.
 97493 */
 97494 #ifndef SQLITE_OMIT_FOREIGN_KEY
 97495 static const char *actionName(u8 action){
 97496   const char *zName;
 97497   switch( action ){
 97498     case OE_SetNull:  zName = "SET NULL";        break;
 97499     case OE_SetDflt:  zName = "SET DEFAULT";     break;
 97500     case OE_Cascade:  zName = "CASCADE";         break;
 97501     case OE_Restrict: zName = "RESTRICT";        break;
 97502     default:          zName = "NO ACTION";  
 97503                       assert( action==OE_None ); break;
 97505   return zName;
 97507 #endif
 97510 /*
 97511 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
 97512 ** defined in pager.h. This function returns the associated lowercase
 97513 ** journal-mode name.
 97514 */
 97515 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
 97516   static char * const azModeName[] = {
 97517     "delete", "persist", "off", "truncate", "memory"
 97518 #ifndef SQLITE_OMIT_WAL
 97519      , "wal"
 97520 #endif
 97521   };
 97522   assert( PAGER_JOURNALMODE_DELETE==0 );
 97523   assert( PAGER_JOURNALMODE_PERSIST==1 );
 97524   assert( PAGER_JOURNALMODE_OFF==2 );
 97525   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
 97526   assert( PAGER_JOURNALMODE_MEMORY==4 );
 97527   assert( PAGER_JOURNALMODE_WAL==5 );
 97528   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
 97530   if( eMode==ArraySize(azModeName) ) return 0;
 97531   return azModeName[eMode];
 97534 /*
 97535 ** Process a pragma statement.  
 97536 **
 97537 ** Pragmas are of this form:
 97538 **
 97539 **      PRAGMA [database.]id [= value]
 97540 **
 97541 ** The identifier might also be a string.  The value is a string, and
 97542 ** identifier, or a number.  If minusFlag is true, then the value is
 97543 ** a number that was preceded by a minus sign.
 97544 **
 97545 ** If the left side is "database.id" then pId1 is the database name
 97546 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
 97547 ** id and pId2 is any empty string.
 97548 */
 97549 SQLITE_PRIVATE void sqlite3Pragma(
 97550   Parse *pParse, 
 97551   Token *pId1,        /* First part of [database.]id field */
 97552   Token *pId2,        /* Second part of [database.]id field, or NULL */
 97553   Token *pValue,      /* Token for <value>, or NULL */
 97554   int minusFlag       /* True if a '-' sign preceded <value> */
 97555 ){
 97556   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
 97557   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
 97558   const char *zDb = 0;   /* The database name */
 97559   Token *pId;            /* Pointer to <id> token */
 97560   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
 97561   int iDb;               /* Database index for <database> */
 97562   int lwr, upr, mid;           /* Binary search bounds */
 97563   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
 97564   sqlite3 *db = pParse->db;    /* The database connection */
 97565   Db *pDb;                     /* The specific database being pragmaed */
 97566   Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
 97568   if( v==0 ) return;
 97569   sqlite3VdbeRunOnlyOnce(v);
 97570   pParse->nMem = 2;
 97572   /* Interpret the [database.] part of the pragma statement. iDb is the
 97573   ** index of the database this pragma is being applied to in db.aDb[]. */
 97574   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
 97575   if( iDb<0 ) return;
 97576   pDb = &db->aDb[iDb];
 97578   /* If the temp database has been explicitly named as part of the 
 97579   ** pragma, make sure it is open. 
 97580   */
 97581   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
 97582     return;
 97585   zLeft = sqlite3NameFromToken(db, pId);
 97586   if( !zLeft ) return;
 97587   if( minusFlag ){
 97588     zRight = sqlite3MPrintf(db, "-%T", pValue);
 97589   }else{
 97590     zRight = sqlite3NameFromToken(db, pValue);
 97593   assert( pId2 );
 97594   zDb = pId2->n>0 ? pDb->zName : 0;
 97595   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
 97596     goto pragma_out;
 97599   /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
 97600   ** connection.  If it returns SQLITE_OK, then assume that the VFS
 97601   ** handled the pragma and generate a no-op prepared statement.
 97602   */
 97603   aFcntl[0] = 0;
 97604   aFcntl[1] = zLeft;
 97605   aFcntl[2] = zRight;
 97606   aFcntl[3] = 0;
 97607   db->busyHandler.nBusy = 0;
 97608   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
 97609   if( rc==SQLITE_OK ){
 97610     if( aFcntl[0] ){
 97611       int mem = ++pParse->nMem;
 97612       sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
 97613       sqlite3VdbeSetNumCols(v, 1);
 97614       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
 97615       sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
 97616       sqlite3_free(aFcntl[0]);
 97618     goto pragma_out;
 97620   if( rc!=SQLITE_NOTFOUND ){
 97621     if( aFcntl[0] ){
 97622       sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
 97623       sqlite3_free(aFcntl[0]);
 97625     pParse->nErr++;
 97626     pParse->rc = rc;
 97627     goto pragma_out;
 97630   /* Locate the pragma in the lookup table */
 97631   lwr = 0;
 97632   upr = ArraySize(aPragmaNames)-1;
 97633   while( lwr<=upr ){
 97634     mid = (lwr+upr)/2;
 97635     rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
 97636     if( rc==0 ) break;
 97637     if( rc<0 ){
 97638       upr = mid - 1;
 97639     }else{
 97640       lwr = mid + 1;
 97643   if( lwr>upr ) goto pragma_out;
 97645   /* Make sure the database schema is loaded if the pragma requires that */
 97646   if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
 97647     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 97650   /* Jump to the appropriate pragma handler */
 97651   switch( aPragmaNames[mid].ePragTyp ){
 97653 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
 97654   /*
 97655   **  PRAGMA [database.]default_cache_size
 97656   **  PRAGMA [database.]default_cache_size=N
 97657   **
 97658   ** The first form reports the current persistent setting for the
 97659   ** page cache size.  The value returned is the maximum number of
 97660   ** pages in the page cache.  The second form sets both the current
 97661   ** page cache size value and the persistent page cache size value
 97662   ** stored in the database file.
 97663   **
 97664   ** Older versions of SQLite would set the default cache size to a
 97665   ** negative number to indicate synchronous=OFF.  These days, synchronous
 97666   ** is always on by default regardless of the sign of the default cache
 97667   ** size.  But continue to take the absolute value of the default cache
 97668   ** size of historical compatibility.
 97669   */
 97670   case PragTyp_DEFAULT_CACHE_SIZE: {
 97671     static const int iLn = VDBE_OFFSET_LINENO(2);
 97672     static const VdbeOpList getCacheSize[] = {
 97673       { OP_Transaction, 0, 0,        0},                         /* 0 */
 97674       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
 97675       { OP_IfPos,       1, 8,        0},
 97676       { OP_Integer,     0, 2,        0},
 97677       { OP_Subtract,    1, 2,        1},
 97678       { OP_IfPos,       1, 8,        0},
 97679       { OP_Integer,     0, 1,        0},                         /* 6 */
 97680       { OP_Noop,        0, 0,        0},
 97681       { OP_ResultRow,   1, 1,        0},
 97682     };
 97683     int addr;
 97684     sqlite3VdbeUsesBtree(v, iDb);
 97685     if( !zRight ){
 97686       sqlite3VdbeSetNumCols(v, 1);
 97687       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
 97688       pParse->nMem += 2;
 97689       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
 97690       sqlite3VdbeChangeP1(v, addr, iDb);
 97691       sqlite3VdbeChangeP1(v, addr+1, iDb);
 97692       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
 97693     }else{
 97694       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 97695       sqlite3BeginWriteOperation(pParse, 0, iDb);
 97696       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
 97697       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
 97698       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 97699       pDb->pSchema->cache_size = size;
 97700       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 97702     break;
 97704 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
 97706 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
 97707   /*
 97708   **  PRAGMA [database.]page_size
 97709   **  PRAGMA [database.]page_size=N
 97710   **
 97711   ** The first form reports the current setting for the
 97712   ** database page size in bytes.  The second form sets the
 97713   ** database page size value.  The value can only be set if
 97714   ** the database has not yet been created.
 97715   */
 97716   case PragTyp_PAGE_SIZE: {
 97717     Btree *pBt = pDb->pBt;
 97718     assert( pBt!=0 );
 97719     if( !zRight ){
 97720       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
 97721       returnSingleInt(pParse, "page_size", size);
 97722     }else{
 97723       /* Malloc may fail when setting the page-size, as there is an internal
 97724       ** buffer that the pager module resizes using sqlite3_realloc().
 97725       */
 97726       db->nextPagesize = sqlite3Atoi(zRight);
 97727       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
 97728         db->mallocFailed = 1;
 97731     break;
 97734   /*
 97735   **  PRAGMA [database.]secure_delete
 97736   **  PRAGMA [database.]secure_delete=ON/OFF
 97737   **
 97738   ** The first form reports the current setting for the
 97739   ** secure_delete flag.  The second form changes the secure_delete
 97740   ** flag setting and reports thenew value.
 97741   */
 97742   case PragTyp_SECURE_DELETE: {
 97743     Btree *pBt = pDb->pBt;
 97744     int b = -1;
 97745     assert( pBt!=0 );
 97746     if( zRight ){
 97747       b = sqlite3GetBoolean(zRight, 0);
 97749     if( pId2->n==0 && b>=0 ){
 97750       int ii;
 97751       for(ii=0; ii<db->nDb; ii++){
 97752         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
 97755     b = sqlite3BtreeSecureDelete(pBt, b);
 97756     returnSingleInt(pParse, "secure_delete", b);
 97757     break;
 97760   /*
 97761   **  PRAGMA [database.]max_page_count
 97762   **  PRAGMA [database.]max_page_count=N
 97763   **
 97764   ** The first form reports the current setting for the
 97765   ** maximum number of pages in the database file.  The 
 97766   ** second form attempts to change this setting.  Both
 97767   ** forms return the current setting.
 97768   **
 97769   ** The absolute value of N is used.  This is undocumented and might
 97770   ** change.  The only purpose is to provide an easy way to test
 97771   ** the sqlite3AbsInt32() function.
 97772   **
 97773   **  PRAGMA [database.]page_count
 97774   **
 97775   ** Return the number of pages in the specified database.
 97776   */
 97777   case PragTyp_PAGE_COUNT: {
 97778     int iReg;
 97779     sqlite3CodeVerifySchema(pParse, iDb);
 97780     iReg = ++pParse->nMem;
 97781     if( sqlite3Tolower(zLeft[0])=='p' ){
 97782       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
 97783     }else{
 97784       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, 
 97785                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
 97787     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
 97788     sqlite3VdbeSetNumCols(v, 1);
 97789     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 97790     break;
 97793   /*
 97794   **  PRAGMA [database.]locking_mode
 97795   **  PRAGMA [database.]locking_mode = (normal|exclusive)
 97796   */
 97797   case PragTyp_LOCKING_MODE: {
 97798     const char *zRet = "normal";
 97799     int eMode = getLockingMode(zRight);
 97801     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
 97802       /* Simple "PRAGMA locking_mode;" statement. This is a query for
 97803       ** the current default locking mode (which may be different to
 97804       ** the locking-mode of the main database).
 97805       */
 97806       eMode = db->dfltLockMode;
 97807     }else{
 97808       Pager *pPager;
 97809       if( pId2->n==0 ){
 97810         /* This indicates that no database name was specified as part
 97811         ** of the PRAGMA command. In this case the locking-mode must be
 97812         ** set on all attached databases, as well as the main db file.
 97813         **
 97814         ** Also, the sqlite3.dfltLockMode variable is set so that
 97815         ** any subsequently attached databases also use the specified
 97816         ** locking mode.
 97817         */
 97818         int ii;
 97819         assert(pDb==&db->aDb[0]);
 97820         for(ii=2; ii<db->nDb; ii++){
 97821           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
 97822           sqlite3PagerLockingMode(pPager, eMode);
 97824         db->dfltLockMode = (u8)eMode;
 97826       pPager = sqlite3BtreePager(pDb->pBt);
 97827       eMode = sqlite3PagerLockingMode(pPager, eMode);
 97830     assert( eMode==PAGER_LOCKINGMODE_NORMAL
 97831             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
 97832     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
 97833       zRet = "exclusive";
 97835     sqlite3VdbeSetNumCols(v, 1);
 97836     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
 97837     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
 97838     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 97839     break;
 97842   /*
 97843   **  PRAGMA [database.]journal_mode
 97844   **  PRAGMA [database.]journal_mode =
 97845   **                      (delete|persist|off|truncate|memory|wal|off)
 97846   */
 97847   case PragTyp_JOURNAL_MODE: {
 97848     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
 97849     int ii;           /* Loop counter */
 97851     sqlite3VdbeSetNumCols(v, 1);
 97852     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
 97854     if( zRight==0 ){
 97855       /* If there is no "=MODE" part of the pragma, do a query for the
 97856       ** current mode */
 97857       eMode = PAGER_JOURNALMODE_QUERY;
 97858     }else{
 97859       const char *zMode;
 97860       int n = sqlite3Strlen30(zRight);
 97861       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
 97862         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
 97864       if( !zMode ){
 97865         /* If the "=MODE" part does not match any known journal mode,
 97866         ** then do a query */
 97867         eMode = PAGER_JOURNALMODE_QUERY;
 97870     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
 97871       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
 97872       iDb = 0;
 97873       pId2->n = 1;
 97875     for(ii=db->nDb-1; ii>=0; ii--){
 97876       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
 97877         sqlite3VdbeUsesBtree(v, ii);
 97878         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
 97881     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 97882     break;
 97885   /*
 97886   **  PRAGMA [database.]journal_size_limit
 97887   **  PRAGMA [database.]journal_size_limit=N
 97888   **
 97889   ** Get or set the size limit on rollback journal files.
 97890   */
 97891   case PragTyp_JOURNAL_SIZE_LIMIT: {
 97892     Pager *pPager = sqlite3BtreePager(pDb->pBt);
 97893     i64 iLimit = -2;
 97894     if( zRight ){
 97895       sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
 97896       if( iLimit<-1 ) iLimit = -1;
 97898     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
 97899     returnSingleInt(pParse, "journal_size_limit", iLimit);
 97900     break;
 97903 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 97905   /*
 97906   **  PRAGMA [database.]auto_vacuum
 97907   **  PRAGMA [database.]auto_vacuum=N
 97908   **
 97909   ** Get or set the value of the database 'auto-vacuum' parameter.
 97910   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
 97911   */
 97912 #ifndef SQLITE_OMIT_AUTOVACUUM
 97913   case PragTyp_AUTO_VACUUM: {
 97914     Btree *pBt = pDb->pBt;
 97915     assert( pBt!=0 );
 97916     if( !zRight ){
 97917       returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
 97918     }else{
 97919       int eAuto = getAutoVacuum(zRight);
 97920       assert( eAuto>=0 && eAuto<=2 );
 97921       db->nextAutovac = (u8)eAuto;
 97922       /* Call SetAutoVacuum() to set initialize the internal auto and
 97923       ** incr-vacuum flags. This is required in case this connection
 97924       ** creates the database file. It is important that it is created
 97925       ** as an auto-vacuum capable db.
 97926       */
 97927       rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
 97928       if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
 97929         /* When setting the auto_vacuum mode to either "full" or 
 97930         ** "incremental", write the value of meta[6] in the database
 97931         ** file. Before writing to meta[6], check that meta[3] indicates
 97932         ** that this really is an auto-vacuum capable database.
 97933         */
 97934         static const int iLn = VDBE_OFFSET_LINENO(2);
 97935         static const VdbeOpList setMeta6[] = {
 97936           { OP_Transaction,    0,         1,                 0},    /* 0 */
 97937           { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
 97938           { OP_If,             1,         0,                 0},    /* 2 */
 97939           { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
 97940           { OP_Integer,        0,         1,                 0},    /* 4 */
 97941           { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
 97942         };
 97943         int iAddr;
 97944         iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
 97945         sqlite3VdbeChangeP1(v, iAddr, iDb);
 97946         sqlite3VdbeChangeP1(v, iAddr+1, iDb);
 97947         sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
 97948         sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
 97949         sqlite3VdbeChangeP1(v, iAddr+5, iDb);
 97950         sqlite3VdbeUsesBtree(v, iDb);
 97953     break;
 97955 #endif
 97957   /*
 97958   **  PRAGMA [database.]incremental_vacuum(N)
 97959   **
 97960   ** Do N steps of incremental vacuuming on a database.
 97961   */
 97962 #ifndef SQLITE_OMIT_AUTOVACUUM
 97963   case PragTyp_INCREMENTAL_VACUUM: {
 97964     int iLimit, addr;
 97965     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
 97966       iLimit = 0x7fffffff;
 97968     sqlite3BeginWriteOperation(pParse, 0, iDb);
 97969     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
 97970     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
 97971     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
 97972     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 97973     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
 97974     sqlite3VdbeJumpHere(v, addr);
 97975     break;
 97977 #endif
 97979 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 97980   /*
 97981   **  PRAGMA [database.]cache_size
 97982   **  PRAGMA [database.]cache_size=N
 97983   **
 97984   ** The first form reports the current local setting for the
 97985   ** page cache size. The second form sets the local
 97986   ** page cache size value.  If N is positive then that is the
 97987   ** number of pages in the cache.  If N is negative, then the
 97988   ** number of pages is adjusted so that the cache uses -N kibibytes
 97989   ** of memory.
 97990   */
 97991   case PragTyp_CACHE_SIZE: {
 97992     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 97993     if( !zRight ){
 97994       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
 97995     }else{
 97996       int size = sqlite3Atoi(zRight);
 97997       pDb->pSchema->cache_size = size;
 97998       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 98000     break;
 98003   /*
 98004   **  PRAGMA [database.]mmap_size(N)
 98005   **
 98006   ** Used to set mapping size limit. The mapping size limit is
 98007   ** used to limit the aggregate size of all memory mapped regions of the
 98008   ** database file. If this parameter is set to zero, then memory mapping
 98009   ** is not used at all.  If N is negative, then the default memory map
 98010   ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
 98011   ** The parameter N is measured in bytes.
 98012   **
 98013   ** This value is advisory.  The underlying VFS is free to memory map
 98014   ** as little or as much as it wants.  Except, if N is set to 0 then the
 98015   ** upper layers will never invoke the xFetch interfaces to the VFS.
 98016   */
 98017   case PragTyp_MMAP_SIZE: {
 98018     sqlite3_int64 sz;
 98019 #if SQLITE_MAX_MMAP_SIZE>0
 98020     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 98021     if( zRight ){
 98022       int ii;
 98023       sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
 98024       if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
 98025       if( pId2->n==0 ) db->szMmap = sz;
 98026       for(ii=db->nDb-1; ii>=0; ii--){
 98027         if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
 98028           sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
 98032     sz = -1;
 98033     rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
 98034 #else
 98035     sz = 0;
 98036     rc = SQLITE_OK;
 98037 #endif
 98038     if( rc==SQLITE_OK ){
 98039       returnSingleInt(pParse, "mmap_size", sz);
 98040     }else if( rc!=SQLITE_NOTFOUND ){
 98041       pParse->nErr++;
 98042       pParse->rc = rc;
 98044     break;
 98047   /*
 98048   **   PRAGMA temp_store
 98049   **   PRAGMA temp_store = "default"|"memory"|"file"
 98050   **
 98051   ** Return or set the local value of the temp_store flag.  Changing
 98052   ** the local value does not make changes to the disk file and the default
 98053   ** value will be restored the next time the database is opened.
 98054   **
 98055   ** Note that it is possible for the library compile-time options to
 98056   ** override this setting
 98057   */
 98058   case PragTyp_TEMP_STORE: {
 98059     if( !zRight ){
 98060       returnSingleInt(pParse, "temp_store", db->temp_store);
 98061     }else{
 98062       changeTempStorage(pParse, zRight);
 98064     break;
 98067   /*
 98068   **   PRAGMA temp_store_directory
 98069   **   PRAGMA temp_store_directory = ""|"directory_name"
 98070   **
 98071   ** Return or set the local value of the temp_store_directory flag.  Changing
 98072   ** the value sets a specific directory to be used for temporary files.
 98073   ** Setting to a null string reverts to the default temporary directory search.
 98074   ** If temporary directory is changed, then invalidateTempStorage.
 98075   **
 98076   */
 98077   case PragTyp_TEMP_STORE_DIRECTORY: {
 98078     if( !zRight ){
 98079       if( sqlite3_temp_directory ){
 98080         sqlite3VdbeSetNumCols(v, 1);
 98081         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 98082             "temp_store_directory", SQLITE_STATIC);
 98083         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
 98084         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 98086     }else{
 98087 #ifndef SQLITE_OMIT_WSD
 98088       if( zRight[0] ){
 98089         int res;
 98090         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 98091         if( rc!=SQLITE_OK || res==0 ){
 98092           sqlite3ErrorMsg(pParse, "not a writable directory");
 98093           goto pragma_out;
 98096       if( SQLITE_TEMP_STORE==0
 98097        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
 98098        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
 98099       ){
 98100         invalidateTempStorage(pParse);
 98102       sqlite3_free(sqlite3_temp_directory);
 98103       if( zRight[0] ){
 98104         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
 98105       }else{
 98106         sqlite3_temp_directory = 0;
 98108 #endif /* SQLITE_OMIT_WSD */
 98110     break;
 98113 #if SQLITE_OS_WIN
 98114   /*
 98115   **   PRAGMA data_store_directory
 98116   **   PRAGMA data_store_directory = ""|"directory_name"
 98117   **
 98118   ** Return or set the local value of the data_store_directory flag.  Changing
 98119   ** the value sets a specific directory to be used for database files that
 98120   ** were specified with a relative pathname.  Setting to a null string reverts
 98121   ** to the default database directory, which for database files specified with
 98122   ** a relative path will probably be based on the current directory for the
 98123   ** process.  Database file specified with an absolute path are not impacted
 98124   ** by this setting, regardless of its value.
 98125   **
 98126   */
 98127   case PragTyp_DATA_STORE_DIRECTORY: {
 98128     if( !zRight ){
 98129       if( sqlite3_data_directory ){
 98130         sqlite3VdbeSetNumCols(v, 1);
 98131         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 98132             "data_store_directory", SQLITE_STATIC);
 98133         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
 98134         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 98136     }else{
 98137 #ifndef SQLITE_OMIT_WSD
 98138       if( zRight[0] ){
 98139         int res;
 98140         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
 98141         if( rc!=SQLITE_OK || res==0 ){
 98142           sqlite3ErrorMsg(pParse, "not a writable directory");
 98143           goto pragma_out;
 98146       sqlite3_free(sqlite3_data_directory);
 98147       if( zRight[0] ){
 98148         sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
 98149       }else{
 98150         sqlite3_data_directory = 0;
 98152 #endif /* SQLITE_OMIT_WSD */
 98154     break;
 98156 #endif
 98158 #if SQLITE_ENABLE_LOCKING_STYLE
 98159   /*
 98160   **   PRAGMA [database.]lock_proxy_file
 98161   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
 98162   **
 98163   ** Return or set the value of the lock_proxy_file flag.  Changing
 98164   ** the value sets a specific file to be used for database access locks.
 98165   **
 98166   */
 98167   case PragTyp_LOCK_PROXY_FILE: {
 98168     if( !zRight ){
 98169       Pager *pPager = sqlite3BtreePager(pDb->pBt);
 98170       char *proxy_file_path = NULL;
 98171       sqlite3_file *pFile = sqlite3PagerFile(pPager);
 98172       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
 98173                            &proxy_file_path);
 98175       if( proxy_file_path ){
 98176         sqlite3VdbeSetNumCols(v, 1);
 98177         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
 98178                               "lock_proxy_file", SQLITE_STATIC);
 98179         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
 98180         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 98182     }else{
 98183       Pager *pPager = sqlite3BtreePager(pDb->pBt);
 98184       sqlite3_file *pFile = sqlite3PagerFile(pPager);
 98185       int res;
 98186       if( zRight[0] ){
 98187         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 98188                                      zRight);
 98189       } else {
 98190         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
 98191                                      NULL);
 98193       if( res!=SQLITE_OK ){
 98194         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
 98195         goto pragma_out;
 98198     break;
 98200 #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
 98202   /*
 98203   **   PRAGMA [database.]synchronous
 98204   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
 98205   **
 98206   ** Return or set the local value of the synchronous flag.  Changing
 98207   ** the local value does not make changes to the disk file and the
 98208   ** default value will be restored the next time the database is
 98209   ** opened.
 98210   */
 98211   case PragTyp_SYNCHRONOUS: {
 98212     if( !zRight ){
 98213       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
 98214     }else{
 98215       if( !db->autoCommit ){
 98216         sqlite3ErrorMsg(pParse, 
 98217             "Safety level may not be changed inside a transaction");
 98218       }else{
 98219         pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
 98220         setAllPagerFlags(db);
 98223     break;
 98225 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 98227 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
 98228   case PragTyp_FLAG: {
 98229     if( zRight==0 ){
 98230       returnSingleInt(pParse, aPragmaNames[mid].zName,
 98231                      (db->flags & aPragmaNames[mid].iArg)!=0 );
 98232     }else{
 98233       int mask = aPragmaNames[mid].iArg;    /* Mask of bits to set or clear. */
 98234       if( db->autoCommit==0 ){
 98235         /* Foreign key support may not be enabled or disabled while not
 98236         ** in auto-commit mode.  */
 98237         mask &= ~(SQLITE_ForeignKeys);
 98240       if( sqlite3GetBoolean(zRight, 0) ){
 98241         db->flags |= mask;
 98242       }else{
 98243         db->flags &= ~mask;
 98244         if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
 98247       /* Many of the flag-pragmas modify the code generated by the SQL 
 98248       ** compiler (eg. count_changes). So add an opcode to expire all
 98249       ** compiled SQL statements after modifying a pragma value.
 98250       */
 98251       sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
 98252       setAllPagerFlags(db);
 98254     break;
 98256 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
 98258 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
 98259   /*
 98260   **   PRAGMA table_info(<table>)
 98261   **
 98262   ** Return a single row for each column of the named table. The columns of
 98263   ** the returned data set are:
 98264   **
 98265   ** cid:        Column id (numbered from left to right, starting at 0)
 98266   ** name:       Column name
 98267   ** type:       Column declaration type.
 98268   ** notnull:    True if 'NOT NULL' is part of column declaration
 98269   ** dflt_value: The default value for the column, if any.
 98270   */
 98271   case PragTyp_TABLE_INFO: if( zRight ){
 98272     Table *pTab;
 98273     pTab = sqlite3FindTable(db, zRight, zDb);
 98274     if( pTab ){
 98275       int i, k;
 98276       int nHidden = 0;
 98277       Column *pCol;
 98278       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
 98279       sqlite3VdbeSetNumCols(v, 6);
 98280       pParse->nMem = 6;
 98281       sqlite3CodeVerifySchema(pParse, iDb);
 98282       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
 98283       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 98284       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
 98285       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
 98286       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
 98287       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
 98288       sqlite3ViewGetColumnNames(pParse, pTab);
 98289       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
 98290         if( IsHiddenColumn(pCol) ){
 98291           nHidden++;
 98292           continue;
 98294         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
 98295         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
 98296         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 98297            pCol->zType ? pCol->zType : "", 0);
 98298         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
 98299         if( pCol->zDflt ){
 98300           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
 98301         }else{
 98302           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
 98304         if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
 98305           k = 0;
 98306         }else if( pPk==0 ){
 98307           k = 1;
 98308         }else{
 98309           for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
 98311         sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
 98312         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
 98316   break;
 98318   case PragTyp_STATS: {
 98319     Index *pIdx;
 98320     HashElem *i;
 98321     v = sqlite3GetVdbe(pParse);
 98322     sqlite3VdbeSetNumCols(v, 4);
 98323     pParse->nMem = 4;
 98324     sqlite3CodeVerifySchema(pParse, iDb);
 98325     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
 98326     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
 98327     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
 98328     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
 98329     for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
 98330       Table *pTab = sqliteHashData(i);
 98331       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
 98332       sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
 98333       sqlite3VdbeAddOp2(v, OP_Integer,
 98334                            (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
 98335       sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
 98336       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
 98337       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 98338         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
 98339         sqlite3VdbeAddOp2(v, OP_Integer,
 98340                              (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
 98341         sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
 98342         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
 98346   break;
 98348   case PragTyp_INDEX_INFO: if( zRight ){
 98349     Index *pIdx;
 98350     Table *pTab;
 98351     pIdx = sqlite3FindIndex(db, zRight, zDb);
 98352     if( pIdx ){
 98353       int i;
 98354       pTab = pIdx->pTable;
 98355       sqlite3VdbeSetNumCols(v, 3);
 98356       pParse->nMem = 3;
 98357       sqlite3CodeVerifySchema(pParse, iDb);
 98358       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
 98359       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
 98360       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
 98361       for(i=0; i<pIdx->nKeyCol; i++){
 98362         i16 cnum = pIdx->aiColumn[i];
 98363         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 98364         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
 98365         assert( pTab->nCol>cnum );
 98366         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
 98367         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 98371   break;
 98373   case PragTyp_INDEX_LIST: if( zRight ){
 98374     Index *pIdx;
 98375     Table *pTab;
 98376     int i;
 98377     pTab = sqlite3FindTable(db, zRight, zDb);
 98378     if( pTab ){
 98379       v = sqlite3GetVdbe(pParse);
 98380       sqlite3VdbeSetNumCols(v, 3);
 98381       pParse->nMem = 3;
 98382       sqlite3CodeVerifySchema(pParse, iDb);
 98383       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 98384       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 98385       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
 98386       for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
 98387         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 98388         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
 98389         sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
 98390         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 98394   break;
 98396   case PragTyp_DATABASE_LIST: {
 98397     int i;
 98398     sqlite3VdbeSetNumCols(v, 3);
 98399     pParse->nMem = 3;
 98400     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 98401     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 98402     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
 98403     for(i=0; i<db->nDb; i++){
 98404       if( db->aDb[i].pBt==0 ) continue;
 98405       assert( db->aDb[i].zName!=0 );
 98406       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 98407       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
 98408       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 98409            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
 98410       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 98413   break;
 98415   case PragTyp_COLLATION_LIST: {
 98416     int i = 0;
 98417     HashElem *p;
 98418     sqlite3VdbeSetNumCols(v, 2);
 98419     pParse->nMem = 2;
 98420     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
 98421     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
 98422     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
 98423       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
 98424       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
 98425       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
 98426       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 98429   break;
 98430 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
 98432 #ifndef SQLITE_OMIT_FOREIGN_KEY
 98433   case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
 98434     FKey *pFK;
 98435     Table *pTab;
 98436     pTab = sqlite3FindTable(db, zRight, zDb);
 98437     if( pTab ){
 98438       v = sqlite3GetVdbe(pParse);
 98439       pFK = pTab->pFKey;
 98440       if( pFK ){
 98441         int i = 0; 
 98442         sqlite3VdbeSetNumCols(v, 8);
 98443         pParse->nMem = 8;
 98444         sqlite3CodeVerifySchema(pParse, iDb);
 98445         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
 98446         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
 98447         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
 98448         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
 98449         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
 98450         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
 98451         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
 98452         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
 98453         while(pFK){
 98454           int j;
 98455           for(j=0; j<pFK->nCol; j++){
 98456             char *zCol = pFK->aCol[j].zCol;
 98457             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
 98458             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
 98459             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
 98460             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
 98461             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
 98462             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
 98463                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
 98464             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
 98465             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
 98466             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
 98467             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
 98468             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
 98470           ++i;
 98471           pFK = pFK->pNextFrom;
 98476   break;
 98477 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 98479 #ifndef SQLITE_OMIT_FOREIGN_KEY
 98480 #ifndef SQLITE_OMIT_TRIGGER
 98481   case PragTyp_FOREIGN_KEY_CHECK: {
 98482     FKey *pFK;             /* A foreign key constraint */
 98483     Table *pTab;           /* Child table contain "REFERENCES" keyword */
 98484     Table *pParent;        /* Parent table that child points to */
 98485     Index *pIdx;           /* Index in the parent table */
 98486     int i;                 /* Loop counter:  Foreign key number for pTab */
 98487     int j;                 /* Loop counter:  Field of the foreign key */
 98488     HashElem *k;           /* Loop counter:  Next table in schema */
 98489     int x;                 /* result variable */
 98490     int regResult;         /* 3 registers to hold a result row */
 98491     int regKey;            /* Register to hold key for checking the FK */
 98492     int regRow;            /* Registers to hold a row from pTab */
 98493     int addrTop;           /* Top of a loop checking foreign keys */
 98494     int addrOk;            /* Jump here if the key is OK */
 98495     int *aiCols;           /* child to parent column mapping */
 98497     regResult = pParse->nMem+1;
 98498     pParse->nMem += 4;
 98499     regKey = ++pParse->nMem;
 98500     regRow = ++pParse->nMem;
 98501     v = sqlite3GetVdbe(pParse);
 98502     sqlite3VdbeSetNumCols(v, 4);
 98503     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
 98504     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
 98505     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
 98506     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
 98507     sqlite3CodeVerifySchema(pParse, iDb);
 98508     k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
 98509     while( k ){
 98510       if( zRight ){
 98511         pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
 98512         k = 0;
 98513       }else{
 98514         pTab = (Table*)sqliteHashData(k);
 98515         k = sqliteHashNext(k);
 98517       if( pTab==0 || pTab->pFKey==0 ) continue;
 98518       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 98519       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
 98520       sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
 98521       sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
 98522                         P4_TRANSIENT);
 98523       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
 98524         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
 98525         if( pParent==0 ) continue;
 98526         pIdx = 0;
 98527         sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
 98528         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
 98529         if( x==0 ){
 98530           if( pIdx==0 ){
 98531             sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
 98532           }else{
 98533             sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
 98534             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 98536         }else{
 98537           k = 0;
 98538           break;
 98541       assert( pParse->nErr>0 || pFK==0 );
 98542       if( pFK ) break;
 98543       if( pParse->nTab<i ) pParse->nTab = i;
 98544       addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
 98545       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
 98546         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
 98547         pIdx = 0;
 98548         aiCols = 0;
 98549         if( pParent ){
 98550           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
 98551           assert( x==0 );
 98553         addrOk = sqlite3VdbeMakeLabel(v);
 98554         if( pParent && pIdx==0 ){
 98555           int iKey = pFK->aCol[0].iFrom;
 98556           assert( iKey>=0 && iKey<pTab->nCol );
 98557           if( iKey!=pTab->iPKey ){
 98558             sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
 98559             sqlite3ColumnDefault(v, pTab, iKey, regRow);
 98560             sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
 98561             sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 
 98562                sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
 98563           }else{
 98564             sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
 98566           sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
 98567           sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
 98568           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
 98569         }else{
 98570           for(j=0; j<pFK->nCol; j++){
 98571             sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
 98572                             aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
 98573             sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
 98575           if( pParent ){
 98576             sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
 98577                               sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
 98578             sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
 98579             VdbeCoverage(v);
 98582         sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
 98583         sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0, 
 98584                           pFK->zTo, P4_TRANSIENT);
 98585         sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
 98586         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
 98587         sqlite3VdbeResolveLabel(v, addrOk);
 98588         sqlite3DbFree(db, aiCols);
 98590       sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
 98591       sqlite3VdbeJumpHere(v, addrTop);
 98594   break;
 98595 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 98596 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
 98598 #ifndef NDEBUG
 98599   case PragTyp_PARSER_TRACE: {
 98600     if( zRight ){
 98601       if( sqlite3GetBoolean(zRight, 0) ){
 98602         sqlite3ParserTrace(stderr, "parser: ");
 98603       }else{
 98604         sqlite3ParserTrace(0, 0);
 98608   break;
 98609 #endif
 98611   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
 98612   ** used will be case sensitive or not depending on the RHS.
 98613   */
 98614   case PragTyp_CASE_SENSITIVE_LIKE: {
 98615     if( zRight ){
 98616       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
 98619   break;
 98621 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
 98622 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
 98623 #endif
 98625 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
 98626   /* Pragma "quick_check" is reduced version of 
 98627   ** integrity_check designed to detect most database corruption
 98628   ** without most of the overhead of a full integrity-check.
 98629   */
 98630   case PragTyp_INTEGRITY_CHECK: {
 98631     int i, j, addr, mxErr;
 98633     /* Code that appears at the end of the integrity check.  If no error
 98634     ** messages have been generated, output OK.  Otherwise output the
 98635     ** error message
 98636     */
 98637     static const int iLn = VDBE_OFFSET_LINENO(2);
 98638     static const VdbeOpList endCode[] = {
 98639       { OP_AddImm,      1, 0,        0},    /* 0 */
 98640       { OP_IfNeg,       1, 0,        0},    /* 1 */
 98641       { OP_String8,     0, 3,        0},    /* 2 */
 98642       { OP_ResultRow,   3, 1,        0},
 98643     };
 98645     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
 98647     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
 98648     ** then iDb is set to the index of the database identified by <db>.
 98649     ** In this case, the integrity of database iDb only is verified by
 98650     ** the VDBE created below.
 98651     **
 98652     ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
 98653     ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
 98654     ** to -1 here, to indicate that the VDBE should verify the integrity
 98655     ** of all attached databases.  */
 98656     assert( iDb>=0 );
 98657     assert( iDb==0 || pId2->z );
 98658     if( pId2->z==0 ) iDb = -1;
 98660     /* Initialize the VDBE program */
 98661     pParse->nMem = 6;
 98662     sqlite3VdbeSetNumCols(v, 1);
 98663     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
 98665     /* Set the maximum error count */
 98666     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 98667     if( zRight ){
 98668       sqlite3GetInt32(zRight, &mxErr);
 98669       if( mxErr<=0 ){
 98670         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
 98673     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
 98675     /* Do an integrity check on each database file */
 98676     for(i=0; i<db->nDb; i++){
 98677       HashElem *x;
 98678       Hash *pTbls;
 98679       int cnt = 0;
 98681       if( OMIT_TEMPDB && i==1 ) continue;
 98682       if( iDb>=0 && i!=iDb ) continue;
 98684       sqlite3CodeVerifySchema(pParse, i);
 98685       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
 98686       VdbeCoverage(v);
 98687       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98688       sqlite3VdbeJumpHere(v, addr);
 98690       /* Do an integrity check of the B-Tree
 98691       **
 98692       ** Begin by filling registers 2, 3, ... with the root pages numbers
 98693       ** for all tables and indices in the database.
 98694       */
 98695       assert( sqlite3SchemaMutexHeld(db, i, 0) );
 98696       pTbls = &db->aDb[i].pSchema->tblHash;
 98697       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
 98698         Table *pTab = sqliteHashData(x);
 98699         Index *pIdx;
 98700         if( HasRowid(pTab) ){
 98701           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
 98702           VdbeComment((v, "%s", pTab->zName));
 98703           cnt++;
 98705         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 98706           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
 98707           VdbeComment((v, "%s", pIdx->zName));
 98708           cnt++;
 98712       /* Make sure sufficient number of registers have been allocated */
 98713       pParse->nMem = MAX( pParse->nMem, cnt+8 );
 98715       /* Do the b-tree integrity checks */
 98716       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
 98717       sqlite3VdbeChangeP5(v, (u8)i);
 98718       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
 98719       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
 98720          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
 98721          P4_DYNAMIC);
 98722       sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
 98723       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
 98724       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
 98725       sqlite3VdbeJumpHere(v, addr);
 98727       /* Make sure all the indices are constructed correctly.
 98728       */
 98729       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
 98730         Table *pTab = sqliteHashData(x);
 98731         Index *pIdx, *pPk;
 98732         Index *pPrior = 0;
 98733         int loopTop;
 98734         int iDataCur, iIdxCur;
 98735         int r1 = -1;
 98737         if( pTab->pIndex==0 ) continue;
 98738         pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
 98739         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
 98740         VdbeCoverage(v);
 98741         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98742         sqlite3VdbeJumpHere(v, addr);
 98743         sqlite3ExprCacheClear(pParse);
 98744         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
 98745                                    1, 0, &iDataCur, &iIdxCur);
 98746         sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
 98747         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98748           sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
 98750         pParse->nMem = MAX(pParse->nMem, 8+j);
 98751         sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
 98752         loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
 98753         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98754           int jmp2, jmp3, jmp4;
 98755           if( pPk==pIdx ) continue;
 98756           r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
 98757                                        pPrior, r1);
 98758           pPrior = pIdx;
 98759           sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
 98760           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
 98761                                       pIdx->nColumn); VdbeCoverage(v);
 98762           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
 98763           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
 98764           sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
 98765           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
 98766                             P4_STATIC);
 98767           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
 98768           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
 98769           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
 98770           sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
 98771           jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
 98772           sqlite3VdbeAddOp0(v, OP_Halt);
 98773           sqlite3VdbeJumpHere(v, jmp4);
 98774           sqlite3VdbeJumpHere(v, jmp2);
 98775           sqlite3VdbeResolveLabel(v, jmp3);
 98777         sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
 98778         sqlite3VdbeJumpHere(v, loopTop-1);
 98779 #ifndef SQLITE_OMIT_BTREECOUNT
 98780         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
 98781                      "wrong # of entries in index ", P4_STATIC);
 98782         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 98783           if( pPk==pIdx ) continue;
 98784           addr = sqlite3VdbeCurrentAddr(v);
 98785           sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
 98786           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
 98787           sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
 98788           sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
 98789           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
 98790           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
 98791           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
 98792           sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
 98793           sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
 98795 #endif /* SQLITE_OMIT_BTREECOUNT */
 98798     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
 98799     sqlite3VdbeChangeP2(v, addr, -mxErr);
 98800     sqlite3VdbeJumpHere(v, addr+1);
 98801     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
 98803   break;
 98804 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 98806 #ifndef SQLITE_OMIT_UTF16
 98807   /*
 98808   **   PRAGMA encoding
 98809   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
 98810   **
 98811   ** In its first form, this pragma returns the encoding of the main
 98812   ** database. If the database is not initialized, it is initialized now.
 98813   **
 98814   ** The second form of this pragma is a no-op if the main database file
 98815   ** has not already been initialized. In this case it sets the default
 98816   ** encoding that will be used for the main database file if a new file
 98817   ** is created. If an existing main database file is opened, then the
 98818   ** default text encoding for the existing database is used.
 98819   ** 
 98820   ** In all cases new databases created using the ATTACH command are
 98821   ** created to use the same default text encoding as the main database. If
 98822   ** the main database has not been initialized and/or created when ATTACH
 98823   ** is executed, this is done before the ATTACH operation.
 98824   **
 98825   ** In the second form this pragma sets the text encoding to be used in
 98826   ** new database files created using this database handle. It is only
 98827   ** useful if invoked immediately after the main database i
 98828   */
 98829   case PragTyp_ENCODING: {
 98830     static const struct EncName {
 98831       char *zName;
 98832       u8 enc;
 98833     } encnames[] = {
 98834       { "UTF8",     SQLITE_UTF8        },
 98835       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
 98836       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
 98837       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
 98838       { "UTF16le",  SQLITE_UTF16LE     },
 98839       { "UTF16be",  SQLITE_UTF16BE     },
 98840       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
 98841       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
 98842       { 0, 0 }
 98843     };
 98844     const struct EncName *pEnc;
 98845     if( !zRight ){    /* "PRAGMA encoding" */
 98846       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 98847       sqlite3VdbeSetNumCols(v, 1);
 98848       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
 98849       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
 98850       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
 98851       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
 98852       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
 98853       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
 98854       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 98855     }else{                        /* "PRAGMA encoding = XXX" */
 98856       /* Only change the value of sqlite.enc if the database handle is not
 98857       ** initialized. If the main database exists, the new sqlite.enc value
 98858       ** will be overwritten when the schema is next loaded. If it does not
 98859       ** already exists, it will be created to use the new encoding value.
 98860       */
 98861       if( 
 98862         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
 98863         DbHasProperty(db, 0, DB_Empty) 
 98864       ){
 98865         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
 98866           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
 98867             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
 98868             break;
 98871         if( !pEnc->zName ){
 98872           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
 98877   break;
 98878 #endif /* SQLITE_OMIT_UTF16 */
 98880 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 98881   /*
 98882   **   PRAGMA [database.]schema_version
 98883   **   PRAGMA [database.]schema_version = <integer>
 98884   **
 98885   **   PRAGMA [database.]user_version
 98886   **   PRAGMA [database.]user_version = <integer>
 98887   **
 98888   **   PRAGMA [database.]freelist_count = <integer>
 98889   **
 98890   **   PRAGMA [database.]application_id
 98891   **   PRAGMA [database.]application_id = <integer>
 98892   **
 98893   ** The pragma's schema_version and user_version are used to set or get
 98894   ** the value of the schema-version and user-version, respectively. Both
 98895   ** the schema-version and the user-version are 32-bit signed integers
 98896   ** stored in the database header.
 98897   **
 98898   ** The schema-cookie is usually only manipulated internally by SQLite. It
 98899   ** is incremented by SQLite whenever the database schema is modified (by
 98900   ** creating or dropping a table or index). The schema version is used by
 98901   ** SQLite each time a query is executed to ensure that the internal cache
 98902   ** of the schema used when compiling the SQL query matches the schema of
 98903   ** the database against which the compiled query is actually executed.
 98904   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
 98905   ** the schema-version is potentially dangerous and may lead to program
 98906   ** crashes or database corruption. Use with caution!
 98907   **
 98908   ** The user-version is not used internally by SQLite. It may be used by
 98909   ** applications for any purpose.
 98910   */
 98911   case PragTyp_HEADER_VALUE: {
 98912     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
 98913     sqlite3VdbeUsesBtree(v, iDb);
 98914     switch( zLeft[0] ){
 98915       case 'a': case 'A':
 98916         iCookie = BTREE_APPLICATION_ID;
 98917         break;
 98918       case 'f': case 'F':
 98919         iCookie = BTREE_FREE_PAGE_COUNT;
 98920         break;
 98921       case 's': case 'S':
 98922         iCookie = BTREE_SCHEMA_VERSION;
 98923         break;
 98924       default:
 98925         iCookie = BTREE_USER_VERSION;
 98926         break;
 98929     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
 98930       /* Write the specified cookie value */
 98931       static const VdbeOpList setCookie[] = {
 98932         { OP_Transaction,    0,  1,  0},    /* 0 */
 98933         { OP_Integer,        0,  1,  0},    /* 1 */
 98934         { OP_SetCookie,      0,  0,  1},    /* 2 */
 98935       };
 98936       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
 98937       sqlite3VdbeChangeP1(v, addr, iDb);
 98938       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
 98939       sqlite3VdbeChangeP1(v, addr+2, iDb);
 98940       sqlite3VdbeChangeP2(v, addr+2, iCookie);
 98941     }else{
 98942       /* Read the specified cookie value */
 98943       static const VdbeOpList readCookie[] = {
 98944         { OP_Transaction,     0,  0,  0},    /* 0 */
 98945         { OP_ReadCookie,      0,  1,  0},    /* 1 */
 98946         { OP_ResultRow,       1,  1,  0}
 98947       };
 98948       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
 98949       sqlite3VdbeChangeP1(v, addr, iDb);
 98950       sqlite3VdbeChangeP1(v, addr+1, iDb);
 98951       sqlite3VdbeChangeP3(v, addr+1, iCookie);
 98952       sqlite3VdbeSetNumCols(v, 1);
 98953       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
 98956   break;
 98957 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
 98959 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
 98960   /*
 98961   **   PRAGMA compile_options
 98962   **
 98963   ** Return the names of all compile-time options used in this build,
 98964   ** one option per row.
 98965   */
 98966   case PragTyp_COMPILE_OPTIONS: {
 98967     int i = 0;
 98968     const char *zOpt;
 98969     sqlite3VdbeSetNumCols(v, 1);
 98970     pParse->nMem = 1;
 98971     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
 98972     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
 98973       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
 98974       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 98977   break;
 98978 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 98980 #ifndef SQLITE_OMIT_WAL
 98981   /*
 98982   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
 98983   **
 98984   ** Checkpoint the database.
 98985   */
 98986   case PragTyp_WAL_CHECKPOINT: {
 98987     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
 98988     int eMode = SQLITE_CHECKPOINT_PASSIVE;
 98989     if( zRight ){
 98990       if( sqlite3StrICmp(zRight, "full")==0 ){
 98991         eMode = SQLITE_CHECKPOINT_FULL;
 98992       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
 98993         eMode = SQLITE_CHECKPOINT_RESTART;
 98996     sqlite3VdbeSetNumCols(v, 3);
 98997     pParse->nMem = 3;
 98998     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
 98999     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
 99000     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
 99002     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
 99003     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 99005   break;
 99007   /*
 99008   **   PRAGMA wal_autocheckpoint
 99009   **   PRAGMA wal_autocheckpoint = N
 99010   **
 99011   ** Configure a database connection to automatically checkpoint a database
 99012   ** after accumulating N frames in the log. Or query for the current value
 99013   ** of N.
 99014   */
 99015   case PragTyp_WAL_AUTOCHECKPOINT: {
 99016     if( zRight ){
 99017       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
 99019     returnSingleInt(pParse, "wal_autocheckpoint", 
 99020        db->xWalCallback==sqlite3WalDefaultHook ? 
 99021            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
 99023   break;
 99024 #endif
 99026   /*
 99027   **  PRAGMA shrink_memory
 99028   **
 99029   ** This pragma attempts to free as much memory as possible from the
 99030   ** current database connection.
 99031   */
 99032   case PragTyp_SHRINK_MEMORY: {
 99033     sqlite3_db_release_memory(db);
 99034     break;
 99037   /*
 99038   **   PRAGMA busy_timeout
 99039   **   PRAGMA busy_timeout = N
 99040   **
 99041   ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
 99042   ** if one is set.  If no busy handler or a different busy handler is set
 99043   ** then 0 is returned.  Setting the busy_timeout to 0 or negative
 99044   ** disables the timeout.
 99045   */
 99046   /*case PragTyp_BUSY_TIMEOUT*/ default: {
 99047     assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
 99048     if( zRight ){
 99049       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
 99051     returnSingleInt(pParse, "timeout",  db->busyTimeout);
 99052     break;
 99055   /*
 99056   **   PRAGMA soft_heap_limit
 99057   **   PRAGMA soft_heap_limit = N
 99058   **
 99059   ** Call sqlite3_soft_heap_limit64(N).  Return the result.  If N is omitted,
 99060   ** use -1.
 99061   */
 99062   case PragTyp_SOFT_HEAP_LIMIT: {
 99063     sqlite3_int64 N;
 99064     if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
 99065       sqlite3_soft_heap_limit64(N);
 99067     returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
 99068     break;
 99071 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
 99072   /*
 99073   ** Report the current state of file logs for all databases
 99074   */
 99075   case PragTyp_LOCK_STATUS: {
 99076     static const char *const azLockName[] = {
 99077       "unlocked", "shared", "reserved", "pending", "exclusive"
 99078     };
 99079     int i;
 99080     sqlite3VdbeSetNumCols(v, 2);
 99081     pParse->nMem = 2;
 99082     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
 99083     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
 99084     for(i=0; i<db->nDb; i++){
 99085       Btree *pBt;
 99086       const char *zState = "unknown";
 99087       int j;
 99088       if( db->aDb[i].zName==0 ) continue;
 99089       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
 99090       pBt = db->aDb[i].pBt;
 99091       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
 99092         zState = "closed";
 99093       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
 99094                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
 99095          zState = azLockName[j];
 99097       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
 99098       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
 99100     break;
 99102 #endif
 99104 #ifdef SQLITE_HAS_CODEC
 99105   case PragTyp_KEY: {
 99106     if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
 99107     break;
 99109   case PragTyp_REKEY: {
 99110     if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
 99111     break;
 99113   case PragTyp_HEXKEY: {
 99114     if( zRight ){
 99115       u8 iByte;
 99116       int i;
 99117       char zKey[40];
 99118       for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
 99119         iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
 99120         if( (i&1)!=0 ) zKey[i/2] = iByte;
 99122       if( (zLeft[3] & 0xf)==0xb ){
 99123         sqlite3_key_v2(db, zDb, zKey, i/2);
 99124       }else{
 99125         sqlite3_rekey_v2(db, zDb, zKey, i/2);
 99128     break;
 99130 #endif
 99131 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
 99132   case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
 99133 #ifdef SQLITE_HAS_CODEC
 99134     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
 99135       sqlite3_activate_see(&zRight[4]);
 99137 #endif
 99138 #ifdef SQLITE_ENABLE_CEROD
 99139     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
 99140       sqlite3_activate_cerod(&zRight[6]);
 99142 #endif
 99144   break;
 99145 #endif
 99147   } /* End of the PRAGMA switch */
 99149 pragma_out:
 99150   sqlite3DbFree(db, zLeft);
 99151   sqlite3DbFree(db, zRight);
 99154 #endif /* SQLITE_OMIT_PRAGMA */
 99156 /************** End of pragma.c **********************************************/
 99157 /************** Begin file prepare.c *****************************************/
 99158 /*
 99159 ** 2005 May 25
 99160 **
 99161 ** The author disclaims copyright to this source code.  In place of
 99162 ** a legal notice, here is a blessing:
 99163 **
 99164 **    May you do good and not evil.
 99165 **    May you find forgiveness for yourself and forgive others.
 99166 **    May you share freely, never taking more than you give.
 99167 **
 99168 *************************************************************************
 99169 ** This file contains the implementation of the sqlite3_prepare()
 99170 ** interface, and routines that contribute to loading the database schema
 99171 ** from disk.
 99172 */
 99174 /*
 99175 ** Fill the InitData structure with an error message that indicates
 99176 ** that the database is corrupt.
 99177 */
 99178 static void corruptSchema(
 99179   InitData *pData,     /* Initialization context */
 99180   const char *zObj,    /* Object being parsed at the point of error */
 99181   const char *zExtra   /* Error information */
 99182 ){
 99183   sqlite3 *db = pData->db;
 99184   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
 99185     if( zObj==0 ) zObj = "?";
 99186     sqlite3SetString(pData->pzErrMsg, db,
 99187       "malformed database schema (%s)", zObj);
 99188     if( zExtra ){
 99189       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
 99190                                  "%s - %s", *pData->pzErrMsg, zExtra);
 99193   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
 99196 /*
 99197 ** This is the callback routine for the code that initializes the
 99198 ** database.  See sqlite3Init() below for additional information.
 99199 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
 99200 **
 99201 ** Each callback contains the following information:
 99202 **
 99203 **     argv[0] = name of thing being created
 99204 **     argv[1] = root page number for table or index. 0 for trigger or view.
 99205 **     argv[2] = SQL text for the CREATE statement.
 99206 **
 99207 */
 99208 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
 99209   InitData *pData = (InitData*)pInit;
 99210   sqlite3 *db = pData->db;
 99211   int iDb = pData->iDb;
 99213   assert( argc==3 );
 99214   UNUSED_PARAMETER2(NotUsed, argc);
 99215   assert( sqlite3_mutex_held(db->mutex) );
 99216   DbClearProperty(db, iDb, DB_Empty);
 99217   if( db->mallocFailed ){
 99218     corruptSchema(pData, argv[0], 0);
 99219     return 1;
 99222   assert( iDb>=0 && iDb<db->nDb );
 99223   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
 99224   if( argv[1]==0 ){
 99225     corruptSchema(pData, argv[0], 0);
 99226   }else if( argv[2] && argv[2][0] ){
 99227     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
 99228     ** But because db->init.busy is set to 1, no VDBE code is generated
 99229     ** or executed.  All the parser does is build the internal data
 99230     ** structures that describe the table, index, or view.
 99231     */
 99232     int rc;
 99233     sqlite3_stmt *pStmt;
 99234     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
 99236     assert( db->init.busy );
 99237     db->init.iDb = iDb;
 99238     db->init.newTnum = sqlite3Atoi(argv[1]);
 99239     db->init.orphanTrigger = 0;
 99240     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
 99241     rc = db->errCode;
 99242     assert( (rc&0xFF)==(rcp&0xFF) );
 99243     db->init.iDb = 0;
 99244     if( SQLITE_OK!=rc ){
 99245       if( db->init.orphanTrigger ){
 99246         assert( iDb==1 );
 99247       }else{
 99248         pData->rc = rc;
 99249         if( rc==SQLITE_NOMEM ){
 99250           db->mallocFailed = 1;
 99251         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
 99252           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
 99256     sqlite3_finalize(pStmt);
 99257   }else if( argv[0]==0 ){
 99258     corruptSchema(pData, 0, 0);
 99259   }else{
 99260     /* If the SQL column is blank it means this is an index that
 99261     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
 99262     ** constraint for a CREATE TABLE.  The index should have already
 99263     ** been created when we processed the CREATE TABLE.  All we have
 99264     ** to do here is record the root page number for that index.
 99265     */
 99266     Index *pIndex;
 99267     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
 99268     if( pIndex==0 ){
 99269       /* This can occur if there exists an index on a TEMP table which
 99270       ** has the same name as another index on a permanent index.  Since
 99271       ** the permanent table is hidden by the TEMP table, we can also
 99272       ** safely ignore the index on the permanent table.
 99273       */
 99274       /* Do Nothing */;
 99275     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
 99276       corruptSchema(pData, argv[0], "invalid rootpage");
 99279   return 0;
 99282 /*
 99283 ** Attempt to read the database schema and initialize internal
 99284 ** data structures for a single database file.  The index of the
 99285 ** database file is given by iDb.  iDb==0 is used for the main
 99286 ** database.  iDb==1 should never be used.  iDb>=2 is used for
 99287 ** auxiliary databases.  Return one of the SQLITE_ error codes to
 99288 ** indicate success or failure.
 99289 */
 99290 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
 99291   int rc;
 99292   int i;
 99293 #ifndef SQLITE_OMIT_DEPRECATED
 99294   int size;
 99295 #endif
 99296   Table *pTab;
 99297   Db *pDb;
 99298   char const *azArg[4];
 99299   int meta[5];
 99300   InitData initData;
 99301   char const *zMasterSchema;
 99302   char const *zMasterName;
 99303   int openedTransaction = 0;
 99305   /*
 99306   ** The master database table has a structure like this
 99307   */
 99308   static const char master_schema[] = 
 99309      "CREATE TABLE sqlite_master(\n"
 99310      "  type text,\n"
 99311      "  name text,\n"
 99312      "  tbl_name text,\n"
 99313      "  rootpage integer,\n"
 99314      "  sql text\n"
 99315      ")"
 99317 #ifndef SQLITE_OMIT_TEMPDB
 99318   static const char temp_master_schema[] = 
 99319      "CREATE TEMP TABLE sqlite_temp_master(\n"
 99320      "  type text,\n"
 99321      "  name text,\n"
 99322      "  tbl_name text,\n"
 99323      "  rootpage integer,\n"
 99324      "  sql text\n"
 99325      ")"
 99327 #else
 99328   #define temp_master_schema 0
 99329 #endif
 99331   assert( iDb>=0 && iDb<db->nDb );
 99332   assert( db->aDb[iDb].pSchema );
 99333   assert( sqlite3_mutex_held(db->mutex) );
 99334   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
 99336   /* zMasterSchema and zInitScript are set to point at the master schema
 99337   ** and initialisation script appropriate for the database being
 99338   ** initialized. zMasterName is the name of the master table.
 99339   */
 99340   if( !OMIT_TEMPDB && iDb==1 ){
 99341     zMasterSchema = temp_master_schema;
 99342   }else{
 99343     zMasterSchema = master_schema;
 99345   zMasterName = SCHEMA_TABLE(iDb);
 99347   /* Construct the schema tables.  */
 99348   azArg[0] = zMasterName;
 99349   azArg[1] = "1";
 99350   azArg[2] = zMasterSchema;
 99351   azArg[3] = 0;
 99352   initData.db = db;
 99353   initData.iDb = iDb;
 99354   initData.rc = SQLITE_OK;
 99355   initData.pzErrMsg = pzErrMsg;
 99356   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
 99357   if( initData.rc ){
 99358     rc = initData.rc;
 99359     goto error_out;
 99361   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
 99362   if( ALWAYS(pTab) ){
 99363     pTab->tabFlags |= TF_Readonly;
 99366   /* Create a cursor to hold the database open
 99367   */
 99368   pDb = &db->aDb[iDb];
 99369   if( pDb->pBt==0 ){
 99370     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
 99371       DbSetProperty(db, 1, DB_SchemaLoaded);
 99373     return SQLITE_OK;
 99376   /* If there is not already a read-only (or read-write) transaction opened
 99377   ** on the b-tree database, open one now. If a transaction is opened, it 
 99378   ** will be closed before this function returns.  */
 99379   sqlite3BtreeEnter(pDb->pBt);
 99380   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
 99381     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
 99382     if( rc!=SQLITE_OK ){
 99383       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
 99384       goto initone_error_out;
 99386     openedTransaction = 1;
 99389   /* Get the database meta information.
 99390   **
 99391   ** Meta values are as follows:
 99392   **    meta[0]   Schema cookie.  Changes with each schema change.
 99393   **    meta[1]   File format of schema layer.
 99394   **    meta[2]   Size of the page cache.
 99395   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
 99396   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
 99397   **    meta[5]   User version
 99398   **    meta[6]   Incremental vacuum mode
 99399   **    meta[7]   unused
 99400   **    meta[8]   unused
 99401   **    meta[9]   unused
 99402   **
 99403   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
 99404   ** the possible values of meta[4].
 99405   */
 99406   for(i=0; i<ArraySize(meta); i++){
 99407     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
 99409   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
 99411   /* If opening a non-empty database, check the text encoding. For the
 99412   ** main database, set sqlite3.enc to the encoding of the main database.
 99413   ** For an attached db, it is an error if the encoding is not the same
 99414   ** as sqlite3.enc.
 99415   */
 99416   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
 99417     if( iDb==0 ){
 99418 #ifndef SQLITE_OMIT_UTF16
 99419       u8 encoding;
 99420       /* If opening the main database, set ENC(db). */
 99421       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
 99422       if( encoding==0 ) encoding = SQLITE_UTF8;
 99423       ENC(db) = encoding;
 99424 #else
 99425       ENC(db) = SQLITE_UTF8;
 99426 #endif
 99427     }else{
 99428       /* If opening an attached database, the encoding much match ENC(db) */
 99429       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
 99430         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
 99431             " text encoding as main database");
 99432         rc = SQLITE_ERROR;
 99433         goto initone_error_out;
 99436   }else{
 99437     DbSetProperty(db, iDb, DB_Empty);
 99439   pDb->pSchema->enc = ENC(db);
 99441   if( pDb->pSchema->cache_size==0 ){
 99442 #ifndef SQLITE_OMIT_DEPRECATED
 99443     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
 99444     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
 99445     pDb->pSchema->cache_size = size;
 99446 #else
 99447     pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
 99448 #endif
 99449     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 99452   /*
 99453   ** file_format==1    Version 3.0.0.
 99454   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
 99455   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
 99456   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
 99457   */
 99458   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
 99459   if( pDb->pSchema->file_format==0 ){
 99460     pDb->pSchema->file_format = 1;
 99462   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
 99463     sqlite3SetString(pzErrMsg, db, "unsupported file format");
 99464     rc = SQLITE_ERROR;
 99465     goto initone_error_out;
 99468   /* Ticket #2804:  When we open a database in the newer file format,
 99469   ** clear the legacy_file_format pragma flag so that a VACUUM will
 99470   ** not downgrade the database and thus invalidate any descending
 99471   ** indices that the user might have created.
 99472   */
 99473   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
 99474     db->flags &= ~SQLITE_LegacyFileFmt;
 99477   /* Read the schema information out of the schema tables
 99478   */
 99479   assert( db->init.busy );
 99481     char *zSql;
 99482     zSql = sqlite3MPrintf(db, 
 99483         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
 99484         db->aDb[iDb].zName, zMasterName);
 99485 #ifndef SQLITE_OMIT_AUTHORIZATION
 99487       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
 99488       xAuth = db->xAuth;
 99489       db->xAuth = 0;
 99490 #endif
 99491       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
 99492 #ifndef SQLITE_OMIT_AUTHORIZATION
 99493       db->xAuth = xAuth;
 99495 #endif
 99496     if( rc==SQLITE_OK ) rc = initData.rc;
 99497     sqlite3DbFree(db, zSql);
 99498 #ifndef SQLITE_OMIT_ANALYZE
 99499     if( rc==SQLITE_OK ){
 99500       sqlite3AnalysisLoad(db, iDb);
 99502 #endif
 99504   if( db->mallocFailed ){
 99505     rc = SQLITE_NOMEM;
 99506     sqlite3ResetAllSchemasOfConnection(db);
 99508   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
 99509     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
 99510     ** the schema loaded, even if errors occurred. In this situation the 
 99511     ** current sqlite3_prepare() operation will fail, but the following one
 99512     ** will attempt to compile the supplied statement against whatever subset
 99513     ** of the schema was loaded before the error occurred. The primary
 99514     ** purpose of this is to allow access to the sqlite_master table
 99515     ** even when its contents have been corrupted.
 99516     */
 99517     DbSetProperty(db, iDb, DB_SchemaLoaded);
 99518     rc = SQLITE_OK;
 99521   /* Jump here for an error that occurs after successfully allocating
 99522   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
 99523   ** before that point, jump to error_out.
 99524   */
 99525 initone_error_out:
 99526   if( openedTransaction ){
 99527     sqlite3BtreeCommit(pDb->pBt);
 99529   sqlite3BtreeLeave(pDb->pBt);
 99531 error_out:
 99532   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 99533     db->mallocFailed = 1;
 99535   return rc;
 99538 /*
 99539 ** Initialize all database files - the main database file, the file
 99540 ** used to store temporary tables, and any additional database files
 99541 ** created using ATTACH statements.  Return a success code.  If an
 99542 ** error occurs, write an error message into *pzErrMsg.
 99543 **
 99544 ** After a database is initialized, the DB_SchemaLoaded bit is set
 99545 ** bit is set in the flags field of the Db structure. If the database
 99546 ** file was of zero-length, then the DB_Empty flag is also set.
 99547 */
 99548 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
 99549   int i, rc;
 99550   int commit_internal = !(db->flags&SQLITE_InternChanges);
 99552   assert( sqlite3_mutex_held(db->mutex) );
 99553   rc = SQLITE_OK;
 99554   db->init.busy = 1;
 99555   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 99556     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
 99557     rc = sqlite3InitOne(db, i, pzErrMsg);
 99558     if( rc ){
 99559       sqlite3ResetOneSchema(db, i);
 99563   /* Once all the other databases have been initialized, load the schema
 99564   ** for the TEMP database. This is loaded last, as the TEMP database
 99565   ** schema may contain references to objects in other databases.
 99566   */
 99567 #ifndef SQLITE_OMIT_TEMPDB
 99568   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
 99569                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
 99570     rc = sqlite3InitOne(db, 1, pzErrMsg);
 99571     if( rc ){
 99572       sqlite3ResetOneSchema(db, 1);
 99575 #endif
 99577   db->init.busy = 0;
 99578   if( rc==SQLITE_OK && commit_internal ){
 99579     sqlite3CommitInternalChanges(db);
 99582   return rc; 
 99585 /*
 99586 ** This routine is a no-op if the database schema is already initialized.
 99587 ** Otherwise, the schema is loaded. An error code is returned.
 99588 */
 99589 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
 99590   int rc = SQLITE_OK;
 99591   sqlite3 *db = pParse->db;
 99592   assert( sqlite3_mutex_held(db->mutex) );
 99593   if( !db->init.busy ){
 99594     rc = sqlite3Init(db, &pParse->zErrMsg);
 99596   if( rc!=SQLITE_OK ){
 99597     pParse->rc = rc;
 99598     pParse->nErr++;
 99600   return rc;
 99604 /*
 99605 ** Check schema cookies in all databases.  If any cookie is out
 99606 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
 99607 ** make no changes to pParse->rc.
 99608 */
 99609 static void schemaIsValid(Parse *pParse){
 99610   sqlite3 *db = pParse->db;
 99611   int iDb;
 99612   int rc;
 99613   int cookie;
 99615   assert( pParse->checkSchema );
 99616   assert( sqlite3_mutex_held(db->mutex) );
 99617   for(iDb=0; iDb<db->nDb; iDb++){
 99618     int openedTransaction = 0;         /* True if a transaction is opened */
 99619     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
 99620     if( pBt==0 ) continue;
 99622     /* If there is not already a read-only (or read-write) transaction opened
 99623     ** on the b-tree database, open one now. If a transaction is opened, it 
 99624     ** will be closed immediately after reading the meta-value. */
 99625     if( !sqlite3BtreeIsInReadTrans(pBt) ){
 99626       rc = sqlite3BtreeBeginTrans(pBt, 0);
 99627       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 99628         db->mallocFailed = 1;
 99630       if( rc!=SQLITE_OK ) return;
 99631       openedTransaction = 1;
 99634     /* Read the schema cookie from the database. If it does not match the 
 99635     ** value stored as part of the in-memory schema representation,
 99636     ** set Parse.rc to SQLITE_SCHEMA. */
 99637     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
 99638     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 99639     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
 99640       sqlite3ResetOneSchema(db, iDb);
 99641       pParse->rc = SQLITE_SCHEMA;
 99644     /* Close the transaction, if one was opened. */
 99645     if( openedTransaction ){
 99646       sqlite3BtreeCommit(pBt);
 99651 /*
 99652 ** Convert a schema pointer into the iDb index that indicates
 99653 ** which database file in db->aDb[] the schema refers to.
 99654 **
 99655 ** If the same database is attached more than once, the first
 99656 ** attached database is returned.
 99657 */
 99658 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
 99659   int i = -1000000;
 99661   /* If pSchema is NULL, then return -1000000. This happens when code in 
 99662   ** expr.c is trying to resolve a reference to a transient table (i.e. one
 99663   ** created by a sub-select). In this case the return value of this 
 99664   ** function should never be used.
 99665   **
 99666   ** We return -1000000 instead of the more usual -1 simply because using
 99667   ** -1000000 as the incorrect index into db->aDb[] is much 
 99668   ** more likely to cause a segfault than -1 (of course there are assert()
 99669   ** statements too, but it never hurts to play the odds).
 99670   */
 99671   assert( sqlite3_mutex_held(db->mutex) );
 99672   if( pSchema ){
 99673     for(i=0; ALWAYS(i<db->nDb); i++){
 99674       if( db->aDb[i].pSchema==pSchema ){
 99675         break;
 99678     assert( i>=0 && i<db->nDb );
 99680   return i;
 99683 /*
 99684 ** Free all memory allocations in the pParse object
 99685 */
 99686 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
 99687   if( pParse ){
 99688     sqlite3 *db = pParse->db;
 99689     sqlite3DbFree(db, pParse->aLabel);
 99690     sqlite3ExprListDelete(db, pParse->pConstExpr);
 99694 /*
 99695 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
 99696 */
 99697 static int sqlite3Prepare(
 99698   sqlite3 *db,              /* Database handle. */
 99699   const char *zSql,         /* UTF-8 encoded SQL statement. */
 99700   int nBytes,               /* Length of zSql in bytes. */
 99701   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 99702   Vdbe *pReprepare,         /* VM being reprepared */
 99703   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 99704   const char **pzTail       /* OUT: End of parsed string */
 99705 ){
 99706   Parse *pParse;            /* Parsing context */
 99707   char *zErrMsg = 0;        /* Error message */
 99708   int rc = SQLITE_OK;       /* Result code */
 99709   int i;                    /* Loop counter */
 99711   /* Allocate the parsing context */
 99712   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
 99713   if( pParse==0 ){
 99714     rc = SQLITE_NOMEM;
 99715     goto end_prepare;
 99717   pParse->pReprepare = pReprepare;
 99718   assert( ppStmt && *ppStmt==0 );
 99719   assert( !db->mallocFailed );
 99720   assert( sqlite3_mutex_held(db->mutex) );
 99722   /* Check to verify that it is possible to get a read lock on all
 99723   ** database schemas.  The inability to get a read lock indicates that
 99724   ** some other database connection is holding a write-lock, which in
 99725   ** turn means that the other connection has made uncommitted changes
 99726   ** to the schema.
 99727   **
 99728   ** Were we to proceed and prepare the statement against the uncommitted
 99729   ** schema changes and if those schema changes are subsequently rolled
 99730   ** back and different changes are made in their place, then when this
 99731   ** prepared statement goes to run the schema cookie would fail to detect
 99732   ** the schema change.  Disaster would follow.
 99733   **
 99734   ** This thread is currently holding mutexes on all Btrees (because
 99735   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
 99736   ** is not possible for another thread to start a new schema change
 99737   ** while this routine is running.  Hence, we do not need to hold 
 99738   ** locks on the schema, we just need to make sure nobody else is 
 99739   ** holding them.
 99740   **
 99741   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
 99742   ** but it does *not* override schema lock detection, so this all still
 99743   ** works even if READ_UNCOMMITTED is set.
 99744   */
 99745   for(i=0; i<db->nDb; i++) {
 99746     Btree *pBt = db->aDb[i].pBt;
 99747     if( pBt ){
 99748       assert( sqlite3BtreeHoldsMutex(pBt) );
 99749       rc = sqlite3BtreeSchemaLocked(pBt);
 99750       if( rc ){
 99751         const char *zDb = db->aDb[i].zName;
 99752         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
 99753         testcase( db->flags & SQLITE_ReadUncommitted );
 99754         goto end_prepare;
 99759   sqlite3VtabUnlockList(db);
 99761   pParse->db = db;
 99762   pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
 99763   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
 99764     char *zSqlCopy;
 99765     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 99766     testcase( nBytes==mxLen );
 99767     testcase( nBytes==mxLen+1 );
 99768     if( nBytes>mxLen ){
 99769       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
 99770       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
 99771       goto end_prepare;
 99773     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
 99774     if( zSqlCopy ){
 99775       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
 99776       sqlite3DbFree(db, zSqlCopy);
 99777       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
 99778     }else{
 99779       pParse->zTail = &zSql[nBytes];
 99781   }else{
 99782     sqlite3RunParser(pParse, zSql, &zErrMsg);
 99784   assert( 0==pParse->nQueryLoop );
 99786   if( db->mallocFailed ){
 99787     pParse->rc = SQLITE_NOMEM;
 99789   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
 99790   if( pParse->checkSchema ){
 99791     schemaIsValid(pParse);
 99793   if( db->mallocFailed ){
 99794     pParse->rc = SQLITE_NOMEM;
 99796   if( pzTail ){
 99797     *pzTail = pParse->zTail;
 99799   rc = pParse->rc;
 99801 #ifndef SQLITE_OMIT_EXPLAIN
 99802   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
 99803     static const char * const azColName[] = {
 99804        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
 99805        "selectid", "order", "from", "detail"
 99806     };
 99807     int iFirst, mx;
 99808     if( pParse->explain==2 ){
 99809       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
 99810       iFirst = 8;
 99811       mx = 12;
 99812     }else{
 99813       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
 99814       iFirst = 0;
 99815       mx = 8;
 99817     for(i=iFirst; i<mx; i++){
 99818       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
 99819                             azColName[i], SQLITE_STATIC);
 99822 #endif
 99824   if( db->init.busy==0 ){
 99825     Vdbe *pVdbe = pParse->pVdbe;
 99826     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
 99828   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
 99829     sqlite3VdbeFinalize(pParse->pVdbe);
 99830     assert(!(*ppStmt));
 99831   }else{
 99832     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
 99835   if( zErrMsg ){
 99836     sqlite3Error(db, rc, "%s", zErrMsg);
 99837     sqlite3DbFree(db, zErrMsg);
 99838   }else{
 99839     sqlite3Error(db, rc, 0);
 99842   /* Delete any TriggerPrg structures allocated while parsing this statement. */
 99843   while( pParse->pTriggerPrg ){
 99844     TriggerPrg *pT = pParse->pTriggerPrg;
 99845     pParse->pTriggerPrg = pT->pNext;
 99846     sqlite3DbFree(db, pT);
 99849 end_prepare:
 99851   sqlite3ParserReset(pParse);
 99852   sqlite3StackFree(db, pParse);
 99853   rc = sqlite3ApiExit(db, rc);
 99854   assert( (rc&db->errMask)==rc );
 99855   return rc;
 99857 static int sqlite3LockAndPrepare(
 99858   sqlite3 *db,              /* Database handle. */
 99859   const char *zSql,         /* UTF-8 encoded SQL statement. */
 99860   int nBytes,               /* Length of zSql in bytes. */
 99861   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
 99862   Vdbe *pOld,               /* VM being reprepared */
 99863   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 99864   const char **pzTail       /* OUT: End of parsed string */
 99865 ){
 99866   int rc;
 99867   assert( ppStmt!=0 );
 99868   *ppStmt = 0;
 99869   if( !sqlite3SafetyCheckOk(db) ){
 99870     return SQLITE_MISUSE_BKPT;
 99872   sqlite3_mutex_enter(db->mutex);
 99873   sqlite3BtreeEnterAll(db);
 99874   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 99875   if( rc==SQLITE_SCHEMA ){
 99876     sqlite3_finalize(*ppStmt);
 99877     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
 99879   sqlite3BtreeLeaveAll(db);
 99880   sqlite3_mutex_leave(db->mutex);
 99881   assert( rc==SQLITE_OK || *ppStmt==0 );
 99882   return rc;
 99885 /*
 99886 ** Rerun the compilation of a statement after a schema change.
 99887 **
 99888 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
 99889 ** if the statement cannot be recompiled because another connection has
 99890 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
 99891 ** occurs, return SQLITE_SCHEMA.
 99892 */
 99893 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
 99894   int rc;
 99895   sqlite3_stmt *pNew;
 99896   const char *zSql;
 99897   sqlite3 *db;
 99899   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
 99900   zSql = sqlite3_sql((sqlite3_stmt *)p);
 99901   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
 99902   db = sqlite3VdbeDb(p);
 99903   assert( sqlite3_mutex_held(db->mutex) );
 99904   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
 99905   if( rc ){
 99906     if( rc==SQLITE_NOMEM ){
 99907       db->mallocFailed = 1;
 99909     assert( pNew==0 );
 99910     return rc;
 99911   }else{
 99912     assert( pNew!=0 );
 99914   sqlite3VdbeSwap((Vdbe*)pNew, p);
 99915   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
 99916   sqlite3VdbeResetStepResult((Vdbe*)pNew);
 99917   sqlite3VdbeFinalize((Vdbe*)pNew);
 99918   return SQLITE_OK;
 99922 /*
 99923 ** Two versions of the official API.  Legacy and new use.  In the legacy
 99924 ** version, the original SQL text is not saved in the prepared statement
 99925 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 99926 ** sqlite3_step().  In the new version, the original SQL text is retained
 99927 ** and the statement is automatically recompiled if an schema change
 99928 ** occurs.
 99929 */
 99930 SQLITE_API int sqlite3_prepare(
 99931   sqlite3 *db,              /* Database handle. */
 99932   const char *zSql,         /* UTF-8 encoded SQL statement. */
 99933   int nBytes,               /* Length of zSql in bytes. */
 99934   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 99935   const char **pzTail       /* OUT: End of parsed string */
 99936 ){
 99937   int rc;
 99938   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
 99939   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 99940   return rc;
 99942 SQLITE_API int sqlite3_prepare_v2(
 99943   sqlite3 *db,              /* Database handle. */
 99944   const char *zSql,         /* UTF-8 encoded SQL statement. */
 99945   int nBytes,               /* Length of zSql in bytes. */
 99946   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 99947   const char **pzTail       /* OUT: End of parsed string */
 99948 ){
 99949   int rc;
 99950   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
 99951   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 99952   return rc;
 99956 #ifndef SQLITE_OMIT_UTF16
 99957 /*
 99958 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
 99959 */
 99960 static int sqlite3Prepare16(
 99961   sqlite3 *db,              /* Database handle. */ 
 99962   const void *zSql,         /* UTF-16 encoded SQL statement. */
 99963   int nBytes,               /* Length of zSql in bytes. */
 99964   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
 99965   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 99966   const void **pzTail       /* OUT: End of parsed string */
 99967 ){
 99968   /* This function currently works by first transforming the UTF-16
 99969   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
 99970   ** tricky bit is figuring out the pointer to return in *pzTail.
 99971   */
 99972   char *zSql8;
 99973   const char *zTail8 = 0;
 99974   int rc = SQLITE_OK;
 99976   assert( ppStmt );
 99977   *ppStmt = 0;
 99978   if( !sqlite3SafetyCheckOk(db) ){
 99979     return SQLITE_MISUSE_BKPT;
 99981   if( nBytes>=0 ){
 99982     int sz;
 99983     const char *z = (const char*)zSql;
 99984     for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
 99985     nBytes = sz;
 99987   sqlite3_mutex_enter(db->mutex);
 99988   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
 99989   if( zSql8 ){
 99990     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
 99993   if( zTail8 && pzTail ){
 99994     /* If sqlite3_prepare returns a tail pointer, we calculate the
 99995     ** equivalent pointer into the UTF-16 string by counting the unicode
 99996     ** characters between zSql8 and zTail8, and then returning a pointer
 99997     ** the same number of characters into the UTF-16 string.
 99998     */
 99999     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
 100000     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
 100002   sqlite3DbFree(db, zSql8); 
 100003   rc = sqlite3ApiExit(db, rc);
 100004   sqlite3_mutex_leave(db->mutex);
 100005   return rc;
 100009 ** Two versions of the official API.  Legacy and new use.  In the legacy
 100010 ** version, the original SQL text is not saved in the prepared statement
 100011 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
 100012 ** sqlite3_step().  In the new version, the original SQL text is retained
 100013 ** and the statement is automatically recompiled if an schema change
 100014 ** occurs.
 100016 SQLITE_API int sqlite3_prepare16(
 100017   sqlite3 *db,              /* Database handle. */ 
 100018   const void *zSql,         /* UTF-16 encoded SQL statement. */
 100019   int nBytes,               /* Length of zSql in bytes. */
 100020   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 100021   const void **pzTail       /* OUT: End of parsed string */
 100023   int rc;
 100024   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
 100025   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 100026   return rc;
 100028 SQLITE_API int sqlite3_prepare16_v2(
 100029   sqlite3 *db,              /* Database handle. */ 
 100030   const void *zSql,         /* UTF-16 encoded SQL statement. */
 100031   int nBytes,               /* Length of zSql in bytes. */
 100032   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
 100033   const void **pzTail       /* OUT: End of parsed string */
 100035   int rc;
 100036   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
 100037   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
 100038   return rc;
 100041 #endif /* SQLITE_OMIT_UTF16 */
 100043 /************** End of prepare.c *********************************************/
 100044 /************** Begin file select.c ******************************************/
 100046 ** 2001 September 15
 100048 ** The author disclaims copyright to this source code.  In place of
 100049 ** a legal notice, here is a blessing:
 100051 **    May you do good and not evil.
 100052 **    May you find forgiveness for yourself and forgive others.
 100053 **    May you share freely, never taking more than you give.
 100055 *************************************************************************
 100056 ** This file contains C code routines that are called by the parser
 100057 ** to handle SELECT statements in SQLite.
 100062 ** Delete all the content of a Select structure but do not deallocate
 100063 ** the select structure itself.
 100065 static void clearSelect(sqlite3 *db, Select *p){
 100066   sqlite3ExprListDelete(db, p->pEList);
 100067   sqlite3SrcListDelete(db, p->pSrc);
 100068   sqlite3ExprDelete(db, p->pWhere);
 100069   sqlite3ExprListDelete(db, p->pGroupBy);
 100070   sqlite3ExprDelete(db, p->pHaving);
 100071   sqlite3ExprListDelete(db, p->pOrderBy);
 100072   sqlite3SelectDelete(db, p->pPrior);
 100073   sqlite3ExprDelete(db, p->pLimit);
 100074   sqlite3ExprDelete(db, p->pOffset);
 100075   sqlite3WithDelete(db, p->pWith);
 100079 ** Initialize a SelectDest structure.
 100081 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
 100082   pDest->eDest = (u8)eDest;
 100083   pDest->iSDParm = iParm;
 100084   pDest->affSdst = 0;
 100085   pDest->iSdst = 0;
 100086   pDest->nSdst = 0;
 100091 ** Allocate a new Select structure and return a pointer to that
 100092 ** structure.
 100094 SQLITE_PRIVATE Select *sqlite3SelectNew(
 100095   Parse *pParse,        /* Parsing context */
 100096   ExprList *pEList,     /* which columns to include in the result */
 100097   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
 100098   Expr *pWhere,         /* the WHERE clause */
 100099   ExprList *pGroupBy,   /* the GROUP BY clause */
 100100   Expr *pHaving,        /* the HAVING clause */
 100101   ExprList *pOrderBy,   /* the ORDER BY clause */
 100102   u16 selFlags,         /* Flag parameters, such as SF_Distinct */
 100103   Expr *pLimit,         /* LIMIT value.  NULL means not used */
 100104   Expr *pOffset         /* OFFSET value.  NULL means no offset */
 100106   Select *pNew;
 100107   Select standin;
 100108   sqlite3 *db = pParse->db;
 100109   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
 100110   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
 100111   if( pNew==0 ){
 100112     assert( db->mallocFailed );
 100113     pNew = &standin;
 100114     memset(pNew, 0, sizeof(*pNew));
 100116   if( pEList==0 ){
 100117     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
 100119   pNew->pEList = pEList;
 100120   if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
 100121   pNew->pSrc = pSrc;
 100122   pNew->pWhere = pWhere;
 100123   pNew->pGroupBy = pGroupBy;
 100124   pNew->pHaving = pHaving;
 100125   pNew->pOrderBy = pOrderBy;
 100126   pNew->selFlags = selFlags;
 100127   pNew->op = TK_SELECT;
 100128   pNew->pLimit = pLimit;
 100129   pNew->pOffset = pOffset;
 100130   assert( pOffset==0 || pLimit!=0 );
 100131   pNew->addrOpenEphm[0] = -1;
 100132   pNew->addrOpenEphm[1] = -1;
 100133   pNew->addrOpenEphm[2] = -1;
 100134   if( db->mallocFailed ) {
 100135     clearSelect(db, pNew);
 100136     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
 100137     pNew = 0;
 100138   }else{
 100139     assert( pNew->pSrc!=0 || pParse->nErr>0 );
 100141   assert( pNew!=&standin );
 100142   return pNew;
 100146 ** Delete the given Select structure and all of its substructures.
 100148 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
 100149   if( p ){
 100150     clearSelect(db, p);
 100151     sqlite3DbFree(db, p);
 100156 ** Return a pointer to the right-most SELECT statement in a compound.
 100158 static Select *findRightmost(Select *p){
 100159   while( p->pNext ) p = p->pNext;
 100160   return p;
 100164 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
 100165 ** type of join.  Return an integer constant that expresses that type
 100166 ** in terms of the following bit values:
 100168 **     JT_INNER
 100169 **     JT_CROSS
 100170 **     JT_OUTER
 100171 **     JT_NATURAL
 100172 **     JT_LEFT
 100173 **     JT_RIGHT
 100175 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
 100177 ** If an illegal or unsupported join type is seen, then still return
 100178 ** a join type, but put an error in the pParse structure.
 100180 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
 100181   int jointype = 0;
 100182   Token *apAll[3];
 100183   Token *p;
 100184                              /*   0123456789 123456789 123456789 123 */
 100185   static const char zKeyText[] = "naturaleftouterightfullinnercross";
 100186   static const struct {
 100187     u8 i;        /* Beginning of keyword text in zKeyText[] */
 100188     u8 nChar;    /* Length of the keyword in characters */
 100189     u8 code;     /* Join type mask */
 100190   } aKeyword[] = {
 100191     /* natural */ { 0,  7, JT_NATURAL                },
 100192     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
 100193     /* outer   */ { 10, 5, JT_OUTER                  },
 100194     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
 100195     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
 100196     /* inner   */ { 23, 5, JT_INNER                  },
 100197     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
 100199   int i, j;
 100200   apAll[0] = pA;
 100201   apAll[1] = pB;
 100202   apAll[2] = pC;
 100203   for(i=0; i<3 && apAll[i]; i++){
 100204     p = apAll[i];
 100205     for(j=0; j<ArraySize(aKeyword); j++){
 100206       if( p->n==aKeyword[j].nChar 
 100207           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
 100208         jointype |= aKeyword[j].code;
 100209         break;
 100212     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
 100213     if( j>=ArraySize(aKeyword) ){
 100214       jointype |= JT_ERROR;
 100215       break;
 100218   if(
 100219      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
 100220      (jointype & JT_ERROR)!=0
 100222     const char *zSp = " ";
 100223     assert( pB!=0 );
 100224     if( pC==0 ){ zSp++; }
 100225     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
 100226        "%T %T%s%T", pA, pB, zSp, pC);
 100227     jointype = JT_INNER;
 100228   }else if( (jointype & JT_OUTER)!=0 
 100229          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
 100230     sqlite3ErrorMsg(pParse, 
 100231       "RIGHT and FULL OUTER JOINs are not currently supported");
 100232     jointype = JT_INNER;
 100234   return jointype;
 100238 ** Return the index of a column in a table.  Return -1 if the column
 100239 ** is not contained in the table.
 100241 static int columnIndex(Table *pTab, const char *zCol){
 100242   int i;
 100243   for(i=0; i<pTab->nCol; i++){
 100244     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
 100246   return -1;
 100250 ** Search the first N tables in pSrc, from left to right, looking for a
 100251 ** table that has a column named zCol.  
 100253 ** When found, set *piTab and *piCol to the table index and column index
 100254 ** of the matching column and return TRUE.
 100256 ** If not found, return FALSE.
 100258 static int tableAndColumnIndex(
 100259   SrcList *pSrc,       /* Array of tables to search */
 100260   int N,               /* Number of tables in pSrc->a[] to search */
 100261   const char *zCol,    /* Name of the column we are looking for */
 100262   int *piTab,          /* Write index of pSrc->a[] here */
 100263   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
 100265   int i;               /* For looping over tables in pSrc */
 100266   int iCol;            /* Index of column matching zCol */
 100268   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
 100269   for(i=0; i<N; i++){
 100270     iCol = columnIndex(pSrc->a[i].pTab, zCol);
 100271     if( iCol>=0 ){
 100272       if( piTab ){
 100273         *piTab = i;
 100274         *piCol = iCol;
 100276       return 1;
 100279   return 0;
 100283 ** This function is used to add terms implied by JOIN syntax to the
 100284 ** WHERE clause expression of a SELECT statement. The new term, which
 100285 ** is ANDed with the existing WHERE clause, is of the form:
 100287 **    (tab1.col1 = tab2.col2)
 100289 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the 
 100290 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
 100291 ** column iColRight of tab2.
 100293 static void addWhereTerm(
 100294   Parse *pParse,                  /* Parsing context */
 100295   SrcList *pSrc,                  /* List of tables in FROM clause */
 100296   int iLeft,                      /* Index of first table to join in pSrc */
 100297   int iColLeft,                   /* Index of column in first table */
 100298   int iRight,                     /* Index of second table in pSrc */
 100299   int iColRight,                  /* Index of column in second table */
 100300   int isOuterJoin,                /* True if this is an OUTER join */
 100301   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
 100303   sqlite3 *db = pParse->db;
 100304   Expr *pE1;
 100305   Expr *pE2;
 100306   Expr *pEq;
 100308   assert( iLeft<iRight );
 100309   assert( pSrc->nSrc>iRight );
 100310   assert( pSrc->a[iLeft].pTab );
 100311   assert( pSrc->a[iRight].pTab );
 100313   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
 100314   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
 100316   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
 100317   if( pEq && isOuterJoin ){
 100318     ExprSetProperty(pEq, EP_FromJoin);
 100319     assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
 100320     ExprSetVVAProperty(pEq, EP_NoReduce);
 100321     pEq->iRightJoinTable = (i16)pE2->iTable;
 100323   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
 100327 ** Set the EP_FromJoin property on all terms of the given expression.
 100328 ** And set the Expr.iRightJoinTable to iTable for every term in the
 100329 ** expression.
 100331 ** The EP_FromJoin property is used on terms of an expression to tell
 100332 ** the LEFT OUTER JOIN processing logic that this term is part of the
 100333 ** join restriction specified in the ON or USING clause and not a part
 100334 ** of the more general WHERE clause.  These terms are moved over to the
 100335 ** WHERE clause during join processing but we need to remember that they
 100336 ** originated in the ON or USING clause.
 100338 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
 100339 ** expression depends on table iRightJoinTable even if that table is not
 100340 ** explicitly mentioned in the expression.  That information is needed
 100341 ** for cases like this:
 100343 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
 100345 ** The where clause needs to defer the handling of the t1.x=5
 100346 ** term until after the t2 loop of the join.  In that way, a
 100347 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
 100348 ** defer the handling of t1.x=5, it will be processed immediately
 100349 ** after the t1 loop and rows with t1.x!=5 will never appear in
 100350 ** the output, which is incorrect.
 100352 static void setJoinExpr(Expr *p, int iTable){
 100353   while( p ){
 100354     ExprSetProperty(p, EP_FromJoin);
 100355     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
 100356     ExprSetVVAProperty(p, EP_NoReduce);
 100357     p->iRightJoinTable = (i16)iTable;
 100358     setJoinExpr(p->pLeft, iTable);
 100359     p = p->pRight;
 100364 ** This routine processes the join information for a SELECT statement.
 100365 ** ON and USING clauses are converted into extra terms of the WHERE clause.
 100366 ** NATURAL joins also create extra WHERE clause terms.
 100368 ** The terms of a FROM clause are contained in the Select.pSrc structure.
 100369 ** The left most table is the first entry in Select.pSrc.  The right-most
 100370 ** table is the last entry.  The join operator is held in the entry to
 100371 ** the left.  Thus entry 0 contains the join operator for the join between
 100372 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
 100373 ** also attached to the left entry.
 100375 ** This routine returns the number of errors encountered.
 100377 static int sqliteProcessJoin(Parse *pParse, Select *p){
 100378   SrcList *pSrc;                  /* All tables in the FROM clause */
 100379   int i, j;                       /* Loop counters */
 100380   struct SrcList_item *pLeft;     /* Left table being joined */
 100381   struct SrcList_item *pRight;    /* Right table being joined */
 100383   pSrc = p->pSrc;
 100384   pLeft = &pSrc->a[0];
 100385   pRight = &pLeft[1];
 100386   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
 100387     Table *pLeftTab = pLeft->pTab;
 100388     Table *pRightTab = pRight->pTab;
 100389     int isOuter;
 100391     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
 100392     isOuter = (pRight->jointype & JT_OUTER)!=0;
 100394     /* When the NATURAL keyword is present, add WHERE clause terms for
 100395     ** every column that the two tables have in common.
 100397     if( pRight->jointype & JT_NATURAL ){
 100398       if( pRight->pOn || pRight->pUsing ){
 100399         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
 100400            "an ON or USING clause", 0);
 100401         return 1;
 100403       for(j=0; j<pRightTab->nCol; j++){
 100404         char *zName;   /* Name of column in the right table */
 100405         int iLeft;     /* Matching left table */
 100406         int iLeftCol;  /* Matching column in the left table */
 100408         zName = pRightTab->aCol[j].zName;
 100409         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
 100410           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
 100411                        isOuter, &p->pWhere);
 100416     /* Disallow both ON and USING clauses in the same join
 100418     if( pRight->pOn && pRight->pUsing ){
 100419       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
 100420         "clauses in the same join");
 100421       return 1;
 100424     /* Add the ON clause to the end of the WHERE clause, connected by
 100425     ** an AND operator.
 100427     if( pRight->pOn ){
 100428       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
 100429       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
 100430       pRight->pOn = 0;
 100433     /* Create extra terms on the WHERE clause for each column named
 100434     ** in the USING clause.  Example: If the two tables to be joined are 
 100435     ** A and B and the USING clause names X, Y, and Z, then add this
 100436     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
 100437     ** Report an error if any column mentioned in the USING clause is
 100438     ** not contained in both tables to be joined.
 100440     if( pRight->pUsing ){
 100441       IdList *pList = pRight->pUsing;
 100442       for(j=0; j<pList->nId; j++){
 100443         char *zName;     /* Name of the term in the USING clause */
 100444         int iLeft;       /* Table on the left with matching column name */
 100445         int iLeftCol;    /* Column number of matching column on the left */
 100446         int iRightCol;   /* Column number of matching column on the right */
 100448         zName = pList->a[j].zName;
 100449         iRightCol = columnIndex(pRightTab, zName);
 100450         if( iRightCol<0
 100451          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
 100453           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
 100454             "not present in both tables", zName);
 100455           return 1;
 100457         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
 100458                      isOuter, &p->pWhere);
 100462   return 0;
 100466 ** Insert code into "v" that will push the record on the top of the
 100467 ** stack into the sorter.
 100469 static void pushOntoSorter(
 100470   Parse *pParse,         /* Parser context */
 100471   ExprList *pOrderBy,    /* The ORDER BY clause */
 100472   Select *pSelect,       /* The whole SELECT statement */
 100473   int regData            /* Register holding data to be sorted */
 100475   Vdbe *v = pParse->pVdbe;
 100476   int nExpr = pOrderBy->nExpr;
 100477   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
 100478   int regRecord = sqlite3GetTempReg(pParse);
 100479   int op;
 100480   sqlite3ExprCacheClear(pParse);
 100481   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
 100482   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
 100483   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
 100484   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
 100485   if( pSelect->selFlags & SF_UseSorter ){
 100486     op = OP_SorterInsert;
 100487   }else{
 100488     op = OP_IdxInsert;
 100490   sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
 100491   sqlite3ReleaseTempReg(pParse, regRecord);
 100492   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
 100493   if( pSelect->iLimit ){
 100494     int addr1, addr2;
 100495     int iLimit;
 100496     if( pSelect->iOffset ){
 100497       iLimit = pSelect->iOffset+1;
 100498     }else{
 100499       iLimit = pSelect->iLimit;
 100501     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v);
 100502     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
 100503     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
 100504     sqlite3VdbeJumpHere(v, addr1);
 100505     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
 100506     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
 100507     sqlite3VdbeJumpHere(v, addr2);
 100512 ** Add code to implement the OFFSET
 100514 static void codeOffset(
 100515   Vdbe *v,          /* Generate code into this VM */
 100516   int iOffset,      /* Register holding the offset counter */
 100517   int iContinue     /* Jump here to skip the current record */
 100519   if( iOffset>0 && iContinue!=0 ){
 100520     int addr;
 100521     sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
 100522     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
 100523     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
 100524     VdbeComment((v, "skip OFFSET records"));
 100525     sqlite3VdbeJumpHere(v, addr);
 100530 ** Add code that will check to make sure the N registers starting at iMem
 100531 ** form a distinct entry.  iTab is a sorting index that holds previously
 100532 ** seen combinations of the N values.  A new entry is made in iTab
 100533 ** if the current N values are new.
 100535 ** A jump to addrRepeat is made and the N+1 values are popped from the
 100536 ** stack if the top N elements are not distinct.
 100538 static void codeDistinct(
 100539   Parse *pParse,     /* Parsing and code generating context */
 100540   int iTab,          /* A sorting index used to test for distinctness */
 100541   int addrRepeat,    /* Jump to here if not distinct */
 100542   int N,             /* Number of elements */
 100543   int iMem           /* First element */
 100545   Vdbe *v;
 100546   int r1;
 100548   v = pParse->pVdbe;
 100549   r1 = sqlite3GetTempReg(pParse);
 100550   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
 100551   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
 100552   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
 100553   sqlite3ReleaseTempReg(pParse, r1);
 100556 #ifndef SQLITE_OMIT_SUBQUERY
 100558 ** Generate an error message when a SELECT is used within a subexpression
 100559 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
 100560 ** column.  We do this in a subroutine because the error used to occur
 100561 ** in multiple places.  (The error only occurs in one place now, but we
 100562 ** retain the subroutine to minimize code disruption.)
 100564 static int checkForMultiColumnSelectError(
 100565   Parse *pParse,       /* Parse context. */
 100566   SelectDest *pDest,   /* Destination of SELECT results */
 100567   int nExpr            /* Number of result columns returned by SELECT */
 100569   int eDest = pDest->eDest;
 100570   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
 100571     sqlite3ErrorMsg(pParse, "only a single result allowed for "
 100572        "a SELECT that is part of an expression");
 100573     return 1;
 100574   }else{
 100575     return 0;
 100578 #endif
 100581 ** An instance of the following object is used to record information about
 100582 ** how to process the DISTINCT keyword, to simplify passing that information
 100583 ** into the selectInnerLoop() routine.
 100585 typedef struct DistinctCtx DistinctCtx;
 100586 struct DistinctCtx {
 100587   u8 isTnct;      /* True if the DISTINCT keyword is present */
 100588   u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
 100589   int tabTnct;    /* Ephemeral table used for DISTINCT processing */
 100590   int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
 100594 ** This routine generates the code for the inside of the inner loop
 100595 ** of a SELECT.
 100597 ** If srcTab is negative, then the pEList expressions
 100598 ** are evaluated in order to get the data for this row.  If srcTab is
 100599 ** zero or more, then data is pulled from srcTab and pEList is used only 
 100600 ** to get number columns and the datatype for each column.
 100602 static void selectInnerLoop(
 100603   Parse *pParse,          /* The parser context */
 100604   Select *p,              /* The complete select statement being coded */
 100605   ExprList *pEList,       /* List of values being extracted */
 100606   int srcTab,             /* Pull data from this table */
 100607   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
 100608   DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
 100609   SelectDest *pDest,      /* How to dispose of the results */
 100610   int iContinue,          /* Jump here to continue with next row */
 100611   int iBreak              /* Jump here to break out of the inner loop */
 100613   Vdbe *v = pParse->pVdbe;
 100614   int i;
 100615   int hasDistinct;        /* True if the DISTINCT keyword is present */
 100616   int regResult;              /* Start of memory holding result set */
 100617   int eDest = pDest->eDest;   /* How to dispose of results */
 100618   int iParm = pDest->iSDParm; /* First argument to disposal method */
 100619   int nResultCol;             /* Number of result columns */
 100621   assert( v );
 100622   assert( pEList!=0 );
 100623   hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
 100624   if( pOrderBy==0 && !hasDistinct ){
 100625     codeOffset(v, p->iOffset, iContinue);
 100628   /* Pull the requested columns.
 100630   nResultCol = pEList->nExpr;
 100632   if( pDest->iSdst==0 ){
 100633     pDest->iSdst = pParse->nMem+1;
 100634     pParse->nMem += nResultCol;
 100635   }else if( pDest->iSdst+nResultCol > pParse->nMem ){
 100636     /* This is an error condition that can result, for example, when a SELECT
 100637     ** on the right-hand side of an INSERT contains more result columns than
 100638     ** there are columns in the table on the left.  The error will be caught
 100639     ** and reported later.  But we need to make sure enough memory is allocated
 100640     ** to avoid other spurious errors in the meantime. */
 100641     pParse->nMem += nResultCol;
 100643   pDest->nSdst = nResultCol;
 100644   regResult = pDest->iSdst;
 100645   if( srcTab>=0 ){
 100646     for(i=0; i<nResultCol; i++){
 100647       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
 100648       VdbeComment((v, "%s", pEList->a[i].zName));
 100650   }else if( eDest!=SRT_Exists ){
 100651     /* If the destination is an EXISTS(...) expression, the actual
 100652     ** values returned by the SELECT are not required.
 100654     sqlite3ExprCodeExprList(pParse, pEList, regResult,
 100655                   (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
 100658   /* If the DISTINCT keyword was present on the SELECT statement
 100659   ** and this row has been seen before, then do not make this row
 100660   ** part of the result.
 100662   if( hasDistinct ){
 100663     switch( pDistinct->eTnctType ){
 100664       case WHERE_DISTINCT_ORDERED: {
 100665         VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
 100666         int iJump;              /* Jump destination */
 100667         int regPrev;            /* Previous row content */
 100669         /* Allocate space for the previous row */
 100670         regPrev = pParse->nMem+1;
 100671         pParse->nMem += nResultCol;
 100673         /* Change the OP_OpenEphemeral coded earlier to an OP_Null
 100674         ** sets the MEM_Cleared bit on the first register of the
 100675         ** previous value.  This will cause the OP_Ne below to always
 100676         ** fail on the first iteration of the loop even if the first
 100677         ** row is all NULLs.
 100679         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 100680         pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
 100681         pOp->opcode = OP_Null;
 100682         pOp->p1 = 1;
 100683         pOp->p2 = regPrev;
 100685         iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
 100686         for(i=0; i<nResultCol; i++){
 100687           CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
 100688           if( i<nResultCol-1 ){
 100689             sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
 100690             VdbeCoverage(v);
 100691           }else{
 100692             sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
 100693             VdbeCoverage(v);
 100695           sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
 100696           sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
 100698         assert( sqlite3VdbeCurrentAddr(v)==iJump );
 100699         sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
 100700         break;
 100703       case WHERE_DISTINCT_UNIQUE: {
 100704         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
 100705         break;
 100708       default: {
 100709         assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
 100710         codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
 100711         break;
 100714     if( pOrderBy==0 ){
 100715       codeOffset(v, p->iOffset, iContinue);
 100719   switch( eDest ){
 100720     /* In this mode, write each query result to the key of the temporary
 100721     ** table iParm.
 100723 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 100724     case SRT_Union: {
 100725       int r1;
 100726       r1 = sqlite3GetTempReg(pParse);
 100727       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
 100728       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 100729       sqlite3ReleaseTempReg(pParse, r1);
 100730       break;
 100733     /* Construct a record from the query result, but instead of
 100734     ** saving that record, use it as a key to delete elements from
 100735     ** the temporary table iParm.
 100737     case SRT_Except: {
 100738       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
 100739       break;
 100741 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 100743     /* Store the result as data using a unique key.
 100745     case SRT_DistTable:
 100746     case SRT_Table:
 100747     case SRT_EphemTab: {
 100748       int r1 = sqlite3GetTempReg(pParse);
 100749       testcase( eDest==SRT_Table );
 100750       testcase( eDest==SRT_EphemTab );
 100751       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
 100752 #ifndef SQLITE_OMIT_CTE
 100753       if( eDest==SRT_DistTable ){
 100754         /* If the destination is DistTable, then cursor (iParm+1) is open
 100755         ** on an ephemeral index. If the current row is already present
 100756         ** in the index, do not write it to the output. If not, add the
 100757         ** current row to the index and proceed with writing it to the
 100758         ** output table as well.  */
 100759         int addr = sqlite3VdbeCurrentAddr(v) + 4;
 100760         sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
 100761         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
 100762         assert( pOrderBy==0 );
 100764 #endif
 100765       if( pOrderBy ){
 100766         pushOntoSorter(pParse, pOrderBy, p, r1);
 100767       }else{
 100768         int r2 = sqlite3GetTempReg(pParse);
 100769         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
 100770         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
 100771         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 100772         sqlite3ReleaseTempReg(pParse, r2);
 100774       sqlite3ReleaseTempReg(pParse, r1);
 100775       break;
 100778 #ifndef SQLITE_OMIT_SUBQUERY
 100779     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 100780     ** then there should be a single item on the stack.  Write this
 100781     ** item into the set table with bogus data.
 100783     case SRT_Set: {
 100784       assert( nResultCol==1 );
 100785       pDest->affSdst =
 100786                   sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 100787       if( pOrderBy ){
 100788         /* At first glance you would think we could optimize out the
 100789         ** ORDER BY in this case since the order of entries in the set
 100790         ** does not matter.  But there might be a LIMIT clause, in which
 100791         ** case the order does matter */
 100792         pushOntoSorter(pParse, pOrderBy, p, regResult);
 100793       }else{
 100794         int r1 = sqlite3GetTempReg(pParse);
 100795         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
 100796         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
 100797         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 100798         sqlite3ReleaseTempReg(pParse, r1);
 100800       break;
 100803     /* If any row exist in the result set, record that fact and abort.
 100805     case SRT_Exists: {
 100806       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
 100807       /* The LIMIT clause will terminate the loop for us */
 100808       break;
 100811     /* If this is a scalar select that is part of an expression, then
 100812     ** store the results in the appropriate memory cell and break out
 100813     ** of the scan loop.
 100815     case SRT_Mem: {
 100816       assert( nResultCol==1 );
 100817       if( pOrderBy ){
 100818         pushOntoSorter(pParse, pOrderBy, p, regResult);
 100819       }else{
 100820         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
 100821         /* The LIMIT clause will jump out of the loop for us */
 100823       break;
 100825 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 100827     case SRT_Coroutine:       /* Send data to a co-routine */
 100828     case SRT_Output: {        /* Return the results */
 100829       testcase( eDest==SRT_Coroutine );
 100830       testcase( eDest==SRT_Output );
 100831       if( pOrderBy ){
 100832         int r1 = sqlite3GetTempReg(pParse);
 100833         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
 100834         pushOntoSorter(pParse, pOrderBy, p, r1);
 100835         sqlite3ReleaseTempReg(pParse, r1);
 100836       }else if( eDest==SRT_Coroutine ){
 100837         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 100838       }else{
 100839         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
 100840         sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
 100842       break;
 100845 #ifndef SQLITE_OMIT_CTE
 100846     /* Write the results into a priority queue that is order according to
 100847     ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
 100848     ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
 100849     ** pSO->nExpr columns, then make sure all keys are unique by adding a
 100850     ** final OP_Sequence column.  The last column is the record as a blob.
 100852     case SRT_DistQueue:
 100853     case SRT_Queue: {
 100854       int nKey;
 100855       int r1, r2, r3;
 100856       int addrTest = 0;
 100857       ExprList *pSO;
 100858       pSO = pDest->pOrderBy;
 100859       assert( pSO );
 100860       nKey = pSO->nExpr;
 100861       r1 = sqlite3GetTempReg(pParse);
 100862       r2 = sqlite3GetTempRange(pParse, nKey+2);
 100863       r3 = r2+nKey+1;
 100864       if( eDest==SRT_DistQueue ){
 100865         /* If the destination is DistQueue, then cursor (iParm+1) is open
 100866         ** on a second ephemeral index that holds all values every previously
 100867         ** added to the queue. */
 100868         addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, 
 100869                                         regResult, nResultCol);
 100870         VdbeCoverage(v);
 100872       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
 100873       if( eDest==SRT_DistQueue ){
 100874         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
 100875         sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 100877       for(i=0; i<nKey; i++){
 100878         sqlite3VdbeAddOp2(v, OP_SCopy,
 100879                           regResult + pSO->a[i].u.x.iOrderByCol - 1,
 100880                           r2+i);
 100882       sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
 100883       sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
 100884       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
 100885       if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
 100886       sqlite3ReleaseTempReg(pParse, r1);
 100887       sqlite3ReleaseTempRange(pParse, r2, nKey+2);
 100888       break;
 100890 #endif /* SQLITE_OMIT_CTE */
 100894 #if !defined(SQLITE_OMIT_TRIGGER)
 100895     /* Discard the results.  This is used for SELECT statements inside
 100896     ** the body of a TRIGGER.  The purpose of such selects is to call
 100897     ** user-defined functions that have side effects.  We do not care
 100898     ** about the actual results of the select.
 100900     default: {
 100901       assert( eDest==SRT_Discard );
 100902       break;
 100904 #endif
 100907   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
 100908   ** there is a sorter, in which case the sorter has already limited
 100909   ** the output for us.
 100911   if( pOrderBy==0 && p->iLimit ){
 100912     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
 100917 ** Allocate a KeyInfo object sufficient for an index of N key columns and
 100918 ** X extra columns.
 100920 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
 100921   KeyInfo *p = sqlite3DbMallocZero(0, 
 100922                    sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
 100923   if( p ){
 100924     p->aSortOrder = (u8*)&p->aColl[N+X];
 100925     p->nField = (u16)N;
 100926     p->nXField = (u16)X;
 100927     p->enc = ENC(db);
 100928     p->db = db;
 100929     p->nRef = 1;
 100930   }else{
 100931     db->mallocFailed = 1;
 100933   return p;
 100937 ** Deallocate a KeyInfo object
 100939 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
 100940   if( p ){
 100941     assert( p->nRef>0 );
 100942     p->nRef--;
 100943     if( p->nRef==0 ) sqlite3DbFree(0, p);
 100948 ** Make a new pointer to a KeyInfo object
 100950 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
 100951   if( p ){
 100952     assert( p->nRef>0 );
 100953     p->nRef++;
 100955   return p;
 100958 #ifdef SQLITE_DEBUG
 100960 ** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
 100961 ** can only be changed if this is just a single reference to the object.
 100963 ** This routine is used only inside of assert() statements.
 100965 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
 100966 #endif /* SQLITE_DEBUG */
 100969 ** Given an expression list, generate a KeyInfo structure that records
 100970 ** the collating sequence for each expression in that expression list.
 100972 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
 100973 ** KeyInfo structure is appropriate for initializing a virtual index to
 100974 ** implement that clause.  If the ExprList is the result set of a SELECT
 100975 ** then the KeyInfo structure is appropriate for initializing a virtual
 100976 ** index to implement a DISTINCT test.
 100978 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
 100979 ** function is responsible for seeing that this structure is eventually
 100980 ** freed.
 100982 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList, int nExtra){
 100983   int nExpr;
 100984   KeyInfo *pInfo;
 100985   struct ExprList_item *pItem;
 100986   sqlite3 *db = pParse->db;
 100987   int i;
 100989   nExpr = pList->nExpr;
 100990   pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra, 1);
 100991   if( pInfo ){
 100992     assert( sqlite3KeyInfoIsWriteable(pInfo) );
 100993     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
 100994       CollSeq *pColl;
 100995       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 100996       if( !pColl ) pColl = db->pDfltColl;
 100997       pInfo->aColl[i] = pColl;
 100998       pInfo->aSortOrder[i] = pItem->sortOrder;
 101001   return pInfo;
 101004 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 101006 ** Name of the connection operator, used for error messages.
 101008 static const char *selectOpName(int id){
 101009   char *z;
 101010   switch( id ){
 101011     case TK_ALL:       z = "UNION ALL";   break;
 101012     case TK_INTERSECT: z = "INTERSECT";   break;
 101013     case TK_EXCEPT:    z = "EXCEPT";      break;
 101014     default:           z = "UNION";       break;
 101016   return z;
 101018 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 101020 #ifndef SQLITE_OMIT_EXPLAIN
 101022 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 101023 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 101024 ** where the caption is of the form:
 101026 **   "USE TEMP B-TREE FOR xxx"
 101028 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
 101029 ** is determined by the zUsage argument.
 101031 static void explainTempTable(Parse *pParse, const char *zUsage){
 101032   if( pParse->explain==2 ){
 101033     Vdbe *v = pParse->pVdbe;
 101034     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
 101035     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 101040 ** Assign expression b to lvalue a. A second, no-op, version of this macro
 101041 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
 101042 ** in sqlite3Select() to assign values to structure member variables that
 101043 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
 101044 ** code with #ifndef directives.
 101046 # define explainSetInteger(a, b) a = b
 101048 #else
 101049 /* No-op versions of the explainXXX() functions and macros. */
 101050 # define explainTempTable(y,z)
 101051 # define explainSetInteger(y,z)
 101052 #endif
 101054 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
 101056 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
 101057 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
 101058 ** where the caption is of one of the two forms:
 101060 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
 101061 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
 101063 ** where iSub1 and iSub2 are the integers passed as the corresponding
 101064 ** function parameters, and op is the text representation of the parameter
 101065 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
 101066 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
 101067 ** false, or the second form if it is true.
 101069 static void explainComposite(
 101070   Parse *pParse,                  /* Parse context */
 101071   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
 101072   int iSub1,                      /* Subquery id 1 */
 101073   int iSub2,                      /* Subquery id 2 */
 101074   int bUseTmp                     /* True if a temp table was used */
 101076   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
 101077   if( pParse->explain==2 ){
 101078     Vdbe *v = pParse->pVdbe;
 101079     char *zMsg = sqlite3MPrintf(
 101080         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
 101081         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
 101083     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
 101086 #else
 101087 /* No-op versions of the explainXXX() functions and macros. */
 101088 # define explainComposite(v,w,x,y,z)
 101089 #endif
 101092 ** If the inner loop was generated using a non-null pOrderBy argument,
 101093 ** then the results were placed in a sorter.  After the loop is terminated
 101094 ** we need to run the sorter and output the results.  The following
 101095 ** routine generates the code needed to do that.
 101097 static void generateSortTail(
 101098   Parse *pParse,    /* Parsing context */
 101099   Select *p,        /* The SELECT statement */
 101100   Vdbe *v,          /* Generate code into this VDBE */
 101101   int nColumn,      /* Number of columns of data */
 101102   SelectDest *pDest /* Write the sorted results here */
 101104   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
 101105   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
 101106   int addr;
 101107   int iTab;
 101108   int pseudoTab = 0;
 101109   ExprList *pOrderBy = p->pOrderBy;
 101111   int eDest = pDest->eDest;
 101112   int iParm = pDest->iSDParm;
 101114   int regRow;
 101115   int regRowid;
 101117   iTab = pOrderBy->iECursor;
 101118   regRow = sqlite3GetTempReg(pParse);
 101119   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 101120     pseudoTab = pParse->nTab++;
 101121     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
 101122     regRowid = 0;
 101123   }else{
 101124     regRowid = sqlite3GetTempReg(pParse);
 101126   if( p->selFlags & SF_UseSorter ){
 101127     int regSortOut = ++pParse->nMem;
 101128     int ptab2 = pParse->nTab++;
 101129     sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
 101130     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
 101131     VdbeCoverage(v);
 101132     codeOffset(v, p->iOffset, addrContinue);
 101133     sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
 101134     sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
 101135     sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 101136   }else{
 101137     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
 101138     codeOffset(v, p->iOffset, addrContinue);
 101139     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
 101141   switch( eDest ){
 101142     case SRT_Table:
 101143     case SRT_EphemTab: {
 101144       testcase( eDest==SRT_Table );
 101145       testcase( eDest==SRT_EphemTab );
 101146       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
 101147       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
 101148       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 101149       break;
 101151 #ifndef SQLITE_OMIT_SUBQUERY
 101152     case SRT_Set: {
 101153       assert( nColumn==1 );
 101154       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
 101155                         &pDest->affSdst, 1);
 101156       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
 101157       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
 101158       break;
 101160     case SRT_Mem: {
 101161       assert( nColumn==1 );
 101162       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
 101163       /* The LIMIT clause will terminate the loop for us */
 101164       break;
 101166 #endif
 101167     default: {
 101168       int i;
 101169       assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
 101170       testcase( eDest==SRT_Output );
 101171       testcase( eDest==SRT_Coroutine );
 101172       for(i=0; i<nColumn; i++){
 101173         assert( regRow!=pDest->iSdst+i );
 101174         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
 101175         if( i==0 ){
 101176           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 101179       if( eDest==SRT_Output ){
 101180         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
 101181         sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
 101182       }else{
 101183         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 101185       break;
 101188   sqlite3ReleaseTempReg(pParse, regRow);
 101189   sqlite3ReleaseTempReg(pParse, regRowid);
 101191   /* The bottom of the loop
 101193   sqlite3VdbeResolveLabel(v, addrContinue);
 101194   if( p->selFlags & SF_UseSorter ){
 101195     sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
 101196   }else{
 101197     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
 101199   sqlite3VdbeResolveLabel(v, addrBreak);
 101200   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
 101201     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
 101206 ** Return a pointer to a string containing the 'declaration type' of the
 101207 ** expression pExpr. The string may be treated as static by the caller.
 101209 ** Also try to estimate the size of the returned value and return that
 101210 ** result in *pEstWidth.
 101212 ** The declaration type is the exact datatype definition extracted from the
 101213 ** original CREATE TABLE statement if the expression is a column. The
 101214 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
 101215 ** is considered a column can be complex in the presence of subqueries. The
 101216 ** result-set expression in all of the following SELECT statements is 
 101217 ** considered a column by this function.
 101219 **   SELECT col FROM tbl;
 101220 **   SELECT (SELECT col FROM tbl;
 101221 **   SELECT (SELECT col FROM tbl);
 101222 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
 101224 ** The declaration type for any expression other than a column is NULL.
 101226 ** This routine has either 3 or 6 parameters depending on whether or not
 101227 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
 101229 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 101230 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
 101231 static const char *columnTypeImpl(
 101232   NameContext *pNC, 
 101233   Expr *pExpr,
 101234   const char **pzOrigDb,
 101235   const char **pzOrigTab,
 101236   const char **pzOrigCol,
 101237   u8 *pEstWidth
 101239   char const *zOrigDb = 0;
 101240   char const *zOrigTab = 0;
 101241   char const *zOrigCol = 0;
 101242 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
 101243 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
 101244 static const char *columnTypeImpl(
 101245   NameContext *pNC, 
 101246   Expr *pExpr,
 101247   u8 *pEstWidth
 101249 #endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
 101250   char const *zType = 0;
 101251   int j;
 101252   u8 estWidth = 1;
 101254   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
 101255   switch( pExpr->op ){
 101256     case TK_AGG_COLUMN:
 101257     case TK_COLUMN: {
 101258       /* The expression is a column. Locate the table the column is being
 101259       ** extracted from in NameContext.pSrcList. This table may be real
 101260       ** database table or a subquery.
 101262       Table *pTab = 0;            /* Table structure column is extracted from */
 101263       Select *pS = 0;             /* Select the column is extracted from */
 101264       int iCol = pExpr->iColumn;  /* Index of column in pTab */
 101265       testcase( pExpr->op==TK_AGG_COLUMN );
 101266       testcase( pExpr->op==TK_COLUMN );
 101267       while( pNC && !pTab ){
 101268         SrcList *pTabList = pNC->pSrcList;
 101269         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
 101270         if( j<pTabList->nSrc ){
 101271           pTab = pTabList->a[j].pTab;
 101272           pS = pTabList->a[j].pSelect;
 101273         }else{
 101274           pNC = pNC->pNext;
 101278       if( pTab==0 ){
 101279         /* At one time, code such as "SELECT new.x" within a trigger would
 101280         ** cause this condition to run.  Since then, we have restructured how
 101281         ** trigger code is generated and so this condition is no longer 
 101282         ** possible. However, it can still be true for statements like
 101283         ** the following:
 101285         **   CREATE TABLE t1(col INTEGER);
 101286         **   SELECT (SELECT t1.col) FROM FROM t1;
 101288         ** when columnType() is called on the expression "t1.col" in the 
 101289         ** sub-select. In this case, set the column type to NULL, even
 101290         ** though it should really be "INTEGER".
 101292         ** This is not a problem, as the column type of "t1.col" is never
 101293         ** used. When columnType() is called on the expression 
 101294         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
 101295         ** branch below.  */
 101296         break;
 101299       assert( pTab && pExpr->pTab==pTab );
 101300       if( pS ){
 101301         /* The "table" is actually a sub-select or a view in the FROM clause
 101302         ** of the SELECT statement. Return the declaration type and origin
 101303         ** data for the result-set column of the sub-select.
 101305         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
 101306           /* If iCol is less than zero, then the expression requests the
 101307           ** rowid of the sub-select or view. This expression is legal (see 
 101308           ** test case misc2.2.2) - it always evaluates to NULL.
 101310           NameContext sNC;
 101311           Expr *p = pS->pEList->a[iCol].pExpr;
 101312           sNC.pSrcList = pS->pSrc;
 101313           sNC.pNext = pNC;
 101314           sNC.pParse = pNC->pParse;
 101315           zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
 101317       }else if( pTab->pSchema ){
 101318         /* A real table */
 101319         assert( !pS );
 101320         if( iCol<0 ) iCol = pTab->iPKey;
 101321         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 101322 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 101323         if( iCol<0 ){
 101324           zType = "INTEGER";
 101325           zOrigCol = "rowid";
 101326         }else{
 101327           zType = pTab->aCol[iCol].zType;
 101328           zOrigCol = pTab->aCol[iCol].zName;
 101329           estWidth = pTab->aCol[iCol].szEst;
 101331         zOrigTab = pTab->zName;
 101332         if( pNC->pParse ){
 101333           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
 101334           zOrigDb = pNC->pParse->db->aDb[iDb].zName;
 101336 #else
 101337         if( iCol<0 ){
 101338           zType = "INTEGER";
 101339         }else{
 101340           zType = pTab->aCol[iCol].zType;
 101341           estWidth = pTab->aCol[iCol].szEst;
 101343 #endif
 101345       break;
 101347 #ifndef SQLITE_OMIT_SUBQUERY
 101348     case TK_SELECT: {
 101349       /* The expression is a sub-select. Return the declaration type and
 101350       ** origin info for the single column in the result set of the SELECT
 101351       ** statement.
 101353       NameContext sNC;
 101354       Select *pS = pExpr->x.pSelect;
 101355       Expr *p = pS->pEList->a[0].pExpr;
 101356       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 101357       sNC.pSrcList = pS->pSrc;
 101358       sNC.pNext = pNC;
 101359       sNC.pParse = pNC->pParse;
 101360       zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); 
 101361       break;
 101363 #endif
 101366 #ifdef SQLITE_ENABLE_COLUMN_METADATA  
 101367   if( pzOrigDb ){
 101368     assert( pzOrigTab && pzOrigCol );
 101369     *pzOrigDb = zOrigDb;
 101370     *pzOrigTab = zOrigTab;
 101371     *pzOrigCol = zOrigCol;
 101373 #endif
 101374   if( pEstWidth ) *pEstWidth = estWidth;
 101375   return zType;
 101379 ** Generate code that will tell the VDBE the declaration types of columns
 101380 ** in the result set.
 101382 static void generateColumnTypes(
 101383   Parse *pParse,      /* Parser context */
 101384   SrcList *pTabList,  /* List of tables */
 101385   ExprList *pEList    /* Expressions defining the result set */
 101387 #ifndef SQLITE_OMIT_DECLTYPE
 101388   Vdbe *v = pParse->pVdbe;
 101389   int i;
 101390   NameContext sNC;
 101391   sNC.pSrcList = pTabList;
 101392   sNC.pParse = pParse;
 101393   for(i=0; i<pEList->nExpr; i++){
 101394     Expr *p = pEList->a[i].pExpr;
 101395     const char *zType;
 101396 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 101397     const char *zOrigDb = 0;
 101398     const char *zOrigTab = 0;
 101399     const char *zOrigCol = 0;
 101400     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
 101402     /* The vdbe must make its own copy of the column-type and other 
 101403     ** column specific strings, in case the schema is reset before this
 101404     ** virtual machine is deleted.
 101406     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
 101407     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
 101408     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
 101409 #else
 101410     zType = columnType(&sNC, p, 0, 0, 0, 0);
 101411 #endif
 101412     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
 101414 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
 101418 ** Generate code that will tell the VDBE the names of columns
 101419 ** in the result set.  This information is used to provide the
 101420 ** azCol[] values in the callback.
 101422 static void generateColumnNames(
 101423   Parse *pParse,      /* Parser context */
 101424   SrcList *pTabList,  /* List of tables */
 101425   ExprList *pEList    /* Expressions defining the result set */
 101427   Vdbe *v = pParse->pVdbe;
 101428   int i, j;
 101429   sqlite3 *db = pParse->db;
 101430   int fullNames, shortNames;
 101432 #ifndef SQLITE_OMIT_EXPLAIN
 101433   /* If this is an EXPLAIN, skip this step */
 101434   if( pParse->explain ){
 101435     return;
 101437 #endif
 101439   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
 101440   pParse->colNamesSet = 1;
 101441   fullNames = (db->flags & SQLITE_FullColNames)!=0;
 101442   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
 101443   sqlite3VdbeSetNumCols(v, pEList->nExpr);
 101444   for(i=0; i<pEList->nExpr; i++){
 101445     Expr *p;
 101446     p = pEList->a[i].pExpr;
 101447     if( NEVER(p==0) ) continue;
 101448     if( pEList->a[i].zName ){
 101449       char *zName = pEList->a[i].zName;
 101450       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
 101451     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
 101452       Table *pTab;
 101453       char *zCol;
 101454       int iCol = p->iColumn;
 101455       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
 101456         if( pTabList->a[j].iCursor==p->iTable ) break;
 101458       assert( j<pTabList->nSrc );
 101459       pTab = pTabList->a[j].pTab;
 101460       if( iCol<0 ) iCol = pTab->iPKey;
 101461       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 101462       if( iCol<0 ){
 101463         zCol = "rowid";
 101464       }else{
 101465         zCol = pTab->aCol[iCol].zName;
 101467       if( !shortNames && !fullNames ){
 101468         sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
 101469             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
 101470       }else if( fullNames ){
 101471         char *zName = 0;
 101472         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
 101473         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
 101474       }else{
 101475         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
 101477     }else{
 101478       const char *z = pEList->a[i].zSpan;
 101479       z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
 101480       sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
 101483   generateColumnTypes(pParse, pTabList, pEList);
 101487 ** Given a an expression list (which is really the list of expressions
 101488 ** that form the result set of a SELECT statement) compute appropriate
 101489 ** column names for a table that would hold the expression list.
 101491 ** All column names will be unique.
 101493 ** Only the column names are computed.  Column.zType, Column.zColl,
 101494 ** and other fields of Column are zeroed.
 101496 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
 101497 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
 101499 static int selectColumnsFromExprList(
 101500   Parse *pParse,          /* Parsing context */
 101501   ExprList *pEList,       /* Expr list from which to derive column names */
 101502   i16 *pnCol,             /* Write the number of columns here */
 101503   Column **paCol          /* Write the new column list here */
 101505   sqlite3 *db = pParse->db;   /* Database connection */
 101506   int i, j;                   /* Loop counters */
 101507   int cnt;                    /* Index added to make the name unique */
 101508   Column *aCol, *pCol;        /* For looping over result columns */
 101509   int nCol;                   /* Number of columns in the result set */
 101510   Expr *p;                    /* Expression for a single result column */
 101511   char *zName;                /* Column name */
 101512   int nName;                  /* Size of name in zName[] */
 101514   if( pEList ){
 101515     nCol = pEList->nExpr;
 101516     aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
 101517     testcase( aCol==0 );
 101518   }else{
 101519     nCol = 0;
 101520     aCol = 0;
 101522   *pnCol = nCol;
 101523   *paCol = aCol;
 101525   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
 101526     /* Get an appropriate name for the column
 101528     p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
 101529     if( (zName = pEList->a[i].zName)!=0 ){
 101530       /* If the column contains an "AS <name>" phrase, use <name> as the name */
 101531       zName = sqlite3DbStrDup(db, zName);
 101532     }else{
 101533       Expr *pColExpr = p;  /* The expression that is the result column name */
 101534       Table *pTab;         /* Table associated with this expression */
 101535       while( pColExpr->op==TK_DOT ){
 101536         pColExpr = pColExpr->pRight;
 101537         assert( pColExpr!=0 );
 101539       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
 101540         /* For columns use the column name name */
 101541         int iCol = pColExpr->iColumn;
 101542         pTab = pColExpr->pTab;
 101543         if( iCol<0 ) iCol = pTab->iPKey;
 101544         zName = sqlite3MPrintf(db, "%s",
 101545                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
 101546       }else if( pColExpr->op==TK_ID ){
 101547         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
 101548         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
 101549       }else{
 101550         /* Use the original text of the column expression as its name */
 101551         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
 101554     if( db->mallocFailed ){
 101555       sqlite3DbFree(db, zName);
 101556       break;
 101559     /* Make sure the column name is unique.  If the name is not unique,
 101560     ** append a integer to the name so that it becomes unique.
 101562     nName = sqlite3Strlen30(zName);
 101563     for(j=cnt=0; j<i; j++){
 101564       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
 101565         char *zNewName;
 101566         int k;
 101567         for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
 101568         if( k>=0 && zName[k]==':' ) nName = k;
 101569         zName[nName] = 0;
 101570         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
 101571         sqlite3DbFree(db, zName);
 101572         zName = zNewName;
 101573         j = -1;
 101574         if( zName==0 ) break;
 101577     pCol->zName = zName;
 101579   if( db->mallocFailed ){
 101580     for(j=0; j<i; j++){
 101581       sqlite3DbFree(db, aCol[j].zName);
 101583     sqlite3DbFree(db, aCol);
 101584     *paCol = 0;
 101585     *pnCol = 0;
 101586     return SQLITE_NOMEM;
 101588   return SQLITE_OK;
 101592 ** Add type and collation information to a column list based on
 101593 ** a SELECT statement.
 101595 ** The column list presumably came from selectColumnNamesFromExprList().
 101596 ** The column list has only names, not types or collations.  This
 101597 ** routine goes through and adds the types and collations.
 101599 ** This routine requires that all identifiers in the SELECT
 101600 ** statement be resolved.
 101602 static void selectAddColumnTypeAndCollation(
 101603   Parse *pParse,        /* Parsing contexts */
 101604   Table *pTab,          /* Add column type information to this table */
 101605   Select *pSelect       /* SELECT used to determine types and collations */
 101607   sqlite3 *db = pParse->db;
 101608   NameContext sNC;
 101609   Column *pCol;
 101610   CollSeq *pColl;
 101611   int i;
 101612   Expr *p;
 101613   struct ExprList_item *a;
 101614   u64 szAll = 0;
 101616   assert( pSelect!=0 );
 101617   assert( (pSelect->selFlags & SF_Resolved)!=0 );
 101618   assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
 101619   if( db->mallocFailed ) return;
 101620   memset(&sNC, 0, sizeof(sNC));
 101621   sNC.pSrcList = pSelect->pSrc;
 101622   a = pSelect->pEList->a;
 101623   for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
 101624     p = a[i].pExpr;
 101625     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
 101626     szAll += pCol->szEst;
 101627     pCol->affinity = sqlite3ExprAffinity(p);
 101628     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
 101629     pColl = sqlite3ExprCollSeq(pParse, p);
 101630     if( pColl ){
 101631       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
 101634   pTab->szTabRow = sqlite3LogEst(szAll*4);
 101638 ** Given a SELECT statement, generate a Table structure that describes
 101639 ** the result set of that SELECT.
 101641 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
 101642   Table *pTab;
 101643   sqlite3 *db = pParse->db;
 101644   int savedFlags;
 101646   savedFlags = db->flags;
 101647   db->flags &= ~SQLITE_FullColNames;
 101648   db->flags |= SQLITE_ShortColNames;
 101649   sqlite3SelectPrep(pParse, pSelect, 0);
 101650   if( pParse->nErr ) return 0;
 101651   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
 101652   db->flags = savedFlags;
 101653   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
 101654   if( pTab==0 ){
 101655     return 0;
 101657   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
 101658   ** is disabled */
 101659   assert( db->lookaside.bEnabled==0 );
 101660   pTab->nRef = 1;
 101661   pTab->zName = 0;
 101662   pTab->nRowEst = 1048576;
 101663   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
 101664   selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
 101665   pTab->iPKey = -1;
 101666   if( db->mallocFailed ){
 101667     sqlite3DeleteTable(db, pTab);
 101668     return 0;
 101670   return pTab;
 101674 ** Get a VDBE for the given parser context.  Create a new one if necessary.
 101675 ** If an error occurs, return NULL and leave a message in pParse.
 101677 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
 101678   Vdbe *v = pParse->pVdbe;
 101679   if( v==0 ){
 101680     v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
 101681     if( v ) sqlite3VdbeAddOp0(v, OP_Init);
 101682     if( pParse->pToplevel==0
 101683      && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
 101685       pParse->okConstFactor = 1;
 101689   return v;
 101694 ** Compute the iLimit and iOffset fields of the SELECT based on the
 101695 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
 101696 ** that appear in the original SQL statement after the LIMIT and OFFSET
 101697 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
 101698 ** are the integer memory register numbers for counters used to compute 
 101699 ** the limit and offset.  If there is no limit and/or offset, then 
 101700 ** iLimit and iOffset are negative.
 101702 ** This routine changes the values of iLimit and iOffset only if
 101703 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
 101704 ** iOffset should have been preset to appropriate default values (zero)
 101705 ** prior to calling this routine.
 101707 ** The iOffset register (if it exists) is initialized to the value
 101708 ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
 101709 ** iOffset+1 is initialized to LIMIT+OFFSET.
 101711 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
 101712 ** redefined.  The UNION ALL operator uses this property to force
 101713 ** the reuse of the same limit and offset registers across multiple
 101714 ** SELECT statements.
 101716 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
 101717   Vdbe *v = 0;
 101718   int iLimit = 0;
 101719   int iOffset;
 101720   int addr1, n;
 101721   if( p->iLimit ) return;
 101724   ** "LIMIT -1" always shows all rows.  There is some
 101725   ** controversy about what the correct behavior should be.
 101726   ** The current implementation interprets "LIMIT 0" to mean
 101727   ** no rows.
 101729   sqlite3ExprCacheClear(pParse);
 101730   assert( p->pOffset==0 || p->pLimit!=0 );
 101731   if( p->pLimit ){
 101732     p->iLimit = iLimit = ++pParse->nMem;
 101733     v = sqlite3GetVdbe(pParse);
 101734     assert( v!=0 );
 101735     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
 101736       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
 101737       VdbeComment((v, "LIMIT counter"));
 101738       if( n==0 ){
 101739         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
 101740       }else if( n>=0 && p->nSelectRow>(u64)n ){
 101741         p->nSelectRow = n;
 101743     }else{
 101744       sqlite3ExprCode(pParse, p->pLimit, iLimit);
 101745       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
 101746       VdbeComment((v, "LIMIT counter"));
 101747       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); VdbeCoverage(v);
 101749     if( p->pOffset ){
 101750       p->iOffset = iOffset = ++pParse->nMem;
 101751       pParse->nMem++;   /* Allocate an extra register for limit+offset */
 101752       sqlite3ExprCode(pParse, p->pOffset, iOffset);
 101753       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
 101754       VdbeComment((v, "OFFSET counter"));
 101755       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
 101756       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
 101757       sqlite3VdbeJumpHere(v, addr1);
 101758       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
 101759       VdbeComment((v, "LIMIT+OFFSET"));
 101760       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
 101761       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
 101762       sqlite3VdbeJumpHere(v, addr1);
 101767 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 101769 ** Return the appropriate collating sequence for the iCol-th column of
 101770 ** the result set for the compound-select statement "p".  Return NULL if
 101771 ** the column has no default collating sequence.
 101773 ** The collating sequence for the compound select is taken from the
 101774 ** left-most term of the select that has a collating sequence.
 101776 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
 101777   CollSeq *pRet;
 101778   if( p->pPrior ){
 101779     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
 101780   }else{
 101781     pRet = 0;
 101783   assert( iCol>=0 );
 101784   if( pRet==0 && iCol<p->pEList->nExpr ){
 101785     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
 101787   return pRet;
 101791 ** The select statement passed as the second parameter is a compound SELECT
 101792 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
 101793 ** structure suitable for implementing the ORDER BY.
 101795 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
 101796 ** function is responsible for ensuring that this structure is eventually
 101797 ** freed.
 101799 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
 101800   ExprList *pOrderBy = p->pOrderBy;
 101801   int nOrderBy = p->pOrderBy->nExpr;
 101802   sqlite3 *db = pParse->db;
 101803   KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
 101804   if( pRet ){
 101805     int i;
 101806     for(i=0; i<nOrderBy; i++){
 101807       struct ExprList_item *pItem = &pOrderBy->a[i];
 101808       Expr *pTerm = pItem->pExpr;
 101809       CollSeq *pColl;
 101811       if( pTerm->flags & EP_Collate ){
 101812         pColl = sqlite3ExprCollSeq(pParse, pTerm);
 101813       }else{
 101814         pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
 101815         if( pColl==0 ) pColl = db->pDfltColl;
 101816         pOrderBy->a[i].pExpr =
 101817           sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
 101819       assert( sqlite3KeyInfoIsWriteable(pRet) );
 101820       pRet->aColl[i] = pColl;
 101821       pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
 101825   return pRet;
 101828 #ifndef SQLITE_OMIT_CTE
 101830 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
 101831 ** query of the form:
 101833 **   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
 101834 **                         \___________/             \_______________/
 101835 **                           p->pPrior                      p
 101838 ** There is exactly one reference to the recursive-table in the FROM clause
 101839 ** of recursive-query, marked with the SrcList->a[].isRecursive flag.
 101841 ** The setup-query runs once to generate an initial set of rows that go
 101842 ** into a Queue table.  Rows are extracted from the Queue table one by
 101843 ** one.  Each row extracted from Queue is output to pDest.  Then the single
 101844 ** extracted row (now in the iCurrent table) becomes the content of the
 101845 ** recursive-table for a recursive-query run.  The output of the recursive-query
 101846 ** is added back into the Queue table.  Then another row is extracted from Queue
 101847 ** and the iteration continues until the Queue table is empty.
 101849 ** If the compound query operator is UNION then no duplicate rows are ever
 101850 ** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
 101851 ** that have ever been inserted into Queue and causes duplicates to be
 101852 ** discarded.  If the operator is UNION ALL, then duplicates are allowed.
 101854 ** If the query has an ORDER BY, then entries in the Queue table are kept in
 101855 ** ORDER BY order and the first entry is extracted for each cycle.  Without
 101856 ** an ORDER BY, the Queue table is just a FIFO.
 101858 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
 101859 ** have been output to pDest.  A LIMIT of zero means to output no rows and a
 101860 ** negative LIMIT means to output all rows.  If there is also an OFFSET clause
 101861 ** with a positive value, then the first OFFSET outputs are discarded rather
 101862 ** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
 101863 ** rows have been skipped.
 101865 static void generateWithRecursiveQuery(
 101866   Parse *pParse,        /* Parsing context */
 101867   Select *p,            /* The recursive SELECT to be coded */
 101868   SelectDest *pDest     /* What to do with query results */
 101870   SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
 101871   int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
 101872   Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
 101873   Select *pSetup = p->pPrior;   /* The setup query */
 101874   int addrTop;                  /* Top of the loop */
 101875   int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
 101876   int iCurrent = 0;             /* The Current table */
 101877   int regCurrent;               /* Register holding Current table */
 101878   int iQueue;                   /* The Queue table */
 101879   int iDistinct = 0;            /* To ensure unique results if UNION */
 101880   int eDest = SRT_Table;        /* How to write to Queue */
 101881   SelectDest destQueue;         /* SelectDest targetting the Queue table */
 101882   int i;                        /* Loop counter */
 101883   int rc;                       /* Result code */
 101884   ExprList *pOrderBy;           /* The ORDER BY clause */
 101885   Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
 101886   int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
 101888   /* Obtain authorization to do a recursive query */
 101889   if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
 101891   /* Process the LIMIT and OFFSET clauses, if they exist */
 101892   addrBreak = sqlite3VdbeMakeLabel(v);
 101893   computeLimitRegisters(pParse, p, addrBreak);
 101894   pLimit = p->pLimit;
 101895   pOffset = p->pOffset;
 101896   regLimit = p->iLimit;
 101897   regOffset = p->iOffset;
 101898   p->pLimit = p->pOffset = 0;
 101899   p->iLimit = p->iOffset = 0;
 101900   pOrderBy = p->pOrderBy;
 101902   /* Locate the cursor number of the Current table */
 101903   for(i=0; ALWAYS(i<pSrc->nSrc); i++){
 101904     if( pSrc->a[i].isRecursive ){
 101905       iCurrent = pSrc->a[i].iCursor;
 101906       break;
 101910   /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
 101911   ** the Distinct table must be exactly one greater than Queue in order
 101912   ** for the SRT_DistTable and SRT_DistQueue destinations to work. */
 101913   iQueue = pParse->nTab++;
 101914   if( p->op==TK_UNION ){
 101915     eDest = pOrderBy ? SRT_DistQueue : SRT_DistTable;
 101916     iDistinct = pParse->nTab++;
 101917   }else{
 101918     eDest = pOrderBy ? SRT_Queue : SRT_Table;
 101920   sqlite3SelectDestInit(&destQueue, eDest, iQueue);
 101922   /* Allocate cursors for Current, Queue, and Distinct. */
 101923   regCurrent = ++pParse->nMem;
 101924   sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
 101925   if( pOrderBy ){
 101926     KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
 101927     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
 101928                       (char*)pKeyInfo, P4_KEYINFO);
 101929     destQueue.pOrderBy = pOrderBy;
 101930   }else{
 101931     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
 101933   VdbeComment((v, "Queue table"));
 101934   if( iDistinct ){
 101935     p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
 101936     p->selFlags |= SF_UsesEphemeral;
 101939   /* Detach the ORDER BY clause from the compound SELECT */
 101940   p->pOrderBy = 0;
 101942   /* Store the results of the setup-query in Queue. */
 101943   pSetup->pNext = 0;
 101944   rc = sqlite3Select(pParse, pSetup, &destQueue);
 101945   pSetup->pNext = p;
 101946   if( rc ) goto end_of_recursive_query;
 101948   /* Find the next row in the Queue and output that row */
 101949   addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
 101951   /* Transfer the next row in Queue over to Current */
 101952   sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
 101953   if( pOrderBy ){
 101954     sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
 101955   }else{
 101956     sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
 101958   sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
 101960   /* Output the single row in Current */
 101961   addrCont = sqlite3VdbeMakeLabel(v);
 101962   codeOffset(v, regOffset, addrCont);
 101963   selectInnerLoop(pParse, p, p->pEList, iCurrent,
 101964       0, 0, pDest, addrCont, addrBreak);
 101965   if( regLimit ){
 101966     sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
 101967     VdbeCoverage(v);
 101969   sqlite3VdbeResolveLabel(v, addrCont);
 101971   /* Execute the recursive SELECT taking the single row in Current as
 101972   ** the value for the recursive-table. Store the results in the Queue.
 101974   p->pPrior = 0;
 101975   sqlite3Select(pParse, p, &destQueue);
 101976   assert( p->pPrior==0 );
 101977   p->pPrior = pSetup;
 101979   /* Keep running the loop until the Queue is empty */
 101980   sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
 101981   sqlite3VdbeResolveLabel(v, addrBreak);
 101983 end_of_recursive_query:
 101984   p->pOrderBy = pOrderBy;
 101985   p->pLimit = pLimit;
 101986   p->pOffset = pOffset;
 101987   return;
 101989 #endif /* SQLITE_OMIT_CTE */
 101991 /* Forward references */
 101992 static int multiSelectOrderBy(
 101993   Parse *pParse,        /* Parsing context */
 101994   Select *p,            /* The right-most of SELECTs to be coded */
 101995   SelectDest *pDest     /* What to do with query results */
 102000 ** This routine is called to process a compound query form from
 102001 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
 102002 ** INTERSECT
 102004 ** "p" points to the right-most of the two queries.  the query on the
 102005 ** left is p->pPrior.  The left query could also be a compound query
 102006 ** in which case this routine will be called recursively. 
 102008 ** The results of the total query are to be written into a destination
 102009 ** of type eDest with parameter iParm.
 102011 ** Example 1:  Consider a three-way compound SQL statement.
 102013 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
 102015 ** This statement is parsed up as follows:
 102017 **     SELECT c FROM t3
 102018 **      |
 102019 **      `----->  SELECT b FROM t2
 102020 **                |
 102021 **                `------>  SELECT a FROM t1
 102023 ** The arrows in the diagram above represent the Select.pPrior pointer.
 102024 ** So if this routine is called with p equal to the t3 query, then
 102025 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
 102027 ** Notice that because of the way SQLite parses compound SELECTs, the
 102028 ** individual selects always group from left to right.
 102030 static int multiSelect(
 102031   Parse *pParse,        /* Parsing context */
 102032   Select *p,            /* The right-most of SELECTs to be coded */
 102033   SelectDest *pDest     /* What to do with query results */
 102035   int rc = SQLITE_OK;   /* Success code from a subroutine */
 102036   Select *pPrior;       /* Another SELECT immediately to our left */
 102037   Vdbe *v;              /* Generate code to this VDBE */
 102038   SelectDest dest;      /* Alternative data destination */
 102039   Select *pDelete = 0;  /* Chain of simple selects to delete */
 102040   sqlite3 *db;          /* Database connection */
 102041 #ifndef SQLITE_OMIT_EXPLAIN
 102042   int iSub1 = 0;        /* EQP id of left-hand query */
 102043   int iSub2 = 0;        /* EQP id of right-hand query */
 102044 #endif
 102046   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
 102047   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
 102049   assert( p && p->pPrior );  /* Calling function guarantees this much */
 102050   assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
 102051   db = pParse->db;
 102052   pPrior = p->pPrior;
 102053   dest = *pDest;
 102054   if( pPrior->pOrderBy ){
 102055     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
 102056       selectOpName(p->op));
 102057     rc = 1;
 102058     goto multi_select_end;
 102060   if( pPrior->pLimit ){
 102061     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
 102062       selectOpName(p->op));
 102063     rc = 1;
 102064     goto multi_select_end;
 102067   v = sqlite3GetVdbe(pParse);
 102068   assert( v!=0 );  /* The VDBE already created by calling function */
 102070   /* Create the destination temporary table if necessary
 102072   if( dest.eDest==SRT_EphemTab ){
 102073     assert( p->pEList );
 102074     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
 102075     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 102076     dest.eDest = SRT_Table;
 102079   /* Make sure all SELECTs in the statement have the same number of elements
 102080   ** in their result sets.
 102082   assert( p->pEList && pPrior->pEList );
 102083   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
 102084     if( p->selFlags & SF_Values ){
 102085       sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
 102086     }else{
 102087       sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
 102088         " do not have the same number of result columns", selectOpName(p->op));
 102090     rc = 1;
 102091     goto multi_select_end;
 102094 #ifndef SQLITE_OMIT_CTE
 102095   if( p->selFlags & SF_Recursive ){
 102096     generateWithRecursiveQuery(pParse, p, &dest);
 102097   }else
 102098 #endif
 102100   /* Compound SELECTs that have an ORDER BY clause are handled separately.
 102102   if( p->pOrderBy ){
 102103     return multiSelectOrderBy(pParse, p, pDest);
 102104   }else
 102106   /* Generate code for the left and right SELECT statements.
 102108   switch( p->op ){
 102109     case TK_ALL: {
 102110       int addr = 0;
 102111       int nLimit;
 102112       assert( !pPrior->pLimit );
 102113       pPrior->iLimit = p->iLimit;
 102114       pPrior->iOffset = p->iOffset;
 102115       pPrior->pLimit = p->pLimit;
 102116       pPrior->pOffset = p->pOffset;
 102117       explainSetInteger(iSub1, pParse->iNextSelectId);
 102118       rc = sqlite3Select(pParse, pPrior, &dest);
 102119       p->pLimit = 0;
 102120       p->pOffset = 0;
 102121       if( rc ){
 102122         goto multi_select_end;
 102124       p->pPrior = 0;
 102125       p->iLimit = pPrior->iLimit;
 102126       p->iOffset = pPrior->iOffset;
 102127       if( p->iLimit ){
 102128         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); VdbeCoverage(v);
 102129         VdbeComment((v, "Jump ahead if LIMIT reached"));
 102131       explainSetInteger(iSub2, pParse->iNextSelectId);
 102132       rc = sqlite3Select(pParse, p, &dest);
 102133       testcase( rc!=SQLITE_OK );
 102134       pDelete = p->pPrior;
 102135       p->pPrior = pPrior;
 102136       p->nSelectRow += pPrior->nSelectRow;
 102137       if( pPrior->pLimit
 102138        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
 102139        && nLimit>0 && p->nSelectRow > (u64)nLimit 
 102141         p->nSelectRow = nLimit;
 102143       if( addr ){
 102144         sqlite3VdbeJumpHere(v, addr);
 102146       break;
 102148     case TK_EXCEPT:
 102149     case TK_UNION: {
 102150       int unionTab;    /* Cursor number of the temporary table holding result */
 102151       u8 op = 0;       /* One of the SRT_ operations to apply to self */
 102152       int priorOp;     /* The SRT_ operation to apply to prior selects */
 102153       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
 102154       int addr;
 102155       SelectDest uniondest;
 102157       testcase( p->op==TK_EXCEPT );
 102158       testcase( p->op==TK_UNION );
 102159       priorOp = SRT_Union;
 102160       if( dest.eDest==priorOp ){
 102161         /* We can reuse a temporary table generated by a SELECT to our
 102162         ** right.
 102164         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
 102165         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
 102166         unionTab = dest.iSDParm;
 102167       }else{
 102168         /* We will need to create our own temporary table to hold the
 102169         ** intermediate results.
 102171         unionTab = pParse->nTab++;
 102172         assert( p->pOrderBy==0 );
 102173         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
 102174         assert( p->addrOpenEphm[0] == -1 );
 102175         p->addrOpenEphm[0] = addr;
 102176         findRightmost(p)->selFlags |= SF_UsesEphemeral;
 102177         assert( p->pEList );
 102180       /* Code the SELECT statements to our left
 102182       assert( !pPrior->pOrderBy );
 102183       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
 102184       explainSetInteger(iSub1, pParse->iNextSelectId);
 102185       rc = sqlite3Select(pParse, pPrior, &uniondest);
 102186       if( rc ){
 102187         goto multi_select_end;
 102190       /* Code the current SELECT statement
 102192       if( p->op==TK_EXCEPT ){
 102193         op = SRT_Except;
 102194       }else{
 102195         assert( p->op==TK_UNION );
 102196         op = SRT_Union;
 102198       p->pPrior = 0;
 102199       pLimit = p->pLimit;
 102200       p->pLimit = 0;
 102201       pOffset = p->pOffset;
 102202       p->pOffset = 0;
 102203       uniondest.eDest = op;
 102204       explainSetInteger(iSub2, pParse->iNextSelectId);
 102205       rc = sqlite3Select(pParse, p, &uniondest);
 102206       testcase( rc!=SQLITE_OK );
 102207       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
 102208       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
 102209       sqlite3ExprListDelete(db, p->pOrderBy);
 102210       pDelete = p->pPrior;
 102211       p->pPrior = pPrior;
 102212       p->pOrderBy = 0;
 102213       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
 102214       sqlite3ExprDelete(db, p->pLimit);
 102215       p->pLimit = pLimit;
 102216       p->pOffset = pOffset;
 102217       p->iLimit = 0;
 102218       p->iOffset = 0;
 102220       /* Convert the data in the temporary table into whatever form
 102221       ** it is that we currently need.
 102223       assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
 102224       if( dest.eDest!=priorOp ){
 102225         int iCont, iBreak, iStart;
 102226         assert( p->pEList );
 102227         if( dest.eDest==SRT_Output ){
 102228           Select *pFirst = p;
 102229           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 102230           generateColumnNames(pParse, 0, pFirst->pEList);
 102232         iBreak = sqlite3VdbeMakeLabel(v);
 102233         iCont = sqlite3VdbeMakeLabel(v);
 102234         computeLimitRegisters(pParse, p, iBreak);
 102235         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
 102236         iStart = sqlite3VdbeCurrentAddr(v);
 102237         selectInnerLoop(pParse, p, p->pEList, unionTab,
 102238                         0, 0, &dest, iCont, iBreak);
 102239         sqlite3VdbeResolveLabel(v, iCont);
 102240         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
 102241         sqlite3VdbeResolveLabel(v, iBreak);
 102242         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
 102244       break;
 102246     default: assert( p->op==TK_INTERSECT ); {
 102247       int tab1, tab2;
 102248       int iCont, iBreak, iStart;
 102249       Expr *pLimit, *pOffset;
 102250       int addr;
 102251       SelectDest intersectdest;
 102252       int r1;
 102254       /* INTERSECT is different from the others since it requires
 102255       ** two temporary tables.  Hence it has its own case.  Begin
 102256       ** by allocating the tables we will need.
 102258       tab1 = pParse->nTab++;
 102259       tab2 = pParse->nTab++;
 102260       assert( p->pOrderBy==0 );
 102262       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
 102263       assert( p->addrOpenEphm[0] == -1 );
 102264       p->addrOpenEphm[0] = addr;
 102265       findRightmost(p)->selFlags |= SF_UsesEphemeral;
 102266       assert( p->pEList );
 102268       /* Code the SELECTs to our left into temporary table "tab1".
 102270       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
 102271       explainSetInteger(iSub1, pParse->iNextSelectId);
 102272       rc = sqlite3Select(pParse, pPrior, &intersectdest);
 102273       if( rc ){
 102274         goto multi_select_end;
 102277       /* Code the current SELECT into temporary table "tab2"
 102279       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
 102280       assert( p->addrOpenEphm[1] == -1 );
 102281       p->addrOpenEphm[1] = addr;
 102282       p->pPrior = 0;
 102283       pLimit = p->pLimit;
 102284       p->pLimit = 0;
 102285       pOffset = p->pOffset;
 102286       p->pOffset = 0;
 102287       intersectdest.iSDParm = tab2;
 102288       explainSetInteger(iSub2, pParse->iNextSelectId);
 102289       rc = sqlite3Select(pParse, p, &intersectdest);
 102290       testcase( rc!=SQLITE_OK );
 102291       pDelete = p->pPrior;
 102292       p->pPrior = pPrior;
 102293       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 102294       sqlite3ExprDelete(db, p->pLimit);
 102295       p->pLimit = pLimit;
 102296       p->pOffset = pOffset;
 102298       /* Generate code to take the intersection of the two temporary
 102299       ** tables.
 102301       assert( p->pEList );
 102302       if( dest.eDest==SRT_Output ){
 102303         Select *pFirst = p;
 102304         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 102305         generateColumnNames(pParse, 0, pFirst->pEList);
 102307       iBreak = sqlite3VdbeMakeLabel(v);
 102308       iCont = sqlite3VdbeMakeLabel(v);
 102309       computeLimitRegisters(pParse, p, iBreak);
 102310       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
 102311       r1 = sqlite3GetTempReg(pParse);
 102312       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
 102313       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
 102314       sqlite3ReleaseTempReg(pParse, r1);
 102315       selectInnerLoop(pParse, p, p->pEList, tab1,
 102316                       0, 0, &dest, iCont, iBreak);
 102317       sqlite3VdbeResolveLabel(v, iCont);
 102318       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
 102319       sqlite3VdbeResolveLabel(v, iBreak);
 102320       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
 102321       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
 102322       break;
 102326   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
 102328   /* Compute collating sequences used by 
 102329   ** temporary tables needed to implement the compound select.
 102330   ** Attach the KeyInfo structure to all temporary tables.
 102332   ** This section is run by the right-most SELECT statement only.
 102333   ** SELECT statements to the left always skip this part.  The right-most
 102334   ** SELECT might also skip this part if it has no ORDER BY clause and
 102335   ** no temp tables are required.
 102337   if( p->selFlags & SF_UsesEphemeral ){
 102338     int i;                        /* Loop counter */
 102339     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
 102340     Select *pLoop;                /* For looping through SELECT statements */
 102341     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
 102342     int nCol;                     /* Number of columns in result set */
 102344     assert( p->pNext==0 );
 102345     nCol = p->pEList->nExpr;
 102346     pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
 102347     if( !pKeyInfo ){
 102348       rc = SQLITE_NOMEM;
 102349       goto multi_select_end;
 102351     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
 102352       *apColl = multiSelectCollSeq(pParse, p, i);
 102353       if( 0==*apColl ){
 102354         *apColl = db->pDfltColl;
 102358     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
 102359       for(i=0; i<2; i++){
 102360         int addr = pLoop->addrOpenEphm[i];
 102361         if( addr<0 ){
 102362           /* If [0] is unused then [1] is also unused.  So we can
 102363           ** always safely abort as soon as the first unused slot is found */
 102364           assert( pLoop->addrOpenEphm[1]<0 );
 102365           break;
 102367         sqlite3VdbeChangeP2(v, addr, nCol);
 102368         sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
 102369                             P4_KEYINFO);
 102370         pLoop->addrOpenEphm[i] = -1;
 102373     sqlite3KeyInfoUnref(pKeyInfo);
 102376 multi_select_end:
 102377   pDest->iSdst = dest.iSdst;
 102378   pDest->nSdst = dest.nSdst;
 102379   sqlite3SelectDelete(db, pDelete);
 102380   return rc;
 102382 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 102385 ** Code an output subroutine for a coroutine implementation of a
 102386 ** SELECT statment.
 102388 ** The data to be output is contained in pIn->iSdst.  There are
 102389 ** pIn->nSdst columns to be output.  pDest is where the output should
 102390 ** be sent.
 102392 ** regReturn is the number of the register holding the subroutine
 102393 ** return address.
 102395 ** If regPrev>0 then it is the first register in a vector that
 102396 ** records the previous output.  mem[regPrev] is a flag that is false
 102397 ** if there has been no previous output.  If regPrev>0 then code is
 102398 ** generated to suppress duplicates.  pKeyInfo is used for comparing
 102399 ** keys.
 102401 ** If the LIMIT found in p->iLimit is reached, jump immediately to
 102402 ** iBreak.
 102404 static int generateOutputSubroutine(
 102405   Parse *pParse,          /* Parsing context */
 102406   Select *p,              /* The SELECT statement */
 102407   SelectDest *pIn,        /* Coroutine supplying data */
 102408   SelectDest *pDest,      /* Where to send the data */
 102409   int regReturn,          /* The return address register */
 102410   int regPrev,            /* Previous result register.  No uniqueness if 0 */
 102411   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
 102412   int iBreak              /* Jump here if we hit the LIMIT */
 102414   Vdbe *v = pParse->pVdbe;
 102415   int iContinue;
 102416   int addr;
 102418   addr = sqlite3VdbeCurrentAddr(v);
 102419   iContinue = sqlite3VdbeMakeLabel(v);
 102421   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
 102423   if( regPrev ){
 102424     int j1, j2;
 102425     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
 102426     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
 102427                               (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
 102428     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
 102429     sqlite3VdbeJumpHere(v, j1);
 102430     sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
 102431     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
 102433   if( pParse->db->mallocFailed ) return 0;
 102435   /* Suppress the first OFFSET entries if there is an OFFSET clause
 102437   codeOffset(v, p->iOffset, iContinue);
 102439   switch( pDest->eDest ){
 102440     /* Store the result as data using a unique key.
 102442     case SRT_Table:
 102443     case SRT_EphemTab: {
 102444       int r1 = sqlite3GetTempReg(pParse);
 102445       int r2 = sqlite3GetTempReg(pParse);
 102446       testcase( pDest->eDest==SRT_Table );
 102447       testcase( pDest->eDest==SRT_EphemTab );
 102448       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
 102449       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
 102450       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
 102451       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 102452       sqlite3ReleaseTempReg(pParse, r2);
 102453       sqlite3ReleaseTempReg(pParse, r1);
 102454       break;
 102457 #ifndef SQLITE_OMIT_SUBQUERY
 102458     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
 102459     ** then there should be a single item on the stack.  Write this
 102460     ** item into the set table with bogus data.
 102462     case SRT_Set: {
 102463       int r1;
 102464       assert( pIn->nSdst==1 );
 102465       pDest->affSdst = 
 102466          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
 102467       r1 = sqlite3GetTempReg(pParse);
 102468       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
 102469       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
 102470       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
 102471       sqlite3ReleaseTempReg(pParse, r1);
 102472       break;
 102475 #if 0  /* Never occurs on an ORDER BY query */
 102476     /* If any row exist in the result set, record that fact and abort.
 102478     case SRT_Exists: {
 102479       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
 102480       /* The LIMIT clause will terminate the loop for us */
 102481       break;
 102483 #endif
 102485     /* If this is a scalar select that is part of an expression, then
 102486     ** store the results in the appropriate memory cell and break out
 102487     ** of the scan loop.
 102489     case SRT_Mem: {
 102490       assert( pIn->nSdst==1 );
 102491       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
 102492       /* The LIMIT clause will jump out of the loop for us */
 102493       break;
 102495 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 102497     /* The results are stored in a sequence of registers
 102498     ** starting at pDest->iSdst.  Then the co-routine yields.
 102500     case SRT_Coroutine: {
 102501       if( pDest->iSdst==0 ){
 102502         pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
 102503         pDest->nSdst = pIn->nSdst;
 102505       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
 102506       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
 102507       break;
 102510     /* If none of the above, then the result destination must be
 102511     ** SRT_Output.  This routine is never called with any other
 102512     ** destination other than the ones handled above or SRT_Output.
 102514     ** For SRT_Output, results are stored in a sequence of registers.  
 102515     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
 102516     ** return the next row of result.
 102518     default: {
 102519       assert( pDest->eDest==SRT_Output );
 102520       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
 102521       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
 102522       break;
 102526   /* Jump to the end of the loop if the LIMIT is reached.
 102528   if( p->iLimit ){
 102529     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
 102532   /* Generate the subroutine return
 102534   sqlite3VdbeResolveLabel(v, iContinue);
 102535   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
 102537   return addr;
 102541 ** Alternative compound select code generator for cases when there
 102542 ** is an ORDER BY clause.
 102544 ** We assume a query of the following form:
 102546 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
 102548 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
 102549 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
 102550 ** co-routines.  Then run the co-routines in parallel and merge the results
 102551 ** into the output.  In addition to the two coroutines (called selectA and
 102552 ** selectB) there are 7 subroutines:
 102554 **    outA:    Move the output of the selectA coroutine into the output
 102555 **             of the compound query.
 102557 **    outB:    Move the output of the selectB coroutine into the output
 102558 **             of the compound query.  (Only generated for UNION and
 102559 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
 102560 **             appears only in B.)
 102562 **    AltB:    Called when there is data from both coroutines and A<B.
 102564 **    AeqB:    Called when there is data from both coroutines and A==B.
 102566 **    AgtB:    Called when there is data from both coroutines and A>B.
 102568 **    EofA:    Called when data is exhausted from selectA.
 102570 **    EofB:    Called when data is exhausted from selectB.
 102572 ** The implementation of the latter five subroutines depend on which 
 102573 ** <operator> is used:
 102576 **             UNION ALL         UNION            EXCEPT          INTERSECT
 102577 **          -------------  -----------------  --------------  -----------------
 102578 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
 102580 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
 102582 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
 102584 **   EofA:   outB, nextB      outB, nextB          halt             halt
 102586 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
 102588 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
 102589 ** causes an immediate jump to EofA and an EOF on B following nextB causes
 102590 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
 102591 ** following nextX causes a jump to the end of the select processing.
 102593 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
 102594 ** within the output subroutine.  The regPrev register set holds the previously
 102595 ** output value.  A comparison is made against this value and the output
 102596 ** is skipped if the next results would be the same as the previous.
 102598 ** The implementation plan is to implement the two coroutines and seven
 102599 ** subroutines first, then put the control logic at the bottom.  Like this:
 102601 **          goto Init
 102602 **     coA: coroutine for left query (A)
 102603 **     coB: coroutine for right query (B)
 102604 **    outA: output one row of A
 102605 **    outB: output one row of B (UNION and UNION ALL only)
 102606 **    EofA: ...
 102607 **    EofB: ...
 102608 **    AltB: ...
 102609 **    AeqB: ...
 102610 **    AgtB: ...
 102611 **    Init: initialize coroutine registers
 102612 **          yield coA
 102613 **          if eof(A) goto EofA
 102614 **          yield coB
 102615 **          if eof(B) goto EofB
 102616 **    Cmpr: Compare A, B
 102617 **          Jump AltB, AeqB, AgtB
 102618 **     End: ...
 102620 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
 102621 ** actually called using Gosub and they do not Return.  EofA and EofB loop
 102622 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
 102623 ** and AgtB jump to either L2 or to one of EofA or EofB.
 102625 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 102626 static int multiSelectOrderBy(
 102627   Parse *pParse,        /* Parsing context */
 102628   Select *p,            /* The right-most of SELECTs to be coded */
 102629   SelectDest *pDest     /* What to do with query results */
 102631   int i, j;             /* Loop counters */
 102632   Select *pPrior;       /* Another SELECT immediately to our left */
 102633   Vdbe *v;              /* Generate code to this VDBE */
 102634   SelectDest destA;     /* Destination for coroutine A */
 102635   SelectDest destB;     /* Destination for coroutine B */
 102636   int regAddrA;         /* Address register for select-A coroutine */
 102637   int regAddrB;         /* Address register for select-B coroutine */
 102638   int addrSelectA;      /* Address of the select-A coroutine */
 102639   int addrSelectB;      /* Address of the select-B coroutine */
 102640   int regOutA;          /* Address register for the output-A subroutine */
 102641   int regOutB;          /* Address register for the output-B subroutine */
 102642   int addrOutA;         /* Address of the output-A subroutine */
 102643   int addrOutB = 0;     /* Address of the output-B subroutine */
 102644   int addrEofA;         /* Address of the select-A-exhausted subroutine */
 102645   int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
 102646   int addrEofB;         /* Address of the select-B-exhausted subroutine */
 102647   int addrAltB;         /* Address of the A<B subroutine */
 102648   int addrAeqB;         /* Address of the A==B subroutine */
 102649   int addrAgtB;         /* Address of the A>B subroutine */
 102650   int regLimitA;        /* Limit register for select-A */
 102651   int regLimitB;        /* Limit register for select-A */
 102652   int regPrev;          /* A range of registers to hold previous output */
 102653   int savedLimit;       /* Saved value of p->iLimit */
 102654   int savedOffset;      /* Saved value of p->iOffset */
 102655   int labelCmpr;        /* Label for the start of the merge algorithm */
 102656   int labelEnd;         /* Label for the end of the overall SELECT stmt */
 102657   int j1;               /* Jump instructions that get retargetted */
 102658   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
 102659   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
 102660   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
 102661   sqlite3 *db;          /* Database connection */
 102662   ExprList *pOrderBy;   /* The ORDER BY clause */
 102663   int nOrderBy;         /* Number of terms in the ORDER BY clause */
 102664   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
 102665 #ifndef SQLITE_OMIT_EXPLAIN
 102666   int iSub1;            /* EQP id of left-hand query */
 102667   int iSub2;            /* EQP id of right-hand query */
 102668 #endif
 102670   assert( p->pOrderBy!=0 );
 102671   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
 102672   db = pParse->db;
 102673   v = pParse->pVdbe;
 102674   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
 102675   labelEnd = sqlite3VdbeMakeLabel(v);
 102676   labelCmpr = sqlite3VdbeMakeLabel(v);
 102679   /* Patch up the ORDER BY clause
 102681   op = p->op;  
 102682   pPrior = p->pPrior;
 102683   assert( pPrior->pOrderBy==0 );
 102684   pOrderBy = p->pOrderBy;
 102685   assert( pOrderBy );
 102686   nOrderBy = pOrderBy->nExpr;
 102688   /* For operators other than UNION ALL we have to make sure that
 102689   ** the ORDER BY clause covers every term of the result set.  Add
 102690   ** terms to the ORDER BY clause as necessary.
 102692   if( op!=TK_ALL ){
 102693     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
 102694       struct ExprList_item *pItem;
 102695       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
 102696         assert( pItem->u.x.iOrderByCol>0 );
 102697         if( pItem->u.x.iOrderByCol==i ) break;
 102699       if( j==nOrderBy ){
 102700         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
 102701         if( pNew==0 ) return SQLITE_NOMEM;
 102702         pNew->flags |= EP_IntValue;
 102703         pNew->u.iValue = i;
 102704         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
 102705         if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
 102710   /* Compute the comparison permutation and keyinfo that is used with
 102711   ** the permutation used to determine if the next
 102712   ** row of results comes from selectA or selectB.  Also add explicit
 102713   ** collations to the ORDER BY clause terms so that when the subqueries
 102714   ** to the right and the left are evaluated, they use the correct
 102715   ** collation.
 102717   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
 102718   if( aPermute ){
 102719     struct ExprList_item *pItem;
 102720     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
 102721       assert( pItem->u.x.iOrderByCol>0
 102722           && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
 102723       aPermute[i] = pItem->u.x.iOrderByCol - 1;
 102725     pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
 102726   }else{
 102727     pKeyMerge = 0;
 102730   /* Reattach the ORDER BY clause to the query.
 102732   p->pOrderBy = pOrderBy;
 102733   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
 102735   /* Allocate a range of temporary registers and the KeyInfo needed
 102736   ** for the logic that removes duplicate result rows when the
 102737   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
 102739   if( op==TK_ALL ){
 102740     regPrev = 0;
 102741   }else{
 102742     int nExpr = p->pEList->nExpr;
 102743     assert( nOrderBy>=nExpr || db->mallocFailed );
 102744     regPrev = pParse->nMem+1;
 102745     pParse->nMem += nExpr+1;
 102746     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
 102747     pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
 102748     if( pKeyDup ){
 102749       assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
 102750       for(i=0; i<nExpr; i++){
 102751         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
 102752         pKeyDup->aSortOrder[i] = 0;
 102757   /* Separate the left and the right query from one another
 102759   p->pPrior = 0;
 102760   pPrior->pNext = 0;
 102761   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
 102762   if( pPrior->pPrior==0 ){
 102763     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
 102766   /* Compute the limit registers */
 102767   computeLimitRegisters(pParse, p, labelEnd);
 102768   if( p->iLimit && op==TK_ALL ){
 102769     regLimitA = ++pParse->nMem;
 102770     regLimitB = ++pParse->nMem;
 102771     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
 102772                                   regLimitA);
 102773     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
 102774   }else{
 102775     regLimitA = regLimitB = 0;
 102777   sqlite3ExprDelete(db, p->pLimit);
 102778   p->pLimit = 0;
 102779   sqlite3ExprDelete(db, p->pOffset);
 102780   p->pOffset = 0;
 102782   regAddrA = ++pParse->nMem;
 102783   regAddrB = ++pParse->nMem;
 102784   regOutA = ++pParse->nMem;
 102785   regOutB = ++pParse->nMem;
 102786   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
 102787   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
 102789   /* Generate a coroutine to evaluate the SELECT statement to the
 102790   ** left of the compound operator - the "A" select.
 102792   addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
 102793   j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
 102794   VdbeComment((v, "left SELECT"));
 102795   pPrior->iLimit = regLimitA;
 102796   explainSetInteger(iSub1, pParse->iNextSelectId);
 102797   sqlite3Select(pParse, pPrior, &destA);
 102798   sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
 102799   sqlite3VdbeJumpHere(v, j1);
 102801   /* Generate a coroutine to evaluate the SELECT statement on 
 102802   ** the right - the "B" select
 102804   addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
 102805   j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
 102806   VdbeComment((v, "right SELECT"));
 102807   savedLimit = p->iLimit;
 102808   savedOffset = p->iOffset;
 102809   p->iLimit = regLimitB;
 102810   p->iOffset = 0;  
 102811   explainSetInteger(iSub2, pParse->iNextSelectId);
 102812   sqlite3Select(pParse, p, &destB);
 102813   p->iLimit = savedLimit;
 102814   p->iOffset = savedOffset;
 102815   sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
 102817   /* Generate a subroutine that outputs the current row of the A
 102818   ** select as the next output row of the compound select.
 102820   VdbeNoopComment((v, "Output routine for A"));
 102821   addrOutA = generateOutputSubroutine(pParse,
 102822                  p, &destA, pDest, regOutA,
 102823                  regPrev, pKeyDup, labelEnd);
 102825   /* Generate a subroutine that outputs the current row of the B
 102826   ** select as the next output row of the compound select.
 102828   if( op==TK_ALL || op==TK_UNION ){
 102829     VdbeNoopComment((v, "Output routine for B"));
 102830     addrOutB = generateOutputSubroutine(pParse,
 102831                  p, &destB, pDest, regOutB,
 102832                  regPrev, pKeyDup, labelEnd);
 102834   sqlite3KeyInfoUnref(pKeyDup);
 102836   /* Generate a subroutine to run when the results from select A
 102837   ** are exhausted and only data in select B remains.
 102839   if( op==TK_EXCEPT || op==TK_INTERSECT ){
 102840     addrEofA_noB = addrEofA = labelEnd;
 102841   }else{  
 102842     VdbeNoopComment((v, "eof-A subroutine"));
 102843     addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 102844     addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
 102845                                      VdbeCoverage(v);
 102846     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
 102847     p->nSelectRow += pPrior->nSelectRow;
 102850   /* Generate a subroutine to run when the results from select B
 102851   ** are exhausted and only data in select A remains.
 102853   if( op==TK_INTERSECT ){
 102854     addrEofB = addrEofA;
 102855     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
 102856   }else{  
 102857     VdbeNoopComment((v, "eof-B subroutine"));
 102858     addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 102859     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
 102860     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
 102863   /* Generate code to handle the case of A<B
 102865   VdbeNoopComment((v, "A-lt-B subroutine"));
 102866   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 102867   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
 102868   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 102870   /* Generate code to handle the case of A==B
 102872   if( op==TK_ALL ){
 102873     addrAeqB = addrAltB;
 102874   }else if( op==TK_INTERSECT ){
 102875     addrAeqB = addrAltB;
 102876     addrAltB++;
 102877   }else{
 102878     VdbeNoopComment((v, "A-eq-B subroutine"));
 102879     addrAeqB =
 102880     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
 102881     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 102884   /* Generate code to handle the case of A>B
 102886   VdbeNoopComment((v, "A-gt-B subroutine"));
 102887   addrAgtB = sqlite3VdbeCurrentAddr(v);
 102888   if( op==TK_ALL || op==TK_UNION ){
 102889     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 102891   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
 102892   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
 102894   /* This code runs once to initialize everything.
 102896   sqlite3VdbeJumpHere(v, j1);
 102897   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
 102898   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
 102900   /* Implement the main merge loop
 102902   sqlite3VdbeResolveLabel(v, labelCmpr);
 102903   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
 102904   sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
 102905                          (char*)pKeyMerge, P4_KEYINFO);
 102906   sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
 102907   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
 102909   /* Jump to the this point in order to terminate the query.
 102911   sqlite3VdbeResolveLabel(v, labelEnd);
 102913   /* Set the number of output columns
 102915   if( pDest->eDest==SRT_Output ){
 102916     Select *pFirst = pPrior;
 102917     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
 102918     generateColumnNames(pParse, 0, pFirst->pEList);
 102921   /* Reassembly the compound query so that it will be freed correctly
 102922   ** by the calling function */
 102923   if( p->pPrior ){
 102924     sqlite3SelectDelete(db, p->pPrior);
 102926   p->pPrior = pPrior;
 102927   pPrior->pNext = p;
 102929   /*** TBD:  Insert subroutine calls to close cursors on incomplete
 102930   **** subqueries ****/
 102931   explainComposite(pParse, p->op, iSub1, iSub2, 0);
 102932   return SQLITE_OK;
 102934 #endif
 102936 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 102937 /* Forward Declarations */
 102938 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
 102939 static void substSelect(sqlite3*, Select *, int, ExprList *);
 102942 ** Scan through the expression pExpr.  Replace every reference to
 102943 ** a column in table number iTable with a copy of the iColumn-th
 102944 ** entry in pEList.  (But leave references to the ROWID column 
 102945 ** unchanged.)
 102947 ** This routine is part of the flattening procedure.  A subquery
 102948 ** whose result set is defined by pEList appears as entry in the
 102949 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
 102950 ** FORM clause entry is iTable.  This routine make the necessary 
 102951 ** changes to pExpr so that it refers directly to the source table
 102952 ** of the subquery rather the result set of the subquery.
 102954 static Expr *substExpr(
 102955   sqlite3 *db,        /* Report malloc errors to this connection */
 102956   Expr *pExpr,        /* Expr in which substitution occurs */
 102957   int iTable,         /* Table to be substituted */
 102958   ExprList *pEList    /* Substitute expressions */
 102960   if( pExpr==0 ) return 0;
 102961   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
 102962     if( pExpr->iColumn<0 ){
 102963       pExpr->op = TK_NULL;
 102964     }else{
 102965       Expr *pNew;
 102966       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
 102967       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
 102968       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
 102969       sqlite3ExprDelete(db, pExpr);
 102970       pExpr = pNew;
 102972   }else{
 102973     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
 102974     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
 102975     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 102976       substSelect(db, pExpr->x.pSelect, iTable, pEList);
 102977     }else{
 102978       substExprList(db, pExpr->x.pList, iTable, pEList);
 102981   return pExpr;
 102983 static void substExprList(
 102984   sqlite3 *db,         /* Report malloc errors here */
 102985   ExprList *pList,     /* List to scan and in which to make substitutes */
 102986   int iTable,          /* Table to be substituted */
 102987   ExprList *pEList     /* Substitute values */
 102989   int i;
 102990   if( pList==0 ) return;
 102991   for(i=0; i<pList->nExpr; i++){
 102992     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
 102995 static void substSelect(
 102996   sqlite3 *db,         /* Report malloc errors here */
 102997   Select *p,           /* SELECT statement in which to make substitutions */
 102998   int iTable,          /* Table to be replaced */
 102999   ExprList *pEList     /* Substitute values */
 103001   SrcList *pSrc;
 103002   struct SrcList_item *pItem;
 103003   int i;
 103004   if( !p ) return;
 103005   substExprList(db, p->pEList, iTable, pEList);
 103006   substExprList(db, p->pGroupBy, iTable, pEList);
 103007   substExprList(db, p->pOrderBy, iTable, pEList);
 103008   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
 103009   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
 103010   substSelect(db, p->pPrior, iTable, pEList);
 103011   pSrc = p->pSrc;
 103012   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
 103013   if( ALWAYS(pSrc) ){
 103014     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
 103015       substSelect(db, pItem->pSelect, iTable, pEList);
 103019 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 103021 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 103023 ** This routine attempts to flatten subqueries as a performance optimization.
 103024 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
 103026 ** To understand the concept of flattening, consider the following
 103027 ** query:
 103029 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
 103031 ** The default way of implementing this query is to execute the
 103032 ** subquery first and store the results in a temporary table, then
 103033 ** run the outer query on that temporary table.  This requires two
 103034 ** passes over the data.  Furthermore, because the temporary table
 103035 ** has no indices, the WHERE clause on the outer query cannot be
 103036 ** optimized.
 103038 ** This routine attempts to rewrite queries such as the above into
 103039 ** a single flat select, like this:
 103041 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
 103043 ** The code generated for this simpification gives the same result
 103044 ** but only has to scan the data once.  And because indices might 
 103045 ** exist on the table t1, a complete scan of the data might be
 103046 ** avoided.
 103048 ** Flattening is only attempted if all of the following are true:
 103050 **   (1)  The subquery and the outer query do not both use aggregates.
 103052 **   (2)  The subquery is not an aggregate or the outer query is not a join.
 103054 **   (3)  The subquery is not the right operand of a left outer join
 103055 **        (Originally ticket #306.  Strengthened by ticket #3300)
 103057 **   (4)  The subquery is not DISTINCT.
 103059 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
 103060 **        sub-queries that were excluded from this optimization. Restriction 
 103061 **        (4) has since been expanded to exclude all DISTINCT subqueries.
 103063 **   (6)  The subquery does not use aggregates or the outer query is not
 103064 **        DISTINCT.
 103066 **   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
 103067 **        A FROM clause, consider adding a FROM close with the special
 103068 **        table sqlite_once that consists of a single row containing a
 103069 **        single NULL.
 103071 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
 103073 **   (9)  The subquery does not use LIMIT or the outer query does not use
 103074 **        aggregates.
 103076 **  (10)  The subquery does not use aggregates or the outer query does not
 103077 **        use LIMIT.
 103079 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
 103081 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
 103082 **        a separate restriction deriving from ticket #350.
 103084 **  (13)  The subquery and outer query do not both use LIMIT.
 103086 **  (14)  The subquery does not use OFFSET.
 103088 **  (15)  The outer query is not part of a compound select or the
 103089 **        subquery does not have a LIMIT clause.
 103090 **        (See ticket #2339 and ticket [02a8e81d44]).
 103092 **  (16)  The outer query is not an aggregate or the subquery does
 103093 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
 103094 **        until we introduced the group_concat() function.  
 103096 **  (17)  The sub-query is not a compound select, or it is a UNION ALL 
 103097 **        compound clause made up entirely of non-aggregate queries, and 
 103098 **        the parent query:
 103100 **          * is not itself part of a compound select,
 103101 **          * is not an aggregate or DISTINCT query, and
 103102 **          * is not a join
 103104 **        The parent and sub-query may contain WHERE clauses. Subject to
 103105 **        rules (11), (13) and (14), they may also contain ORDER BY,
 103106 **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
 103107 **        operator other than UNION ALL because all the other compound
 103108 **        operators have an implied DISTINCT which is disallowed by
 103109 **        restriction (4).
 103111 **        Also, each component of the sub-query must return the same number
 103112 **        of result columns. This is actually a requirement for any compound
 103113 **        SELECT statement, but all the code here does is make sure that no
 103114 **        such (illegal) sub-query is flattened. The caller will detect the
 103115 **        syntax error and return a detailed message.
 103117 **  (18)  If the sub-query is a compound select, then all terms of the
 103118 **        ORDER by clause of the parent must be simple references to 
 103119 **        columns of the sub-query.
 103121 **  (19)  The subquery does not use LIMIT or the outer query does not
 103122 **        have a WHERE clause.
 103124 **  (20)  If the sub-query is a compound select, then it must not use
 103125 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
 103126 **        somewhat by saying that the terms of the ORDER BY clause must
 103127 **        appear as unmodified result columns in the outer query.  But we
 103128 **        have other optimizations in mind to deal with that case.
 103130 **  (21)  The subquery does not use LIMIT or the outer query is not
 103131 **        DISTINCT.  (See ticket [752e1646fc]).
 103133 **  (22)  The subquery is not a recursive CTE.
 103135 **  (23)  The parent is not a recursive CTE, or the sub-query is not a
 103136 **        compound query. This restriction is because transforming the
 103137 **        parent to a compound query confuses the code that handles
 103138 **        recursive queries in multiSelect().
 103141 ** In this routine, the "p" parameter is a pointer to the outer query.
 103142 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
 103143 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
 103145 ** If flattening is not attempted, this routine is a no-op and returns 0.
 103146 ** If flattening is attempted this routine returns 1.
 103148 ** All of the expression analysis must occur on both the outer query and
 103149 ** the subquery before this routine runs.
 103151 static int flattenSubquery(
 103152   Parse *pParse,       /* Parsing context */
 103153   Select *p,           /* The parent or outer SELECT statement */
 103154   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
 103155   int isAgg,           /* True if outer SELECT uses aggregate functions */
 103156   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
 103158   const char *zSavedAuthContext = pParse->zAuthContext;
 103159   Select *pParent;
 103160   Select *pSub;       /* The inner query or "subquery" */
 103161   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
 103162   SrcList *pSrc;      /* The FROM clause of the outer query */
 103163   SrcList *pSubSrc;   /* The FROM clause of the subquery */
 103164   ExprList *pList;    /* The result set of the outer query */
 103165   int iParent;        /* VDBE cursor number of the pSub result set temp table */
 103166   int i;              /* Loop counter */
 103167   Expr *pWhere;                    /* The WHERE clause */
 103168   struct SrcList_item *pSubitem;   /* The subquery */
 103169   sqlite3 *db = pParse->db;
 103171   /* Check to see if flattening is permitted.  Return 0 if not.
 103173   assert( p!=0 );
 103174   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
 103175   if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
 103176   pSrc = p->pSrc;
 103177   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
 103178   pSubitem = &pSrc->a[iFrom];
 103179   iParent = pSubitem->iCursor;
 103180   pSub = pSubitem->pSelect;
 103181   assert( pSub!=0 );
 103182   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
 103183   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
 103184   pSubSrc = pSub->pSrc;
 103185   assert( pSubSrc );
 103186   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
 103187   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
 103188   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
 103189   ** became arbitrary expressions, we were forced to add restrictions (13)
 103190   ** and (14). */
 103191   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
 103192   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
 103193   if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
 103194     return 0;                                            /* Restriction (15) */
 103196   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
 103197   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
 103198   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
 103199      return 0;         /* Restrictions (8)(9) */
 103201   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
 103202      return 0;         /* Restriction (6)  */
 103204   if( p->pOrderBy && pSub->pOrderBy ){
 103205      return 0;                                           /* Restriction (11) */
 103207   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
 103208   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
 103209   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
 103210      return 0;         /* Restriction (21) */
 103212   if( pSub->selFlags & SF_Recursive ) return 0;          /* Restriction (22)  */
 103213   if( (p->selFlags & SF_Recursive) && pSub->pPrior ) return 0;       /* (23)  */
 103215   /* OBSOLETE COMMENT 1:
 103216   ** Restriction 3:  If the subquery is a join, make sure the subquery is 
 103217   ** not used as the right operand of an outer join.  Examples of why this
 103218   ** is not allowed:
 103220   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
 103222   ** If we flatten the above, we would get
 103224   **         (t1 LEFT OUTER JOIN t2) JOIN t3
 103226   ** which is not at all the same thing.
 103228   ** OBSOLETE COMMENT 2:
 103229   ** Restriction 12:  If the subquery is the right operand of a left outer
 103230   ** join, make sure the subquery has no WHERE clause.
 103231   ** An examples of why this is not allowed:
 103233   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
 103235   ** If we flatten the above, we would get
 103237   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
 103239   ** But the t2.x>0 test will always fail on a NULL row of t2, which
 103240   ** effectively converts the OUTER JOIN into an INNER JOIN.
 103242   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
 103243   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
 103244   ** is fraught with danger.  Best to avoid the whole thing.  If the
 103245   ** subquery is the right term of a LEFT JOIN, then do not flatten.
 103247   if( (pSubitem->jointype & JT_OUTER)!=0 ){
 103248     return 0;
 103251   /* Restriction 17: If the sub-query is a compound SELECT, then it must
 103252   ** use only the UNION ALL operator. And none of the simple select queries
 103253   ** that make up the compound SELECT are allowed to be aggregate or distinct
 103254   ** queries.
 103256   if( pSub->pPrior ){
 103257     if( pSub->pOrderBy ){
 103258       return 0;  /* Restriction 20 */
 103260     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
 103261       return 0;
 103263     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
 103264       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 103265       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 103266       assert( pSub->pSrc!=0 );
 103267       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
 103268        || (pSub1->pPrior && pSub1->op!=TK_ALL) 
 103269        || pSub1->pSrc->nSrc<1
 103270        || pSub->pEList->nExpr!=pSub1->pEList->nExpr
 103272         return 0;
 103274       testcase( pSub1->pSrc->nSrc>1 );
 103277     /* Restriction 18. */
 103278     if( p->pOrderBy ){
 103279       int ii;
 103280       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
 103281         if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
 103286   /***** If we reach this point, flattening is permitted. *****/
 103288   /* Authorize the subquery */
 103289   pParse->zAuthContext = pSubitem->zName;
 103290   TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
 103291   testcase( i==SQLITE_DENY );
 103292   pParse->zAuthContext = zSavedAuthContext;
 103294   /* If the sub-query is a compound SELECT statement, then (by restrictions
 103295   ** 17 and 18 above) it must be a UNION ALL and the parent query must 
 103296   ** be of the form:
 103298   **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
 103300   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
 103301   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
 103302   ** OFFSET clauses and joins them to the left-hand-side of the original
 103303   ** using UNION ALL operators. In this case N is the number of simple
 103304   ** select statements in the compound sub-query.
 103306   ** Example:
 103308   **     SELECT a+1 FROM (
 103309   **        SELECT x FROM tab
 103310   **        UNION ALL
 103311   **        SELECT y FROM tab
 103312   **        UNION ALL
 103313   **        SELECT abs(z*2) FROM tab2
 103314   **     ) WHERE a!=5 ORDER BY 1
 103316   ** Transformed into:
 103318   **     SELECT x+1 FROM tab WHERE x+1!=5
 103319   **     UNION ALL
 103320   **     SELECT y+1 FROM tab WHERE y+1!=5
 103321   **     UNION ALL
 103322   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
 103323   **     ORDER BY 1
 103325   ** We call this the "compound-subquery flattening".
 103327   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
 103328     Select *pNew;
 103329     ExprList *pOrderBy = p->pOrderBy;
 103330     Expr *pLimit = p->pLimit;
 103331     Expr *pOffset = p->pOffset;
 103332     Select *pPrior = p->pPrior;
 103333     p->pOrderBy = 0;
 103334     p->pSrc = 0;
 103335     p->pPrior = 0;
 103336     p->pLimit = 0;
 103337     p->pOffset = 0;
 103338     pNew = sqlite3SelectDup(db, p, 0);
 103339     p->pOffset = pOffset;
 103340     p->pLimit = pLimit;
 103341     p->pOrderBy = pOrderBy;
 103342     p->pSrc = pSrc;
 103343     p->op = TK_ALL;
 103344     if( pNew==0 ){
 103345       p->pPrior = pPrior;
 103346     }else{
 103347       pNew->pPrior = pPrior;
 103348       if( pPrior ) pPrior->pNext = pNew;
 103349       pNew->pNext = p;
 103350       p->pPrior = pNew;
 103352     if( db->mallocFailed ) return 1;
 103355   /* Begin flattening the iFrom-th entry of the FROM clause 
 103356   ** in the outer query.
 103358   pSub = pSub1 = pSubitem->pSelect;
 103360   /* Delete the transient table structure associated with the
 103361   ** subquery
 103363   sqlite3DbFree(db, pSubitem->zDatabase);
 103364   sqlite3DbFree(db, pSubitem->zName);
 103365   sqlite3DbFree(db, pSubitem->zAlias);
 103366   pSubitem->zDatabase = 0;
 103367   pSubitem->zName = 0;
 103368   pSubitem->zAlias = 0;
 103369   pSubitem->pSelect = 0;
 103371   /* Defer deleting the Table object associated with the
 103372   ** subquery until code generation is
 103373   ** complete, since there may still exist Expr.pTab entries that
 103374   ** refer to the subquery even after flattening.  Ticket #3346.
 103376   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
 103378   if( ALWAYS(pSubitem->pTab!=0) ){
 103379     Table *pTabToDel = pSubitem->pTab;
 103380     if( pTabToDel->nRef==1 ){
 103381       Parse *pToplevel = sqlite3ParseToplevel(pParse);
 103382       pTabToDel->pNextZombie = pToplevel->pZombieTab;
 103383       pToplevel->pZombieTab = pTabToDel;
 103384     }else{
 103385       pTabToDel->nRef--;
 103387     pSubitem->pTab = 0;
 103390   /* The following loop runs once for each term in a compound-subquery
 103391   ** flattening (as described above).  If we are doing a different kind
 103392   ** of flattening - a flattening other than a compound-subquery flattening -
 103393   ** then this loop only runs once.
 103395   ** This loop moves all of the FROM elements of the subquery into the
 103396   ** the FROM clause of the outer query.  Before doing this, remember
 103397   ** the cursor number for the original outer query FROM element in
 103398   ** iParent.  The iParent cursor will never be used.  Subsequent code
 103399   ** will scan expressions looking for iParent references and replace
 103400   ** those references with expressions that resolve to the subquery FROM
 103401   ** elements we are now copying in.
 103403   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
 103404     int nSubSrc;
 103405     u8 jointype = 0;
 103406     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
 103407     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
 103408     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
 103410     if( pSrc ){
 103411       assert( pParent==p );  /* First time through the loop */
 103412       jointype = pSubitem->jointype;
 103413     }else{
 103414       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
 103415       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
 103416       if( pSrc==0 ){
 103417         assert( db->mallocFailed );
 103418         break;
 103422     /* The subquery uses a single slot of the FROM clause of the outer
 103423     ** query.  If the subquery has more than one element in its FROM clause,
 103424     ** then expand the outer query to make space for it to hold all elements
 103425     ** of the subquery.
 103427     ** Example:
 103429     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
 103431     ** The outer query has 3 slots in its FROM clause.  One slot of the
 103432     ** outer query (the middle slot) is used by the subquery.  The next
 103433     ** block of code will expand the out query to 4 slots.  The middle
 103434     ** slot is expanded to two slots in order to make space for the
 103435     ** two elements in the FROM clause of the subquery.
 103437     if( nSubSrc>1 ){
 103438       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
 103439       if( db->mallocFailed ){
 103440         break;
 103444     /* Transfer the FROM clause terms from the subquery into the
 103445     ** outer query.
 103447     for(i=0; i<nSubSrc; i++){
 103448       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
 103449       pSrc->a[i+iFrom] = pSubSrc->a[i];
 103450       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
 103452     pSrc->a[iFrom].jointype = jointype;
 103454     /* Now begin substituting subquery result set expressions for 
 103455     ** references to the iParent in the outer query.
 103457     ** Example:
 103459     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
 103460     **   \                     \_____________ subquery __________/          /
 103461     **    \_____________________ outer query ______________________________/
 103463     ** We look at every expression in the outer query and every place we see
 103464     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
 103466     pList = pParent->pEList;
 103467     for(i=0; i<pList->nExpr; i++){
 103468       if( pList->a[i].zName==0 ){
 103469         char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
 103470         sqlite3Dequote(zName);
 103471         pList->a[i].zName = zName;
 103474     substExprList(db, pParent->pEList, iParent, pSub->pEList);
 103475     if( isAgg ){
 103476       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
 103477       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 103479     if( pSub->pOrderBy ){
 103480       assert( pParent->pOrderBy==0 );
 103481       pParent->pOrderBy = pSub->pOrderBy;
 103482       pSub->pOrderBy = 0;
 103483     }else if( pParent->pOrderBy ){
 103484       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
 103486     if( pSub->pWhere ){
 103487       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
 103488     }else{
 103489       pWhere = 0;
 103491     if( subqueryIsAgg ){
 103492       assert( pParent->pHaving==0 );
 103493       pParent->pHaving = pParent->pWhere;
 103494       pParent->pWhere = pWhere;
 103495       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
 103496       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
 103497                                   sqlite3ExprDup(db, pSub->pHaving, 0));
 103498       assert( pParent->pGroupBy==0 );
 103499       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
 103500     }else{
 103501       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
 103502       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
 103505     /* The flattened query is distinct if either the inner or the
 103506     ** outer query is distinct. 
 103508     pParent->selFlags |= pSub->selFlags & SF_Distinct;
 103511     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
 103513     ** One is tempted to try to add a and b to combine the limits.  But this
 103514     ** does not work if either limit is negative.
 103516     if( pSub->pLimit ){
 103517       pParent->pLimit = pSub->pLimit;
 103518       pSub->pLimit = 0;
 103522   /* Finially, delete what is left of the subquery and return
 103523   ** success.
 103525   sqlite3SelectDelete(db, pSub1);
 103527   return 1;
 103529 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 103532 ** Based on the contents of the AggInfo structure indicated by the first
 103533 ** argument, this function checks if the following are true:
 103535 **    * the query contains just a single aggregate function,
 103536 **    * the aggregate function is either min() or max(), and
 103537 **    * the argument to the aggregate function is a column value.
 103539 ** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
 103540 ** is returned as appropriate. Also, *ppMinMax is set to point to the 
 103541 ** list of arguments passed to the aggregate before returning.
 103543 ** Or, if the conditions above are not met, *ppMinMax is set to 0 and
 103544 ** WHERE_ORDERBY_NORMAL is returned.
 103546 static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
 103547   int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
 103549   *ppMinMax = 0;
 103550   if( pAggInfo->nFunc==1 ){
 103551     Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
 103552     ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
 103554     assert( pExpr->op==TK_AGG_FUNCTION );
 103555     if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
 103556       const char *zFunc = pExpr->u.zToken;
 103557       if( sqlite3StrICmp(zFunc, "min")==0 ){
 103558         eRet = WHERE_ORDERBY_MIN;
 103559         *ppMinMax = pEList;
 103560       }else if( sqlite3StrICmp(zFunc, "max")==0 ){
 103561         eRet = WHERE_ORDERBY_MAX;
 103562         *ppMinMax = pEList;
 103567   assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
 103568   return eRet;
 103572 ** The select statement passed as the first argument is an aggregate query.
 103573 ** The second argment is the associated aggregate-info object. This 
 103574 ** function tests if the SELECT is of the form:
 103576 **   SELECT count(*) FROM <tbl>
 103578 ** where table is a database table, not a sub-select or view. If the query
 103579 ** does match this pattern, then a pointer to the Table object representing
 103580 ** <tbl> is returned. Otherwise, 0 is returned.
 103582 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
 103583   Table *pTab;
 103584   Expr *pExpr;
 103586   assert( !p->pGroupBy );
 103588   if( p->pWhere || p->pEList->nExpr!=1 
 103589    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
 103591     return 0;
 103593   pTab = p->pSrc->a[0].pTab;
 103594   pExpr = p->pEList->a[0].pExpr;
 103595   assert( pTab && !pTab->pSelect && pExpr );
 103597   if( IsVirtual(pTab) ) return 0;
 103598   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
 103599   if( NEVER(pAggInfo->nFunc==0) ) return 0;
 103600   if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
 103601   if( pExpr->flags&EP_Distinct ) return 0;
 103603   return pTab;
 103607 ** If the source-list item passed as an argument was augmented with an
 103608 ** INDEXED BY clause, then try to locate the specified index. If there
 103609 ** was such a clause and the named index cannot be found, return 
 103610 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
 103611 ** pFrom->pIndex and return SQLITE_OK.
 103613 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
 103614   if( pFrom->pTab && pFrom->zIndex ){
 103615     Table *pTab = pFrom->pTab;
 103616     char *zIndex = pFrom->zIndex;
 103617     Index *pIdx;
 103618     for(pIdx=pTab->pIndex; 
 103619         pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
 103620         pIdx=pIdx->pNext
 103622     if( !pIdx ){
 103623       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
 103624       pParse->checkSchema = 1;
 103625       return SQLITE_ERROR;
 103627     pFrom->pIndex = pIdx;
 103629   return SQLITE_OK;
 103632 ** Detect compound SELECT statements that use an ORDER BY clause with 
 103633 ** an alternative collating sequence.
 103635 **    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
 103637 ** These are rewritten as a subquery:
 103639 **    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
 103640 **     ORDER BY ... COLLATE ...
 103642 ** This transformation is necessary because the multiSelectOrderBy() routine
 103643 ** above that generates the code for a compound SELECT with an ORDER BY clause
 103644 ** uses a merge algorithm that requires the same collating sequence on the
 103645 ** result columns as on the ORDER BY clause.  See ticket
 103646 ** http://www.sqlite.org/src/info/6709574d2a
 103648 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
 103649 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
 103650 ** there are COLLATE terms in the ORDER BY.
 103652 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
 103653   int i;
 103654   Select *pNew;
 103655   Select *pX;
 103656   sqlite3 *db;
 103657   struct ExprList_item *a;
 103658   SrcList *pNewSrc;
 103659   Parse *pParse;
 103660   Token dummy;
 103662   if( p->pPrior==0 ) return WRC_Continue;
 103663   if( p->pOrderBy==0 ) return WRC_Continue;
 103664   for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
 103665   if( pX==0 ) return WRC_Continue;
 103666   a = p->pOrderBy->a;
 103667   for(i=p->pOrderBy->nExpr-1; i>=0; i--){
 103668     if( a[i].pExpr->flags & EP_Collate ) break;
 103670   if( i<0 ) return WRC_Continue;
 103672   /* If we reach this point, that means the transformation is required. */
 103674   pParse = pWalker->pParse;
 103675   db = pParse->db;
 103676   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
 103677   if( pNew==0 ) return WRC_Abort;
 103678   memset(&dummy, 0, sizeof(dummy));
 103679   pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
 103680   if( pNewSrc==0 ) return WRC_Abort;
 103681   *pNew = *p;
 103682   p->pSrc = pNewSrc;
 103683   p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
 103684   p->op = TK_SELECT;
 103685   p->pWhere = 0;
 103686   pNew->pGroupBy = 0;
 103687   pNew->pHaving = 0;
 103688   pNew->pOrderBy = 0;
 103689   p->pPrior = 0;
 103690   p->pNext = 0;
 103691   p->selFlags &= ~SF_Compound;
 103692   assert( pNew->pPrior!=0 );
 103693   pNew->pPrior->pNext = pNew;
 103694   pNew->pLimit = 0;
 103695   pNew->pOffset = 0;
 103696   return WRC_Continue;
 103699 #ifndef SQLITE_OMIT_CTE
 103701 ** Argument pWith (which may be NULL) points to a linked list of nested 
 103702 ** WITH contexts, from inner to outermost. If the table identified by 
 103703 ** FROM clause element pItem is really a common-table-expression (CTE) 
 103704 ** then return a pointer to the CTE definition for that table. Otherwise
 103705 ** return NULL.
 103707 ** If a non-NULL value is returned, set *ppContext to point to the With
 103708 ** object that the returned CTE belongs to.
 103710 static struct Cte *searchWith(
 103711   With *pWith,                    /* Current outermost WITH clause */
 103712   struct SrcList_item *pItem,     /* FROM clause element to resolve */
 103713   With **ppContext                /* OUT: WITH clause return value belongs to */
 103715   const char *zName;
 103716   if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
 103717     With *p;
 103718     for(p=pWith; p; p=p->pOuter){
 103719       int i;
 103720       for(i=0; i<p->nCte; i++){
 103721         if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
 103722           *ppContext = p;
 103723           return &p->a[i];
 103728   return 0;
 103731 /* The code generator maintains a stack of active WITH clauses
 103732 ** with the inner-most WITH clause being at the top of the stack.
 103734 ** This routine pushes the WITH clause passed as the second argument
 103735 ** onto the top of the stack. If argument bFree is true, then this
 103736 ** WITH clause will never be popped from the stack. In this case it
 103737 ** should be freed along with the Parse object. In other cases, when
 103738 ** bFree==0, the With object will be freed along with the SELECT 
 103739 ** statement with which it is associated.
 103741 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
 103742   assert( bFree==0 || pParse->pWith==0 );
 103743   if( pWith ){
 103744     pWith->pOuter = pParse->pWith;
 103745     pParse->pWith = pWith;
 103746     pParse->bFreeWith = bFree;
 103751 ** This function checks if argument pFrom refers to a CTE declared by 
 103752 ** a WITH clause on the stack currently maintained by the parser. And,
 103753 ** if currently processing a CTE expression, if it is a recursive
 103754 ** reference to the current CTE.
 103756 ** If pFrom falls into either of the two categories above, pFrom->pTab
 103757 ** and other fields are populated accordingly. The caller should check
 103758 ** (pFrom->pTab!=0) to determine whether or not a successful match
 103759 ** was found.
 103761 ** Whether or not a match is found, SQLITE_OK is returned if no error
 103762 ** occurs. If an error does occur, an error message is stored in the
 103763 ** parser and some error code other than SQLITE_OK returned.
 103765 static int withExpand(
 103766   Walker *pWalker, 
 103767   struct SrcList_item *pFrom
 103769   Parse *pParse = pWalker->pParse;
 103770   sqlite3 *db = pParse->db;
 103771   struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
 103772   With *pWith;                    /* WITH clause that pCte belongs to */
 103774   assert( pFrom->pTab==0 );
 103776   pCte = searchWith(pParse->pWith, pFrom, &pWith);
 103777   if( pCte ){
 103778     Table *pTab;
 103779     ExprList *pEList;
 103780     Select *pSel;
 103781     Select *pLeft;                /* Left-most SELECT statement */
 103782     int bMayRecursive;            /* True if compound joined by UNION [ALL] */
 103783     With *pSavedWith;             /* Initial value of pParse->pWith */
 103785     /* If pCte->zErr is non-NULL at this point, then this is an illegal
 103786     ** recursive reference to CTE pCte. Leave an error in pParse and return
 103787     ** early. If pCte->zErr is NULL, then this is not a recursive reference.
 103788     ** In this case, proceed.  */
 103789     if( pCte->zErr ){
 103790       sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
 103791       return SQLITE_ERROR;
 103794     assert( pFrom->pTab==0 );
 103795     pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
 103796     if( pTab==0 ) return WRC_Abort;
 103797     pTab->nRef = 1;
 103798     pTab->zName = sqlite3DbStrDup(db, pCte->zName);
 103799     pTab->iPKey = -1;
 103800     pTab->nRowEst = 1048576;
 103801     pTab->tabFlags |= TF_Ephemeral;
 103802     pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
 103803     if( db->mallocFailed ) return SQLITE_NOMEM;
 103804     assert( pFrom->pSelect );
 103806     /* Check if this is a recursive CTE. */
 103807     pSel = pFrom->pSelect;
 103808     bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
 103809     if( bMayRecursive ){
 103810       int i;
 103811       SrcList *pSrc = pFrom->pSelect->pSrc;
 103812       for(i=0; i<pSrc->nSrc; i++){
 103813         struct SrcList_item *pItem = &pSrc->a[i];
 103814         if( pItem->zDatabase==0 
 103815          && pItem->zName!=0 
 103816          && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
 103818           pItem->pTab = pTab;
 103819           pItem->isRecursive = 1;
 103820           pTab->nRef++;
 103821           pSel->selFlags |= SF_Recursive;
 103826     /* Only one recursive reference is permitted. */ 
 103827     if( pTab->nRef>2 ){
 103828       sqlite3ErrorMsg(
 103829           pParse, "multiple references to recursive table: %s", pCte->zName
 103831       return SQLITE_ERROR;
 103833     assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
 103835     pCte->zErr = "circular reference: %s";
 103836     pSavedWith = pParse->pWith;
 103837     pParse->pWith = pWith;
 103838     sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
 103840     for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
 103841     pEList = pLeft->pEList;
 103842     if( pCte->pCols ){
 103843       if( pEList->nExpr!=pCte->pCols->nExpr ){
 103844         sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
 103845             pCte->zName, pEList->nExpr, pCte->pCols->nExpr
 103847         pParse->pWith = pSavedWith;
 103848         return SQLITE_ERROR;
 103850       pEList = pCte->pCols;
 103853     selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
 103854     if( bMayRecursive ){
 103855       if( pSel->selFlags & SF_Recursive ){
 103856         pCte->zErr = "multiple recursive references: %s";
 103857       }else{
 103858         pCte->zErr = "recursive reference in a subquery: %s";
 103860       sqlite3WalkSelect(pWalker, pSel);
 103862     pCte->zErr = 0;
 103863     pParse->pWith = pSavedWith;
 103866   return SQLITE_OK;
 103868 #endif
 103870 #ifndef SQLITE_OMIT_CTE
 103872 ** If the SELECT passed as the second argument has an associated WITH 
 103873 ** clause, pop it from the stack stored as part of the Parse object.
 103875 ** This function is used as the xSelectCallback2() callback by
 103876 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
 103877 ** names and other FROM clause elements. 
 103879 static void selectPopWith(Walker *pWalker, Select *p){
 103880   Parse *pParse = pWalker->pParse;
 103881   With *pWith = findRightmost(p)->pWith;
 103882   if( pWith!=0 ){
 103883     assert( pParse->pWith==pWith );
 103884     pParse->pWith = pWith->pOuter;
 103887 #else
 103888 #define selectPopWith 0
 103889 #endif
 103892 ** This routine is a Walker callback for "expanding" a SELECT statement.
 103893 ** "Expanding" means to do the following:
 103895 **    (1)  Make sure VDBE cursor numbers have been assigned to every
 103896 **         element of the FROM clause.
 103898 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
 103899 **         defines FROM clause.  When views appear in the FROM clause,
 103900 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
 103901 **         that implements the view.  A copy is made of the view's SELECT
 103902 **         statement so that we can freely modify or delete that statement
 103903 **         without worrying about messing up the presistent representation
 103904 **         of the view.
 103906 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
 103907 **         on joins and the ON and USING clause of joins.
 103909 **    (4)  Scan the list of columns in the result set (pEList) looking
 103910 **         for instances of the "*" operator or the TABLE.* operator.
 103911 **         If found, expand each "*" to be every column in every table
 103912 **         and TABLE.* to be every column in TABLE.
 103915 static int selectExpander(Walker *pWalker, Select *p){
 103916   Parse *pParse = pWalker->pParse;
 103917   int i, j, k;
 103918   SrcList *pTabList;
 103919   ExprList *pEList;
 103920   struct SrcList_item *pFrom;
 103921   sqlite3 *db = pParse->db;
 103922   Expr *pE, *pRight, *pExpr;
 103923   u16 selFlags = p->selFlags;
 103925   p->selFlags |= SF_Expanded;
 103926   if( db->mallocFailed  ){
 103927     return WRC_Abort;
 103929   if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
 103930     return WRC_Prune;
 103932   pTabList = p->pSrc;
 103933   pEList = p->pEList;
 103934   sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
 103936   /* Make sure cursor numbers have been assigned to all entries in
 103937   ** the FROM clause of the SELECT statement.
 103939   sqlite3SrcListAssignCursors(pParse, pTabList);
 103941   /* Look up every table named in the FROM clause of the select.  If
 103942   ** an entry of the FROM clause is a subquery instead of a table or view,
 103943   ** then create a transient table structure to describe the subquery.
 103945   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 103946     Table *pTab;
 103947     assert( pFrom->isRecursive==0 || pFrom->pTab );
 103948     if( pFrom->isRecursive ) continue;
 103949     if( pFrom->pTab!=0 ){
 103950       /* This statement has already been prepared.  There is no need
 103951       ** to go further. */
 103952       assert( i==0 );
 103953 #ifndef SQLITE_OMIT_CTE
 103954       selectPopWith(pWalker, p);
 103955 #endif
 103956       return WRC_Prune;
 103958 #ifndef SQLITE_OMIT_CTE
 103959     if( withExpand(pWalker, pFrom) ) return WRC_Abort;
 103960     if( pFrom->pTab ) {} else
 103961 #endif
 103962     if( pFrom->zName==0 ){
 103963 #ifndef SQLITE_OMIT_SUBQUERY
 103964       Select *pSel = pFrom->pSelect;
 103965       /* A sub-query in the FROM clause of a SELECT */
 103966       assert( pSel!=0 );
 103967       assert( pFrom->pTab==0 );
 103968       sqlite3WalkSelect(pWalker, pSel);
 103969       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
 103970       if( pTab==0 ) return WRC_Abort;
 103971       pTab->nRef = 1;
 103972       pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
 103973       while( pSel->pPrior ){ pSel = pSel->pPrior; }
 103974       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
 103975       pTab->iPKey = -1;
 103976       pTab->nRowEst = 1048576;
 103977       pTab->tabFlags |= TF_Ephemeral;
 103978 #endif
 103979     }else{
 103980       /* An ordinary table or view name in the FROM clause */
 103981       assert( pFrom->pTab==0 );
 103982       pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
 103983       if( pTab==0 ) return WRC_Abort;
 103984       if( pTab->nRef==0xffff ){
 103985         sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
 103986            pTab->zName);
 103987         pFrom->pTab = 0;
 103988         return WRC_Abort;
 103990       pTab->nRef++;
 103991 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
 103992       if( pTab->pSelect || IsVirtual(pTab) ){
 103993         /* We reach here if the named table is a really a view */
 103994         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
 103995         assert( pFrom->pSelect==0 );
 103996         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
 103997         sqlite3WalkSelect(pWalker, pFrom->pSelect);
 103999 #endif
 104002     /* Locate the index named by the INDEXED BY clause, if any. */
 104003     if( sqlite3IndexedByLookup(pParse, pFrom) ){
 104004       return WRC_Abort;
 104008   /* Process NATURAL keywords, and ON and USING clauses of joins.
 104010   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
 104011     return WRC_Abort;
 104014   /* For every "*" that occurs in the column list, insert the names of
 104015   ** all columns in all tables.  And for every TABLE.* insert the names
 104016   ** of all columns in TABLE.  The parser inserted a special expression
 104017   ** with the TK_ALL operator for each "*" that it found in the column list.
 104018   ** The following code just has to locate the TK_ALL expressions and expand
 104019   ** each one to the list of all columns in all tables.
 104021   ** The first loop just checks to see if there are any "*" operators
 104022   ** that need expanding.
 104024   for(k=0; k<pEList->nExpr; k++){
 104025     pE = pEList->a[k].pExpr;
 104026     if( pE->op==TK_ALL ) break;
 104027     assert( pE->op!=TK_DOT || pE->pRight!=0 );
 104028     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
 104029     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
 104031   if( k<pEList->nExpr ){
 104033     ** If we get here it means the result set contains one or more "*"
 104034     ** operators that need to be expanded.  Loop through each expression
 104035     ** in the result set and expand them one by one.
 104037     struct ExprList_item *a = pEList->a;
 104038     ExprList *pNew = 0;
 104039     int flags = pParse->db->flags;
 104040     int longNames = (flags & SQLITE_FullColNames)!=0
 104041                       && (flags & SQLITE_ShortColNames)==0;
 104043     /* When processing FROM-clause subqueries, it is always the case
 104044     ** that full_column_names=OFF and short_column_names=ON.  The
 104045     ** sqlite3ResultSetOfSelect() routine makes it so. */
 104046     assert( (p->selFlags & SF_NestedFrom)==0
 104047           || ((flags & SQLITE_FullColNames)==0 &&
 104048               (flags & SQLITE_ShortColNames)!=0) );
 104050     for(k=0; k<pEList->nExpr; k++){
 104051       pE = a[k].pExpr;
 104052       pRight = pE->pRight;
 104053       assert( pE->op!=TK_DOT || pRight!=0 );
 104054       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
 104055         /* This particular expression does not need to be expanded.
 104057         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
 104058         if( pNew ){
 104059           pNew->a[pNew->nExpr-1].zName = a[k].zName;
 104060           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
 104061           a[k].zName = 0;
 104062           a[k].zSpan = 0;
 104064         a[k].pExpr = 0;
 104065       }else{
 104066         /* This expression is a "*" or a "TABLE.*" and needs to be
 104067         ** expanded. */
 104068         int tableSeen = 0;      /* Set to 1 when TABLE matches */
 104069         char *zTName = 0;       /* text of name of TABLE */
 104070         if( pE->op==TK_DOT ){
 104071           assert( pE->pLeft!=0 );
 104072           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
 104073           zTName = pE->pLeft->u.zToken;
 104075         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 104076           Table *pTab = pFrom->pTab;
 104077           Select *pSub = pFrom->pSelect;
 104078           char *zTabName = pFrom->zAlias;
 104079           const char *zSchemaName = 0;
 104080           int iDb;
 104081           if( zTabName==0 ){
 104082             zTabName = pTab->zName;
 104084           if( db->mallocFailed ) break;
 104085           if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
 104086             pSub = 0;
 104087             if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
 104088               continue;
 104090             iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 104091             zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
 104093           for(j=0; j<pTab->nCol; j++){
 104094             char *zName = pTab->aCol[j].zName;
 104095             char *zColname;  /* The computed column name */
 104096             char *zToFree;   /* Malloced string that needs to be freed */
 104097             Token sColname;  /* Computed column name as a token */
 104099             assert( zName );
 104100             if( zTName && pSub
 104101              && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
 104103               continue;
 104106             /* If a column is marked as 'hidden' (currently only possible
 104107             ** for virtual tables), do not include it in the expanded
 104108             ** result-set list.
 104110             if( IsHiddenColumn(&pTab->aCol[j]) ){
 104111               assert(IsVirtual(pTab));
 104112               continue;
 104114             tableSeen = 1;
 104116             if( i>0 && zTName==0 ){
 104117               if( (pFrom->jointype & JT_NATURAL)!=0
 104118                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
 104120                 /* In a NATURAL join, omit the join columns from the 
 104121                 ** table to the right of the join */
 104122                 continue;
 104124               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
 104125                 /* In a join with a USING clause, omit columns in the
 104126                 ** using clause from the table on the right. */
 104127                 continue;
 104130             pRight = sqlite3Expr(db, TK_ID, zName);
 104131             zColname = zName;
 104132             zToFree = 0;
 104133             if( longNames || pTabList->nSrc>1 ){
 104134               Expr *pLeft;
 104135               pLeft = sqlite3Expr(db, TK_ID, zTabName);
 104136               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
 104137               if( zSchemaName ){
 104138                 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
 104139                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
 104141               if( longNames ){
 104142                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
 104143                 zToFree = zColname;
 104145             }else{
 104146               pExpr = pRight;
 104148             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
 104149             sColname.z = zColname;
 104150             sColname.n = sqlite3Strlen30(zColname);
 104151             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
 104152             if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
 104153               struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
 104154               if( pSub ){
 104155                 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
 104156                 testcase( pX->zSpan==0 );
 104157               }else{
 104158                 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
 104159                                            zSchemaName, zTabName, zColname);
 104160                 testcase( pX->zSpan==0 );
 104162               pX->bSpanIsTab = 1;
 104164             sqlite3DbFree(db, zToFree);
 104167         if( !tableSeen ){
 104168           if( zTName ){
 104169             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
 104170           }else{
 104171             sqlite3ErrorMsg(pParse, "no tables specified");
 104176     sqlite3ExprListDelete(db, pEList);
 104177     p->pEList = pNew;
 104179 #if SQLITE_MAX_COLUMN
 104180   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
 104181     sqlite3ErrorMsg(pParse, "too many columns in result set");
 104183 #endif
 104184   return WRC_Continue;
 104188 ** No-op routine for the parse-tree walker.
 104190 ** When this routine is the Walker.xExprCallback then expression trees
 104191 ** are walked without any actions being taken at each node.  Presumably,
 104192 ** when this routine is used for Walker.xExprCallback then 
 104193 ** Walker.xSelectCallback is set to do something useful for every 
 104194 ** subquery in the parser tree.
 104196 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
 104197   UNUSED_PARAMETER2(NotUsed, NotUsed2);
 104198   return WRC_Continue;
 104202 ** This routine "expands" a SELECT statement and all of its subqueries.
 104203 ** For additional information on what it means to "expand" a SELECT
 104204 ** statement, see the comment on the selectExpand worker callback above.
 104206 ** Expanding a SELECT statement is the first step in processing a
 104207 ** SELECT statement.  The SELECT statement must be expanded before
 104208 ** name resolution is performed.
 104210 ** If anything goes wrong, an error message is written into pParse.
 104211 ** The calling function can detect the problem by looking at pParse->nErr
 104212 ** and/or pParse->db->mallocFailed.
 104214 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
 104215   Walker w;
 104216   memset(&w, 0, sizeof(w));
 104217   w.xExprCallback = exprWalkNoop;
 104218   w.pParse = pParse;
 104219   if( pParse->hasCompound ){
 104220     w.xSelectCallback = convertCompoundSelectToSubquery;
 104221     sqlite3WalkSelect(&w, pSelect);
 104223   w.xSelectCallback = selectExpander;
 104224   w.xSelectCallback2 = selectPopWith;
 104225   sqlite3WalkSelect(&w, pSelect);
 104229 #ifndef SQLITE_OMIT_SUBQUERY
 104231 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
 104232 ** interface.
 104234 ** For each FROM-clause subquery, add Column.zType and Column.zColl
 104235 ** information to the Table structure that represents the result set
 104236 ** of that subquery.
 104238 ** The Table structure that represents the result set was constructed
 104239 ** by selectExpander() but the type and collation information was omitted
 104240 ** at that point because identifiers had not yet been resolved.  This
 104241 ** routine is called after identifier resolution.
 104243 static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
 104244   Parse *pParse;
 104245   int i;
 104246   SrcList *pTabList;
 104247   struct SrcList_item *pFrom;
 104249   assert( p->selFlags & SF_Resolved );
 104250   if( (p->selFlags & SF_HasTypeInfo)==0 ){
 104251     p->selFlags |= SF_HasTypeInfo;
 104252     pParse = pWalker->pParse;
 104253     pTabList = p->pSrc;
 104254     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
 104255       Table *pTab = pFrom->pTab;
 104256       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
 104257         /* A sub-query in the FROM clause of a SELECT */
 104258         Select *pSel = pFrom->pSelect;
 104259         if( pSel ){
 104260           while( pSel->pPrior ) pSel = pSel->pPrior;
 104261           selectAddColumnTypeAndCollation(pParse, pTab, pSel);
 104267 #endif
 104271 ** This routine adds datatype and collating sequence information to
 104272 ** the Table structures of all FROM-clause subqueries in a
 104273 ** SELECT statement.
 104275 ** Use this routine after name resolution.
 104277 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
 104278 #ifndef SQLITE_OMIT_SUBQUERY
 104279   Walker w;
 104280   memset(&w, 0, sizeof(w));
 104281   w.xSelectCallback2 = selectAddSubqueryTypeInfo;
 104282   w.xExprCallback = exprWalkNoop;
 104283   w.pParse = pParse;
 104284   sqlite3WalkSelect(&w, pSelect);
 104285 #endif
 104290 ** This routine sets up a SELECT statement for processing.  The
 104291 ** following is accomplished:
 104293 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
 104294 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
 104295 **     *  ON and USING clauses are shifted into WHERE statements
 104296 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
 104297 **     *  Identifiers in expression are matched to tables.
 104299 ** This routine acts recursively on all subqueries within the SELECT.
 104301 SQLITE_PRIVATE void sqlite3SelectPrep(
 104302   Parse *pParse,         /* The parser context */
 104303   Select *p,             /* The SELECT statement being coded. */
 104304   NameContext *pOuterNC  /* Name context for container */
 104306   sqlite3 *db;
 104307   if( NEVER(p==0) ) return;
 104308   db = pParse->db;
 104309   if( db->mallocFailed ) return;
 104310   if( p->selFlags & SF_HasTypeInfo ) return;
 104311   sqlite3SelectExpand(pParse, p);
 104312   if( pParse->nErr || db->mallocFailed ) return;
 104313   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
 104314   if( pParse->nErr || db->mallocFailed ) return;
 104315   sqlite3SelectAddTypeInfo(pParse, p);
 104319 ** Reset the aggregate accumulator.
 104321 ** The aggregate accumulator is a set of memory cells that hold
 104322 ** intermediate results while calculating an aggregate.  This
 104323 ** routine generates code that stores NULLs in all of those memory
 104324 ** cells.
 104326 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
 104327   Vdbe *v = pParse->pVdbe;
 104328   int i;
 104329   struct AggInfo_func *pFunc;
 104330   int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
 104331   if( nReg==0 ) return;
 104332 #ifdef SQLITE_DEBUG
 104333   /* Verify that all AggInfo registers are within the range specified by
 104334   ** AggInfo.mnReg..AggInfo.mxReg */
 104335   assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
 104336   for(i=0; i<pAggInfo->nColumn; i++){
 104337     assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
 104338          && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
 104340   for(i=0; i<pAggInfo->nFunc; i++){
 104341     assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
 104342          && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
 104344 #endif
 104345   sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
 104346   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
 104347     if( pFunc->iDistinct>=0 ){
 104348       Expr *pE = pFunc->pExpr;
 104349       assert( !ExprHasProperty(pE, EP_xIsSelect) );
 104350       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
 104351         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
 104352            "argument");
 104353         pFunc->iDistinct = -1;
 104354       }else{
 104355         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0);
 104356         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
 104357                           (char*)pKeyInfo, P4_KEYINFO);
 104364 ** Invoke the OP_AggFinalize opcode for every aggregate function
 104365 ** in the AggInfo structure.
 104367 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
 104368   Vdbe *v = pParse->pVdbe;
 104369   int i;
 104370   struct AggInfo_func *pF;
 104371   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 104372     ExprList *pList = pF->pExpr->x.pList;
 104373     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 104374     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
 104375                       (void*)pF->pFunc, P4_FUNCDEF);
 104380 ** Update the accumulator memory cells for an aggregate based on
 104381 ** the current cursor position.
 104383 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
 104384   Vdbe *v = pParse->pVdbe;
 104385   int i;
 104386   int regHit = 0;
 104387   int addrHitTest = 0;
 104388   struct AggInfo_func *pF;
 104389   struct AggInfo_col *pC;
 104391   pAggInfo->directMode = 1;
 104392   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
 104393     int nArg;
 104394     int addrNext = 0;
 104395     int regAgg;
 104396     ExprList *pList = pF->pExpr->x.pList;
 104397     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
 104398     if( pList ){
 104399       nArg = pList->nExpr;
 104400       regAgg = sqlite3GetTempRange(pParse, nArg);
 104401       sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
 104402     }else{
 104403       nArg = 0;
 104404       regAgg = 0;
 104406     if( pF->iDistinct>=0 ){
 104407       addrNext = sqlite3VdbeMakeLabel(v);
 104408       assert( nArg==1 );
 104409       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
 104411     if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
 104412       CollSeq *pColl = 0;
 104413       struct ExprList_item *pItem;
 104414       int j;
 104415       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
 104416       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
 104417         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
 104419       if( !pColl ){
 104420         pColl = pParse->db->pDfltColl;
 104422       if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
 104423       sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
 104425     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
 104426                       (void*)pF->pFunc, P4_FUNCDEF);
 104427     sqlite3VdbeChangeP5(v, (u8)nArg);
 104428     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
 104429     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
 104430     if( addrNext ){
 104431       sqlite3VdbeResolveLabel(v, addrNext);
 104432       sqlite3ExprCacheClear(pParse);
 104436   /* Before populating the accumulator registers, clear the column cache.
 104437   ** Otherwise, if any of the required column values are already present 
 104438   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
 104439   ** to pC->iMem. But by the time the value is used, the original register
 104440   ** may have been used, invalidating the underlying buffer holding the
 104441   ** text or blob value. See ticket [883034dcb5].
 104443   ** Another solution would be to change the OP_SCopy used to copy cached
 104444   ** values to an OP_Copy.
 104446   if( regHit ){
 104447     addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
 104449   sqlite3ExprCacheClear(pParse);
 104450   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
 104451     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
 104453   pAggInfo->directMode = 0;
 104454   sqlite3ExprCacheClear(pParse);
 104455   if( addrHitTest ){
 104456     sqlite3VdbeJumpHere(v, addrHitTest);
 104461 ** Add a single OP_Explain instruction to the VDBE to explain a simple
 104462 ** count(*) query ("SELECT count(*) FROM pTab").
 104464 #ifndef SQLITE_OMIT_EXPLAIN
 104465 static void explainSimpleCount(
 104466   Parse *pParse,                  /* Parse context */
 104467   Table *pTab,                    /* Table being queried */
 104468   Index *pIdx                     /* Index used to optimize scan, or NULL */
 104470   if( pParse->explain==2 ){
 104471     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
 104472         pTab->zName, 
 104473         pIdx ? " USING COVERING INDEX " : "",
 104474         pIdx ? pIdx->zName : ""
 104476     sqlite3VdbeAddOp4(
 104477         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
 104481 #else
 104482 # define explainSimpleCount(a,b,c)
 104483 #endif
 104486 ** Generate code for the SELECT statement given in the p argument.  
 104488 ** The results are returned according to the SelectDest structure.
 104489 ** See comments in sqliteInt.h for further information.
 104491 ** This routine returns the number of errors.  If any errors are
 104492 ** encountered, then an appropriate error message is left in
 104493 ** pParse->zErrMsg.
 104495 ** This routine does NOT free the Select structure passed in.  The
 104496 ** calling function needs to do that.
 104498 SQLITE_PRIVATE int sqlite3Select(
 104499   Parse *pParse,         /* The parser context */
 104500   Select *p,             /* The SELECT statement being coded. */
 104501   SelectDest *pDest      /* What to do with the query results */
 104503   int i, j;              /* Loop counters */
 104504   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
 104505   Vdbe *v;               /* The virtual machine under construction */
 104506   int isAgg;             /* True for select lists like "count(*)" */
 104507   ExprList *pEList;      /* List of columns to extract. */
 104508   SrcList *pTabList;     /* List of tables to select from */
 104509   Expr *pWhere;          /* The WHERE clause.  May be NULL */
 104510   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
 104511   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
 104512   Expr *pHaving;         /* The HAVING clause.  May be NULL */
 104513   int rc = 1;            /* Value to return from this function */
 104514   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
 104515   DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
 104516   AggInfo sAggInfo;      /* Information used by aggregate queries */
 104517   int iEnd;              /* Address of the end of the query */
 104518   sqlite3 *db;           /* The database connection */
 104520 #ifndef SQLITE_OMIT_EXPLAIN
 104521   int iRestoreSelectId = pParse->iSelectId;
 104522   pParse->iSelectId = pParse->iNextSelectId++;
 104523 #endif
 104525   db = pParse->db;
 104526   if( p==0 || db->mallocFailed || pParse->nErr ){
 104527     return 1;
 104529   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
 104530   memset(&sAggInfo, 0, sizeof(sAggInfo));
 104532   if( IgnorableOrderby(pDest) ){
 104533     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
 104534            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
 104535     /* If ORDER BY makes no difference in the output then neither does
 104536     ** DISTINCT so it can be removed too. */
 104537     sqlite3ExprListDelete(db, p->pOrderBy);
 104538     p->pOrderBy = 0;
 104539     p->selFlags &= ~SF_Distinct;
 104541   sqlite3SelectPrep(pParse, p, 0);
 104542   pOrderBy = p->pOrderBy;
 104543   pTabList = p->pSrc;
 104544   pEList = p->pEList;
 104545   if( pParse->nErr || db->mallocFailed ){
 104546     goto select_end;
 104548   isAgg = (p->selFlags & SF_Aggregate)!=0;
 104549   assert( pEList!=0 );
 104551   /* Begin generating code.
 104553   v = sqlite3GetVdbe(pParse);
 104554   if( v==0 ) goto select_end;
 104556   /* If writing to memory or generating a set
 104557   ** only a single column may be output.
 104559 #ifndef SQLITE_OMIT_SUBQUERY
 104560   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
 104561     goto select_end;
 104563 #endif
 104565   /* Generate code for all sub-queries in the FROM clause
 104567 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
 104568   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
 104569     struct SrcList_item *pItem = &pTabList->a[i];
 104570     SelectDest dest;
 104571     Select *pSub = pItem->pSelect;
 104572     int isAggSub;
 104574     if( pSub==0 ) continue;
 104576     /* Sometimes the code for a subquery will be generated more than
 104577     ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
 104578     ** for example.  In that case, do not regenerate the code to manifest
 104579     ** a view or the co-routine to implement a view.  The first instance
 104580     ** is sufficient, though the subroutine to manifest the view does need
 104581     ** to be invoked again. */
 104582     if( pItem->addrFillSub ){
 104583       if( pItem->viaCoroutine==0 ){
 104584         sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
 104586       continue;
 104589     /* Increment Parse.nHeight by the height of the largest expression
 104590     ** tree referred to by this, the parent select. The child select
 104591     ** may contain expression trees of at most
 104592     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
 104593     ** more conservative than necessary, but much easier than enforcing
 104594     ** an exact limit.
 104596     pParse->nHeight += sqlite3SelectExprHeight(p);
 104598     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
 104599     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
 104600       /* This subquery can be absorbed into its parent. */
 104601       if( isAggSub ){
 104602         isAgg = 1;
 104603         p->selFlags |= SF_Aggregate;
 104605       i = -1;
 104606     }else if( pTabList->nSrc==1
 104607            && OptimizationEnabled(db, SQLITE_SubqCoroutine)
 104609       /* Implement a co-routine that will return a single row of the result
 104610       ** set on each invocation.
 104612       int addrTop = sqlite3VdbeCurrentAddr(v)+1;
 104613       pItem->regReturn = ++pParse->nMem;
 104614       sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
 104615       VdbeComment((v, "%s", pItem->pTab->zName));
 104616       pItem->addrFillSub = addrTop;
 104617       sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
 104618       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 104619       sqlite3Select(pParse, pSub, &dest);
 104620       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 104621       pItem->viaCoroutine = 1;
 104622       pItem->regResult = dest.iSdst;
 104623       sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
 104624       sqlite3VdbeJumpHere(v, addrTop-1);
 104625       sqlite3ClearTempRegCache(pParse);
 104626     }else{
 104627       /* Generate a subroutine that will fill an ephemeral table with
 104628       ** the content of this subquery.  pItem->addrFillSub will point
 104629       ** to the address of the generated subroutine.  pItem->regReturn
 104630       ** is a register allocated to hold the subroutine return address
 104632       int topAddr;
 104633       int onceAddr = 0;
 104634       int retAddr;
 104635       assert( pItem->addrFillSub==0 );
 104636       pItem->regReturn = ++pParse->nMem;
 104637       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
 104638       pItem->addrFillSub = topAddr+1;
 104639       if( pItem->isCorrelated==0 ){
 104640         /* If the subquery is not correlated and if we are not inside of
 104641         ** a trigger, then we only need to compute the value of the subquery
 104642         ** once. */
 104643         onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 104644         VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
 104645       }else{
 104646         VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
 104648       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
 104649       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
 104650       sqlite3Select(pParse, pSub, &dest);
 104651       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
 104652       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
 104653       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
 104654       VdbeComment((v, "end %s", pItem->pTab->zName));
 104655       sqlite3VdbeChangeP1(v, topAddr, retAddr);
 104656       sqlite3ClearTempRegCache(pParse);
 104658     if( /*pParse->nErr ||*/ db->mallocFailed ){
 104659       goto select_end;
 104661     pParse->nHeight -= sqlite3SelectExprHeight(p);
 104662     pTabList = p->pSrc;
 104663     if( !IgnorableOrderby(pDest) ){
 104664       pOrderBy = p->pOrderBy;
 104667   pEList = p->pEList;
 104668 #endif
 104669   pWhere = p->pWhere;
 104670   pGroupBy = p->pGroupBy;
 104671   pHaving = p->pHaving;
 104672   sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
 104674 #ifndef SQLITE_OMIT_COMPOUND_SELECT
 104675   /* If there is are a sequence of queries, do the earlier ones first.
 104677   if( p->pPrior ){
 104678     rc = multiSelect(pParse, p, pDest);
 104679     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 104680     return rc;
 104682 #endif
 104684   /* If there is both a GROUP BY and an ORDER BY clause and they are
 104685   ** identical, then disable the ORDER BY clause since the GROUP BY
 104686   ** will cause elements to come out in the correct order.  This is
 104687   ** an optimization - the correct answer should result regardless.
 104688   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
 104689   ** to disable this optimization for testing purposes.
 104691   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy, -1)==0
 104692          && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
 104693     pOrderBy = 0;
 104696   /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
 104697   ** if the select-list is the same as the ORDER BY list, then this query
 104698   ** can be rewritten as a GROUP BY. In other words, this:
 104700   **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
 104702   ** is transformed to:
 104704   **     SELECT xyz FROM ... GROUP BY xyz
 104706   ** The second form is preferred as a single index (or temp-table) may be 
 104707   ** used for both the ORDER BY and DISTINCT processing. As originally 
 104708   ** written the query must use a temp-table for at least one of the ORDER 
 104709   ** BY and DISTINCT, and an index or separate temp-table for the other.
 104711   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
 104712    && sqlite3ExprListCompare(pOrderBy, p->pEList, -1)==0
 104714     p->selFlags &= ~SF_Distinct;
 104715     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
 104716     pGroupBy = p->pGroupBy;
 104717     pOrderBy = 0;
 104718     /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
 104719     ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
 104720     ** original setting of the SF_Distinct flag, not the current setting */
 104721     assert( sDistinct.isTnct );
 104724   /* If there is an ORDER BY clause, then this sorting
 104725   ** index might end up being unused if the data can be 
 104726   ** extracted in pre-sorted order.  If that is the case, then the
 104727   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
 104728   ** we figure out that the sorting index is not needed.  The addrSortIndex
 104729   ** variable is used to facilitate that change.
 104731   if( pOrderBy ){
 104732     KeyInfo *pKeyInfo;
 104733     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 0);
 104734     pOrderBy->iECursor = pParse->nTab++;
 104735     p->addrOpenEphm[2] = addrSortIndex =
 104736       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 104737                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
 104738                            (char*)pKeyInfo, P4_KEYINFO);
 104739   }else{
 104740     addrSortIndex = -1;
 104743   /* If the output is destined for a temporary table, open that table.
 104745   if( pDest->eDest==SRT_EphemTab ){
 104746     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
 104749   /* Set the limiter.
 104751   iEnd = sqlite3VdbeMakeLabel(v);
 104752   p->nSelectRow = LARGEST_INT64;
 104753   computeLimitRegisters(pParse, p, iEnd);
 104754   if( p->iLimit==0 && addrSortIndex>=0 ){
 104755     sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
 104756     p->selFlags |= SF_UseSorter;
 104759   /* Open a virtual index to use for the distinct set.
 104761   if( p->selFlags & SF_Distinct ){
 104762     sDistinct.tabTnct = pParse->nTab++;
 104763     sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
 104764                                 sDistinct.tabTnct, 0, 0,
 104765                                 (char*)keyInfoFromExprList(pParse, p->pEList, 0),
 104766                                 P4_KEYINFO);
 104767     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 104768     sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
 104769   }else{
 104770     sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
 104773   if( !isAgg && pGroupBy==0 ){
 104774     /* No aggregate functions and no GROUP BY clause */
 104775     u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
 104777     /* Begin the database scan. */
 104778     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, p->pEList,
 104779                                wctrlFlags, 0);
 104780     if( pWInfo==0 ) goto select_end;
 104781     if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
 104782       p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
 104784     if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
 104785       sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
 104787     if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
 104789     /* If sorting index that was created by a prior OP_OpenEphemeral 
 104790     ** instruction ended up not being needed, then change the OP_OpenEphemeral
 104791     ** into an OP_Noop.
 104793     if( addrSortIndex>=0 && pOrderBy==0 ){
 104794       sqlite3VdbeChangeToNoop(v, addrSortIndex);
 104795       p->addrOpenEphm[2] = -1;
 104798     /* Use the standard inner loop. */
 104799     selectInnerLoop(pParse, p, pEList, -1, pOrderBy, &sDistinct, pDest,
 104800                     sqlite3WhereContinueLabel(pWInfo),
 104801                     sqlite3WhereBreakLabel(pWInfo));
 104803     /* End the database scan loop.
 104805     sqlite3WhereEnd(pWInfo);
 104806   }else{
 104807     /* This case when there exist aggregate functions or a GROUP BY clause
 104808     ** or both */
 104809     NameContext sNC;    /* Name context for processing aggregate information */
 104810     int iAMem;          /* First Mem address for storing current GROUP BY */
 104811     int iBMem;          /* First Mem address for previous GROUP BY */
 104812     int iUseFlag;       /* Mem address holding flag indicating that at least
 104813                         ** one row of the input to the aggregator has been
 104814                         ** processed */
 104815     int iAbortFlag;     /* Mem address which causes query abort if positive */
 104816     int groupBySort;    /* Rows come from source in GROUP BY order */
 104817     int addrEnd;        /* End of processing for this SELECT */
 104818     int sortPTab = 0;   /* Pseudotable used to decode sorting results */
 104819     int sortOut = 0;    /* Output register from the sorter */
 104821     /* Remove any and all aliases between the result set and the
 104822     ** GROUP BY clause.
 104824     if( pGroupBy ){
 104825       int k;                        /* Loop counter */
 104826       struct ExprList_item *pItem;  /* For looping over expression in a list */
 104828       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
 104829         pItem->u.x.iAlias = 0;
 104831       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
 104832         pItem->u.x.iAlias = 0;
 104834       if( p->nSelectRow>100 ) p->nSelectRow = 100;
 104835     }else{
 104836       p->nSelectRow = 1;
 104840     /* Create a label to jump to when we want to abort the query */
 104841     addrEnd = sqlite3VdbeMakeLabel(v);
 104843     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
 104844     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
 104845     ** SELECT statement.
 104847     memset(&sNC, 0, sizeof(sNC));
 104848     sNC.pParse = pParse;
 104849     sNC.pSrcList = pTabList;
 104850     sNC.pAggInfo = &sAggInfo;
 104851     sAggInfo.mnReg = pParse->nMem+1;
 104852     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
 104853     sAggInfo.pGroupBy = pGroupBy;
 104854     sqlite3ExprAnalyzeAggList(&sNC, pEList);
 104855     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
 104856     if( pHaving ){
 104857       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
 104859     sAggInfo.nAccumulator = sAggInfo.nColumn;
 104860     for(i=0; i<sAggInfo.nFunc; i++){
 104861       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
 104862       sNC.ncFlags |= NC_InAggFunc;
 104863       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
 104864       sNC.ncFlags &= ~NC_InAggFunc;
 104866     sAggInfo.mxReg = pParse->nMem;
 104867     if( db->mallocFailed ) goto select_end;
 104869     /* Processing for aggregates with GROUP BY is very different and
 104870     ** much more complex than aggregates without a GROUP BY.
 104872     if( pGroupBy ){
 104873       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
 104874       int j1;             /* A-vs-B comparision jump */
 104875       int addrOutputRow;  /* Start of subroutine that outputs a result row */
 104876       int regOutputRow;   /* Return address register for output subroutine */
 104877       int addrSetAbort;   /* Set the abort flag and return */
 104878       int addrTopOfLoop;  /* Top of the input loop */
 104879       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
 104880       int addrReset;      /* Subroutine for resetting the accumulator */
 104881       int regReset;       /* Return address register for reset subroutine */
 104883       /* If there is a GROUP BY clause we might need a sorting index to
 104884       ** implement it.  Allocate that sorting index now.  If it turns out
 104885       ** that we do not need it after all, the OP_SorterOpen instruction
 104886       ** will be converted into a Noop.  
 104888       sAggInfo.sortingIdx = pParse->nTab++;
 104889       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0);
 104890       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
 104891           sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
 104892           0, (char*)pKeyInfo, P4_KEYINFO);
 104894       /* Initialize memory locations used by GROUP BY aggregate processing
 104896       iUseFlag = ++pParse->nMem;
 104897       iAbortFlag = ++pParse->nMem;
 104898       regOutputRow = ++pParse->nMem;
 104899       addrOutputRow = sqlite3VdbeMakeLabel(v);
 104900       regReset = ++pParse->nMem;
 104901       addrReset = sqlite3VdbeMakeLabel(v);
 104902       iAMem = pParse->nMem + 1;
 104903       pParse->nMem += pGroupBy->nExpr;
 104904       iBMem = pParse->nMem + 1;
 104905       pParse->nMem += pGroupBy->nExpr;
 104906       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
 104907       VdbeComment((v, "clear abort flag"));
 104908       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
 104909       VdbeComment((v, "indicate accumulator empty"));
 104910       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
 104912       /* Begin a loop that will extract all source rows in GROUP BY order.
 104913       ** This might involve two separate loops with an OP_Sort in between, or
 104914       ** it might be a single loop that uses an index to extract information
 104915       ** in the right order to begin with.
 104917       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 104918       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 
 104919                                  WHERE_GROUPBY, 0);
 104920       if( pWInfo==0 ) goto select_end;
 104921       if( sqlite3WhereIsOrdered(pWInfo) ){
 104922         /* The optimizer is able to deliver rows in group by order so
 104923         ** we do not have to sort.  The OP_OpenEphemeral table will be
 104924         ** cancelled later because we still need to use the pKeyInfo
 104926         groupBySort = 0;
 104927       }else{
 104928         /* Rows are coming out in undetermined order.  We have to push
 104929         ** each row into a sorting index, terminate the first loop,
 104930         ** then loop over the sorting index in order to get the output
 104931         ** in sorted order
 104933         int regBase;
 104934         int regRecord;
 104935         int nCol;
 104936         int nGroupBy;
 104938         explainTempTable(pParse, 
 104939             (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
 104940                     "DISTINCT" : "GROUP BY");
 104942         groupBySort = 1;
 104943         nGroupBy = pGroupBy->nExpr;
 104944         nCol = nGroupBy + 1;
 104945         j = nGroupBy+1;
 104946         for(i=0; i<sAggInfo.nColumn; i++){
 104947           if( sAggInfo.aCol[i].iSorterColumn>=j ){
 104948             nCol++;
 104949             j++;
 104952         regBase = sqlite3GetTempRange(pParse, nCol);
 104953         sqlite3ExprCacheClear(pParse);
 104954         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
 104955         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
 104956         j = nGroupBy+1;
 104957         for(i=0; i<sAggInfo.nColumn; i++){
 104958           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
 104959           if( pCol->iSorterColumn>=j ){
 104960             int r1 = j + regBase;
 104961             int r2;
 104963             r2 = sqlite3ExprCodeGetColumn(pParse, 
 104964                                pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
 104965             if( r1!=r2 ){
 104966               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
 104968             j++;
 104971         regRecord = sqlite3GetTempReg(pParse);
 104972         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
 104973         sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
 104974         sqlite3ReleaseTempReg(pParse, regRecord);
 104975         sqlite3ReleaseTempRange(pParse, regBase, nCol);
 104976         sqlite3WhereEnd(pWInfo);
 104977         sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
 104978         sortOut = sqlite3GetTempReg(pParse);
 104979         sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
 104980         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
 104981         VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
 104982         sAggInfo.useSortingIdx = 1;
 104983         sqlite3ExprCacheClear(pParse);
 104986       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
 104987       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
 104988       ** Then compare the current GROUP BY terms against the GROUP BY terms
 104989       ** from the previous row currently stored in a0, a1, a2...
 104991       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
 104992       sqlite3ExprCacheClear(pParse);
 104993       if( groupBySort ){
 104994         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
 104996       for(j=0; j<pGroupBy->nExpr; j++){
 104997         if( groupBySort ){
 104998           sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
 104999           if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
 105000         }else{
 105001           sAggInfo.directMode = 1;
 105002           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
 105005       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
 105006                           (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
 105007       j1 = sqlite3VdbeCurrentAddr(v);
 105008       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
 105010       /* Generate code that runs whenever the GROUP BY changes.
 105011       ** Changes in the GROUP BY are detected by the previous code
 105012       ** block.  If there were no changes, this block is skipped.
 105014       ** This code copies current group by terms in b0,b1,b2,...
 105015       ** over to a0,a1,a2.  It then calls the output subroutine
 105016       ** and resets the aggregate accumulator registers in preparation
 105017       ** for the next GROUP BY batch.
 105019       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
 105020       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 105021       VdbeComment((v, "output one row"));
 105022       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
 105023       VdbeComment((v, "check abort flag"));
 105024       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
 105025       VdbeComment((v, "reset accumulator"));
 105027       /* Update the aggregate accumulators based on the content of
 105028       ** the current row
 105030       sqlite3VdbeJumpHere(v, j1);
 105031       updateAccumulator(pParse, &sAggInfo);
 105032       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
 105033       VdbeComment((v, "indicate data in accumulator"));
 105035       /* End of the loop
 105037       if( groupBySort ){
 105038         sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
 105039         VdbeCoverage(v);
 105040       }else{
 105041         sqlite3WhereEnd(pWInfo);
 105042         sqlite3VdbeChangeToNoop(v, addrSortingIdx);
 105045       /* Output the final row of result
 105047       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
 105048       VdbeComment((v, "output final row"));
 105050       /* Jump over the subroutines
 105052       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
 105054       /* Generate a subroutine that outputs a single row of the result
 105055       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
 105056       ** is less than or equal to zero, the subroutine is a no-op.  If
 105057       ** the processing calls for the query to abort, this subroutine
 105058       ** increments the iAbortFlag memory location before returning in
 105059       ** order to signal the caller to abort.
 105061       addrSetAbort = sqlite3VdbeCurrentAddr(v);
 105062       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
 105063       VdbeComment((v, "set abort flag"));
 105064       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 105065       sqlite3VdbeResolveLabel(v, addrOutputRow);
 105066       addrOutputRow = sqlite3VdbeCurrentAddr(v);
 105067       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
 105068       VdbeComment((v, "Groupby result generator entry point"));
 105069       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 105070       finalizeAggFunctions(pParse, &sAggInfo);
 105071       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
 105072       selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
 105073                       &sDistinct, pDest,
 105074                       addrOutputRow+1, addrSetAbort);
 105075       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
 105076       VdbeComment((v, "end groupby result generator"));
 105078       /* Generate a subroutine that will reset the group-by accumulator
 105080       sqlite3VdbeResolveLabel(v, addrReset);
 105081       resetAccumulator(pParse, &sAggInfo);
 105082       sqlite3VdbeAddOp1(v, OP_Return, regReset);
 105084     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
 105085     else {
 105086       ExprList *pDel = 0;
 105087 #ifndef SQLITE_OMIT_BTREECOUNT
 105088       Table *pTab;
 105089       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
 105090         /* If isSimpleCount() returns a pointer to a Table structure, then
 105091         ** the SQL statement is of the form:
 105093         **   SELECT count(*) FROM <tbl>
 105095         ** where the Table structure returned represents table <tbl>.
 105097         ** This statement is so common that it is optimized specially. The
 105098         ** OP_Count instruction is executed either on the intkey table that
 105099         ** contains the data for table <tbl> or on one of its indexes. It
 105100         ** is better to execute the op on an index, as indexes are almost
 105101         ** always spread across less pages than their corresponding tables.
 105103         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 105104         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
 105105         Index *pIdx;                         /* Iterator variable */
 105106         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
 105107         Index *pBest = 0;                    /* Best index found so far */
 105108         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
 105110         sqlite3CodeVerifySchema(pParse, iDb);
 105111         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 105113         /* Search for the index that has the lowest scan cost.
 105115         ** (2011-04-15) Do not do a full scan of an unordered index.
 105117         ** (2013-10-03) Do not count the entries in a partial index.
 105119         ** In practice the KeyInfo structure will not be used. It is only 
 105120         ** passed to keep OP_OpenRead happy.
 105122         if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
 105123         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 105124           if( pIdx->bUnordered==0
 105125            && pIdx->szIdxRow<pTab->szTabRow
 105126            && pIdx->pPartIdxWhere==0
 105127            && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
 105129             pBest = pIdx;
 105132         if( pBest ){
 105133           iRoot = pBest->tnum;
 105134           pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
 105137         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
 105138         sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
 105139         if( pKeyInfo ){
 105140           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
 105142         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
 105143         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
 105144         explainSimpleCount(pParse, pTab, pBest);
 105145       }else
 105146 #endif /* SQLITE_OMIT_BTREECOUNT */
 105148         /* Check if the query is of one of the following forms:
 105150         **   SELECT min(x) FROM ...
 105151         **   SELECT max(x) FROM ...
 105153         ** If it is, then ask the code in where.c to attempt to sort results
 105154         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
 105155         ** If where.c is able to produce results sorted in this order, then
 105156         ** add vdbe code to break out of the processing loop after the 
 105157         ** first iteration (since the first iteration of the loop is 
 105158         ** guaranteed to operate on the row with the minimum or maximum 
 105159         ** value of x, the only row required).
 105161         ** A special flag must be passed to sqlite3WhereBegin() to slightly
 105162         ** modify behavior as follows:
 105164         **   + If the query is a "SELECT min(x)", then the loop coded by
 105165         **     where.c should not iterate over any values with a NULL value
 105166         **     for x.
 105168         **   + The optimizer code in where.c (the thing that decides which
 105169         **     index or indices to use) should place a different priority on 
 105170         **     satisfying the 'ORDER BY' clause than it does in other cases.
 105171         **     Refer to code and comments in where.c for details.
 105173         ExprList *pMinMax = 0;
 105174         u8 flag = WHERE_ORDERBY_NORMAL;
 105176         assert( p->pGroupBy==0 );
 105177         assert( flag==0 );
 105178         if( p->pHaving==0 ){
 105179           flag = minMaxQuery(&sAggInfo, &pMinMax);
 105181         assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
 105183         if( flag ){
 105184           pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
 105185           pDel = pMinMax;
 105186           if( pMinMax && !db->mallocFailed ){
 105187             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
 105188             pMinMax->a[0].pExpr->op = TK_COLUMN;
 105192         /* This case runs if the aggregate has no GROUP BY clause.  The
 105193         ** processing is much simpler since there is only a single row
 105194         ** of output.
 105196         resetAccumulator(pParse, &sAggInfo);
 105197         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
 105198         if( pWInfo==0 ){
 105199           sqlite3ExprListDelete(db, pDel);
 105200           goto select_end;
 105202         updateAccumulator(pParse, &sAggInfo);
 105203         assert( pMinMax==0 || pMinMax->nExpr==1 );
 105204         if( sqlite3WhereIsOrdered(pWInfo) ){
 105205           sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
 105206           VdbeComment((v, "%s() by index",
 105207                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
 105209         sqlite3WhereEnd(pWInfo);
 105210         finalizeAggFunctions(pParse, &sAggInfo);
 105213       pOrderBy = 0;
 105214       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
 105215       selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, 
 105216                       pDest, addrEnd, addrEnd);
 105217       sqlite3ExprListDelete(db, pDel);
 105219     sqlite3VdbeResolveLabel(v, addrEnd);
 105221   } /* endif aggregate query */
 105223   if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
 105224     explainTempTable(pParse, "DISTINCT");
 105227   /* If there is an ORDER BY clause, then we need to sort the results
 105228   ** and send them to the callback one by one.
 105230   if( pOrderBy ){
 105231     explainTempTable(pParse, "ORDER BY");
 105232     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
 105235   /* Jump here to skip this query
 105237   sqlite3VdbeResolveLabel(v, iEnd);
 105239   /* The SELECT was successfully coded.   Set the return code to 0
 105240   ** to indicate no errors.
 105242   rc = 0;
 105244   /* Control jumps to here if an error is encountered above, or upon
 105245   ** successful coding of the SELECT.
 105247 select_end:
 105248   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
 105250   /* Identify column names if results of the SELECT are to be output.
 105252   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
 105253     generateColumnNames(pParse, pTabList, pEList);
 105256   sqlite3DbFree(db, sAggInfo.aCol);
 105257   sqlite3DbFree(db, sAggInfo.aFunc);
 105258   return rc;
 105261 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 105263 ** Generate a human-readable description of a the Select object.
 105265 static void explainOneSelect(Vdbe *pVdbe, Select *p){
 105266   sqlite3ExplainPrintf(pVdbe, "SELECT ");
 105267   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 105268     if( p->selFlags & SF_Distinct ){
 105269       sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
 105271     if( p->selFlags & SF_Aggregate ){
 105272       sqlite3ExplainPrintf(pVdbe, "agg_flag ");
 105274     sqlite3ExplainNL(pVdbe);
 105275     sqlite3ExplainPrintf(pVdbe, "   ");
 105277   sqlite3ExplainExprList(pVdbe, p->pEList);
 105278   sqlite3ExplainNL(pVdbe);
 105279   if( p->pSrc && p->pSrc->nSrc ){
 105280     int i;
 105281     sqlite3ExplainPrintf(pVdbe, "FROM ");
 105282     sqlite3ExplainPush(pVdbe);
 105283     for(i=0; i<p->pSrc->nSrc; i++){
 105284       struct SrcList_item *pItem = &p->pSrc->a[i];
 105285       sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
 105286       if( pItem->pSelect ){
 105287         sqlite3ExplainSelect(pVdbe, pItem->pSelect);
 105288         if( pItem->pTab ){
 105289           sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
 105291       }else if( pItem->zName ){
 105292         sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
 105294       if( pItem->zAlias ){
 105295         sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
 105297       if( pItem->jointype & JT_LEFT ){
 105298         sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
 105300       sqlite3ExplainNL(pVdbe);
 105302     sqlite3ExplainPop(pVdbe);
 105304   if( p->pWhere ){
 105305     sqlite3ExplainPrintf(pVdbe, "WHERE ");
 105306     sqlite3ExplainExpr(pVdbe, p->pWhere);
 105307     sqlite3ExplainNL(pVdbe);
 105309   if( p->pGroupBy ){
 105310     sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
 105311     sqlite3ExplainExprList(pVdbe, p->pGroupBy);
 105312     sqlite3ExplainNL(pVdbe);
 105314   if( p->pHaving ){
 105315     sqlite3ExplainPrintf(pVdbe, "HAVING ");
 105316     sqlite3ExplainExpr(pVdbe, p->pHaving);
 105317     sqlite3ExplainNL(pVdbe);
 105319   if( p->pOrderBy ){
 105320     sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
 105321     sqlite3ExplainExprList(pVdbe, p->pOrderBy);
 105322     sqlite3ExplainNL(pVdbe);
 105324   if( p->pLimit ){
 105325     sqlite3ExplainPrintf(pVdbe, "LIMIT ");
 105326     sqlite3ExplainExpr(pVdbe, p->pLimit);
 105327     sqlite3ExplainNL(pVdbe);
 105329   if( p->pOffset ){
 105330     sqlite3ExplainPrintf(pVdbe, "OFFSET ");
 105331     sqlite3ExplainExpr(pVdbe, p->pOffset);
 105332     sqlite3ExplainNL(pVdbe);
 105335 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
 105336   if( p==0 ){
 105337     sqlite3ExplainPrintf(pVdbe, "(null-select)");
 105338     return;
 105340   sqlite3ExplainPush(pVdbe);
 105341   while( p ){
 105342     explainOneSelect(pVdbe, p);
 105343     p = p->pNext;
 105344     if( p==0 ) break;
 105345     sqlite3ExplainNL(pVdbe);
 105346     sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
 105348   sqlite3ExplainPrintf(pVdbe, "END");
 105349   sqlite3ExplainPop(pVdbe);
 105352 /* End of the structure debug printing code
 105353 *****************************************************************************/
 105354 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
 105356 /************** End of select.c **********************************************/
 105357 /************** Begin file table.c *******************************************/
 105359 ** 2001 September 15
 105361 ** The author disclaims copyright to this source code.  In place of
 105362 ** a legal notice, here is a blessing:
 105364 **    May you do good and not evil.
 105365 **    May you find forgiveness for yourself and forgive others.
 105366 **    May you share freely, never taking more than you give.
 105368 *************************************************************************
 105369 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
 105370 ** interface routines.  These are just wrappers around the main
 105371 ** interface routine of sqlite3_exec().
 105373 ** These routines are in a separate files so that they will not be linked
 105374 ** if they are not used.
 105376 /* #include <stdlib.h> */
 105377 /* #include <string.h> */
 105379 #ifndef SQLITE_OMIT_GET_TABLE
 105382 ** This structure is used to pass data from sqlite3_get_table() through
 105383 ** to the callback function is uses to build the result.
 105385 typedef struct TabResult {
 105386   char **azResult;   /* Accumulated output */
 105387   char *zErrMsg;     /* Error message text, if an error occurs */
 105388   int nAlloc;        /* Slots allocated for azResult[] */
 105389   int nRow;          /* Number of rows in the result */
 105390   int nColumn;       /* Number of columns in the result */
 105391   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
 105392   int rc;            /* Return code from sqlite3_exec() */
 105393 } TabResult;
 105396 ** This routine is called once for each row in the result table.  Its job
 105397 ** is to fill in the TabResult structure appropriately, allocating new
 105398 ** memory as necessary.
 105400 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
 105401   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
 105402   int need;                         /* Slots needed in p->azResult[] */
 105403   int i;                            /* Loop counter */
 105404   char *z;                          /* A single column of result */
 105406   /* Make sure there is enough space in p->azResult to hold everything
 105407   ** we need to remember from this invocation of the callback.
 105409   if( p->nRow==0 && argv!=0 ){
 105410     need = nCol*2;
 105411   }else{
 105412     need = nCol;
 105414   if( p->nData + need > p->nAlloc ){
 105415     char **azNew;
 105416     p->nAlloc = p->nAlloc*2 + need;
 105417     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
 105418     if( azNew==0 ) goto malloc_failed;
 105419     p->azResult = azNew;
 105422   /* If this is the first row, then generate an extra row containing
 105423   ** the names of all columns.
 105425   if( p->nRow==0 ){
 105426     p->nColumn = nCol;
 105427     for(i=0; i<nCol; i++){
 105428       z = sqlite3_mprintf("%s", colv[i]);
 105429       if( z==0 ) goto malloc_failed;
 105430       p->azResult[p->nData++] = z;
 105432   }else if( p->nColumn!=nCol ){
 105433     sqlite3_free(p->zErrMsg);
 105434     p->zErrMsg = sqlite3_mprintf(
 105435        "sqlite3_get_table() called with two or more incompatible queries"
 105437     p->rc = SQLITE_ERROR;
 105438     return 1;
 105441   /* Copy over the row data
 105443   if( argv!=0 ){
 105444     for(i=0; i<nCol; i++){
 105445       if( argv[i]==0 ){
 105446         z = 0;
 105447       }else{
 105448         int n = sqlite3Strlen30(argv[i])+1;
 105449         z = sqlite3_malloc( n );
 105450         if( z==0 ) goto malloc_failed;
 105451         memcpy(z, argv[i], n);
 105453       p->azResult[p->nData++] = z;
 105455     p->nRow++;
 105457   return 0;
 105459 malloc_failed:
 105460   p->rc = SQLITE_NOMEM;
 105461   return 1;
 105465 ** Query the database.  But instead of invoking a callback for each row,
 105466 ** malloc() for space to hold the result and return the entire results
 105467 ** at the conclusion of the call.
 105469 ** The result that is written to ***pazResult is held in memory obtained
 105470 ** from malloc().  But the caller cannot free this memory directly.  
 105471 ** Instead, the entire table should be passed to sqlite3_free_table() when
 105472 ** the calling procedure is finished using it.
 105474 SQLITE_API int sqlite3_get_table(
 105475   sqlite3 *db,                /* The database on which the SQL executes */
 105476   const char *zSql,           /* The SQL to be executed */
 105477   char ***pazResult,          /* Write the result table here */
 105478   int *pnRow,                 /* Write the number of rows in the result here */
 105479   int *pnColumn,              /* Write the number of columns of result here */
 105480   char **pzErrMsg             /* Write error messages here */
 105482   int rc;
 105483   TabResult res;
 105485   *pazResult = 0;
 105486   if( pnColumn ) *pnColumn = 0;
 105487   if( pnRow ) *pnRow = 0;
 105488   if( pzErrMsg ) *pzErrMsg = 0;
 105489   res.zErrMsg = 0;
 105490   res.nRow = 0;
 105491   res.nColumn = 0;
 105492   res.nData = 1;
 105493   res.nAlloc = 20;
 105494   res.rc = SQLITE_OK;
 105495   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
 105496   if( res.azResult==0 ){
 105497      db->errCode = SQLITE_NOMEM;
 105498      return SQLITE_NOMEM;
 105500   res.azResult[0] = 0;
 105501   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
 105502   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
 105503   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
 105504   if( (rc&0xff)==SQLITE_ABORT ){
 105505     sqlite3_free_table(&res.azResult[1]);
 105506     if( res.zErrMsg ){
 105507       if( pzErrMsg ){
 105508         sqlite3_free(*pzErrMsg);
 105509         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
 105511       sqlite3_free(res.zErrMsg);
 105513     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
 105514     return res.rc;
 105516   sqlite3_free(res.zErrMsg);
 105517   if( rc!=SQLITE_OK ){
 105518     sqlite3_free_table(&res.azResult[1]);
 105519     return rc;
 105521   if( res.nAlloc>res.nData ){
 105522     char **azNew;
 105523     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
 105524     if( azNew==0 ){
 105525       sqlite3_free_table(&res.azResult[1]);
 105526       db->errCode = SQLITE_NOMEM;
 105527       return SQLITE_NOMEM;
 105529     res.azResult = azNew;
 105531   *pazResult = &res.azResult[1];
 105532   if( pnColumn ) *pnColumn = res.nColumn;
 105533   if( pnRow ) *pnRow = res.nRow;
 105534   return rc;
 105538 ** This routine frees the space the sqlite3_get_table() malloced.
 105540 SQLITE_API void sqlite3_free_table(
 105541   char **azResult            /* Result returned from from sqlite3_get_table() */
 105543   if( azResult ){
 105544     int i, n;
 105545     azResult--;
 105546     assert( azResult!=0 );
 105547     n = SQLITE_PTR_TO_INT(azResult[0]);
 105548     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
 105549     sqlite3_free(azResult);
 105553 #endif /* SQLITE_OMIT_GET_TABLE */
 105555 /************** End of table.c ***********************************************/
 105556 /************** Begin file trigger.c *****************************************/
 105559 ** The author disclaims copyright to this source code.  In place of
 105560 ** a legal notice, here is a blessing:
 105562 **    May you do good and not evil.
 105563 **    May you find forgiveness for yourself and forgive others.
 105564 **    May you share freely, never taking more than you give.
 105566 *************************************************************************
 105567 ** This file contains the implementation for TRIGGERs
 105570 #ifndef SQLITE_OMIT_TRIGGER
 105572 ** Delete a linked list of TriggerStep structures.
 105574 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
 105575   while( pTriggerStep ){
 105576     TriggerStep * pTmp = pTriggerStep;
 105577     pTriggerStep = pTriggerStep->pNext;
 105579     sqlite3ExprDelete(db, pTmp->pWhere);
 105580     sqlite3ExprListDelete(db, pTmp->pExprList);
 105581     sqlite3SelectDelete(db, pTmp->pSelect);
 105582     sqlite3IdListDelete(db, pTmp->pIdList);
 105584     sqlite3DbFree(db, pTmp);
 105589 ** Given table pTab, return a list of all the triggers attached to 
 105590 ** the table. The list is connected by Trigger.pNext pointers.
 105592 ** All of the triggers on pTab that are in the same database as pTab
 105593 ** are already attached to pTab->pTrigger.  But there might be additional
 105594 ** triggers on pTab in the TEMP schema.  This routine prepends all
 105595 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
 105596 ** and returns the combined list.
 105598 ** To state it another way:  This routine returns a list of all triggers
 105599 ** that fire off of pTab.  The list will include any TEMP triggers on
 105600 ** pTab as well as the triggers lised in pTab->pTrigger.
 105602 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
 105603   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
 105604   Trigger *pList = 0;                  /* List of triggers to return */
 105606   if( pParse->disableTriggers ){
 105607     return 0;
 105610   if( pTmpSchema!=pTab->pSchema ){
 105611     HashElem *p;
 105612     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
 105613     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
 105614       Trigger *pTrig = (Trigger *)sqliteHashData(p);
 105615       if( pTrig->pTabSchema==pTab->pSchema
 105616        && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
 105618         pTrig->pNext = (pList ? pList : pTab->pTrigger);
 105619         pList = pTrig;
 105624   return (pList ? pList : pTab->pTrigger);
 105628 ** This is called by the parser when it sees a CREATE TRIGGER statement
 105629 ** up to the point of the BEGIN before the trigger actions.  A Trigger
 105630 ** structure is generated based on the information available and stored
 105631 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
 105632 ** sqlite3FinishTrigger() function is called to complete the trigger
 105633 ** construction process.
 105635 SQLITE_PRIVATE void sqlite3BeginTrigger(
 105636   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
 105637   Token *pName1,      /* The name of the trigger */
 105638   Token *pName2,      /* The name of the trigger */
 105639   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
 105640   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
 105641   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
 105642   SrcList *pTableName,/* The name of the table/view the trigger applies to */
 105643   Expr *pWhen,        /* WHEN clause */
 105644   int isTemp,         /* True if the TEMPORARY keyword is present */
 105645   int noErr           /* Suppress errors if the trigger already exists */
 105647   Trigger *pTrigger = 0;  /* The new trigger */
 105648   Table *pTab;            /* Table that the trigger fires off of */
 105649   char *zName = 0;        /* Name of the trigger */
 105650   sqlite3 *db = pParse->db;  /* The database connection */
 105651   int iDb;                /* The database to store the trigger in */
 105652   Token *pName;           /* The unqualified db name */
 105653   DbFixer sFix;           /* State vector for the DB fixer */
 105654   int iTabDb;             /* Index of the database holding pTab */
 105656   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
 105657   assert( pName2!=0 );
 105658   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
 105659   assert( op>0 && op<0xff );
 105660   if( isTemp ){
 105661     /* If TEMP was specified, then the trigger name may not be qualified. */
 105662     if( pName2->n>0 ){
 105663       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
 105664       goto trigger_cleanup;
 105666     iDb = 1;
 105667     pName = pName1;
 105668   }else{
 105669     /* Figure out the db that the trigger will be created in */
 105670     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
 105671     if( iDb<0 ){
 105672       goto trigger_cleanup;
 105675   if( !pTableName || db->mallocFailed ){
 105676     goto trigger_cleanup;
 105679   /* A long-standing parser bug is that this syntax was allowed:
 105681   **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
 105682   **                                                 ^^^^^^^^
 105684   ** To maintain backwards compatibility, ignore the database
 105685   ** name on pTableName if we are reparsing our of SQLITE_MASTER.
 105687   if( db->init.busy && iDb!=1 ){
 105688     sqlite3DbFree(db, pTableName->a[0].zDatabase);
 105689     pTableName->a[0].zDatabase = 0;
 105692   /* If the trigger name was unqualified, and the table is a temp table,
 105693   ** then set iDb to 1 to create the trigger in the temporary database.
 105694   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
 105695   ** exist, the error is caught by the block below.
 105697   pTab = sqlite3SrcListLookup(pParse, pTableName);
 105698   if( db->init.busy==0 && pName2->n==0 && pTab
 105699         && pTab->pSchema==db->aDb[1].pSchema ){
 105700     iDb = 1;
 105703   /* Ensure the table name matches database name and that the table exists */
 105704   if( db->mallocFailed ) goto trigger_cleanup;
 105705   assert( pTableName->nSrc==1 );
 105706   sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
 105707   if( sqlite3FixSrcList(&sFix, pTableName) ){
 105708     goto trigger_cleanup;
 105710   pTab = sqlite3SrcListLookup(pParse, pTableName);
 105711   if( !pTab ){
 105712     /* The table does not exist. */
 105713     if( db->init.iDb==1 ){
 105714       /* Ticket #3810.
 105715       ** Normally, whenever a table is dropped, all associated triggers are
 105716       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
 105717       ** and the table is dropped by a different database connection, the
 105718       ** trigger is not visible to the database connection that does the
 105719       ** drop so the trigger cannot be dropped.  This results in an
 105720       ** "orphaned trigger" - a trigger whose associated table is missing.
 105722       db->init.orphanTrigger = 1;
 105724     goto trigger_cleanup;
 105726   if( IsVirtual(pTab) ){
 105727     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
 105728     goto trigger_cleanup;
 105731   /* Check that the trigger name is not reserved and that no trigger of the
 105732   ** specified name exists */
 105733   zName = sqlite3NameFromToken(db, pName);
 105734   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 105735     goto trigger_cleanup;
 105737   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 105738   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
 105739                       zName, sqlite3Strlen30(zName)) ){
 105740     if( !noErr ){
 105741       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
 105742     }else{
 105743       assert( !db->init.busy );
 105744       sqlite3CodeVerifySchema(pParse, iDb);
 105746     goto trigger_cleanup;
 105749   /* Do not create a trigger on a system table */
 105750   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
 105751     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
 105752     pParse->nErr++;
 105753     goto trigger_cleanup;
 105756   /* INSTEAD of triggers are only for views and views only support INSTEAD
 105757   ** of triggers.
 105759   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
 105760     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
 105761         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
 105762     goto trigger_cleanup;
 105764   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
 105765     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
 105766         " trigger on table: %S", pTableName, 0);
 105767     goto trigger_cleanup;
 105769   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 105771 #ifndef SQLITE_OMIT_AUTHORIZATION
 105773     int code = SQLITE_CREATE_TRIGGER;
 105774     const char *zDb = db->aDb[iTabDb].zName;
 105775     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
 105776     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
 105777     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
 105778       goto trigger_cleanup;
 105780     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
 105781       goto trigger_cleanup;
 105784 #endif
 105786   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
 105787   ** cannot appear on views.  So we might as well translate every
 105788   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
 105789   ** elsewhere.
 105791   if (tr_tm == TK_INSTEAD){
 105792     tr_tm = TK_BEFORE;
 105795   /* Build the Trigger object */
 105796   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
 105797   if( pTrigger==0 ) goto trigger_cleanup;
 105798   pTrigger->zName = zName;
 105799   zName = 0;
 105800   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
 105801   pTrigger->pSchema = db->aDb[iDb].pSchema;
 105802   pTrigger->pTabSchema = pTab->pSchema;
 105803   pTrigger->op = (u8)op;
 105804   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
 105805   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
 105806   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
 105807   assert( pParse->pNewTrigger==0 );
 105808   pParse->pNewTrigger = pTrigger;
 105810 trigger_cleanup:
 105811   sqlite3DbFree(db, zName);
 105812   sqlite3SrcListDelete(db, pTableName);
 105813   sqlite3IdListDelete(db, pColumns);
 105814   sqlite3ExprDelete(db, pWhen);
 105815   if( !pParse->pNewTrigger ){
 105816     sqlite3DeleteTrigger(db, pTrigger);
 105817   }else{
 105818     assert( pParse->pNewTrigger==pTrigger );
 105823 ** This routine is called after all of the trigger actions have been parsed
 105824 ** in order to complete the process of building the trigger.
 105826 SQLITE_PRIVATE void sqlite3FinishTrigger(
 105827   Parse *pParse,          /* Parser context */
 105828   TriggerStep *pStepList, /* The triggered program */
 105829   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
 105831   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
 105832   char *zName;                            /* Name of trigger */
 105833   sqlite3 *db = pParse->db;               /* The database */
 105834   DbFixer sFix;                           /* Fixer object */
 105835   int iDb;                                /* Database containing the trigger */
 105836   Token nameToken;                        /* Trigger name for error reporting */
 105838   pParse->pNewTrigger = 0;
 105839   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
 105840   zName = pTrig->zName;
 105841   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
 105842   pTrig->step_list = pStepList;
 105843   while( pStepList ){
 105844     pStepList->pTrig = pTrig;
 105845     pStepList = pStepList->pNext;
 105847   nameToken.z = pTrig->zName;
 105848   nameToken.n = sqlite3Strlen30(nameToken.z);
 105849   sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
 105850   if( sqlite3FixTriggerStep(&sFix, pTrig->step_list) 
 105851    || sqlite3FixExpr(&sFix, pTrig->pWhen) 
 105853     goto triggerfinish_cleanup;
 105856   /* if we are not initializing,
 105857   ** build the sqlite_master entry
 105859   if( !db->init.busy ){
 105860     Vdbe *v;
 105861     char *z;
 105863     /* Make an entry in the sqlite_master table */
 105864     v = sqlite3GetVdbe(pParse);
 105865     if( v==0 ) goto triggerfinish_cleanup;
 105866     sqlite3BeginWriteOperation(pParse, 0, iDb);
 105867     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
 105868     sqlite3NestedParse(pParse,
 105869        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
 105870        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
 105871        pTrig->table, z);
 105872     sqlite3DbFree(db, z);
 105873     sqlite3ChangeCookie(pParse, iDb);
 105874     sqlite3VdbeAddParseSchemaOp(v, iDb,
 105875         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
 105878   if( db->init.busy ){
 105879     Trigger *pLink = pTrig;
 105880     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
 105881     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 105882     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
 105883     if( pTrig ){
 105884       db->mallocFailed = 1;
 105885     }else if( pLink->pSchema==pLink->pTabSchema ){
 105886       Table *pTab;
 105887       int n = sqlite3Strlen30(pLink->table);
 105888       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
 105889       assert( pTab!=0 );
 105890       pLink->pNext = pTab->pTrigger;
 105891       pTab->pTrigger = pLink;
 105895 triggerfinish_cleanup:
 105896   sqlite3DeleteTrigger(db, pTrig);
 105897   assert( !pParse->pNewTrigger );
 105898   sqlite3DeleteTriggerStep(db, pStepList);
 105902 ** Turn a SELECT statement (that the pSelect parameter points to) into
 105903 ** a trigger step.  Return a pointer to a TriggerStep structure.
 105905 ** The parser calls this routine when it finds a SELECT statement in
 105906 ** body of a TRIGGER.  
 105908 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
 105909   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
 105910   if( pTriggerStep==0 ) {
 105911     sqlite3SelectDelete(db, pSelect);
 105912     return 0;
 105914   pTriggerStep->op = TK_SELECT;
 105915   pTriggerStep->pSelect = pSelect;
 105916   pTriggerStep->orconf = OE_Default;
 105917   return pTriggerStep;
 105921 ** Allocate space to hold a new trigger step.  The allocated space
 105922 ** holds both the TriggerStep object and the TriggerStep.target.z string.
 105924 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
 105926 static TriggerStep *triggerStepAllocate(
 105927   sqlite3 *db,                /* Database connection */
 105928   u8 op,                      /* Trigger opcode */
 105929   Token *pName                /* The target name */
 105931   TriggerStep *pTriggerStep;
 105933   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
 105934   if( pTriggerStep ){
 105935     char *z = (char*)&pTriggerStep[1];
 105936     memcpy(z, pName->z, pName->n);
 105937     pTriggerStep->target.z = z;
 105938     pTriggerStep->target.n = pName->n;
 105939     pTriggerStep->op = op;
 105941   return pTriggerStep;
 105945 ** Build a trigger step out of an INSERT statement.  Return a pointer
 105946 ** to the new trigger step.
 105948 ** The parser calls this routine when it sees an INSERT inside the
 105949 ** body of a trigger.
 105951 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
 105952   sqlite3 *db,        /* The database connection */
 105953   Token *pTableName,  /* Name of the table into which we insert */
 105954   IdList *pColumn,    /* List of columns in pTableName to insert into */
 105955   Select *pSelect,    /* A SELECT statement that supplies values */
 105956   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
 105958   TriggerStep *pTriggerStep;
 105960   assert(pSelect != 0 || db->mallocFailed);
 105962   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
 105963   if( pTriggerStep ){
 105964     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
 105965     pTriggerStep->pIdList = pColumn;
 105966     pTriggerStep->orconf = orconf;
 105967   }else{
 105968     sqlite3IdListDelete(db, pColumn);
 105970   sqlite3SelectDelete(db, pSelect);
 105972   return pTriggerStep;
 105976 ** Construct a trigger step that implements an UPDATE statement and return
 105977 ** a pointer to that trigger step.  The parser calls this routine when it
 105978 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
 105980 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
 105981   sqlite3 *db,         /* The database connection */
 105982   Token *pTableName,   /* Name of the table to be updated */
 105983   ExprList *pEList,    /* The SET clause: list of column and new values */
 105984   Expr *pWhere,        /* The WHERE clause */
 105985   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
 105987   TriggerStep *pTriggerStep;
 105989   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
 105990   if( pTriggerStep ){
 105991     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
 105992     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 105993     pTriggerStep->orconf = orconf;
 105995   sqlite3ExprListDelete(db, pEList);
 105996   sqlite3ExprDelete(db, pWhere);
 105997   return pTriggerStep;
 106001 ** Construct a trigger step that implements a DELETE statement and return
 106002 ** a pointer to that trigger step.  The parser calls this routine when it
 106003 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
 106005 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
 106006   sqlite3 *db,            /* Database connection */
 106007   Token *pTableName,      /* The table from which rows are deleted */
 106008   Expr *pWhere            /* The WHERE clause */
 106010   TriggerStep *pTriggerStep;
 106012   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
 106013   if( pTriggerStep ){
 106014     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
 106015     pTriggerStep->orconf = OE_Default;
 106017   sqlite3ExprDelete(db, pWhere);
 106018   return pTriggerStep;
 106022 ** Recursively delete a Trigger structure
 106024 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
 106025   if( pTrigger==0 ) return;
 106026   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
 106027   sqlite3DbFree(db, pTrigger->zName);
 106028   sqlite3DbFree(db, pTrigger->table);
 106029   sqlite3ExprDelete(db, pTrigger->pWhen);
 106030   sqlite3IdListDelete(db, pTrigger->pColumns);
 106031   sqlite3DbFree(db, pTrigger);
 106035 ** This function is called to drop a trigger from the database schema. 
 106037 ** This may be called directly from the parser and therefore identifies
 106038 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
 106039 ** same job as this routine except it takes a pointer to the trigger
 106040 ** instead of the trigger name.
 106041 **/
 106042 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
 106043   Trigger *pTrigger = 0;
 106044   int i;
 106045   const char *zDb;
 106046   const char *zName;
 106047   int nName;
 106048   sqlite3 *db = pParse->db;
 106050   if( db->mallocFailed ) goto drop_trigger_cleanup;
 106051   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 106052     goto drop_trigger_cleanup;
 106055   assert( pName->nSrc==1 );
 106056   zDb = pName->a[0].zDatabase;
 106057   zName = pName->a[0].zName;
 106058   nName = sqlite3Strlen30(zName);
 106059   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 106060   for(i=OMIT_TEMPDB; i<db->nDb; i++){
 106061     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 106062     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
 106063     assert( sqlite3SchemaMutexHeld(db, j, 0) );
 106064     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
 106065     if( pTrigger ) break;
 106067   if( !pTrigger ){
 106068     if( !noErr ){
 106069       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
 106070     }else{
 106071       sqlite3CodeVerifyNamedSchema(pParse, zDb);
 106073     pParse->checkSchema = 1;
 106074     goto drop_trigger_cleanup;
 106076   sqlite3DropTriggerPtr(pParse, pTrigger);
 106078 drop_trigger_cleanup:
 106079   sqlite3SrcListDelete(db, pName);
 106083 ** Return a pointer to the Table structure for the table that a trigger
 106084 ** is set on.
 106086 static Table *tableOfTrigger(Trigger *pTrigger){
 106087   int n = sqlite3Strlen30(pTrigger->table);
 106088   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
 106093 ** Drop a trigger given a pointer to that trigger. 
 106095 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
 106096   Table   *pTable;
 106097   Vdbe *v;
 106098   sqlite3 *db = pParse->db;
 106099   int iDb;
 106101   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
 106102   assert( iDb>=0 && iDb<db->nDb );
 106103   pTable = tableOfTrigger(pTrigger);
 106104   assert( pTable );
 106105   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
 106106 #ifndef SQLITE_OMIT_AUTHORIZATION
 106108     int code = SQLITE_DROP_TRIGGER;
 106109     const char *zDb = db->aDb[iDb].zName;
 106110     const char *zTab = SCHEMA_TABLE(iDb);
 106111     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
 106112     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
 106113       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
 106114       return;
 106117 #endif
 106119   /* Generate code to destroy the database record of the trigger.
 106121   assert( pTable!=0 );
 106122   if( (v = sqlite3GetVdbe(pParse))!=0 ){
 106123     int base;
 106124     static const int iLn = VDBE_OFFSET_LINENO(2);
 106125     static const VdbeOpList dropTrigger[] = {
 106126       { OP_Rewind,     0, ADDR(9),  0},
 106127       { OP_String8,    0, 1,        0}, /* 1 */
 106128       { OP_Column,     0, 1,        2},
 106129       { OP_Ne,         2, ADDR(8),  1},
 106130       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
 106131       { OP_Column,     0, 0,        2},
 106132       { OP_Ne,         2, ADDR(8),  1},
 106133       { OP_Delete,     0, 0,        0},
 106134       { OP_Next,       0, ADDR(1),  0}, /* 8 */
 106137     sqlite3BeginWriteOperation(pParse, 0, iDb);
 106138     sqlite3OpenMasterTable(pParse, iDb);
 106139     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
 106140     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
 106141     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
 106142     sqlite3ChangeCookie(pParse, iDb);
 106143     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
 106144     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
 106145     if( pParse->nMem<3 ){
 106146       pParse->nMem = 3;
 106152 ** Remove a trigger from the hash tables of the sqlite* pointer.
 106154 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
 106155   Trigger *pTrigger;
 106156   Hash *pHash;
 106158   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 106159   pHash = &(db->aDb[iDb].pSchema->trigHash);
 106160   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
 106161   if( ALWAYS(pTrigger) ){
 106162     if( pTrigger->pSchema==pTrigger->pTabSchema ){
 106163       Table *pTab = tableOfTrigger(pTrigger);
 106164       Trigger **pp;
 106165       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
 106166       *pp = (*pp)->pNext;
 106168     sqlite3DeleteTrigger(db, pTrigger);
 106169     db->flags |= SQLITE_InternChanges;
 106174 ** pEList is the SET clause of an UPDATE statement.  Each entry
 106175 ** in pEList is of the format <id>=<expr>.  If any of the entries
 106176 ** in pEList have an <id> which matches an identifier in pIdList,
 106177 ** then return TRUE.  If pIdList==NULL, then it is considered a
 106178 ** wildcard that matches anything.  Likewise if pEList==NULL then
 106179 ** it matches anything so always return true.  Return false only
 106180 ** if there is no match.
 106182 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
 106183   int e;
 106184   if( pIdList==0 || NEVER(pEList==0) ) return 1;
 106185   for(e=0; e<pEList->nExpr; e++){
 106186     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
 106188   return 0; 
 106192 ** Return a list of all triggers on table pTab if there exists at least
 106193 ** one trigger that must be fired when an operation of type 'op' is 
 106194 ** performed on the table, and, if that operation is an UPDATE, if at
 106195 ** least one of the columns in pChanges is being modified.
 106197 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
 106198   Parse *pParse,          /* Parse context */
 106199   Table *pTab,            /* The table the contains the triggers */
 106200   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
 106201   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
 106202   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 106204   int mask = 0;
 106205   Trigger *pList = 0;
 106206   Trigger *p;
 106208   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
 106209     pList = sqlite3TriggerList(pParse, pTab);
 106211   assert( pList==0 || IsVirtual(pTab)==0 );
 106212   for(p=pList; p; p=p->pNext){
 106213     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
 106214       mask |= p->tr_tm;
 106217   if( pMask ){
 106218     *pMask = mask;
 106220   return (mask ? pList : 0);
 106224 ** Convert the pStep->target token into a SrcList and return a pointer
 106225 ** to that SrcList.
 106227 ** This routine adds a specific database name, if needed, to the target when
 106228 ** forming the SrcList.  This prevents a trigger in one database from
 106229 ** referring to a target in another database.  An exception is when the
 106230 ** trigger is in TEMP in which case it can refer to any other database it
 106231 ** wants.
 106233 static SrcList *targetSrcList(
 106234   Parse *pParse,       /* The parsing context */
 106235   TriggerStep *pStep   /* The trigger containing the target token */
 106237   int iDb;             /* Index of the database to use */
 106238   SrcList *pSrc;       /* SrcList to be returned */
 106240   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
 106241   if( pSrc ){
 106242     assert( pSrc->nSrc>0 );
 106243     assert( pSrc->a!=0 );
 106244     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
 106245     if( iDb==0 || iDb>=2 ){
 106246       sqlite3 *db = pParse->db;
 106247       assert( iDb<pParse->db->nDb );
 106248       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
 106251   return pSrc;
 106255 ** Generate VDBE code for the statements inside the body of a single 
 106256 ** trigger.
 106258 static int codeTriggerProgram(
 106259   Parse *pParse,            /* The parser context */
 106260   TriggerStep *pStepList,   /* List of statements inside the trigger body */
 106261   int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
 106263   TriggerStep *pStep;
 106264   Vdbe *v = pParse->pVdbe;
 106265   sqlite3 *db = pParse->db;
 106267   assert( pParse->pTriggerTab && pParse->pToplevel );
 106268   assert( pStepList );
 106269   assert( v!=0 );
 106270   for(pStep=pStepList; pStep; pStep=pStep->pNext){
 106271     /* Figure out the ON CONFLICT policy that will be used for this step
 106272     ** of the trigger program. If the statement that caused this trigger
 106273     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
 106274     ** the ON CONFLICT policy that was specified as part of the trigger
 106275     ** step statement. Example:
 106277     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
 106278     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
 106279     **   END;
 106281     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
 106282     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
 106284     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
 106285     assert( pParse->okConstFactor==0 );
 106287     switch( pStep->op ){
 106288       case TK_UPDATE: {
 106289         sqlite3Update(pParse, 
 106290           targetSrcList(pParse, pStep),
 106291           sqlite3ExprListDup(db, pStep->pExprList, 0), 
 106292           sqlite3ExprDup(db, pStep->pWhere, 0), 
 106293           pParse->eOrconf
 106295         break;
 106297       case TK_INSERT: {
 106298         sqlite3Insert(pParse, 
 106299           targetSrcList(pParse, pStep),
 106300           sqlite3SelectDup(db, pStep->pSelect, 0), 
 106301           sqlite3IdListDup(db, pStep->pIdList), 
 106302           pParse->eOrconf
 106304         break;
 106306       case TK_DELETE: {
 106307         sqlite3DeleteFrom(pParse, 
 106308           targetSrcList(pParse, pStep),
 106309           sqlite3ExprDup(db, pStep->pWhere, 0)
 106311         break;
 106313       default: assert( pStep->op==TK_SELECT ); {
 106314         SelectDest sDest;
 106315         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
 106316         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
 106317         sqlite3Select(pParse, pSelect, &sDest);
 106318         sqlite3SelectDelete(db, pSelect);
 106319         break;
 106322     if( pStep->op!=TK_SELECT ){
 106323       sqlite3VdbeAddOp0(v, OP_ResetCount);
 106327   return 0;
 106330 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
 106332 ** This function is used to add VdbeComment() annotations to a VDBE
 106333 ** program. It is not used in production code, only for debugging.
 106335 static const char *onErrorText(int onError){
 106336   switch( onError ){
 106337     case OE_Abort:    return "abort";
 106338     case OE_Rollback: return "rollback";
 106339     case OE_Fail:     return "fail";
 106340     case OE_Replace:  return "replace";
 106341     case OE_Ignore:   return "ignore";
 106342     case OE_Default:  return "default";
 106344   return "n/a";
 106346 #endif
 106349 ** Parse context structure pFrom has just been used to create a sub-vdbe
 106350 ** (trigger program). If an error has occurred, transfer error information
 106351 ** from pFrom to pTo.
 106353 static void transferParseError(Parse *pTo, Parse *pFrom){
 106354   assert( pFrom->zErrMsg==0 || pFrom->nErr );
 106355   assert( pTo->zErrMsg==0 || pTo->nErr );
 106356   if( pTo->nErr==0 ){
 106357     pTo->zErrMsg = pFrom->zErrMsg;
 106358     pTo->nErr = pFrom->nErr;
 106359   }else{
 106360     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
 106365 ** Create and populate a new TriggerPrg object with a sub-program 
 106366 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
 106368 static TriggerPrg *codeRowTrigger(
 106369   Parse *pParse,       /* Current parse context */
 106370   Trigger *pTrigger,   /* Trigger to code */
 106371   Table *pTab,         /* The table pTrigger is attached to */
 106372   int orconf           /* ON CONFLICT policy to code trigger program with */
 106374   Parse *pTop = sqlite3ParseToplevel(pParse);
 106375   sqlite3 *db = pParse->db;   /* Database handle */
 106376   TriggerPrg *pPrg;           /* Value to return */
 106377   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
 106378   Vdbe *v;                    /* Temporary VM */
 106379   NameContext sNC;            /* Name context for sub-vdbe */
 106380   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
 106381   Parse *pSubParse;           /* Parse context for sub-vdbe */
 106382   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
 106384   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 106385   assert( pTop->pVdbe );
 106387   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
 106388   ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
 106389   ** list of the top-level Parse object sooner rather than later.  */
 106390   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
 106391   if( !pPrg ) return 0;
 106392   pPrg->pNext = pTop->pTriggerPrg;
 106393   pTop->pTriggerPrg = pPrg;
 106394   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
 106395   if( !pProgram ) return 0;
 106396   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
 106397   pPrg->pTrigger = pTrigger;
 106398   pPrg->orconf = orconf;
 106399   pPrg->aColmask[0] = 0xffffffff;
 106400   pPrg->aColmask[1] = 0xffffffff;
 106402   /* Allocate and populate a new Parse context to use for coding the 
 106403   ** trigger sub-program.  */
 106404   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
 106405   if( !pSubParse ) return 0;
 106406   memset(&sNC, 0, sizeof(sNC));
 106407   sNC.pParse = pSubParse;
 106408   pSubParse->db = db;
 106409   pSubParse->pTriggerTab = pTab;
 106410   pSubParse->pToplevel = pTop;
 106411   pSubParse->zAuthContext = pTrigger->zName;
 106412   pSubParse->eTriggerOp = pTrigger->op;
 106413   pSubParse->nQueryLoop = pParse->nQueryLoop;
 106415   v = sqlite3GetVdbe(pSubParse);
 106416   if( v ){
 106417     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
 106418       pTrigger->zName, onErrorText(orconf),
 106419       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
 106420         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
 106421         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
 106422         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
 106423       pTab->zName
 106424     ));
 106425 #ifndef SQLITE_OMIT_TRACE
 106426     sqlite3VdbeChangeP4(v, -1, 
 106427       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
 106429 #endif
 106431     /* If one was specified, code the WHEN clause. If it evaluates to false
 106432     ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
 106433     ** OP_Halt inserted at the end of the program.  */
 106434     if( pTrigger->pWhen ){
 106435       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
 106436       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
 106437        && db->mallocFailed==0 
 106439         iEndTrigger = sqlite3VdbeMakeLabel(v);
 106440         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
 106442       sqlite3ExprDelete(db, pWhen);
 106445     /* Code the trigger program into the sub-vdbe. */
 106446     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
 106448     /* Insert an OP_Halt at the end of the sub-program. */
 106449     if( iEndTrigger ){
 106450       sqlite3VdbeResolveLabel(v, iEndTrigger);
 106452     sqlite3VdbeAddOp0(v, OP_Halt);
 106453     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
 106455     transferParseError(pParse, pSubParse);
 106456     if( db->mallocFailed==0 ){
 106457       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
 106459     pProgram->nMem = pSubParse->nMem;
 106460     pProgram->nCsr = pSubParse->nTab;
 106461     pProgram->nOnce = pSubParse->nOnce;
 106462     pProgram->token = (void *)pTrigger;
 106463     pPrg->aColmask[0] = pSubParse->oldmask;
 106464     pPrg->aColmask[1] = pSubParse->newmask;
 106465     sqlite3VdbeDelete(v);
 106468   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
 106469   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
 106470   sqlite3ParserReset(pSubParse);
 106471   sqlite3StackFree(db, pSubParse);
 106473   return pPrg;
 106477 ** Return a pointer to a TriggerPrg object containing the sub-program for
 106478 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
 106479 ** TriggerPrg object exists, a new object is allocated and populated before
 106480 ** being returned.
 106482 static TriggerPrg *getRowTrigger(
 106483   Parse *pParse,       /* Current parse context */
 106484   Trigger *pTrigger,   /* Trigger to code */
 106485   Table *pTab,         /* The table trigger pTrigger is attached to */
 106486   int orconf           /* ON CONFLICT algorithm. */
 106488   Parse *pRoot = sqlite3ParseToplevel(pParse);
 106489   TriggerPrg *pPrg;
 106491   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
 106493   /* It may be that this trigger has already been coded (or is in the
 106494   ** process of being coded). If this is the case, then an entry with
 106495   ** a matching TriggerPrg.pTrigger field will be present somewhere
 106496   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
 106497   for(pPrg=pRoot->pTriggerPrg; 
 106498       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
 106499       pPrg=pPrg->pNext
 106502   /* If an existing TriggerPrg could not be located, create a new one. */
 106503   if( !pPrg ){
 106504     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
 106507   return pPrg;
 106511 ** Generate code for the trigger program associated with trigger p on 
 106512 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
 106513 ** function are the same as those described in the header function for
 106514 ** sqlite3CodeRowTrigger()
 106516 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
 106517   Parse *pParse,       /* Parse context */
 106518   Trigger *p,          /* Trigger to code */
 106519   Table *pTab,         /* The table to code triggers from */
 106520   int reg,             /* Reg array containing OLD.* and NEW.* values */
 106521   int orconf,          /* ON CONFLICT policy */
 106522   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
 106524   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
 106525   TriggerPrg *pPrg;
 106526   pPrg = getRowTrigger(pParse, p, pTab, orconf);
 106527   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
 106529   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
 106530   ** is a pointer to the sub-vdbe containing the trigger program.  */
 106531   if( pPrg ){
 106532     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
 106534     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
 106535     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
 106536     VdbeComment(
 106537         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
 106539     /* Set the P5 operand of the OP_Program instruction to non-zero if
 106540     ** recursive invocation of this trigger program is disallowed. Recursive
 106541     ** invocation is disallowed if (a) the sub-program is really a trigger,
 106542     ** not a foreign key action, and (b) the flag to enable recursive triggers
 106543     ** is clear.  */
 106544     sqlite3VdbeChangeP5(v, (u8)bRecursive);
 106549 ** This is called to code the required FOR EACH ROW triggers for an operation
 106550 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
 106551 ** is given by the op parameter. The tr_tm parameter determines whether the
 106552 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
 106553 ** parameter pChanges is passed the list of columns being modified.
 106555 ** If there are no triggers that fire at the specified time for the specified
 106556 ** operation on pTab, this function is a no-op.
 106558 ** The reg argument is the address of the first in an array of registers 
 106559 ** that contain the values substituted for the new.* and old.* references
 106560 ** in the trigger program. If N is the number of columns in table pTab
 106561 ** (a copy of pTab->nCol), then registers are populated as follows:
 106563 **   Register       Contains
 106564 **   ------------------------------------------------------
 106565 **   reg+0          OLD.rowid
 106566 **   reg+1          OLD.* value of left-most column of pTab
 106567 **   ...            ...
 106568 **   reg+N          OLD.* value of right-most column of pTab
 106569 **   reg+N+1        NEW.rowid
 106570 **   reg+N+2        OLD.* value of left-most column of pTab
 106571 **   ...            ...
 106572 **   reg+N+N+1      NEW.* value of right-most column of pTab
 106574 ** For ON DELETE triggers, the registers containing the NEW.* values will
 106575 ** never be accessed by the trigger program, so they are not allocated or 
 106576 ** populated by the caller (there is no data to populate them with anyway). 
 106577 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
 106578 ** are never accessed, and so are not allocated by the caller. So, for an
 106579 ** ON INSERT trigger, the value passed to this function as parameter reg
 106580 ** is not a readable register, although registers (reg+N) through 
 106581 ** (reg+N+N+1) are.
 106583 ** Parameter orconf is the default conflict resolution algorithm for the
 106584 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
 106585 ** is the instruction that control should jump to if a trigger program
 106586 ** raises an IGNORE exception.
 106588 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
 106589   Parse *pParse,       /* Parse context */
 106590   Trigger *pTrigger,   /* List of triggers on table pTab */
 106591   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
 106592   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 106593   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
 106594   Table *pTab,         /* The table to code triggers from */
 106595   int reg,             /* The first in an array of registers (see above) */
 106596   int orconf,          /* ON CONFLICT policy */
 106597   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
 106599   Trigger *p;          /* Used to iterate through pTrigger list */
 106601   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
 106602   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
 106603   assert( (op==TK_UPDATE)==(pChanges!=0) );
 106605   for(p=pTrigger; p; p=p->pNext){
 106607     /* Sanity checking:  The schema for the trigger and for the table are
 106608     ** always defined.  The trigger must be in the same schema as the table
 106609     ** or else it must be a TEMP trigger. */
 106610     assert( p->pSchema!=0 );
 106611     assert( p->pTabSchema!=0 );
 106612     assert( p->pSchema==p->pTabSchema 
 106613          || p->pSchema==pParse->db->aDb[1].pSchema );
 106615     /* Determine whether we should code this trigger */
 106616     if( p->op==op 
 106617      && p->tr_tm==tr_tm 
 106618      && checkColumnOverlap(p->pColumns, pChanges)
 106620       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
 106626 ** Triggers may access values stored in the old.* or new.* pseudo-table. 
 106627 ** This function returns a 32-bit bitmask indicating which columns of the 
 106628 ** old.* or new.* tables actually are used by triggers. This information 
 106629 ** may be used by the caller, for example, to avoid having to load the entire
 106630 ** old.* record into memory when executing an UPDATE or DELETE command.
 106632 ** Bit 0 of the returned mask is set if the left-most column of the
 106633 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
 106634 ** the second leftmost column value is required, and so on. If there
 106635 ** are more than 32 columns in the table, and at least one of the columns
 106636 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
 106638 ** It is not possible to determine if the old.rowid or new.rowid column is 
 106639 ** accessed by triggers. The caller must always assume that it is.
 106641 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
 106642 ** applies to the old.* table. If 1, the new.* table.
 106644 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
 106645 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
 106646 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
 106647 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
 106648 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
 106650 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
 106651   Parse *pParse,       /* Parse context */
 106652   Trigger *pTrigger,   /* List of triggers on table pTab */
 106653   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
 106654   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
 106655   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 106656   Table *pTab,         /* The table to code triggers from */
 106657   int orconf           /* Default ON CONFLICT policy for trigger steps */
 106659   const int op = pChanges ? TK_UPDATE : TK_DELETE;
 106660   u32 mask = 0;
 106661   Trigger *p;
 106663   assert( isNew==1 || isNew==0 );
 106664   for(p=pTrigger; p; p=p->pNext){
 106665     if( p->op==op && (tr_tm&p->tr_tm)
 106666      && checkColumnOverlap(p->pColumns,pChanges)
 106668       TriggerPrg *pPrg;
 106669       pPrg = getRowTrigger(pParse, p, pTab, orconf);
 106670       if( pPrg ){
 106671         mask |= pPrg->aColmask[isNew];
 106676   return mask;
 106679 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
 106681 /************** End of trigger.c *********************************************/
 106682 /************** Begin file update.c ******************************************/
 106684 ** 2001 September 15
 106686 ** The author disclaims copyright to this source code.  In place of
 106687 ** a legal notice, here is a blessing:
 106689 **    May you do good and not evil.
 106690 **    May you find forgiveness for yourself and forgive others.
 106691 **    May you share freely, never taking more than you give.
 106693 *************************************************************************
 106694 ** This file contains C code routines that are called by the parser
 106695 ** to handle UPDATE statements.
 106698 #ifndef SQLITE_OMIT_VIRTUALTABLE
 106699 /* Forward declaration */
 106700 static void updateVirtualTable(
 106701   Parse *pParse,       /* The parsing context */
 106702   SrcList *pSrc,       /* The virtual table to be modified */
 106703   Table *pTab,         /* The virtual table */
 106704   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
 106705   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
 106706   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
 106707   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
 106708   int onError          /* ON CONFLICT strategy */
 106710 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 106713 ** The most recently coded instruction was an OP_Column to retrieve the
 106714 ** i-th column of table pTab. This routine sets the P4 parameter of the 
 106715 ** OP_Column to the default value, if any.
 106717 ** The default value of a column is specified by a DEFAULT clause in the 
 106718 ** column definition. This was either supplied by the user when the table
 106719 ** was created, or added later to the table definition by an ALTER TABLE
 106720 ** command. If the latter, then the row-records in the table btree on disk
 106721 ** may not contain a value for the column and the default value, taken
 106722 ** from the P4 parameter of the OP_Column instruction, is returned instead.
 106723 ** If the former, then all row-records are guaranteed to include a value
 106724 ** for the column and the P4 value is not required.
 106726 ** Column definitions created by an ALTER TABLE command may only have 
 106727 ** literal default values specified: a number, null or a string. (If a more
 106728 ** complicated default expression value was provided, it is evaluated 
 106729 ** when the ALTER TABLE is executed and one of the literal values written
 106730 ** into the sqlite_master table.)
 106732 ** Therefore, the P4 parameter is only required if the default value for
 106733 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
 106734 ** function is capable of transforming these types of expressions into
 106735 ** sqlite3_value objects.
 106737 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
 106738 ** on register iReg. This is used when an equivalent integer value is 
 106739 ** stored in place of an 8-byte floating point value in order to save 
 106740 ** space.
 106742 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
 106743   assert( pTab!=0 );
 106744   if( !pTab->pSelect ){
 106745     sqlite3_value *pValue = 0;
 106746     u8 enc = ENC(sqlite3VdbeDb(v));
 106747     Column *pCol = &pTab->aCol[i];
 106748     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
 106749     assert( i<pTab->nCol );
 106750     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
 106751                          pCol->affinity, &pValue);
 106752     if( pValue ){
 106753       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
 106755 #ifndef SQLITE_OMIT_FLOATING_POINT
 106756     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
 106757       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
 106759 #endif
 106764 ** Process an UPDATE statement.
 106766 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
 106767 **          \_______/ \________/     \______/       \________________/
 106768 *            onError   pTabList      pChanges             pWhere
 106770 SQLITE_PRIVATE void sqlite3Update(
 106771   Parse *pParse,         /* The parser context */
 106772   SrcList *pTabList,     /* The table in which we should change things */
 106773   ExprList *pChanges,    /* Things to be changed */
 106774   Expr *pWhere,          /* The WHERE clause.  May be null */
 106775   int onError            /* How to handle constraint errors */
 106777   int i, j;              /* Loop counters */
 106778   Table *pTab;           /* The table to be updated */
 106779   int addrTop = 0;       /* VDBE instruction address of the start of the loop */
 106780   WhereInfo *pWInfo;     /* Information about the WHERE clause */
 106781   Vdbe *v;               /* The virtual database engine */
 106782   Index *pIdx;           /* For looping over indices */
 106783   Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
 106784   int nIdx;              /* Number of indices that need updating */
 106785   int iBaseCur;          /* Base cursor number */
 106786   int iDataCur;          /* Cursor for the canonical data btree */
 106787   int iIdxCur;           /* Cursor for the first index */
 106788   sqlite3 *db;           /* The database structure */
 106789   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
 106790   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
 106791                          ** an expression for the i-th column of the table.
 106792                          ** aXRef[i]==-1 if the i-th column is not changed. */
 106793   u8 *aToOpen;           /* 1 for tables and indices to be opened */
 106794   u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
 106795   u8 chngRowid;          /* Rowid changed in a normal table */
 106796   u8 chngKey;            /* Either chngPk or chngRowid */
 106797   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
 106798   AuthContext sContext;  /* The authorization context */
 106799   NameContext sNC;       /* The name-context to resolve expressions in */
 106800   int iDb;               /* Database containing the table being updated */
 106801   int okOnePass;         /* True for one-pass algorithm without the FIFO */
 106802   int hasFK;             /* True if foreign key processing is required */
 106803   int labelBreak;        /* Jump here to break out of UPDATE loop */
 106804   int labelContinue;     /* Jump here to continue next step of UPDATE loop */
 106806 #ifndef SQLITE_OMIT_TRIGGER
 106807   int isView;            /* True when updating a view (INSTEAD OF trigger) */
 106808   Trigger *pTrigger;     /* List of triggers on pTab, if required */
 106809   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
 106810 #endif
 106811   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
 106812   int iEph = 0;          /* Ephemeral table holding all primary key values */
 106813   int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
 106814   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
 106816   /* Register Allocations */
 106817   int regRowCount = 0;   /* A count of rows changed */
 106818   int regOldRowid;       /* The old rowid */
 106819   int regNewRowid;       /* The new rowid */
 106820   int regNew;            /* Content of the NEW.* table in triggers */
 106821   int regOld = 0;        /* Content of OLD.* table in triggers */
 106822   int regRowSet = 0;     /* Rowset of rows to be updated */
 106823   int regKey = 0;        /* composite PRIMARY KEY value */
 106825   memset(&sContext, 0, sizeof(sContext));
 106826   db = pParse->db;
 106827   if( pParse->nErr || db->mallocFailed ){
 106828     goto update_cleanup;
 106830   assert( pTabList->nSrc==1 );
 106832   /* Locate the table which we want to update. 
 106834   pTab = sqlite3SrcListLookup(pParse, pTabList);
 106835   if( pTab==0 ) goto update_cleanup;
 106836   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 106838   /* Figure out if we have any triggers and if the table being
 106839   ** updated is a view.
 106841 #ifndef SQLITE_OMIT_TRIGGER
 106842   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
 106843   isView = pTab->pSelect!=0;
 106844   assert( pTrigger || tmask==0 );
 106845 #else
 106846 # define pTrigger 0
 106847 # define isView 0
 106848 # define tmask 0
 106849 #endif
 106850 #ifdef SQLITE_OMIT_VIEW
 106851 # undef isView
 106852 # define isView 0
 106853 #endif
 106855   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
 106856     goto update_cleanup;
 106858   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
 106859     goto update_cleanup;
 106862   /* Allocate a cursors for the main database table and for all indices.
 106863   ** The index cursors might not be used, but if they are used they
 106864   ** need to occur right after the database cursor.  So go ahead and
 106865   ** allocate enough space, just in case.
 106867   pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
 106868   iIdxCur = iDataCur+1;
 106869   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
 106870   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
 106871     if( pIdx->autoIndex==2 && pPk!=0 ){
 106872       iDataCur = pParse->nTab;
 106873       pTabList->a[0].iCursor = iDataCur;
 106875     pParse->nTab++;
 106878   /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
 106879   ** Initialize aXRef[] and aToOpen[] to their default values.
 106881   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
 106882   if( aXRef==0 ) goto update_cleanup;
 106883   aRegIdx = aXRef+pTab->nCol;
 106884   aToOpen = (u8*)(aRegIdx+nIdx);
 106885   memset(aToOpen, 1, nIdx+1);
 106886   aToOpen[nIdx+1] = 0;
 106887   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
 106889   /* Initialize the name-context */
 106890   memset(&sNC, 0, sizeof(sNC));
 106891   sNC.pParse = pParse;
 106892   sNC.pSrcList = pTabList;
 106894   /* Resolve the column names in all the expressions of the
 106895   ** of the UPDATE statement.  Also find the column index
 106896   ** for each column to be updated in the pChanges array.  For each
 106897   ** column to be updated, make sure we have authorization to change
 106898   ** that column.
 106900   chngRowid = chngPk = 0;
 106901   for(i=0; i<pChanges->nExpr; i++){
 106902     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
 106903       goto update_cleanup;
 106905     for(j=0; j<pTab->nCol; j++){
 106906       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
 106907         if( j==pTab->iPKey ){
 106908           chngRowid = 1;
 106909           pRowidExpr = pChanges->a[i].pExpr;
 106910         }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
 106911           chngPk = 1;
 106913         aXRef[j] = i;
 106914         break;
 106917     if( j>=pTab->nCol ){
 106918       if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
 106919         j = -1;
 106920         chngRowid = 1;
 106921         pRowidExpr = pChanges->a[i].pExpr;
 106922       }else{
 106923         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
 106924         pParse->checkSchema = 1;
 106925         goto update_cleanup;
 106928 #ifndef SQLITE_OMIT_AUTHORIZATION
 106930       int rc;
 106931       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
 106932                             j<0 ? "ROWID" : pTab->aCol[j].zName,
 106933                             db->aDb[iDb].zName);
 106934       if( rc==SQLITE_DENY ){
 106935         goto update_cleanup;
 106936       }else if( rc==SQLITE_IGNORE ){
 106937         aXRef[j] = -1;
 106940 #endif
 106942   assert( (chngRowid & chngPk)==0 );
 106943   assert( chngRowid==0 || chngRowid==1 );
 106944   assert( chngPk==0 || chngPk==1 );
 106945   chngKey = chngRowid + chngPk;
 106947   /* The SET expressions are not actually used inside the WHERE loop.
 106948   ** So reset the colUsed mask
 106950   pTabList->a[0].colUsed = 0;
 106952   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
 106954   /* There is one entry in the aRegIdx[] array for each index on the table
 106955   ** being updated.  Fill in aRegIdx[] with a register number that will hold
 106956   ** the key for accessing each index.  
 106958   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 106959     int reg;
 106960     if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
 106961       reg = ++pParse->nMem;
 106962     }else{
 106963       reg = 0;
 106964       for(i=0; i<pIdx->nKeyCol; i++){
 106965         if( aXRef[pIdx->aiColumn[i]]>=0 ){
 106966           reg = ++pParse->nMem;
 106967           break;
 106971     if( reg==0 ) aToOpen[j+1] = 0;
 106972     aRegIdx[j] = reg;
 106975   /* Begin generating code. */
 106976   v = sqlite3GetVdbe(pParse);
 106977   if( v==0 ) goto update_cleanup;
 106978   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
 106979   sqlite3BeginWriteOperation(pParse, 1, iDb);
 106981 #ifndef SQLITE_OMIT_VIRTUALTABLE
 106982   /* Virtual tables must be handled separately */
 106983   if( IsVirtual(pTab) ){
 106984     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
 106985                        pWhere, onError);
 106986     pWhere = 0;
 106987     pTabList = 0;
 106988     goto update_cleanup;
 106990 #endif
 106992   /* Allocate required registers. */
 106993   regRowSet = ++pParse->nMem;
 106994   regOldRowid = regNewRowid = ++pParse->nMem;
 106995   if( chngPk || pTrigger || hasFK ){
 106996     regOld = pParse->nMem + 1;
 106997     pParse->nMem += pTab->nCol;
 106999   if( chngKey || pTrigger || hasFK ){
 107000     regNewRowid = ++pParse->nMem;
 107002   regNew = pParse->nMem + 1;
 107003   pParse->nMem += pTab->nCol;
 107005   /* Start the view context. */
 107006   if( isView ){
 107007     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
 107010   /* If we are trying to update a view, realize that view into
 107011   ** a ephemeral table.
 107013 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 107014   if( isView ){
 107015     sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
 107017 #endif
 107019   /* Resolve the column names in all the expressions in the
 107020   ** WHERE clause.
 107022   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
 107023     goto update_cleanup;
 107026   /* Begin the database scan
 107028   if( HasRowid(pTab) ){
 107029     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
 107030     pWInfo = sqlite3WhereBegin(
 107031         pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
 107033     if( pWInfo==0 ) goto update_cleanup;
 107034     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
 107036     /* Remember the rowid of every item to be updated.
 107038     sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
 107039     if( !okOnePass ){
 107040       sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
 107043     /* End the database scan loop.
 107045     sqlite3WhereEnd(pWInfo);
 107046   }else{
 107047     int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
 107048     i16 nPk;         /* Number of components of the PRIMARY KEY */
 107049     int addrOpen;    /* Address of the OpenEphemeral instruction */
 107051     assert( pPk!=0 );
 107052     nPk = pPk->nKeyCol;
 107053     iPk = pParse->nMem+1;
 107054     pParse->nMem += nPk;
 107055     regKey = ++pParse->nMem;
 107056     iEph = pParse->nTab++;
 107057     sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
 107058     addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
 107059     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
 107060     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
 107061                                WHERE_ONEPASS_DESIRED, iIdxCur);
 107062     if( pWInfo==0 ) goto update_cleanup;
 107063     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
 107064     for(i=0; i<nPk; i++){
 107065       sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
 107066                                       iPk+i);
 107068     if( okOnePass ){
 107069       sqlite3VdbeChangeToNoop(v, addrOpen);
 107070       nKey = nPk;
 107071       regKey = iPk;
 107072     }else{
 107073       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
 107074                         sqlite3IndexAffinityStr(v, pPk), nPk);
 107075       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
 107077     sqlite3WhereEnd(pWInfo);
 107080   /* Initialize the count of updated rows
 107082   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
 107083     regRowCount = ++pParse->nMem;
 107084     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
 107087   labelBreak = sqlite3VdbeMakeLabel(v);
 107088   if( !isView ){
 107090     ** Open every index that needs updating.  Note that if any
 107091     ** index could potentially invoke a REPLACE conflict resolution 
 107092     ** action, then we need to open all indices because we might need
 107093     ** to be deleting some records.
 107095     if( onError==OE_Replace ){
 107096       memset(aToOpen, 1, nIdx+1);
 107097     }else{
 107098       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 107099         if( pIdx->onError==OE_Replace ){
 107100           memset(aToOpen, 1, nIdx+1);
 107101           break;
 107105     if( okOnePass ){
 107106       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
 107107       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
 107109     sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
 107110                                0, 0);
 107113   /* Top of the update loop */
 107114   if( okOnePass ){
 107115     if( aToOpen[iDataCur-iBaseCur] ){
 107116       assert( pPk!=0 );
 107117       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
 107118       VdbeCoverageNeverTaken(v);
 107120     labelContinue = labelBreak;
 107121     sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
 107122     VdbeCoverage(v);
 107123   }else if( pPk ){
 107124     labelContinue = sqlite3VdbeMakeLabel(v);
 107125     sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
 107126     addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
 107127     sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
 107128     VdbeCoverage(v);
 107129   }else{
 107130     labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
 107131                              regOldRowid);
 107132     VdbeCoverage(v);
 107133     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
 107134     VdbeCoverage(v);
 107137   /* If the record number will change, set register regNewRowid to
 107138   ** contain the new value. If the record number is not being modified,
 107139   ** then regNewRowid is the same register as regOldRowid, which is
 107140   ** already populated.  */
 107141   assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
 107142   if( chngRowid ){
 107143     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
 107144     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
 107147   /* Compute the old pre-UPDATE content of the row being changed, if that
 107148   ** information is needed */
 107149   if( chngPk || hasFK || pTrigger ){
 107150     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
 107151     oldmask |= sqlite3TriggerColmask(pParse, 
 107152         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
 107154     for(i=0; i<pTab->nCol; i++){
 107155       if( oldmask==0xffffffff
 107156        || (i<32 && (oldmask & MASKBIT32(i))!=0)
 107157        || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
 107159         testcase(  oldmask!=0xffffffff && i==31 );
 107160         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
 107161       }else{
 107162         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
 107165     if( chngRowid==0 && pPk==0 ){
 107166       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
 107170   /* Populate the array of registers beginning at regNew with the new
 107171   ** row data. This array is used to check constaints, create the new
 107172   ** table and index records, and as the values for any new.* references
 107173   ** made by triggers.
 107175   ** If there are one or more BEFORE triggers, then do not populate the
 107176   ** registers associated with columns that are (a) not modified by
 107177   ** this UPDATE statement and (b) not accessed by new.* references. The
 107178   ** values for registers not modified by the UPDATE must be reloaded from 
 107179   ** the database after the BEFORE triggers are fired anyway (as the trigger 
 107180   ** may have modified them). So not loading those that are not going to
 107181   ** be used eliminates some redundant opcodes.
 107183   newmask = sqlite3TriggerColmask(
 107184       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
 107186   /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
 107187   for(i=0; i<pTab->nCol; i++){
 107188     if( i==pTab->iPKey ){
 107189       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
 107190     }else{
 107191       j = aXRef[i];
 107192       if( j>=0 ){
 107193         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
 107194       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
 107195         /* This branch loads the value of a column that will not be changed 
 107196         ** into a register. This is done if there are no BEFORE triggers, or
 107197         ** if there are one or more BEFORE triggers that use this value via
 107198         ** a new.* reference in a trigger program.
 107200         testcase( i==31 );
 107201         testcase( i==32 );
 107202         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
 107203       }else{
 107204         sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
 107209   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
 107210   ** verified. One could argue that this is wrong.
 107212   if( tmask&TRIGGER_BEFORE ){
 107213     sqlite3TableAffinity(v, pTab, regNew);
 107214     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 107215         TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
 107217     /* The row-trigger may have deleted the row being updated. In this
 107218     ** case, jump to the next row. No updates or AFTER triggers are 
 107219     ** required. This behavior - what happens when the row being updated
 107220     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
 107221     ** documentation.
 107223     if( pPk ){
 107224       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
 107225       VdbeCoverage(v);
 107226     }else{
 107227       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
 107228       VdbeCoverage(v);
 107231     /* If it did not delete it, the row-trigger may still have modified 
 107232     ** some of the columns of the row being updated. Load the values for 
 107233     ** all columns not modified by the update statement into their 
 107234     ** registers in case this has happened.
 107236     for(i=0; i<pTab->nCol; i++){
 107237       if( aXRef[i]<0 && i!=pTab->iPKey ){
 107238         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
 107243   if( !isView ){
 107244     int j1 = 0;           /* Address of jump instruction */
 107245     int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
 107247     /* Do constraint checks. */
 107248     assert( regOldRowid>0 );
 107249     sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
 107250         regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
 107252     /* Do FK constraint checks. */
 107253     if( hasFK ){
 107254       sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
 107257     /* Delete the index entries associated with the current record.  */
 107258     if( bReplace || chngKey ){
 107259       if( pPk ){
 107260         j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
 107261       }else{
 107262         j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
 107264       VdbeCoverageNeverTaken(v);
 107266     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
 107268     /* If changing the record number, delete the old record.  */
 107269     if( hasFK || chngKey || pPk!=0 ){
 107270       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
 107272     if( bReplace || chngKey ){
 107273       sqlite3VdbeJumpHere(v, j1);
 107276     if( hasFK ){
 107277       sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
 107280     /* Insert the new index entries and the new record. */
 107281     sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
 107282                              regNewRowid, aRegIdx, 1, 0, 0);
 107284     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 107285     ** handle rows (possibly in other tables) that refer via a foreign key
 107286     ** to the row just updated. */ 
 107287     if( hasFK ){
 107288       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
 107292   /* Increment the row counter 
 107294   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
 107295     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
 107298   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
 107299       TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
 107301   /* Repeat the above with the next record to be updated, until
 107302   ** all record selected by the WHERE clause have been updated.
 107304   if( okOnePass ){
 107305     /* Nothing to do at end-of-loop for a single-pass */
 107306   }else if( pPk ){
 107307     sqlite3VdbeResolveLabel(v, labelContinue);
 107308     sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
 107309   }else{
 107310     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
 107312   sqlite3VdbeResolveLabel(v, labelBreak);
 107314   /* Close all tables */
 107315   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 107316     assert( aRegIdx );
 107317     if( aToOpen[i+1] ){
 107318       sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
 107321   if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
 107323   /* Update the sqlite_sequence table by storing the content of the
 107324   ** maximum rowid counter values recorded while inserting into
 107325   ** autoincrement tables.
 107327   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
 107328     sqlite3AutoincrementEnd(pParse);
 107332   ** Return the number of rows that were changed. If this routine is 
 107333   ** generating code because of a call to sqlite3NestedParse(), do not
 107334   ** invoke the callback function.
 107336   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
 107337     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
 107338     sqlite3VdbeSetNumCols(v, 1);
 107339     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
 107342 update_cleanup:
 107343   sqlite3AuthContextPop(&sContext);
 107344   sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
 107345   sqlite3SrcListDelete(db, pTabList);
 107346   sqlite3ExprListDelete(db, pChanges);
 107347   sqlite3ExprDelete(db, pWhere);
 107348   return;
 107350 /* Make sure "isView" and other macros defined above are undefined. Otherwise
 107351 ** thely may interfere with compilation of other functions in this file
 107352 ** (or in another file, if this file becomes part of the amalgamation).  */
 107353 #ifdef isView
 107354  #undef isView
 107355 #endif
 107356 #ifdef pTrigger
 107357  #undef pTrigger
 107358 #endif
 107360 #ifndef SQLITE_OMIT_VIRTUALTABLE
 107362 ** Generate code for an UPDATE of a virtual table.
 107364 ** The strategy is that we create an ephemerial table that contains
 107365 ** for each row to be changed:
 107367 **   (A)  The original rowid of that row.
 107368 **   (B)  The revised rowid for the row. (note1)
 107369 **   (C)  The content of every column in the row.
 107371 ** Then we loop over this ephemeral table and for each row in
 107372 ** the ephermeral table call VUpdate.
 107374 ** When finished, drop the ephemeral table.
 107376 ** (note1) Actually, if we know in advance that (A) is always the same
 107377 ** as (B) we only store (A), then duplicate (A) when pulling
 107378 ** it out of the ephemeral table before calling VUpdate.
 107380 static void updateVirtualTable(
 107381   Parse *pParse,       /* The parsing context */
 107382   SrcList *pSrc,       /* The virtual table to be modified */
 107383   Table *pTab,         /* The virtual table */
 107384   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
 107385   Expr *pRowid,        /* Expression used to recompute the rowid */
 107386   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
 107387   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
 107388   int onError          /* ON CONFLICT strategy */
 107390   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
 107391   ExprList *pEList = 0;     /* The result set of the SELECT statement */
 107392   Select *pSelect = 0;      /* The SELECT statement */
 107393   Expr *pExpr;              /* Temporary expression */
 107394   int ephemTab;             /* Table holding the result of the SELECT */
 107395   int i;                    /* Loop counter */
 107396   int addr;                 /* Address of top of loop */
 107397   int iReg;                 /* First register in set passed to OP_VUpdate */
 107398   sqlite3 *db = pParse->db; /* Database connection */
 107399   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
 107400   SelectDest dest;
 107402   /* Construct the SELECT statement that will find the new values for
 107403   ** all updated rows. 
 107405   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
 107406   if( pRowid ){
 107407     pEList = sqlite3ExprListAppend(pParse, pEList,
 107408                                    sqlite3ExprDup(db, pRowid, 0));
 107410   assert( pTab->iPKey<0 );
 107411   for(i=0; i<pTab->nCol; i++){
 107412     if( aXRef[i]>=0 ){
 107413       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
 107414     }else{
 107415       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
 107417     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
 107419   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
 107421   /* Create the ephemeral table into which the update results will
 107422   ** be stored.
 107424   assert( v );
 107425   ephemTab = pParse->nTab++;
 107426   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
 107427   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 107429   /* fill the ephemeral table 
 107431   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
 107432   sqlite3Select(pParse, pSelect, &dest);
 107434   /* Generate code to scan the ephemeral table and call VUpdate. */
 107435   iReg = ++pParse->nMem;
 107436   pParse->nMem += pTab->nCol+1;
 107437   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
 107438   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
 107439   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
 107440   for(i=0; i<pTab->nCol; i++){
 107441     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
 107443   sqlite3VtabMakeWritable(pParse, pTab);
 107444   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
 107445   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
 107446   sqlite3MayAbort(pParse);
 107447   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
 107448   sqlite3VdbeJumpHere(v, addr);
 107449   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
 107451   /* Cleanup */
 107452   sqlite3SelectDelete(db, pSelect);  
 107454 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 107456 /************** End of update.c **********************************************/
 107457 /************** Begin file vacuum.c ******************************************/
 107459 ** 2003 April 6
 107461 ** The author disclaims copyright to this source code.  In place of
 107462 ** a legal notice, here is a blessing:
 107464 **    May you do good and not evil.
 107465 **    May you find forgiveness for yourself and forgive others.
 107466 **    May you share freely, never taking more than you give.
 107468 *************************************************************************
 107469 ** This file contains code used to implement the VACUUM command.
 107471 ** Most of the code in this file may be omitted by defining the
 107472 ** SQLITE_OMIT_VACUUM macro.
 107475 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 107477 ** Finalize a prepared statement.  If there was an error, store the
 107478 ** text of the error message in *pzErrMsg.  Return the result code.
 107480 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
 107481   int rc;
 107482   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
 107483   if( rc ){
 107484     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
 107486   return rc;
 107490 ** Execute zSql on database db. Return an error code.
 107492 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
 107493   sqlite3_stmt *pStmt;
 107494   VVA_ONLY( int rc; )
 107495   if( !zSql ){
 107496     return SQLITE_NOMEM;
 107498   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
 107499     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
 107500     return sqlite3_errcode(db);
 107502   VVA_ONLY( rc = ) sqlite3_step(pStmt);
 107503   assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
 107504   return vacuumFinalize(db, pStmt, pzErrMsg);
 107508 ** Execute zSql on database db. The statement returns exactly
 107509 ** one column. Execute this as SQL on the same database.
 107511 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
 107512   sqlite3_stmt *pStmt;
 107513   int rc;
 107515   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 107516   if( rc!=SQLITE_OK ) return rc;
 107518   while( SQLITE_ROW==sqlite3_step(pStmt) ){
 107519     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
 107520     if( rc!=SQLITE_OK ){
 107521       vacuumFinalize(db, pStmt, pzErrMsg);
 107522       return rc;
 107526   return vacuumFinalize(db, pStmt, pzErrMsg);
 107530 ** The VACUUM command is used to clean up the database,
 107531 ** collapse free space, etc.  It is modelled after the VACUUM command
 107532 ** in PostgreSQL.  The VACUUM command works as follows:
 107534 **   (1)  Create a new transient database file
 107535 **   (2)  Copy all content from the database being vacuumed into
 107536 **        the new transient database file
 107537 **   (3)  Copy content from the transient database back into the
 107538 **        original database.
 107540 ** The transient database requires temporary disk space approximately
 107541 ** equal to the size of the original database.  The copy operation of
 107542 ** step (3) requires additional temporary disk space approximately equal
 107543 ** to the size of the original database for the rollback journal.
 107544 ** Hence, temporary disk space that is approximately 2x the size of the
 107545 ** orginal database is required.  Every page of the database is written
 107546 ** approximately 3 times:  Once for step (2) and twice for step (3).
 107547 ** Two writes per page are required in step (3) because the original
 107548 ** database content must be written into the rollback journal prior to
 107549 ** overwriting the database with the vacuumed content.
 107551 ** Only 1x temporary space and only 1x writes would be required if
 107552 ** the copy of step (3) were replace by deleting the original database
 107553 ** and renaming the transient database as the original.  But that will
 107554 ** not work if other processes are attached to the original database.
 107555 ** And a power loss in between deleting the original and renaming the
 107556 ** transient would cause the database file to appear to be deleted
 107557 ** following reboot.
 107559 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
 107560   Vdbe *v = sqlite3GetVdbe(pParse);
 107561   if( v ){
 107562     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
 107563     sqlite3VdbeUsesBtree(v, 0);
 107565   return;
 107569 ** This routine implements the OP_Vacuum opcode of the VDBE.
 107571 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
 107572   int rc = SQLITE_OK;     /* Return code from service routines */
 107573   Btree *pMain;           /* The database being vacuumed */
 107574   Btree *pTemp;           /* The temporary database we vacuum into */
 107575   char *zSql = 0;         /* SQL statements */
 107576   int saved_flags;        /* Saved value of the db->flags */
 107577   int saved_nChange;      /* Saved value of db->nChange */
 107578   int saved_nTotalChange; /* Saved value of db->nTotalChange */
 107579   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
 107580   Db *pDb = 0;            /* Database to detach at end of vacuum */
 107581   int isMemDb;            /* True if vacuuming a :memory: database */
 107582   int nRes;               /* Bytes of reserved space at the end of each page */
 107583   int nDb;                /* Number of attached databases */
 107585   if( !db->autoCommit ){
 107586     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
 107587     return SQLITE_ERROR;
 107589   if( db->nVdbeActive>1 ){
 107590     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
 107591     return SQLITE_ERROR;
 107594   /* Save the current value of the database flags so that it can be 
 107595   ** restored before returning. Then set the writable-schema flag, and
 107596   ** disable CHECK and foreign key constraints.  */
 107597   saved_flags = db->flags;
 107598   saved_nChange = db->nChange;
 107599   saved_nTotalChange = db->nTotalChange;
 107600   saved_xTrace = db->xTrace;
 107601   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
 107602   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
 107603   db->xTrace = 0;
 107605   pMain = db->aDb[0].pBt;
 107606   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
 107608   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
 107609   ** can be set to 'off' for this file, as it is not recovered if a crash
 107610   ** occurs anyway. The integrity of the database is maintained by a
 107611   ** (possibly synchronous) transaction opened on the main database before
 107612   ** sqlite3BtreeCopyFile() is called.
 107614   ** An optimisation would be to use a non-journaled pager.
 107615   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
 107616   ** that actually made the VACUUM run slower.  Very little journalling
 107617   ** actually occurs when doing a vacuum since the vacuum_db is initially
 107618   ** empty.  Only the journal header is written.  Apparently it takes more
 107619   ** time to parse and run the PRAGMA to turn journalling off than it does
 107620   ** to write the journal header file.
 107622   nDb = db->nDb;
 107623   if( sqlite3TempInMemory(db) ){
 107624     zSql = "ATTACH ':memory:' AS vacuum_db;";
 107625   }else{
 107626     zSql = "ATTACH '' AS vacuum_db;";
 107628   rc = execSql(db, pzErrMsg, zSql);
 107629   if( db->nDb>nDb ){
 107630     pDb = &db->aDb[db->nDb-1];
 107631     assert( strcmp(pDb->zName,"vacuum_db")==0 );
 107633   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107634   pTemp = db->aDb[db->nDb-1].pBt;
 107636   /* The call to execSql() to attach the temp database has left the file
 107637   ** locked (as there was more than one active statement when the transaction
 107638   ** to read the schema was concluded. Unlock it here so that this doesn't
 107639   ** cause problems for the call to BtreeSetPageSize() below.  */
 107640   sqlite3BtreeCommit(pTemp);
 107642   nRes = sqlite3BtreeGetReserve(pMain);
 107644   /* A VACUUM cannot change the pagesize of an encrypted database. */
 107645 #ifdef SQLITE_HAS_CODEC
 107646   if( db->nextPagesize ){
 107647     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
 107648     int nKey;
 107649     char *zKey;
 107650     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 107651     if( nKey ) db->nextPagesize = 0;
 107653 #endif
 107655   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
 107656   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107658   /* Begin a transaction and take an exclusive lock on the main database
 107659   ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
 107660   ** to ensure that we do not try to change the page-size on a WAL database.
 107662   rc = execSql(db, pzErrMsg, "BEGIN;");
 107663   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107664   rc = sqlite3BtreeBeginTrans(pMain, 2);
 107665   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107667   /* Do not attempt to change the page size for a WAL database */
 107668   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
 107669                                                ==PAGER_JOURNALMODE_WAL ){
 107670     db->nextPagesize = 0;
 107673   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
 107674    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
 107675    || NEVER(db->mallocFailed)
 107677     rc = SQLITE_NOMEM;
 107678     goto end_of_vacuum;
 107681 #ifndef SQLITE_OMIT_AUTOVACUUM
 107682   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
 107683                                            sqlite3BtreeGetAutoVacuum(pMain));
 107684 #endif
 107686   /* Query the schema of the main database. Create a mirror schema
 107687   ** in the temporary database.
 107689   rc = execExecSql(db, pzErrMsg,
 107690       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
 107691       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
 107692       "   AND coalesce(rootpage,1)>0"
 107694   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107695   rc = execExecSql(db, pzErrMsg,
 107696       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
 107697       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
 107698   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107699   rc = execExecSql(db, pzErrMsg,
 107700       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
 107701       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
 107702   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107704   /* Loop through the tables in the main database. For each, do
 107705   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
 107706   ** the contents to the temporary database.
 107708   rc = execExecSql(db, pzErrMsg,
 107709       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
 107710       "|| ' SELECT * FROM main.' || quote(name) || ';'"
 107711       "FROM main.sqlite_master "
 107712       "WHERE type = 'table' AND name!='sqlite_sequence' "
 107713       "  AND coalesce(rootpage,1)>0"
 107715   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107717   /* Copy over the sequence table
 107719   rc = execExecSql(db, pzErrMsg,
 107720       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
 107721       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
 107723   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107724   rc = execExecSql(db, pzErrMsg,
 107725       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
 107726       "|| ' SELECT * FROM main.' || quote(name) || ';' "
 107727       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
 107729   if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107732   /* Copy the triggers, views, and virtual tables from the main database
 107733   ** over to the temporary database.  None of these objects has any
 107734   ** associated storage, so all we have to do is copy their entries
 107735   ** from the SQLITE_MASTER table.
 107737   rc = execSql(db, pzErrMsg,
 107738       "INSERT INTO vacuum_db.sqlite_master "
 107739       "  SELECT type, name, tbl_name, rootpage, sql"
 107740       "    FROM main.sqlite_master"
 107741       "   WHERE type='view' OR type='trigger'"
 107742       "      OR (type='table' AND rootpage=0)"
 107744   if( rc ) goto end_of_vacuum;
 107746   /* At this point, there is a write transaction open on both the 
 107747   ** vacuum database and the main database. Assuming no error occurs,
 107748   ** both transactions are closed by this block - the main database
 107749   ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
 107750   ** call to sqlite3BtreeCommit().
 107753     u32 meta;
 107754     int i;
 107756     /* This array determines which meta meta values are preserved in the
 107757     ** vacuum.  Even entries are the meta value number and odd entries
 107758     ** are an increment to apply to the meta value after the vacuum.
 107759     ** The increment is used to increase the schema cookie so that other
 107760     ** connections to the same database will know to reread the schema.
 107762     static const unsigned char aCopy[] = {
 107763        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
 107764        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
 107765        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
 107766        BTREE_USER_VERSION,       0,  /* Preserve the user version */
 107767        BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
 107770     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
 107771     assert( 1==sqlite3BtreeIsInTrans(pMain) );
 107773     /* Copy Btree meta values */
 107774     for(i=0; i<ArraySize(aCopy); i+=2){
 107775       /* GetMeta() and UpdateMeta() cannot fail in this context because
 107776       ** we already have page 1 loaded into cache and marked dirty. */
 107777       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
 107778       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
 107779       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
 107782     rc = sqlite3BtreeCopyFile(pMain, pTemp);
 107783     if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107784     rc = sqlite3BtreeCommit(pTemp);
 107785     if( rc!=SQLITE_OK ) goto end_of_vacuum;
 107786 #ifndef SQLITE_OMIT_AUTOVACUUM
 107787     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
 107788 #endif
 107791   assert( rc==SQLITE_OK );
 107792   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
 107794 end_of_vacuum:
 107795   /* Restore the original value of db->flags */
 107796   db->flags = saved_flags;
 107797   db->nChange = saved_nChange;
 107798   db->nTotalChange = saved_nTotalChange;
 107799   db->xTrace = saved_xTrace;
 107800   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
 107802   /* Currently there is an SQL level transaction open on the vacuum
 107803   ** database. No locks are held on any other files (since the main file
 107804   ** was committed at the btree level). So it safe to end the transaction
 107805   ** by manually setting the autoCommit flag to true and detaching the
 107806   ** vacuum database. The vacuum_db journal file is deleted when the pager
 107807   ** is closed by the DETACH.
 107809   db->autoCommit = 1;
 107811   if( pDb ){
 107812     sqlite3BtreeClose(pDb->pBt);
 107813     pDb->pBt = 0;
 107814     pDb->pSchema = 0;
 107817   /* This both clears the schemas and reduces the size of the db->aDb[]
 107818   ** array. */ 
 107819   sqlite3ResetAllSchemasOfConnection(db);
 107821   return rc;
 107824 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
 107826 /************** End of vacuum.c **********************************************/
 107827 /************** Begin file vtab.c ********************************************/
 107829 ** 2006 June 10
 107831 ** The author disclaims copyright to this source code.  In place of
 107832 ** a legal notice, here is a blessing:
 107834 **    May you do good and not evil.
 107835 **    May you find forgiveness for yourself and forgive others.
 107836 **    May you share freely, never taking more than you give.
 107838 *************************************************************************
 107839 ** This file contains code used to help implement virtual tables.
 107841 #ifndef SQLITE_OMIT_VIRTUALTABLE
 107844 ** Before a virtual table xCreate() or xConnect() method is invoked, the
 107845 ** sqlite3.pVtabCtx member variable is set to point to an instance of
 107846 ** this struct allocated on the stack. It is used by the implementation of 
 107847 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
 107848 ** are invoked only from within xCreate and xConnect methods.
 107850 struct VtabCtx {
 107851   VTable *pVTable;    /* The virtual table being constructed */
 107852   Table *pTab;        /* The Table object to which the virtual table belongs */
 107856 ** The actual function that does the work of creating a new module.
 107857 ** This function implements the sqlite3_create_module() and
 107858 ** sqlite3_create_module_v2() interfaces.
 107860 static int createModule(
 107861   sqlite3 *db,                    /* Database in which module is registered */
 107862   const char *zName,              /* Name assigned to this module */
 107863   const sqlite3_module *pModule,  /* The definition of the module */
 107864   void *pAux,                     /* Context pointer for xCreate/xConnect */
 107865   void (*xDestroy)(void *)        /* Module destructor function */
 107867   int rc = SQLITE_OK;
 107868   int nName;
 107870   sqlite3_mutex_enter(db->mutex);
 107871   nName = sqlite3Strlen30(zName);
 107872   if( sqlite3HashFind(&db->aModule, zName, nName) ){
 107873     rc = SQLITE_MISUSE_BKPT;
 107874   }else{
 107875     Module *pMod;
 107876     pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
 107877     if( pMod ){
 107878       Module *pDel;
 107879       char *zCopy = (char *)(&pMod[1]);
 107880       memcpy(zCopy, zName, nName+1);
 107881       pMod->zName = zCopy;
 107882       pMod->pModule = pModule;
 107883       pMod->pAux = pAux;
 107884       pMod->xDestroy = xDestroy;
 107885       pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
 107886       assert( pDel==0 || pDel==pMod );
 107887       if( pDel ){
 107888         db->mallocFailed = 1;
 107889         sqlite3DbFree(db, pDel);
 107893   rc = sqlite3ApiExit(db, rc);
 107894   if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
 107896   sqlite3_mutex_leave(db->mutex);
 107897   return rc;
 107902 ** External API function used to create a new virtual-table module.
 107904 SQLITE_API int sqlite3_create_module(
 107905   sqlite3 *db,                    /* Database in which module is registered */
 107906   const char *zName,              /* Name assigned to this module */
 107907   const sqlite3_module *pModule,  /* The definition of the module */
 107908   void *pAux                      /* Context pointer for xCreate/xConnect */
 107910   return createModule(db, zName, pModule, pAux, 0);
 107914 ** External API function used to create a new virtual-table module.
 107916 SQLITE_API int sqlite3_create_module_v2(
 107917   sqlite3 *db,                    /* Database in which module is registered */
 107918   const char *zName,              /* Name assigned to this module */
 107919   const sqlite3_module *pModule,  /* The definition of the module */
 107920   void *pAux,                     /* Context pointer for xCreate/xConnect */
 107921   void (*xDestroy)(void *)        /* Module destructor function */
 107923   return createModule(db, zName, pModule, pAux, xDestroy);
 107927 ** Lock the virtual table so that it cannot be disconnected.
 107928 ** Locks nest.  Every lock should have a corresponding unlock.
 107929 ** If an unlock is omitted, resources leaks will occur.  
 107931 ** If a disconnect is attempted while a virtual table is locked,
 107932 ** the disconnect is deferred until all locks have been removed.
 107934 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
 107935   pVTab->nRef++;
 107940 ** pTab is a pointer to a Table structure representing a virtual-table.
 107941 ** Return a pointer to the VTable object used by connection db to access 
 107942 ** this virtual-table, if one has been created, or NULL otherwise.
 107944 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
 107945   VTable *pVtab;
 107946   assert( IsVirtual(pTab) );
 107947   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
 107948   return pVtab;
 107952 ** Decrement the ref-count on a virtual table object. When the ref-count
 107953 ** reaches zero, call the xDisconnect() method to delete the object.
 107955 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
 107956   sqlite3 *db = pVTab->db;
 107958   assert( db );
 107959   assert( pVTab->nRef>0 );
 107960   assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
 107962   pVTab->nRef--;
 107963   if( pVTab->nRef==0 ){
 107964     sqlite3_vtab *p = pVTab->pVtab;
 107965     if( p ){
 107966       p->pModule->xDisconnect(p);
 107968     sqlite3DbFree(db, pVTab);
 107973 ** Table p is a virtual table. This function moves all elements in the
 107974 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
 107975 ** database connections to be disconnected at the next opportunity. 
 107976 ** Except, if argument db is not NULL, then the entry associated with
 107977 ** connection db is left in the p->pVTable list.
 107979 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
 107980   VTable *pRet = 0;
 107981   VTable *pVTable = p->pVTable;
 107982   p->pVTable = 0;
 107984   /* Assert that the mutex (if any) associated with the BtShared database 
 107985   ** that contains table p is held by the caller. See header comments 
 107986   ** above function sqlite3VtabUnlockList() for an explanation of why
 107987   ** this makes it safe to access the sqlite3.pDisconnect list of any
 107988   ** database connection that may have an entry in the p->pVTable list.
 107990   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 107992   while( pVTable ){
 107993     sqlite3 *db2 = pVTable->db;
 107994     VTable *pNext = pVTable->pNext;
 107995     assert( db2 );
 107996     if( db2==db ){
 107997       pRet = pVTable;
 107998       p->pVTable = pRet;
 107999       pRet->pNext = 0;
 108000     }else{
 108001       pVTable->pNext = db2->pDisconnect;
 108002       db2->pDisconnect = pVTable;
 108004     pVTable = pNext;
 108007   assert( !db || pRet );
 108008   return pRet;
 108012 ** Table *p is a virtual table. This function removes the VTable object
 108013 ** for table *p associated with database connection db from the linked
 108014 ** list in p->pVTab. It also decrements the VTable ref count. This is
 108015 ** used when closing database connection db to free all of its VTable
 108016 ** objects without disturbing the rest of the Schema object (which may
 108017 ** be being used by other shared-cache connections).
 108019 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
 108020   VTable **ppVTab;
 108022   assert( IsVirtual(p) );
 108023   assert( sqlite3BtreeHoldsAllMutexes(db) );
 108024   assert( sqlite3_mutex_held(db->mutex) );
 108026   for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
 108027     if( (*ppVTab)->db==db  ){
 108028       VTable *pVTab = *ppVTab;
 108029       *ppVTab = pVTab->pNext;
 108030       sqlite3VtabUnlock(pVTab);
 108031       break;
 108038 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
 108040 ** This function may only be called when the mutexes associated with all
 108041 ** shared b-tree databases opened using connection db are held by the 
 108042 ** caller. This is done to protect the sqlite3.pDisconnect list. The
 108043 ** sqlite3.pDisconnect list is accessed only as follows:
 108045 **   1) By this function. In this case, all BtShared mutexes and the mutex
 108046 **      associated with the database handle itself must be held.
 108048 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
 108049 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
 108050 **      associated with the database the virtual table is stored in is held
 108051 **      or, if the virtual table is stored in a non-sharable database, then
 108052 **      the database handle mutex is held.
 108054 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
 108055 ** by multiple threads. It is thread-safe.
 108057 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
 108058   VTable *p = db->pDisconnect;
 108059   db->pDisconnect = 0;
 108061   assert( sqlite3BtreeHoldsAllMutexes(db) );
 108062   assert( sqlite3_mutex_held(db->mutex) );
 108064   if( p ){
 108065     sqlite3ExpirePreparedStatements(db);
 108066     do {
 108067       VTable *pNext = p->pNext;
 108068       sqlite3VtabUnlock(p);
 108069       p = pNext;
 108070     }while( p );
 108075 ** Clear any and all virtual-table information from the Table record.
 108076 ** This routine is called, for example, just before deleting the Table
 108077 ** record.
 108079 ** Since it is a virtual-table, the Table structure contains a pointer
 108080 ** to the head of a linked list of VTable structures. Each VTable 
 108081 ** structure is associated with a single sqlite3* user of the schema.
 108082 ** The reference count of the VTable structure associated with database 
 108083 ** connection db is decremented immediately (which may lead to the 
 108084 ** structure being xDisconnected and free). Any other VTable structures
 108085 ** in the list are moved to the sqlite3.pDisconnect list of the associated 
 108086 ** database connection.
 108088 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
 108089   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
 108090   if( p->azModuleArg ){
 108091     int i;
 108092     for(i=0; i<p->nModuleArg; i++){
 108093       if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
 108095     sqlite3DbFree(db, p->azModuleArg);
 108100 ** Add a new module argument to pTable->azModuleArg[].
 108101 ** The string is not copied - the pointer is stored.  The
 108102 ** string will be freed automatically when the table is
 108103 ** deleted.
 108105 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
 108106   int i = pTable->nModuleArg++;
 108107   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
 108108   char **azModuleArg;
 108109   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
 108110   if( azModuleArg==0 ){
 108111     int j;
 108112     for(j=0; j<i; j++){
 108113       sqlite3DbFree(db, pTable->azModuleArg[j]);
 108115     sqlite3DbFree(db, zArg);
 108116     sqlite3DbFree(db, pTable->azModuleArg);
 108117     pTable->nModuleArg = 0;
 108118   }else{
 108119     azModuleArg[i] = zArg;
 108120     azModuleArg[i+1] = 0;
 108122   pTable->azModuleArg = azModuleArg;
 108126 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
 108127 ** statement.  The module name has been parsed, but the optional list
 108128 ** of parameters that follow the module name are still pending.
 108130 SQLITE_PRIVATE void sqlite3VtabBeginParse(
 108131   Parse *pParse,        /* Parsing context */
 108132   Token *pName1,        /* Name of new table, or database name */
 108133   Token *pName2,        /* Name of new table or NULL */
 108134   Token *pModuleName,   /* Name of the module for the virtual table */
 108135   int ifNotExists       /* No error if the table already exists */
 108137   int iDb;              /* The database the table is being created in */
 108138   Table *pTable;        /* The new virtual table */
 108139   sqlite3 *db;          /* Database connection */
 108141   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
 108142   pTable = pParse->pNewTable;
 108143   if( pTable==0 ) return;
 108144   assert( 0==pTable->pIndex );
 108146   db = pParse->db;
 108147   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
 108148   assert( iDb>=0 );
 108150   pTable->tabFlags |= TF_Virtual;
 108151   pTable->nModuleArg = 0;
 108152   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
 108153   addModuleArgument(db, pTable, 0);
 108154   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
 108155   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
 108157 #ifndef SQLITE_OMIT_AUTHORIZATION
 108158   /* Creating a virtual table invokes the authorization callback twice.
 108159   ** The first invocation, to obtain permission to INSERT a row into the
 108160   ** sqlite_master table, has already been made by sqlite3StartTable().
 108161   ** The second call, to obtain permission to create the table, is made now.
 108163   if( pTable->azModuleArg ){
 108164     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
 108165             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
 108167 #endif
 108171 ** This routine takes the module argument that has been accumulating
 108172 ** in pParse->zArg[] and appends it to the list of arguments on the
 108173 ** virtual table currently under construction in pParse->pTable.
 108175 static void addArgumentToVtab(Parse *pParse){
 108176   if( pParse->sArg.z && pParse->pNewTable ){
 108177     const char *z = (const char*)pParse->sArg.z;
 108178     int n = pParse->sArg.n;
 108179     sqlite3 *db = pParse->db;
 108180     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
 108185 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
 108186 ** has been completely parsed.
 108188 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
 108189   Table *pTab = pParse->pNewTable;  /* The table being constructed */
 108190   sqlite3 *db = pParse->db;         /* The database connection */
 108192   if( pTab==0 ) return;
 108193   addArgumentToVtab(pParse);
 108194   pParse->sArg.z = 0;
 108195   if( pTab->nModuleArg<1 ) return;
 108197   /* If the CREATE VIRTUAL TABLE statement is being entered for the
 108198   ** first time (in other words if the virtual table is actually being
 108199   ** created now instead of just being read out of sqlite_master) then
 108200   ** do additional initialization work and store the statement text
 108201   ** in the sqlite_master table.
 108203   if( !db->init.busy ){
 108204     char *zStmt;
 108205     char *zWhere;
 108206     int iDb;
 108207     Vdbe *v;
 108209     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
 108210     if( pEnd ){
 108211       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
 108213     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
 108215     /* A slot for the record has already been allocated in the 
 108216     ** SQLITE_MASTER table.  We just need to update that slot with all
 108217     ** the information we've collected.  
 108219     ** The VM register number pParse->regRowid holds the rowid of an
 108220     ** entry in the sqlite_master table tht was created for this vtab
 108221     ** by sqlite3StartTable().
 108223     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 108224     sqlite3NestedParse(pParse,
 108225       "UPDATE %Q.%s "
 108226          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
 108227        "WHERE rowid=#%d",
 108228       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
 108229       pTab->zName,
 108230       pTab->zName,
 108231       zStmt,
 108232       pParse->regRowid
 108234     sqlite3DbFree(db, zStmt);
 108235     v = sqlite3GetVdbe(pParse);
 108236     sqlite3ChangeCookie(pParse, iDb);
 108238     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
 108239     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
 108240     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
 108241     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
 108242                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
 108245   /* If we are rereading the sqlite_master table create the in-memory
 108246   ** record of the table. The xConnect() method is not called until
 108247   ** the first time the virtual table is used in an SQL statement. This
 108248   ** allows a schema that contains virtual tables to be loaded before
 108249   ** the required virtual table implementations are registered.  */
 108250   else {
 108251     Table *pOld;
 108252     Schema *pSchema = pTab->pSchema;
 108253     const char *zName = pTab->zName;
 108254     int nName = sqlite3Strlen30(zName);
 108255     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
 108256     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
 108257     if( pOld ){
 108258       db->mallocFailed = 1;
 108259       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
 108260       return;
 108262     pParse->pNewTable = 0;
 108267 ** The parser calls this routine when it sees the first token
 108268 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
 108270 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
 108271   addArgumentToVtab(pParse);
 108272   pParse->sArg.z = 0;
 108273   pParse->sArg.n = 0;
 108277 ** The parser calls this routine for each token after the first token
 108278 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
 108280 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
 108281   Token *pArg = &pParse->sArg;
 108282   if( pArg->z==0 ){
 108283     pArg->z = p->z;
 108284     pArg->n = p->n;
 108285   }else{
 108286     assert(pArg->z < p->z);
 108287     pArg->n = (int)(&p->z[p->n] - pArg->z);
 108292 ** Invoke a virtual table constructor (either xCreate or xConnect). The
 108293 ** pointer to the function to invoke is passed as the fourth parameter
 108294 ** to this procedure.
 108296 static int vtabCallConstructor(
 108297   sqlite3 *db, 
 108298   Table *pTab,
 108299   Module *pMod,
 108300   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
 108301   char **pzErr
 108303   VtabCtx sCtx, *pPriorCtx;
 108304   VTable *pVTable;
 108305   int rc;
 108306   const char *const*azArg = (const char *const*)pTab->azModuleArg;
 108307   int nArg = pTab->nModuleArg;
 108308   char *zErr = 0;
 108309   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
 108310   int iDb;
 108312   if( !zModuleName ){
 108313     return SQLITE_NOMEM;
 108316   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
 108317   if( !pVTable ){
 108318     sqlite3DbFree(db, zModuleName);
 108319     return SQLITE_NOMEM;
 108321   pVTable->db = db;
 108322   pVTable->pMod = pMod;
 108324   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 108325   pTab->azModuleArg[1] = db->aDb[iDb].zName;
 108327   /* Invoke the virtual table constructor */
 108328   assert( &db->pVtabCtx );
 108329   assert( xConstruct );
 108330   sCtx.pTab = pTab;
 108331   sCtx.pVTable = pVTable;
 108332   pPriorCtx = db->pVtabCtx;
 108333   db->pVtabCtx = &sCtx;
 108334   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
 108335   db->pVtabCtx = pPriorCtx;
 108336   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 108338   if( SQLITE_OK!=rc ){
 108339     if( zErr==0 ){
 108340       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
 108341     }else {
 108342       *pzErr = sqlite3MPrintf(db, "%s", zErr);
 108343       sqlite3_free(zErr);
 108345     sqlite3DbFree(db, pVTable);
 108346   }else if( ALWAYS(pVTable->pVtab) ){
 108347     /* Justification of ALWAYS():  A correct vtab constructor must allocate
 108348     ** the sqlite3_vtab object if successful.  */
 108349     pVTable->pVtab->pModule = pMod->pModule;
 108350     pVTable->nRef = 1;
 108351     if( sCtx.pTab ){
 108352       const char *zFormat = "vtable constructor did not declare schema: %s";
 108353       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
 108354       sqlite3VtabUnlock(pVTable);
 108355       rc = SQLITE_ERROR;
 108356     }else{
 108357       int iCol;
 108358       /* If everything went according to plan, link the new VTable structure
 108359       ** into the linked list headed by pTab->pVTable. Then loop through the 
 108360       ** columns of the table to see if any of them contain the token "hidden".
 108361       ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
 108362       ** the type string.  */
 108363       pVTable->pNext = pTab->pVTable;
 108364       pTab->pVTable = pVTable;
 108366       for(iCol=0; iCol<pTab->nCol; iCol++){
 108367         char *zType = pTab->aCol[iCol].zType;
 108368         int nType;
 108369         int i = 0;
 108370         if( !zType ) continue;
 108371         nType = sqlite3Strlen30(zType);
 108372         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
 108373           for(i=0; i<nType; i++){
 108374             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
 108375              && (zType[i+7]=='\0' || zType[i+7]==' ')
 108377               i++;
 108378               break;
 108382         if( i<nType ){
 108383           int j;
 108384           int nDel = 6 + (zType[i+6] ? 1 : 0);
 108385           for(j=i; (j+nDel)<=nType; j++){
 108386             zType[j] = zType[j+nDel];
 108388           if( zType[i]=='\0' && i>0 ){
 108389             assert(zType[i-1]==' ');
 108390             zType[i-1] = '\0';
 108392           pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
 108398   sqlite3DbFree(db, zModuleName);
 108399   return rc;
 108403 ** This function is invoked by the parser to call the xConnect() method
 108404 ** of the virtual table pTab. If an error occurs, an error code is returned 
 108405 ** and an error left in pParse.
 108407 ** This call is a no-op if table pTab is not a virtual table.
 108409 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
 108410   sqlite3 *db = pParse->db;
 108411   const char *zMod;
 108412   Module *pMod;
 108413   int rc;
 108415   assert( pTab );
 108416   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
 108417     return SQLITE_OK;
 108420   /* Locate the required virtual table module */
 108421   zMod = pTab->azModuleArg[0];
 108422   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
 108424   if( !pMod ){
 108425     const char *zModule = pTab->azModuleArg[0];
 108426     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
 108427     rc = SQLITE_ERROR;
 108428   }else{
 108429     char *zErr = 0;
 108430     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
 108431     if( rc!=SQLITE_OK ){
 108432       sqlite3ErrorMsg(pParse, "%s", zErr);
 108434     sqlite3DbFree(db, zErr);
 108437   return rc;
 108440 ** Grow the db->aVTrans[] array so that there is room for at least one
 108441 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
 108443 static int growVTrans(sqlite3 *db){
 108444   const int ARRAY_INCR = 5;
 108446   /* Grow the sqlite3.aVTrans array if required */
 108447   if( (db->nVTrans%ARRAY_INCR)==0 ){
 108448     VTable **aVTrans;
 108449     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
 108450     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
 108451     if( !aVTrans ){
 108452       return SQLITE_NOMEM;
 108454     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
 108455     db->aVTrans = aVTrans;
 108458   return SQLITE_OK;
 108462 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
 108463 ** have already been reserved using growVTrans().
 108465 static void addToVTrans(sqlite3 *db, VTable *pVTab){
 108466   /* Add pVtab to the end of sqlite3.aVTrans */
 108467   db->aVTrans[db->nVTrans++] = pVTab;
 108468   sqlite3VtabLock(pVTab);
 108472 ** This function is invoked by the vdbe to call the xCreate method
 108473 ** of the virtual table named zTab in database iDb. 
 108475 ** If an error occurs, *pzErr is set to point an an English language
 108476 ** description of the error and an SQLITE_XXX error code is returned.
 108477 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
 108479 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
 108480   int rc = SQLITE_OK;
 108481   Table *pTab;
 108482   Module *pMod;
 108483   const char *zMod;
 108485   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
 108486   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
 108488   /* Locate the required virtual table module */
 108489   zMod = pTab->azModuleArg[0];
 108490   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
 108492   /* If the module has been registered and includes a Create method, 
 108493   ** invoke it now. If the module has not been registered, return an 
 108494   ** error. Otherwise, do nothing.
 108496   if( !pMod ){
 108497     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
 108498     rc = SQLITE_ERROR;
 108499   }else{
 108500     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
 108503   /* Justification of ALWAYS():  The xConstructor method is required to
 108504   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
 108505   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
 108506     rc = growVTrans(db);
 108507     if( rc==SQLITE_OK ){
 108508       addToVTrans(db, sqlite3GetVTable(db, pTab));
 108512   return rc;
 108516 ** This function is used to set the schema of a virtual table.  It is only
 108517 ** valid to call this function from within the xCreate() or xConnect() of a
 108518 ** virtual table module.
 108520 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
 108521   Parse *pParse;
 108523   int rc = SQLITE_OK;
 108524   Table *pTab;
 108525   char *zErr = 0;
 108527   sqlite3_mutex_enter(db->mutex);
 108528   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
 108529     sqlite3Error(db, SQLITE_MISUSE, 0);
 108530     sqlite3_mutex_leave(db->mutex);
 108531     return SQLITE_MISUSE_BKPT;
 108533   assert( (pTab->tabFlags & TF_Virtual)!=0 );
 108535   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
 108536   if( pParse==0 ){
 108537     rc = SQLITE_NOMEM;
 108538   }else{
 108539     pParse->declareVtab = 1;
 108540     pParse->db = db;
 108541     pParse->nQueryLoop = 1;
 108543     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
 108544      && pParse->pNewTable
 108545      && !db->mallocFailed
 108546      && !pParse->pNewTable->pSelect
 108547      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
 108549       if( !pTab->aCol ){
 108550         pTab->aCol = pParse->pNewTable->aCol;
 108551         pTab->nCol = pParse->pNewTable->nCol;
 108552         pParse->pNewTable->nCol = 0;
 108553         pParse->pNewTable->aCol = 0;
 108555       db->pVtabCtx->pTab = 0;
 108556     }else{
 108557       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
 108558       sqlite3DbFree(db, zErr);
 108559       rc = SQLITE_ERROR;
 108561     pParse->declareVtab = 0;
 108563     if( pParse->pVdbe ){
 108564       sqlite3VdbeFinalize(pParse->pVdbe);
 108566     sqlite3DeleteTable(db, pParse->pNewTable);
 108567     sqlite3ParserReset(pParse);
 108568     sqlite3StackFree(db, pParse);
 108571   assert( (rc&0xff)==rc );
 108572   rc = sqlite3ApiExit(db, rc);
 108573   sqlite3_mutex_leave(db->mutex);
 108574   return rc;
 108578 ** This function is invoked by the vdbe to call the xDestroy method
 108579 ** of the virtual table named zTab in database iDb. This occurs
 108580 ** when a DROP TABLE is mentioned.
 108582 ** This call is a no-op if zTab is not a virtual table.
 108584 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
 108585   int rc = SQLITE_OK;
 108586   Table *pTab;
 108588   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
 108589   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
 108590     VTable *p = vtabDisconnectAll(db, pTab);
 108592     assert( rc==SQLITE_OK );
 108593     rc = p->pMod->pModule->xDestroy(p->pVtab);
 108595     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
 108596     if( rc==SQLITE_OK ){
 108597       assert( pTab->pVTable==p && p->pNext==0 );
 108598       p->pVtab = 0;
 108599       pTab->pVTable = 0;
 108600       sqlite3VtabUnlock(p);
 108604   return rc;
 108608 ** This function invokes either the xRollback or xCommit method
 108609 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
 108610 ** called is identified by the second argument, "offset", which is
 108611 ** the offset of the method to call in the sqlite3_module structure.
 108613 ** The array is cleared after invoking the callbacks. 
 108615 static void callFinaliser(sqlite3 *db, int offset){
 108616   int i;
 108617   if( db->aVTrans ){
 108618     for(i=0; i<db->nVTrans; i++){
 108619       VTable *pVTab = db->aVTrans[i];
 108620       sqlite3_vtab *p = pVTab->pVtab;
 108621       if( p ){
 108622         int (*x)(sqlite3_vtab *);
 108623         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
 108624         if( x ) x(p);
 108626       pVTab->iSavepoint = 0;
 108627       sqlite3VtabUnlock(pVTab);
 108629     sqlite3DbFree(db, db->aVTrans);
 108630     db->nVTrans = 0;
 108631     db->aVTrans = 0;
 108636 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
 108637 ** array. Return the error code for the first error that occurs, or
 108638 ** SQLITE_OK if all xSync operations are successful.
 108640 ** If an error message is available, leave it in p->zErrMsg.
 108642 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
 108643   int i;
 108644   int rc = SQLITE_OK;
 108645   VTable **aVTrans = db->aVTrans;
 108647   db->aVTrans = 0;
 108648   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
 108649     int (*x)(sqlite3_vtab *);
 108650     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
 108651     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
 108652       rc = x(pVtab);
 108653       sqlite3VtabImportErrmsg(p, pVtab);
 108656   db->aVTrans = aVTrans;
 108657   return rc;
 108661 ** Invoke the xRollback method of all virtual tables in the 
 108662 ** sqlite3.aVTrans array. Then clear the array itself.
 108664 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
 108665   callFinaliser(db, offsetof(sqlite3_module,xRollback));
 108666   return SQLITE_OK;
 108670 ** Invoke the xCommit method of all virtual tables in the 
 108671 ** sqlite3.aVTrans array. Then clear the array itself.
 108673 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
 108674   callFinaliser(db, offsetof(sqlite3_module,xCommit));
 108675   return SQLITE_OK;
 108679 ** If the virtual table pVtab supports the transaction interface
 108680 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
 108681 ** not currently open, invoke the xBegin method now.
 108683 ** If the xBegin call is successful, place the sqlite3_vtab pointer
 108684 ** in the sqlite3.aVTrans array.
 108686 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
 108687   int rc = SQLITE_OK;
 108688   const sqlite3_module *pModule;
 108690   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
 108691   ** than zero, then this function is being called from within a
 108692   ** virtual module xSync() callback. It is illegal to write to 
 108693   ** virtual module tables in this case, so return SQLITE_LOCKED.
 108695   if( sqlite3VtabInSync(db) ){
 108696     return SQLITE_LOCKED;
 108698   if( !pVTab ){
 108699     return SQLITE_OK;
 108701   pModule = pVTab->pVtab->pModule;
 108703   if( pModule->xBegin ){
 108704     int i;
 108706     /* If pVtab is already in the aVTrans array, return early */
 108707     for(i=0; i<db->nVTrans; i++){
 108708       if( db->aVTrans[i]==pVTab ){
 108709         return SQLITE_OK;
 108713     /* Invoke the xBegin method. If successful, add the vtab to the 
 108714     ** sqlite3.aVTrans[] array. */
 108715     rc = growVTrans(db);
 108716     if( rc==SQLITE_OK ){
 108717       rc = pModule->xBegin(pVTab->pVtab);
 108718       if( rc==SQLITE_OK ){
 108719         addToVTrans(db, pVTab);
 108723   return rc;
 108727 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
 108728 ** virtual tables that currently have an open transaction. Pass iSavepoint
 108729 ** as the second argument to the virtual table method invoked.
 108731 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
 108732 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is 
 108733 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
 108734 ** an open transaction is invoked.
 108736 ** If any virtual table method returns an error code other than SQLITE_OK, 
 108737 ** processing is abandoned and the error returned to the caller of this
 108738 ** function immediately. If all calls to virtual table methods are successful,
 108739 ** SQLITE_OK is returned.
 108741 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
 108742   int rc = SQLITE_OK;
 108744   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
 108745   assert( iSavepoint>=0 );
 108746   if( db->aVTrans ){
 108747     int i;
 108748     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
 108749       VTable *pVTab = db->aVTrans[i];
 108750       const sqlite3_module *pMod = pVTab->pMod->pModule;
 108751       if( pVTab->pVtab && pMod->iVersion>=2 ){
 108752         int (*xMethod)(sqlite3_vtab *, int);
 108753         switch( op ){
 108754           case SAVEPOINT_BEGIN:
 108755             xMethod = pMod->xSavepoint;
 108756             pVTab->iSavepoint = iSavepoint+1;
 108757             break;
 108758           case SAVEPOINT_ROLLBACK:
 108759             xMethod = pMod->xRollbackTo;
 108760             break;
 108761           default:
 108762             xMethod = pMod->xRelease;
 108763             break;
 108765         if( xMethod && pVTab->iSavepoint>iSavepoint ){
 108766           rc = xMethod(pVTab->pVtab, iSavepoint);
 108771   return rc;
 108775 ** The first parameter (pDef) is a function implementation.  The
 108776 ** second parameter (pExpr) is the first argument to this function.
 108777 ** If pExpr is a column in a virtual table, then let the virtual
 108778 ** table implementation have an opportunity to overload the function.
 108780 ** This routine is used to allow virtual table implementations to
 108781 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
 108783 ** Return either the pDef argument (indicating no change) or a 
 108784 ** new FuncDef structure that is marked as ephemeral using the
 108785 ** SQLITE_FUNC_EPHEM flag.
 108787 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
 108788   sqlite3 *db,    /* Database connection for reporting malloc problems */
 108789   FuncDef *pDef,  /* Function to possibly overload */
 108790   int nArg,       /* Number of arguments to the function */
 108791   Expr *pExpr     /* First argument to the function */
 108793   Table *pTab;
 108794   sqlite3_vtab *pVtab;
 108795   sqlite3_module *pMod;
 108796   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
 108797   void *pArg = 0;
 108798   FuncDef *pNew;
 108799   int rc = 0;
 108800   char *zLowerName;
 108801   unsigned char *z;
 108804   /* Check to see the left operand is a column in a virtual table */
 108805   if( NEVER(pExpr==0) ) return pDef;
 108806   if( pExpr->op!=TK_COLUMN ) return pDef;
 108807   pTab = pExpr->pTab;
 108808   if( NEVER(pTab==0) ) return pDef;
 108809   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
 108810   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
 108811   assert( pVtab!=0 );
 108812   assert( pVtab->pModule!=0 );
 108813   pMod = (sqlite3_module *)pVtab->pModule;
 108814   if( pMod->xFindFunction==0 ) return pDef;
 108816   /* Call the xFindFunction method on the virtual table implementation
 108817   ** to see if the implementation wants to overload this function 
 108819   zLowerName = sqlite3DbStrDup(db, pDef->zName);
 108820   if( zLowerName ){
 108821     for(z=(unsigned char*)zLowerName; *z; z++){
 108822       *z = sqlite3UpperToLower[*z];
 108824     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
 108825     sqlite3DbFree(db, zLowerName);
 108827   if( rc==0 ){
 108828     return pDef;
 108831   /* Create a new ephemeral function definition for the overloaded
 108832   ** function */
 108833   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
 108834                              + sqlite3Strlen30(pDef->zName) + 1);
 108835   if( pNew==0 ){
 108836     return pDef;
 108838   *pNew = *pDef;
 108839   pNew->zName = (char *)&pNew[1];
 108840   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
 108841   pNew->xFunc = xFunc;
 108842   pNew->pUserData = pArg;
 108843   pNew->funcFlags |= SQLITE_FUNC_EPHEM;
 108844   return pNew;
 108848 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
 108849 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
 108850 ** array if it is missing.  If pTab is already in the array, this routine
 108851 ** is a no-op.
 108853 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
 108854   Parse *pToplevel = sqlite3ParseToplevel(pParse);
 108855   int i, n;
 108856   Table **apVtabLock;
 108858   assert( IsVirtual(pTab) );
 108859   for(i=0; i<pToplevel->nVtabLock; i++){
 108860     if( pTab==pToplevel->apVtabLock[i] ) return;
 108862   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
 108863   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
 108864   if( apVtabLock ){
 108865     pToplevel->apVtabLock = apVtabLock;
 108866     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
 108867   }else{
 108868     pToplevel->db->mallocFailed = 1;
 108873 ** Return the ON CONFLICT resolution mode in effect for the virtual
 108874 ** table update operation currently in progress.
 108876 ** The results of this routine are undefined unless it is called from
 108877 ** within an xUpdate method.
 108879 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
 108880   static const unsigned char aMap[] = { 
 108881     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 
 108883   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
 108884   assert( OE_Ignore==4 && OE_Replace==5 );
 108885   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
 108886   return (int)aMap[db->vtabOnConflict-1];
 108890 ** Call from within the xCreate() or xConnect() methods to provide 
 108891 ** the SQLite core with additional information about the behavior
 108892 ** of the virtual table being implemented.
 108894 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
 108895   va_list ap;
 108896   int rc = SQLITE_OK;
 108898   sqlite3_mutex_enter(db->mutex);
 108900   va_start(ap, op);
 108901   switch( op ){
 108902     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
 108903       VtabCtx *p = db->pVtabCtx;
 108904       if( !p ){
 108905         rc = SQLITE_MISUSE_BKPT;
 108906       }else{
 108907         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
 108908         p->pVTable->bConstraint = (u8)va_arg(ap, int);
 108910       break;
 108912     default:
 108913       rc = SQLITE_MISUSE_BKPT;
 108914       break;
 108916   va_end(ap);
 108918   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
 108919   sqlite3_mutex_leave(db->mutex);
 108920   return rc;
 108923 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 108925 /************** End of vtab.c ************************************************/
 108926 /************** Begin file where.c *******************************************/
 108928 ** 2001 September 15
 108930 ** The author disclaims copyright to this source code.  In place of
 108931 ** a legal notice, here is a blessing:
 108933 **    May you do good and not evil.
 108934 **    May you find forgiveness for yourself and forgive others.
 108935 **    May you share freely, never taking more than you give.
 108937 *************************************************************************
 108938 ** This module contains C code that generates VDBE code used to process
 108939 ** the WHERE clause of SQL statements.  This module is responsible for
 108940 ** generating the code that loops through a table looking for applicable
 108941 ** rows.  Indices are selected and used to speed the search when doing
 108942 ** so is applicable.  Because this module is responsible for selecting
 108943 ** indices, you might also think of this module as the "query optimizer".
 108945 /************** Include whereInt.h in the middle of where.c ******************/
 108946 /************** Begin file whereInt.h ****************************************/
 108948 ** 2013-11-12
 108950 ** The author disclaims copyright to this source code.  In place of
 108951 ** a legal notice, here is a blessing:
 108953 **    May you do good and not evil.
 108954 **    May you find forgiveness for yourself and forgive others.
 108955 **    May you share freely, never taking more than you give.
 108957 *************************************************************************
 108959 ** This file contains structure and macro definitions for the query
 108960 ** planner logic in "where.c".  These definitions are broken out into
 108961 ** a separate source file for easier editing.
 108965 ** Trace output macros
 108967 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 108968 /***/ int sqlite3WhereTrace = 0;
 108969 #endif
 108970 #if defined(SQLITE_DEBUG) \
 108971     && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
 108972 # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
 108973 # define WHERETRACE_ENABLED 1
 108974 #else
 108975 # define WHERETRACE(K,X)
 108976 #endif
 108978 /* Forward references
 108980 typedef struct WhereClause WhereClause;
 108981 typedef struct WhereMaskSet WhereMaskSet;
 108982 typedef struct WhereOrInfo WhereOrInfo;
 108983 typedef struct WhereAndInfo WhereAndInfo;
 108984 typedef struct WhereLevel WhereLevel;
 108985 typedef struct WhereLoop WhereLoop;
 108986 typedef struct WherePath WherePath;
 108987 typedef struct WhereTerm WhereTerm;
 108988 typedef struct WhereLoopBuilder WhereLoopBuilder;
 108989 typedef struct WhereScan WhereScan;
 108990 typedef struct WhereOrCost WhereOrCost;
 108991 typedef struct WhereOrSet WhereOrSet;
 108994 ** This object contains information needed to implement a single nested
 108995 ** loop in WHERE clause.
 108997 ** Contrast this object with WhereLoop.  This object describes the
 108998 ** implementation of the loop.  WhereLoop describes the algorithm.
 108999 ** This object contains a pointer to the WhereLoop algorithm as one of
 109000 ** its elements.
 109002 ** The WhereInfo object contains a single instance of this object for
 109003 ** each term in the FROM clause (which is to say, for each of the
 109004 ** nested loops as implemented).  The order of WhereLevel objects determines
 109005 ** the loop nested order, with WhereInfo.a[0] being the outer loop and
 109006 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
 109008 struct WhereLevel {
 109009   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
 109010   int iTabCur;          /* The VDBE cursor used to access the table */
 109011   int iIdxCur;          /* The VDBE cursor used to access pIdx */
 109012   int addrBrk;          /* Jump here to break out of the loop */
 109013   int addrNxt;          /* Jump here to start the next IN combination */
 109014   int addrSkip;         /* Jump here for next iteration of skip-scan */
 109015   int addrCont;         /* Jump here to continue with the next loop cycle */
 109016   int addrFirst;        /* First instruction of interior of the loop */
 109017   int addrBody;         /* Beginning of the body of this loop */
 109018   u8 iFrom;             /* Which entry in the FROM clause */
 109019   u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
 109020   int p1, p2;           /* Operands of the opcode used to ends the loop */
 109021   union {               /* Information that depends on pWLoop->wsFlags */
 109022     struct {
 109023       int nIn;              /* Number of entries in aInLoop[] */
 109024       struct InLoop {
 109025         int iCur;              /* The VDBE cursor used by this IN operator */
 109026         int addrInTop;         /* Top of the IN loop */
 109027         u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
 109028       } *aInLoop;           /* Information about each nested IN operator */
 109029     } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
 109030     Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
 109031   } u;
 109032   struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
 109033   Bitmask notReady;          /* FROM entries not usable at this level */
 109037 ** Each instance of this object represents an algorithm for evaluating one
 109038 ** term of a join.  Every term of the FROM clause will have at least
 109039 ** one corresponding WhereLoop object (unless INDEXED BY constraints
 109040 ** prevent a query solution - which is an error) and many terms of the
 109041 ** FROM clause will have multiple WhereLoop objects, each describing a
 109042 ** potential way of implementing that FROM-clause term, together with
 109043 ** dependencies and cost estimates for using the chosen algorithm.
 109045 ** Query planning consists of building up a collection of these WhereLoop
 109046 ** objects, then computing a particular sequence of WhereLoop objects, with
 109047 ** one WhereLoop object per FROM clause term, that satisfy all dependencies
 109048 ** and that minimize the overall cost.
 109050 struct WhereLoop {
 109051   Bitmask prereq;       /* Bitmask of other loops that must run first */
 109052   Bitmask maskSelf;     /* Bitmask identifying table iTab */
 109053 #ifdef SQLITE_DEBUG
 109054   char cId;             /* Symbolic ID of this loop for debugging use */
 109055 #endif
 109056   u8 iTab;              /* Position in FROM clause of table for this loop */
 109057   u8 iSortIdx;          /* Sorting index number.  0==None */
 109058   LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
 109059   LogEst rRun;          /* Cost of running each loop */
 109060   LogEst nOut;          /* Estimated number of output rows */
 109061   union {
 109062     struct {               /* Information for internal btree tables */
 109063       u16 nEq;               /* Number of equality constraints */
 109064       u16 nSkip;             /* Number of initial index columns to skip */
 109065       Index *pIndex;         /* Index used, or NULL */
 109066     } btree;
 109067     struct {               /* Information for virtual tables */
 109068       int idxNum;            /* Index number */
 109069       u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
 109070       u8 isOrdered;          /* True if satisfies ORDER BY */
 109071       u16 omitMask;          /* Terms that may be omitted */
 109072       char *idxStr;          /* Index identifier string */
 109073     } vtab;
 109074   } u;
 109075   u32 wsFlags;          /* WHERE_* flags describing the plan */
 109076   u16 nLTerm;           /* Number of entries in aLTerm[] */
 109077   /**** whereLoopXfer() copies fields above ***********************/
 109078 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
 109079   u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
 109080   WhereTerm **aLTerm;   /* WhereTerms used */
 109081   WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
 109082   WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
 109085 /* This object holds the prerequisites and the cost of running a
 109086 ** subquery on one operand of an OR operator in the WHERE clause.
 109087 ** See WhereOrSet for additional information 
 109089 struct WhereOrCost {
 109090   Bitmask prereq;     /* Prerequisites */
 109091   LogEst rRun;        /* Cost of running this subquery */
 109092   LogEst nOut;        /* Number of outputs for this subquery */
 109095 /* The WhereOrSet object holds a set of possible WhereOrCosts that
 109096 ** correspond to the subquery(s) of OR-clause processing.  Only the
 109097 ** best N_OR_COST elements are retained.
 109099 #define N_OR_COST 3
 109100 struct WhereOrSet {
 109101   u16 n;                      /* Number of valid a[] entries */
 109102   WhereOrCost a[N_OR_COST];   /* Set of best costs */
 109106 /* Forward declaration of methods */
 109107 static int whereLoopResize(sqlite3*, WhereLoop*, int);
 109110 ** Each instance of this object holds a sequence of WhereLoop objects
 109111 ** that implement some or all of a query plan.
 109113 ** Think of each WhereLoop object as a node in a graph with arcs
 109114 ** showing dependencies and costs for travelling between nodes.  (That is
 109115 ** not a completely accurate description because WhereLoop costs are a
 109116 ** vector, not a scalar, and because dependencies are many-to-one, not
 109117 ** one-to-one as are graph nodes.  But it is a useful visualization aid.)
 109118 ** Then a WherePath object is a path through the graph that visits some
 109119 ** or all of the WhereLoop objects once.
 109121 ** The "solver" works by creating the N best WherePath objects of length
 109122 ** 1.  Then using those as a basis to compute the N best WherePath objects
 109123 ** of length 2.  And so forth until the length of WherePaths equals the
 109124 ** number of nodes in the FROM clause.  The best (lowest cost) WherePath
 109125 ** at the end is the choosen query plan.
 109127 struct WherePath {
 109128   Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
 109129   Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
 109130   LogEst nRow;          /* Estimated number of rows generated by this path */
 109131   LogEst rCost;         /* Total cost of this path */
 109132   u8 isOrdered;         /* True if this path satisfies ORDER BY */
 109133   u8 isOrderedValid;    /* True if the isOrdered field is valid */
 109134   WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
 109138 ** The query generator uses an array of instances of this structure to
 109139 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
 109140 ** clause subexpression is separated from the others by AND operators,
 109141 ** usually, or sometimes subexpressions separated by OR.
 109143 ** All WhereTerms are collected into a single WhereClause structure.  
 109144 ** The following identity holds:
 109146 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
 109148 ** When a term is of the form:
 109150 **              X <op> <expr>
 109152 ** where X is a column name and <op> is one of certain operators,
 109153 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
 109154 ** cursor number and column number for X.  WhereTerm.eOperator records
 109155 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
 109156 ** use of a bitmask encoding for the operator allows us to search
 109157 ** quickly for terms that match any of several different operators.
 109159 ** A WhereTerm might also be two or more subterms connected by OR:
 109161 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
 109163 ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
 109164 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
 109165 ** is collected about the OR clause.
 109167 ** If a term in the WHERE clause does not match either of the two previous
 109168 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
 109169 ** to the original subexpression content and wtFlags is set up appropriately
 109170 ** but no other fields in the WhereTerm object are meaningful.
 109172 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
 109173 ** but they do so indirectly.  A single WhereMaskSet structure translates
 109174 ** cursor number into bits and the translated bit is stored in the prereq
 109175 ** fields.  The translation is used in order to maximize the number of
 109176 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
 109177 ** spread out over the non-negative integers.  For example, the cursor
 109178 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
 109179 ** translates these sparse cursor numbers into consecutive integers
 109180 ** beginning with 0 in order to make the best possible use of the available
 109181 ** bits in the Bitmask.  So, in the example above, the cursor numbers
 109182 ** would be mapped into integers 0 through 7.
 109184 ** The number of terms in a join is limited by the number of bits
 109185 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
 109186 ** is only able to process joins with 64 or fewer tables.
 109188 struct WhereTerm {
 109189   Expr *pExpr;            /* Pointer to the subexpression that is this term */
 109190   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
 109191   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
 109192   union {
 109193     int leftColumn;         /* Column number of X in "X <op> <expr>" */
 109194     WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
 109195     WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
 109196   } u;
 109197   LogEst truthProb;       /* Probability of truth for this expression */
 109198   u16 eOperator;          /* A WO_xx value describing <op> */
 109199   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
 109200   u8 nChild;              /* Number of children that must disable us */
 109201   WhereClause *pWC;       /* The clause this term is part of */
 109202   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
 109203   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
 109207 ** Allowed values of WhereTerm.wtFlags
 109209 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
 109210 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
 109211 #define TERM_CODED      0x04   /* This term is already coded */
 109212 #define TERM_COPIED     0x08   /* Has a child */
 109213 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
 109214 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
 109215 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
 109216 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 109217 #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
 109218 #else
 109219 #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
 109220 #endif
 109223 ** An instance of the WhereScan object is used as an iterator for locating
 109224 ** terms in the WHERE clause that are useful to the query planner.
 109226 struct WhereScan {
 109227   WhereClause *pOrigWC;      /* Original, innermost WhereClause */
 109228   WhereClause *pWC;          /* WhereClause currently being scanned */
 109229   char *zCollName;           /* Required collating sequence, if not NULL */
 109230   char idxaff;               /* Must match this affinity, if zCollName!=NULL */
 109231   unsigned char nEquiv;      /* Number of entries in aEquiv[] */
 109232   unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
 109233   u32 opMask;                /* Acceptable operators */
 109234   int k;                     /* Resume scanning at this->pWC->a[this->k] */
 109235   int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
 109239 ** An instance of the following structure holds all information about a
 109240 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
 109242 ** Explanation of pOuter:  For a WHERE clause of the form
 109244 **           a AND ((b AND c) OR (d AND e)) AND f
 109246 ** There are separate WhereClause objects for the whole clause and for
 109247 ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
 109248 ** subclauses points to the WhereClause object for the whole clause.
 109250 struct WhereClause {
 109251   WhereInfo *pWInfo;       /* WHERE clause processing context */
 109252   WhereClause *pOuter;     /* Outer conjunction */
 109253   u8 op;                   /* Split operator.  TK_AND or TK_OR */
 109254   int nTerm;               /* Number of terms */
 109255   int nSlot;               /* Number of entries in a[] */
 109256   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
 109257 #if defined(SQLITE_SMALL_STACK)
 109258   WhereTerm aStatic[1];    /* Initial static space for a[] */
 109259 #else
 109260   WhereTerm aStatic[8];    /* Initial static space for a[] */
 109261 #endif
 109265 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
 109266 ** a dynamically allocated instance of the following structure.
 109268 struct WhereOrInfo {
 109269   WhereClause wc;          /* Decomposition into subterms */
 109270   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
 109274 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
 109275 ** a dynamically allocated instance of the following structure.
 109277 struct WhereAndInfo {
 109278   WhereClause wc;          /* The subexpression broken out */
 109282 ** An instance of the following structure keeps track of a mapping
 109283 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
 109285 ** The VDBE cursor numbers are small integers contained in 
 109286 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
 109287 ** clause, the cursor numbers might not begin with 0 and they might
 109288 ** contain gaps in the numbering sequence.  But we want to make maximum
 109289 ** use of the bits in our bitmasks.  This structure provides a mapping
 109290 ** from the sparse cursor numbers into consecutive integers beginning
 109291 ** with 0.
 109293 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
 109294 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
 109296 ** For example, if the WHERE clause expression used these VDBE
 109297 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
 109298 ** would map those cursor numbers into bits 0 through 5.
 109300 ** Note that the mapping is not necessarily ordered.  In the example
 109301 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
 109302 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
 109303 ** does not really matter.  What is important is that sparse cursor
 109304 ** numbers all get mapped into bit numbers that begin with 0 and contain
 109305 ** no gaps.
 109307 struct WhereMaskSet {
 109308   int n;                        /* Number of assigned cursor values */
 109309   int ix[BMS];                  /* Cursor assigned to each bit */
 109313 ** This object is a convenience wrapper holding all information needed
 109314 ** to construct WhereLoop objects for a particular query.
 109316 struct WhereLoopBuilder {
 109317   WhereInfo *pWInfo;        /* Information about this WHERE */
 109318   WhereClause *pWC;         /* WHERE clause terms */
 109319   ExprList *pOrderBy;       /* ORDER BY clause */
 109320   WhereLoop *pNew;          /* Template WhereLoop */
 109321   WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
 109322 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 109323   UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
 109324   int nRecValid;            /* Number of valid fields currently in pRec */
 109325 #endif
 109329 ** The WHERE clause processing routine has two halves.  The
 109330 ** first part does the start of the WHERE loop and the second
 109331 ** half does the tail of the WHERE loop.  An instance of
 109332 ** this structure is returned by the first half and passed
 109333 ** into the second half to give some continuity.
 109335 ** An instance of this object holds the complete state of the query
 109336 ** planner.
 109338 struct WhereInfo {
 109339   Parse *pParse;            /* Parsing and code generating context */
 109340   SrcList *pTabList;        /* List of tables in the join */
 109341   ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
 109342   ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
 109343   WhereLoop *pLoops;        /* List of all WhereLoop objects */
 109344   Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
 109345   LogEst nRowOut;           /* Estimated number of output rows */
 109346   u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
 109347   u8 bOBSat;                /* ORDER BY satisfied by indices */
 109348   u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
 109349   u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
 109350   u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
 109351   u8 nLevel;                /* Number of nested loop */
 109352   int iTop;                 /* The very beginning of the WHERE loop */
 109353   int iContinue;            /* Jump here to continue with next record */
 109354   int iBreak;               /* Jump here to break out of the loop */
 109355   int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
 109356   int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
 109357   WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
 109358   WhereClause sWC;          /* Decomposition of the WHERE clause */
 109359   WhereLevel a[1];          /* Information about each nest loop in WHERE */
 109363 ** Bitmasks for the operators on WhereTerm objects.  These are all
 109364 ** operators that are of interest to the query planner.  An
 109365 ** OR-ed combination of these values can be used when searching for
 109366 ** particular WhereTerms within a WhereClause.
 109368 #define WO_IN     0x001
 109369 #define WO_EQ     0x002
 109370 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
 109371 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
 109372 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
 109373 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
 109374 #define WO_MATCH  0x040
 109375 #define WO_ISNULL 0x080
 109376 #define WO_OR     0x100       /* Two or more OR-connected terms */
 109377 #define WO_AND    0x200       /* Two or more AND-connected terms */
 109378 #define WO_EQUIV  0x400       /* Of the form A==B, both columns */
 109379 #define WO_NOOP   0x800       /* This term does not restrict search space */
 109381 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
 109382 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
 109385 ** These are definitions of bits in the WhereLoop.wsFlags field.
 109386 ** The particular combination of bits in each WhereLoop help to
 109387 ** determine the algorithm that WhereLoop represents.
 109389 #define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR */
 109390 #define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
 109391 #define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
 109392 #define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
 109393 #define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
 109394 #define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
 109395 #define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
 109396 #define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
 109397 #define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
 109398 #define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
 109399 #define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
 109400 #define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
 109401 #define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
 109402 #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
 109403 #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
 109404 #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
 109405 #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
 109406 #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
 109408 /************** End of whereInt.h ********************************************/
 109409 /************** Continuing where we left off in where.c **********************/
 109412 ** Return the estimated number of output rows from a WHERE clause
 109414 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
 109415   return sqlite3LogEstToInt(pWInfo->nRowOut);
 109419 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
 109420 ** WHERE clause returns outputs for DISTINCT processing.
 109422 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
 109423   return pWInfo->eDistinct;
 109427 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
 109428 ** Return FALSE if the output needs to be sorted.
 109430 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
 109431   return pWInfo->bOBSat!=0;
 109435 ** Return the VDBE address or label to jump to in order to continue
 109436 ** immediately with the next row of a WHERE clause.
 109438 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
 109439   return pWInfo->iContinue;
 109443 ** Return the VDBE address or label to jump to in order to break
 109444 ** out of a WHERE loop.
 109446 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
 109447   return pWInfo->iBreak;
 109451 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
 109452 ** the rowids returned by a WHERE clause.  Return FALSE if doing an
 109453 ** UPDATE or DELETE might change subsequent WHERE clause results.
 109455 ** If the ONEPASS optimization is used (if this routine returns true)
 109456 ** then also write the indices of open cursors used by ONEPASS
 109457 ** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
 109458 ** table and iaCur[1] gets the cursor used by an auxiliary index.
 109459 ** Either value may be -1, indicating that cursor is not used.
 109460 ** Any cursors returned will have been opened for writing.
 109462 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
 109463 ** unable to use the ONEPASS optimization.
 109465 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
 109466   memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
 109467   return pWInfo->okOnePass;
 109471 ** Move the content of pSrc into pDest
 109473 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
 109474   pDest->n = pSrc->n;
 109475   memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
 109479 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
 109481 ** The new entry might overwrite an existing entry, or it might be
 109482 ** appended, or it might be discarded.  Do whatever is the right thing
 109483 ** so that pSet keeps the N_OR_COST best entries seen so far.
 109485 static int whereOrInsert(
 109486   WhereOrSet *pSet,      /* The WhereOrSet to be updated */
 109487   Bitmask prereq,        /* Prerequisites of the new entry */
 109488   LogEst rRun,           /* Run-cost of the new entry */
 109489   LogEst nOut            /* Number of outputs for the new entry */
 109491   u16 i;
 109492   WhereOrCost *p;
 109493   for(i=pSet->n, p=pSet->a; i>0; i--, p++){
 109494     if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
 109495       goto whereOrInsert_done;
 109497     if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
 109498       return 0;
 109501   if( pSet->n<N_OR_COST ){
 109502     p = &pSet->a[pSet->n++];
 109503     p->nOut = nOut;
 109504   }else{
 109505     p = pSet->a;
 109506     for(i=1; i<pSet->n; i++){
 109507       if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
 109509     if( p->rRun<=rRun ) return 0;
 109511 whereOrInsert_done:
 109512   p->prereq = prereq;
 109513   p->rRun = rRun;
 109514   if( p->nOut>nOut ) p->nOut = nOut;
 109515   return 1;
 109519 ** Initialize a preallocated WhereClause structure.
 109521 static void whereClauseInit(
 109522   WhereClause *pWC,        /* The WhereClause to be initialized */
 109523   WhereInfo *pWInfo        /* The WHERE processing context */
 109525   pWC->pWInfo = pWInfo;
 109526   pWC->pOuter = 0;
 109527   pWC->nTerm = 0;
 109528   pWC->nSlot = ArraySize(pWC->aStatic);
 109529   pWC->a = pWC->aStatic;
 109532 /* Forward reference */
 109533 static void whereClauseClear(WhereClause*);
 109536 ** Deallocate all memory associated with a WhereOrInfo object.
 109538 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
 109539   whereClauseClear(&p->wc);
 109540   sqlite3DbFree(db, p);
 109544 ** Deallocate all memory associated with a WhereAndInfo object.
 109546 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
 109547   whereClauseClear(&p->wc);
 109548   sqlite3DbFree(db, p);
 109552 ** Deallocate a WhereClause structure.  The WhereClause structure
 109553 ** itself is not freed.  This routine is the inverse of whereClauseInit().
 109555 static void whereClauseClear(WhereClause *pWC){
 109556   int i;
 109557   WhereTerm *a;
 109558   sqlite3 *db = pWC->pWInfo->pParse->db;
 109559   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
 109560     if( a->wtFlags & TERM_DYNAMIC ){
 109561       sqlite3ExprDelete(db, a->pExpr);
 109563     if( a->wtFlags & TERM_ORINFO ){
 109564       whereOrInfoDelete(db, a->u.pOrInfo);
 109565     }else if( a->wtFlags & TERM_ANDINFO ){
 109566       whereAndInfoDelete(db, a->u.pAndInfo);
 109569   if( pWC->a!=pWC->aStatic ){
 109570     sqlite3DbFree(db, pWC->a);
 109575 ** Add a single new WhereTerm entry to the WhereClause object pWC.
 109576 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
 109577 ** The index in pWC->a[] of the new WhereTerm is returned on success.
 109578 ** 0 is returned if the new WhereTerm could not be added due to a memory
 109579 ** allocation error.  The memory allocation failure will be recorded in
 109580 ** the db->mallocFailed flag so that higher-level functions can detect it.
 109582 ** This routine will increase the size of the pWC->a[] array as necessary.
 109584 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
 109585 ** for freeing the expression p is assumed by the WhereClause object pWC.
 109586 ** This is true even if this routine fails to allocate a new WhereTerm.
 109588 ** WARNING:  This routine might reallocate the space used to store
 109589 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
 109590 ** calling this routine.  Such pointers may be reinitialized by referencing
 109591 ** the pWC->a[] array.
 109593 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
 109594   WhereTerm *pTerm;
 109595   int idx;
 109596   testcase( wtFlags & TERM_VIRTUAL );
 109597   if( pWC->nTerm>=pWC->nSlot ){
 109598     WhereTerm *pOld = pWC->a;
 109599     sqlite3 *db = pWC->pWInfo->pParse->db;
 109600     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
 109601     if( pWC->a==0 ){
 109602       if( wtFlags & TERM_DYNAMIC ){
 109603         sqlite3ExprDelete(db, p);
 109605       pWC->a = pOld;
 109606       return 0;
 109608     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
 109609     if( pOld!=pWC->aStatic ){
 109610       sqlite3DbFree(db, pOld);
 109612     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
 109614   pTerm = &pWC->a[idx = pWC->nTerm++];
 109615   if( p && ExprHasProperty(p, EP_Unlikely) ){
 109616     pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
 109617   }else{
 109618     pTerm->truthProb = -1;
 109620   pTerm->pExpr = sqlite3ExprSkipCollate(p);
 109621   pTerm->wtFlags = wtFlags;
 109622   pTerm->pWC = pWC;
 109623   pTerm->iParent = -1;
 109624   return idx;
 109628 ** This routine identifies subexpressions in the WHERE clause where
 109629 ** each subexpression is separated by the AND operator or some other
 109630 ** operator specified in the op parameter.  The WhereClause structure
 109631 ** is filled with pointers to subexpressions.  For example:
 109633 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
 109634 **           \________/     \_______________/     \________________/
 109635 **            slot[0]            slot[1]               slot[2]
 109637 ** The original WHERE clause in pExpr is unaltered.  All this routine
 109638 ** does is make slot[] entries point to substructure within pExpr.
 109640 ** In the previous sentence and in the diagram, "slot[]" refers to
 109641 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
 109642 ** all terms of the WHERE clause.
 109644 static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
 109645   pWC->op = op;
 109646   if( pExpr==0 ) return;
 109647   if( pExpr->op!=op ){
 109648     whereClauseInsert(pWC, pExpr, 0);
 109649   }else{
 109650     whereSplit(pWC, pExpr->pLeft, op);
 109651     whereSplit(pWC, pExpr->pRight, op);
 109656 ** Initialize a WhereMaskSet object
 109658 #define initMaskSet(P)  (P)->n=0
 109661 ** Return the bitmask for the given cursor number.  Return 0 if
 109662 ** iCursor is not in the set.
 109664 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
 109665   int i;
 109666   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
 109667   for(i=0; i<pMaskSet->n; i++){
 109668     if( pMaskSet->ix[i]==iCursor ){
 109669       return MASKBIT(i);
 109672   return 0;
 109676 ** Create a new mask for cursor iCursor.
 109678 ** There is one cursor per table in the FROM clause.  The number of
 109679 ** tables in the FROM clause is limited by a test early in the
 109680 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
 109681 ** array will never overflow.
 109683 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
 109684   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
 109685   pMaskSet->ix[pMaskSet->n++] = iCursor;
 109689 ** These routines walk (recursively) an expression tree and generate
 109690 ** a bitmask indicating which tables are used in that expression
 109691 ** tree.
 109693 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
 109694 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
 109695 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
 109696   Bitmask mask = 0;
 109697   if( p==0 ) return 0;
 109698   if( p->op==TK_COLUMN ){
 109699     mask = getMask(pMaskSet, p->iTable);
 109700     return mask;
 109702   mask = exprTableUsage(pMaskSet, p->pRight);
 109703   mask |= exprTableUsage(pMaskSet, p->pLeft);
 109704   if( ExprHasProperty(p, EP_xIsSelect) ){
 109705     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
 109706   }else{
 109707     mask |= exprListTableUsage(pMaskSet, p->x.pList);
 109709   return mask;
 109711 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
 109712   int i;
 109713   Bitmask mask = 0;
 109714   if( pList ){
 109715     for(i=0; i<pList->nExpr; i++){
 109716       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
 109719   return mask;
 109721 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
 109722   Bitmask mask = 0;
 109723   while( pS ){
 109724     SrcList *pSrc = pS->pSrc;
 109725     mask |= exprListTableUsage(pMaskSet, pS->pEList);
 109726     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
 109727     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
 109728     mask |= exprTableUsage(pMaskSet, pS->pWhere);
 109729     mask |= exprTableUsage(pMaskSet, pS->pHaving);
 109730     if( ALWAYS(pSrc!=0) ){
 109731       int i;
 109732       for(i=0; i<pSrc->nSrc; i++){
 109733         mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
 109734         mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
 109737     pS = pS->pPrior;
 109739   return mask;
 109743 ** Return TRUE if the given operator is one of the operators that is
 109744 ** allowed for an indexable WHERE clause term.  The allowed operators are
 109745 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
 109747 static int allowedOp(int op){
 109748   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
 109749   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
 109750   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
 109751   assert( TK_GE==TK_EQ+4 );
 109752   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
 109756 ** Swap two objects of type TYPE.
 109758 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
 109761 ** Commute a comparison operator.  Expressions of the form "X op Y"
 109762 ** are converted into "Y op X".
 109764 ** If left/right precedence rules come into play when determining the
 109765 ** collating sequence, then COLLATE operators are adjusted to ensure
 109766 ** that the collating sequence does not change.  For example:
 109767 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
 109768 ** the left hand side of a comparison overrides any collation sequence 
 109769 ** attached to the right. For the same reason the EP_Collate flag
 109770 ** is not commuted.
 109772 static void exprCommute(Parse *pParse, Expr *pExpr){
 109773   u16 expRight = (pExpr->pRight->flags & EP_Collate);
 109774   u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
 109775   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
 109776   if( expRight==expLeft ){
 109777     /* Either X and Y both have COLLATE operator or neither do */
 109778     if( expRight ){
 109779       /* Both X and Y have COLLATE operators.  Make sure X is always
 109780       ** used by clearing the EP_Collate flag from Y. */
 109781       pExpr->pRight->flags &= ~EP_Collate;
 109782     }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
 109783       /* Neither X nor Y have COLLATE operators, but X has a non-default
 109784       ** collating sequence.  So add the EP_Collate marker on X to cause
 109785       ** it to be searched first. */
 109786       pExpr->pLeft->flags |= EP_Collate;
 109789   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
 109790   if( pExpr->op>=TK_GT ){
 109791     assert( TK_LT==TK_GT+2 );
 109792     assert( TK_GE==TK_LE+2 );
 109793     assert( TK_GT>TK_EQ );
 109794     assert( TK_GT<TK_LE );
 109795     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
 109796     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
 109801 ** Translate from TK_xx operator to WO_xx bitmask.
 109803 static u16 operatorMask(int op){
 109804   u16 c;
 109805   assert( allowedOp(op) );
 109806   if( op==TK_IN ){
 109807     c = WO_IN;
 109808   }else if( op==TK_ISNULL ){
 109809     c = WO_ISNULL;
 109810   }else{
 109811     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
 109812     c = (u16)(WO_EQ<<(op-TK_EQ));
 109814   assert( op!=TK_ISNULL || c==WO_ISNULL );
 109815   assert( op!=TK_IN || c==WO_IN );
 109816   assert( op!=TK_EQ || c==WO_EQ );
 109817   assert( op!=TK_LT || c==WO_LT );
 109818   assert( op!=TK_LE || c==WO_LE );
 109819   assert( op!=TK_GT || c==WO_GT );
 109820   assert( op!=TK_GE || c==WO_GE );
 109821   return c;
 109825 ** Advance to the next WhereTerm that matches according to the criteria
 109826 ** established when the pScan object was initialized by whereScanInit().
 109827 ** Return NULL if there are no more matching WhereTerms.
 109829 static WhereTerm *whereScanNext(WhereScan *pScan){
 109830   int iCur;            /* The cursor on the LHS of the term */
 109831   int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
 109832   Expr *pX;            /* An expression being tested */
 109833   WhereClause *pWC;    /* Shorthand for pScan->pWC */
 109834   WhereTerm *pTerm;    /* The term being tested */
 109835   int k = pScan->k;    /* Where to start scanning */
 109837   while( pScan->iEquiv<=pScan->nEquiv ){
 109838     iCur = pScan->aEquiv[pScan->iEquiv-2];
 109839     iColumn = pScan->aEquiv[pScan->iEquiv-1];
 109840     while( (pWC = pScan->pWC)!=0 ){
 109841       for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
 109842         if( pTerm->leftCursor==iCur
 109843          && pTerm->u.leftColumn==iColumn
 109844          && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
 109846           if( (pTerm->eOperator & WO_EQUIV)!=0
 109847            && pScan->nEquiv<ArraySize(pScan->aEquiv)
 109849             int j;
 109850             pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
 109851             assert( pX->op==TK_COLUMN );
 109852             for(j=0; j<pScan->nEquiv; j+=2){
 109853               if( pScan->aEquiv[j]==pX->iTable
 109854                && pScan->aEquiv[j+1]==pX->iColumn ){
 109855                   break;
 109858             if( j==pScan->nEquiv ){
 109859               pScan->aEquiv[j] = pX->iTable;
 109860               pScan->aEquiv[j+1] = pX->iColumn;
 109861               pScan->nEquiv += 2;
 109864           if( (pTerm->eOperator & pScan->opMask)!=0 ){
 109865             /* Verify the affinity and collating sequence match */
 109866             if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
 109867               CollSeq *pColl;
 109868               Parse *pParse = pWC->pWInfo->pParse;
 109869               pX = pTerm->pExpr;
 109870               if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
 109871                 continue;
 109873               assert(pX->pLeft);
 109874               pColl = sqlite3BinaryCompareCollSeq(pParse,
 109875                                                   pX->pLeft, pX->pRight);
 109876               if( pColl==0 ) pColl = pParse->db->pDfltColl;
 109877               if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
 109878                 continue;
 109881             if( (pTerm->eOperator & WO_EQ)!=0
 109882              && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
 109883              && pX->iTable==pScan->aEquiv[0]
 109884              && pX->iColumn==pScan->aEquiv[1]
 109886               continue;
 109888             pScan->k = k+1;
 109889             return pTerm;
 109893       pScan->pWC = pScan->pWC->pOuter;
 109894       k = 0;
 109896     pScan->pWC = pScan->pOrigWC;
 109897     k = 0;
 109898     pScan->iEquiv += 2;
 109900   return 0;
 109904 ** Initialize a WHERE clause scanner object.  Return a pointer to the
 109905 ** first match.  Return NULL if there are no matches.
 109907 ** The scanner will be searching the WHERE clause pWC.  It will look
 109908 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
 109909 ** iCur.  The <op> must be one of the operators described by opMask.
 109911 ** If the search is for X and the WHERE clause contains terms of the
 109912 ** form X=Y then this routine might also return terms of the form
 109913 ** "Y <op> <expr>".  The number of levels of transitivity is limited,
 109914 ** but is enough to handle most commonly occurring SQL statements.
 109916 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
 109917 ** index pIdx.
 109919 static WhereTerm *whereScanInit(
 109920   WhereScan *pScan,       /* The WhereScan object being initialized */
 109921   WhereClause *pWC,       /* The WHERE clause to be scanned */
 109922   int iCur,               /* Cursor to scan for */
 109923   int iColumn,            /* Column to scan for */
 109924   u32 opMask,             /* Operator(s) to scan for */
 109925   Index *pIdx             /* Must be compatible with this index */
 109927   int j;
 109929   /* memset(pScan, 0, sizeof(*pScan)); */
 109930   pScan->pOrigWC = pWC;
 109931   pScan->pWC = pWC;
 109932   if( pIdx && iColumn>=0 ){
 109933     pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
 109934     for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
 109935       if( NEVER(j>=pIdx->nKeyCol) ) return 0;
 109937     pScan->zCollName = pIdx->azColl[j];
 109938   }else{
 109939     pScan->idxaff = 0;
 109940     pScan->zCollName = 0;
 109942   pScan->opMask = opMask;
 109943   pScan->k = 0;
 109944   pScan->aEquiv[0] = iCur;
 109945   pScan->aEquiv[1] = iColumn;
 109946   pScan->nEquiv = 2;
 109947   pScan->iEquiv = 2;
 109948   return whereScanNext(pScan);
 109952 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
 109953 ** where X is a reference to the iColumn of table iCur and <op> is one of
 109954 ** the WO_xx operator codes specified by the op parameter.
 109955 ** Return a pointer to the term.  Return 0 if not found.
 109957 ** The term returned might by Y=<expr> if there is another constraint in
 109958 ** the WHERE clause that specifies that X=Y.  Any such constraints will be
 109959 ** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
 109960 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
 109961 ** taking up two slots in aEquiv[].  The first slot is for the cursor number
 109962 ** and the second is for the column number.  There are 22 slots in aEquiv[]
 109963 ** so that means we can look for X plus up to 10 other equivalent values.
 109964 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
 109965 ** and ... and A9=A10 and A10=<expr>.
 109967 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
 109968 ** then try for the one with no dependencies on <expr> - in other words where
 109969 ** <expr> is a constant expression of some kind.  Only return entries of
 109970 ** the form "X <op> Y" where Y is a column in another table if no terms of
 109971 ** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
 109972 ** exist, try to return a term that does not use WO_EQUIV.
 109974 static WhereTerm *findTerm(
 109975   WhereClause *pWC,     /* The WHERE clause to be searched */
 109976   int iCur,             /* Cursor number of LHS */
 109977   int iColumn,          /* Column number of LHS */
 109978   Bitmask notReady,     /* RHS must not overlap with this mask */
 109979   u32 op,               /* Mask of WO_xx values describing operator */
 109980   Index *pIdx           /* Must be compatible with this index, if not NULL */
 109982   WhereTerm *pResult = 0;
 109983   WhereTerm *p;
 109984   WhereScan scan;
 109986   p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
 109987   while( p ){
 109988     if( (p->prereqRight & notReady)==0 ){
 109989       if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
 109990         return p;
 109992       if( pResult==0 ) pResult = p;
 109994     p = whereScanNext(&scan);
 109996   return pResult;
 109999 /* Forward reference */
 110000 static void exprAnalyze(SrcList*, WhereClause*, int);
 110003 ** Call exprAnalyze on all terms in a WHERE clause.  
 110005 static void exprAnalyzeAll(
 110006   SrcList *pTabList,       /* the FROM clause */
 110007   WhereClause *pWC         /* the WHERE clause to be analyzed */
 110009   int i;
 110010   for(i=pWC->nTerm-1; i>=0; i--){
 110011     exprAnalyze(pTabList, pWC, i);
 110015 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
 110017 ** Check to see if the given expression is a LIKE or GLOB operator that
 110018 ** can be optimized using inequality constraints.  Return TRUE if it is
 110019 ** so and false if not.
 110021 ** In order for the operator to be optimizible, the RHS must be a string
 110022 ** literal that does not begin with a wildcard.  
 110024 static int isLikeOrGlob(
 110025   Parse *pParse,    /* Parsing and code generating context */
 110026   Expr *pExpr,      /* Test this expression */
 110027   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
 110028   int *pisComplete, /* True if the only wildcard is % in the last character */
 110029   int *pnoCase      /* True if uppercase is equivalent to lowercase */
 110031   const char *z = 0;         /* String on RHS of LIKE operator */
 110032   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
 110033   ExprList *pList;           /* List of operands to the LIKE operator */
 110034   int c;                     /* One character in z[] */
 110035   int cnt;                   /* Number of non-wildcard prefix characters */
 110036   char wc[3];                /* Wildcard characters */
 110037   sqlite3 *db = pParse->db;  /* Database connection */
 110038   sqlite3_value *pVal = 0;
 110039   int op;                    /* Opcode of pRight */
 110041   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
 110042     return 0;
 110044 #ifdef SQLITE_EBCDIC
 110045   if( *pnoCase ) return 0;
 110046 #endif
 110047   pList = pExpr->x.pList;
 110048   pLeft = pList->a[1].pExpr;
 110049   if( pLeft->op!=TK_COLUMN 
 110050    || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
 110051    || IsVirtual(pLeft->pTab)
 110053     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
 110054     ** be the name of an indexed column with TEXT affinity. */
 110055     return 0;
 110057   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
 110059   pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
 110060   op = pRight->op;
 110061   if( op==TK_VARIABLE ){
 110062     Vdbe *pReprepare = pParse->pReprepare;
 110063     int iCol = pRight->iColumn;
 110064     pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
 110065     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
 110066       z = (char *)sqlite3_value_text(pVal);
 110068     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
 110069     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
 110070   }else if( op==TK_STRING ){
 110071     z = pRight->u.zToken;
 110073   if( z ){
 110074     cnt = 0;
 110075     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
 110076       cnt++;
 110078     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
 110079       Expr *pPrefix;
 110080       *pisComplete = c==wc[0] && z[cnt+1]==0;
 110081       pPrefix = sqlite3Expr(db, TK_STRING, z);
 110082       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
 110083       *ppPrefix = pPrefix;
 110084       if( op==TK_VARIABLE ){
 110085         Vdbe *v = pParse->pVdbe;
 110086         sqlite3VdbeSetVarmask(v, pRight->iColumn);
 110087         if( *pisComplete && pRight->u.zToken[1] ){
 110088           /* If the rhs of the LIKE expression is a variable, and the current
 110089           ** value of the variable means there is no need to invoke the LIKE
 110090           ** function, then no OP_Variable will be added to the program.
 110091           ** This causes problems for the sqlite3_bind_parameter_name()
 110092           ** API. To workaround them, add a dummy OP_Variable here.
 110094           int r1 = sqlite3GetTempReg(pParse);
 110095           sqlite3ExprCodeTarget(pParse, pRight, r1);
 110096           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
 110097           sqlite3ReleaseTempReg(pParse, r1);
 110100     }else{
 110101       z = 0;
 110105   sqlite3ValueFree(pVal);
 110106   return (z!=0);
 110108 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
 110111 #ifndef SQLITE_OMIT_VIRTUALTABLE
 110113 ** Check to see if the given expression is of the form
 110115 **         column MATCH expr
 110117 ** If it is then return TRUE.  If not, return FALSE.
 110119 static int isMatchOfColumn(
 110120   Expr *pExpr      /* Test this expression */
 110122   ExprList *pList;
 110124   if( pExpr->op!=TK_FUNCTION ){
 110125     return 0;
 110127   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
 110128     return 0;
 110130   pList = pExpr->x.pList;
 110131   if( pList->nExpr!=2 ){
 110132     return 0;
 110134   if( pList->a[1].pExpr->op != TK_COLUMN ){
 110135     return 0;
 110137   return 1;
 110139 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 110142 ** If the pBase expression originated in the ON or USING clause of
 110143 ** a join, then transfer the appropriate markings over to derived.
 110145 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
 110146   if( pDerived ){
 110147     pDerived->flags |= pBase->flags & EP_FromJoin;
 110148     pDerived->iRightJoinTable = pBase->iRightJoinTable;
 110152 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
 110154 ** Analyze a term that consists of two or more OR-connected
 110155 ** subterms.  So in:
 110157 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
 110158 **                          ^^^^^^^^^^^^^^^^^^^^
 110160 ** This routine analyzes terms such as the middle term in the above example.
 110161 ** A WhereOrTerm object is computed and attached to the term under
 110162 ** analysis, regardless of the outcome of the analysis.  Hence:
 110164 **     WhereTerm.wtFlags   |=  TERM_ORINFO
 110165 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
 110167 ** The term being analyzed must have two or more of OR-connected subterms.
 110168 ** A single subterm might be a set of AND-connected sub-subterms.
 110169 ** Examples of terms under analysis:
 110171 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
 110172 **     (B)     x=expr1 OR expr2=x OR x=expr3
 110173 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
 110174 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
 110175 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
 110177 ** CASE 1:
 110179 ** If all subterms are of the form T.C=expr for some single column of C and
 110180 ** a single table T (as shown in example B above) then create a new virtual
 110181 ** term that is an equivalent IN expression.  In other words, if the term
 110182 ** being analyzed is:
 110184 **      x = expr1  OR  expr2 = x  OR  x = expr3
 110186 ** then create a new virtual term like this:
 110188 **      x IN (expr1,expr2,expr3)
 110190 ** CASE 2:
 110192 ** If all subterms are indexable by a single table T, then set
 110194 **     WhereTerm.eOperator              =  WO_OR
 110195 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
 110197 ** A subterm is "indexable" if it is of the form
 110198 ** "T.C <op> <expr>" where C is any column of table T and 
 110199 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
 110200 ** A subterm is also indexable if it is an AND of two or more
 110201 ** subsubterms at least one of which is indexable.  Indexable AND 
 110202 ** subterms have their eOperator set to WO_AND and they have
 110203 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
 110205 ** From another point of view, "indexable" means that the subterm could
 110206 ** potentially be used with an index if an appropriate index exists.
 110207 ** This analysis does not consider whether or not the index exists; that
 110208 ** is decided elsewhere.  This analysis only looks at whether subterms
 110209 ** appropriate for indexing exist.
 110211 ** All examples A through E above satisfy case 2.  But if a term
 110212 ** also statisfies case 1 (such as B) we know that the optimizer will
 110213 ** always prefer case 1, so in that case we pretend that case 2 is not
 110214 ** satisfied.
 110216 ** It might be the case that multiple tables are indexable.  For example,
 110217 ** (E) above is indexable on tables P, Q, and R.
 110219 ** Terms that satisfy case 2 are candidates for lookup by using
 110220 ** separate indices to find rowids for each subterm and composing
 110221 ** the union of all rowids using a RowSet object.  This is similar
 110222 ** to "bitmap indices" in other database engines.
 110224 ** OTHERWISE:
 110226 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
 110227 ** zero.  This term is not useful for search.
 110229 static void exprAnalyzeOrTerm(
 110230   SrcList *pSrc,            /* the FROM clause */
 110231   WhereClause *pWC,         /* the complete WHERE clause */
 110232   int idxTerm               /* Index of the OR-term to be analyzed */
 110234   WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
 110235   Parse *pParse = pWInfo->pParse;         /* Parser context */
 110236   sqlite3 *db = pParse->db;               /* Database connection */
 110237   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
 110238   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
 110239   int i;                                  /* Loop counters */
 110240   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
 110241   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
 110242   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
 110243   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
 110244   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
 110247   ** Break the OR clause into its separate subterms.  The subterms are
 110248   ** stored in a WhereClause structure containing within the WhereOrInfo
 110249   ** object that is attached to the original OR clause term.
 110251   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
 110252   assert( pExpr->op==TK_OR );
 110253   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
 110254   if( pOrInfo==0 ) return;
 110255   pTerm->wtFlags |= TERM_ORINFO;
 110256   pOrWc = &pOrInfo->wc;
 110257   whereClauseInit(pOrWc, pWInfo);
 110258   whereSplit(pOrWc, pExpr, TK_OR);
 110259   exprAnalyzeAll(pSrc, pOrWc);
 110260   if( db->mallocFailed ) return;
 110261   assert( pOrWc->nTerm>=2 );
 110264   ** Compute the set of tables that might satisfy cases 1 or 2.
 110266   indexable = ~(Bitmask)0;
 110267   chngToIN = ~(Bitmask)0;
 110268   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
 110269     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
 110270       WhereAndInfo *pAndInfo;
 110271       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
 110272       chngToIN = 0;
 110273       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
 110274       if( pAndInfo ){
 110275         WhereClause *pAndWC;
 110276         WhereTerm *pAndTerm;
 110277         int j;
 110278         Bitmask b = 0;
 110279         pOrTerm->u.pAndInfo = pAndInfo;
 110280         pOrTerm->wtFlags |= TERM_ANDINFO;
 110281         pOrTerm->eOperator = WO_AND;
 110282         pAndWC = &pAndInfo->wc;
 110283         whereClauseInit(pAndWC, pWC->pWInfo);
 110284         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
 110285         exprAnalyzeAll(pSrc, pAndWC);
 110286         pAndWC->pOuter = pWC;
 110287         testcase( db->mallocFailed );
 110288         if( !db->mallocFailed ){
 110289           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
 110290             assert( pAndTerm->pExpr );
 110291             if( allowedOp(pAndTerm->pExpr->op) ){
 110292               b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
 110296         indexable &= b;
 110298     }else if( pOrTerm->wtFlags & TERM_COPIED ){
 110299       /* Skip this term for now.  We revisit it when we process the
 110300       ** corresponding TERM_VIRTUAL term */
 110301     }else{
 110302       Bitmask b;
 110303       b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
 110304       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
 110305         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
 110306         b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
 110308       indexable &= b;
 110309       if( (pOrTerm->eOperator & WO_EQ)==0 ){
 110310         chngToIN = 0;
 110311       }else{
 110312         chngToIN &= b;
 110318   ** Record the set of tables that satisfy case 2.  The set might be
 110319   ** empty.
 110321   pOrInfo->indexable = indexable;
 110322   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
 110325   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
 110326   ** we have to do some additional checking to see if case 1 really
 110327   ** is satisfied.
 110329   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
 110330   ** that there is no possibility of transforming the OR clause into an
 110331   ** IN operator because one or more terms in the OR clause contain
 110332   ** something other than == on a column in the single table.  The 1-bit
 110333   ** case means that every term of the OR clause is of the form
 110334   ** "table.column=expr" for some single table.  The one bit that is set
 110335   ** will correspond to the common table.  We still need to check to make
 110336   ** sure the same column is used on all terms.  The 2-bit case is when
 110337   ** the all terms are of the form "table1.column=table2.column".  It
 110338   ** might be possible to form an IN operator with either table1.column
 110339   ** or table2.column as the LHS if either is common to every term of
 110340   ** the OR clause.
 110342   ** Note that terms of the form "table.column1=table.column2" (the
 110343   ** same table on both sizes of the ==) cannot be optimized.
 110345   if( chngToIN ){
 110346     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
 110347     int iColumn = -1;         /* Column index on lhs of IN operator */
 110348     int iCursor = -1;         /* Table cursor common to all terms */
 110349     int j = 0;                /* Loop counter */
 110351     /* Search for a table and column that appears on one side or the
 110352     ** other of the == operator in every subterm.  That table and column
 110353     ** will be recorded in iCursor and iColumn.  There might not be any
 110354     ** such table and column.  Set okToChngToIN if an appropriate table
 110355     ** and column is found but leave okToChngToIN false if not found.
 110357     for(j=0; j<2 && !okToChngToIN; j++){
 110358       pOrTerm = pOrWc->a;
 110359       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
 110360         assert( pOrTerm->eOperator & WO_EQ );
 110361         pOrTerm->wtFlags &= ~TERM_OR_OK;
 110362         if( pOrTerm->leftCursor==iCursor ){
 110363           /* This is the 2-bit case and we are on the second iteration and
 110364           ** current term is from the first iteration.  So skip this term. */
 110365           assert( j==1 );
 110366           continue;
 110368         if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
 110369           /* This term must be of the form t1.a==t2.b where t2 is in the
 110370           ** chngToIN set but t1 is not.  This term will be either preceeded
 110371           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
 110372           ** and use its inversion. */
 110373           testcase( pOrTerm->wtFlags & TERM_COPIED );
 110374           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
 110375           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
 110376           continue;
 110378         iColumn = pOrTerm->u.leftColumn;
 110379         iCursor = pOrTerm->leftCursor;
 110380         break;
 110382       if( i<0 ){
 110383         /* No candidate table+column was found.  This can only occur
 110384         ** on the second iteration */
 110385         assert( j==1 );
 110386         assert( IsPowerOfTwo(chngToIN) );
 110387         assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
 110388         break;
 110390       testcase( j==1 );
 110392       /* We have found a candidate table and column.  Check to see if that
 110393       ** table and column is common to every term in the OR clause */
 110394       okToChngToIN = 1;
 110395       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
 110396         assert( pOrTerm->eOperator & WO_EQ );
 110397         if( pOrTerm->leftCursor!=iCursor ){
 110398           pOrTerm->wtFlags &= ~TERM_OR_OK;
 110399         }else if( pOrTerm->u.leftColumn!=iColumn ){
 110400           okToChngToIN = 0;
 110401         }else{
 110402           int affLeft, affRight;
 110403           /* If the right-hand side is also a column, then the affinities
 110404           ** of both right and left sides must be such that no type
 110405           ** conversions are required on the right.  (Ticket #2249)
 110407           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
 110408           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
 110409           if( affRight!=0 && affRight!=affLeft ){
 110410             okToChngToIN = 0;
 110411           }else{
 110412             pOrTerm->wtFlags |= TERM_OR_OK;
 110418     /* At this point, okToChngToIN is true if original pTerm satisfies
 110419     ** case 1.  In that case, construct a new virtual term that is 
 110420     ** pTerm converted into an IN operator.
 110422     if( okToChngToIN ){
 110423       Expr *pDup;            /* A transient duplicate expression */
 110424       ExprList *pList = 0;   /* The RHS of the IN operator */
 110425       Expr *pLeft = 0;       /* The LHS of the IN operator */
 110426       Expr *pNew;            /* The complete IN operator */
 110428       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
 110429         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
 110430         assert( pOrTerm->eOperator & WO_EQ );
 110431         assert( pOrTerm->leftCursor==iCursor );
 110432         assert( pOrTerm->u.leftColumn==iColumn );
 110433         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
 110434         pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
 110435         pLeft = pOrTerm->pExpr->pLeft;
 110437       assert( pLeft!=0 );
 110438       pDup = sqlite3ExprDup(db, pLeft, 0);
 110439       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
 110440       if( pNew ){
 110441         int idxNew;
 110442         transferJoinMarkings(pNew, pExpr);
 110443         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
 110444         pNew->x.pList = pList;
 110445         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
 110446         testcase( idxNew==0 );
 110447         exprAnalyze(pSrc, pWC, idxNew);
 110448         pTerm = &pWC->a[idxTerm];
 110449         pWC->a[idxNew].iParent = idxTerm;
 110450         pTerm->nChild = 1;
 110451       }else{
 110452         sqlite3ExprListDelete(db, pList);
 110454       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
 110458 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
 110461 ** The input to this routine is an WhereTerm structure with only the
 110462 ** "pExpr" field filled in.  The job of this routine is to analyze the
 110463 ** subexpression and populate all the other fields of the WhereTerm
 110464 ** structure.
 110466 ** If the expression is of the form "<expr> <op> X" it gets commuted
 110467 ** to the standard form of "X <op> <expr>".
 110469 ** If the expression is of the form "X <op> Y" where both X and Y are
 110470 ** columns, then the original expression is unchanged and a new virtual
 110471 ** term of the form "Y <op> X" is added to the WHERE clause and
 110472 ** analyzed separately.  The original term is marked with TERM_COPIED
 110473 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
 110474 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
 110475 ** is a commuted copy of a prior term.)  The original term has nChild=1
 110476 ** and the copy has idxParent set to the index of the original term.
 110478 static void exprAnalyze(
 110479   SrcList *pSrc,            /* the FROM clause */
 110480   WhereClause *pWC,         /* the WHERE clause */
 110481   int idxTerm               /* Index of the term to be analyzed */
 110483   WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
 110484   WhereTerm *pTerm;                /* The term to be analyzed */
 110485   WhereMaskSet *pMaskSet;          /* Set of table index masks */
 110486   Expr *pExpr;                     /* The expression to be analyzed */
 110487   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
 110488   Bitmask prereqAll;               /* Prerequesites of pExpr */
 110489   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
 110490   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
 110491   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
 110492   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
 110493   int op;                          /* Top-level operator.  pExpr->op */
 110494   Parse *pParse = pWInfo->pParse;  /* Parsing context */
 110495   sqlite3 *db = pParse->db;        /* Database connection */
 110497   if( db->mallocFailed ){
 110498     return;
 110500   pTerm = &pWC->a[idxTerm];
 110501   pMaskSet = &pWInfo->sMaskSet;
 110502   pExpr = pTerm->pExpr;
 110503   assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
 110504   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
 110505   op = pExpr->op;
 110506   if( op==TK_IN ){
 110507     assert( pExpr->pRight==0 );
 110508     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 110509       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
 110510     }else{
 110511       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
 110513   }else if( op==TK_ISNULL ){
 110514     pTerm->prereqRight = 0;
 110515   }else{
 110516     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
 110518   prereqAll = exprTableUsage(pMaskSet, pExpr);
 110519   if( ExprHasProperty(pExpr, EP_FromJoin) ){
 110520     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
 110521     prereqAll |= x;
 110522     extraRight = x-1;  /* ON clause terms may not be used with an index
 110523                        ** on left table of a LEFT JOIN.  Ticket #3015 */
 110525   pTerm->prereqAll = prereqAll;
 110526   pTerm->leftCursor = -1;
 110527   pTerm->iParent = -1;
 110528   pTerm->eOperator = 0;
 110529   if( allowedOp(op) ){
 110530     Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
 110531     Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
 110532     u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
 110533     if( pLeft->op==TK_COLUMN ){
 110534       pTerm->leftCursor = pLeft->iTable;
 110535       pTerm->u.leftColumn = pLeft->iColumn;
 110536       pTerm->eOperator = operatorMask(op) & opMask;
 110538     if( pRight && pRight->op==TK_COLUMN ){
 110539       WhereTerm *pNew;
 110540       Expr *pDup;
 110541       u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
 110542       if( pTerm->leftCursor>=0 ){
 110543         int idxNew;
 110544         pDup = sqlite3ExprDup(db, pExpr, 0);
 110545         if( db->mallocFailed ){
 110546           sqlite3ExprDelete(db, pDup);
 110547           return;
 110549         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
 110550         if( idxNew==0 ) return;
 110551         pNew = &pWC->a[idxNew];
 110552         pNew->iParent = idxTerm;
 110553         pTerm = &pWC->a[idxTerm];
 110554         pTerm->nChild = 1;
 110555         pTerm->wtFlags |= TERM_COPIED;
 110556         if( pExpr->op==TK_EQ
 110557          && !ExprHasProperty(pExpr, EP_FromJoin)
 110558          && OptimizationEnabled(db, SQLITE_Transitive)
 110560           pTerm->eOperator |= WO_EQUIV;
 110561           eExtraOp = WO_EQUIV;
 110563       }else{
 110564         pDup = pExpr;
 110565         pNew = pTerm;
 110567       exprCommute(pParse, pDup);
 110568       pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
 110569       pNew->leftCursor = pLeft->iTable;
 110570       pNew->u.leftColumn = pLeft->iColumn;
 110571       testcase( (prereqLeft | extraRight) != prereqLeft );
 110572       pNew->prereqRight = prereqLeft | extraRight;
 110573       pNew->prereqAll = prereqAll;
 110574       pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
 110578 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
 110579   /* If a term is the BETWEEN operator, create two new virtual terms
 110580   ** that define the range that the BETWEEN implements.  For example:
 110582   **      a BETWEEN b AND c
 110584   ** is converted into:
 110586   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
 110588   ** The two new terms are added onto the end of the WhereClause object.
 110589   ** The new terms are "dynamic" and are children of the original BETWEEN
 110590   ** term.  That means that if the BETWEEN term is coded, the children are
 110591   ** skipped.  Or, if the children are satisfied by an index, the original
 110592   ** BETWEEN term is skipped.
 110594   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
 110595     ExprList *pList = pExpr->x.pList;
 110596     int i;
 110597     static const u8 ops[] = {TK_GE, TK_LE};
 110598     assert( pList!=0 );
 110599     assert( pList->nExpr==2 );
 110600     for(i=0; i<2; i++){
 110601       Expr *pNewExpr;
 110602       int idxNew;
 110603       pNewExpr = sqlite3PExpr(pParse, ops[i], 
 110604                              sqlite3ExprDup(db, pExpr->pLeft, 0),
 110605                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
 110606       transferJoinMarkings(pNewExpr, pExpr);
 110607       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
 110608       testcase( idxNew==0 );
 110609       exprAnalyze(pSrc, pWC, idxNew);
 110610       pTerm = &pWC->a[idxTerm];
 110611       pWC->a[idxNew].iParent = idxTerm;
 110613     pTerm->nChild = 2;
 110615 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
 110617 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
 110618   /* Analyze a term that is composed of two or more subterms connected by
 110619   ** an OR operator.
 110621   else if( pExpr->op==TK_OR ){
 110622     assert( pWC->op==TK_AND );
 110623     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
 110624     pTerm = &pWC->a[idxTerm];
 110626 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 110628 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
 110629   /* Add constraints to reduce the search space on a LIKE or GLOB
 110630   ** operator.
 110632   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
 110634   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
 110636   ** The last character of the prefix "abc" is incremented to form the
 110637   ** termination condition "abd".
 110639   if( pWC->op==TK_AND 
 110640    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
 110642     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
 110643     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
 110644     Expr *pNewExpr1;
 110645     Expr *pNewExpr2;
 110646     int idxNew1;
 110647     int idxNew2;
 110648     Token sCollSeqName;  /* Name of collating sequence */
 110650     pLeft = pExpr->x.pList->a[1].pExpr;
 110651     pStr2 = sqlite3ExprDup(db, pStr1, 0);
 110652     if( !db->mallocFailed ){
 110653       u8 c, *pC;       /* Last character before the first wildcard */
 110654       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
 110655       c = *pC;
 110656       if( noCase ){
 110657         /* The point is to increment the last character before the first
 110658         ** wildcard.  But if we increment '@', that will push it into the
 110659         ** alphabetic range where case conversions will mess up the 
 110660         ** inequality.  To avoid this, make sure to also run the full
 110661         ** LIKE on all candidate expressions by clearing the isComplete flag
 110663         if( c=='A'-1 ) isComplete = 0;
 110664         c = sqlite3UpperToLower[c];
 110666       *pC = c + 1;
 110668     sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
 110669     sCollSeqName.n = 6;
 110670     pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
 110671     pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
 110672            sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
 110673            pStr1, 0);
 110674     transferJoinMarkings(pNewExpr1, pExpr);
 110675     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
 110676     testcase( idxNew1==0 );
 110677     exprAnalyze(pSrc, pWC, idxNew1);
 110678     pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
 110679     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
 110680            sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
 110681            pStr2, 0);
 110682     transferJoinMarkings(pNewExpr2, pExpr);
 110683     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
 110684     testcase( idxNew2==0 );
 110685     exprAnalyze(pSrc, pWC, idxNew2);
 110686     pTerm = &pWC->a[idxTerm];
 110687     if( isComplete ){
 110688       pWC->a[idxNew1].iParent = idxTerm;
 110689       pWC->a[idxNew2].iParent = idxTerm;
 110690       pTerm->nChild = 2;
 110693 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
 110695 #ifndef SQLITE_OMIT_VIRTUALTABLE
 110696   /* Add a WO_MATCH auxiliary term to the constraint set if the
 110697   ** current expression is of the form:  column MATCH expr.
 110698   ** This information is used by the xBestIndex methods of
 110699   ** virtual tables.  The native query optimizer does not attempt
 110700   ** to do anything with MATCH functions.
 110702   if( isMatchOfColumn(pExpr) ){
 110703     int idxNew;
 110704     Expr *pRight, *pLeft;
 110705     WhereTerm *pNewTerm;
 110706     Bitmask prereqColumn, prereqExpr;
 110708     pRight = pExpr->x.pList->a[0].pExpr;
 110709     pLeft = pExpr->x.pList->a[1].pExpr;
 110710     prereqExpr = exprTableUsage(pMaskSet, pRight);
 110711     prereqColumn = exprTableUsage(pMaskSet, pLeft);
 110712     if( (prereqExpr & prereqColumn)==0 ){
 110713       Expr *pNewExpr;
 110714       pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
 110715                               0, sqlite3ExprDup(db, pRight, 0), 0);
 110716       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
 110717       testcase( idxNew==0 );
 110718       pNewTerm = &pWC->a[idxNew];
 110719       pNewTerm->prereqRight = prereqExpr;
 110720       pNewTerm->leftCursor = pLeft->iTable;
 110721       pNewTerm->u.leftColumn = pLeft->iColumn;
 110722       pNewTerm->eOperator = WO_MATCH;
 110723       pNewTerm->iParent = idxTerm;
 110724       pTerm = &pWC->a[idxTerm];
 110725       pTerm->nChild = 1;
 110726       pTerm->wtFlags |= TERM_COPIED;
 110727       pNewTerm->prereqAll = pTerm->prereqAll;
 110730 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 110732 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 110733   /* When sqlite_stat3 histogram data is available an operator of the
 110734   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
 110735   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
 110736   ** virtual term of that form.
 110738   ** Note that the virtual term must be tagged with TERM_VNULL.  This
 110739   ** TERM_VNULL tag will suppress the not-null check at the beginning
 110740   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
 110741   ** the start of the loop will prevent any results from being returned.
 110743   if( pExpr->op==TK_NOTNULL
 110744    && pExpr->pLeft->op==TK_COLUMN
 110745    && pExpr->pLeft->iColumn>=0
 110746    && OptimizationEnabled(db, SQLITE_Stat3)
 110748     Expr *pNewExpr;
 110749     Expr *pLeft = pExpr->pLeft;
 110750     int idxNew;
 110751     WhereTerm *pNewTerm;
 110753     pNewExpr = sqlite3PExpr(pParse, TK_GT,
 110754                             sqlite3ExprDup(db, pLeft, 0),
 110755                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
 110757     idxNew = whereClauseInsert(pWC, pNewExpr,
 110758                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
 110759     if( idxNew ){
 110760       pNewTerm = &pWC->a[idxNew];
 110761       pNewTerm->prereqRight = 0;
 110762       pNewTerm->leftCursor = pLeft->iTable;
 110763       pNewTerm->u.leftColumn = pLeft->iColumn;
 110764       pNewTerm->eOperator = WO_GT;
 110765       pNewTerm->iParent = idxTerm;
 110766       pTerm = &pWC->a[idxTerm];
 110767       pTerm->nChild = 1;
 110768       pTerm->wtFlags |= TERM_COPIED;
 110769       pNewTerm->prereqAll = pTerm->prereqAll;
 110772 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 110774   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
 110775   ** an index for tables to the left of the join.
 110777   pTerm->prereqRight |= extraRight;
 110781 ** This function searches pList for a entry that matches the iCol-th column
 110782 ** of index pIdx.
 110784 ** If such an expression is found, its index in pList->a[] is returned. If
 110785 ** no expression is found, -1 is returned.
 110787 static int findIndexCol(
 110788   Parse *pParse,                  /* Parse context */
 110789   ExprList *pList,                /* Expression list to search */
 110790   int iBase,                      /* Cursor for table associated with pIdx */
 110791   Index *pIdx,                    /* Index to match column of */
 110792   int iCol                        /* Column of index to match */
 110794   int i;
 110795   const char *zColl = pIdx->azColl[iCol];
 110797   for(i=0; i<pList->nExpr; i++){
 110798     Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
 110799     if( p->op==TK_COLUMN
 110800      && p->iColumn==pIdx->aiColumn[iCol]
 110801      && p->iTable==iBase
 110803       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
 110804       if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
 110805         return i;
 110810   return -1;
 110814 ** Return true if the DISTINCT expression-list passed as the third argument
 110815 ** is redundant.
 110817 ** A DISTINCT list is redundant if the database contains some subset of
 110818 ** columns that are unique and non-null.
 110820 static int isDistinctRedundant(
 110821   Parse *pParse,            /* Parsing context */
 110822   SrcList *pTabList,        /* The FROM clause */
 110823   WhereClause *pWC,         /* The WHERE clause */
 110824   ExprList *pDistinct       /* The result set that needs to be DISTINCT */
 110826   Table *pTab;
 110827   Index *pIdx;
 110828   int i;                          
 110829   int iBase;
 110831   /* If there is more than one table or sub-select in the FROM clause of
 110832   ** this query, then it will not be possible to show that the DISTINCT 
 110833   ** clause is redundant. */
 110834   if( pTabList->nSrc!=1 ) return 0;
 110835   iBase = pTabList->a[0].iCursor;
 110836   pTab = pTabList->a[0].pTab;
 110838   /* If any of the expressions is an IPK column on table iBase, then return 
 110839   ** true. Note: The (p->iTable==iBase) part of this test may be false if the
 110840   ** current SELECT is a correlated sub-query.
 110842   for(i=0; i<pDistinct->nExpr; i++){
 110843     Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
 110844     if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
 110847   /* Loop through all indices on the table, checking each to see if it makes
 110848   ** the DISTINCT qualifier redundant. It does so if:
 110850   **   1. The index is itself UNIQUE, and
 110852   **   2. All of the columns in the index are either part of the pDistinct
 110853   **      list, or else the WHERE clause contains a term of the form "col=X",
 110854   **      where X is a constant value. The collation sequences of the
 110855   **      comparison and select-list expressions must match those of the index.
 110857   **   3. All of those index columns for which the WHERE clause does not
 110858   **      contain a "col=X" term are subject to a NOT NULL constraint.
 110860   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 110861     if( pIdx->onError==OE_None ) continue;
 110862     for(i=0; i<pIdx->nKeyCol; i++){
 110863       i16 iCol = pIdx->aiColumn[i];
 110864       if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
 110865         int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
 110866         if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
 110867           break;
 110871     if( i==pIdx->nKeyCol ){
 110872       /* This index implies that the DISTINCT qualifier is redundant. */
 110873       return 1;
 110877   return 0;
 110882 ** Estimate the logarithm of the input value to base 2.
 110884 static LogEst estLog(LogEst N){
 110885   LogEst x = sqlite3LogEst(N);
 110886   return x>33 ? x - 33 : 0;
 110890 ** Two routines for printing the content of an sqlite3_index_info
 110891 ** structure.  Used for testing and debugging only.  If neither
 110892 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
 110893 ** are no-ops.
 110895 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
 110896 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
 110897   int i;
 110898   if( !sqlite3WhereTrace ) return;
 110899   for(i=0; i<p->nConstraint; i++){
 110900     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
 110902        p->aConstraint[i].iColumn,
 110903        p->aConstraint[i].iTermOffset,
 110904        p->aConstraint[i].op,
 110905        p->aConstraint[i].usable);
 110907   for(i=0; i<p->nOrderBy; i++){
 110908     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
 110910        p->aOrderBy[i].iColumn,
 110911        p->aOrderBy[i].desc);
 110914 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
 110915   int i;
 110916   if( !sqlite3WhereTrace ) return;
 110917   for(i=0; i<p->nConstraint; i++){
 110918     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
 110920        p->aConstraintUsage[i].argvIndex,
 110921        p->aConstraintUsage[i].omit);
 110923   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
 110924   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
 110925   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
 110926   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
 110927   sqlite3DebugPrintf("  estimatedRows=%lld\n", p->estimatedRows);
 110929 #else
 110930 #define TRACE_IDX_INPUTS(A)
 110931 #define TRACE_IDX_OUTPUTS(A)
 110932 #endif
 110934 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 110936 ** Return TRUE if the WHERE clause term pTerm is of a form where it
 110937 ** could be used with an index to access pSrc, assuming an appropriate
 110938 ** index existed.
 110940 static int termCanDriveIndex(
 110941   WhereTerm *pTerm,              /* WHERE clause term to check */
 110942   struct SrcList_item *pSrc,     /* Table we are trying to access */
 110943   Bitmask notReady               /* Tables in outer loops of the join */
 110945   char aff;
 110946   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
 110947   if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
 110948   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
 110949   if( pTerm->u.leftColumn<0 ) return 0;
 110950   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
 110951   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
 110952   return 1;
 110954 #endif
 110957 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 110959 ** Generate code to construct the Index object for an automatic index
 110960 ** and to set up the WhereLevel object pLevel so that the code generator
 110961 ** makes use of the automatic index.
 110963 static void constructAutomaticIndex(
 110964   Parse *pParse,              /* The parsing context */
 110965   WhereClause *pWC,           /* The WHERE clause */
 110966   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
 110967   Bitmask notReady,           /* Mask of cursors that are not available */
 110968   WhereLevel *pLevel          /* Write new index here */
 110970   int nKeyCol;                /* Number of columns in the constructed index */
 110971   WhereTerm *pTerm;           /* A single term of the WHERE clause */
 110972   WhereTerm *pWCEnd;          /* End of pWC->a[] */
 110973   Index *pIdx;                /* Object describing the transient index */
 110974   Vdbe *v;                    /* Prepared statement under construction */
 110975   int addrInit;               /* Address of the initialization bypass jump */
 110976   Table *pTable;              /* The table being indexed */
 110977   int addrTop;                /* Top of the index fill loop */
 110978   int regRecord;              /* Register holding an index record */
 110979   int n;                      /* Column counter */
 110980   int i;                      /* Loop counter */
 110981   int mxBitCol;               /* Maximum column in pSrc->colUsed */
 110982   CollSeq *pColl;             /* Collating sequence to on a column */
 110983   WhereLoop *pLoop;           /* The Loop object */
 110984   char *zNotUsed;             /* Extra space on the end of pIdx */
 110985   Bitmask idxCols;            /* Bitmap of columns used for indexing */
 110986   Bitmask extraCols;          /* Bitmap of additional columns */
 110987   u8 sentWarning = 0;         /* True if a warnning has been issued */
 110989   /* Generate code to skip over the creation and initialization of the
 110990   ** transient index on 2nd and subsequent iterations of the loop. */
 110991   v = pParse->pVdbe;
 110992   assert( v!=0 );
 110993   addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
 110995   /* Count the number of columns that will be added to the index
 110996   ** and used to match WHERE clause constraints */
 110997   nKeyCol = 0;
 110998   pTable = pSrc->pTab;
 110999   pWCEnd = &pWC->a[pWC->nTerm];
 111000   pLoop = pLevel->pWLoop;
 111001   idxCols = 0;
 111002   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 111003     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
 111004       int iCol = pTerm->u.leftColumn;
 111005       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
 111006       testcase( iCol==BMS );
 111007       testcase( iCol==BMS-1 );
 111008       if( !sentWarning ){
 111009         sqlite3_log(SQLITE_WARNING_AUTOINDEX,
 111010             "automatic index on %s(%s)", pTable->zName,
 111011             pTable->aCol[iCol].zName);
 111012         sentWarning = 1;
 111014       if( (idxCols & cMask)==0 ){
 111015         if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
 111016         pLoop->aLTerm[nKeyCol++] = pTerm;
 111017         idxCols |= cMask;
 111021   assert( nKeyCol>0 );
 111022   pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
 111023   pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
 111024                      | WHERE_AUTO_INDEX;
 111026   /* Count the number of additional columns needed to create a
 111027   ** covering index.  A "covering index" is an index that contains all
 111028   ** columns that are needed by the query.  With a covering index, the
 111029   ** original table never needs to be accessed.  Automatic indices must
 111030   ** be a covering index because the index will not be updated if the
 111031   ** original table changes and the index and table cannot both be used
 111032   ** if they go out of sync.
 111034   extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
 111035   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
 111036   testcase( pTable->nCol==BMS-1 );
 111037   testcase( pTable->nCol==BMS-2 );
 111038   for(i=0; i<mxBitCol; i++){
 111039     if( extraCols & MASKBIT(i) ) nKeyCol++;
 111041   if( pSrc->colUsed & MASKBIT(BMS-1) ){
 111042     nKeyCol += pTable->nCol - BMS + 1;
 111044   pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
 111046   /* Construct the Index object to describe this index */
 111047   pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
 111048   if( pIdx==0 ) return;
 111049   pLoop->u.btree.pIndex = pIdx;
 111050   pIdx->zName = "auto-index";
 111051   pIdx->pTable = pTable;
 111052   n = 0;
 111053   idxCols = 0;
 111054   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
 111055     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
 111056       int iCol = pTerm->u.leftColumn;
 111057       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
 111058       testcase( iCol==BMS-1 );
 111059       testcase( iCol==BMS );
 111060       if( (idxCols & cMask)==0 ){
 111061         Expr *pX = pTerm->pExpr;
 111062         idxCols |= cMask;
 111063         pIdx->aiColumn[n] = pTerm->u.leftColumn;
 111064         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
 111065         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
 111066         n++;
 111070   assert( (u32)n==pLoop->u.btree.nEq );
 111072   /* Add additional columns needed to make the automatic index into
 111073   ** a covering index */
 111074   for(i=0; i<mxBitCol; i++){
 111075     if( extraCols & MASKBIT(i) ){
 111076       pIdx->aiColumn[n] = i;
 111077       pIdx->azColl[n] = "BINARY";
 111078       n++;
 111081   if( pSrc->colUsed & MASKBIT(BMS-1) ){
 111082     for(i=BMS-1; i<pTable->nCol; i++){
 111083       pIdx->aiColumn[n] = i;
 111084       pIdx->azColl[n] = "BINARY";
 111085       n++;
 111088   assert( n==nKeyCol );
 111089   pIdx->aiColumn[n] = -1;
 111090   pIdx->azColl[n] = "BINARY";
 111092   /* Create the automatic index */
 111093   assert( pLevel->iIdxCur>=0 );
 111094   pLevel->iIdxCur = pParse->nTab++;
 111095   sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
 111096   sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 111097   VdbeComment((v, "for %s", pTable->zName));
 111099   /* Fill the automatic index with content */
 111100   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
 111101   regRecord = sqlite3GetTempReg(pParse);
 111102   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
 111103   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
 111104   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 111105   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
 111106   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
 111107   sqlite3VdbeJumpHere(v, addrTop);
 111108   sqlite3ReleaseTempReg(pParse, regRecord);
 111110   /* Jump here when skipping the initialization */
 111111   sqlite3VdbeJumpHere(v, addrInit);
 111113 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
 111115 #ifndef SQLITE_OMIT_VIRTUALTABLE
 111117 ** Allocate and populate an sqlite3_index_info structure. It is the 
 111118 ** responsibility of the caller to eventually release the structure
 111119 ** by passing the pointer returned by this function to sqlite3_free().
 111121 static sqlite3_index_info *allocateIndexInfo(
 111122   Parse *pParse,
 111123   WhereClause *pWC,
 111124   struct SrcList_item *pSrc,
 111125   ExprList *pOrderBy
 111127   int i, j;
 111128   int nTerm;
 111129   struct sqlite3_index_constraint *pIdxCons;
 111130   struct sqlite3_index_orderby *pIdxOrderBy;
 111131   struct sqlite3_index_constraint_usage *pUsage;
 111132   WhereTerm *pTerm;
 111133   int nOrderBy;
 111134   sqlite3_index_info *pIdxInfo;
 111136   /* Count the number of possible WHERE clause constraints referring
 111137   ** to this virtual table */
 111138   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 111139     if( pTerm->leftCursor != pSrc->iCursor ) continue;
 111140     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
 111141     testcase( pTerm->eOperator & WO_IN );
 111142     testcase( pTerm->eOperator & WO_ISNULL );
 111143     testcase( pTerm->eOperator & WO_ALL );
 111144     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
 111145     if( pTerm->wtFlags & TERM_VNULL ) continue;
 111146     nTerm++;
 111149   /* If the ORDER BY clause contains only columns in the current 
 111150   ** virtual table then allocate space for the aOrderBy part of
 111151   ** the sqlite3_index_info structure.
 111153   nOrderBy = 0;
 111154   if( pOrderBy ){
 111155     int n = pOrderBy->nExpr;
 111156     for(i=0; i<n; i++){
 111157       Expr *pExpr = pOrderBy->a[i].pExpr;
 111158       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
 111160     if( i==n){
 111161       nOrderBy = n;
 111165   /* Allocate the sqlite3_index_info structure
 111167   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
 111168                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
 111169                            + sizeof(*pIdxOrderBy)*nOrderBy );
 111170   if( pIdxInfo==0 ){
 111171     sqlite3ErrorMsg(pParse, "out of memory");
 111172     return 0;
 111175   /* Initialize the structure.  The sqlite3_index_info structure contains
 111176   ** many fields that are declared "const" to prevent xBestIndex from
 111177   ** changing them.  We have to do some funky casting in order to
 111178   ** initialize those fields.
 111180   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
 111181   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
 111182   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
 111183   *(int*)&pIdxInfo->nConstraint = nTerm;
 111184   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
 111185   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
 111186   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
 111187   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
 111188                                                                    pUsage;
 111190   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 111191     u8 op;
 111192     if( pTerm->leftCursor != pSrc->iCursor ) continue;
 111193     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
 111194     testcase( pTerm->eOperator & WO_IN );
 111195     testcase( pTerm->eOperator & WO_ISNULL );
 111196     testcase( pTerm->eOperator & WO_ALL );
 111197     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
 111198     if( pTerm->wtFlags & TERM_VNULL ) continue;
 111199     pIdxCons[j].iColumn = pTerm->u.leftColumn;
 111200     pIdxCons[j].iTermOffset = i;
 111201     op = (u8)pTerm->eOperator & WO_ALL;
 111202     if( op==WO_IN ) op = WO_EQ;
 111203     pIdxCons[j].op = op;
 111204     /* The direct assignment in the previous line is possible only because
 111205     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
 111206     ** following asserts verify this fact. */
 111207     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
 111208     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
 111209     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
 111210     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
 111211     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
 111212     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
 111213     assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
 111214     j++;
 111216   for(i=0; i<nOrderBy; i++){
 111217     Expr *pExpr = pOrderBy->a[i].pExpr;
 111218     pIdxOrderBy[i].iColumn = pExpr->iColumn;
 111219     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
 111222   return pIdxInfo;
 111226 ** The table object reference passed as the second argument to this function
 111227 ** must represent a virtual table. This function invokes the xBestIndex()
 111228 ** method of the virtual table with the sqlite3_index_info object that
 111229 ** comes in as the 3rd argument to this function.
 111231 ** If an error occurs, pParse is populated with an error message and a
 111232 ** non-zero value is returned. Otherwise, 0 is returned and the output
 111233 ** part of the sqlite3_index_info structure is left populated.
 111235 ** Whether or not an error is returned, it is the responsibility of the
 111236 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
 111237 ** that this is required.
 111239 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
 111240   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
 111241   int i;
 111242   int rc;
 111244   TRACE_IDX_INPUTS(p);
 111245   rc = pVtab->pModule->xBestIndex(pVtab, p);
 111246   TRACE_IDX_OUTPUTS(p);
 111248   if( rc!=SQLITE_OK ){
 111249     if( rc==SQLITE_NOMEM ){
 111250       pParse->db->mallocFailed = 1;
 111251     }else if( !pVtab->zErrMsg ){
 111252       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
 111253     }else{
 111254       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
 111257   sqlite3_free(pVtab->zErrMsg);
 111258   pVtab->zErrMsg = 0;
 111260   for(i=0; i<p->nConstraint; i++){
 111261     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
 111262       sqlite3ErrorMsg(pParse, 
 111263           "table %s: xBestIndex returned an invalid plan", pTab->zName);
 111267   return pParse->nErr;
 111269 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
 111272 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 111274 ** Estimate the location of a particular key among all keys in an
 111275 ** index.  Store the results in aStat as follows:
 111277 **    aStat[0]      Est. number of rows less than pVal
 111278 **    aStat[1]      Est. number of rows equal to pVal
 111280 ** Return SQLITE_OK on success.
 111282 static void whereKeyStats(
 111283   Parse *pParse,              /* Database connection */
 111284   Index *pIdx,                /* Index to consider domain of */
 111285   UnpackedRecord *pRec,       /* Vector of values to consider */
 111286   int roundUp,                /* Round up if true.  Round down if false */
 111287   tRowcnt *aStat              /* OUT: stats written here */
 111289   IndexSample *aSample = pIdx->aSample;
 111290   int iCol;                   /* Index of required stats in anEq[] etc. */
 111291   int iMin = 0;               /* Smallest sample not yet tested */
 111292   int i = pIdx->nSample;      /* Smallest sample larger than or equal to pRec */
 111293   int iTest;                  /* Next sample to test */
 111294   int res;                    /* Result of comparison operation */
 111296 #ifndef SQLITE_DEBUG
 111297   UNUSED_PARAMETER( pParse );
 111298 #endif
 111299   assert( pRec!=0 );
 111300   iCol = pRec->nField - 1;
 111301   assert( pIdx->nSample>0 );
 111302   assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
 111303   do{
 111304     iTest = (iMin+i)/2;
 111305     res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
 111306     if( res<0 ){
 111307       iMin = iTest+1;
 111308     }else{
 111309       i = iTest;
 111311   }while( res && iMin<i );
 111313 #ifdef SQLITE_DEBUG
 111314   /* The following assert statements check that the binary search code
 111315   ** above found the right answer. This block serves no purpose other
 111316   ** than to invoke the asserts.  */
 111317   if( res==0 ){
 111318     /* If (res==0) is true, then sample $i must be equal to pRec */
 111319     assert( i<pIdx->nSample );
 111320     assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
 111321          || pParse->db->mallocFailed );
 111322   }else{
 111323     /* Otherwise, pRec must be smaller than sample $i and larger than
 111324     ** sample ($i-1).  */
 111325     assert( i==pIdx->nSample 
 111326          || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
 111327          || pParse->db->mallocFailed );
 111328     assert( i==0
 111329          || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
 111330          || pParse->db->mallocFailed );
 111332 #endif /* ifdef SQLITE_DEBUG */
 111334   /* At this point, aSample[i] is the first sample that is greater than
 111335   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
 111336   ** than pVal.  If aSample[i]==pVal, then res==0.
 111338   if( res==0 ){
 111339     aStat[0] = aSample[i].anLt[iCol];
 111340     aStat[1] = aSample[i].anEq[iCol];
 111341   }else{
 111342     tRowcnt iLower, iUpper, iGap;
 111343     if( i==0 ){
 111344       iLower = 0;
 111345       iUpper = aSample[0].anLt[iCol];
 111346     }else{
 111347       iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
 111348       iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
 111350     aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
 111351     if( iLower>=iUpper ){
 111352       iGap = 0;
 111353     }else{
 111354       iGap = iUpper - iLower;
 111356     if( roundUp ){
 111357       iGap = (iGap*2)/3;
 111358     }else{
 111359       iGap = iGap/3;
 111361     aStat[0] = iLower + iGap;
 111364 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 111367 ** This function is used to estimate the number of rows that will be visited
 111368 ** by scanning an index for a range of values. The range may have an upper
 111369 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
 111370 ** and lower bounds are represented by pLower and pUpper respectively. For
 111371 ** example, assuming that index p is on t1(a):
 111373 **   ... FROM t1 WHERE a > ? AND a < ? ...
 111374 **                    |_____|   |_____|
 111375 **                       |         |
 111376 **                     pLower    pUpper
 111378 ** If either of the upper or lower bound is not present, then NULL is passed in
 111379 ** place of the corresponding WhereTerm.
 111381 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
 111382 ** column subject to the range constraint. Or, equivalently, the number of
 111383 ** equality constraints optimized by the proposed index scan. For example,
 111384 ** assuming index p is on t1(a, b), and the SQL query is:
 111386 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
 111388 ** then nEq is set to 1 (as the range restricted column, b, is the second 
 111389 ** left-most column of the index). Or, if the query is:
 111391 **   ... FROM t1 WHERE a > ? AND a < ? ...
 111393 ** then nEq is set to 0.
 111395 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
 111396 ** number of rows that the index scan is expected to visit without 
 111397 ** considering the range constraints. If nEq is 0, this is the number of 
 111398 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
 111399 ** to account for the range contraints pLower and pUpper.
 111401 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
 111402 ** used, each range inequality reduces the search space by a factor of 4. 
 111403 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
 111404 ** rows visited by a factor of 16.
 111406 static int whereRangeScanEst(
 111407   Parse *pParse,       /* Parsing & code generating context */
 111408   WhereLoopBuilder *pBuilder,
 111409   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
 111410   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
 111411   WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
 111413   int rc = SQLITE_OK;
 111414   int nOut = pLoop->nOut;
 111415   LogEst nNew;
 111417 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 111418   Index *p = pLoop->u.btree.pIndex;
 111419   int nEq = pLoop->u.btree.nEq;
 111421   if( p->nSample>0
 111422    && nEq==pBuilder->nRecValid
 111423    && nEq<p->nSampleCol
 111424    && OptimizationEnabled(pParse->db, SQLITE_Stat3) 
 111426     UnpackedRecord *pRec = pBuilder->pRec;
 111427     tRowcnt a[2];
 111428     u8 aff;
 111430     /* Variable iLower will be set to the estimate of the number of rows in 
 111431     ** the index that are less than the lower bound of the range query. The
 111432     ** lower bound being the concatenation of $P and $L, where $P is the
 111433     ** key-prefix formed by the nEq values matched against the nEq left-most
 111434     ** columns of the index, and $L is the value in pLower.
 111436     ** Or, if pLower is NULL or $L cannot be extracted from it (because it
 111437     ** is not a simple variable or literal value), the lower bound of the
 111438     ** range is $P. Due to a quirk in the way whereKeyStats() works, even
 111439     ** if $L is available, whereKeyStats() is called for both ($P) and 
 111440     ** ($P:$L) and the larger of the two returned values used.
 111442     ** Similarly, iUpper is to be set to the estimate of the number of rows
 111443     ** less than the upper bound of the range query. Where the upper bound
 111444     ** is either ($P) or ($P:$U). Again, even if $U is available, both values
 111445     ** of iUpper are requested of whereKeyStats() and the smaller used.
 111447     tRowcnt iLower;
 111448     tRowcnt iUpper;
 111450     if( nEq==p->nKeyCol ){
 111451       aff = SQLITE_AFF_INTEGER;
 111452     }else{
 111453       aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
 111455     /* Determine iLower and iUpper using ($P) only. */
 111456     if( nEq==0 ){
 111457       iLower = 0;
 111458       iUpper = p->aiRowEst[0];
 111459     }else{
 111460       /* Note: this call could be optimized away - since the same values must 
 111461       ** have been requested when testing key $P in whereEqualScanEst().  */
 111462       whereKeyStats(pParse, p, pRec, 0, a);
 111463       iLower = a[0];
 111464       iUpper = a[0] + a[1];
 111467     /* If possible, improve on the iLower estimate using ($P:$L). */
 111468     if( pLower ){
 111469       int bOk;                    /* True if value is extracted from pExpr */
 111470       Expr *pExpr = pLower->pExpr->pRight;
 111471       assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
 111472       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
 111473       if( rc==SQLITE_OK && bOk ){
 111474         tRowcnt iNew;
 111475         whereKeyStats(pParse, p, pRec, 0, a);
 111476         iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
 111477         if( iNew>iLower ) iLower = iNew;
 111478         nOut--;
 111482     /* If possible, improve on the iUpper estimate using ($P:$U). */
 111483     if( pUpper ){
 111484       int bOk;                    /* True if value is extracted from pExpr */
 111485       Expr *pExpr = pUpper->pExpr->pRight;
 111486       assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
 111487       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
 111488       if( rc==SQLITE_OK && bOk ){
 111489         tRowcnt iNew;
 111490         whereKeyStats(pParse, p, pRec, 1, a);
 111491         iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
 111492         if( iNew<iUpper ) iUpper = iNew;
 111493         nOut--;
 111497     pBuilder->pRec = pRec;
 111498     if( rc==SQLITE_OK ){
 111499       if( iUpper>iLower ){
 111500         nNew = sqlite3LogEst(iUpper - iLower);
 111501       }else{
 111502         nNew = 10;        assert( 10==sqlite3LogEst(2) );
 111504       if( nNew<nOut ){
 111505         nOut = nNew;
 111507       pLoop->nOut = (LogEst)nOut;
 111508       WHERETRACE(0x10, ("range scan regions: %u..%u  est=%d\n",
 111509                          (u32)iLower, (u32)iUpper, nOut));
 111510       return SQLITE_OK;
 111513 #else
 111514   UNUSED_PARAMETER(pParse);
 111515   UNUSED_PARAMETER(pBuilder);
 111516 #endif
 111517   assert( pLower || pUpper );
 111518   /* TUNING:  Each inequality constraint reduces the search space 4-fold.
 111519   ** A BETWEEN operator, therefore, reduces the search space 16-fold */
 111520   nNew = nOut;
 111521   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
 111522     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
 111523     nOut--;
 111525   if( pUpper ){
 111526     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
 111527     nOut--;
 111529   if( nNew<10 ) nNew = 10;
 111530   if( nNew<nOut ) nOut = nNew;
 111531   pLoop->nOut = (LogEst)nOut;
 111532   return rc;
 111535 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 111537 ** Estimate the number of rows that will be returned based on
 111538 ** an equality constraint x=VALUE and where that VALUE occurs in
 111539 ** the histogram data.  This only works when x is the left-most
 111540 ** column of an index and sqlite_stat3 histogram data is available
 111541 ** for that index.  When pExpr==NULL that means the constraint is
 111542 ** "x IS NULL" instead of "x=VALUE".
 111544 ** Write the estimated row count into *pnRow and return SQLITE_OK. 
 111545 ** If unable to make an estimate, leave *pnRow unchanged and return
 111546 ** non-zero.
 111548 ** This routine can fail if it is unable to load a collating sequence
 111549 ** required for string comparison, or if unable to allocate memory
 111550 ** for a UTF conversion required for comparison.  The error is stored
 111551 ** in the pParse structure.
 111553 static int whereEqualScanEst(
 111554   Parse *pParse,       /* Parsing & code generating context */
 111555   WhereLoopBuilder *pBuilder,
 111556   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
 111557   tRowcnt *pnRow       /* Write the revised row estimate here */
 111559   Index *p = pBuilder->pNew->u.btree.pIndex;
 111560   int nEq = pBuilder->pNew->u.btree.nEq;
 111561   UnpackedRecord *pRec = pBuilder->pRec;
 111562   u8 aff;                   /* Column affinity */
 111563   int rc;                   /* Subfunction return code */
 111564   tRowcnt a[2];             /* Statistics */
 111565   int bOk;
 111567   assert( nEq>=1 );
 111568   assert( nEq<=(p->nKeyCol+1) );
 111569   assert( p->aSample!=0 );
 111570   assert( p->nSample>0 );
 111571   assert( pBuilder->nRecValid<nEq );
 111573   /* If values are not available for all fields of the index to the left
 111574   ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
 111575   if( pBuilder->nRecValid<(nEq-1) ){
 111576     return SQLITE_NOTFOUND;
 111579   /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
 111580   ** below would return the same value.  */
 111581   if( nEq>p->nKeyCol ){
 111582     *pnRow = 1;
 111583     return SQLITE_OK;
 111586   aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
 111587   rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
 111588   pBuilder->pRec = pRec;
 111589   if( rc!=SQLITE_OK ) return rc;
 111590   if( bOk==0 ) return SQLITE_NOTFOUND;
 111591   pBuilder->nRecValid = nEq;
 111593   whereKeyStats(pParse, p, pRec, 0, a);
 111594   WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
 111595   *pnRow = a[1];
 111597   return rc;
 111599 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 111601 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 111603 ** Estimate the number of rows that will be returned based on
 111604 ** an IN constraint where the right-hand side of the IN operator
 111605 ** is a list of values.  Example:
 111607 **        WHERE x IN (1,2,3,4)
 111609 ** Write the estimated row count into *pnRow and return SQLITE_OK. 
 111610 ** If unable to make an estimate, leave *pnRow unchanged and return
 111611 ** non-zero.
 111613 ** This routine can fail if it is unable to load a collating sequence
 111614 ** required for string comparison, or if unable to allocate memory
 111615 ** for a UTF conversion required for comparison.  The error is stored
 111616 ** in the pParse structure.
 111618 static int whereInScanEst(
 111619   Parse *pParse,       /* Parsing & code generating context */
 111620   WhereLoopBuilder *pBuilder,
 111621   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
 111622   tRowcnt *pnRow       /* Write the revised row estimate here */
 111624   Index *p = pBuilder->pNew->u.btree.pIndex;
 111625   int nRecValid = pBuilder->nRecValid;
 111626   int rc = SQLITE_OK;     /* Subfunction return code */
 111627   tRowcnt nEst;           /* Number of rows for a single term */
 111628   tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
 111629   int i;                  /* Loop counter */
 111631   assert( p->aSample!=0 );
 111632   for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
 111633     nEst = p->aiRowEst[0];
 111634     rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
 111635     nRowEst += nEst;
 111636     pBuilder->nRecValid = nRecValid;
 111639   if( rc==SQLITE_OK ){
 111640     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
 111641     *pnRow = nRowEst;
 111642     WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
 111644   assert( pBuilder->nRecValid==nRecValid );
 111645   return rc;
 111647 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 111650 ** Disable a term in the WHERE clause.  Except, do not disable the term
 111651 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
 111652 ** or USING clause of that join.
 111654 ** Consider the term t2.z='ok' in the following queries:
 111656 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
 111657 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
 111658 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
 111660 ** The t2.z='ok' is disabled in the in (2) because it originates
 111661 ** in the ON clause.  The term is disabled in (3) because it is not part
 111662 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
 111664 ** Disabling a term causes that term to not be tested in the inner loop
 111665 ** of the join.  Disabling is an optimization.  When terms are satisfied
 111666 ** by indices, we disable them to prevent redundant tests in the inner
 111667 ** loop.  We would get the correct results if nothing were ever disabled,
 111668 ** but joins might run a little slower.  The trick is to disable as much
 111669 ** as we can without disabling too much.  If we disabled in (1), we'd get
 111670 ** the wrong answer.  See ticket #813.
 111672 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
 111673   if( pTerm
 111674       && (pTerm->wtFlags & TERM_CODED)==0
 111675       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
 111676       && (pLevel->notReady & pTerm->prereqAll)==0
 111678     pTerm->wtFlags |= TERM_CODED;
 111679     if( pTerm->iParent>=0 ){
 111680       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
 111681       if( (--pOther->nChild)==0 ){
 111682         disableTerm(pLevel, pOther);
 111689 ** Code an OP_Affinity opcode to apply the column affinity string zAff
 111690 ** to the n registers starting at base. 
 111692 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
 111693 ** beginning and end of zAff are ignored.  If all entries in zAff are
 111694 ** SQLITE_AFF_NONE, then no code gets generated.
 111696 ** This routine makes its own copy of zAff so that the caller is free
 111697 ** to modify zAff after this routine returns.
 111699 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
 111700   Vdbe *v = pParse->pVdbe;
 111701   if( zAff==0 ){
 111702     assert( pParse->db->mallocFailed );
 111703     return;
 111705   assert( v!=0 );
 111707   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
 111708   ** and end of the affinity string.
 111710   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
 111711     n--;
 111712     base++;
 111713     zAff++;
 111715   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
 111716     n--;
 111719   /* Code the OP_Affinity opcode if there is anything left to do. */
 111720   if( n>0 ){
 111721     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
 111722     sqlite3VdbeChangeP4(v, -1, zAff, n);
 111723     sqlite3ExprCacheAffinityChange(pParse, base, n);
 111729 ** Generate code for a single equality term of the WHERE clause.  An equality
 111730 ** term can be either X=expr or X IN (...).   pTerm is the term to be 
 111731 ** coded.
 111733 ** The current value for the constraint is left in register iReg.
 111735 ** For a constraint of the form X=expr, the expression is evaluated and its
 111736 ** result is left on the stack.  For constraints of the form X IN (...)
 111737 ** this routine sets up a loop that will iterate over all values of X.
 111739 static int codeEqualityTerm(
 111740   Parse *pParse,      /* The parsing context */
 111741   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
 111742   WhereLevel *pLevel, /* The level of the FROM clause we are working on */
 111743   int iEq,            /* Index of the equality term within this level */
 111744   int bRev,           /* True for reverse-order IN operations */
 111745   int iTarget         /* Attempt to leave results in this register */
 111747   Expr *pX = pTerm->pExpr;
 111748   Vdbe *v = pParse->pVdbe;
 111749   int iReg;                  /* Register holding results */
 111751   assert( iTarget>0 );
 111752   if( pX->op==TK_EQ ){
 111753     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
 111754   }else if( pX->op==TK_ISNULL ){
 111755     iReg = iTarget;
 111756     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
 111757 #ifndef SQLITE_OMIT_SUBQUERY
 111758   }else{
 111759     int eType;
 111760     int iTab;
 111761     struct InLoop *pIn;
 111762     WhereLoop *pLoop = pLevel->pWLoop;
 111764     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
 111765       && pLoop->u.btree.pIndex!=0
 111766       && pLoop->u.btree.pIndex->aSortOrder[iEq]
 111768       testcase( iEq==0 );
 111769       testcase( bRev );
 111770       bRev = !bRev;
 111772     assert( pX->op==TK_IN );
 111773     iReg = iTarget;
 111774     eType = sqlite3FindInIndex(pParse, pX, 0);
 111775     if( eType==IN_INDEX_INDEX_DESC ){
 111776       testcase( bRev );
 111777       bRev = !bRev;
 111779     iTab = pX->iTable;
 111780     sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
 111781     VdbeCoverageIf(v, bRev);
 111782     VdbeCoverageIf(v, !bRev);
 111783     assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
 111784     pLoop->wsFlags |= WHERE_IN_ABLE;
 111785     if( pLevel->u.in.nIn==0 ){
 111786       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
 111788     pLevel->u.in.nIn++;
 111789     pLevel->u.in.aInLoop =
 111790        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
 111791                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
 111792     pIn = pLevel->u.in.aInLoop;
 111793     if( pIn ){
 111794       pIn += pLevel->u.in.nIn - 1;
 111795       pIn->iCur = iTab;
 111796       if( eType==IN_INDEX_ROWID ){
 111797         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
 111798       }else{
 111799         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
 111801       pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
 111802       sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
 111803     }else{
 111804       pLevel->u.in.nIn = 0;
 111806 #endif
 111808   disableTerm(pLevel, pTerm);
 111809   return iReg;
 111813 ** Generate code that will evaluate all == and IN constraints for an
 111814 ** index scan.
 111816 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
 111817 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
 111818 ** The index has as many as three equality constraints, but in this
 111819 ** example, the third "c" value is an inequality.  So only two 
 111820 ** constraints are coded.  This routine will generate code to evaluate
 111821 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
 111822 ** in consecutive registers and the index of the first register is returned.
 111824 ** In the example above nEq==2.  But this subroutine works for any value
 111825 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
 111826 ** The only thing it does is allocate the pLevel->iMem memory cell and
 111827 ** compute the affinity string.
 111829 ** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
 111830 ** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
 111831 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
 111832 ** occurs after the nEq quality constraints.
 111834 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
 111835 ** the index of the first memory cell in that range. The code that
 111836 ** calls this routine will use that memory range to store keys for
 111837 ** start and termination conditions of the loop.
 111838 ** key value of the loop.  If one or more IN operators appear, then
 111839 ** this routine allocates an additional nEq memory cells for internal
 111840 ** use.
 111842 ** Before returning, *pzAff is set to point to a buffer containing a
 111843 ** copy of the column affinity string of the index allocated using
 111844 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
 111845 ** with equality constraints that use NONE affinity are set to
 111846 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
 111848 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
 111849 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
 111851 ** In the example above, the index on t1(a) has TEXT affinity. But since
 111852 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
 111853 ** no conversion should be attempted before using a t2.b value as part of
 111854 ** a key to search the index. Hence the first byte in the returned affinity
 111855 ** string in this example would be set to SQLITE_AFF_NONE.
 111857 static int codeAllEqualityTerms(
 111858   Parse *pParse,        /* Parsing context */
 111859   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
 111860   int bRev,             /* Reverse the order of IN operators */
 111861   int nExtraReg,        /* Number of extra registers to allocate */
 111862   char **pzAff          /* OUT: Set to point to affinity string */
 111864   u16 nEq;                      /* The number of == or IN constraints to code */
 111865   u16 nSkip;                    /* Number of left-most columns to skip */
 111866   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
 111867   Index *pIdx;                  /* The index being used for this loop */
 111868   WhereTerm *pTerm;             /* A single constraint term */
 111869   WhereLoop *pLoop;             /* The WhereLoop object */
 111870   int j;                        /* Loop counter */
 111871   int regBase;                  /* Base register */
 111872   int nReg;                     /* Number of registers to allocate */
 111873   char *zAff;                   /* Affinity string to return */
 111875   /* This module is only called on query plans that use an index. */
 111876   pLoop = pLevel->pWLoop;
 111877   assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
 111878   nEq = pLoop->u.btree.nEq;
 111879   nSkip = pLoop->u.btree.nSkip;
 111880   pIdx = pLoop->u.btree.pIndex;
 111881   assert( pIdx!=0 );
 111883   /* Figure out how many memory cells we will need then allocate them.
 111885   regBase = pParse->nMem + 1;
 111886   nReg = pLoop->u.btree.nEq + nExtraReg;
 111887   pParse->nMem += nReg;
 111889   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
 111890   if( !zAff ){
 111891     pParse->db->mallocFailed = 1;
 111894   if( nSkip ){
 111895     int iIdxCur = pLevel->iIdxCur;
 111896     sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
 111897     VdbeCoverageIf(v, bRev==0);
 111898     VdbeCoverageIf(v, bRev!=0);
 111899     VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
 111900     j = sqlite3VdbeAddOp0(v, OP_Goto);
 111901     pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
 111902                             iIdxCur, 0, regBase, nSkip);
 111903     VdbeCoverageIf(v, bRev==0);
 111904     VdbeCoverageIf(v, bRev!=0);
 111905     sqlite3VdbeJumpHere(v, j);
 111906     for(j=0; j<nSkip; j++){
 111907       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
 111908       assert( pIdx->aiColumn[j]>=0 );
 111909       VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
 111913   /* Evaluate the equality constraints
 111915   assert( zAff==0 || (int)strlen(zAff)>=nEq );
 111916   for(j=nSkip; j<nEq; j++){
 111917     int r1;
 111918     pTerm = pLoop->aLTerm[j];
 111919     assert( pTerm!=0 );
 111920     /* The following testcase is true for indices with redundant columns. 
 111921     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
 111922     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
 111923     testcase( pTerm->wtFlags & TERM_VIRTUAL );
 111924     r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
 111925     if( r1!=regBase+j ){
 111926       if( nReg==1 ){
 111927         sqlite3ReleaseTempReg(pParse, regBase);
 111928         regBase = r1;
 111929       }else{
 111930         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
 111933     testcase( pTerm->eOperator & WO_ISNULL );
 111934     testcase( pTerm->eOperator & WO_IN );
 111935     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
 111936       Expr *pRight = pTerm->pExpr->pRight;
 111937       if( sqlite3ExprCanBeNull(pRight) ){
 111938         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
 111939         VdbeCoverage(v);
 111941       if( zAff ){
 111942         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
 111943           zAff[j] = SQLITE_AFF_NONE;
 111945         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
 111946           zAff[j] = SQLITE_AFF_NONE;
 111951   *pzAff = zAff;
 111952   return regBase;
 111955 #ifndef SQLITE_OMIT_EXPLAIN
 111957 ** This routine is a helper for explainIndexRange() below
 111959 ** pStr holds the text of an expression that we are building up one term
 111960 ** at a time.  This routine adds a new term to the end of the expression.
 111961 ** Terms are separated by AND so add the "AND" text for second and subsequent
 111962 ** terms only.
 111964 static void explainAppendTerm(
 111965   StrAccum *pStr,             /* The text expression being built */
 111966   int iTerm,                  /* Index of this term.  First is zero */
 111967   const char *zColumn,        /* Name of the column */
 111968   const char *zOp             /* Name of the operator */
 111970   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
 111971   sqlite3StrAccumAppendAll(pStr, zColumn);
 111972   sqlite3StrAccumAppend(pStr, zOp, 1);
 111973   sqlite3StrAccumAppend(pStr, "?", 1);
 111977 ** Argument pLevel describes a strategy for scanning table pTab. This 
 111978 ** function returns a pointer to a string buffer containing a description
 111979 ** of the subset of table rows scanned by the strategy in the form of an
 111980 ** SQL expression. Or, if all rows are scanned, NULL is returned.
 111982 ** For example, if the query:
 111984 **   SELECT * FROM t1 WHERE a=1 AND b>2;
 111986 ** is run and there is an index on (a, b), then this function returns a
 111987 ** string similar to:
 111989 **   "a=? AND b>?"
 111991 ** The returned pointer points to memory obtained from sqlite3DbMalloc().
 111992 ** It is the responsibility of the caller to free the buffer when it is
 111993 ** no longer required.
 111995 static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){
 111996   Index *pIndex = pLoop->u.btree.pIndex;
 111997   u16 nEq = pLoop->u.btree.nEq;
 111998   u16 nSkip = pLoop->u.btree.nSkip;
 111999   int i, j;
 112000   Column *aCol = pTab->aCol;
 112001   i16 *aiColumn = pIndex->aiColumn;
 112002   StrAccum txt;
 112004   if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
 112005     return 0;
 112007   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
 112008   txt.db = db;
 112009   sqlite3StrAccumAppend(&txt, " (", 2);
 112010   for(i=0; i<nEq; i++){
 112011     char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
 112012     if( i>=nSkip ){
 112013       explainAppendTerm(&txt, i, z, "=");
 112014     }else{
 112015       if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
 112016       sqlite3StrAccumAppend(&txt, "ANY(", 4);
 112017       sqlite3StrAccumAppendAll(&txt, z);
 112018       sqlite3StrAccumAppend(&txt, ")", 1);
 112022   j = i;
 112023   if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
 112024     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
 112025     explainAppendTerm(&txt, i++, z, ">");
 112027   if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
 112028     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
 112029     explainAppendTerm(&txt, i, z, "<");
 112031   sqlite3StrAccumAppend(&txt, ")", 1);
 112032   return sqlite3StrAccumFinish(&txt);
 112036 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
 112037 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
 112038 ** record is added to the output to describe the table scan strategy in 
 112039 ** pLevel.
 112041 static void explainOneScan(
 112042   Parse *pParse,                  /* Parse context */
 112043   SrcList *pTabList,              /* Table list this loop refers to */
 112044   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
 112045   int iLevel,                     /* Value for "level" column of output */
 112046   int iFrom,                      /* Value for "from" column of output */
 112047   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
 112049 #ifndef SQLITE_DEBUG
 112050   if( pParse->explain==2 )
 112051 #endif
 112053     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
 112054     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
 112055     sqlite3 *db = pParse->db;     /* Database handle */
 112056     char *zMsg;                   /* Text to add to EQP output */
 112057     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
 112058     int isSearch;                 /* True for a SEARCH. False for SCAN. */
 112059     WhereLoop *pLoop;             /* The controlling WhereLoop object */
 112060     u32 flags;                    /* Flags that describe this loop */
 112062     pLoop = pLevel->pWLoop;
 112063     flags = pLoop->wsFlags;
 112064     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
 112066     isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
 112067             || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
 112068             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
 112070     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
 112071     if( pItem->pSelect ){
 112072       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
 112073     }else{
 112074       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
 112077     if( pItem->zAlias ){
 112078       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
 112080     if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
 112081      && ALWAYS(pLoop->u.btree.pIndex!=0)
 112083       char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
 112084       zMsg = sqlite3MAppendf(db, zMsg,
 112085                ((flags & WHERE_AUTO_INDEX) ? 
 112086                    "%s USING AUTOMATIC %sINDEX%.0s%s" :
 112087                    "%s USING %sINDEX %s%s"), 
 112088                zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
 112089                pLoop->u.btree.pIndex->zName, zWhere);
 112090       sqlite3DbFree(db, zWhere);
 112091     }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
 112092       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
 112094       if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
 112095         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
 112096       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
 112097         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
 112098       }else if( flags&WHERE_BTM_LIMIT ){
 112099         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
 112100       }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){
 112101         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
 112104 #ifndef SQLITE_OMIT_VIRTUALTABLE
 112105     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
 112106       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
 112107                   pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
 112109 #endif
 112110     zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg);
 112111     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
 112114 #else
 112115 # define explainOneScan(u,v,w,x,y,z)
 112116 #endif /* SQLITE_OMIT_EXPLAIN */
 112120 ** Generate code for the start of the iLevel-th loop in the WHERE clause
 112121 ** implementation described by pWInfo.
 112123 static Bitmask codeOneLoopStart(
 112124   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
 112125   int iLevel,          /* Which level of pWInfo->a[] should be coded */
 112126   Bitmask notReady     /* Which tables are currently available */
 112128   int j, k;            /* Loop counters */
 112129   int iCur;            /* The VDBE cursor for the table */
 112130   int addrNxt;         /* Where to jump to continue with the next IN case */
 112131   int omitTable;       /* True if we use the index only */
 112132   int bRev;            /* True if we need to scan in reverse order */
 112133   WhereLevel *pLevel;  /* The where level to be coded */
 112134   WhereLoop *pLoop;    /* The WhereLoop object being coded */
 112135   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
 112136   WhereTerm *pTerm;               /* A WHERE clause term */
 112137   Parse *pParse;                  /* Parsing context */
 112138   sqlite3 *db;                    /* Database connection */
 112139   Vdbe *v;                        /* The prepared stmt under constructions */
 112140   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
 112141   int addrBrk;                    /* Jump here to break out of the loop */
 112142   int addrCont;                   /* Jump here to continue with next cycle */
 112143   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
 112144   int iReleaseReg = 0;      /* Temp register to free before returning */
 112146   pParse = pWInfo->pParse;
 112147   v = pParse->pVdbe;
 112148   pWC = &pWInfo->sWC;
 112149   db = pParse->db;
 112150   pLevel = &pWInfo->a[iLevel];
 112151   pLoop = pLevel->pWLoop;
 112152   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
 112153   iCur = pTabItem->iCursor;
 112154   pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
 112155   bRev = (pWInfo->revMask>>iLevel)&1;
 112156   omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
 112157            && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
 112158   VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
 112160   /* Create labels for the "break" and "continue" instructions
 112161   ** for the current loop.  Jump to addrBrk to break out of a loop.
 112162   ** Jump to cont to go immediately to the next iteration of the
 112163   ** loop.
 112165   ** When there is an IN operator, we also have a "addrNxt" label that
 112166   ** means to continue with the next IN value combination.  When
 112167   ** there are no IN operators in the constraints, the "addrNxt" label
 112168   ** is the same as "addrBrk".
 112170   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
 112171   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
 112173   /* If this is the right table of a LEFT OUTER JOIN, allocate and
 112174   ** initialize a memory cell that records if this table matches any
 112175   ** row of the left table of the join.
 112177   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
 112178     pLevel->iLeftJoin = ++pParse->nMem;
 112179     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
 112180     VdbeComment((v, "init LEFT JOIN no-match flag"));
 112183   /* Special case of a FROM clause subquery implemented as a co-routine */
 112184   if( pTabItem->viaCoroutine ){
 112185     int regYield = pTabItem->regReturn;
 112186     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
 112187     pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
 112188     VdbeCoverage(v);
 112189     VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
 112190     pLevel->op = OP_Goto;
 112191   }else
 112193 #ifndef SQLITE_OMIT_VIRTUALTABLE
 112194   if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
 112195     /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
 112196     **          to access the data.
 112198     int iReg;   /* P3 Value for OP_VFilter */
 112199     int addrNotFound;
 112200     int nConstraint = pLoop->nLTerm;
 112202     sqlite3ExprCachePush(pParse);
 112203     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
 112204     addrNotFound = pLevel->addrBrk;
 112205     for(j=0; j<nConstraint; j++){
 112206       int iTarget = iReg+j+2;
 112207       pTerm = pLoop->aLTerm[j];
 112208       if( pTerm==0 ) continue;
 112209       if( pTerm->eOperator & WO_IN ){
 112210         codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
 112211         addrNotFound = pLevel->addrNxt;
 112212       }else{
 112213         sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
 112216     sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
 112217     sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
 112218     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
 112219                       pLoop->u.vtab.idxStr,
 112220                       pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
 112221     VdbeCoverage(v);
 112222     pLoop->u.vtab.needFree = 0;
 112223     for(j=0; j<nConstraint && j<16; j++){
 112224       if( (pLoop->u.vtab.omitMask>>j)&1 ){
 112225         disableTerm(pLevel, pLoop->aLTerm[j]);
 112228     pLevel->op = OP_VNext;
 112229     pLevel->p1 = iCur;
 112230     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
 112231     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
 112232     sqlite3ExprCachePop(pParse, 1);
 112233   }else
 112234 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 112236   if( (pLoop->wsFlags & WHERE_IPK)!=0
 112237    && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
 112239     /* Case 2:  We can directly reference a single row using an
 112240     **          equality comparison against the ROWID field.  Or
 112241     **          we reference multiple rows using a "rowid IN (...)"
 112242     **          construct.
 112244     assert( pLoop->u.btree.nEq==1 );
 112245     pTerm = pLoop->aLTerm[0];
 112246     assert( pTerm!=0 );
 112247     assert( pTerm->pExpr!=0 );
 112248     assert( omitTable==0 );
 112249     testcase( pTerm->wtFlags & TERM_VIRTUAL );
 112250     iReleaseReg = ++pParse->nMem;
 112251     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
 112252     if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
 112253     addrNxt = pLevel->addrNxt;
 112254     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
 112255     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
 112256     VdbeCoverage(v);
 112257     sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
 112258     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 112259     VdbeComment((v, "pk"));
 112260     pLevel->op = OP_Noop;
 112261   }else if( (pLoop->wsFlags & WHERE_IPK)!=0
 112262          && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
 112264     /* Case 3:  We have an inequality comparison against the ROWID field.
 112266     int testOp = OP_Noop;
 112267     int start;
 112268     int memEndValue = 0;
 112269     WhereTerm *pStart, *pEnd;
 112271     assert( omitTable==0 );
 112272     j = 0;
 112273     pStart = pEnd = 0;
 112274     if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
 112275     if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
 112276     assert( pStart!=0 || pEnd!=0 );
 112277     if( bRev ){
 112278       pTerm = pStart;
 112279       pStart = pEnd;
 112280       pEnd = pTerm;
 112282     if( pStart ){
 112283       Expr *pX;             /* The expression that defines the start bound */
 112284       int r1, rTemp;        /* Registers for holding the start boundary */
 112286       /* The following constant maps TK_xx codes into corresponding 
 112287       ** seek opcodes.  It depends on a particular ordering of TK_xx
 112289       const u8 aMoveOp[] = {
 112290            /* TK_GT */  OP_SeekGT,
 112291            /* TK_LE */  OP_SeekLE,
 112292            /* TK_LT */  OP_SeekLT,
 112293            /* TK_GE */  OP_SeekGE
 112295       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
 112296       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
 112297       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
 112299       assert( (pStart->wtFlags & TERM_VNULL)==0 );
 112300       testcase( pStart->wtFlags & TERM_VIRTUAL );
 112301       pX = pStart->pExpr;
 112302       assert( pX!=0 );
 112303       testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
 112304       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
 112305       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
 112306       VdbeComment((v, "pk"));
 112307       VdbeCoverageIf(v, pX->op==TK_GT);
 112308       VdbeCoverageIf(v, pX->op==TK_LE);
 112309       VdbeCoverageIf(v, pX->op==TK_LT);
 112310       VdbeCoverageIf(v, pX->op==TK_GE);
 112311       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
 112312       sqlite3ReleaseTempReg(pParse, rTemp);
 112313       disableTerm(pLevel, pStart);
 112314     }else{
 112315       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
 112316       VdbeCoverageIf(v, bRev==0);
 112317       VdbeCoverageIf(v, bRev!=0);
 112319     if( pEnd ){
 112320       Expr *pX;
 112321       pX = pEnd->pExpr;
 112322       assert( pX!=0 );
 112323       assert( (pEnd->wtFlags & TERM_VNULL)==0 );
 112324       testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
 112325       testcase( pEnd->wtFlags & TERM_VIRTUAL );
 112326       memEndValue = ++pParse->nMem;
 112327       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
 112328       if( pX->op==TK_LT || pX->op==TK_GT ){
 112329         testOp = bRev ? OP_Le : OP_Ge;
 112330       }else{
 112331         testOp = bRev ? OP_Lt : OP_Gt;
 112333       disableTerm(pLevel, pEnd);
 112335     start = sqlite3VdbeCurrentAddr(v);
 112336     pLevel->op = bRev ? OP_Prev : OP_Next;
 112337     pLevel->p1 = iCur;
 112338     pLevel->p2 = start;
 112339     assert( pLevel->p5==0 );
 112340     if( testOp!=OP_Noop ){
 112341       iRowidReg = ++pParse->nMem;
 112342       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
 112343       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 112344       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
 112345       VdbeCoverageIf(v, testOp==OP_Le);
 112346       VdbeCoverageIf(v, testOp==OP_Lt);
 112347       VdbeCoverageIf(v, testOp==OP_Ge);
 112348       VdbeCoverageIf(v, testOp==OP_Gt);
 112349       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
 112351   }else if( pLoop->wsFlags & WHERE_INDEXED ){
 112352     /* Case 4: A scan using an index.
 112354     **         The WHERE clause may contain zero or more equality 
 112355     **         terms ("==" or "IN" operators) that refer to the N
 112356     **         left-most columns of the index. It may also contain
 112357     **         inequality constraints (>, <, >= or <=) on the indexed
 112358     **         column that immediately follows the N equalities. Only 
 112359     **         the right-most column can be an inequality - the rest must
 112360     **         use the "==" and "IN" operators. For example, if the 
 112361     **         index is on (x,y,z), then the following clauses are all 
 112362     **         optimized:
 112364     **            x=5
 112365     **            x=5 AND y=10
 112366     **            x=5 AND y<10
 112367     **            x=5 AND y>5 AND y<10
 112368     **            x=5 AND y=5 AND z<=10
 112370     **         The z<10 term of the following cannot be used, only
 112371     **         the x=5 term:
 112373     **            x=5 AND z<10
 112375     **         N may be zero if there are inequality constraints.
 112376     **         If there are no inequality constraints, then N is at
 112377     **         least one.
 112379     **         This case is also used when there are no WHERE clause
 112380     **         constraints but an index is selected anyway, in order
 112381     **         to force the output order to conform to an ORDER BY.
 112383     static const u8 aStartOp[] = {
 112386       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
 112387       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
 112388       OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
 112389       OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
 112390       OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
 112391       OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
 112393     static const u8 aEndOp[] = {
 112394       OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
 112395       OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
 112396       OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
 112397       OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
 112399     u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
 112400     int regBase;                 /* Base register holding constraint values */
 112401     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
 112402     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
 112403     int startEq;                 /* True if range start uses ==, >= or <= */
 112404     int endEq;                   /* True if range end uses ==, >= or <= */
 112405     int start_constraints;       /* Start of range is constrained */
 112406     int nConstraint;             /* Number of constraint terms */
 112407     Index *pIdx;                 /* The index we will be using */
 112408     int iIdxCur;                 /* The VDBE cursor for the index */
 112409     int nExtraReg = 0;           /* Number of extra registers needed */
 112410     int op;                      /* Instruction opcode */
 112411     char *zStartAff;             /* Affinity for start of range constraint */
 112412     char cEndAff = 0;            /* Affinity for end of range constraint */
 112413     u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
 112414     u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
 112416     pIdx = pLoop->u.btree.pIndex;
 112417     iIdxCur = pLevel->iIdxCur;
 112418     assert( nEq>=pLoop->u.btree.nSkip );
 112420     /* If this loop satisfies a sort order (pOrderBy) request that 
 112421     ** was passed to this function to implement a "SELECT min(x) ..." 
 112422     ** query, then the caller will only allow the loop to run for
 112423     ** a single iteration. This means that the first row returned
 112424     ** should not have a NULL value stored in 'x'. If column 'x' is
 112425     ** the first one after the nEq equality constraints in the index,
 112426     ** this requires some special handling.
 112428     if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
 112429      && (pWInfo->bOBSat!=0)
 112430      && (pIdx->nKeyCol>nEq)
 112432       assert( pLoop->u.btree.nSkip==0 );
 112433       bSeekPastNull = 1;
 112434       nExtraReg = 1;
 112437     /* Find any inequality constraint terms for the start and end 
 112438     ** of the range. 
 112440     j = nEq;
 112441     if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
 112442       pRangeStart = pLoop->aLTerm[j++];
 112443       nExtraReg = 1;
 112445     if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
 112446       pRangeEnd = pLoop->aLTerm[j++];
 112447       nExtraReg = 1;
 112448       if( pRangeStart==0
 112449        && (j = pIdx->aiColumn[nEq])>=0 
 112450        && pIdx->pTable->aCol[j].notNull==0
 112452         bSeekPastNull = 1;
 112455     assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
 112457     /* Generate code to evaluate all constraint terms using == or IN
 112458     ** and store the values of those terms in an array of registers
 112459     ** starting at regBase.
 112461     regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
 112462     assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
 112463     if( zStartAff ) cEndAff = zStartAff[nEq];
 112464     addrNxt = pLevel->addrNxt;
 112466     /* If we are doing a reverse order scan on an ascending index, or
 112467     ** a forward order scan on a descending index, interchange the 
 112468     ** start and end terms (pRangeStart and pRangeEnd).
 112470     if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
 112471      || (bRev && pIdx->nKeyCol==nEq)
 112473       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
 112474       SWAP(u8, bSeekPastNull, bStopAtNull);
 112477     testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
 112478     testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
 112479     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
 112480     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
 112481     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
 112482     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
 112483     start_constraints = pRangeStart || nEq>0;
 112485     /* Seek the index cursor to the start of the range. */
 112486     nConstraint = nEq;
 112487     if( pRangeStart ){
 112488       Expr *pRight = pRangeStart->pExpr->pRight;
 112489       sqlite3ExprCode(pParse, pRight, regBase+nEq);
 112490       if( (pRangeStart->wtFlags & TERM_VNULL)==0
 112491        && sqlite3ExprCanBeNull(pRight)
 112493         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
 112494         VdbeCoverage(v);
 112496       if( zStartAff ){
 112497         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
 112498           /* Since the comparison is to be performed with no conversions
 112499           ** applied to the operands, set the affinity to apply to pRight to 
 112500           ** SQLITE_AFF_NONE.  */
 112501           zStartAff[nEq] = SQLITE_AFF_NONE;
 112503         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
 112504           zStartAff[nEq] = SQLITE_AFF_NONE;
 112507       nConstraint++;
 112508       testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
 112509     }else if( bSeekPastNull ){
 112510       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
 112511       nConstraint++;
 112512       startEq = 0;
 112513       start_constraints = 1;
 112515     codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
 112516     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
 112517     assert( op!=0 );
 112518     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
 112519     VdbeCoverage(v);
 112520     VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
 112521     VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
 112522     VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
 112523     VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
 112524     VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
 112525     VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
 112527     /* Load the value for the inequality constraint at the end of the
 112528     ** range (if any).
 112530     nConstraint = nEq;
 112531     if( pRangeEnd ){
 112532       Expr *pRight = pRangeEnd->pExpr->pRight;
 112533       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
 112534       sqlite3ExprCode(pParse, pRight, regBase+nEq);
 112535       if( (pRangeEnd->wtFlags & TERM_VNULL)==0
 112536        && sqlite3ExprCanBeNull(pRight)
 112538         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
 112539         VdbeCoverage(v);
 112541       if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
 112542        && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
 112544         codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
 112546       nConstraint++;
 112547       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
 112548     }else if( bStopAtNull ){
 112549       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
 112550       endEq = 0;
 112551       nConstraint++;
 112553     sqlite3DbFree(db, zStartAff);
 112555     /* Top of the loop body */
 112556     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
 112558     /* Check if the index cursor is past the end of the range. */
 112559     if( nConstraint ){
 112560       op = aEndOp[bRev*2 + endEq];
 112561       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
 112562       testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
 112563       testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
 112564       testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
 112565       testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
 112568     /* Seek the table cursor, if required */
 112569     disableTerm(pLevel, pRangeStart);
 112570     disableTerm(pLevel, pRangeEnd);
 112571     if( omitTable ){
 112572       /* pIdx is a covering index.  No need to access the main table. */
 112573     }else if( HasRowid(pIdx->pTable) ){
 112574       iRowidReg = ++pParse->nMem;
 112575       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
 112576       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
 112577       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
 112578     }else{
 112579       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
 112580       iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
 112581       for(j=0; j<pPk->nKeyCol; j++){
 112582         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
 112583         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
 112585       sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
 112586                            iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
 112589     /* Record the instruction used to terminate the loop. Disable 
 112590     ** WHERE clause terms made redundant by the index range scan.
 112592     if( pLoop->wsFlags & WHERE_ONEROW ){
 112593       pLevel->op = OP_Noop;
 112594     }else if( bRev ){
 112595       pLevel->op = OP_Prev;
 112596     }else{
 112597       pLevel->op = OP_Next;
 112599     pLevel->p1 = iIdxCur;
 112600     assert( (WHERE_UNQ_WANTED>>16)==1 );
 112601     pLevel->p3 = (pLoop->wsFlags>>16)&1;
 112602     if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
 112603       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 112604     }else{
 112605       assert( pLevel->p5==0 );
 112607   }else
 112609 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
 112610   if( pLoop->wsFlags & WHERE_MULTI_OR ){
 112611     /* Case 5:  Two or more separately indexed terms connected by OR
 112613     ** Example:
 112615     **   CREATE TABLE t1(a,b,c,d);
 112616     **   CREATE INDEX i1 ON t1(a);
 112617     **   CREATE INDEX i2 ON t1(b);
 112618     **   CREATE INDEX i3 ON t1(c);
 112620     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
 112622     ** In the example, there are three indexed terms connected by OR.
 112623     ** The top of the loop looks like this:
 112625     **          Null       1                # Zero the rowset in reg 1
 112627     ** Then, for each indexed term, the following. The arguments to
 112628     ** RowSetTest are such that the rowid of the current row is inserted
 112629     ** into the RowSet. If it is already present, control skips the
 112630     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
 112632     **        sqlite3WhereBegin(<term>)
 112633     **          RowSetTest                  # Insert rowid into rowset
 112634     **          Gosub      2 A
 112635     **        sqlite3WhereEnd()
 112637     ** Following the above, code to terminate the loop. Label A, the target
 112638     ** of the Gosub above, jumps to the instruction right after the Goto.
 112640     **          Null       1                # Zero the rowset in reg 1
 112641     **          Goto       B                # The loop is finished.
 112643     **       A: <loop body>                 # Return data, whatever.
 112645     **          Return     2                # Jump back to the Gosub
 112647     **       B: <after the loop>
 112650     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
 112651     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
 112652     Index *pCov = 0;             /* Potential covering index (or NULL) */
 112653     int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
 112655     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
 112656     int regRowset = 0;                        /* Register for RowSet object */
 112657     int regRowid = 0;                         /* Register holding rowid */
 112658     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
 112659     int iRetInit;                             /* Address of regReturn init */
 112660     int untestedTerms = 0;             /* Some terms not completely tested */
 112661     int ii;                            /* Loop counter */
 112662     Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
 112664     pTerm = pLoop->aLTerm[0];
 112665     assert( pTerm!=0 );
 112666     assert( pTerm->eOperator & WO_OR );
 112667     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
 112668     pOrWc = &pTerm->u.pOrInfo->wc;
 112669     pLevel->op = OP_Return;
 112670     pLevel->p1 = regReturn;
 112672     /* Set up a new SrcList in pOrTab containing the table being scanned
 112673     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
 112674     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
 112676     if( pWInfo->nLevel>1 ){
 112677       int nNotReady;                 /* The number of notReady tables */
 112678       struct SrcList_item *origSrc;     /* Original list of tables */
 112679       nNotReady = pWInfo->nLevel - iLevel - 1;
 112680       pOrTab = sqlite3StackAllocRaw(db,
 112681                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
 112682       if( pOrTab==0 ) return notReady;
 112683       pOrTab->nAlloc = (u8)(nNotReady + 1);
 112684       pOrTab->nSrc = pOrTab->nAlloc;
 112685       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
 112686       origSrc = pWInfo->pTabList->a;
 112687       for(k=1; k<=nNotReady; k++){
 112688         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
 112690     }else{
 112691       pOrTab = pWInfo->pTabList;
 112694     /* Initialize the rowset register to contain NULL. An SQL NULL is 
 112695     ** equivalent to an empty rowset.
 112697     ** Also initialize regReturn to contain the address of the instruction 
 112698     ** immediately following the OP_Return at the bottom of the loop. This
 112699     ** is required in a few obscure LEFT JOIN cases where control jumps
 112700     ** over the top of the loop into the body of it. In this case the 
 112701     ** correct response for the end-of-loop code (the OP_Return) is to 
 112702     ** fall through to the next instruction, just as an OP_Next does if
 112703     ** called on an uninitialized cursor.
 112705     if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
 112706       regRowset = ++pParse->nMem;
 112707       regRowid = ++pParse->nMem;
 112708       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
 112710     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
 112712     /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
 112713     ** Then for every term xN, evaluate as the subexpression: xN AND z
 112714     ** That way, terms in y that are factored into the disjunction will
 112715     ** be picked up by the recursive calls to sqlite3WhereBegin() below.
 112717     ** Actually, each subexpression is converted to "xN AND w" where w is
 112718     ** the "interesting" terms of z - terms that did not originate in the
 112719     ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
 112720     ** indices.
 112722     ** This optimization also only applies if the (x1 OR x2 OR ...) term
 112723     ** is not contained in the ON clause of a LEFT JOIN.
 112724     ** See ticket http://www.sqlite.org/src/info/f2369304e4
 112726     if( pWC->nTerm>1 ){
 112727       int iTerm;
 112728       for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
 112729         Expr *pExpr = pWC->a[iTerm].pExpr;
 112730         if( &pWC->a[iTerm] == pTerm ) continue;
 112731         if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
 112732         testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
 112733         testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
 112734         if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
 112735         if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
 112736         pExpr = sqlite3ExprDup(db, pExpr, 0);
 112737         pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
 112739       if( pAndExpr ){
 112740         pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
 112744     for(ii=0; ii<pOrWc->nTerm; ii++){
 112745       WhereTerm *pOrTerm = &pOrWc->a[ii];
 112746       if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
 112747         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
 112748         Expr *pOrExpr = pOrTerm->pExpr;
 112749         if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
 112750           pAndExpr->pLeft = pOrExpr;
 112751           pOrExpr = pAndExpr;
 112753         /* Loop through table entries that match term pOrTerm. */
 112754         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
 112755                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
 112756                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
 112757         assert( pSubWInfo || pParse->nErr || db->mallocFailed );
 112758         if( pSubWInfo ){
 112759           WhereLoop *pSubLoop;
 112760           explainOneScan(
 112761               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
 112763           if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
 112764             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
 112765             int r;
 112766             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
 112767                                          regRowid, 0);
 112768             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
 112769                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
 112770             VdbeCoverage(v);
 112772           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
 112774           /* The pSubWInfo->untestedTerms flag means that this OR term
 112775           ** contained one or more AND term from a notReady table.  The
 112776           ** terms from the notReady table could not be tested and will
 112777           ** need to be tested later.
 112779           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
 112781           /* If all of the OR-connected terms are optimized using the same
 112782           ** index, and the index is opened using the same cursor number
 112783           ** by each call to sqlite3WhereBegin() made by this loop, it may
 112784           ** be possible to use that index as a covering index.
 112786           ** If the call to sqlite3WhereBegin() above resulted in a scan that
 112787           ** uses an index, and this is either the first OR-connected term
 112788           ** processed or the index is the same as that used by all previous
 112789           ** terms, set pCov to the candidate covering index. Otherwise, set 
 112790           ** pCov to NULL to indicate that no candidate covering index will 
 112791           ** be available.
 112793           pSubLoop = pSubWInfo->a[0].pWLoop;
 112794           assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
 112795           if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
 112796            && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
 112798             assert( pSubWInfo->a[0].iIdxCur==iCovCur );
 112799             pCov = pSubLoop->u.btree.pIndex;
 112800           }else{
 112801             pCov = 0;
 112804           /* Finish the loop through table entries that match term pOrTerm. */
 112805           sqlite3WhereEnd(pSubWInfo);
 112809     pLevel->u.pCovidx = pCov;
 112810     if( pCov ) pLevel->iIdxCur = iCovCur;
 112811     if( pAndExpr ){
 112812       pAndExpr->pLeft = 0;
 112813       sqlite3ExprDelete(db, pAndExpr);
 112815     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
 112816     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
 112817     sqlite3VdbeResolveLabel(v, iLoopBody);
 112819     if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
 112820     if( !untestedTerms ) disableTerm(pLevel, pTerm);
 112821   }else
 112822 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
 112825     /* Case 6:  There is no usable index.  We must do a complete
 112826     **          scan of the entire table.
 112828     static const u8 aStep[] = { OP_Next, OP_Prev };
 112829     static const u8 aStart[] = { OP_Rewind, OP_Last };
 112830     assert( bRev==0 || bRev==1 );
 112831     if( pTabItem->isRecursive ){
 112832       /* Tables marked isRecursive have only a single row that is stored in
 112833       ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
 112834       pLevel->op = OP_Noop;
 112835     }else{
 112836       pLevel->op = aStep[bRev];
 112837       pLevel->p1 = iCur;
 112838       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
 112839       VdbeCoverageIf(v, bRev==0);
 112840       VdbeCoverageIf(v, bRev!=0);
 112841       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
 112845   /* Insert code to test every subexpression that can be completely
 112846   ** computed using the current set of tables.
 112848   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
 112849     Expr *pE;
 112850     testcase( pTerm->wtFlags & TERM_VIRTUAL );
 112851     testcase( pTerm->wtFlags & TERM_CODED );
 112852     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
 112853     if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
 112854       testcase( pWInfo->untestedTerms==0
 112855                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
 112856       pWInfo->untestedTerms = 1;
 112857       continue;
 112859     pE = pTerm->pExpr;
 112860     assert( pE!=0 );
 112861     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
 112862       continue;
 112864     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
 112865     pTerm->wtFlags |= TERM_CODED;
 112868   /* Insert code to test for implied constraints based on transitivity
 112869   ** of the "==" operator.
 112871   ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
 112872   ** and we are coding the t1 loop and the t2 loop has not yet coded,
 112873   ** then we cannot use the "t1.a=t2.b" constraint, but we can code
 112874   ** the implied "t1.a=123" constraint.
 112876   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
 112877     Expr *pE, *pEAlt;
 112878     WhereTerm *pAlt;
 112879     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
 112880     if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
 112881     if( pTerm->leftCursor!=iCur ) continue;
 112882     if( pLevel->iLeftJoin ) continue;
 112883     pE = pTerm->pExpr;
 112884     assert( !ExprHasProperty(pE, EP_FromJoin) );
 112885     assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
 112886     pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
 112887     if( pAlt==0 ) continue;
 112888     if( pAlt->wtFlags & (TERM_CODED) ) continue;
 112889     testcase( pAlt->eOperator & WO_EQ );
 112890     testcase( pAlt->eOperator & WO_IN );
 112891     VdbeModuleComment((v, "begin transitive constraint"));
 112892     pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
 112893     if( pEAlt ){
 112894       *pEAlt = *pAlt->pExpr;
 112895       pEAlt->pLeft = pE->pLeft;
 112896       sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
 112897       sqlite3StackFree(db, pEAlt);
 112901   /* For a LEFT OUTER JOIN, generate code that will record the fact that
 112902   ** at least one row of the right table has matched the left table.  
 112904   if( pLevel->iLeftJoin ){
 112905     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
 112906     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
 112907     VdbeComment((v, "record LEFT JOIN hit"));
 112908     sqlite3ExprCacheClear(pParse);
 112909     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
 112910       testcase( pTerm->wtFlags & TERM_VIRTUAL );
 112911       testcase( pTerm->wtFlags & TERM_CODED );
 112912       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
 112913       if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
 112914         assert( pWInfo->untestedTerms );
 112915         continue;
 112917       assert( pTerm->pExpr );
 112918       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
 112919       pTerm->wtFlags |= TERM_CODED;
 112923   return pLevel->notReady;
 112926 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
 112928 ** Generate "Explanation" text for a WhereTerm.
 112930 static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){
 112931   char zType[4];
 112932   memcpy(zType, "...", 4);
 112933   if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
 112934   if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
 112935   if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
 112936   sqlite3ExplainPrintf(v, "%s ", zType);
 112937   sqlite3ExplainExpr(v, pTerm->pExpr);
 112939 #endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */
 112942 #ifdef WHERETRACE_ENABLED
 112944 ** Print a WhereLoop object for debugging purposes
 112946 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
 112947   WhereInfo *pWInfo = pWC->pWInfo;
 112948   int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
 112949   struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
 112950   Table *pTab = pItem->pTab;
 112951   sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
 112952                      p->iTab, nb, p->maskSelf, nb, p->prereq);
 112953   sqlite3DebugPrintf(" %12s",
 112954                      pItem->zAlias ? pItem->zAlias : pTab->zName);
 112955   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
 112956      const char *zName;
 112957      if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
 112958       if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
 112959         int i = sqlite3Strlen30(zName) - 1;
 112960         while( zName[i]!='_' ) i--;
 112961         zName += i;
 112963       sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
 112964     }else{
 112965       sqlite3DebugPrintf("%20s","");
 112967   }else{
 112968     char *z;
 112969     if( p->u.vtab.idxStr ){
 112970       z = sqlite3_mprintf("(%d,\"%s\",%x)",
 112971                 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
 112972     }else{
 112973       z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
 112975     sqlite3DebugPrintf(" %-19s", z);
 112976     sqlite3_free(z);
 112978   sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm);
 112979   sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
 112980 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
 112981   /* If the 0x100 bit of wheretracing is set, then show all of the constraint
 112982   ** expressions in the WhereLoop.aLTerm[] array.
 112984   if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){  /* WHERETRACE 0x100 */
 112985     int i;
 112986     Vdbe *v = pWInfo->pParse->pVdbe;
 112987     sqlite3ExplainBegin(v);
 112988     for(i=0; i<p->nLTerm; i++){
 112989       WhereTerm *pTerm = p->aLTerm[i];
 112990       if( pTerm==0 ) continue;
 112991       sqlite3ExplainPrintf(v, "  (%d) #%-2d ", i+1, (int)(pTerm-pWC->a));
 112992       sqlite3ExplainPush(v);
 112993       whereExplainTerm(v, pTerm);
 112994       sqlite3ExplainPop(v);
 112995       sqlite3ExplainNL(v);
 112997     sqlite3ExplainFinish(v);
 112998     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
 113000 #endif
 113002 #endif
 113005 ** Convert bulk memory into a valid WhereLoop that can be passed
 113006 ** to whereLoopClear harmlessly.
 113008 static void whereLoopInit(WhereLoop *p){
 113009   p->aLTerm = p->aLTermSpace;
 113010   p->nLTerm = 0;
 113011   p->nLSlot = ArraySize(p->aLTermSpace);
 113012   p->wsFlags = 0;
 113016 ** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
 113018 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
 113019   if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
 113020     if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
 113021       sqlite3_free(p->u.vtab.idxStr);
 113022       p->u.vtab.needFree = 0;
 113023       p->u.vtab.idxStr = 0;
 113024     }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
 113025       sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
 113026       sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo);
 113027       sqlite3DbFree(db, p->u.btree.pIndex);
 113028       p->u.btree.pIndex = 0;
 113034 ** Deallocate internal memory used by a WhereLoop object
 113036 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
 113037   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
 113038   whereLoopClearUnion(db, p);
 113039   whereLoopInit(p);
 113043 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
 113045 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
 113046   WhereTerm **paNew;
 113047   if( p->nLSlot>=n ) return SQLITE_OK;
 113048   n = (n+7)&~7;
 113049   paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
 113050   if( paNew==0 ) return SQLITE_NOMEM;
 113051   memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
 113052   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
 113053   p->aLTerm = paNew;
 113054   p->nLSlot = n;
 113055   return SQLITE_OK;
 113059 ** Transfer content from the second pLoop into the first.
 113061 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
 113062   whereLoopClearUnion(db, pTo);
 113063   if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
 113064     memset(&pTo->u, 0, sizeof(pTo->u));
 113065     return SQLITE_NOMEM;
 113067   memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
 113068   memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
 113069   if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
 113070     pFrom->u.vtab.needFree = 0;
 113071   }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
 113072     pFrom->u.btree.pIndex = 0;
 113074   return SQLITE_OK;
 113078 ** Delete a WhereLoop object
 113080 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
 113081   whereLoopClear(db, p);
 113082   sqlite3DbFree(db, p);
 113086 ** Free a WhereInfo structure
 113088 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
 113089   if( ALWAYS(pWInfo) ){
 113090     whereClauseClear(&pWInfo->sWC);
 113091     while( pWInfo->pLoops ){
 113092       WhereLoop *p = pWInfo->pLoops;
 113093       pWInfo->pLoops = p->pNextLoop;
 113094       whereLoopDelete(db, p);
 113096     sqlite3DbFree(db, pWInfo);
 113101 ** Insert or replace a WhereLoop entry using the template supplied.
 113103 ** An existing WhereLoop entry might be overwritten if the new template
 113104 ** is better and has fewer dependencies.  Or the template will be ignored
 113105 ** and no insert will occur if an existing WhereLoop is faster and has
 113106 ** fewer dependencies than the template.  Otherwise a new WhereLoop is
 113107 ** added based on the template.
 113109 ** If pBuilder->pOrSet is not NULL then we only care about only the
 113110 ** prerequisites and rRun and nOut costs of the N best loops.  That
 113111 ** information is gathered in the pBuilder->pOrSet object.  This special
 113112 ** processing mode is used only for OR clause processing.
 113114 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
 113115 ** still might overwrite similar loops with the new template if the
 113116 ** template is better.  Loops may be overwritten if the following 
 113117 ** conditions are met:
 113119 **    (1)  They have the same iTab.
 113120 **    (2)  They have the same iSortIdx.
 113121 **    (3)  The template has same or fewer dependencies than the current loop
 113122 **    (4)  The template has the same or lower cost than the current loop
 113123 **    (5)  The template uses more terms of the same index but has no additional
 113124 **         dependencies          
 113126 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
 113127   WhereLoop **ppPrev, *p, *pNext = 0;
 113128   WhereInfo *pWInfo = pBuilder->pWInfo;
 113129   sqlite3 *db = pWInfo->pParse->db;
 113131   /* If pBuilder->pOrSet is defined, then only keep track of the costs
 113132   ** and prereqs.
 113134   if( pBuilder->pOrSet!=0 ){
 113135 #if WHERETRACE_ENABLED
 113136     u16 n = pBuilder->pOrSet->n;
 113137     int x =
 113138 #endif
 113139     whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
 113140                                     pTemplate->nOut);
 113141 #if WHERETRACE_ENABLED /* 0x8 */
 113142     if( sqlite3WhereTrace & 0x8 ){
 113143       sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
 113144       whereLoopPrint(pTemplate, pBuilder->pWC);
 113146 #endif
 113147     return SQLITE_OK;
 113150   /* Search for an existing WhereLoop to overwrite, or which takes
 113151   ** priority over pTemplate.
 113153   for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
 113154     if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
 113155       /* If either the iTab or iSortIdx values for two WhereLoop are different
 113156       ** then those WhereLoops need to be considered separately.  Neither is
 113157       ** a candidate to replace the other. */
 113158       continue;
 113160     /* In the current implementation, the rSetup value is either zero
 113161     ** or the cost of building an automatic index (NlogN) and the NlogN
 113162     ** is the same for compatible WhereLoops. */
 113163     assert( p->rSetup==0 || pTemplate->rSetup==0 
 113164                  || p->rSetup==pTemplate->rSetup );
 113166     /* whereLoopAddBtree() always generates and inserts the automatic index
 113167     ** case first.  Hence compatible candidate WhereLoops never have a larger
 113168     ** rSetup. Call this SETUP-INVARIANT */
 113169     assert( p->rSetup>=pTemplate->rSetup );
 113171     if( (p->prereq & pTemplate->prereq)==p->prereq
 113172      && p->rSetup<=pTemplate->rSetup
 113173      && p->rRun<=pTemplate->rRun
 113174      && p->nOut<=pTemplate->nOut
 113176       /* This branch taken when p is equal or better than pTemplate in 
 113177       ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
 113178       ** (4) number of output rows. */
 113179       assert( p->rSetup==pTemplate->rSetup );
 113180       if( p->prereq==pTemplate->prereq
 113181        && p->nLTerm<pTemplate->nLTerm
 113182        && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
 113183        && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
 113184           || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
 113186         /* Overwrite an existing WhereLoop with an similar one that uses
 113187         ** more terms of the index */
 113188         pNext = p->pNextLoop;
 113189         break;
 113190       }else{
 113191         /* pTemplate is not helpful.
 113192         ** Return without changing or adding anything */
 113193         goto whereLoopInsert_noop;
 113196     if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
 113197      && p->rRun>=pTemplate->rRun
 113198      && p->nOut>=pTemplate->nOut
 113200       /* Overwrite an existing WhereLoop with a better one: one that is
 113201       ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
 113202       ** or (4) number of output rows, and is no worse in any of those
 113203       ** categories. */
 113204       assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
 113205       pNext = p->pNextLoop;
 113206       break;
 113210   /* If we reach this point it means that either p[] should be overwritten
 113211   ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
 113212   ** WhereLoop and insert it.
 113214 #if WHERETRACE_ENABLED /* 0x8 */
 113215   if( sqlite3WhereTrace & 0x8 ){
 113216     if( p!=0 ){
 113217       sqlite3DebugPrintf("ins-del:  ");
 113218       whereLoopPrint(p, pBuilder->pWC);
 113220     sqlite3DebugPrintf("ins-new:  ");
 113221     whereLoopPrint(pTemplate, pBuilder->pWC);
 113223 #endif
 113224   if( p==0 ){
 113225     p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
 113226     if( p==0 ) return SQLITE_NOMEM;
 113227     whereLoopInit(p);
 113229   whereLoopXfer(db, p, pTemplate);
 113230   p->pNextLoop = pNext;
 113231   *ppPrev = p;
 113232   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
 113233     Index *pIndex = p->u.btree.pIndex;
 113234     if( pIndex && pIndex->tnum==0 ){
 113235       p->u.btree.pIndex = 0;
 113238   return SQLITE_OK;
 113240   /* Jump here if the insert is a no-op */
 113241 whereLoopInsert_noop:
 113242 #if WHERETRACE_ENABLED /* 0x8 */
 113243   if( sqlite3WhereTrace & 0x8 ){
 113244     sqlite3DebugPrintf("ins-noop: ");
 113245     whereLoopPrint(pTemplate, pBuilder->pWC);
 113247 #endif
 113248   return SQLITE_OK;  
 113252 ** Adjust the WhereLoop.nOut value downward to account for terms of the
 113253 ** WHERE clause that reference the loop but which are not used by an
 113254 ** index.
 113256 ** In the current implementation, the first extra WHERE clause term reduces
 113257 ** the number of output rows by a factor of 10 and each additional term
 113258 ** reduces the number of output rows by sqrt(2).
 113260 static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){
 113261   WhereTerm *pTerm, *pX;
 113262   Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
 113263   int i, j;
 113265   if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
 113266     return;
 113268   for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
 113269     if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
 113270     if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
 113271     if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
 113272     for(j=pLoop->nLTerm-1; j>=0; j--){
 113273       pX = pLoop->aLTerm[j];
 113274       if( pX==0 ) continue;
 113275       if( pX==pTerm ) break;
 113276       if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
 113278     if( j<0 ) pLoop->nOut += pTerm->truthProb;
 113283 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
 113284 ** Try to match one more.
 113286 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
 113287 ** INTEGER PRIMARY KEY.
 113289 static int whereLoopAddBtreeIndex(
 113290   WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
 113291   struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
 113292   Index *pProbe,                  /* An index on pSrc */
 113293   LogEst nInMul                   /* log(Number of iterations due to IN) */
 113295   WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
 113296   Parse *pParse = pWInfo->pParse;        /* Parsing context */
 113297   sqlite3 *db = pParse->db;       /* Database connection malloc context */
 113298   WhereLoop *pNew;                /* Template WhereLoop under construction */
 113299   WhereTerm *pTerm;               /* A WhereTerm under consideration */
 113300   int opMask;                     /* Valid operators for constraints */
 113301   WhereScan scan;                 /* Iterator for WHERE terms */
 113302   Bitmask saved_prereq;           /* Original value of pNew->prereq */
 113303   u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
 113304   u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
 113305   u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
 113306   u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
 113307   LogEst saved_nOut;              /* Original value of pNew->nOut */
 113308   int iCol;                       /* Index of the column in the table */
 113309   int rc = SQLITE_OK;             /* Return code */
 113310   LogEst nRowEst;                 /* Estimated index selectivity */
 113311   LogEst rLogSize;                /* Logarithm of table size */
 113312   WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
 113314   pNew = pBuilder->pNew;
 113315   if( db->mallocFailed ) return SQLITE_NOMEM;
 113317   assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
 113318   assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
 113319   if( pNew->wsFlags & WHERE_BTM_LIMIT ){
 113320     opMask = WO_LT|WO_LE;
 113321   }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
 113322     opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
 113323   }else{
 113324     opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
 113326   if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
 113328   assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
 113329   if( pNew->u.btree.nEq < pProbe->nKeyCol ){
 113330     iCol = pProbe->aiColumn[pNew->u.btree.nEq];
 113331     nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
 113332     if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
 113333   }else{
 113334     iCol = -1;
 113335     nRowEst = 0;
 113337   pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
 113338                         opMask, pProbe);
 113339   saved_nEq = pNew->u.btree.nEq;
 113340   saved_nSkip = pNew->u.btree.nSkip;
 113341   saved_nLTerm = pNew->nLTerm;
 113342   saved_wsFlags = pNew->wsFlags;
 113343   saved_prereq = pNew->prereq;
 113344   saved_nOut = pNew->nOut;
 113345   pNew->rSetup = 0;
 113346   rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
 113348   /* Consider using a skip-scan if there are no WHERE clause constraints
 113349   ** available for the left-most terms of the index, and if the average
 113350   ** number of repeats in the left-most terms is at least 18.  The magic
 113351   ** number 18 was found by experimentation to be the payoff point where
 113352   ** skip-scan become faster than a full-scan.
 113354   if( pTerm==0
 113355    && saved_nEq==saved_nSkip
 113356    && saved_nEq+1<pProbe->nKeyCol
 113357    && pProbe->aiRowEst[saved_nEq+1]>=18  /* TUNING: Minimum for skip-scan */
 113358    && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
 113360     LogEst nIter;
 113361     pNew->u.btree.nEq++;
 113362     pNew->u.btree.nSkip++;
 113363     pNew->aLTerm[pNew->nLTerm++] = 0;
 113364     pNew->wsFlags |= WHERE_SKIPSCAN;
 113365     nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
 113366     pNew->rRun = rLogSize + nIter;
 113367     pNew->nOut += nIter;
 113368     whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
 113369     pNew->nOut = saved_nOut;
 113371   for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
 113372     int nIn = 0;
 113373 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 113374     int nRecValid = pBuilder->nRecValid;
 113375 #endif
 113376     if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
 113377      && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
 113379       continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
 113381     if( pTerm->prereqRight & pNew->maskSelf ) continue;
 113383     assert( pNew->nOut==saved_nOut );
 113385     pNew->wsFlags = saved_wsFlags;
 113386     pNew->u.btree.nEq = saved_nEq;
 113387     pNew->nLTerm = saved_nLTerm;
 113388     if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
 113389     pNew->aLTerm[pNew->nLTerm++] = pTerm;
 113390     pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
 113391     pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
 113392     if( pTerm->eOperator & WO_IN ){
 113393       Expr *pExpr = pTerm->pExpr;
 113394       pNew->wsFlags |= WHERE_COLUMN_IN;
 113395       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 113396         /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
 113397         nIn = 46;  assert( 46==sqlite3LogEst(25) );
 113398       }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
 113399         /* "x IN (value, value, ...)" */
 113400         nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
 113402       pNew->rRun += nIn;
 113403       pNew->u.btree.nEq++;
 113404       pNew->nOut = nRowEst + nInMul + nIn;
 113405     }else if( pTerm->eOperator & (WO_EQ) ){
 113406       assert(
 113407         (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
 113408         || nInMul==0
 113410       pNew->wsFlags |= WHERE_COLUMN_EQ;
 113411       if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
 113412         assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
 113413         if( iCol>=0 && pProbe->onError==OE_None ){
 113414           pNew->wsFlags |= WHERE_UNQ_WANTED;
 113415         }else{
 113416           pNew->wsFlags |= WHERE_ONEROW;
 113419       pNew->u.btree.nEq++;
 113420       pNew->nOut = nRowEst + nInMul;
 113421     }else if( pTerm->eOperator & (WO_ISNULL) ){
 113422       pNew->wsFlags |= WHERE_COLUMN_NULL;
 113423       pNew->u.btree.nEq++;
 113424       /* TUNING: IS NULL selects 2 rows */
 113425       nIn = 10;  assert( 10==sqlite3LogEst(2) );
 113426       pNew->nOut = nRowEst + nInMul + nIn;
 113427     }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
 113428       testcase( pTerm->eOperator & WO_GT );
 113429       testcase( pTerm->eOperator & WO_GE );
 113430       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
 113431       pBtm = pTerm;
 113432       pTop = 0;
 113433     }else{
 113434       assert( pTerm->eOperator & (WO_LT|WO_LE) );
 113435       testcase( pTerm->eOperator & WO_LT );
 113436       testcase( pTerm->eOperator & WO_LE );
 113437       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
 113438       pTop = pTerm;
 113439       pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
 113440                      pNew->aLTerm[pNew->nLTerm-2] : 0;
 113442     if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
 113443       /* Adjust nOut and rRun for STAT3 range values */
 113444       assert( pNew->nOut==saved_nOut );
 113445       whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
 113447 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 113448     if( nInMul==0 
 113449      && pProbe->nSample 
 113450      && pNew->u.btree.nEq<=pProbe->nSampleCol
 113451      && OptimizationEnabled(db, SQLITE_Stat3) 
 113453       Expr *pExpr = pTerm->pExpr;
 113454       tRowcnt nOut = 0;
 113455       if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
 113456         testcase( pTerm->eOperator & WO_EQ );
 113457         testcase( pTerm->eOperator & WO_ISNULL );
 113458         rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
 113459       }else if( (pTerm->eOperator & WO_IN)
 113460              &&  !ExprHasProperty(pExpr, EP_xIsSelect)  ){
 113461         rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
 113463       assert( nOut==0 || rc==SQLITE_OK );
 113464       if( nOut ){
 113465         pNew->nOut = sqlite3LogEst(nOut);
 113466         if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
 113469 #endif
 113470     if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
 113471       /* Each row involves a step of the index, then a binary search of
 113472       ** the main table */
 113473       pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
 113475     /* Step cost for each output row */
 113476     pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
 113477     whereLoopOutputAdjust(pBuilder->pWC, pNew);
 113478     rc = whereLoopInsert(pBuilder, pNew);
 113479     if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
 113480      && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
 113482       whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
 113484     pNew->nOut = saved_nOut;
 113485 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 113486     pBuilder->nRecValid = nRecValid;
 113487 #endif
 113489   pNew->prereq = saved_prereq;
 113490   pNew->u.btree.nEq = saved_nEq;
 113491   pNew->u.btree.nSkip = saved_nSkip;
 113492   pNew->wsFlags = saved_wsFlags;
 113493   pNew->nOut = saved_nOut;
 113494   pNew->nLTerm = saved_nLTerm;
 113495   return rc;
 113499 ** Return True if it is possible that pIndex might be useful in
 113500 ** implementing the ORDER BY clause in pBuilder.
 113502 ** Return False if pBuilder does not contain an ORDER BY clause or
 113503 ** if there is no way for pIndex to be useful in implementing that
 113504 ** ORDER BY clause.
 113506 static int indexMightHelpWithOrderBy(
 113507   WhereLoopBuilder *pBuilder,
 113508   Index *pIndex,
 113509   int iCursor
 113511   ExprList *pOB;
 113512   int ii, jj;
 113514   if( pIndex->bUnordered ) return 0;
 113515   if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
 113516   for(ii=0; ii<pOB->nExpr; ii++){
 113517     Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
 113518     if( pExpr->op!=TK_COLUMN ) return 0;
 113519     if( pExpr->iTable==iCursor ){
 113520       for(jj=0; jj<pIndex->nKeyCol; jj++){
 113521         if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
 113525   return 0;
 113529 ** Return a bitmask where 1s indicate that the corresponding column of
 113530 ** the table is used by an index.  Only the first 63 columns are considered.
 113532 static Bitmask columnsInIndex(Index *pIdx){
 113533   Bitmask m = 0;
 113534   int j;
 113535   for(j=pIdx->nColumn-1; j>=0; j--){
 113536     int x = pIdx->aiColumn[j];
 113537     if( x>=0 ){
 113538       testcase( x==BMS-1 );
 113539       testcase( x==BMS-2 );
 113540       if( x<BMS-1 ) m |= MASKBIT(x);
 113543   return m;
 113546 /* Check to see if a partial index with pPartIndexWhere can be used
 113547 ** in the current query.  Return true if it can be and false if not.
 113549 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
 113550   int i;
 113551   WhereTerm *pTerm;
 113552   for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 113553     if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1;
 113555   return 0;
 113559 ** Add all WhereLoop objects for a single table of the join where the table
 113560 ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
 113561 ** a b-tree table, not a virtual table.
 113563 static int whereLoopAddBtree(
 113564   WhereLoopBuilder *pBuilder, /* WHERE clause information */
 113565   Bitmask mExtra              /* Extra prerequesites for using this table */
 113567   WhereInfo *pWInfo;          /* WHERE analysis context */
 113568   Index *pProbe;              /* An index we are evaluating */
 113569   Index sPk;                  /* A fake index object for the primary key */
 113570   tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
 113571   i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
 113572   SrcList *pTabList;          /* The FROM clause */
 113573   struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
 113574   WhereLoop *pNew;            /* Template WhereLoop object */
 113575   int rc = SQLITE_OK;         /* Return code */
 113576   int iSortIdx = 1;           /* Index number */
 113577   int b;                      /* A boolean value */
 113578   LogEst rSize;               /* number of rows in the table */
 113579   LogEst rLogSize;            /* Logarithm of the number of rows in the table */
 113580   WhereClause *pWC;           /* The parsed WHERE clause */
 113581   Table *pTab;                /* Table being queried */
 113583   pNew = pBuilder->pNew;
 113584   pWInfo = pBuilder->pWInfo;
 113585   pTabList = pWInfo->pTabList;
 113586   pSrc = pTabList->a + pNew->iTab;
 113587   pTab = pSrc->pTab;
 113588   pWC = pBuilder->pWC;
 113589   assert( !IsVirtual(pSrc->pTab) );
 113591   if( pSrc->pIndex ){
 113592     /* An INDEXED BY clause specifies a particular index to use */
 113593     pProbe = pSrc->pIndex;
 113594   }else if( !HasRowid(pTab) ){
 113595     pProbe = pTab->pIndex;
 113596   }else{
 113597     /* There is no INDEXED BY clause.  Create a fake Index object in local
 113598     ** variable sPk to represent the rowid primary key index.  Make this
 113599     ** fake index the first in a chain of Index objects with all of the real
 113600     ** indices to follow */
 113601     Index *pFirst;                  /* First of real indices on the table */
 113602     memset(&sPk, 0, sizeof(Index));
 113603     sPk.nKeyCol = 1;
 113604     sPk.aiColumn = &aiColumnPk;
 113605     sPk.aiRowEst = aiRowEstPk;
 113606     sPk.onError = OE_Replace;
 113607     sPk.pTable = pTab;
 113608     aiRowEstPk[0] = pTab->nRowEst;
 113609     aiRowEstPk[1] = 1;
 113610     pFirst = pSrc->pTab->pIndex;
 113611     if( pSrc->notIndexed==0 ){
 113612       /* The real indices of the table are only considered if the
 113613       ** NOT INDEXED qualifier is omitted from the FROM clause */
 113614       sPk.pNext = pFirst;
 113616     pProbe = &sPk;
 113618   rSize = sqlite3LogEst(pTab->nRowEst);
 113619   rLogSize = estLog(rSize);
 113621 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 113622   /* Automatic indexes */
 113623   if( !pBuilder->pOrSet
 113624    && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
 113625    && pSrc->pIndex==0
 113626    && !pSrc->viaCoroutine
 113627    && !pSrc->notIndexed
 113628    && HasRowid(pTab)
 113629    && !pSrc->isCorrelated
 113630    && !pSrc->isRecursive
 113632     /* Generate auto-index WhereLoops */
 113633     WhereTerm *pTerm;
 113634     WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
 113635     for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
 113636       if( pTerm->prereqRight & pNew->maskSelf ) continue;
 113637       if( termCanDriveIndex(pTerm, pSrc, 0) ){
 113638         pNew->u.btree.nEq = 1;
 113639         pNew->u.btree.nSkip = 0;
 113640         pNew->u.btree.pIndex = 0;
 113641         pNew->nLTerm = 1;
 113642         pNew->aLTerm[0] = pTerm;
 113643         /* TUNING: One-time cost for computing the automatic index is
 113644         ** approximately 7*N*log2(N) where N is the number of rows in
 113645         ** the table being indexed. */
 113646         pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
 113647         /* TUNING: Each index lookup yields 20 rows in the table.  This
 113648         ** is more than the usual guess of 10 rows, since we have no way
 113649         ** of knowning how selective the index will ultimately be.  It would
 113650         ** not be unreasonable to make this value much larger. */
 113651         pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
 113652         pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
 113653         pNew->wsFlags = WHERE_AUTO_INDEX;
 113654         pNew->prereq = mExtra | pTerm->prereqRight;
 113655         rc = whereLoopInsert(pBuilder, pNew);
 113659 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
 113661   /* Loop over all indices
 113663   for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
 113664     if( pProbe->pPartIdxWhere!=0
 113665      && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
 113666       continue;  /* Partial index inappropriate for this query */
 113668     pNew->u.btree.nEq = 0;
 113669     pNew->u.btree.nSkip = 0;
 113670     pNew->nLTerm = 0;
 113671     pNew->iSortIdx = 0;
 113672     pNew->rSetup = 0;
 113673     pNew->prereq = mExtra;
 113674     pNew->nOut = rSize;
 113675     pNew->u.btree.pIndex = pProbe;
 113676     b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
 113677     /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
 113678     assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
 113679     if( pProbe->tnum<=0 ){
 113680       /* Integer primary key index */
 113681       pNew->wsFlags = WHERE_IPK;
 113683       /* Full table scan */
 113684       pNew->iSortIdx = b ? iSortIdx : 0;
 113685       /* TUNING: Cost of full table scan is 3*(N + log2(N)).
 113686       **  +  The extra 3 factor is to encourage the use of indexed lookups
 113687       **     over full scans.  FIXME */
 113688       pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
 113689       whereLoopOutputAdjust(pWC, pNew);
 113690       rc = whereLoopInsert(pBuilder, pNew);
 113691       pNew->nOut = rSize;
 113692       if( rc ) break;
 113693     }else{
 113694       Bitmask m;
 113695       if( pProbe->isCovering ){
 113696         pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
 113697         m = 0;
 113698       }else{
 113699         m = pSrc->colUsed & ~columnsInIndex(pProbe);
 113700         pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
 113703       /* Full scan via index */
 113704       if( b
 113705        || !HasRowid(pTab)
 113706        || ( m==0
 113707          && pProbe->bUnordered==0
 113708          && (pProbe->szIdxRow<pTab->szTabRow)
 113709          && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
 113710          && sqlite3GlobalConfig.bUseCis
 113711          && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
 113714         pNew->iSortIdx = b ? iSortIdx : 0;
 113715         if( m==0 ){
 113716           /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
 113717           **  +  The extra factor K of between 1.1 and 3.0 that depends
 113718           **     on the relative sizes of the table and the index.  K
 113719           **     is smaller for smaller indices, thus favoring them.
 113721           pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
 113722                         (15*pProbe->szIdxRow)/pTab->szTabRow;
 113723         }else{
 113724           /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
 113725           ** which we will simplify to just N*log2(N) */
 113726           pNew->rRun = rSize + rLogSize;
 113728         whereLoopOutputAdjust(pWC, pNew);
 113729         rc = whereLoopInsert(pBuilder, pNew);
 113730         pNew->nOut = rSize;
 113731         if( rc ) break;
 113735     rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
 113736 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
 113737     sqlite3Stat4ProbeFree(pBuilder->pRec);
 113738     pBuilder->nRecValid = 0;
 113739     pBuilder->pRec = 0;
 113740 #endif
 113742     /* If there was an INDEXED BY clause, then only that one index is
 113743     ** considered. */
 113744     if( pSrc->pIndex ) break;
 113746   return rc;
 113749 #ifndef SQLITE_OMIT_VIRTUALTABLE
 113751 ** Add all WhereLoop objects for a table of the join identified by
 113752 ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
 113754 static int whereLoopAddVirtual(
 113755   WhereLoopBuilder *pBuilder,  /* WHERE clause information */
 113756   Bitmask mExtra
 113758   WhereInfo *pWInfo;           /* WHERE analysis context */
 113759   Parse *pParse;               /* The parsing context */
 113760   WhereClause *pWC;            /* The WHERE clause */
 113761   struct SrcList_item *pSrc;   /* The FROM clause term to search */
 113762   Table *pTab;
 113763   sqlite3 *db;
 113764   sqlite3_index_info *pIdxInfo;
 113765   struct sqlite3_index_constraint *pIdxCons;
 113766   struct sqlite3_index_constraint_usage *pUsage;
 113767   WhereTerm *pTerm;
 113768   int i, j;
 113769   int iTerm, mxTerm;
 113770   int nConstraint;
 113771   int seenIn = 0;              /* True if an IN operator is seen */
 113772   int seenVar = 0;             /* True if a non-constant constraint is seen */
 113773   int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
 113774   WhereLoop *pNew;
 113775   int rc = SQLITE_OK;
 113777   pWInfo = pBuilder->pWInfo;
 113778   pParse = pWInfo->pParse;
 113779   db = pParse->db;
 113780   pWC = pBuilder->pWC;
 113781   pNew = pBuilder->pNew;
 113782   pSrc = &pWInfo->pTabList->a[pNew->iTab];
 113783   pTab = pSrc->pTab;
 113784   assert( IsVirtual(pTab) );
 113785   pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
 113786   if( pIdxInfo==0 ) return SQLITE_NOMEM;
 113787   pNew->prereq = 0;
 113788   pNew->rSetup = 0;
 113789   pNew->wsFlags = WHERE_VIRTUALTABLE;
 113790   pNew->nLTerm = 0;
 113791   pNew->u.vtab.needFree = 0;
 113792   pUsage = pIdxInfo->aConstraintUsage;
 113793   nConstraint = pIdxInfo->nConstraint;
 113794   if( whereLoopResize(db, pNew, nConstraint) ){
 113795     sqlite3DbFree(db, pIdxInfo);
 113796     return SQLITE_NOMEM;
 113799   for(iPhase=0; iPhase<=3; iPhase++){
 113800     if( !seenIn && (iPhase&1)!=0 ){
 113801       iPhase++;
 113802       if( iPhase>3 ) break;
 113804     if( !seenVar && iPhase>1 ) break;
 113805     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
 113806     for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
 113807       j = pIdxCons->iTermOffset;
 113808       pTerm = &pWC->a[j];
 113809       switch( iPhase ){
 113810         case 0:    /* Constants without IN operator */
 113811           pIdxCons->usable = 0;
 113812           if( (pTerm->eOperator & WO_IN)!=0 ){
 113813             seenIn = 1;
 113815           if( pTerm->prereqRight!=0 ){
 113816             seenVar = 1;
 113817           }else if( (pTerm->eOperator & WO_IN)==0 ){
 113818             pIdxCons->usable = 1;
 113820           break;
 113821         case 1:    /* Constants with IN operators */
 113822           assert( seenIn );
 113823           pIdxCons->usable = (pTerm->prereqRight==0);
 113824           break;
 113825         case 2:    /* Variables without IN */
 113826           assert( seenVar );
 113827           pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
 113828           break;
 113829         default:   /* Variables with IN */
 113830           assert( seenVar && seenIn );
 113831           pIdxCons->usable = 1;
 113832           break;
 113835     memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
 113836     if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
 113837     pIdxInfo->idxStr = 0;
 113838     pIdxInfo->idxNum = 0;
 113839     pIdxInfo->needToFreeIdxStr = 0;
 113840     pIdxInfo->orderByConsumed = 0;
 113841     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
 113842     pIdxInfo->estimatedRows = 25;
 113843     rc = vtabBestIndex(pParse, pTab, pIdxInfo);
 113844     if( rc ) goto whereLoopAddVtab_exit;
 113845     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
 113846     pNew->prereq = mExtra;
 113847     mxTerm = -1;
 113848     assert( pNew->nLSlot>=nConstraint );
 113849     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
 113850     pNew->u.vtab.omitMask = 0;
 113851     for(i=0; i<nConstraint; i++, pIdxCons++){
 113852       if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
 113853         j = pIdxCons->iTermOffset;
 113854         if( iTerm>=nConstraint
 113855          || j<0
 113856          || j>=pWC->nTerm
 113857          || pNew->aLTerm[iTerm]!=0
 113859           rc = SQLITE_ERROR;
 113860           sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
 113861           goto whereLoopAddVtab_exit;
 113863         testcase( iTerm==nConstraint-1 );
 113864         testcase( j==0 );
 113865         testcase( j==pWC->nTerm-1 );
 113866         pTerm = &pWC->a[j];
 113867         pNew->prereq |= pTerm->prereqRight;
 113868         assert( iTerm<pNew->nLSlot );
 113869         pNew->aLTerm[iTerm] = pTerm;
 113870         if( iTerm>mxTerm ) mxTerm = iTerm;
 113871         testcase( iTerm==15 );
 113872         testcase( iTerm==16 );
 113873         if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
 113874         if( (pTerm->eOperator & WO_IN)!=0 ){
 113875           if( pUsage[i].omit==0 ){
 113876             /* Do not attempt to use an IN constraint if the virtual table
 113877             ** says that the equivalent EQ constraint cannot be safely omitted.
 113878             ** If we do attempt to use such a constraint, some rows might be
 113879             ** repeated in the output. */
 113880             break;
 113882           /* A virtual table that is constrained by an IN clause may not
 113883           ** consume the ORDER BY clause because (1) the order of IN terms
 113884           ** is not necessarily related to the order of output terms and
 113885           ** (2) Multiple outputs from a single IN value will not merge
 113886           ** together.  */
 113887           pIdxInfo->orderByConsumed = 0;
 113891     if( i>=nConstraint ){
 113892       pNew->nLTerm = mxTerm+1;
 113893       assert( pNew->nLTerm<=pNew->nLSlot );
 113894       pNew->u.vtab.idxNum = pIdxInfo->idxNum;
 113895       pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
 113896       pIdxInfo->needToFreeIdxStr = 0;
 113897       pNew->u.vtab.idxStr = pIdxInfo->idxStr;
 113898       pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
 113899                                      && pIdxInfo->orderByConsumed);
 113900       pNew->rSetup = 0;
 113901       pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
 113902       pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
 113903       whereLoopInsert(pBuilder, pNew);
 113904       if( pNew->u.vtab.needFree ){
 113905         sqlite3_free(pNew->u.vtab.idxStr);
 113906         pNew->u.vtab.needFree = 0;
 113911 whereLoopAddVtab_exit:
 113912   if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
 113913   sqlite3DbFree(db, pIdxInfo);
 113914   return rc;
 113916 #endif /* SQLITE_OMIT_VIRTUALTABLE */
 113919 ** Add WhereLoop entries to handle OR terms.  This works for either
 113920 ** btrees or virtual tables.
 113922 static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
 113923   WhereInfo *pWInfo = pBuilder->pWInfo;
 113924   WhereClause *pWC;
 113925   WhereLoop *pNew;
 113926   WhereTerm *pTerm, *pWCEnd;
 113927   int rc = SQLITE_OK;
 113928   int iCur;
 113929   WhereClause tempWC;
 113930   WhereLoopBuilder sSubBuild;
 113931   WhereOrSet sSum, sCur, sPrev;
 113932   struct SrcList_item *pItem;
 113934   pWC = pBuilder->pWC;
 113935   if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
 113936   pWCEnd = pWC->a + pWC->nTerm;
 113937   pNew = pBuilder->pNew;
 113938   memset(&sSum, 0, sizeof(sSum));
 113939   pItem = pWInfo->pTabList->a + pNew->iTab;
 113940   if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
 113941   iCur = pItem->iCursor;
 113943   for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
 113944     if( (pTerm->eOperator & WO_OR)!=0
 113945      && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
 113947       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
 113948       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
 113949       WhereTerm *pOrTerm;
 113950       int once = 1;
 113951       int i, j;
 113953       sSubBuild = *pBuilder;
 113954       sSubBuild.pOrderBy = 0;
 113955       sSubBuild.pOrSet = &sCur;
 113957       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
 113958         if( (pOrTerm->eOperator & WO_AND)!=0 ){
 113959           sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
 113960         }else if( pOrTerm->leftCursor==iCur ){
 113961           tempWC.pWInfo = pWC->pWInfo;
 113962           tempWC.pOuter = pWC;
 113963           tempWC.op = TK_AND;
 113964           tempWC.nTerm = 1;
 113965           tempWC.a = pOrTerm;
 113966           sSubBuild.pWC = &tempWC;
 113967         }else{
 113968           continue;
 113970         sCur.n = 0;
 113971 #ifndef SQLITE_OMIT_VIRTUALTABLE
 113972         if( IsVirtual(pItem->pTab) ){
 113973           rc = whereLoopAddVirtual(&sSubBuild, mExtra);
 113974         }else
 113975 #endif
 113977           rc = whereLoopAddBtree(&sSubBuild, mExtra);
 113979         assert( rc==SQLITE_OK || sCur.n==0 );
 113980         if( sCur.n==0 ){
 113981           sSum.n = 0;
 113982           break;
 113983         }else if( once ){
 113984           whereOrMove(&sSum, &sCur);
 113985           once = 0;
 113986         }else{
 113987           whereOrMove(&sPrev, &sSum);
 113988           sSum.n = 0;
 113989           for(i=0; i<sPrev.n; i++){
 113990             for(j=0; j<sCur.n; j++){
 113991               whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
 113992                             sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
 113993                             sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
 113998       pNew->nLTerm = 1;
 113999       pNew->aLTerm[0] = pTerm;
 114000       pNew->wsFlags = WHERE_MULTI_OR;
 114001       pNew->rSetup = 0;
 114002       pNew->iSortIdx = 0;
 114003       memset(&pNew->u, 0, sizeof(pNew->u));
 114004       for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
 114005         /* TUNING: Multiple by 3.5 for the secondary table lookup */
 114006         pNew->rRun = sSum.a[i].rRun + 18;
 114007         pNew->nOut = sSum.a[i].nOut;
 114008         pNew->prereq = sSum.a[i].prereq;
 114009         rc = whereLoopInsert(pBuilder, pNew);
 114013   return rc;
 114017 ** Add all WhereLoop objects for all tables 
 114019 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
 114020   WhereInfo *pWInfo = pBuilder->pWInfo;
 114021   Bitmask mExtra = 0;
 114022   Bitmask mPrior = 0;
 114023   int iTab;
 114024   SrcList *pTabList = pWInfo->pTabList;
 114025   struct SrcList_item *pItem;
 114026   sqlite3 *db = pWInfo->pParse->db;
 114027   int nTabList = pWInfo->nLevel;
 114028   int rc = SQLITE_OK;
 114029   u8 priorJoinType = 0;
 114030   WhereLoop *pNew;
 114032   /* Loop over the tables in the join, from left to right */
 114033   pNew = pBuilder->pNew;
 114034   whereLoopInit(pNew);
 114035   for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
 114036     pNew->iTab = iTab;
 114037     pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
 114038     if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
 114039       mExtra = mPrior;
 114041     priorJoinType = pItem->jointype;
 114042     if( IsVirtual(pItem->pTab) ){
 114043       rc = whereLoopAddVirtual(pBuilder, mExtra);
 114044     }else{
 114045       rc = whereLoopAddBtree(pBuilder, mExtra);
 114047     if( rc==SQLITE_OK ){
 114048       rc = whereLoopAddOr(pBuilder, mExtra);
 114050     mPrior |= pNew->maskSelf;
 114051     if( rc || db->mallocFailed ) break;
 114053   whereLoopClear(db, pNew);
 114054   return rc;
 114058 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
 114059 ** parameters) to see if it outputs rows in the requested ORDER BY
 114060 ** (or GROUP BY) without requiring a separate sort operation.  Return:
 114062 **    0:  ORDER BY is not satisfied.  Sorting required
 114063 **    1:  ORDER BY is satisfied.      Omit sorting
 114064 **   -1:  Unknown at this time
 114066 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
 114067 ** strict.  With GROUP BY and DISTINCT the only requirement is that
 114068 ** equivalent rows appear immediately adjacent to one another.  GROUP BY
 114069 ** and DISTINT do not require rows to appear in any particular order as long
 114070 ** as equivelent rows are grouped together.  Thus for GROUP BY and DISTINCT
 114071 ** the pOrderBy terms can be matched in any order.  With ORDER BY, the 
 114072 ** pOrderBy terms must be matched in strict left-to-right order.
 114074 static int wherePathSatisfiesOrderBy(
 114075   WhereInfo *pWInfo,    /* The WHERE clause */
 114076   ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
 114077   WherePath *pPath,     /* The WherePath to check */
 114078   u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
 114079   u16 nLoop,            /* Number of entries in pPath->aLoop[] */
 114080   WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
 114081   Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
 114083   u8 revSet;            /* True if rev is known */
 114084   u8 rev;               /* Composite sort order */
 114085   u8 revIdx;            /* Index sort order */
 114086   u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
 114087   u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
 114088   u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
 114089   u16 nKeyCol;          /* Number of key columns in pIndex */
 114090   u16 nColumn;          /* Total number of ordered columns in the index */
 114091   u16 nOrderBy;         /* Number terms in the ORDER BY clause */
 114092   int iLoop;            /* Index of WhereLoop in pPath being processed */
 114093   int i, j;             /* Loop counters */
 114094   int iCur;             /* Cursor number for current WhereLoop */
 114095   int iColumn;          /* A column number within table iCur */
 114096   WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
 114097   WhereTerm *pTerm;     /* A single term of the WHERE clause */
 114098   Expr *pOBExpr;        /* An expression from the ORDER BY clause */
 114099   CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
 114100   Index *pIndex;        /* The index associated with pLoop */
 114101   sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
 114102   Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
 114103   Bitmask obDone;       /* Mask of all ORDER BY terms */
 114104   Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
 114105   Bitmask ready;              /* Mask of inner loops */
 114108   ** We say the WhereLoop is "one-row" if it generates no more than one
 114109   ** row of output.  A WhereLoop is one-row if all of the following are true:
 114110   **  (a) All index columns match with WHERE_COLUMN_EQ.
 114111   **  (b) The index is unique
 114112   ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
 114113   ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
 114115   ** We say the WhereLoop is "order-distinct" if the set of columns from
 114116   ** that WhereLoop that are in the ORDER BY clause are different for every
 114117   ** row of the WhereLoop.  Every one-row WhereLoop is automatically
 114118   ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
 114119   ** is not order-distinct. To be order-distinct is not quite the same as being
 114120   ** UNIQUE since a UNIQUE column or index can have multiple rows that 
 114121   ** are NULL and NULL values are equivalent for the purpose of order-distinct.
 114122   ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
 114124   ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
 114125   ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
 114126   ** automatically order-distinct.
 114129   assert( pOrderBy!=0 );
 114131   /* Sortability of virtual tables is determined by the xBestIndex method
 114132   ** of the virtual table itself */
 114133   if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
 114134     testcase( nLoop>0 );  /* True when outer loops are one-row and match 
 114135                           ** no ORDER BY terms */
 114136     return pLast->u.vtab.isOrdered;
 114138   if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
 114140   nOrderBy = pOrderBy->nExpr;
 114141   testcase( nOrderBy==BMS-1 );
 114142   if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
 114143   isOrderDistinct = 1;
 114144   obDone = MASKBIT(nOrderBy)-1;
 114145   orderDistinctMask = 0;
 114146   ready = 0;
 114147   for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
 114148     if( iLoop>0 ) ready |= pLoop->maskSelf;
 114149     pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
 114150     assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
 114151     iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
 114153     /* Mark off any ORDER BY term X that is a column in the table of
 114154     ** the current loop for which there is term in the WHERE
 114155     ** clause of the form X IS NULL or X=? that reference only outer
 114156     ** loops.
 114158     for(i=0; i<nOrderBy; i++){
 114159       if( MASKBIT(i) & obSat ) continue;
 114160       pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
 114161       if( pOBExpr->op!=TK_COLUMN ) continue;
 114162       if( pOBExpr->iTable!=iCur ) continue;
 114163       pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
 114164                        ~ready, WO_EQ|WO_ISNULL, 0);
 114165       if( pTerm==0 ) continue;
 114166       if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
 114167         const char *z1, *z2;
 114168         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
 114169         if( !pColl ) pColl = db->pDfltColl;
 114170         z1 = pColl->zName;
 114171         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
 114172         if( !pColl ) pColl = db->pDfltColl;
 114173         z2 = pColl->zName;
 114174         if( sqlite3StrICmp(z1, z2)!=0 ) continue;
 114176       obSat |= MASKBIT(i);
 114179     if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
 114180       if( pLoop->wsFlags & WHERE_IPK ){
 114181         pIndex = 0;
 114182         nKeyCol = 0;
 114183         nColumn = 1;
 114184       }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
 114185         return 0;
 114186       }else{
 114187         nKeyCol = pIndex->nKeyCol;
 114188         nColumn = pIndex->nColumn;
 114189         assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
 114190         assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
 114191         isOrderDistinct = pIndex->onError!=OE_None;
 114194       /* Loop through all columns of the index and deal with the ones
 114195       ** that are not constrained by == or IN.
 114197       rev = revSet = 0;
 114198       distinctColumns = 0;
 114199       for(j=0; j<nColumn; j++){
 114200         u8 bOnce;   /* True to run the ORDER BY search loop */
 114202         /* Skip over == and IS NULL terms */
 114203         if( j<pLoop->u.btree.nEq
 114204          && pLoop->u.btree.nSkip==0
 114205          && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
 114207           if( i & WO_ISNULL ){
 114208             testcase( isOrderDistinct );
 114209             isOrderDistinct = 0;
 114211           continue;  
 114214         /* Get the column number in the table (iColumn) and sort order
 114215         ** (revIdx) for the j-th column of the index.
 114217         if( pIndex ){
 114218           iColumn = pIndex->aiColumn[j];
 114219           revIdx = pIndex->aSortOrder[j];
 114220           if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
 114221         }else{
 114222           iColumn = -1;
 114223           revIdx = 0;
 114226         /* An unconstrained column that might be NULL means that this
 114227         ** WhereLoop is not well-ordered
 114229         if( isOrderDistinct
 114230          && iColumn>=0
 114231          && j>=pLoop->u.btree.nEq
 114232          && pIndex->pTable->aCol[iColumn].notNull==0
 114234           isOrderDistinct = 0;
 114237         /* Find the ORDER BY term that corresponds to the j-th column
 114238         ** of the index and and mark that ORDER BY term off 
 114240         bOnce = 1;
 114241         isMatch = 0;
 114242         for(i=0; bOnce && i<nOrderBy; i++){
 114243           if( MASKBIT(i) & obSat ) continue;
 114244           pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
 114245           testcase( wctrlFlags & WHERE_GROUPBY );
 114246           testcase( wctrlFlags & WHERE_DISTINCTBY );
 114247           if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
 114248           if( pOBExpr->op!=TK_COLUMN ) continue;
 114249           if( pOBExpr->iTable!=iCur ) continue;
 114250           if( pOBExpr->iColumn!=iColumn ) continue;
 114251           if( iColumn>=0 ){
 114252             pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
 114253             if( !pColl ) pColl = db->pDfltColl;
 114254             if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
 114256           isMatch = 1;
 114257           break;
 114259         if( isMatch ){
 114260           if( iColumn<0 ){
 114261             testcase( distinctColumns==0 );
 114262             distinctColumns = 1;
 114264           obSat |= MASKBIT(i);
 114265           if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
 114266             /* Make sure the sort order is compatible in an ORDER BY clause.
 114267             ** Sort order is irrelevant for a GROUP BY clause. */
 114268             if( revSet ){
 114269               if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
 114270             }else{
 114271               rev = revIdx ^ pOrderBy->a[i].sortOrder;
 114272               if( rev ) *pRevMask |= MASKBIT(iLoop);
 114273               revSet = 1;
 114276         }else{
 114277           /* No match found */
 114278           if( j==0 || j<nKeyCol ){
 114279             testcase( isOrderDistinct!=0 );
 114280             isOrderDistinct = 0;
 114282           break;
 114284       } /* end Loop over all index columns */
 114285       if( distinctColumns ){
 114286         testcase( isOrderDistinct==0 );
 114287         isOrderDistinct = 1;
 114289     } /* end-if not one-row */
 114291     /* Mark off any other ORDER BY terms that reference pLoop */
 114292     if( isOrderDistinct ){
 114293       orderDistinctMask |= pLoop->maskSelf;
 114294       for(i=0; i<nOrderBy; i++){
 114295         Expr *p;
 114296         Bitmask mTerm;
 114297         if( MASKBIT(i) & obSat ) continue;
 114298         p = pOrderBy->a[i].pExpr;
 114299         mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
 114300         if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
 114301         if( (mTerm&~orderDistinctMask)==0 ){
 114302           obSat |= MASKBIT(i);
 114306   } /* End the loop over all WhereLoops from outer-most down to inner-most */
 114307   if( obSat==obDone ) return 1;
 114308   if( !isOrderDistinct ) return 0;
 114309   return -1;
 114312 #ifdef WHERETRACE_ENABLED
 114313 /* For debugging use only: */
 114314 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
 114315   static char zName[65];
 114316   int i;
 114317   for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
 114318   if( pLast ) zName[i++] = pLast->cId;
 114319   zName[i] = 0;
 114320   return zName;
 114322 #endif
 114326 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
 114327 ** attempts to find the lowest cost path that visits each WhereLoop
 114328 ** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
 114330 ** Assume that the total number of output rows that will need to be sorted
 114331 ** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
 114332 ** costs if nRowEst==0.
 114334 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
 114335 ** error occurs.
 114337 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
 114338   int mxChoice;             /* Maximum number of simultaneous paths tracked */
 114339   int nLoop;                /* Number of terms in the join */
 114340   Parse *pParse;            /* Parsing context */
 114341   sqlite3 *db;              /* The database connection */
 114342   int iLoop;                /* Loop counter over the terms of the join */
 114343   int ii, jj;               /* Loop counters */
 114344   int mxI = 0;              /* Index of next entry to replace */
 114345   LogEst rCost;             /* Cost of a path */
 114346   LogEst nOut;              /* Number of outputs */
 114347   LogEst mxCost = 0;        /* Maximum cost of a set of paths */
 114348   LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
 114349   LogEst rSortCost;         /* Cost to do a sort */
 114350   int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
 114351   WherePath *aFrom;         /* All nFrom paths at the previous level */
 114352   WherePath *aTo;           /* The nTo best paths at the current level */
 114353   WherePath *pFrom;         /* An element of aFrom[] that we are working on */
 114354   WherePath *pTo;           /* An element of aTo[] that we are working on */
 114355   WhereLoop *pWLoop;        /* One of the WhereLoop objects */
 114356   WhereLoop **pX;           /* Used to divy up the pSpace memory */
 114357   char *pSpace;             /* Temporary memory used by this routine */
 114359   pParse = pWInfo->pParse;
 114360   db = pParse->db;
 114361   nLoop = pWInfo->nLevel;
 114362   /* TUNING: For simple queries, only the best path is tracked.
 114363   ** For 2-way joins, the 5 best paths are followed.
 114364   ** For joins of 3 or more tables, track the 10 best paths */
 114365   mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
 114366   assert( nLoop<=pWInfo->pTabList->nSrc );
 114367   WHERETRACE(0x002, ("---- begin solver\n"));
 114369   /* Allocate and initialize space for aTo and aFrom */
 114370   ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
 114371   pSpace = sqlite3DbMallocRaw(db, ii);
 114372   if( pSpace==0 ) return SQLITE_NOMEM;
 114373   aTo = (WherePath*)pSpace;
 114374   aFrom = aTo+mxChoice;
 114375   memset(aFrom, 0, sizeof(aFrom[0]));
 114376   pX = (WhereLoop**)(aFrom+mxChoice);
 114377   for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
 114378     pFrom->aLoop = pX;
 114381   /* Seed the search with a single WherePath containing zero WhereLoops.
 114383   ** TUNING: Do not let the number of iterations go above 25.  If the cost
 114384   ** of computing an automatic index is not paid back within the first 25
 114385   ** rows, then do not use the automatic index. */
 114386   aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==sqlite3LogEst(25) );
 114387   nFrom = 1;
 114389   /* Precompute the cost of sorting the final result set, if the caller
 114390   ** to sqlite3WhereBegin() was concerned about sorting */
 114391   rSortCost = 0;
 114392   if( pWInfo->pOrderBy==0 || nRowEst==0 ){
 114393     aFrom[0].isOrderedValid = 1;
 114394   }else{
 114395     /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
 114396     ** number of output rows. The 48 is the expected size of a row to sort. 
 114397     ** FIXME:  compute a better estimate of the 48 multiplier based on the
 114398     ** result set expressions. */
 114399     rSortCost = nRowEst + estLog(nRowEst);
 114400     WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
 114403   /* Compute successively longer WherePaths using the previous generation
 114404   ** of WherePaths as the basis for the next.  Keep track of the mxChoice
 114405   ** best paths at each generation */
 114406   for(iLoop=0; iLoop<nLoop; iLoop++){
 114407     nTo = 0;
 114408     for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
 114409       for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
 114410         Bitmask maskNew;
 114411         Bitmask revMask = 0;
 114412         u8 isOrderedValid = pFrom->isOrderedValid;
 114413         u8 isOrdered = pFrom->isOrdered;
 114414         if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
 114415         if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
 114416         /* At this point, pWLoop is a candidate to be the next loop. 
 114417         ** Compute its cost */
 114418         rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
 114419         rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
 114420         nOut = pFrom->nRow + pWLoop->nOut;
 114421         maskNew = pFrom->maskLoop | pWLoop->maskSelf;
 114422         if( !isOrderedValid ){
 114423           switch( wherePathSatisfiesOrderBy(pWInfo,
 114424                        pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
 114425                        iLoop, pWLoop, &revMask) ){
 114426             case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
 114427               isOrdered = 1;
 114428               isOrderedValid = 1;
 114429               break;
 114430             case 0:  /* No.  pFrom+pWLoop will require a separate sort */
 114431               isOrdered = 0;
 114432               isOrderedValid = 1;
 114433               rCost = sqlite3LogEstAdd(rCost, rSortCost);
 114434               break;
 114435             default: /* Cannot tell yet.  Try again on the next iteration */
 114436               break;
 114438         }else{
 114439           revMask = pFrom->revLoop;
 114441         /* Check to see if pWLoop should be added to the mxChoice best so far */
 114442         for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
 114443           if( pTo->maskLoop==maskNew
 114444            && pTo->isOrderedValid==isOrderedValid
 114445            && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
 114446                 (pTo->rCost>=rCost && pTo->nRow>=nOut))
 114448             testcase( jj==nTo-1 );
 114449             break;
 114452         if( jj>=nTo ){
 114453           if( nTo>=mxChoice && rCost>=mxCost ){
 114454 #ifdef WHERETRACE_ENABLED /* 0x4 */
 114455             if( sqlite3WhereTrace&0x4 ){
 114456               sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
 114457                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
 114458                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
 114460 #endif
 114461             continue;
 114463           /* Add a new Path to the aTo[] set */
 114464           if( nTo<mxChoice ){
 114465             /* Increase the size of the aTo set by one */
 114466             jj = nTo++;
 114467           }else{
 114468             /* New path replaces the prior worst to keep count below mxChoice */
 114469             jj = mxI;
 114471           pTo = &aTo[jj];
 114472 #ifdef WHERETRACE_ENABLED /* 0x4 */
 114473           if( sqlite3WhereTrace&0x4 ){
 114474             sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
 114475                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
 114476                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
 114478 #endif
 114479         }else{
 114480           if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
 114481 #ifdef WHERETRACE_ENABLED /* 0x4 */
 114482             if( sqlite3WhereTrace&0x4 ){
 114483               sqlite3DebugPrintf(
 114484                   "Skip   %s cost=%-3d,%3d order=%c",
 114485                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
 114486                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
 114487               sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
 114488                   wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
 114489                   pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
 114491 #endif
 114492             testcase( pTo->rCost==rCost );
 114493             continue;
 114495           testcase( pTo->rCost==rCost+1 );
 114496           /* A new and better score for a previously created equivalent path */
 114497 #ifdef WHERETRACE_ENABLED /* 0x4 */
 114498           if( sqlite3WhereTrace&0x4 ){
 114499             sqlite3DebugPrintf(
 114500                 "Update %s cost=%-3d,%3d order=%c",
 114501                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
 114502                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
 114503             sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
 114504                 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
 114505                 pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
 114507 #endif
 114509         /* pWLoop is a winner.  Add it to the set of best so far */
 114510         pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
 114511         pTo->revLoop = revMask;
 114512         pTo->nRow = nOut;
 114513         pTo->rCost = rCost;
 114514         pTo->isOrderedValid = isOrderedValid;
 114515         pTo->isOrdered = isOrdered;
 114516         memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
 114517         pTo->aLoop[iLoop] = pWLoop;
 114518         if( nTo>=mxChoice ){
 114519           mxI = 0;
 114520           mxCost = aTo[0].rCost;
 114521           mxOut = aTo[0].nRow;
 114522           for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
 114523             if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
 114524               mxCost = pTo->rCost;
 114525               mxOut = pTo->nRow;
 114526               mxI = jj;
 114533 #ifdef WHERETRACE_ENABLED  /* >=2 */
 114534     if( sqlite3WhereTrace>=2 ){
 114535       sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
 114536       for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
 114537         sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
 114538            wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
 114539            pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
 114540         if( pTo->isOrderedValid && pTo->isOrdered ){
 114541           sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
 114542         }else{
 114543           sqlite3DebugPrintf("\n");
 114547 #endif
 114549     /* Swap the roles of aFrom and aTo for the next generation */
 114550     pFrom = aTo;
 114551     aTo = aFrom;
 114552     aFrom = pFrom;
 114553     nFrom = nTo;
 114556   if( nFrom==0 ){
 114557     sqlite3ErrorMsg(pParse, "no query solution");
 114558     sqlite3DbFree(db, pSpace);
 114559     return SQLITE_ERROR;
 114562   /* Find the lowest cost path.  pFrom will be left pointing to that path */
 114563   pFrom = aFrom;
 114564   for(ii=1; ii<nFrom; ii++){
 114565     if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
 114567   assert( pWInfo->nLevel==nLoop );
 114568   /* Load the lowest cost path into pWInfo */
 114569   for(iLoop=0; iLoop<nLoop; iLoop++){
 114570     WhereLevel *pLevel = pWInfo->a + iLoop;
 114571     pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
 114572     pLevel->iFrom = pWLoop->iTab;
 114573     pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
 114575   if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
 114576    && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
 114577    && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
 114578    && nRowEst
 114580     Bitmask notUsed;
 114581     int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
 114582                  WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
 114583     if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
 114585   if( pFrom->isOrdered ){
 114586     if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
 114587       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
 114588     }else{
 114589       pWInfo->bOBSat = 1;
 114590       pWInfo->revMask = pFrom->revLoop;
 114593   pWInfo->nRowOut = pFrom->nRow;
 114595   /* Free temporary memory and return success */
 114596   sqlite3DbFree(db, pSpace);
 114597   return SQLITE_OK;
 114601 ** Most queries use only a single table (they are not joins) and have
 114602 ** simple == constraints against indexed fields.  This routine attempts
 114603 ** to plan those simple cases using much less ceremony than the
 114604 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
 114605 ** times for the common case.
 114607 ** Return non-zero on success, if this query can be handled by this
 114608 ** no-frills query planner.  Return zero if this query needs the 
 114609 ** general-purpose query planner.
 114611 static int whereShortCut(WhereLoopBuilder *pBuilder){
 114612   WhereInfo *pWInfo;
 114613   struct SrcList_item *pItem;
 114614   WhereClause *pWC;
 114615   WhereTerm *pTerm;
 114616   WhereLoop *pLoop;
 114617   int iCur;
 114618   int j;
 114619   Table *pTab;
 114620   Index *pIdx;
 114622   pWInfo = pBuilder->pWInfo;
 114623   if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
 114624   assert( pWInfo->pTabList->nSrc>=1 );
 114625   pItem = pWInfo->pTabList->a;
 114626   pTab = pItem->pTab;
 114627   if( IsVirtual(pTab) ) return 0;
 114628   if( pItem->zIndex ) return 0;
 114629   iCur = pItem->iCursor;
 114630   pWC = &pWInfo->sWC;
 114631   pLoop = pBuilder->pNew;
 114632   pLoop->wsFlags = 0;
 114633   pLoop->u.btree.nSkip = 0;
 114634   pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
 114635   if( pTerm ){
 114636     pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
 114637     pLoop->aLTerm[0] = pTerm;
 114638     pLoop->nLTerm = 1;
 114639     pLoop->u.btree.nEq = 1;
 114640     /* TUNING: Cost of a rowid lookup is 10 */
 114641     pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
 114642   }else{
 114643     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 114644       assert( pLoop->aLTermSpace==pLoop->aLTerm );
 114645       assert( ArraySize(pLoop->aLTermSpace)==4 );
 114646       if( pIdx->onError==OE_None 
 114647        || pIdx->pPartIdxWhere!=0 
 114648        || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) 
 114649       ) continue;
 114650       for(j=0; j<pIdx->nKeyCol; j++){
 114651         pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
 114652         if( pTerm==0 ) break;
 114653         pLoop->aLTerm[j] = pTerm;
 114655       if( j!=pIdx->nKeyCol ) continue;
 114656       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
 114657       if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
 114658         pLoop->wsFlags |= WHERE_IDX_ONLY;
 114660       pLoop->nLTerm = j;
 114661       pLoop->u.btree.nEq = j;
 114662       pLoop->u.btree.pIndex = pIdx;
 114663       /* TUNING: Cost of a unique index lookup is 15 */
 114664       pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
 114665       break;
 114668   if( pLoop->wsFlags ){
 114669     pLoop->nOut = (LogEst)1;
 114670     pWInfo->a[0].pWLoop = pLoop;
 114671     pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
 114672     pWInfo->a[0].iTabCur = iCur;
 114673     pWInfo->nRowOut = 1;
 114674     if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
 114675     if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
 114676       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
 114678 #ifdef SQLITE_DEBUG
 114679     pLoop->cId = '0';
 114680 #endif
 114681     return 1;
 114683   return 0;
 114687 ** Generate the beginning of the loop used for WHERE clause processing.
 114688 ** The return value is a pointer to an opaque structure that contains
 114689 ** information needed to terminate the loop.  Later, the calling routine
 114690 ** should invoke sqlite3WhereEnd() with the return value of this function
 114691 ** in order to complete the WHERE clause processing.
 114693 ** If an error occurs, this routine returns NULL.
 114695 ** The basic idea is to do a nested loop, one loop for each table in
 114696 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
 114697 ** same as a SELECT with only a single table in the FROM clause.)  For
 114698 ** example, if the SQL is this:
 114700 **       SELECT * FROM t1, t2, t3 WHERE ...;
 114702 ** Then the code generated is conceptually like the following:
 114704 **      foreach row1 in t1 do       \    Code generated
 114705 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
 114706 **          foreach row3 in t3 do   /
 114707 **            ...
 114708 **          end                     \    Code generated
 114709 **        end                        |-- by sqlite3WhereEnd()
 114710 **      end                         /
 114712 ** Note that the loops might not be nested in the order in which they
 114713 ** appear in the FROM clause if a different order is better able to make
 114714 ** use of indices.  Note also that when the IN operator appears in
 114715 ** the WHERE clause, it might result in additional nested loops for
 114716 ** scanning through all values on the right-hand side of the IN.
 114718 ** There are Btree cursors associated with each table.  t1 uses cursor
 114719 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
 114720 ** And so forth.  This routine generates code to open those VDBE cursors
 114721 ** and sqlite3WhereEnd() generates the code to close them.
 114723 ** The code that sqlite3WhereBegin() generates leaves the cursors named
 114724 ** in pTabList pointing at their appropriate entries.  The [...] code
 114725 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
 114726 ** data from the various tables of the loop.
 114728 ** If the WHERE clause is empty, the foreach loops must each scan their
 114729 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
 114730 ** the tables have indices and there are terms in the WHERE clause that
 114731 ** refer to those indices, a complete table scan can be avoided and the
 114732 ** code will run much faster.  Most of the work of this routine is checking
 114733 ** to see if there are indices that can be used to speed up the loop.
 114735 ** Terms of the WHERE clause are also used to limit which rows actually
 114736 ** make it to the "..." in the middle of the loop.  After each "foreach",
 114737 ** terms of the WHERE clause that use only terms in that loop and outer
 114738 ** loops are evaluated and if false a jump is made around all subsequent
 114739 ** inner loops (or around the "..." if the test occurs within the inner-
 114740 ** most loop)
 114742 ** OUTER JOINS
 114744 ** An outer join of tables t1 and t2 is conceptally coded as follows:
 114746 **    foreach row1 in t1 do
 114747 **      flag = 0
 114748 **      foreach row2 in t2 do
 114749 **        start:
 114750 **          ...
 114751 **          flag = 1
 114752 **      end
 114753 **      if flag==0 then
 114754 **        move the row2 cursor to a null row
 114755 **        goto start
 114756 **      fi
 114757 **    end
 114759 ** ORDER BY CLAUSE PROCESSING
 114761 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
 114762 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
 114763 ** if there is one.  If there is no ORDER BY clause or if this routine
 114764 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
 114766 ** The iIdxCur parameter is the cursor number of an index.  If 
 114767 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
 114768 ** to use for OR clause processing.  The WHERE clause should use this
 114769 ** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
 114770 ** the first cursor in an array of cursors for all indices.  iIdxCur should
 114771 ** be used to compute the appropriate cursor depending on which index is
 114772 ** used.
 114774 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
 114775   Parse *pParse,        /* The parser context */
 114776   SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
 114777   Expr *pWhere,         /* The WHERE clause */
 114778   ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
 114779   ExprList *pResultSet, /* Result set of the query */
 114780   u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
 114781   int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
 114783   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
 114784   int nTabList;              /* Number of elements in pTabList */
 114785   WhereInfo *pWInfo;         /* Will become the return value of this function */
 114786   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
 114787   Bitmask notReady;          /* Cursors that are not yet positioned */
 114788   WhereLoopBuilder sWLB;     /* The WhereLoop builder */
 114789   WhereMaskSet *pMaskSet;    /* The expression mask set */
 114790   WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
 114791   WhereLoop *pLoop;          /* Pointer to a single WhereLoop object */
 114792   int ii;                    /* Loop counter */
 114793   sqlite3 *db;               /* Database connection */
 114794   int rc;                    /* Return code */
 114797   /* Variable initialization */
 114798   db = pParse->db;
 114799   memset(&sWLB, 0, sizeof(sWLB));
 114800   sWLB.pOrderBy = pOrderBy;
 114802   /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
 114803   ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
 114804   if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
 114805     wctrlFlags &= ~WHERE_WANT_DISTINCT;
 114808   /* The number of tables in the FROM clause is limited by the number of
 114809   ** bits in a Bitmask 
 114811   testcase( pTabList->nSrc==BMS );
 114812   if( pTabList->nSrc>BMS ){
 114813     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
 114814     return 0;
 114817   /* This function normally generates a nested loop for all tables in 
 114818   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
 114819   ** only generate code for the first table in pTabList and assume that
 114820   ** any cursors associated with subsequent tables are uninitialized.
 114822   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
 114824   /* Allocate and initialize the WhereInfo structure that will become the
 114825   ** return value. A single allocation is used to store the WhereInfo
 114826   ** struct, the contents of WhereInfo.a[], the WhereClause structure
 114827   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
 114828   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
 114829   ** some architectures. Hence the ROUND8() below.
 114831   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
 114832   pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
 114833   if( db->mallocFailed ){
 114834     sqlite3DbFree(db, pWInfo);
 114835     pWInfo = 0;
 114836     goto whereBeginError;
 114838   pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
 114839   pWInfo->nLevel = nTabList;
 114840   pWInfo->pParse = pParse;
 114841   pWInfo->pTabList = pTabList;
 114842   pWInfo->pOrderBy = pOrderBy;
 114843   pWInfo->pResultSet = pResultSet;
 114844   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
 114845   pWInfo->wctrlFlags = wctrlFlags;
 114846   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
 114847   pMaskSet = &pWInfo->sMaskSet;
 114848   sWLB.pWInfo = pWInfo;
 114849   sWLB.pWC = &pWInfo->sWC;
 114850   sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
 114851   assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
 114852   whereLoopInit(sWLB.pNew);
 114853 #ifdef SQLITE_DEBUG
 114854   sWLB.pNew->cId = '*';
 114855 #endif
 114857   /* Split the WHERE clause into separate subexpressions where each
 114858   ** subexpression is separated by an AND operator.
 114860   initMaskSet(pMaskSet);
 114861   whereClauseInit(&pWInfo->sWC, pWInfo);
 114862   whereSplit(&pWInfo->sWC, pWhere, TK_AND);
 114864   /* Special case: a WHERE clause that is constant.  Evaluate the
 114865   ** expression and either jump over all of the code or fall thru.
 114867   for(ii=0; ii<sWLB.pWC->nTerm; ii++){
 114868     if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
 114869       sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
 114870                          SQLITE_JUMPIFNULL);
 114871       sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
 114875   /* Special case: No FROM clause
 114877   if( nTabList==0 ){
 114878     if( pOrderBy ) pWInfo->bOBSat = 1;
 114879     if( wctrlFlags & WHERE_WANT_DISTINCT ){
 114880       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
 114884   /* Assign a bit from the bitmask to every term in the FROM clause.
 114886   ** When assigning bitmask values to FROM clause cursors, it must be
 114887   ** the case that if X is the bitmask for the N-th FROM clause term then
 114888   ** the bitmask for all FROM clause terms to the left of the N-th term
 114889   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
 114890   ** its Expr.iRightJoinTable value to find the bitmask of the right table
 114891   ** of the join.  Subtracting one from the right table bitmask gives a
 114892   ** bitmask for all tables to the left of the join.  Knowing the bitmask
 114893   ** for all tables to the left of a left join is important.  Ticket #3015.
 114895   ** Note that bitmasks are created for all pTabList->nSrc tables in
 114896   ** pTabList, not just the first nTabList tables.  nTabList is normally
 114897   ** equal to pTabList->nSrc but might be shortened to 1 if the
 114898   ** WHERE_ONETABLE_ONLY flag is set.
 114900   for(ii=0; ii<pTabList->nSrc; ii++){
 114901     createMask(pMaskSet, pTabList->a[ii].iCursor);
 114903 #ifndef NDEBUG
 114905     Bitmask toTheLeft = 0;
 114906     for(ii=0; ii<pTabList->nSrc; ii++){
 114907       Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
 114908       assert( (m-1)==toTheLeft );
 114909       toTheLeft |= m;
 114912 #endif
 114914   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
 114915   ** add new virtual terms onto the end of the WHERE clause.  We do not
 114916   ** want to analyze these virtual terms, so start analyzing at the end
 114917   ** and work forward so that the added virtual terms are never processed.
 114919   exprAnalyzeAll(pTabList, &pWInfo->sWC);
 114920   if( db->mallocFailed ){
 114921     goto whereBeginError;
 114924   if( wctrlFlags & WHERE_WANT_DISTINCT ){
 114925     if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
 114926       /* The DISTINCT marking is pointless.  Ignore it. */
 114927       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
 114928     }else if( pOrderBy==0 ){
 114929       /* Try to ORDER BY the result set to make distinct processing easier */
 114930       pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
 114931       pWInfo->pOrderBy = pResultSet;
 114935   /* Construct the WhereLoop objects */
 114936   WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
 114937   /* Display all terms of the WHERE clause */
 114938 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
 114939   if( sqlite3WhereTrace & 0x100 ){
 114940     int i;
 114941     Vdbe *v = pParse->pVdbe;
 114942     sqlite3ExplainBegin(v);
 114943     for(i=0; i<sWLB.pWC->nTerm; i++){
 114944       sqlite3ExplainPrintf(v, "#%-2d ", i);
 114945       sqlite3ExplainPush(v);
 114946       whereExplainTerm(v, &sWLB.pWC->a[i]);
 114947       sqlite3ExplainPop(v);
 114948       sqlite3ExplainNL(v);
 114950     sqlite3ExplainFinish(v);
 114951     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
 114953 #endif
 114954   if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
 114955     rc = whereLoopAddAll(&sWLB);
 114956     if( rc ) goto whereBeginError;
 114958     /* Display all of the WhereLoop objects if wheretrace is enabled */
 114959 #ifdef WHERETRACE_ENABLED /* !=0 */
 114960     if( sqlite3WhereTrace ){
 114961       WhereLoop *p;
 114962       int i;
 114963       static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
 114964                                        "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
 114965       for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
 114966         p->cId = zLabel[i%sizeof(zLabel)];
 114967         whereLoopPrint(p, sWLB.pWC);
 114970 #endif
 114972     wherePathSolver(pWInfo, 0);
 114973     if( db->mallocFailed ) goto whereBeginError;
 114974     if( pWInfo->pOrderBy ){
 114975        wherePathSolver(pWInfo, pWInfo->nRowOut+1);
 114976        if( db->mallocFailed ) goto whereBeginError;
 114979   if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
 114980      pWInfo->revMask = (Bitmask)(-1);
 114982   if( pParse->nErr || NEVER(db->mallocFailed) ){
 114983     goto whereBeginError;
 114985 #ifdef WHERETRACE_ENABLED /* !=0 */
 114986   if( sqlite3WhereTrace ){
 114987     int ii;
 114988     sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
 114989     if( pWInfo->bOBSat ){
 114990       sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
 114992     switch( pWInfo->eDistinct ){
 114993       case WHERE_DISTINCT_UNIQUE: {
 114994         sqlite3DebugPrintf("  DISTINCT=unique");
 114995         break;
 114997       case WHERE_DISTINCT_ORDERED: {
 114998         sqlite3DebugPrintf("  DISTINCT=ordered");
 114999         break;
 115001       case WHERE_DISTINCT_UNORDERED: {
 115002         sqlite3DebugPrintf("  DISTINCT=unordered");
 115003         break;
 115006     sqlite3DebugPrintf("\n");
 115007     for(ii=0; ii<pWInfo->nLevel; ii++){
 115008       whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
 115011 #endif
 115012   /* Attempt to omit tables from the join that do not effect the result */
 115013   if( pWInfo->nLevel>=2
 115014    && pResultSet!=0
 115015    && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
 115017     Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
 115018     if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
 115019     while( pWInfo->nLevel>=2 ){
 115020       WhereTerm *pTerm, *pEnd;
 115021       pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
 115022       if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
 115023       if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
 115024        && (pLoop->wsFlags & WHERE_ONEROW)==0
 115026         break;
 115028       if( (tabUsed & pLoop->maskSelf)!=0 ) break;
 115029       pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
 115030       for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
 115031         if( (pTerm->prereqAll & pLoop->maskSelf)!=0
 115032          && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
 115034           break;
 115037       if( pTerm<pEnd ) break;
 115038       WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
 115039       pWInfo->nLevel--;
 115040       nTabList--;
 115043   WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
 115044   pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
 115046   /* If the caller is an UPDATE or DELETE statement that is requesting
 115047   ** to use a one-pass algorithm, determine if this is appropriate.
 115048   ** The one-pass algorithm only works if the WHERE clause constrains
 115049   ** the statement to update a single row.
 115051   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
 115052   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
 115053    && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
 115054     pWInfo->okOnePass = 1;
 115055     if( HasRowid(pTabList->a[0].pTab) ){
 115056       pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
 115060   /* Open all tables in the pTabList and any indices selected for
 115061   ** searching those tables.
 115063   notReady = ~(Bitmask)0;
 115064   for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
 115065     Table *pTab;     /* Table to open */
 115066     int iDb;         /* Index of database containing table/index */
 115067     struct SrcList_item *pTabItem;
 115069     pTabItem = &pTabList->a[pLevel->iFrom];
 115070     pTab = pTabItem->pTab;
 115071     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 115072     pLoop = pLevel->pWLoop;
 115073     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
 115074       /* Do nothing */
 115075     }else
 115076 #ifndef SQLITE_OMIT_VIRTUALTABLE
 115077     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
 115078       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
 115079       int iCur = pTabItem->iCursor;
 115080       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
 115081     }else if( IsVirtual(pTab) ){
 115082       /* noop */
 115083     }else
 115084 #endif
 115085     if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
 115086          && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
 115087       int op = OP_OpenRead;
 115088       if( pWInfo->okOnePass ){
 115089         op = OP_OpenWrite;
 115090         pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
 115092       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
 115093       assert( pTabItem->iCursor==pLevel->iTabCur );
 115094       testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
 115095       testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
 115096       if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
 115097         Bitmask b = pTabItem->colUsed;
 115098         int n = 0;
 115099         for(; b; b=b>>1, n++){}
 115100         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
 115101                             SQLITE_INT_TO_PTR(n), P4_INT32);
 115102         assert( n<=pTab->nCol );
 115104     }else{
 115105       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 115107     if( pLoop->wsFlags & WHERE_INDEXED ){
 115108       Index *pIx = pLoop->u.btree.pIndex;
 115109       int iIndexCur;
 115110       int op = OP_OpenRead;
 115111       /* iIdxCur is always set if to a positive value if ONEPASS is possible */
 115112       assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
 115113       if( pWInfo->okOnePass ){
 115114         Index *pJ = pTabItem->pTab->pIndex;
 115115         iIndexCur = iIdxCur;
 115116         assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
 115117         while( ALWAYS(pJ) && pJ!=pIx ){
 115118           iIndexCur++;
 115119           pJ = pJ->pNext;
 115121         op = OP_OpenWrite;
 115122         pWInfo->aiCurOnePass[1] = iIndexCur;
 115123       }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
 115124         iIndexCur = iIdxCur;
 115125       }else{
 115126         iIndexCur = pParse->nTab++;
 115128       pLevel->iIdxCur = iIndexCur;
 115129       assert( pIx->pSchema==pTab->pSchema );
 115130       assert( iIndexCur>=0 );
 115131       sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
 115132       sqlite3VdbeSetP4KeyInfo(pParse, pIx);
 115133       VdbeComment((v, "%s", pIx->zName));
 115135     if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
 115136     notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
 115138   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
 115139   if( db->mallocFailed ) goto whereBeginError;
 115141   /* Generate the code to do the search.  Each iteration of the for
 115142   ** loop below generates code for a single nested loop of the VM
 115143   ** program.
 115145   notReady = ~(Bitmask)0;
 115146   for(ii=0; ii<nTabList; ii++){
 115147     pLevel = &pWInfo->a[ii];
 115148 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
 115149     if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
 115150       constructAutomaticIndex(pParse, &pWInfo->sWC,
 115151                 &pTabList->a[pLevel->iFrom], notReady, pLevel);
 115152       if( db->mallocFailed ) goto whereBeginError;
 115154 #endif
 115155     explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
 115156     pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
 115157     notReady = codeOneLoopStart(pWInfo, ii, notReady);
 115158     pWInfo->iContinue = pLevel->addrCont;
 115161   /* Done. */
 115162   VdbeModuleComment((v, "Begin WHERE-core"));
 115163   return pWInfo;
 115165   /* Jump here if malloc fails */
 115166 whereBeginError:
 115167   if( pWInfo ){
 115168     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
 115169     whereInfoFree(db, pWInfo);
 115171   return 0;
 115175 ** Generate the end of the WHERE loop.  See comments on 
 115176 ** sqlite3WhereBegin() for additional information.
 115178 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
 115179   Parse *pParse = pWInfo->pParse;
 115180   Vdbe *v = pParse->pVdbe;
 115181   int i;
 115182   WhereLevel *pLevel;
 115183   WhereLoop *pLoop;
 115184   SrcList *pTabList = pWInfo->pTabList;
 115185   sqlite3 *db = pParse->db;
 115187   /* Generate loop termination code.
 115189   VdbeModuleComment((v, "End WHERE-core"));
 115190   sqlite3ExprCacheClear(pParse);
 115191   for(i=pWInfo->nLevel-1; i>=0; i--){
 115192     int addr;
 115193     pLevel = &pWInfo->a[i];
 115194     pLoop = pLevel->pWLoop;
 115195     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
 115196     if( pLevel->op!=OP_Noop ){
 115197       sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
 115198       sqlite3VdbeChangeP5(v, pLevel->p5);
 115199       VdbeCoverage(v);
 115200       VdbeCoverageIf(v, pLevel->op==OP_Next);
 115201       VdbeCoverageIf(v, pLevel->op==OP_Prev);
 115202       VdbeCoverageIf(v, pLevel->op==OP_VNext);
 115204     if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
 115205       struct InLoop *pIn;
 115206       int j;
 115207       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
 115208       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
 115209         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
 115210         sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
 115211         VdbeCoverage(v);
 115212         VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
 115213         VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
 115214         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
 115216       sqlite3DbFree(db, pLevel->u.in.aInLoop);
 115218     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
 115219     if( pLevel->addrSkip ){
 115220       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
 115221       VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
 115222       sqlite3VdbeJumpHere(v, pLevel->addrSkip);
 115223       sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
 115225     if( pLevel->iLeftJoin ){
 115226       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
 115227       assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
 115228            || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
 115229       if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
 115230         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
 115232       if( pLoop->wsFlags & WHERE_INDEXED ){
 115233         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
 115235       if( pLevel->op==OP_Return ){
 115236         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
 115237       }else{
 115238         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
 115240       sqlite3VdbeJumpHere(v, addr);
 115242     VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
 115243                      pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
 115246   /* The "break" point is here, just past the end of the outer loop.
 115247   ** Set it.
 115249   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
 115251   assert( pWInfo->nLevel<=pTabList->nSrc );
 115252   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
 115253     int k, last;
 115254     VdbeOp *pOp;
 115255     Index *pIdx = 0;
 115256     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
 115257     Table *pTab = pTabItem->pTab;
 115258     assert( pTab!=0 );
 115259     pLoop = pLevel->pWLoop;
 115261     /* For a co-routine, change all OP_Column references to the table of
 115262     ** the co-routine into OP_SCopy of result contained in a register.
 115263     ** OP_Rowid becomes OP_Null.
 115265     if( pTabItem->viaCoroutine && !db->mallocFailed ){
 115266       last = sqlite3VdbeCurrentAddr(v);
 115267       k = pLevel->addrBody;
 115268       pOp = sqlite3VdbeGetOp(v, k);
 115269       for(; k<last; k++, pOp++){
 115270         if( pOp->p1!=pLevel->iTabCur ) continue;
 115271         if( pOp->opcode==OP_Column ){
 115272           pOp->opcode = OP_SCopy;
 115273           pOp->p1 = pOp->p2 + pTabItem->regResult;
 115274           pOp->p2 = pOp->p3;
 115275           pOp->p3 = 0;
 115276         }else if( pOp->opcode==OP_Rowid ){
 115277           pOp->opcode = OP_Null;
 115278           pOp->p1 = 0;
 115279           pOp->p3 = 0;
 115282       continue;
 115285     /* Close all of the cursors that were opened by sqlite3WhereBegin.
 115286     ** Except, do not close cursors that will be reused by the OR optimization
 115287     ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
 115288     ** created for the ONEPASS optimization.
 115290     if( (pTab->tabFlags & TF_Ephemeral)==0
 115291      && pTab->pSelect==0
 115292      && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
 115294       int ws = pLoop->wsFlags;
 115295       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
 115296         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
 115298       if( (ws & WHERE_INDEXED)!=0
 115299        && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 
 115300        && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
 115302         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
 115306     /* If this scan uses an index, make VDBE code substitutions to read data
 115307     ** from the index instead of from the table where possible.  In some cases
 115308     ** this optimization prevents the table from ever being read, which can
 115309     ** yield a significant performance boost.
 115311     ** Calls to the code generator in between sqlite3WhereBegin and
 115312     ** sqlite3WhereEnd will have created code that references the table
 115313     ** directly.  This loop scans all that code looking for opcodes
 115314     ** that reference the table and converts them into opcodes that
 115315     ** reference the index.
 115317     if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
 115318       pIdx = pLoop->u.btree.pIndex;
 115319     }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
 115320       pIdx = pLevel->u.pCovidx;
 115322     if( pIdx && !db->mallocFailed ){
 115323       last = sqlite3VdbeCurrentAddr(v);
 115324       k = pLevel->addrBody;
 115325       pOp = sqlite3VdbeGetOp(v, k);
 115326       for(; k<last; k++, pOp++){
 115327         if( pOp->p1!=pLevel->iTabCur ) continue;
 115328         if( pOp->opcode==OP_Column ){
 115329           int x = pOp->p2;
 115330           assert( pIdx->pTable==pTab );
 115331           if( !HasRowid(pTab) ){
 115332             Index *pPk = sqlite3PrimaryKeyIndex(pTab);
 115333             x = pPk->aiColumn[x];
 115335           x = sqlite3ColumnOfIndex(pIdx, x);
 115336           if( x>=0 ){
 115337             pOp->p2 = x;
 115338             pOp->p1 = pLevel->iIdxCur;
 115340           assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
 115341         }else if( pOp->opcode==OP_Rowid ){
 115342           pOp->p1 = pLevel->iIdxCur;
 115343           pOp->opcode = OP_IdxRowid;
 115349   /* Final cleanup
 115351   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
 115352   whereInfoFree(db, pWInfo);
 115353   return;
 115356 /************** End of where.c ***********************************************/
 115357 /************** Begin file parse.c *******************************************/
 115358 /* Driver template for the LEMON parser generator.
 115359 ** The author disclaims copyright to this source code.
 115361 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
 115362 ** The only modifications are the addition of a couple of NEVER()
 115363 ** macros to disable tests that are needed in the case of a general
 115364 ** LALR(1) grammar but which are always false in the
 115365 ** specific grammar used by SQLite.
 115367 /* First off, code is included that follows the "include" declaration
 115368 ** in the input grammar file. */
 115369 /* #include <stdio.h> */
 115373 ** Disable all error recovery processing in the parser push-down
 115374 ** automaton.
 115376 #define YYNOERRORRECOVERY 1
 115379 ** Make yytestcase() the same as testcase()
 115381 #define yytestcase(X) testcase(X)
 115384 ** An instance of this structure holds information about the
 115385 ** LIMIT clause of a SELECT statement.
 115387 struct LimitVal {
 115388   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
 115389   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
 115393 ** An instance of this structure is used to store the LIKE,
 115394 ** GLOB, NOT LIKE, and NOT GLOB operators.
 115396 struct LikeOp {
 115397   Token eOperator;  /* "like" or "glob" or "regexp" */
 115398   int bNot;         /* True if the NOT keyword is present */
 115402 ** An instance of the following structure describes the event of a
 115403 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
 115404 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
 115406 **      UPDATE ON (a,b,c)
 115408 ** Then the "b" IdList records the list "a,b,c".
 115410 struct TrigEvent { int a; IdList * b; };
 115413 ** An instance of this structure holds the ATTACH key and the key type.
 115415 struct AttachKey { int type;  Token key; };
 115418   /* This is a utility routine used to set the ExprSpan.zStart and
 115419   ** ExprSpan.zEnd values of pOut so that the span covers the complete
 115420   ** range of text beginning with pStart and going to the end of pEnd.
 115422   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
 115423     pOut->zStart = pStart->z;
 115424     pOut->zEnd = &pEnd->z[pEnd->n];
 115427   /* Construct a new Expr object from a single identifier.  Use the
 115428   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
 115429   ** that created the expression.
 115431   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
 115432     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
 115433     pOut->zStart = pValue->z;
 115434     pOut->zEnd = &pValue->z[pValue->n];
 115437   /* This routine constructs a binary expression node out of two ExprSpan
 115438   ** objects and uses the result to populate a new ExprSpan object.
 115440   static void spanBinaryExpr(
 115441     ExprSpan *pOut,     /* Write the result here */
 115442     Parse *pParse,      /* The parsing context.  Errors accumulate here */
 115443     int op,             /* The binary operation */
 115444     ExprSpan *pLeft,    /* The left operand */
 115445     ExprSpan *pRight    /* The right operand */
 115447     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
 115448     pOut->zStart = pLeft->zStart;
 115449     pOut->zEnd = pRight->zEnd;
 115452   /* Construct an expression node for a unary postfix operator
 115454   static void spanUnaryPostfix(
 115455     ExprSpan *pOut,        /* Write the new expression node here */
 115456     Parse *pParse,         /* Parsing context to record errors */
 115457     int op,                /* The operator */
 115458     ExprSpan *pOperand,    /* The operand */
 115459     Token *pPostOp         /* The operand token for setting the span */
 115461     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
 115462     pOut->zStart = pOperand->zStart;
 115463     pOut->zEnd = &pPostOp->z[pPostOp->n];
 115466   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
 115467   ** unary TK_ISNULL or TK_NOTNULL expression. */
 115468   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
 115469     sqlite3 *db = pParse->db;
 115470     if( db->mallocFailed==0 && pY->op==TK_NULL ){
 115471       pA->op = (u8)op;
 115472       sqlite3ExprDelete(db, pA->pRight);
 115473       pA->pRight = 0;
 115477   /* Construct an expression node for a unary prefix operator
 115479   static void spanUnaryPrefix(
 115480     ExprSpan *pOut,        /* Write the new expression node here */
 115481     Parse *pParse,         /* Parsing context to record errors */
 115482     int op,                /* The operator */
 115483     ExprSpan *pOperand,    /* The operand */
 115484     Token *pPreOp         /* The operand token for setting the span */
 115486     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
 115487     pOut->zStart = pPreOp->z;
 115488     pOut->zEnd = pOperand->zEnd;
 115490 /* Next is all token values, in a form suitable for use by makeheaders.
 115491 ** This section will be null unless lemon is run with the -m switch.
 115494 ** These constants (all generated automatically by the parser generator)
 115495 ** specify the various kinds of tokens (terminals) that the parser
 115496 ** understands. 
 115498 ** Each symbol here is a terminal symbol in the grammar.
 115500 /* Make sure the INTERFACE macro is defined.
 115502 #ifndef INTERFACE
 115503 # define INTERFACE 1
 115504 #endif
 115505 /* The next thing included is series of defines which control
 115506 ** various aspects of the generated parser.
 115507 **    YYCODETYPE         is the data type used for storing terminal
 115508 **                       and nonterminal numbers.  "unsigned char" is
 115509 **                       used if there are fewer than 250 terminals
 115510 **                       and nonterminals.  "int" is used otherwise.
 115511 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
 115512 **                       to no legal terminal or nonterminal number.  This
 115513 **                       number is used to fill in empty slots of the hash 
 115514 **                       table.
 115515 **    YYFALLBACK         If defined, this indicates that one or more tokens
 115516 **                       have fall-back values which should be used if the
 115517 **                       original value of the token will not parse.
 115518 **    YYACTIONTYPE       is the data type used for storing terminal
 115519 **                       and nonterminal numbers.  "unsigned char" is
 115520 **                       used if there are fewer than 250 rules and
 115521 **                       states combined.  "int" is used otherwise.
 115522 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
 115523 **                       directly to the parser from the tokenizer.
 115524 **    YYMINORTYPE        is the data type used for all minor tokens.
 115525 **                       This is typically a union of many types, one of
 115526 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
 115527 **                       for base tokens is called "yy0".
 115528 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
 115529 **                       zero the stack is dynamically sized using realloc()
 115530 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
 115531 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
 115532 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
 115533 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
 115534 **    YYNSTATE           the combined number of states.
 115535 **    YYNRULE            the number of rules in the grammar
 115536 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
 115537 **                       defined, then do no error processing.
 115539 #define YYCODETYPE unsigned char
 115540 #define YYNOCODE 254
 115541 #define YYACTIONTYPE unsigned short int
 115542 #define YYWILDCARD 70
 115543 #define sqlite3ParserTOKENTYPE Token
 115544 typedef union {
 115545   int yyinit;
 115546   sqlite3ParserTOKENTYPE yy0;
 115547   Select* yy3;
 115548   ExprList* yy14;
 115549   With* yy59;
 115550   SrcList* yy65;
 115551   struct LikeOp yy96;
 115552   Expr* yy132;
 115553   u8 yy186;
 115554   int yy328;
 115555   ExprSpan yy346;
 115556   struct TrigEvent yy378;
 115557   u16 yy381;
 115558   IdList* yy408;
 115559   struct {int value; int mask;} yy429;
 115560   TriggerStep* yy473;
 115561   struct LimitVal yy476;
 115562 } YYMINORTYPE;
 115563 #ifndef YYSTACKDEPTH
 115564 #define YYSTACKDEPTH 100
 115565 #endif
 115566 #define sqlite3ParserARG_SDECL Parse *pParse;
 115567 #define sqlite3ParserARG_PDECL ,Parse *pParse
 115568 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
 115569 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
 115570 #define YYNSTATE 642
 115571 #define YYNRULE 327
 115572 #define YYFALLBACK 1
 115573 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
 115574 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
 115575 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
 115577 /* The yyzerominor constant is used to initialize instances of
 115578 ** YYMINORTYPE objects to zero. */
 115579 static const YYMINORTYPE yyzerominor = { 0 };
 115581 /* Define the yytestcase() macro to be a no-op if is not already defined
 115582 ** otherwise.
 115584 ** Applications can choose to define yytestcase() in the %include section
 115585 ** to a macro that can assist in verifying code coverage.  For production
 115586 ** code the yytestcase() macro should be turned off.  But it is useful
 115587 ** for testing.
 115589 #ifndef yytestcase
 115590 # define yytestcase(X)
 115591 #endif
 115594 /* Next are the tables used to determine what action to take based on the
 115595 ** current state and lookahead token.  These tables are used to implement
 115596 ** functions that take a state number and lookahead value and return an
 115597 ** action integer.  
 115599 ** Suppose the action integer is N.  Then the action is determined as
 115600 ** follows
 115602 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
 115603 **                                      token onto the stack and goto state N.
 115605 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
 115607 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
 115609 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
 115611 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
 115612 **                                      slots in the yy_action[] table.
 115614 ** The action table is constructed as a single large table named yy_action[].
 115615 ** Given state S and lookahead X, the action is computed as
 115617 **      yy_action[ yy_shift_ofst[S] + X ]
 115619 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
 115620 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
 115621 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
 115622 ** and that yy_default[S] should be used instead.  
 115624 ** The formula above is for computing the action when the lookahead is
 115625 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
 115626 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
 115627 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
 115628 ** YY_SHIFT_USE_DFLT.
 115630 ** The following are the tables generated in this section:
 115632 **  yy_action[]        A single table containing all actions.
 115633 **  yy_lookahead[]     A table containing the lookahead for each entry in
 115634 **                     yy_action.  Used to detect hash collisions.
 115635 **  yy_shift_ofst[]    For each state, the offset into yy_action for
 115636 **                     shifting terminals.
 115637 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
 115638 **                     shifting non-terminals after a reduce.
 115639 **  yy_default[]       Default action for each state.
 115641 #define YY_ACTTAB_COUNT (1497)
 115642 static const YYACTIONTYPE yy_action[] = {
 115643  /*     0 */   306,  212,  432,  955,  639,  191,  955,  295,  559,   88,
 115644  /*    10 */    88,   88,   88,   81,   86,   86,   86,   86,   85,   85,
 115645  /*    20 */    84,   84,   84,   83,  330,  185,  184,  183,  635,  635,
 115646  /*    30 */   292,  606,  606,   88,   88,   88,   88,  683,   86,   86,
 115647  /*    40 */    86,   86,   85,   85,   84,   84,   84,   83,  330,   16,
 115648  /*    50 */   436,  597,   89,   90,   80,  600,  599,  601,  601,   87,
 115649  /*    60 */    87,   88,   88,   88,   88,  684,   86,   86,   86,   86,
 115650  /*    70 */    85,   85,   84,   84,   84,   83,  330,  306,  559,   84,
 115651  /*    80 */    84,   84,   83,  330,   65,   86,   86,   86,   86,   85,
 115652  /*    90 */    85,   84,   84,   84,   83,  330,  635,  635,  634,  633,
 115653  /*   100 */   182,  682,  550,  379,  376,  375,   17,  322,  606,  606,
 115654  /*   110 */   371,  198,  479,   91,  374,   82,   79,  165,   85,   85,
 115655  /*   120 */    84,   84,   84,   83,  330,  598,  635,  635,  107,   89,
 115656  /*   130 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
 115657  /*   140 */    88,   88,  186,   86,   86,   86,   86,   85,   85,   84,
 115658  /*   150 */    84,   84,   83,  330,  306,  594,  594,  142,  328,  327,
 115659  /*   160 */   484,  249,  344,  238,  635,  635,  634,  633,  585,  448,
 115660  /*   170 */   526,  525,  229,  388,    1,  394,  450,  584,  449,  635,
 115661  /*   180 */   635,  635,  635,  319,  395,  606,  606,  199,  157,  273,
 115662  /*   190 */   382,  268,  381,  187,  635,  635,  634,  633,  311,  555,
 115663  /*   200 */   266,  593,  593,  266,  347,  588,   89,   90,   80,  600,
 115664  /*   210 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  478,
 115665  /*   220 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
 115666  /*   230 */   330,  306,  272,  536,  634,  633,  146,  610,  197,  310,
 115667  /*   240 */   575,  182,  482,  271,  379,  376,  375,  506,   21,  634,
 115668  /*   250 */   633,  634,  633,  635,  635,  374,  611,  574,  548,  440,
 115669  /*   260 */   111,  563,  606,  606,  634,  633,  324,  479,  608,  608,
 115670  /*   270 */   608,  300,  435,  573,  119,  407,  210,  162,  562,  883,
 115671  /*   280 */   592,  592,  306,   89,   90,   80,  600,  599,  601,  601,
 115672  /*   290 */    87,   87,   88,   88,   88,   88,  506,   86,   86,   86,
 115673  /*   300 */    86,   85,   85,   84,   84,   84,   83,  330,  620,  111,
 115674  /*   310 */   635,  635,  361,  606,  606,  358,  249,  349,  248,  433,
 115675  /*   320 */   243,  479,  586,  634,  633,  195,  611,   93,  119,  221,
 115676  /*   330 */   575,  497,  534,  534,   89,   90,   80,  600,  599,  601,
 115677  /*   340 */   601,   87,   87,   88,   88,   88,   88,  574,   86,   86,
 115678  /*   350 */    86,   86,   85,   85,   84,   84,   84,   83,  330,  306,
 115679  /*   360 */    77,  429,  638,  573,  589,  530,  240,  230,  242,  105,
 115680  /*   370 */   249,  349,  248,  515,  588,  208,  460,  529,  564,  173,
 115681  /*   380 */   634,  633,  970,  144,  430,    2,  424,  228,  380,  557,
 115682  /*   390 */   606,  606,  190,  153,  159,  158,  514,   51,  632,  631,
 115683  /*   400 */   630,   71,  536,  432,  954,  196,  610,  954,  614,   45,
 115684  /*   410 */    18,   89,   90,   80,  600,  599,  601,  601,   87,   87,
 115685  /*   420 */    88,   88,   88,   88,  261,   86,   86,   86,   86,   85,
 115686  /*   430 */    85,   84,   84,   84,   83,  330,  306,  608,  608,  608,
 115687  /*   440 */   542,  424,  402,  385,  241,  506,  451,  320,  211,  543,
 115688  /*   450 */   164,  436,  386,  293,  451,  587,  108,  496,  111,  334,
 115689  /*   460 */   391,  591,  424,  614,   27,  452,  453,  606,  606,   72,
 115690  /*   470 */   257,   70,  259,  452,  339,  342,  564,  582,   68,  415,
 115691  /*   480 */   469,  328,  327,   62,  614,   45,  110,  393,   89,   90,
 115692  /*   490 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
 115693  /*   500 */    88,  152,   86,   86,   86,   86,   85,   85,   84,   84,
 115694  /*   510 */    84,   83,  330,  306,  110,  499,  520,  538,  402,  389,
 115695  /*   520 */   424,  110,  566,  500,  593,  593,  454,   82,   79,  165,
 115696  /*   530 */   424,  591,  384,  564,  340,  615,  188,  162,  424,  350,
 115697  /*   540 */   616,  424,  614,   44,  606,  606,  445,  582,  300,  434,
 115698  /*   550 */   151,   19,  614,    9,  568,  580,  348,  615,  469,  567,
 115699  /*   560 */   614,   26,  616,  614,   45,   89,   90,   80,  600,  599,
 115700  /*   570 */   601,  601,   87,   87,   88,   88,   88,   88,  411,   86,
 115701  /*   580 */    86,   86,   86,   85,   85,   84,   84,   84,   83,  330,
 115702  /*   590 */   306,  579,  110,  578,  521,  282,  433,  398,  400,  255,
 115703  /*   600 */   486,   82,   79,  165,  487,  164,   82,   79,  165,  488,
 115704  /*   610 */   488,  364,  387,  424,  544,  544,  509,  350,  362,  155,
 115705  /*   620 */   191,  606,  606,  559,  642,  640,  333,   82,   79,  165,
 115706  /*   630 */   305,  564,  507,  312,  357,  614,   45,  329,  596,  595,
 115707  /*   640 */   194,  337,   89,   90,   80,  600,  599,  601,  601,   87,
 115708  /*   650 */    87,   88,   88,   88,   88,  424,   86,   86,   86,   86,
 115709  /*   660 */    85,   85,   84,   84,   84,   83,  330,  306,   20,  323,
 115710  /*   670 */   150,  263,  211,  543,  421,  596,  595,  614,   22,  424,
 115711  /*   680 */   193,  424,  284,  424,  391,  424,  509,  424,  577,  424,
 115712  /*   690 */   186,  335,  424,  559,  424,  313,  120,  546,  606,  606,
 115713  /*   700 */    67,  614,   47,  614,   50,  614,   48,  614,  100,  614,
 115714  /*   710 */    99,  614,  101,  576,  614,  102,  614,  109,  326,   89,
 115715  /*   720 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
 115716  /*   730 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
 115717  /*   740 */    84,   84,   83,  330,  306,  424,  311,  424,  585,   54,
 115718  /*   750 */   424,  516,  517,  590,  614,  112,  424,  584,  424,  572,
 115719  /*   760 */   424,  195,  424,  571,  424,   67,  424,  614,   94,  614,
 115720  /*   770 */    98,  424,  614,   97,  264,  606,  606,  195,  614,   46,
 115721  /*   780 */   614,   96,  614,   30,  614,   49,  614,  115,  614,  114,
 115722  /*   790 */   418,  229,  388,  614,  113,  306,   89,   90,   80,  600,
 115723  /*   800 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  424,
 115724  /*   810 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
 115725  /*   820 */   330,  119,  424,  590,  110,  372,  606,  606,  195,   53,
 115726  /*   830 */   250,  614,   29,  195,  472,  438,  729,  190,  302,  498,
 115727  /*   840 */    14,  523,  641,    2,  614,   43,  306,   89,   90,   80,
 115728  /*   850 */   600,  599,  601,  601,   87,   87,   88,   88,   88,   88,
 115729  /*   860 */   424,   86,   86,   86,   86,   85,   85,   84,   84,   84,
 115730  /*   870 */    83,  330,  424,  613,  964,  964,  354,  606,  606,  420,
 115731  /*   880 */   312,   64,  614,   42,  391,  355,  283,  437,  301,  255,
 115732  /*   890 */   414,  410,  495,  492,  614,   28,  471,  306,   89,   90,
 115733  /*   900 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
 115734  /*   910 */    88,  424,   86,   86,   86,   86,   85,   85,   84,   84,
 115735  /*   920 */    84,   83,  330,  424,  110,  110,  110,  110,  606,  606,
 115736  /*   930 */   110,  254,   13,  614,   41,  532,  531,  283,  481,  531,
 115737  /*   940 */   457,  284,  119,  561,  356,  614,   40,  284,  306,   89,
 115738  /*   950 */    78,   80,  600,  599,  601,  601,   87,   87,   88,   88,
 115739  /*   960 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
 115740  /*   970 */    84,   84,   83,  330,  110,  424,  341,  220,  555,  606,
 115741  /*   980 */   606,  351,  555,  318,  614,   95,  413,  255,   83,  330,
 115742  /*   990 */   284,  284,  255,  640,  333,  356,  255,  614,   39,  306,
 115743  /*  1000 */   356,   90,   80,  600,  599,  601,  601,   87,   87,   88,
 115744  /*  1010 */    88,   88,   88,  424,   86,   86,   86,   86,   85,   85,
 115745  /*  1020 */    84,   84,   84,   83,  330,  424,  317,  316,  141,  465,
 115746  /*  1030 */   606,  606,  219,  619,  463,  614,   10,  417,  462,  255,
 115747  /*  1040 */   189,  510,  553,  351,  207,  363,  161,  614,   38,  315,
 115748  /*  1050 */   218,  255,  255,   80,  600,  599,  601,  601,   87,   87,
 115749  /*  1060 */    88,   88,   88,   88,  424,   86,   86,   86,   86,   85,
 115750  /*  1070 */    85,   84,   84,   84,   83,  330,   76,  419,  255,    3,
 115751  /*  1080 */   878,  461,  424,  247,  331,  331,  614,   37,  217,   76,
 115752  /*  1090 */   419,  390,    3,  216,  215,  422,    4,  331,  331,  424,
 115753  /*  1100 */   547,   12,  424,  545,  614,   36,  424,  541,  422,  424,
 115754  /*  1110 */   540,  424,  214,  424,  408,  424,  539,  403,  605,  605,
 115755  /*  1120 */   237,  614,   25,  119,  614,   24,  588,  408,  614,   45,
 115756  /*  1130 */   118,  614,   35,  614,   34,  614,   33,  614,   23,  588,
 115757  /*  1140 */    60,  223,  603,  602,  513,  378,   73,   74,  140,  139,
 115758  /*  1150 */   424,  110,  265,   75,  426,  425,   59,  424,  610,   73,
 115759  /*  1160 */    74,  549,  402,  404,  424,  373,   75,  426,  425,  604,
 115760  /*  1170 */   138,  610,  614,   11,  392,   76,  419,  181,    3,  614,
 115761  /*  1180 */    32,  271,  369,  331,  331,  493,  614,   31,  149,  608,
 115762  /*  1190 */   608,  608,  607,   15,  422,  365,  614,    8,  137,  489,
 115763  /*  1200 */   136,  190,  608,  608,  608,  607,   15,  485,  176,  135,
 115764  /*  1210 */     7,  252,  477,  408,  174,  133,  175,  474,   57,   56,
 115765  /*  1220 */   132,  130,  119,   76,  419,  588,    3,  468,  245,  464,
 115766  /*  1230 */   171,  331,  331,  125,  123,  456,  447,  122,  446,  104,
 115767  /*  1240 */   336,  231,  422,  166,  154,   73,   74,  332,  116,  431,
 115768  /*  1250 */   121,  309,   75,  426,  425,  222,  106,  610,  308,  637,
 115769  /*  1260 */   204,  408,  629,  627,  628,    6,  200,  428,  427,  290,
 115770  /*  1270 */   203,  622,  201,  588,   62,   63,  289,   66,  419,  399,
 115771  /*  1280 */     3,  401,  288,   92,  143,  331,  331,  287,  608,  608,
 115772  /*  1290 */   608,  607,   15,   73,   74,  227,  422,  325,   69,  416,
 115773  /*  1300 */    75,  426,  425,  612,  412,  610,  192,   61,  569,  209,
 115774  /*  1310 */   396,  226,  278,  225,  383,  408,  527,  558,  276,  533,
 115775  /*  1320 */   552,  528,  321,  523,  370,  508,  180,  588,  494,  179,
 115776  /*  1330 */   366,  117,  253,  269,  522,  503,  608,  608,  608,  607,
 115777  /*  1340 */    15,  551,  502,   58,  274,  524,  178,   73,   74,  304,
 115778  /*  1350 */   501,  368,  303,  206,   75,  426,  425,  491,  360,  610,
 115779  /*  1360 */   213,  177,  483,  131,  345,  298,  297,  296,  202,  294,
 115780  /*  1370 */   480,  490,  466,  134,  172,  129,  444,  346,  470,  128,
 115781  /*  1380 */   314,  459,  103,  127,  126,  148,  124,  167,  443,  235,
 115782  /*  1390 */   608,  608,  608,  607,   15,  442,  439,  623,  234,  299,
 115783  /*  1400 */   145,  583,  291,  377,  581,  160,  119,  156,  270,  636,
 115784  /*  1410 */   971,  169,  279,  626,  520,  625,  473,  624,  170,  621,
 115785  /*  1420 */   618,  119,  168,   55,  409,  423,  537,  609,  286,  285,
 115786  /*  1430 */   405,  570,  560,  556,    5,   52,  458,  554,  147,  267,
 115787  /*  1440 */   519,  504,  518,  406,  262,  239,  260,  512,  343,  511,
 115788  /*  1450 */   258,  353,  565,  256,  224,  251,  359,  277,  275,  476,
 115789  /*  1460 */   475,  246,  352,  244,  467,  455,  236,  233,  232,  307,
 115790  /*  1470 */   441,  281,  205,  163,  397,  280,  535,  505,  330,  617,
 115791  /*  1480 */   971,  971,  971,  971,  367,  971,  971,  971,  971,  971,
 115792  /*  1490 */   971,  971,  971,  971,  971,  971,  338,
 115794 static const YYCODETYPE yy_lookahead[] = {
 115795  /*     0 */    19,   22,   22,   23,    1,   24,   26,   15,   27,   80,
 115796  /*    10 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
 115797  /*    20 */    91,   92,   93,   94,   95,  108,  109,  110,   27,   28,
 115798  /*    30 */    23,   50,   51,   80,   81,   82,   83,  122,   85,   86,
 115799  /*    40 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   22,
 115800  /*    50 */    70,   23,   71,   72,   73,   74,   75,   76,   77,   78,
 115801  /*    60 */    79,   80,   81,   82,   83,  122,   85,   86,   87,   88,
 115802  /*    70 */    89,   90,   91,   92,   93,   94,   95,   19,   97,   91,
 115803  /*    80 */    92,   93,   94,   95,   26,   85,   86,   87,   88,   89,
 115804  /*    90 */    90,   91,   92,   93,   94,   95,   27,   28,   97,   98,
 115805  /*   100 */    99,  122,  211,  102,  103,  104,   79,   19,   50,   51,
 115806  /*   110 */    19,  122,   59,   55,  113,  224,  225,  226,   89,   90,
 115807  /*   120 */    91,   92,   93,   94,   95,   23,   27,   28,   26,   71,
 115808  /*   130 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
 115809  /*   140 */    82,   83,   51,   85,   86,   87,   88,   89,   90,   91,
 115810  /*   150 */    92,   93,   94,   95,   19,  132,  133,   58,   89,   90,
 115811  /*   160 */    21,  108,  109,  110,   27,   28,   97,   98,   33,  100,
 115812  /*   170 */     7,    8,  119,  120,   22,   19,  107,   42,  109,   27,
 115813  /*   180 */    28,   27,   28,   95,   28,   50,   51,   99,  100,  101,
 115814  /*   190 */   102,  103,  104,  105,   27,   28,   97,   98,  107,  152,
 115815  /*   200 */   112,  132,  133,  112,   65,   69,   71,   72,   73,   74,
 115816  /*   210 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   11,
 115817  /*   220 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
 115818  /*   230 */    95,   19,  101,   97,   97,   98,   24,  101,  122,  157,
 115819  /*   240 */    12,   99,  103,  112,  102,  103,  104,  152,   22,   97,
 115820  /*   250 */    98,   97,   98,   27,   28,  113,   27,   29,   91,  164,
 115821  /*   260 */   165,  124,   50,   51,   97,   98,  219,   59,  132,  133,
 115822  /*   270 */   134,   22,   23,   45,   66,   47,  212,  213,  124,  140,
 115823  /*   280 */   132,  133,   19,   71,   72,   73,   74,   75,   76,   77,
 115824  /*   290 */    78,   79,   80,   81,   82,   83,  152,   85,   86,   87,
 115825  /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,  164,  165,
 115826  /*   310 */    27,   28,  230,   50,   51,  233,  108,  109,  110,   70,
 115827  /*   320 */    16,   59,   23,   97,   98,   26,   97,   22,   66,  185,
 115828  /*   330 */    12,  187,   27,   28,   71,   72,   73,   74,   75,   76,
 115829  /*   340 */    77,   78,   79,   80,   81,   82,   83,   29,   85,   86,
 115830  /*   350 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   19,
 115831  /*   360 */    22,  148,  149,   45,   23,   47,   62,  154,   64,  156,
 115832  /*   370 */   108,  109,  110,   37,   69,   23,  163,   59,   26,   26,
 115833  /*   380 */    97,   98,  144,  145,  146,  147,  152,  200,   52,   23,
 115834  /*   390 */    50,   51,   26,   22,   89,   90,   60,  210,    7,    8,
 115835  /*   400 */     9,  138,   97,   22,   23,   26,  101,   26,  174,  175,
 115836  /*   410 */   197,   71,   72,   73,   74,   75,   76,   77,   78,   79,
 115837  /*   420 */    80,   81,   82,   83,   16,   85,   86,   87,   88,   89,
 115838  /*   430 */    90,   91,   92,   93,   94,   95,   19,  132,  133,  134,
 115839  /*   440 */    23,  152,  208,  209,  140,  152,  152,  111,  195,  196,
 115840  /*   450 */    98,   70,  163,  160,  152,   23,   22,  164,  165,  246,
 115841  /*   460 */   207,   27,  152,  174,  175,  171,  172,   50,   51,  137,
 115842  /*   470 */    62,  139,   64,  171,  172,  222,  124,   27,  138,   24,
 115843  /*   480 */   163,   89,   90,  130,  174,  175,  197,  163,   71,   72,
 115844  /*   490 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
 115845  /*   500 */    83,   22,   85,   86,   87,   88,   89,   90,   91,   92,
 115846  /*   510 */    93,   94,   95,   19,  197,  181,  182,   23,  208,  209,
 115847  /*   520 */   152,  197,   26,  189,  132,  133,  232,  224,  225,  226,
 115848  /*   530 */   152,   97,   91,   26,  232,  116,  212,  213,  152,  222,
 115849  /*   540 */   121,  152,  174,  175,   50,   51,  243,   97,   22,   23,
 115850  /*   550 */    22,  234,  174,  175,  177,   23,  239,  116,  163,  177,
 115851  /*   560 */   174,  175,  121,  174,  175,   71,   72,   73,   74,   75,
 115852  /*   570 */    76,   77,   78,   79,   80,   81,   82,   83,   24,   85,
 115853  /*   580 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
 115854  /*   590 */    19,   23,  197,   11,   23,  227,   70,  208,  220,  152,
 115855  /*   600 */    31,  224,  225,  226,   35,   98,  224,  225,  226,  108,
 115856  /*   610 */   109,  110,  115,  152,  117,  118,   27,  222,   49,  123,
 115857  /*   620 */    24,   50,   51,   27,    0,    1,    2,  224,  225,  226,
 115858  /*   630 */   166,  124,  168,  169,  239,  174,  175,  170,  171,  172,
 115859  /*   640 */    22,  194,   71,   72,   73,   74,   75,   76,   77,   78,
 115860  /*   650 */    79,   80,   81,   82,   83,  152,   85,   86,   87,   88,
 115861  /*   660 */    89,   90,   91,   92,   93,   94,   95,   19,   22,  208,
 115862  /*   670 */    24,   23,  195,  196,  170,  171,  172,  174,  175,  152,
 115863  /*   680 */    26,  152,  152,  152,  207,  152,   97,  152,   23,  152,
 115864  /*   690 */    51,  244,  152,   97,  152,  247,  248,   23,   50,   51,
 115865  /*   700 */    26,  174,  175,  174,  175,  174,  175,  174,  175,  174,
 115866  /*   710 */   175,  174,  175,   23,  174,  175,  174,  175,  188,   71,
 115867  /*   720 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
 115868  /*   730 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
 115869  /*   740 */    92,   93,   94,   95,   19,  152,  107,  152,   33,   24,
 115870  /*   750 */   152,  100,  101,   27,  174,  175,  152,   42,  152,   23,
 115871  /*   760 */   152,   26,  152,   23,  152,   26,  152,  174,  175,  174,
 115872  /*   770 */   175,  152,  174,  175,   23,   50,   51,   26,  174,  175,
 115873  /*   780 */   174,  175,  174,  175,  174,  175,  174,  175,  174,  175,
 115874  /*   790 */   163,  119,  120,  174,  175,   19,   71,   72,   73,   74,
 115875  /*   800 */    75,   76,   77,   78,   79,   80,   81,   82,   83,  152,
 115876  /*   810 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
 115877  /*   820 */    95,   66,  152,   97,  197,   23,   50,   51,   26,   53,
 115878  /*   830 */    23,  174,  175,   26,   23,   23,   23,   26,   26,   26,
 115879  /*   840 */    36,  106,  146,  147,  174,  175,   19,   71,   72,   73,
 115880  /*   850 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   83,
 115881  /*   860 */   152,   85,   86,   87,   88,   89,   90,   91,   92,   93,
 115882  /*   870 */    94,   95,  152,  196,  119,  120,   19,   50,   51,  168,
 115883  /*   880 */   169,   26,  174,  175,  207,   28,  152,  249,  250,  152,
 115884  /*   890 */   163,  163,  163,  163,  174,  175,  163,   19,   71,   72,
 115885  /*   900 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
 115886  /*   910 */    83,  152,   85,   86,   87,   88,   89,   90,   91,   92,
 115887  /*   920 */    93,   94,   95,  152,  197,  197,  197,  197,   50,   51,
 115888  /*   930 */   197,  194,   36,  174,  175,  191,  192,  152,  191,  192,
 115889  /*   940 */   163,  152,   66,  124,  152,  174,  175,  152,   19,   71,
 115890  /*   950 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
 115891  /*   960 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
 115892  /*   970 */    92,   93,   94,   95,  197,  152,  100,  188,  152,   50,
 115893  /*   980 */    51,  152,  152,  188,  174,  175,  252,  152,   94,   95,
 115894  /*   990 */   152,  152,  152,    1,    2,  152,  152,  174,  175,   19,
 115895  /*  1000 */   152,   72,   73,   74,   75,   76,   77,   78,   79,   80,
 115896  /*  1010 */    81,   82,   83,  152,   85,   86,   87,   88,   89,   90,
 115897  /*  1020 */    91,   92,   93,   94,   95,  152,  188,  188,   22,  194,
 115898  /*  1030 */    50,   51,  240,  173,  194,  174,  175,  252,  194,  152,
 115899  /*  1040 */    36,  181,   28,  152,   23,  219,  122,  174,  175,  219,
 115900  /*  1050 */   221,  152,  152,   73,   74,   75,   76,   77,   78,   79,
 115901  /*  1060 */    80,   81,   82,   83,  152,   85,   86,   87,   88,   89,
 115902  /*  1070 */    90,   91,   92,   93,   94,   95,   19,   20,  152,   22,
 115903  /*  1080 */    23,  194,  152,  240,   27,   28,  174,  175,  240,   19,
 115904  /*  1090 */    20,   26,   22,  194,  194,   38,   22,   27,   28,  152,
 115905  /*  1100 */    23,   22,  152,  116,  174,  175,  152,   23,   38,  152,
 115906  /*  1110 */    23,  152,  221,  152,   57,  152,   23,  163,   50,   51,
 115907  /*  1120 */   194,  174,  175,   66,  174,  175,   69,   57,  174,  175,
 115908  /*  1130 */    40,  174,  175,  174,  175,  174,  175,  174,  175,   69,
 115909  /*  1140 */    22,   53,   74,   75,   30,   53,   89,   90,   22,   22,
 115910  /*  1150 */   152,  197,   23,   96,   97,   98,   22,  152,  101,   89,
 115911  /*  1160 */    90,   91,  208,  209,  152,   53,   96,   97,   98,  101,
 115912  /*  1170 */    22,  101,  174,  175,  152,   19,   20,  105,   22,  174,
 115913  /*  1180 */   175,  112,   19,   27,   28,   20,  174,  175,   24,  132,
 115914  /*  1190 */   133,  134,  135,  136,   38,   44,  174,  175,  107,   61,
 115915  /*  1200 */    54,   26,  132,  133,  134,  135,  136,   54,  107,   22,
 115916  /*  1210 */     5,  140,    1,   57,   36,  111,  122,   28,   79,   79,
 115917  /*  1220 */   131,  123,   66,   19,   20,   69,   22,    1,   16,   20,
 115918  /*  1230 */   125,   27,   28,  123,  111,  120,   23,  131,   23,   16,
 115919  /*  1240 */    68,  142,   38,   15,   22,   89,   90,    3,  167,    4,
 115920  /*  1250 */   248,  251,   96,   97,   98,  180,  180,  101,  251,  151,
 115921  /*  1260 */     6,   57,  151,   13,  151,   26,   25,  151,  161,  202,
 115922  /*  1270 */   153,  162,  153,   69,  130,  128,  203,   19,   20,  127,
 115923  /*  1280 */    22,  126,  204,  129,   22,   27,   28,  205,  132,  133,
 115924  /*  1290 */   134,  135,  136,   89,   90,  231,   38,   95,  137,  179,
 115925  /*  1300 */    96,   97,   98,  206,  179,  101,  122,  107,  159,  159,
 115926  /*  1310 */   125,  231,  216,  228,  107,   57,  184,  217,  216,  176,
 115927  /*  1320 */   217,  176,   48,  106,   18,  184,  158,   69,  159,  158,
 115928  /*  1330 */    46,   71,  237,  176,  176,  176,  132,  133,  134,  135,
 115929  /*  1340 */   136,  217,  176,  137,  216,  178,  158,   89,   90,  179,
 115930  /*  1350 */   176,  159,  179,  159,   96,   97,   98,  159,  159,  101,
 115931  /*  1360 */     5,  158,  202,   22,   18,   10,   11,   12,   13,   14,
 115932  /*  1370 */   190,  238,   17,  190,  158,  193,   41,  159,  202,  193,
 115933  /*  1380 */   159,  202,  245,  193,  193,  223,  190,   32,  159,   34,
 115934  /*  1390 */   132,  133,  134,  135,  136,  159,   39,  155,   43,  150,
 115935  /*  1400 */   223,  177,  201,  178,  177,  186,   66,  199,  177,  152,
 115936  /*  1410 */   253,   56,  215,  152,  182,  152,  202,  152,   63,  152,
 115937  /*  1420 */   152,   66,   67,  242,  229,  152,  174,  152,  152,  152,
 115938  /*  1430 */   152,  152,  152,  152,  199,  242,  202,  152,  198,  152,
 115939  /*  1440 */   152,  152,  183,  192,  152,  215,  152,  183,  215,  183,
 115940  /*  1450 */   152,  241,  214,  152,  211,  152,  152,  211,  211,  152,
 115941  /*  1460 */   152,  241,  152,  152,  152,  152,  152,  152,  152,  114,
 115942  /*  1470 */   152,  152,  235,  152,  152,  152,  174,  187,   95,  174,
 115943  /*  1480 */   253,  253,  253,  253,  236,  253,  253,  253,  253,  253,
 115944  /*  1490 */   253,  253,  253,  253,  253,  253,  141,
 115946 #define YY_SHIFT_USE_DFLT (-86)
 115947 #define YY_SHIFT_COUNT (429)
 115948 #define YY_SHIFT_MIN   (-85)
 115949 #define YY_SHIFT_MAX   (1383)
 115950 static const short yy_shift_ofst[] = {
 115951  /*     0 */   992, 1057, 1355, 1156, 1204, 1204,    1,  262,  -19,  135,
 115952  /*    10 */   135,  776, 1204, 1204, 1204, 1204,   69,   69,   53,  208,
 115953  /*    20 */   283,  755,   58,  725,  648,  571,  494,  417,  340,  263,
 115954  /*    30 */   212,  827,  827,  827,  827,  827,  827,  827,  827,  827,
 115955  /*    40 */   827,  827,  827,  827,  827,  827,  878,  827,  929,  980,
 115956  /*    50 */   980, 1070, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
 115957  /*    60 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
 115958  /*    70 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
 115959  /*    80 */  1258, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
 115960  /*    90 */  1204, 1204, 1204, 1204,  -71,  -47,  -47,  -47,  -47,  -47,
 115961  /*   100 */     0,   29,  -12,  283,  283,  139,   91,  392,  392,  894,
 115962  /*   110 */   672,  726, 1383,  -86,  -86,  -86,   88,  318,  318,   99,
 115963  /*   120 */   381,  -20,  283,  283,  283,  283,  283,  283,  283,  283,
 115964  /*   130 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
 115965  /*   140 */   283,  283,  283,  283,  624,  876,  726,  672, 1340, 1340,
 115966  /*   150 */  1340, 1340, 1340, 1340,  -86,  -86,  -86,  305,  136,  136,
 115967  /*   160 */   142,  167,  226,  154,  137,  152,  283,  283,  283,  283,
 115968  /*   170 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
 115969  /*   180 */   283,  283,  283,  336,  336,  336,  283,  283,  352,  283,
 115970  /*   190 */   283,  283,  283,  283,  228,  283,  283,  283,  283,  283,
 115971  /*   200 */   283,  283,  283,  283,  283,  501,  569,  596,  596,  596,
 115972  /*   210 */   507,  497,  441,  391,  353,  156,  156,  857,  353,  857,
 115973  /*   220 */   735,  813,  639,  715,  156,  332,  715,  715,  496,  419,
 115974  /*   230 */   646, 1357, 1184, 1184, 1335, 1335, 1184, 1341, 1260, 1144,
 115975  /*   240 */  1346, 1346, 1346, 1346, 1184, 1306, 1144, 1341, 1260, 1260,
 115976  /*   250 */  1144, 1184, 1306, 1206, 1284, 1184, 1184, 1306, 1184, 1306,
 115977  /*   260 */  1184, 1306, 1262, 1207, 1207, 1207, 1274, 1262, 1207, 1217,
 115978  /*   270 */  1207, 1274, 1207, 1207, 1185, 1200, 1185, 1200, 1185, 1200,
 115979  /*   280 */  1184, 1184, 1161, 1262, 1202, 1202, 1262, 1154, 1155, 1147,
 115980  /*   290 */  1152, 1144, 1241, 1239, 1250, 1250, 1254, 1254, 1254, 1254,
 115981  /*   300 */   -86,  -86,  -86,  -86,  -86,  -86, 1068,  304,  526,  249,
 115982  /*   310 */   408,  -83,  434,  812,   27,  811,  807,  802,  751,  589,
 115983  /*   320 */   651,  163,  131,  674,  366,  450,  299,  148,   23,  102,
 115984  /*   330 */   229,  -21, 1245, 1244, 1222, 1099, 1228, 1172, 1223, 1215,
 115985  /*   340 */  1213, 1115, 1106, 1123, 1110, 1209, 1105, 1212, 1226, 1098,
 115986  /*   350 */  1089, 1140, 1139, 1104, 1189, 1178, 1094, 1211, 1205, 1187,
 115987  /*   360 */  1101, 1071, 1153, 1175, 1146, 1138, 1151, 1091, 1164, 1165,
 115988  /*   370 */  1163, 1069, 1072, 1148, 1112, 1134, 1127, 1129, 1126, 1092,
 115989  /*   380 */  1114, 1118, 1088, 1090, 1093, 1087, 1084,  987, 1079, 1077,
 115990  /*   390 */  1074, 1065,  924, 1021, 1014, 1004, 1006,  819,  739,  896,
 115991  /*   400 */   855,  804,  739,  740,  736,  690,  654,  665,  618,  582,
 115992  /*   410 */   568,  528,  554,  379,  532,  479,  455,  379,  432,  371,
 115993  /*   420 */   341,   28,  338,  116,  -11,  -57,  -85,    7,   -8,    3,
 115995 #define YY_REDUCE_USE_DFLT (-110)
 115996 #define YY_REDUCE_COUNT (305)
 115997 #define YY_REDUCE_MIN   (-109)
 115998 #define YY_REDUCE_MAX   (1323)
 115999 static const short yy_reduce_ofst[] = {
 116000  /*     0 */   238,  954,  213,  289,  310,  234,  144,  317, -109,  382,
 116001  /*    10 */   377,  303,  461,  389,  378,  368,  302,  294,  253,  395,
 116002  /*    20 */   293,  324,  403,  403,  403,  403,  403,  403,  403,  403,
 116003  /*    30 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
 116004  /*    40 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
 116005  /*    50 */   403, 1022, 1012, 1005,  998,  963,  961,  959,  957,  950,
 116006  /*    60 */   947,  930,  912,  873,  861,  823,  810,  771,  759,  720,
 116007  /*    70 */   708,  670,  657,  619,  614,  612,  610,  608,  606,  604,
 116008  /*    80 */   598,  595,  593,  580,  542,  540,  537,  535,  533,  531,
 116009  /*    90 */   529,  527,  503,  386,  403,  403,  403,  403,  403,  403,
 116010  /*   100 */   403,  403,  403,   95,  447,   82,  334,  504,  467,  403,
 116011  /*   110 */   477,  464,  403,  403,  403,  403,  860,  747,  744,  785,
 116012  /*   120 */   638,  638,  926,  891,  900,  899,  887,  844,  840,  835,
 116013  /*   130 */   848,  830,  843,  829,  792,  839,  826,  737,  838,  795,
 116014  /*   140 */   789,   47,  734,  530,  696,  777,  711,  677,  733,  730,
 116015  /*   150 */   729,  728,  727,  627,  448,   64,  187, 1305, 1302, 1252,
 116016  /*   160 */  1290, 1273, 1323, 1322, 1321, 1319, 1318, 1316, 1315, 1314,
 116017  /*   170 */  1313, 1312, 1311, 1310, 1308, 1307, 1304, 1303, 1301, 1298,
 116018  /*   180 */  1294, 1292, 1289, 1266, 1264, 1259, 1288, 1287, 1238, 1285,
 116019  /*   190 */  1281, 1280, 1279, 1278, 1251, 1277, 1276, 1275, 1273, 1268,
 116020  /*   200 */  1267, 1265, 1263, 1261, 1257, 1248, 1237, 1247, 1246, 1243,
 116021  /*   210 */  1238, 1240, 1235, 1249, 1234, 1233, 1230, 1220, 1214, 1210,
 116022  /*   220 */  1225, 1219, 1232, 1231, 1197, 1195, 1227, 1224, 1201, 1208,
 116023  /*   230 */  1242, 1137, 1236, 1229, 1193, 1181, 1221, 1177, 1196, 1179,
 116024  /*   240 */  1191, 1190, 1186, 1182, 1218, 1216, 1176, 1162, 1183, 1180,
 116025  /*   250 */  1160, 1199, 1203, 1133, 1095, 1198, 1194, 1188, 1192, 1171,
 116026  /*   260 */  1169, 1168, 1173, 1174, 1166, 1159, 1141, 1170, 1158, 1167,
 116027  /*   270 */  1157, 1132, 1145, 1143, 1124, 1128, 1103, 1102, 1100, 1096,
 116028  /*   280 */  1150, 1149, 1085, 1125, 1080, 1064, 1120, 1097, 1082, 1078,
 116029  /*   290 */  1073, 1067, 1109, 1107, 1119, 1117, 1116, 1113, 1111, 1108,
 116030  /*   300 */  1007, 1000, 1002, 1076, 1075, 1081,
 116032 static const YYACTIONTYPE yy_default[] = {
 116033  /*     0 */   647,  964,  964,  964,  878,  878,  969,  964,  774,  802,
 116034  /*    10 */   802,  938,  969,  969,  969,  876,  969,  969,  969,  964,
 116035  /*    20 */   969,  778,  808,  969,  969,  969,  969,  969,  969,  969,
 116036  /*    30 */   969,  937,  939,  816,  815,  918,  789,  813,  806,  810,
 116037  /*    40 */   879,  872,  873,  871,  875,  880,  969,  809,  841,  856,
 116038  /*    50 */   840,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116039  /*    60 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116040  /*    70 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116041  /*    80 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116042  /*    90 */   969,  969,  969,  969,  850,  855,  862,  854,  851,  843,
 116043  /*   100 */   842,  844,  845,  969,  969,  673,  739,  969,  969,  846,
 116044  /*   110 */   969,  685,  847,  859,  858,  857,  680,  969,  969,  969,
 116045  /*   120 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116046  /*   130 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116047  /*   140 */   969,  969,  969,  969,  647,  964,  969,  969,  964,  964,
 116048  /*   150 */   964,  964,  964,  964,  956,  778,  768,  969,  969,  969,
 116049  /*   160 */   969,  969,  969,  969,  969,  969,  969,  944,  942,  969,
 116050  /*   170 */   891,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116051  /*   180 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116052  /*   190 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116053  /*   200 */   969,  969,  969,  969,  653,  969,  911,  774,  774,  774,
 116054  /*   210 */   776,  754,  766,  655,  812,  791,  791,  923,  812,  923,
 116055  /*   220 */   710,  733,  707,  802,  791,  874,  802,  802,  775,  766,
 116056  /*   230 */   969,  949,  782,  782,  941,  941,  782,  821,  743,  812,
 116057  /*   240 */   750,  750,  750,  750,  782,  670,  812,  821,  743,  743,
 116058  /*   250 */   812,  782,  670,  917,  915,  782,  782,  670,  782,  670,
 116059  /*   260 */   782,  670,  884,  741,  741,  741,  725,  884,  741,  710,
 116060  /*   270 */   741,  725,  741,  741,  795,  790,  795,  790,  795,  790,
 116061  /*   280 */   782,  782,  969,  884,  888,  888,  884,  807,  796,  805,
 116062  /*   290 */   803,  812,  676,  728,  663,  663,  652,  652,  652,  652,
 116063  /*   300 */   961,  961,  956,  712,  712,  695,  969,  969,  969,  969,
 116064  /*   310 */   969,  969,  687,  969,  893,  969,  969,  969,  969,  969,
 116065  /*   320 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116066  /*   330 */   969,  828,  969,  648,  951,  969,  969,  948,  969,  969,
 116067  /*   340 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116068  /*   350 */   969,  969,  969,  969,  969,  969,  921,  969,  969,  969,
 116069  /*   360 */   969,  969,  969,  914,  913,  969,  969,  969,  969,  969,
 116070  /*   370 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
 116071  /*   380 */   969,  969,  969,  969,  969,  969,  969,  757,  969,  969,
 116072  /*   390 */   969,  761,  969,  969,  969,  969,  969,  969,  804,  969,
 116073  /*   400 */   797,  969,  877,  969,  969,  969,  969,  969,  969,  969,
 116074  /*   410 */   969,  969,  969,  966,  969,  969,  969,  965,  969,  969,
 116075  /*   420 */   969,  969,  969,  830,  969,  829,  833,  969,  661,  969,
 116076  /*   430 */   644,  649,  960,  963,  962,  959,  958,  957,  952,  950,
 116077  /*   440 */   947,  946,  945,  943,  940,  936,  897,  895,  902,  901,
 116078  /*   450 */   900,  899,  898,  896,  894,  892,  818,  817,  814,  811,
 116079  /*   460 */   753,  935,  890,  752,  749,  748,  669,  953,  920,  929,
 116080  /*   470 */   928,  927,  822,  926,  925,  924,  922,  919,  906,  820,
 116081  /*   480 */   819,  744,  882,  881,  672,  910,  909,  908,  912,  916,
 116082  /*   490 */   907,  784,  751,  671,  668,  675,  679,  731,  732,  740,
 116083  /*   500 */   738,  737,  736,  735,  734,  730,  681,  686,  724,  709,
 116084  /*   510 */   708,  717,  716,  722,  721,  720,  719,  718,  715,  714,
 116085  /*   520 */   713,  706,  705,  711,  704,  727,  726,  723,  703,  747,
 116086  /*   530 */   746,  745,  742,  702,  701,  700,  833,  699,  698,  838,
 116087  /*   540 */   837,  866,  826,  755,  759,  758,  762,  763,  771,  770,
 116088  /*   550 */   769,  780,  781,  793,  792,  824,  823,  794,  779,  773,
 116089  /*   560 */   772,  788,  787,  786,  785,  777,  767,  799,  798,  868,
 116090  /*   570 */   783,  867,  865,  934,  933,  932,  931,  930,  870,  967,
 116091  /*   580 */   968,  887,  889,  886,  801,  800,  885,  869,  839,  836,
 116092  /*   590 */   690,  691,  905,  904,  903,  693,  692,  689,  688,  863,
 116093  /*   600 */   860,  852,  864,  861,  853,  849,  848,  834,  832,  831,
 116094  /*   610 */   827,  835,  760,  756,  825,  765,  764,  697,  696,  694,
 116095  /*   620 */   678,  677,  674,  667,  665,  664,  666,  662,  660,  659,
 116096  /*   630 */   658,  657,  656,  684,  683,  682,  654,  651,  650,  646,
 116097  /*   640 */   645,  643,
 116100 /* The next table maps tokens into fallback tokens.  If a construct
 116101 ** like the following:
 116103 **      %fallback ID X Y Z.
 116105 ** appears in the grammar, then ID becomes a fallback token for X, Y,
 116106 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
 116107 ** but it does not parse, the type of the token is changed to ID and
 116108 ** the parse is retried before an error is thrown.
 116110 #ifdef YYFALLBACK
 116111 static const YYCODETYPE yyFallback[] = {
 116112     0,  /*          $ => nothing */
 116113     0,  /*       SEMI => nothing */
 116114    27,  /*    EXPLAIN => ID */
 116115    27,  /*      QUERY => ID */
 116116    27,  /*       PLAN => ID */
 116117    27,  /*      BEGIN => ID */
 116118     0,  /* TRANSACTION => nothing */
 116119    27,  /*   DEFERRED => ID */
 116120    27,  /*  IMMEDIATE => ID */
 116121    27,  /*  EXCLUSIVE => ID */
 116122     0,  /*     COMMIT => nothing */
 116123    27,  /*        END => ID */
 116124    27,  /*   ROLLBACK => ID */
 116125    27,  /*  SAVEPOINT => ID */
 116126    27,  /*    RELEASE => ID */
 116127     0,  /*         TO => nothing */
 116128     0,  /*      TABLE => nothing */
 116129     0,  /*     CREATE => nothing */
 116130    27,  /*         IF => ID */
 116131     0,  /*        NOT => nothing */
 116132     0,  /*     EXISTS => nothing */
 116133    27,  /*       TEMP => ID */
 116134     0,  /*         LP => nothing */
 116135     0,  /*         RP => nothing */
 116136     0,  /*         AS => nothing */
 116137    27,  /*    WITHOUT => ID */
 116138     0,  /*      COMMA => nothing */
 116139     0,  /*         ID => nothing */
 116140     0,  /*    INDEXED => nothing */
 116141    27,  /*      ABORT => ID */
 116142    27,  /*     ACTION => ID */
 116143    27,  /*      AFTER => ID */
 116144    27,  /*    ANALYZE => ID */
 116145    27,  /*        ASC => ID */
 116146    27,  /*     ATTACH => ID */
 116147    27,  /*     BEFORE => ID */
 116148    27,  /*         BY => ID */
 116149    27,  /*    CASCADE => ID */
 116150    27,  /*       CAST => ID */
 116151    27,  /*   COLUMNKW => ID */
 116152    27,  /*   CONFLICT => ID */
 116153    27,  /*   DATABASE => ID */
 116154    27,  /*       DESC => ID */
 116155    27,  /*     DETACH => ID */
 116156    27,  /*       EACH => ID */
 116157    27,  /*       FAIL => ID */
 116158    27,  /*        FOR => ID */
 116159    27,  /*     IGNORE => ID */
 116160    27,  /*  INITIALLY => ID */
 116161    27,  /*    INSTEAD => ID */
 116162    27,  /*    LIKE_KW => ID */
 116163    27,  /*      MATCH => ID */
 116164    27,  /*         NO => ID */
 116165    27,  /*        KEY => ID */
 116166    27,  /*         OF => ID */
 116167    27,  /*     OFFSET => ID */
 116168    27,  /*     PRAGMA => ID */
 116169    27,  /*      RAISE => ID */
 116170    27,  /*  RECURSIVE => ID */
 116171    27,  /*    REPLACE => ID */
 116172    27,  /*   RESTRICT => ID */
 116173    27,  /*        ROW => ID */
 116174    27,  /*    TRIGGER => ID */
 116175    27,  /*     VACUUM => ID */
 116176    27,  /*       VIEW => ID */
 116177    27,  /*    VIRTUAL => ID */
 116178    27,  /*       WITH => ID */
 116179    27,  /*    REINDEX => ID */
 116180    27,  /*     RENAME => ID */
 116181    27,  /*   CTIME_KW => ID */
 116183 #endif /* YYFALLBACK */
 116185 /* The following structure represents a single element of the
 116186 ** parser's stack.  Information stored includes:
 116188 **   +  The state number for the parser at this level of the stack.
 116190 **   +  The value of the token stored at this level of the stack.
 116191 **      (In other words, the "major" token.)
 116193 **   +  The semantic value stored at this level of the stack.  This is
 116194 **      the information used by the action routines in the grammar.
 116195 **      It is sometimes called the "minor" token.
 116197 struct yyStackEntry {
 116198   YYACTIONTYPE stateno;  /* The state-number */
 116199   YYCODETYPE major;      /* The major token value.  This is the code
 116200                          ** number for the token at this stack level */
 116201   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
 116202                          ** is the value of the token  */
 116204 typedef struct yyStackEntry yyStackEntry;
 116206 /* The state of the parser is completely contained in an instance of
 116207 ** the following structure */
 116208 struct yyParser {
 116209   int yyidx;                    /* Index of top element in stack */
 116210 #ifdef YYTRACKMAXSTACKDEPTH
 116211   int yyidxMax;                 /* Maximum value of yyidx */
 116212 #endif
 116213   int yyerrcnt;                 /* Shifts left before out of the error */
 116214   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
 116215 #if YYSTACKDEPTH<=0
 116216   int yystksz;                  /* Current side of the stack */
 116217   yyStackEntry *yystack;        /* The parser's stack */
 116218 #else
 116219   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
 116220 #endif
 116222 typedef struct yyParser yyParser;
 116224 #ifndef NDEBUG
 116225 /* #include <stdio.h> */
 116226 static FILE *yyTraceFILE = 0;
 116227 static char *yyTracePrompt = 0;
 116228 #endif /* NDEBUG */
 116230 #ifndef NDEBUG
 116232 ** Turn parser tracing on by giving a stream to which to write the trace
 116233 ** and a prompt to preface each trace message.  Tracing is turned off
 116234 ** by making either argument NULL 
 116236 ** Inputs:
 116237 ** <ul>
 116238 ** <li> A FILE* to which trace output should be written.
 116239 **      If NULL, then tracing is turned off.
 116240 ** <li> A prefix string written at the beginning of every
 116241 **      line of trace output.  If NULL, then tracing is
 116242 **      turned off.
 116243 ** </ul>
 116245 ** Outputs:
 116246 ** None.
 116248 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
 116249   yyTraceFILE = TraceFILE;
 116250   yyTracePrompt = zTracePrompt;
 116251   if( yyTraceFILE==0 ) yyTracePrompt = 0;
 116252   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
 116254 #endif /* NDEBUG */
 116256 #ifndef NDEBUG
 116257 /* For tracing shifts, the names of all terminals and nonterminals
 116258 ** are required.  The following table supplies these names */
 116259 static const char *const yyTokenName[] = { 
 116260   "$",             "SEMI",          "EXPLAIN",       "QUERY",       
 116261   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
 116262   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
 116263   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
 116264   "TABLE",         "CREATE",        "IF",            "NOT",         
 116265   "EXISTS",        "TEMP",          "LP",            "RP",          
 116266   "AS",            "WITHOUT",       "COMMA",         "ID",          
 116267   "INDEXED",       "ABORT",         "ACTION",        "AFTER",       
 116268   "ANALYZE",       "ASC",           "ATTACH",        "BEFORE",      
 116269   "BY",            "CASCADE",       "CAST",          "COLUMNKW",    
 116270   "CONFLICT",      "DATABASE",      "DESC",          "DETACH",      
 116271   "EACH",          "FAIL",          "FOR",           "IGNORE",      
 116272   "INITIALLY",     "INSTEAD",       "LIKE_KW",       "MATCH",       
 116273   "NO",            "KEY",           "OF",            "OFFSET",      
 116274   "PRAGMA",        "RAISE",         "RECURSIVE",     "REPLACE",     
 116275   "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",      
 116276   "VIEW",          "VIRTUAL",       "WITH",          "REINDEX",     
 116277   "RENAME",        "CTIME_KW",      "ANY",           "OR",          
 116278   "AND",           "IS",            "BETWEEN",       "IN",          
 116279   "ISNULL",        "NOTNULL",       "NE",            "EQ",          
 116280   "GT",            "LE",            "LT",            "GE",          
 116281   "ESCAPE",        "BITAND",        "BITOR",         "LSHIFT",      
 116282   "RSHIFT",        "PLUS",          "MINUS",         "STAR",        
 116283   "SLASH",         "REM",           "CONCAT",        "COLLATE",     
 116284   "BITNOT",        "STRING",        "JOIN_KW",       "CONSTRAINT",  
 116285   "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",      
 116286   "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",          
 116287   "INSERT",        "DELETE",        "UPDATE",        "SET",         
 116288   "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",       
 116289   "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",      
 116290   "VALUES",        "DISTINCT",      "DOT",           "FROM",        
 116291   "JOIN",          "USING",         "ORDER",         "GROUP",       
 116292   "HAVING",        "LIMIT",         "WHERE",         "INTO",        
 116293   "INTEGER",       "FLOAT",         "BLOB",          "VARIABLE",    
 116294   "CASE",          "WHEN",          "THEN",          "ELSE",        
 116295   "INDEX",         "ALTER",         "ADD",           "error",       
 116296   "input",         "cmdlist",       "ecmd",          "explain",     
 116297   "cmdx",          "cmd",           "transtype",     "trans_opt",   
 116298   "nm",            "savepoint_opt",  "create_table",  "create_table_args",
 116299   "createkw",      "temp",          "ifnotexists",   "dbnm",        
 116300   "columnlist",    "conslist_opt",  "table_options",  "select",      
 116301   "column",        "columnid",      "type",          "carglist",    
 116302   "typetoken",     "typename",      "signed",        "plus_num",    
 116303   "minus_num",     "ccons",         "term",          "expr",        
 116304   "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
 116305   "refargs",       "defer_subclause",  "refarg",        "refact",      
 116306   "init_deferred_pred_opt",  "conslist",      "tconscomma",    "tcons",       
 116307   "idxlist",       "defer_subclause_opt",  "orconf",        "resolvetype", 
 116308   "raisetype",     "ifexists",      "fullname",      "selectnowith",
 116309   "oneselect",     "with",          "multiselect_op",  "distinct",    
 116310   "selcollist",    "from",          "where_opt",     "groupby_opt", 
 116311   "having_opt",    "orderby_opt",   "limit_opt",     "values",      
 116312   "nexprlist",     "exprlist",      "sclp",          "as",          
 116313   "seltablist",    "stl_prefix",    "joinop",        "indexed_opt", 
 116314   "on_opt",        "using_opt",     "joinop2",       "idlist",      
 116315   "sortlist",      "setlist",       "insert_cmd",    "inscollist_opt",
 116316   "likeop",        "between_op",    "in_op",         "case_operand",
 116317   "case_exprlist",  "case_else",     "uniqueflag",    "collate",     
 116318   "nmnum",         "trigger_decl",  "trigger_cmd_list",  "trigger_time",
 116319   "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd", 
 116320   "trnm",          "tridxby",       "database_kw_opt",  "key_opt",     
 116321   "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist", 
 116322   "vtabarg",       "vtabargtoken",  "lp",            "anylist",     
 116323   "wqlist",      
 116325 #endif /* NDEBUG */
 116327 #ifndef NDEBUG
 116328 /* For tracing reduce actions, the names of all rules are required.
 116330 static const char *const yyRuleName[] = {
 116331  /*   0 */ "input ::= cmdlist",
 116332  /*   1 */ "cmdlist ::= cmdlist ecmd",
 116333  /*   2 */ "cmdlist ::= ecmd",
 116334  /*   3 */ "ecmd ::= SEMI",
 116335  /*   4 */ "ecmd ::= explain cmdx SEMI",
 116336  /*   5 */ "explain ::=",
 116337  /*   6 */ "explain ::= EXPLAIN",
 116338  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
 116339  /*   8 */ "cmdx ::= cmd",
 116340  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
 116341  /*  10 */ "trans_opt ::=",
 116342  /*  11 */ "trans_opt ::= TRANSACTION",
 116343  /*  12 */ "trans_opt ::= TRANSACTION nm",
 116344  /*  13 */ "transtype ::=",
 116345  /*  14 */ "transtype ::= DEFERRED",
 116346  /*  15 */ "transtype ::= IMMEDIATE",
 116347  /*  16 */ "transtype ::= EXCLUSIVE",
 116348  /*  17 */ "cmd ::= COMMIT trans_opt",
 116349  /*  18 */ "cmd ::= END trans_opt",
 116350  /*  19 */ "cmd ::= ROLLBACK trans_opt",
 116351  /*  20 */ "savepoint_opt ::= SAVEPOINT",
 116352  /*  21 */ "savepoint_opt ::=",
 116353  /*  22 */ "cmd ::= SAVEPOINT nm",
 116354  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
 116355  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
 116356  /*  25 */ "cmd ::= create_table create_table_args",
 116357  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
 116358  /*  27 */ "createkw ::= CREATE",
 116359  /*  28 */ "ifnotexists ::=",
 116360  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
 116361  /*  30 */ "temp ::= TEMP",
 116362  /*  31 */ "temp ::=",
 116363  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
 116364  /*  33 */ "create_table_args ::= AS select",
 116365  /*  34 */ "table_options ::=",
 116366  /*  35 */ "table_options ::= WITHOUT nm",
 116367  /*  36 */ "columnlist ::= columnlist COMMA column",
 116368  /*  37 */ "columnlist ::= column",
 116369  /*  38 */ "column ::= columnid type carglist",
 116370  /*  39 */ "columnid ::= nm",
 116371  /*  40 */ "nm ::= ID|INDEXED",
 116372  /*  41 */ "nm ::= STRING",
 116373  /*  42 */ "nm ::= JOIN_KW",
 116374  /*  43 */ "type ::=",
 116375  /*  44 */ "type ::= typetoken",
 116376  /*  45 */ "typetoken ::= typename",
 116377  /*  46 */ "typetoken ::= typename LP signed RP",
 116378  /*  47 */ "typetoken ::= typename LP signed COMMA signed RP",
 116379  /*  48 */ "typename ::= ID|STRING",
 116380  /*  49 */ "typename ::= typename ID|STRING",
 116381  /*  50 */ "signed ::= plus_num",
 116382  /*  51 */ "signed ::= minus_num",
 116383  /*  52 */ "carglist ::= carglist ccons",
 116384  /*  53 */ "carglist ::=",
 116385  /*  54 */ "ccons ::= CONSTRAINT nm",
 116386  /*  55 */ "ccons ::= DEFAULT term",
 116387  /*  56 */ "ccons ::= DEFAULT LP expr RP",
 116388  /*  57 */ "ccons ::= DEFAULT PLUS term",
 116389  /*  58 */ "ccons ::= DEFAULT MINUS term",
 116390  /*  59 */ "ccons ::= DEFAULT ID|INDEXED",
 116391  /*  60 */ "ccons ::= NULL onconf",
 116392  /*  61 */ "ccons ::= NOT NULL onconf",
 116393  /*  62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
 116394  /*  63 */ "ccons ::= UNIQUE onconf",
 116395  /*  64 */ "ccons ::= CHECK LP expr RP",
 116396  /*  65 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
 116397  /*  66 */ "ccons ::= defer_subclause",
 116398  /*  67 */ "ccons ::= COLLATE ID|STRING",
 116399  /*  68 */ "autoinc ::=",
 116400  /*  69 */ "autoinc ::= AUTOINCR",
 116401  /*  70 */ "refargs ::=",
 116402  /*  71 */ "refargs ::= refargs refarg",
 116403  /*  72 */ "refarg ::= MATCH nm",
 116404  /*  73 */ "refarg ::= ON INSERT refact",
 116405  /*  74 */ "refarg ::= ON DELETE refact",
 116406  /*  75 */ "refarg ::= ON UPDATE refact",
 116407  /*  76 */ "refact ::= SET NULL",
 116408  /*  77 */ "refact ::= SET DEFAULT",
 116409  /*  78 */ "refact ::= CASCADE",
 116410  /*  79 */ "refact ::= RESTRICT",
 116411  /*  80 */ "refact ::= NO ACTION",
 116412  /*  81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
 116413  /*  82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
 116414  /*  83 */ "init_deferred_pred_opt ::=",
 116415  /*  84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
 116416  /*  85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
 116417  /*  86 */ "conslist_opt ::=",
 116418  /*  87 */ "conslist_opt ::= COMMA conslist",
 116419  /*  88 */ "conslist ::= conslist tconscomma tcons",
 116420  /*  89 */ "conslist ::= tcons",
 116421  /*  90 */ "tconscomma ::= COMMA",
 116422  /*  91 */ "tconscomma ::=",
 116423  /*  92 */ "tcons ::= CONSTRAINT nm",
 116424  /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
 116425  /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
 116426  /*  95 */ "tcons ::= CHECK LP expr RP onconf",
 116427  /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
 116428  /*  97 */ "defer_subclause_opt ::=",
 116429  /*  98 */ "defer_subclause_opt ::= defer_subclause",
 116430  /*  99 */ "onconf ::=",
 116431  /* 100 */ "onconf ::= ON CONFLICT resolvetype",
 116432  /* 101 */ "orconf ::=",
 116433  /* 102 */ "orconf ::= OR resolvetype",
 116434  /* 103 */ "resolvetype ::= raisetype",
 116435  /* 104 */ "resolvetype ::= IGNORE",
 116436  /* 105 */ "resolvetype ::= REPLACE",
 116437  /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
 116438  /* 107 */ "ifexists ::= IF EXISTS",
 116439  /* 108 */ "ifexists ::=",
 116440  /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
 116441  /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
 116442  /* 111 */ "cmd ::= select",
 116443  /* 112 */ "select ::= with selectnowith",
 116444  /* 113 */ "selectnowith ::= oneselect",
 116445  /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
 116446  /* 115 */ "multiselect_op ::= UNION",
 116447  /* 116 */ "multiselect_op ::= UNION ALL",
 116448  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
 116449  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
 116450  /* 119 */ "oneselect ::= values",
 116451  /* 120 */ "values ::= VALUES LP nexprlist RP",
 116452  /* 121 */ "values ::= values COMMA LP exprlist RP",
 116453  /* 122 */ "distinct ::= DISTINCT",
 116454  /* 123 */ "distinct ::= ALL",
 116455  /* 124 */ "distinct ::=",
 116456  /* 125 */ "sclp ::= selcollist COMMA",
 116457  /* 126 */ "sclp ::=",
 116458  /* 127 */ "selcollist ::= sclp expr as",
 116459  /* 128 */ "selcollist ::= sclp STAR",
 116460  /* 129 */ "selcollist ::= sclp nm DOT STAR",
 116461  /* 130 */ "as ::= AS nm",
 116462  /* 131 */ "as ::= ID|STRING",
 116463  /* 132 */ "as ::=",
 116464  /* 133 */ "from ::=",
 116465  /* 134 */ "from ::= FROM seltablist",
 116466  /* 135 */ "stl_prefix ::= seltablist joinop",
 116467  /* 136 */ "stl_prefix ::=",
 116468  /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
 116469  /* 138 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
 116470  /* 139 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
 116471  /* 140 */ "dbnm ::=",
 116472  /* 141 */ "dbnm ::= DOT nm",
 116473  /* 142 */ "fullname ::= nm dbnm",
 116474  /* 143 */ "joinop ::= COMMA|JOIN",
 116475  /* 144 */ "joinop ::= JOIN_KW JOIN",
 116476  /* 145 */ "joinop ::= JOIN_KW nm JOIN",
 116477  /* 146 */ "joinop ::= JOIN_KW nm nm JOIN",
 116478  /* 147 */ "on_opt ::= ON expr",
 116479  /* 148 */ "on_opt ::=",
 116480  /* 149 */ "indexed_opt ::=",
 116481  /* 150 */ "indexed_opt ::= INDEXED BY nm",
 116482  /* 151 */ "indexed_opt ::= NOT INDEXED",
 116483  /* 152 */ "using_opt ::= USING LP idlist RP",
 116484  /* 153 */ "using_opt ::=",
 116485  /* 154 */ "orderby_opt ::=",
 116486  /* 155 */ "orderby_opt ::= ORDER BY sortlist",
 116487  /* 156 */ "sortlist ::= sortlist COMMA expr sortorder",
 116488  /* 157 */ "sortlist ::= expr sortorder",
 116489  /* 158 */ "sortorder ::= ASC",
 116490  /* 159 */ "sortorder ::= DESC",
 116491  /* 160 */ "sortorder ::=",
 116492  /* 161 */ "groupby_opt ::=",
 116493  /* 162 */ "groupby_opt ::= GROUP BY nexprlist",
 116494  /* 163 */ "having_opt ::=",
 116495  /* 164 */ "having_opt ::= HAVING expr",
 116496  /* 165 */ "limit_opt ::=",
 116497  /* 166 */ "limit_opt ::= LIMIT expr",
 116498  /* 167 */ "limit_opt ::= LIMIT expr OFFSET expr",
 116499  /* 168 */ "limit_opt ::= LIMIT expr COMMA expr",
 116500  /* 169 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
 116501  /* 170 */ "where_opt ::=",
 116502  /* 171 */ "where_opt ::= WHERE expr",
 116503  /* 172 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
 116504  /* 173 */ "setlist ::= setlist COMMA nm EQ expr",
 116505  /* 174 */ "setlist ::= nm EQ expr",
 116506  /* 175 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt select",
 116507  /* 176 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
 116508  /* 177 */ "insert_cmd ::= INSERT orconf",
 116509  /* 178 */ "insert_cmd ::= REPLACE",
 116510  /* 179 */ "inscollist_opt ::=",
 116511  /* 180 */ "inscollist_opt ::= LP idlist RP",
 116512  /* 181 */ "idlist ::= idlist COMMA nm",
 116513  /* 182 */ "idlist ::= nm",
 116514  /* 183 */ "expr ::= term",
 116515  /* 184 */ "expr ::= LP expr RP",
 116516  /* 185 */ "term ::= NULL",
 116517  /* 186 */ "expr ::= ID|INDEXED",
 116518  /* 187 */ "expr ::= JOIN_KW",
 116519  /* 188 */ "expr ::= nm DOT nm",
 116520  /* 189 */ "expr ::= nm DOT nm DOT nm",
 116521  /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
 116522  /* 191 */ "term ::= STRING",
 116523  /* 192 */ "expr ::= VARIABLE",
 116524  /* 193 */ "expr ::= expr COLLATE ID|STRING",
 116525  /* 194 */ "expr ::= CAST LP expr AS typetoken RP",
 116526  /* 195 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
 116527  /* 196 */ "expr ::= ID|INDEXED LP STAR RP",
 116528  /* 197 */ "term ::= CTIME_KW",
 116529  /* 198 */ "expr ::= expr AND expr",
 116530  /* 199 */ "expr ::= expr OR expr",
 116531  /* 200 */ "expr ::= expr LT|GT|GE|LE expr",
 116532  /* 201 */ "expr ::= expr EQ|NE expr",
 116533  /* 202 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
 116534  /* 203 */ "expr ::= expr PLUS|MINUS expr",
 116535  /* 204 */ "expr ::= expr STAR|SLASH|REM expr",
 116536  /* 205 */ "expr ::= expr CONCAT expr",
 116537  /* 206 */ "likeop ::= LIKE_KW|MATCH",
 116538  /* 207 */ "likeop ::= NOT LIKE_KW|MATCH",
 116539  /* 208 */ "expr ::= expr likeop expr",
 116540  /* 209 */ "expr ::= expr likeop expr ESCAPE expr",
 116541  /* 210 */ "expr ::= expr ISNULL|NOTNULL",
 116542  /* 211 */ "expr ::= expr NOT NULL",
 116543  /* 212 */ "expr ::= expr IS expr",
 116544  /* 213 */ "expr ::= expr IS NOT expr",
 116545  /* 214 */ "expr ::= NOT expr",
 116546  /* 215 */ "expr ::= BITNOT expr",
 116547  /* 216 */ "expr ::= MINUS expr",
 116548  /* 217 */ "expr ::= PLUS expr",
 116549  /* 218 */ "between_op ::= BETWEEN",
 116550  /* 219 */ "between_op ::= NOT BETWEEN",
 116551  /* 220 */ "expr ::= expr between_op expr AND expr",
 116552  /* 221 */ "in_op ::= IN",
 116553  /* 222 */ "in_op ::= NOT IN",
 116554  /* 223 */ "expr ::= expr in_op LP exprlist RP",
 116555  /* 224 */ "expr ::= LP select RP",
 116556  /* 225 */ "expr ::= expr in_op LP select RP",
 116557  /* 226 */ "expr ::= expr in_op nm dbnm",
 116558  /* 227 */ "expr ::= EXISTS LP select RP",
 116559  /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
 116560  /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
 116561  /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
 116562  /* 231 */ "case_else ::= ELSE expr",
 116563  /* 232 */ "case_else ::=",
 116564  /* 233 */ "case_operand ::= expr",
 116565  /* 234 */ "case_operand ::=",
 116566  /* 235 */ "exprlist ::= nexprlist",
 116567  /* 236 */ "exprlist ::=",
 116568  /* 237 */ "nexprlist ::= nexprlist COMMA expr",
 116569  /* 238 */ "nexprlist ::= expr",
 116570  /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt",
 116571  /* 240 */ "uniqueflag ::= UNIQUE",
 116572  /* 241 */ "uniqueflag ::=",
 116573  /* 242 */ "idxlist_opt ::=",
 116574  /* 243 */ "idxlist_opt ::= LP idxlist RP",
 116575  /* 244 */ "idxlist ::= idxlist COMMA nm collate sortorder",
 116576  /* 245 */ "idxlist ::= nm collate sortorder",
 116577  /* 246 */ "collate ::=",
 116578  /* 247 */ "collate ::= COLLATE ID|STRING",
 116579  /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
 116580  /* 249 */ "cmd ::= VACUUM",
 116581  /* 250 */ "cmd ::= VACUUM nm",
 116582  /* 251 */ "cmd ::= PRAGMA nm dbnm",
 116583  /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
 116584  /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
 116585  /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
 116586  /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
 116587  /* 256 */ "nmnum ::= plus_num",
 116588  /* 257 */ "nmnum ::= nm",
 116589  /* 258 */ "nmnum ::= ON",
 116590  /* 259 */ "nmnum ::= DELETE",
 116591  /* 260 */ "nmnum ::= DEFAULT",
 116592  /* 261 */ "plus_num ::= PLUS INTEGER|FLOAT",
 116593  /* 262 */ "plus_num ::= INTEGER|FLOAT",
 116594  /* 263 */ "minus_num ::= MINUS INTEGER|FLOAT",
 116595  /* 264 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
 116596  /* 265 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
 116597  /* 266 */ "trigger_time ::= BEFORE",
 116598  /* 267 */ "trigger_time ::= AFTER",
 116599  /* 268 */ "trigger_time ::= INSTEAD OF",
 116600  /* 269 */ "trigger_time ::=",
 116601  /* 270 */ "trigger_event ::= DELETE|INSERT",
 116602  /* 271 */ "trigger_event ::= UPDATE",
 116603  /* 272 */ "trigger_event ::= UPDATE OF idlist",
 116604  /* 273 */ "foreach_clause ::=",
 116605  /* 274 */ "foreach_clause ::= FOR EACH ROW",
 116606  /* 275 */ "when_clause ::=",
 116607  /* 276 */ "when_clause ::= WHEN expr",
 116608  /* 277 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
 116609  /* 278 */ "trigger_cmd_list ::= trigger_cmd SEMI",
 116610  /* 279 */ "trnm ::= nm",
 116611  /* 280 */ "trnm ::= nm DOT nm",
 116612  /* 281 */ "tridxby ::=",
 116613  /* 282 */ "tridxby ::= INDEXED BY nm",
 116614  /* 283 */ "tridxby ::= NOT INDEXED",
 116615  /* 284 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
 116616  /* 285 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
 116617  /* 286 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
 116618  /* 287 */ "trigger_cmd ::= select",
 116619  /* 288 */ "expr ::= RAISE LP IGNORE RP",
 116620  /* 289 */ "expr ::= RAISE LP raisetype COMMA nm RP",
 116621  /* 290 */ "raisetype ::= ROLLBACK",
 116622  /* 291 */ "raisetype ::= ABORT",
 116623  /* 292 */ "raisetype ::= FAIL",
 116624  /* 293 */ "cmd ::= DROP TRIGGER ifexists fullname",
 116625  /* 294 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
 116626  /* 295 */ "cmd ::= DETACH database_kw_opt expr",
 116627  /* 296 */ "key_opt ::=",
 116628  /* 297 */ "key_opt ::= KEY expr",
 116629  /* 298 */ "database_kw_opt ::= DATABASE",
 116630  /* 299 */ "database_kw_opt ::=",
 116631  /* 300 */ "cmd ::= REINDEX",
 116632  /* 301 */ "cmd ::= REINDEX nm dbnm",
 116633  /* 302 */ "cmd ::= ANALYZE",
 116634  /* 303 */ "cmd ::= ANALYZE nm dbnm",
 116635  /* 304 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
 116636  /* 305 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
 116637  /* 306 */ "add_column_fullname ::= fullname",
 116638  /* 307 */ "kwcolumn_opt ::=",
 116639  /* 308 */ "kwcolumn_opt ::= COLUMNKW",
 116640  /* 309 */ "cmd ::= create_vtab",
 116641  /* 310 */ "cmd ::= create_vtab LP vtabarglist RP",
 116642  /* 311 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
 116643  /* 312 */ "vtabarglist ::= vtabarg",
 116644  /* 313 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
 116645  /* 314 */ "vtabarg ::=",
 116646  /* 315 */ "vtabarg ::= vtabarg vtabargtoken",
 116647  /* 316 */ "vtabargtoken ::= ANY",
 116648  /* 317 */ "vtabargtoken ::= lp anylist RP",
 116649  /* 318 */ "lp ::= LP",
 116650  /* 319 */ "anylist ::=",
 116651  /* 320 */ "anylist ::= anylist LP anylist RP",
 116652  /* 321 */ "anylist ::= anylist ANY",
 116653  /* 322 */ "with ::=",
 116654  /* 323 */ "with ::= WITH wqlist",
 116655  /* 324 */ "with ::= WITH RECURSIVE wqlist",
 116656  /* 325 */ "wqlist ::= nm idxlist_opt AS LP select RP",
 116657  /* 326 */ "wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP",
 116659 #endif /* NDEBUG */
 116662 #if YYSTACKDEPTH<=0
 116664 ** Try to increase the size of the parser stack.
 116666 static void yyGrowStack(yyParser *p){
 116667   int newSize;
 116668   yyStackEntry *pNew;
 116670   newSize = p->yystksz*2 + 100;
 116671   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
 116672   if( pNew ){
 116673     p->yystack = pNew;
 116674     p->yystksz = newSize;
 116675 #ifndef NDEBUG
 116676     if( yyTraceFILE ){
 116677       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
 116678               yyTracePrompt, p->yystksz);
 116680 #endif
 116683 #endif
 116686 ** This function allocates a new parser.
 116687 ** The only argument is a pointer to a function which works like
 116688 ** malloc.
 116690 ** Inputs:
 116691 ** A pointer to the function used to allocate memory.
 116693 ** Outputs:
 116694 ** A pointer to a parser.  This pointer is used in subsequent calls
 116695 ** to sqlite3Parser and sqlite3ParserFree.
 116697 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
 116698   yyParser *pParser;
 116699   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
 116700   if( pParser ){
 116701     pParser->yyidx = -1;
 116702 #ifdef YYTRACKMAXSTACKDEPTH
 116703     pParser->yyidxMax = 0;
 116704 #endif
 116705 #if YYSTACKDEPTH<=0
 116706     pParser->yystack = NULL;
 116707     pParser->yystksz = 0;
 116708     yyGrowStack(pParser);
 116709 #endif
 116711   return pParser;
 116714 /* The following function deletes the value associated with a
 116715 ** symbol.  The symbol can be either a terminal or nonterminal.
 116716 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
 116717 ** the value.
 116719 static void yy_destructor(
 116720   yyParser *yypParser,    /* The parser */
 116721   YYCODETYPE yymajor,     /* Type code for object to destroy */
 116722   YYMINORTYPE *yypminor   /* The object to be destroyed */
 116724   sqlite3ParserARG_FETCH;
 116725   switch( yymajor ){
 116726     /* Here is inserted the actions which take place when a
 116727     ** terminal or non-terminal is destroyed.  This can happen
 116728     ** when the symbol is popped from the stack during a
 116729     ** reduce or during error processing or when a parser is 
 116730     ** being destroyed before it is finished parsing.
 116732     ** Note: during a reduce, the only symbols destroyed are those
 116733     ** which appear on the RHS of the rule, but which are not used
 116734     ** inside the C code.
 116736     case 163: /* select */
 116737     case 195: /* selectnowith */
 116738     case 196: /* oneselect */
 116739     case 207: /* values */
 116741 sqlite3SelectDelete(pParse->db, (yypminor->yy3));
 116743       break;
 116744     case 174: /* term */
 116745     case 175: /* expr */
 116747 sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
 116749       break;
 116750     case 179: /* idxlist_opt */
 116751     case 188: /* idxlist */
 116752     case 200: /* selcollist */
 116753     case 203: /* groupby_opt */
 116754     case 205: /* orderby_opt */
 116755     case 208: /* nexprlist */
 116756     case 209: /* exprlist */
 116757     case 210: /* sclp */
 116758     case 220: /* sortlist */
 116759     case 221: /* setlist */
 116760     case 228: /* case_exprlist */
 116762 sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
 116764       break;
 116765     case 194: /* fullname */
 116766     case 201: /* from */
 116767     case 212: /* seltablist */
 116768     case 213: /* stl_prefix */
 116770 sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
 116772       break;
 116773     case 197: /* with */
 116774     case 252: /* wqlist */
 116776 sqlite3WithDelete(pParse->db, (yypminor->yy59));
 116778       break;
 116779     case 202: /* where_opt */
 116780     case 204: /* having_opt */
 116781     case 216: /* on_opt */
 116782     case 227: /* case_operand */
 116783     case 229: /* case_else */
 116784     case 238: /* when_clause */
 116785     case 243: /* key_opt */
 116787 sqlite3ExprDelete(pParse->db, (yypminor->yy132));
 116789       break;
 116790     case 217: /* using_opt */
 116791     case 219: /* idlist */
 116792     case 223: /* inscollist_opt */
 116794 sqlite3IdListDelete(pParse->db, (yypminor->yy408));
 116796       break;
 116797     case 234: /* trigger_cmd_list */
 116798     case 239: /* trigger_cmd */
 116800 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
 116802       break;
 116803     case 236: /* trigger_event */
 116805 sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
 116807       break;
 116808     default:  break;   /* If no destructor action specified: do nothing */
 116813 ** Pop the parser's stack once.
 116815 ** If there is a destructor routine associated with the token which
 116816 ** is popped from the stack, then call it.
 116818 ** Return the major token number for the symbol popped.
 116820 static int yy_pop_parser_stack(yyParser *pParser){
 116821   YYCODETYPE yymajor;
 116822   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
 116824   /* There is no mechanism by which the parser stack can be popped below
 116825   ** empty in SQLite.  */
 116826   if( NEVER(pParser->yyidx<0) ) return 0;
 116827 #ifndef NDEBUG
 116828   if( yyTraceFILE && pParser->yyidx>=0 ){
 116829     fprintf(yyTraceFILE,"%sPopping %s\n",
 116830       yyTracePrompt,
 116831       yyTokenName[yytos->major]);
 116833 #endif
 116834   yymajor = yytos->major;
 116835   yy_destructor(pParser, yymajor, &yytos->minor);
 116836   pParser->yyidx--;
 116837   return yymajor;
 116841 ** Deallocate and destroy a parser.  Destructors are all called for
 116842 ** all stack elements before shutting the parser down.
 116844 ** Inputs:
 116845 ** <ul>
 116846 ** <li>  A pointer to the parser.  This should be a pointer
 116847 **       obtained from sqlite3ParserAlloc.
 116848 ** <li>  A pointer to a function used to reclaim memory obtained
 116849 **       from malloc.
 116850 ** </ul>
 116852 SQLITE_PRIVATE void sqlite3ParserFree(
 116853   void *p,                    /* The parser to be deleted */
 116854   void (*freeProc)(void*)     /* Function used to reclaim memory */
 116856   yyParser *pParser = (yyParser*)p;
 116857   /* In SQLite, we never try to destroy a parser that was not successfully
 116858   ** created in the first place. */
 116859   if( NEVER(pParser==0) ) return;
 116860   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
 116861 #if YYSTACKDEPTH<=0
 116862   free(pParser->yystack);
 116863 #endif
 116864   (*freeProc)((void*)pParser);
 116868 ** Return the peak depth of the stack for a parser.
 116870 #ifdef YYTRACKMAXSTACKDEPTH
 116871 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
 116872   yyParser *pParser = (yyParser*)p;
 116873   return pParser->yyidxMax;
 116875 #endif
 116878 ** Find the appropriate action for a parser given the terminal
 116879 ** look-ahead token iLookAhead.
 116881 ** If the look-ahead token is YYNOCODE, then check to see if the action is
 116882 ** independent of the look-ahead.  If it is, return the action, otherwise
 116883 ** return YY_NO_ACTION.
 116885 static int yy_find_shift_action(
 116886   yyParser *pParser,        /* The parser */
 116887   YYCODETYPE iLookAhead     /* The look-ahead token */
 116889   int i;
 116890   int stateno = pParser->yystack[pParser->yyidx].stateno;
 116892   if( stateno>YY_SHIFT_COUNT
 116893    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
 116894     return yy_default[stateno];
 116896   assert( iLookAhead!=YYNOCODE );
 116897   i += iLookAhead;
 116898   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
 116899     if( iLookAhead>0 ){
 116900 #ifdef YYFALLBACK
 116901       YYCODETYPE iFallback;            /* Fallback token */
 116902       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
 116903              && (iFallback = yyFallback[iLookAhead])!=0 ){
 116904 #ifndef NDEBUG
 116905         if( yyTraceFILE ){
 116906           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
 116907              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
 116909 #endif
 116910         return yy_find_shift_action(pParser, iFallback);
 116912 #endif
 116913 #ifdef YYWILDCARD
 116915         int j = i - iLookAhead + YYWILDCARD;
 116916         if( 
 116917 #if YY_SHIFT_MIN+YYWILDCARD<0
 116918           j>=0 &&
 116919 #endif
 116920 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
 116921           j<YY_ACTTAB_COUNT &&
 116922 #endif
 116923           yy_lookahead[j]==YYWILDCARD
 116925 #ifndef NDEBUG
 116926           if( yyTraceFILE ){
 116927             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
 116928                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
 116930 #endif /* NDEBUG */
 116931           return yy_action[j];
 116934 #endif /* YYWILDCARD */
 116936     return yy_default[stateno];
 116937   }else{
 116938     return yy_action[i];
 116943 ** Find the appropriate action for a parser given the non-terminal
 116944 ** look-ahead token iLookAhead.
 116946 ** If the look-ahead token is YYNOCODE, then check to see if the action is
 116947 ** independent of the look-ahead.  If it is, return the action, otherwise
 116948 ** return YY_NO_ACTION.
 116950 static int yy_find_reduce_action(
 116951   int stateno,              /* Current state number */
 116952   YYCODETYPE iLookAhead     /* The look-ahead token */
 116954   int i;
 116955 #ifdef YYERRORSYMBOL
 116956   if( stateno>YY_REDUCE_COUNT ){
 116957     return yy_default[stateno];
 116959 #else
 116960   assert( stateno<=YY_REDUCE_COUNT );
 116961 #endif
 116962   i = yy_reduce_ofst[stateno];
 116963   assert( i!=YY_REDUCE_USE_DFLT );
 116964   assert( iLookAhead!=YYNOCODE );
 116965   i += iLookAhead;
 116966 #ifdef YYERRORSYMBOL
 116967   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
 116968     return yy_default[stateno];
 116970 #else
 116971   assert( i>=0 && i<YY_ACTTAB_COUNT );
 116972   assert( yy_lookahead[i]==iLookAhead );
 116973 #endif
 116974   return yy_action[i];
 116978 ** The following routine is called if the stack overflows.
 116980 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
 116981    sqlite3ParserARG_FETCH;
 116982    yypParser->yyidx--;
 116983 #ifndef NDEBUG
 116984    if( yyTraceFILE ){
 116985      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
 116987 #endif
 116988    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 116989    /* Here code is inserted which will execute if the parser
 116990    ** stack every overflows */
 116992   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
 116993   sqlite3ErrorMsg(pParse, "parser stack overflow");
 116994    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
 116998 ** Perform a shift action.
 117000 static void yy_shift(
 117001   yyParser *yypParser,          /* The parser to be shifted */
 117002   int yyNewState,               /* The new state to shift in */
 117003   int yyMajor,                  /* The major token to shift in */
 117004   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
 117006   yyStackEntry *yytos;
 117007   yypParser->yyidx++;
 117008 #ifdef YYTRACKMAXSTACKDEPTH
 117009   if( yypParser->yyidx>yypParser->yyidxMax ){
 117010     yypParser->yyidxMax = yypParser->yyidx;
 117012 #endif
 117013 #if YYSTACKDEPTH>0 
 117014   if( yypParser->yyidx>=YYSTACKDEPTH ){
 117015     yyStackOverflow(yypParser, yypMinor);
 117016     return;
 117018 #else
 117019   if( yypParser->yyidx>=yypParser->yystksz ){
 117020     yyGrowStack(yypParser);
 117021     if( yypParser->yyidx>=yypParser->yystksz ){
 117022       yyStackOverflow(yypParser, yypMinor);
 117023       return;
 117026 #endif
 117027   yytos = &yypParser->yystack[yypParser->yyidx];
 117028   yytos->stateno = (YYACTIONTYPE)yyNewState;
 117029   yytos->major = (YYCODETYPE)yyMajor;
 117030   yytos->minor = *yypMinor;
 117031 #ifndef NDEBUG
 117032   if( yyTraceFILE && yypParser->yyidx>0 ){
 117033     int i;
 117034     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
 117035     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
 117036     for(i=1; i<=yypParser->yyidx; i++)
 117037       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
 117038     fprintf(yyTraceFILE,"\n");
 117040 #endif
 117043 /* The following table contains information about every rule that
 117044 ** is used during the reduce.
 117046 static const struct {
 117047   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
 117048   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
 117049 } yyRuleInfo[] = {
 117050   { 144, 1 },
 117051   { 145, 2 },
 117052   { 145, 1 },
 117053   { 146, 1 },
 117054   { 146, 3 },
 117055   { 147, 0 },
 117056   { 147, 1 },
 117057   { 147, 3 },
 117058   { 148, 1 },
 117059   { 149, 3 },
 117060   { 151, 0 },
 117061   { 151, 1 },
 117062   { 151, 2 },
 117063   { 150, 0 },
 117064   { 150, 1 },
 117065   { 150, 1 },
 117066   { 150, 1 },
 117067   { 149, 2 },
 117068   { 149, 2 },
 117069   { 149, 2 },
 117070   { 153, 1 },
 117071   { 153, 0 },
 117072   { 149, 2 },
 117073   { 149, 3 },
 117074   { 149, 5 },
 117075   { 149, 2 },
 117076   { 154, 6 },
 117077   { 156, 1 },
 117078   { 158, 0 },
 117079   { 158, 3 },
 117080   { 157, 1 },
 117081   { 157, 0 },
 117082   { 155, 5 },
 117083   { 155, 2 },
 117084   { 162, 0 },
 117085   { 162, 2 },
 117086   { 160, 3 },
 117087   { 160, 1 },
 117088   { 164, 3 },
 117089   { 165, 1 },
 117090   { 152, 1 },
 117091   { 152, 1 },
 117092   { 152, 1 },
 117093   { 166, 0 },
 117094   { 166, 1 },
 117095   { 168, 1 },
 117096   { 168, 4 },
 117097   { 168, 6 },
 117098   { 169, 1 },
 117099   { 169, 2 },
 117100   { 170, 1 },
 117101   { 170, 1 },
 117102   { 167, 2 },
 117103   { 167, 0 },
 117104   { 173, 2 },
 117105   { 173, 2 },
 117106   { 173, 4 },
 117107   { 173, 3 },
 117108   { 173, 3 },
 117109   { 173, 2 },
 117110   { 173, 2 },
 117111   { 173, 3 },
 117112   { 173, 5 },
 117113   { 173, 2 },
 117114   { 173, 4 },
 117115   { 173, 4 },
 117116   { 173, 1 },
 117117   { 173, 2 },
 117118   { 178, 0 },
 117119   { 178, 1 },
 117120   { 180, 0 },
 117121   { 180, 2 },
 117122   { 182, 2 },
 117123   { 182, 3 },
 117124   { 182, 3 },
 117125   { 182, 3 },
 117126   { 183, 2 },
 117127   { 183, 2 },
 117128   { 183, 1 },
 117129   { 183, 1 },
 117130   { 183, 2 },
 117131   { 181, 3 },
 117132   { 181, 2 },
 117133   { 184, 0 },
 117134   { 184, 2 },
 117135   { 184, 2 },
 117136   { 161, 0 },
 117137   { 161, 2 },
 117138   { 185, 3 },
 117139   { 185, 1 },
 117140   { 186, 1 },
 117141   { 186, 0 },
 117142   { 187, 2 },
 117143   { 187, 7 },
 117144   { 187, 5 },
 117145   { 187, 5 },
 117146   { 187, 10 },
 117147   { 189, 0 },
 117148   { 189, 1 },
 117149   { 176, 0 },
 117150   { 176, 3 },
 117151   { 190, 0 },
 117152   { 190, 2 },
 117153   { 191, 1 },
 117154   { 191, 1 },
 117155   { 191, 1 },
 117156   { 149, 4 },
 117157   { 193, 2 },
 117158   { 193, 0 },
 117159   { 149, 8 },
 117160   { 149, 4 },
 117161   { 149, 1 },
 117162   { 163, 2 },
 117163   { 195, 1 },
 117164   { 195, 3 },
 117165   { 198, 1 },
 117166   { 198, 2 },
 117167   { 198, 1 },
 117168   { 196, 9 },
 117169   { 196, 1 },
 117170   { 207, 4 },
 117171   { 207, 5 },
 117172   { 199, 1 },
 117173   { 199, 1 },
 117174   { 199, 0 },
 117175   { 210, 2 },
 117176   { 210, 0 },
 117177   { 200, 3 },
 117178   { 200, 2 },
 117179   { 200, 4 },
 117180   { 211, 2 },
 117181   { 211, 1 },
 117182   { 211, 0 },
 117183   { 201, 0 },
 117184   { 201, 2 },
 117185   { 213, 2 },
 117186   { 213, 0 },
 117187   { 212, 7 },
 117188   { 212, 7 },
 117189   { 212, 7 },
 117190   { 159, 0 },
 117191   { 159, 2 },
 117192   { 194, 2 },
 117193   { 214, 1 },
 117194   { 214, 2 },
 117195   { 214, 3 },
 117196   { 214, 4 },
 117197   { 216, 2 },
 117198   { 216, 0 },
 117199   { 215, 0 },
 117200   { 215, 3 },
 117201   { 215, 2 },
 117202   { 217, 4 },
 117203   { 217, 0 },
 117204   { 205, 0 },
 117205   { 205, 3 },
 117206   { 220, 4 },
 117207   { 220, 2 },
 117208   { 177, 1 },
 117209   { 177, 1 },
 117210   { 177, 0 },
 117211   { 203, 0 },
 117212   { 203, 3 },
 117213   { 204, 0 },
 117214   { 204, 2 },
 117215   { 206, 0 },
 117216   { 206, 2 },
 117217   { 206, 4 },
 117218   { 206, 4 },
 117219   { 149, 6 },
 117220   { 202, 0 },
 117221   { 202, 2 },
 117222   { 149, 8 },
 117223   { 221, 5 },
 117224   { 221, 3 },
 117225   { 149, 6 },
 117226   { 149, 7 },
 117227   { 222, 2 },
 117228   { 222, 1 },
 117229   { 223, 0 },
 117230   { 223, 3 },
 117231   { 219, 3 },
 117232   { 219, 1 },
 117233   { 175, 1 },
 117234   { 175, 3 },
 117235   { 174, 1 },
 117236   { 175, 1 },
 117237   { 175, 1 },
 117238   { 175, 3 },
 117239   { 175, 5 },
 117240   { 174, 1 },
 117241   { 174, 1 },
 117242   { 175, 1 },
 117243   { 175, 3 },
 117244   { 175, 6 },
 117245   { 175, 5 },
 117246   { 175, 4 },
 117247   { 174, 1 },
 117248   { 175, 3 },
 117249   { 175, 3 },
 117250   { 175, 3 },
 117251   { 175, 3 },
 117252   { 175, 3 },
 117253   { 175, 3 },
 117254   { 175, 3 },
 117255   { 175, 3 },
 117256   { 224, 1 },
 117257   { 224, 2 },
 117258   { 175, 3 },
 117259   { 175, 5 },
 117260   { 175, 2 },
 117261   { 175, 3 },
 117262   { 175, 3 },
 117263   { 175, 4 },
 117264   { 175, 2 },
 117265   { 175, 2 },
 117266   { 175, 2 },
 117267   { 175, 2 },
 117268   { 225, 1 },
 117269   { 225, 2 },
 117270   { 175, 5 },
 117271   { 226, 1 },
 117272   { 226, 2 },
 117273   { 175, 5 },
 117274   { 175, 3 },
 117275   { 175, 5 },
 117276   { 175, 4 },
 117277   { 175, 4 },
 117278   { 175, 5 },
 117279   { 228, 5 },
 117280   { 228, 4 },
 117281   { 229, 2 },
 117282   { 229, 0 },
 117283   { 227, 1 },
 117284   { 227, 0 },
 117285   { 209, 1 },
 117286   { 209, 0 },
 117287   { 208, 3 },
 117288   { 208, 1 },
 117289   { 149, 12 },
 117290   { 230, 1 },
 117291   { 230, 0 },
 117292   { 179, 0 },
 117293   { 179, 3 },
 117294   { 188, 5 },
 117295   { 188, 3 },
 117296   { 231, 0 },
 117297   { 231, 2 },
 117298   { 149, 4 },
 117299   { 149, 1 },
 117300   { 149, 2 },
 117301   { 149, 3 },
 117302   { 149, 5 },
 117303   { 149, 6 },
 117304   { 149, 5 },
 117305   { 149, 6 },
 117306   { 232, 1 },
 117307   { 232, 1 },
 117308   { 232, 1 },
 117309   { 232, 1 },
 117310   { 232, 1 },
 117311   { 171, 2 },
 117312   { 171, 1 },
 117313   { 172, 2 },
 117314   { 149, 5 },
 117315   { 233, 11 },
 117316   { 235, 1 },
 117317   { 235, 1 },
 117318   { 235, 2 },
 117319   { 235, 0 },
 117320   { 236, 1 },
 117321   { 236, 1 },
 117322   { 236, 3 },
 117323   { 237, 0 },
 117324   { 237, 3 },
 117325   { 238, 0 },
 117326   { 238, 2 },
 117327   { 234, 3 },
 117328   { 234, 2 },
 117329   { 240, 1 },
 117330   { 240, 3 },
 117331   { 241, 0 },
 117332   { 241, 3 },
 117333   { 241, 2 },
 117334   { 239, 7 },
 117335   { 239, 5 },
 117336   { 239, 5 },
 117337   { 239, 1 },
 117338   { 175, 4 },
 117339   { 175, 6 },
 117340   { 192, 1 },
 117341   { 192, 1 },
 117342   { 192, 1 },
 117343   { 149, 4 },
 117344   { 149, 6 },
 117345   { 149, 3 },
 117346   { 243, 0 },
 117347   { 243, 2 },
 117348   { 242, 1 },
 117349   { 242, 0 },
 117350   { 149, 1 },
 117351   { 149, 3 },
 117352   { 149, 1 },
 117353   { 149, 3 },
 117354   { 149, 6 },
 117355   { 149, 6 },
 117356   { 244, 1 },
 117357   { 245, 0 },
 117358   { 245, 1 },
 117359   { 149, 1 },
 117360   { 149, 4 },
 117361   { 246, 8 },
 117362   { 247, 1 },
 117363   { 247, 3 },
 117364   { 248, 0 },
 117365   { 248, 2 },
 117366   { 249, 1 },
 117367   { 249, 3 },
 117368   { 250, 1 },
 117369   { 251, 0 },
 117370   { 251, 4 },
 117371   { 251, 2 },
 117372   { 197, 0 },
 117373   { 197, 2 },
 117374   { 197, 3 },
 117375   { 252, 6 },
 117376   { 252, 8 },
 117379 static void yy_accept(yyParser*);  /* Forward Declaration */
 117382 ** Perform a reduce action and the shift that must immediately
 117383 ** follow the reduce.
 117385 static void yy_reduce(
 117386   yyParser *yypParser,         /* The parser */
 117387   int yyruleno                 /* Number of the rule by which to reduce */
 117389   int yygoto;                     /* The next state */
 117390   int yyact;                      /* The next action */
 117391   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
 117392   yyStackEntry *yymsp;            /* The top of the parser's stack */
 117393   int yysize;                     /* Amount to pop the stack */
 117394   sqlite3ParserARG_FETCH;
 117395   yymsp = &yypParser->yystack[yypParser->yyidx];
 117396 #ifndef NDEBUG
 117397   if( yyTraceFILE && yyruleno>=0 
 117398         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
 117399     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
 117400       yyRuleName[yyruleno]);
 117402 #endif /* NDEBUG */
 117404   /* Silence complaints from purify about yygotominor being uninitialized
 117405   ** in some cases when it is copied into the stack after the following
 117406   ** switch.  yygotominor is uninitialized when a rule reduces that does
 117407   ** not set the value of its left-hand side nonterminal.  Leaving the
 117408   ** value of the nonterminal uninitialized is utterly harmless as long
 117409   ** as the value is never used.  So really the only thing this code
 117410   ** accomplishes is to quieten purify.  
 117412   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
 117413   ** without this code, their parser segfaults.  I'm not sure what there
 117414   ** parser is doing to make this happen.  This is the second bug report
 117415   ** from wireshark this week.  Clearly they are stressing Lemon in ways
 117416   ** that it has not been previously stressed...  (SQLite ticket #2172)
 117418   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
 117419   yygotominor = yyzerominor;
 117422   switch( yyruleno ){
 117423   /* Beginning here are the reduction cases.  A typical example
 117424   ** follows:
 117425   **   case 0:
 117426   **  #line <lineno> <grammarfile>
 117427   **     { ... }           // User supplied code
 117428   **  #line <lineno> <thisfile>
 117429   **     break;
 117431       case 5: /* explain ::= */
 117432 { sqlite3BeginParse(pParse, 0); }
 117433         break;
 117434       case 6: /* explain ::= EXPLAIN */
 117435 { sqlite3BeginParse(pParse, 1); }
 117436         break;
 117437       case 7: /* explain ::= EXPLAIN QUERY PLAN */
 117438 { sqlite3BeginParse(pParse, 2); }
 117439         break;
 117440       case 8: /* cmdx ::= cmd */
 117441 { sqlite3FinishCoding(pParse); }
 117442         break;
 117443       case 9: /* cmd ::= BEGIN transtype trans_opt */
 117444 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
 117445         break;
 117446       case 13: /* transtype ::= */
 117447 {yygotominor.yy328 = TK_DEFERRED;}
 117448         break;
 117449       case 14: /* transtype ::= DEFERRED */
 117450       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
 117451       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
 117452       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
 117453       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
 117454 {yygotominor.yy328 = yymsp[0].major;}
 117455         break;
 117456       case 17: /* cmd ::= COMMIT trans_opt */
 117457       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
 117458 {sqlite3CommitTransaction(pParse);}
 117459         break;
 117460       case 19: /* cmd ::= ROLLBACK trans_opt */
 117461 {sqlite3RollbackTransaction(pParse);}
 117462         break;
 117463       case 22: /* cmd ::= SAVEPOINT nm */
 117465   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
 117467         break;
 117468       case 23: /* cmd ::= RELEASE savepoint_opt nm */
 117470   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
 117472         break;
 117473       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
 117475   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
 117477         break;
 117478       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
 117480    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
 117482         break;
 117483       case 27: /* createkw ::= CREATE */
 117485   pParse->db->lookaside.bEnabled = 0;
 117486   yygotominor.yy0 = yymsp[0].minor.yy0;
 117488         break;
 117489       case 28: /* ifnotexists ::= */
 117490       case 31: /* temp ::= */ yytestcase(yyruleno==31);
 117491       case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
 117492       case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==81);
 117493       case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
 117494       case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==85);
 117495       case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
 117496       case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
 117497       case 218: /* between_op ::= BETWEEN */ yytestcase(yyruleno==218);
 117498       case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
 117499 {yygotominor.yy328 = 0;}
 117500         break;
 117501       case 29: /* ifnotexists ::= IF NOT EXISTS */
 117502       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
 117503       case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
 117504       case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==84);
 117505       case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
 117506       case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
 117507       case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
 117508 {yygotominor.yy328 = 1;}
 117509         break;
 117510       case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
 117512   sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy186,0);
 117514         break;
 117515       case 33: /* create_table_args ::= AS select */
 117517   sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy3);
 117518   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
 117520         break;
 117521       case 34: /* table_options ::= */
 117522 {yygotominor.yy186 = 0;}
 117523         break;
 117524       case 35: /* table_options ::= WITHOUT nm */
 117526   if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
 117527     yygotominor.yy186 = TF_WithoutRowid;
 117528   }else{
 117529     yygotominor.yy186 = 0;
 117530     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
 117533         break;
 117534       case 38: /* column ::= columnid type carglist */
 117536   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
 117537   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
 117539         break;
 117540       case 39: /* columnid ::= nm */
 117542   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
 117543   yygotominor.yy0 = yymsp[0].minor.yy0;
 117544   pParse->constraintName.n = 0;
 117546         break;
 117547       case 40: /* nm ::= ID|INDEXED */
 117548       case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
 117549       case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
 117550       case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
 117551       case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
 117552       case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
 117553       case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
 117554       case 141: /* dbnm ::= DOT nm */ yytestcase(yyruleno==141);
 117555       case 150: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==150);
 117556       case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
 117557       case 256: /* nmnum ::= plus_num */ yytestcase(yyruleno==256);
 117558       case 257: /* nmnum ::= nm */ yytestcase(yyruleno==257);
 117559       case 258: /* nmnum ::= ON */ yytestcase(yyruleno==258);
 117560       case 259: /* nmnum ::= DELETE */ yytestcase(yyruleno==259);
 117561       case 260: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==260);
 117562       case 261: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==261);
 117563       case 262: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==262);
 117564       case 263: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==263);
 117565       case 279: /* trnm ::= nm */ yytestcase(yyruleno==279);
 117566 {yygotominor.yy0 = yymsp[0].minor.yy0;}
 117567         break;
 117568       case 44: /* type ::= typetoken */
 117569 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
 117570         break;
 117571       case 46: /* typetoken ::= typename LP signed RP */
 117573   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
 117574   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
 117576         break;
 117577       case 47: /* typetoken ::= typename LP signed COMMA signed RP */
 117579   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
 117580   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
 117582         break;
 117583       case 49: /* typename ::= typename ID|STRING */
 117584 {yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
 117585         break;
 117586       case 54: /* ccons ::= CONSTRAINT nm */
 117587       case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
 117588 {pParse->constraintName = yymsp[0].minor.yy0;}
 117589         break;
 117590       case 55: /* ccons ::= DEFAULT term */
 117591       case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
 117592 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
 117593         break;
 117594       case 56: /* ccons ::= DEFAULT LP expr RP */
 117595 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
 117596         break;
 117597       case 58: /* ccons ::= DEFAULT MINUS term */
 117599   ExprSpan v;
 117600   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
 117601   v.zStart = yymsp[-1].minor.yy0.z;
 117602   v.zEnd = yymsp[0].minor.yy346.zEnd;
 117603   sqlite3AddDefaultValue(pParse,&v);
 117605         break;
 117606       case 59: /* ccons ::= DEFAULT ID|INDEXED */
 117608   ExprSpan v;
 117609   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
 117610   sqlite3AddDefaultValue(pParse,&v);
 117612         break;
 117613       case 61: /* ccons ::= NOT NULL onconf */
 117614 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
 117615         break;
 117616       case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
 117617 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
 117618         break;
 117619       case 63: /* ccons ::= UNIQUE onconf */
 117620 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
 117621         break;
 117622       case 64: /* ccons ::= CHECK LP expr RP */
 117623 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
 117624         break;
 117625       case 65: /* ccons ::= REFERENCES nm idxlist_opt refargs */
 117626 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
 117627         break;
 117628       case 66: /* ccons ::= defer_subclause */
 117629 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
 117630         break;
 117631       case 67: /* ccons ::= COLLATE ID|STRING */
 117632 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
 117633         break;
 117634       case 70: /* refargs ::= */
 117635 { yygotominor.yy328 = OE_None*0x0101; /* EV: R-19803-45884 */}
 117636         break;
 117637       case 71: /* refargs ::= refargs refarg */
 117638 { yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
 117639         break;
 117640       case 72: /* refarg ::= MATCH nm */
 117641       case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
 117642 { yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
 117643         break;
 117644       case 74: /* refarg ::= ON DELETE refact */
 117645 { yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
 117646         break;
 117647       case 75: /* refarg ::= ON UPDATE refact */
 117648 { yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
 117649         break;
 117650       case 76: /* refact ::= SET NULL */
 117651 { yygotominor.yy328 = OE_SetNull;  /* EV: R-33326-45252 */}
 117652         break;
 117653       case 77: /* refact ::= SET DEFAULT */
 117654 { yygotominor.yy328 = OE_SetDflt;  /* EV: R-33326-45252 */}
 117655         break;
 117656       case 78: /* refact ::= CASCADE */
 117657 { yygotominor.yy328 = OE_Cascade;  /* EV: R-33326-45252 */}
 117658         break;
 117659       case 79: /* refact ::= RESTRICT */
 117660 { yygotominor.yy328 = OE_Restrict; /* EV: R-33326-45252 */}
 117661         break;
 117662       case 80: /* refact ::= NO ACTION */
 117663 { yygotominor.yy328 = OE_None;     /* EV: R-33326-45252 */}
 117664         break;
 117665       case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
 117666       case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
 117667       case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
 117668       case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
 117669 {yygotominor.yy328 = yymsp[0].minor.yy328;}
 117670         break;
 117671       case 86: /* conslist_opt ::= */
 117672 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
 117673         break;
 117674       case 87: /* conslist_opt ::= COMMA conslist */
 117675 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
 117676         break;
 117677       case 90: /* tconscomma ::= COMMA */
 117678 {pParse->constraintName.n = 0;}
 117679         break;
 117680       case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
 117681 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
 117682         break;
 117683       case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
 117684 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
 117685         break;
 117686       case 95: /* tcons ::= CHECK LP expr RP onconf */
 117687 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
 117688         break;
 117689       case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
 117691     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
 117692     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
 117694         break;
 117695       case 99: /* onconf ::= */
 117696 {yygotominor.yy328 = OE_Default;}
 117697         break;
 117698       case 101: /* orconf ::= */
 117699 {yygotominor.yy186 = OE_Default;}
 117700         break;
 117701       case 102: /* orconf ::= OR resolvetype */
 117702 {yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
 117703         break;
 117704       case 104: /* resolvetype ::= IGNORE */
 117705 {yygotominor.yy328 = OE_Ignore;}
 117706         break;
 117707       case 105: /* resolvetype ::= REPLACE */
 117708 {yygotominor.yy328 = OE_Replace;}
 117709         break;
 117710       case 106: /* cmd ::= DROP TABLE ifexists fullname */
 117712   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
 117714         break;
 117715       case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
 117717   sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy3, yymsp[-6].minor.yy328, yymsp[-4].minor.yy328);
 117719         break;
 117720       case 110: /* cmd ::= DROP VIEW ifexists fullname */
 117722   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
 117724         break;
 117725       case 111: /* cmd ::= select */
 117727   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
 117728   sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
 117729   sqlite3ExplainBegin(pParse->pVdbe);
 117730   sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3);
 117731   sqlite3ExplainFinish(pParse->pVdbe);
 117732   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
 117734         break;
 117735       case 112: /* select ::= with selectnowith */
 117737   Select *p = yymsp[0].minor.yy3, *pNext, *pLoop;
 117738   if( p ){
 117739     int cnt = 0, mxSelect;
 117740     p->pWith = yymsp[-1].minor.yy59;
 117741     if( p->pPrior ){
 117742       pNext = 0;
 117743       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
 117744         pLoop->pNext = pNext;
 117745         pLoop->selFlags |= SF_Compound;
 117747       mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
 117748       if( mxSelect && cnt>mxSelect ){
 117749         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
 117752   }else{
 117753     sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
 117755   yygotominor.yy3 = p;
 117757         break;
 117758       case 113: /* selectnowith ::= oneselect */
 117759       case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
 117760 {yygotominor.yy3 = yymsp[0].minor.yy3;}
 117761         break;
 117762       case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
 117764   Select *pRhs = yymsp[0].minor.yy3;
 117765   if( pRhs && pRhs->pPrior ){
 117766     SrcList *pFrom;
 117767     Token x;
 117768     x.n = 0;
 117769     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
 117770     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
 117772   if( pRhs ){
 117773     pRhs->op = (u8)yymsp[-1].minor.yy328;
 117774     pRhs->pPrior = yymsp[-2].minor.yy3;
 117775     if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
 117776   }else{
 117777     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
 117779   yygotominor.yy3 = pRhs;
 117781         break;
 117782       case 116: /* multiselect_op ::= UNION ALL */
 117783 {yygotominor.yy328 = TK_ALL;}
 117784         break;
 117785       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
 117787   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset);
 117789         break;
 117790       case 120: /* values ::= VALUES LP nexprlist RP */
 117792   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
 117794         break;
 117795       case 121: /* values ::= values COMMA LP exprlist RP */
 117797   Select *pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
 117798   if( pRight ){
 117799     pRight->op = TK_ALL;
 117800     pRight->pPrior = yymsp[-4].minor.yy3;
 117801     yygotominor.yy3 = pRight;
 117802   }else{
 117803     yygotominor.yy3 = yymsp[-4].minor.yy3;
 117806         break;
 117807       case 122: /* distinct ::= DISTINCT */
 117808 {yygotominor.yy381 = SF_Distinct;}
 117809         break;
 117810       case 123: /* distinct ::= ALL */
 117811       case 124: /* distinct ::= */ yytestcase(yyruleno==124);
 117812 {yygotominor.yy381 = 0;}
 117813         break;
 117814       case 125: /* sclp ::= selcollist COMMA */
 117815       case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
 117816 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
 117817         break;
 117818       case 126: /* sclp ::= */
 117819       case 154: /* orderby_opt ::= */ yytestcase(yyruleno==154);
 117820       case 161: /* groupby_opt ::= */ yytestcase(yyruleno==161);
 117821       case 236: /* exprlist ::= */ yytestcase(yyruleno==236);
 117822       case 242: /* idxlist_opt ::= */ yytestcase(yyruleno==242);
 117823 {yygotominor.yy14 = 0;}
 117824         break;
 117825       case 127: /* selcollist ::= sclp expr as */
 117827    yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
 117828    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
 117829    sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
 117831         break;
 117832       case 128: /* selcollist ::= sclp STAR */
 117834   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
 117835   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
 117837         break;
 117838       case 129: /* selcollist ::= sclp nm DOT STAR */
 117840   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
 117841   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 117842   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
 117843   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
 117845         break;
 117846       case 132: /* as ::= */
 117847 {yygotominor.yy0.n = 0;}
 117848         break;
 117849       case 133: /* from ::= */
 117850 {yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
 117851         break;
 117852       case 134: /* from ::= FROM seltablist */
 117854   yygotominor.yy65 = yymsp[0].minor.yy65;
 117855   sqlite3SrcListShiftJoinType(yygotominor.yy65);
 117857         break;
 117858       case 135: /* stl_prefix ::= seltablist joinop */
 117860    yygotominor.yy65 = yymsp[-1].minor.yy65;
 117861    if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
 117863         break;
 117864       case 136: /* stl_prefix ::= */
 117865 {yygotominor.yy65 = 0;}
 117866         break;
 117867       case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
 117869   yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
 117870   sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
 117872         break;
 117873       case 138: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
 117875     yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy3,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
 117877         break;
 117878       case 139: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
 117880     if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
 117881       yygotominor.yy65 = yymsp[-4].minor.yy65;
 117882     }else if( yymsp[-4].minor.yy65->nSrc==1 ){
 117883       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
 117884       if( yygotominor.yy65 ){
 117885         struct SrcList_item *pNew = &yygotominor.yy65->a[yygotominor.yy65->nSrc-1];
 117886         struct SrcList_item *pOld = yymsp[-4].minor.yy65->a;
 117887         pNew->zName = pOld->zName;
 117888         pNew->zDatabase = pOld->zDatabase;
 117889         pNew->pSelect = pOld->pSelect;
 117890         pOld->zName = pOld->zDatabase = 0;
 117891         pOld->pSelect = 0;
 117893       sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy65);
 117894     }else{
 117895       Select *pSubquery;
 117896       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
 117897       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,SF_NestedFrom,0,0);
 117898       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
 117901         break;
 117902       case 140: /* dbnm ::= */
 117903       case 149: /* indexed_opt ::= */ yytestcase(yyruleno==149);
 117904 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
 117905         break;
 117906       case 142: /* fullname ::= nm dbnm */
 117907 {yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
 117908         break;
 117909       case 143: /* joinop ::= COMMA|JOIN */
 117910 { yygotominor.yy328 = JT_INNER; }
 117911         break;
 117912       case 144: /* joinop ::= JOIN_KW JOIN */
 117913 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
 117914         break;
 117915       case 145: /* joinop ::= JOIN_KW nm JOIN */
 117916 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
 117917         break;
 117918       case 146: /* joinop ::= JOIN_KW nm nm JOIN */
 117919 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
 117920         break;
 117921       case 147: /* on_opt ::= ON expr */
 117922       case 164: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==164);
 117923       case 171: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==171);
 117924       case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
 117925       case 233: /* case_operand ::= expr */ yytestcase(yyruleno==233);
 117926 {yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
 117927         break;
 117928       case 148: /* on_opt ::= */
 117929       case 163: /* having_opt ::= */ yytestcase(yyruleno==163);
 117930       case 170: /* where_opt ::= */ yytestcase(yyruleno==170);
 117931       case 232: /* case_else ::= */ yytestcase(yyruleno==232);
 117932       case 234: /* case_operand ::= */ yytestcase(yyruleno==234);
 117933 {yygotominor.yy132 = 0;}
 117934         break;
 117935       case 151: /* indexed_opt ::= NOT INDEXED */
 117936 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
 117937         break;
 117938       case 152: /* using_opt ::= USING LP idlist RP */
 117939       case 180: /* inscollist_opt ::= LP idlist RP */ yytestcase(yyruleno==180);
 117940 {yygotominor.yy408 = yymsp[-1].minor.yy408;}
 117941         break;
 117942       case 153: /* using_opt ::= */
 117943       case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
 117944 {yygotominor.yy408 = 0;}
 117945         break;
 117946       case 155: /* orderby_opt ::= ORDER BY sortlist */
 117947       case 162: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==162);
 117948       case 235: /* exprlist ::= nexprlist */ yytestcase(yyruleno==235);
 117949 {yygotominor.yy14 = yymsp[0].minor.yy14;}
 117950         break;
 117951       case 156: /* sortlist ::= sortlist COMMA expr sortorder */
 117953   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy346.pExpr);
 117954   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
 117956         break;
 117957       case 157: /* sortlist ::= expr sortorder */
 117959   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy346.pExpr);
 117960   if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
 117962         break;
 117963       case 158: /* sortorder ::= ASC */
 117964       case 160: /* sortorder ::= */ yytestcase(yyruleno==160);
 117965 {yygotominor.yy328 = SQLITE_SO_ASC;}
 117966         break;
 117967       case 159: /* sortorder ::= DESC */
 117968 {yygotominor.yy328 = SQLITE_SO_DESC;}
 117969         break;
 117970       case 165: /* limit_opt ::= */
 117971 {yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
 117972         break;
 117973       case 166: /* limit_opt ::= LIMIT expr */
 117974 {yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
 117975         break;
 117976       case 167: /* limit_opt ::= LIMIT expr OFFSET expr */
 117977 {yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
 117978         break;
 117979       case 168: /* limit_opt ::= LIMIT expr COMMA expr */
 117980 {yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
 117981         break;
 117982       case 169: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
 117984   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
 117985   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
 117986   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
 117988         break;
 117989       case 172: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
 117991   sqlite3WithPush(pParse, yymsp[-7].minor.yy59, 1);
 117992   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
 117993   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list"); 
 117994   sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
 117996         break;
 117997       case 173: /* setlist ::= setlist COMMA nm EQ expr */
 117999   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
 118000   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
 118002         break;
 118003       case 174: /* setlist ::= nm EQ expr */
 118005   yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
 118006   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
 118008         break;
 118009       case 175: /* cmd ::= with insert_cmd INTO fullname inscollist_opt select */
 118011   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
 118012   sqlite3Insert(pParse, yymsp[-2].minor.yy65, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);
 118014         break;
 118015       case 176: /* cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
 118017   sqlite3WithPush(pParse, yymsp[-6].minor.yy59, 1);
 118018   sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);
 118020         break;
 118021       case 177: /* insert_cmd ::= INSERT orconf */
 118022 {yygotominor.yy186 = yymsp[0].minor.yy186;}
 118023         break;
 118024       case 178: /* insert_cmd ::= REPLACE */
 118025 {yygotominor.yy186 = OE_Replace;}
 118026         break;
 118027       case 181: /* idlist ::= idlist COMMA nm */
 118028 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
 118029         break;
 118030       case 182: /* idlist ::= nm */
 118031 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
 118032         break;
 118033       case 183: /* expr ::= term */
 118034 {yygotominor.yy346 = yymsp[0].minor.yy346;}
 118035         break;
 118036       case 184: /* expr ::= LP expr RP */
 118037 {yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
 118038         break;
 118039       case 185: /* term ::= NULL */
 118040       case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
 118041       case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
 118042 {spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
 118043         break;
 118044       case 186: /* expr ::= ID|INDEXED */
 118045       case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
 118046 {spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
 118047         break;
 118048       case 188: /* expr ::= nm DOT nm */
 118050   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 118051   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
 118052   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
 118053   spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
 118055         break;
 118056       case 189: /* expr ::= nm DOT nm DOT nm */
 118058   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
 118059   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
 118060   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
 118061   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
 118062   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
 118063   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
 118065         break;
 118066       case 192: /* expr ::= VARIABLE */
 118068   if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]) ){
 118069     /* When doing a nested parse, one can include terms in an expression
 118070     ** that look like this:   #1 #2 ...  These terms refer to registers
 118071     ** in the virtual machine.  #N is the N-th register. */
 118072     if( pParse->nested==0 ){
 118073       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
 118074       yygotominor.yy346.pExpr = 0;
 118075     }else{
 118076       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
 118077       if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
 118079   }else{
 118080     spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
 118081     sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
 118083   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
 118085         break;
 118086       case 193: /* expr ::= expr COLLATE ID|STRING */
 118088   yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
 118089   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
 118090   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118092         break;
 118093       case 194: /* expr ::= CAST LP expr AS typetoken RP */
 118095   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
 118096   spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
 118098         break;
 118099       case 195: /* expr ::= ID|INDEXED LP distinct exprlist RP */
 118101   if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
 118102     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
 118104   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
 118105   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
 118106   if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
 118107     yygotominor.yy346.pExpr->flags |= EP_Distinct;
 118110         break;
 118111       case 196: /* expr ::= ID|INDEXED LP STAR RP */
 118113   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
 118114   spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
 118116         break;
 118117       case 197: /* term ::= CTIME_KW */
 118119   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
 118120   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
 118122         break;
 118123       case 198: /* expr ::= expr AND expr */
 118124       case 199: /* expr ::= expr OR expr */ yytestcase(yyruleno==199);
 118125       case 200: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==200);
 118126       case 201: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==201);
 118127       case 202: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==202);
 118128       case 203: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==203);
 118129       case 204: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==204);
 118130       case 205: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==205);
 118131 {spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
 118132         break;
 118133       case 206: /* likeop ::= LIKE_KW|MATCH */
 118134 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 0;}
 118135         break;
 118136       case 207: /* likeop ::= NOT LIKE_KW|MATCH */
 118137 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 1;}
 118138         break;
 118139       case 208: /* expr ::= expr likeop expr */
 118141   ExprList *pList;
 118142   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy346.pExpr);
 118143   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy346.pExpr);
 118144   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy96.eOperator);
 118145   if( yymsp[-1].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118146   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
 118147   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
 118148   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
 118150         break;
 118151       case 209: /* expr ::= expr likeop expr ESCAPE expr */
 118153   ExprList *pList;
 118154   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
 118155   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy346.pExpr);
 118156   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
 118157   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy96.eOperator);
 118158   if( yymsp[-3].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118159   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
 118160   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
 118161   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
 118163         break;
 118164       case 210: /* expr ::= expr ISNULL|NOTNULL */
 118165 {spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
 118166         break;
 118167       case 211: /* expr ::= expr NOT NULL */
 118168 {spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
 118169         break;
 118170       case 212: /* expr ::= expr IS expr */
 118172   spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
 118173   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_ISNULL);
 118175         break;
 118176       case 213: /* expr ::= expr IS NOT expr */
 118178   spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
 118179   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_NOTNULL);
 118181         break;
 118182       case 214: /* expr ::= NOT expr */
 118183       case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
 118184 {spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
 118185         break;
 118186       case 216: /* expr ::= MINUS expr */
 118187 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
 118188         break;
 118189       case 217: /* expr ::= PLUS expr */
 118190 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
 118191         break;
 118192       case 220: /* expr ::= expr between_op expr AND expr */
 118194   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
 118195   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
 118196   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
 118197   if( yygotominor.yy346.pExpr ){
 118198     yygotominor.yy346.pExpr->x.pList = pList;
 118199   }else{
 118200     sqlite3ExprListDelete(pParse->db, pList);
 118202   if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118203   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
 118204   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
 118206         break;
 118207       case 223: /* expr ::= expr in_op LP exprlist RP */
 118209     if( yymsp[-1].minor.yy14==0 ){
 118210       /* Expressions of the form
 118212       **      expr1 IN ()
 118213       **      expr1 NOT IN ()
 118215       ** simplify to constants 0 (false) and 1 (true), respectively,
 118216       ** regardless of the value of expr1.
 118218       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
 118219       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
 118220     }else{
 118221       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
 118222       if( yygotominor.yy346.pExpr ){
 118223         yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
 118224         sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
 118225       }else{
 118226         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
 118228       if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118230     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
 118231     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118233         break;
 118234       case 224: /* expr ::= LP select RP */
 118236     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
 118237     if( yygotominor.yy346.pExpr ){
 118238       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
 118239       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
 118240       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
 118241     }else{
 118242       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
 118244     yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
 118245     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118247         break;
 118248       case 225: /* expr ::= expr in_op LP select RP */
 118250     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
 118251     if( yygotominor.yy346.pExpr ){
 118252       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
 118253       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
 118254       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
 118255     }else{
 118256       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
 118258     if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118259     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
 118260     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118262         break;
 118263       case 226: /* expr ::= expr in_op nm dbnm */
 118265     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
 118266     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
 118267     if( yygotominor.yy346.pExpr ){
 118268       yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
 118269       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
 118270       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
 118271     }else{
 118272       sqlite3SrcListDelete(pParse->db, pSrc);
 118274     if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
 118275     yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
 118276     yygotominor.yy346.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
 118278         break;
 118279       case 227: /* expr ::= EXISTS LP select RP */
 118281     Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
 118282     if( p ){
 118283       p->x.pSelect = yymsp[-1].minor.yy3;
 118284       ExprSetProperty(p, EP_xIsSelect);
 118285       sqlite3ExprSetHeight(pParse, p);
 118286     }else{
 118287       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
 118289     yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
 118290     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118292         break;
 118293       case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
 118295   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, 0, 0);
 118296   if( yygotominor.yy346.pExpr ){
 118297     yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy132 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy132) : yymsp[-2].minor.yy14;
 118298     sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
 118299   }else{
 118300     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
 118301     sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy132);
 118303   yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
 118304   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118306         break;
 118307       case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
 118309   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
 118310   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
 118312         break;
 118313       case 230: /* case_exprlist ::= WHEN expr THEN expr */
 118315   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
 118316   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
 118318         break;
 118319       case 237: /* nexprlist ::= nexprlist COMMA expr */
 118320 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
 118321         break;
 118322       case 238: /* nexprlist ::= expr */
 118323 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
 118324         break;
 118325       case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt */
 118327   sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, 
 118328                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy328,
 118329                       &yymsp[-11].minor.yy0, yymsp[0].minor.yy132, SQLITE_SO_ASC, yymsp[-8].minor.yy328);
 118331         break;
 118332       case 240: /* uniqueflag ::= UNIQUE */
 118333       case 291: /* raisetype ::= ABORT */ yytestcase(yyruleno==291);
 118334 {yygotominor.yy328 = OE_Abort;}
 118335         break;
 118336       case 241: /* uniqueflag ::= */
 118337 {yygotominor.yy328 = OE_None;}
 118338         break;
 118339       case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
 118341   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 118342   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
 118343   sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
 118344   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
 118345   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
 118347         break;
 118348       case 245: /* idxlist ::= nm collate sortorder */
 118350   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
 118351   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
 118352   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
 118353   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
 118354   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
 118356         break;
 118357       case 246: /* collate ::= */
 118358 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
 118359         break;
 118360       case 248: /* cmd ::= DROP INDEX ifexists fullname */
 118361 {sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
 118362         break;
 118363       case 249: /* cmd ::= VACUUM */
 118364       case 250: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==250);
 118365 {sqlite3Vacuum(pParse);}
 118366         break;
 118367       case 251: /* cmd ::= PRAGMA nm dbnm */
 118368 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
 118369         break;
 118370       case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
 118371 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
 118372         break;
 118373       case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
 118374 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
 118375         break;
 118376       case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
 118377 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
 118378         break;
 118379       case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
 118380 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
 118381         break;
 118382       case 264: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
 118384   Token all;
 118385   all.z = yymsp[-3].minor.yy0.z;
 118386   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
 118387   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
 118389         break;
 118390       case 265: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
 118392   sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy328, yymsp[-4].minor.yy378.a, yymsp[-4].minor.yy378.b, yymsp[-2].minor.yy65, yymsp[0].minor.yy132, yymsp[-10].minor.yy328, yymsp[-8].minor.yy328);
 118393   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
 118395         break;
 118396       case 266: /* trigger_time ::= BEFORE */
 118397       case 269: /* trigger_time ::= */ yytestcase(yyruleno==269);
 118398 { yygotominor.yy328 = TK_BEFORE; }
 118399         break;
 118400       case 267: /* trigger_time ::= AFTER */
 118401 { yygotominor.yy328 = TK_AFTER;  }
 118402         break;
 118403       case 268: /* trigger_time ::= INSTEAD OF */
 118404 { yygotominor.yy328 = TK_INSTEAD;}
 118405         break;
 118406       case 270: /* trigger_event ::= DELETE|INSERT */
 118407       case 271: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==271);
 118408 {yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
 118409         break;
 118410       case 272: /* trigger_event ::= UPDATE OF idlist */
 118411 {yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
 118412         break;
 118413       case 275: /* when_clause ::= */
 118414       case 296: /* key_opt ::= */ yytestcase(yyruleno==296);
 118415 { yygotominor.yy132 = 0; }
 118416         break;
 118417       case 276: /* when_clause ::= WHEN expr */
 118418       case 297: /* key_opt ::= KEY expr */ yytestcase(yyruleno==297);
 118419 { yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
 118420         break;
 118421       case 277: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
 118423   assert( yymsp[-2].minor.yy473!=0 );
 118424   yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
 118425   yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
 118426   yygotominor.yy473 = yymsp[-2].minor.yy473;
 118428         break;
 118429       case 278: /* trigger_cmd_list ::= trigger_cmd SEMI */
 118431   assert( yymsp[-1].minor.yy473!=0 );
 118432   yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
 118433   yygotominor.yy473 = yymsp[-1].minor.yy473;
 118435         break;
 118436       case 280: /* trnm ::= nm DOT nm */
 118438   yygotominor.yy0 = yymsp[0].minor.yy0;
 118439   sqlite3ErrorMsg(pParse, 
 118440         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
 118441         "statements within triggers");
 118443         break;
 118444       case 282: /* tridxby ::= INDEXED BY nm */
 118446   sqlite3ErrorMsg(pParse,
 118447         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
 118448         "within triggers");
 118450         break;
 118451       case 283: /* tridxby ::= NOT INDEXED */
 118453   sqlite3ErrorMsg(pParse,
 118454         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
 118455         "within triggers");
 118457         break;
 118458       case 284: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
 118459 { yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
 118460         break;
 118461       case 285: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
 118462 {yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
 118463         break;
 118464       case 286: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
 118465 {yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
 118466         break;
 118467       case 287: /* trigger_cmd ::= select */
 118468 {yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
 118469         break;
 118470       case 288: /* expr ::= RAISE LP IGNORE RP */
 118472   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
 118473   if( yygotominor.yy346.pExpr ){
 118474     yygotominor.yy346.pExpr->affinity = OE_Ignore;
 118476   yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
 118477   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118479         break;
 118480       case 289: /* expr ::= RAISE LP raisetype COMMA nm RP */
 118482   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
 118483   if( yygotominor.yy346.pExpr ) {
 118484     yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
 118486   yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
 118487   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
 118489         break;
 118490       case 290: /* raisetype ::= ROLLBACK */
 118491 {yygotominor.yy328 = OE_Rollback;}
 118492         break;
 118493       case 292: /* raisetype ::= FAIL */
 118494 {yygotominor.yy328 = OE_Fail;}
 118495         break;
 118496       case 293: /* cmd ::= DROP TRIGGER ifexists fullname */
 118498   sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
 118500         break;
 118501       case 294: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
 118503   sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
 118505         break;
 118506       case 295: /* cmd ::= DETACH database_kw_opt expr */
 118508   sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
 118510         break;
 118511       case 300: /* cmd ::= REINDEX */
 118512 {sqlite3Reindex(pParse, 0, 0);}
 118513         break;
 118514       case 301: /* cmd ::= REINDEX nm dbnm */
 118515 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
 118516         break;
 118517       case 302: /* cmd ::= ANALYZE */
 118518 {sqlite3Analyze(pParse, 0, 0);}
 118519         break;
 118520       case 303: /* cmd ::= ANALYZE nm dbnm */
 118521 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
 118522         break;
 118523       case 304: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
 118525   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
 118527         break;
 118528       case 305: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
 118530   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
 118532         break;
 118533       case 306: /* add_column_fullname ::= fullname */
 118535   pParse->db->lookaside.bEnabled = 0;
 118536   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
 118538         break;
 118539       case 309: /* cmd ::= create_vtab */
 118540 {sqlite3VtabFinishParse(pParse,0);}
 118541         break;
 118542       case 310: /* cmd ::= create_vtab LP vtabarglist RP */
 118543 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
 118544         break;
 118545       case 311: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
 118547     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy328);
 118549         break;
 118550       case 314: /* vtabarg ::= */
 118551 {sqlite3VtabArgInit(pParse);}
 118552         break;
 118553       case 316: /* vtabargtoken ::= ANY */
 118554       case 317: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==317);
 118555       case 318: /* lp ::= LP */ yytestcase(yyruleno==318);
 118556 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
 118557         break;
 118558       case 322: /* with ::= */
 118559 {yygotominor.yy59 = 0;}
 118560         break;
 118561       case 323: /* with ::= WITH wqlist */
 118562       case 324: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==324);
 118563 { yygotominor.yy59 = yymsp[0].minor.yy59; }
 118564         break;
 118565       case 325: /* wqlist ::= nm idxlist_opt AS LP select RP */
 118567   yygotominor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
 118569         break;
 118570       case 326: /* wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP */
 118572   yygotominor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
 118574         break;
 118575       default:
 118576       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
 118577       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
 118578       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
 118579       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
 118580       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
 118581       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
 118582       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
 118583       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
 118584       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
 118585       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
 118586       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
 118587       /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36);
 118588       /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
 118589       /* (43) type ::= */ yytestcase(yyruleno==43);
 118590       /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
 118591       /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
 118592       /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
 118593       /* (53) carglist ::= */ yytestcase(yyruleno==53);
 118594       /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
 118595       /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88);
 118596       /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
 118597       /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
 118598       /* (273) foreach_clause ::= */ yytestcase(yyruleno==273);
 118599       /* (274) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==274);
 118600       /* (281) tridxby ::= */ yytestcase(yyruleno==281);
 118601       /* (298) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==298);
 118602       /* (299) database_kw_opt ::= */ yytestcase(yyruleno==299);
 118603       /* (307) kwcolumn_opt ::= */ yytestcase(yyruleno==307);
 118604       /* (308) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==308);
 118605       /* (312) vtabarglist ::= vtabarg */ yytestcase(yyruleno==312);
 118606       /* (313) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==313);
 118607       /* (315) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==315);
 118608       /* (319) anylist ::= */ yytestcase(yyruleno==319);
 118609       /* (320) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==320);
 118610       /* (321) anylist ::= anylist ANY */ yytestcase(yyruleno==321);
 118611         break;
 118613   assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
 118614   yygoto = yyRuleInfo[yyruleno].lhs;
 118615   yysize = yyRuleInfo[yyruleno].nrhs;
 118616   yypParser->yyidx -= yysize;
 118617   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
 118618   if( yyact < YYNSTATE ){
 118619 #ifdef NDEBUG
 118620     /* If we are not debugging and the reduce action popped at least
 118621     ** one element off the stack, then we can push the new element back
 118622     ** onto the stack here, and skip the stack overflow test in yy_shift().
 118623     ** That gives a significant speed improvement. */
 118624     if( yysize ){
 118625       yypParser->yyidx++;
 118626       yymsp -= yysize-1;
 118627       yymsp->stateno = (YYACTIONTYPE)yyact;
 118628       yymsp->major = (YYCODETYPE)yygoto;
 118629       yymsp->minor = yygotominor;
 118630     }else
 118631 #endif
 118633       yy_shift(yypParser,yyact,yygoto,&yygotominor);
 118635   }else{
 118636     assert( yyact == YYNSTATE + YYNRULE + 1 );
 118637     yy_accept(yypParser);
 118642 ** The following code executes when the parse fails
 118644 #ifndef YYNOERRORRECOVERY
 118645 static void yy_parse_failed(
 118646   yyParser *yypParser           /* The parser */
 118648   sqlite3ParserARG_FETCH;
 118649 #ifndef NDEBUG
 118650   if( yyTraceFILE ){
 118651     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
 118653 #endif
 118654   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 118655   /* Here code is inserted which will be executed whenever the
 118656   ** parser fails */
 118657   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 118659 #endif /* YYNOERRORRECOVERY */
 118662 ** The following code executes when a syntax error first occurs.
 118664 static void yy_syntax_error(
 118665   yyParser *yypParser,           /* The parser */
 118666   int yymajor,                   /* The major type of the error token */
 118667   YYMINORTYPE yyminor            /* The minor type of the error token */
 118669   sqlite3ParserARG_FETCH;
 118670 #define TOKEN (yyminor.yy0)
 118672   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
 118673   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
 118674   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
 118675   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 118679 ** The following is executed when the parser accepts
 118681 static void yy_accept(
 118682   yyParser *yypParser           /* The parser */
 118684   sqlite3ParserARG_FETCH;
 118685 #ifndef NDEBUG
 118686   if( yyTraceFILE ){
 118687     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
 118689 #endif
 118690   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
 118691   /* Here code is inserted which will be executed whenever the
 118692   ** parser accepts */
 118693   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
 118696 /* The main parser program.
 118697 ** The first argument is a pointer to a structure obtained from
 118698 ** "sqlite3ParserAlloc" which describes the current state of the parser.
 118699 ** The second argument is the major token number.  The third is
 118700 ** the minor token.  The fourth optional argument is whatever the
 118701 ** user wants (and specified in the grammar) and is available for
 118702 ** use by the action routines.
 118704 ** Inputs:
 118705 ** <ul>
 118706 ** <li> A pointer to the parser (an opaque structure.)
 118707 ** <li> The major token number.
 118708 ** <li> The minor token number.
 118709 ** <li> An option argument of a grammar-specified type.
 118710 ** </ul>
 118712 ** Outputs:
 118713 ** None.
 118715 SQLITE_PRIVATE void sqlite3Parser(
 118716   void *yyp,                   /* The parser */
 118717   int yymajor,                 /* The major token code number */
 118718   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
 118719   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
 118721   YYMINORTYPE yyminorunion;
 118722   int yyact;            /* The parser action. */
 118723 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
 118724   int yyendofinput;     /* True if we are at the end of input */
 118725 #endif
 118726 #ifdef YYERRORSYMBOL
 118727   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
 118728 #endif
 118729   yyParser *yypParser;  /* The parser */
 118731   /* (re)initialize the parser, if necessary */
 118732   yypParser = (yyParser*)yyp;
 118733   if( yypParser->yyidx<0 ){
 118734 #if YYSTACKDEPTH<=0
 118735     if( yypParser->yystksz <=0 ){
 118736       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
 118737       yyminorunion = yyzerominor;
 118738       yyStackOverflow(yypParser, &yyminorunion);
 118739       return;
 118741 #endif
 118742     yypParser->yyidx = 0;
 118743     yypParser->yyerrcnt = -1;
 118744     yypParser->yystack[0].stateno = 0;
 118745     yypParser->yystack[0].major = 0;
 118747   yyminorunion.yy0 = yyminor;
 118748 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
 118749   yyendofinput = (yymajor==0);
 118750 #endif
 118751   sqlite3ParserARG_STORE;
 118753 #ifndef NDEBUG
 118754   if( yyTraceFILE ){
 118755     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
 118757 #endif
 118759   do{
 118760     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
 118761     if( yyact<YYNSTATE ){
 118762       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
 118763       yypParser->yyerrcnt--;
 118764       yymajor = YYNOCODE;
 118765     }else if( yyact < YYNSTATE + YYNRULE ){
 118766       yy_reduce(yypParser,yyact-YYNSTATE);
 118767     }else{
 118768       assert( yyact == YY_ERROR_ACTION );
 118769 #ifdef YYERRORSYMBOL
 118770       int yymx;
 118771 #endif
 118772 #ifndef NDEBUG
 118773       if( yyTraceFILE ){
 118774         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
 118776 #endif
 118777 #ifdef YYERRORSYMBOL
 118778       /* A syntax error has occurred.
 118779       ** The response to an error depends upon whether or not the
 118780       ** grammar defines an error token "ERROR".  
 118782       ** This is what we do if the grammar does define ERROR:
 118784       **  * Call the %syntax_error function.
 118786       **  * Begin popping the stack until we enter a state where
 118787       **    it is legal to shift the error symbol, then shift
 118788       **    the error symbol.
 118790       **  * Set the error count to three.
 118792       **  * Begin accepting and shifting new tokens.  No new error
 118793       **    processing will occur until three tokens have been
 118794       **    shifted successfully.
 118797       if( yypParser->yyerrcnt<0 ){
 118798         yy_syntax_error(yypParser,yymajor,yyminorunion);
 118800       yymx = yypParser->yystack[yypParser->yyidx].major;
 118801       if( yymx==YYERRORSYMBOL || yyerrorhit ){
 118802 #ifndef NDEBUG
 118803         if( yyTraceFILE ){
 118804           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
 118805              yyTracePrompt,yyTokenName[yymajor]);
 118807 #endif
 118808         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
 118809         yymajor = YYNOCODE;
 118810       }else{
 118811          while(
 118812           yypParser->yyidx >= 0 &&
 118813           yymx != YYERRORSYMBOL &&
 118814           (yyact = yy_find_reduce_action(
 118815                         yypParser->yystack[yypParser->yyidx].stateno,
 118816                         YYERRORSYMBOL)) >= YYNSTATE
 118818           yy_pop_parser_stack(yypParser);
 118820         if( yypParser->yyidx < 0 || yymajor==0 ){
 118821           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 118822           yy_parse_failed(yypParser);
 118823           yymajor = YYNOCODE;
 118824         }else if( yymx!=YYERRORSYMBOL ){
 118825           YYMINORTYPE u2;
 118826           u2.YYERRSYMDT = 0;
 118827           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
 118830       yypParser->yyerrcnt = 3;
 118831       yyerrorhit = 1;
 118832 #elif defined(YYNOERRORRECOVERY)
 118833       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
 118834       ** do any kind of error recovery.  Instead, simply invoke the syntax
 118835       ** error routine and continue going as if nothing had happened.
 118837       ** Applications can set this macro (for example inside %include) if
 118838       ** they intend to abandon the parse upon the first syntax error seen.
 118840       yy_syntax_error(yypParser,yymajor,yyminorunion);
 118841       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 118842       yymajor = YYNOCODE;
 118844 #else  /* YYERRORSYMBOL is not defined */
 118845       /* This is what we do if the grammar does not define ERROR:
 118847       **  * Report an error message, and throw away the input token.
 118849       **  * If the input token is $, then fail the parse.
 118851       ** As before, subsequent error messages are suppressed until
 118852       ** three input tokens have been successfully shifted.
 118854       if( yypParser->yyerrcnt<=0 ){
 118855         yy_syntax_error(yypParser,yymajor,yyminorunion);
 118857       yypParser->yyerrcnt = 3;
 118858       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
 118859       if( yyendofinput ){
 118860         yy_parse_failed(yypParser);
 118862       yymajor = YYNOCODE;
 118863 #endif
 118865   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
 118866   return;
 118869 /************** End of parse.c ***********************************************/
 118870 /************** Begin file tokenize.c ****************************************/
 118872 ** 2001 September 15
 118874 ** The author disclaims copyright to this source code.  In place of
 118875 ** a legal notice, here is a blessing:
 118877 **    May you do good and not evil.
 118878 **    May you find forgiveness for yourself and forgive others.
 118879 **    May you share freely, never taking more than you give.
 118881 *************************************************************************
 118882 ** An tokenizer for SQL
 118884 ** This file contains C code that splits an SQL input string up into
 118885 ** individual tokens and sends those tokens one-by-one over to the
 118886 ** parser for analysis.
 118888 /* #include <stdlib.h> */
 118891 ** The charMap() macro maps alphabetic characters into their
 118892 ** lower-case ASCII equivalent.  On ASCII machines, this is just
 118893 ** an upper-to-lower case map.  On EBCDIC machines we also need
 118894 ** to adjust the encoding.  Only alphabetic characters and underscores
 118895 ** need to be translated.
 118897 #ifdef SQLITE_ASCII
 118898 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
 118899 #endif
 118900 #ifdef SQLITE_EBCDIC
 118901 # define charMap(X) ebcdicToAscii[(unsigned char)X]
 118902 const unsigned char ebcdicToAscii[] = {
 118903 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
 118904    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
 118905    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
 118906    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
 118907    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
 118908    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
 118909    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
 118910    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
 118911    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
 118912    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
 118913    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
 118914    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
 118915    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
 118916    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
 118917    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
 118918    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
 118919    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
 118921 #endif
 118924 ** The sqlite3KeywordCode function looks up an identifier to determine if
 118925 ** it is a keyword.  If it is a keyword, the token code of that keyword is 
 118926 ** returned.  If the input is not a keyword, TK_ID is returned.
 118928 ** The implementation of this routine was generated by a program,
 118929 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
 118930 ** The output of the mkkeywordhash.c program is written into a file
 118931 ** named keywordhash.h and then included into this source file by
 118932 ** the #include below.
 118934 /************** Include keywordhash.h in the middle of tokenize.c ************/
 118935 /************** Begin file keywordhash.h *************************************/
 118936 /***** This file contains automatically generated code ******
 118938 ** The code in this file has been automatically generated by
 118940 **   sqlite/tool/mkkeywordhash.c
 118942 ** The code in this file implements a function that determines whether
 118943 ** or not a given identifier is really an SQL keyword.  The same thing
 118944 ** might be implemented more directly using a hand-written hash table.
 118945 ** But by using this automatically generated code, the size of the code
 118946 ** is substantially reduced.  This is important for embedded applications
 118947 ** on platforms with limited memory.
 118949 /* Hash score: 182 */
 118950 static int keywordCode(const char *z, int n){
 118951   /* zText[] encodes 834 bytes of keywords in 554 bytes */
 118952   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
 118953   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
 118954   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
 118955   /*   UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE         */
 118956   /*   BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH     */
 118957   /*   IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN     */
 118958   /*   WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT         */
 118959   /*   CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL        */
 118960   /*   FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING        */
 118961   /*   VACUUMVIEWINITIALLY                                                */
 118962   static const char zText[553] = {
 118963     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
 118964     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
 118965     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
 118966     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
 118967     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
 118968     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
 118969     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
 118970     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
 118971     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
 118972     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
 118973     'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
 118974     'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
 118975     'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
 118976     'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
 118977     'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
 118978     'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
 118979     'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
 118980     'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
 118981     'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
 118982     'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
 118983     'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
 118984     'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
 118985     'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
 118986     'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
 118987     'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
 118988     'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
 118989     'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
 118990     'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
 118991     'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
 118992     'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
 118993     'V','I','E','W','I','N','I','T','I','A','L','L','Y',
 118995   static const unsigned char aHash[127] = {
 118996       76, 105, 117,  74,   0,  45,   0,   0,  82,   0,  77,   0,   0,
 118997       42,  12,  78,  15,   0, 116,  85,  54, 112,   0,  19,   0,   0,
 118998      121,   0, 119, 115,   0,  22,  93,   0,   9,   0,   0,  70,  71,
 118999        0,  69,   6,   0,  48,  90, 102,   0, 118, 101,   0,   0,  44,
 119000        0, 103,  24,   0,  17,   0, 122,  53,  23,   0,   5, 110,  25,
 119001       96,   0,   0, 124, 106,  60, 123,  57,  28,  55,   0,  91,   0,
 119002      100,  26,   0,  99,   0,   0,   0,  95,  92,  97,  88, 109,  14,
 119003       39, 108,   0,  81,   0,  18,  89, 111,  32,   0, 120,  80, 113,
 119004       62,  46,  84,   0,   0,  94,  40,  59, 114,   0,  36,   0,   0,
 119005       29,   0,  86,  63,  64,   0,  20,  61,   0,  56,
 119007   static const unsigned char aNext[124] = {
 119008        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
 119009        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
 119010        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
 119011        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,   0,   0,  50,
 119012        0,  43,   3,  47,   0,   0,   0,   0,  30,   0,  58,   0,  38,
 119013        0,   0,   0,   1,  66,   0,   0,  67,   0,  41,   0,   0,   0,
 119014        0,   0,   0,  49,  65,   0,   0,   0,   0,  31,  52,  16,  34,
 119015       10,   0,   0,   0,   0,   0,   0,   0,  11,  72,  79,   0,   8,
 119016        0, 104,  98,   0, 107,   0,  87,   0,  75,  51,   0,  27,  37,
 119017       73,  83,   0,  35,  68,   0,   0,
 119019   static const unsigned char aLen[124] = {
 119020        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
 119021        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
 119022       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
 119023        4,   6,   2,   3,   9,   4,   2,   6,   5,   7,   4,   5,   7,
 119024        6,   6,   5,   6,   5,   5,   9,   7,   7,   3,   2,   4,   4,
 119025        7,   3,   6,   4,   7,   6,  12,   6,   9,   4,   6,   5,   4,
 119026        7,   6,   5,   6,   7,   5,   4,   5,   6,   5,   7,   3,   7,
 119027       13,   2,   2,   4,   6,   6,   8,   5,  17,  12,   7,   8,   8,
 119028        2,   4,   4,   4,   4,   4,   2,   2,   6,   5,   8,   5,   8,
 119029        3,   5,   5,   6,   4,   9,   3,
 119031   static const unsigned short int aOffset[124] = {
 119032        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
 119033       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
 119034       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
 119035      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
 119036      199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
 119037      250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
 119038      320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
 119039      387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
 119040      460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
 119041      521, 524, 529, 534, 540, 544, 549,
 119043   static const unsigned char aCode[124] = {
 119044     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
 119045     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
 119046     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
 119047     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
 119048     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
 119049     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
 119050     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
 119051     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
 119052     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
 119053     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    TK_WITH,       
 119054     TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,     TK_HAVING,     TK_GROUP,      
 119055     TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RECURSIVE,  TK_BETWEEN,    
 119056     TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,    
 119057     TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       TK_COLLATE,    
 119058     TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  TK_JOIN,       
 119059     TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,     
 119060     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      TK_WHEN,       
 119061     TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    TK_AND,        
 119062     TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,       
 119063     TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,   
 119064     TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,         
 119065     TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_LIKE_KW,    
 119066     TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      TK_RESTRICT,   
 119067     TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_UNION,      TK_USING,      
 119068     TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  TK_ALL,        
 119070   int h, i;
 119071   if( n<2 ) return TK_ID;
 119072   h = ((charMap(z[0])*4) ^
 119073       (charMap(z[n-1])*3) ^
 119074       n) % 127;
 119075   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
 119076     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
 119077       testcase( i==0 ); /* REINDEX */
 119078       testcase( i==1 ); /* INDEXED */
 119079       testcase( i==2 ); /* INDEX */
 119080       testcase( i==3 ); /* DESC */
 119081       testcase( i==4 ); /* ESCAPE */
 119082       testcase( i==5 ); /* EACH */
 119083       testcase( i==6 ); /* CHECK */
 119084       testcase( i==7 ); /* KEY */
 119085       testcase( i==8 ); /* BEFORE */
 119086       testcase( i==9 ); /* FOREIGN */
 119087       testcase( i==10 ); /* FOR */
 119088       testcase( i==11 ); /* IGNORE */
 119089       testcase( i==12 ); /* REGEXP */
 119090       testcase( i==13 ); /* EXPLAIN */
 119091       testcase( i==14 ); /* INSTEAD */
 119092       testcase( i==15 ); /* ADD */
 119093       testcase( i==16 ); /* DATABASE */
 119094       testcase( i==17 ); /* AS */
 119095       testcase( i==18 ); /* SELECT */
 119096       testcase( i==19 ); /* TABLE */
 119097       testcase( i==20 ); /* LEFT */
 119098       testcase( i==21 ); /* THEN */
 119099       testcase( i==22 ); /* END */
 119100       testcase( i==23 ); /* DEFERRABLE */
 119101       testcase( i==24 ); /* ELSE */
 119102       testcase( i==25 ); /* EXCEPT */
 119103       testcase( i==26 ); /* TRANSACTION */
 119104       testcase( i==27 ); /* ACTION */
 119105       testcase( i==28 ); /* ON */
 119106       testcase( i==29 ); /* NATURAL */
 119107       testcase( i==30 ); /* ALTER */
 119108       testcase( i==31 ); /* RAISE */
 119109       testcase( i==32 ); /* EXCLUSIVE */
 119110       testcase( i==33 ); /* EXISTS */
 119111       testcase( i==34 ); /* SAVEPOINT */
 119112       testcase( i==35 ); /* INTERSECT */
 119113       testcase( i==36 ); /* TRIGGER */
 119114       testcase( i==37 ); /* REFERENCES */
 119115       testcase( i==38 ); /* CONSTRAINT */
 119116       testcase( i==39 ); /* INTO */
 119117       testcase( i==40 ); /* OFFSET */
 119118       testcase( i==41 ); /* OF */
 119119       testcase( i==42 ); /* SET */
 119120       testcase( i==43 ); /* TEMPORARY */
 119121       testcase( i==44 ); /* TEMP */
 119122       testcase( i==45 ); /* OR */
 119123       testcase( i==46 ); /* UNIQUE */
 119124       testcase( i==47 ); /* QUERY */
 119125       testcase( i==48 ); /* WITHOUT */
 119126       testcase( i==49 ); /* WITH */
 119127       testcase( i==50 ); /* OUTER */
 119128       testcase( i==51 ); /* RELEASE */
 119129       testcase( i==52 ); /* ATTACH */
 119130       testcase( i==53 ); /* HAVING */
 119131       testcase( i==54 ); /* GROUP */
 119132       testcase( i==55 ); /* UPDATE */
 119133       testcase( i==56 ); /* BEGIN */
 119134       testcase( i==57 ); /* INNER */
 119135       testcase( i==58 ); /* RECURSIVE */
 119136       testcase( i==59 ); /* BETWEEN */
 119137       testcase( i==60 ); /* NOTNULL */
 119138       testcase( i==61 ); /* NOT */
 119139       testcase( i==62 ); /* NO */
 119140       testcase( i==63 ); /* NULL */
 119141       testcase( i==64 ); /* LIKE */
 119142       testcase( i==65 ); /* CASCADE */
 119143       testcase( i==66 ); /* ASC */
 119144       testcase( i==67 ); /* DELETE */
 119145       testcase( i==68 ); /* CASE */
 119146       testcase( i==69 ); /* COLLATE */
 119147       testcase( i==70 ); /* CREATE */
 119148       testcase( i==71 ); /* CURRENT_DATE */
 119149       testcase( i==72 ); /* DETACH */
 119150       testcase( i==73 ); /* IMMEDIATE */
 119151       testcase( i==74 ); /* JOIN */
 119152       testcase( i==75 ); /* INSERT */
 119153       testcase( i==76 ); /* MATCH */
 119154       testcase( i==77 ); /* PLAN */
 119155       testcase( i==78 ); /* ANALYZE */
 119156       testcase( i==79 ); /* PRAGMA */
 119157       testcase( i==80 ); /* ABORT */
 119158       testcase( i==81 ); /* VALUES */
 119159       testcase( i==82 ); /* VIRTUAL */
 119160       testcase( i==83 ); /* LIMIT */
 119161       testcase( i==84 ); /* WHEN */
 119162       testcase( i==85 ); /* WHERE */
 119163       testcase( i==86 ); /* RENAME */
 119164       testcase( i==87 ); /* AFTER */
 119165       testcase( i==88 ); /* REPLACE */
 119166       testcase( i==89 ); /* AND */
 119167       testcase( i==90 ); /* DEFAULT */
 119168       testcase( i==91 ); /* AUTOINCREMENT */
 119169       testcase( i==92 ); /* TO */
 119170       testcase( i==93 ); /* IN */
 119171       testcase( i==94 ); /* CAST */
 119172       testcase( i==95 ); /* COLUMN */
 119173       testcase( i==96 ); /* COMMIT */
 119174       testcase( i==97 ); /* CONFLICT */
 119175       testcase( i==98 ); /* CROSS */
 119176       testcase( i==99 ); /* CURRENT_TIMESTAMP */
 119177       testcase( i==100 ); /* CURRENT_TIME */
 119178       testcase( i==101 ); /* PRIMARY */
 119179       testcase( i==102 ); /* DEFERRED */
 119180       testcase( i==103 ); /* DISTINCT */
 119181       testcase( i==104 ); /* IS */
 119182       testcase( i==105 ); /* DROP */
 119183       testcase( i==106 ); /* FAIL */
 119184       testcase( i==107 ); /* FROM */
 119185       testcase( i==108 ); /* FULL */
 119186       testcase( i==109 ); /* GLOB */
 119187       testcase( i==110 ); /* BY */
 119188       testcase( i==111 ); /* IF */
 119189       testcase( i==112 ); /* ISNULL */
 119190       testcase( i==113 ); /* ORDER */
 119191       testcase( i==114 ); /* RESTRICT */
 119192       testcase( i==115 ); /* RIGHT */
 119193       testcase( i==116 ); /* ROLLBACK */
 119194       testcase( i==117 ); /* ROW */
 119195       testcase( i==118 ); /* UNION */
 119196       testcase( i==119 ); /* USING */
 119197       testcase( i==120 ); /* VACUUM */
 119198       testcase( i==121 ); /* VIEW */
 119199       testcase( i==122 ); /* INITIALLY */
 119200       testcase( i==123 ); /* ALL */
 119201       return aCode[i];
 119204   return TK_ID;
 119206 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
 119207   return keywordCode((char*)z, n);
 119209 #define SQLITE_N_KEYWORD 124
 119211 /************** End of keywordhash.h *****************************************/
 119212 /************** Continuing where we left off in tokenize.c *******************/
 119216 ** If X is a character that can be used in an identifier then
 119217 ** IdChar(X) will be true.  Otherwise it is false.
 119219 ** For ASCII, any character with the high-order bit set is
 119220 ** allowed in an identifier.  For 7-bit characters, 
 119221 ** sqlite3IsIdChar[X] must be 1.
 119223 ** For EBCDIC, the rules are more complex but have the same
 119224 ** end result.
 119226 ** Ticket #1066.  the SQL standard does not allow '$' in the
 119227 ** middle of identfiers.  But many SQL implementations do. 
 119228 ** SQLite will allow '$' in identifiers for compatibility.
 119229 ** But the feature is undocumented.
 119231 #ifdef SQLITE_ASCII
 119232 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
 119233 #endif
 119234 #ifdef SQLITE_EBCDIC
 119235 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
 119236 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
 119237     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
 119238     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
 119239     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
 119240     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
 119241     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
 119242     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
 119243     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
 119244     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
 119245     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
 119246     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
 119247     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
 119248     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
 119250 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
 119251 #endif
 119255 ** Return the length of the token that begins at z[0]. 
 119256 ** Store the token type in *tokenType before returning.
 119258 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
 119259   int i, c;
 119260   switch( *z ){
 119261     case ' ': case '\t': case '\n': case '\f': case '\r': {
 119262       testcase( z[0]==' ' );
 119263       testcase( z[0]=='\t' );
 119264       testcase( z[0]=='\n' );
 119265       testcase( z[0]=='\f' );
 119266       testcase( z[0]=='\r' );
 119267       for(i=1; sqlite3Isspace(z[i]); i++){}
 119268       *tokenType = TK_SPACE;
 119269       return i;
 119271     case '-': {
 119272       if( z[1]=='-' ){
 119273         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
 119274         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
 119275         return i;
 119277       *tokenType = TK_MINUS;
 119278       return 1;
 119280     case '(': {
 119281       *tokenType = TK_LP;
 119282       return 1;
 119284     case ')': {
 119285       *tokenType = TK_RP;
 119286       return 1;
 119288     case ';': {
 119289       *tokenType = TK_SEMI;
 119290       return 1;
 119292     case '+': {
 119293       *tokenType = TK_PLUS;
 119294       return 1;
 119296     case '*': {
 119297       *tokenType = TK_STAR;
 119298       return 1;
 119300     case '/': {
 119301       if( z[1]!='*' || z[2]==0 ){
 119302         *tokenType = TK_SLASH;
 119303         return 1;
 119305       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
 119306       if( c ) i++;
 119307       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
 119308       return i;
 119310     case '%': {
 119311       *tokenType = TK_REM;
 119312       return 1;
 119314     case '=': {
 119315       *tokenType = TK_EQ;
 119316       return 1 + (z[1]=='=');
 119318     case '<': {
 119319       if( (c=z[1])=='=' ){
 119320         *tokenType = TK_LE;
 119321         return 2;
 119322       }else if( c=='>' ){
 119323         *tokenType = TK_NE;
 119324         return 2;
 119325       }else if( c=='<' ){
 119326         *tokenType = TK_LSHIFT;
 119327         return 2;
 119328       }else{
 119329         *tokenType = TK_LT;
 119330         return 1;
 119333     case '>': {
 119334       if( (c=z[1])=='=' ){
 119335         *tokenType = TK_GE;
 119336         return 2;
 119337       }else if( c=='>' ){
 119338         *tokenType = TK_RSHIFT;
 119339         return 2;
 119340       }else{
 119341         *tokenType = TK_GT;
 119342         return 1;
 119345     case '!': {
 119346       if( z[1]!='=' ){
 119347         *tokenType = TK_ILLEGAL;
 119348         return 2;
 119349       }else{
 119350         *tokenType = TK_NE;
 119351         return 2;
 119354     case '|': {
 119355       if( z[1]!='|' ){
 119356         *tokenType = TK_BITOR;
 119357         return 1;
 119358       }else{
 119359         *tokenType = TK_CONCAT;
 119360         return 2;
 119363     case ',': {
 119364       *tokenType = TK_COMMA;
 119365       return 1;
 119367     case '&': {
 119368       *tokenType = TK_BITAND;
 119369       return 1;
 119371     case '~': {
 119372       *tokenType = TK_BITNOT;
 119373       return 1;
 119375     case '`':
 119376     case '\'':
 119377     case '"': {
 119378       int delim = z[0];
 119379       testcase( delim=='`' );
 119380       testcase( delim=='\'' );
 119381       testcase( delim=='"' );
 119382       for(i=1; (c=z[i])!=0; i++){
 119383         if( c==delim ){
 119384           if( z[i+1]==delim ){
 119385             i++;
 119386           }else{
 119387             break;
 119391       if( c=='\'' ){
 119392         *tokenType = TK_STRING;
 119393         return i+1;
 119394       }else if( c!=0 ){
 119395         *tokenType = TK_ID;
 119396         return i+1;
 119397       }else{
 119398         *tokenType = TK_ILLEGAL;
 119399         return i;
 119402     case '.': {
 119403 #ifndef SQLITE_OMIT_FLOATING_POINT
 119404       if( !sqlite3Isdigit(z[1]) )
 119405 #endif
 119407         *tokenType = TK_DOT;
 119408         return 1;
 119410       /* If the next character is a digit, this is a floating point
 119411       ** number that begins with ".".  Fall thru into the next case */
 119413     case '0': case '1': case '2': case '3': case '4':
 119414     case '5': case '6': case '7': case '8': case '9': {
 119415       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
 119416       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
 119417       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
 119418       testcase( z[0]=='9' );
 119419       *tokenType = TK_INTEGER;
 119420       for(i=0; sqlite3Isdigit(z[i]); i++){}
 119421 #ifndef SQLITE_OMIT_FLOATING_POINT
 119422       if( z[i]=='.' ){
 119423         i++;
 119424         while( sqlite3Isdigit(z[i]) ){ i++; }
 119425         *tokenType = TK_FLOAT;
 119427       if( (z[i]=='e' || z[i]=='E') &&
 119428            ( sqlite3Isdigit(z[i+1]) 
 119429             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
 119432         i += 2;
 119433         while( sqlite3Isdigit(z[i]) ){ i++; }
 119434         *tokenType = TK_FLOAT;
 119436 #endif
 119437       while( IdChar(z[i]) ){
 119438         *tokenType = TK_ILLEGAL;
 119439         i++;
 119441       return i;
 119443     case '[': {
 119444       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
 119445       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
 119446       return i;
 119448     case '?': {
 119449       *tokenType = TK_VARIABLE;
 119450       for(i=1; sqlite3Isdigit(z[i]); i++){}
 119451       return i;
 119453 #ifndef SQLITE_OMIT_TCL_VARIABLE
 119454     case '$':
 119455 #endif
 119456     case '@':  /* For compatibility with MS SQL Server */
 119457     case '#':
 119458     case ':': {
 119459       int n = 0;
 119460       testcase( z[0]=='$' );  testcase( z[0]=='@' );
 119461       testcase( z[0]==':' );  testcase( z[0]=='#' );
 119462       *tokenType = TK_VARIABLE;
 119463       for(i=1; (c=z[i])!=0; i++){
 119464         if( IdChar(c) ){
 119465           n++;
 119466 #ifndef SQLITE_OMIT_TCL_VARIABLE
 119467         }else if( c=='(' && n>0 ){
 119468           do{
 119469             i++;
 119470           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
 119471           if( c==')' ){
 119472             i++;
 119473           }else{
 119474             *tokenType = TK_ILLEGAL;
 119476           break;
 119477         }else if( c==':' && z[i+1]==':' ){
 119478           i++;
 119479 #endif
 119480         }else{
 119481           break;
 119484       if( n==0 ) *tokenType = TK_ILLEGAL;
 119485       return i;
 119487 #ifndef SQLITE_OMIT_BLOB_LITERAL
 119488     case 'x': case 'X': {
 119489       testcase( z[0]=='x' ); testcase( z[0]=='X' );
 119490       if( z[1]=='\'' ){
 119491         *tokenType = TK_BLOB;
 119492         for(i=2; sqlite3Isxdigit(z[i]); i++){}
 119493         if( z[i]!='\'' || i%2 ){
 119494           *tokenType = TK_ILLEGAL;
 119495           while( z[i] && z[i]!='\'' ){ i++; }
 119497         if( z[i] ) i++;
 119498         return i;
 119500       /* Otherwise fall through to the next case */
 119502 #endif
 119503     default: {
 119504       if( !IdChar(*z) ){
 119505         break;
 119507       for(i=1; IdChar(z[i]); i++){}
 119508       *tokenType = keywordCode((char*)z, i);
 119509       return i;
 119512   *tokenType = TK_ILLEGAL;
 119513   return 1;
 119517 ** Run the parser on the given SQL string.  The parser structure is
 119518 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
 119519 ** then an and attempt is made to write an error message into 
 119520 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
 119521 ** error message.
 119523 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
 119524   int nErr = 0;                   /* Number of errors encountered */
 119525   int i;                          /* Loop counter */
 119526   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
 119527   int tokenType;                  /* type of the next token */
 119528   int lastTokenParsed = -1;       /* type of the previous token */
 119529   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
 119530   sqlite3 *db = pParse->db;       /* The database connection */
 119531   int mxSqlLen;                   /* Max length of an SQL string */
 119534   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
 119535   if( db->nVdbeActive==0 ){
 119536     db->u1.isInterrupted = 0;
 119538   pParse->rc = SQLITE_OK;
 119539   pParse->zTail = zSql;
 119540   i = 0;
 119541   assert( pzErrMsg!=0 );
 119542   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
 119543   if( pEngine==0 ){
 119544     db->mallocFailed = 1;
 119545     return SQLITE_NOMEM;
 119547   assert( pParse->pNewTable==0 );
 119548   assert( pParse->pNewTrigger==0 );
 119549   assert( pParse->nVar==0 );
 119550   assert( pParse->nzVar==0 );
 119551   assert( pParse->azVar==0 );
 119552   enableLookaside = db->lookaside.bEnabled;
 119553   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
 119554   while( !db->mallocFailed && zSql[i]!=0 ){
 119555     assert( i>=0 );
 119556     pParse->sLastToken.z = &zSql[i];
 119557     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
 119558     i += pParse->sLastToken.n;
 119559     if( i>mxSqlLen ){
 119560       pParse->rc = SQLITE_TOOBIG;
 119561       break;
 119563     switch( tokenType ){
 119564       case TK_SPACE: {
 119565         if( db->u1.isInterrupted ){
 119566           sqlite3ErrorMsg(pParse, "interrupt");
 119567           pParse->rc = SQLITE_INTERRUPT;
 119568           goto abort_parse;
 119570         break;
 119572       case TK_ILLEGAL: {
 119573         sqlite3DbFree(db, *pzErrMsg);
 119574         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
 119575                         &pParse->sLastToken);
 119576         nErr++;
 119577         goto abort_parse;
 119579       case TK_SEMI: {
 119580         pParse->zTail = &zSql[i];
 119581         /* Fall thru into the default case */
 119583       default: {
 119584         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
 119585         lastTokenParsed = tokenType;
 119586         if( pParse->rc!=SQLITE_OK ){
 119587           goto abort_parse;
 119589         break;
 119593 abort_parse:
 119594   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
 119595     if( lastTokenParsed!=TK_SEMI ){
 119596       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
 119597       pParse->zTail = &zSql[i];
 119599     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
 119601 #ifdef YYTRACKMAXSTACKDEPTH
 119602   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
 119603       sqlite3ParserStackPeak(pEngine)
 119605 #endif /* YYDEBUG */
 119606   sqlite3ParserFree(pEngine, sqlite3_free);
 119607   db->lookaside.bEnabled = enableLookaside;
 119608   if( db->mallocFailed ){
 119609     pParse->rc = SQLITE_NOMEM;
 119611   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
 119612     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
 119614   assert( pzErrMsg!=0 );
 119615   if( pParse->zErrMsg ){
 119616     *pzErrMsg = pParse->zErrMsg;
 119617     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
 119618     pParse->zErrMsg = 0;
 119619     nErr++;
 119621   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
 119622     sqlite3VdbeDelete(pParse->pVdbe);
 119623     pParse->pVdbe = 0;
 119625 #ifndef SQLITE_OMIT_SHARED_CACHE
 119626   if( pParse->nested==0 ){
 119627     sqlite3DbFree(db, pParse->aTableLock);
 119628     pParse->aTableLock = 0;
 119629     pParse->nTableLock = 0;
 119631 #endif
 119632 #ifndef SQLITE_OMIT_VIRTUALTABLE
 119633   sqlite3_free(pParse->apVtabLock);
 119634 #endif
 119636   if( !IN_DECLARE_VTAB ){
 119637     /* If the pParse->declareVtab flag is set, do not delete any table 
 119638     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
 119639     ** will take responsibility for freeing the Table structure.
 119641     sqlite3DeleteTable(db, pParse->pNewTable);
 119644   if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
 119645   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
 119646   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
 119647   sqlite3DbFree(db, pParse->azVar);
 119648   while( pParse->pAinc ){
 119649     AutoincInfo *p = pParse->pAinc;
 119650     pParse->pAinc = p->pNext;
 119651     sqlite3DbFree(db, p);
 119653   while( pParse->pZombieTab ){
 119654     Table *p = pParse->pZombieTab;
 119655     pParse->pZombieTab = p->pNextZombie;
 119656     sqlite3DeleteTable(db, p);
 119658   if( nErr>0 && pParse->rc==SQLITE_OK ){
 119659     pParse->rc = SQLITE_ERROR;
 119661   return nErr;
 119664 /************** End of tokenize.c ********************************************/
 119665 /************** Begin file complete.c ****************************************/
 119667 ** 2001 September 15
 119669 ** The author disclaims copyright to this source code.  In place of
 119670 ** a legal notice, here is a blessing:
 119672 **    May you do good and not evil.
 119673 **    May you find forgiveness for yourself and forgive others.
 119674 **    May you share freely, never taking more than you give.
 119676 *************************************************************************
 119677 ** An tokenizer for SQL
 119679 ** This file contains C code that implements the sqlite3_complete() API.
 119680 ** This code used to be part of the tokenizer.c source file.  But by
 119681 ** separating it out, the code will be automatically omitted from
 119682 ** static links that do not use it.
 119684 #ifndef SQLITE_OMIT_COMPLETE
 119687 ** This is defined in tokenize.c.  We just have to import the definition.
 119689 #ifndef SQLITE_AMALGAMATION
 119690 #ifdef SQLITE_ASCII
 119691 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
 119692 #endif
 119693 #ifdef SQLITE_EBCDIC
 119694 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
 119695 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
 119696 #endif
 119697 #endif /* SQLITE_AMALGAMATION */
 119701 ** Token types used by the sqlite3_complete() routine.  See the header
 119702 ** comments on that procedure for additional information.
 119704 #define tkSEMI    0
 119705 #define tkWS      1
 119706 #define tkOTHER   2
 119707 #ifndef SQLITE_OMIT_TRIGGER
 119708 #define tkEXPLAIN 3
 119709 #define tkCREATE  4
 119710 #define tkTEMP    5
 119711 #define tkTRIGGER 6
 119712 #define tkEND     7
 119713 #endif
 119716 ** Return TRUE if the given SQL string ends in a semicolon.
 119718 ** Special handling is require for CREATE TRIGGER statements.
 119719 ** Whenever the CREATE TRIGGER keywords are seen, the statement
 119720 ** must end with ";END;".
 119722 ** This implementation uses a state machine with 8 states:
 119724 **   (0) INVALID   We have not yet seen a non-whitespace character.
 119726 **   (1) START     At the beginning or end of an SQL statement.  This routine
 119727 **                 returns 1 if it ends in the START state and 0 if it ends
 119728 **                 in any other state.
 119730 **   (2) NORMAL    We are in the middle of statement which ends with a single
 119731 **                 semicolon.
 119733 **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
 119734 **                 a statement.
 119736 **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
 119737 **                 statement, possibly preceeded by EXPLAIN and/or followed by
 119738 **                 TEMP or TEMPORARY
 119740 **   (5) TRIGGER   We are in the middle of a trigger definition that must be
 119741 **                 ended by a semicolon, the keyword END, and another semicolon.
 119743 **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
 119744 **                 the end of a trigger definition.
 119746 **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
 119747 **                 of a trigger difinition.
 119749 ** Transitions between states above are determined by tokens extracted
 119750 ** from the input.  The following tokens are significant:
 119752 **   (0) tkSEMI      A semicolon.
 119753 **   (1) tkWS        Whitespace.
 119754 **   (2) tkOTHER     Any other SQL token.
 119755 **   (3) tkEXPLAIN   The "explain" keyword.
 119756 **   (4) tkCREATE    The "create" keyword.
 119757 **   (5) tkTEMP      The "temp" or "temporary" keyword.
 119758 **   (6) tkTRIGGER   The "trigger" keyword.
 119759 **   (7) tkEND       The "end" keyword.
 119761 ** Whitespace never causes a state transition and is always ignored.
 119762 ** This means that a SQL string of all whitespace is invalid.
 119764 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
 119765 ** to recognize the end of a trigger can be omitted.  All we have to do
 119766 ** is look for a semicolon that is not part of an string or comment.
 119768 SQLITE_API int sqlite3_complete(const char *zSql){
 119769   u8 state = 0;   /* Current state, using numbers defined in header comment */
 119770   u8 token;       /* Value of the next token */
 119772 #ifndef SQLITE_OMIT_TRIGGER
 119773   /* A complex statement machine used to detect the end of a CREATE TRIGGER
 119774   ** statement.  This is the normal case.
 119776   static const u8 trans[8][8] = {
 119777                      /* Token:                                                */
 119778      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
 119779      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
 119780      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
 119781      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
 119782      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
 119783      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
 119784      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
 119785      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
 119786      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
 119788 #else
 119789   /* If triggers are not supported by this compile then the statement machine
 119790   ** used to detect the end of a statement is much simplier
 119792   static const u8 trans[3][3] = {
 119793                      /* Token:           */
 119794      /* State:       **  SEMI  WS  OTHER */
 119795      /* 0 INVALID: */ {    1,  0,     2, },
 119796      /* 1   START: */ {    1,  1,     2, },
 119797      /* 2  NORMAL: */ {    1,  2,     2, },
 119799 #endif /* SQLITE_OMIT_TRIGGER */
 119801   while( *zSql ){
 119802     switch( *zSql ){
 119803       case ';': {  /* A semicolon */
 119804         token = tkSEMI;
 119805         break;
 119807       case ' ':
 119808       case '\r':
 119809       case '\t':
 119810       case '\n':
 119811       case '\f': {  /* White space is ignored */
 119812         token = tkWS;
 119813         break;
 119815       case '/': {   /* C-style comments */
 119816         if( zSql[1]!='*' ){
 119817           token = tkOTHER;
 119818           break;
 119820         zSql += 2;
 119821         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
 119822         if( zSql[0]==0 ) return 0;
 119823         zSql++;
 119824         token = tkWS;
 119825         break;
 119827       case '-': {   /* SQL-style comments from "--" to end of line */
 119828         if( zSql[1]!='-' ){
 119829           token = tkOTHER;
 119830           break;
 119832         while( *zSql && *zSql!='\n' ){ zSql++; }
 119833         if( *zSql==0 ) return state==1;
 119834         token = tkWS;
 119835         break;
 119837       case '[': {   /* Microsoft-style identifiers in [...] */
 119838         zSql++;
 119839         while( *zSql && *zSql!=']' ){ zSql++; }
 119840         if( *zSql==0 ) return 0;
 119841         token = tkOTHER;
 119842         break;
 119844       case '`':     /* Grave-accent quoted symbols used by MySQL */
 119845       case '"':     /* single- and double-quoted strings */
 119846       case '\'': {
 119847         int c = *zSql;
 119848         zSql++;
 119849         while( *zSql && *zSql!=c ){ zSql++; }
 119850         if( *zSql==0 ) return 0;
 119851         token = tkOTHER;
 119852         break;
 119854       default: {
 119855 #ifdef SQLITE_EBCDIC
 119856         unsigned char c;
 119857 #endif
 119858         if( IdChar((u8)*zSql) ){
 119859           /* Keywords and unquoted identifiers */
 119860           int nId;
 119861           for(nId=1; IdChar(zSql[nId]); nId++){}
 119862 #ifdef SQLITE_OMIT_TRIGGER
 119863           token = tkOTHER;
 119864 #else
 119865           switch( *zSql ){
 119866             case 'c': case 'C': {
 119867               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
 119868                 token = tkCREATE;
 119869               }else{
 119870                 token = tkOTHER;
 119872               break;
 119874             case 't': case 'T': {
 119875               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
 119876                 token = tkTRIGGER;
 119877               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
 119878                 token = tkTEMP;
 119879               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
 119880                 token = tkTEMP;
 119881               }else{
 119882                 token = tkOTHER;
 119884               break;
 119886             case 'e':  case 'E': {
 119887               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
 119888                 token = tkEND;
 119889               }else
 119890 #ifndef SQLITE_OMIT_EXPLAIN
 119891               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
 119892                 token = tkEXPLAIN;
 119893               }else
 119894 #endif
 119896                 token = tkOTHER;
 119898               break;
 119900             default: {
 119901               token = tkOTHER;
 119902               break;
 119905 #endif /* SQLITE_OMIT_TRIGGER */
 119906           zSql += nId-1;
 119907         }else{
 119908           /* Operators and special symbols */
 119909           token = tkOTHER;
 119911         break;
 119914     state = trans[state][token];
 119915     zSql++;
 119917   return state==1;
 119920 #ifndef SQLITE_OMIT_UTF16
 119922 ** This routine is the same as the sqlite3_complete() routine described
 119923 ** above, except that the parameter is required to be UTF-16 encoded, not
 119924 ** UTF-8.
 119926 SQLITE_API int sqlite3_complete16(const void *zSql){
 119927   sqlite3_value *pVal;
 119928   char const *zSql8;
 119929   int rc = SQLITE_NOMEM;
 119931 #ifndef SQLITE_OMIT_AUTOINIT
 119932   rc = sqlite3_initialize();
 119933   if( rc ) return rc;
 119934 #endif
 119935   pVal = sqlite3ValueNew(0);
 119936   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
 119937   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
 119938   if( zSql8 ){
 119939     rc = sqlite3_complete(zSql8);
 119940   }else{
 119941     rc = SQLITE_NOMEM;
 119943   sqlite3ValueFree(pVal);
 119944   return sqlite3ApiExit(0, rc);
 119946 #endif /* SQLITE_OMIT_UTF16 */
 119947 #endif /* SQLITE_OMIT_COMPLETE */
 119949 /************** End of complete.c ********************************************/
 119950 /************** Begin file main.c ********************************************/
 119952 ** 2001 September 15
 119954 ** The author disclaims copyright to this source code.  In place of
 119955 ** a legal notice, here is a blessing:
 119957 **    May you do good and not evil.
 119958 **    May you find forgiveness for yourself and forgive others.
 119959 **    May you share freely, never taking more than you give.
 119961 *************************************************************************
 119962 ** Main file for the SQLite library.  The routines in this file
 119963 ** implement the programmer interface to the library.  Routines in
 119964 ** other files are for internal use by SQLite and should not be
 119965 ** accessed by users of the library.
 119968 #ifdef SQLITE_ENABLE_FTS3
 119969 /************** Include fts3.h in the middle of main.c ***********************/
 119970 /************** Begin file fts3.h ********************************************/
 119972 ** 2006 Oct 10
 119974 ** The author disclaims copyright to this source code.  In place of
 119975 ** a legal notice, here is a blessing:
 119977 **    May you do good and not evil.
 119978 **    May you find forgiveness for yourself and forgive others.
 119979 **    May you share freely, never taking more than you give.
 119981 ******************************************************************************
 119983 ** This header file is used by programs that want to link against the
 119984 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
 119987 #if 0
 119988 extern "C" {
 119989 #endif  /* __cplusplus */
 119991 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
 119993 #if 0
 119994 }  /* extern "C" */
 119995 #endif  /* __cplusplus */
 119997 /************** End of fts3.h ************************************************/
 119998 /************** Continuing where we left off in main.c ***********************/
 119999 #endif
 120000 #ifdef SQLITE_ENABLE_RTREE
 120001 /************** Include rtree.h in the middle of main.c **********************/
 120002 /************** Begin file rtree.h *******************************************/
 120004 ** 2008 May 26
 120006 ** The author disclaims copyright to this source code.  In place of
 120007 ** a legal notice, here is a blessing:
 120009 **    May you do good and not evil.
 120010 **    May you find forgiveness for yourself and forgive others.
 120011 **    May you share freely, never taking more than you give.
 120013 ******************************************************************************
 120015 ** This header file is used by programs that want to link against the
 120016 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
 120019 #if 0
 120020 extern "C" {
 120021 #endif  /* __cplusplus */
 120023 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
 120025 #if 0
 120026 }  /* extern "C" */
 120027 #endif  /* __cplusplus */
 120029 /************** End of rtree.h ***********************************************/
 120030 /************** Continuing where we left off in main.c ***********************/
 120031 #endif
 120032 #ifdef SQLITE_ENABLE_ICU
 120033 /************** Include sqliteicu.h in the middle of main.c ******************/
 120034 /************** Begin file sqliteicu.h ***************************************/
 120036 ** 2008 May 26
 120038 ** The author disclaims copyright to this source code.  In place of
 120039 ** a legal notice, here is a blessing:
 120041 **    May you do good and not evil.
 120042 **    May you find forgiveness for yourself and forgive others.
 120043 **    May you share freely, never taking more than you give.
 120045 ******************************************************************************
 120047 ** This header file is used by programs that want to link against the
 120048 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
 120051 #if 0
 120052 extern "C" {
 120053 #endif  /* __cplusplus */
 120055 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
 120057 #if 0
 120058 }  /* extern "C" */
 120059 #endif  /* __cplusplus */
 120062 /************** End of sqliteicu.h *******************************************/
 120063 /************** Continuing where we left off in main.c ***********************/
 120064 #endif
 120066 #ifndef SQLITE_AMALGAMATION
 120067 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
 120068 ** contains the text of SQLITE_VERSION macro. 
 120070 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
 120071 #endif
 120073 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
 120074 ** a pointer to the to the sqlite3_version[] string constant. 
 120076 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
 120078 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
 120079 ** pointer to a string constant whose value is the same as the
 120080 ** SQLITE_SOURCE_ID C preprocessor macro. 
 120082 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
 120084 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
 120085 ** returns an integer equal to SQLITE_VERSION_NUMBER.
 120087 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
 120089 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
 120090 ** zero if and only if SQLite was compiled with mutexing code omitted due to
 120091 ** the SQLITE_THREADSAFE compile-time option being set to 0.
 120093 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
 120095 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
 120097 ** If the following function pointer is not NULL and if
 120098 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
 120099 ** I/O active are written using this function.  These messages
 120100 ** are intended for debugging activity only.
 120102 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
 120103 #endif
 120106 ** If the following global variable points to a string which is the
 120107 ** name of a directory, then that directory will be used to store
 120108 ** temporary files.
 120110 ** See also the "PRAGMA temp_store_directory" SQL command.
 120112 SQLITE_API char *sqlite3_temp_directory = 0;
 120115 ** If the following global variable points to a string which is the
 120116 ** name of a directory, then that directory will be used to store
 120117 ** all database files specified with a relative pathname.
 120119 ** See also the "PRAGMA data_store_directory" SQL command.
 120121 SQLITE_API char *sqlite3_data_directory = 0;
 120124 ** Initialize SQLite.  
 120126 ** This routine must be called to initialize the memory allocation,
 120127 ** VFS, and mutex subsystems prior to doing any serious work with
 120128 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
 120129 ** this routine will be called automatically by key routines such as
 120130 ** sqlite3_open().  
 120132 ** This routine is a no-op except on its very first call for the process,
 120133 ** or for the first call after a call to sqlite3_shutdown.
 120135 ** The first thread to call this routine runs the initialization to
 120136 ** completion.  If subsequent threads call this routine before the first
 120137 ** thread has finished the initialization process, then the subsequent
 120138 ** threads must block until the first thread finishes with the initialization.
 120140 ** The first thread might call this routine recursively.  Recursive
 120141 ** calls to this routine should not block, of course.  Otherwise the
 120142 ** initialization process would never complete.
 120144 ** Let X be the first thread to enter this routine.  Let Y be some other
 120145 ** thread.  Then while the initial invocation of this routine by X is
 120146 ** incomplete, it is required that:
 120148 **    *  Calls to this routine from Y must block until the outer-most
 120149 **       call by X completes.
 120151 **    *  Recursive calls to this routine from thread X return immediately
 120152 **       without blocking.
 120154 SQLITE_API int sqlite3_initialize(void){
 120155   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
 120156   int rc;                                      /* Result code */
 120157 #ifdef SQLITE_EXTRA_INIT
 120158   int bRunExtraInit = 0;                       /* Extra initialization needed */
 120159 #endif
 120161 #ifdef SQLITE_OMIT_WSD
 120162   rc = sqlite3_wsd_init(4096, 24);
 120163   if( rc!=SQLITE_OK ){
 120164     return rc;
 120166 #endif
 120168   /* If SQLite is already completely initialized, then this call
 120169   ** to sqlite3_initialize() should be a no-op.  But the initialization
 120170   ** must be complete.  So isInit must not be set until the very end
 120171   ** of this routine.
 120173   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
 120175   /* Make sure the mutex subsystem is initialized.  If unable to 
 120176   ** initialize the mutex subsystem, return early with the error.
 120177   ** If the system is so sick that we are unable to allocate a mutex,
 120178   ** there is not much SQLite is going to be able to do.
 120180   ** The mutex subsystem must take care of serializing its own
 120181   ** initialization.
 120183   rc = sqlite3MutexInit();
 120184   if( rc ) return rc;
 120186   /* Initialize the malloc() system and the recursive pInitMutex mutex.
 120187   ** This operation is protected by the STATIC_MASTER mutex.  Note that
 120188   ** MutexAlloc() is called for a static mutex prior to initializing the
 120189   ** malloc subsystem - this implies that the allocation of a static
 120190   ** mutex must not require support from the malloc subsystem.
 120192   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
 120193   sqlite3_mutex_enter(pMaster);
 120194   sqlite3GlobalConfig.isMutexInit = 1;
 120195   if( !sqlite3GlobalConfig.isMallocInit ){
 120196     rc = sqlite3MallocInit();
 120198   if( rc==SQLITE_OK ){
 120199     sqlite3GlobalConfig.isMallocInit = 1;
 120200     if( !sqlite3GlobalConfig.pInitMutex ){
 120201       sqlite3GlobalConfig.pInitMutex =
 120202            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
 120203       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
 120204         rc = SQLITE_NOMEM;
 120208   if( rc==SQLITE_OK ){
 120209     sqlite3GlobalConfig.nRefInitMutex++;
 120211   sqlite3_mutex_leave(pMaster);
 120213   /* If rc is not SQLITE_OK at this point, then either the malloc
 120214   ** subsystem could not be initialized or the system failed to allocate
 120215   ** the pInitMutex mutex. Return an error in either case.  */
 120216   if( rc!=SQLITE_OK ){
 120217     return rc;
 120220   /* Do the rest of the initialization under the recursive mutex so
 120221   ** that we will be able to handle recursive calls into
 120222   ** sqlite3_initialize().  The recursive calls normally come through
 120223   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
 120224   ** recursive calls might also be possible.
 120226   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
 120227   ** to the xInit method, so the xInit method need not be threadsafe.
 120229   ** The following mutex is what serializes access to the appdef pcache xInit
 120230   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
 120231   ** call to sqlite3PcacheInitialize().
 120233   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
 120234   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
 120235     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 120236     sqlite3GlobalConfig.inProgress = 1;
 120237     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
 120238     sqlite3RegisterGlobalFunctions();
 120239     if( sqlite3GlobalConfig.isPCacheInit==0 ){
 120240       rc = sqlite3PcacheInitialize();
 120242     if( rc==SQLITE_OK ){
 120243       sqlite3GlobalConfig.isPCacheInit = 1;
 120244       rc = sqlite3OsInit();
 120246     if( rc==SQLITE_OK ){
 120247       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
 120248           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
 120249       sqlite3GlobalConfig.isInit = 1;
 120250 #ifdef SQLITE_EXTRA_INIT
 120251       bRunExtraInit = 1;
 120252 #endif
 120254     sqlite3GlobalConfig.inProgress = 0;
 120256   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
 120258   /* Go back under the static mutex and clean up the recursive
 120259   ** mutex to prevent a resource leak.
 120261   sqlite3_mutex_enter(pMaster);
 120262   sqlite3GlobalConfig.nRefInitMutex--;
 120263   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
 120264     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
 120265     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
 120266     sqlite3GlobalConfig.pInitMutex = 0;
 120268   sqlite3_mutex_leave(pMaster);
 120270   /* The following is just a sanity check to make sure SQLite has
 120271   ** been compiled correctly.  It is important to run this code, but
 120272   ** we don't want to run it too often and soak up CPU cycles for no
 120273   ** reason.  So we run it once during initialization.
 120275 #ifndef NDEBUG
 120276 #ifndef SQLITE_OMIT_FLOATING_POINT
 120277   /* This section of code's only "output" is via assert() statements. */
 120278   if ( rc==SQLITE_OK ){
 120279     u64 x = (((u64)1)<<63)-1;
 120280     double y;
 120281     assert(sizeof(x)==8);
 120282     assert(sizeof(x)==sizeof(y));
 120283     memcpy(&y, &x, 8);
 120284     assert( sqlite3IsNaN(y) );
 120286 #endif
 120287 #endif
 120289   /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
 120290   ** compile-time option.
 120292 #ifdef SQLITE_EXTRA_INIT
 120293   if( bRunExtraInit ){
 120294     int SQLITE_EXTRA_INIT(const char*);
 120295     rc = SQLITE_EXTRA_INIT(0);
 120297 #endif
 120299   return rc;
 120303 ** Undo the effects of sqlite3_initialize().  Must not be called while
 120304 ** there are outstanding database connections or memory allocations or
 120305 ** while any part of SQLite is otherwise in use in any thread.  This
 120306 ** routine is not threadsafe.  But it is safe to invoke this routine
 120307 ** on when SQLite is already shut down.  If SQLite is already shut down
 120308 ** when this routine is invoked, then this routine is a harmless no-op.
 120310 SQLITE_API int sqlite3_shutdown(void){
 120311   if( sqlite3GlobalConfig.isInit ){
 120312 #ifdef SQLITE_EXTRA_SHUTDOWN
 120313     void SQLITE_EXTRA_SHUTDOWN(void);
 120314     SQLITE_EXTRA_SHUTDOWN();
 120315 #endif
 120316     sqlite3_os_end();
 120317     sqlite3_reset_auto_extension();
 120318     sqlite3GlobalConfig.isInit = 0;
 120320   if( sqlite3GlobalConfig.isPCacheInit ){
 120321     sqlite3PcacheShutdown();
 120322     sqlite3GlobalConfig.isPCacheInit = 0;
 120324   if( sqlite3GlobalConfig.isMallocInit ){
 120325     sqlite3MallocEnd();
 120326     sqlite3GlobalConfig.isMallocInit = 0;
 120328 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
 120329     /* The heap subsystem has now been shutdown and these values are supposed
 120330     ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
 120331     ** which would rely on that heap subsystem; therefore, make sure these
 120332     ** values cannot refer to heap memory that was just invalidated when the
 120333     ** heap subsystem was shutdown.  This is only done if the current call to
 120334     ** this function resulted in the heap subsystem actually being shutdown.
 120336     sqlite3_data_directory = 0;
 120337     sqlite3_temp_directory = 0;
 120338 #endif
 120340   if( sqlite3GlobalConfig.isMutexInit ){
 120341     sqlite3MutexEnd();
 120342     sqlite3GlobalConfig.isMutexInit = 0;
 120345   return SQLITE_OK;
 120349 ** This API allows applications to modify the global configuration of
 120350 ** the SQLite library at run-time.
 120352 ** This routine should only be called when there are no outstanding
 120353 ** database connections or memory allocations.  This routine is not
 120354 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
 120355 ** behavior.
 120357 SQLITE_API int sqlite3_config(int op, ...){
 120358   va_list ap;
 120359   int rc = SQLITE_OK;
 120361   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
 120362   ** the SQLite library is in use. */
 120363   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
 120365   va_start(ap, op);
 120366   switch( op ){
 120368     /* Mutex configuration options are only available in a threadsafe
 120369     ** compile. 
 120371 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
 120372     case SQLITE_CONFIG_SINGLETHREAD: {
 120373       /* Disable all mutexing */
 120374       sqlite3GlobalConfig.bCoreMutex = 0;
 120375       sqlite3GlobalConfig.bFullMutex = 0;
 120376       break;
 120378     case SQLITE_CONFIG_MULTITHREAD: {
 120379       /* Disable mutexing of database connections */
 120380       /* Enable mutexing of core data structures */
 120381       sqlite3GlobalConfig.bCoreMutex = 1;
 120382       sqlite3GlobalConfig.bFullMutex = 0;
 120383       break;
 120385     case SQLITE_CONFIG_SERIALIZED: {
 120386       /* Enable all mutexing */
 120387       sqlite3GlobalConfig.bCoreMutex = 1;
 120388       sqlite3GlobalConfig.bFullMutex = 1;
 120389       break;
 120391     case SQLITE_CONFIG_MUTEX: {
 120392       /* Specify an alternative mutex implementation */
 120393       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
 120394       break;
 120396     case SQLITE_CONFIG_GETMUTEX: {
 120397       /* Retrieve the current mutex implementation */
 120398       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
 120399       break;
 120401 #endif
 120404     case SQLITE_CONFIG_MALLOC: {
 120405       /* Specify an alternative malloc implementation */
 120406       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
 120407       break;
 120409     case SQLITE_CONFIG_GETMALLOC: {
 120410       /* Retrieve the current malloc() implementation */
 120411       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
 120412       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
 120413       break;
 120415     case SQLITE_CONFIG_MEMSTATUS: {
 120416       /* Enable or disable the malloc status collection */
 120417       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
 120418       break;
 120420     case SQLITE_CONFIG_SCRATCH: {
 120421       /* Designate a buffer for scratch memory space */
 120422       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
 120423       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
 120424       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
 120425       break;
 120427     case SQLITE_CONFIG_PAGECACHE: {
 120428       /* Designate a buffer for page cache memory space */
 120429       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
 120430       sqlite3GlobalConfig.szPage = va_arg(ap, int);
 120431       sqlite3GlobalConfig.nPage = va_arg(ap, int);
 120432       break;
 120435     case SQLITE_CONFIG_PCACHE: {
 120436       /* no-op */
 120437       break;
 120439     case SQLITE_CONFIG_GETPCACHE: {
 120440       /* now an error */
 120441       rc = SQLITE_ERROR;
 120442       break;
 120445     case SQLITE_CONFIG_PCACHE2: {
 120446       /* Specify an alternative page cache implementation */
 120447       sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
 120448       break;
 120450     case SQLITE_CONFIG_GETPCACHE2: {
 120451       if( sqlite3GlobalConfig.pcache2.xInit==0 ){
 120452         sqlite3PCacheSetDefault();
 120454       *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
 120455       break;
 120458 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
 120459     case SQLITE_CONFIG_HEAP: {
 120460       /* Designate a buffer for heap memory space */
 120461       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
 120462       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
 120463       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
 120465       if( sqlite3GlobalConfig.mnReq<1 ){
 120466         sqlite3GlobalConfig.mnReq = 1;
 120467       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
 120468         /* cap min request size at 2^12 */
 120469         sqlite3GlobalConfig.mnReq = (1<<12);
 120472       if( sqlite3GlobalConfig.pHeap==0 ){
 120473         /* If the heap pointer is NULL, then restore the malloc implementation
 120474         ** back to NULL pointers too.  This will cause the malloc to go
 120475         ** back to its default implementation when sqlite3_initialize() is
 120476         ** run.
 120478         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
 120479       }else{
 120480         /* The heap pointer is not NULL, then install one of the
 120481         ** mem5.c/mem3.c methods.  The enclosing #if guarantees at
 120482         ** least one of these methods is currently enabled.
 120484 #ifdef SQLITE_ENABLE_MEMSYS3
 120485         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
 120486 #endif
 120487 #ifdef SQLITE_ENABLE_MEMSYS5
 120488         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
 120489 #endif
 120491       break;
 120493 #endif
 120495     case SQLITE_CONFIG_LOOKASIDE: {
 120496       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
 120497       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
 120498       break;
 120501     /* Record a pointer to the logger function and its first argument.
 120502     ** The default is NULL.  Logging is disabled if the function pointer is
 120503     ** NULL.
 120505     case SQLITE_CONFIG_LOG: {
 120506       /* MSVC is picky about pulling func ptrs from va lists.
 120507       ** http://support.microsoft.com/kb/47961
 120508       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
 120510       typedef void(*LOGFUNC_t)(void*,int,const char*);
 120511       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
 120512       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
 120513       break;
 120516     case SQLITE_CONFIG_URI: {
 120517       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
 120518       break;
 120521     case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
 120522       sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
 120523       break;
 120526 #ifdef SQLITE_ENABLE_SQLLOG
 120527     case SQLITE_CONFIG_SQLLOG: {
 120528       typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
 120529       sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
 120530       sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
 120531       break;
 120533 #endif
 120535     case SQLITE_CONFIG_MMAP_SIZE: {
 120536       sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
 120537       sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
 120538       if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
 120539         mxMmap = SQLITE_MAX_MMAP_SIZE;
 120541       sqlite3GlobalConfig.mxMmap = mxMmap;
 120542       if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
 120543       if( szMmap>mxMmap) szMmap = mxMmap;
 120544       sqlite3GlobalConfig.szMmap = szMmap;
 120545       break;
 120548 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
 120549     case SQLITE_CONFIG_WIN32_HEAPSIZE: {
 120550       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
 120551       break;
 120553 #endif
 120555     default: {
 120556       rc = SQLITE_ERROR;
 120557       break;
 120560   va_end(ap);
 120561   return rc;
 120565 ** Set up the lookaside buffers for a database connection.
 120566 ** Return SQLITE_OK on success.  
 120567 ** If lookaside is already active, return SQLITE_BUSY.
 120569 ** The sz parameter is the number of bytes in each lookaside slot.
 120570 ** The cnt parameter is the number of slots.  If pStart is NULL the
 120571 ** space for the lookaside memory is obtained from sqlite3_malloc().
 120572 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
 120573 ** the lookaside memory.
 120575 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
 120576   void *pStart;
 120577   if( db->lookaside.nOut ){
 120578     return SQLITE_BUSY;
 120580   /* Free any existing lookaside buffer for this handle before
 120581   ** allocating a new one so we don't have to have space for 
 120582   ** both at the same time.
 120584   if( db->lookaside.bMalloced ){
 120585     sqlite3_free(db->lookaside.pStart);
 120587   /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
 120588   ** than a pointer to be useful.
 120590   sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
 120591   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
 120592   if( cnt<0 ) cnt = 0;
 120593   if( sz==0 || cnt==0 ){
 120594     sz = 0;
 120595     pStart = 0;
 120596   }else if( pBuf==0 ){
 120597     sqlite3BeginBenignMalloc();
 120598     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
 120599     sqlite3EndBenignMalloc();
 120600     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
 120601   }else{
 120602     pStart = pBuf;
 120604   db->lookaside.pStart = pStart;
 120605   db->lookaside.pFree = 0;
 120606   db->lookaside.sz = (u16)sz;
 120607   if( pStart ){
 120608     int i;
 120609     LookasideSlot *p;
 120610     assert( sz > (int)sizeof(LookasideSlot*) );
 120611     p = (LookasideSlot*)pStart;
 120612     for(i=cnt-1; i>=0; i--){
 120613       p->pNext = db->lookaside.pFree;
 120614       db->lookaside.pFree = p;
 120615       p = (LookasideSlot*)&((u8*)p)[sz];
 120617     db->lookaside.pEnd = p;
 120618     db->lookaside.bEnabled = 1;
 120619     db->lookaside.bMalloced = pBuf==0 ?1:0;
 120620   }else{
 120621     db->lookaside.pStart = db;
 120622     db->lookaside.pEnd = db;
 120623     db->lookaside.bEnabled = 0;
 120624     db->lookaside.bMalloced = 0;
 120626   return SQLITE_OK;
 120630 ** Return the mutex associated with a database connection.
 120632 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
 120633   return db->mutex;
 120637 ** Free up as much memory as we can from the given database
 120638 ** connection.
 120640 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
 120641   int i;
 120642   sqlite3_mutex_enter(db->mutex);
 120643   sqlite3BtreeEnterAll(db);
 120644   for(i=0; i<db->nDb; i++){
 120645     Btree *pBt = db->aDb[i].pBt;
 120646     if( pBt ){
 120647       Pager *pPager = sqlite3BtreePager(pBt);
 120648       sqlite3PagerShrink(pPager);
 120651   sqlite3BtreeLeaveAll(db);
 120652   sqlite3_mutex_leave(db->mutex);
 120653   return SQLITE_OK;
 120657 ** Configuration settings for an individual database connection
 120659 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
 120660   va_list ap;
 120661   int rc;
 120662   va_start(ap, op);
 120663   switch( op ){
 120664     case SQLITE_DBCONFIG_LOOKASIDE: {
 120665       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
 120666       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
 120667       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
 120668       rc = setupLookaside(db, pBuf, sz, cnt);
 120669       break;
 120671     default: {
 120672       static const struct {
 120673         int op;      /* The opcode */
 120674         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
 120675       } aFlagOp[] = {
 120676         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
 120677         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
 120679       unsigned int i;
 120680       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
 120681       for(i=0; i<ArraySize(aFlagOp); i++){
 120682         if( aFlagOp[i].op==op ){
 120683           int onoff = va_arg(ap, int);
 120684           int *pRes = va_arg(ap, int*);
 120685           int oldFlags = db->flags;
 120686           if( onoff>0 ){
 120687             db->flags |= aFlagOp[i].mask;
 120688           }else if( onoff==0 ){
 120689             db->flags &= ~aFlagOp[i].mask;
 120691           if( oldFlags!=db->flags ){
 120692             sqlite3ExpirePreparedStatements(db);
 120694           if( pRes ){
 120695             *pRes = (db->flags & aFlagOp[i].mask)!=0;
 120697           rc = SQLITE_OK;
 120698           break;
 120701       break;
 120704   va_end(ap);
 120705   return rc;
 120710 ** Return true if the buffer z[0..n-1] contains all spaces.
 120712 static int allSpaces(const char *z, int n){
 120713   while( n>0 && z[n-1]==' ' ){ n--; }
 120714   return n==0;
 120718 ** This is the default collating function named "BINARY" which is always
 120719 ** available.
 120721 ** If the padFlag argument is not NULL then space padding at the end
 120722 ** of strings is ignored.  This implements the RTRIM collation.
 120724 static int binCollFunc(
 120725   void *padFlag,
 120726   int nKey1, const void *pKey1,
 120727   int nKey2, const void *pKey2
 120729   int rc, n;
 120730   n = nKey1<nKey2 ? nKey1 : nKey2;
 120731   rc = memcmp(pKey1, pKey2, n);
 120732   if( rc==0 ){
 120733     if( padFlag
 120734      && allSpaces(((char*)pKey1)+n, nKey1-n)
 120735      && allSpaces(((char*)pKey2)+n, nKey2-n)
 120737       /* Leave rc unchanged at 0 */
 120738     }else{
 120739       rc = nKey1 - nKey2;
 120742   return rc;
 120746 ** Another built-in collating sequence: NOCASE. 
 120748 ** This collating sequence is intended to be used for "case independent
 120749 ** comparison". SQLite's knowledge of upper and lower case equivalents
 120750 ** extends only to the 26 characters used in the English language.
 120752 ** At the moment there is only a UTF-8 implementation.
 120754 static int nocaseCollatingFunc(
 120755   void *NotUsed,
 120756   int nKey1, const void *pKey1,
 120757   int nKey2, const void *pKey2
 120759   int r = sqlite3StrNICmp(
 120760       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
 120761   UNUSED_PARAMETER(NotUsed);
 120762   if( 0==r ){
 120763     r = nKey1-nKey2;
 120765   return r;
 120769 ** Return the ROWID of the most recent insert
 120771 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
 120772   return db->lastRowid;
 120776 ** Return the number of changes in the most recent call to sqlite3_exec().
 120778 SQLITE_API int sqlite3_changes(sqlite3 *db){
 120779   return db->nChange;
 120783 ** Return the number of changes since the database handle was opened.
 120785 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
 120786   return db->nTotalChange;
 120790 ** Close all open savepoints. This function only manipulates fields of the
 120791 ** database handle object, it does not close any savepoints that may be open
 120792 ** at the b-tree/pager level.
 120794 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
 120795   while( db->pSavepoint ){
 120796     Savepoint *pTmp = db->pSavepoint;
 120797     db->pSavepoint = pTmp->pNext;
 120798     sqlite3DbFree(db, pTmp);
 120800   db->nSavepoint = 0;
 120801   db->nStatement = 0;
 120802   db->isTransactionSavepoint = 0;
 120806 ** Invoke the destructor function associated with FuncDef p, if any. Except,
 120807 ** if this is not the last copy of the function, do not invoke it. Multiple
 120808 ** copies of a single function are created when create_function() is called
 120809 ** with SQLITE_ANY as the encoding.
 120811 static void functionDestroy(sqlite3 *db, FuncDef *p){
 120812   FuncDestructor *pDestructor = p->pDestructor;
 120813   if( pDestructor ){
 120814     pDestructor->nRef--;
 120815     if( pDestructor->nRef==0 ){
 120816       pDestructor->xDestroy(pDestructor->pUserData);
 120817       sqlite3DbFree(db, pDestructor);
 120823 ** Disconnect all sqlite3_vtab objects that belong to database connection
 120824 ** db. This is called when db is being closed.
 120826 static void disconnectAllVtab(sqlite3 *db){
 120827 #ifndef SQLITE_OMIT_VIRTUALTABLE
 120828   int i;
 120829   sqlite3BtreeEnterAll(db);
 120830   for(i=0; i<db->nDb; i++){
 120831     Schema *pSchema = db->aDb[i].pSchema;
 120832     if( db->aDb[i].pSchema ){
 120833       HashElem *p;
 120834       for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 120835         Table *pTab = (Table *)sqliteHashData(p);
 120836         if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
 120840   sqlite3BtreeLeaveAll(db);
 120841 #else
 120842   UNUSED_PARAMETER(db);
 120843 #endif
 120847 ** Return TRUE if database connection db has unfinalized prepared
 120848 ** statements or unfinished sqlite3_backup objects.  
 120850 static int connectionIsBusy(sqlite3 *db){
 120851   int j;
 120852   assert( sqlite3_mutex_held(db->mutex) );
 120853   if( db->pVdbe ) return 1;
 120854   for(j=0; j<db->nDb; j++){
 120855     Btree *pBt = db->aDb[j].pBt;
 120856     if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
 120858   return 0;
 120862 ** Close an existing SQLite database
 120864 static int sqlite3Close(sqlite3 *db, int forceZombie){
 120865   if( !db ){
 120866     return SQLITE_OK;
 120868   if( !sqlite3SafetyCheckSickOrOk(db) ){
 120869     return SQLITE_MISUSE_BKPT;
 120871   sqlite3_mutex_enter(db->mutex);
 120873   /* Force xDisconnect calls on all virtual tables */
 120874   disconnectAllVtab(db);
 120876   /* If a transaction is open, the disconnectAllVtab() call above
 120877   ** will not have called the xDisconnect() method on any virtual
 120878   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
 120879   ** call will do so. We need to do this before the check for active
 120880   ** SQL statements below, as the v-table implementation may be storing
 120881   ** some prepared statements internally.
 120883   sqlite3VtabRollback(db);
 120885   /* Legacy behavior (sqlite3_close() behavior) is to return
 120886   ** SQLITE_BUSY if the connection can not be closed immediately.
 120888   if( !forceZombie && connectionIsBusy(db) ){
 120889     sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
 120890        "statements or unfinished backups");
 120891     sqlite3_mutex_leave(db->mutex);
 120892     return SQLITE_BUSY;
 120895 #ifdef SQLITE_ENABLE_SQLLOG
 120896   if( sqlite3GlobalConfig.xSqllog ){
 120897     /* Closing the handle. Fourth parameter is passed the value 2. */
 120898     sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
 120900 #endif
 120902   /* Convert the connection into a zombie and then close it.
 120904   db->magic = SQLITE_MAGIC_ZOMBIE;
 120905   sqlite3LeaveMutexAndCloseZombie(db);
 120906   return SQLITE_OK;
 120910 ** Two variations on the public interface for closing a database
 120911 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
 120912 ** leaves the connection option if there are unfinalized prepared
 120913 ** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
 120914 ** version forces the connection to become a zombie if there are
 120915 ** unclosed resources, and arranges for deallocation when the last
 120916 ** prepare statement or sqlite3_backup closes.
 120918 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
 120919 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
 120923 ** Close the mutex on database connection db.
 120925 ** Furthermore, if database connection db is a zombie (meaning that there
 120926 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
 120927 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
 120928 ** finished, then free all resources.
 120930 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
 120931   HashElem *i;                    /* Hash table iterator */
 120932   int j;
 120934   /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
 120935   ** or if the connection has not yet been closed by sqlite3_close_v2(),
 120936   ** then just leave the mutex and return.
 120938   if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
 120939     sqlite3_mutex_leave(db->mutex);
 120940     return;
 120943   /* If we reach this point, it means that the database connection has
 120944   ** closed all sqlite3_stmt and sqlite3_backup objects and has been
 120945   ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
 120946   ** go ahead and free all resources.
 120949   /* If a transaction is open, roll it back. This also ensures that if
 120950   ** any database schemas have been modified by an uncommitted transaction
 120951   ** they are reset. And that the required b-tree mutex is held to make
 120952   ** the pager rollback and schema reset an atomic operation. */
 120953   sqlite3RollbackAll(db, SQLITE_OK);
 120955   /* Free any outstanding Savepoint structures. */
 120956   sqlite3CloseSavepoints(db);
 120958   /* Close all database connections */
 120959   for(j=0; j<db->nDb; j++){
 120960     struct Db *pDb = &db->aDb[j];
 120961     if( pDb->pBt ){
 120962       sqlite3BtreeClose(pDb->pBt);
 120963       pDb->pBt = 0;
 120964       if( j!=1 ){
 120965         pDb->pSchema = 0;
 120969   /* Clear the TEMP schema separately and last */
 120970   if( db->aDb[1].pSchema ){
 120971     sqlite3SchemaClear(db->aDb[1].pSchema);
 120973   sqlite3VtabUnlockList(db);
 120975   /* Free up the array of auxiliary databases */
 120976   sqlite3CollapseDatabaseArray(db);
 120977   assert( db->nDb<=2 );
 120978   assert( db->aDb==db->aDbStatic );
 120980   /* Tell the code in notify.c that the connection no longer holds any
 120981   ** locks and does not require any further unlock-notify callbacks.
 120983   sqlite3ConnectionClosed(db);
 120985   for(j=0; j<ArraySize(db->aFunc.a); j++){
 120986     FuncDef *pNext, *pHash, *p;
 120987     for(p=db->aFunc.a[j]; p; p=pHash){
 120988       pHash = p->pHash;
 120989       while( p ){
 120990         functionDestroy(db, p);
 120991         pNext = p->pNext;
 120992         sqlite3DbFree(db, p);
 120993         p = pNext;
 120997   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
 120998     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
 120999     /* Invoke any destructors registered for collation sequence user data. */
 121000     for(j=0; j<3; j++){
 121001       if( pColl[j].xDel ){
 121002         pColl[j].xDel(pColl[j].pUser);
 121005     sqlite3DbFree(db, pColl);
 121007   sqlite3HashClear(&db->aCollSeq);
 121008 #ifndef SQLITE_OMIT_VIRTUALTABLE
 121009   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
 121010     Module *pMod = (Module *)sqliteHashData(i);
 121011     if( pMod->xDestroy ){
 121012       pMod->xDestroy(pMod->pAux);
 121014     sqlite3DbFree(db, pMod);
 121016   sqlite3HashClear(&db->aModule);
 121017 #endif
 121019   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
 121020   sqlite3ValueFree(db->pErr);
 121021   sqlite3CloseExtensions(db);
 121023   db->magic = SQLITE_MAGIC_ERROR;
 121025   /* The temp-database schema is allocated differently from the other schema
 121026   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
 121027   ** So it needs to be freed here. Todo: Why not roll the temp schema into
 121028   ** the same sqliteMalloc() as the one that allocates the database 
 121029   ** structure?
 121031   sqlite3DbFree(db, db->aDb[1].pSchema);
 121032   sqlite3_mutex_leave(db->mutex);
 121033   db->magic = SQLITE_MAGIC_CLOSED;
 121034   sqlite3_mutex_free(db->mutex);
 121035   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
 121036   if( db->lookaside.bMalloced ){
 121037     sqlite3_free(db->lookaside.pStart);
 121039   sqlite3_free(db);
 121043 ** Rollback all database files.  If tripCode is not SQLITE_OK, then
 121044 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
 121045 ** breaker") and made to return tripCode if there are any further
 121046 ** attempts to use that cursor.
 121048 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
 121049   int i;
 121050   int inTrans = 0;
 121051   assert( sqlite3_mutex_held(db->mutex) );
 121052   sqlite3BeginBenignMalloc();
 121054   /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). 
 121055   ** This is important in case the transaction being rolled back has
 121056   ** modified the database schema. If the b-tree mutexes are not taken
 121057   ** here, then another shared-cache connection might sneak in between
 121058   ** the database rollback and schema reset, which can cause false
 121059   ** corruption reports in some cases.  */
 121060   sqlite3BtreeEnterAll(db);
 121062   for(i=0; i<db->nDb; i++){
 121063     Btree *p = db->aDb[i].pBt;
 121064     if( p ){
 121065       if( sqlite3BtreeIsInTrans(p) ){
 121066         inTrans = 1;
 121068       sqlite3BtreeRollback(p, tripCode);
 121071   sqlite3VtabRollback(db);
 121072   sqlite3EndBenignMalloc();
 121074   if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
 121075     sqlite3ExpirePreparedStatements(db);
 121076     sqlite3ResetAllSchemasOfConnection(db);
 121078   sqlite3BtreeLeaveAll(db);
 121080   /* Any deferred constraint violations have now been resolved. */
 121081   db->nDeferredCons = 0;
 121082   db->nDeferredImmCons = 0;
 121083   db->flags &= ~SQLITE_DeferFKs;
 121085   /* If one has been configured, invoke the rollback-hook callback */
 121086   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
 121087     db->xRollbackCallback(db->pRollbackArg);
 121092 ** Return a static string containing the name corresponding to the error code
 121093 ** specified in the argument.
 121095 #if defined(SQLITE_TEST)
 121096 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
 121097   const char *zName = 0;
 121098   int i, origRc = rc;
 121099   for(i=0; i<2 && zName==0; i++, rc &= 0xff){
 121100     switch( rc ){
 121101       case SQLITE_OK:                 zName = "SQLITE_OK";                break;
 121102       case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
 121103       case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
 121104       case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
 121105       case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
 121106       case SQLITE_ABORT_ROLLBACK:     zName = "SQLITE_ABORT_ROLLBACK";    break;
 121107       case SQLITE_BUSY:               zName = "SQLITE_BUSY";              break;
 121108       case SQLITE_BUSY_RECOVERY:      zName = "SQLITE_BUSY_RECOVERY";     break;
 121109       case SQLITE_BUSY_SNAPSHOT:      zName = "SQLITE_BUSY_SNAPSHOT";     break;
 121110       case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
 121111       case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
 121112       case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
 121113       case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
 121114       case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
 121115       case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
 121116       case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
 121117       case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
 121118       case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
 121119       case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
 121120       case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
 121121       case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
 121122       case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
 121123       case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
 121124       case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
 121125       case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
 121126       case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
 121127       case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
 121128       case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
 121129       case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
 121130       case SQLITE_IOERR_BLOCKED:      zName = "SQLITE_IOERR_BLOCKED";     break;
 121131       case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
 121132       case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
 121133       case SQLITE_IOERR_CHECKRESERVEDLOCK:
 121134                                 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
 121135       case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
 121136       case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
 121137       case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
 121138       case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
 121139       case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
 121140       case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
 121141       case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
 121142       case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
 121143       case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
 121144       case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
 121145       case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
 121146       case SQLITE_IOERR_CONVPATH:     zName = "SQLITE_IOERR_CONVPATH";    break;
 121147       case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
 121148       case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
 121149       case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
 121150       case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
 121151       case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
 121152       case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
 121153       case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
 121154       case SQLITE_CANTOPEN_FULLPATH:  zName = "SQLITE_CANTOPEN_FULLPATH"; break;
 121155       case SQLITE_CANTOPEN_CONVPATH:  zName = "SQLITE_CANTOPEN_CONVPATH"; break;
 121156       case SQLITE_PROTOCOL:           zName = "SQLITE_PROTOCOL";          break;
 121157       case SQLITE_EMPTY:              zName = "SQLITE_EMPTY";             break;
 121158       case SQLITE_SCHEMA:             zName = "SQLITE_SCHEMA";            break;
 121159       case SQLITE_TOOBIG:             zName = "SQLITE_TOOBIG";            break;
 121160       case SQLITE_CONSTRAINT:         zName = "SQLITE_CONSTRAINT";        break;
 121161       case SQLITE_CONSTRAINT_UNIQUE:  zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
 121162       case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
 121163       case SQLITE_CONSTRAINT_FOREIGNKEY:
 121164                                 zName = "SQLITE_CONSTRAINT_FOREIGNKEY";   break;
 121165       case SQLITE_CONSTRAINT_CHECK:   zName = "SQLITE_CONSTRAINT_CHECK";  break;
 121166       case SQLITE_CONSTRAINT_PRIMARYKEY:
 121167                                 zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
 121168       case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
 121169       case SQLITE_CONSTRAINT_COMMITHOOK:
 121170                                 zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
 121171       case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
 121172       case SQLITE_CONSTRAINT_FUNCTION:
 121173                                 zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
 121174       case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
 121175       case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
 121176       case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
 121177       case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
 121178       case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
 121179       case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
 121180       case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
 121181       case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
 121182       case SQLITE_ROW:                zName = "SQLITE_ROW";               break;
 121183       case SQLITE_NOTICE:             zName = "SQLITE_NOTICE";            break;
 121184       case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
 121185       case SQLITE_NOTICE_RECOVER_ROLLBACK:
 121186                                 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
 121187       case SQLITE_WARNING:            zName = "SQLITE_WARNING";           break;
 121188       case SQLITE_WARNING_AUTOINDEX:  zName = "SQLITE_WARNING_AUTOINDEX"; break;
 121189       case SQLITE_DONE:               zName = "SQLITE_DONE";              break;
 121192   if( zName==0 ){
 121193     static char zBuf[50];
 121194     sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
 121195     zName = zBuf;
 121197   return zName;
 121199 #endif
 121202 ** Return a static string that describes the kind of error specified in the
 121203 ** argument.
 121205 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
 121206   static const char* const aMsg[] = {
 121207     /* SQLITE_OK          */ "not an error",
 121208     /* SQLITE_ERROR       */ "SQL logic error or missing database",
 121209     /* SQLITE_INTERNAL    */ 0,
 121210     /* SQLITE_PERM        */ "access permission denied",
 121211     /* SQLITE_ABORT       */ "callback requested query abort",
 121212     /* SQLITE_BUSY        */ "database is locked",
 121213     /* SQLITE_LOCKED      */ "database table is locked",
 121214     /* SQLITE_NOMEM       */ "out of memory",
 121215     /* SQLITE_READONLY    */ "attempt to write a readonly database",
 121216     /* SQLITE_INTERRUPT   */ "interrupted",
 121217     /* SQLITE_IOERR       */ "disk I/O error",
 121218     /* SQLITE_CORRUPT     */ "database disk image is malformed",
 121219     /* SQLITE_NOTFOUND    */ "unknown operation",
 121220     /* SQLITE_FULL        */ "database or disk is full",
 121221     /* SQLITE_CANTOPEN    */ "unable to open database file",
 121222     /* SQLITE_PROTOCOL    */ "locking protocol",
 121223     /* SQLITE_EMPTY       */ "table contains no data",
 121224     /* SQLITE_SCHEMA      */ "database schema has changed",
 121225     /* SQLITE_TOOBIG      */ "string or blob too big",
 121226     /* SQLITE_CONSTRAINT  */ "constraint failed",
 121227     /* SQLITE_MISMATCH    */ "datatype mismatch",
 121228     /* SQLITE_MISUSE      */ "library routine called out of sequence",
 121229     /* SQLITE_NOLFS       */ "large file support is disabled",
 121230     /* SQLITE_AUTH        */ "authorization denied",
 121231     /* SQLITE_FORMAT      */ "auxiliary database format error",
 121232     /* SQLITE_RANGE       */ "bind or column index out of range",
 121233     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
 121235   const char *zErr = "unknown error";
 121236   switch( rc ){
 121237     case SQLITE_ABORT_ROLLBACK: {
 121238       zErr = "abort due to ROLLBACK";
 121239       break;
 121241     default: {
 121242       rc &= 0xff;
 121243       if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
 121244         zErr = aMsg[rc];
 121246       break;
 121249   return zErr;
 121253 ** This routine implements a busy callback that sleeps and tries
 121254 ** again until a timeout value is reached.  The timeout value is
 121255 ** an integer number of milliseconds passed in as the first
 121256 ** argument.
 121258 static int sqliteDefaultBusyCallback(
 121259  void *ptr,               /* Database connection */
 121260  int count                /* Number of times table has been busy */
 121262 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
 121263   static const u8 delays[] =
 121264      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
 121265   static const u8 totals[] =
 121266      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
 121267 # define NDELAY ArraySize(delays)
 121268   sqlite3 *db = (sqlite3 *)ptr;
 121269   int timeout = db->busyTimeout;
 121270   int delay, prior;
 121272   assert( count>=0 );
 121273   if( count < NDELAY ){
 121274     delay = delays[count];
 121275     prior = totals[count];
 121276   }else{
 121277     delay = delays[NDELAY-1];
 121278     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
 121280   if( prior + delay > timeout ){
 121281     delay = timeout - prior;
 121282     if( delay<=0 ) return 0;
 121284   sqlite3OsSleep(db->pVfs, delay*1000);
 121285   return 1;
 121286 #else
 121287   sqlite3 *db = (sqlite3 *)ptr;
 121288   int timeout = ((sqlite3 *)ptr)->busyTimeout;
 121289   if( (count+1)*1000 > timeout ){
 121290     return 0;
 121292   sqlite3OsSleep(db->pVfs, 1000000);
 121293   return 1;
 121294 #endif
 121298 ** Invoke the given busy handler.
 121300 ** This routine is called when an operation failed with a lock.
 121301 ** If this routine returns non-zero, the lock is retried.  If it
 121302 ** returns 0, the operation aborts with an SQLITE_BUSY error.
 121304 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
 121305   int rc;
 121306   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
 121307   rc = p->xFunc(p->pArg, p->nBusy);
 121308   if( rc==0 ){
 121309     p->nBusy = -1;
 121310   }else{
 121311     p->nBusy++;
 121313   return rc; 
 121317 ** This routine sets the busy callback for an Sqlite database to the
 121318 ** given callback function with the given argument.
 121320 SQLITE_API int sqlite3_busy_handler(
 121321   sqlite3 *db,
 121322   int (*xBusy)(void*,int),
 121323   void *pArg
 121325   sqlite3_mutex_enter(db->mutex);
 121326   db->busyHandler.xFunc = xBusy;
 121327   db->busyHandler.pArg = pArg;
 121328   db->busyHandler.nBusy = 0;
 121329   db->busyTimeout = 0;
 121330   sqlite3_mutex_leave(db->mutex);
 121331   return SQLITE_OK;
 121334 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 121336 ** This routine sets the progress callback for an Sqlite database to the
 121337 ** given callback function with the given argument. The progress callback will
 121338 ** be invoked every nOps opcodes.
 121340 SQLITE_API void sqlite3_progress_handler(
 121341   sqlite3 *db, 
 121342   int nOps,
 121343   int (*xProgress)(void*), 
 121344   void *pArg
 121346   sqlite3_mutex_enter(db->mutex);
 121347   if( nOps>0 ){
 121348     db->xProgress = xProgress;
 121349     db->nProgressOps = (unsigned)nOps;
 121350     db->pProgressArg = pArg;
 121351   }else{
 121352     db->xProgress = 0;
 121353     db->nProgressOps = 0;
 121354     db->pProgressArg = 0;
 121356   sqlite3_mutex_leave(db->mutex);
 121358 #endif
 121362 ** This routine installs a default busy handler that waits for the
 121363 ** specified number of milliseconds before returning 0.
 121365 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
 121366   if( ms>0 ){
 121367     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 121368     db->busyTimeout = ms;
 121369   }else{
 121370     sqlite3_busy_handler(db, 0, 0);
 121372   return SQLITE_OK;
 121376 ** Cause any pending operation to stop at its earliest opportunity.
 121378 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
 121379   db->u1.isInterrupted = 1;
 121384 ** This function is exactly the same as sqlite3_create_function(), except
 121385 ** that it is designed to be called by internal code. The difference is
 121386 ** that if a malloc() fails in sqlite3_create_function(), an error code
 121387 ** is returned and the mallocFailed flag cleared. 
 121389 SQLITE_PRIVATE int sqlite3CreateFunc(
 121390   sqlite3 *db,
 121391   const char *zFunctionName,
 121392   int nArg,
 121393   int enc,
 121394   void *pUserData,
 121395   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 121396   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 121397   void (*xFinal)(sqlite3_context*),
 121398   FuncDestructor *pDestructor
 121400   FuncDef *p;
 121401   int nName;
 121402   int extraFlags;
 121404   assert( sqlite3_mutex_held(db->mutex) );
 121405   if( zFunctionName==0 ||
 121406       (xFunc && (xFinal || xStep)) || 
 121407       (!xFunc && (xFinal && !xStep)) ||
 121408       (!xFunc && (!xFinal && xStep)) ||
 121409       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
 121410       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
 121411     return SQLITE_MISUSE_BKPT;
 121414   assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
 121415   extraFlags = enc &  SQLITE_DETERMINISTIC;
 121416   enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
 121418 #ifndef SQLITE_OMIT_UTF16
 121419   /* If SQLITE_UTF16 is specified as the encoding type, transform this
 121420   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
 121421   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
 121423   ** If SQLITE_ANY is specified, add three versions of the function
 121424   ** to the hash table.
 121426   if( enc==SQLITE_UTF16 ){
 121427     enc = SQLITE_UTF16NATIVE;
 121428   }else if( enc==SQLITE_ANY ){
 121429     int rc;
 121430     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
 121431          pUserData, xFunc, xStep, xFinal, pDestructor);
 121432     if( rc==SQLITE_OK ){
 121433       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
 121434           pUserData, xFunc, xStep, xFinal, pDestructor);
 121436     if( rc!=SQLITE_OK ){
 121437       return rc;
 121439     enc = SQLITE_UTF16BE;
 121441 #else
 121442   enc = SQLITE_UTF8;
 121443 #endif
 121445   /* Check if an existing function is being overridden or deleted. If so,
 121446   ** and there are active VMs, then return SQLITE_BUSY. If a function
 121447   ** is being overridden/deleted but there are no active VMs, allow the
 121448   ** operation to continue but invalidate all precompiled statements.
 121450   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
 121451   if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
 121452     if( db->nVdbeActive ){
 121453       sqlite3Error(db, SQLITE_BUSY, 
 121454         "unable to delete/modify user-function due to active statements");
 121455       assert( !db->mallocFailed );
 121456       return SQLITE_BUSY;
 121457     }else{
 121458       sqlite3ExpirePreparedStatements(db);
 121462   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
 121463   assert(p || db->mallocFailed);
 121464   if( !p ){
 121465     return SQLITE_NOMEM;
 121468   /* If an older version of the function with a configured destructor is
 121469   ** being replaced invoke the destructor function here. */
 121470   functionDestroy(db, p);
 121472   if( pDestructor ){
 121473     pDestructor->nRef++;
 121475   p->pDestructor = pDestructor;
 121476   p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
 121477   testcase( p->funcFlags & SQLITE_DETERMINISTIC );
 121478   p->xFunc = xFunc;
 121479   p->xStep = xStep;
 121480   p->xFinalize = xFinal;
 121481   p->pUserData = pUserData;
 121482   p->nArg = (u16)nArg;
 121483   return SQLITE_OK;
 121487 ** Create new user functions.
 121489 SQLITE_API int sqlite3_create_function(
 121490   sqlite3 *db,
 121491   const char *zFunc,
 121492   int nArg,
 121493   int enc,
 121494   void *p,
 121495   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 121496   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 121497   void (*xFinal)(sqlite3_context*)
 121499   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
 121500                                     xFinal, 0);
 121503 SQLITE_API int sqlite3_create_function_v2(
 121504   sqlite3 *db,
 121505   const char *zFunc,
 121506   int nArg,
 121507   int enc,
 121508   void *p,
 121509   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
 121510   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
 121511   void (*xFinal)(sqlite3_context*),
 121512   void (*xDestroy)(void *)
 121514   int rc = SQLITE_ERROR;
 121515   FuncDestructor *pArg = 0;
 121516   sqlite3_mutex_enter(db->mutex);
 121517   if( xDestroy ){
 121518     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
 121519     if( !pArg ){
 121520       xDestroy(p);
 121521       goto out;
 121523     pArg->xDestroy = xDestroy;
 121524     pArg->pUserData = p;
 121526   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
 121527   if( pArg && pArg->nRef==0 ){
 121528     assert( rc!=SQLITE_OK );
 121529     xDestroy(p);
 121530     sqlite3DbFree(db, pArg);
 121533  out:
 121534   rc = sqlite3ApiExit(db, rc);
 121535   sqlite3_mutex_leave(db->mutex);
 121536   return rc;
 121539 #ifndef SQLITE_OMIT_UTF16
 121540 SQLITE_API int sqlite3_create_function16(
 121541   sqlite3 *db,
 121542   const void *zFunctionName,
 121543   int nArg,
 121544   int eTextRep,
 121545   void *p,
 121546   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
 121547   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
 121548   void (*xFinal)(sqlite3_context*)
 121550   int rc;
 121551   char *zFunc8;
 121552   sqlite3_mutex_enter(db->mutex);
 121553   assert( !db->mallocFailed );
 121554   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
 121555   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
 121556   sqlite3DbFree(db, zFunc8);
 121557   rc = sqlite3ApiExit(db, rc);
 121558   sqlite3_mutex_leave(db->mutex);
 121559   return rc;
 121561 #endif
 121565 ** Declare that a function has been overloaded by a virtual table.
 121567 ** If the function already exists as a regular global function, then
 121568 ** this routine is a no-op.  If the function does not exist, then create
 121569 ** a new one that always throws a run-time error.  
 121571 ** When virtual tables intend to provide an overloaded function, they
 121572 ** should call this routine to make sure the global function exists.
 121573 ** A global function must exist in order for name resolution to work
 121574 ** properly.
 121576 SQLITE_API int sqlite3_overload_function(
 121577   sqlite3 *db,
 121578   const char *zName,
 121579   int nArg
 121581   int nName = sqlite3Strlen30(zName);
 121582   int rc = SQLITE_OK;
 121583   sqlite3_mutex_enter(db->mutex);
 121584   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
 121585     rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
 121586                            0, sqlite3InvalidFunction, 0, 0, 0);
 121588   rc = sqlite3ApiExit(db, rc);
 121589   sqlite3_mutex_leave(db->mutex);
 121590   return rc;
 121593 #ifndef SQLITE_OMIT_TRACE
 121595 ** Register a trace function.  The pArg from the previously registered trace
 121596 ** is returned.  
 121598 ** A NULL trace function means that no tracing is executes.  A non-NULL
 121599 ** trace is a pointer to a function that is invoked at the start of each
 121600 ** SQL statement.
 121602 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
 121603   void *pOld;
 121604   sqlite3_mutex_enter(db->mutex);
 121605   pOld = db->pTraceArg;
 121606   db->xTrace = xTrace;
 121607   db->pTraceArg = pArg;
 121608   sqlite3_mutex_leave(db->mutex);
 121609   return pOld;
 121612 ** Register a profile function.  The pArg from the previously registered 
 121613 ** profile function is returned.  
 121615 ** A NULL profile function means that no profiling is executes.  A non-NULL
 121616 ** profile is a pointer to a function that is invoked at the conclusion of
 121617 ** each SQL statement that is run.
 121619 SQLITE_API void *sqlite3_profile(
 121620   sqlite3 *db,
 121621   void (*xProfile)(void*,const char*,sqlite_uint64),
 121622   void *pArg
 121624   void *pOld;
 121625   sqlite3_mutex_enter(db->mutex);
 121626   pOld = db->pProfileArg;
 121627   db->xProfile = xProfile;
 121628   db->pProfileArg = pArg;
 121629   sqlite3_mutex_leave(db->mutex);
 121630   return pOld;
 121632 #endif /* SQLITE_OMIT_TRACE */
 121635 ** Register a function to be invoked when a transaction commits.
 121636 ** If the invoked function returns non-zero, then the commit becomes a
 121637 ** rollback.
 121639 SQLITE_API void *sqlite3_commit_hook(
 121640   sqlite3 *db,              /* Attach the hook to this database */
 121641   int (*xCallback)(void*),  /* Function to invoke on each commit */
 121642   void *pArg                /* Argument to the function */
 121644   void *pOld;
 121645   sqlite3_mutex_enter(db->mutex);
 121646   pOld = db->pCommitArg;
 121647   db->xCommitCallback = xCallback;
 121648   db->pCommitArg = pArg;
 121649   sqlite3_mutex_leave(db->mutex);
 121650   return pOld;
 121654 ** Register a callback to be invoked each time a row is updated,
 121655 ** inserted or deleted using this database connection.
 121657 SQLITE_API void *sqlite3_update_hook(
 121658   sqlite3 *db,              /* Attach the hook to this database */
 121659   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
 121660   void *pArg                /* Argument to the function */
 121662   void *pRet;
 121663   sqlite3_mutex_enter(db->mutex);
 121664   pRet = db->pUpdateArg;
 121665   db->xUpdateCallback = xCallback;
 121666   db->pUpdateArg = pArg;
 121667   sqlite3_mutex_leave(db->mutex);
 121668   return pRet;
 121672 ** Register a callback to be invoked each time a transaction is rolled
 121673 ** back by this database connection.
 121675 SQLITE_API void *sqlite3_rollback_hook(
 121676   sqlite3 *db,              /* Attach the hook to this database */
 121677   void (*xCallback)(void*), /* Callback function */
 121678   void *pArg                /* Argument to the function */
 121680   void *pRet;
 121681   sqlite3_mutex_enter(db->mutex);
 121682   pRet = db->pRollbackArg;
 121683   db->xRollbackCallback = xCallback;
 121684   db->pRollbackArg = pArg;
 121685   sqlite3_mutex_leave(db->mutex);
 121686   return pRet;
 121689 #ifndef SQLITE_OMIT_WAL
 121691 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
 121692 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
 121693 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
 121694 ** wal_autocheckpoint()).
 121696 SQLITE_PRIVATE int sqlite3WalDefaultHook(
 121697   void *pClientData,     /* Argument */
 121698   sqlite3 *db,           /* Connection */
 121699   const char *zDb,       /* Database */
 121700   int nFrame             /* Size of WAL */
 121702   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
 121703     sqlite3BeginBenignMalloc();
 121704     sqlite3_wal_checkpoint(db, zDb);
 121705     sqlite3EndBenignMalloc();
 121707   return SQLITE_OK;
 121709 #endif /* SQLITE_OMIT_WAL */
 121712 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
 121713 ** a database after committing a transaction if there are nFrame or
 121714 ** more frames in the log file. Passing zero or a negative value as the
 121715 ** nFrame parameter disables automatic checkpoints entirely.
 121717 ** The callback registered by this function replaces any existing callback
 121718 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
 121719 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
 121720 ** configured by this function.
 121722 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
 121723 #ifdef SQLITE_OMIT_WAL
 121724   UNUSED_PARAMETER(db);
 121725   UNUSED_PARAMETER(nFrame);
 121726 #else
 121727   if( nFrame>0 ){
 121728     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
 121729   }else{
 121730     sqlite3_wal_hook(db, 0, 0);
 121732 #endif
 121733   return SQLITE_OK;
 121737 ** Register a callback to be invoked each time a transaction is written
 121738 ** into the write-ahead-log by this database connection.
 121740 SQLITE_API void *sqlite3_wal_hook(
 121741   sqlite3 *db,                    /* Attach the hook to this db handle */
 121742   int(*xCallback)(void *, sqlite3*, const char*, int),
 121743   void *pArg                      /* First argument passed to xCallback() */
 121745 #ifndef SQLITE_OMIT_WAL
 121746   void *pRet;
 121747   sqlite3_mutex_enter(db->mutex);
 121748   pRet = db->pWalArg;
 121749   db->xWalCallback = xCallback;
 121750   db->pWalArg = pArg;
 121751   sqlite3_mutex_leave(db->mutex);
 121752   return pRet;
 121753 #else
 121754   return 0;
 121755 #endif
 121759 ** Checkpoint database zDb.
 121761 SQLITE_API int sqlite3_wal_checkpoint_v2(
 121762   sqlite3 *db,                    /* Database handle */
 121763   const char *zDb,                /* Name of attached database (or NULL) */
 121764   int eMode,                      /* SQLITE_CHECKPOINT_* value */
 121765   int *pnLog,                     /* OUT: Size of WAL log in frames */
 121766   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
 121768 #ifdef SQLITE_OMIT_WAL
 121769   return SQLITE_OK;
 121770 #else
 121771   int rc;                         /* Return code */
 121772   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
 121774   /* Initialize the output variables to -1 in case an error occurs. */
 121775   if( pnLog ) *pnLog = -1;
 121776   if( pnCkpt ) *pnCkpt = -1;
 121778   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
 121779   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
 121780   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
 121781   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
 121782     return SQLITE_MISUSE;
 121785   sqlite3_mutex_enter(db->mutex);
 121786   if( zDb && zDb[0] ){
 121787     iDb = sqlite3FindDbName(db, zDb);
 121789   if( iDb<0 ){
 121790     rc = SQLITE_ERROR;
 121791     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
 121792   }else{
 121793     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
 121794     sqlite3Error(db, rc, 0);
 121796   rc = sqlite3ApiExit(db, rc);
 121797   sqlite3_mutex_leave(db->mutex);
 121798   return rc;
 121799 #endif
 121804 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
 121805 ** to contains a zero-length string, all attached databases are 
 121806 ** checkpointed.
 121808 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
 121809   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
 121812 #ifndef SQLITE_OMIT_WAL
 121814 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
 121815 ** not currently open in WAL mode.
 121817 ** If a transaction is open on the database being checkpointed, this 
 121818 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
 121819 ** an error occurs while running the checkpoint, an SQLite error code is 
 121820 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
 121822 ** The mutex on database handle db should be held by the caller. The mutex
 121823 ** associated with the specific b-tree being checkpointed is taken by
 121824 ** this function while the checkpoint is running.
 121826 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
 121827 ** checkpointed. If an error is encountered it is returned immediately -
 121828 ** no attempt is made to checkpoint any remaining databases.
 121830 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 121832 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
 121833   int rc = SQLITE_OK;             /* Return code */
 121834   int i;                          /* Used to iterate through attached dbs */
 121835   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
 121837   assert( sqlite3_mutex_held(db->mutex) );
 121838   assert( !pnLog || *pnLog==-1 );
 121839   assert( !pnCkpt || *pnCkpt==-1 );
 121841   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
 121842     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
 121843       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
 121844       pnLog = 0;
 121845       pnCkpt = 0;
 121846       if( rc==SQLITE_BUSY ){
 121847         bBusy = 1;
 121848         rc = SQLITE_OK;
 121853   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
 121855 #endif /* SQLITE_OMIT_WAL */
 121858 ** This function returns true if main-memory should be used instead of
 121859 ** a temporary file for transient pager files and statement journals.
 121860 ** The value returned depends on the value of db->temp_store (runtime
 121861 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
 121862 ** following table describes the relationship between these two values
 121863 ** and this functions return value.
 121865 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
 121866 **   -----------------     --------------     ------------------------------
 121867 **   0                     any                file      (return 0)
 121868 **   1                     1                  file      (return 0)
 121869 **   1                     2                  memory    (return 1)
 121870 **   1                     0                  file      (return 0)
 121871 **   2                     1                  file      (return 0)
 121872 **   2                     2                  memory    (return 1)
 121873 **   2                     0                  memory    (return 1)
 121874 **   3                     any                memory    (return 1)
 121876 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
 121877 #if SQLITE_TEMP_STORE==1
 121878   return ( db->temp_store==2 );
 121879 #endif
 121880 #if SQLITE_TEMP_STORE==2
 121881   return ( db->temp_store!=1 );
 121882 #endif
 121883 #if SQLITE_TEMP_STORE==3
 121884   return 1;
 121885 #endif
 121886 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
 121887   return 0;
 121888 #endif
 121892 ** Return UTF-8 encoded English language explanation of the most recent
 121893 ** error.
 121895 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
 121896   const char *z;
 121897   if( !db ){
 121898     return sqlite3ErrStr(SQLITE_NOMEM);
 121900   if( !sqlite3SafetyCheckSickOrOk(db) ){
 121901     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
 121903   sqlite3_mutex_enter(db->mutex);
 121904   if( db->mallocFailed ){
 121905     z = sqlite3ErrStr(SQLITE_NOMEM);
 121906   }else{
 121907     testcase( db->pErr==0 );
 121908     z = (char*)sqlite3_value_text(db->pErr);
 121909     assert( !db->mallocFailed );
 121910     if( z==0 ){
 121911       z = sqlite3ErrStr(db->errCode);
 121914   sqlite3_mutex_leave(db->mutex);
 121915   return z;
 121918 #ifndef SQLITE_OMIT_UTF16
 121920 ** Return UTF-16 encoded English language explanation of the most recent
 121921 ** error.
 121923 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
 121924   static const u16 outOfMem[] = {
 121925     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
 121927   static const u16 misuse[] = {
 121928     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
 121929     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
 121930     'c', 'a', 'l', 'l', 'e', 'd', ' ', 
 121931     'o', 'u', 't', ' ', 
 121932     'o', 'f', ' ', 
 121933     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
 121936   const void *z;
 121937   if( !db ){
 121938     return (void *)outOfMem;
 121940   if( !sqlite3SafetyCheckSickOrOk(db) ){
 121941     return (void *)misuse;
 121943   sqlite3_mutex_enter(db->mutex);
 121944   if( db->mallocFailed ){
 121945     z = (void *)outOfMem;
 121946   }else{
 121947     z = sqlite3_value_text16(db->pErr);
 121948     if( z==0 ){
 121949       sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
 121950       z = sqlite3_value_text16(db->pErr);
 121952     /* A malloc() may have failed within the call to sqlite3_value_text16()
 121953     ** above. If this is the case, then the db->mallocFailed flag needs to
 121954     ** be cleared before returning. Do this directly, instead of via
 121955     ** sqlite3ApiExit(), to avoid setting the database handle error message.
 121957     db->mallocFailed = 0;
 121959   sqlite3_mutex_leave(db->mutex);
 121960   return z;
 121962 #endif /* SQLITE_OMIT_UTF16 */
 121965 ** Return the most recent error code generated by an SQLite routine. If NULL is
 121966 ** passed to this function, we assume a malloc() failed during sqlite3_open().
 121968 SQLITE_API int sqlite3_errcode(sqlite3 *db){
 121969   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
 121970     return SQLITE_MISUSE_BKPT;
 121972   if( !db || db->mallocFailed ){
 121973     return SQLITE_NOMEM;
 121975   return db->errCode & db->errMask;
 121977 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
 121978   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
 121979     return SQLITE_MISUSE_BKPT;
 121981   if( !db || db->mallocFailed ){
 121982     return SQLITE_NOMEM;
 121984   return db->errCode;
 121988 ** Return a string that describes the kind of error specified in the
 121989 ** argument.  For now, this simply calls the internal sqlite3ErrStr()
 121990 ** function.
 121992 SQLITE_API const char *sqlite3_errstr(int rc){
 121993   return sqlite3ErrStr(rc);
 121997 ** Invalidate all cached KeyInfo objects for database connection "db"
 121999 static void invalidateCachedKeyInfo(sqlite3 *db){
 122000   Db *pDb;                    /* A single database */
 122001   int iDb;                    /* The database index number */
 122002   HashElem *k;                /* For looping over tables in pDb */
 122003   Table *pTab;                /* A table in the database */
 122004   Index *pIdx;                /* Each index */
 122006   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
 122007     if( pDb->pBt==0 ) continue;
 122008     sqlite3BtreeEnter(pDb->pBt);
 122009     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
 122010       pTab = (Table*)sqliteHashData(k);
 122011       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 122012         if( pIdx->pKeyInfo && pIdx->pKeyInfo->db==db ){
 122013           sqlite3KeyInfoUnref(pIdx->pKeyInfo);
 122014           pIdx->pKeyInfo = 0;
 122018     sqlite3BtreeLeave(pDb->pBt);
 122023 ** Create a new collating function for database "db".  The name is zName
 122024 ** and the encoding is enc.
 122026 static int createCollation(
 122027   sqlite3* db,
 122028   const char *zName, 
 122029   u8 enc,
 122030   void* pCtx,
 122031   int(*xCompare)(void*,int,const void*,int,const void*),
 122032   void(*xDel)(void*)
 122034   CollSeq *pColl;
 122035   int enc2;
 122036   int nName = sqlite3Strlen30(zName);
 122038   assert( sqlite3_mutex_held(db->mutex) );
 122040   /* If SQLITE_UTF16 is specified as the encoding type, transform this
 122041   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
 122042   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
 122044   enc2 = enc;
 122045   testcase( enc2==SQLITE_UTF16 );
 122046   testcase( enc2==SQLITE_UTF16_ALIGNED );
 122047   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
 122048     enc2 = SQLITE_UTF16NATIVE;
 122050   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
 122051     return SQLITE_MISUSE_BKPT;
 122054   /* Check if this call is removing or replacing an existing collation 
 122055   ** sequence. If so, and there are active VMs, return busy. If there
 122056   ** are no active VMs, invalidate any pre-compiled statements.
 122058   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
 122059   if( pColl && pColl->xCmp ){
 122060     if( db->nVdbeActive ){
 122061       sqlite3Error(db, SQLITE_BUSY, 
 122062         "unable to delete/modify collation sequence due to active statements");
 122063       return SQLITE_BUSY;
 122065     sqlite3ExpirePreparedStatements(db);
 122066     invalidateCachedKeyInfo(db);
 122068     /* If collation sequence pColl was created directly by a call to
 122069     ** sqlite3_create_collation, and not generated by synthCollSeq(),
 122070     ** then any copies made by synthCollSeq() need to be invalidated.
 122071     ** Also, collation destructor - CollSeq.xDel() - function may need
 122072     ** to be called.
 122074     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
 122075       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 122076       int j;
 122077       for(j=0; j<3; j++){
 122078         CollSeq *p = &aColl[j];
 122079         if( p->enc==pColl->enc ){
 122080           if( p->xDel ){
 122081             p->xDel(p->pUser);
 122083           p->xCmp = 0;
 122089   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
 122090   if( pColl==0 ) return SQLITE_NOMEM;
 122091   pColl->xCmp = xCompare;
 122092   pColl->pUser = pCtx;
 122093   pColl->xDel = xDel;
 122094   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
 122095   sqlite3Error(db, SQLITE_OK, 0);
 122096   return SQLITE_OK;
 122101 ** This array defines hard upper bounds on limit values.  The
 122102 ** initializer must be kept in sync with the SQLITE_LIMIT_*
 122103 ** #defines in sqlite3.h.
 122105 static const int aHardLimit[] = {
 122106   SQLITE_MAX_LENGTH,
 122107   SQLITE_MAX_SQL_LENGTH,
 122108   SQLITE_MAX_COLUMN,
 122109   SQLITE_MAX_EXPR_DEPTH,
 122110   SQLITE_MAX_COMPOUND_SELECT,
 122111   SQLITE_MAX_VDBE_OP,
 122112   SQLITE_MAX_FUNCTION_ARG,
 122113   SQLITE_MAX_ATTACHED,
 122114   SQLITE_MAX_LIKE_PATTERN_LENGTH,
 122115   SQLITE_MAX_VARIABLE_NUMBER,
 122116   SQLITE_MAX_TRIGGER_DEPTH,
 122120 ** Make sure the hard limits are set to reasonable values
 122122 #if SQLITE_MAX_LENGTH<100
 122123 # error SQLITE_MAX_LENGTH must be at least 100
 122124 #endif
 122125 #if SQLITE_MAX_SQL_LENGTH<100
 122126 # error SQLITE_MAX_SQL_LENGTH must be at least 100
 122127 #endif
 122128 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
 122129 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
 122130 #endif
 122131 #if SQLITE_MAX_COMPOUND_SELECT<2
 122132 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
 122133 #endif
 122134 #if SQLITE_MAX_VDBE_OP<40
 122135 # error SQLITE_MAX_VDBE_OP must be at least 40
 122136 #endif
 122137 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
 122138 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
 122139 #endif
 122140 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
 122141 # error SQLITE_MAX_ATTACHED must be between 0 and 62
 122142 #endif
 122143 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
 122144 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
 122145 #endif
 122146 #if SQLITE_MAX_COLUMN>32767
 122147 # error SQLITE_MAX_COLUMN must not exceed 32767
 122148 #endif
 122149 #if SQLITE_MAX_TRIGGER_DEPTH<1
 122150 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
 122151 #endif
 122155 ** Change the value of a limit.  Report the old value.
 122156 ** If an invalid limit index is supplied, report -1.
 122157 ** Make no changes but still report the old value if the
 122158 ** new limit is negative.
 122160 ** A new lower limit does not shrink existing constructs.
 122161 ** It merely prevents new constructs that exceed the limit
 122162 ** from forming.
 122164 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
 122165   int oldLimit;
 122168   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
 122169   ** there is a hard upper bound set at compile-time by a C preprocessor
 122170   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
 122171   ** "_MAX_".)
 122173   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
 122174   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
 122175   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
 122176   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
 122177   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
 122178   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
 122179   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
 122180   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
 122181   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
 122182                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
 122183   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
 122184   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
 122185   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
 122188   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
 122189     return -1;
 122191   oldLimit = db->aLimit[limitId];
 122192   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
 122193     if( newLimit>aHardLimit[limitId] ){
 122194       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
 122196     db->aLimit[limitId] = newLimit;
 122198   return oldLimit;                     /* IMP: R-53341-35419 */
 122202 ** This function is used to parse both URIs and non-URI filenames passed by the
 122203 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
 122204 ** URIs specified as part of ATTACH statements.
 122206 ** The first argument to this function is the name of the VFS to use (or
 122207 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
 122208 ** query parameter. The second argument contains the URI (or non-URI filename)
 122209 ** itself. When this function is called the *pFlags variable should contain
 122210 ** the default flags to open the database handle with. The value stored in
 122211 ** *pFlags may be updated before returning if the URI filename contains 
 122212 ** "cache=xxx" or "mode=xxx" query parameters.
 122214 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
 122215 ** the VFS that should be used to open the database file. *pzFile is set to
 122216 ** point to a buffer containing the name of the file to open. It is the 
 122217 ** responsibility of the caller to eventually call sqlite3_free() to release
 122218 ** this buffer.
 122220 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
 122221 ** may be set to point to a buffer containing an English language error 
 122222 ** message. It is the responsibility of the caller to eventually release
 122223 ** this buffer by calling sqlite3_free().
 122225 SQLITE_PRIVATE int sqlite3ParseUri(
 122226   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
 122227   const char *zUri,               /* Nul-terminated URI to parse */
 122228   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
 122229   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
 122230   char **pzFile,                  /* OUT: Filename component of URI */
 122231   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
 122233   int rc = SQLITE_OK;
 122234   unsigned int flags = *pFlags;
 122235   const char *zVfs = zDefaultVfs;
 122236   char *zFile;
 122237   char c;
 122238   int nUri = sqlite3Strlen30(zUri);
 122240   assert( *pzErrMsg==0 );
 122242   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 
 122243    && nUri>=5 && memcmp(zUri, "file:", 5)==0 
 122245     char *zOpt;
 122246     int eState;                   /* Parser state when parsing URI */
 122247     int iIn;                      /* Input character index */
 122248     int iOut = 0;                 /* Output character index */
 122249     int nByte = nUri+2;           /* Bytes of space to allocate */
 122251     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
 122252     ** method that there may be extra parameters following the file-name.  */
 122253     flags |= SQLITE_OPEN_URI;
 122255     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
 122256     zFile = sqlite3_malloc(nByte);
 122257     if( !zFile ) return SQLITE_NOMEM;
 122259     iIn = 5;
 122260 #ifndef SQLITE_ALLOW_URI_AUTHORITY
 122261     /* Discard the scheme and authority segments of the URI. */
 122262     if( zUri[5]=='/' && zUri[6]=='/' ){
 122263       iIn = 7;
 122264       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
 122265       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
 122266         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 
 122267             iIn-7, &zUri[7]);
 122268         rc = SQLITE_ERROR;
 122269         goto parse_uri_out;
 122272 #endif
 122274     /* Copy the filename and any query parameters into the zFile buffer. 
 122275     ** Decode %HH escape codes along the way. 
 122277     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
 122278     ** on the parsing context. As follows:
 122280     **   0: Parsing file-name.
 122281     **   1: Parsing name section of a name=value query parameter.
 122282     **   2: Parsing value section of a name=value query parameter.
 122284     eState = 0;
 122285     while( (c = zUri[iIn])!=0 && c!='#' ){
 122286       iIn++;
 122287       if( c=='%' 
 122288        && sqlite3Isxdigit(zUri[iIn]) 
 122289        && sqlite3Isxdigit(zUri[iIn+1]) 
 122291         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
 122292         octet += sqlite3HexToInt(zUri[iIn++]);
 122294         assert( octet>=0 && octet<256 );
 122295         if( octet==0 ){
 122296           /* This branch is taken when "%00" appears within the URI. In this
 122297           ** case we ignore all text in the remainder of the path, name or
 122298           ** value currently being parsed. So ignore the current character
 122299           ** and skip to the next "?", "=" or "&", as appropriate. */
 122300           while( (c = zUri[iIn])!=0 && c!='#' 
 122301               && (eState!=0 || c!='?')
 122302               && (eState!=1 || (c!='=' && c!='&'))
 122303               && (eState!=2 || c!='&')
 122305             iIn++;
 122307           continue;
 122309         c = octet;
 122310       }else if( eState==1 && (c=='&' || c=='=') ){
 122311         if( zFile[iOut-1]==0 ){
 122312           /* An empty option name. Ignore this option altogether. */
 122313           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
 122314           continue;
 122316         if( c=='&' ){
 122317           zFile[iOut++] = '\0';
 122318         }else{
 122319           eState = 2;
 122321         c = 0;
 122322       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
 122323         c = 0;
 122324         eState = 1;
 122326       zFile[iOut++] = c;
 122328     if( eState==1 ) zFile[iOut++] = '\0';
 122329     zFile[iOut++] = '\0';
 122330     zFile[iOut++] = '\0';
 122332     /* Check if there were any options specified that should be interpreted 
 122333     ** here. Options that are interpreted here include "vfs" and those that
 122334     ** correspond to flags that may be passed to the sqlite3_open_v2()
 122335     ** method. */
 122336     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
 122337     while( zOpt[0] ){
 122338       int nOpt = sqlite3Strlen30(zOpt);
 122339       char *zVal = &zOpt[nOpt+1];
 122340       int nVal = sqlite3Strlen30(zVal);
 122342       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
 122343         zVfs = zVal;
 122344       }else{
 122345         struct OpenMode {
 122346           const char *z;
 122347           int mode;
 122348         } *aMode = 0;
 122349         char *zModeType = 0;
 122350         int mask = 0;
 122351         int limit = 0;
 122353         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
 122354           static struct OpenMode aCacheMode[] = {
 122355             { "shared",  SQLITE_OPEN_SHAREDCACHE },
 122356             { "private", SQLITE_OPEN_PRIVATECACHE },
 122357             { 0, 0 }
 122360           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
 122361           aMode = aCacheMode;
 122362           limit = mask;
 122363           zModeType = "cache";
 122365         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
 122366           static struct OpenMode aOpenMode[] = {
 122367             { "ro",  SQLITE_OPEN_READONLY },
 122368             { "rw",  SQLITE_OPEN_READWRITE }, 
 122369             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
 122370             { "memory", SQLITE_OPEN_MEMORY },
 122371             { 0, 0 }
 122374           mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
 122375                    | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
 122376           aMode = aOpenMode;
 122377           limit = mask & flags;
 122378           zModeType = "access";
 122381         if( aMode ){
 122382           int i;
 122383           int mode = 0;
 122384           for(i=0; aMode[i].z; i++){
 122385             const char *z = aMode[i].z;
 122386             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
 122387               mode = aMode[i].mode;
 122388               break;
 122391           if( mode==0 ){
 122392             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
 122393             rc = SQLITE_ERROR;
 122394             goto parse_uri_out;
 122396           if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
 122397             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
 122398                                         zModeType, zVal);
 122399             rc = SQLITE_PERM;
 122400             goto parse_uri_out;
 122402           flags = (flags & ~mask) | mode;
 122406       zOpt = &zVal[nVal+1];
 122409   }else{
 122410     zFile = sqlite3_malloc(nUri+2);
 122411     if( !zFile ) return SQLITE_NOMEM;
 122412     memcpy(zFile, zUri, nUri);
 122413     zFile[nUri] = '\0';
 122414     zFile[nUri+1] = '\0';
 122415     flags &= ~SQLITE_OPEN_URI;
 122418   *ppVfs = sqlite3_vfs_find(zVfs);
 122419   if( *ppVfs==0 ){
 122420     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
 122421     rc = SQLITE_ERROR;
 122423  parse_uri_out:
 122424   if( rc!=SQLITE_OK ){
 122425     sqlite3_free(zFile);
 122426     zFile = 0;
 122428   *pFlags = flags;
 122429   *pzFile = zFile;
 122430   return rc;
 122435 ** This routine does the work of opening a database on behalf of
 122436 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
 122437 ** is UTF-8 encoded.
 122439 static int openDatabase(
 122440   const char *zFilename, /* Database filename UTF-8 encoded */
 122441   sqlite3 **ppDb,        /* OUT: Returned database handle */
 122442   unsigned int flags,    /* Operational flags */
 122443   const char *zVfs       /* Name of the VFS to use */
 122445   sqlite3 *db;                    /* Store allocated handle here */
 122446   int rc;                         /* Return code */
 122447   int isThreadsafe;               /* True for threadsafe connections */
 122448   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
 122449   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
 122451   *ppDb = 0;
 122452 #ifndef SQLITE_OMIT_AUTOINIT
 122453   rc = sqlite3_initialize();
 122454   if( rc ) return rc;
 122455 #endif
 122457   /* Only allow sensible combinations of bits in the flags argument.  
 122458   ** Throw an error if any non-sense combination is used.  If we
 122459   ** do not block illegal combinations here, it could trigger
 122460   ** assert() statements in deeper layers.  Sensible combinations
 122461   ** are:
 122463   **  1:  SQLITE_OPEN_READONLY
 122464   **  2:  SQLITE_OPEN_READWRITE
 122465   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
 122467   assert( SQLITE_OPEN_READONLY  == 0x01 );
 122468   assert( SQLITE_OPEN_READWRITE == 0x02 );
 122469   assert( SQLITE_OPEN_CREATE    == 0x04 );
 122470   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
 122471   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
 122472   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
 122473   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
 122475   if( sqlite3GlobalConfig.bCoreMutex==0 ){
 122476     isThreadsafe = 0;
 122477   }else if( flags & SQLITE_OPEN_NOMUTEX ){
 122478     isThreadsafe = 0;
 122479   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
 122480     isThreadsafe = 1;
 122481   }else{
 122482     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
 122484   if( flags & SQLITE_OPEN_PRIVATECACHE ){
 122485     flags &= ~SQLITE_OPEN_SHAREDCACHE;
 122486   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
 122487     flags |= SQLITE_OPEN_SHAREDCACHE;
 122490   /* Remove harmful bits from the flags parameter
 122492   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
 122493   ** dealt with in the previous code block.  Besides these, the only
 122494   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
 122495   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
 122496   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
 122497   ** off all other flags.
 122499   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
 122500                SQLITE_OPEN_EXCLUSIVE |
 122501                SQLITE_OPEN_MAIN_DB |
 122502                SQLITE_OPEN_TEMP_DB | 
 122503                SQLITE_OPEN_TRANSIENT_DB | 
 122504                SQLITE_OPEN_MAIN_JOURNAL | 
 122505                SQLITE_OPEN_TEMP_JOURNAL | 
 122506                SQLITE_OPEN_SUBJOURNAL | 
 122507                SQLITE_OPEN_MASTER_JOURNAL |
 122508                SQLITE_OPEN_NOMUTEX |
 122509                SQLITE_OPEN_FULLMUTEX |
 122510                SQLITE_OPEN_WAL
 122513   /* Allocate the sqlite data structure */
 122514   db = sqlite3MallocZero( sizeof(sqlite3) );
 122515   if( db==0 ) goto opendb_out;
 122516   if( isThreadsafe ){
 122517     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
 122518     if( db->mutex==0 ){
 122519       sqlite3_free(db);
 122520       db = 0;
 122521       goto opendb_out;
 122524   sqlite3_mutex_enter(db->mutex);
 122525   db->errMask = 0xff;
 122526   db->nDb = 2;
 122527   db->magic = SQLITE_MAGIC_BUSY;
 122528   db->aDb = db->aDbStatic;
 122530   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
 122531   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
 122532   db->autoCommit = 1;
 122533   db->nextAutovac = -1;
 122534   db->szMmap = sqlite3GlobalConfig.szMmap;
 122535   db->nextPagesize = 0;
 122536   db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
 122537 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
 122538                  | SQLITE_AutoIndex
 122539 #endif
 122540 #if SQLITE_DEFAULT_FILE_FORMAT<4
 122541                  | SQLITE_LegacyFileFmt
 122542 #endif
 122543 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
 122544                  | SQLITE_LoadExtension
 122545 #endif
 122546 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
 122547                  | SQLITE_RecTriggers
 122548 #endif
 122549 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
 122550                  | SQLITE_ForeignKeys
 122551 #endif
 122553   sqlite3HashInit(&db->aCollSeq);
 122554 #ifndef SQLITE_OMIT_VIRTUALTABLE
 122555   sqlite3HashInit(&db->aModule);
 122556 #endif
 122558   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
 122559   ** and UTF-16, so add a version for each to avoid any unnecessary
 122560   ** conversions. The only error that can occur here is a malloc() failure.
 122562   createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
 122563   createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
 122564   createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
 122565   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
 122566   if( db->mallocFailed ){
 122567     goto opendb_out;
 122569   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
 122570   assert( db->pDfltColl!=0 );
 122572   /* Also add a UTF-8 case-insensitive collation sequence. */
 122573   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
 122575   /* Parse the filename/URI argument. */
 122576   db->openFlags = flags;
 122577   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
 122578   if( rc!=SQLITE_OK ){
 122579     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
 122580     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
 122581     sqlite3_free(zErrMsg);
 122582     goto opendb_out;
 122585   /* Open the backend database driver */
 122586   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
 122587                         flags | SQLITE_OPEN_MAIN_DB);
 122588   if( rc!=SQLITE_OK ){
 122589     if( rc==SQLITE_IOERR_NOMEM ){
 122590       rc = SQLITE_NOMEM;
 122592     sqlite3Error(db, rc, 0);
 122593     goto opendb_out;
 122595   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
 122596   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
 122599   /* The default safety_level for the main database is 'full'; for the temp
 122600   ** database it is 'NONE'. This matches the pager layer defaults.  
 122602   db->aDb[0].zName = "main";
 122603   db->aDb[0].safety_level = 3;
 122604   db->aDb[1].zName = "temp";
 122605   db->aDb[1].safety_level = 1;
 122607   db->magic = SQLITE_MAGIC_OPEN;
 122608   if( db->mallocFailed ){
 122609     goto opendb_out;
 122612   /* Register all built-in functions, but do not attempt to read the
 122613   ** database schema yet. This is delayed until the first time the database
 122614   ** is accessed.
 122616   sqlite3Error(db, SQLITE_OK, 0);
 122617   sqlite3RegisterBuiltinFunctions(db);
 122619   /* Load automatic extensions - extensions that have been registered
 122620   ** using the sqlite3_automatic_extension() API.
 122622   rc = sqlite3_errcode(db);
 122623   if( rc==SQLITE_OK ){
 122624     sqlite3AutoLoadExtensions(db);
 122625     rc = sqlite3_errcode(db);
 122626     if( rc!=SQLITE_OK ){
 122627       goto opendb_out;
 122631 #ifdef SQLITE_ENABLE_FTS1
 122632   if( !db->mallocFailed ){
 122633     extern int sqlite3Fts1Init(sqlite3*);
 122634     rc = sqlite3Fts1Init(db);
 122636 #endif
 122638 #ifdef SQLITE_ENABLE_FTS2
 122639   if( !db->mallocFailed && rc==SQLITE_OK ){
 122640     extern int sqlite3Fts2Init(sqlite3*);
 122641     rc = sqlite3Fts2Init(db);
 122643 #endif
 122645 #ifdef SQLITE_ENABLE_FTS3
 122646   if( !db->mallocFailed && rc==SQLITE_OK ){
 122647     rc = sqlite3Fts3Init(db);
 122649 #endif
 122651 #ifdef SQLITE_ENABLE_ICU
 122652   if( !db->mallocFailed && rc==SQLITE_OK ){
 122653     rc = sqlite3IcuInit(db);
 122655 #endif
 122657 #ifdef SQLITE_ENABLE_RTREE
 122658   if( !db->mallocFailed && rc==SQLITE_OK){
 122659     rc = sqlite3RtreeInit(db);
 122661 #endif
 122663   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
 122664   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
 122665   ** mode.  Doing nothing at all also makes NORMAL the default.
 122667 #ifdef SQLITE_DEFAULT_LOCKING_MODE
 122668   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
 122669   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
 122670                           SQLITE_DEFAULT_LOCKING_MODE);
 122671 #endif
 122673   if( rc ) sqlite3Error(db, rc, 0);
 122675   /* Enable the lookaside-malloc subsystem */
 122676   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
 122677                         sqlite3GlobalConfig.nLookaside);
 122679   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
 122681 opendb_out:
 122682   sqlite3_free(zOpen);
 122683   if( db ){
 122684     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
 122685     sqlite3_mutex_leave(db->mutex);
 122687   rc = sqlite3_errcode(db);
 122688   assert( db!=0 || rc==SQLITE_NOMEM );
 122689   if( rc==SQLITE_NOMEM ){
 122690     sqlite3_close(db);
 122691     db = 0;
 122692   }else if( rc!=SQLITE_OK ){
 122693     db->magic = SQLITE_MAGIC_SICK;
 122695   *ppDb = db;
 122696 #ifdef SQLITE_ENABLE_SQLLOG
 122697   if( sqlite3GlobalConfig.xSqllog ){
 122698     /* Opening a db handle. Fourth parameter is passed 0. */
 122699     void *pArg = sqlite3GlobalConfig.pSqllogArg;
 122700     sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
 122702 #endif
 122703   return sqlite3ApiExit(0, rc);
 122707 ** Open a new database handle.
 122709 SQLITE_API int sqlite3_open(
 122710   const char *zFilename, 
 122711   sqlite3 **ppDb 
 122713   return openDatabase(zFilename, ppDb,
 122714                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
 122716 SQLITE_API int sqlite3_open_v2(
 122717   const char *filename,   /* Database filename (UTF-8) */
 122718   sqlite3 **ppDb,         /* OUT: SQLite db handle */
 122719   int flags,              /* Flags */
 122720   const char *zVfs        /* Name of VFS module to use */
 122722   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
 122725 #ifndef SQLITE_OMIT_UTF16
 122727 ** Open a new database handle.
 122729 SQLITE_API int sqlite3_open16(
 122730   const void *zFilename, 
 122731   sqlite3 **ppDb
 122733   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
 122734   sqlite3_value *pVal;
 122735   int rc;
 122737   assert( zFilename );
 122738   assert( ppDb );
 122739   *ppDb = 0;
 122740 #ifndef SQLITE_OMIT_AUTOINIT
 122741   rc = sqlite3_initialize();
 122742   if( rc ) return rc;
 122743 #endif
 122744   pVal = sqlite3ValueNew(0);
 122745   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
 122746   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
 122747   if( zFilename8 ){
 122748     rc = openDatabase(zFilename8, ppDb,
 122749                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
 122750     assert( *ppDb || rc==SQLITE_NOMEM );
 122751     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
 122752       ENC(*ppDb) = SQLITE_UTF16NATIVE;
 122754   }else{
 122755     rc = SQLITE_NOMEM;
 122757   sqlite3ValueFree(pVal);
 122759   return sqlite3ApiExit(0, rc);
 122761 #endif /* SQLITE_OMIT_UTF16 */
 122764 ** Register a new collation sequence with the database handle db.
 122766 SQLITE_API int sqlite3_create_collation(
 122767   sqlite3* db, 
 122768   const char *zName, 
 122769   int enc, 
 122770   void* pCtx,
 122771   int(*xCompare)(void*,int,const void*,int,const void*)
 122773   int rc;
 122774   sqlite3_mutex_enter(db->mutex);
 122775   assert( !db->mallocFailed );
 122776   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
 122777   rc = sqlite3ApiExit(db, rc);
 122778   sqlite3_mutex_leave(db->mutex);
 122779   return rc;
 122783 ** Register a new collation sequence with the database handle db.
 122785 SQLITE_API int sqlite3_create_collation_v2(
 122786   sqlite3* db, 
 122787   const char *zName, 
 122788   int enc, 
 122789   void* pCtx,
 122790   int(*xCompare)(void*,int,const void*,int,const void*),
 122791   void(*xDel)(void*)
 122793   int rc;
 122794   sqlite3_mutex_enter(db->mutex);
 122795   assert( !db->mallocFailed );
 122796   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
 122797   rc = sqlite3ApiExit(db, rc);
 122798   sqlite3_mutex_leave(db->mutex);
 122799   return rc;
 122802 #ifndef SQLITE_OMIT_UTF16
 122804 ** Register a new collation sequence with the database handle db.
 122806 SQLITE_API int sqlite3_create_collation16(
 122807   sqlite3* db, 
 122808   const void *zName,
 122809   int enc, 
 122810   void* pCtx,
 122811   int(*xCompare)(void*,int,const void*,int,const void*)
 122813   int rc = SQLITE_OK;
 122814   char *zName8;
 122815   sqlite3_mutex_enter(db->mutex);
 122816   assert( !db->mallocFailed );
 122817   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
 122818   if( zName8 ){
 122819     rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
 122820     sqlite3DbFree(db, zName8);
 122822   rc = sqlite3ApiExit(db, rc);
 122823   sqlite3_mutex_leave(db->mutex);
 122824   return rc;
 122826 #endif /* SQLITE_OMIT_UTF16 */
 122829 ** Register a collation sequence factory callback with the database handle
 122830 ** db. Replace any previously installed collation sequence factory.
 122832 SQLITE_API int sqlite3_collation_needed(
 122833   sqlite3 *db, 
 122834   void *pCollNeededArg, 
 122835   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
 122837   sqlite3_mutex_enter(db->mutex);
 122838   db->xCollNeeded = xCollNeeded;
 122839   db->xCollNeeded16 = 0;
 122840   db->pCollNeededArg = pCollNeededArg;
 122841   sqlite3_mutex_leave(db->mutex);
 122842   return SQLITE_OK;
 122845 #ifndef SQLITE_OMIT_UTF16
 122847 ** Register a collation sequence factory callback with the database handle
 122848 ** db. Replace any previously installed collation sequence factory.
 122850 SQLITE_API int sqlite3_collation_needed16(
 122851   sqlite3 *db, 
 122852   void *pCollNeededArg, 
 122853   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
 122855   sqlite3_mutex_enter(db->mutex);
 122856   db->xCollNeeded = 0;
 122857   db->xCollNeeded16 = xCollNeeded16;
 122858   db->pCollNeededArg = pCollNeededArg;
 122859   sqlite3_mutex_leave(db->mutex);
 122860   return SQLITE_OK;
 122862 #endif /* SQLITE_OMIT_UTF16 */
 122864 #ifndef SQLITE_OMIT_DEPRECATED
 122866 ** This function is now an anachronism. It used to be used to recover from a
 122867 ** malloc() failure, but SQLite now does this automatically.
 122869 SQLITE_API int sqlite3_global_recover(void){
 122870   return SQLITE_OK;
 122872 #endif
 122875 ** Test to see whether or not the database connection is in autocommit
 122876 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
 122877 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
 122878 ** by the next COMMIT or ROLLBACK.
 122880 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
 122881   return db->autoCommit;
 122885 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
 122886 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
 122887 ** constants.  They server two purposes:
 122889 **   1.  Serve as a convenient place to set a breakpoint in a debugger
 122890 **       to detect when version error conditions occurs.
 122892 **   2.  Invoke sqlite3_log() to provide the source code location where
 122893 **       a low-level error is first detected.
 122895 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
 122896   testcase( sqlite3GlobalConfig.xLog!=0 );
 122897   sqlite3_log(SQLITE_CORRUPT,
 122898               "database corruption at line %d of [%.10s]",
 122899               lineno, 20+sqlite3_sourceid());
 122900   return SQLITE_CORRUPT;
 122902 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
 122903   testcase( sqlite3GlobalConfig.xLog!=0 );
 122904   sqlite3_log(SQLITE_MISUSE, 
 122905               "misuse at line %d of [%.10s]",
 122906               lineno, 20+sqlite3_sourceid());
 122907   return SQLITE_MISUSE;
 122909 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
 122910   testcase( sqlite3GlobalConfig.xLog!=0 );
 122911   sqlite3_log(SQLITE_CANTOPEN, 
 122912               "cannot open file at line %d of [%.10s]",
 122913               lineno, 20+sqlite3_sourceid());
 122914   return SQLITE_CANTOPEN;
 122918 #ifndef SQLITE_OMIT_DEPRECATED
 122920 ** This is a convenience routine that makes sure that all thread-specific
 122921 ** data for this thread has been deallocated.
 122923 ** SQLite no longer uses thread-specific data so this routine is now a
 122924 ** no-op.  It is retained for historical compatibility.
 122926 SQLITE_API void sqlite3_thread_cleanup(void){
 122928 #endif
 122931 ** Return meta information about a specific column of a database table.
 122932 ** See comment in sqlite3.h (sqlite.h.in) for details.
 122934 #ifdef SQLITE_ENABLE_COLUMN_METADATA
 122935 SQLITE_API int sqlite3_table_column_metadata(
 122936   sqlite3 *db,                /* Connection handle */
 122937   const char *zDbName,        /* Database name or NULL */
 122938   const char *zTableName,     /* Table name */
 122939   const char *zColumnName,    /* Column name */
 122940   char const **pzDataType,    /* OUTPUT: Declared data type */
 122941   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
 122942   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
 122943   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
 122944   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
 122946   int rc;
 122947   char *zErrMsg = 0;
 122948   Table *pTab = 0;
 122949   Column *pCol = 0;
 122950   int iCol;
 122952   char const *zDataType = 0;
 122953   char const *zCollSeq = 0;
 122954   int notnull = 0;
 122955   int primarykey = 0;
 122956   int autoinc = 0;
 122958   /* Ensure the database schema has been loaded */
 122959   sqlite3_mutex_enter(db->mutex);
 122960   sqlite3BtreeEnterAll(db);
 122961   rc = sqlite3Init(db, &zErrMsg);
 122962   if( SQLITE_OK!=rc ){
 122963     goto error_out;
 122966   /* Locate the table in question */
 122967   pTab = sqlite3FindTable(db, zTableName, zDbName);
 122968   if( !pTab || pTab->pSelect ){
 122969     pTab = 0;
 122970     goto error_out;
 122973   /* Find the column for which info is requested */
 122974   if( sqlite3IsRowid(zColumnName) ){
 122975     iCol = pTab->iPKey;
 122976     if( iCol>=0 ){
 122977       pCol = &pTab->aCol[iCol];
 122979   }else{
 122980     for(iCol=0; iCol<pTab->nCol; iCol++){
 122981       pCol = &pTab->aCol[iCol];
 122982       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
 122983         break;
 122986     if( iCol==pTab->nCol ){
 122987       pTab = 0;
 122988       goto error_out;
 122992   /* The following block stores the meta information that will be returned
 122993   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
 122994   ** and autoinc. At this point there are two possibilities:
 122996   **     1. The specified column name was rowid", "oid" or "_rowid_" 
 122997   **        and there is no explicitly declared IPK column. 
 122999   **     2. The table is not a view and the column name identified an 
 123000   **        explicitly declared column. Copy meta information from *pCol.
 123002   if( pCol ){
 123003     zDataType = pCol->zType;
 123004     zCollSeq = pCol->zColl;
 123005     notnull = pCol->notNull!=0;
 123006     primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
 123007     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
 123008   }else{
 123009     zDataType = "INTEGER";
 123010     primarykey = 1;
 123012   if( !zCollSeq ){
 123013     zCollSeq = "BINARY";
 123016 error_out:
 123017   sqlite3BtreeLeaveAll(db);
 123019   /* Whether the function call succeeded or failed, set the output parameters
 123020   ** to whatever their local counterparts contain. If an error did occur,
 123021   ** this has the effect of zeroing all output parameters.
 123023   if( pzDataType ) *pzDataType = zDataType;
 123024   if( pzCollSeq ) *pzCollSeq = zCollSeq;
 123025   if( pNotNull ) *pNotNull = notnull;
 123026   if( pPrimaryKey ) *pPrimaryKey = primarykey;
 123027   if( pAutoinc ) *pAutoinc = autoinc;
 123029   if( SQLITE_OK==rc && !pTab ){
 123030     sqlite3DbFree(db, zErrMsg);
 123031     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
 123032         zColumnName);
 123033     rc = SQLITE_ERROR;
 123035   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
 123036   sqlite3DbFree(db, zErrMsg);
 123037   rc = sqlite3ApiExit(db, rc);
 123038   sqlite3_mutex_leave(db->mutex);
 123039   return rc;
 123041 #endif
 123044 ** Sleep for a little while.  Return the amount of time slept.
 123046 SQLITE_API int sqlite3_sleep(int ms){
 123047   sqlite3_vfs *pVfs;
 123048   int rc;
 123049   pVfs = sqlite3_vfs_find(0);
 123050   if( pVfs==0 ) return 0;
 123052   /* This function works in milliseconds, but the underlying OsSleep() 
 123053   ** API uses microseconds. Hence the 1000's.
 123055   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
 123056   return rc;
 123060 ** Enable or disable the extended result codes.
 123062 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
 123063   sqlite3_mutex_enter(db->mutex);
 123064   db->errMask = onoff ? 0xffffffff : 0xff;
 123065   sqlite3_mutex_leave(db->mutex);
 123066   return SQLITE_OK;
 123070 ** Invoke the xFileControl method on a particular database.
 123072 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
 123073   int rc = SQLITE_ERROR;
 123074   Btree *pBtree;
 123076   sqlite3_mutex_enter(db->mutex);
 123077   pBtree = sqlite3DbNameToBtree(db, zDbName);
 123078   if( pBtree ){
 123079     Pager *pPager;
 123080     sqlite3_file *fd;
 123081     sqlite3BtreeEnter(pBtree);
 123082     pPager = sqlite3BtreePager(pBtree);
 123083     assert( pPager!=0 );
 123084     fd = sqlite3PagerFile(pPager);
 123085     assert( fd!=0 );
 123086     if( op==SQLITE_FCNTL_FILE_POINTER ){
 123087       *(sqlite3_file**)pArg = fd;
 123088       rc = SQLITE_OK;
 123089     }else if( fd->pMethods ){
 123090       rc = sqlite3OsFileControl(fd, op, pArg);
 123091     }else{
 123092       rc = SQLITE_NOTFOUND;
 123094     sqlite3BtreeLeave(pBtree);
 123096   sqlite3_mutex_leave(db->mutex);
 123097   return rc;   
 123101 ** Interface to the testing logic.
 123103 SQLITE_API int sqlite3_test_control(int op, ...){
 123104   int rc = 0;
 123105 #ifndef SQLITE_OMIT_BUILTIN_TEST
 123106   va_list ap;
 123107   va_start(ap, op);
 123108   switch( op ){
 123111     ** Save the current state of the PRNG.
 123113     case SQLITE_TESTCTRL_PRNG_SAVE: {
 123114       sqlite3PrngSaveState();
 123115       break;
 123119     ** Restore the state of the PRNG to the last state saved using
 123120     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
 123121     ** this verb acts like PRNG_RESET.
 123123     case SQLITE_TESTCTRL_PRNG_RESTORE: {
 123124       sqlite3PrngRestoreState();
 123125       break;
 123129     ** Reset the PRNG back to its uninitialized state.  The next call
 123130     ** to sqlite3_randomness() will reseed the PRNG using a single call
 123131     ** to the xRandomness method of the default VFS.
 123133     case SQLITE_TESTCTRL_PRNG_RESET: {
 123134       sqlite3_randomness(0,0);
 123135       break;
 123139     **  sqlite3_test_control(BITVEC_TEST, size, program)
 123141     ** Run a test against a Bitvec object of size.  The program argument
 123142     ** is an array of integers that defines the test.  Return -1 on a
 123143     ** memory allocation error, 0 on success, or non-zero for an error.
 123144     ** See the sqlite3BitvecBuiltinTest() for additional information.
 123146     case SQLITE_TESTCTRL_BITVEC_TEST: {
 123147       int sz = va_arg(ap, int);
 123148       int *aProg = va_arg(ap, int*);
 123149       rc = sqlite3BitvecBuiltinTest(sz, aProg);
 123150       break;
 123154     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
 123156     ** Register hooks to call to indicate which malloc() failures 
 123157     ** are benign.
 123159     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
 123160       typedef void (*void_function)(void);
 123161       void_function xBenignBegin;
 123162       void_function xBenignEnd;
 123163       xBenignBegin = va_arg(ap, void_function);
 123164       xBenignEnd = va_arg(ap, void_function);
 123165       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
 123166       break;
 123170     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
 123172     ** Set the PENDING byte to the value in the argument, if X>0.
 123173     ** Make no changes if X==0.  Return the value of the pending byte
 123174     ** as it existing before this routine was called.
 123176     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
 123177     ** an incompatible database file format.  Changing the PENDING byte
 123178     ** while any database connection is open results in undefined and
 123179     ** dileterious behavior.
 123181     case SQLITE_TESTCTRL_PENDING_BYTE: {
 123182       rc = PENDING_BYTE;
 123183 #ifndef SQLITE_OMIT_WSD
 123185         unsigned int newVal = va_arg(ap, unsigned int);
 123186         if( newVal ) sqlite3PendingByte = newVal;
 123188 #endif
 123189       break;
 123193     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
 123195     ** This action provides a run-time test to see whether or not
 123196     ** assert() was enabled at compile-time.  If X is true and assert()
 123197     ** is enabled, then the return value is true.  If X is true and
 123198     ** assert() is disabled, then the return value is zero.  If X is
 123199     ** false and assert() is enabled, then the assertion fires and the
 123200     ** process aborts.  If X is false and assert() is disabled, then the
 123201     ** return value is zero.
 123203     case SQLITE_TESTCTRL_ASSERT: {
 123204       volatile int x = 0;
 123205       assert( (x = va_arg(ap,int))!=0 );
 123206       rc = x;
 123207       break;
 123212     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
 123214     ** This action provides a run-time test to see how the ALWAYS and
 123215     ** NEVER macros were defined at compile-time.
 123217     ** The return value is ALWAYS(X).  
 123219     ** The recommended test is X==2.  If the return value is 2, that means
 123220     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
 123221     ** default setting.  If the return value is 1, then ALWAYS() is either
 123222     ** hard-coded to true or else it asserts if its argument is false.
 123223     ** The first behavior (hard-coded to true) is the case if
 123224     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
 123225     ** behavior (assert if the argument to ALWAYS() is false) is the case if
 123226     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
 123228     ** The run-time test procedure might look something like this:
 123230     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
 123231     **      // ALWAYS() and NEVER() are no-op pass-through macros
 123232     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
 123233     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
 123234     **    }else{
 123235     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
 123236     **    }
 123238     case SQLITE_TESTCTRL_ALWAYS: {
 123239       int x = va_arg(ap,int);
 123240       rc = ALWAYS(x);
 123241       break;
 123244     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
 123246     ** Set the nReserve size to N for the main database on the database
 123247     ** connection db.
 123249     case SQLITE_TESTCTRL_RESERVE: {
 123250       sqlite3 *db = va_arg(ap, sqlite3*);
 123251       int x = va_arg(ap,int);
 123252       sqlite3_mutex_enter(db->mutex);
 123253       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
 123254       sqlite3_mutex_leave(db->mutex);
 123255       break;
 123258     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
 123260     ** Enable or disable various optimizations for testing purposes.  The 
 123261     ** argument N is a bitmask of optimizations to be disabled.  For normal
 123262     ** operation N should be 0.  The idea is that a test program (like the
 123263     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
 123264     ** with various optimizations disabled to verify that the same answer
 123265     ** is obtained in every case.
 123267     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
 123268       sqlite3 *db = va_arg(ap, sqlite3*);
 123269       db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
 123270       break;
 123273 #ifdef SQLITE_N_KEYWORD
 123274     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
 123276     ** If zWord is a keyword recognized by the parser, then return the
 123277     ** number of keywords.  Or if zWord is not a keyword, return 0.
 123279     ** This test feature is only available in the amalgamation since
 123280     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
 123281     ** is built using separate source files.
 123283     case SQLITE_TESTCTRL_ISKEYWORD: {
 123284       const char *zWord = va_arg(ap, const char*);
 123285       int n = sqlite3Strlen30(zWord);
 123286       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
 123287       break;
 123289 #endif 
 123291     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
 123293     ** Pass pFree into sqlite3ScratchFree(). 
 123294     ** If sz>0 then allocate a scratch buffer into pNew.  
 123296     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
 123297       void *pFree, **ppNew;
 123298       int sz;
 123299       sz = va_arg(ap, int);
 123300       ppNew = va_arg(ap, void**);
 123301       pFree = va_arg(ap, void*);
 123302       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
 123303       sqlite3ScratchFree(pFree);
 123304       break;
 123307     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
 123309     ** If parameter onoff is non-zero, configure the wrappers so that all
 123310     ** subsequent calls to localtime() and variants fail. If onoff is zero,
 123311     ** undo this setting.
 123313     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
 123314       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
 123315       break;
 123318 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
 123319     /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
 123320     **                        sqlite3_stmt*,const char**);
 123322     ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
 123323     ** a string that describes the optimized parse tree.  This test-control
 123324     ** returns a pointer to that string.
 123326     case SQLITE_TESTCTRL_EXPLAIN_STMT: {
 123327       sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
 123328       const char **pzRet = va_arg(ap, const char**);
 123329       *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
 123330       break;
 123332 #endif
 123334     /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
 123336     ** Set or clear a flag that indicates that the database file is always well-
 123337     ** formed and never corrupt.  This flag is clear by default, indicating that
 123338     ** database files might have arbitrary corruption.  Setting the flag during
 123339     ** testing causes certain assert() statements in the code to be activated
 123340     ** that demonstrat invariants on well-formed database files.
 123342     case SQLITE_TESTCTRL_NEVER_CORRUPT: {
 123343       sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
 123344       break;
 123348     /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
 123350     ** Set the VDBE coverage callback function to xCallback with context 
 123351     ** pointer ptr.
 123353     case SQLITE_TESTCTRL_VDBE_COVERAGE: {
 123354 #ifdef SQLITE_VDBE_COVERAGE
 123355       typedef void (*branch_callback)(void*,int,u8,u8);
 123356       sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
 123357       sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
 123358 #endif
 123359       break;
 123363   va_end(ap);
 123364 #endif /* SQLITE_OMIT_BUILTIN_TEST */
 123365   return rc;
 123369 ** This is a utility routine, useful to VFS implementations, that checks
 123370 ** to see if a database file was a URI that contained a specific query 
 123371 ** parameter, and if so obtains the value of the query parameter.
 123373 ** The zFilename argument is the filename pointer passed into the xOpen()
 123374 ** method of a VFS implementation.  The zParam argument is the name of the
 123375 ** query parameter we seek.  This routine returns the value of the zParam
 123376 ** parameter if it exists.  If the parameter does not exist, this routine
 123377 ** returns a NULL pointer.
 123379 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
 123380   if( zFilename==0 ) return 0;
 123381   zFilename += sqlite3Strlen30(zFilename) + 1;
 123382   while( zFilename[0] ){
 123383     int x = strcmp(zFilename, zParam);
 123384     zFilename += sqlite3Strlen30(zFilename) + 1;
 123385     if( x==0 ) return zFilename;
 123386     zFilename += sqlite3Strlen30(zFilename) + 1;
 123388   return 0;
 123392 ** Return a boolean value for a query parameter.
 123394 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
 123395   const char *z = sqlite3_uri_parameter(zFilename, zParam);
 123396   bDflt = bDflt!=0;
 123397   return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
 123401 ** Return a 64-bit integer value for a query parameter.
 123403 SQLITE_API sqlite3_int64 sqlite3_uri_int64(
 123404   const char *zFilename,    /* Filename as passed to xOpen */
 123405   const char *zParam,       /* URI parameter sought */
 123406   sqlite3_int64 bDflt       /* return if parameter is missing */
 123408   const char *z = sqlite3_uri_parameter(zFilename, zParam);
 123409   sqlite3_int64 v;
 123410   if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
 123411     bDflt = v;
 123413   return bDflt;
 123417 ** Return the Btree pointer identified by zDbName.  Return NULL if not found.
 123419 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
 123420   int i;
 123421   for(i=0; i<db->nDb; i++){
 123422     if( db->aDb[i].pBt
 123423      && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
 123425       return db->aDb[i].pBt;
 123428   return 0;
 123432 ** Return the filename of the database associated with a database
 123433 ** connection.
 123435 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
 123436   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
 123437   return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
 123441 ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
 123442 ** no such database exists.
 123444 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
 123445   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
 123446   return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
 123449 /************** End of main.c ************************************************/
 123450 /************** Begin file notify.c ******************************************/
 123452 ** 2009 March 3
 123454 ** The author disclaims copyright to this source code.  In place of
 123455 ** a legal notice, here is a blessing:
 123457 **    May you do good and not evil.
 123458 **    May you find forgiveness for yourself and forgive others.
 123459 **    May you share freely, never taking more than you give.
 123461 *************************************************************************
 123463 ** This file contains the implementation of the sqlite3_unlock_notify()
 123464 ** API method and its associated functionality.
 123467 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
 123468 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
 123471 ** Public interfaces:
 123473 **   sqlite3ConnectionBlocked()
 123474 **   sqlite3ConnectionUnlocked()
 123475 **   sqlite3ConnectionClosed()
 123476 **   sqlite3_unlock_notify()
 123479 #define assertMutexHeld() \
 123480   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
 123483 ** Head of a linked list of all sqlite3 objects created by this process
 123484 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
 123485 ** is not NULL. This variable may only accessed while the STATIC_MASTER
 123486 ** mutex is held.
 123488 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
 123490 #ifndef NDEBUG
 123492 ** This function is a complex assert() that verifies the following 
 123493 ** properties of the blocked connections list:
 123495 **   1) Each entry in the list has a non-NULL value for either 
 123496 **      pUnlockConnection or pBlockingConnection, or both.
 123498 **   2) All entries in the list that share a common value for 
 123499 **      xUnlockNotify are grouped together.
 123501 **   3) If the argument db is not NULL, then none of the entries in the
 123502 **      blocked connections list have pUnlockConnection or pBlockingConnection
 123503 **      set to db. This is used when closing connection db.
 123505 static void checkListProperties(sqlite3 *db){
 123506   sqlite3 *p;
 123507   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
 123508     int seen = 0;
 123509     sqlite3 *p2;
 123511     /* Verify property (1) */
 123512     assert( p->pUnlockConnection || p->pBlockingConnection );
 123514     /* Verify property (2) */
 123515     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
 123516       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
 123517       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
 123518       assert( db==0 || p->pUnlockConnection!=db );
 123519       assert( db==0 || p->pBlockingConnection!=db );
 123523 #else
 123524 # define checkListProperties(x)
 123525 #endif
 123528 ** Remove connection db from the blocked connections list. If connection
 123529 ** db is not currently a part of the list, this function is a no-op.
 123531 static void removeFromBlockedList(sqlite3 *db){
 123532   sqlite3 **pp;
 123533   assertMutexHeld();
 123534   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
 123535     if( *pp==db ){
 123536       *pp = (*pp)->pNextBlocked;
 123537       break;
 123543 ** Add connection db to the blocked connections list. It is assumed
 123544 ** that it is not already a part of the list.
 123546 static void addToBlockedList(sqlite3 *db){
 123547   sqlite3 **pp;
 123548   assertMutexHeld();
 123549   for(
 123550     pp=&sqlite3BlockedList; 
 123551     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
 123552     pp=&(*pp)->pNextBlocked
 123554   db->pNextBlocked = *pp;
 123555   *pp = db;
 123559 ** Obtain the STATIC_MASTER mutex.
 123561 static void enterMutex(void){
 123562   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 123563   checkListProperties(0);
 123567 ** Release the STATIC_MASTER mutex.
 123569 static void leaveMutex(void){
 123570   assertMutexHeld();
 123571   checkListProperties(0);
 123572   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 123576 ** Register an unlock-notify callback.
 123578 ** This is called after connection "db" has attempted some operation
 123579 ** but has received an SQLITE_LOCKED error because another connection
 123580 ** (call it pOther) in the same process was busy using the same shared
 123581 ** cache.  pOther is found by looking at db->pBlockingConnection.
 123583 ** If there is no blocking connection, the callback is invoked immediately,
 123584 ** before this routine returns.
 123586 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
 123587 ** a deadlock.
 123589 ** Otherwise, make arrangements to invoke xNotify when pOther drops
 123590 ** its locks.
 123592 ** Each call to this routine overrides any prior callbacks registered
 123593 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
 123594 ** cancelled.
 123596 SQLITE_API int sqlite3_unlock_notify(
 123597   sqlite3 *db,
 123598   void (*xNotify)(void **, int),
 123599   void *pArg
 123601   int rc = SQLITE_OK;
 123603   sqlite3_mutex_enter(db->mutex);
 123604   enterMutex();
 123606   if( xNotify==0 ){
 123607     removeFromBlockedList(db);
 123608     db->pBlockingConnection = 0;
 123609     db->pUnlockConnection = 0;
 123610     db->xUnlockNotify = 0;
 123611     db->pUnlockArg = 0;
 123612   }else if( 0==db->pBlockingConnection ){
 123613     /* The blocking transaction has been concluded. Or there never was a 
 123614     ** blocking transaction. In either case, invoke the notify callback
 123615     ** immediately. 
 123617     xNotify(&pArg, 1);
 123618   }else{
 123619     sqlite3 *p;
 123621     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
 123622     if( p ){
 123623       rc = SQLITE_LOCKED;              /* Deadlock detected. */
 123624     }else{
 123625       db->pUnlockConnection = db->pBlockingConnection;
 123626       db->xUnlockNotify = xNotify;
 123627       db->pUnlockArg = pArg;
 123628       removeFromBlockedList(db);
 123629       addToBlockedList(db);
 123633   leaveMutex();
 123634   assert( !db->mallocFailed );
 123635   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
 123636   sqlite3_mutex_leave(db->mutex);
 123637   return rc;
 123641 ** This function is called while stepping or preparing a statement 
 123642 ** associated with connection db. The operation will return SQLITE_LOCKED
 123643 ** to the user because it requires a lock that will not be available
 123644 ** until connection pBlocker concludes its current transaction.
 123646 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
 123647   enterMutex();
 123648   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
 123649     addToBlockedList(db);
 123651   db->pBlockingConnection = pBlocker;
 123652   leaveMutex();
 123656 ** This function is called when
 123657 ** the transaction opened by database db has just finished. Locks held 
 123658 ** by database connection db have been released.
 123660 ** This function loops through each entry in the blocked connections
 123661 ** list and does the following:
 123663 **   1) If the sqlite3.pBlockingConnection member of a list entry is
 123664 **      set to db, then set pBlockingConnection=0.
 123666 **   2) If the sqlite3.pUnlockConnection member of a list entry is
 123667 **      set to db, then invoke the configured unlock-notify callback and
 123668 **      set pUnlockConnection=0.
 123670 **   3) If the two steps above mean that pBlockingConnection==0 and
 123671 **      pUnlockConnection==0, remove the entry from the blocked connections
 123672 **      list.
 123674 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
 123675   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
 123676   int nArg = 0;                            /* Number of entries in aArg[] */
 123677   sqlite3 **pp;                            /* Iterator variable */
 123678   void **aArg;               /* Arguments to the unlock callback */
 123679   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
 123680   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
 123682   aArg = aStatic;
 123683   enterMutex();         /* Enter STATIC_MASTER mutex */
 123685   /* This loop runs once for each entry in the blocked-connections list. */
 123686   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
 123687     sqlite3 *p = *pp;
 123689     /* Step 1. */
 123690     if( p->pBlockingConnection==db ){
 123691       p->pBlockingConnection = 0;
 123694     /* Step 2. */
 123695     if( p->pUnlockConnection==db ){
 123696       assert( p->xUnlockNotify );
 123697       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
 123698         xUnlockNotify(aArg, nArg);
 123699         nArg = 0;
 123702       sqlite3BeginBenignMalloc();
 123703       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
 123704       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
 123705       if( (!aDyn && nArg==(int)ArraySize(aStatic))
 123706        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
 123708         /* The aArg[] array needs to grow. */
 123709         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
 123710         if( pNew ){
 123711           memcpy(pNew, aArg, nArg*sizeof(void *));
 123712           sqlite3_free(aDyn);
 123713           aDyn = aArg = pNew;
 123714         }else{
 123715           /* This occurs when the array of context pointers that need to
 123716           ** be passed to the unlock-notify callback is larger than the
 123717           ** aStatic[] array allocated on the stack and the attempt to 
 123718           ** allocate a larger array from the heap has failed.
 123720           ** This is a difficult situation to handle. Returning an error
 123721           ** code to the caller is insufficient, as even if an error code
 123722           ** is returned the transaction on connection db will still be
 123723           ** closed and the unlock-notify callbacks on blocked connections
 123724           ** will go unissued. This might cause the application to wait
 123725           ** indefinitely for an unlock-notify callback that will never 
 123726           ** arrive.
 123728           ** Instead, invoke the unlock-notify callback with the context
 123729           ** array already accumulated. We can then clear the array and
 123730           ** begin accumulating any further context pointers without 
 123731           ** requiring any dynamic allocation. This is sub-optimal because
 123732           ** it means that instead of one callback with a large array of
 123733           ** context pointers the application will receive two or more
 123734           ** callbacks with smaller arrays of context pointers, which will
 123735           ** reduce the applications ability to prioritize multiple 
 123736           ** connections. But it is the best that can be done under the
 123737           ** circumstances.
 123739           xUnlockNotify(aArg, nArg);
 123740           nArg = 0;
 123743       sqlite3EndBenignMalloc();
 123745       aArg[nArg++] = p->pUnlockArg;
 123746       xUnlockNotify = p->xUnlockNotify;
 123747       p->pUnlockConnection = 0;
 123748       p->xUnlockNotify = 0;
 123749       p->pUnlockArg = 0;
 123752     /* Step 3. */
 123753     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
 123754       /* Remove connection p from the blocked connections list. */
 123755       *pp = p->pNextBlocked;
 123756       p->pNextBlocked = 0;
 123757     }else{
 123758       pp = &p->pNextBlocked;
 123762   if( nArg!=0 ){
 123763     xUnlockNotify(aArg, nArg);
 123765   sqlite3_free(aDyn);
 123766   leaveMutex();         /* Leave STATIC_MASTER mutex */
 123770 ** This is called when the database connection passed as an argument is 
 123771 ** being closed. The connection is removed from the blocked list.
 123773 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
 123774   sqlite3ConnectionUnlocked(db);
 123775   enterMutex();
 123776   removeFromBlockedList(db);
 123777   checkListProperties(db);
 123778   leaveMutex();
 123780 #endif
 123782 /************** End of notify.c **********************************************/
 123783 /************** Begin file fts3.c ********************************************/
 123785 ** 2006 Oct 10
 123787 ** The author disclaims copyright to this source code.  In place of
 123788 ** a legal notice, here is a blessing:
 123790 **    May you do good and not evil.
 123791 **    May you find forgiveness for yourself and forgive others.
 123792 **    May you share freely, never taking more than you give.
 123794 ******************************************************************************
 123796 ** This is an SQLite module implementing full-text search.
 123800 ** The code in this file is only compiled if:
 123802 **     * The FTS3 module is being built as an extension
 123803 **       (in which case SQLITE_CORE is not defined), or
 123805 **     * The FTS3 module is being built into the core of
 123806 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 123809 /* The full-text index is stored in a series of b+tree (-like)
 123810 ** structures called segments which map terms to doclists.  The
 123811 ** structures are like b+trees in layout, but are constructed from the
 123812 ** bottom up in optimal fashion and are not updatable.  Since trees
 123813 ** are built from the bottom up, things will be described from the
 123814 ** bottom up.
 123817 **** Varints ****
 123818 ** The basic unit of encoding is a variable-length integer called a
 123819 ** varint.  We encode variable-length integers in little-endian order
 123820 ** using seven bits * per byte as follows:
 123822 ** KEY:
 123823 **         A = 0xxxxxxx    7 bits of data and one flag bit
 123824 **         B = 1xxxxxxx    7 bits of data and one flag bit
 123826 **  7 bits - A
 123827 ** 14 bits - BA
 123828 ** 21 bits - BBA
 123829 ** and so on.
 123831 ** This is similar in concept to how sqlite encodes "varints" but
 123832 ** the encoding is not the same.  SQLite varints are big-endian
 123833 ** are are limited to 9 bytes in length whereas FTS3 varints are
 123834 ** little-endian and can be up to 10 bytes in length (in theory).
 123836 ** Example encodings:
 123838 **     1:    0x01
 123839 **   127:    0x7f
 123840 **   128:    0x81 0x00
 123843 **** Document lists ****
 123844 ** A doclist (document list) holds a docid-sorted list of hits for a
 123845 ** given term.  Doclists hold docids and associated token positions.
 123846 ** A docid is the unique integer identifier for a single document.
 123847 ** A position is the index of a word within the document.  The first 
 123848 ** word of the document has a position of 0.
 123850 ** FTS3 used to optionally store character offsets using a compile-time
 123851 ** option.  But that functionality is no longer supported.
 123853 ** A doclist is stored like this:
 123855 ** array {
 123856 **   varint docid;          (delta from previous doclist)
 123857 **   array {                (position list for column 0)
 123858 **     varint position;     (2 more than the delta from previous position)
 123859 **   }
 123860 **   array {
 123861 **     varint POS_COLUMN;   (marks start of position list for new column)
 123862 **     varint column;       (index of new column)
 123863 **     array {
 123864 **       varint position;   (2 more than the delta from previous position)
 123865 **     }
 123866 **   }
 123867 **   varint POS_END;        (marks end of positions for this document.
 123868 ** }
 123870 ** Here, array { X } means zero or more occurrences of X, adjacent in
 123871 ** memory.  A "position" is an index of a token in the token stream
 123872 ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur 
 123873 ** in the same logical place as the position element, and act as sentinals
 123874 ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
 123875 ** The positions numbers are not stored literally but rather as two more
 123876 ** than the difference from the prior position, or the just the position plus
 123877 ** 2 for the first position.  Example:
 123879 **   label:       A B C D E  F  G H   I  J K
 123880 **   value:     123 5 9 1 1 14 35 0 234 72 0
 123882 ** The 123 value is the first docid.  For column zero in this document
 123883 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
 123884 ** at D signals the start of a new column; the 1 at E indicates that the
 123885 ** new column is column number 1.  There are two positions at 12 and 45
 123886 ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
 123887 ** 234 at I is the delta to next docid (357).  It has one position 70
 123888 ** (72-2) and then terminates with the 0 at K.
 123890 ** A "position-list" is the list of positions for multiple columns for
 123891 ** a single docid.  A "column-list" is the set of positions for a single
 123892 ** column.  Hence, a position-list consists of one or more column-lists,
 123893 ** a document record consists of a docid followed by a position-list and
 123894 ** a doclist consists of one or more document records.
 123896 ** A bare doclist omits the position information, becoming an 
 123897 ** array of varint-encoded docids.
 123899 **** Segment leaf nodes ****
 123900 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
 123901 ** nodes are written using LeafWriter, and read using LeafReader (to
 123902 ** iterate through a single leaf node's data) and LeavesReader (to
 123903 ** iterate through a segment's entire leaf layer).  Leaf nodes have
 123904 ** the format:
 123906 ** varint iHeight;             (height from leaf level, always 0)
 123907 ** varint nTerm;               (length of first term)
 123908 ** char pTerm[nTerm];          (content of first term)
 123909 ** varint nDoclist;            (length of term's associated doclist)
 123910 ** char pDoclist[nDoclist];    (content of doclist)
 123911 ** array {
 123912 **                             (further terms are delta-encoded)
 123913 **   varint nPrefix;           (length of prefix shared with previous term)
 123914 **   varint nSuffix;           (length of unshared suffix)
 123915 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
 123916 **   varint nDoclist;          (length of term's associated doclist)
 123917 **   char pDoclist[nDoclist];  (content of doclist)
 123918 ** }
 123920 ** Here, array { X } means zero or more occurrences of X, adjacent in
 123921 ** memory.
 123923 ** Leaf nodes are broken into blocks which are stored contiguously in
 123924 ** the %_segments table in sorted order.  This means that when the end
 123925 ** of a node is reached, the next term is in the node with the next
 123926 ** greater node id.
 123928 ** New data is spilled to a new leaf node when the current node
 123929 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
 123930 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
 123931 ** node (a leaf node with a single term and doclist).  The goal of
 123932 ** these settings is to pack together groups of small doclists while
 123933 ** making it efficient to directly access large doclists.  The
 123934 ** assumption is that large doclists represent terms which are more
 123935 ** likely to be query targets.
 123937 ** TODO(shess) It may be useful for blocking decisions to be more
 123938 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
 123939 ** node rather than splitting into 2k and .5k nodes.  My intuition is
 123940 ** that this might extend through 2x or 4x the pagesize.
 123943 **** Segment interior nodes ****
 123944 ** Segment interior nodes store blockids for subtree nodes and terms
 123945 ** to describe what data is stored by the each subtree.  Interior
 123946 ** nodes are written using InteriorWriter, and read using
 123947 ** InteriorReader.  InteriorWriters are created as needed when
 123948 ** SegmentWriter creates new leaf nodes, or when an interior node
 123949 ** itself grows too big and must be split.  The format of interior
 123950 ** nodes:
 123952 ** varint iHeight;           (height from leaf level, always >0)
 123953 ** varint iBlockid;          (block id of node's leftmost subtree)
 123954 ** optional {
 123955 **   varint nTerm;           (length of first term)
 123956 **   char pTerm[nTerm];      (content of first term)
 123957 **   array {
 123958 **                                (further terms are delta-encoded)
 123959 **     varint nPrefix;            (length of shared prefix with previous term)
 123960 **     varint nSuffix;            (length of unshared suffix)
 123961 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
 123962 **   }
 123963 ** }
 123965 ** Here, optional { X } means an optional element, while array { X }
 123966 ** means zero or more occurrences of X, adjacent in memory.
 123968 ** An interior node encodes n terms separating n+1 subtrees.  The
 123969 ** subtree blocks are contiguous, so only the first subtree's blockid
 123970 ** is encoded.  The subtree at iBlockid will contain all terms less
 123971 ** than the first term encoded (or all terms if no term is encoded).
 123972 ** Otherwise, for terms greater than or equal to pTerm[i] but less
 123973 ** than pTerm[i+1], the subtree for that term will be rooted at
 123974 ** iBlockid+i.  Interior nodes only store enough term data to
 123975 ** distinguish adjacent children (if the rightmost term of the left
 123976 ** child is "something", and the leftmost term of the right child is
 123977 ** "wicked", only "w" is stored).
 123979 ** New data is spilled to a new interior node at the same height when
 123980 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
 123981 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
 123982 ** interior nodes and making the tree too skinny.  The interior nodes
 123983 ** at a given height are naturally tracked by interior nodes at
 123984 ** height+1, and so on.
 123987 **** Segment directory ****
 123988 ** The segment directory in table %_segdir stores meta-information for
 123989 ** merging and deleting segments, and also the root node of the
 123990 ** segment's tree.
 123992 ** The root node is the top node of the segment's tree after encoding
 123993 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
 123994 ** This could be either a leaf node or an interior node.  If the top
 123995 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
 123996 ** and a new root interior node is generated (which should always fit
 123997 ** within ROOT_MAX because it only needs space for 2 varints, the
 123998 ** height and the blockid of the previous root).
 124000 ** The meta-information in the segment directory is:
 124001 **   level               - segment level (see below)
 124002 **   idx                 - index within level
 124003 **                       - (level,idx uniquely identify a segment)
 124004 **   start_block         - first leaf node
 124005 **   leaves_end_block    - last leaf node
 124006 **   end_block           - last block (including interior nodes)
 124007 **   root                - contents of root node
 124009 ** If the root node is a leaf node, then start_block,
 124010 ** leaves_end_block, and end_block are all 0.
 124013 **** Segment merging ****
 124014 ** To amortize update costs, segments are grouped into levels and
 124015 ** merged in batches.  Each increase in level represents exponentially
 124016 ** more documents.
 124018 ** New documents (actually, document updates) are tokenized and
 124019 ** written individually (using LeafWriter) to a level 0 segment, with
 124020 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
 124021 ** level 0 segments are merged into a single level 1 segment.  Level 1
 124022 ** is populated like level 0, and eventually MERGE_COUNT level 1
 124023 ** segments are merged to a single level 2 segment (representing
 124024 ** MERGE_COUNT^2 updates), and so on.
 124026 ** A segment merge traverses all segments at a given level in
 124027 ** parallel, performing a straightforward sorted merge.  Since segment
 124028 ** leaf nodes are written in to the %_segments table in order, this
 124029 ** merge traverses the underlying sqlite disk structures efficiently.
 124030 ** After the merge, all segment blocks from the merged level are
 124031 ** deleted.
 124033 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
 124034 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
 124035 ** very similar performance numbers to 16 on insertion, though they're
 124036 ** a tiny bit slower (perhaps due to more overhead in merge-time
 124037 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
 124038 ** 16, 2 about 66% slower than 16.
 124040 ** At query time, high MERGE_COUNT increases the number of segments
 124041 ** which need to be scanned and merged.  For instance, with 100k docs
 124042 ** inserted:
 124044 **    MERGE_COUNT   segments
 124045 **       16           25
 124046 **        8           12
 124047 **        4           10
 124048 **        2            6
 124050 ** This appears to have only a moderate impact on queries for very
 124051 ** frequent terms (which are somewhat dominated by segment merge
 124052 ** costs), and infrequent and non-existent terms still seem to be fast
 124053 ** even with many segments.
 124055 ** TODO(shess) That said, it would be nice to have a better query-side
 124056 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
 124057 ** optimizations to things like doclist merging will swing the sweet
 124058 ** spot around.
 124062 **** Handling of deletions and updates ****
 124063 ** Since we're using a segmented structure, with no docid-oriented
 124064 ** index into the term index, we clearly cannot simply update the term
 124065 ** index when a document is deleted or updated.  For deletions, we
 124066 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
 124067 ** we simply write the new doclist.  Segment merges overwrite older
 124068 ** data for a particular docid with newer data, so deletes or updates
 124069 ** will eventually overtake the earlier data and knock it out.  The
 124070 ** query logic likewise merges doclists so that newer data knocks out
 124071 ** older data.
 124074 /************** Include fts3Int.h in the middle of fts3.c ********************/
 124075 /************** Begin file fts3Int.h *****************************************/
 124077 ** 2009 Nov 12
 124079 ** The author disclaims copyright to this source code.  In place of
 124080 ** a legal notice, here is a blessing:
 124082 **    May you do good and not evil.
 124083 **    May you find forgiveness for yourself and forgive others.
 124084 **    May you share freely, never taking more than you give.
 124086 ******************************************************************************
 124089 #ifndef _FTSINT_H
 124090 #define _FTSINT_H
 124092 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 124093 # define NDEBUG 1
 124094 #endif
 124097 ** FTS4 is really an extension for FTS3.  It is enabled using the
 124098 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
 124099 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
 124101 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
 124102 # define SQLITE_ENABLE_FTS3
 124103 #endif
 124105 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 124107 /* If not building as part of the core, include sqlite3ext.h. */
 124108 #ifndef SQLITE_CORE
 124109 SQLITE_EXTENSION_INIT3
 124110 #endif
 124112 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
 124113 /************** Begin file fts3_tokenizer.h **********************************/
 124115 ** 2006 July 10
 124117 ** The author disclaims copyright to this source code.
 124119 *************************************************************************
 124120 ** Defines the interface to tokenizers used by fulltext-search.  There
 124121 ** are three basic components:
 124123 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
 124124 ** interface functions.  This is essentially the class structure for
 124125 ** tokenizers.
 124127 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
 124128 ** including customization information defined at creation time.
 124130 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
 124131 ** tokens from a particular input.
 124133 #ifndef _FTS3_TOKENIZER_H_
 124134 #define _FTS3_TOKENIZER_H_
 124136 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
 124137 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
 124138 ** we will need a way to register the API consistently.
 124142 ** Structures used by the tokenizer interface. When a new tokenizer
 124143 ** implementation is registered, the caller provides a pointer to
 124144 ** an sqlite3_tokenizer_module containing pointers to the callback
 124145 ** functions that make up an implementation.
 124147 ** When an fts3 table is created, it passes any arguments passed to
 124148 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
 124149 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
 124150 ** implementation. The xCreate() function in turn returns an 
 124151 ** sqlite3_tokenizer structure representing the specific tokenizer to
 124152 ** be used for the fts3 table (customized by the tokenizer clause arguments).
 124154 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
 124155 ** method is called. It returns an sqlite3_tokenizer_cursor object
 124156 ** that may be used to tokenize a specific input buffer based on
 124157 ** the tokenization rules supplied by a specific sqlite3_tokenizer
 124158 ** object.
 124160 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
 124161 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
 124162 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
 124164 struct sqlite3_tokenizer_module {
 124167   ** Structure version. Should always be set to 0 or 1.
 124169   int iVersion;
 124172   ** Create a new tokenizer. The values in the argv[] array are the
 124173   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
 124174   ** TABLE statement that created the fts3 table. For example, if
 124175   ** the following SQL is executed:
 124177   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
 124179   ** then argc is set to 2, and the argv[] array contains pointers
 124180   ** to the strings "arg1" and "arg2".
 124182   ** This method should return either SQLITE_OK (0), or an SQLite error 
 124183   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
 124184   ** to point at the newly created tokenizer structure. The generic
 124185   ** sqlite3_tokenizer.pModule variable should not be initialized by
 124186   ** this callback. The caller will do so.
 124188   int (*xCreate)(
 124189     int argc,                           /* Size of argv array */
 124190     const char *const*argv,             /* Tokenizer argument strings */
 124191     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
 124195   ** Destroy an existing tokenizer. The fts3 module calls this method
 124196   ** exactly once for each successful call to xCreate().
 124198   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
 124201   ** Create a tokenizer cursor to tokenize an input buffer. The caller
 124202   ** is responsible for ensuring that the input buffer remains valid
 124203   ** until the cursor is closed (using the xClose() method). 
 124205   int (*xOpen)(
 124206     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
 124207     const char *pInput, int nBytes,      /* Input buffer */
 124208     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
 124212   ** Destroy an existing tokenizer cursor. The fts3 module calls this 
 124213   ** method exactly once for each successful call to xOpen().
 124215   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
 124218   ** Retrieve the next token from the tokenizer cursor pCursor. This
 124219   ** method should either return SQLITE_OK and set the values of the
 124220   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
 124221   ** the end of the buffer has been reached, or an SQLite error code.
 124223   ** *ppToken should be set to point at a buffer containing the 
 124224   ** normalized version of the token (i.e. after any case-folding and/or
 124225   ** stemming has been performed). *pnBytes should be set to the length
 124226   ** of this buffer in bytes. The input text that generated the token is
 124227   ** identified by the byte offsets returned in *piStartOffset and
 124228   ** *piEndOffset. *piStartOffset should be set to the index of the first
 124229   ** byte of the token in the input buffer. *piEndOffset should be set
 124230   ** to the index of the first byte just past the end of the token in
 124231   ** the input buffer.
 124233   ** The buffer *ppToken is set to point at is managed by the tokenizer
 124234   ** implementation. It is only required to be valid until the next call
 124235   ** to xNext() or xClose(). 
 124237   /* TODO(shess) current implementation requires pInput to be
 124238   ** nul-terminated.  This should either be fixed, or pInput/nBytes
 124239   ** should be converted to zInput.
 124241   int (*xNext)(
 124242     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
 124243     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
 124244     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
 124245     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
 124246     int *piPosition      /* OUT: Number of tokens returned before this one */
 124249   /***********************************************************************
 124250   ** Methods below this point are only available if iVersion>=1.
 124254   ** Configure the language id of a tokenizer cursor.
 124256   int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
 124259 struct sqlite3_tokenizer {
 124260   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
 124261   /* Tokenizer implementations will typically add additional fields */
 124264 struct sqlite3_tokenizer_cursor {
 124265   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
 124266   /* Tokenizer implementations will typically add additional fields */
 124269 int fts3_global_term_cnt(int iTerm, int iCol);
 124270 int fts3_term_cnt(int iTerm, int iCol);
 124273 #endif /* _FTS3_TOKENIZER_H_ */
 124275 /************** End of fts3_tokenizer.h **************************************/
 124276 /************** Continuing where we left off in fts3Int.h ********************/
 124277 /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
 124278 /************** Begin file fts3_hash.h ***************************************/
 124280 ** 2001 September 22
 124282 ** The author disclaims copyright to this source code.  In place of
 124283 ** a legal notice, here is a blessing:
 124285 **    May you do good and not evil.
 124286 **    May you find forgiveness for yourself and forgive others.
 124287 **    May you share freely, never taking more than you give.
 124289 *************************************************************************
 124290 ** This is the header file for the generic hash-table implementation
 124291 ** used in SQLite.  We've modified it slightly to serve as a standalone
 124292 ** hash table implementation for the full-text indexing module.
 124295 #ifndef _FTS3_HASH_H_
 124296 #define _FTS3_HASH_H_
 124298 /* Forward declarations of structures. */
 124299 typedef struct Fts3Hash Fts3Hash;
 124300 typedef struct Fts3HashElem Fts3HashElem;
 124302 /* A complete hash table is an instance of the following structure.
 124303 ** The internals of this structure are intended to be opaque -- client
 124304 ** code should not attempt to access or modify the fields of this structure
 124305 ** directly.  Change this structure only by using the routines below.
 124306 ** However, many of the "procedures" and "functions" for modifying and
 124307 ** accessing this structure are really macros, so we can't really make
 124308 ** this structure opaque.
 124310 struct Fts3Hash {
 124311   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
 124312   char copyKey;           /* True if copy of key made on insert */
 124313   int count;              /* Number of entries in this table */
 124314   Fts3HashElem *first;    /* The first element of the array */
 124315   int htsize;             /* Number of buckets in the hash table */
 124316   struct _fts3ht {        /* the hash table */
 124317     int count;               /* Number of entries with this hash */
 124318     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
 124319   } *ht;
 124322 /* Each element in the hash table is an instance of the following 
 124323 ** structure.  All elements are stored on a single doubly-linked list.
 124325 ** Again, this structure is intended to be opaque, but it can't really
 124326 ** be opaque because it is used by macros.
 124328 struct Fts3HashElem {
 124329   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
 124330   void *data;                /* Data associated with this element */
 124331   void *pKey; int nKey;      /* Key associated with this element */
 124335 ** There are 2 different modes of operation for a hash table:
 124337 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
 124338 **                           (including the null-terminator, if any).  Case
 124339 **                           is respected in comparisons.
 124341 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
 124342 **                           memcmp() is used to compare keys.
 124344 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
 124346 #define FTS3_HASH_STRING    1
 124347 #define FTS3_HASH_BINARY    2
 124350 ** Access routines.  To delete, insert a NULL pointer.
 124352 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
 124353 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
 124354 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
 124355 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
 124356 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
 124359 ** Shorthand for the functions above
 124361 #define fts3HashInit     sqlite3Fts3HashInit
 124362 #define fts3HashInsert   sqlite3Fts3HashInsert
 124363 #define fts3HashFind     sqlite3Fts3HashFind
 124364 #define fts3HashClear    sqlite3Fts3HashClear
 124365 #define fts3HashFindElem sqlite3Fts3HashFindElem
 124368 ** Macros for looping over all elements of a hash table.  The idiom is
 124369 ** like this:
 124371 **   Fts3Hash h;
 124372 **   Fts3HashElem *p;
 124373 **   ...
 124374 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
 124375 **     SomeStructure *pData = fts3HashData(p);
 124376 **     // do something with pData
 124377 **   }
 124379 #define fts3HashFirst(H)  ((H)->first)
 124380 #define fts3HashNext(E)   ((E)->next)
 124381 #define fts3HashData(E)   ((E)->data)
 124382 #define fts3HashKey(E)    ((E)->pKey)
 124383 #define fts3HashKeysize(E) ((E)->nKey)
 124386 ** Number of entries in a hash table
 124388 #define fts3HashCount(H)  ((H)->count)
 124390 #endif /* _FTS3_HASH_H_ */
 124392 /************** End of fts3_hash.h *******************************************/
 124393 /************** Continuing where we left off in fts3Int.h ********************/
 124396 ** This constant determines the maximum depth of an FTS expression tree
 124397 ** that the library will create and use. FTS uses recursion to perform 
 124398 ** various operations on the query tree, so the disadvantage of a large
 124399 ** limit is that it may allow very large queries to use large amounts
 124400 ** of stack space (perhaps causing a stack overflow).
 124402 #ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
 124403 # define SQLITE_FTS3_MAX_EXPR_DEPTH 12
 124404 #endif
 124408 ** This constant controls how often segments are merged. Once there are
 124409 ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
 124410 ** segment of level N+1.
 124412 #define FTS3_MERGE_COUNT 16
 124415 ** This is the maximum amount of data (in bytes) to store in the 
 124416 ** Fts3Table.pendingTerms hash table. Normally, the hash table is
 124417 ** populated as documents are inserted/updated/deleted in a transaction
 124418 ** and used to create a new segment when the transaction is committed.
 124419 ** However if this limit is reached midway through a transaction, a new 
 124420 ** segment is created and the hash table cleared immediately.
 124422 #define FTS3_MAX_PENDING_DATA (1*1024*1024)
 124425 ** Macro to return the number of elements in an array. SQLite has a
 124426 ** similar macro called ArraySize(). Use a different name to avoid
 124427 ** a collision when building an amalgamation with built-in FTS3.
 124429 #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
 124432 #ifndef MIN
 124433 # define MIN(x,y) ((x)<(y)?(x):(y))
 124434 #endif
 124435 #ifndef MAX
 124436 # define MAX(x,y) ((x)>(y)?(x):(y))
 124437 #endif
 124440 ** Maximum length of a varint encoded integer. The varint format is different
 124441 ** from that used by SQLite, so the maximum length is 10, not 9.
 124443 #define FTS3_VARINT_MAX 10
 124446 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
 124447 ** in the document set and zero or more prefix indexes. All indexes are stored
 124448 ** as one or more b+-trees in the %_segments and %_segdir tables. 
 124450 ** It is possible to determine which index a b+-tree belongs to based on the
 124451 ** value stored in the "%_segdir.level" column. Given this value L, the index
 124452 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
 124453 ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
 124454 ** between 1024 and 2047 to index 1, and so on.
 124456 ** It is considered impossible for an index to use more than 1024 levels. In 
 124457 ** theory though this may happen, but only after at least 
 124458 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
 124460 #define FTS3_SEGDIR_MAXLEVEL      1024
 124461 #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
 124464 ** The testcase() macro is only used by the amalgamation.  If undefined,
 124465 ** make it a no-op.
 124467 #ifndef testcase
 124468 # define testcase(X)
 124469 #endif
 124472 ** Terminator values for position-lists and column-lists.
 124474 #define POS_COLUMN  (1)     /* Column-list terminator */
 124475 #define POS_END     (0)     /* Position-list terminator */ 
 124478 ** This section provides definitions to allow the
 124479 ** FTS3 extension to be compiled outside of the 
 124480 ** amalgamation.
 124482 #ifndef SQLITE_AMALGAMATION
 124484 ** Macros indicating that conditional expressions are always true or
 124485 ** false.
 124487 #ifdef SQLITE_COVERAGE_TEST
 124488 # define ALWAYS(x) (1)
 124489 # define NEVER(X)  (0)
 124490 #else
 124491 # define ALWAYS(x) (x)
 124492 # define NEVER(x)  (x)
 124493 #endif
 124496 ** Internal types used by SQLite.
 124498 typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
 124499 typedef short int i16;            /* 2-byte (or larger) signed integer */
 124500 typedef unsigned int u32;         /* 4-byte unsigned integer */
 124501 typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
 124502 typedef sqlite3_int64 i64;        /* 8-byte signed integer */
 124505 ** Macro used to suppress compiler warnings for unused parameters.
 124507 #define UNUSED_PARAMETER(x) (void)(x)
 124510 ** Activate assert() only if SQLITE_TEST is enabled.
 124512 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 124513 # define NDEBUG 1
 124514 #endif
 124517 ** The TESTONLY macro is used to enclose variable declarations or
 124518 ** other bits of code that are needed to support the arguments
 124519 ** within testcase() and assert() macros.
 124521 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 124522 # define TESTONLY(X)  X
 124523 #else
 124524 # define TESTONLY(X)
 124525 #endif
 124527 #endif /* SQLITE_AMALGAMATION */
 124529 #ifdef SQLITE_DEBUG
 124530 SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
 124531 # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
 124532 #else
 124533 # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
 124534 #endif
 124536 typedef struct Fts3Table Fts3Table;
 124537 typedef struct Fts3Cursor Fts3Cursor;
 124538 typedef struct Fts3Expr Fts3Expr;
 124539 typedef struct Fts3Phrase Fts3Phrase;
 124540 typedef struct Fts3PhraseToken Fts3PhraseToken;
 124542 typedef struct Fts3Doclist Fts3Doclist;
 124543 typedef struct Fts3SegFilter Fts3SegFilter;
 124544 typedef struct Fts3DeferredToken Fts3DeferredToken;
 124545 typedef struct Fts3SegReader Fts3SegReader;
 124546 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
 124549 ** A connection to a fulltext index is an instance of the following
 124550 ** structure. The xCreate and xConnect methods create an instance
 124551 ** of this structure and xDestroy and xDisconnect free that instance.
 124552 ** All other methods receive a pointer to the structure as one of their
 124553 ** arguments.
 124555 struct Fts3Table {
 124556   sqlite3_vtab base;              /* Base class used by SQLite core */
 124557   sqlite3 *db;                    /* The database connection */
 124558   const char *zDb;                /* logical database name */
 124559   const char *zName;              /* virtual table name */
 124560   int nColumn;                    /* number of named columns in virtual table */
 124561   char **azColumn;                /* column names.  malloced */
 124562   u8 *abNotindexed;               /* True for 'notindexed' columns */
 124563   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
 124564   char *zContentTbl;              /* content=xxx option, or NULL */
 124565   char *zLanguageid;              /* languageid=xxx option, or NULL */
 124566   u8 bAutoincrmerge;              /* True if automerge=1 */
 124567   u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
 124569   /* Precompiled statements used by the implementation. Each of these 
 124570   ** statements is run and reset within a single virtual table API call. 
 124572   sqlite3_stmt *aStmt[37];
 124574   char *zReadExprlist;
 124575   char *zWriteExprlist;
 124577   int nNodeSize;                  /* Soft limit for node size */
 124578   u8 bFts4;                       /* True for FTS4, false for FTS3 */
 124579   u8 bHasStat;                    /* True if %_stat table exists */
 124580   u8 bHasDocsize;                 /* True if %_docsize table exists */
 124581   u8 bDescIdx;                    /* True if doclists are in reverse order */
 124582   u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
 124583   int nPgsz;                      /* Page size for host database */
 124584   char *zSegmentsTbl;             /* Name of %_segments table */
 124585   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
 124588   ** The following array of hash tables is used to buffer pending index 
 124589   ** updates during transactions. All pending updates buffered at any one
 124590   ** time must share a common language-id (see the FTS4 langid= feature).
 124591   ** The current language id is stored in variable iPrevLangid.
 124593   ** A single FTS4 table may have multiple full-text indexes. For each index
 124594   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
 124595   ** terms that appear in the document set. Each subsequent index in aIndex[]
 124596   ** is an index of prefixes of a specific length.
 124598   ** Variable nPendingData contains an estimate the memory consumed by the 
 124599   ** pending data structures, including hash table overhead, but not including
 124600   ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
 124601   ** tables are flushed to disk. Variable iPrevDocid is the docid of the most 
 124602   ** recently inserted record.
 124604   int nIndex;                     /* Size of aIndex[] */
 124605   struct Fts3Index {
 124606     int nPrefix;                  /* Prefix length (0 for main terms index) */
 124607     Fts3Hash hPending;            /* Pending terms table for this index */
 124608   } *aIndex;
 124609   int nMaxPendingData;            /* Max pending data before flush to disk */
 124610   int nPendingData;               /* Current bytes of pending data */
 124611   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
 124612   int iPrevLangid;                /* Langid of recently inserted document */
 124614 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
 124615   /* State variables used for validating that the transaction control
 124616   ** methods of the virtual table are called at appropriate times.  These
 124617   ** values do not contribute to FTS functionality; they are used for
 124618   ** verifying the operation of the SQLite core.
 124620   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
 124621   int mxSavepoint;       /* Largest valid xSavepoint integer */
 124622 #endif
 124624 #ifdef SQLITE_TEST
 124625   /* True to disable the incremental doclist optimization. This is controled
 124626   ** by special insert command 'test-no-incr-doclist'.  */
 124627   int bNoIncrDoclist;
 124628 #endif
 124632 ** When the core wants to read from the virtual table, it creates a
 124633 ** virtual table cursor (an instance of the following structure) using
 124634 ** the xOpen method. Cursors are destroyed using the xClose method.
 124636 struct Fts3Cursor {
 124637   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 124638   i16 eSearch;                    /* Search strategy (see below) */
 124639   u8 isEof;                       /* True if at End Of Results */
 124640   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
 124641   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
 124642   Fts3Expr *pExpr;                /* Parsed MATCH query string */
 124643   int iLangid;                    /* Language being queried for */
 124644   int nPhrase;                    /* Number of matchable phrases in query */
 124645   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
 124646   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
 124647   char *pNextId;                  /* Pointer into the body of aDoclist */
 124648   char *aDoclist;                 /* List of docids for full-text queries */
 124649   int nDoclist;                   /* Size of buffer at aDoclist */
 124650   u8 bDesc;                       /* True to sort in descending order */
 124651   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
 124652   int nRowAvg;                    /* Average size of database rows, in pages */
 124653   sqlite3_int64 nDoc;             /* Documents in table */
 124654   i64 iMinDocid;                  /* Minimum docid to return */
 124655   i64 iMaxDocid;                  /* Maximum docid to return */
 124656   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
 124657   u32 *aMatchinfo;                /* Information about most recent match */
 124658   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
 124659   char *zMatchinfo;               /* Matchinfo specification */
 124662 #define FTS3_EVAL_FILTER    0
 124663 #define FTS3_EVAL_NEXT      1
 124664 #define FTS3_EVAL_MATCHINFO 2
 124667 ** The Fts3Cursor.eSearch member is always set to one of the following.
 124668 ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
 124669 ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
 124670 ** of the column to be searched.  For example, in
 124672 **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
 124673 **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
 124675 ** Because the LHS of the MATCH operator is 2nd column "b",
 124676 ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
 124677 ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
 124678 ** indicating that all columns should be searched,
 124679 ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
 124681 #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
 124682 #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
 124683 #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
 124686 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
 124687 ** the xBestIndex() method contains the Fts3Cursor.eSearch value described
 124688 ** above. The upper 16-bits contain a combination of the following
 124689 ** bits, used to describe extra constraints on full-text searches.
 124691 #define FTS3_HAVE_LANGID    0x00010000      /* languageid=? */
 124692 #define FTS3_HAVE_DOCID_GE  0x00020000      /* docid>=? */
 124693 #define FTS3_HAVE_DOCID_LE  0x00040000      /* docid<=? */
 124695 struct Fts3Doclist {
 124696   char *aAll;                    /* Array containing doclist (or NULL) */
 124697   int nAll;                      /* Size of a[] in bytes */
 124698   char *pNextDocid;              /* Pointer to next docid */
 124700   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
 124701   int bFreeList;                 /* True if pList should be sqlite3_free()d */
 124702   char *pList;                   /* Pointer to position list following iDocid */
 124703   int nList;                     /* Length of position list */
 124707 ** A "phrase" is a sequence of one or more tokens that must match in
 124708 ** sequence.  A single token is the base case and the most common case.
 124709 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
 124710 ** nToken will be the number of tokens in the string.
 124712 struct Fts3PhraseToken {
 124713   char *z;                        /* Text of the token */
 124714   int n;                          /* Number of bytes in buffer z */
 124715   int isPrefix;                   /* True if token ends with a "*" character */
 124716   int bFirst;                     /* True if token must appear at position 0 */
 124718   /* Variables above this point are populated when the expression is
 124719   ** parsed (by code in fts3_expr.c). Below this point the variables are
 124720   ** used when evaluating the expression. */
 124721   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
 124722   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
 124725 struct Fts3Phrase {
 124726   /* Cache of doclist for this phrase. */
 124727   Fts3Doclist doclist;
 124728   int bIncr;                 /* True if doclist is loaded incrementally */
 124729   int iDoclistToken;
 124731   /* Variables below this point are populated by fts3_expr.c when parsing 
 124732   ** a MATCH expression. Everything above is part of the evaluation phase. 
 124734   int nToken;                /* Number of tokens in the phrase */
 124735   int iColumn;               /* Index of column this phrase must match */
 124736   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
 124740 ** A tree of these objects forms the RHS of a MATCH operator.
 124742 ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist 
 124743 ** points to a malloced buffer, size nDoclist bytes, containing the results 
 124744 ** of this phrase query in FTS3 doclist format. As usual, the initial 
 124745 ** "Length" field found in doclists stored on disk is omitted from this 
 124746 ** buffer.
 124748 ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
 124749 ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
 124750 ** where nCol is the number of columns in the queried FTS table. The array
 124751 ** is populated as follows:
 124753 **   aMI[iCol*3 + 0] = Undefined
 124754 **   aMI[iCol*3 + 1] = Number of occurrences
 124755 **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
 124757 ** The aMI array is allocated using sqlite3_malloc(). It should be freed 
 124758 ** when the expression node is.
 124760 struct Fts3Expr {
 124761   int eType;                 /* One of the FTSQUERY_XXX values defined below */
 124762   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
 124763   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
 124764   Fts3Expr *pLeft;           /* Left operand */
 124765   Fts3Expr *pRight;          /* Right operand */
 124766   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
 124768   /* The following are used by the fts3_eval.c module. */
 124769   sqlite3_int64 iDocid;      /* Current docid */
 124770   u8 bEof;                   /* True this expression is at EOF already */
 124771   u8 bStart;                 /* True if iDocid is valid */
 124772   u8 bDeferred;              /* True if this expression is entirely deferred */
 124774   u32 *aMI;
 124778 ** Candidate values for Fts3Query.eType. Note that the order of the first
 124779 ** four values is in order of precedence when parsing expressions. For 
 124780 ** example, the following:
 124782 **   "a OR b AND c NOT d NEAR e"
 124784 ** is equivalent to:
 124786 **   "a OR (b AND (c NOT (d NEAR e)))"
 124788 #define FTSQUERY_NEAR   1
 124789 #define FTSQUERY_NOT    2
 124790 #define FTSQUERY_AND    3
 124791 #define FTSQUERY_OR     4
 124792 #define FTSQUERY_PHRASE 5
 124795 /* fts3_write.c */
 124796 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
 124797 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
 124798 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
 124799 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
 124800 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
 124801   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
 124802 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
 124803   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
 124804 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
 124805 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
 124806 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
 124808 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
 124809 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
 124811 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 124812 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
 124813 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
 124814 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
 124815 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
 124816 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
 124817 #else
 124818 # define sqlite3Fts3FreeDeferredTokens(x)
 124819 # define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
 124820 # define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
 124821 # define sqlite3Fts3FreeDeferredDoclists(x)
 124822 # define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
 124823 #endif
 124825 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
 124826 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
 124828 /* Special values interpreted by sqlite3SegReaderCursor() */
 124829 #define FTS3_SEGCURSOR_PENDING        -1
 124830 #define FTS3_SEGCURSOR_ALL            -2
 124832 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
 124833 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
 124834 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
 124836 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *, 
 124837     int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
 124839 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
 124840 #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
 124841 #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
 124842 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
 124843 #define FTS3_SEGMENT_PREFIX        0x00000008
 124844 #define FTS3_SEGMENT_SCAN          0x00000010
 124845 #define FTS3_SEGMENT_FIRST         0x00000020
 124847 /* Type passed as 4th argument to SegmentReaderIterate() */
 124848 struct Fts3SegFilter {
 124849   const char *zTerm;
 124850   int nTerm;
 124851   int iCol;
 124852   int flags;
 124855 struct Fts3MultiSegReader {
 124856   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
 124857   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
 124858   int nSegment;                   /* Size of apSegment array */
 124859   int nAdvance;                   /* How many seg-readers to advance */
 124860   Fts3SegFilter *pFilter;         /* Pointer to filter object */
 124861   char *aBuffer;                  /* Buffer to merge doclists in */
 124862   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
 124864   int iColFilter;                 /* If >=0, filter for this column */
 124865   int bRestart;
 124867   /* Used by fts3.c only. */
 124868   int nCost;                      /* Cost of running iterator */
 124869   int bLookup;                    /* True if a lookup of a single entry. */
 124871   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
 124872   char *zTerm;                    /* Pointer to term buffer */
 124873   int nTerm;                      /* Size of zTerm in bytes */
 124874   char *aDoclist;                 /* Pointer to doclist buffer */
 124875   int nDoclist;                   /* Size of aDoclist[] in bytes */
 124878 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
 124880 #define fts3GetVarint32(p, piVal) (                                           \
 124881   (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
 124884 /* fts3.c */
 124885 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
 124886 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
 124887 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
 124888 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
 124889 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
 124890 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
 124891 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
 124892 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
 124893 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
 124895 /* fts3_tokenizer.c */
 124896 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
 124897 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
 124898 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
 124899     sqlite3_tokenizer **, char **
 124901 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
 124903 /* fts3_snippet.c */
 124904 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
 124905 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
 124906   const char *, const char *, int, int
 124908 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
 124910 /* fts3_expr.c */
 124911 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
 124912   char **, int, int, int, const char *, int, Fts3Expr **, char **
 124914 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
 124915 #ifdef SQLITE_TEST
 124916 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
 124917 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
 124918 #endif
 124920 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
 124921   sqlite3_tokenizer_cursor **
 124924 /* fts3_aux.c */
 124925 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
 124927 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
 124929 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
 124930     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
 124931 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
 124932     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
 124933 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); 
 124934 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
 124935 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
 124937 /* fts3_tokenize_vtab.c */
 124938 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
 124940 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
 124941 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 124942 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
 124943 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
 124944 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
 124945 #endif
 124947 #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
 124948 #endif /* _FTSINT_H */
 124950 /************** End of fts3Int.h *********************************************/
 124951 /************** Continuing where we left off in fts3.c ***********************/
 124952 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 124954 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
 124955 # define SQLITE_CORE 1
 124956 #endif
 124958 /* #include <assert.h> */
 124959 /* #include <stdlib.h> */
 124960 /* #include <stddef.h> */
 124961 /* #include <stdio.h> */
 124962 /* #include <string.h> */
 124963 /* #include <stdarg.h> */
 124965 #ifndef SQLITE_CORE 
 124966   SQLITE_EXTENSION_INIT1
 124967 #endif
 124969 static int fts3EvalNext(Fts3Cursor *pCsr);
 124970 static int fts3EvalStart(Fts3Cursor *pCsr);
 124971 static int fts3TermSegReaderCursor(
 124972     Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
 124975 ** Write a 64-bit variable-length integer to memory starting at p[0].
 124976 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
 124977 ** The number of bytes written is returned.
 124979 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
 124980   unsigned char *q = (unsigned char *) p;
 124981   sqlite_uint64 vu = v;
 124982   do{
 124983     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
 124984     vu >>= 7;
 124985   }while( vu!=0 );
 124986   q[-1] &= 0x7f;  /* turn off high bit in final byte */
 124987   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
 124988   return (int) (q - (unsigned char *)p);
 124991 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
 124992   v = (v & mask1) | ( (*ptr++) << shift );                    \
 124993   if( (v & mask2)==0 ){ var = v; return ret; }
 124994 #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
 124995   v = (*ptr++);                                               \
 124996   if( (v & mask2)==0 ){ var = v; return ret; }
 124999 ** Read a 64-bit variable-length integer from memory starting at p[0].
 125000 ** Return the number of bytes read, or 0 on error.
 125001 ** The value is stored in *v.
 125003 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
 125004   const char *pStart = p;
 125005   u32 a;
 125006   u64 b;
 125007   int shift;
 125009   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
 125010   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
 125011   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
 125012   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
 125013   b = (a & 0x0FFFFFFF );
 125015   for(shift=28; shift<=63; shift+=7){
 125016     u64 c = *p++;
 125017     b += (c&0x7F) << shift;
 125018     if( (c & 0x80)==0 ) break;
 125020   *v = b;
 125021   return (int)(p - pStart);
 125025 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
 125026 ** 32-bit integer before it is returned.
 125028 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
 125029   u32 a;
 125031 #ifndef fts3GetVarint32
 125032   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
 125033 #else
 125034   a = (*p++);
 125035   assert( a & 0x80 );
 125036 #endif
 125038   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
 125039   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);
 125040   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
 125041   a = (a & 0x0FFFFFFF );
 125042   *pi = (int)(a | ((u32)(*p & 0x0F) << 28));
 125043   return 5;
 125047 ** Return the number of bytes required to encode v as a varint
 125049 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
 125050   int i = 0;
 125051   do{
 125052     i++;
 125053     v >>= 7;
 125054   }while( v!=0 );
 125055   return i;
 125059 ** Convert an SQL-style quoted string into a normal string by removing
 125060 ** the quote characters.  The conversion is done in-place.  If the
 125061 ** input does not begin with a quote character, then this routine
 125062 ** is a no-op.
 125064 ** Examples:
 125066 **     "abc"   becomes   abc
 125067 **     'xyz'   becomes   xyz
 125068 **     [pqr]   becomes   pqr
 125069 **     `mno`   becomes   mno
 125072 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
 125073   char quote;                     /* Quote character (if any ) */
 125075   quote = z[0];
 125076   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
 125077     int iIn = 1;                  /* Index of next byte to read from input */
 125078     int iOut = 0;                 /* Index of next byte to write to output */
 125080     /* If the first byte was a '[', then the close-quote character is a ']' */
 125081     if( quote=='[' ) quote = ']';  
 125083     while( ALWAYS(z[iIn]) ){
 125084       if( z[iIn]==quote ){
 125085         if( z[iIn+1]!=quote ) break;
 125086         z[iOut++] = quote;
 125087         iIn += 2;
 125088       }else{
 125089         z[iOut++] = z[iIn++];
 125092     z[iOut] = '\0';
 125097 ** Read a single varint from the doclist at *pp and advance *pp to point
 125098 ** to the first byte past the end of the varint.  Add the value of the varint
 125099 ** to *pVal.
 125101 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
 125102   sqlite3_int64 iVal;
 125103   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
 125104   *pVal += iVal;
 125108 ** When this function is called, *pp points to the first byte following a
 125109 ** varint that is part of a doclist (or position-list, or any other list
 125110 ** of varints). This function moves *pp to point to the start of that varint,
 125111 ** and sets *pVal by the varint value.
 125113 ** Argument pStart points to the first byte of the doclist that the
 125114 ** varint is part of.
 125116 static void fts3GetReverseVarint(
 125117   char **pp, 
 125118   char *pStart, 
 125119   sqlite3_int64 *pVal
 125121   sqlite3_int64 iVal;
 125122   char *p;
 125124   /* Pointer p now points at the first byte past the varint we are 
 125125   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
 125126   ** clear on character p[-1]. */
 125127   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
 125128   p++;
 125129   *pp = p;
 125131   sqlite3Fts3GetVarint(p, &iVal);
 125132   *pVal = iVal;
 125136 ** The xDisconnect() virtual table method.
 125138 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
 125139   Fts3Table *p = (Fts3Table *)pVtab;
 125140   int i;
 125142   assert( p->nPendingData==0 );
 125143   assert( p->pSegments==0 );
 125145   /* Free any prepared statements held */
 125146   for(i=0; i<SizeofArray(p->aStmt); i++){
 125147     sqlite3_finalize(p->aStmt[i]);
 125149   sqlite3_free(p->zSegmentsTbl);
 125150   sqlite3_free(p->zReadExprlist);
 125151   sqlite3_free(p->zWriteExprlist);
 125152   sqlite3_free(p->zContentTbl);
 125153   sqlite3_free(p->zLanguageid);
 125155   /* Invoke the tokenizer destructor to free the tokenizer. */
 125156   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
 125158   sqlite3_free(p);
 125159   return SQLITE_OK;
 125163 ** Construct one or more SQL statements from the format string given
 125164 ** and then evaluate those statements. The success code is written
 125165 ** into *pRc.
 125167 ** If *pRc is initially non-zero then this routine is a no-op.
 125169 static void fts3DbExec(
 125170   int *pRc,              /* Success code */
 125171   sqlite3 *db,           /* Database in which to run SQL */
 125172   const char *zFormat,   /* Format string for SQL */
 125173   ...                    /* Arguments to the format string */
 125175   va_list ap;
 125176   char *zSql;
 125177   if( *pRc ) return;
 125178   va_start(ap, zFormat);
 125179   zSql = sqlite3_vmprintf(zFormat, ap);
 125180   va_end(ap);
 125181   if( zSql==0 ){
 125182     *pRc = SQLITE_NOMEM;
 125183   }else{
 125184     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
 125185     sqlite3_free(zSql);
 125190 ** The xDestroy() virtual table method.
 125192 static int fts3DestroyMethod(sqlite3_vtab *pVtab){
 125193   Fts3Table *p = (Fts3Table *)pVtab;
 125194   int rc = SQLITE_OK;              /* Return code */
 125195   const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
 125196   sqlite3 *db = p->db;             /* Database handle */
 125198   /* Drop the shadow tables */
 125199   if( p->zContentTbl==0 ){
 125200     fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
 125202   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
 125203   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
 125204   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
 125205   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
 125207   /* If everything has worked, invoke fts3DisconnectMethod() to free the
 125208   ** memory associated with the Fts3Table structure and return SQLITE_OK.
 125209   ** Otherwise, return an SQLite error code.
 125211   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
 125216 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
 125217 ** passed as the first argument. This is done as part of the xConnect()
 125218 ** and xCreate() methods.
 125220 ** If *pRc is non-zero when this function is called, it is a no-op. 
 125221 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
 125222 ** before returning.
 125224 static void fts3DeclareVtab(int *pRc, Fts3Table *p){
 125225   if( *pRc==SQLITE_OK ){
 125226     int i;                        /* Iterator variable */
 125227     int rc;                       /* Return code */
 125228     char *zSql;                   /* SQL statement passed to declare_vtab() */
 125229     char *zCols;                  /* List of user defined columns */
 125230     const char *zLanguageid;
 125232     zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
 125233     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
 125235     /* Create a list of user columns for the virtual table */
 125236     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
 125237     for(i=1; zCols && i<p->nColumn; i++){
 125238       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
 125241     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
 125242     zSql = sqlite3_mprintf(
 125243         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)", 
 125244         zCols, p->zName, zLanguageid
 125246     if( !zCols || !zSql ){
 125247       rc = SQLITE_NOMEM;
 125248     }else{
 125249       rc = sqlite3_declare_vtab(p->db, zSql);
 125252     sqlite3_free(zSql);
 125253     sqlite3_free(zCols);
 125254     *pRc = rc;
 125259 ** Create the %_stat table if it does not already exist.
 125261 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
 125262   fts3DbExec(pRc, p->db, 
 125263       "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
 125264           "(id INTEGER PRIMARY KEY, value BLOB);",
 125265       p->zDb, p->zName
 125267   if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
 125271 ** Create the backing store tables (%_content, %_segments and %_segdir)
 125272 ** required by the FTS3 table passed as the only argument. This is done
 125273 ** as part of the vtab xCreate() method.
 125275 ** If the p->bHasDocsize boolean is true (indicating that this is an
 125276 ** FTS4 table, not an FTS3 table) then also create the %_docsize and
 125277 ** %_stat tables required by FTS4.
 125279 static int fts3CreateTables(Fts3Table *p){
 125280   int rc = SQLITE_OK;             /* Return code */
 125281   int i;                          /* Iterator variable */
 125282   sqlite3 *db = p->db;            /* The database connection */
 125284   if( p->zContentTbl==0 ){
 125285     const char *zLanguageid = p->zLanguageid;
 125286     char *zContentCols;           /* Columns of %_content table */
 125288     /* Create a list of user columns for the content table */
 125289     zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
 125290     for(i=0; zContentCols && i<p->nColumn; i++){
 125291       char *z = p->azColumn[i];
 125292       zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
 125294     if( zLanguageid && zContentCols ){
 125295       zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
 125297     if( zContentCols==0 ) rc = SQLITE_NOMEM;
 125299     /* Create the content table */
 125300     fts3DbExec(&rc, db, 
 125301        "CREATE TABLE %Q.'%q_content'(%s)",
 125302        p->zDb, p->zName, zContentCols
 125304     sqlite3_free(zContentCols);
 125307   /* Create other tables */
 125308   fts3DbExec(&rc, db, 
 125309       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
 125310       p->zDb, p->zName
 125312   fts3DbExec(&rc, db, 
 125313       "CREATE TABLE %Q.'%q_segdir'("
 125314         "level INTEGER,"
 125315         "idx INTEGER,"
 125316         "start_block INTEGER,"
 125317         "leaves_end_block INTEGER,"
 125318         "end_block INTEGER,"
 125319         "root BLOB,"
 125320         "PRIMARY KEY(level, idx)"
 125321       ");",
 125322       p->zDb, p->zName
 125324   if( p->bHasDocsize ){
 125325     fts3DbExec(&rc, db, 
 125326         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
 125327         p->zDb, p->zName
 125330   assert( p->bHasStat==p->bFts4 );
 125331   if( p->bHasStat ){
 125332     sqlite3Fts3CreateStatTable(&rc, p);
 125334   return rc;
 125338 ** Store the current database page-size in bytes in p->nPgsz.
 125340 ** If *pRc is non-zero when this function is called, it is a no-op. 
 125341 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
 125342 ** before returning.
 125344 static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
 125345   if( *pRc==SQLITE_OK ){
 125346     int rc;                       /* Return code */
 125347     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
 125348     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
 125350     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
 125351     if( !zSql ){
 125352       rc = SQLITE_NOMEM;
 125353     }else{
 125354       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
 125355       if( rc==SQLITE_OK ){
 125356         sqlite3_step(pStmt);
 125357         p->nPgsz = sqlite3_column_int(pStmt, 0);
 125358         rc = sqlite3_finalize(pStmt);
 125359       }else if( rc==SQLITE_AUTH ){
 125360         p->nPgsz = 1024;
 125361         rc = SQLITE_OK;
 125364     assert( p->nPgsz>0 || rc!=SQLITE_OK );
 125365     sqlite3_free(zSql);
 125366     *pRc = rc;
 125371 ** "Special" FTS4 arguments are column specifications of the following form:
 125373 **   <key> = <value>
 125375 ** There may not be whitespace surrounding the "=" character. The <value> 
 125376 ** term may be quoted, but the <key> may not.
 125378 static int fts3IsSpecialColumn(
 125379   const char *z, 
 125380   int *pnKey,
 125381   char **pzValue
 125383   char *zValue;
 125384   const char *zCsr = z;
 125386   while( *zCsr!='=' ){
 125387     if( *zCsr=='\0' ) return 0;
 125388     zCsr++;
 125391   *pnKey = (int)(zCsr-z);
 125392   zValue = sqlite3_mprintf("%s", &zCsr[1]);
 125393   if( zValue ){
 125394     sqlite3Fts3Dequote(zValue);
 125396   *pzValue = zValue;
 125397   return 1;
 125401 ** Append the output of a printf() style formatting to an existing string.
 125403 static void fts3Appendf(
 125404   int *pRc,                       /* IN/OUT: Error code */
 125405   char **pz,                      /* IN/OUT: Pointer to string buffer */
 125406   const char *zFormat,            /* Printf format string to append */
 125407   ...                             /* Arguments for printf format string */
 125409   if( *pRc==SQLITE_OK ){
 125410     va_list ap;
 125411     char *z;
 125412     va_start(ap, zFormat);
 125413     z = sqlite3_vmprintf(zFormat, ap);
 125414     va_end(ap);
 125415     if( z && *pz ){
 125416       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
 125417       sqlite3_free(z);
 125418       z = z2;
 125420     if( z==0 ) *pRc = SQLITE_NOMEM;
 125421     sqlite3_free(*pz);
 125422     *pz = z;
 125427 ** Return a copy of input string zInput enclosed in double-quotes (") and
 125428 ** with all double quote characters escaped. For example:
 125430 **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
 125432 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
 125433 ** is the callers responsibility to call sqlite3_free() to release this
 125434 ** memory.
 125436 static char *fts3QuoteId(char const *zInput){
 125437   int nRet;
 125438   char *zRet;
 125439   nRet = 2 + (int)strlen(zInput)*2 + 1;
 125440   zRet = sqlite3_malloc(nRet);
 125441   if( zRet ){
 125442     int i;
 125443     char *z = zRet;
 125444     *(z++) = '"';
 125445     for(i=0; zInput[i]; i++){
 125446       if( zInput[i]=='"' ) *(z++) = '"';
 125447       *(z++) = zInput[i];
 125449     *(z++) = '"';
 125450     *(z++) = '\0';
 125452   return zRet;
 125456 ** Return a list of comma separated SQL expressions and a FROM clause that 
 125457 ** could be used in a SELECT statement such as the following:
 125459 **     SELECT <list of expressions> FROM %_content AS x ...
 125461 ** to return the docid, followed by each column of text data in order
 125462 ** from left to write. If parameter zFunc is not NULL, then instead of
 125463 ** being returned directly each column of text data is passed to an SQL
 125464 ** function named zFunc first. For example, if zFunc is "unzip" and the
 125465 ** table has the three user-defined columns "a", "b", and "c", the following
 125466 ** string is returned:
 125468 **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
 125470 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
 125471 ** is the responsibility of the caller to eventually free it.
 125473 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
 125474 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
 125475 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
 125476 ** no error occurs, *pRc is left unmodified.
 125478 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
 125479   char *zRet = 0;
 125480   char *zFree = 0;
 125481   char *zFunction;
 125482   int i;
 125484   if( p->zContentTbl==0 ){
 125485     if( !zFunc ){
 125486       zFunction = "";
 125487     }else{
 125488       zFree = zFunction = fts3QuoteId(zFunc);
 125490     fts3Appendf(pRc, &zRet, "docid");
 125491     for(i=0; i<p->nColumn; i++){
 125492       fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
 125494     if( p->zLanguageid ){
 125495       fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
 125497     sqlite3_free(zFree);
 125498   }else{
 125499     fts3Appendf(pRc, &zRet, "rowid");
 125500     for(i=0; i<p->nColumn; i++){
 125501       fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
 125503     if( p->zLanguageid ){
 125504       fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
 125507   fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x", 
 125508       p->zDb,
 125509       (p->zContentTbl ? p->zContentTbl : p->zName),
 125510       (p->zContentTbl ? "" : "_content")
 125512   return zRet;
 125516 ** Return a list of N comma separated question marks, where N is the number
 125517 ** of columns in the %_content table (one for the docid plus one for each
 125518 ** user-defined text column).
 125520 ** If argument zFunc is not NULL, then all but the first question mark
 125521 ** is preceded by zFunc and an open bracket, and followed by a closed
 125522 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three 
 125523 ** user-defined text columns, the following string is returned:
 125525 **     "?, zip(?), zip(?), zip(?)"
 125527 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
 125528 ** is the responsibility of the caller to eventually free it.
 125530 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
 125531 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
 125532 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
 125533 ** no error occurs, *pRc is left unmodified.
 125535 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
 125536   char *zRet = 0;
 125537   char *zFree = 0;
 125538   char *zFunction;
 125539   int i;
 125541   if( !zFunc ){
 125542     zFunction = "";
 125543   }else{
 125544     zFree = zFunction = fts3QuoteId(zFunc);
 125546   fts3Appendf(pRc, &zRet, "?");
 125547   for(i=0; i<p->nColumn; i++){
 125548     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
 125550   if( p->zLanguageid ){
 125551     fts3Appendf(pRc, &zRet, ", ?");
 125553   sqlite3_free(zFree);
 125554   return zRet;
 125558 ** This function interprets the string at (*pp) as a non-negative integer
 125559 ** value. It reads the integer and sets *pnOut to the value read, then 
 125560 ** sets *pp to point to the byte immediately following the last byte of
 125561 ** the integer value.
 125563 ** Only decimal digits ('0'..'9') may be part of an integer value. 
 125565 ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
 125566 ** the output value undefined. Otherwise SQLITE_OK is returned.
 125568 ** This function is used when parsing the "prefix=" FTS4 parameter.
 125570 static int fts3GobbleInt(const char **pp, int *pnOut){
 125571   const char *p;                  /* Iterator pointer */
 125572   int nInt = 0;                   /* Output value */
 125574   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
 125575     nInt = nInt * 10 + (p[0] - '0');
 125577   if( p==*pp ) return SQLITE_ERROR;
 125578   *pnOut = nInt;
 125579   *pp = p;
 125580   return SQLITE_OK;
 125584 ** This function is called to allocate an array of Fts3Index structures
 125585 ** representing the indexes maintained by the current FTS table. FTS tables
 125586 ** always maintain the main "terms" index, but may also maintain one or
 125587 ** more "prefix" indexes, depending on the value of the "prefix=" parameter
 125588 ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
 125590 ** Argument zParam is passed the value of the "prefix=" option if one was
 125591 ** specified, or NULL otherwise.
 125593 ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
 125594 ** the allocated array. *pnIndex is set to the number of elements in the
 125595 ** array. If an error does occur, an SQLite error code is returned.
 125597 ** Regardless of whether or not an error is returned, it is the responsibility
 125598 ** of the caller to call sqlite3_free() on the output array to free it.
 125600 static int fts3PrefixParameter(
 125601   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
 125602   int *pnIndex,                   /* OUT: size of *apIndex[] array */
 125603   struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
 125605   struct Fts3Index *aIndex;       /* Allocated array */
 125606   int nIndex = 1;                 /* Number of entries in array */
 125608   if( zParam && zParam[0] ){
 125609     const char *p;
 125610     nIndex++;
 125611     for(p=zParam; *p; p++){
 125612       if( *p==',' ) nIndex++;
 125616   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
 125617   *apIndex = aIndex;
 125618   *pnIndex = nIndex;
 125619   if( !aIndex ){
 125620     return SQLITE_NOMEM;
 125623   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
 125624   if( zParam ){
 125625     const char *p = zParam;
 125626     int i;
 125627     for(i=1; i<nIndex; i++){
 125628       int nPrefix;
 125629       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
 125630       aIndex[i].nPrefix = nPrefix;
 125631       p++;
 125635   return SQLITE_OK;
 125639 ** This function is called when initializing an FTS4 table that uses the
 125640 ** content=xxx option. It determines the number of and names of the columns
 125641 ** of the new FTS4 table.
 125643 ** The third argument passed to this function is the value passed to the
 125644 ** config=xxx option (i.e. "xxx"). This function queries the database for
 125645 ** a table of that name. If found, the output variables are populated
 125646 ** as follows:
 125648 **   *pnCol:   Set to the number of columns table xxx has,
 125650 **   *pnStr:   Set to the total amount of space required to store a copy
 125651 **             of each columns name, including the nul-terminator.
 125653 **   *pazCol:  Set to point to an array of *pnCol strings. Each string is
 125654 **             the name of the corresponding column in table xxx. The array
 125655 **             and its contents are allocated using a single allocation. It
 125656 **             is the responsibility of the caller to free this allocation
 125657 **             by eventually passing the *pazCol value to sqlite3_free().
 125659 ** If the table cannot be found, an error code is returned and the output
 125660 ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
 125661 ** returned (and the output variables are undefined).
 125663 static int fts3ContentColumns(
 125664   sqlite3 *db,                    /* Database handle */
 125665   const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
 125666   const char *zTbl,               /* Name of content table */
 125667   const char ***pazCol,           /* OUT: Malloc'd array of column names */
 125668   int *pnCol,                     /* OUT: Size of array *pazCol */
 125669   int *pnStr                      /* OUT: Bytes of string content */
 125671   int rc = SQLITE_OK;             /* Return code */
 125672   char *zSql;                     /* "SELECT *" statement on zTbl */  
 125673   sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
 125675   zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
 125676   if( !zSql ){
 125677     rc = SQLITE_NOMEM;
 125678   }else{
 125679     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
 125681   sqlite3_free(zSql);
 125683   if( rc==SQLITE_OK ){
 125684     const char **azCol;           /* Output array */
 125685     int nStr = 0;                 /* Size of all column names (incl. 0x00) */
 125686     int nCol;                     /* Number of table columns */
 125687     int i;                        /* Used to iterate through columns */
 125689     /* Loop through the returned columns. Set nStr to the number of bytes of
 125690     ** space required to store a copy of each column name, including the
 125691     ** nul-terminator byte.  */
 125692     nCol = sqlite3_column_count(pStmt);
 125693     for(i=0; i<nCol; i++){
 125694       const char *zCol = sqlite3_column_name(pStmt, i);
 125695       nStr += (int)strlen(zCol) + 1;
 125698     /* Allocate and populate the array to return. */
 125699     azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
 125700     if( azCol==0 ){
 125701       rc = SQLITE_NOMEM;
 125702     }else{
 125703       char *p = (char *)&azCol[nCol];
 125704       for(i=0; i<nCol; i++){
 125705         const char *zCol = sqlite3_column_name(pStmt, i);
 125706         int n = (int)strlen(zCol)+1;
 125707         memcpy(p, zCol, n);
 125708         azCol[i] = p;
 125709         p += n;
 125712     sqlite3_finalize(pStmt);
 125714     /* Set the output variables. */
 125715     *pnCol = nCol;
 125716     *pnStr = nStr;
 125717     *pazCol = azCol;
 125720   return rc;
 125724 ** This function is the implementation of both the xConnect and xCreate
 125725 ** methods of the FTS3 virtual table.
 125727 ** The argv[] array contains the following:
 125729 **   argv[0]   -> module name  ("fts3" or "fts4")
 125730 **   argv[1]   -> database name
 125731 **   argv[2]   -> table name
 125732 **   argv[...] -> "column name" and other module argument fields.
 125734 static int fts3InitVtab(
 125735   int isCreate,                   /* True for xCreate, false for xConnect */
 125736   sqlite3 *db,                    /* The SQLite database connection */
 125737   void *pAux,                     /* Hash table containing tokenizers */
 125738   int argc,                       /* Number of elements in argv array */
 125739   const char * const *argv,       /* xCreate/xConnect argument array */
 125740   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
 125741   char **pzErr                    /* Write any error message here */
 125743   Fts3Hash *pHash = (Fts3Hash *)pAux;
 125744   Fts3Table *p = 0;               /* Pointer to allocated vtab */
 125745   int rc = SQLITE_OK;             /* Return code */
 125746   int i;                          /* Iterator variable */
 125747   int nByte;                      /* Size of allocation used for *p */
 125748   int iCol;                       /* Column index */
 125749   int nString = 0;                /* Bytes required to hold all column names */
 125750   int nCol = 0;                   /* Number of columns in the FTS table */
 125751   char *zCsr;                     /* Space for holding column names */
 125752   int nDb;                        /* Bytes required to hold database name */
 125753   int nName;                      /* Bytes required to hold table name */
 125754   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
 125755   const char **aCol;              /* Array of column names */
 125756   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
 125758   int nIndex;                     /* Size of aIndex[] array */
 125759   struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
 125761   /* The results of parsing supported FTS4 key=value options: */
 125762   int bNoDocsize = 0;             /* True to omit %_docsize table */
 125763   int bDescIdx = 0;               /* True to store descending indexes */
 125764   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
 125765   char *zCompress = 0;            /* compress=? parameter (or NULL) */
 125766   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
 125767   char *zContent = 0;             /* content=? parameter (or NULL) */
 125768   char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
 125769   char **azNotindexed = 0;        /* The set of notindexed= columns */
 125770   int nNotindexed = 0;            /* Size of azNotindexed[] array */
 125772   assert( strlen(argv[0])==4 );
 125773   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
 125774        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
 125777   nDb = (int)strlen(argv[1]) + 1;
 125778   nName = (int)strlen(argv[2]) + 1;
 125780   nByte = sizeof(const char *) * (argc-2);
 125781   aCol = (const char **)sqlite3_malloc(nByte);
 125782   if( aCol ){
 125783     memset((void*)aCol, 0, nByte);
 125784     azNotindexed = (char **)sqlite3_malloc(nByte);
 125786   if( azNotindexed ){
 125787     memset(azNotindexed, 0, nByte);
 125789   if( !aCol || !azNotindexed ){
 125790     rc = SQLITE_NOMEM;
 125791     goto fts3_init_out;
 125794   /* Loop through all of the arguments passed by the user to the FTS3/4
 125795   ** module (i.e. all the column names and special arguments). This loop
 125796   ** does the following:
 125798   **   + Figures out the number of columns the FTSX table will have, and
 125799   **     the number of bytes of space that must be allocated to store copies
 125800   **     of the column names.
 125802   **   + If there is a tokenizer specification included in the arguments,
 125803   **     initializes the tokenizer pTokenizer.
 125805   for(i=3; rc==SQLITE_OK && i<argc; i++){
 125806     char const *z = argv[i];
 125807     int nKey;
 125808     char *zVal;
 125810     /* Check if this is a tokenizer specification */
 125811     if( !pTokenizer 
 125812      && strlen(z)>8
 125813      && 0==sqlite3_strnicmp(z, "tokenize", 8) 
 125814      && 0==sqlite3Fts3IsIdChar(z[8])
 125816       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
 125819     /* Check if it is an FTS4 special argument. */
 125820     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
 125821       struct Fts4Option {
 125822         const char *zOpt;
 125823         int nOpt;
 125824       } aFts4Opt[] = {
 125825         { "matchinfo",   9 },     /* 0 -> MATCHINFO */
 125826         { "prefix",      6 },     /* 1 -> PREFIX */
 125827         { "compress",    8 },     /* 2 -> COMPRESS */
 125828         { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
 125829         { "order",       5 },     /* 4 -> ORDER */
 125830         { "content",     7 },     /* 5 -> CONTENT */
 125831         { "languageid", 10 },     /* 6 -> LANGUAGEID */
 125832         { "notindexed", 10 }      /* 7 -> NOTINDEXED */
 125835       int iOpt;
 125836       if( !zVal ){
 125837         rc = SQLITE_NOMEM;
 125838       }else{
 125839         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
 125840           struct Fts4Option *pOp = &aFts4Opt[iOpt];
 125841           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
 125842             break;
 125845         if( iOpt==SizeofArray(aFts4Opt) ){
 125846           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
 125847           rc = SQLITE_ERROR;
 125848         }else{
 125849           switch( iOpt ){
 125850             case 0:               /* MATCHINFO */
 125851               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
 125852                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
 125853                 rc = SQLITE_ERROR;
 125855               bNoDocsize = 1;
 125856               break;
 125858             case 1:               /* PREFIX */
 125859               sqlite3_free(zPrefix);
 125860               zPrefix = zVal;
 125861               zVal = 0;
 125862               break;
 125864             case 2:               /* COMPRESS */
 125865               sqlite3_free(zCompress);
 125866               zCompress = zVal;
 125867               zVal = 0;
 125868               break;
 125870             case 3:               /* UNCOMPRESS */
 125871               sqlite3_free(zUncompress);
 125872               zUncompress = zVal;
 125873               zVal = 0;
 125874               break;
 125876             case 4:               /* ORDER */
 125877               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
 125878                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
 125880                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
 125881                 rc = SQLITE_ERROR;
 125883               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
 125884               break;
 125886             case 5:              /* CONTENT */
 125887               sqlite3_free(zContent);
 125888               zContent = zVal;
 125889               zVal = 0;
 125890               break;
 125892             case 6:              /* LANGUAGEID */
 125893               assert( iOpt==6 );
 125894               sqlite3_free(zLanguageid);
 125895               zLanguageid = zVal;
 125896               zVal = 0;
 125897               break;
 125899             case 7:              /* NOTINDEXED */
 125900               azNotindexed[nNotindexed++] = zVal;
 125901               zVal = 0;
 125902               break;
 125905         sqlite3_free(zVal);
 125909     /* Otherwise, the argument is a column name. */
 125910     else {
 125911       nString += (int)(strlen(z) + 1);
 125912       aCol[nCol++] = z;
 125916   /* If a content=xxx option was specified, the following:
 125918   **   1. Ignore any compress= and uncompress= options.
 125920   **   2. If no column names were specified as part of the CREATE VIRTUAL
 125921   **      TABLE statement, use all columns from the content table.
 125923   if( rc==SQLITE_OK && zContent ){
 125924     sqlite3_free(zCompress); 
 125925     sqlite3_free(zUncompress); 
 125926     zCompress = 0;
 125927     zUncompress = 0;
 125928     if( nCol==0 ){
 125929       sqlite3_free((void*)aCol); 
 125930       aCol = 0;
 125931       rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
 125933       /* If a languageid= option was specified, remove the language id
 125934       ** column from the aCol[] array. */ 
 125935       if( rc==SQLITE_OK && zLanguageid ){
 125936         int j;
 125937         for(j=0; j<nCol; j++){
 125938           if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
 125939             int k;
 125940             for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
 125941             nCol--;
 125942             break;
 125948   if( rc!=SQLITE_OK ) goto fts3_init_out;
 125950   if( nCol==0 ){
 125951     assert( nString==0 );
 125952     aCol[0] = "content";
 125953     nString = 8;
 125954     nCol = 1;
 125957   if( pTokenizer==0 ){
 125958     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
 125959     if( rc!=SQLITE_OK ) goto fts3_init_out;
 125961   assert( pTokenizer );
 125963   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
 125964   if( rc==SQLITE_ERROR ){
 125965     assert( zPrefix );
 125966     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
 125968   if( rc!=SQLITE_OK ) goto fts3_init_out;
 125970   /* Allocate and populate the Fts3Table structure. */
 125971   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
 125972           nCol * sizeof(char *) +              /* azColumn */
 125973           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
 125974           nCol * sizeof(u8) +                  /* abNotindexed */
 125975           nName +                              /* zName */
 125976           nDb +                                /* zDb */
 125977           nString;                             /* Space for azColumn strings */
 125978   p = (Fts3Table*)sqlite3_malloc(nByte);
 125979   if( p==0 ){
 125980     rc = SQLITE_NOMEM;
 125981     goto fts3_init_out;
 125983   memset(p, 0, nByte);
 125984   p->db = db;
 125985   p->nColumn = nCol;
 125986   p->nPendingData = 0;
 125987   p->azColumn = (char **)&p[1];
 125988   p->pTokenizer = pTokenizer;
 125989   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
 125990   p->bHasDocsize = (isFts4 && bNoDocsize==0);
 125991   p->bHasStat = isFts4;
 125992   p->bFts4 = isFts4;
 125993   p->bDescIdx = bDescIdx;
 125994   p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
 125995   p->zContentTbl = zContent;
 125996   p->zLanguageid = zLanguageid;
 125997   zContent = 0;
 125998   zLanguageid = 0;
 125999   TESTONLY( p->inTransaction = -1 );
 126000   TESTONLY( p->mxSavepoint = -1 );
 126002   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
 126003   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
 126004   p->nIndex = nIndex;
 126005   for(i=0; i<nIndex; i++){
 126006     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
 126008   p->abNotindexed = (u8 *)&p->aIndex[nIndex];
 126010   /* Fill in the zName and zDb fields of the vtab structure. */
 126011   zCsr = (char *)&p->abNotindexed[nCol];
 126012   p->zName = zCsr;
 126013   memcpy(zCsr, argv[2], nName);
 126014   zCsr += nName;
 126015   p->zDb = zCsr;
 126016   memcpy(zCsr, argv[1], nDb);
 126017   zCsr += nDb;
 126019   /* Fill in the azColumn array */
 126020   for(iCol=0; iCol<nCol; iCol++){
 126021     char *z; 
 126022     int n = 0;
 126023     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
 126024     memcpy(zCsr, z, n);
 126025     zCsr[n] = '\0';
 126026     sqlite3Fts3Dequote(zCsr);
 126027     p->azColumn[iCol] = zCsr;
 126028     zCsr += n+1;
 126029     assert( zCsr <= &((char *)p)[nByte] );
 126032   /* Fill in the abNotindexed array */
 126033   for(iCol=0; iCol<nCol; iCol++){
 126034     int n = (int)strlen(p->azColumn[iCol]);
 126035     for(i=0; i<nNotindexed; i++){
 126036       char *zNot = azNotindexed[i];
 126037       if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
 126038         p->abNotindexed[iCol] = 1;
 126039         sqlite3_free(zNot);
 126040         azNotindexed[i] = 0;
 126044   for(i=0; i<nNotindexed; i++){
 126045     if( azNotindexed[i] ){
 126046       *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]);
 126047       rc = SQLITE_ERROR;
 126051   if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
 126052     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
 126053     rc = SQLITE_ERROR;
 126054     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
 126056   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
 126057   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
 126058   if( rc!=SQLITE_OK ) goto fts3_init_out;
 126060   /* If this is an xCreate call, create the underlying tables in the 
 126061   ** database. TODO: For xConnect(), it could verify that said tables exist.
 126063   if( isCreate ){
 126064     rc = fts3CreateTables(p);
 126067   /* Check to see if a legacy fts3 table has been "upgraded" by the
 126068   ** addition of a %_stat table so that it can use incremental merge.
 126070   if( !isFts4 && !isCreate ){
 126071     int rc2 = SQLITE_OK;
 126072     fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
 126073                p->zDb, p->zName);
 126074     if( rc2==SQLITE_OK ) p->bHasStat = 1;
 126077   /* Figure out the page-size for the database. This is required in order to
 126078   ** estimate the cost of loading large doclists from the database.  */
 126079   fts3DatabasePageSize(&rc, p);
 126080   p->nNodeSize = p->nPgsz-35;
 126082   /* Declare the table schema to SQLite. */
 126083   fts3DeclareVtab(&rc, p);
 126085 fts3_init_out:
 126086   sqlite3_free(zPrefix);
 126087   sqlite3_free(aIndex);
 126088   sqlite3_free(zCompress);
 126089   sqlite3_free(zUncompress);
 126090   sqlite3_free(zContent);
 126091   sqlite3_free(zLanguageid);
 126092   for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
 126093   sqlite3_free((void *)aCol);
 126094   sqlite3_free((void *)azNotindexed);
 126095   if( rc!=SQLITE_OK ){
 126096     if( p ){
 126097       fts3DisconnectMethod((sqlite3_vtab *)p);
 126098     }else if( pTokenizer ){
 126099       pTokenizer->pModule->xDestroy(pTokenizer);
 126101   }else{
 126102     assert( p->pSegments==0 );
 126103     *ppVTab = &p->base;
 126105   return rc;
 126109 ** The xConnect() and xCreate() methods for the virtual table. All the
 126110 ** work is done in function fts3InitVtab().
 126112 static int fts3ConnectMethod(
 126113   sqlite3 *db,                    /* Database connection */
 126114   void *pAux,                     /* Pointer to tokenizer hash table */
 126115   int argc,                       /* Number of elements in argv array */
 126116   const char * const *argv,       /* xCreate/xConnect argument array */
 126117   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 126118   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 126120   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
 126122 static int fts3CreateMethod(
 126123   sqlite3 *db,                    /* Database connection */
 126124   void *pAux,                     /* Pointer to tokenizer hash table */
 126125   int argc,                       /* Number of elements in argv array */
 126126   const char * const *argv,       /* xCreate/xConnect argument array */
 126127   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 126128   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 126130   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
 126134 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
 126135 ** extension is currently being used by a version of SQLite too old to
 126136 ** support estimatedRows. In that case this function is a no-op.
 126138 static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
 126139 #if SQLITE_VERSION_NUMBER>=3008002
 126140   if( sqlite3_libversion_number()>=3008002 ){
 126141     pIdxInfo->estimatedRows = nRow;
 126143 #endif
 126147 ** Implementation of the xBestIndex method for FTS3 tables. There
 126148 ** are three possible strategies, in order of preference:
 126150 **   1. Direct lookup by rowid or docid. 
 126151 **   2. Full-text search using a MATCH operator on a non-docid column.
 126152 **   3. Linear scan of %_content table.
 126154 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
 126155   Fts3Table *p = (Fts3Table *)pVTab;
 126156   int i;                          /* Iterator variable */
 126157   int iCons = -1;                 /* Index of constraint to use */
 126159   int iLangidCons = -1;           /* Index of langid=x constraint, if present */
 126160   int iDocidGe = -1;              /* Index of docid>=x constraint, if present */
 126161   int iDocidLe = -1;              /* Index of docid<=x constraint, if present */
 126162   int iIdx;
 126164   /* By default use a full table scan. This is an expensive option,
 126165   ** so search through the constraints to see if a more efficient 
 126166   ** strategy is possible.
 126168   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
 126169   pInfo->estimatedCost = 5000000;
 126170   for(i=0; i<pInfo->nConstraint; i++){
 126171     int bDocid;                 /* True if this constraint is on docid */
 126172     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
 126173     if( pCons->usable==0 ){
 126174       if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
 126175         /* There exists an unusable MATCH constraint. This means that if
 126176         ** the planner does elect to use the results of this call as part
 126177         ** of the overall query plan the user will see an "unable to use
 126178         ** function MATCH in the requested context" error. To discourage
 126179         ** this, return a very high cost here.  */
 126180         pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
 126181         pInfo->estimatedCost = 1e50;
 126182         fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
 126183         return SQLITE_OK;
 126185       continue;
 126188     bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
 126190     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
 126191     if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
 126192       pInfo->idxNum = FTS3_DOCID_SEARCH;
 126193       pInfo->estimatedCost = 1.0;
 126194       iCons = i;
 126197     /* A MATCH constraint. Use a full-text search.
 126199     ** If there is more than one MATCH constraint available, use the first
 126200     ** one encountered. If there is both a MATCH constraint and a direct
 126201     ** rowid/docid lookup, prefer the MATCH strategy. This is done even 
 126202     ** though the rowid/docid lookup is faster than a MATCH query, selecting
 126203     ** it would lead to an "unable to use function MATCH in the requested 
 126204     ** context" error.
 126206     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH 
 126207      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
 126209       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
 126210       pInfo->estimatedCost = 2.0;
 126211       iCons = i;
 126214     /* Equality constraint on the langid column */
 126215     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
 126216      && pCons->iColumn==p->nColumn + 2
 126218       iLangidCons = i;
 126221     if( bDocid ){
 126222       switch( pCons->op ){
 126223         case SQLITE_INDEX_CONSTRAINT_GE:
 126224         case SQLITE_INDEX_CONSTRAINT_GT:
 126225           iDocidGe = i;
 126226           break;
 126228         case SQLITE_INDEX_CONSTRAINT_LE:
 126229         case SQLITE_INDEX_CONSTRAINT_LT:
 126230           iDocidLe = i;
 126231           break;
 126236   iIdx = 1;
 126237   if( iCons>=0 ){
 126238     pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
 126239     pInfo->aConstraintUsage[iCons].omit = 1;
 126241   if( iLangidCons>=0 ){
 126242     pInfo->idxNum |= FTS3_HAVE_LANGID;
 126243     pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
 126245   if( iDocidGe>=0 ){
 126246     pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
 126247     pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
 126249   if( iDocidLe>=0 ){
 126250     pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
 126251     pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
 126254   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
 126255   ** docid) order. Both ascending and descending are possible. 
 126257   if( pInfo->nOrderBy==1 ){
 126258     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
 126259     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
 126260       if( pOrder->desc ){
 126261         pInfo->idxStr = "DESC";
 126262       }else{
 126263         pInfo->idxStr = "ASC";
 126265       pInfo->orderByConsumed = 1;
 126269   assert( p->pSegments==0 );
 126270   return SQLITE_OK;
 126274 ** Implementation of xOpen method.
 126276 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
 126277   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
 126279   UNUSED_PARAMETER(pVTab);
 126281   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
 126282   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
 126283   ** if the allocation fails, return SQLITE_NOMEM.
 126285   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
 126286   if( !pCsr ){
 126287     return SQLITE_NOMEM;
 126289   memset(pCsr, 0, sizeof(Fts3Cursor));
 126290   return SQLITE_OK;
 126294 ** Close the cursor.  For additional information see the documentation
 126295 ** on the xClose method of the virtual table interface.
 126297 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
 126298   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 126299   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 126300   sqlite3_finalize(pCsr->pStmt);
 126301   sqlite3Fts3ExprFree(pCsr->pExpr);
 126302   sqlite3Fts3FreeDeferredTokens(pCsr);
 126303   sqlite3_free(pCsr->aDoclist);
 126304   sqlite3_free(pCsr->aMatchinfo);
 126305   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 126306   sqlite3_free(pCsr);
 126307   return SQLITE_OK;
 126311 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
 126312 ** compose and prepare an SQL statement of the form:
 126314 **    "SELECT <columns> FROM %_content WHERE rowid = ?"
 126316 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
 126317 ** it. If an error occurs, return an SQLite error code.
 126319 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
 126321 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
 126322   int rc = SQLITE_OK;
 126323   if( pCsr->pStmt==0 ){
 126324     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 126325     char *zSql;
 126326     zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
 126327     if( !zSql ) return SQLITE_NOMEM;
 126328     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
 126329     sqlite3_free(zSql);
 126331   *ppStmt = pCsr->pStmt;
 126332   return rc;
 126336 ** Position the pCsr->pStmt statement so that it is on the row
 126337 ** of the %_content table that contains the last match.  Return
 126338 ** SQLITE_OK on success.  
 126340 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
 126341   int rc = SQLITE_OK;
 126342   if( pCsr->isRequireSeek ){
 126343     sqlite3_stmt *pStmt = 0;
 126345     rc = fts3CursorSeekStmt(pCsr, &pStmt);
 126346     if( rc==SQLITE_OK ){
 126347       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
 126348       pCsr->isRequireSeek = 0;
 126349       if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
 126350         return SQLITE_OK;
 126351       }else{
 126352         rc = sqlite3_reset(pCsr->pStmt);
 126353         if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
 126354           /* If no row was found and no error has occurred, then the %_content
 126355           ** table is missing a row that is present in the full-text index.
 126356           ** The data structures are corrupt.  */
 126357           rc = FTS_CORRUPT_VTAB;
 126358           pCsr->isEof = 1;
 126364   if( rc!=SQLITE_OK && pContext ){
 126365     sqlite3_result_error_code(pContext, rc);
 126367   return rc;
 126371 ** This function is used to process a single interior node when searching
 126372 ** a b-tree for a term or term prefix. The node data is passed to this 
 126373 ** function via the zNode/nNode parameters. The term to search for is
 126374 ** passed in zTerm/nTerm.
 126376 ** If piFirst is not NULL, then this function sets *piFirst to the blockid
 126377 ** of the child node that heads the sub-tree that may contain the term.
 126379 ** If piLast is not NULL, then *piLast is set to the right-most child node
 126380 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
 126381 ** a prefix.
 126383 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
 126385 static int fts3ScanInteriorNode(
 126386   const char *zTerm,              /* Term to select leaves for */
 126387   int nTerm,                      /* Size of term zTerm in bytes */
 126388   const char *zNode,              /* Buffer containing segment interior node */
 126389   int nNode,                      /* Size of buffer at zNode */
 126390   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
 126391   sqlite3_int64 *piLast           /* OUT: Selected child node */
 126393   int rc = SQLITE_OK;             /* Return code */
 126394   const char *zCsr = zNode;       /* Cursor to iterate through node */
 126395   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
 126396   char *zBuffer = 0;              /* Buffer to load terms into */
 126397   int nAlloc = 0;                 /* Size of allocated buffer */
 126398   int isFirstTerm = 1;            /* True when processing first term on page */
 126399   sqlite3_int64 iChild;           /* Block id of child node to descend to */
 126401   /* Skip over the 'height' varint that occurs at the start of every 
 126402   ** interior node. Then load the blockid of the left-child of the b-tree
 126403   ** node into variable iChild.  
 126405   ** Even if the data structure on disk is corrupted, this (reading two
 126406   ** varints from the buffer) does not risk an overread. If zNode is a
 126407   ** root node, then the buffer comes from a SELECT statement. SQLite does
 126408   ** not make this guarantee explicitly, but in practice there are always
 126409   ** either more than 20 bytes of allocated space following the nNode bytes of
 126410   ** contents, or two zero bytes. Or, if the node is read from the %_segments
 126411   ** table, then there are always 20 bytes of zeroed padding following the
 126412   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
 126414   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
 126415   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
 126416   if( zCsr>zEnd ){
 126417     return FTS_CORRUPT_VTAB;
 126420   while( zCsr<zEnd && (piFirst || piLast) ){
 126421     int cmp;                      /* memcmp() result */
 126422     int nSuffix;                  /* Size of term suffix */
 126423     int nPrefix = 0;              /* Size of term prefix */
 126424     int nBuffer;                  /* Total term size */
 126426     /* Load the next term on the node into zBuffer. Use realloc() to expand
 126427     ** the size of zBuffer if required.  */
 126428     if( !isFirstTerm ){
 126429       zCsr += fts3GetVarint32(zCsr, &nPrefix);
 126431     isFirstTerm = 0;
 126432     zCsr += fts3GetVarint32(zCsr, &nSuffix);
 126434     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
 126435       rc = FTS_CORRUPT_VTAB;
 126436       goto finish_scan;
 126438     if( nPrefix+nSuffix>nAlloc ){
 126439       char *zNew;
 126440       nAlloc = (nPrefix+nSuffix) * 2;
 126441       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
 126442       if( !zNew ){
 126443         rc = SQLITE_NOMEM;
 126444         goto finish_scan;
 126446       zBuffer = zNew;
 126448     assert( zBuffer );
 126449     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
 126450     nBuffer = nPrefix + nSuffix;
 126451     zCsr += nSuffix;
 126453     /* Compare the term we are searching for with the term just loaded from
 126454     ** the interior node. If the specified term is greater than or equal
 126455     ** to the term from the interior node, then all terms on the sub-tree 
 126456     ** headed by node iChild are smaller than zTerm. No need to search 
 126457     ** iChild.
 126459     ** If the interior node term is larger than the specified term, then
 126460     ** the tree headed by iChild may contain the specified term.
 126462     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
 126463     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
 126464       *piFirst = iChild;
 126465       piFirst = 0;
 126468     if( piLast && cmp<0 ){
 126469       *piLast = iChild;
 126470       piLast = 0;
 126473     iChild++;
 126476   if( piFirst ) *piFirst = iChild;
 126477   if( piLast ) *piLast = iChild;
 126479  finish_scan:
 126480   sqlite3_free(zBuffer);
 126481   return rc;
 126486 ** The buffer pointed to by argument zNode (size nNode bytes) contains an
 126487 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
 126488 ** contains a term. This function searches the sub-tree headed by the zNode
 126489 ** node for the range of leaf nodes that may contain the specified term
 126490 ** or terms for which the specified term is a prefix.
 126492 ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the 
 126493 ** left-most leaf node in the tree that may contain the specified term.
 126494 ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
 126495 ** right-most leaf node that may contain a term for which the specified
 126496 ** term is a prefix.
 126498 ** It is possible that the range of returned leaf nodes does not contain 
 126499 ** the specified term or any terms for which it is a prefix. However, if the 
 126500 ** segment does contain any such terms, they are stored within the identified
 126501 ** range. Because this function only inspects interior segment nodes (and
 126502 ** never loads leaf nodes into memory), it is not possible to be sure.
 126504 ** If an error occurs, an error code other than SQLITE_OK is returned.
 126506 static int fts3SelectLeaf(
 126507   Fts3Table *p,                   /* Virtual table handle */
 126508   const char *zTerm,              /* Term to select leaves for */
 126509   int nTerm,                      /* Size of term zTerm in bytes */
 126510   const char *zNode,              /* Buffer containing segment interior node */
 126511   int nNode,                      /* Size of buffer at zNode */
 126512   sqlite3_int64 *piLeaf,          /* Selected leaf node */
 126513   sqlite3_int64 *piLeaf2          /* Selected leaf node */
 126515   int rc;                         /* Return code */
 126516   int iHeight;                    /* Height of this node in tree */
 126518   assert( piLeaf || piLeaf2 );
 126520   fts3GetVarint32(zNode, &iHeight);
 126521   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
 126522   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
 126524   if( rc==SQLITE_OK && iHeight>1 ){
 126525     char *zBlob = 0;              /* Blob read from %_segments table */
 126526     int nBlob;                    /* Size of zBlob in bytes */
 126528     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
 126529       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
 126530       if( rc==SQLITE_OK ){
 126531         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
 126533       sqlite3_free(zBlob);
 126534       piLeaf = 0;
 126535       zBlob = 0;
 126538     if( rc==SQLITE_OK ){
 126539       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
 126541     if( rc==SQLITE_OK ){
 126542       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
 126544     sqlite3_free(zBlob);
 126547   return rc;
 126551 ** This function is used to create delta-encoded serialized lists of FTS3 
 126552 ** varints. Each call to this function appends a single varint to a list.
 126554 static void fts3PutDeltaVarint(
 126555   char **pp,                      /* IN/OUT: Output pointer */
 126556   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
 126557   sqlite3_int64 iVal              /* Write this value to the list */
 126559   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
 126560   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
 126561   *piPrev = iVal;
 126565 ** When this function is called, *ppPoslist is assumed to point to the 
 126566 ** start of a position-list. After it returns, *ppPoslist points to the
 126567 ** first byte after the position-list.
 126569 ** A position list is list of positions (delta encoded) and columns for 
 126570 ** a single document record of a doclist.  So, in other words, this
 126571 ** routine advances *ppPoslist so that it points to the next docid in
 126572 ** the doclist, or to the first byte past the end of the doclist.
 126574 ** If pp is not NULL, then the contents of the position list are copied
 126575 ** to *pp. *pp is set to point to the first byte past the last byte copied
 126576 ** before this function returns.
 126578 static void fts3PoslistCopy(char **pp, char **ppPoslist){
 126579   char *pEnd = *ppPoslist;
 126580   char c = 0;
 126582   /* The end of a position list is marked by a zero encoded as an FTS3 
 126583   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
 126584   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
 126585   ** of some other, multi-byte, value.
 126587   ** The following while-loop moves pEnd to point to the first byte that is not 
 126588   ** immediately preceded by a byte with the 0x80 bit set. Then increments
 126589   ** pEnd once more so that it points to the byte immediately following the
 126590   ** last byte in the position-list.
 126592   while( *pEnd | c ){
 126593     c = *pEnd++ & 0x80;
 126594     testcase( c!=0 && (*pEnd)==0 );
 126596   pEnd++;  /* Advance past the POS_END terminator byte */
 126598   if( pp ){
 126599     int n = (int)(pEnd - *ppPoslist);
 126600     char *p = *pp;
 126601     memcpy(p, *ppPoslist, n);
 126602     p += n;
 126603     *pp = p;
 126605   *ppPoslist = pEnd;
 126609 ** When this function is called, *ppPoslist is assumed to point to the 
 126610 ** start of a column-list. After it returns, *ppPoslist points to the
 126611 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
 126613 ** A column-list is list of delta-encoded positions for a single column
 126614 ** within a single document within a doclist.
 126616 ** The column-list is terminated either by a POS_COLUMN varint (1) or
 126617 ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
 126618 ** the POS_COLUMN or POS_END that terminates the column-list.
 126620 ** If pp is not NULL, then the contents of the column-list are copied
 126621 ** to *pp. *pp is set to point to the first byte past the last byte copied
 126622 ** before this function returns.  The POS_COLUMN or POS_END terminator
 126623 ** is not copied into *pp.
 126625 static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
 126626   char *pEnd = *ppPoslist;
 126627   char c = 0;
 126629   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
 126630   ** not part of a multi-byte varint.
 126632   while( 0xFE & (*pEnd | c) ){
 126633     c = *pEnd++ & 0x80;
 126634     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
 126636   if( pp ){
 126637     int n = (int)(pEnd - *ppPoslist);
 126638     char *p = *pp;
 126639     memcpy(p, *ppPoslist, n);
 126640     p += n;
 126641     *pp = p;
 126643   *ppPoslist = pEnd;
 126647 ** Value used to signify the end of an position-list. This is safe because
 126648 ** it is not possible to have a document with 2^31 terms.
 126650 #define POSITION_LIST_END 0x7fffffff
 126653 ** This function is used to help parse position-lists. When this function is
 126654 ** called, *pp may point to the start of the next varint in the position-list
 126655 ** being parsed, or it may point to 1 byte past the end of the position-list
 126656 ** (in which case **pp will be a terminator bytes POS_END (0) or
 126657 ** (1)).
 126659 ** If *pp points past the end of the current position-list, set *pi to 
 126660 ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
 126661 ** increment the current value of *pi by the value read, and set *pp to
 126662 ** point to the next value before returning.
 126664 ** Before calling this routine *pi must be initialized to the value of
 126665 ** the previous position, or zero if we are reading the first position
 126666 ** in the position-list.  Because positions are delta-encoded, the value
 126667 ** of the previous position is needed in order to compute the value of
 126668 ** the next position.
 126670 static void fts3ReadNextPos(
 126671   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
 126672   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
 126674   if( (**pp)&0xFE ){
 126675     fts3GetDeltaVarint(pp, pi);
 126676     *pi -= 2;
 126677   }else{
 126678     *pi = POSITION_LIST_END;
 126683 ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
 126684 ** the value of iCol encoded as a varint to *pp.   This will start a new
 126685 ** column list.
 126687 ** Set *pp to point to the byte just after the last byte written before 
 126688 ** returning (do not modify it if iCol==0). Return the total number of bytes
 126689 ** written (0 if iCol==0).
 126691 static int fts3PutColNumber(char **pp, int iCol){
 126692   int n = 0;                      /* Number of bytes written */
 126693   if( iCol ){
 126694     char *p = *pp;                /* Output pointer */
 126695     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
 126696     *p = 0x01;
 126697     *pp = &p[n];
 126699   return n;
 126703 ** Compute the union of two position lists.  The output written
 126704 ** into *pp contains all positions of both *pp1 and *pp2 in sorted
 126705 ** order and with any duplicates removed.  All pointers are
 126706 ** updated appropriately.   The caller is responsible for insuring
 126707 ** that there is enough space in *pp to hold the complete output.
 126709 static void fts3PoslistMerge(
 126710   char **pp,                      /* Output buffer */
 126711   char **pp1,                     /* Left input list */
 126712   char **pp2                      /* Right input list */
 126714   char *p = *pp;
 126715   char *p1 = *pp1;
 126716   char *p2 = *pp2;
 126718   while( *p1 || *p2 ){
 126719     int iCol1;         /* The current column index in pp1 */
 126720     int iCol2;         /* The current column index in pp2 */
 126722     if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
 126723     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
 126724     else iCol1 = 0;
 126726     if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
 126727     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
 126728     else iCol2 = 0;
 126730     if( iCol1==iCol2 ){
 126731       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
 126732       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
 126733       sqlite3_int64 iPrev = 0;
 126734       int n = fts3PutColNumber(&p, iCol1);
 126735       p1 += n;
 126736       p2 += n;
 126738       /* At this point, both p1 and p2 point to the start of column-lists
 126739       ** for the same column (the column with index iCol1 and iCol2).
 126740       ** A column-list is a list of non-negative delta-encoded varints, each 
 126741       ** incremented by 2 before being stored. Each list is terminated by a
 126742       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
 126743       ** and writes the results to buffer p. p is left pointing to the byte
 126744       ** after the list written. No terminator (POS_END or POS_COLUMN) is
 126745       ** written to the output.
 126747       fts3GetDeltaVarint(&p1, &i1);
 126748       fts3GetDeltaVarint(&p2, &i2);
 126749       do {
 126750         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); 
 126751         iPrev -= 2;
 126752         if( i1==i2 ){
 126753           fts3ReadNextPos(&p1, &i1);
 126754           fts3ReadNextPos(&p2, &i2);
 126755         }else if( i1<i2 ){
 126756           fts3ReadNextPos(&p1, &i1);
 126757         }else{
 126758           fts3ReadNextPos(&p2, &i2);
 126760       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
 126761     }else if( iCol1<iCol2 ){
 126762       p1 += fts3PutColNumber(&p, iCol1);
 126763       fts3ColumnlistCopy(&p, &p1);
 126764     }else{
 126765       p2 += fts3PutColNumber(&p, iCol2);
 126766       fts3ColumnlistCopy(&p, &p2);
 126770   *p++ = POS_END;
 126771   *pp = p;
 126772   *pp1 = p1 + 1;
 126773   *pp2 = p2 + 1;
 126777 ** This function is used to merge two position lists into one. When it is
 126778 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
 126779 ** the part of a doclist that follows each document id. For example, if a row
 126780 ** contains:
 126782 **     'a b c'|'x y z'|'a b b a'
 126784 ** Then the position list for this row for token 'b' would consist of:
 126786 **     0x02 0x01 0x02 0x03 0x03 0x00
 126788 ** When this function returns, both *pp1 and *pp2 are left pointing to the
 126789 ** byte following the 0x00 terminator of their respective position lists.
 126791 ** If isSaveLeft is 0, an entry is added to the output position list for 
 126792 ** each position in *pp2 for which there exists one or more positions in
 126793 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
 126794 ** when the *pp1 token appears before the *pp2 token, but not more than nToken
 126795 ** slots before it.
 126797 ** e.g. nToken==1 searches for adjacent positions.
 126799 static int fts3PoslistPhraseMerge(
 126800   char **pp,                      /* IN/OUT: Preallocated output buffer */
 126801   int nToken,                     /* Maximum difference in token positions */
 126802   int isSaveLeft,                 /* Save the left position */
 126803   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
 126804   char **pp1,                     /* IN/OUT: Left input list */
 126805   char **pp2                      /* IN/OUT: Right input list */
 126807   char *p = *pp;
 126808   char *p1 = *pp1;
 126809   char *p2 = *pp2;
 126810   int iCol1 = 0;
 126811   int iCol2 = 0;
 126813   /* Never set both isSaveLeft and isExact for the same invocation. */
 126814   assert( isSaveLeft==0 || isExact==0 );
 126816   assert( p!=0 && *p1!=0 && *p2!=0 );
 126817   if( *p1==POS_COLUMN ){ 
 126818     p1++;
 126819     p1 += fts3GetVarint32(p1, &iCol1);
 126821   if( *p2==POS_COLUMN ){ 
 126822     p2++;
 126823     p2 += fts3GetVarint32(p2, &iCol2);
 126826   while( 1 ){
 126827     if( iCol1==iCol2 ){
 126828       char *pSave = p;
 126829       sqlite3_int64 iPrev = 0;
 126830       sqlite3_int64 iPos1 = 0;
 126831       sqlite3_int64 iPos2 = 0;
 126833       if( iCol1 ){
 126834         *p++ = POS_COLUMN;
 126835         p += sqlite3Fts3PutVarint(p, iCol1);
 126838       assert( *p1!=POS_END && *p1!=POS_COLUMN );
 126839       assert( *p2!=POS_END && *p2!=POS_COLUMN );
 126840       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
 126841       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
 126843       while( 1 ){
 126844         if( iPos2==iPos1+nToken 
 126845          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
 126847           sqlite3_int64 iSave;
 126848           iSave = isSaveLeft ? iPos1 : iPos2;
 126849           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
 126850           pSave = 0;
 126851           assert( p );
 126853         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
 126854           if( (*p2&0xFE)==0 ) break;
 126855           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
 126856         }else{
 126857           if( (*p1&0xFE)==0 ) break;
 126858           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
 126862       if( pSave ){
 126863         assert( pp && p );
 126864         p = pSave;
 126867       fts3ColumnlistCopy(0, &p1);
 126868       fts3ColumnlistCopy(0, &p2);
 126869       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
 126870       if( 0==*p1 || 0==*p2 ) break;
 126872       p1++;
 126873       p1 += fts3GetVarint32(p1, &iCol1);
 126874       p2++;
 126875       p2 += fts3GetVarint32(p2, &iCol2);
 126878     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
 126879     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
 126880     ** end of the position list, or the 0x01 that precedes the next 
 126881     ** column-number in the position list. 
 126883     else if( iCol1<iCol2 ){
 126884       fts3ColumnlistCopy(0, &p1);
 126885       if( 0==*p1 ) break;
 126886       p1++;
 126887       p1 += fts3GetVarint32(p1, &iCol1);
 126888     }else{
 126889       fts3ColumnlistCopy(0, &p2);
 126890       if( 0==*p2 ) break;
 126891       p2++;
 126892       p2 += fts3GetVarint32(p2, &iCol2);
 126896   fts3PoslistCopy(0, &p2);
 126897   fts3PoslistCopy(0, &p1);
 126898   *pp1 = p1;
 126899   *pp2 = p2;
 126900   if( *pp==p ){
 126901     return 0;
 126903   *p++ = 0x00;
 126904   *pp = p;
 126905   return 1;
 126909 ** Merge two position-lists as required by the NEAR operator. The argument
 126910 ** position lists correspond to the left and right phrases of an expression 
 126911 ** like:
 126913 **     "phrase 1" NEAR "phrase number 2"
 126915 ** Position list *pp1 corresponds to the left-hand side of the NEAR 
 126916 ** expression and *pp2 to the right. As usual, the indexes in the position 
 126917 ** lists are the offsets of the last token in each phrase (tokens "1" and "2" 
 126918 ** in the example above).
 126920 ** The output position list - written to *pp - is a copy of *pp2 with those
 126921 ** entries that are not sufficiently NEAR entries in *pp1 removed.
 126923 static int fts3PoslistNearMerge(
 126924   char **pp,                      /* Output buffer */
 126925   char *aTmp,                     /* Temporary buffer space */
 126926   int nRight,                     /* Maximum difference in token positions */
 126927   int nLeft,                      /* Maximum difference in token positions */
 126928   char **pp1,                     /* IN/OUT: Left input list */
 126929   char **pp2                      /* IN/OUT: Right input list */
 126931   char *p1 = *pp1;
 126932   char *p2 = *pp2;
 126934   char *pTmp1 = aTmp;
 126935   char *pTmp2;
 126936   char *aTmp2;
 126937   int res = 1;
 126939   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
 126940   aTmp2 = pTmp2 = pTmp1;
 126941   *pp1 = p1;
 126942   *pp2 = p2;
 126943   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
 126944   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
 126945     fts3PoslistMerge(pp, &aTmp, &aTmp2);
 126946   }else if( pTmp1!=aTmp ){
 126947     fts3PoslistCopy(pp, &aTmp);
 126948   }else if( pTmp2!=aTmp2 ){
 126949     fts3PoslistCopy(pp, &aTmp2);
 126950   }else{
 126951     res = 0;
 126954   return res;
 126958 ** An instance of this function is used to merge together the (potentially
 126959 ** large number of) doclists for each term that matches a prefix query.
 126960 ** See function fts3TermSelectMerge() for details.
 126962 typedef struct TermSelect TermSelect;
 126963 struct TermSelect {
 126964   char *aaOutput[16];             /* Malloc'd output buffers */
 126965   int anOutput[16];               /* Size each output buffer in bytes */
 126969 ** This function is used to read a single varint from a buffer. Parameter
 126970 ** pEnd points 1 byte past the end of the buffer. When this function is
 126971 ** called, if *pp points to pEnd or greater, then the end of the buffer
 126972 ** has been reached. In this case *pp is set to 0 and the function returns.
 126974 ** If *pp does not point to or past pEnd, then a single varint is read
 126975 ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
 126977 ** If bDescIdx is false, the value read is added to *pVal before returning.
 126978 ** If it is true, the value read is subtracted from *pVal before this 
 126979 ** function returns.
 126981 static void fts3GetDeltaVarint3(
 126982   char **pp,                      /* IN/OUT: Point to read varint from */
 126983   char *pEnd,                     /* End of buffer */
 126984   int bDescIdx,                   /* True if docids are descending */
 126985   sqlite3_int64 *pVal             /* IN/OUT: Integer value */
 126987   if( *pp>=pEnd ){
 126988     *pp = 0;
 126989   }else{
 126990     sqlite3_int64 iVal;
 126991     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
 126992     if( bDescIdx ){
 126993       *pVal -= iVal;
 126994     }else{
 126995       *pVal += iVal;
 127001 ** This function is used to write a single varint to a buffer. The varint
 127002 ** is written to *pp. Before returning, *pp is set to point 1 byte past the
 127003 ** end of the value written.
 127005 ** If *pbFirst is zero when this function is called, the value written to
 127006 ** the buffer is that of parameter iVal. 
 127008 ** If *pbFirst is non-zero when this function is called, then the value 
 127009 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
 127010 ** (if bDescIdx is non-zero).
 127012 ** Before returning, this function always sets *pbFirst to 1 and *piPrev
 127013 ** to the value of parameter iVal.
 127015 static void fts3PutDeltaVarint3(
 127016   char **pp,                      /* IN/OUT: Output pointer */
 127017   int bDescIdx,                   /* True for descending docids */
 127018   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
 127019   int *pbFirst,                   /* IN/OUT: True after first int written */
 127020   sqlite3_int64 iVal              /* Write this value to the list */
 127022   sqlite3_int64 iWrite;
 127023   if( bDescIdx==0 || *pbFirst==0 ){
 127024     iWrite = iVal - *piPrev;
 127025   }else{
 127026     iWrite = *piPrev - iVal;
 127028   assert( *pbFirst || *piPrev==0 );
 127029   assert( *pbFirst==0 || iWrite>0 );
 127030   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
 127031   *piPrev = iVal;
 127032   *pbFirst = 1;
 127037 ** This macro is used by various functions that merge doclists. The two
 127038 ** arguments are 64-bit docid values. If the value of the stack variable
 127039 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). 
 127040 ** Otherwise, (i2-i1).
 127042 ** Using this makes it easier to write code that can merge doclists that are
 127043 ** sorted in either ascending or descending order.
 127045 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
 127048 ** This function does an "OR" merge of two doclists (output contains all
 127049 ** positions contained in either argument doclist). If the docids in the 
 127050 ** input doclists are sorted in ascending order, parameter bDescDoclist
 127051 ** should be false. If they are sorted in ascending order, it should be
 127052 ** passed a non-zero value.
 127054 ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
 127055 ** containing the output doclist and SQLITE_OK is returned. In this case
 127056 ** *pnOut is set to the number of bytes in the output doclist.
 127058 ** If an error occurs, an SQLite error code is returned. The output values
 127059 ** are undefined in this case.
 127061 static int fts3DoclistOrMerge(
 127062   int bDescDoclist,               /* True if arguments are desc */
 127063   char *a1, int n1,               /* First doclist */
 127064   char *a2, int n2,               /* Second doclist */
 127065   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
 127067   sqlite3_int64 i1 = 0;
 127068   sqlite3_int64 i2 = 0;
 127069   sqlite3_int64 iPrev = 0;
 127070   char *pEnd1 = &a1[n1];
 127071   char *pEnd2 = &a2[n2];
 127072   char *p1 = a1;
 127073   char *p2 = a2;
 127074   char *p;
 127075   char *aOut;
 127076   int bFirstOut = 0;
 127078   *paOut = 0;
 127079   *pnOut = 0;
 127081   /* Allocate space for the output. Both the input and output doclists
 127082   ** are delta encoded. If they are in ascending order (bDescDoclist==0),
 127083   ** then the first docid in each list is simply encoded as a varint. For
 127084   ** each subsequent docid, the varint stored is the difference between the
 127085   ** current and previous docid (a positive number - since the list is in
 127086   ** ascending order).
 127088   ** The first docid written to the output is therefore encoded using the 
 127089   ** same number of bytes as it is in whichever of the input lists it is
 127090   ** read from. And each subsequent docid read from the same input list 
 127091   ** consumes either the same or less bytes as it did in the input (since
 127092   ** the difference between it and the previous value in the output must
 127093   ** be a positive value less than or equal to the delta value read from 
 127094   ** the input list). The same argument applies to all but the first docid
 127095   ** read from the 'other' list. And to the contents of all position lists
 127096   ** that will be copied and merged from the input to the output.
 127098   ** However, if the first docid copied to the output is a negative number,
 127099   ** then the encoding of the first docid from the 'other' input list may
 127100   ** be larger in the output than it was in the input (since the delta value
 127101   ** may be a larger positive integer than the actual docid).
 127103   ** The space required to store the output is therefore the sum of the
 127104   ** sizes of the two inputs, plus enough space for exactly one of the input
 127105   ** docids to grow. 
 127107   ** A symetric argument may be made if the doclists are in descending 
 127108   ** order.
 127110   aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
 127111   if( !aOut ) return SQLITE_NOMEM;
 127113   p = aOut;
 127114   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
 127115   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
 127116   while( p1 || p2 ){
 127117     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
 127119     if( p2 && p1 && iDiff==0 ){
 127120       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 127121       fts3PoslistMerge(&p, &p1, &p2);
 127122       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 127123       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 127124     }else if( !p2 || (p1 && iDiff<0) ){
 127125       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 127126       fts3PoslistCopy(&p, &p1);
 127127       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 127128     }else{
 127129       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
 127130       fts3PoslistCopy(&p, &p2);
 127131       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 127135   *paOut = aOut;
 127136   *pnOut = (int)(p-aOut);
 127137   assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
 127138   return SQLITE_OK;
 127142 ** This function does a "phrase" merge of two doclists. In a phrase merge,
 127143 ** the output contains a copy of each position from the right-hand input
 127144 ** doclist for which there is a position in the left-hand input doclist
 127145 ** exactly nDist tokens before it.
 127147 ** If the docids in the input doclists are sorted in ascending order,
 127148 ** parameter bDescDoclist should be false. If they are sorted in ascending 
 127149 ** order, it should be passed a non-zero value.
 127151 ** The right-hand input doclist is overwritten by this function.
 127153 static void fts3DoclistPhraseMerge(
 127154   int bDescDoclist,               /* True if arguments are desc */
 127155   int nDist,                      /* Distance from left to right (1=adjacent) */
 127156   char *aLeft, int nLeft,         /* Left doclist */
 127157   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
 127159   sqlite3_int64 i1 = 0;
 127160   sqlite3_int64 i2 = 0;
 127161   sqlite3_int64 iPrev = 0;
 127162   char *pEnd1 = &aLeft[nLeft];
 127163   char *pEnd2 = &aRight[*pnRight];
 127164   char *p1 = aLeft;
 127165   char *p2 = aRight;
 127166   char *p;
 127167   int bFirstOut = 0;
 127168   char *aOut = aRight;
 127170   assert( nDist>0 );
 127172   p = aOut;
 127173   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
 127174   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
 127176   while( p1 && p2 ){
 127177     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
 127178     if( iDiff==0 ){
 127179       char *pSave = p;
 127180       sqlite3_int64 iPrevSave = iPrev;
 127181       int bFirstOutSave = bFirstOut;
 127183       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
 127184       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
 127185         p = pSave;
 127186         iPrev = iPrevSave;
 127187         bFirstOut = bFirstOutSave;
 127189       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 127190       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 127191     }else if( iDiff<0 ){
 127192       fts3PoslistCopy(0, &p1);
 127193       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
 127194     }else{
 127195       fts3PoslistCopy(0, &p2);
 127196       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
 127200   *pnRight = (int)(p - aOut);
 127204 ** Argument pList points to a position list nList bytes in size. This
 127205 ** function checks to see if the position list contains any entries for
 127206 ** a token in position 0 (of any column). If so, it writes argument iDelta
 127207 ** to the output buffer pOut, followed by a position list consisting only
 127208 ** of the entries from pList at position 0, and terminated by an 0x00 byte.
 127209 ** The value returned is the number of bytes written to pOut (if any).
 127211 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
 127212   sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
 127213   char *pList,                    /* Position list (no 0x00 term) */
 127214   int nList,                      /* Size of pList in bytes */
 127215   char *pOut                      /* Write output here */
 127217   int nOut = 0;
 127218   int bWritten = 0;               /* True once iDelta has been written */
 127219   char *p = pList;
 127220   char *pEnd = &pList[nList];
 127222   if( *p!=0x01 ){
 127223     if( *p==0x02 ){
 127224       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
 127225       pOut[nOut++] = 0x02;
 127226       bWritten = 1;
 127228     fts3ColumnlistCopy(0, &p);
 127231   while( p<pEnd && *p==0x01 ){
 127232     sqlite3_int64 iCol;
 127233     p++;
 127234     p += sqlite3Fts3GetVarint(p, &iCol);
 127235     if( *p==0x02 ){
 127236       if( bWritten==0 ){
 127237         nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
 127238         bWritten = 1;
 127240       pOut[nOut++] = 0x01;
 127241       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
 127242       pOut[nOut++] = 0x02;
 127244     fts3ColumnlistCopy(0, &p);
 127246   if( bWritten ){
 127247     pOut[nOut++] = 0x00;
 127250   return nOut;
 127255 ** Merge all doclists in the TermSelect.aaOutput[] array into a single
 127256 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
 127257 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
 127259 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
 127260 ** the responsibility of the caller to free any doclists left in the
 127261 ** TermSelect.aaOutput[] array.
 127263 static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
 127264   char *aOut = 0;
 127265   int nOut = 0;
 127266   int i;
 127268   /* Loop through the doclists in the aaOutput[] array. Merge them all
 127269   ** into a single doclist.
 127271   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
 127272     if( pTS->aaOutput[i] ){
 127273       if( !aOut ){
 127274         aOut = pTS->aaOutput[i];
 127275         nOut = pTS->anOutput[i];
 127276         pTS->aaOutput[i] = 0;
 127277       }else{
 127278         int nNew;
 127279         char *aNew;
 127281         int rc = fts3DoclistOrMerge(p->bDescIdx, 
 127282             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
 127284         if( rc!=SQLITE_OK ){
 127285           sqlite3_free(aOut);
 127286           return rc;
 127289         sqlite3_free(pTS->aaOutput[i]);
 127290         sqlite3_free(aOut);
 127291         pTS->aaOutput[i] = 0;
 127292         aOut = aNew;
 127293         nOut = nNew;
 127298   pTS->aaOutput[0] = aOut;
 127299   pTS->anOutput[0] = nOut;
 127300   return SQLITE_OK;
 127304 ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
 127305 ** as the first argument. The merge is an "OR" merge (see function
 127306 ** fts3DoclistOrMerge() for details).
 127308 ** This function is called with the doclist for each term that matches
 127309 ** a queried prefix. It merges all these doclists into one, the doclist
 127310 ** for the specified prefix. Since there can be a very large number of
 127311 ** doclists to merge, the merging is done pair-wise using the TermSelect
 127312 ** object.
 127314 ** This function returns SQLITE_OK if the merge is successful, or an
 127315 ** SQLite error code (SQLITE_NOMEM) if an error occurs.
 127317 static int fts3TermSelectMerge(
 127318   Fts3Table *p,                   /* FTS table handle */
 127319   TermSelect *pTS,                /* TermSelect object to merge into */
 127320   char *aDoclist,                 /* Pointer to doclist */
 127321   int nDoclist                    /* Size of aDoclist in bytes */
 127323   if( pTS->aaOutput[0]==0 ){
 127324     /* If this is the first term selected, copy the doclist to the output
 127325     ** buffer using memcpy(). */
 127326     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
 127327     pTS->anOutput[0] = nDoclist;
 127328     if( pTS->aaOutput[0] ){
 127329       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
 127330     }else{
 127331       return SQLITE_NOMEM;
 127333   }else{
 127334     char *aMerge = aDoclist;
 127335     int nMerge = nDoclist;
 127336     int iOut;
 127338     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
 127339       if( pTS->aaOutput[iOut]==0 ){
 127340         assert( iOut>0 );
 127341         pTS->aaOutput[iOut] = aMerge;
 127342         pTS->anOutput[iOut] = nMerge;
 127343         break;
 127344       }else{
 127345         char *aNew;
 127346         int nNew;
 127348         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, 
 127349             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
 127351         if( rc!=SQLITE_OK ){
 127352           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
 127353           return rc;
 127356         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
 127357         sqlite3_free(pTS->aaOutput[iOut]);
 127358         pTS->aaOutput[iOut] = 0;
 127360         aMerge = aNew;
 127361         nMerge = nNew;
 127362         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
 127363           pTS->aaOutput[iOut] = aMerge;
 127364           pTS->anOutput[iOut] = nMerge;
 127369   return SQLITE_OK;
 127373 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
 127375 static int fts3SegReaderCursorAppend(
 127376   Fts3MultiSegReader *pCsr, 
 127377   Fts3SegReader *pNew
 127379   if( (pCsr->nSegment%16)==0 ){
 127380     Fts3SegReader **apNew;
 127381     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
 127382     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
 127383     if( !apNew ){
 127384       sqlite3Fts3SegReaderFree(pNew);
 127385       return SQLITE_NOMEM;
 127387     pCsr->apSegment = apNew;
 127389   pCsr->apSegment[pCsr->nSegment++] = pNew;
 127390   return SQLITE_OK;
 127394 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
 127395 ** 8th argument.
 127397 ** This function returns SQLITE_OK if successful, or an SQLite error code
 127398 ** otherwise.
 127400 static int fts3SegReaderCursor(
 127401   Fts3Table *p,                   /* FTS3 table handle */
 127402   int iLangid,                    /* Language id */
 127403   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
 127404   int iLevel,                     /* Level of segments to scan */
 127405   const char *zTerm,              /* Term to query for */
 127406   int nTerm,                      /* Size of zTerm in bytes */
 127407   int isPrefix,                   /* True for a prefix search */
 127408   int isScan,                     /* True to scan from zTerm to EOF */
 127409   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
 127411   int rc = SQLITE_OK;             /* Error code */
 127412   sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
 127413   int rc2;                        /* Result of sqlite3_reset() */
 127415   /* If iLevel is less than 0 and this is not a scan, include a seg-reader 
 127416   ** for the pending-terms. If this is a scan, then this call must be being
 127417   ** made by an fts4aux module, not an FTS table. In this case calling
 127418   ** Fts3SegReaderPending might segfault, as the data structures used by 
 127419   ** fts4aux are not completely populated. So it's easiest to filter these
 127420   ** calls out here.  */
 127421   if( iLevel<0 && p->aIndex ){
 127422     Fts3SegReader *pSeg = 0;
 127423     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
 127424     if( rc==SQLITE_OK && pSeg ){
 127425       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
 127429   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
 127430     if( rc==SQLITE_OK ){
 127431       rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
 127434     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
 127435       Fts3SegReader *pSeg = 0;
 127437       /* Read the values returned by the SELECT into local variables. */
 127438       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
 127439       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
 127440       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
 127441       int nRoot = sqlite3_column_bytes(pStmt, 4);
 127442       char const *zRoot = sqlite3_column_blob(pStmt, 4);
 127444       /* If zTerm is not NULL, and this segment is not stored entirely on its
 127445       ** root node, the range of leaves scanned can be reduced. Do this. */
 127446       if( iStartBlock && zTerm ){
 127447         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
 127448         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
 127449         if( rc!=SQLITE_OK ) goto finished;
 127450         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
 127453       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, 
 127454           (isPrefix==0 && isScan==0),
 127455           iStartBlock, iLeavesEndBlock, 
 127456           iEndBlock, zRoot, nRoot, &pSeg
 127458       if( rc!=SQLITE_OK ) goto finished;
 127459       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
 127463  finished:
 127464   rc2 = sqlite3_reset(pStmt);
 127465   if( rc==SQLITE_DONE ) rc = rc2;
 127467   return rc;
 127471 ** Set up a cursor object for iterating through a full-text index or a 
 127472 ** single level therein.
 127474 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
 127475   Fts3Table *p,                   /* FTS3 table handle */
 127476   int iLangid,                    /* Language-id to search */
 127477   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
 127478   int iLevel,                     /* Level of segments to scan */
 127479   const char *zTerm,              /* Term to query for */
 127480   int nTerm,                      /* Size of zTerm in bytes */
 127481   int isPrefix,                   /* True for a prefix search */
 127482   int isScan,                     /* True to scan from zTerm to EOF */
 127483   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
 127485   assert( iIndex>=0 && iIndex<p->nIndex );
 127486   assert( iLevel==FTS3_SEGCURSOR_ALL
 127487       ||  iLevel==FTS3_SEGCURSOR_PENDING 
 127488       ||  iLevel>=0
 127490   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 127491   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
 127492   assert( isPrefix==0 || isScan==0 );
 127494   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
 127495   return fts3SegReaderCursor(
 127496       p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
 127501 ** In addition to its current configuration, have the Fts3MultiSegReader
 127502 ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
 127504 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 127506 static int fts3SegReaderCursorAddZero(
 127507   Fts3Table *p,                   /* FTS virtual table handle */
 127508   int iLangid,
 127509   const char *zTerm,              /* Term to scan doclist of */
 127510   int nTerm,                      /* Number of bytes in zTerm */
 127511   Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
 127513   return fts3SegReaderCursor(p, 
 127514       iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
 127519 ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
 127520 ** if isPrefix is true, to scan the doclist for all terms for which 
 127521 ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
 127522 ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
 127523 ** an SQLite error code.
 127525 ** It is the responsibility of the caller to free this object by eventually
 127526 ** passing it to fts3SegReaderCursorFree() 
 127528 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 127529 ** Output parameter *ppSegcsr is set to 0 if an error occurs.
 127531 static int fts3TermSegReaderCursor(
 127532   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
 127533   const char *zTerm,              /* Term to query for */
 127534   int nTerm,                      /* Size of zTerm in bytes */
 127535   int isPrefix,                   /* True for a prefix search */
 127536   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
 127538   Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
 127539   int rc = SQLITE_NOMEM;          /* Return code */
 127541   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
 127542   if( pSegcsr ){
 127543     int i;
 127544     int bFound = 0;               /* True once an index has been found */
 127545     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 127547     if( isPrefix ){
 127548       for(i=1; bFound==0 && i<p->nIndex; i++){
 127549         if( p->aIndex[i].nPrefix==nTerm ){
 127550           bFound = 1;
 127551           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 127552               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
 127554           pSegcsr->bLookup = 1;
 127558       for(i=1; bFound==0 && i<p->nIndex; i++){
 127559         if( p->aIndex[i].nPrefix==nTerm+1 ){
 127560           bFound = 1;
 127561           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 127562               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
 127564           if( rc==SQLITE_OK ){
 127565             rc = fts3SegReaderCursorAddZero(
 127566                 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
 127573     if( bFound==0 ){
 127574       rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid, 
 127575           0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
 127577       pSegcsr->bLookup = !isPrefix;
 127581   *ppSegcsr = pSegcsr;
 127582   return rc;
 127586 ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
 127588 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
 127589   sqlite3Fts3SegReaderFinish(pSegcsr);
 127590   sqlite3_free(pSegcsr);
 127594 ** This function retrieves the doclist for the specified term (or term
 127595 ** prefix) from the database.
 127597 static int fts3TermSelect(
 127598   Fts3Table *p,                   /* Virtual table handle */
 127599   Fts3PhraseToken *pTok,          /* Token to query for */
 127600   int iColumn,                    /* Column to query (or -ve for all columns) */
 127601   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
 127602   char **ppOut                    /* OUT: Malloced result buffer */
 127604   int rc;                         /* Return code */
 127605   Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
 127606   TermSelect tsc;                 /* Object for pair-wise doclist merging */
 127607   Fts3SegFilter filter;           /* Segment term filter configuration */
 127609   pSegcsr = pTok->pSegcsr;
 127610   memset(&tsc, 0, sizeof(TermSelect));
 127612   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
 127613         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
 127614         | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
 127615         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
 127616   filter.iCol = iColumn;
 127617   filter.zTerm = pTok->z;
 127618   filter.nTerm = pTok->n;
 127620   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
 127621   while( SQLITE_OK==rc
 127622       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) 
 127624     rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
 127627   if( rc==SQLITE_OK ){
 127628     rc = fts3TermSelectFinishMerge(p, &tsc);
 127630   if( rc==SQLITE_OK ){
 127631     *ppOut = tsc.aaOutput[0];
 127632     *pnOut = tsc.anOutput[0];
 127633   }else{
 127634     int i;
 127635     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
 127636       sqlite3_free(tsc.aaOutput[i]);
 127640   fts3SegReaderCursorFree(pSegcsr);
 127641   pTok->pSegcsr = 0;
 127642   return rc;
 127646 ** This function counts the total number of docids in the doclist stored
 127647 ** in buffer aList[], size nList bytes.
 127649 ** If the isPoslist argument is true, then it is assumed that the doclist
 127650 ** contains a position-list following each docid. Otherwise, it is assumed
 127651 ** that the doclist is simply a list of docids stored as delta encoded 
 127652 ** varints.
 127654 static int fts3DoclistCountDocids(char *aList, int nList){
 127655   int nDoc = 0;                   /* Return value */
 127656   if( aList ){
 127657     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
 127658     char *p = aList;              /* Cursor */
 127659     while( p<aEnd ){
 127660       nDoc++;
 127661       while( (*p++)&0x80 );     /* Skip docid varint */
 127662       fts3PoslistCopy(0, &p);   /* Skip over position list */
 127666   return nDoc;
 127670 ** Advance the cursor to the next row in the %_content table that
 127671 ** matches the search criteria.  For a MATCH search, this will be
 127672 ** the next row that matches. For a full-table scan, this will be
 127673 ** simply the next row in the %_content table.  For a docid lookup,
 127674 ** this routine simply sets the EOF flag.
 127676 ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
 127677 ** even if we reach end-of-file.  The fts3EofMethod() will be called
 127678 ** subsequently to determine whether or not an EOF was hit.
 127680 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
 127681   int rc;
 127682   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 127683   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
 127684     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
 127685       pCsr->isEof = 1;
 127686       rc = sqlite3_reset(pCsr->pStmt);
 127687     }else{
 127688       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
 127689       rc = SQLITE_OK;
 127691   }else{
 127692     rc = fts3EvalNext((Fts3Cursor *)pCursor);
 127694   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 127695   return rc;
 127699 ** The following are copied from sqliteInt.h.
 127701 ** Constants for the largest and smallest possible 64-bit signed integers.
 127702 ** These macros are designed to work correctly on both 32-bit and 64-bit
 127703 ** compilers.
 127705 #ifndef SQLITE_AMALGAMATION
 127706 # define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
 127707 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
 127708 #endif
 127711 ** If the numeric type of argument pVal is "integer", then return it
 127712 ** converted to a 64-bit signed integer. Otherwise, return a copy of
 127713 ** the second parameter, iDefault.
 127715 static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
 127716   if( pVal ){
 127717     int eType = sqlite3_value_numeric_type(pVal);
 127718     if( eType==SQLITE_INTEGER ){
 127719       return sqlite3_value_int64(pVal);
 127722   return iDefault;
 127726 ** This is the xFilter interface for the virtual table.  See
 127727 ** the virtual table xFilter method documentation for additional
 127728 ** information.
 127730 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
 127731 ** the %_content table.
 127733 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
 127734 ** in the %_content table.
 127736 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
 127737 ** column on the left-hand side of the MATCH operator is column
 127738 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
 127739 ** side of the MATCH operator.
 127741 static int fts3FilterMethod(
 127742   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
 127743   int idxNum,                     /* Strategy index */
 127744   const char *idxStr,             /* Unused */
 127745   int nVal,                       /* Number of elements in apVal */
 127746   sqlite3_value **apVal           /* Arguments for the indexing scheme */
 127748   int rc;
 127749   char *zSql;                     /* SQL statement used to access %_content */
 127750   int eSearch;
 127751   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
 127752   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
 127754   sqlite3_value *pCons = 0;       /* The MATCH or rowid constraint, if any */
 127755   sqlite3_value *pLangid = 0;     /* The "langid = ?" constraint, if any */
 127756   sqlite3_value *pDocidGe = 0;    /* The "docid >= ?" constraint, if any */
 127757   sqlite3_value *pDocidLe = 0;    /* The "docid <= ?" constraint, if any */
 127758   int iIdx;
 127760   UNUSED_PARAMETER(idxStr);
 127761   UNUSED_PARAMETER(nVal);
 127763   eSearch = (idxNum & 0x0000FFFF);
 127764   assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
 127765   assert( p->pSegments==0 );
 127767   /* Collect arguments into local variables */
 127768   iIdx = 0;
 127769   if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
 127770   if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
 127771   if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
 127772   if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
 127773   assert( iIdx==nVal );
 127775   /* In case the cursor has been used before, clear it now. */
 127776   sqlite3_finalize(pCsr->pStmt);
 127777   sqlite3_free(pCsr->aDoclist);
 127778   sqlite3Fts3ExprFree(pCsr->pExpr);
 127779   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
 127781   /* Set the lower and upper bounds on docids to return */
 127782   pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
 127783   pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
 127785   if( idxStr ){
 127786     pCsr->bDesc = (idxStr[0]=='D');
 127787   }else{
 127788     pCsr->bDesc = p->bDescIdx;
 127790   pCsr->eSearch = (i16)eSearch;
 127792   if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
 127793     int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
 127794     const char *zQuery = (const char *)sqlite3_value_text(pCons);
 127796     if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
 127797       return SQLITE_NOMEM;
 127800     pCsr->iLangid = 0;
 127801     if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
 127803     assert( p->base.zErrMsg==0 );
 127804     rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
 127805         p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr, 
 127806         &p->base.zErrMsg
 127808     if( rc!=SQLITE_OK ){
 127809       return rc;
 127812     rc = fts3EvalStart(pCsr);
 127813     sqlite3Fts3SegmentsClose(p);
 127814     if( rc!=SQLITE_OK ) return rc;
 127815     pCsr->pNextId = pCsr->aDoclist;
 127816     pCsr->iPrevId = 0;
 127819   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
 127820   ** statement loops through all rows of the %_content table. For a
 127821   ** full-text query or docid lookup, the statement retrieves a single
 127822   ** row by docid.
 127824   if( eSearch==FTS3_FULLSCAN_SEARCH ){
 127825     zSql = sqlite3_mprintf(
 127826         "SELECT %s ORDER BY rowid %s",
 127827         p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
 127829     if( zSql ){
 127830       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
 127831       sqlite3_free(zSql);
 127832     }else{
 127833       rc = SQLITE_NOMEM;
 127835   }else if( eSearch==FTS3_DOCID_SEARCH ){
 127836     rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
 127837     if( rc==SQLITE_OK ){
 127838       rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
 127841   if( rc!=SQLITE_OK ) return rc;
 127843   return fts3NextMethod(pCursor);
 127847 ** This is the xEof method of the virtual table. SQLite calls this 
 127848 ** routine to find out if it has reached the end of a result set.
 127850 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
 127851   return ((Fts3Cursor *)pCursor)->isEof;
 127855 ** This is the xRowid method. The SQLite core calls this routine to
 127856 ** retrieve the rowid for the current row of the result set. fts3
 127857 ** exposes %_content.docid as the rowid for the virtual table. The
 127858 ** rowid should be written to *pRowid.
 127860 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
 127861   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
 127862   *pRowid = pCsr->iPrevId;
 127863   return SQLITE_OK;
 127867 ** This is the xColumn method, called by SQLite to request a value from
 127868 ** the row that the supplied cursor currently points to.
 127870 ** If:
 127872 **   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
 127873 **   (iCol == p->nColumn)   -> Magic column with the same name as the table.
 127874 **   (iCol == p->nColumn+1) -> Docid column
 127875 **   (iCol == p->nColumn+2) -> Langid column
 127877 static int fts3ColumnMethod(
 127878   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 127879   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
 127880   int iCol                        /* Index of column to read value from */
 127882   int rc = SQLITE_OK;             /* Return Code */
 127883   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
 127884   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
 127886   /* The column value supplied by SQLite must be in range. */
 127887   assert( iCol>=0 && iCol<=p->nColumn+2 );
 127889   if( iCol==p->nColumn+1 ){
 127890     /* This call is a request for the "docid" column. Since "docid" is an 
 127891     ** alias for "rowid", use the xRowid() method to obtain the value.
 127893     sqlite3_result_int64(pCtx, pCsr->iPrevId);
 127894   }else if( iCol==p->nColumn ){
 127895     /* The extra column whose name is the same as the table.
 127896     ** Return a blob which is a pointer to the cursor.  */
 127897     sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
 127898   }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
 127899     sqlite3_result_int64(pCtx, pCsr->iLangid);
 127900   }else{
 127901     /* The requested column is either a user column (one that contains 
 127902     ** indexed data), or the language-id column.  */
 127903     rc = fts3CursorSeek(0, pCsr);
 127905     if( rc==SQLITE_OK ){
 127906       if( iCol==p->nColumn+2 ){
 127907         int iLangid = 0;
 127908         if( p->zLanguageid ){
 127909           iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
 127911         sqlite3_result_int(pCtx, iLangid);
 127912       }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
 127913         sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
 127918   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
 127919   return rc;
 127923 ** This function is the implementation of the xUpdate callback used by 
 127924 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
 127925 ** inserted, updated or deleted.
 127927 static int fts3UpdateMethod(
 127928   sqlite3_vtab *pVtab,            /* Virtual table handle */
 127929   int nArg,                       /* Size of argument array */
 127930   sqlite3_value **apVal,          /* Array of arguments */
 127931   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
 127933   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
 127937 ** Implementation of xSync() method. Flush the contents of the pending-terms
 127938 ** hash-table to the database.
 127940 static int fts3SyncMethod(sqlite3_vtab *pVtab){
 127942   /* Following an incremental-merge operation, assuming that the input
 127943   ** segments are not completely consumed (the usual case), they are updated
 127944   ** in place to remove the entries that have already been merged. This
 127945   ** involves updating the leaf block that contains the smallest unmerged
 127946   ** entry and each block (if any) between the leaf and the root node. So
 127947   ** if the height of the input segment b-trees is N, and input segments
 127948   ** are merged eight at a time, updating the input segments at the end
 127949   ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
 127950   ** small - often between 0 and 2. So the overhead of the incremental
 127951   ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
 127952   ** dwarfing the actual productive work accomplished, the incremental merge
 127953   ** is only attempted if it will write at least 64 leaf blocks. Hence
 127954   ** nMinMerge.
 127956   ** Of course, updating the input segments also involves deleting a bunch
 127957   ** of blocks from the segments table. But this is not considered overhead
 127958   ** as it would also be required by a crisis-merge that used the same input 
 127959   ** segments.
 127961   const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
 127963   Fts3Table *p = (Fts3Table*)pVtab;
 127964   int rc = sqlite3Fts3PendingTermsFlush(p);
 127966   if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
 127967     int mxLevel = 0;              /* Maximum relative level value in db */
 127968     int A;                        /* Incr-merge parameter A */
 127970     rc = sqlite3Fts3MaxLevel(p, &mxLevel);
 127971     assert( rc==SQLITE_OK || mxLevel==0 );
 127972     A = p->nLeafAdd * mxLevel;
 127973     A += (A/2);
 127974     if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
 127976   sqlite3Fts3SegmentsClose(p);
 127977   return rc;
 127981 ** Implementation of xBegin() method. This is a no-op.
 127983 static int fts3BeginMethod(sqlite3_vtab *pVtab){
 127984   Fts3Table *p = (Fts3Table*)pVtab;
 127985   UNUSED_PARAMETER(pVtab);
 127986   assert( p->pSegments==0 );
 127987   assert( p->nPendingData==0 );
 127988   assert( p->inTransaction!=1 );
 127989   TESTONLY( p->inTransaction = 1 );
 127990   TESTONLY( p->mxSavepoint = -1; );
 127991   p->nLeafAdd = 0;
 127992   return SQLITE_OK;
 127996 ** Implementation of xCommit() method. This is a no-op. The contents of
 127997 ** the pending-terms hash-table have already been flushed into the database
 127998 ** by fts3SyncMethod().
 128000 static int fts3CommitMethod(sqlite3_vtab *pVtab){
 128001   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
 128002   UNUSED_PARAMETER(pVtab);
 128003   assert( p->nPendingData==0 );
 128004   assert( p->inTransaction!=0 );
 128005   assert( p->pSegments==0 );
 128006   TESTONLY( p->inTransaction = 0 );
 128007   TESTONLY( p->mxSavepoint = -1; );
 128008   return SQLITE_OK;
 128012 ** Implementation of xRollback(). Discard the contents of the pending-terms
 128013 ** hash-table. Any changes made to the database are reverted by SQLite.
 128015 static int fts3RollbackMethod(sqlite3_vtab *pVtab){
 128016   Fts3Table *p = (Fts3Table*)pVtab;
 128017   sqlite3Fts3PendingTermsClear(p);
 128018   assert( p->inTransaction!=0 );
 128019   TESTONLY( p->inTransaction = 0 );
 128020   TESTONLY( p->mxSavepoint = -1; );
 128021   return SQLITE_OK;
 128025 ** When called, *ppPoslist must point to the byte immediately following the
 128026 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
 128027 ** moves *ppPoslist so that it instead points to the first byte of the
 128028 ** same position list.
 128030 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
 128031   char *p = &(*ppPoslist)[-2];
 128032   char c = 0;
 128034   while( p>pStart && (c=*p--)==0 );
 128035   while( p>pStart && (*p & 0x80) | c ){ 
 128036     c = *p--; 
 128038   if( p>pStart ){ p = &p[2]; }
 128039   while( *p++&0x80 );
 128040   *ppPoslist = p;
 128044 ** Helper function used by the implementation of the overloaded snippet(),
 128045 ** offsets() and optimize() SQL functions.
 128047 ** If the value passed as the third argument is a blob of size
 128048 ** sizeof(Fts3Cursor*), then the blob contents are copied to the 
 128049 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
 128050 ** message is written to context pContext and SQLITE_ERROR returned. The
 128051 ** string passed via zFunc is used as part of the error message.
 128053 static int fts3FunctionArg(
 128054   sqlite3_context *pContext,      /* SQL function call context */
 128055   const char *zFunc,              /* Function name */
 128056   sqlite3_value *pVal,            /* argv[0] passed to function */
 128057   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
 128059   Fts3Cursor *pRet;
 128060   if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
 128061    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
 128063     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
 128064     sqlite3_result_error(pContext, zErr, -1);
 128065     sqlite3_free(zErr);
 128066     return SQLITE_ERROR;
 128068   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
 128069   *ppCsr = pRet;
 128070   return SQLITE_OK;
 128074 ** Implementation of the snippet() function for FTS3
 128076 static void fts3SnippetFunc(
 128077   sqlite3_context *pContext,      /* SQLite function call context */
 128078   int nVal,                       /* Size of apVal[] array */
 128079   sqlite3_value **apVal           /* Array of arguments */
 128081   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 128082   const char *zStart = "<b>";
 128083   const char *zEnd = "</b>";
 128084   const char *zEllipsis = "<b>...</b>";
 128085   int iCol = -1;
 128086   int nToken = 15;                /* Default number of tokens in snippet */
 128088   /* There must be at least one argument passed to this function (otherwise
 128089   ** the non-overloaded version would have been called instead of this one).
 128091   assert( nVal>=1 );
 128093   if( nVal>6 ){
 128094     sqlite3_result_error(pContext, 
 128095         "wrong number of arguments to function snippet()", -1);
 128096     return;
 128098   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
 128100   switch( nVal ){
 128101     case 6: nToken = sqlite3_value_int(apVal[5]);
 128102     case 5: iCol = sqlite3_value_int(apVal[4]);
 128103     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
 128104     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
 128105     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
 128107   if( !zEllipsis || !zEnd || !zStart ){
 128108     sqlite3_result_error_nomem(pContext);
 128109   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
 128110     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
 128115 ** Implementation of the offsets() function for FTS3
 128117 static void fts3OffsetsFunc(
 128118   sqlite3_context *pContext,      /* SQLite function call context */
 128119   int nVal,                       /* Size of argument array */
 128120   sqlite3_value **apVal           /* Array of arguments */
 128122   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 128124   UNUSED_PARAMETER(nVal);
 128126   assert( nVal==1 );
 128127   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
 128128   assert( pCsr );
 128129   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
 128130     sqlite3Fts3Offsets(pContext, pCsr);
 128135 ** Implementation of the special optimize() function for FTS3. This 
 128136 ** function merges all segments in the database to a single segment.
 128137 ** Example usage is:
 128139 **   SELECT optimize(t) FROM t LIMIT 1;
 128141 ** where 't' is the name of an FTS3 table.
 128143 static void fts3OptimizeFunc(
 128144   sqlite3_context *pContext,      /* SQLite function call context */
 128145   int nVal,                       /* Size of argument array */
 128146   sqlite3_value **apVal           /* Array of arguments */
 128148   int rc;                         /* Return code */
 128149   Fts3Table *p;                   /* Virtual table handle */
 128150   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
 128152   UNUSED_PARAMETER(nVal);
 128154   assert( nVal==1 );
 128155   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
 128156   p = (Fts3Table *)pCursor->base.pVtab;
 128157   assert( p );
 128159   rc = sqlite3Fts3Optimize(p);
 128161   switch( rc ){
 128162     case SQLITE_OK:
 128163       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
 128164       break;
 128165     case SQLITE_DONE:
 128166       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
 128167       break;
 128168     default:
 128169       sqlite3_result_error_code(pContext, rc);
 128170       break;
 128175 ** Implementation of the matchinfo() function for FTS3
 128177 static void fts3MatchinfoFunc(
 128178   sqlite3_context *pContext,      /* SQLite function call context */
 128179   int nVal,                       /* Size of argument array */
 128180   sqlite3_value **apVal           /* Array of arguments */
 128182   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
 128183   assert( nVal==1 || nVal==2 );
 128184   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
 128185     const char *zArg = 0;
 128186     if( nVal>1 ){
 128187       zArg = (const char *)sqlite3_value_text(apVal[1]);
 128189     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
 128194 ** This routine implements the xFindFunction method for the FTS3
 128195 ** virtual table.
 128197 static int fts3FindFunctionMethod(
 128198   sqlite3_vtab *pVtab,            /* Virtual table handle */
 128199   int nArg,                       /* Number of SQL function arguments */
 128200   const char *zName,              /* Name of SQL function */
 128201   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
 128202   void **ppArg                    /* Unused */
 128204   struct Overloaded {
 128205     const char *zName;
 128206     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 128207   } aOverload[] = {
 128208     { "snippet", fts3SnippetFunc },
 128209     { "offsets", fts3OffsetsFunc },
 128210     { "optimize", fts3OptimizeFunc },
 128211     { "matchinfo", fts3MatchinfoFunc },
 128213   int i;                          /* Iterator variable */
 128215   UNUSED_PARAMETER(pVtab);
 128216   UNUSED_PARAMETER(nArg);
 128217   UNUSED_PARAMETER(ppArg);
 128219   for(i=0; i<SizeofArray(aOverload); i++){
 128220     if( strcmp(zName, aOverload[i].zName)==0 ){
 128221       *pxFunc = aOverload[i].xFunc;
 128222       return 1;
 128226   /* No function of the specified name was found. Return 0. */
 128227   return 0;
 128231 ** Implementation of FTS3 xRename method. Rename an fts3 table.
 128233 static int fts3RenameMethod(
 128234   sqlite3_vtab *pVtab,            /* Virtual table handle */
 128235   const char *zName               /* New name of table */
 128237   Fts3Table *p = (Fts3Table *)pVtab;
 128238   sqlite3 *db = p->db;            /* Database connection */
 128239   int rc;                         /* Return Code */
 128241   /* As it happens, the pending terms table is always empty here. This is
 128242   ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
 128243   ** always opens a savepoint transaction. And the xSavepoint() method 
 128244   ** flushes the pending terms table. But leave the (no-op) call to
 128245   ** PendingTermsFlush() in in case that changes.
 128247   assert( p->nPendingData==0 );
 128248   rc = sqlite3Fts3PendingTermsFlush(p);
 128250   if( p->zContentTbl==0 ){
 128251     fts3DbExec(&rc, db,
 128252       "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
 128253       p->zDb, p->zName, zName
 128257   if( p->bHasDocsize ){
 128258     fts3DbExec(&rc, db,
 128259       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
 128260       p->zDb, p->zName, zName
 128263   if( p->bHasStat ){
 128264     fts3DbExec(&rc, db,
 128265       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
 128266       p->zDb, p->zName, zName
 128269   fts3DbExec(&rc, db,
 128270     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
 128271     p->zDb, p->zName, zName
 128273   fts3DbExec(&rc, db,
 128274     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
 128275     p->zDb, p->zName, zName
 128277   return rc;
 128281 ** The xSavepoint() method.
 128283 ** Flush the contents of the pending-terms table to disk.
 128285 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
 128286   int rc = SQLITE_OK;
 128287   UNUSED_PARAMETER(iSavepoint);
 128288   assert( ((Fts3Table *)pVtab)->inTransaction );
 128289   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
 128290   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
 128291   if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
 128292     rc = fts3SyncMethod(pVtab);
 128294   return rc;
 128298 ** The xRelease() method.
 128300 ** This is a no-op.
 128302 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
 128303   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
 128304   UNUSED_PARAMETER(iSavepoint);
 128305   UNUSED_PARAMETER(pVtab);
 128306   assert( p->inTransaction );
 128307   assert( p->mxSavepoint >= iSavepoint );
 128308   TESTONLY( p->mxSavepoint = iSavepoint-1 );
 128309   return SQLITE_OK;
 128313 ** The xRollbackTo() method.
 128315 ** Discard the contents of the pending terms table.
 128317 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
 128318   Fts3Table *p = (Fts3Table*)pVtab;
 128319   UNUSED_PARAMETER(iSavepoint);
 128320   assert( p->inTransaction );
 128321   assert( p->mxSavepoint >= iSavepoint );
 128322   TESTONLY( p->mxSavepoint = iSavepoint );
 128323   sqlite3Fts3PendingTermsClear(p);
 128324   return SQLITE_OK;
 128327 static const sqlite3_module fts3Module = {
 128328   /* iVersion      */ 2,
 128329   /* xCreate       */ fts3CreateMethod,
 128330   /* xConnect      */ fts3ConnectMethod,
 128331   /* xBestIndex    */ fts3BestIndexMethod,
 128332   /* xDisconnect   */ fts3DisconnectMethod,
 128333   /* xDestroy      */ fts3DestroyMethod,
 128334   /* xOpen         */ fts3OpenMethod,
 128335   /* xClose        */ fts3CloseMethod,
 128336   /* xFilter       */ fts3FilterMethod,
 128337   /* xNext         */ fts3NextMethod,
 128338   /* xEof          */ fts3EofMethod,
 128339   /* xColumn       */ fts3ColumnMethod,
 128340   /* xRowid        */ fts3RowidMethod,
 128341   /* xUpdate       */ fts3UpdateMethod,
 128342   /* xBegin        */ fts3BeginMethod,
 128343   /* xSync         */ fts3SyncMethod,
 128344   /* xCommit       */ fts3CommitMethod,
 128345   /* xRollback     */ fts3RollbackMethod,
 128346   /* xFindFunction */ fts3FindFunctionMethod,
 128347   /* xRename */       fts3RenameMethod,
 128348   /* xSavepoint    */ fts3SavepointMethod,
 128349   /* xRelease      */ fts3ReleaseMethod,
 128350   /* xRollbackTo   */ fts3RollbackToMethod,
 128354 ** This function is registered as the module destructor (called when an
 128355 ** FTS3 enabled database connection is closed). It frees the memory
 128356 ** allocated for the tokenizer hash table.
 128358 static void hashDestroy(void *p){
 128359   Fts3Hash *pHash = (Fts3Hash *)p;
 128360   sqlite3Fts3HashClear(pHash);
 128361   sqlite3_free(pHash);
 128365 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are 
 128366 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
 128367 ** respectively. The following three forward declarations are for functions
 128368 ** declared in these files used to retrieve the respective implementations.
 128370 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
 128371 ** to by the argument to point to the "simple" tokenizer implementation.
 128372 ** And so on.
 128374 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 128375 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 128376 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 128377 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
 128378 #endif
 128379 #ifdef SQLITE_ENABLE_ICU
 128380 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 128381 #endif
 128384 ** Initialize the fts3 extension. If this extension is built as part
 128385 ** of the sqlite library, then this function is called directly by
 128386 ** SQLite. If fts3 is built as a dynamically loadable extension, this
 128387 ** function is called by the sqlite3_extension_init() entry point.
 128389 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
 128390   int rc = SQLITE_OK;
 128391   Fts3Hash *pHash = 0;
 128392   const sqlite3_tokenizer_module *pSimple = 0;
 128393   const sqlite3_tokenizer_module *pPorter = 0;
 128394 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 128395   const sqlite3_tokenizer_module *pUnicode = 0;
 128396 #endif
 128398 #ifdef SQLITE_ENABLE_ICU
 128399   const sqlite3_tokenizer_module *pIcu = 0;
 128400   sqlite3Fts3IcuTokenizerModule(&pIcu);
 128401 #endif
 128403 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 128404   sqlite3Fts3UnicodeTokenizer(&pUnicode);
 128405 #endif
 128407 #ifdef SQLITE_TEST
 128408   rc = sqlite3Fts3InitTerm(db);
 128409   if( rc!=SQLITE_OK ) return rc;
 128410 #endif
 128412   rc = sqlite3Fts3InitAux(db);
 128413   if( rc!=SQLITE_OK ) return rc;
 128415   sqlite3Fts3SimpleTokenizerModule(&pSimple);
 128416   sqlite3Fts3PorterTokenizerModule(&pPorter);
 128418   /* Allocate and initialize the hash-table used to store tokenizers. */
 128419   pHash = sqlite3_malloc(sizeof(Fts3Hash));
 128420   if( !pHash ){
 128421     rc = SQLITE_NOMEM;
 128422   }else{
 128423     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
 128426   /* Load the built-in tokenizers into the hash table */
 128427   if( rc==SQLITE_OK ){
 128428     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
 128429      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
 128431 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 128432      || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) 
 128433 #endif
 128434 #ifdef SQLITE_ENABLE_ICU
 128435      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
 128436 #endif
 128438       rc = SQLITE_NOMEM;
 128442 #ifdef SQLITE_TEST
 128443   if( rc==SQLITE_OK ){
 128444     rc = sqlite3Fts3ExprInitTestInterface(db);
 128446 #endif
 128448   /* Create the virtual table wrapper around the hash-table and overload 
 128449   ** the two scalar functions. If this is successful, register the
 128450   ** module with sqlite.
 128452   if( SQLITE_OK==rc 
 128453    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
 128454    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
 128455    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
 128456    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
 128457    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
 128458    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
 128460     rc = sqlite3_create_module_v2(
 128461         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
 128463     if( rc==SQLITE_OK ){
 128464       rc = sqlite3_create_module_v2(
 128465           db, "fts4", &fts3Module, (void *)pHash, 0
 128468     if( rc==SQLITE_OK ){
 128469       rc = sqlite3Fts3InitTok(db, (void *)pHash);
 128471     return rc;
 128475   /* An error has occurred. Delete the hash table and return the error code. */
 128476   assert( rc!=SQLITE_OK );
 128477   if( pHash ){
 128478     sqlite3Fts3HashClear(pHash);
 128479     sqlite3_free(pHash);
 128481   return rc;
 128485 ** Allocate an Fts3MultiSegReader for each token in the expression headed
 128486 ** by pExpr. 
 128488 ** An Fts3SegReader object is a cursor that can seek or scan a range of
 128489 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
 128490 ** Fts3SegReader objects internally to provide an interface to seek or scan
 128491 ** within the union of all segments of a b-tree. Hence the name.
 128493 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
 128494 ** segment b-tree (if the term is not a prefix or it is a prefix for which
 128495 ** there exists prefix b-tree of the right length) then it may be traversed
 128496 ** and merged incrementally. Otherwise, it has to be merged into an in-memory 
 128497 ** doclist and then traversed.
 128499 static void fts3EvalAllocateReaders(
 128500   Fts3Cursor *pCsr,               /* FTS cursor handle */
 128501   Fts3Expr *pExpr,                /* Allocate readers for this expression */
 128502   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
 128503   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
 128504   int *pRc                        /* IN/OUT: Error code */
 128506   if( pExpr && SQLITE_OK==*pRc ){
 128507     if( pExpr->eType==FTSQUERY_PHRASE ){
 128508       int i;
 128509       int nToken = pExpr->pPhrase->nToken;
 128510       *pnToken += nToken;
 128511       for(i=0; i<nToken; i++){
 128512         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
 128513         int rc = fts3TermSegReaderCursor(pCsr, 
 128514             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
 128516         if( rc!=SQLITE_OK ){
 128517           *pRc = rc;
 128518           return;
 128521       assert( pExpr->pPhrase->iDoclistToken==0 );
 128522       pExpr->pPhrase->iDoclistToken = -1;
 128523     }else{
 128524       *pnOr += (pExpr->eType==FTSQUERY_OR);
 128525       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
 128526       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
 128532 ** Arguments pList/nList contain the doclist for token iToken of phrase p.
 128533 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
 128535 ** This function assumes that pList points to a buffer allocated using
 128536 ** sqlite3_malloc(). This function takes responsibility for eventually
 128537 ** freeing the buffer.
 128539 static void fts3EvalPhraseMergeToken(
 128540   Fts3Table *pTab,                /* FTS Table pointer */
 128541   Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
 128542   int iToken,                     /* Token pList/nList corresponds to */
 128543   char *pList,                    /* Pointer to doclist */
 128544   int nList                       /* Number of bytes in pList */
 128546   assert( iToken!=p->iDoclistToken );
 128548   if( pList==0 ){
 128549     sqlite3_free(p->doclist.aAll);
 128550     p->doclist.aAll = 0;
 128551     p->doclist.nAll = 0;
 128554   else if( p->iDoclistToken<0 ){
 128555     p->doclist.aAll = pList;
 128556     p->doclist.nAll = nList;
 128559   else if( p->doclist.aAll==0 ){
 128560     sqlite3_free(pList);
 128563   else {
 128564     char *pLeft;
 128565     char *pRight;
 128566     int nLeft;
 128567     int nRight;
 128568     int nDiff;
 128570     if( p->iDoclistToken<iToken ){
 128571       pLeft = p->doclist.aAll;
 128572       nLeft = p->doclist.nAll;
 128573       pRight = pList;
 128574       nRight = nList;
 128575       nDiff = iToken - p->iDoclistToken;
 128576     }else{
 128577       pRight = p->doclist.aAll;
 128578       nRight = p->doclist.nAll;
 128579       pLeft = pList;
 128580       nLeft = nList;
 128581       nDiff = p->iDoclistToken - iToken;
 128584     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
 128585     sqlite3_free(pLeft);
 128586     p->doclist.aAll = pRight;
 128587     p->doclist.nAll = nRight;
 128590   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
 128594 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
 128595 ** does not take deferred tokens into account.
 128597 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 128599 static int fts3EvalPhraseLoad(
 128600   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 128601   Fts3Phrase *p                   /* Phrase object */
 128603   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 128604   int iToken;
 128605   int rc = SQLITE_OK;
 128607   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
 128608     Fts3PhraseToken *pToken = &p->aToken[iToken];
 128609     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
 128611     if( pToken->pSegcsr ){
 128612       int nThis = 0;
 128613       char *pThis = 0;
 128614       rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
 128615       if( rc==SQLITE_OK ){
 128616         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
 128619     assert( pToken->pSegcsr==0 );
 128622   return rc;
 128626 ** This function is called on each phrase after the position lists for
 128627 ** any deferred tokens have been loaded into memory. It updates the phrases
 128628 ** current position list to include only those positions that are really
 128629 ** instances of the phrase (after considering deferred tokens). If this
 128630 ** means that the phrase does not appear in the current row, doclist.pList
 128631 ** and doclist.nList are both zeroed.
 128633 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 128635 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
 128636   int iToken;                     /* Used to iterate through phrase tokens */
 128637   char *aPoslist = 0;             /* Position list for deferred tokens */
 128638   int nPoslist = 0;               /* Number of bytes in aPoslist */
 128639   int iPrev = -1;                 /* Token number of previous deferred token */
 128641   assert( pPhrase->doclist.bFreeList==0 );
 128643   for(iToken=0; iToken<pPhrase->nToken; iToken++){
 128644     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
 128645     Fts3DeferredToken *pDeferred = pToken->pDeferred;
 128647     if( pDeferred ){
 128648       char *pList;
 128649       int nList;
 128650       int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
 128651       if( rc!=SQLITE_OK ) return rc;
 128653       if( pList==0 ){
 128654         sqlite3_free(aPoslist);
 128655         pPhrase->doclist.pList = 0;
 128656         pPhrase->doclist.nList = 0;
 128657         return SQLITE_OK;
 128659       }else if( aPoslist==0 ){
 128660         aPoslist = pList;
 128661         nPoslist = nList;
 128663       }else{
 128664         char *aOut = pList;
 128665         char *p1 = aPoslist;
 128666         char *p2 = aOut;
 128668         assert( iPrev>=0 );
 128669         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
 128670         sqlite3_free(aPoslist);
 128671         aPoslist = pList;
 128672         nPoslist = (int)(aOut - aPoslist);
 128673         if( nPoslist==0 ){
 128674           sqlite3_free(aPoslist);
 128675           pPhrase->doclist.pList = 0;
 128676           pPhrase->doclist.nList = 0;
 128677           return SQLITE_OK;
 128680       iPrev = iToken;
 128684   if( iPrev>=0 ){
 128685     int nMaxUndeferred = pPhrase->iDoclistToken;
 128686     if( nMaxUndeferred<0 ){
 128687       pPhrase->doclist.pList = aPoslist;
 128688       pPhrase->doclist.nList = nPoslist;
 128689       pPhrase->doclist.iDocid = pCsr->iPrevId;
 128690       pPhrase->doclist.bFreeList = 1;
 128691     }else{
 128692       int nDistance;
 128693       char *p1;
 128694       char *p2;
 128695       char *aOut;
 128697       if( nMaxUndeferred>iPrev ){
 128698         p1 = aPoslist;
 128699         p2 = pPhrase->doclist.pList;
 128700         nDistance = nMaxUndeferred - iPrev;
 128701       }else{
 128702         p1 = pPhrase->doclist.pList;
 128703         p2 = aPoslist;
 128704         nDistance = iPrev - nMaxUndeferred;
 128707       aOut = (char *)sqlite3_malloc(nPoslist+8);
 128708       if( !aOut ){
 128709         sqlite3_free(aPoslist);
 128710         return SQLITE_NOMEM;
 128713       pPhrase->doclist.pList = aOut;
 128714       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
 128715         pPhrase->doclist.bFreeList = 1;
 128716         pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
 128717       }else{
 128718         sqlite3_free(aOut);
 128719         pPhrase->doclist.pList = 0;
 128720         pPhrase->doclist.nList = 0;
 128722       sqlite3_free(aPoslist);
 128726   return SQLITE_OK;
 128730 ** Maximum number of tokens a phrase may have to be considered for the
 128731 ** incremental doclists strategy.
 128733 #define MAX_INCR_PHRASE_TOKENS 4
 128736 ** This function is called for each Fts3Phrase in a full-text query 
 128737 ** expression to initialize the mechanism for returning rows. Once this
 128738 ** function has been called successfully on an Fts3Phrase, it may be
 128739 ** used with fts3EvalPhraseNext() to iterate through the matching docids.
 128741 ** If parameter bOptOk is true, then the phrase may (or may not) use the
 128742 ** incremental loading strategy. Otherwise, the entire doclist is loaded into
 128743 ** memory within this call.
 128745 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
 128747 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
 128748   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 128749   int rc = SQLITE_OK;             /* Error code */
 128750   int i;
 128752   /* Determine if doclists may be loaded from disk incrementally. This is
 128753   ** possible if the bOptOk argument is true, the FTS doclists will be
 128754   ** scanned in forward order, and the phrase consists of 
 128755   ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
 128756   ** tokens or prefix tokens that cannot use a prefix-index.  */
 128757   int bHaveIncr = 0;
 128758   int bIncrOk = (bOptOk 
 128759    && pCsr->bDesc==pTab->bDescIdx 
 128760    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
 128761    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
 128762 #ifdef SQLITE_TEST
 128763    && pTab->bNoIncrDoclist==0
 128764 #endif
 128766   for(i=0; bIncrOk==1 && i<p->nToken; i++){
 128767     Fts3PhraseToken *pToken = &p->aToken[i];
 128768     if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
 128769       bIncrOk = 0;
 128771     if( pToken->pSegcsr ) bHaveIncr = 1;
 128774   if( bIncrOk && bHaveIncr ){
 128775     /* Use the incremental approach. */
 128776     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
 128777     for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
 128778       Fts3PhraseToken *pToken = &p->aToken[i];
 128779       Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
 128780       if( pSegcsr ){
 128781         rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
 128784     p->bIncr = 1;
 128785   }else{
 128786     /* Load the full doclist for the phrase into memory. */
 128787     rc = fts3EvalPhraseLoad(pCsr, p);
 128788     p->bIncr = 0;
 128791   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
 128792   return rc;
 128796 ** This function is used to iterate backwards (from the end to start) 
 128797 ** through doclists. It is used by this module to iterate through phrase
 128798 ** doclists in reverse and by the fts3_write.c module to iterate through
 128799 ** pending-terms lists when writing to databases with "order=desc".
 128801 ** The doclist may be sorted in ascending (parameter bDescIdx==0) or 
 128802 ** descending (parameter bDescIdx==1) order of docid. Regardless, this
 128803 ** function iterates from the end of the doclist to the beginning.
 128805 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
 128806   int bDescIdx,                   /* True if the doclist is desc */
 128807   char *aDoclist,                 /* Pointer to entire doclist */
 128808   int nDoclist,                   /* Length of aDoclist in bytes */
 128809   char **ppIter,                  /* IN/OUT: Iterator pointer */
 128810   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
 128811   int *pnList,                    /* OUT: List length pointer */
 128812   u8 *pbEof                       /* OUT: End-of-file flag */
 128814   char *p = *ppIter;
 128816   assert( nDoclist>0 );
 128817   assert( *pbEof==0 );
 128818   assert( p || *piDocid==0 );
 128819   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
 128821   if( p==0 ){
 128822     sqlite3_int64 iDocid = 0;
 128823     char *pNext = 0;
 128824     char *pDocid = aDoclist;
 128825     char *pEnd = &aDoclist[nDoclist];
 128826     int iMul = 1;
 128828     while( pDocid<pEnd ){
 128829       sqlite3_int64 iDelta;
 128830       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
 128831       iDocid += (iMul * iDelta);
 128832       pNext = pDocid;
 128833       fts3PoslistCopy(0, &pDocid);
 128834       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
 128835       iMul = (bDescIdx ? -1 : 1);
 128838     *pnList = (int)(pEnd - pNext);
 128839     *ppIter = pNext;
 128840     *piDocid = iDocid;
 128841   }else{
 128842     int iMul = (bDescIdx ? -1 : 1);
 128843     sqlite3_int64 iDelta;
 128844     fts3GetReverseVarint(&p, aDoclist, &iDelta);
 128845     *piDocid -= (iMul * iDelta);
 128847     if( p==aDoclist ){
 128848       *pbEof = 1;
 128849     }else{
 128850       char *pSave = p;
 128851       fts3ReversePoslist(aDoclist, &p);
 128852       *pnList = (int)(pSave - p);
 128854     *ppIter = p;
 128859 ** Iterate forwards through a doclist.
 128861 SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
 128862   int bDescIdx,                   /* True if the doclist is desc */
 128863   char *aDoclist,                 /* Pointer to entire doclist */
 128864   int nDoclist,                   /* Length of aDoclist in bytes */
 128865   char **ppIter,                  /* IN/OUT: Iterator pointer */
 128866   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
 128867   u8 *pbEof                       /* OUT: End-of-file flag */
 128869   char *p = *ppIter;
 128871   assert( nDoclist>0 );
 128872   assert( *pbEof==0 );
 128873   assert( p || *piDocid==0 );
 128874   assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
 128876   if( p==0 ){
 128877     p = aDoclist;
 128878     p += sqlite3Fts3GetVarint(p, piDocid);
 128879   }else{
 128880     fts3PoslistCopy(0, &p);
 128881     if( p>=&aDoclist[nDoclist] ){
 128882       *pbEof = 1;
 128883     }else{
 128884       sqlite3_int64 iVar;
 128885       p += sqlite3Fts3GetVarint(p, &iVar);
 128886       *piDocid += ((bDescIdx ? -1 : 1) * iVar);
 128890   *ppIter = p;
 128894 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
 128895 ** to true if EOF is reached.
 128897 static void fts3EvalDlPhraseNext(
 128898   Fts3Table *pTab,
 128899   Fts3Doclist *pDL,
 128900   u8 *pbEof
 128902   char *pIter;                            /* Used to iterate through aAll */
 128903   char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
 128905   if( pDL->pNextDocid ){
 128906     pIter = pDL->pNextDocid;
 128907   }else{
 128908     pIter = pDL->aAll;
 128911   if( pIter>=pEnd ){
 128912     /* We have already reached the end of this doclist. EOF. */
 128913     *pbEof = 1;
 128914   }else{
 128915     sqlite3_int64 iDelta;
 128916     pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
 128917     if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
 128918       pDL->iDocid += iDelta;
 128919     }else{
 128920       pDL->iDocid -= iDelta;
 128922     pDL->pList = pIter;
 128923     fts3PoslistCopy(0, &pIter);
 128924     pDL->nList = (int)(pIter - pDL->pList);
 128926     /* pIter now points just past the 0x00 that terminates the position-
 128927     ** list for document pDL->iDocid. However, if this position-list was
 128928     ** edited in place by fts3EvalNearTrim(), then pIter may not actually
 128929     ** point to the start of the next docid value. The following line deals
 128930     ** with this case by advancing pIter past the zero-padding added by
 128931     ** fts3EvalNearTrim().  */
 128932     while( pIter<pEnd && *pIter==0 ) pIter++;
 128934     pDL->pNextDocid = pIter;
 128935     assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
 128936     *pbEof = 0;
 128941 ** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
 128943 typedef struct TokenDoclist TokenDoclist;
 128944 struct TokenDoclist {
 128945   int bIgnore;
 128946   sqlite3_int64 iDocid;
 128947   char *pList;
 128948   int nList;
 128952 ** Token pToken is an incrementally loaded token that is part of a 
 128953 ** multi-token phrase. Advance it to the next matching document in the
 128954 ** database and populate output variable *p with the details of the new
 128955 ** entry. Or, if the iterator has reached EOF, set *pbEof to true.
 128957 ** If an error occurs, return an SQLite error code. Otherwise, return 
 128958 ** SQLITE_OK.
 128960 static int incrPhraseTokenNext(
 128961   Fts3Table *pTab,                /* Virtual table handle */
 128962   Fts3Phrase *pPhrase,            /* Phrase to advance token of */
 128963   int iToken,                     /* Specific token to advance */
 128964   TokenDoclist *p,                /* OUT: Docid and doclist for new entry */
 128965   u8 *pbEof                       /* OUT: True if iterator is at EOF */
 128967   int rc = SQLITE_OK;
 128969   if( pPhrase->iDoclistToken==iToken ){
 128970     assert( p->bIgnore==0 );
 128971     assert( pPhrase->aToken[iToken].pSegcsr==0 );
 128972     fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
 128973     p->pList = pPhrase->doclist.pList;
 128974     p->nList = pPhrase->doclist.nList;
 128975     p->iDocid = pPhrase->doclist.iDocid;
 128976   }else{
 128977     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
 128978     assert( pToken->pDeferred==0 );
 128979     assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
 128980     if( pToken->pSegcsr ){
 128981       assert( p->bIgnore==0 );
 128982       rc = sqlite3Fts3MsrIncrNext(
 128983           pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
 128985       if( p->pList==0 ) *pbEof = 1;
 128986     }else{
 128987       p->bIgnore = 1;
 128991   return rc;
 128996 ** The phrase iterator passed as the second argument:
 128998 **   * features at least one token that uses an incremental doclist, and 
 129000 **   * does not contain any deferred tokens.
 129002 ** Advance it to the next matching documnent in the database and populate
 129003 ** the Fts3Doclist.pList and nList fields. 
 129005 ** If there is no "next" entry and no error occurs, then *pbEof is set to
 129006 ** 1 before returning. Otherwise, if no error occurs and the iterator is
 129007 ** successfully advanced, *pbEof is set to 0.
 129009 ** If an error occurs, return an SQLite error code. Otherwise, return 
 129010 ** SQLITE_OK.
 129012 static int fts3EvalIncrPhraseNext(
 129013   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129014   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
 129015   u8 *pbEof                       /* OUT: Set to 1 if EOF */
 129017   int rc = SQLITE_OK;
 129018   Fts3Doclist *pDL = &p->doclist;
 129019   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 129020   u8 bEof = 0;
 129022   /* This is only called if it is guaranteed that the phrase has at least
 129023   ** one incremental token. In which case the bIncr flag is set. */
 129024   assert( p->bIncr==1 );
 129026   if( p->nToken==1 && p->bIncr ){
 129027     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, 
 129028         &pDL->iDocid, &pDL->pList, &pDL->nList
 129030     if( pDL->pList==0 ) bEof = 1;
 129031   }else{
 129032     int bDescDoclist = pCsr->bDesc;
 129033     struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
 129035     memset(a, 0, sizeof(a));
 129036     assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
 129037     assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
 129039     while( bEof==0 ){
 129040       int bMaxSet = 0;
 129041       sqlite3_int64 iMax = 0;     /* Largest docid for all iterators */
 129042       int i;                      /* Used to iterate through tokens */
 129044       /* Advance the iterator for each token in the phrase once. */
 129045       for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
 129046         rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
 129047         if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
 129048           iMax = a[i].iDocid;
 129049           bMaxSet = 1;
 129052       assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 );
 129053       assert( rc!=SQLITE_OK || bMaxSet );
 129055       /* Keep advancing iterators until they all point to the same document */
 129056       for(i=0; i<p->nToken; i++){
 129057         while( rc==SQLITE_OK && bEof==0 
 129058             && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0 
 129060           rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
 129061           if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
 129062             iMax = a[i].iDocid;
 129063             i = 0;
 129068       /* Check if the current entries really are a phrase match */
 129069       if( bEof==0 ){
 129070         int nList = 0;
 129071         int nByte = a[p->nToken-1].nList;
 129072         char *aDoclist = sqlite3_malloc(nByte+1);
 129073         if( !aDoclist ) return SQLITE_NOMEM;
 129074         memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
 129076         for(i=0; i<(p->nToken-1); i++){
 129077           if( a[i].bIgnore==0 ){
 129078             char *pL = a[i].pList;
 129079             char *pR = aDoclist;
 129080             char *pOut = aDoclist;
 129081             int nDist = p->nToken-1-i;
 129082             int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
 129083             if( res==0 ) break;
 129084             nList = (int)(pOut - aDoclist);
 129087         if( i==(p->nToken-1) ){
 129088           pDL->iDocid = iMax;
 129089           pDL->pList = aDoclist;
 129090           pDL->nList = nList;
 129091           pDL->bFreeList = 1;
 129092           break;
 129094         sqlite3_free(aDoclist);
 129099   *pbEof = bEof;
 129100   return rc;
 129104 ** Attempt to move the phrase iterator to point to the next matching docid. 
 129105 ** If an error occurs, return an SQLite error code. Otherwise, return 
 129106 ** SQLITE_OK.
 129108 ** If there is no "next" entry and no error occurs, then *pbEof is set to
 129109 ** 1 before returning. Otherwise, if no error occurs and the iterator is
 129110 ** successfully advanced, *pbEof is set to 0.
 129112 static int fts3EvalPhraseNext(
 129113   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129114   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
 129115   u8 *pbEof                       /* OUT: Set to 1 if EOF */
 129117   int rc = SQLITE_OK;
 129118   Fts3Doclist *pDL = &p->doclist;
 129119   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 129121   if( p->bIncr ){
 129122     rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
 129123   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
 129124     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, 
 129125         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
 129127     pDL->pList = pDL->pNextDocid;
 129128   }else{
 129129     fts3EvalDlPhraseNext(pTab, pDL, pbEof);
 129132   return rc;
 129137 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 129138 ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
 129139 ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
 129140 ** expressions for which all descendent tokens are deferred.
 129142 ** If parameter bOptOk is zero, then it is guaranteed that the
 129143 ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
 129144 ** each phrase in the expression (subject to deferred token processing).
 129145 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
 129146 ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
 129148 ** If an error occurs within this function, *pRc is set to an SQLite error
 129149 ** code before returning.
 129151 static void fts3EvalStartReaders(
 129152   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129153   Fts3Expr *pExpr,                /* Expression to initialize phrases in */
 129154   int *pRc                        /* IN/OUT: Error code */
 129156   if( pExpr && SQLITE_OK==*pRc ){
 129157     if( pExpr->eType==FTSQUERY_PHRASE ){
 129158       int i;
 129159       int nToken = pExpr->pPhrase->nToken;
 129160       for(i=0; i<nToken; i++){
 129161         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
 129163       pExpr->bDeferred = (i==nToken);
 129164       *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
 129165     }else{
 129166       fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
 129167       fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
 129168       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
 129174 ** An array of the following structures is assembled as part of the process
 129175 ** of selecting tokens to defer before the query starts executing (as part
 129176 ** of the xFilter() method). There is one element in the array for each
 129177 ** token in the FTS expression.
 129179 ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
 129180 ** to phrases that are connected only by AND and NEAR operators (not OR or
 129181 ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
 129182 ** separately. The root of a tokens AND/NEAR cluster is stored in 
 129183 ** Fts3TokenAndCost.pRoot.
 129185 typedef struct Fts3TokenAndCost Fts3TokenAndCost;
 129186 struct Fts3TokenAndCost {
 129187   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
 129188   int iToken;                     /* Position of token in phrase */
 129189   Fts3PhraseToken *pToken;        /* The token itself */
 129190   Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
 129191   int nOvfl;                      /* Number of overflow pages to load doclist */
 129192   int iCol;                       /* The column the token must match */
 129196 ** This function is used to populate an allocated Fts3TokenAndCost array.
 129198 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 129199 ** Otherwise, if an error occurs during execution, *pRc is set to an
 129200 ** SQLite error code.
 129202 static void fts3EvalTokenCosts(
 129203   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129204   Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
 129205   Fts3Expr *pExpr,                /* Expression to consider */
 129206   Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
 129207   Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
 129208   int *pRc                        /* IN/OUT: Error code */
 129210   if( *pRc==SQLITE_OK ){
 129211     if( pExpr->eType==FTSQUERY_PHRASE ){
 129212       Fts3Phrase *pPhrase = pExpr->pPhrase;
 129213       int i;
 129214       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
 129215         Fts3TokenAndCost *pTC = (*ppTC)++;
 129216         pTC->pPhrase = pPhrase;
 129217         pTC->iToken = i;
 129218         pTC->pRoot = pRoot;
 129219         pTC->pToken = &pPhrase->aToken[i];
 129220         pTC->iCol = pPhrase->iColumn;
 129221         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
 129223     }else if( pExpr->eType!=FTSQUERY_NOT ){
 129224       assert( pExpr->eType==FTSQUERY_OR
 129225            || pExpr->eType==FTSQUERY_AND
 129226            || pExpr->eType==FTSQUERY_NEAR
 129228       assert( pExpr->pLeft && pExpr->pRight );
 129229       if( pExpr->eType==FTSQUERY_OR ){
 129230         pRoot = pExpr->pLeft;
 129231         **ppOr = pRoot;
 129232         (*ppOr)++;
 129234       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
 129235       if( pExpr->eType==FTSQUERY_OR ){
 129236         pRoot = pExpr->pRight;
 129237         **ppOr = pRoot;
 129238         (*ppOr)++;
 129240       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
 129246 ** Determine the average document (row) size in pages. If successful,
 129247 ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
 129248 ** an SQLite error code.
 129250 ** The average document size in pages is calculated by first calculating 
 129251 ** determining the average size in bytes, B. If B is less than the amount
 129252 ** of data that will fit on a single leaf page of an intkey table in
 129253 ** this database, then the average docsize is 1. Otherwise, it is 1 plus
 129254 ** the number of overflow pages consumed by a record B bytes in size.
 129256 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
 129257   if( pCsr->nRowAvg==0 ){
 129258     /* The average document size, which is required to calculate the cost
 129259     ** of each doclist, has not yet been determined. Read the required 
 129260     ** data from the %_stat table to calculate it.
 129262     ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 
 129263     ** varints, where nCol is the number of columns in the FTS3 table.
 129264     ** The first varint is the number of documents currently stored in
 129265     ** the table. The following nCol varints contain the total amount of
 129266     ** data stored in all rows of each column of the table, from left
 129267     ** to right.
 129269     int rc;
 129270     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
 129271     sqlite3_stmt *pStmt;
 129272     sqlite3_int64 nDoc = 0;
 129273     sqlite3_int64 nByte = 0;
 129274     const char *pEnd;
 129275     const char *a;
 129277     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
 129278     if( rc!=SQLITE_OK ) return rc;
 129279     a = sqlite3_column_blob(pStmt, 0);
 129280     assert( a );
 129282     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
 129283     a += sqlite3Fts3GetVarint(a, &nDoc);
 129284     while( a<pEnd ){
 129285       a += sqlite3Fts3GetVarint(a, &nByte);
 129287     if( nDoc==0 || nByte==0 ){
 129288       sqlite3_reset(pStmt);
 129289       return FTS_CORRUPT_VTAB;
 129292     pCsr->nDoc = nDoc;
 129293     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
 129294     assert( pCsr->nRowAvg>0 ); 
 129295     rc = sqlite3_reset(pStmt);
 129296     if( rc!=SQLITE_OK ) return rc;
 129299   *pnPage = pCsr->nRowAvg;
 129300   return SQLITE_OK;
 129304 ** This function is called to select the tokens (if any) that will be 
 129305 ** deferred. The array aTC[] has already been populated when this is
 129306 ** called.
 129308 ** This function is called once for each AND/NEAR cluster in the 
 129309 ** expression. Each invocation determines which tokens to defer within
 129310 ** the cluster with root node pRoot. See comments above the definition
 129311 ** of struct Fts3TokenAndCost for more details.
 129313 ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
 129314 ** called on each token to defer. Otherwise, an SQLite error code is
 129315 ** returned.
 129317 static int fts3EvalSelectDeferred(
 129318   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129319   Fts3Expr *pRoot,                /* Consider tokens with this root node */
 129320   Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
 129321   int nTC                         /* Number of entries in aTC[] */
 129323   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 129324   int nDocSize = 0;               /* Number of pages per doc loaded */
 129325   int rc = SQLITE_OK;             /* Return code */
 129326   int ii;                         /* Iterator variable for various purposes */
 129327   int nOvfl = 0;                  /* Total overflow pages used by doclists */
 129328   int nToken = 0;                 /* Total number of tokens in cluster */
 129330   int nMinEst = 0;                /* The minimum count for any phrase so far. */
 129331   int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
 129333   /* Tokens are never deferred for FTS tables created using the content=xxx
 129334   ** option. The reason being that it is not guaranteed that the content
 129335   ** table actually contains the same data as the index. To prevent this from
 129336   ** causing any problems, the deferred token optimization is completely
 129337   ** disabled for content=xxx tables. */
 129338   if( pTab->zContentTbl ){
 129339     return SQLITE_OK;
 129342   /* Count the tokens in this AND/NEAR cluster. If none of the doclists
 129343   ** associated with the tokens spill onto overflow pages, or if there is
 129344   ** only 1 token, exit early. No tokens to defer in this case. */
 129345   for(ii=0; ii<nTC; ii++){
 129346     if( aTC[ii].pRoot==pRoot ){
 129347       nOvfl += aTC[ii].nOvfl;
 129348       nToken++;
 129351   if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
 129353   /* Obtain the average docsize (in pages). */
 129354   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
 129355   assert( rc!=SQLITE_OK || nDocSize>0 );
 129358   /* Iterate through all tokens in this AND/NEAR cluster, in ascending order 
 129359   ** of the number of overflow pages that will be loaded by the pager layer 
 129360   ** to retrieve the entire doclist for the token from the full-text index.
 129361   ** Load the doclists for tokens that are either:
 129363   **   a. The cheapest token in the entire query (i.e. the one visited by the
 129364   **      first iteration of this loop), or
 129366   **   b. Part of a multi-token phrase.
 129368   ** After each token doclist is loaded, merge it with the others from the
 129369   ** same phrase and count the number of documents that the merged doclist
 129370   ** contains. Set variable "nMinEst" to the smallest number of documents in 
 129371   ** any phrase doclist for which 1 or more token doclists have been loaded.
 129372   ** Let nOther be the number of other phrases for which it is certain that
 129373   ** one or more tokens will not be deferred.
 129375   ** Then, for each token, defer it if loading the doclist would result in
 129376   ** loading N or more overflow pages into memory, where N is computed as:
 129378   **    (nMinEst + 4^nOther - 1) / (4^nOther)
 129380   for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
 129381     int iTC;                      /* Used to iterate through aTC[] array. */
 129382     Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
 129384     /* Set pTC to point to the cheapest remaining token. */
 129385     for(iTC=0; iTC<nTC; iTC++){
 129386       if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot 
 129387        && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl) 
 129389         pTC = &aTC[iTC];
 129392     assert( pTC );
 129394     if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
 129395       /* The number of overflow pages to load for this (and therefore all
 129396       ** subsequent) tokens is greater than the estimated number of pages 
 129397       ** that will be loaded if all subsequent tokens are deferred.
 129399       Fts3PhraseToken *pToken = pTC->pToken;
 129400       rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
 129401       fts3SegReaderCursorFree(pToken->pSegcsr);
 129402       pToken->pSegcsr = 0;
 129403     }else{
 129404       /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
 129405       ** for-loop. Except, limit the value to 2^24 to prevent it from 
 129406       ** overflowing the 32-bit integer it is stored in. */
 129407       if( ii<12 ) nLoad4 = nLoad4*4;
 129409       if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
 129410         /* Either this is the cheapest token in the entire query, or it is
 129411         ** part of a multi-token phrase. Either way, the entire doclist will
 129412         ** (eventually) be loaded into memory. It may as well be now. */
 129413         Fts3PhraseToken *pToken = pTC->pToken;
 129414         int nList = 0;
 129415         char *pList = 0;
 129416         rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
 129417         assert( rc==SQLITE_OK || pList==0 );
 129418         if( rc==SQLITE_OK ){
 129419           int nCount;
 129420           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
 129421           nCount = fts3DoclistCountDocids(
 129422               pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
 129424           if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
 129428     pTC->pToken = 0;
 129431   return rc;
 129435 ** This function is called from within the xFilter method. It initializes
 129436 ** the full-text query currently stored in pCsr->pExpr. To iterate through
 129437 ** the results of a query, the caller does:
 129439 **    fts3EvalStart(pCsr);
 129440 **    while( 1 ){
 129441 **      fts3EvalNext(pCsr);
 129442 **      if( pCsr->bEof ) break;
 129443 **      ... return row pCsr->iPrevId to the caller ...
 129444 **    }
 129446 static int fts3EvalStart(Fts3Cursor *pCsr){
 129447   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 129448   int rc = SQLITE_OK;
 129449   int nToken = 0;
 129450   int nOr = 0;
 129452   /* Allocate a MultiSegReader for each token in the expression. */
 129453   fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
 129455   /* Determine which, if any, tokens in the expression should be deferred. */
 129456 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 129457   if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
 129458     Fts3TokenAndCost *aTC;
 129459     Fts3Expr **apOr;
 129460     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
 129461         sizeof(Fts3TokenAndCost) * nToken
 129462       + sizeof(Fts3Expr *) * nOr * 2
 129464     apOr = (Fts3Expr **)&aTC[nToken];
 129466     if( !aTC ){
 129467       rc = SQLITE_NOMEM;
 129468     }else{
 129469       int ii;
 129470       Fts3TokenAndCost *pTC = aTC;
 129471       Fts3Expr **ppOr = apOr;
 129473       fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
 129474       nToken = (int)(pTC-aTC);
 129475       nOr = (int)(ppOr-apOr);
 129477       if( rc==SQLITE_OK ){
 129478         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
 129479         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
 129480           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
 129484       sqlite3_free(aTC);
 129487 #endif
 129489   fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
 129490   return rc;
 129494 ** Invalidate the current position list for phrase pPhrase.
 129496 static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
 129497   if( pPhrase->doclist.bFreeList ){
 129498     sqlite3_free(pPhrase->doclist.pList);
 129500   pPhrase->doclist.pList = 0;
 129501   pPhrase->doclist.nList = 0;
 129502   pPhrase->doclist.bFreeList = 0;
 129506 ** This function is called to edit the position list associated with
 129507 ** the phrase object passed as the fifth argument according to a NEAR
 129508 ** condition. For example:
 129510 **     abc NEAR/5 "def ghi"
 129512 ** Parameter nNear is passed the NEAR distance of the expression (5 in
 129513 ** the example above). When this function is called, *paPoslist points to
 129514 ** the position list, and *pnToken is the number of phrase tokens in, the
 129515 ** phrase on the other side of the NEAR operator to pPhrase. For example,
 129516 ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
 129517 ** the position list associated with phrase "abc".
 129519 ** All positions in the pPhrase position list that are not sufficiently
 129520 ** close to a position in the *paPoslist position list are removed. If this
 129521 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
 129523 ** Before returning, *paPoslist is set to point to the position lsit 
 129524 ** associated with pPhrase. And *pnToken is set to the number of tokens in
 129525 ** pPhrase.
 129527 static int fts3EvalNearTrim(
 129528   int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
 129529   char *aTmp,                     /* Temporary space to use */
 129530   char **paPoslist,               /* IN/OUT: Position list */
 129531   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
 129532   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
 129534   int nParam1 = nNear + pPhrase->nToken;
 129535   int nParam2 = nNear + *pnToken;
 129536   int nNew;
 129537   char *p2; 
 129538   char *pOut; 
 129539   int res;
 129541   assert( pPhrase->doclist.pList );
 129543   p2 = pOut = pPhrase->doclist.pList;
 129544   res = fts3PoslistNearMerge(
 129545     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
 129547   if( res ){
 129548     nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
 129549     assert( pPhrase->doclist.pList[nNew]=='\0' );
 129550     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
 129551     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
 129552     pPhrase->doclist.nList = nNew;
 129553     *paPoslist = pPhrase->doclist.pList;
 129554     *pnToken = pPhrase->nToken;
 129557   return res;
 129561 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
 129562 ** Otherwise, it advances the expression passed as the second argument to
 129563 ** point to the next matching row in the database. Expressions iterate through
 129564 ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
 129565 ** or descending if it is non-zero.
 129567 ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
 129568 ** successful, the following variables in pExpr are set:
 129570 **   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
 129571 **   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
 129573 ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
 129574 ** at EOF, then the following variables are populated with the position list
 129575 ** for the phrase for the visited row:
 129577 **   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
 129578 **   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
 129580 ** It says above that this function advances the expression to the next
 129581 ** matching row. This is usually true, but there are the following exceptions:
 129583 **   1. Deferred tokens are not taken into account. If a phrase consists
 129584 **      entirely of deferred tokens, it is assumed to match every row in
 129585 **      the db. In this case the position-list is not populated at all. 
 129587 **      Or, if a phrase contains one or more deferred tokens and one or
 129588 **      more non-deferred tokens, then the expression is advanced to the 
 129589 **      next possible match, considering only non-deferred tokens. In other
 129590 **      words, if the phrase is "A B C", and "B" is deferred, the expression
 129591 **      is advanced to the next row that contains an instance of "A * C", 
 129592 **      where "*" may match any single token. The position list in this case
 129593 **      is populated as for "A * C" before returning.
 129595 **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is 
 129596 **      advanced to point to the next row that matches "x AND y".
 129598 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
 129599 ** really a match, taking into account deferred tokens and NEAR operators.
 129601 static void fts3EvalNextRow(
 129602   Fts3Cursor *pCsr,               /* FTS Cursor handle */
 129603   Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
 129604   int *pRc                        /* IN/OUT: Error code */
 129606   if( *pRc==SQLITE_OK ){
 129607     int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
 129608     assert( pExpr->bEof==0 );
 129609     pExpr->bStart = 1;
 129611     switch( pExpr->eType ){
 129612       case FTSQUERY_NEAR:
 129613       case FTSQUERY_AND: {
 129614         Fts3Expr *pLeft = pExpr->pLeft;
 129615         Fts3Expr *pRight = pExpr->pRight;
 129616         assert( !pLeft->bDeferred || !pRight->bDeferred );
 129618         if( pLeft->bDeferred ){
 129619           /* LHS is entirely deferred. So we assume it matches every row.
 129620           ** Advance the RHS iterator to find the next row visited. */
 129621           fts3EvalNextRow(pCsr, pRight, pRc);
 129622           pExpr->iDocid = pRight->iDocid;
 129623           pExpr->bEof = pRight->bEof;
 129624         }else if( pRight->bDeferred ){
 129625           /* RHS is entirely deferred. So we assume it matches every row.
 129626           ** Advance the LHS iterator to find the next row visited. */
 129627           fts3EvalNextRow(pCsr, pLeft, pRc);
 129628           pExpr->iDocid = pLeft->iDocid;
 129629           pExpr->bEof = pLeft->bEof;
 129630         }else{
 129631           /* Neither the RHS or LHS are deferred. */
 129632           fts3EvalNextRow(pCsr, pLeft, pRc);
 129633           fts3EvalNextRow(pCsr, pRight, pRc);
 129634           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
 129635             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 129636             if( iDiff==0 ) break;
 129637             if( iDiff<0 ){
 129638               fts3EvalNextRow(pCsr, pLeft, pRc);
 129639             }else{
 129640               fts3EvalNextRow(pCsr, pRight, pRc);
 129643           pExpr->iDocid = pLeft->iDocid;
 129644           pExpr->bEof = (pLeft->bEof || pRight->bEof);
 129646         break;
 129649       case FTSQUERY_OR: {
 129650         Fts3Expr *pLeft = pExpr->pLeft;
 129651         Fts3Expr *pRight = pExpr->pRight;
 129652         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 129654         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
 129655         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
 129657         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
 129658           fts3EvalNextRow(pCsr, pLeft, pRc);
 129659         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
 129660           fts3EvalNextRow(pCsr, pRight, pRc);
 129661         }else{
 129662           fts3EvalNextRow(pCsr, pLeft, pRc);
 129663           fts3EvalNextRow(pCsr, pRight, pRc);
 129666         pExpr->bEof = (pLeft->bEof && pRight->bEof);
 129667         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
 129668         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
 129669           pExpr->iDocid = pLeft->iDocid;
 129670         }else{
 129671           pExpr->iDocid = pRight->iDocid;
 129674         break;
 129677       case FTSQUERY_NOT: {
 129678         Fts3Expr *pLeft = pExpr->pLeft;
 129679         Fts3Expr *pRight = pExpr->pRight;
 129681         if( pRight->bStart==0 ){
 129682           fts3EvalNextRow(pCsr, pRight, pRc);
 129683           assert( *pRc!=SQLITE_OK || pRight->bStart );
 129686         fts3EvalNextRow(pCsr, pLeft, pRc);
 129687         if( pLeft->bEof==0 ){
 129688           while( !*pRc 
 129689               && !pRight->bEof 
 129690               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 
 129692             fts3EvalNextRow(pCsr, pRight, pRc);
 129695         pExpr->iDocid = pLeft->iDocid;
 129696         pExpr->bEof = pLeft->bEof;
 129697         break;
 129700       default: {
 129701         Fts3Phrase *pPhrase = pExpr->pPhrase;
 129702         fts3EvalInvalidatePoslist(pPhrase);
 129703         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
 129704         pExpr->iDocid = pPhrase->doclist.iDocid;
 129705         break;
 129712 ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
 129713 ** cluster, then this function returns 1 immediately.
 129715 ** Otherwise, it checks if the current row really does match the NEAR 
 129716 ** expression, using the data currently stored in the position lists 
 129717 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. 
 129719 ** If the current row is a match, the position list associated with each
 129720 ** phrase in the NEAR expression is edited in place to contain only those
 129721 ** phrase instances sufficiently close to their peers to satisfy all NEAR
 129722 ** constraints. In this case it returns 1. If the NEAR expression does not 
 129723 ** match the current row, 0 is returned. The position lists may or may not
 129724 ** be edited if 0 is returned.
 129726 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
 129727   int res = 1;
 129729   /* The following block runs if pExpr is the root of a NEAR query.
 129730   ** For example, the query:
 129732   **         "w" NEAR "x" NEAR "y" NEAR "z"
 129734   ** which is represented in tree form as:
 129736   **                               |
 129737   **                          +--NEAR--+      <-- root of NEAR query
 129738   **                          |        |
 129739   **                     +--NEAR--+   "z"
 129740   **                     |        |
 129741   **                +--NEAR--+   "y"
 129742   **                |        |
 129743   **               "w"      "x"
 129745   ** The right-hand child of a NEAR node is always a phrase. The 
 129746   ** left-hand child may be either a phrase or a NEAR node. There are
 129747   ** no exceptions to this - it's the way the parser in fts3_expr.c works.
 129749   if( *pRc==SQLITE_OK 
 129750    && pExpr->eType==FTSQUERY_NEAR 
 129751    && pExpr->bEof==0
 129752    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
 129754     Fts3Expr *p; 
 129755     int nTmp = 0;                 /* Bytes of temp space */
 129756     char *aTmp;                   /* Temp space for PoslistNearMerge() */
 129758     /* Allocate temporary working space. */
 129759     for(p=pExpr; p->pLeft; p=p->pLeft){
 129760       nTmp += p->pRight->pPhrase->doclist.nList;
 129762     nTmp += p->pPhrase->doclist.nList;
 129763     if( nTmp==0 ){
 129764       res = 0;
 129765     }else{
 129766       aTmp = sqlite3_malloc(nTmp*2);
 129767       if( !aTmp ){
 129768         *pRc = SQLITE_NOMEM;
 129769         res = 0;
 129770       }else{
 129771         char *aPoslist = p->pPhrase->doclist.pList;
 129772         int nToken = p->pPhrase->nToken;
 129774         for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
 129775           Fts3Phrase *pPhrase = p->pRight->pPhrase;
 129776           int nNear = p->nNear;
 129777           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 129780         aPoslist = pExpr->pRight->pPhrase->doclist.pList;
 129781         nToken = pExpr->pRight->pPhrase->nToken;
 129782         for(p=pExpr->pLeft; p && res; p=p->pLeft){
 129783           int nNear;
 129784           Fts3Phrase *pPhrase;
 129785           assert( p->pParent && p->pParent->pLeft==p );
 129786           nNear = p->pParent->nNear;
 129787           pPhrase = (
 129788               p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
 129790           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
 129794       sqlite3_free(aTmp);
 129798   return res;
 129802 ** This function is a helper function for fts3EvalTestDeferredAndNear().
 129803 ** Assuming no error occurs or has occurred, It returns non-zero if the
 129804 ** expression passed as the second argument matches the row that pCsr 
 129805 ** currently points to, or zero if it does not.
 129807 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 129808 ** If an error occurs during execution of this function, *pRc is set to 
 129809 ** the appropriate SQLite error code. In this case the returned value is 
 129810 ** undefined.
 129812 static int fts3EvalTestExpr(
 129813   Fts3Cursor *pCsr,               /* FTS cursor handle */
 129814   Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
 129815   int *pRc                        /* IN/OUT: Error code */
 129817   int bHit = 1;                   /* Return value */
 129818   if( *pRc==SQLITE_OK ){
 129819     switch( pExpr->eType ){
 129820       case FTSQUERY_NEAR:
 129821       case FTSQUERY_AND:
 129822         bHit = (
 129823             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
 129824          && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
 129825          && fts3EvalNearTest(pExpr, pRc)
 129828         /* If the NEAR expression does not match any rows, zero the doclist for 
 129829         ** all phrases involved in the NEAR. This is because the snippet(),
 129830         ** offsets() and matchinfo() functions are not supposed to recognize 
 129831         ** any instances of phrases that are part of unmatched NEAR queries. 
 129832         ** For example if this expression:
 129834         **    ... MATCH 'a OR (b NEAR c)'
 129836         ** is matched against a row containing:
 129838         **        'a b d e'
 129840         ** then any snippet() should ony highlight the "a" term, not the "b"
 129841         ** (as "b" is part of a non-matching NEAR clause).
 129843         if( bHit==0 
 129844          && pExpr->eType==FTSQUERY_NEAR 
 129845          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
 129847           Fts3Expr *p;
 129848           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
 129849             if( p->pRight->iDocid==pCsr->iPrevId ){
 129850               fts3EvalInvalidatePoslist(p->pRight->pPhrase);
 129853           if( p->iDocid==pCsr->iPrevId ){
 129854             fts3EvalInvalidatePoslist(p->pPhrase);
 129858         break;
 129860       case FTSQUERY_OR: {
 129861         int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
 129862         int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
 129863         bHit = bHit1 || bHit2;
 129864         break;
 129867       case FTSQUERY_NOT:
 129868         bHit = (
 129869             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
 129870          && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
 129872         break;
 129874       default: {
 129875 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 129876         if( pCsr->pDeferred 
 129877          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
 129879           Fts3Phrase *pPhrase = pExpr->pPhrase;
 129880           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
 129881           if( pExpr->bDeferred ){
 129882             fts3EvalInvalidatePoslist(pPhrase);
 129884           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
 129885           bHit = (pPhrase->doclist.pList!=0);
 129886           pExpr->iDocid = pCsr->iPrevId;
 129887         }else
 129888 #endif
 129890           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
 129892         break;
 129896   return bHit;
 129900 ** This function is called as the second part of each xNext operation when
 129901 ** iterating through the results of a full-text query. At this point the
 129902 ** cursor points to a row that matches the query expression, with the
 129903 ** following caveats:
 129905 **   * Up until this point, "NEAR" operators in the expression have been
 129906 **     treated as "AND".
 129908 **   * Deferred tokens have not yet been considered.
 129910 ** If *pRc is not SQLITE_OK when this function is called, it immediately
 129911 ** returns 0. Otherwise, it tests whether or not after considering NEAR
 129912 ** operators and deferred tokens the current row is still a match for the
 129913 ** expression. It returns 1 if both of the following are true:
 129915 **   1. *pRc is SQLITE_OK when this function returns, and
 129917 **   2. After scanning the current FTS table row for the deferred tokens,
 129918 **      it is determined that the row does *not* match the query.
 129920 ** Or, if no error occurs and it seems the current row does match the FTS
 129921 ** query, return 0.
 129923 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
 129924   int rc = *pRc;
 129925   int bMiss = 0;
 129926   if( rc==SQLITE_OK ){
 129928     /* If there are one or more deferred tokens, load the current row into
 129929     ** memory and scan it to determine the position list for each deferred
 129930     ** token. Then, see if this row is really a match, considering deferred
 129931     ** tokens and NEAR operators (neither of which were taken into account
 129932     ** earlier, by fts3EvalNextRow()). 
 129934     if( pCsr->pDeferred ){
 129935       rc = fts3CursorSeek(0, pCsr);
 129936       if( rc==SQLITE_OK ){
 129937         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
 129940     bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
 129942     /* Free the position-lists accumulated for each deferred token above. */
 129943     sqlite3Fts3FreeDeferredDoclists(pCsr);
 129944     *pRc = rc;
 129946   return (rc==SQLITE_OK && bMiss);
 129950 ** Advance to the next document that matches the FTS expression in
 129951 ** Fts3Cursor.pExpr.
 129953 static int fts3EvalNext(Fts3Cursor *pCsr){
 129954   int rc = SQLITE_OK;             /* Return Code */
 129955   Fts3Expr *pExpr = pCsr->pExpr;
 129956   assert( pCsr->isEof==0 );
 129957   if( pExpr==0 ){
 129958     pCsr->isEof = 1;
 129959   }else{
 129960     do {
 129961       if( pCsr->isRequireSeek==0 ){
 129962         sqlite3_reset(pCsr->pStmt);
 129964       assert( sqlite3_data_count(pCsr->pStmt)==0 );
 129965       fts3EvalNextRow(pCsr, pExpr, &rc);
 129966       pCsr->isEof = pExpr->bEof;
 129967       pCsr->isRequireSeek = 1;
 129968       pCsr->isMatchinfoNeeded = 1;
 129969       pCsr->iPrevId = pExpr->iDocid;
 129970     }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
 129973   /* Check if the cursor is past the end of the docid range specified
 129974   ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag.  */
 129975   if( rc==SQLITE_OK && (
 129976         (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
 129977      || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
 129978   )){
 129979     pCsr->isEof = 1;
 129982   return rc;
 129986 ** Restart interation for expression pExpr so that the next call to
 129987 ** fts3EvalNext() visits the first row. Do not allow incremental 
 129988 ** loading or merging of phrase doclists for this iteration.
 129990 ** If *pRc is other than SQLITE_OK when this function is called, it is
 129991 ** a no-op. If an error occurs within this function, *pRc is set to an
 129992 ** SQLite error code before returning.
 129994 static void fts3EvalRestart(
 129995   Fts3Cursor *pCsr,
 129996   Fts3Expr *pExpr,
 129997   int *pRc
 129999   if( pExpr && *pRc==SQLITE_OK ){
 130000     Fts3Phrase *pPhrase = pExpr->pPhrase;
 130002     if( pPhrase ){
 130003       fts3EvalInvalidatePoslist(pPhrase);
 130004       if( pPhrase->bIncr ){
 130005         int i;
 130006         for(i=0; i<pPhrase->nToken; i++){
 130007           Fts3PhraseToken *pToken = &pPhrase->aToken[i];
 130008           assert( pToken->pDeferred==0 );
 130009           if( pToken->pSegcsr ){
 130010             sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
 130013         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
 130015       pPhrase->doclist.pNextDocid = 0;
 130016       pPhrase->doclist.iDocid = 0;
 130019     pExpr->iDocid = 0;
 130020     pExpr->bEof = 0;
 130021     pExpr->bStart = 0;
 130023     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
 130024     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
 130029 ** After allocating the Fts3Expr.aMI[] array for each phrase in the 
 130030 ** expression rooted at pExpr, the cursor iterates through all rows matched
 130031 ** by pExpr, calling this function for each row. This function increments
 130032 ** the values in Fts3Expr.aMI[] according to the position-list currently
 130033 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase 
 130034 ** expression nodes.
 130036 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
 130037   if( pExpr ){
 130038     Fts3Phrase *pPhrase = pExpr->pPhrase;
 130039     if( pPhrase && pPhrase->doclist.pList ){
 130040       int iCol = 0;
 130041       char *p = pPhrase->doclist.pList;
 130043       assert( *p );
 130044       while( 1 ){
 130045         u8 c = 0;
 130046         int iCnt = 0;
 130047         while( 0xFE & (*p | c) ){
 130048           if( (c&0x80)==0 ) iCnt++;
 130049           c = *p++ & 0x80;
 130052         /* aMI[iCol*3 + 1] = Number of occurrences
 130053         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
 130055         pExpr->aMI[iCol*3 + 1] += iCnt;
 130056         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
 130057         if( *p==0x00 ) break;
 130058         p++;
 130059         p += fts3GetVarint32(p, &iCol);
 130063     fts3EvalUpdateCounts(pExpr->pLeft);
 130064     fts3EvalUpdateCounts(pExpr->pRight);
 130069 ** Expression pExpr must be of type FTSQUERY_PHRASE.
 130071 ** If it is not already allocated and populated, this function allocates and
 130072 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
 130073 ** of a NEAR expression, then it also allocates and populates the same array
 130074 ** for all other phrases that are part of the NEAR expression.
 130076 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
 130077 ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
 130079 static int fts3EvalGatherStats(
 130080   Fts3Cursor *pCsr,               /* Cursor object */
 130081   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
 130083   int rc = SQLITE_OK;             /* Return code */
 130085   assert( pExpr->eType==FTSQUERY_PHRASE );
 130086   if( pExpr->aMI==0 ){
 130087     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 130088     Fts3Expr *pRoot;                /* Root of NEAR expression */
 130089     Fts3Expr *p;                    /* Iterator used for several purposes */
 130091     sqlite3_int64 iPrevId = pCsr->iPrevId;
 130092     sqlite3_int64 iDocid;
 130093     u8 bEof;
 130095     /* Find the root of the NEAR expression */
 130096     pRoot = pExpr;
 130097     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
 130098       pRoot = pRoot->pParent;
 130100     iDocid = pRoot->iDocid;
 130101     bEof = pRoot->bEof;
 130102     assert( pRoot->bStart );
 130104     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
 130105     for(p=pRoot; p; p=p->pLeft){
 130106       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
 130107       assert( pE->aMI==0 );
 130108       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
 130109       if( !pE->aMI ) return SQLITE_NOMEM;
 130110       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
 130113     fts3EvalRestart(pCsr, pRoot, &rc);
 130115     while( pCsr->isEof==0 && rc==SQLITE_OK ){
 130117       do {
 130118         /* Ensure the %_content statement is reset. */
 130119         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
 130120         assert( sqlite3_data_count(pCsr->pStmt)==0 );
 130122         /* Advance to the next document */
 130123         fts3EvalNextRow(pCsr, pRoot, &rc);
 130124         pCsr->isEof = pRoot->bEof;
 130125         pCsr->isRequireSeek = 1;
 130126         pCsr->isMatchinfoNeeded = 1;
 130127         pCsr->iPrevId = pRoot->iDocid;
 130128       }while( pCsr->isEof==0 
 130129            && pRoot->eType==FTSQUERY_NEAR 
 130130            && fts3EvalTestDeferredAndNear(pCsr, &rc) 
 130133       if( rc==SQLITE_OK && pCsr->isEof==0 ){
 130134         fts3EvalUpdateCounts(pRoot);
 130138     pCsr->isEof = 0;
 130139     pCsr->iPrevId = iPrevId;
 130141     if( bEof ){
 130142       pRoot->bEof = bEof;
 130143     }else{
 130144       /* Caution: pRoot may iterate through docids in ascending or descending
 130145       ** order. For this reason, even though it seems more defensive, the 
 130146       ** do loop can not be written:
 130148       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
 130150       fts3EvalRestart(pCsr, pRoot, &rc);
 130151       do {
 130152         fts3EvalNextRow(pCsr, pRoot, &rc);
 130153         assert( pRoot->bEof==0 );
 130154       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
 130155       fts3EvalTestDeferredAndNear(pCsr, &rc);
 130158   return rc;
 130162 ** This function is used by the matchinfo() module to query a phrase 
 130163 ** expression node for the following information:
 130165 **   1. The total number of occurrences of the phrase in each column of 
 130166 **      the FTS table (considering all rows), and
 130168 **   2. For each column, the number of rows in the table for which the
 130169 **      column contains at least one instance of the phrase.
 130171 ** If no error occurs, SQLITE_OK is returned and the values for each column
 130172 ** written into the array aiOut as follows:
 130174 **   aiOut[iCol*3 + 1] = Number of occurrences
 130175 **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
 130177 ** Caveats:
 130179 **   * If a phrase consists entirely of deferred tokens, then all output 
 130180 **     values are set to the number of documents in the table. In other
 130181 **     words we assume that very common tokens occur exactly once in each 
 130182 **     column of each row of the table.
 130184 **   * If a phrase contains some deferred tokens (and some non-deferred 
 130185 **     tokens), count the potential occurrence identified by considering
 130186 **     the non-deferred tokens instead of actual phrase occurrences.
 130188 **   * If the phrase is part of a NEAR expression, then only phrase instances
 130189 **     that meet the NEAR constraint are included in the counts.
 130191 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
 130192   Fts3Cursor *pCsr,               /* FTS cursor handle */
 130193   Fts3Expr *pExpr,                /* Phrase expression */
 130194   u32 *aiOut                      /* Array to write results into (see above) */
 130196   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 130197   int rc = SQLITE_OK;
 130198   int iCol;
 130200   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
 130201     assert( pCsr->nDoc>0 );
 130202     for(iCol=0; iCol<pTab->nColumn; iCol++){
 130203       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
 130204       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
 130206   }else{
 130207     rc = fts3EvalGatherStats(pCsr, pExpr);
 130208     if( rc==SQLITE_OK ){
 130209       assert( pExpr->aMI );
 130210       for(iCol=0; iCol<pTab->nColumn; iCol++){
 130211         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
 130212         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
 130217   return rc;
 130221 ** The expression pExpr passed as the second argument to this function
 130222 ** must be of type FTSQUERY_PHRASE. 
 130224 ** The returned value is either NULL or a pointer to a buffer containing
 130225 ** a position-list indicating the occurrences of the phrase in column iCol
 130226 ** of the current row. 
 130228 ** More specifically, the returned buffer contains 1 varint for each 
 130229 ** occurrence of the phrase in the column, stored using the normal (delta+2) 
 130230 ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
 130231 ** if the requested column contains "a b X c d X X" and the position-list
 130232 ** for 'X' is requested, the buffer returned may contain:
 130234 **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
 130236 ** This function works regardless of whether or not the phrase is deferred,
 130237 ** incremental, or neither.
 130239 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
 130240   Fts3Cursor *pCsr,               /* FTS3 cursor object */
 130241   Fts3Expr *pExpr,                /* Phrase to return doclist for */
 130242   int iCol,                       /* Column to return position list for */
 130243   char **ppOut                    /* OUT: Pointer to position list */
 130245   Fts3Phrase *pPhrase = pExpr->pPhrase;
 130246   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 130247   char *pIter;
 130248   int iThis;
 130249   sqlite3_int64 iDocid;
 130251   /* If this phrase is applies specifically to some column other than 
 130252   ** column iCol, return a NULL pointer.  */
 130253   *ppOut = 0;
 130254   assert( iCol>=0 && iCol<pTab->nColumn );
 130255   if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
 130256     return SQLITE_OK;
 130259   iDocid = pExpr->iDocid;
 130260   pIter = pPhrase->doclist.pList;
 130261   if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
 130262     int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
 130263     int iMul;                     /* +1 if csr dir matches index dir, else -1 */
 130264     int bOr = 0;
 130265     u8 bEof = 0;
 130266     u8 bTreeEof = 0;
 130267     Fts3Expr *p;                  /* Used to iterate from pExpr to root */
 130268     Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
 130270     /* Check if this phrase descends from an OR expression node. If not, 
 130271     ** return NULL. Otherwise, the entry that corresponds to docid 
 130272     ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
 130273     ** tree that the node is part of has been marked as EOF, but the node
 130274     ** itself is not EOF, then it may point to an earlier entry. */
 130275     pNear = pExpr;
 130276     for(p=pExpr->pParent; p; p=p->pParent){
 130277       if( p->eType==FTSQUERY_OR ) bOr = 1;
 130278       if( p->eType==FTSQUERY_NEAR ) pNear = p;
 130279       if( p->bEof ) bTreeEof = 1;
 130281     if( bOr==0 ) return SQLITE_OK;
 130283     /* This is the descendent of an OR node. In this case we cannot use
 130284     ** an incremental phrase. Load the entire doclist for the phrase
 130285     ** into memory in this case.  */
 130286     if( pPhrase->bIncr ){
 130287       int rc = SQLITE_OK;
 130288       int bEofSave = pExpr->bEof;
 130289       fts3EvalRestart(pCsr, pExpr, &rc);
 130290       while( rc==SQLITE_OK && !pExpr->bEof ){
 130291         fts3EvalNextRow(pCsr, pExpr, &rc);
 130292         if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
 130294       pIter = pPhrase->doclist.pList;
 130295       assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
 130296       if( rc!=SQLITE_OK ) return rc;
 130299     iMul = ((pCsr->bDesc==bDescDoclist) ? 1 : -1);
 130300     while( bTreeEof==1 
 130301         && pNear->bEof==0
 130302         && (DOCID_CMP(pNear->iDocid, pCsr->iPrevId) * iMul)<0
 130304       int rc = SQLITE_OK;
 130305       fts3EvalNextRow(pCsr, pExpr, &rc);
 130306       if( rc!=SQLITE_OK ) return rc;
 130307       iDocid = pExpr->iDocid;
 130308       pIter = pPhrase->doclist.pList;
 130311     bEof = (pPhrase->doclist.nAll==0);
 130312     assert( bDescDoclist==0 || bDescDoclist==1 );
 130313     assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
 130315     if( bEof==0 ){
 130316       if( pCsr->bDesc==bDescDoclist ){
 130317         int dummy;
 130318         if( pNear->bEof ){
 130319           /* This expression is already at EOF. So position it to point to the
 130320           ** last entry in the doclist at pPhrase->doclist.aAll[]. Variable
 130321           ** iDocid is already set for this entry, so all that is required is
 130322           ** to set pIter to point to the first byte of the last position-list
 130323           ** in the doclist. 
 130325           ** It would also be correct to set pIter and iDocid to zero. In
 130326           ** this case, the first call to sqltie3Fts4DoclistPrev() below
 130327           ** would also move the iterator to point to the last entry in the 
 130328           ** doclist. However, this is expensive, as to do so it has to 
 130329           ** iterate through the entire doclist from start to finish (since
 130330           ** it does not know the docid for the last entry).  */
 130331           pIter = &pPhrase->doclist.aAll[pPhrase->doclist.nAll-1];
 130332           fts3ReversePoslist(pPhrase->doclist.aAll, &pIter);
 130334         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
 130335           sqlite3Fts3DoclistPrev(
 130336               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
 130337               &pIter, &iDocid, &dummy, &bEof
 130340       }else{
 130341         if( pNear->bEof ){
 130342           pIter = 0;
 130343           iDocid = 0;
 130345         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
 130346           sqlite3Fts3DoclistNext(
 130347               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
 130348               &pIter, &iDocid, &bEof
 130354     if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
 130356   if( pIter==0 ) return SQLITE_OK;
 130358   if( *pIter==0x01 ){
 130359     pIter++;
 130360     pIter += fts3GetVarint32(pIter, &iThis);
 130361   }else{
 130362     iThis = 0;
 130364   while( iThis<iCol ){
 130365     fts3ColumnlistCopy(0, &pIter);
 130366     if( *pIter==0x00 ) return 0;
 130367     pIter++;
 130368     pIter += fts3GetVarint32(pIter, &iThis);
 130371   *ppOut = ((iCol==iThis)?pIter:0);
 130372   return SQLITE_OK;
 130376 ** Free all components of the Fts3Phrase structure that were allocated by
 130377 ** the eval module. Specifically, this means to free:
 130379 **   * the contents of pPhrase->doclist, and
 130380 **   * any Fts3MultiSegReader objects held by phrase tokens.
 130382 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
 130383   if( pPhrase ){
 130384     int i;
 130385     sqlite3_free(pPhrase->doclist.aAll);
 130386     fts3EvalInvalidatePoslist(pPhrase);
 130387     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
 130388     for(i=0; i<pPhrase->nToken; i++){
 130389       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
 130390       pPhrase->aToken[i].pSegcsr = 0;
 130397 ** Return SQLITE_CORRUPT_VTAB.
 130399 #ifdef SQLITE_DEBUG
 130400 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
 130401   return SQLITE_CORRUPT_VTAB;
 130403 #endif
 130405 #if !SQLITE_CORE
 130407 ** Initialize API pointer table, if required.
 130409 #ifdef _WIN32
 130410 __declspec(dllexport)
 130411 #endif
 130412 SQLITE_API int sqlite3_fts3_init(
 130413   sqlite3 *db, 
 130414   char **pzErrMsg,
 130415   const sqlite3_api_routines *pApi
 130417   SQLITE_EXTENSION_INIT2(pApi)
 130418   return sqlite3Fts3Init(db);
 130420 #endif
 130422 #endif
 130424 /************** End of fts3.c ************************************************/
 130425 /************** Begin file fts3_aux.c ****************************************/
 130427 ** 2011 Jan 27
 130429 ** The author disclaims copyright to this source code.  In place of
 130430 ** a legal notice, here is a blessing:
 130432 **    May you do good and not evil.
 130433 **    May you find forgiveness for yourself and forgive others.
 130434 **    May you share freely, never taking more than you give.
 130436 ******************************************************************************
 130439 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 130441 /* #include <string.h> */
 130442 /* #include <assert.h> */
 130444 typedef struct Fts3auxTable Fts3auxTable;
 130445 typedef struct Fts3auxCursor Fts3auxCursor;
 130447 struct Fts3auxTable {
 130448   sqlite3_vtab base;              /* Base class used by SQLite core */
 130449   Fts3Table *pFts3Tab;
 130452 struct Fts3auxCursor {
 130453   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 130454   Fts3MultiSegReader csr;        /* Must be right after "base" */
 130455   Fts3SegFilter filter;
 130456   char *zStop;
 130457   int nStop;                      /* Byte-length of string zStop */
 130458   int iLangid;                    /* Language id to query */
 130459   int isEof;                      /* True if cursor is at EOF */
 130460   sqlite3_int64 iRowid;           /* Current rowid */
 130462   int iCol;                       /* Current value of 'col' column */
 130463   int nStat;                      /* Size of aStat[] array */
 130464   struct Fts3auxColstats {
 130465     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
 130466     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
 130467   } *aStat;
 130471 ** Schema of the terms table.
 130473 #define FTS3_AUX_SCHEMA \
 130474   "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
 130477 ** This function does all the work for both the xConnect and xCreate methods.
 130478 ** These tables have no persistent representation of their own, so xConnect
 130479 ** and xCreate are identical operations.
 130481 static int fts3auxConnectMethod(
 130482   sqlite3 *db,                    /* Database connection */
 130483   void *pUnused,                  /* Unused */
 130484   int argc,                       /* Number of elements in argv array */
 130485   const char * const *argv,       /* xCreate/xConnect argument array */
 130486   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 130487   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 130489   char const *zDb;                /* Name of database (e.g. "main") */
 130490   char const *zFts3;              /* Name of fts3 table */
 130491   int nDb;                        /* Result of strlen(zDb) */
 130492   int nFts3;                      /* Result of strlen(zFts3) */
 130493   int nByte;                      /* Bytes of space to allocate here */
 130494   int rc;                         /* value returned by declare_vtab() */
 130495   Fts3auxTable *p;                /* Virtual table object to return */
 130497   UNUSED_PARAMETER(pUnused);
 130499   /* The user should invoke this in one of two forms:
 130501   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
 130502   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
 130504   if( argc!=4 && argc!=5 ) goto bad_args;
 130506   zDb = argv[1]; 
 130507   nDb = (int)strlen(zDb);
 130508   if( argc==5 ){
 130509     if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
 130510       zDb = argv[3]; 
 130511       nDb = (int)strlen(zDb);
 130512       zFts3 = argv[4];
 130513     }else{
 130514       goto bad_args;
 130516   }else{
 130517     zFts3 = argv[3];
 130519   nFts3 = (int)strlen(zFts3);
 130521   rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
 130522   if( rc!=SQLITE_OK ) return rc;
 130524   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
 130525   p = (Fts3auxTable *)sqlite3_malloc(nByte);
 130526   if( !p ) return SQLITE_NOMEM;
 130527   memset(p, 0, nByte);
 130529   p->pFts3Tab = (Fts3Table *)&p[1];
 130530   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
 130531   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
 130532   p->pFts3Tab->db = db;
 130533   p->pFts3Tab->nIndex = 1;
 130535   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
 130536   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
 130537   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
 130539   *ppVtab = (sqlite3_vtab *)p;
 130540   return SQLITE_OK;
 130542  bad_args:
 130543   *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
 130544   return SQLITE_ERROR;
 130548 ** This function does the work for both the xDisconnect and xDestroy methods.
 130549 ** These tables have no persistent representation of their own, so xDisconnect
 130550 ** and xDestroy are identical operations.
 130552 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
 130553   Fts3auxTable *p = (Fts3auxTable *)pVtab;
 130554   Fts3Table *pFts3 = p->pFts3Tab;
 130555   int i;
 130557   /* Free any prepared statements held */
 130558   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
 130559     sqlite3_finalize(pFts3->aStmt[i]);
 130561   sqlite3_free(pFts3->zSegmentsTbl);
 130562   sqlite3_free(p);
 130563   return SQLITE_OK;
 130566 #define FTS4AUX_EQ_CONSTRAINT 1
 130567 #define FTS4AUX_GE_CONSTRAINT 2
 130568 #define FTS4AUX_LE_CONSTRAINT 4
 130571 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
 130573 static int fts3auxBestIndexMethod(
 130574   sqlite3_vtab *pVTab, 
 130575   sqlite3_index_info *pInfo
 130577   int i;
 130578   int iEq = -1;
 130579   int iGe = -1;
 130580   int iLe = -1;
 130581   int iLangid = -1;
 130582   int iNext = 1;                  /* Next free argvIndex value */
 130584   UNUSED_PARAMETER(pVTab);
 130586   /* This vtab delivers always results in "ORDER BY term ASC" order. */
 130587   if( pInfo->nOrderBy==1 
 130588    && pInfo->aOrderBy[0].iColumn==0 
 130589    && pInfo->aOrderBy[0].desc==0
 130591     pInfo->orderByConsumed = 1;
 130594   /* Search for equality and range constraints on the "term" column. 
 130595   ** And equality constraints on the hidden "languageid" column. */
 130596   for(i=0; i<pInfo->nConstraint; i++){
 130597     if( pInfo->aConstraint[i].usable ){
 130598       int op = pInfo->aConstraint[i].op;
 130599       int iCol = pInfo->aConstraint[i].iColumn;
 130601       if( iCol==0 ){
 130602         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
 130603         if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
 130604         if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
 130605         if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
 130606         if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
 130608       if( iCol==4 ){
 130609         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
 130614   if( iEq>=0 ){
 130615     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
 130616     pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
 130617     pInfo->estimatedCost = 5;
 130618   }else{
 130619     pInfo->idxNum = 0;
 130620     pInfo->estimatedCost = 20000;
 130621     if( iGe>=0 ){
 130622       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
 130623       pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
 130624       pInfo->estimatedCost /= 2;
 130626     if( iLe>=0 ){
 130627       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
 130628       pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
 130629       pInfo->estimatedCost /= 2;
 130632   if( iLangid>=0 ){
 130633     pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
 130634     pInfo->estimatedCost--;
 130637   return SQLITE_OK;
 130641 ** xOpen - Open a cursor.
 130643 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
 130644   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
 130646   UNUSED_PARAMETER(pVTab);
 130648   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
 130649   if( !pCsr ) return SQLITE_NOMEM;
 130650   memset(pCsr, 0, sizeof(Fts3auxCursor));
 130652   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
 130653   return SQLITE_OK;
 130657 ** xClose - Close a cursor.
 130659 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
 130660   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 130661   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 130663   sqlite3Fts3SegmentsClose(pFts3);
 130664   sqlite3Fts3SegReaderFinish(&pCsr->csr);
 130665   sqlite3_free((void *)pCsr->filter.zTerm);
 130666   sqlite3_free(pCsr->zStop);
 130667   sqlite3_free(pCsr->aStat);
 130668   sqlite3_free(pCsr);
 130669   return SQLITE_OK;
 130672 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
 130673   if( nSize>pCsr->nStat ){
 130674     struct Fts3auxColstats *aNew;
 130675     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
 130676         sizeof(struct Fts3auxColstats) * nSize
 130678     if( aNew==0 ) return SQLITE_NOMEM;
 130679     memset(&aNew[pCsr->nStat], 0, 
 130680         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
 130682     pCsr->aStat = aNew;
 130683     pCsr->nStat = nSize;
 130685   return SQLITE_OK;
 130689 ** xNext - Advance the cursor to the next row, if any.
 130691 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
 130692   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 130693   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 130694   int rc;
 130696   /* Increment our pretend rowid value. */
 130697   pCsr->iRowid++;
 130699   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
 130700     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
 130703   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
 130704   if( rc==SQLITE_ROW ){
 130705     int i = 0;
 130706     int nDoclist = pCsr->csr.nDoclist;
 130707     char *aDoclist = pCsr->csr.aDoclist;
 130708     int iCol;
 130710     int eState = 0;
 130712     if( pCsr->zStop ){
 130713       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
 130714       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
 130715       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
 130716         pCsr->isEof = 1;
 130717         return SQLITE_OK;
 130721     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
 130722     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
 130723     iCol = 0;
 130725     while( i<nDoclist ){
 130726       sqlite3_int64 v = 0;
 130728       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
 130729       switch( eState ){
 130730         /* State 0. In this state the integer just read was a docid. */
 130731         case 0:
 130732           pCsr->aStat[0].nDoc++;
 130733           eState = 1;
 130734           iCol = 0;
 130735           break;
 130737         /* State 1. In this state we are expecting either a 1, indicating
 130738         ** that the following integer will be a column number, or the
 130739         ** start of a position list for column 0.  
 130741         ** The only difference between state 1 and state 2 is that if the
 130742         ** integer encountered in state 1 is not 0 or 1, then we need to
 130743         ** increment the column 0 "nDoc" count for this term.
 130745         case 1:
 130746           assert( iCol==0 );
 130747           if( v>1 ){
 130748             pCsr->aStat[1].nDoc++;
 130750           eState = 2;
 130751           /* fall through */
 130753         case 2:
 130754           if( v==0 ){       /* 0x00. Next integer will be a docid. */
 130755             eState = 0;
 130756           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
 130757             eState = 3;
 130758           }else{            /* 2 or greater. A position. */
 130759             pCsr->aStat[iCol+1].nOcc++;
 130760             pCsr->aStat[0].nOcc++;
 130762           break;
 130764         /* State 3. The integer just read is a column number. */
 130765         default: assert( eState==3 );
 130766           iCol = (int)v;
 130767           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
 130768           pCsr->aStat[iCol+1].nDoc++;
 130769           eState = 2;
 130770           break;
 130774     pCsr->iCol = 0;
 130775     rc = SQLITE_OK;
 130776   }else{
 130777     pCsr->isEof = 1;
 130779   return rc;
 130783 ** xFilter - Initialize a cursor to point at the start of its data.
 130785 static int fts3auxFilterMethod(
 130786   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
 130787   int idxNum,                     /* Strategy index */
 130788   const char *idxStr,             /* Unused */
 130789   int nVal,                       /* Number of elements in apVal */
 130790   sqlite3_value **apVal           /* Arguments for the indexing scheme */
 130792   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 130793   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
 130794   int rc;
 130795   int isScan = 0;
 130796   int iLangVal = 0;               /* Language id to query */
 130798   int iEq = -1;                   /* Index of term=? value in apVal */
 130799   int iGe = -1;                   /* Index of term>=? value in apVal */
 130800   int iLe = -1;                   /* Index of term<=? value in apVal */
 130801   int iLangid = -1;               /* Index of languageid=? value in apVal */
 130802   int iNext = 0;
 130804   UNUSED_PARAMETER(nVal);
 130805   UNUSED_PARAMETER(idxStr);
 130807   assert( idxStr==0 );
 130808   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
 130809        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
 130810        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
 130813   if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
 130814     iEq = iNext++;
 130815   }else{
 130816     isScan = 1;
 130817     if( idxNum & FTS4AUX_GE_CONSTRAINT ){
 130818       iGe = iNext++;
 130820     if( idxNum & FTS4AUX_LE_CONSTRAINT ){
 130821       iLe = iNext++;
 130824   if( iNext<nVal ){
 130825     iLangid = iNext++;
 130828   /* In case this cursor is being reused, close and zero it. */
 130829   testcase(pCsr->filter.zTerm);
 130830   sqlite3Fts3SegReaderFinish(&pCsr->csr);
 130831   sqlite3_free((void *)pCsr->filter.zTerm);
 130832   sqlite3_free(pCsr->aStat);
 130833   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
 130835   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
 130836   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
 130838   if( iEq>=0 || iGe>=0 ){
 130839     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
 130840     assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
 130841     if( zStr ){
 130842       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
 130843       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
 130844       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
 130848   if( iLe>=0 ){
 130849     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
 130850     pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
 130851     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
 130854   if( iLangid>=0 ){
 130855     iLangVal = sqlite3_value_int(apVal[iLangid]);
 130857     /* If the user specified a negative value for the languageid, use zero
 130858     ** instead. This works, as the "languageid=?" constraint will also
 130859     ** be tested by the VDBE layer. The test will always be false (since
 130860     ** this module will not return a row with a negative languageid), and
 130861     ** so the overall query will return zero rows.  */
 130862     if( iLangVal<0 ) iLangVal = 0;
 130864   pCsr->iLangid = iLangVal;
 130866   rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
 130867       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
 130869   if( rc==SQLITE_OK ){
 130870     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
 130873   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
 130874   return rc;
 130878 ** xEof - Return true if the cursor is at EOF, or false otherwise.
 130880 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
 130881   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 130882   return pCsr->isEof;
 130886 ** xColumn - Return a column value.
 130888 static int fts3auxColumnMethod(
 130889   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 130890   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
 130891   int iCol                        /* Index of column to read value from */
 130893   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
 130895   assert( p->isEof==0 );
 130896   switch( iCol ){
 130897     case 0: /* term */
 130898       sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
 130899       break;
 130901     case 1: /* col */
 130902       if( p->iCol ){
 130903         sqlite3_result_int(pCtx, p->iCol-1);
 130904       }else{
 130905         sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
 130907       break;
 130909     case 2: /* documents */
 130910       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
 130911       break;
 130913     case 3: /* occurrences */
 130914       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
 130915       break;
 130917     default: /* languageid */
 130918       assert( iCol==4 );
 130919       sqlite3_result_int(pCtx, p->iLangid);
 130920       break;
 130923   return SQLITE_OK;
 130927 ** xRowid - Return the current rowid for the cursor.
 130929 static int fts3auxRowidMethod(
 130930   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 130931   sqlite_int64 *pRowid            /* OUT: Rowid value */
 130933   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
 130934   *pRowid = pCsr->iRowid;
 130935   return SQLITE_OK;
 130939 ** Register the fts3aux module with database connection db. Return SQLITE_OK
 130940 ** if successful or an error code if sqlite3_create_module() fails.
 130942 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
 130943   static const sqlite3_module fts3aux_module = {
 130944      0,                           /* iVersion      */
 130945      fts3auxConnectMethod,        /* xCreate       */
 130946      fts3auxConnectMethod,        /* xConnect      */
 130947      fts3auxBestIndexMethod,      /* xBestIndex    */
 130948      fts3auxDisconnectMethod,     /* xDisconnect   */
 130949      fts3auxDisconnectMethod,     /* xDestroy      */
 130950      fts3auxOpenMethod,           /* xOpen         */
 130951      fts3auxCloseMethod,          /* xClose        */
 130952      fts3auxFilterMethod,         /* xFilter       */
 130953      fts3auxNextMethod,           /* xNext         */
 130954      fts3auxEofMethod,            /* xEof          */
 130955      fts3auxColumnMethod,         /* xColumn       */
 130956      fts3auxRowidMethod,          /* xRowid        */
 130957      0,                           /* xUpdate       */
 130958      0,                           /* xBegin        */
 130959      0,                           /* xSync         */
 130960      0,                           /* xCommit       */
 130961      0,                           /* xRollback     */
 130962      0,                           /* xFindFunction */
 130963      0,                           /* xRename       */
 130964      0,                           /* xSavepoint    */
 130965      0,                           /* xRelease      */
 130966      0                            /* xRollbackTo   */
 130968   int rc;                         /* Return code */
 130970   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
 130971   return rc;
 130974 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 130976 /************** End of fts3_aux.c ********************************************/
 130977 /************** Begin file fts3_expr.c ***************************************/
 130979 ** 2008 Nov 28
 130981 ** The author disclaims copyright to this source code.  In place of
 130982 ** a legal notice, here is a blessing:
 130984 **    May you do good and not evil.
 130985 **    May you find forgiveness for yourself and forgive others.
 130986 **    May you share freely, never taking more than you give.
 130988 ******************************************************************************
 130990 ** This module contains code that implements a parser for fts3 query strings
 130991 ** (the right-hand argument to the MATCH operator). Because the supported 
 130992 ** syntax is relatively simple, the whole tokenizer/parser system is
 130993 ** hand-coded. 
 130995 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 130998 ** By default, this module parses the legacy syntax that has been 
 130999 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
 131000 ** is defined, then it uses the new syntax. The differences between
 131001 ** the new and the old syntaxes are:
 131003 **  a) The new syntax supports parenthesis. The old does not.
 131005 **  b) The new syntax supports the AND and NOT operators. The old does not.
 131007 **  c) The old syntax supports the "-" token qualifier. This is not 
 131008 **     supported by the new syntax (it is replaced by the NOT operator).
 131010 **  d) When using the old syntax, the OR operator has a greater precedence
 131011 **     than an implicit AND. When using the new, both implicity and explicit
 131012 **     AND operators have a higher precedence than OR.
 131014 ** If compiled with SQLITE_TEST defined, then this module exports the
 131015 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
 131016 ** to zero causes the module to use the old syntax. If it is set to 
 131017 ** non-zero the new syntax is activated. This is so both syntaxes can
 131018 ** be tested using a single build of testfixture.
 131020 ** The following describes the syntax supported by the fts3 MATCH
 131021 ** operator in a similar format to that used by the lemon parser
 131022 ** generator. This module does not use actually lemon, it uses a
 131023 ** custom parser.
 131025 **   query ::= andexpr (OR andexpr)*.
 131027 **   andexpr ::= notexpr (AND? notexpr)*.
 131029 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
 131030 **   notexpr ::= LP query RP.
 131032 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
 131034 **   distance_opt ::= .
 131035 **   distance_opt ::= / INTEGER.
 131037 **   phrase ::= TOKEN.
 131038 **   phrase ::= COLUMN:TOKEN.
 131039 **   phrase ::= "TOKEN TOKEN TOKEN...".
 131042 #ifdef SQLITE_TEST
 131043 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
 131044 #else
 131045 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
 131046 #  define sqlite3_fts3_enable_parentheses 1
 131047 # else
 131048 #  define sqlite3_fts3_enable_parentheses 0
 131049 # endif
 131050 #endif
 131053 ** Default span for NEAR operators.
 131055 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
 131057 /* #include <string.h> */
 131058 /* #include <assert.h> */
 131061 ** isNot:
 131062 **   This variable is used by function getNextNode(). When getNextNode() is
 131063 **   called, it sets ParseContext.isNot to true if the 'next node' is a 
 131064 **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
 131065 **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
 131066 **   zero.
 131068 typedef struct ParseContext ParseContext;
 131069 struct ParseContext {
 131070   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
 131071   int iLangid;                        /* Language id used with tokenizer */
 131072   const char **azCol;                 /* Array of column names for fts3 table */
 131073   int bFts4;                          /* True to allow FTS4-only syntax */
 131074   int nCol;                           /* Number of entries in azCol[] */
 131075   int iDefaultCol;                    /* Default column to query */
 131076   int isNot;                          /* True if getNextNode() sees a unary - */
 131077   sqlite3_context *pCtx;              /* Write error message here */
 131078   int nNest;                          /* Number of nested brackets */
 131082 ** This function is equivalent to the standard isspace() function. 
 131084 ** The standard isspace() can be awkward to use safely, because although it
 131085 ** is defined to accept an argument of type int, its behavior when passed
 131086 ** an integer that falls outside of the range of the unsigned char type
 131087 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
 131088 ** is defined to accept an argument of type char, and always returns 0 for
 131089 ** any values that fall outside of the range of the unsigned char type (i.e.
 131090 ** negative values).
 131092 static int fts3isspace(char c){
 131093   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
 131097 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
 131098 ** zero the memory before returning a pointer to it. If unsuccessful, 
 131099 ** return NULL.
 131101 static void *fts3MallocZero(int nByte){
 131102   void *pRet = sqlite3_malloc(nByte);
 131103   if( pRet ) memset(pRet, 0, nByte);
 131104   return pRet;
 131107 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
 131108   sqlite3_tokenizer *pTokenizer,
 131109   int iLangid,
 131110   const char *z,
 131111   int n,
 131112   sqlite3_tokenizer_cursor **ppCsr
 131114   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 131115   sqlite3_tokenizer_cursor *pCsr = 0;
 131116   int rc;
 131118   rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
 131119   assert( rc==SQLITE_OK || pCsr==0 );
 131120   if( rc==SQLITE_OK ){
 131121     pCsr->pTokenizer = pTokenizer;
 131122     if( pModule->iVersion>=1 ){
 131123       rc = pModule->xLanguageid(pCsr, iLangid);
 131124       if( rc!=SQLITE_OK ){
 131125         pModule->xClose(pCsr);
 131126         pCsr = 0;
 131130   *ppCsr = pCsr;
 131131   return rc;
 131135 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
 131136 ** call fts3ExprParse(). So this forward declaration is required.
 131138 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
 131141 ** Extract the next token from buffer z (length n) using the tokenizer
 131142 ** and other information (column names etc.) in pParse. Create an Fts3Expr
 131143 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
 131144 ** single token and set *ppExpr to point to it. If the end of the buffer is
 131145 ** reached before a token is found, set *ppExpr to zero. It is the
 131146 ** responsibility of the caller to eventually deallocate the allocated 
 131147 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
 131149 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
 131150 ** fails.
 131152 static int getNextToken(
 131153   ParseContext *pParse,                   /* fts3 query parse context */
 131154   int iCol,                               /* Value for Fts3Phrase.iColumn */
 131155   const char *z, int n,                   /* Input string */
 131156   Fts3Expr **ppExpr,                      /* OUT: expression */
 131157   int *pnConsumed                         /* OUT: Number of bytes consumed */
 131159   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
 131160   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 131161   int rc;
 131162   sqlite3_tokenizer_cursor *pCursor;
 131163   Fts3Expr *pRet = 0;
 131164   int nConsumed = 0;
 131166   rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
 131167   if( rc==SQLITE_OK ){
 131168     const char *zToken;
 131169     int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
 131170     int nByte;                               /* total space to allocate */
 131172     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
 131174     if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
 131175       int i;
 131176       if( rc==SQLITE_DONE ) iStart = n;
 131177       for(i=0; i<iStart; i++){
 131178         if( z[i]=='(' ){
 131179           pParse->nNest++;
 131180           rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
 131181           if( rc==SQLITE_OK && !pRet ){
 131182             rc = SQLITE_DONE;
 131184           nConsumed = (int)(i + 1 + nConsumed);
 131185           break;
 131188         if( z[i]==')' ){
 131189           rc = SQLITE_DONE;
 131190           pParse->nNest--;
 131191           nConsumed = i+1;
 131192           break;
 131197     if( nConsumed==0 && rc==SQLITE_OK ){
 131198       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
 131199       pRet = (Fts3Expr *)fts3MallocZero(nByte);
 131200       if( !pRet ){
 131201         rc = SQLITE_NOMEM;
 131202       }else{
 131203         pRet->eType = FTSQUERY_PHRASE;
 131204         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
 131205         pRet->pPhrase->nToken = 1;
 131206         pRet->pPhrase->iColumn = iCol;
 131207         pRet->pPhrase->aToken[0].n = nToken;
 131208         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
 131209         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
 131211         if( iEnd<n && z[iEnd]=='*' ){
 131212           pRet->pPhrase->aToken[0].isPrefix = 1;
 131213           iEnd++;
 131216         while( 1 ){
 131217           if( !sqlite3_fts3_enable_parentheses 
 131218            && iStart>0 && z[iStart-1]=='-' 
 131220             pParse->isNot = 1;
 131221             iStart--;
 131222           }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
 131223             pRet->pPhrase->aToken[0].bFirst = 1;
 131224             iStart--;
 131225           }else{
 131226             break;
 131231       nConsumed = iEnd;
 131234     pModule->xClose(pCursor);
 131237   *pnConsumed = nConsumed;
 131238   *ppExpr = pRet;
 131239   return rc;
 131244 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
 131245 ** then free the old allocation.
 131247 static void *fts3ReallocOrFree(void *pOrig, int nNew){
 131248   void *pRet = sqlite3_realloc(pOrig, nNew);
 131249   if( !pRet ){
 131250     sqlite3_free(pOrig);
 131252   return pRet;
 131256 ** Buffer zInput, length nInput, contains the contents of a quoted string
 131257 ** that appeared as part of an fts3 query expression. Neither quote character
 131258 ** is included in the buffer. This function attempts to tokenize the entire
 131259 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
 131260 ** containing the results.
 131262 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
 131263 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
 131264 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
 131265 ** to 0.
 131267 static int getNextString(
 131268   ParseContext *pParse,                   /* fts3 query parse context */
 131269   const char *zInput, int nInput,         /* Input string */
 131270   Fts3Expr **ppExpr                       /* OUT: expression */
 131272   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
 131273   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 131274   int rc;
 131275   Fts3Expr *p = 0;
 131276   sqlite3_tokenizer_cursor *pCursor = 0;
 131277   char *zTemp = 0;
 131278   int nTemp = 0;
 131280   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
 131281   int nToken = 0;
 131283   /* The final Fts3Expr data structure, including the Fts3Phrase,
 131284   ** Fts3PhraseToken structures token buffers are all stored as a single 
 131285   ** allocation so that the expression can be freed with a single call to
 131286   ** sqlite3_free(). Setting this up requires a two pass approach.
 131288   ** The first pass, in the block below, uses a tokenizer cursor to iterate
 131289   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
 131290   ** to assemble data in two dynamic buffers:
 131292   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
 131293   **             structure, followed by the array of Fts3PhraseToken 
 131294   **             structures. This pass only populates the Fts3PhraseToken array.
 131296   **   Buffer zTemp: Contains copies of all tokens.
 131298   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
 131299   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
 131300   ** structures.
 131302   rc = sqlite3Fts3OpenTokenizer(
 131303       pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
 131304   if( rc==SQLITE_OK ){
 131305     int ii;
 131306     for(ii=0; rc==SQLITE_OK; ii++){
 131307       const char *zByte;
 131308       int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
 131309       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
 131310       if( rc==SQLITE_OK ){
 131311         Fts3PhraseToken *pToken;
 131313         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
 131314         if( !p ) goto no_mem;
 131316         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
 131317         if( !zTemp ) goto no_mem;
 131319         assert( nToken==ii );
 131320         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
 131321         memset(pToken, 0, sizeof(Fts3PhraseToken));
 131323         memcpy(&zTemp[nTemp], zByte, nByte);
 131324         nTemp += nByte;
 131326         pToken->n = nByte;
 131327         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
 131328         pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
 131329         nToken = ii+1;
 131333     pModule->xClose(pCursor);
 131334     pCursor = 0;
 131337   if( rc==SQLITE_DONE ){
 131338     int jj;
 131339     char *zBuf = 0;
 131341     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
 131342     if( !p ) goto no_mem;
 131343     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
 131344     p->eType = FTSQUERY_PHRASE;
 131345     p->pPhrase = (Fts3Phrase *)&p[1];
 131346     p->pPhrase->iColumn = pParse->iDefaultCol;
 131347     p->pPhrase->nToken = nToken;
 131349     zBuf = (char *)&p->pPhrase->aToken[nToken];
 131350     if( zTemp ){
 131351       memcpy(zBuf, zTemp, nTemp);
 131352       sqlite3_free(zTemp);
 131353     }else{
 131354       assert( nTemp==0 );
 131357     for(jj=0; jj<p->pPhrase->nToken; jj++){
 131358       p->pPhrase->aToken[jj].z = zBuf;
 131359       zBuf += p->pPhrase->aToken[jj].n;
 131361     rc = SQLITE_OK;
 131364   *ppExpr = p;
 131365   return rc;
 131366 no_mem:
 131368   if( pCursor ){
 131369     pModule->xClose(pCursor);
 131371   sqlite3_free(zTemp);
 131372   sqlite3_free(p);
 131373   *ppExpr = 0;
 131374   return SQLITE_NOMEM;
 131378 ** The output variable *ppExpr is populated with an allocated Fts3Expr 
 131379 ** structure, or set to 0 if the end of the input buffer is reached.
 131381 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
 131382 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
 131383 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
 131385 static int getNextNode(
 131386   ParseContext *pParse,                   /* fts3 query parse context */
 131387   const char *z, int n,                   /* Input string */
 131388   Fts3Expr **ppExpr,                      /* OUT: expression */
 131389   int *pnConsumed                         /* OUT: Number of bytes consumed */
 131391   static const struct Fts3Keyword {
 131392     char *z;                              /* Keyword text */
 131393     unsigned char n;                      /* Length of the keyword */
 131394     unsigned char parenOnly;              /* Only valid in paren mode */
 131395     unsigned char eType;                  /* Keyword code */
 131396   } aKeyword[] = {
 131397     { "OR" ,  2, 0, FTSQUERY_OR   },
 131398     { "AND",  3, 1, FTSQUERY_AND  },
 131399     { "NOT",  3, 1, FTSQUERY_NOT  },
 131400     { "NEAR", 4, 0, FTSQUERY_NEAR }
 131402   int ii;
 131403   int iCol;
 131404   int iColLen;
 131405   int rc;
 131406   Fts3Expr *pRet = 0;
 131408   const char *zInput = z;
 131409   int nInput = n;
 131411   pParse->isNot = 0;
 131413   /* Skip over any whitespace before checking for a keyword, an open or
 131414   ** close bracket, or a quoted string. 
 131416   while( nInput>0 && fts3isspace(*zInput) ){
 131417     nInput--;
 131418     zInput++;
 131420   if( nInput==0 ){
 131421     return SQLITE_DONE;
 131424   /* See if we are dealing with a keyword. */
 131425   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
 131426     const struct Fts3Keyword *pKey = &aKeyword[ii];
 131428     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
 131429       continue;
 131432     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
 131433       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
 131434       int nKey = pKey->n;
 131435       char cNext;
 131437       /* If this is a "NEAR" keyword, check for an explicit nearness. */
 131438       if( pKey->eType==FTSQUERY_NEAR ){
 131439         assert( nKey==4 );
 131440         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
 131441           nNear = 0;
 131442           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
 131443             nNear = nNear * 10 + (zInput[nKey] - '0');
 131448       /* At this point this is probably a keyword. But for that to be true,
 131449       ** the next byte must contain either whitespace, an open or close
 131450       ** parenthesis, a quote character, or EOF. 
 131452       cNext = zInput[nKey];
 131453       if( fts3isspace(cNext) 
 131454        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
 131456         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
 131457         if( !pRet ){
 131458           return SQLITE_NOMEM;
 131460         pRet->eType = pKey->eType;
 131461         pRet->nNear = nNear;
 131462         *ppExpr = pRet;
 131463         *pnConsumed = (int)((zInput - z) + nKey);
 131464         return SQLITE_OK;
 131467       /* Turns out that wasn't a keyword after all. This happens if the
 131468       ** user has supplied a token such as "ORacle". Continue.
 131473   /* See if we are dealing with a quoted phrase. If this is the case, then
 131474   ** search for the closing quote and pass the whole string to getNextString()
 131475   ** for processing. This is easy to do, as fts3 has no syntax for escaping
 131476   ** a quote character embedded in a string.
 131478   if( *zInput=='"' ){
 131479     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
 131480     *pnConsumed = (int)((zInput - z) + ii + 1);
 131481     if( ii==nInput ){
 131482       return SQLITE_ERROR;
 131484     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
 131488   /* If control flows to this point, this must be a regular token, or 
 131489   ** the end of the input. Read a regular token using the sqlite3_tokenizer
 131490   ** interface. Before doing so, figure out if there is an explicit
 131491   ** column specifier for the token. 
 131493   ** TODO: Strangely, it is not possible to associate a column specifier
 131494   ** with a quoted phrase, only with a single token. Not sure if this was
 131495   ** an implementation artifact or an intentional decision when fts3 was
 131496   ** first implemented. Whichever it was, this module duplicates the 
 131497   ** limitation.
 131499   iCol = pParse->iDefaultCol;
 131500   iColLen = 0;
 131501   for(ii=0; ii<pParse->nCol; ii++){
 131502     const char *zStr = pParse->azCol[ii];
 131503     int nStr = (int)strlen(zStr);
 131504     if( nInput>nStr && zInput[nStr]==':' 
 131505      && sqlite3_strnicmp(zStr, zInput, nStr)==0 
 131507       iCol = ii;
 131508       iColLen = (int)((zInput - z) + nStr + 1);
 131509       break;
 131512   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
 131513   *pnConsumed += iColLen;
 131514   return rc;
 131518 ** The argument is an Fts3Expr structure for a binary operator (any type
 131519 ** except an FTSQUERY_PHRASE). Return an integer value representing the
 131520 ** precedence of the operator. Lower values have a higher precedence (i.e.
 131521 ** group more tightly). For example, in the C language, the == operator
 131522 ** groups more tightly than ||, and would therefore have a higher precedence.
 131524 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
 131525 ** is defined), the order of the operators in precedence from highest to
 131526 ** lowest is:
 131528 **   NEAR
 131529 **   NOT
 131530 **   AND (including implicit ANDs)
 131531 **   OR
 131533 ** Note that when using the old query syntax, the OR operator has a higher
 131534 ** precedence than the AND operator.
 131536 static int opPrecedence(Fts3Expr *p){
 131537   assert( p->eType!=FTSQUERY_PHRASE );
 131538   if( sqlite3_fts3_enable_parentheses ){
 131539     return p->eType;
 131540   }else if( p->eType==FTSQUERY_NEAR ){
 131541     return 1;
 131542   }else if( p->eType==FTSQUERY_OR ){
 131543     return 2;
 131545   assert( p->eType==FTSQUERY_AND );
 131546   return 3;
 131550 ** Argument ppHead contains a pointer to the current head of a query 
 131551 ** expression tree being parsed. pPrev is the expression node most recently
 131552 ** inserted into the tree. This function adds pNew, which is always a binary
 131553 ** operator node, into the expression tree based on the relative precedence
 131554 ** of pNew and the existing nodes of the tree. This may result in the head
 131555 ** of the tree changing, in which case *ppHead is set to the new root node.
 131557 static void insertBinaryOperator(
 131558   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
 131559   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
 131560   Fts3Expr *pNew           /* New binary node to insert into expression tree */
 131562   Fts3Expr *pSplit = pPrev;
 131563   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
 131564     pSplit = pSplit->pParent;
 131567   if( pSplit->pParent ){
 131568     assert( pSplit->pParent->pRight==pSplit );
 131569     pSplit->pParent->pRight = pNew;
 131570     pNew->pParent = pSplit->pParent;
 131571   }else{
 131572     *ppHead = pNew;
 131574   pNew->pLeft = pSplit;
 131575   pSplit->pParent = pNew;
 131579 ** Parse the fts3 query expression found in buffer z, length n. This function
 131580 ** returns either when the end of the buffer is reached or an unmatched 
 131581 ** closing bracket - ')' - is encountered.
 131583 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
 131584 ** parsed form of the expression and *pnConsumed is set to the number of
 131585 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
 131586 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
 131588 static int fts3ExprParse(
 131589   ParseContext *pParse,                   /* fts3 query parse context */
 131590   const char *z, int n,                   /* Text of MATCH query */
 131591   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
 131592   int *pnConsumed                         /* OUT: Number of bytes consumed */
 131594   Fts3Expr *pRet = 0;
 131595   Fts3Expr *pPrev = 0;
 131596   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
 131597   int nIn = n;
 131598   const char *zIn = z;
 131599   int rc = SQLITE_OK;
 131600   int isRequirePhrase = 1;
 131602   while( rc==SQLITE_OK ){
 131603     Fts3Expr *p = 0;
 131604     int nByte = 0;
 131605     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
 131606     if( rc==SQLITE_OK ){
 131607       int isPhrase;
 131609       if( !sqlite3_fts3_enable_parentheses 
 131610        && p->eType==FTSQUERY_PHRASE && pParse->isNot 
 131612         /* Create an implicit NOT operator. */
 131613         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
 131614         if( !pNot ){
 131615           sqlite3Fts3ExprFree(p);
 131616           rc = SQLITE_NOMEM;
 131617           goto exprparse_out;
 131619         pNot->eType = FTSQUERY_NOT;
 131620         pNot->pRight = p;
 131621         p->pParent = pNot;
 131622         if( pNotBranch ){
 131623           pNot->pLeft = pNotBranch;
 131624           pNotBranch->pParent = pNot;
 131626         pNotBranch = pNot;
 131627         p = pPrev;
 131628       }else{
 131629         int eType = p->eType;
 131630         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
 131632         /* The isRequirePhrase variable is set to true if a phrase or
 131633         ** an expression contained in parenthesis is required. If a
 131634         ** binary operator (AND, OR, NOT or NEAR) is encounted when
 131635         ** isRequirePhrase is set, this is a syntax error.
 131637         if( !isPhrase && isRequirePhrase ){
 131638           sqlite3Fts3ExprFree(p);
 131639           rc = SQLITE_ERROR;
 131640           goto exprparse_out;
 131643         if( isPhrase && !isRequirePhrase ){
 131644           /* Insert an implicit AND operator. */
 131645           Fts3Expr *pAnd;
 131646           assert( pRet && pPrev );
 131647           pAnd = fts3MallocZero(sizeof(Fts3Expr));
 131648           if( !pAnd ){
 131649             sqlite3Fts3ExprFree(p);
 131650             rc = SQLITE_NOMEM;
 131651             goto exprparse_out;
 131653           pAnd->eType = FTSQUERY_AND;
 131654           insertBinaryOperator(&pRet, pPrev, pAnd);
 131655           pPrev = pAnd;
 131658         /* This test catches attempts to make either operand of a NEAR
 131659         ** operator something other than a phrase. For example, either of
 131660         ** the following:
 131662         **    (bracketed expression) NEAR phrase
 131663         **    phrase NEAR (bracketed expression)
 131665         ** Return an error in either case.
 131667         if( pPrev && (
 131668             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
 131669          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
 131670         )){
 131671           sqlite3Fts3ExprFree(p);
 131672           rc = SQLITE_ERROR;
 131673           goto exprparse_out;
 131676         if( isPhrase ){
 131677           if( pRet ){
 131678             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
 131679             pPrev->pRight = p;
 131680             p->pParent = pPrev;
 131681           }else{
 131682             pRet = p;
 131684         }else{
 131685           insertBinaryOperator(&pRet, pPrev, p);
 131687         isRequirePhrase = !isPhrase;
 131689       assert( nByte>0 );
 131691     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
 131692     nIn -= nByte;
 131693     zIn += nByte;
 131694     pPrev = p;
 131697   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
 131698     rc = SQLITE_ERROR;
 131701   if( rc==SQLITE_DONE ){
 131702     rc = SQLITE_OK;
 131703     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
 131704       if( !pRet ){
 131705         rc = SQLITE_ERROR;
 131706       }else{
 131707         Fts3Expr *pIter = pNotBranch;
 131708         while( pIter->pLeft ){
 131709           pIter = pIter->pLeft;
 131711         pIter->pLeft = pRet;
 131712         pRet->pParent = pIter;
 131713         pRet = pNotBranch;
 131717   *pnConsumed = n - nIn;
 131719 exprparse_out:
 131720   if( rc!=SQLITE_OK ){
 131721     sqlite3Fts3ExprFree(pRet);
 131722     sqlite3Fts3ExprFree(pNotBranch);
 131723     pRet = 0;
 131725   *ppExpr = pRet;
 131726   return rc;
 131730 ** Return SQLITE_ERROR if the maximum depth of the expression tree passed 
 131731 ** as the only argument is more than nMaxDepth.
 131733 static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
 131734   int rc = SQLITE_OK;
 131735   if( p ){
 131736     if( nMaxDepth<0 ){ 
 131737       rc = SQLITE_TOOBIG;
 131738     }else{
 131739       rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
 131740       if( rc==SQLITE_OK ){
 131741         rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
 131745   return rc;
 131749 ** This function attempts to transform the expression tree at (*pp) to
 131750 ** an equivalent but more balanced form. The tree is modified in place.
 131751 ** If successful, SQLITE_OK is returned and (*pp) set to point to the 
 131752 ** new root expression node. 
 131754 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
 131756 ** Otherwise, if an error occurs, an SQLite error code is returned and 
 131757 ** expression (*pp) freed.
 131759 static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
 131760   int rc = SQLITE_OK;             /* Return code */
 131761   Fts3Expr *pRoot = *pp;          /* Initial root node */
 131762   Fts3Expr *pFree = 0;            /* List of free nodes. Linked by pParent. */
 131763   int eType = pRoot->eType;       /* Type of node in this tree */
 131765   if( nMaxDepth==0 ){
 131766     rc = SQLITE_ERROR;
 131769   if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
 131770     Fts3Expr **apLeaf;
 131771     apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
 131772     if( 0==apLeaf ){
 131773       rc = SQLITE_NOMEM;
 131774     }else{
 131775       memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
 131778     if( rc==SQLITE_OK ){
 131779       int i;
 131780       Fts3Expr *p;
 131782       /* Set $p to point to the left-most leaf in the tree of eType nodes. */
 131783       for(p=pRoot; p->eType==eType; p=p->pLeft){
 131784         assert( p->pParent==0 || p->pParent->pLeft==p );
 131785         assert( p->pLeft && p->pRight );
 131788       /* This loop runs once for each leaf in the tree of eType nodes. */
 131789       while( 1 ){
 131790         int iLvl;
 131791         Fts3Expr *pParent = p->pParent;     /* Current parent of p */
 131793         assert( pParent==0 || pParent->pLeft==p );
 131794         p->pParent = 0;
 131795         if( pParent ){
 131796           pParent->pLeft = 0;
 131797         }else{
 131798           pRoot = 0;
 131800         rc = fts3ExprBalance(&p, nMaxDepth-1);
 131801         if( rc!=SQLITE_OK ) break;
 131803         for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
 131804           if( apLeaf[iLvl]==0 ){
 131805             apLeaf[iLvl] = p;
 131806             p = 0;
 131807           }else{
 131808             assert( pFree );
 131809             pFree->pLeft = apLeaf[iLvl];
 131810             pFree->pRight = p;
 131811             pFree->pLeft->pParent = pFree;
 131812             pFree->pRight->pParent = pFree;
 131814             p = pFree;
 131815             pFree = pFree->pParent;
 131816             p->pParent = 0;
 131817             apLeaf[iLvl] = 0;
 131820         if( p ){
 131821           sqlite3Fts3ExprFree(p);
 131822           rc = SQLITE_TOOBIG;
 131823           break;
 131826         /* If that was the last leaf node, break out of the loop */
 131827         if( pParent==0 ) break;
 131829         /* Set $p to point to the next leaf in the tree of eType nodes */
 131830         for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
 131832         /* Remove pParent from the original tree. */
 131833         assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
 131834         pParent->pRight->pParent = pParent->pParent;
 131835         if( pParent->pParent ){
 131836           pParent->pParent->pLeft = pParent->pRight;
 131837         }else{
 131838           assert( pParent==pRoot );
 131839           pRoot = pParent->pRight;
 131842         /* Link pParent into the free node list. It will be used as an
 131843         ** internal node of the new tree.  */
 131844         pParent->pParent = pFree;
 131845         pFree = pParent;
 131848       if( rc==SQLITE_OK ){
 131849         p = 0;
 131850         for(i=0; i<nMaxDepth; i++){
 131851           if( apLeaf[i] ){
 131852             if( p==0 ){
 131853               p = apLeaf[i];
 131854               p->pParent = 0;
 131855             }else{
 131856               assert( pFree!=0 );
 131857               pFree->pRight = p;
 131858               pFree->pLeft = apLeaf[i];
 131859               pFree->pLeft->pParent = pFree;
 131860               pFree->pRight->pParent = pFree;
 131862               p = pFree;
 131863               pFree = pFree->pParent;
 131864               p->pParent = 0;
 131868         pRoot = p;
 131869       }else{
 131870         /* An error occurred. Delete the contents of the apLeaf[] array 
 131871         ** and pFree list. Everything else is cleaned up by the call to
 131872         ** sqlite3Fts3ExprFree(pRoot) below.  */
 131873         Fts3Expr *pDel;
 131874         for(i=0; i<nMaxDepth; i++){
 131875           sqlite3Fts3ExprFree(apLeaf[i]);
 131877         while( (pDel=pFree)!=0 ){
 131878           pFree = pDel->pParent;
 131879           sqlite3_free(pDel);
 131883       assert( pFree==0 );
 131884       sqlite3_free( apLeaf );
 131888   if( rc!=SQLITE_OK ){
 131889     sqlite3Fts3ExprFree(pRoot);
 131890     pRoot = 0;
 131892   *pp = pRoot;
 131893   return rc;
 131897 ** This function is similar to sqlite3Fts3ExprParse(), with the following
 131898 ** differences:
 131900 **   1. It does not do expression rebalancing.
 131901 **   2. It does not check that the expression does not exceed the 
 131902 **      maximum allowable depth.
 131903 **   3. Even if it fails, *ppExpr may still be set to point to an 
 131904 **      expression tree. It should be deleted using sqlite3Fts3ExprFree()
 131905 **      in this case.
 131907 static int fts3ExprParseUnbalanced(
 131908   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
 131909   int iLangid,                        /* Language id for tokenizer */
 131910   char **azCol,                       /* Array of column names for fts3 table */
 131911   int bFts4,                          /* True to allow FTS4-only syntax */
 131912   int nCol,                           /* Number of entries in azCol[] */
 131913   int iDefaultCol,                    /* Default column to query */
 131914   const char *z, int n,               /* Text of MATCH query */
 131915   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
 131917   int nParsed;
 131918   int rc;
 131919   ParseContext sParse;
 131921   memset(&sParse, 0, sizeof(ParseContext));
 131922   sParse.pTokenizer = pTokenizer;
 131923   sParse.iLangid = iLangid;
 131924   sParse.azCol = (const char **)azCol;
 131925   sParse.nCol = nCol;
 131926   sParse.iDefaultCol = iDefaultCol;
 131927   sParse.bFts4 = bFts4;
 131928   if( z==0 ){
 131929     *ppExpr = 0;
 131930     return SQLITE_OK;
 131932   if( n<0 ){
 131933     n = (int)strlen(z);
 131935   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
 131936   assert( rc==SQLITE_OK || *ppExpr==0 );
 131938   /* Check for mismatched parenthesis */
 131939   if( rc==SQLITE_OK && sParse.nNest ){
 131940     rc = SQLITE_ERROR;
 131943   return rc;
 131947 ** Parameters z and n contain a pointer to and length of a buffer containing
 131948 ** an fts3 query expression, respectively. This function attempts to parse the
 131949 ** query expression and create a tree of Fts3Expr structures representing the
 131950 ** parsed expression. If successful, *ppExpr is set to point to the head
 131951 ** of the parsed expression tree and SQLITE_OK is returned. If an error
 131952 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
 131953 ** error) is returned and *ppExpr is set to 0.
 131955 ** If parameter n is a negative number, then z is assumed to point to a
 131956 ** nul-terminated string and the length is determined using strlen().
 131958 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
 131959 ** use to normalize query tokens while parsing the expression. The azCol[]
 131960 ** array, which is assumed to contain nCol entries, should contain the names
 131961 ** of each column in the target fts3 table, in order from left to right. 
 131962 ** Column names must be nul-terminated strings.
 131964 ** The iDefaultCol parameter should be passed the index of the table column
 131965 ** that appears on the left-hand-side of the MATCH operator (the default
 131966 ** column to match against for tokens for which a column name is not explicitly
 131967 ** specified as part of the query string), or -1 if tokens may by default
 131968 ** match any table column.
 131970 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
 131971   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
 131972   int iLangid,                        /* Language id for tokenizer */
 131973   char **azCol,                       /* Array of column names for fts3 table */
 131974   int bFts4,                          /* True to allow FTS4-only syntax */
 131975   int nCol,                           /* Number of entries in azCol[] */
 131976   int iDefaultCol,                    /* Default column to query */
 131977   const char *z, int n,               /* Text of MATCH query */
 131978   Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
 131979   char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
 131981   int rc = fts3ExprParseUnbalanced(
 131982       pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
 131985   /* Rebalance the expression. And check that its depth does not exceed
 131986   ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
 131987   if( rc==SQLITE_OK && *ppExpr ){
 131988     rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
 131989     if( rc==SQLITE_OK ){
 131990       rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
 131994   if( rc!=SQLITE_OK ){
 131995     sqlite3Fts3ExprFree(*ppExpr);
 131996     *ppExpr = 0;
 131997     if( rc==SQLITE_TOOBIG ){
 131998       *pzErr = sqlite3_mprintf(
 131999           "FTS expression tree is too large (maximum depth %d)", 
 132000           SQLITE_FTS3_MAX_EXPR_DEPTH
 132002       rc = SQLITE_ERROR;
 132003     }else if( rc==SQLITE_ERROR ){
 132004       *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
 132008   return rc;
 132012 ** Free a single node of an expression tree.
 132014 static void fts3FreeExprNode(Fts3Expr *p){
 132015   assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
 132016   sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
 132017   sqlite3_free(p->aMI);
 132018   sqlite3_free(p);
 132022 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
 132024 ** This function would be simpler if it recursively called itself. But
 132025 ** that would mean passing a sufficiently large expression to ExprParse()
 132026 ** could cause a stack overflow.
 132028 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
 132029   Fts3Expr *p;
 132030   assert( pDel==0 || pDel->pParent==0 );
 132031   for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
 132032     assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
 132034   while( p ){
 132035     Fts3Expr *pParent = p->pParent;
 132036     fts3FreeExprNode(p);
 132037     if( pParent && p==pParent->pLeft && pParent->pRight ){
 132038       p = pParent->pRight;
 132039       while( p && (p->pLeft || p->pRight) ){
 132040         assert( p==p->pParent->pRight || p==p->pParent->pLeft );
 132041         p = (p->pLeft ? p->pLeft : p->pRight);
 132043     }else{
 132044       p = pParent;
 132049 /****************************************************************************
 132050 *****************************************************************************
 132051 ** Everything after this point is just test code.
 132054 #ifdef SQLITE_TEST
 132056 /* #include <stdio.h> */
 132059 ** Function to query the hash-table of tokenizers (see README.tokenizers).
 132061 static int queryTestTokenizer(
 132062   sqlite3 *db, 
 132063   const char *zName,  
 132064   const sqlite3_tokenizer_module **pp
 132066   int rc;
 132067   sqlite3_stmt *pStmt;
 132068   const char zSql[] = "SELECT fts3_tokenizer(?)";
 132070   *pp = 0;
 132071   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 132072   if( rc!=SQLITE_OK ){
 132073     return rc;
 132076   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 132077   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 132078     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
 132079       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
 132083   return sqlite3_finalize(pStmt);
 132087 ** Return a pointer to a buffer containing a text representation of the
 132088 ** expression passed as the first argument. The buffer is obtained from
 132089 ** sqlite3_malloc(). It is the responsibility of the caller to use 
 132090 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
 132091 ** NULL is returned.
 132093 ** If the second argument is not NULL, then its contents are prepended to 
 132094 ** the returned expression text and then freed using sqlite3_free().
 132096 static char *exprToString(Fts3Expr *pExpr, char *zBuf){
 132097   if( pExpr==0 ){
 132098     return sqlite3_mprintf("");
 132100   switch( pExpr->eType ){
 132101     case FTSQUERY_PHRASE: {
 132102       Fts3Phrase *pPhrase = pExpr->pPhrase;
 132103       int i;
 132104       zBuf = sqlite3_mprintf(
 132105           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
 132106       for(i=0; zBuf && i<pPhrase->nToken; i++){
 132107         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 
 132108             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
 132109             (pPhrase->aToken[i].isPrefix?"+":"")
 132112       return zBuf;
 132115     case FTSQUERY_NEAR:
 132116       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
 132117       break;
 132118     case FTSQUERY_NOT:
 132119       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
 132120       break;
 132121     case FTSQUERY_AND:
 132122       zBuf = sqlite3_mprintf("%zAND ", zBuf);
 132123       break;
 132124     case FTSQUERY_OR:
 132125       zBuf = sqlite3_mprintf("%zOR ", zBuf);
 132126       break;
 132129   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
 132130   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
 132131   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
 132133   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
 132134   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
 132136   return zBuf;
 132140 ** This is the implementation of a scalar SQL function used to test the 
 132141 ** expression parser. It should be called as follows:
 132143 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
 132145 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
 132146 ** to parse the query expression (see README.tokenizers). The second argument
 132147 ** is the query expression to parse. Each subsequent argument is the name
 132148 ** of a column of the fts3 table that the query expression may refer to.
 132149 ** For example:
 132151 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
 132153 static void fts3ExprTest(
 132154   sqlite3_context *context,
 132155   int argc,
 132156   sqlite3_value **argv
 132158   sqlite3_tokenizer_module const *pModule = 0;
 132159   sqlite3_tokenizer *pTokenizer = 0;
 132160   int rc;
 132161   char **azCol = 0;
 132162   const char *zExpr;
 132163   int nExpr;
 132164   int nCol;
 132165   int ii;
 132166   Fts3Expr *pExpr;
 132167   char *zBuf = 0;
 132168   sqlite3 *db = sqlite3_context_db_handle(context);
 132170   if( argc<3 ){
 132171     sqlite3_result_error(context, 
 132172         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
 132174     return;
 132177   rc = queryTestTokenizer(db,
 132178                           (const char *)sqlite3_value_text(argv[0]), &pModule);
 132179   if( rc==SQLITE_NOMEM ){
 132180     sqlite3_result_error_nomem(context);
 132181     goto exprtest_out;
 132182   }else if( !pModule ){
 132183     sqlite3_result_error(context, "No such tokenizer module", -1);
 132184     goto exprtest_out;
 132187   rc = pModule->xCreate(0, 0, &pTokenizer);
 132188   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
 132189   if( rc==SQLITE_NOMEM ){
 132190     sqlite3_result_error_nomem(context);
 132191     goto exprtest_out;
 132193   pTokenizer->pModule = pModule;
 132195   zExpr = (const char *)sqlite3_value_text(argv[1]);
 132196   nExpr = sqlite3_value_bytes(argv[1]);
 132197   nCol = argc-2;
 132198   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
 132199   if( !azCol ){
 132200     sqlite3_result_error_nomem(context);
 132201     goto exprtest_out;
 132203   for(ii=0; ii<nCol; ii++){
 132204     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
 132207   if( sqlite3_user_data(context) ){
 132208     char *zDummy = 0;
 132209     rc = sqlite3Fts3ExprParse(
 132210         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
 132212     assert( rc==SQLITE_OK || pExpr==0 );
 132213     sqlite3_free(zDummy);
 132214   }else{
 132215     rc = fts3ExprParseUnbalanced(
 132216         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
 132220   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
 132221     sqlite3Fts3ExprFree(pExpr);
 132222     sqlite3_result_error(context, "Error parsing expression", -1);
 132223   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
 132224     sqlite3_result_error_nomem(context);
 132225   }else{
 132226     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 132227     sqlite3_free(zBuf);
 132230   sqlite3Fts3ExprFree(pExpr);
 132232 exprtest_out:
 132233   if( pModule && pTokenizer ){
 132234     rc = pModule->xDestroy(pTokenizer);
 132236   sqlite3_free(azCol);
 132240 ** Register the query expression parser test function fts3_exprtest() 
 132241 ** with database connection db. 
 132243 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
 132244   int rc = sqlite3_create_function(
 132245       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
 132247   if( rc==SQLITE_OK ){
 132248     rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", 
 132249         -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
 132252   return rc;
 132255 #endif
 132256 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 132258 /************** End of fts3_expr.c *******************************************/
 132259 /************** Begin file fts3_hash.c ***************************************/
 132261 ** 2001 September 22
 132263 ** The author disclaims copyright to this source code.  In place of
 132264 ** a legal notice, here is a blessing:
 132266 **    May you do good and not evil.
 132267 **    May you find forgiveness for yourself and forgive others.
 132268 **    May you share freely, never taking more than you give.
 132270 *************************************************************************
 132271 ** This is the implementation of generic hash-tables used in SQLite.
 132272 ** We've modified it slightly to serve as a standalone hash table
 132273 ** implementation for the full-text indexing module.
 132277 ** The code in this file is only compiled if:
 132279 **     * The FTS3 module is being built as an extension
 132280 **       (in which case SQLITE_CORE is not defined), or
 132282 **     * The FTS3 module is being built into the core of
 132283 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 132285 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 132287 /* #include <assert.h> */
 132288 /* #include <stdlib.h> */
 132289 /* #include <string.h> */
 132293 ** Malloc and Free functions
 132295 static void *fts3HashMalloc(int n){
 132296   void *p = sqlite3_malloc(n);
 132297   if( p ){
 132298     memset(p, 0, n);
 132300   return p;
 132302 static void fts3HashFree(void *p){
 132303   sqlite3_free(p);
 132306 /* Turn bulk memory into a hash table object by initializing the
 132307 ** fields of the Hash structure.
 132309 ** "pNew" is a pointer to the hash table that is to be initialized.
 132310 ** keyClass is one of the constants 
 132311 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
 132312 ** determines what kind of key the hash table will use.  "copyKey" is
 132313 ** true if the hash table should make its own private copy of keys and
 132314 ** false if it should just use the supplied pointer.
 132316 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
 132317   assert( pNew!=0 );
 132318   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
 132319   pNew->keyClass = keyClass;
 132320   pNew->copyKey = copyKey;
 132321   pNew->first = 0;
 132322   pNew->count = 0;
 132323   pNew->htsize = 0;
 132324   pNew->ht = 0;
 132327 /* Remove all entries from a hash table.  Reclaim all memory.
 132328 ** Call this routine to delete a hash table or to reset a hash table
 132329 ** to the empty state.
 132331 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
 132332   Fts3HashElem *elem;         /* For looping over all elements of the table */
 132334   assert( pH!=0 );
 132335   elem = pH->first;
 132336   pH->first = 0;
 132337   fts3HashFree(pH->ht);
 132338   pH->ht = 0;
 132339   pH->htsize = 0;
 132340   while( elem ){
 132341     Fts3HashElem *next_elem = elem->next;
 132342     if( pH->copyKey && elem->pKey ){
 132343       fts3HashFree(elem->pKey);
 132345     fts3HashFree(elem);
 132346     elem = next_elem;
 132348   pH->count = 0;
 132352 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
 132354 static int fts3StrHash(const void *pKey, int nKey){
 132355   const char *z = (const char *)pKey;
 132356   unsigned h = 0;
 132357   if( nKey<=0 ) nKey = (int) strlen(z);
 132358   while( nKey > 0  ){
 132359     h = (h<<3) ^ h ^ *z++;
 132360     nKey--;
 132362   return (int)(h & 0x7fffffff);
 132364 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
 132365   if( n1!=n2 ) return 1;
 132366   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
 132370 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
 132372 static int fts3BinHash(const void *pKey, int nKey){
 132373   int h = 0;
 132374   const char *z = (const char *)pKey;
 132375   while( nKey-- > 0 ){
 132376     h = (h<<3) ^ h ^ *(z++);
 132378   return h & 0x7fffffff;
 132380 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
 132381   if( n1!=n2 ) return 1;
 132382   return memcmp(pKey1,pKey2,n1);
 132386 ** Return a pointer to the appropriate hash function given the key class.
 132388 ** The C syntax in this function definition may be unfamilar to some 
 132389 ** programmers, so we provide the following additional explanation:
 132391 ** The name of the function is "ftsHashFunction".  The function takes a
 132392 ** single parameter "keyClass".  The return value of ftsHashFunction()
 132393 ** is a pointer to another function.  Specifically, the return value
 132394 ** of ftsHashFunction() is a pointer to a function that takes two parameters
 132395 ** with types "const void*" and "int" and returns an "int".
 132397 static int (*ftsHashFunction(int keyClass))(const void*,int){
 132398   if( keyClass==FTS3_HASH_STRING ){
 132399     return &fts3StrHash;
 132400   }else{
 132401     assert( keyClass==FTS3_HASH_BINARY );
 132402     return &fts3BinHash;
 132407 ** Return a pointer to the appropriate hash function given the key class.
 132409 ** For help in interpreted the obscure C code in the function definition,
 132410 ** see the header comment on the previous function.
 132412 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
 132413   if( keyClass==FTS3_HASH_STRING ){
 132414     return &fts3StrCompare;
 132415   }else{
 132416     assert( keyClass==FTS3_HASH_BINARY );
 132417     return &fts3BinCompare;
 132421 /* Link an element into the hash table
 132423 static void fts3HashInsertElement(
 132424   Fts3Hash *pH,            /* The complete hash table */
 132425   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
 132426   Fts3HashElem *pNew       /* The element to be inserted */
 132428   Fts3HashElem *pHead;     /* First element already in pEntry */
 132429   pHead = pEntry->chain;
 132430   if( pHead ){
 132431     pNew->next = pHead;
 132432     pNew->prev = pHead->prev;
 132433     if( pHead->prev ){ pHead->prev->next = pNew; }
 132434     else             { pH->first = pNew; }
 132435     pHead->prev = pNew;
 132436   }else{
 132437     pNew->next = pH->first;
 132438     if( pH->first ){ pH->first->prev = pNew; }
 132439     pNew->prev = 0;
 132440     pH->first = pNew;
 132442   pEntry->count++;
 132443   pEntry->chain = pNew;
 132447 /* Resize the hash table so that it cantains "new_size" buckets.
 132448 ** "new_size" must be a power of 2.  The hash table might fail 
 132449 ** to resize if sqliteMalloc() fails.
 132451 ** Return non-zero if a memory allocation error occurs.
 132453 static int fts3Rehash(Fts3Hash *pH, int new_size){
 132454   struct _fts3ht *new_ht;          /* The new hash table */
 132455   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
 132456   int (*xHash)(const void*,int);   /* The hash function */
 132458   assert( (new_size & (new_size-1))==0 );
 132459   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
 132460   if( new_ht==0 ) return 1;
 132461   fts3HashFree(pH->ht);
 132462   pH->ht = new_ht;
 132463   pH->htsize = new_size;
 132464   xHash = ftsHashFunction(pH->keyClass);
 132465   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
 132466     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
 132467     next_elem = elem->next;
 132468     fts3HashInsertElement(pH, &new_ht[h], elem);
 132470   return 0;
 132473 /* This function (for internal use only) locates an element in an
 132474 ** hash table that matches the given key.  The hash for this key has
 132475 ** already been computed and is passed as the 4th parameter.
 132477 static Fts3HashElem *fts3FindElementByHash(
 132478   const Fts3Hash *pH, /* The pH to be searched */
 132479   const void *pKey,   /* The key we are searching for */
 132480   int nKey,
 132481   int h               /* The hash for this key. */
 132483   Fts3HashElem *elem;            /* Used to loop thru the element list */
 132484   int count;                     /* Number of elements left to test */
 132485   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
 132487   if( pH->ht ){
 132488     struct _fts3ht *pEntry = &pH->ht[h];
 132489     elem = pEntry->chain;
 132490     count = pEntry->count;
 132491     xCompare = ftsCompareFunction(pH->keyClass);
 132492     while( count-- && elem ){
 132493       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
 132494         return elem;
 132496       elem = elem->next;
 132499   return 0;
 132502 /* Remove a single entry from the hash table given a pointer to that
 132503 ** element and a hash on the element's key.
 132505 static void fts3RemoveElementByHash(
 132506   Fts3Hash *pH,         /* The pH containing "elem" */
 132507   Fts3HashElem* elem,   /* The element to be removed from the pH */
 132508   int h                 /* Hash value for the element */
 132510   struct _fts3ht *pEntry;
 132511   if( elem->prev ){
 132512     elem->prev->next = elem->next; 
 132513   }else{
 132514     pH->first = elem->next;
 132516   if( elem->next ){
 132517     elem->next->prev = elem->prev;
 132519   pEntry = &pH->ht[h];
 132520   if( pEntry->chain==elem ){
 132521     pEntry->chain = elem->next;
 132523   pEntry->count--;
 132524   if( pEntry->count<=0 ){
 132525     pEntry->chain = 0;
 132527   if( pH->copyKey && elem->pKey ){
 132528     fts3HashFree(elem->pKey);
 132530   fts3HashFree( elem );
 132531   pH->count--;
 132532   if( pH->count<=0 ){
 132533     assert( pH->first==0 );
 132534     assert( pH->count==0 );
 132535     fts3HashClear(pH);
 132539 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
 132540   const Fts3Hash *pH, 
 132541   const void *pKey, 
 132542   int nKey
 132544   int h;                          /* A hash on key */
 132545   int (*xHash)(const void*,int);  /* The hash function */
 132547   if( pH==0 || pH->ht==0 ) return 0;
 132548   xHash = ftsHashFunction(pH->keyClass);
 132549   assert( xHash!=0 );
 132550   h = (*xHash)(pKey,nKey);
 132551   assert( (pH->htsize & (pH->htsize-1))==0 );
 132552   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
 132556 ** Attempt to locate an element of the hash table pH with a key
 132557 ** that matches pKey,nKey.  Return the data for this element if it is
 132558 ** found, or NULL if there is no match.
 132560 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
 132561   Fts3HashElem *pElem;            /* The element that matches key (if any) */
 132563   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
 132564   return pElem ? pElem->data : 0;
 132567 /* Insert an element into the hash table pH.  The key is pKey,nKey
 132568 ** and the data is "data".
 132570 ** If no element exists with a matching key, then a new
 132571 ** element is created.  A copy of the key is made if the copyKey
 132572 ** flag is set.  NULL is returned.
 132574 ** If another element already exists with the same key, then the
 132575 ** new data replaces the old data and the old data is returned.
 132576 ** The key is not copied in this instance.  If a malloc fails, then
 132577 ** the new data is returned and the hash table is unchanged.
 132579 ** If the "data" parameter to this function is NULL, then the
 132580 ** element corresponding to "key" is removed from the hash table.
 132582 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
 132583   Fts3Hash *pH,        /* The hash table to insert into */
 132584   const void *pKey,    /* The key */
 132585   int nKey,            /* Number of bytes in the key */
 132586   void *data           /* The data */
 132588   int hraw;                 /* Raw hash value of the key */
 132589   int h;                    /* the hash of the key modulo hash table size */
 132590   Fts3HashElem *elem;       /* Used to loop thru the element list */
 132591   Fts3HashElem *new_elem;   /* New element added to the pH */
 132592   int (*xHash)(const void*,int);  /* The hash function */
 132594   assert( pH!=0 );
 132595   xHash = ftsHashFunction(pH->keyClass);
 132596   assert( xHash!=0 );
 132597   hraw = (*xHash)(pKey, nKey);
 132598   assert( (pH->htsize & (pH->htsize-1))==0 );
 132599   h = hraw & (pH->htsize-1);
 132600   elem = fts3FindElementByHash(pH,pKey,nKey,h);
 132601   if( elem ){
 132602     void *old_data = elem->data;
 132603     if( data==0 ){
 132604       fts3RemoveElementByHash(pH,elem,h);
 132605     }else{
 132606       elem->data = data;
 132608     return old_data;
 132610   if( data==0 ) return 0;
 132611   if( (pH->htsize==0 && fts3Rehash(pH,8))
 132612    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
 132614     pH->count = 0;
 132615     return data;
 132617   assert( pH->htsize>0 );
 132618   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
 132619   if( new_elem==0 ) return data;
 132620   if( pH->copyKey && pKey!=0 ){
 132621     new_elem->pKey = fts3HashMalloc( nKey );
 132622     if( new_elem->pKey==0 ){
 132623       fts3HashFree(new_elem);
 132624       return data;
 132626     memcpy((void*)new_elem->pKey, pKey, nKey);
 132627   }else{
 132628     new_elem->pKey = (void*)pKey;
 132630   new_elem->nKey = nKey;
 132631   pH->count++;
 132632   assert( pH->htsize>0 );
 132633   assert( (pH->htsize & (pH->htsize-1))==0 );
 132634   h = hraw & (pH->htsize-1);
 132635   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
 132636   new_elem->data = data;
 132637   return 0;
 132640 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 132642 /************** End of fts3_hash.c *******************************************/
 132643 /************** Begin file fts3_porter.c *************************************/
 132645 ** 2006 September 30
 132647 ** The author disclaims copyright to this source code.  In place of
 132648 ** a legal notice, here is a blessing:
 132650 **    May you do good and not evil.
 132651 **    May you find forgiveness for yourself and forgive others.
 132652 **    May you share freely, never taking more than you give.
 132654 *************************************************************************
 132655 ** Implementation of the full-text-search tokenizer that implements
 132656 ** a Porter stemmer.
 132660 ** The code in this file is only compiled if:
 132662 **     * The FTS3 module is being built as an extension
 132663 **       (in which case SQLITE_CORE is not defined), or
 132665 **     * The FTS3 module is being built into the core of
 132666 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 132668 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 132670 /* #include <assert.h> */
 132671 /* #include <stdlib.h> */
 132672 /* #include <stdio.h> */
 132673 /* #include <string.h> */
 132677 ** Class derived from sqlite3_tokenizer
 132679 typedef struct porter_tokenizer {
 132680   sqlite3_tokenizer base;      /* Base class */
 132681 } porter_tokenizer;
 132684 ** Class derived from sqlite3_tokenizer_cursor
 132686 typedef struct porter_tokenizer_cursor {
 132687   sqlite3_tokenizer_cursor base;
 132688   const char *zInput;          /* input we are tokenizing */
 132689   int nInput;                  /* size of the input */
 132690   int iOffset;                 /* current position in zInput */
 132691   int iToken;                  /* index of next token to be returned */
 132692   char *zToken;                /* storage for current token */
 132693   int nAllocated;              /* space allocated to zToken buffer */
 132694 } porter_tokenizer_cursor;
 132698 ** Create a new tokenizer instance.
 132700 static int porterCreate(
 132701   int argc, const char * const *argv,
 132702   sqlite3_tokenizer **ppTokenizer
 132704   porter_tokenizer *t;
 132706   UNUSED_PARAMETER(argc);
 132707   UNUSED_PARAMETER(argv);
 132709   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
 132710   if( t==NULL ) return SQLITE_NOMEM;
 132711   memset(t, 0, sizeof(*t));
 132712   *ppTokenizer = &t->base;
 132713   return SQLITE_OK;
 132717 ** Destroy a tokenizer
 132719 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
 132720   sqlite3_free(pTokenizer);
 132721   return SQLITE_OK;
 132725 ** Prepare to begin tokenizing a particular string.  The input
 132726 ** string to be tokenized is zInput[0..nInput-1].  A cursor
 132727 ** used to incrementally tokenize this string is returned in 
 132728 ** *ppCursor.
 132730 static int porterOpen(
 132731   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 132732   const char *zInput, int nInput,        /* String to be tokenized */
 132733   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 132735   porter_tokenizer_cursor *c;
 132737   UNUSED_PARAMETER(pTokenizer);
 132739   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
 132740   if( c==NULL ) return SQLITE_NOMEM;
 132742   c->zInput = zInput;
 132743   if( zInput==0 ){
 132744     c->nInput = 0;
 132745   }else if( nInput<0 ){
 132746     c->nInput = (int)strlen(zInput);
 132747   }else{
 132748     c->nInput = nInput;
 132750   c->iOffset = 0;                 /* start tokenizing at the beginning */
 132751   c->iToken = 0;
 132752   c->zToken = NULL;               /* no space allocated, yet. */
 132753   c->nAllocated = 0;
 132755   *ppCursor = &c->base;
 132756   return SQLITE_OK;
 132760 ** Close a tokenization cursor previously opened by a call to
 132761 ** porterOpen() above.
 132763 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
 132764   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
 132765   sqlite3_free(c->zToken);
 132766   sqlite3_free(c);
 132767   return SQLITE_OK;
 132770 ** Vowel or consonant
 132772 static const char cType[] = {
 132773    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
 132774    1, 1, 1, 2, 1
 132778 ** isConsonant() and isVowel() determine if their first character in
 132779 ** the string they point to is a consonant or a vowel, according
 132780 ** to Porter ruls.  
 132782 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
 132783 ** 'Y' is a consonant unless it follows another consonant,
 132784 ** in which case it is a vowel.
 132786 ** In these routine, the letters are in reverse order.  So the 'y' rule
 132787 ** is that 'y' is a consonant unless it is followed by another
 132788 ** consonent.
 132790 static int isVowel(const char*);
 132791 static int isConsonant(const char *z){
 132792   int j;
 132793   char x = *z;
 132794   if( x==0 ) return 0;
 132795   assert( x>='a' && x<='z' );
 132796   j = cType[x-'a'];
 132797   if( j<2 ) return j;
 132798   return z[1]==0 || isVowel(z + 1);
 132800 static int isVowel(const char *z){
 132801   int j;
 132802   char x = *z;
 132803   if( x==0 ) return 0;
 132804   assert( x>='a' && x<='z' );
 132805   j = cType[x-'a'];
 132806   if( j<2 ) return 1-j;
 132807   return isConsonant(z + 1);
 132811 ** Let any sequence of one or more vowels be represented by V and let
 132812 ** C be sequence of one or more consonants.  Then every word can be
 132813 ** represented as:
 132815 **           [C] (VC){m} [V]
 132817 ** In prose:  A word is an optional consonant followed by zero or
 132818 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
 132819 ** number of vowel consonant pairs.  This routine computes the value
 132820 ** of m for the first i bytes of a word.
 132822 ** Return true if the m-value for z is 1 or more.  In other words,
 132823 ** return true if z contains at least one vowel that is followed
 132824 ** by a consonant.
 132826 ** In this routine z[] is in reverse order.  So we are really looking
 132827 ** for an instance of of a consonant followed by a vowel.
 132829 static int m_gt_0(const char *z){
 132830   while( isVowel(z) ){ z++; }
 132831   if( *z==0 ) return 0;
 132832   while( isConsonant(z) ){ z++; }
 132833   return *z!=0;
 132836 /* Like mgt0 above except we are looking for a value of m which is
 132837 ** exactly 1
 132839 static int m_eq_1(const char *z){
 132840   while( isVowel(z) ){ z++; }
 132841   if( *z==0 ) return 0;
 132842   while( isConsonant(z) ){ z++; }
 132843   if( *z==0 ) return 0;
 132844   while( isVowel(z) ){ z++; }
 132845   if( *z==0 ) return 1;
 132846   while( isConsonant(z) ){ z++; }
 132847   return *z==0;
 132850 /* Like mgt0 above except we are looking for a value of m>1 instead
 132851 ** or m>0
 132853 static int m_gt_1(const char *z){
 132854   while( isVowel(z) ){ z++; }
 132855   if( *z==0 ) return 0;
 132856   while( isConsonant(z) ){ z++; }
 132857   if( *z==0 ) return 0;
 132858   while( isVowel(z) ){ z++; }
 132859   if( *z==0 ) return 0;
 132860   while( isConsonant(z) ){ z++; }
 132861   return *z!=0;
 132865 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
 132867 static int hasVowel(const char *z){
 132868   while( isConsonant(z) ){ z++; }
 132869   return *z!=0;
 132873 ** Return TRUE if the word ends in a double consonant.
 132875 ** The text is reversed here. So we are really looking at
 132876 ** the first two characters of z[].
 132878 static int doubleConsonant(const char *z){
 132879   return isConsonant(z) && z[0]==z[1];
 132883 ** Return TRUE if the word ends with three letters which
 132884 ** are consonant-vowel-consonent and where the final consonant
 132885 ** is not 'w', 'x', or 'y'.
 132887 ** The word is reversed here.  So we are really checking the
 132888 ** first three letters and the first one cannot be in [wxy].
 132890 static int star_oh(const char *z){
 132891   return
 132892     isConsonant(z) &&
 132893     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
 132894     isVowel(z+1) &&
 132895     isConsonant(z+2);
 132899 ** If the word ends with zFrom and xCond() is true for the stem
 132900 ** of the word that preceeds the zFrom ending, then change the 
 132901 ** ending to zTo.
 132903 ** The input word *pz and zFrom are both in reverse order.  zTo
 132904 ** is in normal order. 
 132906 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
 132907 ** match.  Not that TRUE is returned even if xCond() fails and
 132908 ** no substitution occurs.
 132910 static int stem(
 132911   char **pz,             /* The word being stemmed (Reversed) */
 132912   const char *zFrom,     /* If the ending matches this... (Reversed) */
 132913   const char *zTo,       /* ... change the ending to this (not reversed) */
 132914   int (*xCond)(const char*)   /* Condition that must be true */
 132916   char *z = *pz;
 132917   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
 132918   if( *zFrom!=0 ) return 0;
 132919   if( xCond && !xCond(z) ) return 1;
 132920   while( *zTo ){
 132921     *(--z) = *(zTo++);
 132923   *pz = z;
 132924   return 1;
 132928 ** This is the fallback stemmer used when the porter stemmer is
 132929 ** inappropriate.  The input word is copied into the output with
 132930 ** US-ASCII case folding.  If the input word is too long (more
 132931 ** than 20 bytes if it contains no digits or more than 6 bytes if
 132932 ** it contains digits) then word is truncated to 20 or 6 bytes
 132933 ** by taking 10 or 3 bytes from the beginning and end.
 132935 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
 132936   int i, mx, j;
 132937   int hasDigit = 0;
 132938   for(i=0; i<nIn; i++){
 132939     char c = zIn[i];
 132940     if( c>='A' && c<='Z' ){
 132941       zOut[i] = c - 'A' + 'a';
 132942     }else{
 132943       if( c>='0' && c<='9' ) hasDigit = 1;
 132944       zOut[i] = c;
 132947   mx = hasDigit ? 3 : 10;
 132948   if( nIn>mx*2 ){
 132949     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
 132950       zOut[j] = zOut[i];
 132952     i = j;
 132954   zOut[i] = 0;
 132955   *pnOut = i;
 132960 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
 132961 ** zOut is at least big enough to hold nIn bytes.  Write the actual
 132962 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
 132964 ** Any upper-case characters in the US-ASCII character set ([A-Z])
 132965 ** are converted to lower case.  Upper-case UTF characters are
 132966 ** unchanged.
 132968 ** Words that are longer than about 20 bytes are stemmed by retaining
 132969 ** a few bytes from the beginning and the end of the word.  If the
 132970 ** word contains digits, 3 bytes are taken from the beginning and
 132971 ** 3 bytes from the end.  For long words without digits, 10 bytes
 132972 ** are taken from each end.  US-ASCII case folding still applies.
 132974 ** If the input word contains not digits but does characters not 
 132975 ** in [a-zA-Z] then no stemming is attempted and this routine just 
 132976 ** copies the input into the input into the output with US-ASCII
 132977 ** case folding.
 132979 ** Stemming never increases the length of the word.  So there is
 132980 ** no chance of overflowing the zOut buffer.
 132982 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
 132983   int i, j;
 132984   char zReverse[28];
 132985   char *z, *z2;
 132986   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
 132987     /* The word is too big or too small for the porter stemmer.
 132988     ** Fallback to the copy stemmer */
 132989     copy_stemmer(zIn, nIn, zOut, pnOut);
 132990     return;
 132992   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
 132993     char c = zIn[i];
 132994     if( c>='A' && c<='Z' ){
 132995       zReverse[j] = c + 'a' - 'A';
 132996     }else if( c>='a' && c<='z' ){
 132997       zReverse[j] = c;
 132998     }else{
 132999       /* The use of a character not in [a-zA-Z] means that we fallback
 133000       ** to the copy stemmer */
 133001       copy_stemmer(zIn, nIn, zOut, pnOut);
 133002       return;
 133005   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
 133006   z = &zReverse[j+1];
 133009   /* Step 1a */
 133010   if( z[0]=='s' ){
 133011     if(
 133012      !stem(&z, "sess", "ss", 0) &&
 133013      !stem(&z, "sei", "i", 0)  &&
 133014      !stem(&z, "ss", "ss", 0)
 133016       z++;
 133020   /* Step 1b */  
 133021   z2 = z;
 133022   if( stem(&z, "dee", "ee", m_gt_0) ){
 133023     /* Do nothing.  The work was all in the test */
 133024   }else if( 
 133025      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
 133026       && z!=z2
 133028      if( stem(&z, "ta", "ate", 0) ||
 133029          stem(&z, "lb", "ble", 0) ||
 133030          stem(&z, "zi", "ize", 0) ){
 133031        /* Do nothing.  The work was all in the test */
 133032      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
 133033        z++;
 133034      }else if( m_eq_1(z) && star_oh(z) ){
 133035        *(--z) = 'e';
 133039   /* Step 1c */
 133040   if( z[0]=='y' && hasVowel(z+1) ){
 133041     z[0] = 'i';
 133044   /* Step 2 */
 133045   switch( z[1] ){
 133046    case 'a':
 133047      if( !stem(&z, "lanoita", "ate", m_gt_0) ){
 133048        stem(&z, "lanoit", "tion", m_gt_0);
 133050      break;
 133051    case 'c':
 133052      if( !stem(&z, "icne", "ence", m_gt_0) ){
 133053        stem(&z, "icna", "ance", m_gt_0);
 133055      break;
 133056    case 'e':
 133057      stem(&z, "rezi", "ize", m_gt_0);
 133058      break;
 133059    case 'g':
 133060      stem(&z, "igol", "log", m_gt_0);
 133061      break;
 133062    case 'l':
 133063      if( !stem(&z, "ilb", "ble", m_gt_0) 
 133064       && !stem(&z, "illa", "al", m_gt_0)
 133065       && !stem(&z, "iltne", "ent", m_gt_0)
 133066       && !stem(&z, "ile", "e", m_gt_0)
 133068        stem(&z, "ilsuo", "ous", m_gt_0);
 133070      break;
 133071    case 'o':
 133072      if( !stem(&z, "noitazi", "ize", m_gt_0)
 133073       && !stem(&z, "noita", "ate", m_gt_0)
 133075        stem(&z, "rota", "ate", m_gt_0);
 133077      break;
 133078    case 's':
 133079      if( !stem(&z, "msila", "al", m_gt_0)
 133080       && !stem(&z, "ssenevi", "ive", m_gt_0)
 133081       && !stem(&z, "ssenluf", "ful", m_gt_0)
 133083        stem(&z, "ssensuo", "ous", m_gt_0);
 133085      break;
 133086    case 't':
 133087      if( !stem(&z, "itila", "al", m_gt_0)
 133088       && !stem(&z, "itivi", "ive", m_gt_0)
 133090        stem(&z, "itilib", "ble", m_gt_0);
 133092      break;
 133095   /* Step 3 */
 133096   switch( z[0] ){
 133097    case 'e':
 133098      if( !stem(&z, "etaci", "ic", m_gt_0)
 133099       && !stem(&z, "evita", "", m_gt_0)
 133101        stem(&z, "ezila", "al", m_gt_0);
 133103      break;
 133104    case 'i':
 133105      stem(&z, "itici", "ic", m_gt_0);
 133106      break;
 133107    case 'l':
 133108      if( !stem(&z, "laci", "ic", m_gt_0) ){
 133109        stem(&z, "luf", "", m_gt_0);
 133111      break;
 133112    case 's':
 133113      stem(&z, "ssen", "", m_gt_0);
 133114      break;
 133117   /* Step 4 */
 133118   switch( z[1] ){
 133119    case 'a':
 133120      if( z[0]=='l' && m_gt_1(z+2) ){
 133121        z += 2;
 133123      break;
 133124    case 'c':
 133125      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
 133126        z += 4;
 133128      break;
 133129    case 'e':
 133130      if( z[0]=='r' && m_gt_1(z+2) ){
 133131        z += 2;
 133133      break;
 133134    case 'i':
 133135      if( z[0]=='c' && m_gt_1(z+2) ){
 133136        z += 2;
 133138      break;
 133139    case 'l':
 133140      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
 133141        z += 4;
 133143      break;
 133144    case 'n':
 133145      if( z[0]=='t' ){
 133146        if( z[2]=='a' ){
 133147          if( m_gt_1(z+3) ){
 133148            z += 3;
 133150        }else if( z[2]=='e' ){
 133151          if( !stem(&z, "tneme", "", m_gt_1)
 133152           && !stem(&z, "tnem", "", m_gt_1)
 133154            stem(&z, "tne", "", m_gt_1);
 133158      break;
 133159    case 'o':
 133160      if( z[0]=='u' ){
 133161        if( m_gt_1(z+2) ){
 133162          z += 2;
 133164      }else if( z[3]=='s' || z[3]=='t' ){
 133165        stem(&z, "noi", "", m_gt_1);
 133167      break;
 133168    case 's':
 133169      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
 133170        z += 3;
 133172      break;
 133173    case 't':
 133174      if( !stem(&z, "eta", "", m_gt_1) ){
 133175        stem(&z, "iti", "", m_gt_1);
 133177      break;
 133178    case 'u':
 133179      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
 133180        z += 3;
 133182      break;
 133183    case 'v':
 133184    case 'z':
 133185      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
 133186        z += 3;
 133188      break;
 133191   /* Step 5a */
 133192   if( z[0]=='e' ){
 133193     if( m_gt_1(z+1) ){
 133194       z++;
 133195     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
 133196       z++;
 133200   /* Step 5b */
 133201   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
 133202     z++;
 133205   /* z[] is now the stemmed word in reverse order.  Flip it back
 133206   ** around into forward order and return.
 133208   *pnOut = i = (int)strlen(z);
 133209   zOut[i] = 0;
 133210   while( *z ){
 133211     zOut[--i] = *(z++);
 133216 ** Characters that can be part of a token.  We assume any character
 133217 ** whose value is greater than 0x80 (any UTF character) can be
 133218 ** part of a token.  In other words, delimiters all must have
 133219 ** values of 0x7f or lower.
 133221 static const char porterIdChar[] = {
 133222 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
 133223     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
 133224     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
 133225     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
 133226     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
 133227     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 133229 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
 133232 ** Extract the next token from a tokenization cursor.  The cursor must
 133233 ** have been opened by a prior call to porterOpen().
 133235 static int porterNext(
 133236   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
 133237   const char **pzToken,               /* OUT: *pzToken is the token text */
 133238   int *pnBytes,                       /* OUT: Number of bytes in token */
 133239   int *piStartOffset,                 /* OUT: Starting offset of token */
 133240   int *piEndOffset,                   /* OUT: Ending offset of token */
 133241   int *piPosition                     /* OUT: Position integer of token */
 133243   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
 133244   const char *z = c->zInput;
 133246   while( c->iOffset<c->nInput ){
 133247     int iStartOffset, ch;
 133249     /* Scan past delimiter characters */
 133250     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
 133251       c->iOffset++;
 133254     /* Count non-delimiter characters. */
 133255     iStartOffset = c->iOffset;
 133256     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
 133257       c->iOffset++;
 133260     if( c->iOffset>iStartOffset ){
 133261       int n = c->iOffset-iStartOffset;
 133262       if( n>c->nAllocated ){
 133263         char *pNew;
 133264         c->nAllocated = n+20;
 133265         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
 133266         if( !pNew ) return SQLITE_NOMEM;
 133267         c->zToken = pNew;
 133269       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
 133270       *pzToken = c->zToken;
 133271       *piStartOffset = iStartOffset;
 133272       *piEndOffset = c->iOffset;
 133273       *piPosition = c->iToken++;
 133274       return SQLITE_OK;
 133277   return SQLITE_DONE;
 133281 ** The set of routines that implement the porter-stemmer tokenizer
 133283 static const sqlite3_tokenizer_module porterTokenizerModule = {
 133285   porterCreate,
 133286   porterDestroy,
 133287   porterOpen,
 133288   porterClose,
 133289   porterNext,
 133294 ** Allocate a new porter tokenizer.  Return a pointer to the new
 133295 ** tokenizer in *ppModule
 133297 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
 133298   sqlite3_tokenizer_module const**ppModule
 133300   *ppModule = &porterTokenizerModule;
 133303 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 133305 /************** End of fts3_porter.c *****************************************/
 133306 /************** Begin file fts3_tokenizer.c **********************************/
 133308 ** 2007 June 22
 133310 ** The author disclaims copyright to this source code.  In place of
 133311 ** a legal notice, here is a blessing:
 133313 **    May you do good and not evil.
 133314 **    May you find forgiveness for yourself and forgive others.
 133315 **    May you share freely, never taking more than you give.
 133317 ******************************************************************************
 133319 ** This is part of an SQLite module implementing full-text search.
 133320 ** This particular file implements the generic tokenizer interface.
 133324 ** The code in this file is only compiled if:
 133326 **     * The FTS3 module is being built as an extension
 133327 **       (in which case SQLITE_CORE is not defined), or
 133329 **     * The FTS3 module is being built into the core of
 133330 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 133332 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 133334 /* #include <assert.h> */
 133335 /* #include <string.h> */
 133338 ** Implementation of the SQL scalar function for accessing the underlying 
 133339 ** hash table. This function may be called as follows:
 133341 **   SELECT <function-name>(<key-name>);
 133342 **   SELECT <function-name>(<key-name>, <pointer>);
 133344 ** where <function-name> is the name passed as the second argument
 133345 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
 133347 ** If the <pointer> argument is specified, it must be a blob value
 133348 ** containing a pointer to be stored as the hash data corresponding
 133349 ** to the string <key-name>. If <pointer> is not specified, then
 133350 ** the string <key-name> must already exist in the has table. Otherwise,
 133351 ** an error is returned.
 133353 ** Whether or not the <pointer> argument is specified, the value returned
 133354 ** is a blob containing the pointer stored as the hash data corresponding
 133355 ** to string <key-name> (after the hash-table is updated, if applicable).
 133357 static void scalarFunc(
 133358   sqlite3_context *context,
 133359   int argc,
 133360   sqlite3_value **argv
 133362   Fts3Hash *pHash;
 133363   void *pPtr = 0;
 133364   const unsigned char *zName;
 133365   int nName;
 133367   assert( argc==1 || argc==2 );
 133369   pHash = (Fts3Hash *)sqlite3_user_data(context);
 133371   zName = sqlite3_value_text(argv[0]);
 133372   nName = sqlite3_value_bytes(argv[0])+1;
 133374   if( argc==2 ){
 133375     void *pOld;
 133376     int n = sqlite3_value_bytes(argv[1]);
 133377     if( n!=sizeof(pPtr) ){
 133378       sqlite3_result_error(context, "argument type mismatch", -1);
 133379       return;
 133381     pPtr = *(void **)sqlite3_value_blob(argv[1]);
 133382     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
 133383     if( pOld==pPtr ){
 133384       sqlite3_result_error(context, "out of memory", -1);
 133385       return;
 133387   }else{
 133388     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
 133389     if( !pPtr ){
 133390       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
 133391       sqlite3_result_error(context, zErr, -1);
 133392       sqlite3_free(zErr);
 133393       return;
 133397   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
 133400 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
 133401   static const char isFtsIdChar[] = {
 133402       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
 133403       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
 133404       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
 133405       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
 133406       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
 133407       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
 133408       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
 133409       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 133411   return (c&0x80 || isFtsIdChar[(int)(c)]);
 133414 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
 133415   const char *z1;
 133416   const char *z2 = 0;
 133418   /* Find the start of the next token. */
 133419   z1 = zStr;
 133420   while( z2==0 ){
 133421     char c = *z1;
 133422     switch( c ){
 133423       case '\0': return 0;        /* No more tokens here */
 133424       case '\'':
 133425       case '"':
 133426       case '`': {
 133427         z2 = z1;
 133428         while( *++z2 && (*z2!=c || *++z2==c) );
 133429         break;
 133431       case '[':
 133432         z2 = &z1[1];
 133433         while( *z2 && z2[0]!=']' ) z2++;
 133434         if( *z2 ) z2++;
 133435         break;
 133437       default:
 133438         if( sqlite3Fts3IsIdChar(*z1) ){
 133439           z2 = &z1[1];
 133440           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
 133441         }else{
 133442           z1++;
 133447   *pn = (int)(z2-z1);
 133448   return z1;
 133451 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
 133452   Fts3Hash *pHash,                /* Tokenizer hash table */
 133453   const char *zArg,               /* Tokenizer name */
 133454   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
 133455   char **pzErr                    /* OUT: Set to malloced error message */
 133457   int rc;
 133458   char *z = (char *)zArg;
 133459   int n = 0;
 133460   char *zCopy;
 133461   char *zEnd;                     /* Pointer to nul-term of zCopy */
 133462   sqlite3_tokenizer_module *m;
 133464   zCopy = sqlite3_mprintf("%s", zArg);
 133465   if( !zCopy ) return SQLITE_NOMEM;
 133466   zEnd = &zCopy[strlen(zCopy)];
 133468   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
 133469   z[n] = '\0';
 133470   sqlite3Fts3Dequote(z);
 133472   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
 133473   if( !m ){
 133474     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
 133475     rc = SQLITE_ERROR;
 133476   }else{
 133477     char const **aArg = 0;
 133478     int iArg = 0;
 133479     z = &z[n+1];
 133480     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
 133481       int nNew = sizeof(char *)*(iArg+1);
 133482       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
 133483       if( !aNew ){
 133484         sqlite3_free(zCopy);
 133485         sqlite3_free((void *)aArg);
 133486         return SQLITE_NOMEM;
 133488       aArg = aNew;
 133489       aArg[iArg++] = z;
 133490       z[n] = '\0';
 133491       sqlite3Fts3Dequote(z);
 133492       z = &z[n+1];
 133494     rc = m->xCreate(iArg, aArg, ppTok);
 133495     assert( rc!=SQLITE_OK || *ppTok );
 133496     if( rc!=SQLITE_OK ){
 133497       *pzErr = sqlite3_mprintf("unknown tokenizer");
 133498     }else{
 133499       (*ppTok)->pModule = m; 
 133501     sqlite3_free((void *)aArg);
 133504   sqlite3_free(zCopy);
 133505   return rc;
 133509 #ifdef SQLITE_TEST
 133511 #include <tcl.h>
 133512 /* #include <string.h> */
 133515 ** Implementation of a special SQL scalar function for testing tokenizers 
 133516 ** designed to be used in concert with the Tcl testing framework. This
 133517 ** function must be called with two or more arguments:
 133519 **   SELECT <function-name>(<key-name>, ..., <input-string>);
 133521 ** where <function-name> is the name passed as the second argument
 133522 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
 133523 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
 133525 ** The return value is a string that may be interpreted as a Tcl
 133526 ** list. For each token in the <input-string>, three elements are
 133527 ** added to the returned list. The first is the token position, the 
 133528 ** second is the token text (folded, stemmed, etc.) and the third is the
 133529 ** substring of <input-string> associated with the token. For example, 
 133530 ** using the built-in "simple" tokenizer:
 133532 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
 133534 ** will return the string:
 133536 **   "{0 i I 1 dont don't 2 see see 3 how how}"
 133539 static void testFunc(
 133540   sqlite3_context *context,
 133541   int argc,
 133542   sqlite3_value **argv
 133544   Fts3Hash *pHash;
 133545   sqlite3_tokenizer_module *p;
 133546   sqlite3_tokenizer *pTokenizer = 0;
 133547   sqlite3_tokenizer_cursor *pCsr = 0;
 133549   const char *zErr = 0;
 133551   const char *zName;
 133552   int nName;
 133553   const char *zInput;
 133554   int nInput;
 133556   const char *azArg[64];
 133558   const char *zToken;
 133559   int nToken = 0;
 133560   int iStart = 0;
 133561   int iEnd = 0;
 133562   int iPos = 0;
 133563   int i;
 133565   Tcl_Obj *pRet;
 133567   if( argc<2 ){
 133568     sqlite3_result_error(context, "insufficient arguments", -1);
 133569     return;
 133572   nName = sqlite3_value_bytes(argv[0]);
 133573   zName = (const char *)sqlite3_value_text(argv[0]);
 133574   nInput = sqlite3_value_bytes(argv[argc-1]);
 133575   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
 133577   pHash = (Fts3Hash *)sqlite3_user_data(context);
 133578   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
 133580   if( !p ){
 133581     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
 133582     sqlite3_result_error(context, zErr, -1);
 133583     sqlite3_free(zErr);
 133584     return;
 133587   pRet = Tcl_NewObj();
 133588   Tcl_IncrRefCount(pRet);
 133590   for(i=1; i<argc-1; i++){
 133591     azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
 133594   if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
 133595     zErr = "error in xCreate()";
 133596     goto finish;
 133598   pTokenizer->pModule = p;
 133599   if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
 133600     zErr = "error in xOpen()";
 133601     goto finish;
 133604   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
 133605     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
 133606     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
 133607     zToken = &zInput[iStart];
 133608     nToken = iEnd-iStart;
 133609     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
 133612   if( SQLITE_OK!=p->xClose(pCsr) ){
 133613     zErr = "error in xClose()";
 133614     goto finish;
 133616   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
 133617     zErr = "error in xDestroy()";
 133618     goto finish;
 133621 finish:
 133622   if( zErr ){
 133623     sqlite3_result_error(context, zErr, -1);
 133624   }else{
 133625     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
 133627   Tcl_DecrRefCount(pRet);
 133630 static
 133631 int registerTokenizer(
 133632   sqlite3 *db, 
 133633   char *zName, 
 133634   const sqlite3_tokenizer_module *p
 133636   int rc;
 133637   sqlite3_stmt *pStmt;
 133638   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
 133640   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 133641   if( rc!=SQLITE_OK ){
 133642     return rc;
 133645   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 133646   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
 133647   sqlite3_step(pStmt);
 133649   return sqlite3_finalize(pStmt);
 133652 static
 133653 int queryTokenizer(
 133654   sqlite3 *db, 
 133655   char *zName,  
 133656   const sqlite3_tokenizer_module **pp
 133658   int rc;
 133659   sqlite3_stmt *pStmt;
 133660   const char zSql[] = "SELECT fts3_tokenizer(?)";
 133662   *pp = 0;
 133663   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 133664   if( rc!=SQLITE_OK ){
 133665     return rc;
 133668   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
 133669   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 133670     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
 133671       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
 133675   return sqlite3_finalize(pStmt);
 133678 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
 133681 ** Implementation of the scalar function fts3_tokenizer_internal_test().
 133682 ** This function is used for testing only, it is not included in the
 133683 ** build unless SQLITE_TEST is defined.
 133685 ** The purpose of this is to test that the fts3_tokenizer() function
 133686 ** can be used as designed by the C-code in the queryTokenizer and
 133687 ** registerTokenizer() functions above. These two functions are repeated
 133688 ** in the README.tokenizer file as an example, so it is important to
 133689 ** test them.
 133691 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
 133692 ** function with no arguments. An assert() will fail if a problem is
 133693 ** detected. i.e.:
 133695 **     SELECT fts3_tokenizer_internal_test();
 133698 static void intTestFunc(
 133699   sqlite3_context *context,
 133700   int argc,
 133701   sqlite3_value **argv
 133703   int rc;
 133704   const sqlite3_tokenizer_module *p1;
 133705   const sqlite3_tokenizer_module *p2;
 133706   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
 133708   UNUSED_PARAMETER(argc);
 133709   UNUSED_PARAMETER(argv);
 133711   /* Test the query function */
 133712   sqlite3Fts3SimpleTokenizerModule(&p1);
 133713   rc = queryTokenizer(db, "simple", &p2);
 133714   assert( rc==SQLITE_OK );
 133715   assert( p1==p2 );
 133716   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
 133717   assert( rc==SQLITE_ERROR );
 133718   assert( p2==0 );
 133719   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
 133721   /* Test the storage function */
 133722   rc = registerTokenizer(db, "nosuchtokenizer", p1);
 133723   assert( rc==SQLITE_OK );
 133724   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
 133725   assert( rc==SQLITE_OK );
 133726   assert( p2==p1 );
 133728   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
 133731 #endif
 133734 ** Set up SQL objects in database db used to access the contents of
 133735 ** the hash table pointed to by argument pHash. The hash table must
 133736 ** been initialized to use string keys, and to take a private copy 
 133737 ** of the key when a value is inserted. i.e. by a call similar to:
 133739 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
 133741 ** This function adds a scalar function (see header comment above
 133742 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
 133743 ** defined at compilation time, a temporary virtual table (see header 
 133744 ** comment above struct HashTableVtab) to the database schema. Both 
 133745 ** provide read/write access to the contents of *pHash.
 133747 ** The third argument to this function, zName, is used as the name
 133748 ** of both the scalar and, if created, the virtual table.
 133750 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
 133751   sqlite3 *db, 
 133752   Fts3Hash *pHash, 
 133753   const char *zName
 133755   int rc = SQLITE_OK;
 133756   void *p = (void *)pHash;
 133757   const int any = SQLITE_ANY;
 133759 #ifdef SQLITE_TEST
 133760   char *zTest = 0;
 133761   char *zTest2 = 0;
 133762   void *pdb = (void *)db;
 133763   zTest = sqlite3_mprintf("%s_test", zName);
 133764   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
 133765   if( !zTest || !zTest2 ){
 133766     rc = SQLITE_NOMEM;
 133768 #endif
 133770   if( SQLITE_OK==rc ){
 133771     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
 133773   if( SQLITE_OK==rc ){
 133774     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
 133776 #ifdef SQLITE_TEST
 133777   if( SQLITE_OK==rc ){
 133778     rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
 133780   if( SQLITE_OK==rc ){
 133781     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
 133783 #endif
 133785 #ifdef SQLITE_TEST
 133786   sqlite3_free(zTest);
 133787   sqlite3_free(zTest2);
 133788 #endif
 133790   return rc;
 133793 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 133795 /************** End of fts3_tokenizer.c **************************************/
 133796 /************** Begin file fts3_tokenizer1.c *********************************/
 133798 ** 2006 Oct 10
 133800 ** The author disclaims copyright to this source code.  In place of
 133801 ** a legal notice, here is a blessing:
 133803 **    May you do good and not evil.
 133804 **    May you find forgiveness for yourself and forgive others.
 133805 **    May you share freely, never taking more than you give.
 133807 ******************************************************************************
 133809 ** Implementation of the "simple" full-text-search tokenizer.
 133813 ** The code in this file is only compiled if:
 133815 **     * The FTS3 module is being built as an extension
 133816 **       (in which case SQLITE_CORE is not defined), or
 133818 **     * The FTS3 module is being built into the core of
 133819 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
 133821 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 133823 /* #include <assert.h> */
 133824 /* #include <stdlib.h> */
 133825 /* #include <stdio.h> */
 133826 /* #include <string.h> */
 133829 typedef struct simple_tokenizer {
 133830   sqlite3_tokenizer base;
 133831   char delim[128];             /* flag ASCII delimiters */
 133832 } simple_tokenizer;
 133834 typedef struct simple_tokenizer_cursor {
 133835   sqlite3_tokenizer_cursor base;
 133836   const char *pInput;          /* input we are tokenizing */
 133837   int nBytes;                  /* size of the input */
 133838   int iOffset;                 /* current position in pInput */
 133839   int iToken;                  /* index of next token to be returned */
 133840   char *pToken;                /* storage for current token */
 133841   int nTokenAllocated;         /* space allocated to zToken buffer */
 133842 } simple_tokenizer_cursor;
 133845 static int simpleDelim(simple_tokenizer *t, unsigned char c){
 133846   return c<0x80 && t->delim[c];
 133848 static int fts3_isalnum(int x){
 133849   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
 133853 ** Create a new tokenizer instance.
 133855 static int simpleCreate(
 133856   int argc, const char * const *argv,
 133857   sqlite3_tokenizer **ppTokenizer
 133859   simple_tokenizer *t;
 133861   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
 133862   if( t==NULL ) return SQLITE_NOMEM;
 133863   memset(t, 0, sizeof(*t));
 133865   /* TODO(shess) Delimiters need to remain the same from run to run,
 133866   ** else we need to reindex.  One solution would be a meta-table to
 133867   ** track such information in the database, then we'd only want this
 133868   ** information on the initial create.
 133870   if( argc>1 ){
 133871     int i, n = (int)strlen(argv[1]);
 133872     for(i=0; i<n; i++){
 133873       unsigned char ch = argv[1][i];
 133874       /* We explicitly don't support UTF-8 delimiters for now. */
 133875       if( ch>=0x80 ){
 133876         sqlite3_free(t);
 133877         return SQLITE_ERROR;
 133879       t->delim[ch] = 1;
 133881   } else {
 133882     /* Mark non-alphanumeric ASCII characters as delimiters */
 133883     int i;
 133884     for(i=1; i<0x80; i++){
 133885       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
 133889   *ppTokenizer = &t->base;
 133890   return SQLITE_OK;
 133894 ** Destroy a tokenizer
 133896 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
 133897   sqlite3_free(pTokenizer);
 133898   return SQLITE_OK;
 133902 ** Prepare to begin tokenizing a particular string.  The input
 133903 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 133904 ** used to incrementally tokenize this string is returned in 
 133905 ** *ppCursor.
 133907 static int simpleOpen(
 133908   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 133909   const char *pInput, int nBytes,        /* String to be tokenized */
 133910   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 133912   simple_tokenizer_cursor *c;
 133914   UNUSED_PARAMETER(pTokenizer);
 133916   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
 133917   if( c==NULL ) return SQLITE_NOMEM;
 133919   c->pInput = pInput;
 133920   if( pInput==0 ){
 133921     c->nBytes = 0;
 133922   }else if( nBytes<0 ){
 133923     c->nBytes = (int)strlen(pInput);
 133924   }else{
 133925     c->nBytes = nBytes;
 133927   c->iOffset = 0;                 /* start tokenizing at the beginning */
 133928   c->iToken = 0;
 133929   c->pToken = NULL;               /* no space allocated, yet. */
 133930   c->nTokenAllocated = 0;
 133932   *ppCursor = &c->base;
 133933   return SQLITE_OK;
 133937 ** Close a tokenization cursor previously opened by a call to
 133938 ** simpleOpen() above.
 133940 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
 133941   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
 133942   sqlite3_free(c->pToken);
 133943   sqlite3_free(c);
 133944   return SQLITE_OK;
 133948 ** Extract the next token from a tokenization cursor.  The cursor must
 133949 ** have been opened by a prior call to simpleOpen().
 133951 static int simpleNext(
 133952   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
 133953   const char **ppToken,               /* OUT: *ppToken is the token text */
 133954   int *pnBytes,                       /* OUT: Number of bytes in token */
 133955   int *piStartOffset,                 /* OUT: Starting offset of token */
 133956   int *piEndOffset,                   /* OUT: Ending offset of token */
 133957   int *piPosition                     /* OUT: Position integer of token */
 133959   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
 133960   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
 133961   unsigned char *p = (unsigned char *)c->pInput;
 133963   while( c->iOffset<c->nBytes ){
 133964     int iStartOffset;
 133966     /* Scan past delimiter characters */
 133967     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
 133968       c->iOffset++;
 133971     /* Count non-delimiter characters. */
 133972     iStartOffset = c->iOffset;
 133973     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
 133974       c->iOffset++;
 133977     if( c->iOffset>iStartOffset ){
 133978       int i, n = c->iOffset-iStartOffset;
 133979       if( n>c->nTokenAllocated ){
 133980         char *pNew;
 133981         c->nTokenAllocated = n+20;
 133982         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
 133983         if( !pNew ) return SQLITE_NOMEM;
 133984         c->pToken = pNew;
 133986       for(i=0; i<n; i++){
 133987         /* TODO(shess) This needs expansion to handle UTF-8
 133988         ** case-insensitivity.
 133990         unsigned char ch = p[iStartOffset+i];
 133991         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
 133993       *ppToken = c->pToken;
 133994       *pnBytes = n;
 133995       *piStartOffset = iStartOffset;
 133996       *piEndOffset = c->iOffset;
 133997       *piPosition = c->iToken++;
 133999       return SQLITE_OK;
 134002   return SQLITE_DONE;
 134006 ** The set of routines that implement the simple tokenizer
 134008 static const sqlite3_tokenizer_module simpleTokenizerModule = {
 134010   simpleCreate,
 134011   simpleDestroy,
 134012   simpleOpen,
 134013   simpleClose,
 134014   simpleNext,
 134019 ** Allocate a new simple tokenizer.  Return a pointer to the new
 134020 ** tokenizer in *ppModule
 134022 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
 134023   sqlite3_tokenizer_module const**ppModule
 134025   *ppModule = &simpleTokenizerModule;
 134028 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 134030 /************** End of fts3_tokenizer1.c *************************************/
 134031 /************** Begin file fts3_tokenize_vtab.c ******************************/
 134033 ** 2013 Apr 22
 134035 ** The author disclaims copyright to this source code.  In place of
 134036 ** a legal notice, here is a blessing:
 134038 **    May you do good and not evil.
 134039 **    May you find forgiveness for yourself and forgive others.
 134040 **    May you share freely, never taking more than you give.
 134042 ******************************************************************************
 134044 ** This file contains code for the "fts3tokenize" virtual table module.
 134045 ** An fts3tokenize virtual table is created as follows:
 134047 **   CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
 134048 **       <tokenizer-name>, <arg-1>, ...
 134049 **   );
 134051 ** The table created has the following schema:
 134053 **   CREATE TABLE <tbl>(input, token, start, end, position)
 134055 ** When queried, the query must include a WHERE clause of type:
 134057 **   input = <string>
 134059 ** The virtual table module tokenizes this <string>, using the FTS3 
 134060 ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE 
 134061 ** statement and returns one row for each token in the result. With
 134062 ** fields set as follows:
 134064 **   input:   Always set to a copy of <string>
 134065 **   token:   A token from the input.
 134066 **   start:   Byte offset of the token within the input <string>.
 134067 **   end:     Byte offset of the byte immediately following the end of the
 134068 **            token within the input string.
 134069 **   pos:     Token offset of token within input.
 134072 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 134074 /* #include <string.h> */
 134075 /* #include <assert.h> */
 134077 typedef struct Fts3tokTable Fts3tokTable;
 134078 typedef struct Fts3tokCursor Fts3tokCursor;
 134081 ** Virtual table structure.
 134083 struct Fts3tokTable {
 134084   sqlite3_vtab base;              /* Base class used by SQLite core */
 134085   const sqlite3_tokenizer_module *pMod;
 134086   sqlite3_tokenizer *pTok;
 134090 ** Virtual table cursor structure.
 134092 struct Fts3tokCursor {
 134093   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
 134094   char *zInput;                   /* Input string */
 134095   sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
 134096   int iRowid;                     /* Current 'rowid' value */
 134097   const char *zToken;             /* Current 'token' value */
 134098   int nToken;                     /* Size of zToken in bytes */
 134099   int iStart;                     /* Current 'start' value */
 134100   int iEnd;                       /* Current 'end' value */
 134101   int iPos;                       /* Current 'pos' value */
 134105 ** Query FTS for the tokenizer implementation named zName.
 134107 static int fts3tokQueryTokenizer(
 134108   Fts3Hash *pHash,
 134109   const char *zName,
 134110   const sqlite3_tokenizer_module **pp,
 134111   char **pzErr
 134113   sqlite3_tokenizer_module *p;
 134114   int nName = (int)strlen(zName);
 134116   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
 134117   if( !p ){
 134118     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
 134119     return SQLITE_ERROR;
 134122   *pp = p;
 134123   return SQLITE_OK;
 134127 ** The second argument, argv[], is an array of pointers to nul-terminated
 134128 ** strings. This function makes a copy of the array and strings into a 
 134129 ** single block of memory. It then dequotes any of the strings that appear
 134130 ** to be quoted.
 134132 ** If successful, output parameter *pazDequote is set to point at the
 134133 ** array of dequoted strings and SQLITE_OK is returned. The caller is
 134134 ** responsible for eventually calling sqlite3_free() to free the array
 134135 ** in this case. Or, if an error occurs, an SQLite error code is returned.
 134136 ** The final value of *pazDequote is undefined in this case.
 134138 static int fts3tokDequoteArray(
 134139   int argc,                       /* Number of elements in argv[] */
 134140   const char * const *argv,       /* Input array */
 134141   char ***pazDequote              /* Output array */
 134143   int rc = SQLITE_OK;             /* Return code */
 134144   if( argc==0 ){
 134145     *pazDequote = 0;
 134146   }else{
 134147     int i;
 134148     int nByte = 0;
 134149     char **azDequote;
 134151     for(i=0; i<argc; i++){
 134152       nByte += (int)(strlen(argv[i]) + 1);
 134155     *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
 134156     if( azDequote==0 ){
 134157       rc = SQLITE_NOMEM;
 134158     }else{
 134159       char *pSpace = (char *)&azDequote[argc];
 134160       for(i=0; i<argc; i++){
 134161         int n = (int)strlen(argv[i]);
 134162         azDequote[i] = pSpace;
 134163         memcpy(pSpace, argv[i], n+1);
 134164         sqlite3Fts3Dequote(pSpace);
 134165         pSpace += (n+1);
 134170   return rc;
 134174 ** Schema of the tokenizer table.
 134176 #define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
 134179 ** This function does all the work for both the xConnect and xCreate methods.
 134180 ** These tables have no persistent representation of their own, so xConnect
 134181 ** and xCreate are identical operations.
 134183 **   argv[0]: module name
 134184 **   argv[1]: database name 
 134185 **   argv[2]: table name
 134186 **   argv[3]: first argument (tokenizer name)
 134188 static int fts3tokConnectMethod(
 134189   sqlite3 *db,                    /* Database connection */
 134190   void *pHash,                    /* Hash table of tokenizers */
 134191   int argc,                       /* Number of elements in argv array */
 134192   const char * const *argv,       /* xCreate/xConnect argument array */
 134193   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
 134194   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
 134196   Fts3tokTable *pTab;
 134197   const sqlite3_tokenizer_module *pMod = 0;
 134198   sqlite3_tokenizer *pTok = 0;
 134199   int rc;
 134200   char **azDequote = 0;
 134201   int nDequote;
 134203   rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
 134204   if( rc!=SQLITE_OK ) return rc;
 134206   nDequote = argc-3;
 134207   rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
 134209   if( rc==SQLITE_OK ){
 134210     const char *zModule;
 134211     if( nDequote<1 ){
 134212       zModule = "simple";
 134213     }else{
 134214       zModule = azDequote[0];
 134216     rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
 134219   assert( (rc==SQLITE_OK)==(pMod!=0) );
 134220   if( rc==SQLITE_OK ){
 134221     const char * const *azArg = (const char * const *)&azDequote[1];
 134222     rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
 134225   if( rc==SQLITE_OK ){
 134226     pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
 134227     if( pTab==0 ){
 134228       rc = SQLITE_NOMEM;
 134232   if( rc==SQLITE_OK ){
 134233     memset(pTab, 0, sizeof(Fts3tokTable));
 134234     pTab->pMod = pMod;
 134235     pTab->pTok = pTok;
 134236     *ppVtab = &pTab->base;
 134237   }else{
 134238     if( pTok ){
 134239       pMod->xDestroy(pTok);
 134243   sqlite3_free(azDequote);
 134244   return rc;
 134248 ** This function does the work for both the xDisconnect and xDestroy methods.
 134249 ** These tables have no persistent representation of their own, so xDisconnect
 134250 ** and xDestroy are identical operations.
 134252 static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
 134253   Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
 134255   pTab->pMod->xDestroy(pTab->pTok);
 134256   sqlite3_free(pTab);
 134257   return SQLITE_OK;
 134261 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
 134263 static int fts3tokBestIndexMethod(
 134264   sqlite3_vtab *pVTab, 
 134265   sqlite3_index_info *pInfo
 134267   int i;
 134268   UNUSED_PARAMETER(pVTab);
 134270   for(i=0; i<pInfo->nConstraint; i++){
 134271     if( pInfo->aConstraint[i].usable 
 134272      && pInfo->aConstraint[i].iColumn==0 
 134273      && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ 
 134275       pInfo->idxNum = 1;
 134276       pInfo->aConstraintUsage[i].argvIndex = 1;
 134277       pInfo->aConstraintUsage[i].omit = 1;
 134278       pInfo->estimatedCost = 1;
 134279       return SQLITE_OK;
 134283   pInfo->idxNum = 0;
 134284   assert( pInfo->estimatedCost>1000000.0 );
 134286   return SQLITE_OK;
 134290 ** xOpen - Open a cursor.
 134292 static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
 134293   Fts3tokCursor *pCsr;
 134294   UNUSED_PARAMETER(pVTab);
 134296   pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
 134297   if( pCsr==0 ){
 134298     return SQLITE_NOMEM;
 134300   memset(pCsr, 0, sizeof(Fts3tokCursor));
 134302   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
 134303   return SQLITE_OK;
 134307 ** Reset the tokenizer cursor passed as the only argument. As if it had
 134308 ** just been returned by fts3tokOpenMethod().
 134310 static void fts3tokResetCursor(Fts3tokCursor *pCsr){
 134311   if( pCsr->pCsr ){
 134312     Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
 134313     pTab->pMod->xClose(pCsr->pCsr);
 134314     pCsr->pCsr = 0;
 134316   sqlite3_free(pCsr->zInput);
 134317   pCsr->zInput = 0;
 134318   pCsr->zToken = 0;
 134319   pCsr->nToken = 0;
 134320   pCsr->iStart = 0;
 134321   pCsr->iEnd = 0;
 134322   pCsr->iPos = 0;
 134323   pCsr->iRowid = 0;
 134327 ** xClose - Close a cursor.
 134329 static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
 134330   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134332   fts3tokResetCursor(pCsr);
 134333   sqlite3_free(pCsr);
 134334   return SQLITE_OK;
 134338 ** xNext - Advance the cursor to the next row, if any.
 134340 static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
 134341   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134342   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
 134343   int rc;                         /* Return code */
 134345   pCsr->iRowid++;
 134346   rc = pTab->pMod->xNext(pCsr->pCsr,
 134347       &pCsr->zToken, &pCsr->nToken,
 134348       &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
 134351   if( rc!=SQLITE_OK ){
 134352     fts3tokResetCursor(pCsr);
 134353     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 134356   return rc;
 134360 ** xFilter - Initialize a cursor to point at the start of its data.
 134362 static int fts3tokFilterMethod(
 134363   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
 134364   int idxNum,                     /* Strategy index */
 134365   const char *idxStr,             /* Unused */
 134366   int nVal,                       /* Number of elements in apVal */
 134367   sqlite3_value **apVal           /* Arguments for the indexing scheme */
 134369   int rc = SQLITE_ERROR;
 134370   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134371   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
 134372   UNUSED_PARAMETER(idxStr);
 134373   UNUSED_PARAMETER(nVal);
 134375   fts3tokResetCursor(pCsr);
 134376   if( idxNum==1 ){
 134377     const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
 134378     int nByte = sqlite3_value_bytes(apVal[0]);
 134379     pCsr->zInput = sqlite3_malloc(nByte+1);
 134380     if( pCsr->zInput==0 ){
 134381       rc = SQLITE_NOMEM;
 134382     }else{
 134383       memcpy(pCsr->zInput, zByte, nByte);
 134384       pCsr->zInput[nByte] = 0;
 134385       rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
 134386       if( rc==SQLITE_OK ){
 134387         pCsr->pCsr->pTokenizer = pTab->pTok;
 134392   if( rc!=SQLITE_OK ) return rc;
 134393   return fts3tokNextMethod(pCursor);
 134397 ** xEof - Return true if the cursor is at EOF, or false otherwise.
 134399 static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
 134400   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134401   return (pCsr->zToken==0);
 134405 ** xColumn - Return a column value.
 134407 static int fts3tokColumnMethod(
 134408   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 134409   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
 134410   int iCol                        /* Index of column to read value from */
 134412   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134414   /* CREATE TABLE x(input, token, start, end, position) */
 134415   switch( iCol ){
 134416     case 0:
 134417       sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
 134418       break;
 134419     case 1:
 134420       sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
 134421       break;
 134422     case 2:
 134423       sqlite3_result_int(pCtx, pCsr->iStart);
 134424       break;
 134425     case 3:
 134426       sqlite3_result_int(pCtx, pCsr->iEnd);
 134427       break;
 134428     default:
 134429       assert( iCol==4 );
 134430       sqlite3_result_int(pCtx, pCsr->iPos);
 134431       break;
 134433   return SQLITE_OK;
 134437 ** xRowid - Return the current rowid for the cursor.
 134439 static int fts3tokRowidMethod(
 134440   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
 134441   sqlite_int64 *pRowid            /* OUT: Rowid value */
 134443   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
 134444   *pRowid = (sqlite3_int64)pCsr->iRowid;
 134445   return SQLITE_OK;
 134449 ** Register the fts3tok module with database connection db. Return SQLITE_OK
 134450 ** if successful or an error code if sqlite3_create_module() fails.
 134452 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
 134453   static const sqlite3_module fts3tok_module = {
 134454      0,                           /* iVersion      */
 134455      fts3tokConnectMethod,        /* xCreate       */
 134456      fts3tokConnectMethod,        /* xConnect      */
 134457      fts3tokBestIndexMethod,      /* xBestIndex    */
 134458      fts3tokDisconnectMethod,     /* xDisconnect   */
 134459      fts3tokDisconnectMethod,     /* xDestroy      */
 134460      fts3tokOpenMethod,           /* xOpen         */
 134461      fts3tokCloseMethod,          /* xClose        */
 134462      fts3tokFilterMethod,         /* xFilter       */
 134463      fts3tokNextMethod,           /* xNext         */
 134464      fts3tokEofMethod,            /* xEof          */
 134465      fts3tokColumnMethod,         /* xColumn       */
 134466      fts3tokRowidMethod,          /* xRowid        */
 134467      0,                           /* xUpdate       */
 134468      0,                           /* xBegin        */
 134469      0,                           /* xSync         */
 134470      0,                           /* xCommit       */
 134471      0,                           /* xRollback     */
 134472      0,                           /* xFindFunction */
 134473      0,                           /* xRename       */
 134474      0,                           /* xSavepoint    */
 134475      0,                           /* xRelease      */
 134476      0                            /* xRollbackTo   */
 134478   int rc;                         /* Return code */
 134480   rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
 134481   return rc;
 134484 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 134486 /************** End of fts3_tokenize_vtab.c **********************************/
 134487 /************** Begin file fts3_write.c **************************************/
 134489 ** 2009 Oct 23
 134491 ** The author disclaims copyright to this source code.  In place of
 134492 ** a legal notice, here is a blessing:
 134494 **    May you do good and not evil.
 134495 **    May you find forgiveness for yourself and forgive others.
 134496 **    May you share freely, never taking more than you give.
 134498 ******************************************************************************
 134500 ** This file is part of the SQLite FTS3 extension module. Specifically,
 134501 ** this file contains code to insert, update and delete rows from FTS3
 134502 ** tables. It also contains code to merge FTS3 b-tree segments. Some
 134503 ** of the sub-routines used to merge segments are also used by the query 
 134504 ** code in fts3.c.
 134507 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 134509 /* #include <string.h> */
 134510 /* #include <assert.h> */
 134511 /* #include <stdlib.h> */
 134514 #define FTS_MAX_APPENDABLE_HEIGHT 16
 134517 ** When full-text index nodes are loaded from disk, the buffer that they
 134518 ** are loaded into has the following number of bytes of padding at the end 
 134519 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
 134520 ** of 920 bytes is allocated for it.
 134522 ** This means that if we have a pointer into a buffer containing node data,
 134523 ** it is always safe to read up to two varints from it without risking an
 134524 ** overread, even if the node data is corrupted.
 134526 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
 134529 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
 134530 ** memory incrementally instead of all at once. This can be a big performance
 134531 ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
 134532 ** method before retrieving all query results (as may happen, for example,
 134533 ** if a query has a LIMIT clause).
 134535 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD 
 134536 ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
 134537 ** The code is written so that the hard lower-limit for each of these values 
 134538 ** is 1. Clearly such small values would be inefficient, but can be useful 
 134539 ** for testing purposes.
 134541 ** If this module is built with SQLITE_TEST defined, these constants may
 134542 ** be overridden at runtime for testing purposes. File fts3_test.c contains
 134543 ** a Tcl interface to read and write the values.
 134545 #ifdef SQLITE_TEST
 134546 int test_fts3_node_chunksize = (4*1024);
 134547 int test_fts3_node_chunk_threshold = (4*1024)*4;
 134548 # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
 134549 # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
 134550 #else
 134551 # define FTS3_NODE_CHUNKSIZE (4*1024) 
 134552 # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
 134553 #endif
 134556 ** The two values that may be meaningfully bound to the :1 parameter in
 134557 ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
 134559 #define FTS_STAT_DOCTOTAL      0
 134560 #define FTS_STAT_INCRMERGEHINT 1
 134561 #define FTS_STAT_AUTOINCRMERGE 2
 134564 ** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
 134565 ** and incremental merge operation that takes place. This is used for 
 134566 ** debugging FTS only, it should not usually be turned on in production
 134567 ** systems.
 134569 #ifdef FTS3_LOG_MERGES
 134570 static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
 134571   sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
 134573 #else
 134574 #define fts3LogMerge(x, y)
 134575 #endif
 134578 typedef struct PendingList PendingList;
 134579 typedef struct SegmentNode SegmentNode;
 134580 typedef struct SegmentWriter SegmentWriter;
 134583 ** An instance of the following data structure is used to build doclists
 134584 ** incrementally. See function fts3PendingListAppend() for details.
 134586 struct PendingList {
 134587   int nData;
 134588   char *aData;
 134589   int nSpace;
 134590   sqlite3_int64 iLastDocid;
 134591   sqlite3_int64 iLastCol;
 134592   sqlite3_int64 iLastPos;
 134597 ** Each cursor has a (possibly empty) linked list of the following objects.
 134599 struct Fts3DeferredToken {
 134600   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
 134601   int iCol;                       /* Column token must occur in */
 134602   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
 134603   PendingList *pList;             /* Doclist is assembled here */
 134607 ** An instance of this structure is used to iterate through the terms on
 134608 ** a contiguous set of segment b-tree leaf nodes. Although the details of
 134609 ** this structure are only manipulated by code in this file, opaque handles
 134610 ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
 134611 ** terms when querying the full-text index. See functions:
 134613 **   sqlite3Fts3SegReaderNew()
 134614 **   sqlite3Fts3SegReaderFree()
 134615 **   sqlite3Fts3SegReaderIterate()
 134617 ** Methods used to manipulate Fts3SegReader structures:
 134619 **   fts3SegReaderNext()
 134620 **   fts3SegReaderFirstDocid()
 134621 **   fts3SegReaderNextDocid()
 134623 struct Fts3SegReader {
 134624   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
 134625   u8 bLookup;                     /* True for a lookup only */
 134626   u8 rootOnly;                    /* True for a root-only reader */
 134628   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
 134629   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
 134630   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
 134631   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
 134633   char *aNode;                    /* Pointer to node data (or NULL) */
 134634   int nNode;                      /* Size of buffer at aNode (or 0) */
 134635   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
 134636   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
 134638   Fts3HashElem **ppNextElem;
 134640   /* Variables set by fts3SegReaderNext(). These may be read directly
 134641   ** by the caller. They are valid from the time SegmentReaderNew() returns
 134642   ** until SegmentReaderNext() returns something other than SQLITE_OK
 134643   ** (i.e. SQLITE_DONE).
 134645   int nTerm;                      /* Number of bytes in current term */
 134646   char *zTerm;                    /* Pointer to current term */
 134647   int nTermAlloc;                 /* Allocated size of zTerm buffer */
 134648   char *aDoclist;                 /* Pointer to doclist of current entry */
 134649   int nDoclist;                   /* Size of doclist in current entry */
 134651   /* The following variables are used by fts3SegReaderNextDocid() to iterate 
 134652   ** through the current doclist (aDoclist/nDoclist).
 134654   char *pOffsetList;
 134655   int nOffsetList;                /* For descending pending seg-readers only */
 134656   sqlite3_int64 iDocid;
 134659 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
 134660 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
 134663 ** An instance of this structure is used to create a segment b-tree in the
 134664 ** database. The internal details of this type are only accessed by the
 134665 ** following functions:
 134667 **   fts3SegWriterAdd()
 134668 **   fts3SegWriterFlush()
 134669 **   fts3SegWriterFree()
 134671 struct SegmentWriter {
 134672   SegmentNode *pTree;             /* Pointer to interior tree structure */
 134673   sqlite3_int64 iFirst;           /* First slot in %_segments written */
 134674   sqlite3_int64 iFree;            /* Next free slot in %_segments */
 134675   char *zTerm;                    /* Pointer to previous term buffer */
 134676   int nTerm;                      /* Number of bytes in zTerm */
 134677   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
 134678   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
 134679   int nSize;                      /* Size of allocation at aData */
 134680   int nData;                      /* Bytes of data in aData */
 134681   char *aData;                    /* Pointer to block from malloc() */
 134685 ** Type SegmentNode is used by the following three functions to create
 134686 ** the interior part of the segment b+-tree structures (everything except
 134687 ** the leaf nodes). These functions and type are only ever used by code
 134688 ** within the fts3SegWriterXXX() family of functions described above.
 134690 **   fts3NodeAddTerm()
 134691 **   fts3NodeWrite()
 134692 **   fts3NodeFree()
 134694 ** When a b+tree is written to the database (either as a result of a merge
 134695 ** or the pending-terms table being flushed), leaves are written into the 
 134696 ** database file as soon as they are completely populated. The interior of
 134697 ** the tree is assembled in memory and written out only once all leaves have
 134698 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
 134699 ** very large, meaning that the interior of the tree consumes relatively 
 134700 ** little memory.
 134702 struct SegmentNode {
 134703   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
 134704   SegmentNode *pRight;            /* Pointer to right-sibling */
 134705   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
 134706   int nEntry;                     /* Number of terms written to node so far */
 134707   char *zTerm;                    /* Pointer to previous term buffer */
 134708   int nTerm;                      /* Number of bytes in zTerm */
 134709   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
 134710   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
 134711   int nData;                      /* Bytes of valid data so far */
 134712   char *aData;                    /* Node data */
 134716 ** Valid values for the second argument to fts3SqlStmt().
 134718 #define SQL_DELETE_CONTENT             0
 134719 #define SQL_IS_EMPTY                   1
 134720 #define SQL_DELETE_ALL_CONTENT         2 
 134721 #define SQL_DELETE_ALL_SEGMENTS        3
 134722 #define SQL_DELETE_ALL_SEGDIR          4
 134723 #define SQL_DELETE_ALL_DOCSIZE         5
 134724 #define SQL_DELETE_ALL_STAT            6
 134725 #define SQL_SELECT_CONTENT_BY_ROWID    7
 134726 #define SQL_NEXT_SEGMENT_INDEX         8
 134727 #define SQL_INSERT_SEGMENTS            9
 134728 #define SQL_NEXT_SEGMENTS_ID          10
 134729 #define SQL_INSERT_SEGDIR             11
 134730 #define SQL_SELECT_LEVEL              12
 134731 #define SQL_SELECT_LEVEL_RANGE        13
 134732 #define SQL_SELECT_LEVEL_COUNT        14
 134733 #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
 134734 #define SQL_DELETE_SEGDIR_LEVEL       16
 134735 #define SQL_DELETE_SEGMENTS_RANGE     17
 134736 #define SQL_CONTENT_INSERT            18
 134737 #define SQL_DELETE_DOCSIZE            19
 134738 #define SQL_REPLACE_DOCSIZE           20
 134739 #define SQL_SELECT_DOCSIZE            21
 134740 #define SQL_SELECT_STAT               22
 134741 #define SQL_REPLACE_STAT              23
 134743 #define SQL_SELECT_ALL_PREFIX_LEVEL   24
 134744 #define SQL_DELETE_ALL_TERMS_SEGDIR   25
 134745 #define SQL_DELETE_SEGDIR_RANGE       26
 134746 #define SQL_SELECT_ALL_LANGID         27
 134747 #define SQL_FIND_MERGE_LEVEL          28
 134748 #define SQL_MAX_LEAF_NODE_ESTIMATE    29
 134749 #define SQL_DELETE_SEGDIR_ENTRY       30
 134750 #define SQL_SHIFT_SEGDIR_ENTRY        31
 134751 #define SQL_SELECT_SEGDIR             32
 134752 #define SQL_CHOMP_SEGDIR              33
 134753 #define SQL_SEGMENT_IS_APPENDABLE     34
 134754 #define SQL_SELECT_INDEXES            35
 134755 #define SQL_SELECT_MXLEVEL            36
 134758 ** This function is used to obtain an SQLite prepared statement handle
 134759 ** for the statement identified by the second argument. If successful,
 134760 ** *pp is set to the requested statement handle and SQLITE_OK returned.
 134761 ** Otherwise, an SQLite error code is returned and *pp is set to 0.
 134763 ** If argument apVal is not NULL, then it must point to an array with
 134764 ** at least as many entries as the requested statement has bound 
 134765 ** parameters. The values are bound to the statements parameters before
 134766 ** returning.
 134768 static int fts3SqlStmt(
 134769   Fts3Table *p,                   /* Virtual table handle */
 134770   int eStmt,                      /* One of the SQL_XXX constants above */
 134771   sqlite3_stmt **pp,              /* OUT: Statement handle */
 134772   sqlite3_value **apVal           /* Values to bind to statement */
 134774   const char *azSql[] = {
 134775 /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
 134776 /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
 134777 /* 2  */  "DELETE FROM %Q.'%q_content'",
 134778 /* 3  */  "DELETE FROM %Q.'%q_segments'",
 134779 /* 4  */  "DELETE FROM %Q.'%q_segdir'",
 134780 /* 5  */  "DELETE FROM %Q.'%q_docsize'",
 134781 /* 6  */  "DELETE FROM %Q.'%q_stat'",
 134782 /* 7  */  "SELECT %s WHERE rowid=?",
 134783 /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
 134784 /* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
 134785 /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
 134786 /* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
 134788           /* Return segments in order from oldest to newest.*/ 
 134789 /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 134790             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
 134791 /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 134792             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
 134793             "ORDER BY level DESC, idx ASC",
 134795 /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
 134796 /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
 134798 /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
 134799 /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
 134800 /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
 134801 /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
 134802 /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
 134803 /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
 134804 /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
 134805 /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
 134806 /* 24 */  "",
 134807 /* 25 */  "",
 134809 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
 134810 /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
 134812 /* This statement is used to determine which level to read the input from
 134813 ** when performing an incremental merge. It returns the absolute level number
 134814 ** of the oldest level in the db that contains at least ? segments. Or,
 134815 ** if no level in the FTS index contains more than ? segments, the statement
 134816 ** returns zero rows.  */
 134817 /* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
 134818          "  ORDER BY (level %% 1024) ASC LIMIT 1",
 134820 /* Estimate the upper limit on the number of leaf nodes in a new segment
 134821 ** created by merging the oldest :2 segments from absolute level :1. See 
 134822 ** function sqlite3Fts3Incrmerge() for details.  */
 134823 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
 134824          "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
 134826 /* SQL_DELETE_SEGDIR_ENTRY
 134827 **   Delete the %_segdir entry on absolute level :1 with index :2.  */
 134828 /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
 134830 /* SQL_SHIFT_SEGDIR_ENTRY
 134831 **   Modify the idx value for the segment with idx=:3 on absolute level :2
 134832 **   to :1.  */
 134833 /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
 134835 /* SQL_SELECT_SEGDIR
 134836 **   Read a single entry from the %_segdir table. The entry from absolute 
 134837 **   level :1 with index value :2.  */
 134838 /* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
 134839             "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
 134841 /* SQL_CHOMP_SEGDIR
 134842 **   Update the start_block (:1) and root (:2) fields of the %_segdir
 134843 **   entry located on absolute level :3 with index :4.  */
 134844 /* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
 134845             "WHERE level = ? AND idx = ?",
 134847 /* SQL_SEGMENT_IS_APPENDABLE
 134848 **   Return a single row if the segment with end_block=? is appendable. Or
 134849 **   no rows otherwise.  */
 134850 /* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
 134852 /* SQL_SELECT_INDEXES
 134853 **   Return the list of valid segment indexes for absolute level ?  */
 134854 /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
 134856 /* SQL_SELECT_MXLEVEL
 134857 **   Return the largest relative level in the FTS index or indexes.  */
 134858 /* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
 134860   int rc = SQLITE_OK;
 134861   sqlite3_stmt *pStmt;
 134863   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
 134864   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
 134866   pStmt = p->aStmt[eStmt];
 134867   if( !pStmt ){
 134868     char *zSql;
 134869     if( eStmt==SQL_CONTENT_INSERT ){
 134870       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
 134871     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
 134872       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
 134873     }else{
 134874       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
 134876     if( !zSql ){
 134877       rc = SQLITE_NOMEM;
 134878     }else{
 134879       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
 134880       sqlite3_free(zSql);
 134881       assert( rc==SQLITE_OK || pStmt==0 );
 134882       p->aStmt[eStmt] = pStmt;
 134885   if( apVal ){
 134886     int i;
 134887     int nParam = sqlite3_bind_parameter_count(pStmt);
 134888     for(i=0; rc==SQLITE_OK && i<nParam; i++){
 134889       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
 134892   *pp = pStmt;
 134893   return rc;
 134897 static int fts3SelectDocsize(
 134898   Fts3Table *pTab,                /* FTS3 table handle */
 134899   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
 134900   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 134902   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
 134903   int rc;                         /* Return code */
 134905   rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
 134906   if( rc==SQLITE_OK ){
 134907     sqlite3_bind_int64(pStmt, 1, iDocid);
 134908     rc = sqlite3_step(pStmt);
 134909     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
 134910       rc = sqlite3_reset(pStmt);
 134911       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
 134912       pStmt = 0;
 134913     }else{
 134914       rc = SQLITE_OK;
 134918   *ppStmt = pStmt;
 134919   return rc;
 134922 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
 134923   Fts3Table *pTab,                /* Fts3 table handle */
 134924   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 134926   sqlite3_stmt *pStmt = 0;
 134927   int rc;
 134928   rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
 134929   if( rc==SQLITE_OK ){
 134930     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 134931     if( sqlite3_step(pStmt)!=SQLITE_ROW
 134932      || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
 134934       rc = sqlite3_reset(pStmt);
 134935       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
 134936       pStmt = 0;
 134939   *ppStmt = pStmt;
 134940   return rc;
 134943 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
 134944   Fts3Table *pTab,                /* Fts3 table handle */
 134945   sqlite3_int64 iDocid,           /* Docid to read size data for */
 134946   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
 134948   return fts3SelectDocsize(pTab, iDocid, ppStmt);
 134952 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
 134953 ** array apVal[] to the SQL statement identified by eStmt, the statement
 134954 ** is executed.
 134956 ** Returns SQLITE_OK if the statement is successfully executed, or an
 134957 ** SQLite error code otherwise.
 134959 static void fts3SqlExec(
 134960   int *pRC,                /* Result code */
 134961   Fts3Table *p,            /* The FTS3 table */
 134962   int eStmt,               /* Index of statement to evaluate */
 134963   sqlite3_value **apVal    /* Parameters to bind */
 134965   sqlite3_stmt *pStmt;
 134966   int rc;
 134967   if( *pRC ) return;
 134968   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
 134969   if( rc==SQLITE_OK ){
 134970     sqlite3_step(pStmt);
 134971     rc = sqlite3_reset(pStmt);
 134973   *pRC = rc;
 134978 ** This function ensures that the caller has obtained an exclusive 
 134979 ** shared-cache table-lock on the %_segdir table. This is required before 
 134980 ** writing data to the fts3 table. If this lock is not acquired first, then
 134981 ** the caller may end up attempting to take this lock as part of committing
 134982 ** a transaction, causing SQLite to return SQLITE_LOCKED or 
 134983 ** LOCKED_SHAREDCACHEto a COMMIT command.
 134985 ** It is best to avoid this because if FTS3 returns any error when 
 134986 ** committing a transaction, the whole transaction will be rolled back. 
 134987 ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. 
 134988 ** It can still happen if the user locks the underlying tables directly 
 134989 ** instead of accessing them via FTS.
 134991 static int fts3Writelock(Fts3Table *p){
 134992   int rc = SQLITE_OK;
 134994   if( p->nPendingData==0 ){
 134995     sqlite3_stmt *pStmt;
 134996     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
 134997     if( rc==SQLITE_OK ){
 134998       sqlite3_bind_null(pStmt, 1);
 134999       sqlite3_step(pStmt);
 135000       rc = sqlite3_reset(pStmt);
 135004   return rc;
 135008 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
 135009 ** Within each language id, a separate index is maintained to store the
 135010 ** document terms, and each configured prefix size (configured the FTS 
 135011 ** "prefix=" option). And each index consists of multiple levels ("relative
 135012 ** levels").
 135014 ** All three of these values (the language id, the specific index and the
 135015 ** level within the index) are encoded in 64-bit integer values stored
 135016 ** in the %_segdir table on disk. This function is used to convert three
 135017 ** separate component values into the single 64-bit integer value that
 135018 ** can be used to query the %_segdir table.
 135020 ** Specifically, each language-id/index combination is allocated 1024 
 135021 ** 64-bit integer level values ("absolute levels"). The main terms index
 135022 ** for language-id 0 is allocate values 0-1023. The first prefix index
 135023 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
 135024 ** Language 1 indexes are allocated immediately following language 0.
 135026 ** So, for a system with nPrefix prefix indexes configured, the block of
 135027 ** absolute levels that corresponds to language-id iLangid and index 
 135028 ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
 135030 static sqlite3_int64 getAbsoluteLevel(
 135031   Fts3Table *p,                   /* FTS3 table handle */
 135032   int iLangid,                    /* Language id */
 135033   int iIndex,                     /* Index in p->aIndex[] */
 135034   int iLevel                      /* Level of segments */
 135036   sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
 135037   assert( iLangid>=0 );
 135038   assert( p->nIndex>0 );
 135039   assert( iIndex>=0 && iIndex<p->nIndex );
 135041   iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
 135042   return iBase + iLevel;
 135046 ** Set *ppStmt to a statement handle that may be used to iterate through
 135047 ** all rows in the %_segdir table, from oldest to newest. If successful,
 135048 ** return SQLITE_OK. If an error occurs while preparing the statement, 
 135049 ** return an SQLite error code.
 135051 ** There is only ever one instance of this SQL statement compiled for
 135052 ** each FTS3 table.
 135054 ** The statement returns the following columns from the %_segdir table:
 135056 **   0: idx
 135057 **   1: start_block
 135058 **   2: leaves_end_block
 135059 **   3: end_block
 135060 **   4: root
 135062 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
 135063   Fts3Table *p,                   /* FTS3 table */
 135064   int iLangid,                    /* Language being queried */
 135065   int iIndex,                     /* Index for p->aIndex[] */
 135066   int iLevel,                     /* Level to select (relative level) */
 135067   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
 135069   int rc;
 135070   sqlite3_stmt *pStmt = 0;
 135072   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
 135073   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 135074   assert( iIndex>=0 && iIndex<p->nIndex );
 135076   if( iLevel<0 ){
 135077     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
 135078     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
 135079     if( rc==SQLITE_OK ){ 
 135080       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 135081       sqlite3_bind_int64(pStmt, 2, 
 135082           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 135085   }else{
 135086     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
 135087     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
 135088     if( rc==SQLITE_OK ){ 
 135089       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
 135092   *ppStmt = pStmt;
 135093   return rc;
 135098 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
 135099 ** if successful, or an SQLite error code otherwise.
 135101 ** This function also serves to allocate the PendingList structure itself.
 135102 ** For example, to create a new PendingList structure containing two
 135103 ** varints:
 135105 **   PendingList *p = 0;
 135106 **   fts3PendingListAppendVarint(&p, 1);
 135107 **   fts3PendingListAppendVarint(&p, 2);
 135109 static int fts3PendingListAppendVarint(
 135110   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
 135111   sqlite3_int64 i                 /* Value to append to data */
 135113   PendingList *p = *pp;
 135115   /* Allocate or grow the PendingList as required. */
 135116   if( !p ){
 135117     p = sqlite3_malloc(sizeof(*p) + 100);
 135118     if( !p ){
 135119       return SQLITE_NOMEM;
 135121     p->nSpace = 100;
 135122     p->aData = (char *)&p[1];
 135123     p->nData = 0;
 135125   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
 135126     int nNew = p->nSpace * 2;
 135127     p = sqlite3_realloc(p, sizeof(*p) + nNew);
 135128     if( !p ){
 135129       sqlite3_free(*pp);
 135130       *pp = 0;
 135131       return SQLITE_NOMEM;
 135133     p->nSpace = nNew;
 135134     p->aData = (char *)&p[1];
 135137   /* Append the new serialized varint to the end of the list. */
 135138   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
 135139   p->aData[p->nData] = '\0';
 135140   *pp = p;
 135141   return SQLITE_OK;
 135145 ** Add a docid/column/position entry to a PendingList structure. Non-zero
 135146 ** is returned if the structure is sqlite3_realloced as part of adding
 135147 ** the entry. Otherwise, zero.
 135149 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
 135150 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
 135151 ** it is set to SQLITE_OK.
 135153 static int fts3PendingListAppend(
 135154   PendingList **pp,               /* IN/OUT: PendingList structure */
 135155   sqlite3_int64 iDocid,           /* Docid for entry to add */
 135156   sqlite3_int64 iCol,             /* Column for entry to add */
 135157   sqlite3_int64 iPos,             /* Position of term for entry to add */
 135158   int *pRc                        /* OUT: Return code */
 135160   PendingList *p = *pp;
 135161   int rc = SQLITE_OK;
 135163   assert( !p || p->iLastDocid<=iDocid );
 135165   if( !p || p->iLastDocid!=iDocid ){
 135166     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
 135167     if( p ){
 135168       assert( p->nData<p->nSpace );
 135169       assert( p->aData[p->nData]==0 );
 135170       p->nData++;
 135172     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
 135173       goto pendinglistappend_out;
 135175     p->iLastCol = -1;
 135176     p->iLastPos = 0;
 135177     p->iLastDocid = iDocid;
 135179   if( iCol>0 && p->iLastCol!=iCol ){
 135180     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
 135181      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
 135183       goto pendinglistappend_out;
 135185     p->iLastCol = iCol;
 135186     p->iLastPos = 0;
 135188   if( iCol>=0 ){
 135189     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
 135190     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
 135191     if( rc==SQLITE_OK ){
 135192       p->iLastPos = iPos;
 135196  pendinglistappend_out:
 135197   *pRc = rc;
 135198   if( p!=*pp ){
 135199     *pp = p;
 135200     return 1;
 135202   return 0;
 135206 ** Free a PendingList object allocated by fts3PendingListAppend().
 135208 static void fts3PendingListDelete(PendingList *pList){
 135209   sqlite3_free(pList);
 135213 ** Add an entry to one of the pending-terms hash tables.
 135215 static int fts3PendingTermsAddOne(
 135216   Fts3Table *p,
 135217   int iCol,
 135218   int iPos,
 135219   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
 135220   const char *zToken,
 135221   int nToken
 135223   PendingList *pList;
 135224   int rc = SQLITE_OK;
 135226   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
 135227   if( pList ){
 135228     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
 135230   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
 135231     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
 135232       /* Malloc failed while inserting the new entry. This can only 
 135233       ** happen if there was no previous entry for this token.
 135235       assert( 0==fts3HashFind(pHash, zToken, nToken) );
 135236       sqlite3_free(pList);
 135237       rc = SQLITE_NOMEM;
 135240   if( rc==SQLITE_OK ){
 135241     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
 135243   return rc;
 135247 ** Tokenize the nul-terminated string zText and add all tokens to the
 135248 ** pending-terms hash-table. The docid used is that currently stored in
 135249 ** p->iPrevDocid, and the column is specified by argument iCol.
 135251 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
 135253 static int fts3PendingTermsAdd(
 135254   Fts3Table *p,                   /* Table into which text will be inserted */
 135255   int iLangid,                    /* Language id to use */
 135256   const char *zText,              /* Text of document to be inserted */
 135257   int iCol,                       /* Column into which text is being inserted */
 135258   u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
 135260   int rc;
 135261   int iStart = 0;
 135262   int iEnd = 0;
 135263   int iPos = 0;
 135264   int nWord = 0;
 135266   char const *zToken;
 135267   int nToken = 0;
 135269   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
 135270   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
 135271   sqlite3_tokenizer_cursor *pCsr;
 135272   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
 135273       const char**,int*,int*,int*,int*);
 135275   assert( pTokenizer && pModule );
 135277   /* If the user has inserted a NULL value, this function may be called with
 135278   ** zText==0. In this case, add zero token entries to the hash table and 
 135279   ** return early. */
 135280   if( zText==0 ){
 135281     *pnWord = 0;
 135282     return SQLITE_OK;
 135285   rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
 135286   if( rc!=SQLITE_OK ){
 135287     return rc;
 135290   xNext = pModule->xNext;
 135291   while( SQLITE_OK==rc
 135292       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
 135294     int i;
 135295     if( iPos>=nWord ) nWord = iPos+1;
 135297     /* Positions cannot be negative; we use -1 as a terminator internally.
 135298     ** Tokens must have a non-zero length.
 135300     if( iPos<0 || !zToken || nToken<=0 ){
 135301       rc = SQLITE_ERROR;
 135302       break;
 135305     /* Add the term to the terms index */
 135306     rc = fts3PendingTermsAddOne(
 135307         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
 135310     /* Add the term to each of the prefix indexes that it is not too 
 135311     ** short for. */
 135312     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
 135313       struct Fts3Index *pIndex = &p->aIndex[i];
 135314       if( nToken<pIndex->nPrefix ) continue;
 135315       rc = fts3PendingTermsAddOne(
 135316           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
 135321   pModule->xClose(pCsr);
 135322   *pnWord += nWord;
 135323   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
 135327 ** Calling this function indicates that subsequent calls to 
 135328 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
 135329 ** contents of the document with docid iDocid.
 135331 static int fts3PendingTermsDocid(
 135332   Fts3Table *p,                   /* Full-text table handle */
 135333   int iLangid,                    /* Language id of row being written */
 135334   sqlite_int64 iDocid             /* Docid of row being written */
 135336   assert( iLangid>=0 );
 135338   /* TODO(shess) Explore whether partially flushing the buffer on
 135339   ** forced-flush would provide better performance.  I suspect that if
 135340   ** we ordered the doclists by size and flushed the largest until the
 135341   ** buffer was half empty, that would let the less frequent terms
 135342   ** generate longer doclists.
 135344   if( iDocid<=p->iPrevDocid 
 135345    || p->iPrevLangid!=iLangid
 135346    || p->nPendingData>p->nMaxPendingData 
 135348     int rc = sqlite3Fts3PendingTermsFlush(p);
 135349     if( rc!=SQLITE_OK ) return rc;
 135351   p->iPrevDocid = iDocid;
 135352   p->iPrevLangid = iLangid;
 135353   return SQLITE_OK;
 135357 ** Discard the contents of the pending-terms hash tables. 
 135359 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
 135360   int i;
 135361   for(i=0; i<p->nIndex; i++){
 135362     Fts3HashElem *pElem;
 135363     Fts3Hash *pHash = &p->aIndex[i].hPending;
 135364     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
 135365       PendingList *pList = (PendingList *)fts3HashData(pElem);
 135366       fts3PendingListDelete(pList);
 135368     fts3HashClear(pHash);
 135370   p->nPendingData = 0;
 135374 ** This function is called by the xUpdate() method as part of an INSERT
 135375 ** operation. It adds entries for each term in the new record to the
 135376 ** pendingTerms hash table.
 135378 ** Argument apVal is the same as the similarly named argument passed to
 135379 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
 135381 static int fts3InsertTerms(
 135382   Fts3Table *p, 
 135383   int iLangid, 
 135384   sqlite3_value **apVal, 
 135385   u32 *aSz
 135387   int i;                          /* Iterator variable */
 135388   for(i=2; i<p->nColumn+2; i++){
 135389     int iCol = i-2;
 135390     if( p->abNotindexed[iCol]==0 ){
 135391       const char *zText = (const char *)sqlite3_value_text(apVal[i]);
 135392       int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
 135393       if( rc!=SQLITE_OK ){
 135394         return rc;
 135396       aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
 135399   return SQLITE_OK;
 135403 ** This function is called by the xUpdate() method for an INSERT operation.
 135404 ** The apVal parameter is passed a copy of the apVal argument passed by
 135405 ** SQLite to the xUpdate() method. i.e:
 135407 **   apVal[0]                Not used for INSERT.
 135408 **   apVal[1]                rowid
 135409 **   apVal[2]                Left-most user-defined column
 135410 **   ...
 135411 **   apVal[p->nColumn+1]     Right-most user-defined column
 135412 **   apVal[p->nColumn+2]     Hidden column with same name as table
 135413 **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
 135414 **   apVal[p->nColumn+4]     Hidden languageid column
 135416 static int fts3InsertData(
 135417   Fts3Table *p,                   /* Full-text table */
 135418   sqlite3_value **apVal,          /* Array of values to insert */
 135419   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
 135421   int rc;                         /* Return code */
 135422   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
 135424   if( p->zContentTbl ){
 135425     sqlite3_value *pRowid = apVal[p->nColumn+3];
 135426     if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
 135427       pRowid = apVal[1];
 135429     if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
 135430       return SQLITE_CONSTRAINT;
 135432     *piDocid = sqlite3_value_int64(pRowid);
 135433     return SQLITE_OK;
 135436   /* Locate the statement handle used to insert data into the %_content
 135437   ** table. The SQL for this statement is:
 135439   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
 135441   ** The statement features N '?' variables, where N is the number of user
 135442   ** defined columns in the FTS3 table, plus one for the docid field.
 135444   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
 135445   if( rc==SQLITE_OK && p->zLanguageid ){
 135446     rc = sqlite3_bind_int(
 135447         pContentInsert, p->nColumn+2, 
 135448         sqlite3_value_int(apVal[p->nColumn+4])
 135451   if( rc!=SQLITE_OK ) return rc;
 135453   /* There is a quirk here. The users INSERT statement may have specified
 135454   ** a value for the "rowid" field, for the "docid" field, or for both.
 135455   ** Which is a problem, since "rowid" and "docid" are aliases for the
 135456   ** same value. For example:
 135458   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
 135460   ** In FTS3, this is an error. It is an error to specify non-NULL values
 135461   ** for both docid and some other rowid alias.
 135463   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
 135464     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
 135465      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
 135467       /* A rowid/docid conflict. */
 135468       return SQLITE_ERROR;
 135470     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
 135471     if( rc!=SQLITE_OK ) return rc;
 135474   /* Execute the statement to insert the record. Set *piDocid to the 
 135475   ** new docid value. 
 135477   sqlite3_step(pContentInsert);
 135478   rc = sqlite3_reset(pContentInsert);
 135480   *piDocid = sqlite3_last_insert_rowid(p->db);
 135481   return rc;
 135487 ** Remove all data from the FTS3 table. Clear the hash table containing
 135488 ** pending terms.
 135490 static int fts3DeleteAll(Fts3Table *p, int bContent){
 135491   int rc = SQLITE_OK;             /* Return code */
 135493   /* Discard the contents of the pending-terms hash table. */
 135494   sqlite3Fts3PendingTermsClear(p);
 135496   /* Delete everything from the shadow tables. Except, leave %_content as
 135497   ** is if bContent is false.  */
 135498   assert( p->zContentTbl==0 || bContent==0 );
 135499   if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
 135500   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
 135501   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
 135502   if( p->bHasDocsize ){
 135503     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
 135505   if( p->bHasStat ){
 135506     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
 135508   return rc;
 135514 static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
 135515   int iLangid = 0;
 135516   if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
 135517   return iLangid;
 135521 ** The first element in the apVal[] array is assumed to contain the docid
 135522 ** (an integer) of a row about to be deleted. Remove all terms from the
 135523 ** full-text index.
 135525 static void fts3DeleteTerms( 
 135526   int *pRC,               /* Result code */
 135527   Fts3Table *p,           /* The FTS table to delete from */
 135528   sqlite3_value *pRowid,  /* The docid to be deleted */
 135529   u32 *aSz,               /* Sizes of deleted document written here */
 135530   int *pbFound            /* OUT: Set to true if row really does exist */
 135532   int rc;
 135533   sqlite3_stmt *pSelect;
 135535   assert( *pbFound==0 );
 135536   if( *pRC ) return;
 135537   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
 135538   if( rc==SQLITE_OK ){
 135539     if( SQLITE_ROW==sqlite3_step(pSelect) ){
 135540       int i;
 135541       int iLangid = langidFromSelect(p, pSelect);
 135542       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
 135543       for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
 135544         int iCol = i-1;
 135545         if( p->abNotindexed[iCol]==0 ){
 135546           const char *zText = (const char *)sqlite3_column_text(pSelect, i);
 135547           rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
 135548           aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
 135551       if( rc!=SQLITE_OK ){
 135552         sqlite3_reset(pSelect);
 135553         *pRC = rc;
 135554         return;
 135556       *pbFound = 1;
 135558     rc = sqlite3_reset(pSelect);
 135559   }else{
 135560     sqlite3_reset(pSelect);
 135562   *pRC = rc;
 135566 ** Forward declaration to account for the circular dependency between
 135567 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
 135569 static int fts3SegmentMerge(Fts3Table *, int, int, int);
 135572 ** This function allocates a new level iLevel index in the segdir table.
 135573 ** Usually, indexes are allocated within a level sequentially starting
 135574 ** with 0, so the allocated index is one greater than the value returned
 135575 ** by:
 135577 **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
 135579 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
 135580 ** level, they are merged into a single level (iLevel+1) segment and the 
 135581 ** allocated index is 0.
 135583 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
 135584 ** returned. Otherwise, an SQLite error code is returned.
 135586 static int fts3AllocateSegdirIdx(
 135587   Fts3Table *p, 
 135588   int iLangid,                    /* Language id */
 135589   int iIndex,                     /* Index for p->aIndex */
 135590   int iLevel, 
 135591   int *piIdx
 135593   int rc;                         /* Return Code */
 135594   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
 135595   int iNext = 0;                  /* Result of query pNextIdx */
 135597   assert( iLangid>=0 );
 135598   assert( p->nIndex>=1 );
 135600   /* Set variable iNext to the next available segdir index at level iLevel. */
 135601   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
 135602   if( rc==SQLITE_OK ){
 135603     sqlite3_bind_int64(
 135604         pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
 135606     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
 135607       iNext = sqlite3_column_int(pNextIdx, 0);
 135609     rc = sqlite3_reset(pNextIdx);
 135612   if( rc==SQLITE_OK ){
 135613     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
 135614     ** full, merge all segments in level iLevel into a single iLevel+1
 135615     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
 135616     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
 135618     if( iNext>=FTS3_MERGE_COUNT ){
 135619       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
 135620       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
 135621       *piIdx = 0;
 135622     }else{
 135623       *piIdx = iNext;
 135627   return rc;
 135631 ** The %_segments table is declared as follows:
 135633 **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
 135635 ** This function reads data from a single row of the %_segments table. The
 135636 ** specific row is identified by the iBlockid parameter. If paBlob is not
 135637 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
 135638 ** with the contents of the blob stored in the "block" column of the 
 135639 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
 135640 ** to the size of the blob in bytes before returning.
 135642 ** If an error occurs, or the table does not contain the specified row,
 135643 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
 135644 ** paBlob is non-NULL, then it is the responsibility of the caller to
 135645 ** eventually free the returned buffer.
 135647 ** This function may leave an open sqlite3_blob* handle in the
 135648 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
 135649 ** to this function. The handle may be closed by calling the
 135650 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
 135651 ** performance improvement, but the blob handle should always be closed
 135652 ** before control is returned to the user (to prevent a lock being held
 135653 ** on the database file for longer than necessary). Thus, any virtual table
 135654 ** method (xFilter etc.) that may directly or indirectly call this function
 135655 ** must call sqlite3Fts3SegmentsClose() before returning.
 135657 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
 135658   Fts3Table *p,                   /* FTS3 table handle */
 135659   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
 135660   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
 135661   int *pnBlob,                    /* OUT: Size of blob data */
 135662   int *pnLoad                     /* OUT: Bytes actually loaded */
 135664   int rc;                         /* Return code */
 135666   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
 135667   assert( pnBlob );
 135669   if( p->pSegments ){
 135670     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
 135671   }else{
 135672     if( 0==p->zSegmentsTbl ){
 135673       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
 135674       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
 135676     rc = sqlite3_blob_open(
 135677        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
 135681   if( rc==SQLITE_OK ){
 135682     int nByte = sqlite3_blob_bytes(p->pSegments);
 135683     *pnBlob = nByte;
 135684     if( paBlob ){
 135685       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
 135686       if( !aByte ){
 135687         rc = SQLITE_NOMEM;
 135688       }else{
 135689         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
 135690           nByte = FTS3_NODE_CHUNKSIZE;
 135691           *pnLoad = nByte;
 135693         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
 135694         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
 135695         if( rc!=SQLITE_OK ){
 135696           sqlite3_free(aByte);
 135697           aByte = 0;
 135700       *paBlob = aByte;
 135704   return rc;
 135708 ** Close the blob handle at p->pSegments, if it is open. See comments above
 135709 ** the sqlite3Fts3ReadBlock() function for details.
 135711 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
 135712   sqlite3_blob_close(p->pSegments);
 135713   p->pSegments = 0;
 135716 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
 135717   int nRead;                      /* Number of bytes to read */
 135718   int rc;                         /* Return code */
 135720   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
 135721   rc = sqlite3_blob_read(
 135722       pReader->pBlob, 
 135723       &pReader->aNode[pReader->nPopulate],
 135724       nRead,
 135725       pReader->nPopulate
 135728   if( rc==SQLITE_OK ){
 135729     pReader->nPopulate += nRead;
 135730     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
 135731     if( pReader->nPopulate==pReader->nNode ){
 135732       sqlite3_blob_close(pReader->pBlob);
 135733       pReader->pBlob = 0;
 135734       pReader->nPopulate = 0;
 135737   return rc;
 135740 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
 135741   int rc = SQLITE_OK;
 135742   assert( !pReader->pBlob 
 135743        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
 135745   while( pReader->pBlob && rc==SQLITE_OK 
 135746      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
 135748     rc = fts3SegReaderIncrRead(pReader);
 135750   return rc;
 135754 ** Set an Fts3SegReader cursor to point at EOF.
 135756 static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
 135757   if( !fts3SegReaderIsRootOnly(pSeg) ){
 135758     sqlite3_free(pSeg->aNode);
 135759     sqlite3_blob_close(pSeg->pBlob);
 135760     pSeg->pBlob = 0;
 135762   pSeg->aNode = 0;
 135766 ** Move the iterator passed as the first argument to the next term in the
 135767 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
 135768 ** SQLITE_DONE. Otherwise, an SQLite error code.
 135770 static int fts3SegReaderNext(
 135771   Fts3Table *p, 
 135772   Fts3SegReader *pReader,
 135773   int bIncr
 135775   int rc;                         /* Return code of various sub-routines */
 135776   char *pNext;                    /* Cursor variable */
 135777   int nPrefix;                    /* Number of bytes in term prefix */
 135778   int nSuffix;                    /* Number of bytes in term suffix */
 135780   if( !pReader->aDoclist ){
 135781     pNext = pReader->aNode;
 135782   }else{
 135783     pNext = &pReader->aDoclist[pReader->nDoclist];
 135786   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
 135788     if( fts3SegReaderIsPending(pReader) ){
 135789       Fts3HashElem *pElem = *(pReader->ppNextElem);
 135790       if( pElem==0 ){
 135791         pReader->aNode = 0;
 135792       }else{
 135793         PendingList *pList = (PendingList *)fts3HashData(pElem);
 135794         pReader->zTerm = (char *)fts3HashKey(pElem);
 135795         pReader->nTerm = fts3HashKeysize(pElem);
 135796         pReader->nNode = pReader->nDoclist = pList->nData + 1;
 135797         pReader->aNode = pReader->aDoclist = pList->aData;
 135798         pReader->ppNextElem++;
 135799         assert( pReader->aNode );
 135801       return SQLITE_OK;
 135804     fts3SegReaderSetEof(pReader);
 135806     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
 135807     ** blocks have already been traversed.  */
 135808     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
 135809     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
 135810       return SQLITE_OK;
 135813     rc = sqlite3Fts3ReadBlock(
 135814         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
 135815         (bIncr ? &pReader->nPopulate : 0)
 135817     if( rc!=SQLITE_OK ) return rc;
 135818     assert( pReader->pBlob==0 );
 135819     if( bIncr && pReader->nPopulate<pReader->nNode ){
 135820       pReader->pBlob = p->pSegments;
 135821       p->pSegments = 0;
 135823     pNext = pReader->aNode;
 135826   assert( !fts3SegReaderIsPending(pReader) );
 135828   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
 135829   if( rc!=SQLITE_OK ) return rc;
 135831   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
 135832   ** safe (no risk of overread) even if the node data is corrupted. */
 135833   pNext += fts3GetVarint32(pNext, &nPrefix);
 135834   pNext += fts3GetVarint32(pNext, &nSuffix);
 135835   if( nPrefix<0 || nSuffix<=0 
 135836    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
 135838     return FTS_CORRUPT_VTAB;
 135841   if( nPrefix+nSuffix>pReader->nTermAlloc ){
 135842     int nNew = (nPrefix+nSuffix)*2;
 135843     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
 135844     if( !zNew ){
 135845       return SQLITE_NOMEM;
 135847     pReader->zTerm = zNew;
 135848     pReader->nTermAlloc = nNew;
 135851   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
 135852   if( rc!=SQLITE_OK ) return rc;
 135854   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
 135855   pReader->nTerm = nPrefix+nSuffix;
 135856   pNext += nSuffix;
 135857   pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
 135858   pReader->aDoclist = pNext;
 135859   pReader->pOffsetList = 0;
 135861   /* Check that the doclist does not appear to extend past the end of the
 135862   ** b-tree node. And that the final byte of the doclist is 0x00. If either 
 135863   ** of these statements is untrue, then the data structure is corrupt.
 135865   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
 135866    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
 135868     return FTS_CORRUPT_VTAB;
 135870   return SQLITE_OK;
 135874 ** Set the SegReader to point to the first docid in the doclist associated
 135875 ** with the current term.
 135877 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
 135878   int rc = SQLITE_OK;
 135879   assert( pReader->aDoclist );
 135880   assert( !pReader->pOffsetList );
 135881   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
 135882     u8 bEof = 0;
 135883     pReader->iDocid = 0;
 135884     pReader->nOffsetList = 0;
 135885     sqlite3Fts3DoclistPrev(0,
 135886         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, 
 135887         &pReader->iDocid, &pReader->nOffsetList, &bEof
 135889   }else{
 135890     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
 135891     if( rc==SQLITE_OK ){
 135892       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
 135893       pReader->pOffsetList = &pReader->aDoclist[n];
 135896   return rc;
 135900 ** Advance the SegReader to point to the next docid in the doclist
 135901 ** associated with the current term.
 135903 ** If arguments ppOffsetList and pnOffsetList are not NULL, then 
 135904 ** *ppOffsetList is set to point to the first column-offset list
 135905 ** in the doclist entry (i.e. immediately past the docid varint).
 135906 ** *pnOffsetList is set to the length of the set of column-offset
 135907 ** lists, not including the nul-terminator byte. For example:
 135909 static int fts3SegReaderNextDocid(
 135910   Fts3Table *pTab,
 135911   Fts3SegReader *pReader,         /* Reader to advance to next docid */
 135912   char **ppOffsetList,            /* OUT: Pointer to current position-list */
 135913   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
 135915   int rc = SQLITE_OK;
 135916   char *p = pReader->pOffsetList;
 135917   char c = 0;
 135919   assert( p );
 135921   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
 135922     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
 135923     ** Pending-terms doclists are always built up in ascending order, so
 135924     ** we have to iterate through them backwards here. */
 135925     u8 bEof = 0;
 135926     if( ppOffsetList ){
 135927       *ppOffsetList = pReader->pOffsetList;
 135928       *pnOffsetList = pReader->nOffsetList - 1;
 135930     sqlite3Fts3DoclistPrev(0,
 135931         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
 135932         &pReader->nOffsetList, &bEof
 135934     if( bEof ){
 135935       pReader->pOffsetList = 0;
 135936     }else{
 135937       pReader->pOffsetList = p;
 135939   }else{
 135940     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
 135942     /* Pointer p currently points at the first byte of an offset list. The
 135943     ** following block advances it to point one byte past the end of
 135944     ** the same offset list. */
 135945     while( 1 ){
 135947       /* The following line of code (and the "p++" below the while() loop) is
 135948       ** normally all that is required to move pointer p to the desired 
 135949       ** position. The exception is if this node is being loaded from disk
 135950       ** incrementally and pointer "p" now points to the first byte past
 135951       ** the populated part of pReader->aNode[].
 135953       while( *p | c ) c = *p++ & 0x80;
 135954       assert( *p==0 );
 135956       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
 135957       rc = fts3SegReaderIncrRead(pReader);
 135958       if( rc!=SQLITE_OK ) return rc;
 135960     p++;
 135962     /* If required, populate the output variables with a pointer to and the
 135963     ** size of the previous offset-list.
 135965     if( ppOffsetList ){
 135966       *ppOffsetList = pReader->pOffsetList;
 135967       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
 135970     /* List may have been edited in place by fts3EvalNearTrim() */
 135971     while( p<pEnd && *p==0 ) p++;
 135973     /* If there are no more entries in the doclist, set pOffsetList to
 135974     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
 135975     ** Fts3SegReader.pOffsetList to point to the next offset list before
 135976     ** returning.
 135978     if( p>=pEnd ){
 135979       pReader->pOffsetList = 0;
 135980     }else{
 135981       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
 135982       if( rc==SQLITE_OK ){
 135983         sqlite3_int64 iDelta;
 135984         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
 135985         if( pTab->bDescIdx ){
 135986           pReader->iDocid -= iDelta;
 135987         }else{
 135988           pReader->iDocid += iDelta;
 135994   return SQLITE_OK;
 135998 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
 135999   Fts3Cursor *pCsr, 
 136000   Fts3MultiSegReader *pMsr,
 136001   int *pnOvfl
 136003   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
 136004   int nOvfl = 0;
 136005   int ii;
 136006   int rc = SQLITE_OK;
 136007   int pgsz = p->nPgsz;
 136009   assert( p->bFts4 );
 136010   assert( pgsz>0 );
 136012   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
 136013     Fts3SegReader *pReader = pMsr->apSegment[ii];
 136014     if( !fts3SegReaderIsPending(pReader) 
 136015      && !fts3SegReaderIsRootOnly(pReader) 
 136017       sqlite3_int64 jj;
 136018       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
 136019         int nBlob;
 136020         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
 136021         if( rc!=SQLITE_OK ) break;
 136022         if( (nBlob+35)>pgsz ){
 136023           nOvfl += (nBlob + 34)/pgsz;
 136028   *pnOvfl = nOvfl;
 136029   return rc;
 136033 ** Free all allocations associated with the iterator passed as the 
 136034 ** second argument.
 136036 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
 136037   if( pReader && !fts3SegReaderIsPending(pReader) ){
 136038     sqlite3_free(pReader->zTerm);
 136039     if( !fts3SegReaderIsRootOnly(pReader) ){
 136040       sqlite3_free(pReader->aNode);
 136041       sqlite3_blob_close(pReader->pBlob);
 136044   sqlite3_free(pReader);
 136048 ** Allocate a new SegReader object.
 136050 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
 136051   int iAge,                       /* Segment "age". */
 136052   int bLookup,                    /* True for a lookup only */
 136053   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
 136054   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
 136055   sqlite3_int64 iEndBlock,        /* Final block of segment */
 136056   const char *zRoot,              /* Buffer containing root node */
 136057   int nRoot,                      /* Size of buffer containing root node */
 136058   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
 136060   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
 136061   int nExtra = 0;                 /* Bytes to allocate segment root node */
 136063   assert( iStartLeaf<=iEndLeaf );
 136064   if( iStartLeaf==0 ){
 136065     nExtra = nRoot + FTS3_NODE_PADDING;
 136068   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
 136069   if( !pReader ){
 136070     return SQLITE_NOMEM;
 136072   memset(pReader, 0, sizeof(Fts3SegReader));
 136073   pReader->iIdx = iAge;
 136074   pReader->bLookup = bLookup!=0;
 136075   pReader->iStartBlock = iStartLeaf;
 136076   pReader->iLeafEndBlock = iEndLeaf;
 136077   pReader->iEndBlock = iEndBlock;
 136079   if( nExtra ){
 136080     /* The entire segment is stored in the root node. */
 136081     pReader->aNode = (char *)&pReader[1];
 136082     pReader->rootOnly = 1;
 136083     pReader->nNode = nRoot;
 136084     memcpy(pReader->aNode, zRoot, nRoot);
 136085     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
 136086   }else{
 136087     pReader->iCurrentBlock = iStartLeaf-1;
 136089   *ppReader = pReader;
 136090   return SQLITE_OK;
 136094 ** This is a comparison function used as a qsort() callback when sorting
 136095 ** an array of pending terms by term. This occurs as part of flushing
 136096 ** the contents of the pending-terms hash table to the database.
 136098 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
 136099   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
 136100   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
 136101   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
 136102   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
 136104   int n = (n1<n2 ? n1 : n2);
 136105   int c = memcmp(z1, z2, n);
 136106   if( c==0 ){
 136107     c = n1 - n2;
 136109   return c;
 136113 ** This function is used to allocate an Fts3SegReader that iterates through
 136114 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
 136116 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
 136117 ** through each term in the pending-terms table. Or, if isPrefixIter is
 136118 ** non-zero, it iterates through each term and its prefixes. For example, if
 136119 ** the pending terms hash table contains the terms "sqlite", "mysql" and
 136120 ** "firebird", then the iterator visits the following 'terms' (in the order
 136121 ** shown):
 136123 **   f fi fir fire fireb firebi firebir firebird
 136124 **   m my mys mysq mysql
 136125 **   s sq sql sqli sqlit sqlite
 136127 ** Whereas if isPrefixIter is zero, the terms visited are:
 136129 **   firebird mysql sqlite
 136131 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
 136132   Fts3Table *p,                   /* Virtual table handle */
 136133   int iIndex,                     /* Index for p->aIndex */
 136134   const char *zTerm,              /* Term to search for */
 136135   int nTerm,                      /* Size of buffer zTerm */
 136136   int bPrefix,                    /* True for a prefix iterator */
 136137   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
 136139   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
 136140   Fts3HashElem *pE;               /* Iterator variable */
 136141   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
 136142   int nElem = 0;                  /* Size of array at aElem */
 136143   int rc = SQLITE_OK;             /* Return Code */
 136144   Fts3Hash *pHash;
 136146   pHash = &p->aIndex[iIndex].hPending;
 136147   if( bPrefix ){
 136148     int nAlloc = 0;               /* Size of allocated array at aElem */
 136150     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
 136151       char *zKey = (char *)fts3HashKey(pE);
 136152       int nKey = fts3HashKeysize(pE);
 136153       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
 136154         if( nElem==nAlloc ){
 136155           Fts3HashElem **aElem2;
 136156           nAlloc += 16;
 136157           aElem2 = (Fts3HashElem **)sqlite3_realloc(
 136158               aElem, nAlloc*sizeof(Fts3HashElem *)
 136160           if( !aElem2 ){
 136161             rc = SQLITE_NOMEM;
 136162             nElem = 0;
 136163             break;
 136165           aElem = aElem2;
 136168         aElem[nElem++] = pE;
 136172     /* If more than one term matches the prefix, sort the Fts3HashElem
 136173     ** objects in term order using qsort(). This uses the same comparison
 136174     ** callback as is used when flushing terms to disk.
 136176     if( nElem>1 ){
 136177       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
 136180   }else{
 136181     /* The query is a simple term lookup that matches at most one term in
 136182     ** the index. All that is required is a straight hash-lookup. 
 136184     ** Because the stack address of pE may be accessed via the aElem pointer
 136185     ** below, the "Fts3HashElem *pE" must be declared so that it is valid
 136186     ** within this entire function, not just this "else{...}" block.
 136188     pE = fts3HashFindElem(pHash, zTerm, nTerm);
 136189     if( pE ){
 136190       aElem = &pE;
 136191       nElem = 1;
 136195   if( nElem>0 ){
 136196     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
 136197     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
 136198     if( !pReader ){
 136199       rc = SQLITE_NOMEM;
 136200     }else{
 136201       memset(pReader, 0, nByte);
 136202       pReader->iIdx = 0x7FFFFFFF;
 136203       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
 136204       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
 136208   if( bPrefix ){
 136209     sqlite3_free(aElem);
 136211   *ppReader = pReader;
 136212   return rc;
 136216 ** Compare the entries pointed to by two Fts3SegReader structures. 
 136217 ** Comparison is as follows:
 136219 **   1) EOF is greater than not EOF.
 136221 **   2) The current terms (if any) are compared using memcmp(). If one
 136222 **      term is a prefix of another, the longer term is considered the
 136223 **      larger.
 136225 **   3) By segment age. An older segment is considered larger.
 136227 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 136228   int rc;
 136229   if( pLhs->aNode && pRhs->aNode ){
 136230     int rc2 = pLhs->nTerm - pRhs->nTerm;
 136231     if( rc2<0 ){
 136232       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
 136233     }else{
 136234       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
 136236     if( rc==0 ){
 136237       rc = rc2;
 136239   }else{
 136240     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
 136242   if( rc==0 ){
 136243     rc = pRhs->iIdx - pLhs->iIdx;
 136245   assert( rc!=0 );
 136246   return rc;
 136250 ** A different comparison function for SegReader structures. In this
 136251 ** version, it is assumed that each SegReader points to an entry in
 136252 ** a doclist for identical terms. Comparison is made as follows:
 136254 **   1) EOF (end of doclist in this case) is greater than not EOF.
 136256 **   2) By current docid.
 136258 **   3) By segment age. An older segment is considered larger.
 136260 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 136261   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
 136262   if( rc==0 ){
 136263     if( pLhs->iDocid==pRhs->iDocid ){
 136264       rc = pRhs->iIdx - pLhs->iIdx;
 136265     }else{
 136266       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
 136269   assert( pLhs->aNode && pRhs->aNode );
 136270   return rc;
 136272 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
 136273   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
 136274   if( rc==0 ){
 136275     if( pLhs->iDocid==pRhs->iDocid ){
 136276       rc = pRhs->iIdx - pLhs->iIdx;
 136277     }else{
 136278       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
 136281   assert( pLhs->aNode && pRhs->aNode );
 136282   return rc;
 136286 ** Compare the term that the Fts3SegReader object passed as the first argument
 136287 ** points to with the term specified by arguments zTerm and nTerm. 
 136289 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
 136290 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
 136291 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
 136293 static int fts3SegReaderTermCmp(
 136294   Fts3SegReader *pSeg,            /* Segment reader object */
 136295   const char *zTerm,              /* Term to compare to */
 136296   int nTerm                       /* Size of term zTerm in bytes */
 136298   int res = 0;
 136299   if( pSeg->aNode ){
 136300     if( pSeg->nTerm>nTerm ){
 136301       res = memcmp(pSeg->zTerm, zTerm, nTerm);
 136302     }else{
 136303       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
 136305     if( res==0 ){
 136306       res = pSeg->nTerm-nTerm;
 136309   return res;
 136313 ** Argument apSegment is an array of nSegment elements. It is known that
 136314 ** the final (nSegment-nSuspect) members are already in sorted order
 136315 ** (according to the comparison function provided). This function shuffles
 136316 ** the array around until all entries are in sorted order.
 136318 static void fts3SegReaderSort(
 136319   Fts3SegReader **apSegment,                     /* Array to sort entries of */
 136320   int nSegment,                                  /* Size of apSegment array */
 136321   int nSuspect,                                  /* Unsorted entry count */
 136322   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
 136324   int i;                          /* Iterator variable */
 136326   assert( nSuspect<=nSegment );
 136328   if( nSuspect==nSegment ) nSuspect--;
 136329   for(i=nSuspect-1; i>=0; i--){
 136330     int j;
 136331     for(j=i; j<(nSegment-1); j++){
 136332       Fts3SegReader *pTmp;
 136333       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
 136334       pTmp = apSegment[j+1];
 136335       apSegment[j+1] = apSegment[j];
 136336       apSegment[j] = pTmp;
 136340 #ifndef NDEBUG
 136341   /* Check that the list really is sorted now. */
 136342   for(i=0; i<(nSuspect-1); i++){
 136343     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
 136345 #endif
 136349 ** Insert a record into the %_segments table.
 136351 static int fts3WriteSegment(
 136352   Fts3Table *p,                   /* Virtual table handle */
 136353   sqlite3_int64 iBlock,           /* Block id for new block */
 136354   char *z,                        /* Pointer to buffer containing block data */
 136355   int n                           /* Size of buffer z in bytes */
 136357   sqlite3_stmt *pStmt;
 136358   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
 136359   if( rc==SQLITE_OK ){
 136360     sqlite3_bind_int64(pStmt, 1, iBlock);
 136361     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
 136362     sqlite3_step(pStmt);
 136363     rc = sqlite3_reset(pStmt);
 136365   return rc;
 136369 ** Find the largest relative level number in the table. If successful, set
 136370 ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
 136371 ** set *pnMax to zero and return an SQLite error code.
 136373 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
 136374   int rc;
 136375   int mxLevel = 0;
 136376   sqlite3_stmt *pStmt = 0;
 136378   rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
 136379   if( rc==SQLITE_OK ){
 136380     if( SQLITE_ROW==sqlite3_step(pStmt) ){
 136381       mxLevel = sqlite3_column_int(pStmt, 0);
 136383     rc = sqlite3_reset(pStmt);
 136385   *pnMax = mxLevel;
 136386   return rc;
 136390 ** Insert a record into the %_segdir table.
 136392 static int fts3WriteSegdir(
 136393   Fts3Table *p,                   /* Virtual table handle */
 136394   sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
 136395   int iIdx,                       /* Value for "idx" field */
 136396   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
 136397   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
 136398   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
 136399   char *zRoot,                    /* Blob value for "root" field */
 136400   int nRoot                       /* Number of bytes in buffer zRoot */
 136402   sqlite3_stmt *pStmt;
 136403   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
 136404   if( rc==SQLITE_OK ){
 136405     sqlite3_bind_int64(pStmt, 1, iLevel);
 136406     sqlite3_bind_int(pStmt, 2, iIdx);
 136407     sqlite3_bind_int64(pStmt, 3, iStartBlock);
 136408     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
 136409     sqlite3_bind_int64(pStmt, 5, iEndBlock);
 136410     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
 136411     sqlite3_step(pStmt);
 136412     rc = sqlite3_reset(pStmt);
 136414   return rc;
 136418 ** Return the size of the common prefix (if any) shared by zPrev and
 136419 ** zNext, in bytes. For example, 
 136421 **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
 136422 **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
 136423 **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
 136425 static int fts3PrefixCompress(
 136426   const char *zPrev,              /* Buffer containing previous term */
 136427   int nPrev,                      /* Size of buffer zPrev in bytes */
 136428   const char *zNext,              /* Buffer containing next term */
 136429   int nNext                       /* Size of buffer zNext in bytes */
 136431   int n;
 136432   UNUSED_PARAMETER(nNext);
 136433   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
 136434   return n;
 136438 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
 136439 ** (according to memcmp) than the previous term.
 136441 static int fts3NodeAddTerm(
 136442   Fts3Table *p,                   /* Virtual table handle */
 136443   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
 136444   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
 136445   const char *zTerm,              /* Pointer to buffer containing term */
 136446   int nTerm                       /* Size of term in bytes */
 136448   SegmentNode *pTree = *ppTree;
 136449   int rc;
 136450   SegmentNode *pNew;
 136452   /* First try to append the term to the current node. Return early if 
 136453   ** this is possible.
 136455   if( pTree ){
 136456     int nData = pTree->nData;     /* Current size of node in bytes */
 136457     int nReq = nData;             /* Required space after adding zTerm */
 136458     int nPrefix;                  /* Number of bytes of prefix compression */
 136459     int nSuffix;                  /* Suffix length */
 136461     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
 136462     nSuffix = nTerm-nPrefix;
 136464     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
 136465     if( nReq<=p->nNodeSize || !pTree->zTerm ){
 136467       if( nReq>p->nNodeSize ){
 136468         /* An unusual case: this is the first term to be added to the node
 136469         ** and the static node buffer (p->nNodeSize bytes) is not large
 136470         ** enough. Use a separately malloced buffer instead This wastes
 136471         ** p->nNodeSize bytes, but since this scenario only comes about when
 136472         ** the database contain two terms that share a prefix of almost 2KB, 
 136473         ** this is not expected to be a serious problem. 
 136475         assert( pTree->aData==(char *)&pTree[1] );
 136476         pTree->aData = (char *)sqlite3_malloc(nReq);
 136477         if( !pTree->aData ){
 136478           return SQLITE_NOMEM;
 136482       if( pTree->zTerm ){
 136483         /* There is no prefix-length field for first term in a node */
 136484         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
 136487       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
 136488       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
 136489       pTree->nData = nData + nSuffix;
 136490       pTree->nEntry++;
 136492       if( isCopyTerm ){
 136493         if( pTree->nMalloc<nTerm ){
 136494           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
 136495           if( !zNew ){
 136496             return SQLITE_NOMEM;
 136498           pTree->nMalloc = nTerm*2;
 136499           pTree->zMalloc = zNew;
 136501         pTree->zTerm = pTree->zMalloc;
 136502         memcpy(pTree->zTerm, zTerm, nTerm);
 136503         pTree->nTerm = nTerm;
 136504       }else{
 136505         pTree->zTerm = (char *)zTerm;
 136506         pTree->nTerm = nTerm;
 136508       return SQLITE_OK;
 136512   /* If control flows to here, it was not possible to append zTerm to the
 136513   ** current node. Create a new node (a right-sibling of the current node).
 136514   ** If this is the first node in the tree, the term is added to it.
 136516   ** Otherwise, the term is not added to the new node, it is left empty for
 136517   ** now. Instead, the term is inserted into the parent of pTree. If pTree 
 136518   ** has no parent, one is created here.
 136520   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
 136521   if( !pNew ){
 136522     return SQLITE_NOMEM;
 136524   memset(pNew, 0, sizeof(SegmentNode));
 136525   pNew->nData = 1 + FTS3_VARINT_MAX;
 136526   pNew->aData = (char *)&pNew[1];
 136528   if( pTree ){
 136529     SegmentNode *pParent = pTree->pParent;
 136530     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
 136531     if( pTree->pParent==0 ){
 136532       pTree->pParent = pParent;
 136534     pTree->pRight = pNew;
 136535     pNew->pLeftmost = pTree->pLeftmost;
 136536     pNew->pParent = pParent;
 136537     pNew->zMalloc = pTree->zMalloc;
 136538     pNew->nMalloc = pTree->nMalloc;
 136539     pTree->zMalloc = 0;
 136540   }else{
 136541     pNew->pLeftmost = pNew;
 136542     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
 136545   *ppTree = pNew;
 136546   return rc;
 136550 ** Helper function for fts3NodeWrite().
 136552 static int fts3TreeFinishNode(
 136553   SegmentNode *pTree, 
 136554   int iHeight, 
 136555   sqlite3_int64 iLeftChild
 136557   int nStart;
 136558   assert( iHeight>=1 && iHeight<128 );
 136559   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
 136560   pTree->aData[nStart] = (char)iHeight;
 136561   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
 136562   return nStart;
 136566 ** Write the buffer for the segment node pTree and all of its peers to the
 136567 ** database. Then call this function recursively to write the parent of 
 136568 ** pTree and its peers to the database. 
 136570 ** Except, if pTree is a root node, do not write it to the database. Instead,
 136571 ** set output variables *paRoot and *pnRoot to contain the root node.
 136573 ** If successful, SQLITE_OK is returned and output variable *piLast is
 136574 ** set to the largest blockid written to the database (or zero if no
 136575 ** blocks were written to the db). Otherwise, an SQLite error code is 
 136576 ** returned.
 136578 static int fts3NodeWrite(
 136579   Fts3Table *p,                   /* Virtual table handle */
 136580   SegmentNode *pTree,             /* SegmentNode handle */
 136581   int iHeight,                    /* Height of this node in tree */
 136582   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
 136583   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
 136584   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
 136585   char **paRoot,                  /* OUT: Data for root node */
 136586   int *pnRoot                     /* OUT: Size of root node in bytes */
 136588   int rc = SQLITE_OK;
 136590   if( !pTree->pParent ){
 136591     /* Root node of the tree. */
 136592     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
 136593     *piLast = iFree-1;
 136594     *pnRoot = pTree->nData - nStart;
 136595     *paRoot = &pTree->aData[nStart];
 136596   }else{
 136597     SegmentNode *pIter;
 136598     sqlite3_int64 iNextFree = iFree;
 136599     sqlite3_int64 iNextLeaf = iLeaf;
 136600     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
 136601       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
 136602       int nWrite = pIter->nData - nStart;
 136604       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
 136605       iNextFree++;
 136606       iNextLeaf += (pIter->nEntry+1);
 136608     if( rc==SQLITE_OK ){
 136609       assert( iNextLeaf==iFree );
 136610       rc = fts3NodeWrite(
 136611           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
 136616   return rc;
 136620 ** Free all memory allocations associated with the tree pTree.
 136622 static void fts3NodeFree(SegmentNode *pTree){
 136623   if( pTree ){
 136624     SegmentNode *p = pTree->pLeftmost;
 136625     fts3NodeFree(p->pParent);
 136626     while( p ){
 136627       SegmentNode *pRight = p->pRight;
 136628       if( p->aData!=(char *)&p[1] ){
 136629         sqlite3_free(p->aData);
 136631       assert( pRight==0 || p->zMalloc==0 );
 136632       sqlite3_free(p->zMalloc);
 136633       sqlite3_free(p);
 136634       p = pRight;
 136640 ** Add a term to the segment being constructed by the SegmentWriter object
 136641 ** *ppWriter. When adding the first term to a segment, *ppWriter should
 136642 ** be passed NULL. This function will allocate a new SegmentWriter object
 136643 ** and return it via the input/output variable *ppWriter in this case.
 136645 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
 136647 static int fts3SegWriterAdd(
 136648   Fts3Table *p,                   /* Virtual table handle */
 136649   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
 136650   int isCopyTerm,                 /* True if buffer zTerm must be copied */
 136651   const char *zTerm,              /* Pointer to buffer containing term */
 136652   int nTerm,                      /* Size of term in bytes */
 136653   const char *aDoclist,           /* Pointer to buffer containing doclist */
 136654   int nDoclist                    /* Size of doclist in bytes */
 136656   int nPrefix;                    /* Size of term prefix in bytes */
 136657   int nSuffix;                    /* Size of term suffix in bytes */
 136658   int nReq;                       /* Number of bytes required on leaf page */
 136659   int nData;
 136660   SegmentWriter *pWriter = *ppWriter;
 136662   if( !pWriter ){
 136663     int rc;
 136664     sqlite3_stmt *pStmt;
 136666     /* Allocate the SegmentWriter structure */
 136667     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
 136668     if( !pWriter ) return SQLITE_NOMEM;
 136669     memset(pWriter, 0, sizeof(SegmentWriter));
 136670     *ppWriter = pWriter;
 136672     /* Allocate a buffer in which to accumulate data */
 136673     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
 136674     if( !pWriter->aData ) return SQLITE_NOMEM;
 136675     pWriter->nSize = p->nNodeSize;
 136677     /* Find the next free blockid in the %_segments table */
 136678     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
 136679     if( rc!=SQLITE_OK ) return rc;
 136680     if( SQLITE_ROW==sqlite3_step(pStmt) ){
 136681       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
 136682       pWriter->iFirst = pWriter->iFree;
 136684     rc = sqlite3_reset(pStmt);
 136685     if( rc!=SQLITE_OK ) return rc;
 136687   nData = pWriter->nData;
 136689   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
 136690   nSuffix = nTerm-nPrefix;
 136692   /* Figure out how many bytes are required by this new entry */
 136693   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
 136694     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
 136695     nSuffix +                               /* Term suffix */
 136696     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
 136697     nDoclist;                               /* Doclist data */
 136699   if( nData>0 && nData+nReq>p->nNodeSize ){
 136700     int rc;
 136702     /* The current leaf node is full. Write it out to the database. */
 136703     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
 136704     if( rc!=SQLITE_OK ) return rc;
 136705     p->nLeafAdd++;
 136707     /* Add the current term to the interior node tree. The term added to
 136708     ** the interior tree must:
 136710     **   a) be greater than the largest term on the leaf node just written
 136711     **      to the database (still available in pWriter->zTerm), and
 136713     **   b) be less than or equal to the term about to be added to the new
 136714     **      leaf node (zTerm/nTerm).
 136716     ** In other words, it must be the prefix of zTerm 1 byte longer than
 136717     ** the common prefix (if any) of zTerm and pWriter->zTerm.
 136719     assert( nPrefix<nTerm );
 136720     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
 136721     if( rc!=SQLITE_OK ) return rc;
 136723     nData = 0;
 136724     pWriter->nTerm = 0;
 136726     nPrefix = 0;
 136727     nSuffix = nTerm;
 136728     nReq = 1 +                              /* varint containing prefix size */
 136729       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
 136730       nTerm +                               /* Term suffix */
 136731       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
 136732       nDoclist;                             /* Doclist data */
 136735   /* If the buffer currently allocated is too small for this entry, realloc
 136736   ** the buffer to make it large enough.
 136738   if( nReq>pWriter->nSize ){
 136739     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
 136740     if( !aNew ) return SQLITE_NOMEM;
 136741     pWriter->aData = aNew;
 136742     pWriter->nSize = nReq;
 136744   assert( nData+nReq<=pWriter->nSize );
 136746   /* Append the prefix-compressed term and doclist to the buffer. */
 136747   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
 136748   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
 136749   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
 136750   nData += nSuffix;
 136751   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
 136752   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
 136753   pWriter->nData = nData + nDoclist;
 136755   /* Save the current term so that it can be used to prefix-compress the next.
 136756   ** If the isCopyTerm parameter is true, then the buffer pointed to by
 136757   ** zTerm is transient, so take a copy of the term data. Otherwise, just
 136758   ** store a copy of the pointer.
 136760   if( isCopyTerm ){
 136761     if( nTerm>pWriter->nMalloc ){
 136762       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
 136763       if( !zNew ){
 136764         return SQLITE_NOMEM;
 136766       pWriter->nMalloc = nTerm*2;
 136767       pWriter->zMalloc = zNew;
 136768       pWriter->zTerm = zNew;
 136770     assert( pWriter->zTerm==pWriter->zMalloc );
 136771     memcpy(pWriter->zTerm, zTerm, nTerm);
 136772   }else{
 136773     pWriter->zTerm = (char *)zTerm;
 136775   pWriter->nTerm = nTerm;
 136777   return SQLITE_OK;
 136781 ** Flush all data associated with the SegmentWriter object pWriter to the
 136782 ** database. This function must be called after all terms have been added
 136783 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
 136784 ** returned. Otherwise, an SQLite error code.
 136786 static int fts3SegWriterFlush(
 136787   Fts3Table *p,                   /* Virtual table handle */
 136788   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
 136789   sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
 136790   int iIdx                        /* Value for 'idx' column of %_segdir */
 136792   int rc;                         /* Return code */
 136793   if( pWriter->pTree ){
 136794     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
 136795     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
 136796     char *zRoot = NULL;           /* Pointer to buffer containing root node */
 136797     int nRoot = 0;                /* Size of buffer zRoot */
 136799     iLastLeaf = pWriter->iFree;
 136800     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
 136801     if( rc==SQLITE_OK ){
 136802       rc = fts3NodeWrite(p, pWriter->pTree, 1,
 136803           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
 136805     if( rc==SQLITE_OK ){
 136806       rc = fts3WriteSegdir(
 136807           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
 136809   }else{
 136810     /* The entire tree fits on the root node. Write it to the segdir table. */
 136811     rc = fts3WriteSegdir(
 136812         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
 136814   p->nLeafAdd++;
 136815   return rc;
 136819 ** Release all memory held by the SegmentWriter object passed as the 
 136820 ** first argument.
 136822 static void fts3SegWriterFree(SegmentWriter *pWriter){
 136823   if( pWriter ){
 136824     sqlite3_free(pWriter->aData);
 136825     sqlite3_free(pWriter->zMalloc);
 136826     fts3NodeFree(pWriter->pTree);
 136827     sqlite3_free(pWriter);
 136832 ** The first value in the apVal[] array is assumed to contain an integer.
 136833 ** This function tests if there exist any documents with docid values that
 136834 ** are different from that integer. i.e. if deleting the document with docid
 136835 ** pRowid would mean the FTS3 table were empty.
 136837 ** If successful, *pisEmpty is set to true if the table is empty except for
 136838 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
 136839 ** error occurs, an SQLite error code is returned.
 136841 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
 136842   sqlite3_stmt *pStmt;
 136843   int rc;
 136844   if( p->zContentTbl ){
 136845     /* If using the content=xxx option, assume the table is never empty */
 136846     *pisEmpty = 0;
 136847     rc = SQLITE_OK;
 136848   }else{
 136849     rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
 136850     if( rc==SQLITE_OK ){
 136851       if( SQLITE_ROW==sqlite3_step(pStmt) ){
 136852         *pisEmpty = sqlite3_column_int(pStmt, 0);
 136854       rc = sqlite3_reset(pStmt);
 136857   return rc;
 136861 ** Set *pnMax to the largest segment level in the database for the index
 136862 ** iIndex.
 136864 ** Segment levels are stored in the 'level' column of the %_segdir table.
 136866 ** Return SQLITE_OK if successful, or an SQLite error code if not.
 136868 static int fts3SegmentMaxLevel(
 136869   Fts3Table *p, 
 136870   int iLangid,
 136871   int iIndex, 
 136872   sqlite3_int64 *pnMax
 136874   sqlite3_stmt *pStmt;
 136875   int rc;
 136876   assert( iIndex>=0 && iIndex<p->nIndex );
 136878   /* Set pStmt to the compiled version of:
 136880   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
 136882   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
 136884   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
 136885   if( rc!=SQLITE_OK ) return rc;
 136886   sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 136887   sqlite3_bind_int64(pStmt, 2, 
 136888       getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 136890   if( SQLITE_ROW==sqlite3_step(pStmt) ){
 136891     *pnMax = sqlite3_column_int64(pStmt, 0);
 136893   return sqlite3_reset(pStmt);
 136897 ** Delete all entries in the %_segments table associated with the segment
 136898 ** opened with seg-reader pSeg. This function does not affect the contents
 136899 ** of the %_segdir table.
 136901 static int fts3DeleteSegment(
 136902   Fts3Table *p,                   /* FTS table handle */
 136903   Fts3SegReader *pSeg             /* Segment to delete */
 136905   int rc = SQLITE_OK;             /* Return code */
 136906   if( pSeg->iStartBlock ){
 136907     sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
 136908     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
 136909     if( rc==SQLITE_OK ){
 136910       sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
 136911       sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
 136912       sqlite3_step(pDelete);
 136913       rc = sqlite3_reset(pDelete);
 136916   return rc;
 136920 ** This function is used after merging multiple segments into a single large
 136921 ** segment to delete the old, now redundant, segment b-trees. Specifically,
 136922 ** it:
 136924 **   1) Deletes all %_segments entries for the segments associated with 
 136925 **      each of the SegReader objects in the array passed as the third 
 136926 **      argument, and
 136928 **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
 136929 **      entries regardless of level if (iLevel<0).
 136931 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
 136933 static int fts3DeleteSegdir(
 136934   Fts3Table *p,                   /* Virtual table handle */
 136935   int iLangid,                    /* Language id */
 136936   int iIndex,                     /* Index for p->aIndex */
 136937   int iLevel,                     /* Level of %_segdir entries to delete */
 136938   Fts3SegReader **apSegment,      /* Array of SegReader objects */
 136939   int nReader                     /* Size of array apSegment */
 136941   int rc = SQLITE_OK;             /* Return Code */
 136942   int i;                          /* Iterator variable */
 136943   sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
 136945   for(i=0; rc==SQLITE_OK && i<nReader; i++){
 136946     rc = fts3DeleteSegment(p, apSegment[i]);
 136948   if( rc!=SQLITE_OK ){
 136949     return rc;
 136952   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
 136953   if( iLevel==FTS3_SEGCURSOR_ALL ){
 136954     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
 136955     if( rc==SQLITE_OK ){
 136956       sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
 136957       sqlite3_bind_int64(pDelete, 2, 
 136958           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
 136961   }else{
 136962     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
 136963     if( rc==SQLITE_OK ){
 136964       sqlite3_bind_int64(
 136965           pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
 136970   if( rc==SQLITE_OK ){
 136971     sqlite3_step(pDelete);
 136972     rc = sqlite3_reset(pDelete);
 136975   return rc;
 136979 ** When this function is called, buffer *ppList (size *pnList bytes) contains 
 136980 ** a position list that may (or may not) feature multiple columns. This
 136981 ** function adjusts the pointer *ppList and the length *pnList so that they
 136982 ** identify the subset of the position list that corresponds to column iCol.
 136984 ** If there are no entries in the input position list for column iCol, then
 136985 ** *pnList is set to zero before returning.
 136987 ** If parameter bZero is non-zero, then any part of the input list following
 136988 ** the end of the output list is zeroed before returning.
 136990 static void fts3ColumnFilter(
 136991   int iCol,                       /* Column to filter on */
 136992   int bZero,                      /* Zero out anything following *ppList */
 136993   char **ppList,                  /* IN/OUT: Pointer to position list */
 136994   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
 136996   char *pList = *ppList;
 136997   int nList = *pnList;
 136998   char *pEnd = &pList[nList];
 136999   int iCurrent = 0;
 137000   char *p = pList;
 137002   assert( iCol>=0 );
 137003   while( 1 ){
 137004     char c = 0;
 137005     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
 137007     if( iCol==iCurrent ){
 137008       nList = (int)(p - pList);
 137009       break;
 137012     nList -= (int)(p - pList);
 137013     pList = p;
 137014     if( nList==0 ){
 137015       break;
 137017     p = &pList[1];
 137018     p += fts3GetVarint32(p, &iCurrent);
 137021   if( bZero && &pList[nList]!=pEnd ){
 137022     memset(&pList[nList], 0, pEnd - &pList[nList]);
 137024   *ppList = pList;
 137025   *pnList = nList;
 137029 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
 137030 ** existing data). Grow the buffer if required.
 137032 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
 137033 ** trying to resize the buffer, return SQLITE_NOMEM.
 137035 static int fts3MsrBufferData(
 137036   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
 137037   char *pList,
 137038   int nList
 137040   if( nList>pMsr->nBuffer ){
 137041     char *pNew;
 137042     pMsr->nBuffer = nList*2;
 137043     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
 137044     if( !pNew ) return SQLITE_NOMEM;
 137045     pMsr->aBuffer = pNew;
 137048   memcpy(pMsr->aBuffer, pList, nList);
 137049   return SQLITE_OK;
 137052 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
 137053   Fts3Table *p,                   /* Virtual table handle */
 137054   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
 137055   sqlite3_int64 *piDocid,         /* OUT: Docid value */
 137056   char **paPoslist,               /* OUT: Pointer to position list */
 137057   int *pnPoslist                  /* OUT: Size of position list in bytes */
 137059   int nMerge = pMsr->nAdvance;
 137060   Fts3SegReader **apSegment = pMsr->apSegment;
 137061   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 137062     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 137065   if( nMerge==0 ){
 137066     *paPoslist = 0;
 137067     return SQLITE_OK;
 137070   while( 1 ){
 137071     Fts3SegReader *pSeg;
 137072     pSeg = pMsr->apSegment[0];
 137074     if( pSeg->pOffsetList==0 ){
 137075       *paPoslist = 0;
 137076       break;
 137077     }else{
 137078       int rc;
 137079       char *pList;
 137080       int nList;
 137081       int j;
 137082       sqlite3_int64 iDocid = apSegment[0]->iDocid;
 137084       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
 137085       j = 1;
 137086       while( rc==SQLITE_OK 
 137087         && j<nMerge
 137088         && apSegment[j]->pOffsetList
 137089         && apSegment[j]->iDocid==iDocid
 137091         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
 137092         j++;
 137094       if( rc!=SQLITE_OK ) return rc;
 137095       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
 137097       if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
 137098         rc = fts3MsrBufferData(pMsr, pList, nList+1);
 137099         if( rc!=SQLITE_OK ) return rc;
 137100         assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
 137101         pList = pMsr->aBuffer;
 137104       if( pMsr->iColFilter>=0 ){
 137105         fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
 137108       if( nList>0 ){
 137109         *paPoslist = pList;
 137110         *piDocid = iDocid;
 137111         *pnPoslist = nList;
 137112         break;
 137117   return SQLITE_OK;
 137120 static int fts3SegReaderStart(
 137121   Fts3Table *p,                   /* Virtual table handle */
 137122   Fts3MultiSegReader *pCsr,       /* Cursor object */
 137123   const char *zTerm,              /* Term searched for (or NULL) */
 137124   int nTerm                       /* Length of zTerm in bytes */
 137126   int i;
 137127   int nSeg = pCsr->nSegment;
 137129   /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
 137130   ** for, then advance each segment iterator until it points to a term of
 137131   ** equal or greater value than the specified term. This prevents many
 137132   ** unnecessary merge/sort operations for the case where single segment
 137133   ** b-tree leaf nodes contain more than one term.
 137135   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
 137136     int res = 0;
 137137     Fts3SegReader *pSeg = pCsr->apSegment[i];
 137138     do {
 137139       int rc = fts3SegReaderNext(p, pSeg, 0);
 137140       if( rc!=SQLITE_OK ) return rc;
 137141     }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
 137143     if( pSeg->bLookup && res!=0 ){
 137144       fts3SegReaderSetEof(pSeg);
 137147   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
 137149   return SQLITE_OK;
 137152 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
 137153   Fts3Table *p,                   /* Virtual table handle */
 137154   Fts3MultiSegReader *pCsr,       /* Cursor object */
 137155   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
 137157   pCsr->pFilter = pFilter;
 137158   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
 137161 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
 137162   Fts3Table *p,                   /* Virtual table handle */
 137163   Fts3MultiSegReader *pCsr,       /* Cursor object */
 137164   int iCol,                       /* Column to match on. */
 137165   const char *zTerm,              /* Term to iterate through a doclist for */
 137166   int nTerm                       /* Number of bytes in zTerm */
 137168   int i;
 137169   int rc;
 137170   int nSegment = pCsr->nSegment;
 137171   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 137172     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 137175   assert( pCsr->pFilter==0 );
 137176   assert( zTerm && nTerm>0 );
 137178   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
 137179   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
 137180   if( rc!=SQLITE_OK ) return rc;
 137182   /* Determine how many of the segments actually point to zTerm/nTerm. */
 137183   for(i=0; i<nSegment; i++){
 137184     Fts3SegReader *pSeg = pCsr->apSegment[i];
 137185     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
 137186       break;
 137189   pCsr->nAdvance = i;
 137191   /* Advance each of the segments to point to the first docid. */
 137192   for(i=0; i<pCsr->nAdvance; i++){
 137193     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
 137194     if( rc!=SQLITE_OK ) return rc;
 137196   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
 137198   assert( iCol<0 || iCol<p->nColumn );
 137199   pCsr->iColFilter = iCol;
 137201   return SQLITE_OK;
 137205 ** This function is called on a MultiSegReader that has been started using
 137206 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
 137207 ** have been made. Calling this function puts the MultiSegReader in such
 137208 ** a state that if the next two calls are:
 137210 **   sqlite3Fts3SegReaderStart()
 137211 **   sqlite3Fts3SegReaderStep()
 137213 ** then the entire doclist for the term is available in 
 137214 ** MultiSegReader.aDoclist/nDoclist.
 137216 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
 137217   int i;                          /* Used to iterate through segment-readers */
 137219   assert( pCsr->zTerm==0 );
 137220   assert( pCsr->nTerm==0 );
 137221   assert( pCsr->aDoclist==0 );
 137222   assert( pCsr->nDoclist==0 );
 137224   pCsr->nAdvance = 0;
 137225   pCsr->bRestart = 1;
 137226   for(i=0; i<pCsr->nSegment; i++){
 137227     pCsr->apSegment[i]->pOffsetList = 0;
 137228     pCsr->apSegment[i]->nOffsetList = 0;
 137229     pCsr->apSegment[i]->iDocid = 0;
 137232   return SQLITE_OK;
 137236 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
 137237   Fts3Table *p,                   /* Virtual table handle */
 137238   Fts3MultiSegReader *pCsr        /* Cursor object */
 137240   int rc = SQLITE_OK;
 137242   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
 137243   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
 137244   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
 137245   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
 137246   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
 137247   int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
 137249   Fts3SegReader **apSegment = pCsr->apSegment;
 137250   int nSegment = pCsr->nSegment;
 137251   Fts3SegFilter *pFilter = pCsr->pFilter;
 137252   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
 137253     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
 137256   if( pCsr->nSegment==0 ) return SQLITE_OK;
 137258   do {
 137259     int nMerge;
 137260     int i;
 137262     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
 137263     ** forward. Then sort the list in order of current term again.  
 137265     for(i=0; i<pCsr->nAdvance; i++){
 137266       Fts3SegReader *pSeg = apSegment[i];
 137267       if( pSeg->bLookup ){
 137268         fts3SegReaderSetEof(pSeg);
 137269       }else{
 137270         rc = fts3SegReaderNext(p, pSeg, 0);
 137272       if( rc!=SQLITE_OK ) return rc;
 137274     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
 137275     pCsr->nAdvance = 0;
 137277     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
 137278     assert( rc==SQLITE_OK );
 137279     if( apSegment[0]->aNode==0 ) break;
 137281     pCsr->nTerm = apSegment[0]->nTerm;
 137282     pCsr->zTerm = apSegment[0]->zTerm;
 137284     /* If this is a prefix-search, and if the term that apSegment[0] points
 137285     ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
 137286     ** required callbacks have been made. In this case exit early.
 137288     ** Similarly, if this is a search for an exact match, and the first term
 137289     ** of segment apSegment[0] is not a match, exit early.
 137291     if( pFilter->zTerm && !isScan ){
 137292       if( pCsr->nTerm<pFilter->nTerm 
 137293        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
 137294        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
 137296         break;
 137300     nMerge = 1;
 137301     while( nMerge<nSegment 
 137302         && apSegment[nMerge]->aNode
 137303         && apSegment[nMerge]->nTerm==pCsr->nTerm 
 137304         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
 137306       nMerge++;
 137309     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
 137310     if( nMerge==1 
 137311      && !isIgnoreEmpty 
 137312      && !isFirst 
 137313      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
 137315       pCsr->nDoclist = apSegment[0]->nDoclist;
 137316       if( fts3SegReaderIsPending(apSegment[0]) ){
 137317         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
 137318         pCsr->aDoclist = pCsr->aBuffer;
 137319       }else{
 137320         pCsr->aDoclist = apSegment[0]->aDoclist;
 137322       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
 137323     }else{
 137324       int nDoclist = 0;           /* Size of doclist */
 137325       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
 137327       /* The current term of the first nMerge entries in the array
 137328       ** of Fts3SegReader objects is the same. The doclists must be merged
 137329       ** and a single term returned with the merged doclist.
 137331       for(i=0; i<nMerge; i++){
 137332         fts3SegReaderFirstDocid(p, apSegment[i]);
 137334       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
 137335       while( apSegment[0]->pOffsetList ){
 137336         int j;                    /* Number of segments that share a docid */
 137337         char *pList = 0;
 137338         int nList = 0;
 137339         int nByte;
 137340         sqlite3_int64 iDocid = apSegment[0]->iDocid;
 137341         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
 137342         j = 1;
 137343         while( j<nMerge
 137344             && apSegment[j]->pOffsetList
 137345             && apSegment[j]->iDocid==iDocid
 137347           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
 137348           j++;
 137351         if( isColFilter ){
 137352           fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
 137355         if( !isIgnoreEmpty || nList>0 ){
 137357           /* Calculate the 'docid' delta value to write into the merged 
 137358           ** doclist. */
 137359           sqlite3_int64 iDelta;
 137360           if( p->bDescIdx && nDoclist>0 ){
 137361             iDelta = iPrev - iDocid;
 137362           }else{
 137363             iDelta = iDocid - iPrev;
 137365           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
 137366           assert( nDoclist>0 || iDelta==iDocid );
 137368           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
 137369           if( nDoclist+nByte>pCsr->nBuffer ){
 137370             char *aNew;
 137371             pCsr->nBuffer = (nDoclist+nByte)*2;
 137372             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
 137373             if( !aNew ){
 137374               return SQLITE_NOMEM;
 137376             pCsr->aBuffer = aNew;
 137379           if( isFirst ){
 137380             char *a = &pCsr->aBuffer[nDoclist];
 137381             int nWrite;
 137383             nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
 137384             if( nWrite ){
 137385               iPrev = iDocid;
 137386               nDoclist += nWrite;
 137388           }else{
 137389             nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
 137390             iPrev = iDocid;
 137391             if( isRequirePos ){
 137392               memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
 137393               nDoclist += nList;
 137394               pCsr->aBuffer[nDoclist++] = '\0';
 137399         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
 137401       if( nDoclist>0 ){
 137402         pCsr->aDoclist = pCsr->aBuffer;
 137403         pCsr->nDoclist = nDoclist;
 137404         rc = SQLITE_ROW;
 137407     pCsr->nAdvance = nMerge;
 137408   }while( rc==SQLITE_OK );
 137410   return rc;
 137414 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
 137415   Fts3MultiSegReader *pCsr       /* Cursor object */
 137417   if( pCsr ){
 137418     int i;
 137419     for(i=0; i<pCsr->nSegment; i++){
 137420       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
 137422     sqlite3_free(pCsr->apSegment);
 137423     sqlite3_free(pCsr->aBuffer);
 137425     pCsr->nSegment = 0;
 137426     pCsr->apSegment = 0;
 137427     pCsr->aBuffer = 0;
 137432 ** Merge all level iLevel segments in the database into a single 
 137433 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
 137434 ** single segment with a level equal to the numerically largest level 
 137435 ** currently present in the database.
 137437 ** If this function is called with iLevel<0, but there is only one
 137438 ** segment in the database, SQLITE_DONE is returned immediately. 
 137439 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
 137440 ** an SQLite error code is returned.
 137442 static int fts3SegmentMerge(
 137443   Fts3Table *p, 
 137444   int iLangid,                    /* Language id to merge */
 137445   int iIndex,                     /* Index in p->aIndex[] to merge */
 137446   int iLevel                      /* Level to merge */
 137448   int rc;                         /* Return code */
 137449   int iIdx = 0;                   /* Index of new segment */
 137450   sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
 137451   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
 137452   Fts3SegFilter filter;           /* Segment term filter condition */
 137453   Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
 137454   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
 137456   assert( iLevel==FTS3_SEGCURSOR_ALL
 137457        || iLevel==FTS3_SEGCURSOR_PENDING
 137458        || iLevel>=0
 137460   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
 137461   assert( iIndex>=0 && iIndex<p->nIndex );
 137463   rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
 137464   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
 137466   if( iLevel==FTS3_SEGCURSOR_ALL ){
 137467     /* This call is to merge all segments in the database to a single
 137468     ** segment. The level of the new segment is equal to the numerically
 137469     ** greatest segment level currently present in the database for this
 137470     ** index. The idx of the new segment is always 0.  */
 137471     if( csr.nSegment==1 ){
 137472       rc = SQLITE_DONE;
 137473       goto finished;
 137475     rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
 137476     bIgnoreEmpty = 1;
 137478   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
 137479     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
 137480     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
 137481   }else{
 137482     /* This call is to merge all segments at level iLevel. find the next
 137483     ** available segment index at level iLevel+1. The call to
 137484     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
 137485     ** a single iLevel+2 segment if necessary.  */
 137486     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
 137487     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
 137489   if( rc!=SQLITE_OK ) goto finished;
 137490   assert( csr.nSegment>0 );
 137491   assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
 137492   assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
 137494   memset(&filter, 0, sizeof(Fts3SegFilter));
 137495   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
 137496   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
 137498   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
 137499   while( SQLITE_OK==rc ){
 137500     rc = sqlite3Fts3SegReaderStep(p, &csr);
 137501     if( rc!=SQLITE_ROW ) break;
 137502     rc = fts3SegWriterAdd(p, &pWriter, 1, 
 137503         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
 137505   if( rc!=SQLITE_OK ) goto finished;
 137506   assert( pWriter );
 137508   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
 137509     rc = fts3DeleteSegdir(
 137510         p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
 137512     if( rc!=SQLITE_OK ) goto finished;
 137514   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
 137516  finished:
 137517   fts3SegWriterFree(pWriter);
 137518   sqlite3Fts3SegReaderFinish(&csr);
 137519   return rc;
 137524 ** Flush the contents of pendingTerms to level 0 segments.
 137526 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
 137527   int rc = SQLITE_OK;
 137528   int i;
 137530   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
 137531     rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
 137532     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 137534   sqlite3Fts3PendingTermsClear(p);
 137536   /* Determine the auto-incr-merge setting if unknown.  If enabled,
 137537   ** estimate the number of leaf blocks of content to be written
 137539   if( rc==SQLITE_OK && p->bHasStat
 137540    && p->bAutoincrmerge==0xff && p->nLeafAdd>0
 137542     sqlite3_stmt *pStmt = 0;
 137543     rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
 137544     if( rc==SQLITE_OK ){
 137545       sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
 137546       rc = sqlite3_step(pStmt);
 137547       p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
 137548       rc = sqlite3_reset(pStmt);
 137551   return rc;
 137555 ** Encode N integers as varints into a blob.
 137557 static void fts3EncodeIntArray(
 137558   int N,             /* The number of integers to encode */
 137559   u32 *a,            /* The integer values */
 137560   char *zBuf,        /* Write the BLOB here */
 137561   int *pNBuf         /* Write number of bytes if zBuf[] used here */
 137563   int i, j;
 137564   for(i=j=0; i<N; i++){
 137565     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
 137567   *pNBuf = j;
 137571 ** Decode a blob of varints into N integers
 137573 static void fts3DecodeIntArray(
 137574   int N,             /* The number of integers to decode */
 137575   u32 *a,            /* Write the integer values */
 137576   const char *zBuf,  /* The BLOB containing the varints */
 137577   int nBuf           /* size of the BLOB */
 137579   int i, j;
 137580   UNUSED_PARAMETER(nBuf);
 137581   for(i=j=0; i<N; i++){
 137582     sqlite3_int64 x;
 137583     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
 137584     assert(j<=nBuf);
 137585     a[i] = (u32)(x & 0xffffffff);
 137590 ** Insert the sizes (in tokens) for each column of the document
 137591 ** with docid equal to p->iPrevDocid.  The sizes are encoded as
 137592 ** a blob of varints.
 137594 static void fts3InsertDocsize(
 137595   int *pRC,                       /* Result code */
 137596   Fts3Table *p,                   /* Table into which to insert */
 137597   u32 *aSz                        /* Sizes of each column, in tokens */
 137599   char *pBlob;             /* The BLOB encoding of the document size */
 137600   int nBlob;               /* Number of bytes in the BLOB */
 137601   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
 137602   int rc;                  /* Result code from subfunctions */
 137604   if( *pRC ) return;
 137605   pBlob = sqlite3_malloc( 10*p->nColumn );
 137606   if( pBlob==0 ){
 137607     *pRC = SQLITE_NOMEM;
 137608     return;
 137610   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
 137611   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
 137612   if( rc ){
 137613     sqlite3_free(pBlob);
 137614     *pRC = rc;
 137615     return;
 137617   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
 137618   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
 137619   sqlite3_step(pStmt);
 137620   *pRC = sqlite3_reset(pStmt);
 137624 ** Record 0 of the %_stat table contains a blob consisting of N varints,
 137625 ** where N is the number of user defined columns in the fts3 table plus
 137626 ** two. If nCol is the number of user defined columns, then values of the 
 137627 ** varints are set as follows:
 137629 **   Varint 0:       Total number of rows in the table.
 137631 **   Varint 1..nCol: For each column, the total number of tokens stored in
 137632 **                   the column for all rows of the table.
 137634 **   Varint 1+nCol:  The total size, in bytes, of all text values in all
 137635 **                   columns of all rows of the table.
 137638 static void fts3UpdateDocTotals(
 137639   int *pRC,                       /* The result code */
 137640   Fts3Table *p,                   /* Table being updated */
 137641   u32 *aSzIns,                    /* Size increases */
 137642   u32 *aSzDel,                    /* Size decreases */
 137643   int nChng                       /* Change in the number of documents */
 137645   char *pBlob;             /* Storage for BLOB written into %_stat */
 137646   int nBlob;               /* Size of BLOB written into %_stat */
 137647   u32 *a;                  /* Array of integers that becomes the BLOB */
 137648   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
 137649   int i;                   /* Loop counter */
 137650   int rc;                  /* Result code from subfunctions */
 137652   const int nStat = p->nColumn+2;
 137654   if( *pRC ) return;
 137655   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
 137656   if( a==0 ){
 137657     *pRC = SQLITE_NOMEM;
 137658     return;
 137660   pBlob = (char*)&a[nStat];
 137661   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
 137662   if( rc ){
 137663     sqlite3_free(a);
 137664     *pRC = rc;
 137665     return;
 137667   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 137668   if( sqlite3_step(pStmt)==SQLITE_ROW ){
 137669     fts3DecodeIntArray(nStat, a,
 137670          sqlite3_column_blob(pStmt, 0),
 137671          sqlite3_column_bytes(pStmt, 0));
 137672   }else{
 137673     memset(a, 0, sizeof(u32)*(nStat) );
 137675   rc = sqlite3_reset(pStmt);
 137676   if( rc!=SQLITE_OK ){
 137677     sqlite3_free(a);
 137678     *pRC = rc;
 137679     return;
 137681   if( nChng<0 && a[0]<(u32)(-nChng) ){
 137682     a[0] = 0;
 137683   }else{
 137684     a[0] += nChng;
 137686   for(i=0; i<p->nColumn+1; i++){
 137687     u32 x = a[i+1];
 137688     if( x+aSzIns[i] < aSzDel[i] ){
 137689       x = 0;
 137690     }else{
 137691       x = x + aSzIns[i] - aSzDel[i];
 137693     a[i+1] = x;
 137695   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
 137696   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
 137697   if( rc ){
 137698     sqlite3_free(a);
 137699     *pRC = rc;
 137700     return;
 137702   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
 137703   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
 137704   sqlite3_step(pStmt);
 137705   *pRC = sqlite3_reset(pStmt);
 137706   sqlite3_free(a);
 137710 ** Merge the entire database so that there is one segment for each 
 137711 ** iIndex/iLangid combination.
 137713 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
 137714   int bSeenDone = 0;
 137715   int rc;
 137716   sqlite3_stmt *pAllLangid = 0;
 137718   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 137719   if( rc==SQLITE_OK ){
 137720     int rc2;
 137721     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
 137722     while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
 137723       int i;
 137724       int iLangid = sqlite3_column_int(pAllLangid, 0);
 137725       for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
 137726         rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
 137727         if( rc==SQLITE_DONE ){
 137728           bSeenDone = 1;
 137729           rc = SQLITE_OK;
 137733     rc2 = sqlite3_reset(pAllLangid);
 137734     if( rc==SQLITE_OK ) rc = rc2;
 137737   sqlite3Fts3SegmentsClose(p);
 137738   sqlite3Fts3PendingTermsClear(p);
 137740   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
 137744 ** This function is called when the user executes the following statement:
 137746 **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
 137748 ** The entire FTS index is discarded and rebuilt. If the table is one 
 137749 ** created using the content=xxx option, then the new index is based on
 137750 ** the current contents of the xxx table. Otherwise, it is rebuilt based
 137751 ** on the contents of the %_content table.
 137753 static int fts3DoRebuild(Fts3Table *p){
 137754   int rc;                         /* Return Code */
 137756   rc = fts3DeleteAll(p, 0);
 137757   if( rc==SQLITE_OK ){
 137758     u32 *aSz = 0;
 137759     u32 *aSzIns = 0;
 137760     u32 *aSzDel = 0;
 137761     sqlite3_stmt *pStmt = 0;
 137762     int nEntry = 0;
 137764     /* Compose and prepare an SQL statement to loop through the content table */
 137765     char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
 137766     if( !zSql ){
 137767       rc = SQLITE_NOMEM;
 137768     }else{
 137769       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 137770       sqlite3_free(zSql);
 137773     if( rc==SQLITE_OK ){
 137774       int nByte = sizeof(u32) * (p->nColumn+1)*3;
 137775       aSz = (u32 *)sqlite3_malloc(nByte);
 137776       if( aSz==0 ){
 137777         rc = SQLITE_NOMEM;
 137778       }else{
 137779         memset(aSz, 0, nByte);
 137780         aSzIns = &aSz[p->nColumn+1];
 137781         aSzDel = &aSzIns[p->nColumn+1];
 137785     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 137786       int iCol;
 137787       int iLangid = langidFromSelect(p, pStmt);
 137788       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
 137789       memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
 137790       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
 137791         if( p->abNotindexed[iCol]==0 ){
 137792           const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
 137793           rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
 137794           aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
 137797       if( p->bHasDocsize ){
 137798         fts3InsertDocsize(&rc, p, aSz);
 137800       if( rc!=SQLITE_OK ){
 137801         sqlite3_finalize(pStmt);
 137802         pStmt = 0;
 137803       }else{
 137804         nEntry++;
 137805         for(iCol=0; iCol<=p->nColumn; iCol++){
 137806           aSzIns[iCol] += aSz[iCol];
 137810     if( p->bFts4 ){
 137811       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
 137813     sqlite3_free(aSz);
 137815     if( pStmt ){
 137816       int rc2 = sqlite3_finalize(pStmt);
 137817       if( rc==SQLITE_OK ){
 137818         rc = rc2;
 137823   return rc;
 137828 ** This function opens a cursor used to read the input data for an 
 137829 ** incremental merge operation. Specifically, it opens a cursor to scan
 137830 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute 
 137831 ** level iAbsLevel.
 137833 static int fts3IncrmergeCsr(
 137834   Fts3Table *p,                   /* FTS3 table handle */
 137835   sqlite3_int64 iAbsLevel,        /* Absolute level to open */
 137836   int nSeg,                       /* Number of segments to merge */
 137837   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
 137839   int rc;                         /* Return Code */
 137840   sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
 137841   int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
 137843   /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
 137844   memset(pCsr, 0, sizeof(*pCsr));
 137845   nByte = sizeof(Fts3SegReader *) * nSeg;
 137846   pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
 137848   if( pCsr->apSegment==0 ){
 137849     rc = SQLITE_NOMEM;
 137850   }else{
 137851     memset(pCsr->apSegment, 0, nByte);
 137852     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
 137854   if( rc==SQLITE_OK ){
 137855     int i;
 137856     int rc2;
 137857     sqlite3_bind_int64(pStmt, 1, iAbsLevel);
 137858     assert( pCsr->nSegment==0 );
 137859     for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
 137860       rc = sqlite3Fts3SegReaderNew(i, 0,
 137861           sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
 137862           sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
 137863           sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
 137864           sqlite3_column_blob(pStmt, 4),         /* segdir.root */
 137865           sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
 137866           &pCsr->apSegment[i]
 137868       pCsr->nSegment++;
 137870     rc2 = sqlite3_reset(pStmt);
 137871     if( rc==SQLITE_OK ) rc = rc2;
 137874   return rc;
 137877 typedef struct IncrmergeWriter IncrmergeWriter;
 137878 typedef struct NodeWriter NodeWriter;
 137879 typedef struct Blob Blob;
 137880 typedef struct NodeReader NodeReader;
 137883 ** An instance of the following structure is used as a dynamic buffer
 137884 ** to build up nodes or other blobs of data in.
 137886 ** The function blobGrowBuffer() is used to extend the allocation.
 137888 struct Blob {
 137889   char *a;                        /* Pointer to allocation */
 137890   int n;                          /* Number of valid bytes of data in a[] */
 137891   int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
 137895 ** This structure is used to build up buffers containing segment b-tree 
 137896 ** nodes (blocks).
 137898 struct NodeWriter {
 137899   sqlite3_int64 iBlock;           /* Current block id */
 137900   Blob key;                       /* Last key written to the current block */
 137901   Blob block;                     /* Current block image */
 137905 ** An object of this type contains the state required to create or append
 137906 ** to an appendable b-tree segment.
 137908 struct IncrmergeWriter {
 137909   int nLeafEst;                   /* Space allocated for leaf blocks */
 137910   int nWork;                      /* Number of leaf pages flushed */
 137911   sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
 137912   int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
 137913   sqlite3_int64 iStart;           /* Block number of first allocated block */
 137914   sqlite3_int64 iEnd;             /* Block number of last allocated block */
 137915   NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
 137919 ** An object of the following type is used to read data from a single
 137920 ** FTS segment node. See the following functions:
 137922 **     nodeReaderInit()
 137923 **     nodeReaderNext()
 137924 **     nodeReaderRelease()
 137926 struct NodeReader {
 137927   const char *aNode;
 137928   int nNode;
 137929   int iOff;                       /* Current offset within aNode[] */
 137931   /* Output variables. Containing the current node entry. */
 137932   sqlite3_int64 iChild;           /* Pointer to child node */
 137933   Blob term;                      /* Current term */
 137934   const char *aDoclist;           /* Pointer to doclist */
 137935   int nDoclist;                   /* Size of doclist in bytes */
 137939 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 137940 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
 137941 ** bytes in size, extend (realloc) it to be so.
 137943 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
 137944 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
 137945 ** to reflect the new size of the pBlob->a[] buffer.
 137947 static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
 137948   if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
 137949     int nAlloc = nMin;
 137950     char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
 137951     if( a ){
 137952       pBlob->nAlloc = nAlloc;
 137953       pBlob->a = a;
 137954     }else{
 137955       *pRc = SQLITE_NOMEM;
 137961 ** Attempt to advance the node-reader object passed as the first argument to
 137962 ** the next entry on the node. 
 137964 ** Return an error code if an error occurs (SQLITE_NOMEM is possible). 
 137965 ** Otherwise return SQLITE_OK. If there is no next entry on the node
 137966 ** (e.g. because the current entry is the last) set NodeReader->aNode to
 137967 ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output 
 137968 ** variables for the new entry.
 137970 static int nodeReaderNext(NodeReader *p){
 137971   int bFirst = (p->term.n==0);    /* True for first term on the node */
 137972   int nPrefix = 0;                /* Bytes to copy from previous term */
 137973   int nSuffix = 0;                /* Bytes to append to the prefix */
 137974   int rc = SQLITE_OK;             /* Return code */
 137976   assert( p->aNode );
 137977   if( p->iChild && bFirst==0 ) p->iChild++;
 137978   if( p->iOff>=p->nNode ){
 137979     /* EOF */
 137980     p->aNode = 0;
 137981   }else{
 137982     if( bFirst==0 ){
 137983       p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
 137985     p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
 137987     blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
 137988     if( rc==SQLITE_OK ){
 137989       memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
 137990       p->term.n = nPrefix+nSuffix;
 137991       p->iOff += nSuffix;
 137992       if( p->iChild==0 ){
 137993         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
 137994         p->aDoclist = &p->aNode[p->iOff];
 137995         p->iOff += p->nDoclist;
 138000   assert( p->iOff<=p->nNode );
 138002   return rc;
 138006 ** Release all dynamic resources held by node-reader object *p.
 138008 static void nodeReaderRelease(NodeReader *p){
 138009   sqlite3_free(p->term.a);
 138013 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
 138015 ** If successful, SQLITE_OK is returned and the NodeReader object set to 
 138016 ** point to the first entry on the node (if any). Otherwise, an SQLite
 138017 ** error code is returned.
 138019 static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
 138020   memset(p, 0, sizeof(NodeReader));
 138021   p->aNode = aNode;
 138022   p->nNode = nNode;
 138024   /* Figure out if this is a leaf or an internal node. */
 138025   if( p->aNode[0] ){
 138026     /* An internal node. */
 138027     p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
 138028   }else{
 138029     p->iOff = 1;
 138032   return nodeReaderNext(p);
 138036 ** This function is called while writing an FTS segment each time a leaf o
 138037 ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
 138038 ** to be greater than the largest key on the node just written, but smaller
 138039 ** than or equal to the first key that will be written to the next leaf
 138040 ** node.
 138042 ** The block id of the leaf node just written to disk may be found in
 138043 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
 138045 static int fts3IncrmergePush(
 138046   Fts3Table *p,                   /* Fts3 table handle */
 138047   IncrmergeWriter *pWriter,       /* Writer object */
 138048   const char *zTerm,              /* Term to write to internal node */
 138049   int nTerm                       /* Bytes at zTerm */
 138051   sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
 138052   int iLayer;
 138054   assert( nTerm>0 );
 138055   for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
 138056     sqlite3_int64 iNextPtr = 0;
 138057     NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
 138058     int rc = SQLITE_OK;
 138059     int nPrefix;
 138060     int nSuffix;
 138061     int nSpace;
 138063     /* Figure out how much space the key will consume if it is written to
 138064     ** the current node of layer iLayer. Due to the prefix compression, 
 138065     ** the space required changes depending on which node the key is to
 138066     ** be added to.  */
 138067     nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
 138068     nSuffix = nTerm - nPrefix;
 138069     nSpace  = sqlite3Fts3VarintLen(nPrefix);
 138070     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 138072     if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){ 
 138073       /* If the current node of layer iLayer contains zero keys, or if adding
 138074       ** the key to it will not cause it to grow to larger than nNodeSize 
 138075       ** bytes in size, write the key here.  */
 138077       Blob *pBlk = &pNode->block;
 138078       if( pBlk->n==0 ){
 138079         blobGrowBuffer(pBlk, p->nNodeSize, &rc);
 138080         if( rc==SQLITE_OK ){
 138081           pBlk->a[0] = (char)iLayer;
 138082           pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
 138085       blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
 138086       blobGrowBuffer(&pNode->key, nTerm, &rc);
 138088       if( rc==SQLITE_OK ){
 138089         if( pNode->key.n ){
 138090           pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
 138092         pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
 138093         memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
 138094         pBlk->n += nSuffix;
 138096         memcpy(pNode->key.a, zTerm, nTerm);
 138097         pNode->key.n = nTerm;
 138099     }else{
 138100       /* Otherwise, flush the current node of layer iLayer to disk.
 138101       ** Then allocate a new, empty sibling node. The key will be written
 138102       ** into the parent of this node. */
 138103       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
 138105       assert( pNode->block.nAlloc>=p->nNodeSize );
 138106       pNode->block.a[0] = (char)iLayer;
 138107       pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
 138109       iNextPtr = pNode->iBlock;
 138110       pNode->iBlock++;
 138111       pNode->key.n = 0;
 138114     if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
 138115     iPtr = iNextPtr;
 138118   assert( 0 );
 138119   return 0;
 138123 ** Append a term and (optionally) doclist to the FTS segment node currently
 138124 ** stored in blob *pNode. The node need not contain any terms, but the
 138125 ** header must be written before this function is called.
 138127 ** A node header is a single 0x00 byte for a leaf node, or a height varint
 138128 ** followed by the left-hand-child varint for an internal node.
 138130 ** The term to be appended is passed via arguments zTerm/nTerm. For a 
 138131 ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
 138132 ** node, both aDoclist and nDoclist must be passed 0.
 138134 ** If the size of the value in blob pPrev is zero, then this is the first
 138135 ** term written to the node. Otherwise, pPrev contains a copy of the 
 138136 ** previous term. Before this function returns, it is updated to contain a
 138137 ** copy of zTerm/nTerm.
 138139 ** It is assumed that the buffer associated with pNode is already large
 138140 ** enough to accommodate the new entry. The buffer associated with pPrev
 138141 ** is extended by this function if requrired.
 138143 ** If an error (i.e. OOM condition) occurs, an SQLite error code is
 138144 ** returned. Otherwise, SQLITE_OK.
 138146 static int fts3AppendToNode(
 138147   Blob *pNode,                    /* Current node image to append to */
 138148   Blob *pPrev,                    /* Buffer containing previous term written */
 138149   const char *zTerm,              /* New term to write */
 138150   int nTerm,                      /* Size of zTerm in bytes */
 138151   const char *aDoclist,           /* Doclist (or NULL) to write */
 138152   int nDoclist                    /* Size of aDoclist in bytes */ 
 138154   int rc = SQLITE_OK;             /* Return code */
 138155   int bFirst = (pPrev->n==0);     /* True if this is the first term written */
 138156   int nPrefix;                    /* Size of term prefix in bytes */
 138157   int nSuffix;                    /* Size of term suffix in bytes */
 138159   /* Node must have already been started. There must be a doclist for a
 138160   ** leaf node, and there must not be a doclist for an internal node.  */
 138161   assert( pNode->n>0 );
 138162   assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
 138164   blobGrowBuffer(pPrev, nTerm, &rc);
 138165   if( rc!=SQLITE_OK ) return rc;
 138167   nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
 138168   nSuffix = nTerm - nPrefix;
 138169   memcpy(pPrev->a, zTerm, nTerm);
 138170   pPrev->n = nTerm;
 138172   if( bFirst==0 ){
 138173     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
 138175   pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
 138176   memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
 138177   pNode->n += nSuffix;
 138179   if( aDoclist ){
 138180     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
 138181     memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
 138182     pNode->n += nDoclist;
 138185   assert( pNode->n<=pNode->nAlloc );
 138187   return SQLITE_OK;
 138191 ** Append the current term and doclist pointed to by cursor pCsr to the
 138192 ** appendable b-tree segment opened for writing by pWriter.
 138194 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
 138196 static int fts3IncrmergeAppend(
 138197   Fts3Table *p,                   /* Fts3 table handle */
 138198   IncrmergeWriter *pWriter,       /* Writer object */
 138199   Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
 138201   const char *zTerm = pCsr->zTerm;
 138202   int nTerm = pCsr->nTerm;
 138203   const char *aDoclist = pCsr->aDoclist;
 138204   int nDoclist = pCsr->nDoclist;
 138205   int rc = SQLITE_OK;           /* Return code */
 138206   int nSpace;                   /* Total space in bytes required on leaf */
 138207   int nPrefix;                  /* Size of prefix shared with previous term */
 138208   int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
 138209   NodeWriter *pLeaf;            /* Object used to write leaf nodes */
 138211   pLeaf = &pWriter->aNodeWriter[0];
 138212   nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
 138213   nSuffix = nTerm - nPrefix;
 138215   nSpace  = sqlite3Fts3VarintLen(nPrefix);
 138216   nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 138217   nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
 138219   /* If the current block is not empty, and if adding this term/doclist
 138220   ** to the current block would make it larger than Fts3Table.nNodeSize
 138221   ** bytes, write this block out to the database. */
 138222   if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
 138223     rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
 138224     pWriter->nWork++;
 138226     /* Add the current term to the parent node. The term added to the 
 138227     ** parent must:
 138229     **   a) be greater than the largest term on the leaf node just written
 138230     **      to the database (still available in pLeaf->key), and
 138232     **   b) be less than or equal to the term about to be added to the new
 138233     **      leaf node (zTerm/nTerm).
 138235     ** In other words, it must be the prefix of zTerm 1 byte longer than
 138236     ** the common prefix (if any) of zTerm and pWriter->zTerm.
 138238     if( rc==SQLITE_OK ){
 138239       rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
 138242     /* Advance to the next output block */
 138243     pLeaf->iBlock++;
 138244     pLeaf->key.n = 0;
 138245     pLeaf->block.n = 0;
 138247     nSuffix = nTerm;
 138248     nSpace  = 1;
 138249     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
 138250     nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
 138253   blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
 138255   if( rc==SQLITE_OK ){
 138256     if( pLeaf->block.n==0 ){
 138257       pLeaf->block.n = 1;
 138258       pLeaf->block.a[0] = '\0';
 138260     rc = fts3AppendToNode(
 138261         &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
 138265   return rc;
 138269 ** This function is called to release all dynamic resources held by the
 138270 ** merge-writer object pWriter, and if no error has occurred, to flush
 138271 ** all outstanding node buffers held by pWriter to disk.
 138273 ** If *pRc is not SQLITE_OK when this function is called, then no attempt
 138274 ** is made to write any data to disk. Instead, this function serves only
 138275 ** to release outstanding resources.
 138277 ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
 138278 ** flushing buffers to disk, *pRc is set to an SQLite error code before
 138279 ** returning.
 138281 static void fts3IncrmergeRelease(
 138282   Fts3Table *p,                   /* FTS3 table handle */
 138283   IncrmergeWriter *pWriter,       /* Merge-writer object */
 138284   int *pRc                        /* IN/OUT: Error code */
 138286   int i;                          /* Used to iterate through non-root layers */
 138287   int iRoot;                      /* Index of root in pWriter->aNodeWriter */
 138288   NodeWriter *pRoot;              /* NodeWriter for root node */
 138289   int rc = *pRc;                  /* Error code */
 138291   /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment 
 138292   ** root node. If the segment fits entirely on a single leaf node, iRoot
 138293   ** will be set to 0. If the root node is the parent of the leaves, iRoot
 138294   ** will be 1. And so on.  */
 138295   for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
 138296     NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
 138297     if( pNode->block.n>0 ) break;
 138298     assert( *pRc || pNode->block.nAlloc==0 );
 138299     assert( *pRc || pNode->key.nAlloc==0 );
 138300     sqlite3_free(pNode->block.a);
 138301     sqlite3_free(pNode->key.a);
 138304   /* Empty output segment. This is a no-op. */
 138305   if( iRoot<0 ) return;
 138307   /* The entire output segment fits on a single node. Normally, this means
 138308   ** the node would be stored as a blob in the "root" column of the %_segdir
 138309   ** table. However, this is not permitted in this case. The problem is that 
 138310   ** space has already been reserved in the %_segments table, and so the 
 138311   ** start_block and end_block fields of the %_segdir table must be populated. 
 138312   ** And, by design or by accident, released versions of FTS cannot handle 
 138313   ** segments that fit entirely on the root node with start_block!=0.
 138315   ** Instead, create a synthetic root node that contains nothing but a 
 138316   ** pointer to the single content node. So that the segment consists of a
 138317   ** single leaf and a single interior (root) node.
 138319   ** Todo: Better might be to defer allocating space in the %_segments 
 138320   ** table until we are sure it is needed.
 138322   if( iRoot==0 ){
 138323     Blob *pBlock = &pWriter->aNodeWriter[1].block;
 138324     blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
 138325     if( rc==SQLITE_OK ){
 138326       pBlock->a[0] = 0x01;
 138327       pBlock->n = 1 + sqlite3Fts3PutVarint(
 138328           &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
 138331     iRoot = 1;
 138333   pRoot = &pWriter->aNodeWriter[iRoot];
 138335   /* Flush all currently outstanding nodes to disk. */
 138336   for(i=0; i<iRoot; i++){
 138337     NodeWriter *pNode = &pWriter->aNodeWriter[i];
 138338     if( pNode->block.n>0 && rc==SQLITE_OK ){
 138339       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
 138341     sqlite3_free(pNode->block.a);
 138342     sqlite3_free(pNode->key.a);
 138345   /* Write the %_segdir record. */
 138346   if( rc==SQLITE_OK ){
 138347     rc = fts3WriteSegdir(p, 
 138348         pWriter->iAbsLevel+1,               /* level */
 138349         pWriter->iIdx,                      /* idx */
 138350         pWriter->iStart,                    /* start_block */
 138351         pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
 138352         pWriter->iEnd,                      /* end_block */
 138353         pRoot->block.a, pRoot->block.n      /* root */
 138356   sqlite3_free(pRoot->block.a);
 138357   sqlite3_free(pRoot->key.a);
 138359   *pRc = rc;
 138363 ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
 138364 ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
 138365 ** the other, it is considered to be smaller than the other.
 138367 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
 138368 ** if it is greater.
 138370 static int fts3TermCmp(
 138371   const char *zLhs, int nLhs,     /* LHS of comparison */
 138372   const char *zRhs, int nRhs      /* RHS of comparison */
 138374   int nCmp = MIN(nLhs, nRhs);
 138375   int res;
 138377   res = memcmp(zLhs, zRhs, nCmp);
 138378   if( res==0 ) res = nLhs - nRhs;
 138380   return res;
 138385 ** Query to see if the entry in the %_segments table with blockid iEnd is 
 138386 ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
 138387 ** returning. Otherwise, set *pbRes to 0. 
 138389 ** Or, if an error occurs while querying the database, return an SQLite 
 138390 ** error code. The final value of *pbRes is undefined in this case.
 138392 ** This is used to test if a segment is an "appendable" segment. If it
 138393 ** is, then a NULL entry has been inserted into the %_segments table
 138394 ** with blockid %_segdir.end_block.
 138396 static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
 138397   int bRes = 0;                   /* Result to set *pbRes to */
 138398   sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
 138399   int rc;                         /* Return code */
 138401   rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
 138402   if( rc==SQLITE_OK ){
 138403     sqlite3_bind_int64(pCheck, 1, iEnd);
 138404     if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
 138405     rc = sqlite3_reset(pCheck);
 138408   *pbRes = bRes;
 138409   return rc;
 138413 ** This function is called when initializing an incremental-merge operation.
 138414 ** It checks if the existing segment with index value iIdx at absolute level 
 138415 ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
 138416 ** merge-writer object *pWriter is initialized to write to it.
 138418 ** An existing segment can be appended to by an incremental merge if:
 138420 **   * It was initially created as an appendable segment (with all required
 138421 **     space pre-allocated), and
 138423 **   * The first key read from the input (arguments zKey and nKey) is 
 138424 **     greater than the largest key currently stored in the potential
 138425 **     output segment.
 138427 static int fts3IncrmergeLoad(
 138428   Fts3Table *p,                   /* Fts3 table handle */
 138429   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
 138430   int iIdx,                       /* Index of candidate output segment */
 138431   const char *zKey,               /* First key to write */
 138432   int nKey,                       /* Number of bytes in nKey */
 138433   IncrmergeWriter *pWriter        /* Populate this object */
 138435   int rc;                         /* Return code */
 138436   sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
 138438   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
 138439   if( rc==SQLITE_OK ){
 138440     sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
 138441     sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
 138442     sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
 138443     const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
 138444     int nRoot = 0;                /* Size of aRoot[] in bytes */
 138445     int rc2;                      /* Return code from sqlite3_reset() */
 138446     int bAppendable = 0;          /* Set to true if segment is appendable */
 138448     /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
 138449     sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
 138450     sqlite3_bind_int(pSelect, 2, iIdx);
 138451     if( sqlite3_step(pSelect)==SQLITE_ROW ){
 138452       iStart = sqlite3_column_int64(pSelect, 1);
 138453       iLeafEnd = sqlite3_column_int64(pSelect, 2);
 138454       iEnd = sqlite3_column_int64(pSelect, 3);
 138455       nRoot = sqlite3_column_bytes(pSelect, 4);
 138456       aRoot = sqlite3_column_blob(pSelect, 4);
 138457     }else{
 138458       return sqlite3_reset(pSelect);
 138461     /* Check for the zero-length marker in the %_segments table */
 138462     rc = fts3IsAppendable(p, iEnd, &bAppendable);
 138464     /* Check that zKey/nKey is larger than the largest key the candidate */
 138465     if( rc==SQLITE_OK && bAppendable ){
 138466       char *aLeaf = 0;
 138467       int nLeaf = 0;
 138469       rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
 138470       if( rc==SQLITE_OK ){
 138471         NodeReader reader;
 138472         for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
 138473             rc==SQLITE_OK && reader.aNode;
 138474             rc = nodeReaderNext(&reader)
 138476           assert( reader.aNode );
 138478         if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
 138479           bAppendable = 0;
 138481         nodeReaderRelease(&reader);
 138483       sqlite3_free(aLeaf);
 138486     if( rc==SQLITE_OK && bAppendable ){
 138487       /* It is possible to append to this segment. Set up the IncrmergeWriter
 138488       ** object to do so.  */
 138489       int i;
 138490       int nHeight = (int)aRoot[0];
 138491       NodeWriter *pNode;
 138493       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
 138494       pWriter->iStart = iStart;
 138495       pWriter->iEnd = iEnd;
 138496       pWriter->iAbsLevel = iAbsLevel;
 138497       pWriter->iIdx = iIdx;
 138499       for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
 138500         pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
 138503       pNode = &pWriter->aNodeWriter[nHeight];
 138504       pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
 138505       blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
 138506       if( rc==SQLITE_OK ){
 138507         memcpy(pNode->block.a, aRoot, nRoot);
 138508         pNode->block.n = nRoot;
 138511       for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
 138512         NodeReader reader;
 138513         pNode = &pWriter->aNodeWriter[i];
 138515         rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
 138516         while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
 138517         blobGrowBuffer(&pNode->key, reader.term.n, &rc);
 138518         if( rc==SQLITE_OK ){
 138519           memcpy(pNode->key.a, reader.term.a, reader.term.n);
 138520           pNode->key.n = reader.term.n;
 138521           if( i>0 ){
 138522             char *aBlock = 0;
 138523             int nBlock = 0;
 138524             pNode = &pWriter->aNodeWriter[i-1];
 138525             pNode->iBlock = reader.iChild;
 138526             rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
 138527             blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
 138528             if( rc==SQLITE_OK ){
 138529               memcpy(pNode->block.a, aBlock, nBlock);
 138530               pNode->block.n = nBlock;
 138532             sqlite3_free(aBlock);
 138535         nodeReaderRelease(&reader);
 138539     rc2 = sqlite3_reset(pSelect);
 138540     if( rc==SQLITE_OK ) rc = rc2;
 138543   return rc;
 138547 ** Determine the largest segment index value that exists within absolute
 138548 ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
 138549 ** one before returning SQLITE_OK. Or, if there are no segments at all 
 138550 ** within level iAbsLevel, set *piIdx to zero.
 138552 ** If an error occurs, return an SQLite error code. The final value of
 138553 ** *piIdx is undefined in this case.
 138555 static int fts3IncrmergeOutputIdx( 
 138556   Fts3Table *p,                   /* FTS Table handle */
 138557   sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
 138558   int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
 138560   int rc;
 138561   sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
 138563   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
 138564   if( rc==SQLITE_OK ){
 138565     sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
 138566     sqlite3_step(pOutputIdx);
 138567     *piIdx = sqlite3_column_int(pOutputIdx, 0);
 138568     rc = sqlite3_reset(pOutputIdx);
 138571   return rc;
 138575 ** Allocate an appendable output segment on absolute level iAbsLevel+1
 138576 ** with idx value iIdx.
 138578 ** In the %_segdir table, a segment is defined by the values in three
 138579 ** columns:
 138581 **     start_block
 138582 **     leaves_end_block
 138583 **     end_block
 138585 ** When an appendable segment is allocated, it is estimated that the
 138586 ** maximum number of leaf blocks that may be required is the sum of the
 138587 ** number of leaf blocks consumed by the input segments, plus the number
 138588 ** of input segments, multiplied by two. This value is stored in stack 
 138589 ** variable nLeafEst.
 138591 ** A total of 16*nLeafEst blocks are allocated when an appendable segment
 138592 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
 138593 ** array of leaf nodes starts at the first block allocated. The array
 138594 ** of interior nodes that are parents of the leaf nodes start at block
 138595 ** (start_block + (1 + end_block - start_block) / 16). And so on.
 138597 ** In the actual code below, the value "16" is replaced with the 
 138598 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
 138600 static int fts3IncrmergeWriter( 
 138601   Fts3Table *p,                   /* Fts3 table handle */
 138602   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
 138603   int iIdx,                       /* Index of new output segment */
 138604   Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
 138605   IncrmergeWriter *pWriter        /* Populate this object */
 138607   int rc;                         /* Return Code */
 138608   int i;                          /* Iterator variable */
 138609   int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
 138610   sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
 138611   sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
 138613   /* Calculate nLeafEst. */
 138614   rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
 138615   if( rc==SQLITE_OK ){
 138616     sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
 138617     sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
 138618     if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
 138619       nLeafEst = sqlite3_column_int(pLeafEst, 0);
 138621     rc = sqlite3_reset(pLeafEst);
 138623   if( rc!=SQLITE_OK ) return rc;
 138625   /* Calculate the first block to use in the output segment */
 138626   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
 138627   if( rc==SQLITE_OK ){
 138628     if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
 138629       pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
 138630       pWriter->iEnd = pWriter->iStart - 1;
 138631       pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
 138633     rc = sqlite3_reset(pFirstBlock);
 138635   if( rc!=SQLITE_OK ) return rc;
 138637   /* Insert the marker in the %_segments table to make sure nobody tries
 138638   ** to steal the space just allocated. This is also used to identify 
 138639   ** appendable segments.  */
 138640   rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
 138641   if( rc!=SQLITE_OK ) return rc;
 138643   pWriter->iAbsLevel = iAbsLevel;
 138644   pWriter->nLeafEst = nLeafEst;
 138645   pWriter->iIdx = iIdx;
 138647   /* Set up the array of NodeWriter objects */
 138648   for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
 138649     pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
 138651   return SQLITE_OK;
 138655 ** Remove an entry from the %_segdir table. This involves running the 
 138656 ** following two statements:
 138658 **   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
 138659 **   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
 138661 ** The DELETE statement removes the specific %_segdir level. The UPDATE 
 138662 ** statement ensures that the remaining segments have contiguously allocated
 138663 ** idx values.
 138665 static int fts3RemoveSegdirEntry(
 138666   Fts3Table *p,                   /* FTS3 table handle */
 138667   sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
 138668   int iIdx                        /* Index of %_segdir entry to delete */
 138670   int rc;                         /* Return code */
 138671   sqlite3_stmt *pDelete = 0;      /* DELETE statement */
 138673   rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
 138674   if( rc==SQLITE_OK ){
 138675     sqlite3_bind_int64(pDelete, 1, iAbsLevel);
 138676     sqlite3_bind_int(pDelete, 2, iIdx);
 138677     sqlite3_step(pDelete);
 138678     rc = sqlite3_reset(pDelete);
 138681   return rc;
 138685 ** One or more segments have just been removed from absolute level iAbsLevel.
 138686 ** Update the 'idx' values of the remaining segments in the level so that
 138687 ** the idx values are a contiguous sequence starting from 0.
 138689 static int fts3RepackSegdirLevel(
 138690   Fts3Table *p,                   /* FTS3 table handle */
 138691   sqlite3_int64 iAbsLevel         /* Absolute level to repack */
 138693   int rc;                         /* Return code */
 138694   int *aIdx = 0;                  /* Array of remaining idx values */
 138695   int nIdx = 0;                   /* Valid entries in aIdx[] */
 138696   int nAlloc = 0;                 /* Allocated size of aIdx[] */
 138697   int i;                          /* Iterator variable */
 138698   sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
 138699   sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
 138701   rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
 138702   if( rc==SQLITE_OK ){
 138703     int rc2;
 138704     sqlite3_bind_int64(pSelect, 1, iAbsLevel);
 138705     while( SQLITE_ROW==sqlite3_step(pSelect) ){
 138706       if( nIdx>=nAlloc ){
 138707         int *aNew;
 138708         nAlloc += 16;
 138709         aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
 138710         if( !aNew ){
 138711           rc = SQLITE_NOMEM;
 138712           break;
 138714         aIdx = aNew;
 138716       aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
 138718     rc2 = sqlite3_reset(pSelect);
 138719     if( rc==SQLITE_OK ) rc = rc2;
 138722   if( rc==SQLITE_OK ){
 138723     rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
 138725   if( rc==SQLITE_OK ){
 138726     sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
 138729   assert( p->bIgnoreSavepoint==0 );
 138730   p->bIgnoreSavepoint = 1;
 138731   for(i=0; rc==SQLITE_OK && i<nIdx; i++){
 138732     if( aIdx[i]!=i ){
 138733       sqlite3_bind_int(pUpdate, 3, aIdx[i]);
 138734       sqlite3_bind_int(pUpdate, 1, i);
 138735       sqlite3_step(pUpdate);
 138736       rc = sqlite3_reset(pUpdate);
 138739   p->bIgnoreSavepoint = 0;
 138741   sqlite3_free(aIdx);
 138742   return rc;
 138745 static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
 138746   pNode->a[0] = (char)iHeight;
 138747   if( iChild ){
 138748     assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
 138749     pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
 138750   }else{
 138751     assert( pNode->nAlloc>=1 );
 138752     pNode->n = 1;
 138757 ** The first two arguments are a pointer to and the size of a segment b-tree
 138758 ** node. The node may be a leaf or an internal node.
 138760 ** This function creates a new node image in blob object *pNew by copying
 138761 ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
 138762 ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
 138764 static int fts3TruncateNode(
 138765   const char *aNode,              /* Current node image */
 138766   int nNode,                      /* Size of aNode in bytes */
 138767   Blob *pNew,                     /* OUT: Write new node image here */
 138768   const char *zTerm,              /* Omit all terms smaller than this */
 138769   int nTerm,                      /* Size of zTerm in bytes */
 138770   sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
 138772   NodeReader reader;              /* Reader object */
 138773   Blob prev = {0, 0, 0};          /* Previous term written to new node */
 138774   int rc = SQLITE_OK;             /* Return code */
 138775   int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
 138777   /* Allocate required output space */
 138778   blobGrowBuffer(pNew, nNode, &rc);
 138779   if( rc!=SQLITE_OK ) return rc;
 138780   pNew->n = 0;
 138782   /* Populate new node buffer */
 138783   for(rc = nodeReaderInit(&reader, aNode, nNode); 
 138784       rc==SQLITE_OK && reader.aNode; 
 138785       rc = nodeReaderNext(&reader)
 138787     if( pNew->n==0 ){
 138788       int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
 138789       if( res<0 || (bLeaf==0 && res==0) ) continue;
 138790       fts3StartNode(pNew, (int)aNode[0], reader.iChild);
 138791       *piBlock = reader.iChild;
 138793     rc = fts3AppendToNode(
 138794         pNew, &prev, reader.term.a, reader.term.n,
 138795         reader.aDoclist, reader.nDoclist
 138797     if( rc!=SQLITE_OK ) break;
 138799   if( pNew->n==0 ){
 138800     fts3StartNode(pNew, (int)aNode[0], reader.iChild);
 138801     *piBlock = reader.iChild;
 138803   assert( pNew->n<=pNew->nAlloc );
 138805   nodeReaderRelease(&reader);
 138806   sqlite3_free(prev.a);
 138807   return rc;
 138811 ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute 
 138812 ** level iAbsLevel. This may involve deleting entries from the %_segments
 138813 ** table, and modifying existing entries in both the %_segments and %_segdir
 138814 ** tables.
 138816 ** SQLITE_OK is returned if the segment is updated successfully. Or an
 138817 ** SQLite error code otherwise.
 138819 static int fts3TruncateSegment(
 138820   Fts3Table *p,                   /* FTS3 table handle */
 138821   sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
 138822   int iIdx,                       /* Index within level of segment to modify */
 138823   const char *zTerm,              /* Remove terms smaller than this */
 138824   int nTerm                      /* Number of bytes in buffer zTerm */
 138826   int rc = SQLITE_OK;             /* Return code */
 138827   Blob root = {0,0,0};            /* New root page image */
 138828   Blob block = {0,0,0};           /* Buffer used for any other block */
 138829   sqlite3_int64 iBlock = 0;       /* Block id */
 138830   sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
 138831   sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
 138832   sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
 138834   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
 138835   if( rc==SQLITE_OK ){
 138836     int rc2;                      /* sqlite3_reset() return code */
 138837     sqlite3_bind_int64(pFetch, 1, iAbsLevel);
 138838     sqlite3_bind_int(pFetch, 2, iIdx);
 138839     if( SQLITE_ROW==sqlite3_step(pFetch) ){
 138840       const char *aRoot = sqlite3_column_blob(pFetch, 4);
 138841       int nRoot = sqlite3_column_bytes(pFetch, 4);
 138842       iOldStart = sqlite3_column_int64(pFetch, 1);
 138843       rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
 138845     rc2 = sqlite3_reset(pFetch);
 138846     if( rc==SQLITE_OK ) rc = rc2;
 138849   while( rc==SQLITE_OK && iBlock ){
 138850     char *aBlock = 0;
 138851     int nBlock = 0;
 138852     iNewStart = iBlock;
 138854     rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
 138855     if( rc==SQLITE_OK ){
 138856       rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
 138858     if( rc==SQLITE_OK ){
 138859       rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
 138861     sqlite3_free(aBlock);
 138864   /* Variable iNewStart now contains the first valid leaf node. */
 138865   if( rc==SQLITE_OK && iNewStart ){
 138866     sqlite3_stmt *pDel = 0;
 138867     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
 138868     if( rc==SQLITE_OK ){
 138869       sqlite3_bind_int64(pDel, 1, iOldStart);
 138870       sqlite3_bind_int64(pDel, 2, iNewStart-1);
 138871       sqlite3_step(pDel);
 138872       rc = sqlite3_reset(pDel);
 138876   if( rc==SQLITE_OK ){
 138877     sqlite3_stmt *pChomp = 0;
 138878     rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
 138879     if( rc==SQLITE_OK ){
 138880       sqlite3_bind_int64(pChomp, 1, iNewStart);
 138881       sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
 138882       sqlite3_bind_int64(pChomp, 3, iAbsLevel);
 138883       sqlite3_bind_int(pChomp, 4, iIdx);
 138884       sqlite3_step(pChomp);
 138885       rc = sqlite3_reset(pChomp);
 138889   sqlite3_free(root.a);
 138890   sqlite3_free(block.a);
 138891   return rc;
 138895 ** This function is called after an incrmental-merge operation has run to
 138896 ** merge (or partially merge) two or more segments from absolute level
 138897 ** iAbsLevel.
 138899 ** Each input segment is either removed from the db completely (if all of
 138900 ** its data was copied to the output segment by the incrmerge operation)
 138901 ** or modified in place so that it no longer contains those entries that
 138902 ** have been duplicated in the output segment.
 138904 static int fts3IncrmergeChomp(
 138905   Fts3Table *p,                   /* FTS table handle */
 138906   sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
 138907   Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
 138908   int *pnRem                      /* Number of segments not deleted */
 138910   int i;
 138911   int nRem = 0;
 138912   int rc = SQLITE_OK;
 138914   for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
 138915     Fts3SegReader *pSeg = 0;
 138916     int j;
 138918     /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
 138919     ** somewhere in the pCsr->apSegment[] array.  */
 138920     for(j=0; ALWAYS(j<pCsr->nSegment); j++){
 138921       pSeg = pCsr->apSegment[j];
 138922       if( pSeg->iIdx==i ) break;
 138924     assert( j<pCsr->nSegment && pSeg->iIdx==i );
 138926     if( pSeg->aNode==0 ){
 138927       /* Seg-reader is at EOF. Remove the entire input segment. */
 138928       rc = fts3DeleteSegment(p, pSeg);
 138929       if( rc==SQLITE_OK ){
 138930         rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
 138932       *pnRem = 0;
 138933     }else{
 138934       /* The incremental merge did not copy all the data from this 
 138935       ** segment to the upper level. The segment is modified in place
 138936       ** so that it contains no keys smaller than zTerm/nTerm. */ 
 138937       const char *zTerm = pSeg->zTerm;
 138938       int nTerm = pSeg->nTerm;
 138939       rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
 138940       nRem++;
 138944   if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
 138945     rc = fts3RepackSegdirLevel(p, iAbsLevel);
 138948   *pnRem = nRem;
 138949   return rc;
 138953 ** Store an incr-merge hint in the database.
 138955 static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
 138956   sqlite3_stmt *pReplace = 0;
 138957   int rc;                         /* Return code */
 138959   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
 138960   if( rc==SQLITE_OK ){
 138961     sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
 138962     sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
 138963     sqlite3_step(pReplace);
 138964     rc = sqlite3_reset(pReplace);
 138967   return rc;
 138971 ** Load an incr-merge hint from the database. The incr-merge hint, if one 
 138972 ** exists, is stored in the rowid==1 row of the %_stat table.
 138974 ** If successful, populate blob *pHint with the value read from the %_stat
 138975 ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
 138976 ** SQLite error code.
 138978 static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
 138979   sqlite3_stmt *pSelect = 0;
 138980   int rc;
 138982   pHint->n = 0;
 138983   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
 138984   if( rc==SQLITE_OK ){
 138985     int rc2;
 138986     sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
 138987     if( SQLITE_ROW==sqlite3_step(pSelect) ){
 138988       const char *aHint = sqlite3_column_blob(pSelect, 0);
 138989       int nHint = sqlite3_column_bytes(pSelect, 0);
 138990       if( aHint ){
 138991         blobGrowBuffer(pHint, nHint, &rc);
 138992         if( rc==SQLITE_OK ){
 138993           memcpy(pHint->a, aHint, nHint);
 138994           pHint->n = nHint;
 138998     rc2 = sqlite3_reset(pSelect);
 138999     if( rc==SQLITE_OK ) rc = rc2;
 139002   return rc;
 139006 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
 139007 ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
 139008 ** consists of two varints, the absolute level number of the input segments 
 139009 ** and the number of input segments.
 139011 ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
 139012 ** set *pRc to an SQLite error code before returning.
 139014 static void fts3IncrmergeHintPush(
 139015   Blob *pHint,                    /* Hint blob to append to */
 139016   i64 iAbsLevel,                  /* First varint to store in hint */
 139017   int nInput,                     /* Second varint to store in hint */
 139018   int *pRc                        /* IN/OUT: Error code */
 139020   blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
 139021   if( *pRc==SQLITE_OK ){
 139022     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
 139023     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
 139028 ** Read the last entry (most recently pushed) from the hint blob *pHint
 139029 ** and then remove the entry. Write the two values read to *piAbsLevel and 
 139030 ** *pnInput before returning.
 139032 ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
 139033 ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
 139035 static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
 139036   const int nHint = pHint->n;
 139037   int i;
 139039   i = pHint->n-2;
 139040   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
 139041   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
 139043   pHint->n = i;
 139044   i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
 139045   i += fts3GetVarint32(&pHint->a[i], pnInput);
 139046   if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
 139048   return SQLITE_OK;
 139053 ** Attempt an incremental merge that writes nMerge leaf blocks.
 139055 ** Incremental merges happen nMin segments at a time. The two
 139056 ** segments to be merged are the nMin oldest segments (the ones with
 139057 ** the smallest indexes) in the highest level that contains at least
 139058 ** nMin segments. Multiple merges might occur in an attempt to write the 
 139059 ** quota of nMerge leaf blocks.
 139061 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
 139062   int rc;                         /* Return code */
 139063   int nRem = nMerge;              /* Number of leaf pages yet to  be written */
 139064   Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
 139065   Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
 139066   IncrmergeWriter *pWriter;       /* Writer object */
 139067   int nSeg = 0;                   /* Number of input segments */
 139068   sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
 139069   Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
 139070   int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
 139072   /* Allocate space for the cursor, filter and writer objects */
 139073   const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
 139074   pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
 139075   if( !pWriter ) return SQLITE_NOMEM;
 139076   pFilter = (Fts3SegFilter *)&pWriter[1];
 139077   pCsr = (Fts3MultiSegReader *)&pFilter[1];
 139079   rc = fts3IncrmergeHintLoad(p, &hint);
 139080   while( rc==SQLITE_OK && nRem>0 ){
 139081     const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
 139082     sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
 139083     int bUseHint = 0;             /* True if attempting to append */
 139085     /* Search the %_segdir table for the absolute level with the smallest
 139086     ** relative level number that contains at least nMin segments, if any.
 139087     ** If one is found, set iAbsLevel to the absolute level number and
 139088     ** nSeg to nMin. If no level with at least nMin segments can be found, 
 139089     ** set nSeg to -1.
 139091     rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
 139092     sqlite3_bind_int(pFindLevel, 1, nMin);
 139093     if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
 139094       iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
 139095       nSeg = nMin;
 139096     }else{
 139097       nSeg = -1;
 139099     rc = sqlite3_reset(pFindLevel);
 139101     /* If the hint read from the %_stat table is not empty, check if the
 139102     ** last entry in it specifies a relative level smaller than or equal
 139103     ** to the level identified by the block above (if any). If so, this 
 139104     ** iteration of the loop will work on merging at the hinted level.
 139106     if( rc==SQLITE_OK && hint.n ){
 139107       int nHint = hint.n;
 139108       sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
 139109       int nHintSeg = 0;                     /* Hint number of segments */
 139111       rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
 139112       if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
 139113         iAbsLevel = iHintAbsLevel;
 139114         nSeg = nHintSeg;
 139115         bUseHint = 1;
 139116         bDirtyHint = 1;
 139117       }else{
 139118         /* This undoes the effect of the HintPop() above - so that no entry
 139119         ** is removed from the hint blob.  */
 139120         hint.n = nHint;
 139124     /* If nSeg is less that zero, then there is no level with at least
 139125     ** nMin segments and no hint in the %_stat table. No work to do.
 139126     ** Exit early in this case.  */
 139127     if( nSeg<0 ) break;
 139129     /* Open a cursor to iterate through the contents of the oldest nSeg 
 139130     ** indexes of absolute level iAbsLevel. If this cursor is opened using 
 139131     ** the 'hint' parameters, it is possible that there are less than nSeg
 139132     ** segments available in level iAbsLevel. In this case, no work is
 139133     ** done on iAbsLevel - fall through to the next iteration of the loop 
 139134     ** to start work on some other level.  */
 139135     memset(pWriter, 0, nAlloc);
 139136     pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
 139137     if( rc==SQLITE_OK ){
 139138       rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
 139140     if( SQLITE_OK==rc && pCsr->nSegment==nSeg
 139141      && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
 139142      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
 139144       int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
 139145       rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
 139146       if( rc==SQLITE_OK ){
 139147         if( bUseHint && iIdx>0 ){
 139148           const char *zKey = pCsr->zTerm;
 139149           int nKey = pCsr->nTerm;
 139150           rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
 139151         }else{
 139152           rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
 139156       if( rc==SQLITE_OK && pWriter->nLeafEst ){
 139157         fts3LogMerge(nSeg, iAbsLevel);
 139158         do {
 139159           rc = fts3IncrmergeAppend(p, pWriter, pCsr);
 139160           if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
 139161           if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
 139162         }while( rc==SQLITE_ROW );
 139164         /* Update or delete the input segments */
 139165         if( rc==SQLITE_OK ){
 139166           nRem -= (1 + pWriter->nWork);
 139167           rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
 139168           if( nSeg!=0 ){
 139169             bDirtyHint = 1;
 139170             fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
 139175       fts3IncrmergeRelease(p, pWriter, &rc);
 139178     sqlite3Fts3SegReaderFinish(pCsr);
 139181   /* Write the hint values into the %_stat table for the next incr-merger */
 139182   if( bDirtyHint && rc==SQLITE_OK ){
 139183     rc = fts3IncrmergeHintStore(p, &hint);
 139186   sqlite3_free(pWriter);
 139187   sqlite3_free(hint.a);
 139188   return rc;
 139192 ** Convert the text beginning at *pz into an integer and return
 139193 ** its value.  Advance *pz to point to the first character past
 139194 ** the integer.
 139196 static int fts3Getint(const char **pz){
 139197   const char *z = *pz;
 139198   int i = 0;
 139199   while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
 139200   *pz = z;
 139201   return i;
 139205 ** Process statements of the form:
 139207 **    INSERT INTO table(table) VALUES('merge=A,B');
 139209 ** A and B are integers that decode to be the number of leaf pages
 139210 ** written for the merge, and the minimum number of segments on a level
 139211 ** before it will be selected for a merge, respectively.
 139213 static int fts3DoIncrmerge(
 139214   Fts3Table *p,                   /* FTS3 table handle */
 139215   const char *zParam              /* Nul-terminated string containing "A,B" */
 139217   int rc;
 139218   int nMin = (FTS3_MERGE_COUNT / 2);
 139219   int nMerge = 0;
 139220   const char *z = zParam;
 139222   /* Read the first integer value */
 139223   nMerge = fts3Getint(&z);
 139225   /* If the first integer value is followed by a ',',  read the second
 139226   ** integer value. */
 139227   if( z[0]==',' && z[1]!='\0' ){
 139228     z++;
 139229     nMin = fts3Getint(&z);
 139232   if( z[0]!='\0' || nMin<2 ){
 139233     rc = SQLITE_ERROR;
 139234   }else{
 139235     rc = SQLITE_OK;
 139236     if( !p->bHasStat ){
 139237       assert( p->bFts4==0 );
 139238       sqlite3Fts3CreateStatTable(&rc, p);
 139240     if( rc==SQLITE_OK ){
 139241       rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
 139243     sqlite3Fts3SegmentsClose(p);
 139245   return rc;
 139249 ** Process statements of the form:
 139251 **    INSERT INTO table(table) VALUES('automerge=X');
 139253 ** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
 139254 ** turn it on.  The setting is persistent.
 139256 static int fts3DoAutoincrmerge(
 139257   Fts3Table *p,                   /* FTS3 table handle */
 139258   const char *zParam              /* Nul-terminated string containing boolean */
 139260   int rc = SQLITE_OK;
 139261   sqlite3_stmt *pStmt = 0;
 139262   p->bAutoincrmerge = fts3Getint(&zParam)!=0;
 139263   if( !p->bHasStat ){
 139264     assert( p->bFts4==0 );
 139265     sqlite3Fts3CreateStatTable(&rc, p);
 139266     if( rc ) return rc;
 139268   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
 139269   if( rc ) return rc;
 139270   sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
 139271   sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
 139272   sqlite3_step(pStmt);
 139273   rc = sqlite3_reset(pStmt);
 139274   return rc;
 139278 ** Return a 64-bit checksum for the FTS index entry specified by the
 139279 ** arguments to this function.
 139281 static u64 fts3ChecksumEntry(
 139282   const char *zTerm,              /* Pointer to buffer containing term */
 139283   int nTerm,                      /* Size of zTerm in bytes */
 139284   int iLangid,                    /* Language id for current row */
 139285   int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
 139286   i64 iDocid,                     /* Docid for current row. */
 139287   int iCol,                       /* Column number */
 139288   int iPos                        /* Position */
 139290   int i;
 139291   u64 ret = (u64)iDocid;
 139293   ret += (ret<<3) + iLangid;
 139294   ret += (ret<<3) + iIndex;
 139295   ret += (ret<<3) + iCol;
 139296   ret += (ret<<3) + iPos;
 139297   for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
 139299   return ret;
 139303 ** Return a checksum of all entries in the FTS index that correspond to
 139304 ** language id iLangid. The checksum is calculated by XORing the checksums
 139305 ** of each individual entry (see fts3ChecksumEntry()) together.
 139307 ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
 139308 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
 139309 ** return value is undefined in this case.
 139311 static u64 fts3ChecksumIndex(
 139312   Fts3Table *p,                   /* FTS3 table handle */
 139313   int iLangid,                    /* Language id to return cksum for */
 139314   int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
 139315   int *pRc                        /* OUT: Return code */
 139317   Fts3SegFilter filter;
 139318   Fts3MultiSegReader csr;
 139319   int rc;
 139320   u64 cksum = 0;
 139322   assert( *pRc==SQLITE_OK );
 139324   memset(&filter, 0, sizeof(filter));
 139325   memset(&csr, 0, sizeof(csr));
 139326   filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
 139327   filter.flags |= FTS3_SEGMENT_SCAN;
 139329   rc = sqlite3Fts3SegReaderCursor(
 139330       p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
 139332   if( rc==SQLITE_OK ){
 139333     rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
 139336   if( rc==SQLITE_OK ){
 139337     while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
 139338       char *pCsr = csr.aDoclist;
 139339       char *pEnd = &pCsr[csr.nDoclist];
 139341       i64 iDocid = 0;
 139342       i64 iCol = 0;
 139343       i64 iPos = 0;
 139345       pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
 139346       while( pCsr<pEnd ){
 139347         i64 iVal = 0;
 139348         pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
 139349         if( pCsr<pEnd ){
 139350           if( iVal==0 || iVal==1 ){
 139351             iCol = 0;
 139352             iPos = 0;
 139353             if( iVal ){
 139354               pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
 139355             }else{
 139356               pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
 139357               iDocid += iVal;
 139359           }else{
 139360             iPos += (iVal - 2);
 139361             cksum = cksum ^ fts3ChecksumEntry(
 139362                 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
 139363                 (int)iCol, (int)iPos
 139370   sqlite3Fts3SegReaderFinish(&csr);
 139372   *pRc = rc;
 139373   return cksum;
 139377 ** Check if the contents of the FTS index match the current contents of the
 139378 ** content table. If no error occurs and the contents do match, set *pbOk
 139379 ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
 139380 ** to false before returning.
 139382 ** If an error occurs (e.g. an OOM or IO error), return an SQLite error 
 139383 ** code. The final value of *pbOk is undefined in this case.
 139385 static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
 139386   int rc = SQLITE_OK;             /* Return code */
 139387   u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
 139388   u64 cksum2 = 0;                 /* Checksum based on %_content contents */
 139389   sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
 139391   /* This block calculates the checksum according to the FTS index. */
 139392   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 139393   if( rc==SQLITE_OK ){
 139394     int rc2;
 139395     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
 139396     while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
 139397       int iLangid = sqlite3_column_int(pAllLangid, 0);
 139398       int i;
 139399       for(i=0; i<p->nIndex; i++){
 139400         cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
 139403     rc2 = sqlite3_reset(pAllLangid);
 139404     if( rc==SQLITE_OK ) rc = rc2;
 139407   /* This block calculates the checksum according to the %_content table */
 139408   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
 139409   if( rc==SQLITE_OK ){
 139410     sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
 139411     sqlite3_stmt *pStmt = 0;
 139412     char *zSql;
 139414     zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
 139415     if( !zSql ){
 139416       rc = SQLITE_NOMEM;
 139417     }else{
 139418       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 139419       sqlite3_free(zSql);
 139422     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
 139423       i64 iDocid = sqlite3_column_int64(pStmt, 0);
 139424       int iLang = langidFromSelect(p, pStmt);
 139425       int iCol;
 139427       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
 139428         const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
 139429         int nText = sqlite3_column_bytes(pStmt, iCol+1);
 139430         sqlite3_tokenizer_cursor *pT = 0;
 139432         rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
 139433         while( rc==SQLITE_OK ){
 139434           char const *zToken;       /* Buffer containing token */
 139435           int nToken = 0;           /* Number of bytes in token */
 139436           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 139437           int iPos = 0;             /* Position of token in zText */
 139439           rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 139440           if( rc==SQLITE_OK ){
 139441             int i;
 139442             cksum2 = cksum2 ^ fts3ChecksumEntry(
 139443                 zToken, nToken, iLang, 0, iDocid, iCol, iPos
 139445             for(i=1; i<p->nIndex; i++){
 139446               if( p->aIndex[i].nPrefix<=nToken ){
 139447                 cksum2 = cksum2 ^ fts3ChecksumEntry(
 139448                   zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
 139454         if( pT ) pModule->xClose(pT);
 139455         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 139459     sqlite3_finalize(pStmt);
 139462   *pbOk = (cksum1==cksum2);
 139463   return rc;
 139467 ** Run the integrity-check. If no error occurs and the current contents of
 139468 ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
 139469 ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
 139471 ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite 
 139472 ** error code.
 139474 ** The integrity-check works as follows. For each token and indexed token
 139475 ** prefix in the document set, a 64-bit checksum is calculated (by code
 139476 ** in fts3ChecksumEntry()) based on the following:
 139478 **     + The index number (0 for the main index, 1 for the first prefix
 139479 **       index etc.),
 139480 **     + The token (or token prefix) text itself, 
 139481 **     + The language-id of the row it appears in,
 139482 **     + The docid of the row it appears in,
 139483 **     + The column it appears in, and
 139484 **     + The tokens position within that column.
 139486 ** The checksums for all entries in the index are XORed together to create
 139487 ** a single checksum for the entire index.
 139489 ** The integrity-check code calculates the same checksum in two ways:
 139491 **     1. By scanning the contents of the FTS index, and 
 139492 **     2. By scanning and tokenizing the content table.
 139494 ** If the two checksums are identical, the integrity-check is deemed to have
 139495 ** passed.
 139497 static int fts3DoIntegrityCheck(
 139498   Fts3Table *p                    /* FTS3 table handle */
 139500   int rc;
 139501   int bOk = 0;
 139502   rc = fts3IntegrityCheck(p, &bOk);
 139503   if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
 139504   return rc;
 139508 ** Handle a 'special' INSERT of the form:
 139510 **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
 139512 ** Argument pVal contains the result of <expr>. Currently the only 
 139513 ** meaningful value to insert is the text 'optimize'.
 139515 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
 139516   int rc;                         /* Return Code */
 139517   const char *zVal = (const char *)sqlite3_value_text(pVal);
 139518   int nVal = sqlite3_value_bytes(pVal);
 139520   if( !zVal ){
 139521     return SQLITE_NOMEM;
 139522   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
 139523     rc = fts3DoOptimize(p, 0);
 139524   }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
 139525     rc = fts3DoRebuild(p);
 139526   }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
 139527     rc = fts3DoIntegrityCheck(p);
 139528   }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
 139529     rc = fts3DoIncrmerge(p, &zVal[6]);
 139530   }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
 139531     rc = fts3DoAutoincrmerge(p, &zVal[10]);
 139532 #ifdef SQLITE_TEST
 139533   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
 139534     p->nNodeSize = atoi(&zVal[9]);
 139535     rc = SQLITE_OK;
 139536   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
 139537     p->nMaxPendingData = atoi(&zVal[11]);
 139538     rc = SQLITE_OK;
 139539   }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
 139540     p->bNoIncrDoclist = atoi(&zVal[21]);
 139541     rc = SQLITE_OK;
 139542 #endif
 139543   }else{
 139544     rc = SQLITE_ERROR;
 139547   return rc;
 139550 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
 139552 ** Delete all cached deferred doclists. Deferred doclists are cached
 139553 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
 139555 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
 139556   Fts3DeferredToken *pDef;
 139557   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
 139558     fts3PendingListDelete(pDef->pList);
 139559     pDef->pList = 0;
 139564 ** Free all entries in the pCsr->pDeffered list. Entries are added to 
 139565 ** this list using sqlite3Fts3DeferToken().
 139567 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
 139568   Fts3DeferredToken *pDef;
 139569   Fts3DeferredToken *pNext;
 139570   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
 139571     pNext = pDef->pNext;
 139572     fts3PendingListDelete(pDef->pList);
 139573     sqlite3_free(pDef);
 139575   pCsr->pDeferred = 0;
 139579 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
 139580 ** based on the row that pCsr currently points to.
 139582 ** A deferred-doclist is like any other doclist with position information
 139583 ** included, except that it only contains entries for a single row of the
 139584 ** table, not for all rows.
 139586 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
 139587   int rc = SQLITE_OK;             /* Return code */
 139588   if( pCsr->pDeferred ){
 139589     int i;                        /* Used to iterate through table columns */
 139590     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
 139591     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
 139593     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
 139594     sqlite3_tokenizer *pT = p->pTokenizer;
 139595     sqlite3_tokenizer_module const *pModule = pT->pModule;
 139597     assert( pCsr->isRequireSeek==0 );
 139598     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
 139600     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
 139601       if( p->abNotindexed[i]==0 ){
 139602         const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
 139603         sqlite3_tokenizer_cursor *pTC = 0;
 139605         rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
 139606         while( rc==SQLITE_OK ){
 139607           char const *zToken;       /* Buffer containing token */
 139608           int nToken = 0;           /* Number of bytes in token */
 139609           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
 139610           int iPos = 0;             /* Position of token in zText */
 139612           rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
 139613           for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
 139614             Fts3PhraseToken *pPT = pDef->pToken;
 139615             if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
 139616                 && (pPT->bFirst==0 || iPos==0)
 139617                 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
 139618                 && (0==memcmp(zToken, pPT->z, pPT->n))
 139620               fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
 139624         if( pTC ) pModule->xClose(pTC);
 139625         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
 139629     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
 139630       if( pDef->pList ){
 139631         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
 139636   return rc;
 139639 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
 139640   Fts3DeferredToken *p, 
 139641   char **ppData, 
 139642   int *pnData
 139644   char *pRet;
 139645   int nSkip;
 139646   sqlite3_int64 dummy;
 139648   *ppData = 0;
 139649   *pnData = 0;
 139651   if( p->pList==0 ){
 139652     return SQLITE_OK;
 139655   pRet = (char *)sqlite3_malloc(p->pList->nData);
 139656   if( !pRet ) return SQLITE_NOMEM;
 139658   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
 139659   *pnData = p->pList->nData - nSkip;
 139660   *ppData = pRet;
 139662   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
 139663   return SQLITE_OK;
 139667 ** Add an entry for token pToken to the pCsr->pDeferred list.
 139669 SQLITE_PRIVATE int sqlite3Fts3DeferToken(
 139670   Fts3Cursor *pCsr,               /* Fts3 table cursor */
 139671   Fts3PhraseToken *pToken,        /* Token to defer */
 139672   int iCol                        /* Column that token must appear in (or -1) */
 139674   Fts3DeferredToken *pDeferred;
 139675   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
 139676   if( !pDeferred ){
 139677     return SQLITE_NOMEM;
 139679   memset(pDeferred, 0, sizeof(*pDeferred));
 139680   pDeferred->pToken = pToken;
 139681   pDeferred->pNext = pCsr->pDeferred; 
 139682   pDeferred->iCol = iCol;
 139683   pCsr->pDeferred = pDeferred;
 139685   assert( pToken->pDeferred==0 );
 139686   pToken->pDeferred = pDeferred;
 139688   return SQLITE_OK;
 139690 #endif
 139693 ** SQLite value pRowid contains the rowid of a row that may or may not be
 139694 ** present in the FTS3 table. If it is, delete it and adjust the contents
 139695 ** of subsiduary data structures accordingly.
 139697 static int fts3DeleteByRowid(
 139698   Fts3Table *p, 
 139699   sqlite3_value *pRowid, 
 139700   int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
 139701   u32 *aSzDel
 139703   int rc = SQLITE_OK;             /* Return code */
 139704   int bFound = 0;                 /* True if *pRowid really is in the table */
 139706   fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
 139707   if( bFound && rc==SQLITE_OK ){
 139708     int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
 139709     rc = fts3IsEmpty(p, pRowid, &isEmpty);
 139710     if( rc==SQLITE_OK ){
 139711       if( isEmpty ){
 139712         /* Deleting this row means the whole table is empty. In this case
 139713         ** delete the contents of all three tables and throw away any
 139714         ** data in the pendingTerms hash table.  */
 139715         rc = fts3DeleteAll(p, 1);
 139716         *pnChng = 0;
 139717         memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
 139718       }else{
 139719         *pnChng = *pnChng - 1;
 139720         if( p->zContentTbl==0 ){
 139721           fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
 139723         if( p->bHasDocsize ){
 139724           fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
 139730   return rc;
 139734 ** This function does the work for the xUpdate method of FTS3 virtual
 139735 ** tables. The schema of the virtual table being:
 139737 **     CREATE TABLE <table name>( 
 139738 **       <user columns>,
 139739 **       <table name> HIDDEN, 
 139740 **       docid HIDDEN, 
 139741 **       <langid> HIDDEN
 139742 **     );
 139746 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
 139747   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
 139748   int nArg,                       /* Size of argument array */
 139749   sqlite3_value **apVal,          /* Array of arguments */
 139750   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
 139752   Fts3Table *p = (Fts3Table *)pVtab;
 139753   int rc = SQLITE_OK;             /* Return Code */
 139754   int isRemove = 0;               /* True for an UPDATE or DELETE */
 139755   u32 *aSzIns = 0;                /* Sizes of inserted documents */
 139756   u32 *aSzDel = 0;                /* Sizes of deleted documents */
 139757   int nChng = 0;                  /* Net change in number of documents */
 139758   int bInsertDone = 0;
 139760   assert( p->pSegments==0 );
 139761   assert( 
 139762       nArg==1                     /* DELETE operations */
 139763    || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
 139766   /* Check for a "special" INSERT operation. One of the form:
 139768   **   INSERT INTO xyz(xyz) VALUES('command');
 139770   if( nArg>1 
 139771    && sqlite3_value_type(apVal[0])==SQLITE_NULL 
 139772    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL 
 139774     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
 139775     goto update_out;
 139778   if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
 139779     rc = SQLITE_CONSTRAINT;
 139780     goto update_out;
 139783   /* Allocate space to hold the change in document sizes */
 139784   aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
 139785   if( aSzDel==0 ){
 139786     rc = SQLITE_NOMEM;
 139787     goto update_out;
 139789   aSzIns = &aSzDel[p->nColumn+1];
 139790   memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
 139792   rc = fts3Writelock(p);
 139793   if( rc!=SQLITE_OK ) goto update_out;
 139795   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
 139796   ** value, then this operation requires constraint handling.
 139798   ** If the on-conflict mode is REPLACE, this means that the existing row
 139799   ** should be deleted from the database before inserting the new row. Or,
 139800   ** if the on-conflict mode is other than REPLACE, then this method must
 139801   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
 139802   ** modify the database file.
 139804   if( nArg>1 && p->zContentTbl==0 ){
 139805     /* Find the value object that holds the new rowid value. */
 139806     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
 139807     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
 139808       pNewRowid = apVal[1];
 139811     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
 139812         sqlite3_value_type(apVal[0])==SQLITE_NULL
 139813      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
 139814     )){
 139815       /* The new rowid is not NULL (in this case the rowid will be
 139816       ** automatically assigned and there is no chance of a conflict), and 
 139817       ** the statement is either an INSERT or an UPDATE that modifies the
 139818       ** rowid column. So if the conflict mode is REPLACE, then delete any
 139819       ** existing row with rowid=pNewRowid. 
 139821       ** Or, if the conflict mode is not REPLACE, insert the new record into 
 139822       ** the %_content table. If we hit the duplicate rowid constraint (or any
 139823       ** other error) while doing so, return immediately.
 139825       ** This branch may also run if pNewRowid contains a value that cannot
 139826       ** be losslessly converted to an integer. In this case, the eventual 
 139827       ** call to fts3InsertData() (either just below or further on in this
 139828       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is 
 139829       ** invoked, it will delete zero rows (since no row will have
 139830       ** docid=$pNewRowid if $pNewRowid is not an integer value).
 139832       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
 139833         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
 139834       }else{
 139835         rc = fts3InsertData(p, apVal, pRowid);
 139836         bInsertDone = 1;
 139840   if( rc!=SQLITE_OK ){
 139841     goto update_out;
 139844   /* If this is a DELETE or UPDATE operation, remove the old record. */
 139845   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
 139846     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
 139847     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
 139848     isRemove = 1;
 139851   /* If this is an INSERT or UPDATE operation, insert the new record. */
 139852   if( nArg>1 && rc==SQLITE_OK ){
 139853     int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
 139854     if( bInsertDone==0 ){
 139855       rc = fts3InsertData(p, apVal, pRowid);
 139856       if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
 139857         rc = FTS_CORRUPT_VTAB;
 139860     if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
 139861       rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
 139863     if( rc==SQLITE_OK ){
 139864       assert( p->iPrevDocid==*pRowid );
 139865       rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
 139867     if( p->bHasDocsize ){
 139868       fts3InsertDocsize(&rc, p, aSzIns);
 139870     nChng++;
 139873   if( p->bFts4 ){
 139874     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
 139877  update_out:
 139878   sqlite3_free(aSzDel);
 139879   sqlite3Fts3SegmentsClose(p);
 139880   return rc;
 139884 ** Flush any data in the pending-terms hash table to disk. If successful,
 139885 ** merge all segments in the database (including the new segment, if 
 139886 ** there was any data to flush) into a single segment. 
 139888 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
 139889   int rc;
 139890   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
 139891   if( rc==SQLITE_OK ){
 139892     rc = fts3DoOptimize(p, 1);
 139893     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
 139894       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
 139895       if( rc2!=SQLITE_OK ) rc = rc2;
 139896     }else{
 139897       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
 139898       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
 139901   sqlite3Fts3SegmentsClose(p);
 139902   return rc;
 139905 #endif
 139907 /************** End of fts3_write.c ******************************************/
 139908 /************** Begin file fts3_snippet.c ************************************/
 139910 ** 2009 Oct 23
 139912 ** The author disclaims copyright to this source code.  In place of
 139913 ** a legal notice, here is a blessing:
 139915 **    May you do good and not evil.
 139916 **    May you find forgiveness for yourself and forgive others.
 139917 **    May you share freely, never taking more than you give.
 139919 ******************************************************************************
 139922 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 139924 /* #include <string.h> */
 139925 /* #include <assert.h> */
 139928 ** Characters that may appear in the second argument to matchinfo().
 139930 #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
 139931 #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
 139932 #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
 139933 #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
 139934 #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
 139935 #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
 139936 #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
 139939 ** The default value for the second argument to matchinfo(). 
 139941 #define FTS3_MATCHINFO_DEFAULT   "pcx"
 139945 ** Used as an fts3ExprIterate() context when loading phrase doclists to
 139946 ** Fts3Expr.aDoclist[]/nDoclist.
 139948 typedef struct LoadDoclistCtx LoadDoclistCtx;
 139949 struct LoadDoclistCtx {
 139950   Fts3Cursor *pCsr;               /* FTS3 Cursor */
 139951   int nPhrase;                    /* Number of phrases seen so far */
 139952   int nToken;                     /* Number of tokens seen so far */
 139956 ** The following types are used as part of the implementation of the 
 139957 ** fts3BestSnippet() routine.
 139959 typedef struct SnippetIter SnippetIter;
 139960 typedef struct SnippetPhrase SnippetPhrase;
 139961 typedef struct SnippetFragment SnippetFragment;
 139963 struct SnippetIter {
 139964   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
 139965   int iCol;                       /* Extract snippet from this column */
 139966   int nSnippet;                   /* Requested snippet length (in tokens) */
 139967   int nPhrase;                    /* Number of phrases in query */
 139968   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
 139969   int iCurrent;                   /* First token of current snippet */
 139972 struct SnippetPhrase {
 139973   int nToken;                     /* Number of tokens in phrase */
 139974   char *pList;                    /* Pointer to start of phrase position list */
 139975   int iHead;                      /* Next value in position list */
 139976   char *pHead;                    /* Position list data following iHead */
 139977   int iTail;                      /* Next value in trailing position list */
 139978   char *pTail;                    /* Position list data following iTail */
 139981 struct SnippetFragment {
 139982   int iCol;                       /* Column snippet is extracted from */
 139983   int iPos;                       /* Index of first token in snippet */
 139984   u64 covered;                    /* Mask of query phrases covered */
 139985   u64 hlmask;                     /* Mask of snippet terms to highlight */
 139989 ** This type is used as an fts3ExprIterate() context object while 
 139990 ** accumulating the data returned by the matchinfo() function.
 139992 typedef struct MatchInfo MatchInfo;
 139993 struct MatchInfo {
 139994   Fts3Cursor *pCursor;            /* FTS3 Cursor */
 139995   int nCol;                       /* Number of columns in table */
 139996   int nPhrase;                    /* Number of matchable phrases in query */
 139997   sqlite3_int64 nDoc;             /* Number of docs in database */
 139998   u32 *aMatchinfo;                /* Pre-allocated buffer */
 140004 ** The snippet() and offsets() functions both return text values. An instance
 140005 ** of the following structure is used to accumulate those values while the
 140006 ** functions are running. See fts3StringAppend() for details.
 140008 typedef struct StrBuffer StrBuffer;
 140009 struct StrBuffer {
 140010   char *z;                        /* Pointer to buffer containing string */
 140011   int n;                          /* Length of z in bytes (excl. nul-term) */
 140012   int nAlloc;                     /* Allocated size of buffer z in bytes */
 140017 ** This function is used to help iterate through a position-list. A position
 140018 ** list is a list of unique integers, sorted from smallest to largest. Each
 140019 ** element of the list is represented by an FTS3 varint that takes the value
 140020 ** of the difference between the current element and the previous one plus
 140021 ** two. For example, to store the position-list:
 140023 **     4 9 113
 140025 ** the three varints:
 140027 **     6 7 106
 140029 ** are encoded.
 140031 ** When this function is called, *pp points to the start of an element of
 140032 ** the list. *piPos contains the value of the previous entry in the list.
 140033 ** After it returns, *piPos contains the value of the next element of the
 140034 ** list and *pp is advanced to the following varint.
 140036 static void fts3GetDeltaPosition(char **pp, int *piPos){
 140037   int iVal;
 140038   *pp += fts3GetVarint32(*pp, &iVal);
 140039   *piPos += (iVal-2);
 140043 ** Helper function for fts3ExprIterate() (see below).
 140045 static int fts3ExprIterate2(
 140046   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
 140047   int *piPhrase,                  /* Pointer to phrase counter */
 140048   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
 140049   void *pCtx                      /* Second argument to pass to callback */
 140051   int rc;                         /* Return code */
 140052   int eType = pExpr->eType;       /* Type of expression node pExpr */
 140054   if( eType!=FTSQUERY_PHRASE ){
 140055     assert( pExpr->pLeft && pExpr->pRight );
 140056     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
 140057     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
 140058       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
 140060   }else{
 140061     rc = x(pExpr, *piPhrase, pCtx);
 140062     (*piPhrase)++;
 140064   return rc;
 140068 ** Iterate through all phrase nodes in an FTS3 query, except those that
 140069 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
 140070 ** For each phrase node found, the supplied callback function is invoked.
 140072 ** If the callback function returns anything other than SQLITE_OK, 
 140073 ** the iteration is abandoned and the error code returned immediately.
 140074 ** Otherwise, SQLITE_OK is returned after a callback has been made for
 140075 ** all eligible phrase nodes.
 140077 static int fts3ExprIterate(
 140078   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
 140079   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
 140080   void *pCtx                      /* Second argument to pass to callback */
 140082   int iPhrase = 0;                /* Variable used as the phrase counter */
 140083   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
 140087 ** This is an fts3ExprIterate() callback used while loading the doclists
 140088 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
 140089 ** fts3ExprLoadDoclists().
 140091 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
 140092   int rc = SQLITE_OK;
 140093   Fts3Phrase *pPhrase = pExpr->pPhrase;
 140094   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
 140096   UNUSED_PARAMETER(iPhrase);
 140098   p->nPhrase++;
 140099   p->nToken += pPhrase->nToken;
 140101   return rc;
 140105 ** Load the doclists for each phrase in the query associated with FTS3 cursor
 140106 ** pCsr. 
 140108 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
 140109 ** phrases in the expression (all phrases except those directly or 
 140110 ** indirectly descended from the right-hand-side of a NOT operator). If 
 140111 ** pnToken is not NULL, then it is set to the number of tokens in all
 140112 ** matchable phrases of the expression.
 140114 static int fts3ExprLoadDoclists(
 140115   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
 140116   int *pnPhrase,                  /* OUT: Number of phrases in query */
 140117   int *pnToken                    /* OUT: Number of tokens in query */
 140119   int rc;                         /* Return Code */
 140120   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
 140121   sCtx.pCsr = pCsr;
 140122   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
 140123   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
 140124   if( pnToken ) *pnToken = sCtx.nToken;
 140125   return rc;
 140128 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
 140129   (*(int *)ctx)++;
 140130   UNUSED_PARAMETER(pExpr);
 140131   UNUSED_PARAMETER(iPhrase);
 140132   return SQLITE_OK;
 140134 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
 140135   int nPhrase = 0;
 140136   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
 140137   return nPhrase;
 140141 ** Advance the position list iterator specified by the first two 
 140142 ** arguments so that it points to the first element with a value greater
 140143 ** than or equal to parameter iNext.
 140145 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
 140146   char *pIter = *ppIter;
 140147   if( pIter ){
 140148     int iIter = *piIter;
 140150     while( iIter<iNext ){
 140151       if( 0==(*pIter & 0xFE) ){
 140152         iIter = -1;
 140153         pIter = 0;
 140154         break;
 140156       fts3GetDeltaPosition(&pIter, &iIter);
 140159     *piIter = iIter;
 140160     *ppIter = pIter;
 140165 ** Advance the snippet iterator to the next candidate snippet.
 140167 static int fts3SnippetNextCandidate(SnippetIter *pIter){
 140168   int i;                          /* Loop counter */
 140170   if( pIter->iCurrent<0 ){
 140171     /* The SnippetIter object has just been initialized. The first snippet
 140172     ** candidate always starts at offset 0 (even if this candidate has a
 140173     ** score of 0.0).
 140175     pIter->iCurrent = 0;
 140177     /* Advance the 'head' iterator of each phrase to the first offset that
 140178     ** is greater than or equal to (iNext+nSnippet).
 140180     for(i=0; i<pIter->nPhrase; i++){
 140181       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 140182       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
 140184   }else{
 140185     int iStart;
 140186     int iEnd = 0x7FFFFFFF;
 140188     for(i=0; i<pIter->nPhrase; i++){
 140189       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 140190       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
 140191         iEnd = pPhrase->iHead;
 140194     if( iEnd==0x7FFFFFFF ){
 140195       return 1;
 140198     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
 140199     for(i=0; i<pIter->nPhrase; i++){
 140200       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 140201       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
 140202       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
 140206   return 0;
 140210 ** Retrieve information about the current candidate snippet of snippet 
 140211 ** iterator pIter.
 140213 static void fts3SnippetDetails(
 140214   SnippetIter *pIter,             /* Snippet iterator */
 140215   u64 mCovered,                   /* Bitmask of phrases already covered */
 140216   int *piToken,                   /* OUT: First token of proposed snippet */
 140217   int *piScore,                   /* OUT: "Score" for this snippet */
 140218   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
 140219   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
 140221   int iStart = pIter->iCurrent;   /* First token of snippet */
 140222   int iScore = 0;                 /* Score of this snippet */
 140223   int i;                          /* Loop counter */
 140224   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
 140225   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
 140227   for(i=0; i<pIter->nPhrase; i++){
 140228     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
 140229     if( pPhrase->pTail ){
 140230       char *pCsr = pPhrase->pTail;
 140231       int iCsr = pPhrase->iTail;
 140233       while( iCsr<(iStart+pIter->nSnippet) ){
 140234         int j;
 140235         u64 mPhrase = (u64)1 << i;
 140236         u64 mPos = (u64)1 << (iCsr - iStart);
 140237         assert( iCsr>=iStart );
 140238         if( (mCover|mCovered)&mPhrase ){
 140239           iScore++;
 140240         }else{
 140241           iScore += 1000;
 140243         mCover |= mPhrase;
 140245         for(j=0; j<pPhrase->nToken; j++){
 140246           mHighlight |= (mPos>>j);
 140249         if( 0==(*pCsr & 0x0FE) ) break;
 140250         fts3GetDeltaPosition(&pCsr, &iCsr);
 140255   /* Set the output variables before returning. */
 140256   *piToken = iStart;
 140257   *piScore = iScore;
 140258   *pmCover = mCover;
 140259   *pmHighlight = mHighlight;
 140263 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
 140264 ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
 140266 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
 140267   SnippetIter *p = (SnippetIter *)ctx;
 140268   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
 140269   char *pCsr;
 140270   int rc;
 140272   pPhrase->nToken = pExpr->pPhrase->nToken;
 140273   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
 140274   assert( rc==SQLITE_OK || pCsr==0 );
 140275   if( pCsr ){
 140276     int iFirst = 0;
 140277     pPhrase->pList = pCsr;
 140278     fts3GetDeltaPosition(&pCsr, &iFirst);
 140279     assert( iFirst>=0 );
 140280     pPhrase->pHead = pCsr;
 140281     pPhrase->pTail = pCsr;
 140282     pPhrase->iHead = iFirst;
 140283     pPhrase->iTail = iFirst;
 140284   }else{
 140285     assert( rc!=SQLITE_OK || (
 140286        pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
 140287     ));
 140290   return rc;
 140294 ** Select the fragment of text consisting of nFragment contiguous tokens 
 140295 ** from column iCol that represent the "best" snippet. The best snippet
 140296 ** is the snippet with the highest score, where scores are calculated
 140297 ** by adding:
 140299 **   (a) +1 point for each occurrence of a matchable phrase in the snippet.
 140301 **   (b) +1000 points for the first occurrence of each matchable phrase in 
 140302 **       the snippet for which the corresponding mCovered bit is not set.
 140304 ** The selected snippet parameters are stored in structure *pFragment before
 140305 ** returning. The score of the selected snippet is stored in *piScore
 140306 ** before returning.
 140308 static int fts3BestSnippet(
 140309   int nSnippet,                   /* Desired snippet length */
 140310   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
 140311   int iCol,                       /* Index of column to create snippet from */
 140312   u64 mCovered,                   /* Mask of phrases already covered */
 140313   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
 140314   SnippetFragment *pFragment,     /* OUT: Best snippet found */
 140315   int *piScore                    /* OUT: Score of snippet pFragment */
 140317   int rc;                         /* Return Code */
 140318   int nList;                      /* Number of phrases in expression */
 140319   SnippetIter sIter;              /* Iterates through snippet candidates */
 140320   int nByte;                      /* Number of bytes of space to allocate */
 140321   int iBestScore = -1;            /* Best snippet score found so far */
 140322   int i;                          /* Loop counter */
 140324   memset(&sIter, 0, sizeof(sIter));
 140326   /* Iterate through the phrases in the expression to count them. The same
 140327   ** callback makes sure the doclists are loaded for each phrase.
 140329   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
 140330   if( rc!=SQLITE_OK ){
 140331     return rc;
 140334   /* Now that it is known how many phrases there are, allocate and zero
 140335   ** the required space using malloc().
 140337   nByte = sizeof(SnippetPhrase) * nList;
 140338   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
 140339   if( !sIter.aPhrase ){
 140340     return SQLITE_NOMEM;
 140342   memset(sIter.aPhrase, 0, nByte);
 140344   /* Initialize the contents of the SnippetIter object. Then iterate through
 140345   ** the set of phrases in the expression to populate the aPhrase[] array.
 140347   sIter.pCsr = pCsr;
 140348   sIter.iCol = iCol;
 140349   sIter.nSnippet = nSnippet;
 140350   sIter.nPhrase = nList;
 140351   sIter.iCurrent = -1;
 140352   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
 140354   /* Set the *pmSeen output variable. */
 140355   for(i=0; i<nList; i++){
 140356     if( sIter.aPhrase[i].pHead ){
 140357       *pmSeen |= (u64)1 << i;
 140361   /* Loop through all candidate snippets. Store the best snippet in 
 140362   ** *pFragment. Store its associated 'score' in iBestScore.
 140364   pFragment->iCol = iCol;
 140365   while( !fts3SnippetNextCandidate(&sIter) ){
 140366     int iPos;
 140367     int iScore;
 140368     u64 mCover;
 140369     u64 mHighlight;
 140370     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
 140371     assert( iScore>=0 );
 140372     if( iScore>iBestScore ){
 140373       pFragment->iPos = iPos;
 140374       pFragment->hlmask = mHighlight;
 140375       pFragment->covered = mCover;
 140376       iBestScore = iScore;
 140380   sqlite3_free(sIter.aPhrase);
 140381   *piScore = iBestScore;
 140382   return SQLITE_OK;
 140387 ** Append a string to the string-buffer passed as the first argument.
 140389 ** If nAppend is negative, then the length of the string zAppend is
 140390 ** determined using strlen().
 140392 static int fts3StringAppend(
 140393   StrBuffer *pStr,                /* Buffer to append to */
 140394   const char *zAppend,            /* Pointer to data to append to buffer */
 140395   int nAppend                     /* Size of zAppend in bytes (or -1) */
 140397   if( nAppend<0 ){
 140398     nAppend = (int)strlen(zAppend);
 140401   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
 140402   ** to grow the buffer until so that it is big enough to accomadate the
 140403   ** appended data.
 140405   if( pStr->n+nAppend+1>=pStr->nAlloc ){
 140406     int nAlloc = pStr->nAlloc+nAppend+100;
 140407     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
 140408     if( !zNew ){
 140409       return SQLITE_NOMEM;
 140411     pStr->z = zNew;
 140412     pStr->nAlloc = nAlloc;
 140414   assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
 140416   /* Append the data to the string buffer. */
 140417   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
 140418   pStr->n += nAppend;
 140419   pStr->z[pStr->n] = '\0';
 140421   return SQLITE_OK;
 140425 ** The fts3BestSnippet() function often selects snippets that end with a
 140426 ** query term. That is, the final term of the snippet is always a term
 140427 ** that requires highlighting. For example, if 'X' is a highlighted term
 140428 ** and '.' is a non-highlighted term, BestSnippet() may select:
 140430 **     ........X.....X
 140432 ** This function "shifts" the beginning of the snippet forward in the 
 140433 ** document so that there are approximately the same number of 
 140434 ** non-highlighted terms to the right of the final highlighted term as there
 140435 ** are to the left of the first highlighted term. For example, to this:
 140437 **     ....X.....X....
 140439 ** This is done as part of extracting the snippet text, not when selecting
 140440 ** the snippet. Snippet selection is done based on doclists only, so there
 140441 ** is no way for fts3BestSnippet() to know whether or not the document 
 140442 ** actually contains terms that follow the final highlighted term. 
 140444 static int fts3SnippetShift(
 140445   Fts3Table *pTab,                /* FTS3 table snippet comes from */
 140446   int iLangid,                    /* Language id to use in tokenizing */
 140447   int nSnippet,                   /* Number of tokens desired for snippet */
 140448   const char *zDoc,               /* Document text to extract snippet from */
 140449   int nDoc,                       /* Size of buffer zDoc in bytes */
 140450   int *piPos,                     /* IN/OUT: First token of snippet */
 140451   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
 140453   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
 140455   if( hlmask ){
 140456     int nLeft;                    /* Tokens to the left of first highlight */
 140457     int nRight;                   /* Tokens to the right of last highlight */
 140458     int nDesired;                 /* Ideal number of tokens to shift forward */
 140460     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
 140461     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
 140462     nDesired = (nLeft-nRight)/2;
 140464     /* Ideally, the start of the snippet should be pushed forward in the
 140465     ** document nDesired tokens. This block checks if there are actually
 140466     ** nDesired tokens to the right of the snippet. If so, *piPos and
 140467     ** *pHlMask are updated to shift the snippet nDesired tokens to the
 140468     ** right. Otherwise, the snippet is shifted by the number of tokens
 140469     ** available.
 140471     if( nDesired>0 ){
 140472       int nShift;                 /* Number of tokens to shift snippet by */
 140473       int iCurrent = 0;           /* Token counter */
 140474       int rc;                     /* Return Code */
 140475       sqlite3_tokenizer_module *pMod;
 140476       sqlite3_tokenizer_cursor *pC;
 140477       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
 140479       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
 140480       ** or more tokens in zDoc/nDoc.
 140482       rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
 140483       if( rc!=SQLITE_OK ){
 140484         return rc;
 140486       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
 140487         const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
 140488         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
 140490       pMod->xClose(pC);
 140491       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
 140493       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
 140494       assert( nShift<=nDesired );
 140495       if( nShift>0 ){
 140496         *piPos += nShift;
 140497         *pHlmask = hlmask >> nShift;
 140501   return SQLITE_OK;
 140505 ** Extract the snippet text for fragment pFragment from cursor pCsr and
 140506 ** append it to string buffer pOut.
 140508 static int fts3SnippetText(
 140509   Fts3Cursor *pCsr,               /* FTS3 Cursor */
 140510   SnippetFragment *pFragment,     /* Snippet to extract */
 140511   int iFragment,                  /* Fragment number */
 140512   int isLast,                     /* True for final fragment in snippet */
 140513   int nSnippet,                   /* Number of tokens in extracted snippet */
 140514   const char *zOpen,              /* String inserted before highlighted term */
 140515   const char *zClose,             /* String inserted after highlighted term */
 140516   const char *zEllipsis,          /* String inserted between snippets */
 140517   StrBuffer *pOut                 /* Write output here */
 140519   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 140520   int rc;                         /* Return code */
 140521   const char *zDoc;               /* Document text to extract snippet from */
 140522   int nDoc;                       /* Size of zDoc in bytes */
 140523   int iCurrent = 0;               /* Current token number of document */
 140524   int iEnd = 0;                   /* Byte offset of end of current token */
 140525   int isShiftDone = 0;            /* True after snippet is shifted */
 140526   int iPos = pFragment->iPos;     /* First token of snippet */
 140527   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
 140528   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
 140529   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
 140530   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
 140532   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
 140533   if( zDoc==0 ){
 140534     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
 140535       return SQLITE_NOMEM;
 140537     return SQLITE_OK;
 140539   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
 140541   /* Open a token cursor on the document. */
 140542   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
 140543   rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
 140544   if( rc!=SQLITE_OK ){
 140545     return rc;
 140548   while( rc==SQLITE_OK ){
 140549     const char *ZDUMMY;           /* Dummy argument used with tokenizer */
 140550     int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
 140551     int iBegin = 0;               /* Offset in zDoc of start of token */
 140552     int iFin = 0;                 /* Offset in zDoc of end of token */
 140553     int isHighlight = 0;          /* True for highlighted terms */
 140555     /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
 140556     ** in the FTS code the variable that the third argument to xNext points to
 140557     ** is initialized to zero before the first (*but not necessarily
 140558     ** subsequent*) call to xNext(). This is done for a particular application
 140559     ** that needs to know whether or not the tokenizer is being used for
 140560     ** snippet generation or for some other purpose.
 140562     ** Extreme care is required when writing code to depend on this
 140563     ** initialization. It is not a documented part of the tokenizer interface.
 140564     ** If a tokenizer is used directly by any code outside of FTS, this
 140565     ** convention might not be respected.  */
 140566     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
 140567     if( rc!=SQLITE_OK ){
 140568       if( rc==SQLITE_DONE ){
 140569         /* Special case - the last token of the snippet is also the last token
 140570         ** of the column. Append any punctuation that occurred between the end
 140571         ** of the previous token and the end of the document to the output. 
 140572         ** Then break out of the loop. */
 140573         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
 140575       break;
 140577     if( iCurrent<iPos ){ continue; }
 140579     if( !isShiftDone ){
 140580       int n = nDoc - iBegin;
 140581       rc = fts3SnippetShift(
 140582           pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
 140584       isShiftDone = 1;
 140586       /* Now that the shift has been done, check if the initial "..." are
 140587       ** required. They are required if (a) this is not the first fragment,
 140588       ** or (b) this fragment does not begin at position 0 of its column. 
 140590       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
 140591         rc = fts3StringAppend(pOut, zEllipsis, -1);
 140593       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
 140596     if( iCurrent>=(iPos+nSnippet) ){
 140597       if( isLast ){
 140598         rc = fts3StringAppend(pOut, zEllipsis, -1);
 140600       break;
 140603     /* Set isHighlight to true if this term should be highlighted. */
 140604     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
 140606     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
 140607     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
 140608     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
 140609     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
 140611     iEnd = iFin;
 140614   pMod->xClose(pC);
 140615   return rc;
 140620 ** This function is used to count the entries in a column-list (a 
 140621 ** delta-encoded list of term offsets within a single column of a single 
 140622 ** row). When this function is called, *ppCollist should point to the
 140623 ** beginning of the first varint in the column-list (the varint that
 140624 ** contains the position of the first matching term in the column data).
 140625 ** Before returning, *ppCollist is set to point to the first byte after
 140626 ** the last varint in the column-list (either the 0x00 signifying the end
 140627 ** of the position-list, or the 0x01 that precedes the column number of
 140628 ** the next column in the position-list).
 140630 ** The number of elements in the column-list is returned.
 140632 static int fts3ColumnlistCount(char **ppCollist){
 140633   char *pEnd = *ppCollist;
 140634   char c = 0;
 140635   int nEntry = 0;
 140637   /* A column-list is terminated by either a 0x01 or 0x00. */
 140638   while( 0xFE & (*pEnd | c) ){
 140639     c = *pEnd++ & 0x80;
 140640     if( !c ) nEntry++;
 140643   *ppCollist = pEnd;
 140644   return nEntry;
 140648 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
 140649 ** for a single query. 
 140651 ** fts3ExprIterate() callback to load the 'global' elements of a
 140652 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
 140653 ** of the matchinfo array that are constant for all rows returned by the 
 140654 ** current query.
 140656 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
 140657 ** function populates Matchinfo.aMatchinfo[] as follows:
 140659 **   for(iCol=0; iCol<nCol; iCol++){
 140660 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
 140661 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
 140662 **   }
 140664 ** where X is the number of matches for phrase iPhrase is column iCol of all
 140665 ** rows of the table. Y is the number of rows for which column iCol contains
 140666 ** at least one instance of phrase iPhrase.
 140668 ** If the phrase pExpr consists entirely of deferred tokens, then all X and
 140669 ** Y values are set to nDoc, where nDoc is the number of documents in the 
 140670 ** file system. This is done because the full-text index doclist is required
 140671 ** to calculate these values properly, and the full-text index doclist is
 140672 ** not available for deferred tokens.
 140674 static int fts3ExprGlobalHitsCb(
 140675   Fts3Expr *pExpr,                /* Phrase expression node */
 140676   int iPhrase,                    /* Phrase number (numbered from zero) */
 140677   void *pCtx                      /* Pointer to MatchInfo structure */
 140679   MatchInfo *p = (MatchInfo *)pCtx;
 140680   return sqlite3Fts3EvalPhraseStats(
 140681       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
 140686 ** fts3ExprIterate() callback used to collect the "local" part of the
 140687 ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the 
 140688 ** array that are different for each row returned by the query.
 140690 static int fts3ExprLocalHitsCb(
 140691   Fts3Expr *pExpr,                /* Phrase expression node */
 140692   int iPhrase,                    /* Phrase number */
 140693   void *pCtx                      /* Pointer to MatchInfo structure */
 140695   int rc = SQLITE_OK;
 140696   MatchInfo *p = (MatchInfo *)pCtx;
 140697   int iStart = iPhrase * p->nCol * 3;
 140698   int i;
 140700   for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
 140701     char *pCsr;
 140702     rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
 140703     if( pCsr ){
 140704       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
 140705     }else{
 140706       p->aMatchinfo[iStart+i*3] = 0;
 140710   return rc;
 140713 static int fts3MatchinfoCheck(
 140714   Fts3Table *pTab, 
 140715   char cArg,
 140716   char **pzErr
 140718   if( (cArg==FTS3_MATCHINFO_NPHRASE)
 140719    || (cArg==FTS3_MATCHINFO_NCOL)
 140720    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
 140721    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
 140722    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
 140723    || (cArg==FTS3_MATCHINFO_LCS)
 140724    || (cArg==FTS3_MATCHINFO_HITS)
 140726     return SQLITE_OK;
 140728   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
 140729   return SQLITE_ERROR;
 140732 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
 140733   int nVal;                       /* Number of integers output by cArg */
 140735   switch( cArg ){
 140736     case FTS3_MATCHINFO_NDOC:
 140737     case FTS3_MATCHINFO_NPHRASE: 
 140738     case FTS3_MATCHINFO_NCOL: 
 140739       nVal = 1;
 140740       break;
 140742     case FTS3_MATCHINFO_AVGLENGTH:
 140743     case FTS3_MATCHINFO_LENGTH:
 140744     case FTS3_MATCHINFO_LCS:
 140745       nVal = pInfo->nCol;
 140746       break;
 140748     default:
 140749       assert( cArg==FTS3_MATCHINFO_HITS );
 140750       nVal = pInfo->nCol * pInfo->nPhrase * 3;
 140751       break;
 140754   return nVal;
 140757 static int fts3MatchinfoSelectDoctotal(
 140758   Fts3Table *pTab,
 140759   sqlite3_stmt **ppStmt,
 140760   sqlite3_int64 *pnDoc,
 140761   const char **paLen
 140763   sqlite3_stmt *pStmt;
 140764   const char *a;
 140765   sqlite3_int64 nDoc;
 140767   if( !*ppStmt ){
 140768     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
 140769     if( rc!=SQLITE_OK ) return rc;
 140771   pStmt = *ppStmt;
 140772   assert( sqlite3_data_count(pStmt)==1 );
 140774   a = sqlite3_column_blob(pStmt, 0);
 140775   a += sqlite3Fts3GetVarint(a, &nDoc);
 140776   if( nDoc==0 ) return FTS_CORRUPT_VTAB;
 140777   *pnDoc = (u32)nDoc;
 140779   if( paLen ) *paLen = a;
 140780   return SQLITE_OK;
 140784 ** An instance of the following structure is used to store state while 
 140785 ** iterating through a multi-column position-list corresponding to the
 140786 ** hits for a single phrase on a single row in order to calculate the
 140787 ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
 140789 typedef struct LcsIterator LcsIterator;
 140790 struct LcsIterator {
 140791   Fts3Expr *pExpr;                /* Pointer to phrase expression */
 140792   int iPosOffset;                 /* Tokens count up to end of this phrase */
 140793   char *pRead;                    /* Cursor used to iterate through aDoclist */
 140794   int iPos;                       /* Current position */
 140798 ** If LcsIterator.iCol is set to the following value, the iterator has
 140799 ** finished iterating through all offsets for all columns.
 140801 #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
 140803 static int fts3MatchinfoLcsCb(
 140804   Fts3Expr *pExpr,                /* Phrase expression node */
 140805   int iPhrase,                    /* Phrase number (numbered from zero) */
 140806   void *pCtx                      /* Pointer to MatchInfo structure */
 140808   LcsIterator *aIter = (LcsIterator *)pCtx;
 140809   aIter[iPhrase].pExpr = pExpr;
 140810   return SQLITE_OK;
 140814 ** Advance the iterator passed as an argument to the next position. Return
 140815 ** 1 if the iterator is at EOF or if it now points to the start of the
 140816 ** position list for the next column.
 140818 static int fts3LcsIteratorAdvance(LcsIterator *pIter){
 140819   char *pRead = pIter->pRead;
 140820   sqlite3_int64 iRead;
 140821   int rc = 0;
 140823   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
 140824   if( iRead==0 || iRead==1 ){
 140825     pRead = 0;
 140826     rc = 1;
 140827   }else{
 140828     pIter->iPos += (int)(iRead-2);
 140831   pIter->pRead = pRead;
 140832   return rc;
 140836 ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. 
 140838 ** If the call is successful, the longest-common-substring lengths for each
 140839 ** column are written into the first nCol elements of the pInfo->aMatchinfo[] 
 140840 ** array before returning. SQLITE_OK is returned in this case.
 140842 ** Otherwise, if an error occurs, an SQLite error code is returned and the
 140843 ** data written to the first nCol elements of pInfo->aMatchinfo[] is 
 140844 ** undefined.
 140846 static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
 140847   LcsIterator *aIter;
 140848   int i;
 140849   int iCol;
 140850   int nToken = 0;
 140852   /* Allocate and populate the array of LcsIterator objects. The array
 140853   ** contains one element for each matchable phrase in the query.
 140854   **/
 140855   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
 140856   if( !aIter ) return SQLITE_NOMEM;
 140857   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
 140858   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
 140860   for(i=0; i<pInfo->nPhrase; i++){
 140861     LcsIterator *pIter = &aIter[i];
 140862     nToken -= pIter->pExpr->pPhrase->nToken;
 140863     pIter->iPosOffset = nToken;
 140866   for(iCol=0; iCol<pInfo->nCol; iCol++){
 140867     int nLcs = 0;                 /* LCS value for this column */
 140868     int nLive = 0;                /* Number of iterators in aIter not at EOF */
 140870     for(i=0; i<pInfo->nPhrase; i++){
 140871       int rc;
 140872       LcsIterator *pIt = &aIter[i];
 140873       rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
 140874       if( rc!=SQLITE_OK ) return rc;
 140875       if( pIt->pRead ){
 140876         pIt->iPos = pIt->iPosOffset;
 140877         fts3LcsIteratorAdvance(&aIter[i]);
 140878         nLive++;
 140882     while( nLive>0 ){
 140883       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
 140884       int nThisLcs = 0;           /* LCS for the current iterator positions */
 140886       for(i=0; i<pInfo->nPhrase; i++){
 140887         LcsIterator *pIter = &aIter[i];
 140888         if( pIter->pRead==0 ){
 140889           /* This iterator is already at EOF for this column. */
 140890           nThisLcs = 0;
 140891         }else{
 140892           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
 140893             pAdv = pIter;
 140895           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
 140896             nThisLcs++;
 140897           }else{
 140898             nThisLcs = 1;
 140900           if( nThisLcs>nLcs ) nLcs = nThisLcs;
 140903       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
 140906     pInfo->aMatchinfo[iCol] = nLcs;
 140909   sqlite3_free(aIter);
 140910   return SQLITE_OK;
 140914 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
 140915 ** be returned by the matchinfo() function. Argument zArg contains the 
 140916 ** format string passed as the second argument to matchinfo (or the
 140917 ** default value "pcx" if no second argument was specified). The format
 140918 ** string has already been validated and the pInfo->aMatchinfo[] array
 140919 ** is guaranteed to be large enough for the output.
 140921 ** If bGlobal is true, then populate all fields of the matchinfo() output.
 140922 ** If it is false, then assume that those fields that do not change between
 140923 ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
 140924 ** have already been populated.
 140926 ** Return SQLITE_OK if successful, or an SQLite error code if an error 
 140927 ** occurs. If a value other than SQLITE_OK is returned, the state the
 140928 ** pInfo->aMatchinfo[] buffer is left in is undefined.
 140930 static int fts3MatchinfoValues(
 140931   Fts3Cursor *pCsr,               /* FTS3 cursor object */
 140932   int bGlobal,                    /* True to grab the global stats */
 140933   MatchInfo *pInfo,               /* Matchinfo context object */
 140934   const char *zArg                /* Matchinfo format string */
 140936   int rc = SQLITE_OK;
 140937   int i;
 140938   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 140939   sqlite3_stmt *pSelect = 0;
 140941   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
 140943     switch( zArg[i] ){
 140944       case FTS3_MATCHINFO_NPHRASE:
 140945         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
 140946         break;
 140948       case FTS3_MATCHINFO_NCOL:
 140949         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
 140950         break;
 140952       case FTS3_MATCHINFO_NDOC:
 140953         if( bGlobal ){
 140954           sqlite3_int64 nDoc = 0;
 140955           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
 140956           pInfo->aMatchinfo[0] = (u32)nDoc;
 140958         break;
 140960       case FTS3_MATCHINFO_AVGLENGTH: 
 140961         if( bGlobal ){
 140962           sqlite3_int64 nDoc;     /* Number of rows in table */
 140963           const char *a;          /* Aggregate column length array */
 140965           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
 140966           if( rc==SQLITE_OK ){
 140967             int iCol;
 140968             for(iCol=0; iCol<pInfo->nCol; iCol++){
 140969               u32 iVal;
 140970               sqlite3_int64 nToken;
 140971               a += sqlite3Fts3GetVarint(a, &nToken);
 140972               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
 140973               pInfo->aMatchinfo[iCol] = iVal;
 140977         break;
 140979       case FTS3_MATCHINFO_LENGTH: {
 140980         sqlite3_stmt *pSelectDocsize = 0;
 140981         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
 140982         if( rc==SQLITE_OK ){
 140983           int iCol;
 140984           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
 140985           for(iCol=0; iCol<pInfo->nCol; iCol++){
 140986             sqlite3_int64 nToken;
 140987             a += sqlite3Fts3GetVarint(a, &nToken);
 140988             pInfo->aMatchinfo[iCol] = (u32)nToken;
 140991         sqlite3_reset(pSelectDocsize);
 140992         break;
 140995       case FTS3_MATCHINFO_LCS:
 140996         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
 140997         if( rc==SQLITE_OK ){
 140998           rc = fts3MatchinfoLcs(pCsr, pInfo);
 141000         break;
 141002       default: {
 141003         Fts3Expr *pExpr;
 141004         assert( zArg[i]==FTS3_MATCHINFO_HITS );
 141005         pExpr = pCsr->pExpr;
 141006         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
 141007         if( rc!=SQLITE_OK ) break;
 141008         if( bGlobal ){
 141009           if( pCsr->pDeferred ){
 141010             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
 141011             if( rc!=SQLITE_OK ) break;
 141013           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
 141014           if( rc!=SQLITE_OK ) break;
 141016         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
 141017         break;
 141021     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
 141024   sqlite3_reset(pSelect);
 141025   return rc;
 141030 ** Populate pCsr->aMatchinfo[] with data for the current row. The 
 141031 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
 141033 static int fts3GetMatchinfo(
 141034   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
 141035   const char *zArg                /* Second argument to matchinfo() function */
 141037   MatchInfo sInfo;
 141038   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 141039   int rc = SQLITE_OK;
 141040   int bGlobal = 0;                /* Collect 'global' stats as well as local */
 141042   memset(&sInfo, 0, sizeof(MatchInfo));
 141043   sInfo.pCursor = pCsr;
 141044   sInfo.nCol = pTab->nColumn;
 141046   /* If there is cached matchinfo() data, but the format string for the 
 141047   ** cache does not match the format string for this request, discard 
 141048   ** the cached data. */
 141049   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
 141050     assert( pCsr->aMatchinfo );
 141051     sqlite3_free(pCsr->aMatchinfo);
 141052     pCsr->zMatchinfo = 0;
 141053     pCsr->aMatchinfo = 0;
 141056   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
 141057   ** matchinfo function has been called for this query. In this case 
 141058   ** allocate the array used to accumulate the matchinfo data and
 141059   ** initialize those elements that are constant for every row.
 141061   if( pCsr->aMatchinfo==0 ){
 141062     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
 141063     int nArg;                     /* Bytes in zArg */
 141064     int i;                        /* Used to iterate through zArg */
 141066     /* Determine the number of phrases in the query */
 141067     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
 141068     sInfo.nPhrase = pCsr->nPhrase;
 141070     /* Determine the number of integers in the buffer returned by this call. */
 141071     for(i=0; zArg[i]; i++){
 141072       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
 141075     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
 141076     nArg = (int)strlen(zArg);
 141077     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
 141078     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
 141080     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
 141081     pCsr->nMatchinfo = nMatchinfo;
 141082     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
 141083     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
 141084     pCsr->isMatchinfoNeeded = 1;
 141085     bGlobal = 1;
 141088   sInfo.aMatchinfo = pCsr->aMatchinfo;
 141089   sInfo.nPhrase = pCsr->nPhrase;
 141090   if( pCsr->isMatchinfoNeeded ){
 141091     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
 141092     pCsr->isMatchinfoNeeded = 0;
 141095   return rc;
 141099 ** Implementation of snippet() function.
 141101 SQLITE_PRIVATE void sqlite3Fts3Snippet(
 141102   sqlite3_context *pCtx,          /* SQLite function call context */
 141103   Fts3Cursor *pCsr,               /* Cursor object */
 141104   const char *zStart,             /* Snippet start text - "<b>" */
 141105   const char *zEnd,               /* Snippet end text - "</b>" */
 141106   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
 141107   int iCol,                       /* Extract snippet from this column */
 141108   int nToken                      /* Approximate number of tokens in snippet */
 141110   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 141111   int rc = SQLITE_OK;
 141112   int i;
 141113   StrBuffer res = {0, 0, 0};
 141115   /* The returned text includes up to four fragments of text extracted from
 141116   ** the data in the current row. The first iteration of the for(...) loop
 141117   ** below attempts to locate a single fragment of text nToken tokens in 
 141118   ** size that contains at least one instance of all phrases in the query
 141119   ** expression that appear in the current row. If such a fragment of text
 141120   ** cannot be found, the second iteration of the loop attempts to locate
 141121   ** a pair of fragments, and so on.
 141123   int nSnippet = 0;               /* Number of fragments in this snippet */
 141124   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
 141125   int nFToken = -1;               /* Number of tokens in each fragment */
 141127   if( !pCsr->pExpr ){
 141128     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
 141129     return;
 141132   for(nSnippet=1; 1; nSnippet++){
 141134     int iSnip;                    /* Loop counter 0..nSnippet-1 */
 141135     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
 141136     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
 141138     if( nToken>=0 ){
 141139       nFToken = (nToken+nSnippet-1) / nSnippet;
 141140     }else{
 141141       nFToken = -1 * nToken;
 141144     for(iSnip=0; iSnip<nSnippet; iSnip++){
 141145       int iBestScore = -1;        /* Best score of columns checked so far */
 141146       int iRead;                  /* Used to iterate through columns */
 141147       SnippetFragment *pFragment = &aSnippet[iSnip];
 141149       memset(pFragment, 0, sizeof(*pFragment));
 141151       /* Loop through all columns of the table being considered for snippets.
 141152       ** If the iCol argument to this function was negative, this means all
 141153       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
 141155       for(iRead=0; iRead<pTab->nColumn; iRead++){
 141156         SnippetFragment sF = {0, 0, 0, 0};
 141157         int iS;
 141158         if( iCol>=0 && iRead!=iCol ) continue;
 141160         /* Find the best snippet of nFToken tokens in column iRead. */
 141161         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
 141162         if( rc!=SQLITE_OK ){
 141163           goto snippet_out;
 141165         if( iS>iBestScore ){
 141166           *pFragment = sF;
 141167           iBestScore = iS;
 141171       mCovered |= pFragment->covered;
 141174     /* If all query phrases seen by fts3BestSnippet() are present in at least
 141175     ** one of the nSnippet snippet fragments, break out of the loop.
 141177     assert( (mCovered&mSeen)==mCovered );
 141178     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
 141181   assert( nFToken>0 );
 141183   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
 141184     rc = fts3SnippetText(pCsr, &aSnippet[i], 
 141185         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
 141189  snippet_out:
 141190   sqlite3Fts3SegmentsClose(pTab);
 141191   if( rc!=SQLITE_OK ){
 141192     sqlite3_result_error_code(pCtx, rc);
 141193     sqlite3_free(res.z);
 141194   }else{
 141195     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
 141200 typedef struct TermOffset TermOffset;
 141201 typedef struct TermOffsetCtx TermOffsetCtx;
 141203 struct TermOffset {
 141204   char *pList;                    /* Position-list */
 141205   int iPos;                       /* Position just read from pList */
 141206   int iOff;                       /* Offset of this term from read positions */
 141209 struct TermOffsetCtx {
 141210   Fts3Cursor *pCsr;
 141211   int iCol;                       /* Column of table to populate aTerm for */
 141212   int iTerm;
 141213   sqlite3_int64 iDocid;
 141214   TermOffset *aTerm;
 141218 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
 141220 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
 141221   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
 141222   int nTerm;                      /* Number of tokens in phrase */
 141223   int iTerm;                      /* For looping through nTerm phrase terms */
 141224   char *pList;                    /* Pointer to position list for phrase */
 141225   int iPos = 0;                   /* First position in position-list */
 141226   int rc;
 141228   UNUSED_PARAMETER(iPhrase);
 141229   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
 141230   nTerm = pExpr->pPhrase->nToken;
 141231   if( pList ){
 141232     fts3GetDeltaPosition(&pList, &iPos);
 141233     assert( iPos>=0 );
 141236   for(iTerm=0; iTerm<nTerm; iTerm++){
 141237     TermOffset *pT = &p->aTerm[p->iTerm++];
 141238     pT->iOff = nTerm-iTerm-1;
 141239     pT->pList = pList;
 141240     pT->iPos = iPos;
 141243   return rc;
 141247 ** Implementation of offsets() function.
 141249 SQLITE_PRIVATE void sqlite3Fts3Offsets(
 141250   sqlite3_context *pCtx,          /* SQLite function call context */
 141251   Fts3Cursor *pCsr                /* Cursor object */
 141253   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 141254   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
 141255   int rc;                         /* Return Code */
 141256   int nToken;                     /* Number of tokens in query */
 141257   int iCol;                       /* Column currently being processed */
 141258   StrBuffer res = {0, 0, 0};      /* Result string */
 141259   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
 141261   if( !pCsr->pExpr ){
 141262     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
 141263     return;
 141266   memset(&sCtx, 0, sizeof(sCtx));
 141267   assert( pCsr->isRequireSeek==0 );
 141269   /* Count the number of terms in the query */
 141270   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
 141271   if( rc!=SQLITE_OK ) goto offsets_out;
 141273   /* Allocate the array of TermOffset iterators. */
 141274   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
 141275   if( 0==sCtx.aTerm ){
 141276     rc = SQLITE_NOMEM;
 141277     goto offsets_out;
 141279   sCtx.iDocid = pCsr->iPrevId;
 141280   sCtx.pCsr = pCsr;
 141282   /* Loop through the table columns, appending offset information to 
 141283   ** string-buffer res for each column.
 141285   for(iCol=0; iCol<pTab->nColumn; iCol++){
 141286     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
 141287     const char *ZDUMMY;           /* Dummy argument used with xNext() */
 141288     int NDUMMY = 0;               /* Dummy argument used with xNext() */
 141289     int iStart = 0;
 141290     int iEnd = 0;
 141291     int iCurrent = 0;
 141292     const char *zDoc;
 141293     int nDoc;
 141295     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
 141296     ** no way that this operation can fail, so the return code from
 141297     ** fts3ExprIterate() can be discarded.
 141299     sCtx.iCol = iCol;
 141300     sCtx.iTerm = 0;
 141301     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
 141303     /* Retreive the text stored in column iCol. If an SQL NULL is stored 
 141304     ** in column iCol, jump immediately to the next iteration of the loop.
 141305     ** If an OOM occurs while retrieving the data (this can happen if SQLite
 141306     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
 141307     ** to the caller. 
 141309     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
 141310     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
 141311     if( zDoc==0 ){
 141312       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
 141313         continue;
 141315       rc = SQLITE_NOMEM;
 141316       goto offsets_out;
 141319     /* Initialize a tokenizer iterator to iterate through column iCol. */
 141320     rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
 141321         zDoc, nDoc, &pC
 141323     if( rc!=SQLITE_OK ) goto offsets_out;
 141325     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
 141326     while( rc==SQLITE_OK ){
 141327       int i;                      /* Used to loop through terms */
 141328       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
 141329       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
 141331       for(i=0; i<nToken; i++){
 141332         TermOffset *pT = &sCtx.aTerm[i];
 141333         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
 141334           iMinPos = pT->iPos-pT->iOff;
 141335           pTerm = pT;
 141339       if( !pTerm ){
 141340         /* All offsets for this column have been gathered. */
 141341         rc = SQLITE_DONE;
 141342       }else{
 141343         assert( iCurrent<=iMinPos );
 141344         if( 0==(0xFE&*pTerm->pList) ){
 141345           pTerm->pList = 0;
 141346         }else{
 141347           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
 141349         while( rc==SQLITE_OK && iCurrent<iMinPos ){
 141350           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
 141352         if( rc==SQLITE_OK ){
 141353           char aBuffer[64];
 141354           sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
 141355               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
 141357           rc = fts3StringAppend(&res, aBuffer, -1);
 141358         }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
 141359           rc = FTS_CORRUPT_VTAB;
 141363     if( rc==SQLITE_DONE ){
 141364       rc = SQLITE_OK;
 141367     pMod->xClose(pC);
 141368     if( rc!=SQLITE_OK ) goto offsets_out;
 141371  offsets_out:
 141372   sqlite3_free(sCtx.aTerm);
 141373   assert( rc!=SQLITE_DONE );
 141374   sqlite3Fts3SegmentsClose(pTab);
 141375   if( rc!=SQLITE_OK ){
 141376     sqlite3_result_error_code(pCtx,  rc);
 141377     sqlite3_free(res.z);
 141378   }else{
 141379     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
 141381   return;
 141385 ** Implementation of matchinfo() function.
 141387 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
 141388   sqlite3_context *pContext,      /* Function call context */
 141389   Fts3Cursor *pCsr,               /* FTS3 table cursor */
 141390   const char *zArg                /* Second arg to matchinfo() function */
 141392   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
 141393   int rc;
 141394   int i;
 141395   const char *zFormat;
 141397   if( zArg ){
 141398     for(i=0; zArg[i]; i++){
 141399       char *zErr = 0;
 141400       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
 141401         sqlite3_result_error(pContext, zErr, -1);
 141402         sqlite3_free(zErr);
 141403         return;
 141406     zFormat = zArg;
 141407   }else{
 141408     zFormat = FTS3_MATCHINFO_DEFAULT;
 141411   if( !pCsr->pExpr ){
 141412     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
 141413     return;
 141416   /* Retrieve matchinfo() data. */
 141417   rc = fts3GetMatchinfo(pCsr, zFormat);
 141418   sqlite3Fts3SegmentsClose(pTab);
 141420   if( rc!=SQLITE_OK ){
 141421     sqlite3_result_error_code(pContext, rc);
 141422   }else{
 141423     int n = pCsr->nMatchinfo * sizeof(u32);
 141424     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
 141428 #endif
 141430 /************** End of fts3_snippet.c ****************************************/
 141431 /************** Begin file fts3_unicode.c ************************************/
 141433 ** 2012 May 24
 141435 ** The author disclaims copyright to this source code.  In place of
 141436 ** a legal notice, here is a blessing:
 141438 **    May you do good and not evil.
 141439 **    May you find forgiveness for yourself and forgive others.
 141440 **    May you share freely, never taking more than you give.
 141442 ******************************************************************************
 141444 ** Implementation of the "unicode" full-text-search tokenizer.
 141447 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
 141449 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 141451 /* #include <assert.h> */
 141452 /* #include <stdlib.h> */
 141453 /* #include <stdio.h> */
 141454 /* #include <string.h> */
 141458 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
 141459 ** from the sqlite3 source file utf.c. If this file is compiled as part
 141460 ** of the amalgamation, they are not required.
 141462 #ifndef SQLITE_AMALGAMATION
 141464 static const unsigned char sqlite3Utf8Trans1[] = {
 141465   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 141466   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 141467   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 141468   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
 141469   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 141470   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
 141471   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 141472   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
 141475 #define READ_UTF8(zIn, zTerm, c)                           \
 141476   c = *(zIn++);                                            \
 141477   if( c>=0xc0 ){                                           \
 141478     c = sqlite3Utf8Trans1[c-0xc0];                         \
 141479     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
 141480       c = (c<<6) + (0x3f & *(zIn++));                      \
 141481     }                                                      \
 141482     if( c<0x80                                             \
 141483         || (c&0xFFFFF800)==0xD800                          \
 141484         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 141487 #define WRITE_UTF8(zOut, c) {                          \
 141488   if( c<0x00080 ){                                     \
 141489     *zOut++ = (u8)(c&0xFF);                            \
 141490   }                                                    \
 141491   else if( c<0x00800 ){                                \
 141492     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
 141493     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 141494   }                                                    \
 141495   else if( c<0x10000 ){                                \
 141496     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
 141497     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 141498     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 141499   }else{                                               \
 141500     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
 141501     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
 141502     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
 141503     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
 141504   }                                                    \
 141507 #endif /* ifndef SQLITE_AMALGAMATION */
 141509 typedef struct unicode_tokenizer unicode_tokenizer;
 141510 typedef struct unicode_cursor unicode_cursor;
 141512 struct unicode_tokenizer {
 141513   sqlite3_tokenizer base;
 141514   int bRemoveDiacritic;
 141515   int nException;
 141516   int *aiException;
 141519 struct unicode_cursor {
 141520   sqlite3_tokenizer_cursor base;
 141521   const unsigned char *aInput;    /* Input text being tokenized */
 141522   int nInput;                     /* Size of aInput[] in bytes */
 141523   int iOff;                       /* Current offset within aInput[] */
 141524   int iToken;                     /* Index of next token to be returned */
 141525   char *zToken;                   /* storage for current token */
 141526   int nAlloc;                     /* space allocated at zToken */
 141531 ** Destroy a tokenizer allocated by unicodeCreate().
 141533 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
 141534   if( pTokenizer ){
 141535     unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
 141536     sqlite3_free(p->aiException);
 141537     sqlite3_free(p);
 141539   return SQLITE_OK;
 141543 ** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
 141544 ** statement has specified that the tokenizer for this table shall consider
 141545 ** all characters in string zIn/nIn to be separators (if bAlnum==0) or
 141546 ** token characters (if bAlnum==1).
 141548 ** For each codepoint in the zIn/nIn string, this function checks if the
 141549 ** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
 141550 ** If so, no action is taken. Otherwise, the codepoint is added to the 
 141551 ** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
 141552 ** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
 141553 ** codepoints in the aiException[] array.
 141555 ** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
 141556 ** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
 141557 ** It is not possible to change the behavior of the tokenizer with respect
 141558 ** to these codepoints.
 141560 static int unicodeAddExceptions(
 141561   unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
 141562   int bAlnum,                     /* Replace Isalnum() return value with this */
 141563   const char *zIn,                /* Array of characters to make exceptions */
 141564   int nIn                         /* Length of z in bytes */
 141566   const unsigned char *z = (const unsigned char *)zIn;
 141567   const unsigned char *zTerm = &z[nIn];
 141568   int iCode;
 141569   int nEntry = 0;
 141571   assert( bAlnum==0 || bAlnum==1 );
 141573   while( z<zTerm ){
 141574     READ_UTF8(z, zTerm, iCode);
 141575     assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
 141576     if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
 141577      && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
 141579       nEntry++;
 141583   if( nEntry ){
 141584     int *aNew;                    /* New aiException[] array */
 141585     int nNew;                     /* Number of valid entries in array aNew[] */
 141587     aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
 141588     if( aNew==0 ) return SQLITE_NOMEM;
 141589     nNew = p->nException;
 141591     z = (const unsigned char *)zIn;
 141592     while( z<zTerm ){
 141593       READ_UTF8(z, zTerm, iCode);
 141594       if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
 141595        && sqlite3FtsUnicodeIsdiacritic(iCode)==0
 141597         int i, j;
 141598         for(i=0; i<nNew && aNew[i]<iCode; i++);
 141599         for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
 141600         aNew[i] = iCode;
 141601         nNew++;
 141604     p->aiException = aNew;
 141605     p->nException = nNew;
 141608   return SQLITE_OK;
 141612 ** Return true if the p->aiException[] array contains the value iCode.
 141614 static int unicodeIsException(unicode_tokenizer *p, int iCode){
 141615   if( p->nException>0 ){
 141616     int *a = p->aiException;
 141617     int iLo = 0;
 141618     int iHi = p->nException-1;
 141620     while( iHi>=iLo ){
 141621       int iTest = (iHi + iLo) / 2;
 141622       if( iCode==a[iTest] ){
 141623         return 1;
 141624       }else if( iCode>a[iTest] ){
 141625         iLo = iTest+1;
 141626       }else{
 141627         iHi = iTest-1;
 141632   return 0;
 141636 ** Return true if, for the purposes of tokenization, codepoint iCode is
 141637 ** considered a token character (not a separator).
 141639 static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
 141640   assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
 141641   return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
 141645 ** Create a new tokenizer instance.
 141647 static int unicodeCreate(
 141648   int nArg,                       /* Size of array argv[] */
 141649   const char * const *azArg,      /* Tokenizer creation arguments */
 141650   sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
 141652   unicode_tokenizer *pNew;        /* New tokenizer object */
 141653   int i;
 141654   int rc = SQLITE_OK;
 141656   pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
 141657   if( pNew==NULL ) return SQLITE_NOMEM;
 141658   memset(pNew, 0, sizeof(unicode_tokenizer));
 141659   pNew->bRemoveDiacritic = 1;
 141661   for(i=0; rc==SQLITE_OK && i<nArg; i++){
 141662     const char *z = azArg[i];
 141663     int n = strlen(z);
 141665     if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
 141666       pNew->bRemoveDiacritic = 1;
 141668     else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
 141669       pNew->bRemoveDiacritic = 0;
 141671     else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
 141672       rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
 141674     else if( n>=11 && memcmp("separators=", z, 11)==0 ){
 141675       rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
 141677     else{
 141678       /* Unrecognized argument */
 141679       rc  = SQLITE_ERROR;
 141683   if( rc!=SQLITE_OK ){
 141684     unicodeDestroy((sqlite3_tokenizer *)pNew);
 141685     pNew = 0;
 141687   *pp = (sqlite3_tokenizer *)pNew;
 141688   return rc;
 141692 ** Prepare to begin tokenizing a particular string.  The input
 141693 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 141694 ** used to incrementally tokenize this string is returned in 
 141695 ** *ppCursor.
 141697 static int unicodeOpen(
 141698   sqlite3_tokenizer *p,           /* The tokenizer */
 141699   const char *aInput,             /* Input string */
 141700   int nInput,                     /* Size of string aInput in bytes */
 141701   sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
 141703   unicode_cursor *pCsr;
 141705   pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
 141706   if( pCsr==0 ){
 141707     return SQLITE_NOMEM;
 141709   memset(pCsr, 0, sizeof(unicode_cursor));
 141711   pCsr->aInput = (const unsigned char *)aInput;
 141712   if( aInput==0 ){
 141713     pCsr->nInput = 0;
 141714   }else if( nInput<0 ){
 141715     pCsr->nInput = (int)strlen(aInput);
 141716   }else{
 141717     pCsr->nInput = nInput;
 141720   *pp = &pCsr->base;
 141721   UNUSED_PARAMETER(p);
 141722   return SQLITE_OK;
 141726 ** Close a tokenization cursor previously opened by a call to
 141727 ** simpleOpen() above.
 141729 static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
 141730   unicode_cursor *pCsr = (unicode_cursor *) pCursor;
 141731   sqlite3_free(pCsr->zToken);
 141732   sqlite3_free(pCsr);
 141733   return SQLITE_OK;
 141737 ** Extract the next token from a tokenization cursor.  The cursor must
 141738 ** have been opened by a prior call to simpleOpen().
 141740 static int unicodeNext(
 141741   sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
 141742   const char **paToken,           /* OUT: Token text */
 141743   int *pnToken,                   /* OUT: Number of bytes at *paToken */
 141744   int *piStart,                   /* OUT: Starting offset of token */
 141745   int *piEnd,                     /* OUT: Ending offset of token */
 141746   int *piPos                      /* OUT: Position integer of token */
 141748   unicode_cursor *pCsr = (unicode_cursor *)pC;
 141749   unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
 141750   int iCode;
 141751   char *zOut;
 141752   const unsigned char *z = &pCsr->aInput[pCsr->iOff];
 141753   const unsigned char *zStart = z;
 141754   const unsigned char *zEnd;
 141755   const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
 141757   /* Scan past any delimiter characters before the start of the next token.
 141758   ** Return SQLITE_DONE early if this takes us all the way to the end of 
 141759   ** the input.  */
 141760   while( z<zTerm ){
 141761     READ_UTF8(z, zTerm, iCode);
 141762     if( unicodeIsAlnum(p, iCode) ) break;
 141763     zStart = z;
 141765   if( zStart>=zTerm ) return SQLITE_DONE;
 141767   zOut = pCsr->zToken;
 141768   do {
 141769     int iOut;
 141771     /* Grow the output buffer if required. */
 141772     if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
 141773       char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
 141774       if( !zNew ) return SQLITE_NOMEM;
 141775       zOut = &zNew[zOut - pCsr->zToken];
 141776       pCsr->zToken = zNew;
 141777       pCsr->nAlloc += 64;
 141780     /* Write the folded case of the last character read to the output */
 141781     zEnd = z;
 141782     iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
 141783     if( iOut ){
 141784       WRITE_UTF8(zOut, iOut);
 141787     /* If the cursor is not at EOF, read the next character */
 141788     if( z>=zTerm ) break;
 141789     READ_UTF8(z, zTerm, iCode);
 141790   }while( unicodeIsAlnum(p, iCode) 
 141791        || sqlite3FtsUnicodeIsdiacritic(iCode)
 141794   /* Set the output variables and return. */
 141795   pCsr->iOff = (z - pCsr->aInput);
 141796   *paToken = pCsr->zToken;
 141797   *pnToken = zOut - pCsr->zToken;
 141798   *piStart = (zStart - pCsr->aInput);
 141799   *piEnd = (zEnd - pCsr->aInput);
 141800   *piPos = pCsr->iToken++;
 141801   return SQLITE_OK;
 141805 ** Set *ppModule to a pointer to the sqlite3_tokenizer_module 
 141806 ** structure for the unicode tokenizer.
 141808 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
 141809   static const sqlite3_tokenizer_module module = {
 141811     unicodeCreate,
 141812     unicodeDestroy,
 141813     unicodeOpen,
 141814     unicodeClose,
 141815     unicodeNext,
 141818   *ppModule = &module;
 141821 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 141822 #endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
 141824 /************** End of fts3_unicode.c ****************************************/
 141825 /************** Begin file fts3_unicode2.c ***********************************/
 141827 ** 2012 May 25
 141829 ** The author disclaims copyright to this source code.  In place of
 141830 ** a legal notice, here is a blessing:
 141832 **    May you do good and not evil.
 141833 **    May you find forgiveness for yourself and forgive others.
 141834 **    May you share freely, never taking more than you give.
 141836 ******************************************************************************
 141840 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
 141843 #if defined(SQLITE_ENABLE_FTS4_UNICODE61)
 141844 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
 141846 /* #include <assert.h> */
 141849 ** Return true if the argument corresponds to a unicode codepoint
 141850 ** classified as either a letter or a number. Otherwise false.
 141852 ** The results are undefined if the value passed to this function
 141853 ** is less than zero.
 141855 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
 141856   /* Each unsigned integer in the following array corresponds to a contiguous
 141857   ** range of unicode codepoints that are not either letters or numbers (i.e.
 141858   ** codepoints for which this function should return 0).
 141860   ** The most significant 22 bits in each 32-bit value contain the first 
 141861   ** codepoint in the range. The least significant 10 bits are used to store
 141862   ** the size of the range (always at least 1). In other words, the value 
 141863   ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
 141864   ** C. It is not possible to represent a range larger than 1023 codepoints 
 141865   ** using this format.
 141867   const static unsigned int aEntry[] = {
 141868     0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
 141869     0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
 141870     0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
 141871     0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
 141872     0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
 141873     0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
 141874     0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
 141875     0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
 141876     0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
 141877     0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
 141878     0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
 141879     0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
 141880     0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
 141881     0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
 141882     0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
 141883     0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
 141884     0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
 141885     0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
 141886     0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
 141887     0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
 141888     0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
 141889     0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
 141890     0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
 141891     0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
 141892     0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
 141893     0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
 141894     0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
 141895     0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
 141896     0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
 141897     0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
 141898     0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
 141899     0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
 141900     0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
 141901     0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
 141902     0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
 141903     0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
 141904     0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
 141905     0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
 141906     0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
 141907     0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
 141908     0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
 141909     0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
 141910     0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
 141911     0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
 141912     0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
 141913     0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
 141914     0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
 141915     0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
 141916     0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
 141917     0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
 141918     0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
 141919     0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
 141920     0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
 141921     0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
 141922     0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
 141923     0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
 141924     0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
 141925     0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
 141926     0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
 141927     0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
 141928     0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
 141929     0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
 141930     0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
 141931     0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
 141932     0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
 141933     0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
 141934     0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
 141935     0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
 141936     0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
 141937     0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
 141938     0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
 141939     0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
 141940     0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
 141941     0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
 141942     0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
 141943     0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
 141944     0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
 141945     0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
 141946     0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
 141947     0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
 141948     0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
 141949     0x380400F0,
 141951   static const unsigned int aAscii[4] = {
 141952     0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
 141955   if( c<128 ){
 141956     return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
 141957   }else if( c<(1<<22) ){
 141958     unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
 141959     int iRes;
 141960     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
 141961     int iLo = 0;
 141962     while( iHi>=iLo ){
 141963       int iTest = (iHi + iLo) / 2;
 141964       if( key >= aEntry[iTest] ){
 141965         iRes = iTest;
 141966         iLo = iTest+1;
 141967       }else{
 141968         iHi = iTest-1;
 141971     assert( aEntry[0]<key );
 141972     assert( key>=aEntry[iRes] );
 141973     return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
 141975   return 1;
 141980 ** If the argument is a codepoint corresponding to a lowercase letter
 141981 ** in the ASCII range with a diacritic added, return the codepoint
 141982 ** of the ASCII letter only. For example, if passed 235 - "LATIN
 141983 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
 141984 ** E"). The resuls of passing a codepoint that corresponds to an
 141985 ** uppercase letter are undefined.
 141987 static int remove_diacritic(int c){
 141988   unsigned short aDia[] = {
 141989         0,  1797,  1848,  1859,  1891,  1928,  1940,  1995, 
 141990      2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286, 
 141991      2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732, 
 141992      2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336, 
 141993      3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928, 
 141994      3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234, 
 141995      4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504, 
 141996      6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529, 
 141997     61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, 
 141998     61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, 
 141999     62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, 
 142000     62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, 
 142001     62924, 63050, 63082, 63274, 63390, 
 142003   char aChar[] = {
 142004     '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',  
 142005     'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',  
 142006     's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',  
 142007     'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',  
 142008     'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0', 
 142009     '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',  
 142010     'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',  
 142011     'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',  
 142012     'e',  'i',  'o',  'u',  'y',  
 142015   unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
 142016   int iRes = 0;
 142017   int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
 142018   int iLo = 0;
 142019   while( iHi>=iLo ){
 142020     int iTest = (iHi + iLo) / 2;
 142021     if( key >= aDia[iTest] ){
 142022       iRes = iTest;
 142023       iLo = iTest+1;
 142024     }else{
 142025       iHi = iTest-1;
 142028   assert( key>=aDia[iRes] );
 142029   return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
 142034 ** Return true if the argument interpreted as a unicode codepoint
 142035 ** is a diacritical modifier character.
 142037 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
 142038   unsigned int mask0 = 0x08029FDF;
 142039   unsigned int mask1 = 0x000361F8;
 142040   if( c<768 || c>817 ) return 0;
 142041   return (c < 768+32) ?
 142042       (mask0 & (1 << (c-768))) :
 142043       (mask1 & (1 << (c-768-32)));
 142048 ** Interpret the argument as a unicode codepoint. If the codepoint
 142049 ** is an upper case character that has a lower case equivalent,
 142050 ** return the codepoint corresponding to the lower case version.
 142051 ** Otherwise, return a copy of the argument.
 142053 ** The results are undefined if the value passed to this function
 142054 ** is less than zero.
 142056 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
 142057   /* Each entry in the following array defines a rule for folding a range
 142058   ** of codepoints to lower case. The rule applies to a range of nRange
 142059   ** codepoints starting at codepoint iCode.
 142061   ** If the least significant bit in flags is clear, then the rule applies
 142062   ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
 142063   ** need to be folded). Or, if it is set, then the rule only applies to
 142064   ** every second codepoint in the range, starting with codepoint C.
 142066   ** The 7 most significant bits in flags are an index into the aiOff[]
 142067   ** array. If a specific codepoint C does require folding, then its lower
 142068   ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
 142070   ** The contents of this array are generated by parsing the CaseFolding.txt
 142071   ** file distributed as part of the "Unicode Character Database". See
 142072   ** http://www.unicode.org for details.
 142074   static const struct TableEntry {
 142075     unsigned short iCode;
 142076     unsigned char flags;
 142077     unsigned char nRange;
 142078   } aEntry[] = {
 142079     {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
 142080     {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
 142081     {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
 142082     {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
 142083     {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
 142084     {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
 142085     {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
 142086     {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
 142087     {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
 142088     {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
 142089     {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
 142090     {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
 142091     {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
 142092     {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
 142093     {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
 142094     {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
 142095     {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
 142096     {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
 142097     {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
 142098     {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
 142099     {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
 142100     {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
 142101     {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
 142102     {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
 142103     {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
 142104     {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
 142105     {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
 142106     {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
 142107     {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
 142108     {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
 142109     {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
 142110     {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
 142111     {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
 142112     {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
 142113     {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
 142114     {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
 142115     {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
 142116     {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
 142117     {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
 142118     {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
 142119     {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
 142120     {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
 142121     {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
 142122     {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
 142123     {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
 142124     {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
 142125     {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
 142126     {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
 142127     {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
 142128     {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
 142129     {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
 142130     {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
 142131     {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
 142132     {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
 142133     {65313, 14, 26},       
 142135   static const unsigned short aiOff[] = {
 142136    1,     2,     8,     15,    16,    26,    28,    32,    
 142137    37,    38,    40,    48,    63,    64,    69,    71,    
 142138    79,    80,    116,   202,   203,   205,   206,   207,   
 142139    209,   210,   211,   213,   214,   217,   218,   219,   
 142140    775,   7264,  10792, 10795, 23228, 23256, 30204, 54721, 
 142141    54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274, 
 142142    57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406, 
 142143    65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
 142144    65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
 142145    65514, 65521, 65527, 65528, 65529, 
 142148   int ret = c;
 142150   assert( c>=0 );
 142151   assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
 142153   if( c<128 ){
 142154     if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
 142155   }else if( c<65536 ){
 142156     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
 142157     int iLo = 0;
 142158     int iRes = -1;
 142160     while( iHi>=iLo ){
 142161       int iTest = (iHi + iLo) / 2;
 142162       int cmp = (c - aEntry[iTest].iCode);
 142163       if( cmp>=0 ){
 142164         iRes = iTest;
 142165         iLo = iTest+1;
 142166       }else{
 142167         iHi = iTest-1;
 142170     assert( iRes<0 || c>=aEntry[iRes].iCode );
 142172     if( iRes>=0 ){
 142173       const struct TableEntry *p = &aEntry[iRes];
 142174       if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
 142175         ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
 142176         assert( ret>0 );
 142180     if( bRemoveDiacritic ) ret = remove_diacritic(ret);
 142183   else if( c>=66560 && c<66600 ){
 142184     ret = c + 40;
 142187   return ret;
 142189 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
 142190 #endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
 142192 /************** End of fts3_unicode2.c ***************************************/
 142193 /************** Begin file rtree.c *******************************************/
 142195 ** 2001 September 15
 142197 ** The author disclaims copyright to this source code.  In place of
 142198 ** a legal notice, here is a blessing:
 142200 **    May you do good and not evil.
 142201 **    May you find forgiveness for yourself and forgive others.
 142202 **    May you share freely, never taking more than you give.
 142204 *************************************************************************
 142205 ** This file contains code for implementations of the r-tree and r*-tree
 142206 ** algorithms packaged as an SQLite virtual table module.
 142210 ** Database Format of R-Tree Tables
 142211 ** --------------------------------
 142213 ** The data structure for a single virtual r-tree table is stored in three 
 142214 ** native SQLite tables declared as follows. In each case, the '%' character
 142215 ** in the table name is replaced with the user-supplied name of the r-tree
 142216 ** table.
 142218 **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
 142219 **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
 142220 **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
 142222 ** The data for each node of the r-tree structure is stored in the %_node
 142223 ** table. For each node that is not the root node of the r-tree, there is
 142224 ** an entry in the %_parent table associating the node with its parent.
 142225 ** And for each row of data in the table, there is an entry in the %_rowid
 142226 ** table that maps from the entries rowid to the id of the node that it
 142227 ** is stored on.
 142229 ** The root node of an r-tree always exists, even if the r-tree table is
 142230 ** empty. The nodeno of the root node is always 1. All other nodes in the
 142231 ** table must be the same size as the root node. The content of each node
 142232 ** is formatted as follows:
 142234 **   1. If the node is the root node (node 1), then the first 2 bytes
 142235 **      of the node contain the tree depth as a big-endian integer.
 142236 **      For non-root nodes, the first 2 bytes are left unused.
 142238 **   2. The next 2 bytes contain the number of entries currently 
 142239 **      stored in the node.
 142241 **   3. The remainder of the node contains the node entries. Each entry
 142242 **      consists of a single 8-byte integer followed by an even number
 142243 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
 142244 **      of a record. For internal nodes it is the node number of a
 142245 **      child page.
 142248 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
 142251 ** This file contains an implementation of a couple of different variants
 142252 ** of the r-tree algorithm. See the README file for further details. The 
 142253 ** same data-structure is used for all, but the algorithms for insert and
 142254 ** delete operations vary. The variants used are selected at compile time 
 142255 ** by defining the following symbols:
 142258 /* Either, both or none of the following may be set to activate 
 142259 ** r*tree variant algorithms.
 142261 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
 142262 #define VARIANT_RSTARTREE_REINSERT      1
 142265 ** Exactly one of the following must be set to 1.
 142267 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
 142268 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
 142269 #define VARIANT_RSTARTREE_SPLIT         1
 142271 #define VARIANT_GUTTMAN_SPLIT \
 142272         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
 142274 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
 142275   #define PickNext QuadraticPickNext
 142276   #define PickSeeds QuadraticPickSeeds
 142277   #define AssignCells splitNodeGuttman
 142278 #endif
 142279 #if VARIANT_GUTTMAN_LINEAR_SPLIT
 142280   #define PickNext LinearPickNext
 142281   #define PickSeeds LinearPickSeeds
 142282   #define AssignCells splitNodeGuttman
 142283 #endif
 142284 #if VARIANT_RSTARTREE_SPLIT
 142285   #define AssignCells splitNodeStartree
 142286 #endif
 142288 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
 142289 # define NDEBUG 1
 142290 #endif
 142292 #ifndef SQLITE_CORE
 142293   SQLITE_EXTENSION_INIT1
 142294 #else
 142295 #endif
 142297 /* #include <string.h> */
 142298 /* #include <assert.h> */
 142300 #ifndef SQLITE_AMALGAMATION
 142301 #include "sqlite3rtree.h"
 142302 typedef sqlite3_int64 i64;
 142303 typedef unsigned char u8;
 142304 typedef unsigned int u32;
 142305 #endif
 142307 /*  The following macro is used to suppress compiler warnings.
 142309 #ifndef UNUSED_PARAMETER
 142310 # define UNUSED_PARAMETER(x) (void)(x)
 142311 #endif
 142313 typedef struct Rtree Rtree;
 142314 typedef struct RtreeCursor RtreeCursor;
 142315 typedef struct RtreeNode RtreeNode;
 142316 typedef struct RtreeCell RtreeCell;
 142317 typedef struct RtreeConstraint RtreeConstraint;
 142318 typedef struct RtreeMatchArg RtreeMatchArg;
 142319 typedef struct RtreeGeomCallback RtreeGeomCallback;
 142320 typedef union RtreeCoord RtreeCoord;
 142322 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
 142323 #define RTREE_MAX_DIMENSIONS 5
 142325 /* Size of hash table Rtree.aHash. This hash table is not expected to
 142326 ** ever contain very many entries, so a fixed number of buckets is 
 142327 ** used.
 142329 #define HASHSIZE 128
 142331 /* The xBestIndex method of this virtual table requires an estimate of
 142332 ** the number of rows in the virtual table to calculate the costs of
 142333 ** various strategies. If possible, this estimate is loaded from the
 142334 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
 142335 ** Otherwise, if no sqlite_stat1 entry is available, use 
 142336 ** RTREE_DEFAULT_ROWEST.
 142338 #define RTREE_DEFAULT_ROWEST 1048576
 142339 #define RTREE_MIN_ROWEST         100
 142342 ** An rtree virtual-table object.
 142344 struct Rtree {
 142345   sqlite3_vtab base;
 142346   sqlite3 *db;                /* Host database connection */
 142347   int iNodeSize;              /* Size in bytes of each node in the node table */
 142348   int nDim;                   /* Number of dimensions */
 142349   int nBytesPerCell;          /* Bytes consumed per cell */
 142350   int iDepth;                 /* Current depth of the r-tree structure */
 142351   char *zDb;                  /* Name of database containing r-tree table */
 142352   char *zName;                /* Name of r-tree table */ 
 142353   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
 142354   int nBusy;                  /* Current number of users of this structure */
 142355   i64 nRowEst;                /* Estimated number of rows in this table */
 142357   /* List of nodes removed during a CondenseTree operation. List is
 142358   ** linked together via the pointer normally used for hash chains -
 142359   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
 142360   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
 142362   RtreeNode *pDeleted;
 142363   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
 142365   /* Statements to read/write/delete a record from xxx_node */
 142366   sqlite3_stmt *pReadNode;
 142367   sqlite3_stmt *pWriteNode;
 142368   sqlite3_stmt *pDeleteNode;
 142370   /* Statements to read/write/delete a record from xxx_rowid */
 142371   sqlite3_stmt *pReadRowid;
 142372   sqlite3_stmt *pWriteRowid;
 142373   sqlite3_stmt *pDeleteRowid;
 142375   /* Statements to read/write/delete a record from xxx_parent */
 142376   sqlite3_stmt *pReadParent;
 142377   sqlite3_stmt *pWriteParent;
 142378   sqlite3_stmt *pDeleteParent;
 142380   int eCoordType;
 142383 /* Possible values for eCoordType: */
 142384 #define RTREE_COORD_REAL32 0
 142385 #define RTREE_COORD_INT32  1
 142388 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
 142389 ** only deal with integer coordinates.  No floating point operations
 142390 ** will be done.
 142392 #ifdef SQLITE_RTREE_INT_ONLY
 142393   typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
 142394   typedef int RtreeValue;                  /* Low accuracy coordinate */
 142395 #else
 142396   typedef double RtreeDValue;              /* High accuracy coordinate */
 142397   typedef float RtreeValue;                /* Low accuracy coordinate */
 142398 #endif
 142401 ** The minimum number of cells allowed for a node is a third of the 
 142402 ** maximum. In Gutman's notation:
 142404 **     m = M/3
 142406 ** If an R*-tree "Reinsert" operation is required, the same number of
 142407 ** cells are removed from the overfull node and reinserted into the tree.
 142409 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
 142410 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
 142411 #define RTREE_MAXCELLS 51
 142414 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
 142415 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
 142416 ** Therefore all non-root nodes must contain at least 3 entries. Since 
 142417 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
 142418 ** 40 or less.
 142420 #define RTREE_MAX_DEPTH 40
 142423 ** An rtree cursor object.
 142425 struct RtreeCursor {
 142426   sqlite3_vtab_cursor base;
 142427   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
 142428   int iCell;                        /* Index of current cell in pNode */
 142429   int iStrategy;                    /* Copy of idxNum search parameter */
 142430   int nConstraint;                  /* Number of entries in aConstraint */
 142431   RtreeConstraint *aConstraint;     /* Search constraints. */
 142434 union RtreeCoord {
 142435   RtreeValue f;
 142436   int i;
 142440 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
 142441 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
 142442 ** variable pRtree points to the Rtree structure associated with the
 142443 ** RtreeCoord.
 142445 #ifdef SQLITE_RTREE_INT_ONLY
 142446 # define DCOORD(coord) ((RtreeDValue)coord.i)
 142447 #else
 142448 # define DCOORD(coord) (                           \
 142449     (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
 142450       ((double)coord.f) :                           \
 142451       ((double)coord.i)                             \
 142453 #endif
 142456 ** A search constraint.
 142458 struct RtreeConstraint {
 142459   int iCoord;                     /* Index of constrained coordinate */
 142460   int op;                         /* Constraining operation */
 142461   RtreeDValue rValue;             /* Constraint value. */
 142462   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
 142463   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
 142466 /* Possible values for RtreeConstraint.op */
 142467 #define RTREE_EQ    0x41
 142468 #define RTREE_LE    0x42
 142469 #define RTREE_LT    0x43
 142470 #define RTREE_GE    0x44
 142471 #define RTREE_GT    0x45
 142472 #define RTREE_MATCH 0x46
 142475 ** An rtree structure node.
 142477 struct RtreeNode {
 142478   RtreeNode *pParent;               /* Parent node */
 142479   i64 iNode;
 142480   int nRef;
 142481   int isDirty;
 142482   u8 *zData;
 142483   RtreeNode *pNext;                 /* Next node in this hash chain */
 142485 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
 142488 ** Structure to store a deserialized rtree record.
 142490 struct RtreeCell {
 142491   i64 iRowid;
 142492   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
 142497 ** Value for the first field of every RtreeMatchArg object. The MATCH
 142498 ** operator tests that the first field of a blob operand matches this
 142499 ** value to avoid operating on invalid blobs (which could cause a segfault).
 142501 #define RTREE_GEOMETRY_MAGIC 0x891245AB
 142504 ** An instance of this structure must be supplied as a blob argument to
 142505 ** the right-hand-side of an SQL MATCH operator used to constrain an
 142506 ** r-tree query.
 142508 struct RtreeMatchArg {
 142509   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
 142510   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
 142511   void *pContext;
 142512   int nParam;
 142513   RtreeDValue aParam[1];
 142517 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
 142518 ** a single instance of the following structure is allocated. It is used
 142519 ** as the context for the user-function created by by s_r_g_c(). The object
 142520 ** is eventually deleted by the destructor mechanism provided by
 142521 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
 142522 ** the geometry callback function).
 142524 struct RtreeGeomCallback {
 142525   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
 142526   void *pContext;
 142529 #ifndef MAX
 142530 # define MAX(x,y) ((x) < (y) ? (y) : (x))
 142531 #endif
 142532 #ifndef MIN
 142533 # define MIN(x,y) ((x) > (y) ? (y) : (x))
 142534 #endif
 142537 ** Functions to deserialize a 16 bit integer, 32 bit real number and
 142538 ** 64 bit integer. The deserialized value is returned.
 142540 static int readInt16(u8 *p){
 142541   return (p[0]<<8) + p[1];
 142543 static void readCoord(u8 *p, RtreeCoord *pCoord){
 142544   u32 i = (
 142545     (((u32)p[0]) << 24) + 
 142546     (((u32)p[1]) << 16) + 
 142547     (((u32)p[2]) <<  8) + 
 142548     (((u32)p[3]) <<  0)
 142550   *(u32 *)pCoord = i;
 142552 static i64 readInt64(u8 *p){
 142553   return (
 142554     (((i64)p[0]) << 56) + 
 142555     (((i64)p[1]) << 48) + 
 142556     (((i64)p[2]) << 40) + 
 142557     (((i64)p[3]) << 32) + 
 142558     (((i64)p[4]) << 24) + 
 142559     (((i64)p[5]) << 16) + 
 142560     (((i64)p[6]) <<  8) + 
 142561     (((i64)p[7]) <<  0)
 142566 ** Functions to serialize a 16 bit integer, 32 bit real number and
 142567 ** 64 bit integer. The value returned is the number of bytes written
 142568 ** to the argument buffer (always 2, 4 and 8 respectively).
 142570 static int writeInt16(u8 *p, int i){
 142571   p[0] = (i>> 8)&0xFF;
 142572   p[1] = (i>> 0)&0xFF;
 142573   return 2;
 142575 static int writeCoord(u8 *p, RtreeCoord *pCoord){
 142576   u32 i;
 142577   assert( sizeof(RtreeCoord)==4 );
 142578   assert( sizeof(u32)==4 );
 142579   i = *(u32 *)pCoord;
 142580   p[0] = (i>>24)&0xFF;
 142581   p[1] = (i>>16)&0xFF;
 142582   p[2] = (i>> 8)&0xFF;
 142583   p[3] = (i>> 0)&0xFF;
 142584   return 4;
 142586 static int writeInt64(u8 *p, i64 i){
 142587   p[0] = (i>>56)&0xFF;
 142588   p[1] = (i>>48)&0xFF;
 142589   p[2] = (i>>40)&0xFF;
 142590   p[3] = (i>>32)&0xFF;
 142591   p[4] = (i>>24)&0xFF;
 142592   p[5] = (i>>16)&0xFF;
 142593   p[6] = (i>> 8)&0xFF;
 142594   p[7] = (i>> 0)&0xFF;
 142595   return 8;
 142599 ** Increment the reference count of node p.
 142601 static void nodeReference(RtreeNode *p){
 142602   if( p ){
 142603     p->nRef++;
 142608 ** Clear the content of node p (set all bytes to 0x00).
 142610 static void nodeZero(Rtree *pRtree, RtreeNode *p){
 142611   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
 142612   p->isDirty = 1;
 142616 ** Given a node number iNode, return the corresponding key to use
 142617 ** in the Rtree.aHash table.
 142619 static int nodeHash(i64 iNode){
 142620   return (
 142621     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
 142622     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
 142623   ) % HASHSIZE;
 142627 ** Search the node hash table for node iNode. If found, return a pointer
 142628 ** to it. Otherwise, return 0.
 142630 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
 142631   RtreeNode *p;
 142632   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
 142633   return p;
 142637 ** Add node pNode to the node hash table.
 142639 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
 142640   int iHash;
 142641   assert( pNode->pNext==0 );
 142642   iHash = nodeHash(pNode->iNode);
 142643   pNode->pNext = pRtree->aHash[iHash];
 142644   pRtree->aHash[iHash] = pNode;
 142648 ** Remove node pNode from the node hash table.
 142650 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
 142651   RtreeNode **pp;
 142652   if( pNode->iNode!=0 ){
 142653     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
 142654     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
 142655     *pp = pNode->pNext;
 142656     pNode->pNext = 0;
 142661 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
 142662 ** indicating that node has not yet been assigned a node number. It is
 142663 ** assigned a node number when nodeWrite() is called to write the
 142664 ** node contents out to the database.
 142666 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
 142667   RtreeNode *pNode;
 142668   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
 142669   if( pNode ){
 142670     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
 142671     pNode->zData = (u8 *)&pNode[1];
 142672     pNode->nRef = 1;
 142673     pNode->pParent = pParent;
 142674     pNode->isDirty = 1;
 142675     nodeReference(pParent);
 142677   return pNode;
 142681 ** Obtain a reference to an r-tree node.
 142683 static int
 142684 nodeAcquire(
 142685   Rtree *pRtree,             /* R-tree structure */
 142686   i64 iNode,                 /* Node number to load */
 142687   RtreeNode *pParent,        /* Either the parent node or NULL */
 142688   RtreeNode **ppNode         /* OUT: Acquired node */
 142690   int rc;
 142691   int rc2 = SQLITE_OK;
 142692   RtreeNode *pNode;
 142694   /* Check if the requested node is already in the hash table. If so,
 142695   ** increase its reference count and return it.
 142697   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
 142698     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
 142699     if( pParent && !pNode->pParent ){
 142700       nodeReference(pParent);
 142701       pNode->pParent = pParent;
 142703     pNode->nRef++;
 142704     *ppNode = pNode;
 142705     return SQLITE_OK;
 142708   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
 142709   rc = sqlite3_step(pRtree->pReadNode);
 142710   if( rc==SQLITE_ROW ){
 142711     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
 142712     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
 142713       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
 142714       if( !pNode ){
 142715         rc2 = SQLITE_NOMEM;
 142716       }else{
 142717         pNode->pParent = pParent;
 142718         pNode->zData = (u8 *)&pNode[1];
 142719         pNode->nRef = 1;
 142720         pNode->iNode = iNode;
 142721         pNode->isDirty = 0;
 142722         pNode->pNext = 0;
 142723         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
 142724         nodeReference(pParent);
 142728   rc = sqlite3_reset(pRtree->pReadNode);
 142729   if( rc==SQLITE_OK ) rc = rc2;
 142731   /* If the root node was just loaded, set pRtree->iDepth to the height
 142732   ** of the r-tree structure. A height of zero means all data is stored on
 142733   ** the root node. A height of one means the children of the root node
 142734   ** are the leaves, and so on. If the depth as specified on the root node
 142735   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
 142737   if( pNode && iNode==1 ){
 142738     pRtree->iDepth = readInt16(pNode->zData);
 142739     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
 142740       rc = SQLITE_CORRUPT_VTAB;
 142744   /* If no error has occurred so far, check if the "number of entries"
 142745   ** field on the node is too large. If so, set the return code to 
 142746   ** SQLITE_CORRUPT_VTAB.
 142748   if( pNode && rc==SQLITE_OK ){
 142749     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
 142750       rc = SQLITE_CORRUPT_VTAB;
 142754   if( rc==SQLITE_OK ){
 142755     if( pNode!=0 ){
 142756       nodeHashInsert(pRtree, pNode);
 142757     }else{
 142758       rc = SQLITE_CORRUPT_VTAB;
 142760     *ppNode = pNode;
 142761   }else{
 142762     sqlite3_free(pNode);
 142763     *ppNode = 0;
 142766   return rc;
 142770 ** Overwrite cell iCell of node pNode with the contents of pCell.
 142772 static void nodeOverwriteCell(
 142773   Rtree *pRtree, 
 142774   RtreeNode *pNode,  
 142775   RtreeCell *pCell, 
 142776   int iCell
 142778   int ii;
 142779   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
 142780   p += writeInt64(p, pCell->iRowid);
 142781   for(ii=0; ii<(pRtree->nDim*2); ii++){
 142782     p += writeCoord(p, &pCell->aCoord[ii]);
 142784   pNode->isDirty = 1;
 142788 ** Remove cell the cell with index iCell from node pNode.
 142790 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
 142791   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
 142792   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
 142793   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
 142794   memmove(pDst, pSrc, nByte);
 142795   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
 142796   pNode->isDirty = 1;
 142800 ** Insert the contents of cell pCell into node pNode. If the insert
 142801 ** is successful, return SQLITE_OK.
 142803 ** If there is not enough free space in pNode, return SQLITE_FULL.
 142805 static int
 142806 nodeInsertCell(
 142807   Rtree *pRtree, 
 142808   RtreeNode *pNode, 
 142809   RtreeCell *pCell 
 142811   int nCell;                    /* Current number of cells in pNode */
 142812   int nMaxCell;                 /* Maximum number of cells for pNode */
 142814   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
 142815   nCell = NCELL(pNode);
 142817   assert( nCell<=nMaxCell );
 142818   if( nCell<nMaxCell ){
 142819     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
 142820     writeInt16(&pNode->zData[2], nCell+1);
 142821     pNode->isDirty = 1;
 142824   return (nCell==nMaxCell);
 142828 ** If the node is dirty, write it out to the database.
 142830 static int
 142831 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
 142832   int rc = SQLITE_OK;
 142833   if( pNode->isDirty ){
 142834     sqlite3_stmt *p = pRtree->pWriteNode;
 142835     if( pNode->iNode ){
 142836       sqlite3_bind_int64(p, 1, pNode->iNode);
 142837     }else{
 142838       sqlite3_bind_null(p, 1);
 142840     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
 142841     sqlite3_step(p);
 142842     pNode->isDirty = 0;
 142843     rc = sqlite3_reset(p);
 142844     if( pNode->iNode==0 && rc==SQLITE_OK ){
 142845       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
 142846       nodeHashInsert(pRtree, pNode);
 142849   return rc;
 142853 ** Release a reference to a node. If the node is dirty and the reference
 142854 ** count drops to zero, the node data is written to the database.
 142856 static int
 142857 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
 142858   int rc = SQLITE_OK;
 142859   if( pNode ){
 142860     assert( pNode->nRef>0 );
 142861     pNode->nRef--;
 142862     if( pNode->nRef==0 ){
 142863       if( pNode->iNode==1 ){
 142864         pRtree->iDepth = -1;
 142866       if( pNode->pParent ){
 142867         rc = nodeRelease(pRtree, pNode->pParent);
 142869       if( rc==SQLITE_OK ){
 142870         rc = nodeWrite(pRtree, pNode);
 142872       nodeHashDelete(pRtree, pNode);
 142873       sqlite3_free(pNode);
 142876   return rc;
 142880 ** Return the 64-bit integer value associated with cell iCell of
 142881 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
 142882 ** an internal node, then the 64-bit integer is a child page number.
 142884 static i64 nodeGetRowid(
 142885   Rtree *pRtree, 
 142886   RtreeNode *pNode, 
 142887   int iCell
 142889   assert( iCell<NCELL(pNode) );
 142890   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
 142894 ** Return coordinate iCoord from cell iCell in node pNode.
 142896 static void nodeGetCoord(
 142897   Rtree *pRtree, 
 142898   RtreeNode *pNode, 
 142899   int iCell,
 142900   int iCoord,
 142901   RtreeCoord *pCoord           /* Space to write result to */
 142903   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
 142907 ** Deserialize cell iCell of node pNode. Populate the structure pointed
 142908 ** to by pCell with the results.
 142910 static void nodeGetCell(
 142911   Rtree *pRtree, 
 142912   RtreeNode *pNode, 
 142913   int iCell,
 142914   RtreeCell *pCell
 142916   int ii;
 142917   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
 142918   for(ii=0; ii<pRtree->nDim*2; ii++){
 142919     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
 142924 /* Forward declaration for the function that does the work of
 142925 ** the virtual table module xCreate() and xConnect() methods.
 142927 static int rtreeInit(
 142928   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
 142932 ** Rtree virtual table module xCreate method.
 142934 static int rtreeCreate(
 142935   sqlite3 *db,
 142936   void *pAux,
 142937   int argc, const char *const*argv,
 142938   sqlite3_vtab **ppVtab,
 142939   char **pzErr
 142941   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
 142945 ** Rtree virtual table module xConnect method.
 142947 static int rtreeConnect(
 142948   sqlite3 *db,
 142949   void *pAux,
 142950   int argc, const char *const*argv,
 142951   sqlite3_vtab **ppVtab,
 142952   char **pzErr
 142954   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
 142958 ** Increment the r-tree reference count.
 142960 static void rtreeReference(Rtree *pRtree){
 142961   pRtree->nBusy++;
 142965 ** Decrement the r-tree reference count. When the reference count reaches
 142966 ** zero the structure is deleted.
 142968 static void rtreeRelease(Rtree *pRtree){
 142969   pRtree->nBusy--;
 142970   if( pRtree->nBusy==0 ){
 142971     sqlite3_finalize(pRtree->pReadNode);
 142972     sqlite3_finalize(pRtree->pWriteNode);
 142973     sqlite3_finalize(pRtree->pDeleteNode);
 142974     sqlite3_finalize(pRtree->pReadRowid);
 142975     sqlite3_finalize(pRtree->pWriteRowid);
 142976     sqlite3_finalize(pRtree->pDeleteRowid);
 142977     sqlite3_finalize(pRtree->pReadParent);
 142978     sqlite3_finalize(pRtree->pWriteParent);
 142979     sqlite3_finalize(pRtree->pDeleteParent);
 142980     sqlite3_free(pRtree);
 142985 ** Rtree virtual table module xDisconnect method.
 142987 static int rtreeDisconnect(sqlite3_vtab *pVtab){
 142988   rtreeRelease((Rtree *)pVtab);
 142989   return SQLITE_OK;
 142993 ** Rtree virtual table module xDestroy method.
 142995 static int rtreeDestroy(sqlite3_vtab *pVtab){
 142996   Rtree *pRtree = (Rtree *)pVtab;
 142997   int rc;
 142998   char *zCreate = sqlite3_mprintf(
 142999     "DROP TABLE '%q'.'%q_node';"
 143000     "DROP TABLE '%q'.'%q_rowid';"
 143001     "DROP TABLE '%q'.'%q_parent';",
 143002     pRtree->zDb, pRtree->zName, 
 143003     pRtree->zDb, pRtree->zName,
 143004     pRtree->zDb, pRtree->zName
 143006   if( !zCreate ){
 143007     rc = SQLITE_NOMEM;
 143008   }else{
 143009     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
 143010     sqlite3_free(zCreate);
 143012   if( rc==SQLITE_OK ){
 143013     rtreeRelease(pRtree);
 143016   return rc;
 143020 ** Rtree virtual table module xOpen method.
 143022 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
 143023   int rc = SQLITE_NOMEM;
 143024   RtreeCursor *pCsr;
 143026   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
 143027   if( pCsr ){
 143028     memset(pCsr, 0, sizeof(RtreeCursor));
 143029     pCsr->base.pVtab = pVTab;
 143030     rc = SQLITE_OK;
 143032   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
 143034   return rc;
 143039 ** Free the RtreeCursor.aConstraint[] array and its contents.
 143041 static void freeCursorConstraints(RtreeCursor *pCsr){
 143042   if( pCsr->aConstraint ){
 143043     int i;                        /* Used to iterate through constraint array */
 143044     for(i=0; i<pCsr->nConstraint; i++){
 143045       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
 143046       if( pGeom ){
 143047         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
 143048         sqlite3_free(pGeom);
 143051     sqlite3_free(pCsr->aConstraint);
 143052     pCsr->aConstraint = 0;
 143057 ** Rtree virtual table module xClose method.
 143059 static int rtreeClose(sqlite3_vtab_cursor *cur){
 143060   Rtree *pRtree = (Rtree *)(cur->pVtab);
 143061   int rc;
 143062   RtreeCursor *pCsr = (RtreeCursor *)cur;
 143063   freeCursorConstraints(pCsr);
 143064   rc = nodeRelease(pRtree, pCsr->pNode);
 143065   sqlite3_free(pCsr);
 143066   return rc;
 143070 ** Rtree virtual table module xEof method.
 143072 ** Return non-zero if the cursor does not currently point to a valid 
 143073 ** record (i.e if the scan has finished), or zero otherwise.
 143075 static int rtreeEof(sqlite3_vtab_cursor *cur){
 143076   RtreeCursor *pCsr = (RtreeCursor *)cur;
 143077   return (pCsr->pNode==0);
 143081 ** The r-tree constraint passed as the second argument to this function is
 143082 ** guaranteed to be a MATCH constraint.
 143084 static int testRtreeGeom(
 143085   Rtree *pRtree,                  /* R-Tree object */
 143086   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
 143087   RtreeCell *pCell,               /* Cell to test */
 143088   int *pbRes                      /* OUT: Test result */
 143090   int i;
 143091   RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
 143092   int nCoord = pRtree->nDim*2;
 143094   assert( pConstraint->op==RTREE_MATCH );
 143095   assert( pConstraint->pGeom );
 143097   for(i=0; i<nCoord; i++){
 143098     aCoord[i] = DCOORD(pCell->aCoord[i]);
 143100   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
 143104 ** Cursor pCursor currently points to a cell in a non-leaf page.
 143105 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
 143106 ** (excluded) by the constraints in the pCursor->aConstraint[] 
 143107 ** array, or false otherwise.
 143109 ** Return SQLITE_OK if successful or an SQLite error code if an error
 143110 ** occurs within a geometry callback.
 143112 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
 143113   RtreeCell cell;
 143114   int ii;
 143115   int bRes = 0;
 143116   int rc = SQLITE_OK;
 143118   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
 143119   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
 143120     RtreeConstraint *p = &pCursor->aConstraint[ii];
 143121     RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
 143122     RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
 143124     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
 143125         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
 143128     switch( p->op ){
 143129       case RTREE_LE: case RTREE_LT: 
 143130         bRes = p->rValue<cell_min; 
 143131         break;
 143133       case RTREE_GE: case RTREE_GT: 
 143134         bRes = p->rValue>cell_max; 
 143135         break;
 143137       case RTREE_EQ:
 143138         bRes = (p->rValue>cell_max || p->rValue<cell_min);
 143139         break;
 143141       default: {
 143142         assert( p->op==RTREE_MATCH );
 143143         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
 143144         bRes = !bRes;
 143145         break;
 143150   *pbEof = bRes;
 143151   return rc;
 143155 ** Test if the cell that cursor pCursor currently points to
 143156 ** would be filtered (excluded) by the constraints in the 
 143157 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
 143158 ** returning. If the cell is not filtered (excluded) by the constraints,
 143159 ** set pbEof to zero.
 143161 ** Return SQLITE_OK if successful or an SQLite error code if an error
 143162 ** occurs within a geometry callback.
 143164 ** This function assumes that the cell is part of a leaf node.
 143166 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
 143167   RtreeCell cell;
 143168   int ii;
 143169   *pbEof = 0;
 143171   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
 143172   for(ii=0; ii<pCursor->nConstraint; ii++){
 143173     RtreeConstraint *p = &pCursor->aConstraint[ii];
 143174     RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
 143175     int res;
 143176     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
 143177         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
 143179     switch( p->op ){
 143180       case RTREE_LE: res = (coord<=p->rValue); break;
 143181       case RTREE_LT: res = (coord<p->rValue);  break;
 143182       case RTREE_GE: res = (coord>=p->rValue); break;
 143183       case RTREE_GT: res = (coord>p->rValue);  break;
 143184       case RTREE_EQ: res = (coord==p->rValue); break;
 143185       default: {
 143186         int rc;
 143187         assert( p->op==RTREE_MATCH );
 143188         rc = testRtreeGeom(pRtree, p, &cell, &res);
 143189         if( rc!=SQLITE_OK ){
 143190           return rc;
 143192         break;
 143196     if( !res ){
 143197       *pbEof = 1;
 143198       return SQLITE_OK;
 143202   return SQLITE_OK;
 143206 ** Cursor pCursor currently points at a node that heads a sub-tree of
 143207 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
 143208 ** to point to the left-most cell of the sub-tree that matches the 
 143209 ** configured constraints.
 143211 static int descendToCell(
 143212   Rtree *pRtree, 
 143213   RtreeCursor *pCursor, 
 143214   int iHeight,
 143215   int *pEof                 /* OUT: Set to true if cannot descend */
 143217   int isEof;
 143218   int rc;
 143219   int ii;
 143220   RtreeNode *pChild;
 143221   sqlite3_int64 iRowid;
 143223   RtreeNode *pSavedNode = pCursor->pNode;
 143224   int iSavedCell = pCursor->iCell;
 143226   assert( iHeight>=0 );
 143228   if( iHeight==0 ){
 143229     rc = testRtreeEntry(pRtree, pCursor, &isEof);
 143230   }else{
 143231     rc = testRtreeCell(pRtree, pCursor, &isEof);
 143233   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
 143234     goto descend_to_cell_out;
 143237   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
 143238   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
 143239   if( rc!=SQLITE_OK ){
 143240     goto descend_to_cell_out;
 143243   nodeRelease(pRtree, pCursor->pNode);
 143244   pCursor->pNode = pChild;
 143245   isEof = 1;
 143246   for(ii=0; isEof && ii<NCELL(pChild); ii++){
 143247     pCursor->iCell = ii;
 143248     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
 143249     if( rc!=SQLITE_OK ){
 143250       goto descend_to_cell_out;
 143254   if( isEof ){
 143255     assert( pCursor->pNode==pChild );
 143256     nodeReference(pSavedNode);
 143257     nodeRelease(pRtree, pChild);
 143258     pCursor->pNode = pSavedNode;
 143259     pCursor->iCell = iSavedCell;
 143262 descend_to_cell_out:
 143263   *pEof = isEof;
 143264   return rc;
 143268 ** One of the cells in node pNode is guaranteed to have a 64-bit 
 143269 ** integer value equal to iRowid. Return the index of this cell.
 143271 static int nodeRowidIndex(
 143272   Rtree *pRtree, 
 143273   RtreeNode *pNode, 
 143274   i64 iRowid,
 143275   int *piIndex
 143277   int ii;
 143278   int nCell = NCELL(pNode);
 143279   for(ii=0; ii<nCell; ii++){
 143280     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
 143281       *piIndex = ii;
 143282       return SQLITE_OK;
 143285   return SQLITE_CORRUPT_VTAB;
 143289 ** Return the index of the cell containing a pointer to node pNode
 143290 ** in its parent. If pNode is the root node, return -1.
 143292 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
 143293   RtreeNode *pParent = pNode->pParent;
 143294   if( pParent ){
 143295     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
 143297   *piIndex = -1;
 143298   return SQLITE_OK;
 143302 ** Rtree virtual table module xNext method.
 143304 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
 143305   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
 143306   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 143307   int rc = SQLITE_OK;
 143309   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
 143310   ** already at EOF. It is against the rules to call the xNext() method of
 143311   ** a cursor that has already reached EOF.
 143313   assert( pCsr->pNode );
 143315   if( pCsr->iStrategy==1 ){
 143316     /* This "scan" is a direct lookup by rowid. There is no next entry. */
 143317     nodeRelease(pRtree, pCsr->pNode);
 143318     pCsr->pNode = 0;
 143319   }else{
 143320     /* Move to the next entry that matches the configured constraints. */
 143321     int iHeight = 0;
 143322     while( pCsr->pNode ){
 143323       RtreeNode *pNode = pCsr->pNode;
 143324       int nCell = NCELL(pNode);
 143325       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
 143326         int isEof;
 143327         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
 143328         if( rc!=SQLITE_OK || !isEof ){
 143329           return rc;
 143332       pCsr->pNode = pNode->pParent;
 143333       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
 143334       if( rc!=SQLITE_OK ){
 143335         return rc;
 143337       nodeReference(pCsr->pNode);
 143338       nodeRelease(pRtree, pNode);
 143339       iHeight++;
 143343   return rc;
 143347 ** Rtree virtual table module xRowid method.
 143349 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
 143350   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
 143351   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 143353   assert(pCsr->pNode);
 143354   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
 143356   return SQLITE_OK;
 143360 ** Rtree virtual table module xColumn method.
 143362 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
 143363   Rtree *pRtree = (Rtree *)cur->pVtab;
 143364   RtreeCursor *pCsr = (RtreeCursor *)cur;
 143366   if( i==0 ){
 143367     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
 143368     sqlite3_result_int64(ctx, iRowid);
 143369   }else{
 143370     RtreeCoord c;
 143371     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
 143372 #ifndef SQLITE_RTREE_INT_ONLY
 143373     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 143374       sqlite3_result_double(ctx, c.f);
 143375     }else
 143376 #endif
 143378       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
 143379       sqlite3_result_int(ctx, c.i);
 143383   return SQLITE_OK;
 143387 ** Use nodeAcquire() to obtain the leaf node containing the record with 
 143388 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
 143389 ** return SQLITE_OK. If there is no such record in the table, set
 143390 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
 143391 ** to zero and return an SQLite error code.
 143393 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
 143394   int rc;
 143395   *ppLeaf = 0;
 143396   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
 143397   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
 143398     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
 143399     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
 143400     sqlite3_reset(pRtree->pReadRowid);
 143401   }else{
 143402     rc = sqlite3_reset(pRtree->pReadRowid);
 143404   return rc;
 143408 ** This function is called to configure the RtreeConstraint object passed
 143409 ** as the second argument for a MATCH constraint. The value passed as the
 143410 ** first argument to this function is the right-hand operand to the MATCH
 143411 ** operator.
 143413 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
 143414   RtreeMatchArg *p;
 143415   sqlite3_rtree_geometry *pGeom;
 143416   int nBlob;
 143418   /* Check that value is actually a blob. */
 143419   if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
 143421   /* Check that the blob is roughly the right size. */
 143422   nBlob = sqlite3_value_bytes(pValue);
 143423   if( nBlob<(int)sizeof(RtreeMatchArg) 
 143424    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
 143426     return SQLITE_ERROR;
 143429   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
 143430       sizeof(sqlite3_rtree_geometry) + nBlob
 143432   if( !pGeom ) return SQLITE_NOMEM;
 143433   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
 143434   p = (RtreeMatchArg *)&pGeom[1];
 143436   memcpy(p, sqlite3_value_blob(pValue), nBlob);
 143437   if( p->magic!=RTREE_GEOMETRY_MAGIC 
 143438    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
 143440     sqlite3_free(pGeom);
 143441     return SQLITE_ERROR;
 143444   pGeom->pContext = p->pContext;
 143445   pGeom->nParam = p->nParam;
 143446   pGeom->aParam = p->aParam;
 143448   pCons->xGeom = p->xGeom;
 143449   pCons->pGeom = pGeom;
 143450   return SQLITE_OK;
 143454 ** Rtree virtual table module xFilter method.
 143456 static int rtreeFilter(
 143457   sqlite3_vtab_cursor *pVtabCursor, 
 143458   int idxNum, const char *idxStr,
 143459   int argc, sqlite3_value **argv
 143461   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
 143462   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 143464   RtreeNode *pRoot = 0;
 143465   int ii;
 143466   int rc = SQLITE_OK;
 143468   rtreeReference(pRtree);
 143470   freeCursorConstraints(pCsr);
 143471   pCsr->iStrategy = idxNum;
 143473   if( idxNum==1 ){
 143474     /* Special case - lookup by rowid. */
 143475     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
 143476     i64 iRowid = sqlite3_value_int64(argv[0]);
 143477     rc = findLeafNode(pRtree, iRowid, &pLeaf);
 143478     pCsr->pNode = pLeaf; 
 143479     if( pLeaf ){
 143480       assert( rc==SQLITE_OK );
 143481       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
 143483   }else{
 143484     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
 143485     ** with the configured constraints. 
 143487     if( argc>0 ){
 143488       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
 143489       pCsr->nConstraint = argc;
 143490       if( !pCsr->aConstraint ){
 143491         rc = SQLITE_NOMEM;
 143492       }else{
 143493         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
 143494         assert( (idxStr==0 && argc==0)
 143495                 || (idxStr && (int)strlen(idxStr)==argc*2) );
 143496         for(ii=0; ii<argc; ii++){
 143497           RtreeConstraint *p = &pCsr->aConstraint[ii];
 143498           p->op = idxStr[ii*2];
 143499           p->iCoord = idxStr[ii*2+1]-'a';
 143500           if( p->op==RTREE_MATCH ){
 143501             /* A MATCH operator. The right-hand-side must be a blob that
 143502             ** can be cast into an RtreeMatchArg object. One created using
 143503             ** an sqlite3_rtree_geometry_callback() SQL user function.
 143505             rc = deserializeGeometry(argv[ii], p);
 143506             if( rc!=SQLITE_OK ){
 143507               break;
 143509           }else{
 143510 #ifdef SQLITE_RTREE_INT_ONLY
 143511             p->rValue = sqlite3_value_int64(argv[ii]);
 143512 #else
 143513             p->rValue = sqlite3_value_double(argv[ii]);
 143514 #endif
 143520     if( rc==SQLITE_OK ){
 143521       pCsr->pNode = 0;
 143522       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
 143524     if( rc==SQLITE_OK ){
 143525       int isEof = 1;
 143526       int nCell = NCELL(pRoot);
 143527       pCsr->pNode = pRoot;
 143528       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
 143529         assert( pCsr->pNode==pRoot );
 143530         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
 143531         if( !isEof ){
 143532           break;
 143535       if( rc==SQLITE_OK && isEof ){
 143536         assert( pCsr->pNode==pRoot );
 143537         nodeRelease(pRtree, pRoot);
 143538         pCsr->pNode = 0;
 143540       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
 143544   rtreeRelease(pRtree);
 143545   return rc;
 143549 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
 143550 ** extension is currently being used by a version of SQLite too old to
 143551 ** support estimatedRows. In that case this function is a no-op.
 143553 static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
 143554 #if SQLITE_VERSION_NUMBER>=3008002
 143555   if( sqlite3_libversion_number()>=3008002 ){
 143556     pIdxInfo->estimatedRows = nRow;
 143558 #endif
 143562 ** Rtree virtual table module xBestIndex method. There are three
 143563 ** table scan strategies to choose from (in order from most to 
 143564 ** least desirable):
 143566 **   idxNum     idxStr        Strategy
 143567 **   ------------------------------------------------
 143568 **     1        Unused        Direct lookup by rowid.
 143569 **     2        See below     R-tree query or full-table scan.
 143570 **   ------------------------------------------------
 143572 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
 143573 ** 2 is used, idxStr is formatted to contain 2 bytes for each 
 143574 ** constraint used. The first two bytes of idxStr correspond to 
 143575 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
 143576 ** (argvIndex==1) etc.
 143578 ** The first of each pair of bytes in idxStr identifies the constraint
 143579 ** operator as follows:
 143581 **   Operator    Byte Value
 143582 **   ----------------------
 143583 **      =        0x41 ('A')
 143584 **     <=        0x42 ('B')
 143585 **      <        0x43 ('C')
 143586 **     >=        0x44 ('D')
 143587 **      >        0x45 ('E')
 143588 **   MATCH       0x46 ('F')
 143589 **   ----------------------
 143591 ** The second of each pair of bytes identifies the coordinate column
 143592 ** to which the constraint applies. The leftmost coordinate column
 143593 ** is 'a', the second from the left 'b' etc.
 143595 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
 143596   Rtree *pRtree = (Rtree*)tab;
 143597   int rc = SQLITE_OK;
 143598   int ii;
 143599   i64 nRow;                       /* Estimated rows returned by this scan */
 143601   int iIdx = 0;
 143602   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
 143603   memset(zIdxStr, 0, sizeof(zIdxStr));
 143605   assert( pIdxInfo->idxStr==0 );
 143606   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
 143607     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
 143609     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
 143610       /* We have an equality constraint on the rowid. Use strategy 1. */
 143611       int jj;
 143612       for(jj=0; jj<ii; jj++){
 143613         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
 143614         pIdxInfo->aConstraintUsage[jj].omit = 0;
 143616       pIdxInfo->idxNum = 1;
 143617       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
 143618       pIdxInfo->aConstraintUsage[jj].omit = 1;
 143620       /* This strategy involves a two rowid lookups on an B-Tree structures
 143621       ** and then a linear search of an R-Tree node. This should be 
 143622       ** considered almost as quick as a direct rowid lookup (for which 
 143623       ** sqlite uses an internal cost of 0.0). It is expected to return
 143624       ** a single row.
 143626       pIdxInfo->estimatedCost = 30.0;
 143627       setEstimatedRows(pIdxInfo, 1);
 143628       return SQLITE_OK;
 143631     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
 143632       u8 op;
 143633       switch( p->op ){
 143634         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
 143635         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
 143636         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
 143637         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
 143638         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
 143639         default:
 143640           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
 143641           op = RTREE_MATCH; 
 143642           break;
 143644       zIdxStr[iIdx++] = op;
 143645       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
 143646       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
 143647       pIdxInfo->aConstraintUsage[ii].omit = 1;
 143651   pIdxInfo->idxNum = 2;
 143652   pIdxInfo->needToFreeIdxStr = 1;
 143653   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
 143654     return SQLITE_NOMEM;
 143657   nRow = pRtree->nRowEst / (iIdx + 1);
 143658   pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
 143659   setEstimatedRows(pIdxInfo, nRow);
 143661   return rc;
 143665 ** Return the N-dimensional volumn of the cell stored in *p.
 143667 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
 143668   RtreeDValue area = (RtreeDValue)1;
 143669   int ii;
 143670   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 143671     area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
 143673   return area;
 143677 ** Return the margin length of cell p. The margin length is the sum
 143678 ** of the objects size in each dimension.
 143680 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
 143681   RtreeDValue margin = (RtreeDValue)0;
 143682   int ii;
 143683   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 143684     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
 143686   return margin;
 143690 ** Store the union of cells p1 and p2 in p1.
 143692 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
 143693   int ii;
 143694   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 143695     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 143696       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
 143697       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
 143699   }else{
 143700     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 143701       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
 143702       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
 143708 ** Return true if the area covered by p2 is a subset of the area covered
 143709 ** by p1. False otherwise.
 143711 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
 143712   int ii;
 143713   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
 143714   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 143715     RtreeCoord *a1 = &p1->aCoord[ii];
 143716     RtreeCoord *a2 = &p2->aCoord[ii];
 143717     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
 143718      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
 143720       return 0;
 143723   return 1;
 143727 ** Return the amount cell p would grow by if it were unioned with pCell.
 143729 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
 143730   RtreeDValue area;
 143731   RtreeCell cell;
 143732   memcpy(&cell, p, sizeof(RtreeCell));
 143733   area = cellArea(pRtree, &cell);
 143734   cellUnion(pRtree, &cell, pCell);
 143735   return (cellArea(pRtree, &cell)-area);
 143738 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
 143739 static RtreeDValue cellOverlap(
 143740   Rtree *pRtree, 
 143741   RtreeCell *p, 
 143742   RtreeCell *aCell, 
 143743   int nCell, 
 143744   int iExclude
 143746   int ii;
 143747   RtreeDValue overlap = 0.0;
 143748   for(ii=0; ii<nCell; ii++){
 143749 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 143750     if( ii!=iExclude )
 143751 #else
 143752     assert( iExclude==-1 );
 143753     UNUSED_PARAMETER(iExclude);
 143754 #endif
 143756       int jj;
 143757       RtreeDValue o = (RtreeDValue)1;
 143758       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
 143759         RtreeDValue x1, x2;
 143761         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
 143762         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
 143764         if( x2<x1 ){
 143765           o = 0.0;
 143766           break;
 143767         }else{
 143768           o = o * (x2-x1);
 143771       overlap += o;
 143774   return overlap;
 143776 #endif
 143778 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 143779 static RtreeDValue cellOverlapEnlargement(
 143780   Rtree *pRtree, 
 143781   RtreeCell *p, 
 143782   RtreeCell *pInsert, 
 143783   RtreeCell *aCell, 
 143784   int nCell, 
 143785   int iExclude
 143787   RtreeDValue before, after;
 143788   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
 143789   cellUnion(pRtree, p, pInsert);
 143790   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
 143791   return (after-before);
 143793 #endif
 143797 ** This function implements the ChooseLeaf algorithm from Gutman[84].
 143798 ** ChooseSubTree in r*tree terminology.
 143800 static int ChooseLeaf(
 143801   Rtree *pRtree,               /* Rtree table */
 143802   RtreeCell *pCell,            /* Cell to insert into rtree */
 143803   int iHeight,                 /* Height of sub-tree rooted at pCell */
 143804   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
 143806   int rc;
 143807   int ii;
 143808   RtreeNode *pNode;
 143809   rc = nodeAcquire(pRtree, 1, 0, &pNode);
 143811   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
 143812     int iCell;
 143813     sqlite3_int64 iBest = 0;
 143815     RtreeDValue fMinGrowth = 0.0;
 143816     RtreeDValue fMinArea = 0.0;
 143817 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 143818     RtreeDValue fMinOverlap = 0.0;
 143819     RtreeDValue overlap;
 143820 #endif
 143822     int nCell = NCELL(pNode);
 143823     RtreeCell cell;
 143824     RtreeNode *pChild;
 143826     RtreeCell *aCell = 0;
 143828 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 143829     if( ii==(pRtree->iDepth-1) ){
 143830       int jj;
 143831       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
 143832       if( !aCell ){
 143833         rc = SQLITE_NOMEM;
 143834         nodeRelease(pRtree, pNode);
 143835         pNode = 0;
 143836         continue;
 143838       for(jj=0; jj<nCell; jj++){
 143839         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
 143842 #endif
 143844     /* Select the child node which will be enlarged the least if pCell
 143845     ** is inserted into it. Resolve ties by choosing the entry with
 143846     ** the smallest area.
 143848     for(iCell=0; iCell<nCell; iCell++){
 143849       int bBest = 0;
 143850       RtreeDValue growth;
 143851       RtreeDValue area;
 143852       nodeGetCell(pRtree, pNode, iCell, &cell);
 143853       growth = cellGrowth(pRtree, &cell, pCell);
 143854       area = cellArea(pRtree, &cell);
 143856 #if VARIANT_RSTARTREE_CHOOSESUBTREE
 143857       if( ii==(pRtree->iDepth-1) ){
 143858         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
 143859       }else{
 143860         overlap = 0.0;
 143862       if( (iCell==0) 
 143863        || (overlap<fMinOverlap) 
 143864        || (overlap==fMinOverlap && growth<fMinGrowth)
 143865        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
 143867         bBest = 1;
 143868         fMinOverlap = overlap;
 143870 #else
 143871       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
 143872         bBest = 1;
 143874 #endif
 143875       if( bBest ){
 143876         fMinGrowth = growth;
 143877         fMinArea = area;
 143878         iBest = cell.iRowid;
 143882     sqlite3_free(aCell);
 143883     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
 143884     nodeRelease(pRtree, pNode);
 143885     pNode = pChild;
 143888   *ppLeaf = pNode;
 143889   return rc;
 143893 ** A cell with the same content as pCell has just been inserted into
 143894 ** the node pNode. This function updates the bounding box cells in
 143895 ** all ancestor elements.
 143897 static int AdjustTree(
 143898   Rtree *pRtree,                    /* Rtree table */
 143899   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
 143900   RtreeCell *pCell                  /* This cell was just inserted */
 143902   RtreeNode *p = pNode;
 143903   while( p->pParent ){
 143904     RtreeNode *pParent = p->pParent;
 143905     RtreeCell cell;
 143906     int iCell;
 143908     if( nodeParentIndex(pRtree, p, &iCell) ){
 143909       return SQLITE_CORRUPT_VTAB;
 143912     nodeGetCell(pRtree, pParent, iCell, &cell);
 143913     if( !cellContains(pRtree, &cell, pCell) ){
 143914       cellUnion(pRtree, &cell, pCell);
 143915       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
 143918     p = pParent;
 143920   return SQLITE_OK;
 143924 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
 143926 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
 143927   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
 143928   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
 143929   sqlite3_step(pRtree->pWriteRowid);
 143930   return sqlite3_reset(pRtree->pWriteRowid);
 143934 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
 143936 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
 143937   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
 143938   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
 143939   sqlite3_step(pRtree->pWriteParent);
 143940   return sqlite3_reset(pRtree->pWriteParent);
 143943 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
 143945 #if VARIANT_GUTTMAN_LINEAR_SPLIT
 143947 ** Implementation of the linear variant of the PickNext() function from
 143948 ** Guttman[84].
 143950 static RtreeCell *LinearPickNext(
 143951   Rtree *pRtree,
 143952   RtreeCell *aCell, 
 143953   int nCell, 
 143954   RtreeCell *pLeftBox, 
 143955   RtreeCell *pRightBox,
 143956   int *aiUsed
 143958   int ii;
 143959   for(ii=0; aiUsed[ii]; ii++);
 143960   aiUsed[ii] = 1;
 143961   return &aCell[ii];
 143965 ** Implementation of the linear variant of the PickSeeds() function from
 143966 ** Guttman[84].
 143968 static void LinearPickSeeds(
 143969   Rtree *pRtree,
 143970   RtreeCell *aCell, 
 143971   int nCell, 
 143972   int *piLeftSeed, 
 143973   int *piRightSeed
 143975   int i;
 143976   int iLeftSeed = 0;
 143977   int iRightSeed = 1;
 143978   RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
 143980   /* Pick two "seed" cells from the array of cells. The algorithm used
 143981   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
 143982   ** indices of the two seed cells in the array are stored in local
 143983   ** variables iLeftSeek and iRightSeed.
 143985   for(i=0; i<pRtree->nDim; i++){
 143986     RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
 143987     RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
 143988     RtreeDValue x3 = x1;
 143989     RtreeDValue x4 = x2;
 143990     int jj;
 143992     int iCellLeft = 0;
 143993     int iCellRight = 0;
 143995     for(jj=1; jj<nCell; jj++){
 143996       RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
 143997       RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
 143999       if( left<x1 ) x1 = left;
 144000       if( right>x4 ) x4 = right;
 144001       if( left>x3 ){
 144002         x3 = left;
 144003         iCellRight = jj;
 144005       if( right<x2 ){
 144006         x2 = right;
 144007         iCellLeft = jj;
 144011     if( x4!=x1 ){
 144012       RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
 144013       if( normalwidth>maxNormalInnerWidth ){
 144014         iLeftSeed = iCellLeft;
 144015         iRightSeed = iCellRight;
 144020   *piLeftSeed = iLeftSeed;
 144021   *piRightSeed = iRightSeed;
 144023 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
 144025 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
 144027 ** Implementation of the quadratic variant of the PickNext() function from
 144028 ** Guttman[84].
 144030 static RtreeCell *QuadraticPickNext(
 144031   Rtree *pRtree,
 144032   RtreeCell *aCell, 
 144033   int nCell, 
 144034   RtreeCell *pLeftBox, 
 144035   RtreeCell *pRightBox,
 144036   int *aiUsed
 144038   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
 144040   int iSelect = -1;
 144041   RtreeDValue fDiff;
 144042   int ii;
 144043   for(ii=0; ii<nCell; ii++){
 144044     if( aiUsed[ii]==0 ){
 144045       RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
 144046       RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
 144047       RtreeDValue diff = FABS(right-left);
 144048       if( iSelect<0 || diff>fDiff ){
 144049         fDiff = diff;
 144050         iSelect = ii;
 144054   aiUsed[iSelect] = 1;
 144055   return &aCell[iSelect];
 144059 ** Implementation of the quadratic variant of the PickSeeds() function from
 144060 ** Guttman[84].
 144062 static void QuadraticPickSeeds(
 144063   Rtree *pRtree,
 144064   RtreeCell *aCell, 
 144065   int nCell, 
 144066   int *piLeftSeed, 
 144067   int *piRightSeed
 144069   int ii;
 144070   int jj;
 144072   int iLeftSeed = 0;
 144073   int iRightSeed = 1;
 144074   RtreeDValue fWaste = 0.0;
 144076   for(ii=0; ii<nCell; ii++){
 144077     for(jj=ii+1; jj<nCell; jj++){
 144078       RtreeDValue right = cellArea(pRtree, &aCell[jj]);
 144079       RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
 144080       RtreeDValue waste = growth - right;
 144082       if( waste>fWaste ){
 144083         iLeftSeed = ii;
 144084         iRightSeed = jj;
 144085         fWaste = waste;
 144090   *piLeftSeed = iLeftSeed;
 144091   *piRightSeed = iRightSeed;
 144093 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
 144096 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
 144097 ** nIdx. The aIdx array contains the set of integers from 0 to 
 144098 ** (nIdx-1) in no particular order. This function sorts the values
 144099 ** in aIdx according to the indexed values in aDistance. For
 144100 ** example, assuming the inputs:
 144102 **   aIdx      = { 0,   1,   2,   3 }
 144103 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
 144105 ** this function sets the aIdx array to contain:
 144107 **   aIdx      = { 0,   1,   2,   3 }
 144109 ** The aSpare array is used as temporary working space by the
 144110 ** sorting algorithm.
 144112 static void SortByDistance(
 144113   int *aIdx, 
 144114   int nIdx, 
 144115   RtreeDValue *aDistance, 
 144116   int *aSpare
 144118   if( nIdx>1 ){
 144119     int iLeft = 0;
 144120     int iRight = 0;
 144122     int nLeft = nIdx/2;
 144123     int nRight = nIdx-nLeft;
 144124     int *aLeft = aIdx;
 144125     int *aRight = &aIdx[nLeft];
 144127     SortByDistance(aLeft, nLeft, aDistance, aSpare);
 144128     SortByDistance(aRight, nRight, aDistance, aSpare);
 144130     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
 144131     aLeft = aSpare;
 144133     while( iLeft<nLeft || iRight<nRight ){
 144134       if( iLeft==nLeft ){
 144135         aIdx[iLeft+iRight] = aRight[iRight];
 144136         iRight++;
 144137       }else if( iRight==nRight ){
 144138         aIdx[iLeft+iRight] = aLeft[iLeft];
 144139         iLeft++;
 144140       }else{
 144141         RtreeDValue fLeft = aDistance[aLeft[iLeft]];
 144142         RtreeDValue fRight = aDistance[aRight[iRight]];
 144143         if( fLeft<fRight ){
 144144           aIdx[iLeft+iRight] = aLeft[iLeft];
 144145           iLeft++;
 144146         }else{
 144147           aIdx[iLeft+iRight] = aRight[iRight];
 144148           iRight++;
 144153 #if 0
 144154     /* Check that the sort worked */
 144156       int jj;
 144157       for(jj=1; jj<nIdx; jj++){
 144158         RtreeDValue left = aDistance[aIdx[jj-1]];
 144159         RtreeDValue right = aDistance[aIdx[jj]];
 144160         assert( left<=right );
 144163 #endif
 144168 ** Arguments aIdx, aCell and aSpare all point to arrays of size
 144169 ** nIdx. The aIdx array contains the set of integers from 0 to 
 144170 ** (nIdx-1) in no particular order. This function sorts the values
 144171 ** in aIdx according to dimension iDim of the cells in aCell. The
 144172 ** minimum value of dimension iDim is considered first, the
 144173 ** maximum used to break ties.
 144175 ** The aSpare array is used as temporary working space by the
 144176 ** sorting algorithm.
 144178 static void SortByDimension(
 144179   Rtree *pRtree,
 144180   int *aIdx, 
 144181   int nIdx, 
 144182   int iDim, 
 144183   RtreeCell *aCell, 
 144184   int *aSpare
 144186   if( nIdx>1 ){
 144188     int iLeft = 0;
 144189     int iRight = 0;
 144191     int nLeft = nIdx/2;
 144192     int nRight = nIdx-nLeft;
 144193     int *aLeft = aIdx;
 144194     int *aRight = &aIdx[nLeft];
 144196     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
 144197     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
 144199     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
 144200     aLeft = aSpare;
 144201     while( iLeft<nLeft || iRight<nRight ){
 144202       RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
 144203       RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
 144204       RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
 144205       RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
 144206       if( (iLeft!=nLeft) && ((iRight==nRight)
 144207        || (xleft1<xright1)
 144208        || (xleft1==xright1 && xleft2<xright2)
 144209       )){
 144210         aIdx[iLeft+iRight] = aLeft[iLeft];
 144211         iLeft++;
 144212       }else{
 144213         aIdx[iLeft+iRight] = aRight[iRight];
 144214         iRight++;
 144218 #if 0
 144219     /* Check that the sort worked */
 144221       int jj;
 144222       for(jj=1; jj<nIdx; jj++){
 144223         RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
 144224         RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
 144225         RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
 144226         RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
 144227         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
 144230 #endif
 144234 #if VARIANT_RSTARTREE_SPLIT
 144236 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
 144238 static int splitNodeStartree(
 144239   Rtree *pRtree,
 144240   RtreeCell *aCell,
 144241   int nCell,
 144242   RtreeNode *pLeft,
 144243   RtreeNode *pRight,
 144244   RtreeCell *pBboxLeft,
 144245   RtreeCell *pBboxRight
 144247   int **aaSorted;
 144248   int *aSpare;
 144249   int ii;
 144251   int iBestDim = 0;
 144252   int iBestSplit = 0;
 144253   RtreeDValue fBestMargin = 0.0;
 144255   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
 144257   aaSorted = (int **)sqlite3_malloc(nByte);
 144258   if( !aaSorted ){
 144259     return SQLITE_NOMEM;
 144262   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
 144263   memset(aaSorted, 0, nByte);
 144264   for(ii=0; ii<pRtree->nDim; ii++){
 144265     int jj;
 144266     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
 144267     for(jj=0; jj<nCell; jj++){
 144268       aaSorted[ii][jj] = jj;
 144270     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
 144273   for(ii=0; ii<pRtree->nDim; ii++){
 144274     RtreeDValue margin = 0.0;
 144275     RtreeDValue fBestOverlap = 0.0;
 144276     RtreeDValue fBestArea = 0.0;
 144277     int iBestLeft = 0;
 144278     int nLeft;
 144280     for(
 144281       nLeft=RTREE_MINCELLS(pRtree); 
 144282       nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
 144283       nLeft++
 144285       RtreeCell left;
 144286       RtreeCell right;
 144287       int kk;
 144288       RtreeDValue overlap;
 144289       RtreeDValue area;
 144291       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
 144292       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
 144293       for(kk=1; kk<(nCell-1); kk++){
 144294         if( kk<nLeft ){
 144295           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
 144296         }else{
 144297           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
 144300       margin += cellMargin(pRtree, &left);
 144301       margin += cellMargin(pRtree, &right);
 144302       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
 144303       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
 144304       if( (nLeft==RTREE_MINCELLS(pRtree))
 144305        || (overlap<fBestOverlap)
 144306        || (overlap==fBestOverlap && area<fBestArea)
 144308         iBestLeft = nLeft;
 144309         fBestOverlap = overlap;
 144310         fBestArea = area;
 144314     if( ii==0 || margin<fBestMargin ){
 144315       iBestDim = ii;
 144316       fBestMargin = margin;
 144317       iBestSplit = iBestLeft;
 144321   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
 144322   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
 144323   for(ii=0; ii<nCell; ii++){
 144324     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
 144325     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
 144326     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
 144327     nodeInsertCell(pRtree, pTarget, pCell);
 144328     cellUnion(pRtree, pBbox, pCell);
 144331   sqlite3_free(aaSorted);
 144332   return SQLITE_OK;
 144334 #endif
 144336 #if VARIANT_GUTTMAN_SPLIT
 144338 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
 144340 static int splitNodeGuttman(
 144341   Rtree *pRtree,
 144342   RtreeCell *aCell,
 144343   int nCell,
 144344   RtreeNode *pLeft,
 144345   RtreeNode *pRight,
 144346   RtreeCell *pBboxLeft,
 144347   RtreeCell *pBboxRight
 144349   int iLeftSeed = 0;
 144350   int iRightSeed = 1;
 144351   int *aiUsed;
 144352   int i;
 144354   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
 144355   if( !aiUsed ){
 144356     return SQLITE_NOMEM;
 144358   memset(aiUsed, 0, sizeof(int)*nCell);
 144360   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
 144362   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
 144363   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
 144364   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
 144365   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
 144366   aiUsed[iLeftSeed] = 1;
 144367   aiUsed[iRightSeed] = 1;
 144369   for(i=nCell-2; i>0; i--){
 144370     RtreeCell *pNext;
 144371     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
 144372     RtreeDValue diff =  
 144373       cellGrowth(pRtree, pBboxLeft, pNext) - 
 144374       cellGrowth(pRtree, pBboxRight, pNext)
 144376     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
 144377      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
 144379       nodeInsertCell(pRtree, pRight, pNext);
 144380       cellUnion(pRtree, pBboxRight, pNext);
 144381     }else{
 144382       nodeInsertCell(pRtree, pLeft, pNext);
 144383       cellUnion(pRtree, pBboxLeft, pNext);
 144387   sqlite3_free(aiUsed);
 144388   return SQLITE_OK;
 144390 #endif
 144392 static int updateMapping(
 144393   Rtree *pRtree, 
 144394   i64 iRowid, 
 144395   RtreeNode *pNode, 
 144396   int iHeight
 144398   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
 144399   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
 144400   if( iHeight>0 ){
 144401     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
 144402     if( pChild ){
 144403       nodeRelease(pRtree, pChild->pParent);
 144404       nodeReference(pNode);
 144405       pChild->pParent = pNode;
 144408   return xSetMapping(pRtree, iRowid, pNode->iNode);
 144411 static int SplitNode(
 144412   Rtree *pRtree,
 144413   RtreeNode *pNode,
 144414   RtreeCell *pCell,
 144415   int iHeight
 144417   int i;
 144418   int newCellIsRight = 0;
 144420   int rc = SQLITE_OK;
 144421   int nCell = NCELL(pNode);
 144422   RtreeCell *aCell;
 144423   int *aiUsed;
 144425   RtreeNode *pLeft = 0;
 144426   RtreeNode *pRight = 0;
 144428   RtreeCell leftbbox;
 144429   RtreeCell rightbbox;
 144431   /* Allocate an array and populate it with a copy of pCell and 
 144432   ** all cells from node pLeft. Then zero the original node.
 144434   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
 144435   if( !aCell ){
 144436     rc = SQLITE_NOMEM;
 144437     goto splitnode_out;
 144439   aiUsed = (int *)&aCell[nCell+1];
 144440   memset(aiUsed, 0, sizeof(int)*(nCell+1));
 144441   for(i=0; i<nCell; i++){
 144442     nodeGetCell(pRtree, pNode, i, &aCell[i]);
 144444   nodeZero(pRtree, pNode);
 144445   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
 144446   nCell++;
 144448   if( pNode->iNode==1 ){
 144449     pRight = nodeNew(pRtree, pNode);
 144450     pLeft = nodeNew(pRtree, pNode);
 144451     pRtree->iDepth++;
 144452     pNode->isDirty = 1;
 144453     writeInt16(pNode->zData, pRtree->iDepth);
 144454   }else{
 144455     pLeft = pNode;
 144456     pRight = nodeNew(pRtree, pLeft->pParent);
 144457     nodeReference(pLeft);
 144460   if( !pLeft || !pRight ){
 144461     rc = SQLITE_NOMEM;
 144462     goto splitnode_out;
 144465   memset(pLeft->zData, 0, pRtree->iNodeSize);
 144466   memset(pRight->zData, 0, pRtree->iNodeSize);
 144468   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
 144469   if( rc!=SQLITE_OK ){
 144470     goto splitnode_out;
 144473   /* Ensure both child nodes have node numbers assigned to them by calling
 144474   ** nodeWrite(). Node pRight always needs a node number, as it was created
 144475   ** by nodeNew() above. But node pLeft sometimes already has a node number.
 144476   ** In this case avoid the all to nodeWrite().
 144478   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
 144479    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
 144481     goto splitnode_out;
 144484   rightbbox.iRowid = pRight->iNode;
 144485   leftbbox.iRowid = pLeft->iNode;
 144487   if( pNode->iNode==1 ){
 144488     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
 144489     if( rc!=SQLITE_OK ){
 144490       goto splitnode_out;
 144492   }else{
 144493     RtreeNode *pParent = pLeft->pParent;
 144494     int iCell;
 144495     rc = nodeParentIndex(pRtree, pLeft, &iCell);
 144496     if( rc==SQLITE_OK ){
 144497       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
 144498       rc = AdjustTree(pRtree, pParent, &leftbbox);
 144500     if( rc!=SQLITE_OK ){
 144501       goto splitnode_out;
 144504   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
 144505     goto splitnode_out;
 144508   for(i=0; i<NCELL(pRight); i++){
 144509     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
 144510     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
 144511     if( iRowid==pCell->iRowid ){
 144512       newCellIsRight = 1;
 144514     if( rc!=SQLITE_OK ){
 144515       goto splitnode_out;
 144518   if( pNode->iNode==1 ){
 144519     for(i=0; i<NCELL(pLeft); i++){
 144520       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
 144521       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
 144522       if( rc!=SQLITE_OK ){
 144523         goto splitnode_out;
 144526   }else if( newCellIsRight==0 ){
 144527     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
 144530   if( rc==SQLITE_OK ){
 144531     rc = nodeRelease(pRtree, pRight);
 144532     pRight = 0;
 144534   if( rc==SQLITE_OK ){
 144535     rc = nodeRelease(pRtree, pLeft);
 144536     pLeft = 0;
 144539 splitnode_out:
 144540   nodeRelease(pRtree, pRight);
 144541   nodeRelease(pRtree, pLeft);
 144542   sqlite3_free(aCell);
 144543   return rc;
 144547 ** If node pLeaf is not the root of the r-tree and its pParent pointer is 
 144548 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
 144549 ** the pLeaf->pParent chain all the way up to the root node.
 144551 ** This operation is required when a row is deleted (or updated - an update
 144552 ** is implemented as a delete followed by an insert). SQLite provides the
 144553 ** rowid of the row to delete, which can be used to find the leaf on which
 144554 ** the entry resides (argument pLeaf). Once the leaf is located, this 
 144555 ** function is called to determine its ancestry.
 144557 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
 144558   int rc = SQLITE_OK;
 144559   RtreeNode *pChild = pLeaf;
 144560   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
 144561     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
 144562     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
 144563     rc = sqlite3_step(pRtree->pReadParent);
 144564     if( rc==SQLITE_ROW ){
 144565       RtreeNode *pTest;           /* Used to test for reference loops */
 144566       i64 iNode;                  /* Node number of parent node */
 144568       /* Before setting pChild->pParent, test that we are not creating a
 144569       ** loop of references (as we would if, say, pChild==pParent). We don't
 144570       ** want to do this as it leads to a memory leak when trying to delete
 144571       ** the referenced counted node structures.
 144573       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
 144574       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
 144575       if( !pTest ){
 144576         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
 144579     rc = sqlite3_reset(pRtree->pReadParent);
 144580     if( rc==SQLITE_OK ) rc = rc2;
 144581     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
 144582     pChild = pChild->pParent;
 144584   return rc;
 144587 static int deleteCell(Rtree *, RtreeNode *, int, int);
 144589 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
 144590   int rc;
 144591   int rc2;
 144592   RtreeNode *pParent = 0;
 144593   int iCell;
 144595   assert( pNode->nRef==1 );
 144597   /* Remove the entry in the parent cell. */
 144598   rc = nodeParentIndex(pRtree, pNode, &iCell);
 144599   if( rc==SQLITE_OK ){
 144600     pParent = pNode->pParent;
 144601     pNode->pParent = 0;
 144602     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
 144604   rc2 = nodeRelease(pRtree, pParent);
 144605   if( rc==SQLITE_OK ){
 144606     rc = rc2;
 144608   if( rc!=SQLITE_OK ){
 144609     return rc;
 144612   /* Remove the xxx_node entry. */
 144613   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
 144614   sqlite3_step(pRtree->pDeleteNode);
 144615   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
 144616     return rc;
 144619   /* Remove the xxx_parent entry. */
 144620   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
 144621   sqlite3_step(pRtree->pDeleteParent);
 144622   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
 144623     return rc;
 144626   /* Remove the node from the in-memory hash table and link it into
 144627   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
 144629   nodeHashDelete(pRtree, pNode);
 144630   pNode->iNode = iHeight;
 144631   pNode->pNext = pRtree->pDeleted;
 144632   pNode->nRef++;
 144633   pRtree->pDeleted = pNode;
 144635   return SQLITE_OK;
 144638 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
 144639   RtreeNode *pParent = pNode->pParent;
 144640   int rc = SQLITE_OK; 
 144641   if( pParent ){
 144642     int ii; 
 144643     int nCell = NCELL(pNode);
 144644     RtreeCell box;                            /* Bounding box for pNode */
 144645     nodeGetCell(pRtree, pNode, 0, &box);
 144646     for(ii=1; ii<nCell; ii++){
 144647       RtreeCell cell;
 144648       nodeGetCell(pRtree, pNode, ii, &cell);
 144649       cellUnion(pRtree, &box, &cell);
 144651     box.iRowid = pNode->iNode;
 144652     rc = nodeParentIndex(pRtree, pNode, &ii);
 144653     if( rc==SQLITE_OK ){
 144654       nodeOverwriteCell(pRtree, pParent, &box, ii);
 144655       rc = fixBoundingBox(pRtree, pParent);
 144658   return rc;
 144662 ** Delete the cell at index iCell of node pNode. After removing the
 144663 ** cell, adjust the r-tree data structure if required.
 144665 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
 144666   RtreeNode *pParent;
 144667   int rc;
 144669   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
 144670     return rc;
 144673   /* Remove the cell from the node. This call just moves bytes around
 144674   ** the in-memory node image, so it cannot fail.
 144676   nodeDeleteCell(pRtree, pNode, iCell);
 144678   /* If the node is not the tree root and now has less than the minimum
 144679   ** number of cells, remove it from the tree. Otherwise, update the
 144680   ** cell in the parent node so that it tightly contains the updated
 144681   ** node.
 144683   pParent = pNode->pParent;
 144684   assert( pParent || pNode->iNode==1 );
 144685   if( pParent ){
 144686     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
 144687       rc = removeNode(pRtree, pNode, iHeight);
 144688     }else{
 144689       rc = fixBoundingBox(pRtree, pNode);
 144693   return rc;
 144696 static int Reinsert(
 144697   Rtree *pRtree, 
 144698   RtreeNode *pNode, 
 144699   RtreeCell *pCell, 
 144700   int iHeight
 144702   int *aOrder;
 144703   int *aSpare;
 144704   RtreeCell *aCell;
 144705   RtreeDValue *aDistance;
 144706   int nCell;
 144707   RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
 144708   int iDim;
 144709   int ii;
 144710   int rc = SQLITE_OK;
 144711   int n;
 144713   memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
 144715   nCell = NCELL(pNode)+1;
 144716   n = (nCell+1)&(~1);
 144718   /* Allocate the buffers used by this operation. The allocation is
 144719   ** relinquished before this function returns.
 144721   aCell = (RtreeCell *)sqlite3_malloc(n * (
 144722     sizeof(RtreeCell)     +         /* aCell array */
 144723     sizeof(int)           +         /* aOrder array */
 144724     sizeof(int)           +         /* aSpare array */
 144725     sizeof(RtreeDValue)             /* aDistance array */
 144726   ));
 144727   if( !aCell ){
 144728     return SQLITE_NOMEM;
 144730   aOrder    = (int *)&aCell[n];
 144731   aSpare    = (int *)&aOrder[n];
 144732   aDistance = (RtreeDValue *)&aSpare[n];
 144734   for(ii=0; ii<nCell; ii++){
 144735     if( ii==(nCell-1) ){
 144736       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
 144737     }else{
 144738       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
 144740     aOrder[ii] = ii;
 144741     for(iDim=0; iDim<pRtree->nDim; iDim++){
 144742       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
 144743       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
 144746   for(iDim=0; iDim<pRtree->nDim; iDim++){
 144747     aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
 144750   for(ii=0; ii<nCell; ii++){
 144751     aDistance[ii] = 0.0;
 144752     for(iDim=0; iDim<pRtree->nDim; iDim++){
 144753       RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
 144754                                DCOORD(aCell[ii].aCoord[iDim*2]));
 144755       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
 144759   SortByDistance(aOrder, nCell, aDistance, aSpare);
 144760   nodeZero(pRtree, pNode);
 144762   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
 144763     RtreeCell *p = &aCell[aOrder[ii]];
 144764     nodeInsertCell(pRtree, pNode, p);
 144765     if( p->iRowid==pCell->iRowid ){
 144766       if( iHeight==0 ){
 144767         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
 144768       }else{
 144769         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
 144773   if( rc==SQLITE_OK ){
 144774     rc = fixBoundingBox(pRtree, pNode);
 144776   for(; rc==SQLITE_OK && ii<nCell; ii++){
 144777     /* Find a node to store this cell in. pNode->iNode currently contains
 144778     ** the height of the sub-tree headed by the cell.
 144780     RtreeNode *pInsert;
 144781     RtreeCell *p = &aCell[aOrder[ii]];
 144782     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
 144783     if( rc==SQLITE_OK ){
 144784       int rc2;
 144785       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
 144786       rc2 = nodeRelease(pRtree, pInsert);
 144787       if( rc==SQLITE_OK ){
 144788         rc = rc2;
 144793   sqlite3_free(aCell);
 144794   return rc;
 144798 ** Insert cell pCell into node pNode. Node pNode is the head of a 
 144799 ** subtree iHeight high (leaf nodes have iHeight==0).
 144801 static int rtreeInsertCell(
 144802   Rtree *pRtree,
 144803   RtreeNode *pNode,
 144804   RtreeCell *pCell,
 144805   int iHeight
 144807   int rc = SQLITE_OK;
 144808   if( iHeight>0 ){
 144809     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
 144810     if( pChild ){
 144811       nodeRelease(pRtree, pChild->pParent);
 144812       nodeReference(pNode);
 144813       pChild->pParent = pNode;
 144816   if( nodeInsertCell(pRtree, pNode, pCell) ){
 144817 #if VARIANT_RSTARTREE_REINSERT
 144818     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
 144819       rc = SplitNode(pRtree, pNode, pCell, iHeight);
 144820     }else{
 144821       pRtree->iReinsertHeight = iHeight;
 144822       rc = Reinsert(pRtree, pNode, pCell, iHeight);
 144824 #else
 144825     rc = SplitNode(pRtree, pNode, pCell, iHeight);
 144826 #endif
 144827   }else{
 144828     rc = AdjustTree(pRtree, pNode, pCell);
 144829     if( rc==SQLITE_OK ){
 144830       if( iHeight==0 ){
 144831         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
 144832       }else{
 144833         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
 144837   return rc;
 144840 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
 144841   int ii;
 144842   int rc = SQLITE_OK;
 144843   int nCell = NCELL(pNode);
 144845   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
 144846     RtreeNode *pInsert;
 144847     RtreeCell cell;
 144848     nodeGetCell(pRtree, pNode, ii, &cell);
 144850     /* Find a node to store this cell in. pNode->iNode currently contains
 144851     ** the height of the sub-tree headed by the cell.
 144853     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
 144854     if( rc==SQLITE_OK ){
 144855       int rc2;
 144856       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
 144857       rc2 = nodeRelease(pRtree, pInsert);
 144858       if( rc==SQLITE_OK ){
 144859         rc = rc2;
 144863   return rc;
 144867 ** Select a currently unused rowid for a new r-tree record.
 144869 static int newRowid(Rtree *pRtree, i64 *piRowid){
 144870   int rc;
 144871   sqlite3_bind_null(pRtree->pWriteRowid, 1);
 144872   sqlite3_bind_null(pRtree->pWriteRowid, 2);
 144873   sqlite3_step(pRtree->pWriteRowid);
 144874   rc = sqlite3_reset(pRtree->pWriteRowid);
 144875   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
 144876   return rc;
 144880 ** Remove the entry with rowid=iDelete from the r-tree structure.
 144882 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
 144883   int rc;                         /* Return code */
 144884   RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
 144885   int iCell;                      /* Index of iDelete cell in pLeaf */
 144886   RtreeNode *pRoot;               /* Root node of rtree structure */
 144889   /* Obtain a reference to the root node to initialize Rtree.iDepth */
 144890   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
 144892   /* Obtain a reference to the leaf node that contains the entry 
 144893   ** about to be deleted. 
 144895   if( rc==SQLITE_OK ){
 144896     rc = findLeafNode(pRtree, iDelete, &pLeaf);
 144899   /* Delete the cell in question from the leaf node. */
 144900   if( rc==SQLITE_OK ){
 144901     int rc2;
 144902     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
 144903     if( rc==SQLITE_OK ){
 144904       rc = deleteCell(pRtree, pLeaf, iCell, 0);
 144906     rc2 = nodeRelease(pRtree, pLeaf);
 144907     if( rc==SQLITE_OK ){
 144908       rc = rc2;
 144912   /* Delete the corresponding entry in the <rtree>_rowid table. */
 144913   if( rc==SQLITE_OK ){
 144914     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
 144915     sqlite3_step(pRtree->pDeleteRowid);
 144916     rc = sqlite3_reset(pRtree->pDeleteRowid);
 144919   /* Check if the root node now has exactly one child. If so, remove
 144920   ** it, schedule the contents of the child for reinsertion and 
 144921   ** reduce the tree height by one.
 144923   ** This is equivalent to copying the contents of the child into
 144924   ** the root node (the operation that Gutman's paper says to perform 
 144925   ** in this scenario).
 144927   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
 144928     int rc2;
 144929     RtreeNode *pChild;
 144930     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
 144931     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
 144932     if( rc==SQLITE_OK ){
 144933       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
 144935     rc2 = nodeRelease(pRtree, pChild);
 144936     if( rc==SQLITE_OK ) rc = rc2;
 144937     if( rc==SQLITE_OK ){
 144938       pRtree->iDepth--;
 144939       writeInt16(pRoot->zData, pRtree->iDepth);
 144940       pRoot->isDirty = 1;
 144944   /* Re-insert the contents of any underfull nodes removed from the tree. */
 144945   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
 144946     if( rc==SQLITE_OK ){
 144947       rc = reinsertNodeContent(pRtree, pLeaf);
 144949     pRtree->pDeleted = pLeaf->pNext;
 144950     sqlite3_free(pLeaf);
 144953   /* Release the reference to the root node. */
 144954   if( rc==SQLITE_OK ){
 144955     rc = nodeRelease(pRtree, pRoot);
 144956   }else{
 144957     nodeRelease(pRtree, pRoot);
 144960   return rc;
 144964 ** Rounding constants for float->double conversion.
 144966 #define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
 144967 #define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
 144969 #if !defined(SQLITE_RTREE_INT_ONLY)
 144971 ** Convert an sqlite3_value into an RtreeValue (presumably a float)
 144972 ** while taking care to round toward negative or positive, respectively.
 144974 static RtreeValue rtreeValueDown(sqlite3_value *v){
 144975   double d = sqlite3_value_double(v);
 144976   float f = (float)d;
 144977   if( f>d ){
 144978     f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
 144980   return f;
 144982 static RtreeValue rtreeValueUp(sqlite3_value *v){
 144983   double d = sqlite3_value_double(v);
 144984   float f = (float)d;
 144985   if( f<d ){
 144986     f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
 144988   return f;
 144990 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
 144994 ** The xUpdate method for rtree module virtual tables.
 144996 static int rtreeUpdate(
 144997   sqlite3_vtab *pVtab, 
 144998   int nData, 
 144999   sqlite3_value **azData, 
 145000   sqlite_int64 *pRowid
 145002   Rtree *pRtree = (Rtree *)pVtab;
 145003   int rc = SQLITE_OK;
 145004   RtreeCell cell;                 /* New cell to insert if nData>1 */
 145005   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
 145007   rtreeReference(pRtree);
 145008   assert(nData>=1);
 145010   /* Constraint handling. A write operation on an r-tree table may return
 145011   ** SQLITE_CONSTRAINT for two reasons:
 145013   **   1. A duplicate rowid value, or
 145014   **   2. The supplied data violates the "x2>=x1" constraint.
 145016   ** In the first case, if the conflict-handling mode is REPLACE, then
 145017   ** the conflicting row can be removed before proceeding. In the second
 145018   ** case, SQLITE_CONSTRAINT must be returned regardless of the
 145019   ** conflict-handling mode specified by the user.
 145021   if( nData>1 ){
 145022     int ii;
 145024     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
 145025     assert( nData==(pRtree->nDim*2 + 3) );
 145026 #ifndef SQLITE_RTREE_INT_ONLY
 145027     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
 145028       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 145029         cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
 145030         cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
 145031         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
 145032           rc = SQLITE_CONSTRAINT;
 145033           goto constraint;
 145036     }else
 145037 #endif
 145039       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
 145040         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
 145041         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
 145042         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
 145043           rc = SQLITE_CONSTRAINT;
 145044           goto constraint;
 145049     /* If a rowid value was supplied, check if it is already present in 
 145050     ** the table. If so, the constraint has failed. */
 145051     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
 145052       cell.iRowid = sqlite3_value_int64(azData[2]);
 145053       if( sqlite3_value_type(azData[0])==SQLITE_NULL
 145054        || sqlite3_value_int64(azData[0])!=cell.iRowid
 145056         int steprc;
 145057         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
 145058         steprc = sqlite3_step(pRtree->pReadRowid);
 145059         rc = sqlite3_reset(pRtree->pReadRowid);
 145060         if( SQLITE_ROW==steprc ){
 145061           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
 145062             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
 145063           }else{
 145064             rc = SQLITE_CONSTRAINT;
 145065             goto constraint;
 145069       bHaveRowid = 1;
 145073   /* If azData[0] is not an SQL NULL value, it is the rowid of a
 145074   ** record to delete from the r-tree table. The following block does
 145075   ** just that.
 145077   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
 145078     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
 145081   /* If the azData[] array contains more than one element, elements
 145082   ** (azData[2]..azData[argc-1]) contain a new record to insert into
 145083   ** the r-tree structure.
 145085   if( rc==SQLITE_OK && nData>1 ){
 145086     /* Insert the new record into the r-tree */
 145087     RtreeNode *pLeaf = 0;
 145089     /* Figure out the rowid of the new row. */
 145090     if( bHaveRowid==0 ){
 145091       rc = newRowid(pRtree, &cell.iRowid);
 145093     *pRowid = cell.iRowid;
 145095     if( rc==SQLITE_OK ){
 145096       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
 145098     if( rc==SQLITE_OK ){
 145099       int rc2;
 145100       pRtree->iReinsertHeight = -1;
 145101       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
 145102       rc2 = nodeRelease(pRtree, pLeaf);
 145103       if( rc==SQLITE_OK ){
 145104         rc = rc2;
 145109 constraint:
 145110   rtreeRelease(pRtree);
 145111   return rc;
 145115 ** The xRename method for rtree module virtual tables.
 145117 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
 145118   Rtree *pRtree = (Rtree *)pVtab;
 145119   int rc = SQLITE_NOMEM;
 145120   char *zSql = sqlite3_mprintf(
 145121     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
 145122     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
 145123     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
 145124     , pRtree->zDb, pRtree->zName, zNewName 
 145125     , pRtree->zDb, pRtree->zName, zNewName 
 145126     , pRtree->zDb, pRtree->zName, zNewName
 145128   if( zSql ){
 145129     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
 145130     sqlite3_free(zSql);
 145132   return rc;
 145136 ** This function populates the pRtree->nRowEst variable with an estimate
 145137 ** of the number of rows in the virtual table. If possible, this is based
 145138 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
 145140 static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
 145141   const char *zSql = "SELECT stat FROM sqlite_stat1 WHERE tbl= ? || '_rowid'";
 145142   sqlite3_stmt *p;
 145143   int rc;
 145144   i64 nRow = 0;
 145146   rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
 145147   if( rc==SQLITE_OK ){
 145148     sqlite3_bind_text(p, 1, pRtree->zName, -1, SQLITE_STATIC);
 145149     if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
 145150     rc = sqlite3_finalize(p);
 145151   }else if( rc!=SQLITE_NOMEM ){
 145152     rc = SQLITE_OK;
 145155   if( rc==SQLITE_OK ){
 145156     if( nRow==0 ){
 145157       pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
 145158     }else{
 145159       pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
 145163   return rc;
 145166 static sqlite3_module rtreeModule = {
 145167   0,                          /* iVersion */
 145168   rtreeCreate,                /* xCreate - create a table */
 145169   rtreeConnect,               /* xConnect - connect to an existing table */
 145170   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
 145171   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
 145172   rtreeDestroy,               /* xDestroy - Drop a table */
 145173   rtreeOpen,                  /* xOpen - open a cursor */
 145174   rtreeClose,                 /* xClose - close a cursor */
 145175   rtreeFilter,                /* xFilter - configure scan constraints */
 145176   rtreeNext,                  /* xNext - advance a cursor */
 145177   rtreeEof,                   /* xEof */
 145178   rtreeColumn,                /* xColumn - read data */
 145179   rtreeRowid,                 /* xRowid - read data */
 145180   rtreeUpdate,                /* xUpdate - write data */
 145181   0,                          /* xBegin - begin transaction */
 145182   0,                          /* xSync - sync transaction */
 145183   0,                          /* xCommit - commit transaction */
 145184   0,                          /* xRollback - rollback transaction */
 145185   0,                          /* xFindFunction - function overloading */
 145186   rtreeRename,                /* xRename - rename the table */
 145187   0,                          /* xSavepoint */
 145188   0,                          /* xRelease */
 145189   0                           /* xRollbackTo */
 145192 static int rtreeSqlInit(
 145193   Rtree *pRtree, 
 145194   sqlite3 *db, 
 145195   const char *zDb, 
 145196   const char *zPrefix, 
 145197   int isCreate
 145199   int rc = SQLITE_OK;
 145201   #define N_STATEMENT 9
 145202   static const char *azSql[N_STATEMENT] = {
 145203     /* Read and write the xxx_node table */
 145204     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
 145205     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
 145206     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
 145208     /* Read and write the xxx_rowid table */
 145209     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
 145210     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
 145211     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
 145213     /* Read and write the xxx_parent table */
 145214     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
 145215     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
 145216     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
 145218   sqlite3_stmt **appStmt[N_STATEMENT];
 145219   int i;
 145221   pRtree->db = db;
 145223   if( isCreate ){
 145224     char *zCreate = sqlite3_mprintf(
 145225 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
 145226 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
 145227 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
 145228 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
 145229       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
 145231     if( !zCreate ){
 145232       return SQLITE_NOMEM;
 145234     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
 145235     sqlite3_free(zCreate);
 145236     if( rc!=SQLITE_OK ){
 145237       return rc;
 145241   appStmt[0] = &pRtree->pReadNode;
 145242   appStmt[1] = &pRtree->pWriteNode;
 145243   appStmt[2] = &pRtree->pDeleteNode;
 145244   appStmt[3] = &pRtree->pReadRowid;
 145245   appStmt[4] = &pRtree->pWriteRowid;
 145246   appStmt[5] = &pRtree->pDeleteRowid;
 145247   appStmt[6] = &pRtree->pReadParent;
 145248   appStmt[7] = &pRtree->pWriteParent;
 145249   appStmt[8] = &pRtree->pDeleteParent;
 145251   rc = rtreeQueryStat1(db, pRtree);
 145252   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
 145253     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
 145254     if( zSql ){
 145255       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
 145256     }else{
 145257       rc = SQLITE_NOMEM;
 145259     sqlite3_free(zSql);
 145262   return rc;
 145266 ** The second argument to this function contains the text of an SQL statement
 145267 ** that returns a single integer value. The statement is compiled and executed
 145268 ** using database connection db. If successful, the integer value returned
 145269 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
 145270 ** code is returned and the value of *piVal after returning is not defined.
 145272 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
 145273   int rc = SQLITE_NOMEM;
 145274   if( zSql ){
 145275     sqlite3_stmt *pStmt = 0;
 145276     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 145277     if( rc==SQLITE_OK ){
 145278       if( SQLITE_ROW==sqlite3_step(pStmt) ){
 145279         *piVal = sqlite3_column_int(pStmt, 0);
 145281       rc = sqlite3_finalize(pStmt);
 145284   return rc;
 145288 ** This function is called from within the xConnect() or xCreate() method to
 145289 ** determine the node-size used by the rtree table being created or connected
 145290 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
 145291 ** Otherwise, an SQLite error code is returned.
 145293 ** If this function is being called as part of an xConnect(), then the rtree
 145294 ** table already exists. In this case the node-size is determined by inspecting
 145295 ** the root node of the tree.
 145297 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 
 145298 ** This ensures that each node is stored on a single database page. If the 
 145299 ** database page-size is so large that more than RTREE_MAXCELLS entries 
 145300 ** would fit in a single node, use a smaller node-size.
 145302 static int getNodeSize(
 145303   sqlite3 *db,                    /* Database handle */
 145304   Rtree *pRtree,                  /* Rtree handle */
 145305   int isCreate,                   /* True for xCreate, false for xConnect */
 145306   char **pzErr                    /* OUT: Error message, if any */
 145308   int rc;
 145309   char *zSql;
 145310   if( isCreate ){
 145311     int iPageSize = 0;
 145312     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
 145313     rc = getIntFromStmt(db, zSql, &iPageSize);
 145314     if( rc==SQLITE_OK ){
 145315       pRtree->iNodeSize = iPageSize-64;
 145316       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
 145317         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
 145319     }else{
 145320       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 145322   }else{
 145323     zSql = sqlite3_mprintf(
 145324         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
 145325         pRtree->zDb, pRtree->zName
 145327     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
 145328     if( rc!=SQLITE_OK ){
 145329       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 145333   sqlite3_free(zSql);
 145334   return rc;
 145338 ** This function is the implementation of both the xConnect and xCreate
 145339 ** methods of the r-tree virtual table.
 145341 **   argv[0]   -> module name
 145342 **   argv[1]   -> database name
 145343 **   argv[2]   -> table name
 145344 **   argv[...] -> column names...
 145346 static int rtreeInit(
 145347   sqlite3 *db,                        /* Database connection */
 145348   void *pAux,                         /* One of the RTREE_COORD_* constants */
 145349   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
 145350   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
 145351   char **pzErr,                       /* OUT: Error message, if any */
 145352   int isCreate                        /* True for xCreate, false for xConnect */
 145354   int rc = SQLITE_OK;
 145355   Rtree *pRtree;
 145356   int nDb;              /* Length of string argv[1] */
 145357   int nName;            /* Length of string argv[2] */
 145358   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
 145360   const char *aErrMsg[] = {
 145361     0,                                                    /* 0 */
 145362     "Wrong number of columns for an rtree table",         /* 1 */
 145363     "Too few columns for an rtree table",                 /* 2 */
 145364     "Too many columns for an rtree table"                 /* 3 */
 145367   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
 145368   if( aErrMsg[iErr] ){
 145369     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
 145370     return SQLITE_ERROR;
 145373   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
 145375   /* Allocate the sqlite3_vtab structure */
 145376   nDb = (int)strlen(argv[1]);
 145377   nName = (int)strlen(argv[2]);
 145378   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
 145379   if( !pRtree ){
 145380     return SQLITE_NOMEM;
 145382   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
 145383   pRtree->nBusy = 1;
 145384   pRtree->base.pModule = &rtreeModule;
 145385   pRtree->zDb = (char *)&pRtree[1];
 145386   pRtree->zName = &pRtree->zDb[nDb+1];
 145387   pRtree->nDim = (argc-4)/2;
 145388   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
 145389   pRtree->eCoordType = eCoordType;
 145390   memcpy(pRtree->zDb, argv[1], nDb);
 145391   memcpy(pRtree->zName, argv[2], nName);
 145393   /* Figure out the node size to use. */
 145394   rc = getNodeSize(db, pRtree, isCreate, pzErr);
 145396   /* Create/Connect to the underlying relational database schema. If
 145397   ** that is successful, call sqlite3_declare_vtab() to configure
 145398   ** the r-tree table schema.
 145400   if( rc==SQLITE_OK ){
 145401     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
 145402       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 145403     }else{
 145404       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
 145405       char *zTmp;
 145406       int ii;
 145407       for(ii=4; zSql && ii<argc; ii++){
 145408         zTmp = zSql;
 145409         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
 145410         sqlite3_free(zTmp);
 145412       if( zSql ){
 145413         zTmp = zSql;
 145414         zSql = sqlite3_mprintf("%s);", zTmp);
 145415         sqlite3_free(zTmp);
 145417       if( !zSql ){
 145418         rc = SQLITE_NOMEM;
 145419       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
 145420         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
 145422       sqlite3_free(zSql);
 145426   if( rc==SQLITE_OK ){
 145427     *ppVtab = (sqlite3_vtab *)pRtree;
 145428   }else{
 145429     rtreeRelease(pRtree);
 145431   return rc;
 145436 ** Implementation of a scalar function that decodes r-tree nodes to
 145437 ** human readable strings. This can be used for debugging and analysis.
 145439 ** The scalar function takes two arguments, a blob of data containing
 145440 ** an r-tree node, and the number of dimensions the r-tree indexes.
 145441 ** For a two-dimensional r-tree structure called "rt", to deserialize
 145442 ** all nodes, a statement like:
 145444 **   SELECT rtreenode(2, data) FROM rt_node;
 145446 ** The human readable string takes the form of a Tcl list with one
 145447 ** entry for each cell in the r-tree node. Each entry is itself a
 145448 ** list, containing the 8-byte rowid/pageno followed by the 
 145449 ** <num-dimension>*2 coordinates.
 145451 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
 145452   char *zText = 0;
 145453   RtreeNode node;
 145454   Rtree tree;
 145455   int ii;
 145457   UNUSED_PARAMETER(nArg);
 145458   memset(&node, 0, sizeof(RtreeNode));
 145459   memset(&tree, 0, sizeof(Rtree));
 145460   tree.nDim = sqlite3_value_int(apArg[0]);
 145461   tree.nBytesPerCell = 8 + 8 * tree.nDim;
 145462   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
 145464   for(ii=0; ii<NCELL(&node); ii++){
 145465     char zCell[512];
 145466     int nCell = 0;
 145467     RtreeCell cell;
 145468     int jj;
 145470     nodeGetCell(&tree, &node, ii, &cell);
 145471     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
 145472     nCell = (int)strlen(zCell);
 145473     for(jj=0; jj<tree.nDim*2; jj++){
 145474 #ifndef SQLITE_RTREE_INT_ONLY
 145475       sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
 145476                        (double)cell.aCoord[jj].f);
 145477 #else
 145478       sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
 145479                        cell.aCoord[jj].i);
 145480 #endif
 145481       nCell = (int)strlen(zCell);
 145484     if( zText ){
 145485       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
 145486       sqlite3_free(zText);
 145487       zText = zTextNew;
 145488     }else{
 145489       zText = sqlite3_mprintf("{%s}", zCell);
 145493   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
 145496 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
 145497   UNUSED_PARAMETER(nArg);
 145498   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
 145499    || sqlite3_value_bytes(apArg[0])<2
 145501     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
 145502   }else{
 145503     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
 145504     sqlite3_result_int(ctx, readInt16(zBlob));
 145509 ** Register the r-tree module with database handle db. This creates the
 145510 ** virtual table module "rtree" and the debugging/analysis scalar 
 145511 ** function "rtreenode".
 145513 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
 145514   const int utf8 = SQLITE_UTF8;
 145515   int rc;
 145517   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
 145518   if( rc==SQLITE_OK ){
 145519     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
 145521   if( rc==SQLITE_OK ){
 145522 #ifdef SQLITE_RTREE_INT_ONLY
 145523     void *c = (void *)RTREE_COORD_INT32;
 145524 #else
 145525     void *c = (void *)RTREE_COORD_REAL32;
 145526 #endif
 145527     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
 145529   if( rc==SQLITE_OK ){
 145530     void *c = (void *)RTREE_COORD_INT32;
 145531     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
 145534   return rc;
 145538 ** A version of sqlite3_free() that can be used as a callback. This is used
 145539 ** in two places - as the destructor for the blob value returned by the
 145540 ** invocation of a geometry function, and as the destructor for the geometry
 145541 ** functions themselves.
 145543 static void doSqlite3Free(void *p){
 145544   sqlite3_free(p);
 145548 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
 145549 ** scalar user function. This C function is the callback used for all such
 145550 ** registered SQL functions.
 145552 ** The scalar user functions return a blob that is interpreted by r-tree
 145553 ** table MATCH operators.
 145555 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
 145556   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
 145557   RtreeMatchArg *pBlob;
 145558   int nBlob;
 145560   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
 145561   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
 145562   if( !pBlob ){
 145563     sqlite3_result_error_nomem(ctx);
 145564   }else{
 145565     int i;
 145566     pBlob->magic = RTREE_GEOMETRY_MAGIC;
 145567     pBlob->xGeom = pGeomCtx->xGeom;
 145568     pBlob->pContext = pGeomCtx->pContext;
 145569     pBlob->nParam = nArg;
 145570     for(i=0; i<nArg; i++){
 145571 #ifdef SQLITE_RTREE_INT_ONLY
 145572       pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
 145573 #else
 145574       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
 145575 #endif
 145577     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
 145582 ** Register a new geometry function for use with the r-tree MATCH operator.
 145584 SQLITE_API int sqlite3_rtree_geometry_callback(
 145585   sqlite3 *db,
 145586   const char *zGeom,
 145587   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
 145588   void *pContext
 145590   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
 145592   /* Allocate and populate the context object. */
 145593   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
 145594   if( !pGeomCtx ) return SQLITE_NOMEM;
 145595   pGeomCtx->xGeom = xGeom;
 145596   pGeomCtx->pContext = pContext;
 145598   /* Create the new user-function. Register a destructor function to delete
 145599   ** the context object when it is no longer required.  */
 145600   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 
 145601       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
 145605 #if !SQLITE_CORE
 145606 #ifdef _WIN32
 145607 __declspec(dllexport)
 145608 #endif
 145609 SQLITE_API int sqlite3_rtree_init(
 145610   sqlite3 *db,
 145611   char **pzErrMsg,
 145612   const sqlite3_api_routines *pApi
 145614   SQLITE_EXTENSION_INIT2(pApi)
 145615   return sqlite3RtreeInit(db);
 145617 #endif
 145619 #endif
 145621 /************** End of rtree.c ***********************************************/
 145622 /************** Begin file icu.c *********************************************/
 145624 ** 2007 May 6
 145626 ** The author disclaims copyright to this source code.  In place of
 145627 ** a legal notice, here is a blessing:
 145629 **    May you do good and not evil.
 145630 **    May you find forgiveness for yourself and forgive others.
 145631 **    May you share freely, never taking more than you give.
 145633 *************************************************************************
 145634 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
 145636 ** This file implements an integration between the ICU library 
 145637 ** ("International Components for Unicode", an open-source library 
 145638 ** for handling unicode data) and SQLite. The integration uses 
 145639 ** ICU to provide the following to SQLite:
 145641 **   * An implementation of the SQL regexp() function (and hence REGEXP
 145642 **     operator) using the ICU uregex_XX() APIs.
 145644 **   * Implementations of the SQL scalar upper() and lower() functions
 145645 **     for case mapping.
 145647 **   * Integration of ICU and SQLite collation sequences.
 145649 **   * An implementation of the LIKE operator that uses ICU to 
 145650 **     provide case-independent matching.
 145653 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
 145655 /* Include ICU headers */
 145656 #include <unicode/utypes.h>
 145657 #include <unicode/uregex.h>
 145658 #include <unicode/ustring.h>
 145659 #include <unicode/ucol.h>
 145661 /* #include <assert.h> */
 145663 #ifndef SQLITE_CORE
 145664   SQLITE_EXTENSION_INIT1
 145665 #else
 145666 #endif
 145669 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
 145670 ** operator.
 145672 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
 145673 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
 145674 #endif
 145677 ** Version of sqlite3_free() that is always a function, never a macro.
 145679 static void xFree(void *p){
 145680   sqlite3_free(p);
 145684 ** Compare two UTF-8 strings for equality where the first string is
 145685 ** a "LIKE" expression. Return true (1) if they are the same and 
 145686 ** false (0) if they are different.
 145688 static int icuLikeCompare(
 145689   const uint8_t *zPattern,   /* LIKE pattern */
 145690   const uint8_t *zString,    /* The UTF-8 string to compare against */
 145691   const UChar32 uEsc         /* The escape character */
 145693   static const int MATCH_ONE = (UChar32)'_';
 145694   static const int MATCH_ALL = (UChar32)'%';
 145696   int iPattern = 0;       /* Current byte index in zPattern */
 145697   int iString = 0;        /* Current byte index in zString */
 145699   int prevEscape = 0;     /* True if the previous character was uEsc */
 145701   while( zPattern[iPattern]!=0 ){
 145703     /* Read (and consume) the next character from the input pattern. */
 145704     UChar32 uPattern;
 145705     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
 145706     assert(uPattern!=0);
 145708     /* There are now 4 possibilities:
 145710     **     1. uPattern is an unescaped match-all character "%",
 145711     **     2. uPattern is an unescaped match-one character "_",
 145712     **     3. uPattern is an unescaped escape character, or
 145713     **     4. uPattern is to be handled as an ordinary character
 145715     if( !prevEscape && uPattern==MATCH_ALL ){
 145716       /* Case 1. */
 145717       uint8_t c;
 145719       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
 145720       ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
 145721       ** test string.
 145723       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
 145724         if( c==MATCH_ONE ){
 145725           if( zString[iString]==0 ) return 0;
 145726           U8_FWD_1_UNSAFE(zString, iString);
 145728         iPattern++;
 145731       if( zPattern[iPattern]==0 ) return 1;
 145733       while( zString[iString] ){
 145734         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
 145735           return 1;
 145737         U8_FWD_1_UNSAFE(zString, iString);
 145739       return 0;
 145741     }else if( !prevEscape && uPattern==MATCH_ONE ){
 145742       /* Case 2. */
 145743       if( zString[iString]==0 ) return 0;
 145744       U8_FWD_1_UNSAFE(zString, iString);
 145746     }else if( !prevEscape && uPattern==uEsc){
 145747       /* Case 3. */
 145748       prevEscape = 1;
 145750     }else{
 145751       /* Case 4. */
 145752       UChar32 uString;
 145753       U8_NEXT_UNSAFE(zString, iString, uString);
 145754       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
 145755       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
 145756       if( uString!=uPattern ){
 145757         return 0;
 145759       prevEscape = 0;
 145763   return zString[iString]==0;
 145767 ** Implementation of the like() SQL function.  This function implements
 145768 ** the build-in LIKE operator.  The first argument to the function is the
 145769 ** pattern and the second argument is the string.  So, the SQL statements:
 145771 **       A LIKE B
 145773 ** is implemented as like(B, A). If there is an escape character E, 
 145775 **       A LIKE B ESCAPE E
 145777 ** is mapped to like(B, A, E).
 145779 static void icuLikeFunc(
 145780   sqlite3_context *context, 
 145781   int argc, 
 145782   sqlite3_value **argv
 145784   const unsigned char *zA = sqlite3_value_text(argv[0]);
 145785   const unsigned char *zB = sqlite3_value_text(argv[1]);
 145786   UChar32 uEsc = 0;
 145788   /* Limit the length of the LIKE or GLOB pattern to avoid problems
 145789   ** of deep recursion and N*N behavior in patternCompare().
 145791   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
 145792     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
 145793     return;
 145797   if( argc==3 ){
 145798     /* The escape character string must consist of a single UTF-8 character.
 145799     ** Otherwise, return an error.
 145801     int nE= sqlite3_value_bytes(argv[2]);
 145802     const unsigned char *zE = sqlite3_value_text(argv[2]);
 145803     int i = 0;
 145804     if( zE==0 ) return;
 145805     U8_NEXT(zE, i, nE, uEsc);
 145806     if( i!=nE){
 145807       sqlite3_result_error(context, 
 145808           "ESCAPE expression must be a single character", -1);
 145809       return;
 145813   if( zA && zB ){
 145814     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
 145819 ** This function is called when an ICU function called from within
 145820 ** the implementation of an SQL scalar function returns an error.
 145822 ** The scalar function context passed as the first argument is 
 145823 ** loaded with an error message based on the following two args.
 145825 static void icuFunctionError(
 145826   sqlite3_context *pCtx,       /* SQLite scalar function context */
 145827   const char *zName,           /* Name of ICU function that failed */
 145828   UErrorCode e                 /* Error code returned by ICU function */
 145830   char zBuf[128];
 145831   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
 145832   zBuf[127] = '\0';
 145833   sqlite3_result_error(pCtx, zBuf, -1);
 145837 ** Function to delete compiled regexp objects. Registered as
 145838 ** a destructor function with sqlite3_set_auxdata().
 145840 static void icuRegexpDelete(void *p){
 145841   URegularExpression *pExpr = (URegularExpression *)p;
 145842   uregex_close(pExpr);
 145846 ** Implementation of SQLite REGEXP operator. This scalar function takes
 145847 ** two arguments. The first is a regular expression pattern to compile
 145848 ** the second is a string to match against that pattern. If either 
 145849 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
 145850 ** is 1 if the string matches the pattern, or 0 otherwise.
 145852 ** SQLite maps the regexp() function to the regexp() operator such
 145853 ** that the following two are equivalent:
 145855 **     zString REGEXP zPattern
 145856 **     regexp(zPattern, zString)
 145858 ** Uses the following ICU regexp APIs:
 145860 **     uregex_open()
 145861 **     uregex_matches()
 145862 **     uregex_close()
 145864 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
 145865   UErrorCode status = U_ZERO_ERROR;
 145866   URegularExpression *pExpr;
 145867   UBool res;
 145868   const UChar *zString = sqlite3_value_text16(apArg[1]);
 145870   (void)nArg;  /* Unused parameter */
 145872   /* If the left hand side of the regexp operator is NULL, 
 145873   ** then the result is also NULL. 
 145875   if( !zString ){
 145876     return;
 145879   pExpr = sqlite3_get_auxdata(p, 0);
 145880   if( !pExpr ){
 145881     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
 145882     if( !zPattern ){
 145883       return;
 145885     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
 145887     if( U_SUCCESS(status) ){
 145888       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
 145889     }else{
 145890       assert(!pExpr);
 145891       icuFunctionError(p, "uregex_open", status);
 145892       return;
 145896   /* Configure the text that the regular expression operates on. */
 145897   uregex_setText(pExpr, zString, -1, &status);
 145898   if( !U_SUCCESS(status) ){
 145899     icuFunctionError(p, "uregex_setText", status);
 145900     return;
 145903   /* Attempt the match */
 145904   res = uregex_matches(pExpr, 0, &status);
 145905   if( !U_SUCCESS(status) ){
 145906     icuFunctionError(p, "uregex_matches", status);
 145907     return;
 145910   /* Set the text that the regular expression operates on to a NULL
 145911   ** pointer. This is not really necessary, but it is tidier than 
 145912   ** leaving the regular expression object configured with an invalid
 145913   ** pointer after this function returns.
 145915   uregex_setText(pExpr, 0, 0, &status);
 145917   /* Return 1 or 0. */
 145918   sqlite3_result_int(p, res ? 1 : 0);
 145922 ** Implementations of scalar functions for case mapping - upper() and 
 145923 ** lower(). Function upper() converts its input to upper-case (ABC).
 145924 ** Function lower() converts to lower-case (abc).
 145926 ** ICU provides two types of case mapping, "general" case mapping and
 145927 ** "language specific". Refer to ICU documentation for the differences
 145928 ** between the two.
 145930 ** To utilise "general" case mapping, the upper() or lower() scalar 
 145931 ** functions are invoked with one argument:
 145933 **     upper('ABC') -> 'abc'
 145934 **     lower('abc') -> 'ABC'
 145936 ** To access ICU "language specific" case mapping, upper() or lower()
 145937 ** should be invoked with two arguments. The second argument is the name
 145938 ** of the locale to use. Passing an empty string ("") or SQL NULL value
 145939 ** as the second argument is the same as invoking the 1 argument version
 145940 ** of upper() or lower().
 145942 **     lower('I', 'en_us') -> 'i'
 145943 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
 145945 ** http://www.icu-project.org/userguide/posix.html#case_mappings
 145947 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
 145948   const UChar *zInput;
 145949   UChar *zOutput;
 145950   int nInput;
 145951   int nOutput;
 145953   UErrorCode status = U_ZERO_ERROR;
 145954   const char *zLocale = 0;
 145956   assert(nArg==1 || nArg==2);
 145957   if( nArg==2 ){
 145958     zLocale = (const char *)sqlite3_value_text(apArg[1]);
 145961   zInput = sqlite3_value_text16(apArg[0]);
 145962   if( !zInput ){
 145963     return;
 145965   nInput = sqlite3_value_bytes16(apArg[0]);
 145967   nOutput = nInput * 2 + 2;
 145968   zOutput = sqlite3_malloc(nOutput);
 145969   if( !zOutput ){
 145970     return;
 145973   if( sqlite3_user_data(p) ){
 145974     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
 145975   }else{
 145976     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
 145979   if( !U_SUCCESS(status) ){
 145980     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
 145981     return;
 145984   sqlite3_result_text16(p, zOutput, -1, xFree);
 145988 ** Collation sequence destructor function. The pCtx argument points to
 145989 ** a UCollator structure previously allocated using ucol_open().
 145991 static void icuCollationDel(void *pCtx){
 145992   UCollator *p = (UCollator *)pCtx;
 145993   ucol_close(p);
 145997 ** Collation sequence comparison function. The pCtx argument points to
 145998 ** a UCollator structure previously allocated using ucol_open().
 146000 static int icuCollationColl(
 146001   void *pCtx,
 146002   int nLeft,
 146003   const void *zLeft,
 146004   int nRight,
 146005   const void *zRight
 146007   UCollationResult res;
 146008   UCollator *p = (UCollator *)pCtx;
 146009   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
 146010   switch( res ){
 146011     case UCOL_LESS:    return -1;
 146012     case UCOL_GREATER: return +1;
 146013     case UCOL_EQUAL:   return 0;
 146015   assert(!"Unexpected return value from ucol_strcoll()");
 146016   return 0;
 146020 ** Implementation of the scalar function icu_load_collation().
 146022 ** This scalar function is used to add ICU collation based collation 
 146023 ** types to an SQLite database connection. It is intended to be called
 146024 ** as follows:
 146026 **     SELECT icu_load_collation(<locale>, <collation-name>);
 146028 ** Where <locale> is a string containing an ICU locale identifier (i.e.
 146029 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
 146030 ** collation sequence to create.
 146032 static void icuLoadCollation(
 146033   sqlite3_context *p, 
 146034   int nArg, 
 146035   sqlite3_value **apArg
 146037   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
 146038   UErrorCode status = U_ZERO_ERROR;
 146039   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
 146040   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
 146041   UCollator *pUCollator;    /* ICU library collation object */
 146042   int rc;                   /* Return code from sqlite3_create_collation_x() */
 146044   assert(nArg==2);
 146045   zLocale = (const char *)sqlite3_value_text(apArg[0]);
 146046   zName = (const char *)sqlite3_value_text(apArg[1]);
 146048   if( !zLocale || !zName ){
 146049     return;
 146052   pUCollator = ucol_open(zLocale, &status);
 146053   if( !U_SUCCESS(status) ){
 146054     icuFunctionError(p, "ucol_open", status);
 146055     return;
 146057   assert(p);
 146059   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
 146060       icuCollationColl, icuCollationDel
 146062   if( rc!=SQLITE_OK ){
 146063     ucol_close(pUCollator);
 146064     sqlite3_result_error(p, "Error registering collation function", -1);
 146069 ** Register the ICU extension functions with database db.
 146071 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
 146072   struct IcuScalar {
 146073     const char *zName;                        /* Function name */
 146074     int nArg;                                 /* Number of arguments */
 146075     int enc;                                  /* Optimal text encoding */
 146076     void *pContext;                           /* sqlite3_user_data() context */
 146077     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
 146078   } scalars[] = {
 146079     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
 146081     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
 146082     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
 146083     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
 146084     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
 146086     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
 146087     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
 146088     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
 146089     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
 146091     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
 146092     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
 146094     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
 146097   int rc = SQLITE_OK;
 146098   int i;
 146100   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
 146101     struct IcuScalar *p = &scalars[i];
 146102     rc = sqlite3_create_function(
 146103         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
 146107   return rc;
 146110 #if !SQLITE_CORE
 146111 #ifdef _WIN32
 146112 __declspec(dllexport)
 146113 #endif
 146114 SQLITE_API int sqlite3_icu_init(
 146115   sqlite3 *db, 
 146116   char **pzErrMsg,
 146117   const sqlite3_api_routines *pApi
 146119   SQLITE_EXTENSION_INIT2(pApi)
 146120   return sqlite3IcuInit(db);
 146122 #endif
 146124 #endif
 146126 /************** End of icu.c *************************************************/
 146127 /************** Begin file fts3_icu.c ****************************************/
 146129 ** 2007 June 22
 146131 ** The author disclaims copyright to this source code.  In place of
 146132 ** a legal notice, here is a blessing:
 146134 **    May you do good and not evil.
 146135 **    May you find forgiveness for yourself and forgive others.
 146136 **    May you share freely, never taking more than you give.
 146138 *************************************************************************
 146139 ** This file implements a tokenizer for fts3 based on the ICU library.
 146141 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
 146142 #ifdef SQLITE_ENABLE_ICU
 146144 /* #include <assert.h> */
 146145 /* #include <string.h> */
 146147 #include <unicode/ubrk.h>
 146148 /* #include <unicode/ucol.h> */
 146149 /* #include <unicode/ustring.h> */
 146150 #include <unicode/utf16.h>
 146152 typedef struct IcuTokenizer IcuTokenizer;
 146153 typedef struct IcuCursor IcuCursor;
 146155 struct IcuTokenizer {
 146156   sqlite3_tokenizer base;
 146157   char *zLocale;
 146160 struct IcuCursor {
 146161   sqlite3_tokenizer_cursor base;
 146163   UBreakIterator *pIter;      /* ICU break-iterator object */
 146164   int nChar;                  /* Number of UChar elements in pInput */
 146165   UChar *aChar;               /* Copy of input using utf-16 encoding */
 146166   int *aOffset;               /* Offsets of each character in utf-8 input */
 146168   int nBuffer;
 146169   char *zBuffer;
 146171   int iToken;
 146175 ** Create a new tokenizer instance.
 146177 static int icuCreate(
 146178   int argc,                            /* Number of entries in argv[] */
 146179   const char * const *argv,            /* Tokenizer creation arguments */
 146180   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
 146182   IcuTokenizer *p;
 146183   int n = 0;
 146185   if( argc>0 ){
 146186     n = strlen(argv[0])+1;
 146188   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
 146189   if( !p ){
 146190     return SQLITE_NOMEM;
 146192   memset(p, 0, sizeof(IcuTokenizer));
 146194   if( n ){
 146195     p->zLocale = (char *)&p[1];
 146196     memcpy(p->zLocale, argv[0], n);
 146199   *ppTokenizer = (sqlite3_tokenizer *)p;
 146201   return SQLITE_OK;
 146205 ** Destroy a tokenizer
 146207 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
 146208   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
 146209   sqlite3_free(p);
 146210   return SQLITE_OK;
 146214 ** Prepare to begin tokenizing a particular string.  The input
 146215 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
 146216 ** used to incrementally tokenize this string is returned in 
 146217 ** *ppCursor.
 146219 static int icuOpen(
 146220   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
 146221   const char *zInput,                    /* Input string */
 146222   int nInput,                            /* Length of zInput in bytes */
 146223   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
 146225   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
 146226   IcuCursor *pCsr;
 146228   const int32_t opt = U_FOLD_CASE_DEFAULT;
 146229   UErrorCode status = U_ZERO_ERROR;
 146230   int nChar;
 146232   UChar32 c;
 146233   int iInput = 0;
 146234   int iOut = 0;
 146236   *ppCursor = 0;
 146238   if( zInput==0 ){
 146239     nInput = 0;
 146240     zInput = "";
 146241   }else if( nInput<0 ){
 146242     nInput = strlen(zInput);
 146244   nChar = nInput+1;
 146245   pCsr = (IcuCursor *)sqlite3_malloc(
 146246       sizeof(IcuCursor) +                /* IcuCursor */
 146247       ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
 146248       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
 146250   if( !pCsr ){
 146251     return SQLITE_NOMEM;
 146253   memset(pCsr, 0, sizeof(IcuCursor));
 146254   pCsr->aChar = (UChar *)&pCsr[1];
 146255   pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
 146257   pCsr->aOffset[iOut] = iInput;
 146258   U8_NEXT(zInput, iInput, nInput, c); 
 146259   while( c>0 ){
 146260     int isError = 0;
 146261     c = u_foldCase(c, opt);
 146262     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
 146263     if( isError ){
 146264       sqlite3_free(pCsr);
 146265       return SQLITE_ERROR;
 146267     pCsr->aOffset[iOut] = iInput;
 146269     if( iInput<nInput ){
 146270       U8_NEXT(zInput, iInput, nInput, c);
 146271     }else{
 146272       c = 0;
 146276   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
 146277   if( !U_SUCCESS(status) ){
 146278     sqlite3_free(pCsr);
 146279     return SQLITE_ERROR;
 146281   pCsr->nChar = iOut;
 146283   ubrk_first(pCsr->pIter);
 146284   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
 146285   return SQLITE_OK;
 146289 ** Close a tokenization cursor previously opened by a call to icuOpen().
 146291 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
 146292   IcuCursor *pCsr = (IcuCursor *)pCursor;
 146293   ubrk_close(pCsr->pIter);
 146294   sqlite3_free(pCsr->zBuffer);
 146295   sqlite3_free(pCsr);
 146296   return SQLITE_OK;
 146300 ** Extract the next token from a tokenization cursor.
 146302 static int icuNext(
 146303   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
 146304   const char **ppToken,               /* OUT: *ppToken is the token text */
 146305   int *pnBytes,                       /* OUT: Number of bytes in token */
 146306   int *piStartOffset,                 /* OUT: Starting offset of token */
 146307   int *piEndOffset,                   /* OUT: Ending offset of token */
 146308   int *piPosition                     /* OUT: Position integer of token */
 146310   IcuCursor *pCsr = (IcuCursor *)pCursor;
 146312   int iStart = 0;
 146313   int iEnd = 0;
 146314   int nByte = 0;
 146316   while( iStart==iEnd ){
 146317     UChar32 c;
 146319     iStart = ubrk_current(pCsr->pIter);
 146320     iEnd = ubrk_next(pCsr->pIter);
 146321     if( iEnd==UBRK_DONE ){
 146322       return SQLITE_DONE;
 146325     while( iStart<iEnd ){
 146326       int iWhite = iStart;
 146327       U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
 146328       if( u_isspace(c) ){
 146329         iStart = iWhite;
 146330       }else{
 146331         break;
 146334     assert(iStart<=iEnd);
 146337   do {
 146338     UErrorCode status = U_ZERO_ERROR;
 146339     if( nByte ){
 146340       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
 146341       if( !zNew ){
 146342         return SQLITE_NOMEM;
 146344       pCsr->zBuffer = zNew;
 146345       pCsr->nBuffer = nByte;
 146348     u_strToUTF8(
 146349         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
 146350         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
 146351         &status                                  /* Output success/failure */
 146353   } while( nByte>pCsr->nBuffer );
 146355   *ppToken = pCsr->zBuffer;
 146356   *pnBytes = nByte;
 146357   *piStartOffset = pCsr->aOffset[iStart];
 146358   *piEndOffset = pCsr->aOffset[iEnd];
 146359   *piPosition = pCsr->iToken++;
 146361   return SQLITE_OK;
 146365 ** The set of routines that implement the simple tokenizer
 146367 static const sqlite3_tokenizer_module icuTokenizerModule = {
 146368   0,                           /* iVersion */
 146369   icuCreate,                   /* xCreate  */
 146370   icuDestroy,                  /* xCreate  */
 146371   icuOpen,                     /* xOpen    */
 146372   icuClose,                    /* xClose   */
 146373   icuNext,                     /* xNext    */
 146377 ** Set *ppModule to point at the implementation of the ICU tokenizer.
 146379 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
 146380   sqlite3_tokenizer_module const**ppModule
 146382   *ppModule = &icuTokenizerModule;
 146385 #endif /* defined(SQLITE_ENABLE_ICU) */
 146386 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
 146388 /************** End of fts3_icu.c ********************************************/

mercurial